diff --git a/pyproject.toml b/pyproject.toml index 00856161b..680cab71a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -155,7 +155,7 @@ ignore = [ [tool.interrogate] # Monitor doc-string coverage -fail-under = 37 # Fail CI if the doc-string coverage falls below this level TODO: Set to 100% +fail-under = 25 # Fail CI if the doc-string coverage falls below this level TODO: Set to 100% ignore-init-method = true ignore-init-module = false ignore-magic = false diff --git a/src/libecalc/presentation/yaml/mappers/create_references.py b/src/libecalc/presentation/yaml/mappers/create_references.py index 16241d36b..1b57a940e 100644 --- a/src/libecalc/presentation/yaml/mappers/create_references.py +++ b/src/libecalc/presentation/yaml/mappers/create_references.py @@ -21,10 +21,10 @@ def create_references(configuration: PyYamlYamlModel, resources: Resources) -> R facility_input_mapper = FacilityInputMapper(resources=resources) facility_inputs_from_files = { facility_input.get(EcalcYamlKeywords.name): facility_input_mapper.from_yaml_to_dto(facility_input) - for facility_input in configuration.facility_inputs + for facility_input in configuration.facility_inputs_raise_if_invalid } models = create_model_references( - models_yaml_config=configuration.models, + models_yaml_config=configuration.models_raise_if_invalid, facility_inputs=facility_inputs_from_files, resources=resources, ) diff --git a/src/libecalc/presentation/yaml/yaml_models/pyyaml_yaml_model.py b/src/libecalc/presentation/yaml/yaml_models/pyyaml_yaml_model.py index 5f6eb1155..51425ab34 100644 --- a/src/libecalc/presentation/yaml/yaml_models/pyyaml_yaml_model.py +++ b/src/libecalc/presentation/yaml/yaml_models/pyyaml_yaml_model.py @@ -37,6 +37,10 @@ ) from libecalc.presentation.yaml.yaml_models.yaml_model import YamlModel, YamlValidator from libecalc.presentation.yaml.yaml_types.components.yaml_asset import YamlAsset +from libecalc.presentation.yaml.yaml_types.facility_model.yaml_facility_model import ( + YamlFacilityModel, +) +from libecalc.presentation.yaml.yaml_types.models import YamlModel as YamlModelsModel from libecalc.presentation.yaml.yaml_types.time_series.yaml_time_series import ( YamlTimeSeriesCollection, ) @@ -340,9 +344,32 @@ def yaml_variables(self) -> Dict[YamlVariableReferenceId, dict]: return self._internal_datamodel.get(EcalcYamlKeywords.variables, {}) @property - def facility_inputs(self): + @deprecated("Deprecated, facility_inputs in combination with validate should be used instead") + def facility_inputs_raise_if_invalid(self): return self._internal_datamodel.get(EcalcYamlKeywords.facility_inputs, []) + @property + def facility_inputs(self) -> List[YamlFacilityModel]: + facility_inputs = [] + for facility_input in self._internal_datamodel.get(EcalcYamlKeywords.facility_inputs, []): + try: + facility_inputs.append(TypeAdapter(YamlFacilityModel).validate_python(facility_input)) + except PydanticValidationError: + pass + + return facility_inputs + + @property + def models(self) -> List[YamlModelsModel]: + models = [] + for model in self._internal_datamodel.get(EcalcYamlKeywords.models, []): + try: + models.append(TypeAdapter(YamlModelsModel).validate_python(model)) + except PydanticValidationError: + pass + + return models + @property @deprecated("Deprecated, time_series in combination with validate should be used instead") def time_series_raise_if_invalid(self) -> List[YamlTimeSeriesCollection]: @@ -373,7 +400,7 @@ def time_series(self) -> List[YamlTimeSeriesCollection]: return time_series @property - def models(self): + def models_raise_if_invalid(self): return self._internal_datamodel.get(EcalcYamlKeywords.models, []) @property diff --git a/src/libecalc/presentation/yaml/yaml_models/yaml_model.py b/src/libecalc/presentation/yaml/yaml_models/yaml_model.py index 7d66bbdea..770f14d1d 100644 --- a/src/libecalc/presentation/yaml/yaml_models/yaml_model.py +++ b/src/libecalc/presentation/yaml/yaml_models/yaml_model.py @@ -42,6 +42,7 @@ def all_resource_names(self) -> List[str]: def variables(self) -> Dict[str, YamlVariable]: pass + @property @abc.abstractmethod def facility_inputs(self): pass @@ -51,6 +52,7 @@ def facility_inputs(self): def time_series(self) -> List[YamlTimeSeriesCollection]: pass + @property @abc.abstractmethod def models(self): pass diff --git a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/__init__.py b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/__init__.py index a083bd57e..f5bf7ccf3 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/__init__.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/__init__.py @@ -6,13 +6,13 @@ from libecalc.presentation.yaml.yaml_types.components.legacy.energy_usage_model.yaml_energy_usage_model_compressor import ( YamlEnergyUsageModelCompressor, ) +from libecalc.presentation.yaml.yaml_types.components.legacy.energy_usage_model.yaml_energy_usage_model_compressor_train_multiple_streams import ( + YamlEnergyUsageModelCompressorTrainMultipleStreams, +) from libecalc.presentation.yaml.yaml_types.components.legacy.energy_usage_model.yaml_energy_usage_model_consumer_system import ( YamlEnergyUsageModelCompressorSystem, YamlEnergyUsageModelPumpSystem, ) -from libecalc.presentation.yaml.yaml_types.components.legacy.energy_usage_model.yaml_energy_usage_model_consumer_system_multiple_streams import ( - YamlEnergyUsageModelCompressorTrainMultipleStreams, -) from libecalc.presentation.yaml.yaml_types.components.legacy.energy_usage_model.yaml_energy_usage_model_direct import ( YamlEnergyUsageModelDirect, ) diff --git a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_compressor.py b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_compressor.py index 1a2ba3f16..e6a975a95 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_compressor.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_compressor.py @@ -8,6 +8,9 @@ from libecalc.presentation.yaml.yaml_types.components.yaml_expression_type import ( YamlExpressionType, ) +from libecalc.presentation.yaml.yaml_types.models.model_reference_validation import ( + CompressorEnergyUsageModelModelReference, +) class YamlEnergyUsageModelCompressor(EnergyUsageModelCommon): @@ -16,7 +19,7 @@ class YamlEnergyUsageModelCompressor(EnergyUsageModelCommon): title="TYPE", description="Defines the energy usage model type.\n\n$ECALC_DOCS_KEYWORDS_URL/TYPE", ) - energy_function: str = Field( + energy_function: CompressorEnergyUsageModelModelReference = Field( ..., title="ENERGY_FUNCTION", description="The compressor energy function, reference to a compressor type facility model defined in FACILITY_INPUTS", diff --git a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_consumer_system_multiple_streams.py b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_compressor_train_multiple_streams.py similarity index 90% rename from src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_consumer_system_multiple_streams.py rename to src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_compressor_train_multiple_streams.py index 7488d2a9a..55758ccb4 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_consumer_system_multiple_streams.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_compressor_train_multiple_streams.py @@ -8,6 +8,9 @@ from libecalc.presentation.yaml.yaml_types.components.yaml_expression_type import ( YamlExpressionType, ) +from libecalc.presentation.yaml.yaml_types.models.model_reference_validation import ( + MultipleStreamsEnergyUsageModelModelReference, +) class YamlEnergyUsageModelCompressorTrainMultipleStreams(EnergyUsageModelCommon): @@ -21,7 +24,7 @@ class YamlEnergyUsageModelCompressorTrainMultipleStreams(EnergyUsageModelCommon) title="RATE_UNIT", description="Defaults to SM3_PER_DAY, only SM3_PER_DAY implemented for now", ) - compressor_train_model: str = Field( + compressor_train_model: MultipleStreamsEnergyUsageModelModelReference = Field( ..., title="COMPRESSOR_TRAIN_MODEL", description="The compressor train model, reference to a compressor type model defined in MODELS", diff --git a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_consumer_system.py b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_consumer_system.py index 504200446..926c1a61e 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_consumer_system.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_consumer_system.py @@ -9,6 +9,10 @@ from libecalc.presentation.yaml.yaml_types.components.yaml_expression_type import ( YamlExpressionType, ) +from libecalc.presentation.yaml.yaml_types.models.model_reference_validation import ( + CompressorEnergyUsageModelModelReference, + PumpEnergyUsageModelModelReference, +) class YamlCompressorSystemCompressor(YamlBase): @@ -17,7 +21,7 @@ class YamlCompressorSystemCompressor(YamlBase): title="NAME", description="Name of the compressor", ) - compressor_model: str = Field( + compressor_model: CompressorEnergyUsageModelModelReference = Field( ..., title="COMPRESSOR_MODEL", description="Reference to a compressor type facility model defined in FACILITY_INPUTS", @@ -99,7 +103,7 @@ class YamlPumpSystemPump(YamlBase): title="NAME", description="Name of the pump", ) - chart: str = Field( + chart: PumpEnergyUsageModelModelReference = Field( ..., title="COMPRESSOR_MODEL", description="Reference to a pump type facility model defined in FACILITY_INPUTS", diff --git a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_pump.py b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_pump.py index ceb6b51c5..0cde87a9b 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_pump.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_pump.py @@ -8,6 +8,9 @@ from libecalc.presentation.yaml.yaml_types.components.yaml_expression_type import ( YamlExpressionType, ) +from libecalc.presentation.yaml.yaml_types.models.model_reference_validation import ( + PumpEnergyUsageModelModelReference, +) class YamlEnergyUsageModelPump(EnergyUsageModelCommon): @@ -16,7 +19,7 @@ class YamlEnergyUsageModelPump(EnergyUsageModelCommon): title="TYPE", description="Defines the energy usage model type.\n\n$ECALC_DOCS_KEYWORDS_URL/TYPE", ) - energy_function: str = Field( + energy_function: PumpEnergyUsageModelModelReference = Field( ..., title="ENERGY_FUNCTION", description="The pump energy function, reference to a pump type facility model defined in FACILITY_INPUTS", diff --git a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_tabulated.py b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_tabulated.py index dd1958acc..1a7576e17 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_tabulated.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/legacy/energy_usage_model/yaml_energy_usage_model_tabulated.py @@ -9,6 +9,9 @@ from libecalc.presentation.yaml.yaml_types.components.yaml_expression_type import ( YamlExpressionType, ) +from libecalc.presentation.yaml.yaml_types.models.model_reference_validation import ( + TabulatedEnergyUsageModelModelReference, +) class YamlTabulatedVariable(YamlBase): @@ -30,7 +33,7 @@ class YamlEnergyUsageModelTabulated(EnergyUsageModelCommon): title="TYPE", description="Defines the energy usage model type.\n\n$ECALC_DOCS_KEYWORDS_URL/TYPE", ) - energy_function: str = Field( + energy_function: TabulatedEnergyUsageModelModelReference = Field( ..., title="ENERGY_FUNCTION", description="The tabulated energy function, reference to a tabular type facility model defined in FACILITY_INPUTS", diff --git a/src/libecalc/presentation/yaml/yaml_types/components/yaml_asset.py b/src/libecalc/presentation/yaml/yaml_types/components/yaml_asset.py index 399296567..a364b9639 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/yaml_asset.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/yaml_asset.py @@ -6,8 +6,8 @@ from libecalc.presentation.yaml.yaml_types.components.yaml_installation import ( YamlInstallation, ) -from libecalc.presentation.yaml.yaml_types.facility_type.yaml_facility_type import ( - YamlFacilityType, +from libecalc.presentation.yaml.yaml_types.facility_model.yaml_facility_model import ( + YamlFacilityModel, ) from libecalc.presentation.yaml.yaml_types.fuel_type.yaml_fuel_type import YamlFuelType from libecalc.presentation.yaml.yaml_types.models import YamlModel @@ -33,7 +33,7 @@ class YamlAsset(YamlBase): description="Defines the inputs for time dependent variables, or 'reservoir variables'." "\n\n$ECALC_DOCS_KEYWORDS_URL/TIME_SERIES", ) - facility_inputs: List[YamlFacilityType] = Field( + facility_inputs: List[YamlFacilityModel] = Field( None, title="FACILITY_INPUTS", description="Defines input files which characterize various facility elements." diff --git a/src/libecalc/presentation/yaml/yaml_types/components/yaml_compressor.py b/src/libecalc/presentation/yaml/yaml_types/components/yaml_compressor.py index d547403e8..234a99f04 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/yaml_compressor.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/yaml_compressor.py @@ -15,6 +15,9 @@ YamlConsumerBase, ) from libecalc.presentation.yaml.yaml_types.models import YamlCompressorWithTurbine +from libecalc.presentation.yaml.yaml_types.models.model_reference_validation import ( + CompressorV2ModelReference, +) from libecalc.presentation.yaml.yaml_types.yaml_temporal_model import YamlTemporalModel CompressorModel = Union[YamlCompressorWithTurbine] @@ -30,7 +33,7 @@ class YamlCompressor(YamlConsumerBase): alias="TYPE", ) - energy_usage_model: YamlTemporalModel[str] + energy_usage_model: YamlTemporalModel[CompressorV2ModelReference] def to_dto( self, diff --git a/src/libecalc/presentation/yaml/yaml_types/components/yaml_generator_set.py b/src/libecalc/presentation/yaml/yaml_types/components/yaml_generator_set.py index 3cf38d82a..bf0aa3e65 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/yaml_generator_set.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/yaml_generator_set.py @@ -17,6 +17,9 @@ from libecalc.presentation.yaml.yaml_types.components.yaml_category_field import ( CategoryField, ) +from libecalc.presentation.yaml.yaml_types.models.model_reference_validation import ( + GeneratorSetModelReference, +) from libecalc.presentation.yaml.yaml_types.yaml_temporal_model import YamlTemporalModel @@ -34,7 +37,7 @@ class YamlGeneratorSet(YamlBase): title="FUEL", description="The fuel used by the generator set." "\n\n$ECALC_DOCS_KEYWORDS_URL/FUEL", ) - electricity2fuel: YamlTemporalModel[str] = Field( + electricity2fuel: YamlTemporalModel[GeneratorSetModelReference] = Field( ..., title="ELECTRICITY2FUEL", description="Specifies the correlation between the electric power delivered and the fuel burned by a " diff --git a/src/libecalc/presentation/yaml/yaml_types/components/yaml_pump.py b/src/libecalc/presentation/yaml/yaml_types/components/yaml_pump.py index 8e3033242..d0271ad37 100644 --- a/src/libecalc/presentation/yaml/yaml_types/components/yaml_pump.py +++ b/src/libecalc/presentation/yaml/yaml_types/components/yaml_pump.py @@ -14,6 +14,9 @@ from libecalc.presentation.yaml.yaml_types.components.yaml_base import ( YamlConsumerBase, ) +from libecalc.presentation.yaml.yaml_types.models.model_reference_validation import ( + PumpV2ModelReference, +) from libecalc.presentation.yaml.yaml_types.yaml_temporal_model import YamlTemporalModel @@ -27,7 +30,7 @@ class YamlPump(YamlConsumerBase): alias="TYPE", ) - energy_usage_model: YamlTemporalModel[str] + energy_usage_model: YamlTemporalModel[PumpV2ModelReference] def to_dto( self, diff --git a/src/libecalc/presentation/yaml/yaml_types/facility_type/__init__.py b/src/libecalc/presentation/yaml/yaml_types/facility_model/__init__.py similarity index 100% rename from src/libecalc/presentation/yaml/yaml_types/facility_type/__init__.py rename to src/libecalc/presentation/yaml/yaml_types/facility_model/__init__.py diff --git a/src/libecalc/presentation/yaml/yaml_types/facility_type/yaml_facility_type.py b/src/libecalc/presentation/yaml/yaml_types/facility_model/yaml_facility_model.py similarity index 74% rename from src/libecalc/presentation/yaml/yaml_types/facility_type/yaml_facility_type.py rename to src/libecalc/presentation/yaml/yaml_types/facility_model/yaml_facility_model.py index d55a95894..cd9cbff45 100644 --- a/src/libecalc/presentation/yaml/yaml_types/facility_type/yaml_facility_type.py +++ b/src/libecalc/presentation/yaml/yaml_types/facility_model/yaml_facility_model.py @@ -1,3 +1,4 @@ +import enum from typing import Literal, Union from pydantic import Field, field_validator @@ -9,6 +10,14 @@ ) +class YamlFacilityModelType(str, enum.Enum): + ELECTRICITY2FUEL = "ELECTRICITY2FUEL" + TABULAR = "TABULAR" + COMPRESSOR_TABULAR = "COMPRESSOR_TABULAR" + PUMP_CHART_SINGLE_SPEED = "PUMP_CHART_SINGLE_SPEED" + PUMP_CHART_VARIABLE_SPEED = "PUMP_CHART_VARIABLE_SPEED" + + def FacilityTypeField(): return Field( ..., @@ -30,7 +39,7 @@ class YamlFacilityAdjustment(YamlBase): ) -class YamlFacilityTypeBase(YamlBase): +class YamlFacilityModelBase(YamlBase): name: str = Field( ..., title="NAME", @@ -50,16 +59,16 @@ class YamlFacilityTypeBase(YamlBase): validate_file_exists = field_validator("file", mode="after")(file_exists_validator) -class YamlGeneratorSetModel(YamlFacilityTypeBase): - type: Literal["ELECTRICITY2FUEL"] = FacilityTypeField() +class YamlGeneratorSetModel(YamlFacilityModelBase): + type: Literal[YamlFacilityModelType.ELECTRICITY2FUEL] = FacilityTypeField() -class YamlTabularModel(YamlFacilityTypeBase): - type: Literal["TABULAR"] = FacilityTypeField() +class YamlTabularModel(YamlFacilityModelBase): + type: Literal[YamlFacilityModelType.TABULAR] = FacilityTypeField() -class YamlCompressorTabularModel(YamlFacilityTypeBase): - type: Literal["COMPRESSOR_TABULAR"] = FacilityTypeField() +class YamlCompressorTabularModel(YamlFacilityModelBase): + type: Literal[YamlFacilityModelType.COMPRESSOR_TABULAR] = FacilityTypeField() class YamlPumpChartUnits(YamlBase): @@ -80,7 +89,7 @@ class YamlPumpChartUnits(YamlBase): ) -class YamlPumpChartBase(YamlFacilityTypeBase): +class YamlPumpChartBase(YamlFacilityModelBase): head_margin: float = Field( None, title="HEAD_MARGIN", @@ -92,14 +101,14 @@ class YamlPumpChartBase(YamlFacilityTypeBase): class YamlPumpChartSingleSpeed(YamlPumpChartBase): - type: Literal["PUMP_CHART_SINGLE_SPEED"] = FacilityTypeField() + type: Literal[YamlFacilityModelType.PUMP_CHART_SINGLE_SPEED] = FacilityTypeField() class YamlPumpChartVariableSpeed(YamlPumpChartBase): - type: Literal["PUMP_CHART_VARIABLE_SPEED"] = FacilityTypeField() + type: Literal[YamlFacilityModelType.PUMP_CHART_VARIABLE_SPEED] = FacilityTypeField() -YamlFacilityType = Annotated[ +YamlFacilityModel = Annotated[ Union[ YamlGeneratorSetModel, YamlTabularModel, diff --git a/src/libecalc/presentation/yaml/yaml_types/models/model_reference.py b/src/libecalc/presentation/yaml/yaml_types/models/model_reference.py index f69323c02..82b7e8afc 100644 --- a/src/libecalc/presentation/yaml/yaml_types/models/model_reference.py +++ b/src/libecalc/presentation/yaml/yaml_types/models/model_reference.py @@ -1 +1 @@ -ModelReference = str +ModelName = str diff --git a/src/libecalc/presentation/yaml/yaml_types/models/model_reference_validation.py b/src/libecalc/presentation/yaml/yaml_types/models/model_reference_validation.py new file mode 100644 index 000000000..5232676a3 --- /dev/null +++ b/src/libecalc/presentation/yaml/yaml_types/models/model_reference_validation.py @@ -0,0 +1,219 @@ +from typing import Any, Dict, List + +from pydantic import AfterValidator +from pydantic_core import PydanticCustomError +from pydantic_core.core_schema import ValidationInfo +from typing_extensions import Annotated + +from libecalc.presentation.yaml.yaml_types.facility_model.yaml_facility_model import ( + YamlFacilityModelType, +) +from libecalc.presentation.yaml.yaml_types.models.model_reference import ModelName +from libecalc.presentation.yaml.yaml_types.models.yaml_enums import YamlModelType +from libecalc.presentation.yaml.yaml_validation_context import ( + ModelContext, + YamlModelValidationContextNames, +) + + +class InvalidModelReferenceError(ValueError): + def __init__(self, model_reference: ModelName): + self.model_reference = model_reference + + +class ModelReferenceNotFound(InvalidModelReferenceError): + pass + + +class InvalidModelReferenceType(InvalidModelReferenceError): + def __init__(self, model_reference: ModelName, model: ModelContext): + self.model = model + super().__init__(model_reference=model_reference) + + +def check_model_reference( + model_reference: Any, + available_models: Dict["ModelName", ModelContext], + allowed_types: List[str], +) -> str: + if model_reference not in available_models: + raise ModelReferenceNotFound(model_reference=model_reference) + + model = available_models[model_reference] + + if model.type not in allowed_types: + raise InvalidModelReferenceType(model_reference=model_reference, model=model) + + return model_reference + + +def check_field_model_reference(allowed_types: List[str]): + allowed_model_types = [ + allowed_type for allowed_type in allowed_types if allowed_type != YamlModelType.COMPRESSOR_WITH_TURBINE + ] + + def check_model_reference_wrapper(model_reference: Any, info: ValidationInfo): + if not info.context: + return model_reference + + assert YamlModelValidationContextNames.model_types in info.context + + models_context = info.context.get(YamlModelValidationContextNames.model_types) + try: + model_reference = check_model_reference( + model_reference, + available_models=models_context, + allowed_types=allowed_types, + ) + model = models_context[model_reference] + if model.type == YamlModelType.COMPRESSOR_WITH_TURBINE: + # Handle the compressor_model in turbine, it should be limited to the specified types. + check_model_reference( + model.compressor_model, + allowed_types=allowed_model_types, + available_models=models_context, + ) + except ModelReferenceNotFound as e: + raise PydanticCustomError( + "model_reference_not_found", + "Model '{model_reference}' not found", + { + "model_reference": e.model_reference, + }, + ) from e + except InvalidModelReferenceType as e: + raise PydanticCustomError( + "model_reference_type_invalid", + "Model '{model_reference}' with type {model_type} is not allowed", + { + "model_reference": e.model_reference, + "model_type": e.model.type, + }, + ) from e + + return check_model_reference_wrapper + + +CompressorV2ModelReference = Annotated[ + ModelName, + AfterValidator( + check_field_model_reference( + allowed_types=[ + YamlFacilityModelType.TABULAR, + YamlFacilityModelType.COMPRESSOR_TABULAR, + YamlModelType.COMPRESSOR_CHART, + ] + ) + ), +] + +GeneratorSetModelReference = Annotated[ + ModelName, + AfterValidator( + check_field_model_reference( + allowed_types=[ + YamlFacilityModelType.ELECTRICITY2FUEL, + ] + ) + ), +] + +PumpV2ModelReference = Annotated[ + ModelName, + AfterValidator( + check_field_model_reference( + [ + YamlFacilityModelType.TABULAR, + YamlFacilityModelType.PUMP_CHART_SINGLE_SPEED, + YamlFacilityModelType.PUMP_CHART_VARIABLE_SPEED, + ] + ) + ), +] + +CompressorEnergyUsageModelModelReference = Annotated[ + ModelName, + AfterValidator( + check_field_model_reference( + allowed_types=[ + YamlFacilityModelType.TABULAR, + YamlFacilityModelType.COMPRESSOR_TABULAR, + YamlModelType.COMPRESSOR_WITH_TURBINE, + YamlModelType.SINGLE_SPEED_COMPRESSOR_TRAIN, + YamlModelType.VARIABLE_SPEED_COMPRESSOR_TRAIN, + YamlModelType.SIMPLIFIED_VARIABLE_SPEED_COMPRESSOR_TRAIN, + ] + ) + ), +] + +MultipleStreamsEnergyUsageModelModelReference = Annotated[ + ModelName, + AfterValidator( + check_field_model_reference( + allowed_types=[ + YamlModelType.COMPRESSOR_WITH_TURBINE, + YamlModelType.VARIABLE_SPEED_COMPRESSOR_TRAIN_MULTIPLE_STREAMS_AND_PRESSURES, + ] + ) + ), +] + +PumpEnergyUsageModelModelReference = Annotated[ + ModelName, + AfterValidator( + check_field_model_reference( + allowed_types=[ + YamlFacilityModelType.TABULAR, + YamlFacilityModelType.PUMP_CHART_SINGLE_SPEED, + YamlFacilityModelType.PUMP_CHART_VARIABLE_SPEED, + ] + ) + ), +] + +TabulatedEnergyUsageModelModelReference = Annotated[ + ModelName, + AfterValidator( + check_field_model_reference( + allowed_types=[ + YamlFacilityModelType.TABULAR, + ] + ) + ), +] + +CompressorStageModelReference = Annotated[ + ModelName, + AfterValidator( + check_field_model_reference( + allowed_types=[ + YamlModelType.COMPRESSOR_CHART, + YamlFacilityModelType.COMPRESSOR_TABULAR, + YamlFacilityModelType.TABULAR, + ] + ) + ), +] + +FluidModelReference = Annotated[ + ModelName, + AfterValidator( + check_field_model_reference( + allowed_types=[ + YamlModelType.FLUID, + ] + ) + ), +] + +TurbineModelReference = Annotated[ + ModelName, + AfterValidator( + check_field_model_reference( + allowed_types=[ + YamlModelType.TURBINE, + ] + ) + ), +] diff --git a/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_chart.py b/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_chart.py index e0cdce677..bf251a30d 100644 --- a/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_chart.py +++ b/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_chart.py @@ -5,7 +5,7 @@ from typing_extensions import Annotated from libecalc.presentation.yaml.yaml_types import YamlBase -from libecalc.presentation.yaml.yaml_types.models.model_reference import ModelReference +from libecalc.presentation.yaml.yaml_types.models.model_reference import ModelName from libecalc.presentation.yaml.yaml_types.models.yaml_enums import ( YamlChartType, YamlModelType, @@ -54,7 +54,7 @@ class YamlUnits(YamlBase): class YamlSingleSpeedChart(YamlBase): - name: ModelReference = Field( + name: ModelName = Field( ..., description="Name of the model. See documentation for more information.", title="NAME", @@ -70,7 +70,7 @@ class YamlSingleSpeedChart(YamlBase): class YamlVariableSpeedChart(YamlBase): - name: ModelReference = Field( + name: ModelName = Field( ..., description="Name of the model. See documentation for more information.", title="NAME", @@ -88,7 +88,7 @@ class YamlVariableSpeedChart(YamlBase): class YamlGenericFromInputChart(YamlBase): - name: ModelReference = Field( + name: ModelName = Field( ..., description="Name of the model. See documentation for more information.", title="NAME", @@ -108,7 +108,7 @@ class YamlGenericFromInputChart(YamlBase): class YamlGenericFromDesignPointChart(YamlBase): - name: ModelReference = Field( + name: ModelName = Field( ..., description="Name of the model. See documentation for more information.", title="NAME", diff --git a/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_stages.py b/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_stages.py index ee7378476..020226296 100644 --- a/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_stages.py +++ b/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_stages.py @@ -4,7 +4,12 @@ from pydantic import Field from libecalc.presentation.yaml.yaml_types import YamlBase -from libecalc.presentation.yaml.yaml_types.models.yaml_enums import YamlPressureControl +from libecalc.presentation.yaml.yaml_types.models.model_reference_validation import ( + CompressorStageModelReference, +) +from libecalc.presentation.yaml.yaml_types.models.yaml_enums import ( + YamlPressureControl, +) class YamlControlMarginUnits(enum.Enum): @@ -31,7 +36,7 @@ class YamlCompressorStage(YamlBase): description="Inlet temperature in Celsius for stage", title="INLET_TEMPERATURE", ) - compressor_chart: str = Field( + compressor_chart: CompressorStageModelReference = Field( ..., description="Reference to compressor chart model for stage, must be defined in MODELS or FACILITY_INPUTS", title="COMPRESSOR_CHART", @@ -78,7 +83,7 @@ class YamlUnknownCompressorStages(YamlBase): description="Inlet temperature in Celsius for stage", title="INLET_TEMPERATURE", ) - compressor_chart: str = Field( + compressor_chart: CompressorStageModelReference = Field( ..., description="Reference to compressor chart model for stage, must be defined in MODELS or FACILITY_INPUTS", title="COMPRESSOR_CHART", diff --git a/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_trains.py b/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_trains.py index 2177ee719..ea8e0fbb0 100644 --- a/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_trains.py +++ b/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_trains.py @@ -3,6 +3,9 @@ from pydantic import Field from libecalc.presentation.yaml.yaml_types import YamlBase +from libecalc.presentation.yaml.yaml_types.models.model_reference_validation import ( + FluidModelReference, +) from libecalc.presentation.yaml.yaml_types.models.yaml_compressor_stages import ( YamlCompressorStageMultipleStreams, YamlCompressorStages, @@ -59,7 +62,7 @@ class YamlSingleSpeedCompressorTrain(YamlCompressorTrainBase): description="Constant to adjust power usage in MW", title="POWER_ADJUSTMENT_CONSTANT", ) - fluid_model: str = Field(..., description="Reference to a fluid model", title="FLUID_MODEL") + fluid_model: FluidModelReference = Field(..., description="Reference to a fluid model", title="FLUID_MODEL") def to_dto(self): raise NotImplementedError @@ -92,7 +95,7 @@ class YamlVariableSpeedCompressorTrain(YamlCompressorTrainBase): description="Constant to adjust power usage in MW", title="POWER_ADJUSTMENT_CONSTANT", ) - fluid_model: str = Field(..., description="Reference to a fluid model", title="FLUID_MODEL") + fluid_model: FluidModelReference = Field(..., description="Reference to a fluid model", title="FLUID_MODEL") def to_dto(self): raise NotImplementedError @@ -115,7 +118,7 @@ class YamlSimplifiedVariableSpeedCompressorTrain(YamlCompressorTrainBase): "Default false. Use with caution. This will increase runtime significantly.", title="CALCULATE_MAX_RATE", ) - fluid_model: str = Field(..., description="Reference to a fluid model", title="FLUID_MODEL") + fluid_model: FluidModelReference = Field(..., description="Reference to a fluid model", title="FLUID_MODEL") power_adjustment_constant: float = Field( 0.0, description="Constant to adjust power usage in MW", @@ -129,7 +132,9 @@ def to_dto(self): class YamlMultipleStreamsStream(YamlBase): type: Literal["INGOING", "OUTGOING"] name: str - fluid_model: str = Field(None, description="Reference to a fluid model", title="FLUID_MODEL") + fluid_model: Optional[FluidModelReference] = Field( + None, description="Reference to a fluid model", title="FLUID_MODEL" + ) class YamlVariableSpeedCompressorTrainMultipleStreamsAndPressures(YamlCompressorTrainBase): diff --git a/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_with_turbine.py b/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_with_turbine.py index 51c6dbd8d..3ba0a7dda 100644 --- a/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_with_turbine.py +++ b/src/libecalc/presentation/yaml/yaml_types/models/yaml_compressor_with_turbine.py @@ -3,13 +3,20 @@ from pydantic import Field from libecalc.presentation.yaml.yaml_types import YamlBase -from libecalc.presentation.yaml.yaml_types.models.model_reference import ModelReference +from libecalc.presentation.yaml.yaml_types.models.model_reference import ModelName +from libecalc.presentation.yaml.yaml_types.models.model_reference_validation import ( + TurbineModelReference, +) from libecalc.presentation.yaml.yaml_types.models.yaml_enums import YamlModelType +CompressorModelReference = str # Specific type is handled when referencing the CompressorWithTurbine type, since allowed compressor models varies between components. + class YamlCompressorWithTurbine(YamlBase): - compressor_model: str = Field(..., description="Reference to a compressor model", title="COMPRESSOR_MODEL") - name: ModelReference = Field( + compressor_model: CompressorModelReference = Field( + ..., description="Reference to a compressor model", title="COMPRESSOR_MODEL" + ) + name: ModelName = Field( ..., description="Name of the model. See documentation for more information.", title="NAME", @@ -19,7 +26,7 @@ class YamlCompressorWithTurbine(YamlBase): description="Constant to adjust power usage in MW", title="POWER_ADJUSTMENT_CONSTANT", ) - turbine_model: str = Field(..., description="Reference to a turbine model", title="TURBINE_MODEL") + turbine_model: TurbineModelReference = Field(..., description="Reference to a turbine model", title="TURBINE_MODEL") type: Literal[YamlModelType.COMPRESSOR_WITH_TURBINE] = Field( ..., description="Defines the type of model. See documentation for more information.", diff --git a/src/libecalc/presentation/yaml/yaml_types/models/yaml_fluid.py b/src/libecalc/presentation/yaml/yaml_types/models/yaml_fluid.py index 2d74782bc..83a24e632 100644 --- a/src/libecalc/presentation/yaml/yaml_types/models/yaml_fluid.py +++ b/src/libecalc/presentation/yaml/yaml_types/models/yaml_fluid.py @@ -5,7 +5,7 @@ from typing_extensions import Annotated from libecalc.presentation.yaml.yaml_types import YamlBase -from libecalc.presentation.yaml.yaml_types.models.model_reference import ModelReference +from libecalc.presentation.yaml.yaml_types.models.model_reference import ModelName from libecalc.presentation.yaml.yaml_types.models.yaml_enums import YamlModelType @@ -33,7 +33,7 @@ class YamlPredefinedFluidModel(YamlBase): eos_model: YamlEosModel = YamlEosModel.SRK fluid_model_type: Literal[YamlFluidModelType.PREDEFINED] = YamlFluidModelType.PREDEFINED gas_type: YamlPredefinedFluidType = None - name: ModelReference = Field( + name: ModelName = Field( ..., description="Name of the model. See documentation for more information.", title="NAME", @@ -69,7 +69,7 @@ class YamlCompositionFluidModel(YamlBase): ) eos_model: Optional[YamlEosModel] = YamlEosModel.SRK fluid_model_type: Literal[YamlFluidModelType.COMPOSITION] = YamlFluidModelType.COMPOSITION - name: ModelReference = Field( + name: ModelName = Field( ..., description="Name of the model. See documentation for more information.", title="NAME", diff --git a/src/libecalc/presentation/yaml/yaml_types/models/yaml_turbine.py b/src/libecalc/presentation/yaml/yaml_types/models/yaml_turbine.py index 728f8f237..1d7ee39f0 100644 --- a/src/libecalc/presentation/yaml/yaml_types/models/yaml_turbine.py +++ b/src/libecalc/presentation/yaml/yaml_types/models/yaml_turbine.py @@ -3,7 +3,7 @@ from pydantic import ConfigDict, Field from libecalc.presentation.yaml.yaml_types import YamlBase -from libecalc.presentation.yaml.yaml_types.models.model_reference import ModelReference +from libecalc.presentation.yaml.yaml_types.models.model_reference import ModelName from libecalc.presentation.yaml.yaml_types.models.yaml_enums import YamlModelType @@ -24,7 +24,7 @@ class YamlTurbine(YamlBase): }, ) - name: ModelReference = Field( + name: ModelName = Field( ..., description="Name of the model. See documentation for more information.", title="NAME", diff --git a/src/libecalc/presentation/yaml/yaml_validation_context.py b/src/libecalc/presentation/yaml/yaml_validation_context.py index 8c0e29fa7..ba8dbe5bf 100644 --- a/src/libecalc/presentation/yaml/yaml_validation_context.py +++ b/src/libecalc/presentation/yaml/yaml_validation_context.py @@ -1,9 +1,23 @@ -from typing import List, TypedDict +from typing import Dict, List, Protocol, TypedDict, Union + +ModelName = str + + +class Model(Protocol): + type: str + + +class CompressorWithTurbineModel(Model, Protocol): + compressor_model: ModelName + + +ModelContext = Union[Model, CompressorWithTurbineModel] class YamlModelValidationContextNames: resource_file_names = "resource_file_names" expression_tokens = "expression_tokens" + model_types = "model_types" YamlModelValidationContext = TypedDict( @@ -11,6 +25,7 @@ class YamlModelValidationContextNames: { YamlModelValidationContextNames.resource_file_names: List[str], # type: ignore YamlModelValidationContextNames.expression_tokens: List[str], + YamlModelValidationContextNames.model_types: Dict[ModelName, ModelContext], }, total=True, ) diff --git a/src/tests/libecalc/presentation/yaml/yaml_models/test_pyyaml_yaml_model.py b/src/tests/libecalc/presentation/yaml/yaml_models/test_pyyaml_yaml_model.py index db9c2cc7a..c1a19440c 100644 --- a/src/tests/libecalc/presentation/yaml/yaml_models/test_pyyaml_yaml_model.py +++ b/src/tests/libecalc/presentation/yaml/yaml_models/test_pyyaml_yaml_model.py @@ -20,6 +20,35 @@ def yaml_resource_with_errors(): return ResourceStream(name="yaml_with_errors", stream=StringIO(yaml_with_errors)) +@pytest.fixture() +def yaml_resource_with_invalid_model_reference(): + yaml_with_errors = """ + +TIME_SERIES: + - NAME: lol + +MODELS: + - NAME: fluid_model + TYPE: FLUID + FLUID_MODEL_TYPE: PREDEFINED + EOS_MODEL: SRK + GAS_TYPE: MEDIUM + +INSTALLATIONS: + - NAME: "installation1" + FUELCONSUMERS: + - NAME: tabular_not_existing_model + ENERGY_USAGE_MODEL: + TYPE: TABULATED + ENERGYFUNCTION: not_here + - NAME: tabular_invalid_model_reference + ENERGY_USAGE_MODEL: + TYPE: TABULATED + ENERGYFUNCTION: fluid_model + """ + return ResourceStream(name="yaml_with_invalid_model_reference", stream=StringIO(yaml_with_errors)) + + class TestYamlValidation: def test_pyyaml_validation(self, yaml_resource_with_errors): with pytest.raises(DtoValidationError) as exc_info: @@ -58,15 +87,39 @@ def test_expression_token_validation_ignored_if_no_context(self, minimal_model_y PyYamlYamlModel.read(ResourceStream(name=yaml_model.name, stream=StringIO(yaml_model.source))).validate({}) def test_valid_cases(self, valid_example_case_yaml_case): - PyYamlYamlModel.read( + yaml_model = PyYamlYamlModel.read( ResourceStream( name=valid_example_case_yaml_case.main_file_path.stem, stream=valid_example_case_yaml_case.main_file, ) - ).validate( + ) + yaml_model.validate( { YamlModelValidationContextNames.resource_file_names: list( valid_example_case_yaml_case.resources.keys() ), + YamlModelValidationContextNames.model_types: { + model.name: model for model in [*yaml_model.facility_inputs, *yaml_model.models] + }, } ) + + def test_invalid_model_reference(self, yaml_resource_with_invalid_model_reference): + yaml_model = PyYamlYamlModel.read(yaml_resource_with_invalid_model_reference) + with pytest.raises(DtoValidationError) as e: + yaml_model.validate( + { + YamlModelValidationContextNames.resource_file_names: [], + YamlModelValidationContextNames.model_types: { + model.name: model for model in [*yaml_model.facility_inputs, *yaml_model.models] + }, + } + ) + + errors = e.value.errors() + model_not_found_error = next(error for error in errors if error.message == "Model 'not_here' not found") + assert model_not_found_error.location.keys[:4] == ["INSTALLATIONS", 0, "FUELCONSUMERS", 0] + invalid_type_error = next( + error for error in errors if error.message == "Model 'fluid_model' with type FLUID is not allowed" + ) + assert invalid_type_error.location.keys[:4] == ["INSTALLATIONS", 0, "FUELCONSUMERS", 1] diff --git a/src/tests/libecalc/presentation/yaml/yaml_types/models/test_model_reference_validation.py b/src/tests/libecalc/presentation/yaml/yaml_types/models/test_model_reference_validation.py new file mode 100644 index 000000000..f4073cf5b --- /dev/null +++ b/src/tests/libecalc/presentation/yaml/yaml_types/models/test_model_reference_validation.py @@ -0,0 +1,41 @@ +from dataclasses import dataclass + +import pytest +from libecalc.presentation.yaml.yaml_types.models.model_reference_validation import ( + InvalidModelReferenceError, + ModelReferenceNotFound, + check_model_reference, +) +from libecalc.presentation.yaml.yaml_types.models.yaml_enums import YamlModelType +from libecalc.presentation.yaml.yaml_validation_context import Model + + +@dataclass +class Model(Model): + name: str + type: str + + +class TestCheckModelReference: + def test_not_found(self): + with pytest.raises(ModelReferenceNotFound): + check_model_reference("some_model", available_models={}, allowed_types=[]) + + def test_invalid_type(self): + allowed_type = "ALLOWED" + model_name = "valid_model" + with pytest.raises(InvalidModelReferenceError): + check_model_reference( + model_name, + available_models={model_name: Model(name=model_name, type="SOME_OTHER_TYPE")}, + allowed_types=[allowed_type], + ) + + def test_found_and_correct(self): + allowed_type = YamlModelType.FLUID + model_name = "valid_model" + assert check_model_reference( + model_name, + available_models={model_name: Model(name=model_name, type=allowed_type)}, + allowed_types=[allowed_type], + ) diff --git a/src/tests/libecalc/presentation/yaml/yaml_types/test_missing_files.py b/src/tests/libecalc/presentation/yaml/yaml_types/test_missing_files.py index 28a066836..b556491b3 100644 --- a/src/tests/libecalc/presentation/yaml/yaml_types/test_missing_files.py +++ b/src/tests/libecalc/presentation/yaml/yaml_types/test_missing_files.py @@ -1,5 +1,5 @@ import pytest -from libecalc.presentation.yaml.yaml_types.facility_type.yaml_facility_type import ( +from libecalc.presentation.yaml.yaml_types.facility_model.yaml_facility_model import ( YamlGeneratorSetModel, ) from libecalc.presentation.yaml.yaml_types.models.yaml_compressor_chart import (