From 633e9c8d4549af576fe62cefc2bf2941499132bd Mon Sep 17 00:00:00 2001 From: Joris Nettelstroth Date: Tue, 24 Oct 2023 16:05:39 +0200 Subject: [PATCH] Implement workaround for oemof.solph 0.5.1 compatibility Add a period=0 to lines where the time index is called, which has been changed to a tuple of (period, time step) in oemof.solph 0.5.1 --- dhnx/optimization/oemof_heatpipe.py | 67 ++++++++++++++++++++++------- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/dhnx/optimization/oemof_heatpipe.py b/dhnx/optimization/oemof_heatpipe.py index f9560a9e..dea9547d 100644 --- a/dhnx/optimization/oemof_heatpipe.py +++ b/dhnx/optimization/oemof_heatpipe.py @@ -359,9 +359,15 @@ def _heat_loss_rule_convex(block, n, t): """ expr = 0 expr += - block.heat_loss[n, t] - expr += n.heat_loss_factor[t] * m.InvestmentFlowBlock.invest[ - n, list(n.outputs.keys())[0] - ] + try: # oemof.solph<=0.5.0 + expr += n.heat_loss_factor[t] * m.InvestmentFlowBlock.invest[ + n, list(n.outputs.keys())[0], + ] + except KeyError: # oemof.solph>=0.5.1 + period = 0 # Periods are not (yet) supported in DHNx + expr += n.heat_loss_factor[t] * m.InvestmentFlowBlock.invest[ + n, list(n.outputs.keys())[0], period + ] expr += n.heat_loss_factor_fix[t] return expr == 0 self.heat_loss_equation_convex = Constraint( @@ -375,11 +381,19 @@ def _heat_loss_rule_nonconvex(block, n, t): """ expr = 0 expr += - block.heat_loss[n, t] - expr += n.heat_loss_factor[t] * m.InvestmentFlowBlock.invest[ - n, list(n.outputs.keys())[0]] - expr += n.heat_loss_factor_fix[t] * \ - m.InvestmentFlowBlock.invest_status[ + try: # oemof.solph<=0.5.0 + expr += n.heat_loss_factor[t] * m.InvestmentFlowBlock.invest[ n, list(n.outputs.keys())[0]] + expr += n.heat_loss_factor_fix[t] * \ + m.InvestmentFlowBlock.invest_status[ + n, list(n.outputs.keys())[0]] + except KeyError: # oemof.solph>=0.5.1 + period = 0 # Periods are not (yet) supported in DHNx + expr += n.heat_loss_factor[t] * m.InvestmentFlowBlock.invest[ + n, list(n.outputs.keys())[0], period] + expr += n.heat_loss_factor_fix[t] * \ + m.InvestmentFlowBlock.invest_status[ + n, list(n.outputs.keys())[0], period] return expr == 0 self.heat_loss_equation_nonconvex = Constraint( @@ -393,9 +407,16 @@ def _relation_rule_no_demand(block, n, t): o = list(n.outputs.keys())[0] expr = 0 - expr += - m.flow[n, o, t] - expr += m.flow[i, n, t] + try: # oemof.solph<=0.5.0 + expr += - m.flow[n, o, t] + expr += m.flow[i, n, t] + except KeyError: # oemof.solph>=0.5.1 + period = 0 # Periods are not (yet) supported in DHNx + expr += - m.flow[n, o, period, t] + expr += m.flow[i, n, period, t] + expr += - block.heat_loss[n, t] + return expr == 0 self.relation_no_demand = Constraint( @@ -410,11 +431,20 @@ def _relation_rule_with_demand(block, n, t): d = list(n.outputs.keys())[1] expr = 0 - expr += - m.flow[n, o, t] - expr += m.flow[i, n, t] * n.conversion_factors[ - o][t] / n.conversion_factors[i][t] - expr += - block.heat_loss[n, t] - expr += - m.flow[n, d, t] + try: # oemof.solph<=0.5.0 + expr += - m.flow[n, o, t] + expr += m.flow[i, n, t] * n.conversion_factors[ + o][t] / n.conversion_factors[i][t] + expr += - block.heat_loss[n, t] + expr += - m.flow[n, d, t] + except KeyError: # oemof.solph>=0.5.1 + period = 0 # Periods are not (yet) supported in DHNx + expr += - m.flow[n, o, period, t] + expr += m.flow[i, n, period, t] * n.conversion_factors[ + o][t] / n.conversion_factors[i][t] + expr += - block.heat_loss[n, t] + expr += - m.flow[n, d, period, t] + return expr == 0 self.relation_with_demand = Constraint( self.INVESTHEATPIPES_WITH_DEMAND, m.TIMESTEPS, @@ -429,8 +459,13 @@ def _inflow_outflow_invest_coupling_rule(block, n): # pylint: disable=unused-ar i = list(n.inputs.keys())[0] o = list(n.outputs.keys())[0] - expr = (m.InvestmentFlowBlock.invest[i, n] - == m.InvestmentFlowBlock.invest[n, o]) + try: # oemof.solph<=0.5.0 + expr = (m.InvestmentFlowBlock.invest[i, n] + == m.InvestmentFlowBlock.invest[n, o]) + except KeyError: # oemof.solph>=0.5.1 + period = 0 # Periods are not (yet) supported in DHNx + expr = (m.InvestmentFlowBlock.invest[i, n, period] + == m.InvestmentFlowBlock.invest[n, o, period]) return expr self.inflow_outflow_invest_coupling = Constraint( self.INVESTHEATPIPES, rule=_inflow_outflow_invest_coupling_rule