diff --git a/etrago/execute/market_optimization.py b/etrago/execute/market_optimization.py index 2cd40331..6ee6a983 100644 --- a/etrago/execute/market_optimization.py +++ b/etrago/execute/market_optimization.py @@ -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 @@ -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. @@ -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( diff --git a/etrago/tools/constraints.py b/etrago/tools/constraints.py index 2c7e537a..131c5b4e 100755 --- a/etrago/tools/constraints.py +++ b/etrago/tools/constraints.py @@ -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) @@ -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) @@ -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 @@ -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]), ] @@ -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 @@ -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: