Skip to content

Commit

Permalink
Implement workaround for oemof.solph 0.5.1 compatibility
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jnettels committed Oct 24, 2023
1 parent a66d25c commit 633e9c8
Showing 1 changed file with 51 additions and 16 deletions.
67 changes: 51 additions & 16 deletions dhnx/optimization/oemof_heatpipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 633e9c8

Please sign in to comment.