Skip to content

Commit

Permalink
Merge 3dfeb76 into fb1c5f7
Browse files Browse the repository at this point in the history
  • Loading branch information
jokochems committed Nov 1, 2023
2 parents fb1c5f7 + 3dfeb76 commit 3594a6e
Show file tree
Hide file tree
Showing 13 changed files with 4,459 additions and 404 deletions.
14 changes: 14 additions & 0 deletions src/oemof/solph/_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,7 @@ def __init__(self, energysystem, discount_rate=None, **kwargs):
self._set_discount_rate_with_warning()
else:
pass
self._fix_investments = False
super().__init__(energysystem, **kwargs)

def _set_discount_rate_with_warning(self):
Expand Down Expand Up @@ -521,3 +522,16 @@ def _add_parent_block_variables(self):
if (o, i) in self.UNIDIRECTIONAL_FLOWS:
for p, t in self.TIMEINDEX:
self.flow[o, i, p, t].setlb(0)

def fix_investments(self):
"""Fix investment results of an already solved model"""
if self.solver_results is None:
msg = (
"Cannot fix investments as model has not yet been solved!\n"
"You have to first solve your model and then call method "
"`fix_investments()` on your model instance."
)
raise ValueError(msg)
self._fix_investments = True
if hasattr(self, "InvestmentFlowBlock"):
self.InvestmentFlowBlock.fix_investments_results()
12 changes: 12 additions & 0 deletions src/oemof/solph/flows/_investment_flow_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -1175,3 +1175,15 @@ def _max_invest_rule(_):
self.maximum_rule_build = BuildAction(rule=_max_invest_rule)

return self.maximum_rule

def fix_investments_results(self):
"""Fix investments if `_fix_investments` is set to True for model"""
m = self.parent_block()
for i, o in self.INVESTFLOWS:
for p in m.PERIODS:
self.invest[i, o, p].fix()
self.total[i, o, p].fix()
if m.es.periods is not None:
self.old[i, o, p].fix()
self.old_end[i, o, p].fix()
self.old_exo[i, o, p].fix()
34 changes: 34 additions & 0 deletions tests/constraint_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1995,3 +1995,37 @@ def test_storage_level_constraint(self):
output_levels={out_0: 1 / 8, out_1: 1 / 2},
)
self.compare_lp_files("storage_level_constraint.lp", my_om=om)

def test_investment_fixed(self):
"""test a repeated solve with fixed investments"""
bgas = solph.buses.Bus(label="gas")
bel = solph.buses.Bus(label="electricity")

source = solph.components.Source(
label="gas_source",
outputs={bgas: solph.flows.Flow()},
)
converter = solph.components.Converter(
label="powerplant_gas",
inputs={bgas: solph.flows.Flow()},
outputs={
bel: solph.flows.Flow(
variable_costs=50,
nominal_value=solph.Investment(maximum=1000, ep_costs=20),
)
},
conversion_factors={bel: 0.58},
)
sink = solph.components.Sink(
label="electricity_consumption",
inputs={
bel: solph.flows.Flow(nominal_value=100, fix=[0.8, 0.9, 1.0])
},
)
self.energysystem.add(bgas, bel, source, converter, sink)
om = solph.Model(self.energysystem)
self.compare_lp_files("investment_before_fixing.lp", my_om=om)
om.solve()
om.fix_investments()
om.solve()
self.compare_lp_files("investment_after_fixing.lp", my_om=om)
81 changes: 81 additions & 0 deletions tests/lp_files/investment_after_fixing.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
\* Source Pyomo model name=Model *\

min
objective:
+2000.0 ONE_VAR_CONSTANT
+50 flow(powerplant_gas_electricity_0_0)
+50 flow(powerplant_gas_electricity_0_1)
+50 flow(powerplant_gas_electricity_0_2)

s.t.

c_e_BusBlock_balance(gas_0_0)_:
+1 flow(gas_source_gas_0_0)
-1 flow(gas_powerplant_gas_0_0)
= 0

c_e_BusBlock_balance(gas_0_1)_:
+1 flow(gas_source_gas_0_1)
-1 flow(gas_powerplant_gas_0_1)
= 0

c_e_BusBlock_balance(gas_0_2)_:
+1 flow(gas_source_gas_0_2)
-1 flow(gas_powerplant_gas_0_2)
= 0

c_e_BusBlock_balance(electricity_0_0)_:
+1 flow(powerplant_gas_electricity_0_0)
= 80.0

c_e_BusBlock_balance(electricity_0_1)_:
+1 flow(powerplant_gas_electricity_0_1)
= 90.0

c_e_BusBlock_balance(electricity_0_2)_:
+1 flow(powerplant_gas_electricity_0_2)
= 100.0

c_e_ConverterBlock_relation(powerplant_gas_gas_electricity_0_0)_:
-1 flow(powerplant_gas_electricity_0_0)
+0.58 flow(gas_powerplant_gas_0_0)
= 0

c_e_ConverterBlock_relation(powerplant_gas_gas_electricity_0_1)_:
-1 flow(powerplant_gas_electricity_0_1)
+0.58 flow(gas_powerplant_gas_0_1)
= 0

c_e_ConverterBlock_relation(powerplant_gas_gas_electricity_0_2)_:
-1 flow(powerplant_gas_electricity_0_2)
+0.58 flow(gas_powerplant_gas_0_2)
= 0

c_e_InvestmentFlowBlock_total_rule(powerplant_gas_electricity_0)_:
+0 ONE_VAR_CONSTANT
= 0.0

c_u_InvestmentFlowBlock_max(powerplant_gas_electricity_0_0)_:
+1 flow(powerplant_gas_electricity_0_0)
<= 100.0

c_u_InvestmentFlowBlock_max(powerplant_gas_electricity_0_1)_:
+1 flow(powerplant_gas_electricity_0_1)
<= 100.0

c_u_InvestmentFlowBlock_max(powerplant_gas_electricity_0_2)_:
+1 flow(powerplant_gas_electricity_0_2)
<= 100.0

bounds
1 <= ONE_VAR_CONSTANT <= 1
0 <= flow(powerplant_gas_electricity_0_0) <= +inf
0 <= flow(powerplant_gas_electricity_0_1) <= +inf
0 <= flow(powerplant_gas_electricity_0_2) <= +inf
0 <= flow(gas_source_gas_0_0) <= +inf
0 <= flow(gas_powerplant_gas_0_0) <= +inf
0 <= flow(gas_source_gas_0_1) <= +inf
0 <= flow(gas_powerplant_gas_0_1) <= +inf
0 <= flow(gas_source_gas_0_2) <= +inf
0 <= flow(gas_powerplant_gas_0_2) <= +inf
end
86 changes: 86 additions & 0 deletions tests/lp_files/investment_before_fixing.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
\* Source Pyomo model name=Model *\

min
objective:
+20 InvestmentFlowBlock_invest(powerplant_gas_electricity_0)
+50 flow(powerplant_gas_electricity_0_0)
+50 flow(powerplant_gas_electricity_0_1)
+50 flow(powerplant_gas_electricity_0_2)

s.t.

c_e_BusBlock_balance(electricity_0_0)_:
+1 flow(powerplant_gas_electricity_0_0)
= 80.0

c_e_BusBlock_balance(electricity_0_1)_:
+1 flow(powerplant_gas_electricity_0_1)
= 90.0

c_e_BusBlock_balance(electricity_0_2)_:
+1 flow(powerplant_gas_electricity_0_2)
= 100.0

c_e_BusBlock_balance(gas_0_0)_:
+1 flow(gas_source_gas_0_0)
-1 flow(gas_powerplant_gas_0_0)
= 0

c_e_BusBlock_balance(gas_0_1)_:
+1 flow(gas_source_gas_0_1)
-1 flow(gas_powerplant_gas_0_1)
= 0

c_e_BusBlock_balance(gas_0_2)_:
+1 flow(gas_source_gas_0_2)
-1 flow(gas_powerplant_gas_0_2)
= 0

c_e_ConverterBlock_relation(powerplant_gas_gas_electricity_0_0)_:
-1 flow(powerplant_gas_electricity_0_0)
+0.58 flow(gas_powerplant_gas_0_0)
= 0

c_e_ConverterBlock_relation(powerplant_gas_gas_electricity_0_1)_:
-1 flow(powerplant_gas_electricity_0_1)
+0.58 flow(gas_powerplant_gas_0_1)
= 0

c_e_ConverterBlock_relation(powerplant_gas_gas_electricity_0_2)_:
-1 flow(powerplant_gas_electricity_0_2)
+0.58 flow(gas_powerplant_gas_0_2)
= 0

c_e_InvestmentFlowBlock_total_rule(powerplant_gas_electricity_0)_:
-1 InvestmentFlowBlock_invest(powerplant_gas_electricity_0)
+1 InvestmentFlowBlock_total(powerplant_gas_electricity_0)
= 0

c_u_InvestmentFlowBlock_max(powerplant_gas_electricity_0_0)_:
+1 flow(powerplant_gas_electricity_0_0)
-1 InvestmentFlowBlock_total(powerplant_gas_electricity_0)
<= 0

c_u_InvestmentFlowBlock_max(powerplant_gas_electricity_0_1)_:
+1 flow(powerplant_gas_electricity_0_1)
-1 InvestmentFlowBlock_total(powerplant_gas_electricity_0)
<= 0

c_u_InvestmentFlowBlock_max(powerplant_gas_electricity_0_2)_:
+1 flow(powerplant_gas_electricity_0_2)
-1 InvestmentFlowBlock_total(powerplant_gas_electricity_0)
<= 0

bounds
0 <= InvestmentFlowBlock_invest(powerplant_gas_electricity_0) <= 1000
0 <= flow(powerplant_gas_electricity_0_0) <= +inf
0 <= flow(powerplant_gas_electricity_0_1) <= +inf
0 <= flow(powerplant_gas_electricity_0_2) <= +inf
0 <= flow(gas_source_gas_0_0) <= +inf
0 <= flow(gas_powerplant_gas_0_0) <= +inf
0 <= flow(gas_source_gas_0_1) <= +inf
0 <= flow(gas_powerplant_gas_0_1) <= +inf
0 <= flow(gas_source_gas_0_2) <= +inf
0 <= flow(gas_powerplant_gas_0_2) <= +inf
0 <= InvestmentFlowBlock_total(powerplant_gas_electricity_0) <= +inf
end

0 comments on commit 3594a6e

Please sign in to comment.