Skip to content

Commit

Permalink
refactor: add get_base_grid() to states
Browse files Browse the repository at this point in the history
  • Loading branch information
George Tong committed Aug 10, 2021
1 parent 0619697 commit 26f50e8
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 38 deletions.
4 changes: 1 addition & 3 deletions powersimdata/design/generation/clean_capacity_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import pandas as pd

from powersimdata.design.mimic_grid import mimic_generation_capacity
from powersimdata.input.grid import Grid
from powersimdata.network.model import area_to_loadzone
from powersimdata.scenario.scenario import Scenario

Expand Down Expand Up @@ -455,8 +454,7 @@ def create_change_table(input_targets, ref_scenario):
:return: (*dict*) -- dictionary to be passed to a change table.
"""
epsilon = 1e-3
interconnect = ref_scenario.info["interconnect"]
base_grid = Grid([interconnect])
base_grid = ref_scenario.state.get_base_grid()
grid_zones = base_grid.plant.zone_name.unique()
ref_grid = ref_scenario.state.get_grid()
ct = mimic_generation_capacity(base_grid, ref_grid)
Expand Down
47 changes: 17 additions & 30 deletions powersimdata/design/investment/investment_costs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import copy as cp
import warnings

import numpy as np
Expand All @@ -16,7 +15,6 @@
)
from powersimdata.design.investment.inflation import calculate_inflation
from powersimdata.input.check import _check_grid_models_match
from powersimdata.input.grid import Grid
from powersimdata.utility.distance import haversine


Expand Down Expand Up @@ -67,22 +65,19 @@ def calculate_ac_inv_costs(
"""
grid = scenario.state.get_grid()
if base_grid is None:
base_grid = Grid(
scenario.info["interconnect"].split("_"), source=scenario.info["grid_model"]
)
base_grid = scenario.state.get_base_grid()
else:
_check_grid_models_match(base_grid, grid)

grid_new = cp.deepcopy(grid)
# find upgraded AC lines
capacity_difference = calculate_branch_difference(base_grid.branch, grid.branch)
grid_new.branch = grid.branch.assign(rateA=capacity_difference.diff)
grid_new.branch = grid_new.branch.query("rateA != 0.0")
grid.branch = grid.branch.assign(rateA=capacity_difference.diff)
grid.branch = grid.branch.query("rateA != 0.0")
if exclude_branches is not None:
present_exclude_branches = set(exclude_branches) & set(grid_new.branch.index)
grid_new.branch.drop(index=present_exclude_branches, inplace=True)
present_exclude_branches = set(exclude_branches) & set(grid.branch.index)
grid.branch.drop(index=present_exclude_branches, inplace=True)

costs = _calculate_ac_inv_costs(grid_new, sum_results)
costs = _calculate_ac_inv_costs(grid, sum_results)
return costs


Expand Down Expand Up @@ -287,19 +282,16 @@ def calculate_dc_inv_costs(scenario, sum_results=True, base_grid=None):
"""
grid = scenario.state.get_grid()
if base_grid is None:
base_grid = Grid(
scenario.info["interconnect"].split("_"), source=scenario.info["grid_model"]
)
base_grid = scenario.state.get_base_grid()
else:
_check_grid_models_match(base_grid, grid)

grid_new = cp.deepcopy(grid)
# find upgraded DC lines
capacity_difference = calculate_dcline_difference(base_grid.dcline, grid.dcline)
grid_new.dcline = grid.dcline.assign(Pmax=capacity_difference.diff)
grid_new.dcline = grid_new.dcline.query("Pmax != 0.0")
grid.dcline = grid.dcline.assign(Pmax=capacity_difference.diff)
grid.dcline = grid.dcline.query("Pmax != 0.0")

costs = _calculate_dc_inv_costs(grid_new, sum_results)
costs = _calculate_dc_inv_costs(grid, sum_results)
return costs


Expand Down Expand Up @@ -380,28 +372,23 @@ def calculate_gen_inv_costs(
"""
grid = scenario.state.get_grid()
if base_grid is None:
base_grid = Grid(
scenario.info["interconnect"].split("_"), source=scenario.info["grid_model"]
)
base_grid = scenario.state.get_base_grid()
else:
_check_grid_models_match(base_grid, grid)

grid_new = cp.deepcopy(grid)
# Find change in generation capacity
capacity_difference = calculate_plant_difference(base_grid.plant, grid.plant)
grid_new.plant = grid.plant.assign(Pmax=capacity_difference.diff)
grid_new.plant = grid_new.plant.query("Pmax >= 0.01")
grid.plant = grid.plant.assign(Pmax=capacity_difference.diff)
grid.plant = grid.plant.query("Pmax >= 0.01")
# Find change in storage capacity
# Reindex so that we don't get NaN when calculating upgrades for new storage
base_grid.storage["gen"] = base_grid.storage["gen"].reindex(
grid_new.storage["gen"].index, fill_value=0
)
grid_new.storage["gen"].Pmax = (
grid.storage["gen"].Pmax - base_grid.storage["gen"].Pmax
grid.storage["gen"].index, fill_value=0
)
grid_new.storage["gen"]["type"] = "storage"
grid.storage["gen"].Pmax = grid.storage["gen"].Pmax - base_grid.storage["gen"].Pmax
grid.storage["gen"]["type"] = "storage"

costs = _calculate_gen_inv_costs(grid_new, year, cost_case, sum_results)
costs = _calculate_gen_inv_costs(grid, year, cost_case, sum_results)
return costs


Expand Down
3 changes: 1 addition & 2 deletions powersimdata/design/transmission/mwmiles.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from powersimdata.input.grid import Grid
from powersimdata.input.transform_grid import TransformGrid
from powersimdata.utility.distance import haversine

Expand All @@ -15,7 +14,7 @@ def calculate_mw_miles(scenario, exclude_branches=None):
:return: (*dict*) -- Upgrades to the branches.
"""

original_grid = Grid(scenario.info["interconnect"].split("_"))
original_grid = scenario.state.get_base_grid()
ct = scenario.state.get_ct()
upgrades = _calculate_mw_miles(original_grid, ct, exclude_branches)
return upgrades
Expand Down
4 changes: 1 addition & 3 deletions powersimdata/design/transmission/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,9 +334,7 @@ def _identify_mesh_branch_upgrades(
# Calculate selected cost metric for congested branches
if cost_metric == "cost":
# Calculate costs for an upgrade dataframe containing only composite_allow_list
base_grid = Grid(
ref_scenario.info["interconnect"], ref_scenario.info["grid_model"]
)
base_grid = ref_scenario.state.get_base_grid()
base_grid.branch = base_grid.branch.filter(items=congested_indices, axis=0)
upgrade_costs = _calculate_ac_inv_costs(base_grid, sum_results=False)
# Merge the individual line/transformer data into a single Series
Expand Down
8 changes: 8 additions & 0 deletions powersimdata/scenario/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ class _Builder:
"set_time",
"get_ct",
"get_grid",
"get_base_grid",
"get_demand",
"get_hydro",
"get_solar",
Expand Down Expand Up @@ -373,5 +374,12 @@ def get_grid(self):
"""
return TransformGrid(self.base_grid, self.change_table.ct).get_grid()

def get_base_grid(self):
"""Returns original grid.
:return: (*powersimdata.input.grid.Grid*) -- a Grid object.
"""
return copy.deepcopy(self.base_grid)

def __str__(self):
return self.name
12 changes: 12 additions & 0 deletions powersimdata/scenario/ready.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import copy

from powersimdata.input.grid import Grid
from powersimdata.input.input_data import get_bus_demand
from powersimdata.input.transform_profile import TransformProfile
from powersimdata.scenario.state import State
Expand All @@ -9,6 +10,7 @@ class Ready(State):
exported_methods = {
"get_ct",
"get_grid",
"get_base_grid",
"get_bus_demand",
"get_demand",
"get_hydro",
Expand Down Expand Up @@ -36,6 +38,16 @@ def get_grid(self):
"""
return copy.deepcopy(self.grid)

def get_base_grid(self):
"""Returns original grid.
:return: (*powersimdata.input.grid.Grid*) -- a Grid object.
"""
return Grid(
self._scenario_info["interconnect"].split("_"),
source=self._scenario_info["grid_model"],
)

def get_profile(self, kind):
"""Returns demand, hydro, solar or wind profile.
Expand Down

0 comments on commit 26f50e8

Please sign in to comment.