From b001fe6fb96963899fa8bbaf8903559b735fecfe Mon Sep 17 00:00:00 2001 From: Johannes Kochems Date: Tue, 31 Oct 2023 17:47:16 +0100 Subject: [PATCH] Introduce draft for fixing investments (yet untested) --- src/oemof/solph/_models.py | 3 ++- .../solph/flows/_investment_flow_block.py | 22 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/oemof/solph/_models.py b/src/oemof/solph/_models.py index b9ba79e8b..129a46c6e 100644 --- a/src/oemof/solph/_models.py +++ b/src/oemof/solph/_models.py @@ -368,7 +368,7 @@ class Model(BaseModel): InvestNonConvexFlowBlock, ] - def __init__(self, energysystem, discount_rate=None, **kwargs): + def __init__(self, energysystem, discount_rate=None, fix_investments=False, **kwargs): if discount_rate is not None: self.discount_rate = discount_rate elif ( @@ -383,6 +383,7 @@ def __init__(self, energysystem, discount_rate=None, **kwargs): self._set_discount_rate_with_warning() else: pass + self.fix_investments = fix_investments super().__init__(energysystem, **kwargs) def _set_discount_rate_with_warning(self): diff --git a/src/oemof/solph/flows/_investment_flow_block.py b/src/oemof/solph/flows/_investment_flow_block.py index a8d795ce7..fa9246aba 100644 --- a/src/oemof/solph/flows/_investment_flow_block.py +++ b/src/oemof/solph/flows/_investment_flow_block.py @@ -87,12 +87,15 @@ def _create(self, group=None): attribute investment and the associated source (s) and target (t) of flow e.g. groups=[(s1, t1, f1), (s2, t2, f2),..] """ + m = self.parent_block() if group is None: return None self._create_sets(group) self._create_variables(group) self._create_constraints() + if m.fix_investments: + self._fix_investments() def _create_sets(self, group): """ @@ -1175,3 +1178,22 @@ def _max_invest_rule(_): self.maximum_rule_build = BuildAction(rule=_max_invest_rule) return self.maximum_rule + + def _fix_investments(self): + """Fix investments in case boolean flag `fix_investments` is True""" + m = self.parent_block() + if m.solver_results is not None: + for p in m.PERIODS: + self.invest[p].fix() + self.total[p].fix() + if m.es.periods is not None: + self.old[p].fix() + self.old_end[p].fix() + self.old_exo[p].fix() + else: + msg = ( + "Cannot fix investments as model has not yet been solved!\n" + "You have to first solve your model and then set " + "`fix_investments=True` for your model instance." + ) + raise ValueError(msg)