Skip to content

Commit e764a13

Browse files
committed
New test for previous flow_rates
1 parent 159bcb3 commit e764a13

2 files changed

Lines changed: 29 additions & 7 deletions

File tree

flixopt/elements.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ def __init__(
199199
If the load-profile is just an upper limit, use relative_maximum instead.
200200
previous_flow_rate: previous flow rate of the flow. Used to determine if and how long the
201201
flow is already on / off. If None, the flow is considered to be off for one timestep.
202+
Currently does not support different values in different years or scenarios!
202203
meta_data: used to store more information about the Element. Is not used internally, but saved in the results. Only use python native types.
203204
"""
204205
super().__init__(label, meta_data=meta_data)
@@ -305,7 +306,8 @@ def _plausibility_checks(self) -> None:
305306
]
306307
):
307308
raise TypeError(
308-
f'previous_flow_rate must be None, a scalar, a list of scalars or a 1D-numpy-array. Got {type(self.previous_flow_rate)}'
309+
f'previous_flow_rate must be None, a scalar, a list of scalars or a 1D-numpy-array. Got {type(self.previous_flow_rate)}.'
310+
f'Different values in different years or scenarios are not yetsupported.'
309311
)
310312

311313
@property

tests/test_component.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,26 @@ def test_previous_states_with_multiple_flows(self, basic_flow_system_linopy_coor
349349
+ 1e-5,
350350
)
351351

352-
def test_previous_states_with_multiple_flows_2(self, basic_flow_system_linopy_coords, coords_config):
353-
"""Test that flow model constraints are correctly generated."""
352+
@pytest.mark.parametrize(
353+
'in1_previous_flow_rate, out1_previous_flow_rate, out2_previous_flow_rate, previous_on_hours',
354+
[
355+
(None, None, None, 0),
356+
(np.array([0, 1e-6, 1e-4, 5]), None, None, 2),
357+
(np.array([0, 5, 0, 5]), None, None, 1),
358+
(np.array([0, 5, 0, 0]), 3, 0, 1),
359+
(np.array([0, 0, 2, 0, 4, 5]), [3, 4, 5], None, 4),
360+
],
361+
)
362+
def test_previous_states_with_multiple_flows_parameterized(
363+
self,
364+
basic_flow_system_linopy_coords,
365+
coords_config,
366+
in1_previous_flow_rate,
367+
out1_previous_flow_rate,
368+
out2_previous_flow_rate,
369+
previous_on_hours,
370+
):
371+
"""Test that flow model constraints are correctly generated with different previous flow rates and constraint factors."""
354372
flow_system, coords_config = basic_flow_system_linopy_coords, coords_config
355373

356374
ub_out2 = np.linspace(1, 1.5, 10).round(2)
@@ -360,19 +378,21 @@ def test_previous_states_with_multiple_flows_2(self, basic_flow_system_linopy_co
360378
'Fernwärme',
361379
relative_minimum=np.ones(10) * 0.1,
362380
size=100,
363-
previous_flow_rate=np.array([0, 0, 1e-6, 1e-5, 1e-4, 3, 4]),
381+
previous_flow_rate=in1_previous_flow_rate,
364382
on_off_parameters=fx.OnOffParameters(consecutive_on_hours_min=3),
365383
),
366384
]
367385
outputs = [
368-
fx.Flow('Out1', 'Gas', relative_minimum=np.ones(10) * 0.2, size=200, previous_flow_rate=[3, 4, 5]),
386+
fx.Flow(
387+
'Out1', 'Gas', relative_minimum=np.ones(10) * 0.2, size=200, previous_flow_rate=out1_previous_flow_rate
388+
),
369389
fx.Flow(
370390
'Out2',
371391
'Gas',
372392
relative_minimum=np.ones(10) * 0.3,
373393
relative_maximum=ub_out2,
374394
size=300,
375-
previous_flow_rate=20,
395+
previous_flow_rate=out2_previous_flow_rate,
376396
),
377397
]
378398
comp = flixopt.elements.Component(
@@ -387,7 +407,7 @@ def test_previous_states_with_multiple_flows_2(self, basic_flow_system_linopy_co
387407
assert_conequal(
388408
comp.submodel.constraints['TestComponent|consecutive_on_hours|initial'],
389409
comp.submodel.variables['TestComponent|consecutive_on_hours'].isel(time=0)
390-
== comp.submodel.variables['TestComponent|on'].isel(time=0) * 5,
410+
== comp.submodel.variables['TestComponent|on'].isel(time=0) * (previous_on_hours + 1),
391411
)
392412

393413

0 commit comments

Comments
 (0)