From 1705996a77f53c03379e58ad816366e098b88901 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patrik=20Sch=C3=B6nfeldt?= Date: Fri, 2 Aug 2024 15:53:34 +0200 Subject: [PATCH] Add unittests for lower integral limit --- tests/constraint_tests.py | 23 ++++++++++++++++ tests/lp_files/emission_limit_lower.lp | 38 ++++++++++++++++++++++++++ tests/test_constraints_module.py | 11 +++++--- 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 tests/lp_files/emission_limit_lower.lp diff --git a/tests/constraint_tests.py b/tests/constraint_tests.py index 07b0c3f0f..93bd5784e 100644 --- a/tests/constraint_tests.py +++ b/tests/constraint_tests.py @@ -904,6 +904,29 @@ def test_flow_without_emission_for_emission_constraint_no_error(self): self.compare_lp_files("emission_limit_no_error.lp", my_om=om) + def test_flow_without_emission_for_emission_constraint_lower(self): + """Test that no error is thrown if no flows are explicitly passed""" + bel = solph.buses.Bus(label="electricityBus") + source1 = solph.components.Source( + label="source1", + outputs={ + bel: solph.flows.Flow( + nominal_value=100, + custom_attributes={"emission_factor": 0.8}, + ) + }, + ) + source2 = solph.components.Source( + label="source2", outputs={bel: solph.flows.Flow(nominal_value=100)} + ) + self.energysystem.add(bel, source1, source2) + om = self.get_om() + solph.constraints.generic_integral_limit( + om, keyword="emission_factor", lower_limit=777 + ) + + self.compare_lp_files("emission_limit_lower.lp", my_om=om) + def test_equate_variables_constraint(self): """Testing the equate_variables function in the constraint module.""" bus1 = solph.buses.Bus(label="Bus1") diff --git a/tests/lp_files/emission_limit_lower.lp b/tests/lp_files/emission_limit_lower.lp new file mode 100644 index 000000000..92fa30731 --- /dev/null +++ b/tests/lp_files/emission_limit_lower.lp @@ -0,0 +1,38 @@ +\* Source Pyomo model name=Model *\ + +min +objective: ++0 ONE_VAR_CONSTANT + +s.t. + +c_l_integral_limit_emission_factor_lower_limit_: ++0.8 flow(source1_electricityBus_0) ++0.8 flow(source1_electricityBus_1) ++0.8 flow(source1_electricityBus_2) +>= 777 + +c_e_BusBlock_balance(electricityBus_0)_: ++1 flow(source1_electricityBus_0) ++1 flow(source2_electricityBus_0) += 0 + +c_e_BusBlock_balance(electricityBus_1)_: ++1 flow(source1_electricityBus_1) ++1 flow(source2_electricityBus_1) += 0 + +c_e_BusBlock_balance(electricityBus_2)_: ++1 flow(source1_electricityBus_2) ++1 flow(source2_electricityBus_2) += 0 + +bounds + 1 <= ONE_VAR_CONSTANT <= 1 + 0 <= flow(source1_electricityBus_0) <= 100 + 0 <= flow(source1_electricityBus_1) <= 100 + 0 <= flow(source1_electricityBus_2) <= 100 + 0 <= flow(source2_electricityBus_0) <= 100 + 0 <= flow(source2_electricityBus_1) <= 100 + 0 <= flow(source2_electricityBus_2) <= 100 +end diff --git a/tests/test_constraints_module.py b/tests/test_constraints_module.py index fbf4fc15b..c2614001b 100644 --- a/tests/test_constraints_module.py +++ b/tests/test_constraints_module.py @@ -1,9 +1,11 @@ +import pytest + import pandas as pd from oemof import solph -def test_special(): +def test_integral_limit_wrapper(): date_time_index = pd.date_range("1/1/2012", periods=5, freq="h") energysystem = solph.EnergySystem( timeindex=date_time_index, @@ -22,9 +24,10 @@ def test_special(): flow_with_keyword = { (src1, bel): flow1, } - solph.constraints.generic_integral_limit( - model, "my_factor", flow_with_keyword, limit=777 - ) + with pytest.warns(FutureWarning): + solph.constraints.generic_integral_limit( + model, "my_factor", flow_with_keyword, limit=777 + ) def test_something_else():