<s> import pytest <EOL> import tempfile <EOL> from calliope . utils import AttrDict <EOL> from . import common <EOL> from . common import assert_almost_equal , solver , _add_test_path <EOL> def create_and_run_model ( override , iterative_warmstart = False , <EOL> demand_file = '<STR_LIT>' ) : <EOL> locations = """<STR_LIT>""" <EOL> config_run = """<STR_LIT>""" <EOL> override = AttrDict . from_yaml_string ( override ) <EOL> override . set_key ( '<STR_LIT>' , solver ) <EOL> with tempfile . NamedTemporaryFile ( ) as f : <EOL> f . write ( locations . format ( demand_file = demand_file ) . encode ( '<STR_LIT:utf-8>' ) ) <EOL> f . read ( ) <EOL> model = common . simple_model ( config_run = config_run , <EOL> config_locations = f . name , <EOL> override = override , <EOL> path = _add_test_path ( '<STR_LIT>' ) ) <EOL> model . run ( iterative_warmstart ) <EOL> return model <EOL> class TestModel : <EOL> def test_model_fixed_costs_op ( self ) : <EOL> override = """<STR_LIT>""" <EOL> model1 = create_and_run_model ( override . format ( <NUM_LIT:2> ) ) <EOL> cost1 = model1 . solution [ '<STR_LIT>' ] . loc [ dict ( k = '<STR_LIT>' , x = '<STR_LIT:1>' , y = '<STR_LIT>' ) ] <EOL> lcoe1 = model1 . solution [ '<STR_LIT>' ] . loc [ dict ( techs = '<STR_LIT>' , cols_summary = '<STR_LIT>' ) ] <EOL> model2 = create_and_run_model ( override . format ( <NUM_LIT:4> ) ) <EOL> cost2 = model2 . solution [ '<STR_LIT>' ] . loc [ dict ( k = '<STR_LIT>' , x = '<STR_LIT:1>' , y = '<STR_LIT>' ) ] <EOL> lcoe2 = model2 . solution [ '<STR_LIT>' ] . loc [ dict ( techs = '<STR_LIT>' , cols_summary = '<STR_LIT>' ) ] <EOL> assert_almost_equal ( lcoe1 , lcoe2 , tolerance = <NUM_LIT> ) <EOL> assert_almost_equal ( <NUM_LIT:2> * cost1 , cost2 , tolerance = <NUM_LIT> ) <EOL> def test_model_var_costs_op ( self ) : <EOL> override = """<STR_LIT>""" <EOL> model1 = create_and_run_model ( override . format ( <NUM_LIT:2> ) ) <EOL> cost1 = model1 . solution [ '<STR_LIT>' ] . loc [ dict ( k = '<STR_LIT>' , x = '<STR_LIT:1>' , y = '<STR_LIT>' ) ] <EOL> lcoe1 = model1 . solution [ '<STR_LIT>' ] . loc [ dict ( techs = '<STR_LIT>' , cols_summary = '<STR_LIT>' ) ] <EOL> model2 = create_and_run_model ( override . format ( <NUM_LIT:4> ) ) <EOL> cost2 = model2 . solution [ '<STR_LIT>' ] . loc [ dict ( k = '<STR_LIT>' , x = '<STR_LIT:1>' , y = '<STR_LIT>' ) ] <EOL> lcoe2 = model2 . solution [ '<STR_LIT>' ] . loc [ dict ( techs = '<STR_LIT>' , cols_summary = '<STR_LIT>' ) ] <EOL> assert_almost_equal ( lcoe1 , lcoe2 , tolerance = <NUM_LIT> ) <EOL> assert_almost_equal ( <NUM_LIT:2> * cost1 , cost2 , tolerance = <NUM_LIT> ) <EOL> def test_model_fixed_costs_op_with_varying_demand ( self ) : <EOL> override = """<STR_LIT>""" <EOL> demand = '<STR_LIT>' <EOL> model1 = create_and_run_model ( override . format ( <NUM_LIT:2> ) , demand_file = demand ) <EOL> cost1 = model1 . solution [ '<STR_LIT>' ] . loc [ dict ( k = '<STR_LIT>' , x = '<STR_LIT:1>' , y = '<STR_LIT>' ) ] <EOL> lcoe1 = model1 . solution [ '<STR_LIT>' ] . loc [ dict ( techs = '<STR_LIT>' , cols_summary = '<STR_LIT>' ) ] <EOL> model2 = create_and_run_model ( override . format ( <NUM_LIT:4> ) , demand_file = demand ) <EOL> cost2 = model2 . solution [ '<STR_LIT>' ] . loc [ dict ( k = '<STR_LIT>' , x = '<STR_LIT:1>' , y = '<STR_LIT>' ) ] <EOL> lcoe2 = model2 . solution [ '<STR_LIT>' ] . loc [ dict ( techs = '<STR_LIT>' , cols_summary = '<STR_LIT>' ) ] <EOL> assert lcoe1 > lcoe2 <EOL> assert_almost_equal ( <NUM_LIT:2> * cost1 , cost2 , tolerance = <NUM_LIT> ) <EOL> def test_model_var_costs_op_with_varying_demand ( self ) : <EOL> override = """<STR_LIT>""" <EOL> demand = '<STR_LIT>' <EOL> model1 = create_and_run_model ( override . format ( <NUM_LIT:2> ) , demand_file = demand ) <EOL> cost1 = model1 . solution [ '<STR_LIT>' ] . loc [ dict ( k = '<STR_LIT>' , x = '<STR_LIT:1>' , y = '<STR_LIT>' ) ] <EOL> lcoe1 = model1 . solution [ '<STR_LIT>' ] . loc [ dict ( techs = '<STR_LIT>' , cols_summary = '<STR_LIT>' ) ] <EOL> model2 = create_and_run_model ( override . format ( <NUM_LIT:4> ) , demand_file = demand ) <EOL> cost2 = model2 . solution [ '<STR_LIT>' ] . loc [ dict ( k = '<STR_LIT>' , x = '<STR_LIT:1>' , y = '<STR_LIT>' ) ] <EOL> lcoe2 = model2 . solution [ '<STR_LIT>' ] . loc [ dict ( techs = '<STR_LIT>' , cols_summary = '<STR_LIT>' ) ] <EOL> assert_almost_equal ( lcoe1 , lcoe2 , tolerance = <NUM_LIT> ) <EOL> assert cost1 == <NUM_LIT> <EOL> assert cost2 == <NUM_LIT> </s>
