Skip to content

Commit

Permalink
Fix constraints for setting cyclic storage units is rolling horizon
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaraBuettner committed Apr 18, 2024
1 parent 60281e0 commit b0f7292
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 40 deletions.
10 changes: 6 additions & 4 deletions etrago/execute/market_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def market_optimization(self):
],
solver_name=self.args["solver"],
extra_functionality=Constraints(self.args, False, apply_on="market_model").functionality,
args = self.args,
)

# Reset formulation to previous setting of args
Expand All @@ -122,7 +123,7 @@ def market_optimization(self):

def optimize_with_rolling_horizon(
n, pre_market, snapshots, horizon, overlap, solver_name,
extra_functionality, **kwargs
extra_functionality, args
):
"""
Optimizes the network in a rolling horizon fashion.
Expand Down Expand Up @@ -213,10 +214,11 @@ def optimize_with_rolling_horizon(

elif i == len(starting_points)-1:
extra_functionality=Constraints(
{"extra_functionality": []},
False, apply_on="last_market_model").functionality,
args,
False, apply_on="last_market_model").functionality

status, condition = n.optimize(sns, **kwargs)
status, condition = n.optimize(sns, solver_name=solver_name,
extra_functionality=extra_functionality)

if status != "ok":
logger.warning(
Expand Down
78 changes: 42 additions & 36 deletions etrago/tools/constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,26 @@ def _max_battery_expansion_germany(self, network, snapshots):
"""
home_battery_capacity = network.storage_units[
network.storage_units.carrier=="battery"].p_nom_min.sum()
network.storage_units.carrier == "battery"
].p_nom_min.sum()

batteries = network.storage_units[(
network.storage_units.carrier=="battery")
&(network.storage_units.bus.isin(
network.buses[network.buses.country=="DE"].index))]
batteries = network.storage_units[
(network.storage_units.carrier == "battery")
& (
network.storage_units.bus.isin(
network.buses[network.buses.country == "DE"].index
)
)
]

def _rule_max(m):
batteries_opt = sum(
m.storage_p_nom[index]
for index in batteries.index
m.storage_p_nom[index] for index in batteries.index
)
return batteries_opt <= (1) * (
self.args["extra_functionality"]["max_battery_expansion_germany"]
+ home_battery_capacity
)
return batteries_opt <= (
1
) * (self.args["extra_functionality"]["max_battery_expansion_germany"]
+ home_battery_capacity)

network.model.max_battery_ext = Constraint(rule=_rule_max)

Expand All @@ -245,33 +249,35 @@ def _fixed_battery_expansion_germany(self, network, snapshots):
"""
home_battery_capacity = network.storage_units[
network.storage_units.carrier=="battery"].p_nom_min.sum()
network.storage_units.carrier == "battery"
].p_nom_min.sum()

batteries = network.storage_units[(
network.storage_units.carrier=="battery")
&(network.storage_units.bus.isin(
network.buses[network.buses.country=="DE"].index))]
batteries = network.storage_units[
(network.storage_units.carrier == "battery")
& (
network.storage_units.bus.isin(
network.buses[network.buses.country == "DE"].index
)
)
]

def _rule_min(m):
batteries_opt = sum(
m.storage_p_nom[index]
for index in batteries.index
m.storage_p_nom[index] for index in batteries.index
)
return (batteries_opt) >= (
0.999
) * (
return (batteries_opt) >= (0.999) * (
self.args["extra_functionality"]["fixed_battery_expansion_germany"]
+ home_battery_capacity)
+ home_battery_capacity
)

def _rule_max(m):
batteries_opt = sum(
m.storage_p_nom[index]
for index in batteries.index
m.storage_p_nom[index] for index in batteries.index
)
return batteries_opt <= (1.001) * (
self.args["extra_functionality"]["fixed_battery_expansion_germany"]
+ home_battery_capacity
)
return batteries_opt <= (
1.001
) * (self.args["extra_functionality"]["fixed_battery_expansion_germany"]
+ home_battery_capacity)

network.model.min_battery_ext = Constraint(rule=_rule_min)
network.model.max_battery_ext = Constraint(rule=_rule_max)
Expand Down Expand Up @@ -3181,7 +3187,6 @@ def split_dispatch_disaggregation_constraints_nmp(self, n, sns):
# TODO: implementieren



def fixed_storage_unit_soc_at_the_end(n, sns):
"""
Defines energy balance constraints for storage units. In principal the
Expand All @@ -3206,13 +3211,13 @@ def fixed_storage_unit_soc_at_the_end(n, sns):
eff_store = n.storage_units.efficiency_store

# SOC first hour of the year
post_soc = m[f"{c}-state_of_charge"].loc[n.snapshots[0]]
post_soc = n.storage_units_t.state_of_charge.loc[n.snapshots[0]]

# SOC last hour of the year
soc = m[f"{c}-state_of_charge"].loc[sns]

lhs = [
(-1, soc),
(1, soc),
(-1 / eff_dispatch * eh, m[f"{c}-p_dispatch"].loc[sns]),
(eff_store * eh, m[f"{c}-p_store"].loc[sns]),
]
Expand All @@ -3221,14 +3226,15 @@ def fixed_storage_unit_soc_at_the_end(n, sns):
lhs += [(-eh, m[f"{c}-spill"])]

# We add inflow and initial soc for noncyclic assets to rhs
rhs = DataArray((-n.storage_units.inflow).mul(eh))

lhs += [(eff_stand, post_soc)]
rhs = DataArray((-n.storage_units.inflow).mul(eh) + (eff_stand * post_soc))

m.add_constraints(lhs, "=", rhs, name=f"{c}-energy_balance_end")


class Constraints:
def __init__(self, args, conduct_dispatch_disaggregation, apply_on="grid_model"):
def __init__(
self, args, conduct_dispatch_disaggregation, apply_on="grid_model"
):
self.args = args
self.conduct_dispatch_disaggregation = conduct_dispatch_disaggregation
self.apply_on = apply_on
Expand Down Expand Up @@ -3259,7 +3265,7 @@ def functionality(self, network, snapshots):
):
add_ch4_constraints_linopy(self, network, snapshots)

if self.apply_on=="last_market_model":
if self.apply_on == "last_market_model":
fixed_storage_unit_soc_at_the_end(network, snapshots)
add_chp_constraints_linopy(network, snapshots)
else:
Expand Down

0 comments on commit b0f7292

Please sign in to comment.