From 400374d34a1614203ff07d6803d5cbdfc1990527 Mon Sep 17 00:00:00 2001 From: Jostein Solaas Date: Thu, 24 Oct 2024 08:50:23 +0200 Subject: [PATCH] test: remove dto cases Remove all_energy_usage_models and consumer system v2 DTOCases. Replace with YamlCase and use YamlModel to setup the model. --- src/libecalc/fixtures/__init__.py | 2 - src/libecalc/fixtures/case_types.py | 13 + .../cases/all_energy_usage_models/__init__.py | 1 - .../all_energy_usage_models/all_models_dto.py | 97 --- .../cases/consumer_system_v2/__init__.py | 4 - .../consumer_system_v2_dto.py | 698 ------------------ .../libecalc/dto/test_asset_serialization.py | 8 - .../input/mappers/test_model_mapper.py | 10 +- src/tests/libecalc/input/test_yaml_model.py | 51 -- .../consumer_system_v2.json | 0 .../test_all_energy_usage_models.py | 21 +- .../integration/test_consumer_system_v2.py | 41 +- .../flow_diagram/test_ecalc_model_mapper.py | 9 +- 13 files changed, 55 insertions(+), 900 deletions(-) delete mode 100644 src/libecalc/fixtures/cases/consumer_system_v2/consumer_system_v2_dto.py delete mode 100644 src/tests/libecalc/dto/test_asset_serialization.py delete mode 100644 src/tests/libecalc/input/test_yaml_model.py rename src/tests/libecalc/integration/snapshots/test_consumer_system_v2/test_compressor_system_v2_results/{consumer_system_v2-consumer_system_v20 => consumer_system_v2-consumer_system_v2_yaml}/consumer_system_v2.json (100%) diff --git a/src/libecalc/fixtures/__init__.py b/src/libecalc/fixtures/__init__.py index 6c0e29292c..a916306c2d 100644 --- a/src/libecalc/fixtures/__init__.py +++ b/src/libecalc/fixtures/__init__.py @@ -1,8 +1,6 @@ from .case_types import DTOCase, YamlCase from .cases.all_energy_usage_models import * # noqa: F403 from .cases.consumer_system_v2 import ( - consumer_system_v2_dto, - consumer_system_v2_dto_fixture, consumer_system_v2_yaml, ) from .cases.consumer_with_time_slots_models import * # noqa: F403 diff --git a/src/libecalc/fixtures/case_types.py b/src/libecalc/fixtures/case_types.py index 471d7e4c93..171e4f6bfb 100644 --- a/src/libecalc/fixtures/case_types.py +++ b/src/libecalc/fixtures/case_types.py @@ -3,8 +3,12 @@ from pathlib import Path from typing import Dict, NamedTuple, TextIO +from ecalc_cli.infrastructure.file_resource_service import FileResourceService +from libecalc.common.time_utils import Frequency from libecalc.common.variables import VariablesMap from libecalc.dto import Asset +from libecalc.presentation.yaml.file_configuration_service import FileConfigurationService +from libecalc.presentation.yaml.model import YamlModel from libecalc.presentation.yaml.yaml_entities import MemoryResource @@ -19,6 +23,15 @@ def main_file(self) -> TextIO: lines = f.read() return io.StringIO(lines) + def get_yaml_model(self, frequency: Frequency = Frequency.NONE) -> YamlModel: + configuration_service = FileConfigurationService(self.main_file_path) + resource_service = FileResourceService(self.main_file_path.parent) + return YamlModel( + configuration_service=configuration_service, + resource_service=resource_service, + output_frequency=frequency, + ) + class DTOCase(NamedTuple): ecalc_model: Asset diff --git a/src/libecalc/fixtures/cases/all_energy_usage_models/__init__.py b/src/libecalc/fixtures/cases/all_energy_usage_models/__init__.py index 0bf206f60d..85e529d9b3 100644 --- a/src/libecalc/fixtures/cases/all_energy_usage_models/__init__.py +++ b/src/libecalc/fixtures/cases/all_energy_usage_models/__init__.py @@ -6,7 +6,6 @@ from libecalc.fixtures.case_utils import YamlCaseLoader from .all_models_dto import ( - all_energy_usage_models_dto, compressor, compressor_system, compressor_system_variable_speed_compressor_trains, diff --git a/src/libecalc/fixtures/cases/all_energy_usage_models/all_models_dto.py b/src/libecalc/fixtures/cases/all_energy_usage_models/all_models_dto.py index e70c893760..1a0db40c26 100644 --- a/src/libecalc/fixtures/cases/all_energy_usage_models/all_models_dto.py +++ b/src/libecalc/fixtures/cases/all_energy_usage_models/all_models_dto.py @@ -1110,103 +1110,6 @@ def genset_sampled() -> GeneratorSetSampled: ) -@pytest.fixture -def all_energy_usage_models_dto( - regularity, - fuel_gas, - genset_sampled, - generic_from_design_point_compressor_train_consumer, - simplified_variable_speed_compressor_train_known_stages_consumer, - simplified_variable_speed_compressor_train_unknown_stages_consumer, - deh, - late_start_consumer, - late_start_consumer_evolving_type, - salt_water_injection_tabular, - water_injection_single_speed, - water_injection_variable_speed, - pump_system_el_consumer, - simplified_compressor_system, - simplified_compressor_train_predefined_variable_speed_charts_with_gerg_fluid, - variable_speed_compressor_train_predefined_charts, - single_speed_compressor_train_asv_pressure_control, - single_speed_compressor_train_upstream_choke_pressure_control, - single_speed_compressor_train_downstream_choke_pressure_control, - single_speed_compressor_train_downstream_choke_pressure_control_maximum_discharge_pressure, - variable_speed_compressor_train_multiple_input_streams_and_interstage_pressure, - compressor, - tabulated, - compressor_system, - turbine_driven_compressor_train, - compressor_system_variable_speed_compressor_trains, - methane_venting, - flare, - all_energy_usage_models_variables, -) -> DTOCase: - return DTOCase( - Asset( - name="all_energy_usage_models", - installations=[ - Installation( - user_defined_category=InstallationUserDefinedCategoryType.FIXED, - name="MAIN_INSTALLATION", - regularity=regularity, - hydrocarbon_export={ - Period(datetime(1900, 1, 1), datetime(2021, 1, 1)): Expression.setup_from_expression( - value="SIM1;OIL_PROD {+} SIM1;GAS_PROD {/} 1000" - ), - }, - fuel_consumers=[ - GeneratorSet( - name="GeneratorSet", - user_defined_category={ - Period( - datetime(1900, 1, 1), datetime(2021, 1, 1) - ): ConsumerUserDefinedCategoryType.TURBINE_GENERATOR - }, - generator_set_model={ - Period(datetime(1900, 1, 1), datetime(2018, 1, 1)): genset_sampled, - Period(datetime(2018, 1, 1), datetime(2021, 1, 1)): genset_sampled, - }, - regularity=regularity, - fuel=fuel_gas, - consumers=[ - generic_from_design_point_compressor_train_consumer, - simplified_variable_speed_compressor_train_known_stages_consumer, - simplified_variable_speed_compressor_train_unknown_stages_consumer, - deh, - late_start_consumer, - late_start_consumer_evolving_type, - salt_water_injection_tabular, - water_injection_single_speed, - water_injection_variable_speed, - pump_system_el_consumer, - simplified_compressor_system, - simplified_compressor_train_predefined_variable_speed_charts_with_gerg_fluid, - variable_speed_compressor_train_predefined_charts, - single_speed_compressor_train_asv_pressure_control, - single_speed_compressor_train_upstream_choke_pressure_control, - single_speed_compressor_train_downstream_choke_pressure_control, - single_speed_compressor_train_downstream_choke_pressure_control_maximum_discharge_pressure, - variable_speed_compressor_train_multiple_input_streams_and_interstage_pressure, - ], - ), - flare, - compressor, - tabulated, - compressor_system, - turbine_driven_compressor_train, - compressor_system_variable_speed_compressor_trains, - ], - venting_emitters=[ - methane_venting, - ], - ) - ], - ), - all_energy_usage_models_variables, - ) - - @pytest.fixture def compressor_systems_and_compressor_train_temporal_dto( regularity, diff --git a/src/libecalc/fixtures/cases/consumer_system_v2/__init__.py b/src/libecalc/fixtures/cases/consumer_system_v2/__init__.py index 27d3f9a18b..e960cb35a3 100644 --- a/src/libecalc/fixtures/cases/consumer_system_v2/__init__.py +++ b/src/libecalc/fixtures/cases/consumer_system_v2/__init__.py @@ -4,10 +4,6 @@ from libecalc.fixtures import YamlCase from libecalc.fixtures.case_utils import YamlCaseLoader -from libecalc.fixtures.cases.consumer_system_v2.consumer_system_v2_dto import ( - consumer_system_v2_dto, - consumer_system_v2_dto_fixture, -) """ Test project for Consumer System v2 diff --git a/src/libecalc/fixtures/cases/consumer_system_v2/consumer_system_v2_dto.py b/src/libecalc/fixtures/cases/consumer_system_v2/consumer_system_v2_dto.py deleted file mode 100644 index acdf966af2..0000000000 --- a/src/libecalc/fixtures/cases/consumer_system_v2/consumer_system_v2_dto.py +++ /dev/null @@ -1,698 +0,0 @@ -from datetime import datetime - -import pytest - -from libecalc.common.component_type import ComponentType -from libecalc.common.consumption_type import ConsumptionType -from libecalc.common.energy_usage_type import EnergyUsageType -from libecalc.common.serializable_chart import SingleSpeedChartDTO -from libecalc.common.string.string_utils import generate_id -from libecalc.common.time_utils import Period -from libecalc.common.units import Unit -from libecalc.common.utils.rates import RateType -from libecalc.common.variables import VariablesMap -from libecalc.dto import ( - CompressorSampled, - CompressorSystemCompressor, - CompressorSystemConsumerFunction, - CompressorSystemOperationalSetting, - Emission, - FuelType, - GeneratorSetSampled, - PumpModel, - PumpSystemConsumerFunction, - PumpSystemOperationalSetting, - PumpSystemPump, -) -from libecalc.dto.components import ( - Asset, - CompressorComponent, - ConsumerSystem, - Crossover, - ElectricityConsumer, - ExpressionStreamConditions, - ExpressionTimeSeries, - FuelConsumer, - GeneratorSet, - Installation, - PumpComponent, - SystemComponentConditions, -) -from libecalc.dto.types import ( - ConsumerUserDefinedCategoryType, - FuelTypeUserDefinedCategoryType, - InstallationUserDefinedCategoryType, -) -from libecalc.expression import Expression -from libecalc.fixtures.case_types import DTOCase - -regularity = { - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): Expression.setup_from_expression(1), -} -fuel = { - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): FuelType( - name="fuel_gas", - user_defined_category=FuelTypeUserDefinedCategoryType.FUEL_GAS, - emissions=[ - Emission( - factor=Expression.setup_from_expression(2.2), - name="co2", - ) - ], - ) -} - -genset = GeneratorSetSampled( - headers=["POWER", "FUEL"], - data=[ - [0.0, 0.1, 1000000.0], - [0.0, 0.1, 1000000.0], - ], - energy_usage_adjustment_constant=0.0, - energy_usage_adjustment_factor=1.0, -) -compressor_1d = CompressorSampled( - energy_usage_adjustment_constant=0.0, - energy_usage_adjustment_factor=1.0, - energy_usage_type=EnergyUsageType.FUEL, - energy_usage_values=[0.0, 10000.0, 11000.0, 12000.0, 13000.0], - rate_values=[0.0, 1000000.0, 2000000.0, 3000000.0, 4000000.0], - suction_pressure_values=None, - discharge_pressure_values=None, - power_interpolation_values=[0.0, 1.0, 2.0, 3.0, 4.0], -) - -pump_model_single_speed = PumpModel( - energy_usage_adjustment_factor=1, - energy_usage_adjustment_constant=0, - chart=SingleSpeedChartDTO( - rate_actual_m3_hour=[100, 200, 300, 400, 500], - polytropic_head_joule_per_kg=[9810.0, 19620.0, 29430.0, 39240.0, 49050.0], - efficiency_fraction=[0.4, 0.5, 0.75, 0.70, 0.60], - speed_rpm=1, - ), - head_margin=0, -) - -compressor1 = CompressorComponent( - name="compressor1", - user_defined_category={ - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): ConsumerUserDefinedCategoryType.COMPRESSOR - }, - fuel=fuel, - regularity=regularity, - consumes=ConsumptionType.FUEL, - energy_usage_model={Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): compressor_1d}, -) -compressor2 = CompressorComponent( - name="compressor2", - user_defined_category={ - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): ConsumerUserDefinedCategoryType.COMPRESSOR - }, - fuel=fuel, - regularity=regularity, - consumes=ConsumptionType.FUEL, - energy_usage_model={Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): compressor_1d}, -) -compressor3 = CompressorComponent( - name="compressor3", - user_defined_category={ - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): ConsumerUserDefinedCategoryType.COMPRESSOR - }, - fuel=fuel, - regularity=regularity, - consumes=ConsumptionType.FUEL, - energy_usage_model={Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): compressor_1d}, -) -compressor4_temporal_model = CompressorComponent( - name="compressor3", - user_defined_category={ - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): ConsumerUserDefinedCategoryType.COMPRESSOR - }, - fuel=fuel, - regularity=regularity, - consumes=ConsumptionType.FUEL, - energy_usage_model={ - Period(datetime(2022, 1, 1), datetime(2024, 1, 1)): compressor_1d, - Period(datetime(2024, 1, 1), datetime(2027, 1, 1)): compressor_1d, - }, -) -compressor5_with_overlapping_temporal_model = CompressorComponent( - name="compressor3", - user_defined_category={ - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): ConsumerUserDefinedCategoryType.COMPRESSOR - }, - fuel=fuel, - regularity=regularity, - consumes=ConsumptionType.FUEL, - energy_usage_model={ - Period(datetime(2022, 1, 1), datetime(2024, 1, 1)): compressor_1d, - Period(datetime(2024, 1, 1), datetime(2025, 1, 1)): compressor_1d, - Period(datetime(2025, 1, 1), datetime(2027, 1, 1)): compressor_1d, - }, -) - -pump1 = PumpComponent( - name="pump1", - user_defined_category={Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): ConsumerUserDefinedCategoryType.PUMP}, - regularity=regularity, - consumes=ConsumptionType.ELECTRICITY, - energy_usage_model={Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): pump_model_single_speed}, -) -pump2 = PumpComponent( - name="pump2", - user_defined_category={Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): ConsumerUserDefinedCategoryType.PUMP}, - regularity=regularity, - consumes=ConsumptionType.ELECTRICITY, - energy_usage_model={Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): pump_model_single_speed}, -) -pump3 = PumpComponent( - name="pump3", - user_defined_category={Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): ConsumerUserDefinedCategoryType.PUMP}, - regularity=regularity, - consumes=ConsumptionType.ELECTRICITY, - energy_usage_model={Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): pump_model_single_speed}, -) - -compressor_system = FuelConsumer( - component_type=ComponentType.COMPRESSOR_SYSTEM, - name="compressor_system", - fuel=fuel, - energy_usage_model={ - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): CompressorSystemConsumerFunction( - energy_usage_type=EnergyUsageType.FUEL, - power_loss_factor=None, - compressors=[ - CompressorSystemCompressor( - name="compressor1", - compressor_train=compressor_1d, - ), - CompressorSystemCompressor( - name="compressor2", - compressor_train=compressor_1d, - ), - CompressorSystemCompressor( - name="compressor3", - compressor_train=compressor_1d, - ), - ], - total_system_rate=None, - operational_settings=[ - CompressorSystemOperationalSetting( - rates=[Expression.setup_from_expression(x) for x in [1000000, 6000000, 6000000]], - suction_pressures=[Expression.setup_from_expression("50")] * 3, - discharge_pressures=[Expression.setup_from_expression("250")] * 3, - crossover=[0, 1, 1], - ), - CompressorSystemOperationalSetting( - rates=[Expression.setup_from_expression(x) for x in ["$var.compressor1", 5000000, 5000000]], - suction_pressures=[Expression.setup_from_expression("50")] * 3, - discharge_pressures=[Expression.setup_from_expression("125")] * 3, - crossover=[0, 1, 1], - ), - CompressorSystemOperationalSetting( - rates=[Expression.setup_from_expression(x) for x in [1000000, 5000000, 5000000]], - suction_pressures=[Expression.setup_from_expression("50")] * 3, - discharge_pressures=[Expression.setup_from_expression("125")] * 3, - crossover=[0, 1, 1], - ), - ], - ) - }, - regularity=regularity, - user_defined_category={ - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): ConsumerUserDefinedCategoryType.COMPRESSOR - }, -) - -compressor_system_v2 = ConsumerSystem( - name="compressor_system_v2", - user_defined_category={ - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): ConsumerUserDefinedCategoryType.COMPRESSOR - }, - regularity=regularity, - consumes=ConsumptionType.FUEL, - fuel=fuel, - component_conditions=SystemComponentConditions( - crossover=[ - Crossover(from_component_id=generate_id("compressor2"), to_component_id=generate_id("compressor1")), - Crossover(from_component_id=generate_id("compressor3"), to_component_id=generate_id("compressor1")), - ], - ), - stream_conditions_priorities={ - "pri1": { - "compressor1": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=1000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=250, - unit=Unit.BARA, - ), - ), - }, - "compressor2": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=6000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=250, - unit=Unit.BARA, - ), - ), - }, - "compressor3": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=6000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=250, - unit=Unit.BARA, - ), - ), - }, - }, - "pri2": { - "compressor1": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value="$var.compressor1", - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=125, - unit=Unit.BARA, - ), - ), - }, - "compressor2": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=5000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=125, - unit=Unit.BARA, - ), - ), - }, - "compressor3": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=5000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=125, - unit=Unit.BARA, - ), - ), - }, - }, - "pri3": { - "compressor1": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=1000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=125, - unit=Unit.BARA, - ), - ), - }, - "compressor2": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=5000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=125, - unit=Unit.BARA, - ), - ), - }, - "compressor3": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=5000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=125, - unit=Unit.BARA, - ), - ), - }, - }, - }, - consumers=[ - compressor1, # Max rate of 4000000 - compressor2, # Max rate of 4000000 - compressor3, # Max rate of 4000000 - ], -) - -pump_system = ElectricityConsumer( - component_type=ComponentType.PUMP_SYSTEM, - name="pump_system", - energy_usage_model={ - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): PumpSystemConsumerFunction( - energy_usage_type=EnergyUsageType.POWER, - condition=Expression.setup_from_expression("1"), - power_loss_factor=Expression.setup_from_expression("0"), - pumps=[ - PumpSystemPump( - name="pump1", - pump_model=pump_model_single_speed, - ), - PumpSystemPump( - name="pump2", - pump_model=pump_model_single_speed, - ), - PumpSystemPump( - name="pump3", - pump_model=pump_model_single_speed, - ), - ], - total_system_rate=None, - operational_settings=[ - PumpSystemOperationalSetting( - rates=[Expression.setup_from_expression(x) for x in [4000000, 5000000, 6000000]], - suction_pressures=[Expression.setup_from_expression("50")] * 3, - discharge_pressures=[Expression.setup_from_expression("250")] * 3, - crossover=[0, 1, 1], - ), - PumpSystemOperationalSetting( - rates=[Expression.setup_from_expression(x) for x in [2000000, 2500000, 3000000]], - suction_pressures=[Expression.setup_from_expression("50")] * 3, - discharge_pressures=[Expression.setup_from_expression("125")] * 3, - crossover=[0, 1, 1], - ), - ], - fluid_density=Expression.setup_from_expression("2"), - ) - }, - regularity=regularity, - user_defined_category={Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): ConsumerUserDefinedCategoryType.PUMP}, -) - -pump_system_v2 = ConsumerSystem( - name="pump_system_v2", - user_defined_category={Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): ConsumerUserDefinedCategoryType.PUMP}, - regularity=regularity, - consumes=ConsumptionType.ELECTRICITY, - component_conditions=SystemComponentConditions( - crossover=[ - Crossover(from_component_id=generate_id("pump2"), to_component_id=generate_id("pump1")), - Crossover(from_component_id=generate_id("pump3"), to_component_id=generate_id("pump1")), - ], - ), - stream_conditions_priorities={ - "pri1": { - "pump1": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=4000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - fluid_density=ExpressionTimeSeries( - value=2, - unit=Unit.KG_SM3, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=250, - unit=Unit.BARA, - ), - ), - }, - "pump2": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=5000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - fluid_density=ExpressionTimeSeries( - value=2, - unit=Unit.KG_SM3, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=250, - unit=Unit.BARA, - ), - ), - }, - "pump3": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=6000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - fluid_density=ExpressionTimeSeries( - value=2, - unit=Unit.KG_SM3, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=250, - unit=Unit.BARA, - ), - ), - }, - }, - "pri2": { - "pump1": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=2000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - fluid_density=ExpressionTimeSeries( - value=2, - unit=Unit.KG_SM3, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=125, - unit=Unit.BARA, - ), - ), - }, - "pump2": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=2500000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - fluid_density=ExpressionTimeSeries( - value=2, - unit=Unit.KG_SM3, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=125, - unit=Unit.BARA, - ), - ), - }, - "pump3": { - "inlet": ExpressionStreamConditions( - rate=ExpressionTimeSeries( - value=3000000, - unit=Unit.STANDARD_CUBIC_METER_PER_DAY, - type=RateType.STREAM_DAY, - ), - pressure=ExpressionTimeSeries( - value=50, - unit=Unit.BARA, - ), - fluid_density=ExpressionTimeSeries( - value=2, - unit=Unit.KG_SM3, - ), - ), - "outlet": ExpressionStreamConditions( - pressure=ExpressionTimeSeries( - value=125, - unit=Unit.BARA, - ), - ), - }, - }, - }, - consumers=[pump1, pump2, pump3], -) - - -@pytest.fixture -def consumer_system_v2_dto_fixture() -> DTOCase: - """ - In order to make fixtures easier to spot, we should mark/tag/name them in order - to easily find them. Fixtures can e.g. not be used directly in parameterized test, - so providing a wrapper for fixtures seems appropriate. - :return: - """ - return consumer_system_v2_dto() - - -def consumer_system_v2_dto() -> DTOCase: - """ - Base Case consumer system v2 (no temporal models, no temporal operational settings) - :return: - """ - assert pump1 - assert pump2 - assert pump3 - return DTOCase( - Asset( - component_type=ComponentType.ASSET, - name="consumer_system_v2", - installations=[ - Installation( - name="installation", - user_defined_category=InstallationUserDefinedCategoryType.FIXED, - component_type=ComponentType.INSTALLATION, - regularity=regularity, - hydrocarbon_export={ - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): Expression.setup_from_expression(17) - }, - fuel_consumers=[ - GeneratorSet( - name="GeneratorSet", - user_defined_category={ - Period( - datetime(2022, 1, 1), datetime(2027, 1, 1) - ): ConsumerUserDefinedCategoryType.TURBINE_GENERATOR - }, - generator_set_model={ - Period(datetime(2022, 1, 1), datetime(2027, 1, 1)): genset, - }, - regularity=regularity, - fuel=fuel, - consumers=[pump_system, pump_system_v2], - ), - compressor_system, - compressor_system_v2, - ], - venting_emitters=[], - ) - ], - ), - variables=VariablesMap( - time_vector=[ - datetime(2022, 1, 1), - datetime(2024, 1, 1), - datetime(2025, 1, 1), - datetime(2026, 1, 1), - datetime(2027, 1, 1), - ], - variables={ - "compressor1;rate": [0.0, 0.0, 0.0, 4000000.0], - "$var.compressor1": [0, 0, 0, 4000000], - }, - ), - ) diff --git a/src/tests/libecalc/dto/test_asset_serialization.py b/src/tests/libecalc/dto/test_asset_serialization.py deleted file mode 100644 index c27769c383..0000000000 --- a/src/tests/libecalc/dto/test_asset_serialization.py +++ /dev/null @@ -1,8 +0,0 @@ -from libecalc import dto -from libecalc.fixtures import DTOCase - - -def test_serialization(all_energy_usage_models_dto: DTOCase): - serialized_model = all_energy_usage_models_dto.ecalc_model.model_dump_json() - - assert dto.Asset.model_validate_json(serialized_model) == all_energy_usage_models_dto.ecalc_model diff --git a/src/tests/libecalc/input/mappers/test_model_mapper.py b/src/tests/libecalc/input/mappers/test_model_mapper.py index 6c0f4c9293..15629be058 100644 --- a/src/tests/libecalc/input/mappers/test_model_mapper.py +++ b/src/tests/libecalc/input/mappers/test_model_mapper.py @@ -10,11 +10,19 @@ from libecalc.presentation.yaml.configuration_service import ConfigurationService from libecalc.presentation.yaml.mappers.model import ModelMapper from libecalc.presentation.yaml.model import YamlModel +from libecalc.presentation.yaml.resource_service import ResourceService from libecalc.presentation.yaml.yaml_entities import MemoryResource, ResourceStream from libecalc.presentation.yaml.yaml_keywords import EcalcYamlKeywords from libecalc.presentation.yaml.yaml_models.yaml_model import ReaderType, YamlConfiguration, YamlValidator from libecalc.presentation.yaml.yaml_types.models import YamlCompressorChart -from tests.libecalc.input.test_yaml_model import DirectResourceService + + +class DirectResourceService(ResourceService): + def __init__(self, resources: Dict[str, MemoryResource]): + self._resources = resources + + def get_resources(self, configuration: YamlValidator) -> Dict[str, MemoryResource]: + return self._resources class TestModelMapper: diff --git a/src/tests/libecalc/input/test_yaml_model.py b/src/tests/libecalc/input/test_yaml_model.py deleted file mode 100644 index 3c306d5b9a..0000000000 --- a/src/tests/libecalc/input/test_yaml_model.py +++ /dev/null @@ -1,51 +0,0 @@ -from typing import Dict - -from libecalc.common.time_utils import Frequency -from libecalc.fixtures import DTOCase, YamlCase -from libecalc.presentation.yaml.file_configuration_service import FileConfigurationService -from libecalc.presentation.yaml.model import YamlModel -from libecalc.presentation.yaml.resource_service import ResourceService -from libecalc.presentation.yaml.yaml_entities import MemoryResource -from libecalc.presentation.yaml.yaml_models.yaml_model import YamlValidator - - -class DirectResourceService(ResourceService): - def __init__(self, resources: Dict[str, MemoryResource]): - self._resources = resources - - def get_resources(self, configuration: YamlValidator) -> Dict[str, MemoryResource]: - return self._resources - - -class TestParseYaml: - def test_parse_input_with_all_energy_usage_models( - self, all_energy_usage_models_yaml: YamlCase, all_energy_usage_models_dto: DTOCase - ): - """ - Make sure yaml and dto is consistent for all_energy_usage_models - """ - configuration_service = FileConfigurationService(configuration_path=all_energy_usage_models_yaml.main_file_path) - resource_service = DirectResourceService(resources=all_energy_usage_models_yaml.resources) - model = YamlModel( - configuration_service=configuration_service, - resource_service=resource_service, - output_frequency=Frequency.NONE, - ) - - assert model.dto.model_dump_json() == all_energy_usage_models_dto.ecalc_model.model_dump_json() - assert model.variables == all_energy_usage_models_dto.variables - - def test_parse_input_with_consumer_system_v2(self, consumer_system_v2_yaml, consumer_system_v2_dto_fixture): - """ - Make sure yaml and dto is consistent for consumer_system_v2 - """ - configuration_service = FileConfigurationService(configuration_path=consumer_system_v2_yaml.main_file_path) - resource_service = DirectResourceService(resources=consumer_system_v2_yaml.resources) - model = YamlModel( - configuration_service=configuration_service, - resource_service=resource_service, - output_frequency=Frequency.NONE, - ) - - assert model.dto.model_dump(mode="json") == consumer_system_v2_dto_fixture.ecalc_model.model_dump(mode="json") - assert model.variables == consumer_system_v2_dto_fixture.variables diff --git a/src/tests/libecalc/integration/snapshots/test_consumer_system_v2/test_compressor_system_v2_results/consumer_system_v2-consumer_system_v20/consumer_system_v2.json b/src/tests/libecalc/integration/snapshots/test_consumer_system_v2/test_compressor_system_v2_results/consumer_system_v2-consumer_system_v2_yaml/consumer_system_v2.json similarity index 100% rename from src/tests/libecalc/integration/snapshots/test_consumer_system_v2/test_compressor_system_v2_results/consumer_system_v2-consumer_system_v20/consumer_system_v2.json rename to src/tests/libecalc/integration/snapshots/test_consumer_system_v2/test_compressor_system_v2_results/consumer_system_v2-consumer_system_v2_yaml/consumer_system_v2.json diff --git a/src/tests/libecalc/integration/test_all_energy_usage_models.py b/src/tests/libecalc/integration/test_all_energy_usage_models.py index 80f4f55f1d..89a7c8d7e9 100644 --- a/src/tests/libecalc/integration/test_all_energy_usage_models.py +++ b/src/tests/libecalc/integration/test_all_energy_usage_models.py @@ -1,29 +1,28 @@ import pytest from libecalc.application.energy_calculator import EnergyCalculator -from libecalc.application.graph_result import EnergyCalculatorResult, GraphResult +from libecalc.application.graph_result import EnergyCalculatorResult +from libecalc.fixtures import YamlCase @pytest.fixture -def result(all_energy_usage_models_dto) -> EnergyCalculatorResult: - ecalc_model = all_energy_usage_models_dto.ecalc_model - variables = all_energy_usage_models_dto.variables - - graph = ecalc_model.get_graph() +def result(all_energy_usage_models_yaml: YamlCase) -> EnergyCalculatorResult: + model = all_energy_usage_models_yaml.get_yaml_model() + model.validate_for_run() + graph = model.get_graph() energy_calculator = EnergyCalculator(graph=graph) + variables = model.variables consumer_results = energy_calculator.evaluate_energy_usage(variables) emission_results = energy_calculator.evaluate_emissions( variables_map=variables, consumer_results=consumer_results, ) - result = GraphResult( - graph=graph, + + return EnergyCalculatorResult( consumer_results=consumer_results, variables_map=variables, emission_results=emission_results, - ).get_results() - - return result + ) @pytest.mark.snapshot diff --git a/src/tests/libecalc/integration/test_consumer_system_v2.py b/src/tests/libecalc/integration/test_consumer_system_v2.py index c6309d5164..d6c4c0a7fe 100644 --- a/src/tests/libecalc/integration/test_consumer_system_v2.py +++ b/src/tests/libecalc/integration/test_consumer_system_v2.py @@ -5,45 +5,43 @@ import pytest from libecalc.application.energy_calculator import EnergyCalculator -from libecalc.application.graph_result import EnergyCalculatorResult, GraphResult -from libecalc.fixtures import DTOCase, consumer_system_v2_dto +from libecalc.application.graph_result import EnergyCalculatorResult +from libecalc.fixtures import YamlCase -def result(consumer_system_v2: DTOCase) -> EnergyCalculatorResult: - ecalc_model = consumer_system_v2.ecalc_model - variables = consumer_system_v2.variables - - graph = ecalc_model.get_graph() +def result(consumer_system_v2: YamlCase) -> EnergyCalculatorResult: + model = consumer_system_v2.get_yaml_model() + model.validate_for_run() + graph = model.get_graph() energy_calculator = EnergyCalculator(graph=graph) + variables = model.variables consumer_results = energy_calculator.evaluate_energy_usage(variables) emission_results = energy_calculator.evaluate_emissions( variables_map=variables, consumer_results=consumer_results, ) - result = GraphResult( - graph=graph, + + return EnergyCalculatorResult( consumer_results=consumer_results, variables_map=variables, emission_results=emission_results, - ).get_results() - - return result + ) parameterized_v2_parameters = [ ( "consumer_system_v2", - consumer_system_v2_dto(), + "consumer_system_v2_yaml", # Fixture name ), ] @pytest.mark.parametrize( - "name, consumer_system_v2", + "name, consumer_system_v2_fixture_name", parameterized_v2_parameters, ) @pytest.mark.snapshot -def test_compressor_system_v2_results(name: str, consumer_system_v2: DTOCase, request): +def test_compressor_system_v2_results(name: str, consumer_system_v2_fixture_name: str, request): """ NOTE: The test below depends on this test. If the order of parameters or names of the parameters are changed, the test below will fail. This test is meant to show that despite different permutations of @@ -70,11 +68,12 @@ def test_compressor_system_v2_results(name: str, consumer_system_v2: DTOCase, re Note: Consumer system v2 for pump and compressor are very similar. We should consider if they can share most of the code. Right now we duplicate code. """ + consumer_system_v2 = request.getfixturevalue(consumer_system_v2_fixture_name) rounded_snapshot = request.getfixturevalue("rounded_snapshot") ecalc_result = result(consumer_system_v2) - asset_graph = consumer_system_v2.ecalc_model.get_graph() + asset_graph = consumer_system_v2.get_yaml_model().get_graph() pump_system_id = asset_graph.get_node_id_by_name("pump_system") pump_system_v2_id = asset_graph.get_node_id_by_name("pump_system_v2") compressor_system_id = asset_graph.get_node_id_by_name("compressor_system") @@ -123,17 +122,15 @@ def test_compare_snapshots(snapshot): consumer_system_v2_snapshots = [] - for consumer_system_v2_snapshot_index, consumer_system_v2_snapshot in enumerate(parameterized_v2_parameters): - consumer_system_v2_snapshot_name, _ = consumer_system_v2_snapshot - - print(f"testing {consumer_system_v2_snapshot_name}") + for case_name, fixture_name in parameterized_v2_parameters: + print(f"testing {case_name}") # NOTE: When we use parameterized tests, there is some magic wrt. the name the snapshots are given, to make sure that they are 1. unique and 2. retrievable with open( Path( snapshot.snapshot_dir.parent / "test_compressor_system_v2_results" - / f"{consumer_system_v2_snapshot_name}-consumer_system_v2{consumer_system_v2_snapshot_index}" - / f"{consumer_system_v2_snapshot_name}.json" + / f"{case_name}-{fixture_name}" + / f"{case_name}.json" ) ) as snapshot_file: consumer_system_v2_snapshots.append(json.loads(snapshot_file.read())) diff --git a/src/tests/libecalc/output/flow_diagram/test_ecalc_model_mapper.py b/src/tests/libecalc/output/flow_diagram/test_ecalc_model_mapper.py index b3ae9a5e5a..4fba1148c5 100644 --- a/src/tests/libecalc/output/flow_diagram/test_ecalc_model_mapper.py +++ b/src/tests/libecalc/output/flow_diagram/test_ecalc_model_mapper.py @@ -6,7 +6,7 @@ import pytest from libecalc import dto -from libecalc.fixtures import DTOCase +from libecalc.fixtures import YamlCase from libecalc.presentation.flow_diagram.EcalcModelMapper import ( EcalcModelMapper, FlowDiagram, @@ -17,10 +17,9 @@ class TestEcalcModelMapper: @pytest.mark.snapshot - def test_all_energy_usage_models(self, all_energy_usage_models_dto: DTOCase, snapshot): - actual_fd = EcalcModelMapper.from_dto_to_fde( - all_energy_usage_models_dto.ecalc_model, result_options=dto.ResultOptions() - ) + def test_all_energy_usage_models(self, all_energy_usage_models_yaml: YamlCase, snapshot): + model = all_energy_usage_models_yaml.get_yaml_model() + actual_fd = EcalcModelMapper.from_dto_to_fde(model.dto, result_options=dto.ResultOptions()) snapshot_name = "all_energy_usage_models_fde.json" snapshot.assert_match(