From f6a0b5500d56abd96806acc04a28d34fc040eb7c Mon Sep 17 00:00:00 2001 From: Frode Helgetun Krogh <70878501+frodehk@users.noreply.github.com> Date: Mon, 6 Jan 2025 16:12:12 +0100 Subject: [PATCH] chore: remove base consumer and parents in dto equinor/ecalc-internal#291 --- .../domain/infrastructure/__init__.py | 1 - .../energy_components/base/component_dto.py | 98 +------------------ .../energy_components/common.py | 43 +++++++- .../compressor/component_dto.py | 62 +++++++++++- .../consumer_system/consumer_system_dto.py | 46 +++++++-- .../electricity_consumer.py | 21 +++- .../fuel_consumer/fuel_consumer.py | 56 ++++++++--- .../generator_set/generator_set_dto.py | 25 +++-- .../installation/installation.py | 5 +- .../legacy_consumer/component.py | 3 +- .../energy_components/pump/component_dto.py | 69 ++++++++++++- 11 files changed, 280 insertions(+), 149 deletions(-) diff --git a/src/libecalc/domain/infrastructure/__init__.py b/src/libecalc/domain/infrastructure/__init__.py index 72d305578f..d34322f6a3 100644 --- a/src/libecalc/domain/infrastructure/__init__.py +++ b/src/libecalc/domain/infrastructure/__init__.py @@ -1,5 +1,4 @@ from libecalc.domain.infrastructure.energy_components.asset.asset import Asset -from libecalc.domain.infrastructure.energy_components.base.component_dto import BaseConsumer from libecalc.domain.infrastructure.energy_components.electricity_consumer.electricity_consumer import ( ElectricityConsumer, ) diff --git a/src/libecalc/domain/infrastructure/energy_components/base/component_dto.py b/src/libecalc/domain/infrastructure/energy_components/base/component_dto.py index 6f3a808144..d071d884f3 100644 --- a/src/libecalc/domain/infrastructure/energy_components/base/component_dto.py +++ b/src/libecalc/domain/infrastructure/energy_components/base/component_dto.py @@ -1,106 +1,10 @@ from __future__ import annotations -from abc import ABC, abstractmethod from typing import Optional -from libecalc.common.component_type import ComponentType -from libecalc.common.consumption_type import ConsumptionType -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.domain.infrastructure.energy_components.component_validation_error import ( - ComponentValidationException, - ModelValidationError, -) -from libecalc.domain.infrastructure.energy_components.utils import _convert_keys_in_dictionary_from_str_to_periods -from libecalc.dto.fuel_type import FuelType -from libecalc.dto.types import ConsumerUserDefinedCategoryType -from libecalc.dto.utils.validators import ( - ExpressionType, - validate_temporal_model, -) -from libecalc.expression import Expression - - -class Component(ABC): - component_type: ComponentType - - @property - @abstractmethod - def id(self) -> str: ... - - -class BaseComponent(Component, ABC): - def __init__(self, name: str, regularity: dict[Period, Expression]): - self.name = name - self.regularity = self.check_regularity(regularity) - validate_temporal_model(self.regularity) - - @classmethod - def check_regularity(cls, regularity): - if isinstance(regularity, dict) and len(regularity.values()) > 0: - regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity) - return regularity - - -class BaseEquipment(BaseComponent, ABC): - def __init__( - self, - name: str, - regularity: dict[Period, Expression], - user_defined_category: dict[Period, ConsumerUserDefinedCategoryType], - component_type: ComponentType, - energy_usage_model: Optional[dict[Period, Expression]] = None, - fuel: Optional[dict[Period, FuelType]] = None, - ): - super().__init__(name, regularity) - self.user_defined_category = user_defined_category - self.energy_usage_model = energy_usage_model - self.component_type = component_type - self.fuel = fuel - - @property - def id(self) -> str: - return generate_id(self.name) - - -class BaseConsumer(BaseEquipment, ABC): - """Base class for all consumers.""" - - def __init__( - self, - name: str, - regularity: dict[Period, Expression], - consumes: ConsumptionType, - user_defined_category: dict[Period, ConsumerUserDefinedCategoryType], - component_type: ComponentType, - energy_usage_model: Optional[dict[Period, Expression]] = None, - fuel: Optional[dict[Period, FuelType]] = None, - ): - super().__init__(name, regularity, user_defined_category, component_type, energy_usage_model, fuel) - - self.fuel = self.validate_fuel_exist(name=self.name, fuel=fuel, consumes=consumes) - self.consumes = consumes - - @classmethod - def validate_fuel_exist(cls, name: str, fuel: Optional[dict[Period, FuelType]], consumes: ConsumptionType): - """ - Make sure fuel is set if consumption type is FUEL. - """ - if isinstance(fuel, dict) and len(fuel.values()) > 0: - fuel = _convert_keys_in_dictionary_from_str_to_periods(fuel) - if consumes == ConsumptionType.FUEL and (fuel is None or len(fuel) < 1): - msg = "Missing fuel for fuel consumer" - raise ComponentValidationException( - errors=[ - ModelValidationError( - name=name, - message=str(msg), - ) - ], - ) - return fuel +from libecalc.dto.utils.validators import ExpressionType class ExpressionTimeSeries: diff --git a/src/libecalc/domain/infrastructure/energy_components/common.py b/src/libecalc/domain/infrastructure/energy_components/common.py index c0ab6e4f37..770c328a2f 100644 --- a/src/libecalc/domain/infrastructure/energy_components/common.py +++ b/src/libecalc/domain/infrastructure/energy_components/common.py @@ -1,8 +1,10 @@ from typing import Literal, Optional, TypeVar, Union from libecalc.common.component_type import ComponentType +from libecalc.common.consumption_type import ConsumptionType +from libecalc.common.string.string_utils import generate_id +from libecalc.common.time_utils import Period from libecalc.domain.infrastructure.energy_components.asset.asset import Asset -from libecalc.domain.infrastructure.energy_components.base.component_dto import BaseConsumer from libecalc.domain.infrastructure.energy_components.compressor.component_dto import CompressorComponent from libecalc.domain.infrastructure.energy_components.consumer_system.consumer_system_dto import ConsumerSystem from libecalc.domain.infrastructure.energy_components.electricity_consumer.electricity_consumer import ( @@ -12,6 +14,8 @@ from libecalc.domain.infrastructure.energy_components.generator_set.generator_set_dto import GeneratorSet from libecalc.domain.infrastructure.energy_components.installation.installation import Installation from libecalc.domain.infrastructure.energy_components.pump.component_dto import PumpComponent +from libecalc.domain.infrastructure.energy_components.utils import _convert_keys_in_dictionary_from_str_to_periods +from libecalc.dto.utils.validators import validate_temporal_model from libecalc.expression import Expression Consumer = Union[FuelConsumer, ElectricityConsumer] @@ -55,7 +59,7 @@ def __init__(self, from_component_id: str, to_component_id: str, stream_name: Op ConsumerComponent = TypeVar("ConsumerComponent", bound=Union[CompressorComponent, PumpComponent]) -class TrainComponent(BaseConsumer): +class TrainComponent: component_type: Literal[ComponentType.TRAIN_V2] = ComponentType.TRAIN_V2 def __init__( @@ -68,6 +72,39 @@ def __init__( stages: list, streams: list, ): - super().__init__(name, regularity, consumes, user_defined_category, component_type) + self.name = name + self.regularity = self.check_regularity(regularity) + validate_temporal_model(self.regularity) + self.consumes = consumes + self.user_defined_category = user_defined_category + self.component_type = component_type self.stages = stages self.streams = streams + + @property + def id(self) -> str: + return generate_id(self.name) + + @staticmethod + def check_regularity(regularity: dict[Period, Expression]): + if isinstance(regularity, dict) and len(regularity.values()) > 0: + regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity) + return regularity + + def is_fuel_consumer(self) -> bool: + return self.consumes == ConsumptionType.FUEL + + def is_electricity_consumer(self) -> bool: + return self.consumes == ConsumptionType.ELECTRICITY + + def is_provider(self) -> bool: + return False + + def is_container(self) -> bool: + return False + + def get_component_process_type(self) -> ComponentType: + return self.component_type + + def get_name(self) -> str: + return self.name diff --git a/src/libecalc/domain/infrastructure/energy_components/compressor/component_dto.py b/src/libecalc/domain/infrastructure/energy_components/compressor/component_dto.py index 0935952051..b81c95ccea 100644 --- a/src/libecalc/domain/infrastructure/energy_components/compressor/component_dto.py +++ b/src/libecalc/domain/infrastructure/energy_components/compressor/component_dto.py @@ -1,16 +1,68 @@ -from typing import Literal +from typing import Literal, Optional from libecalc.application.energy.energy_component import EnergyComponent from libecalc.common.component_type import ComponentType from libecalc.common.consumption_type import ConsumptionType +from libecalc.common.string.string_utils import generate_id from libecalc.common.time_utils import Period -from libecalc.domain.infrastructure.energy_components.base.component_dto import BaseConsumer -from libecalc.dto.models.compressor import CompressorModel +from libecalc.domain.infrastructure.energy_components.component_validation_error import ( + ComponentValidationException, + ModelValidationError, +) +from libecalc.domain.infrastructure.energy_components.utils import _convert_keys_in_dictionary_from_str_to_periods +from libecalc.dto import FuelType +from libecalc.dto.types import ConsumerUserDefinedCategoryType +from libecalc.dto.utils.validators import validate_temporal_model +from libecalc.expression import Expression -class CompressorComponent(BaseConsumer, EnergyComponent): +class CompressorComponent(EnergyComponent): component_type: Literal[ComponentType.COMPRESSOR] = ComponentType.COMPRESSOR - energy_usage_model: dict[Period, CompressorModel] + + def __init__( + self, + name: str, + regularity: dict[Period, Expression], + consumes: ConsumptionType, + user_defined_category: dict[Period, ConsumerUserDefinedCategoryType], + energy_usage_model: Optional[dict[Period, Expression]] = None, + fuel: Optional[dict[Period, FuelType]] = None, + component_type: Optional[ComponentType] = ComponentType.COMPRESSOR, + ): + self.name = name + self.regularity = self.check_regularity(regularity) + validate_temporal_model(self.regularity) + self.user_defined_category = user_defined_category + self.energy_usage_model = energy_usage_model + self.fuel = self.validate_fuel_exist(name=self.name, fuel=fuel, consumes=consumes) + self.consumes = consumes + self.component_type = component_type + + @property + def id(self) -> str: + return generate_id(self.name) + + @staticmethod + def check_regularity(regularity): + if isinstance(regularity, dict) and len(regularity.values()) > 0: + regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity) + return regularity + + @staticmethod + def validate_fuel_exist(name: str, fuel: Optional[dict[Period, FuelType]], consumes: ConsumptionType): + if isinstance(fuel, dict) and len(fuel.values()) > 0: + fuel = _convert_keys_in_dictionary_from_str_to_periods(fuel) + if consumes == ConsumptionType.FUEL and (fuel is None or len(fuel) < 1): + msg = "Missing fuel for fuel consumer" + raise ComponentValidationException( + errors=[ + ModelValidationError( + name=name, + message=str(msg), + ) + ], + ) + return fuel def is_fuel_consumer(self) -> bool: return self.consumes == ConsumptionType.FUEL diff --git a/src/libecalc/domain/infrastructure/energy_components/consumer_system/consumer_system_dto.py b/src/libecalc/domain/infrastructure/energy_components/consumer_system/consumer_system_dto.py index 674db9df33..8a5d7d1474 100644 --- a/src/libecalc/domain/infrastructure/energy_components/consumer_system/consumer_system_dto.py +++ b/src/libecalc/domain/infrastructure/energy_components/consumer_system/consumer_system_dto.py @@ -25,7 +25,6 @@ from libecalc.core.result import ComponentResult, EcalcModelResult from libecalc.core.result.emission import EmissionResult from libecalc.domain.infrastructure.energy_components.base.component_dto import ( - BaseConsumer, ConsumerID, PriorityID, SystemComponentConditions, @@ -43,13 +42,15 @@ from libecalc.domain.infrastructure.energy_components.fuel_model.fuel_model import FuelModel from libecalc.domain.infrastructure.energy_components.pump import Pump from libecalc.domain.infrastructure.energy_components.pump.component_dto import PumpComponent +from libecalc.domain.infrastructure.energy_components.utils import _convert_keys_in_dictionary_from_str_to_periods from libecalc.dto import FuelType from libecalc.dto.component_graph import ComponentGraph from libecalc.dto.types import ConsumerUserDefinedCategoryType +from libecalc.dto.utils.validators import validate_temporal_model from libecalc.expression import Expression -class ConsumerSystem(BaseConsumer, Emitter, EnergyComponent): +class ConsumerSystem(Emitter, EnergyComponent): def __init__( self, name: str, @@ -62,17 +63,42 @@ def __init__( fuel: Optional[dict[Period, FuelType]] = None, component_type: Literal[ComponentType.CONSUMER_SYSTEM_V2] = ComponentType.CONSUMER_SYSTEM_V2, ): - super().__init__( - component_type=component_type, - name=name, - user_defined_category=user_defined_category, - regularity=regularity, - consumes=consumes, - fuel=fuel, - ) + self.name = name + self.user_defined_category = user_defined_category + self.regularity = self.check_regularity(regularity) + self.consumes = consumes + validate_temporal_model(self.regularity) self.component_conditions = component_conditions self.stream_conditions_priorities = stream_conditions_priorities self.consumers = consumers + self.fuel = self.validate_fuel_exist(name=self.name, fuel=fuel, consumes=consumes) + self.component_type = component_type + + @property + def id(self) -> str: + return generate_id(self.name) + + @staticmethod + def check_regularity(regularity): + if isinstance(regularity, dict) and len(regularity.values()) > 0: + regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity) + return regularity + + @staticmethod + def validate_fuel_exist(name: str, fuel: Optional[dict[Period, FuelType]], consumes: ConsumptionType): + if isinstance(fuel, dict) and len(fuel.values()) > 0: + fuel = _convert_keys_in_dictionary_from_str_to_periods(fuel) + if consumes == ConsumptionType.FUEL and (fuel is None or len(fuel) < 1): + msg = "Missing fuel for fuel consumer" + raise ComponentValidationException( + errors=[ + ModelValidationError( + name=name, + message=str(msg), + ) + ], + ) + return fuel def is_fuel_consumer(self) -> bool: return self.consumes == ConsumptionType.FUEL diff --git a/src/libecalc/domain/infrastructure/energy_components/electricity_consumer/electricity_consumer.py b/src/libecalc/domain/infrastructure/energy_components/electricity_consumer/electricity_consumer.py index b4cb926c2c..032cfbe4c2 100644 --- a/src/libecalc/domain/infrastructure/energy_components/electricity_consumer/electricity_consumer.py +++ b/src/libecalc/domain/infrastructure/energy_components/electricity_consumer/electricity_consumer.py @@ -5,11 +5,11 @@ 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.string.string_utils import generate_id from libecalc.common.temporal_model import TemporalModel from libecalc.common.time_utils import Period from libecalc.common.variables import ExpressionEvaluator from libecalc.core.result import EcalcModelResult -from libecalc.domain.infrastructure.energy_components.base.component_dto import BaseConsumer from libecalc.domain.infrastructure.energy_components.legacy_consumer.component import ( Consumer as ConsumerEnergyComponent, ) @@ -24,7 +24,7 @@ from libecalc.expression import Expression -class ElectricityConsumer(BaseConsumer, EnergyComponent): +class ElectricityConsumer(EnergyComponent): def __init__( self, name: str, @@ -40,10 +40,25 @@ def __init__( energy_usage_model: dict[Period, ElectricEnergyUsageModel], consumes: Literal[ConsumptionType.ELECTRICITY] = ConsumptionType.ELECTRICITY, ): - super().__init__(name, regularity, consumes, user_defined_category, component_type, energy_usage_model, None) + self.name = name + self.regularity = self.check_regularity(regularity) + validate_temporal_model(self.regularity) + self.user_defined_category = user_defined_category self.energy_usage_model = self.check_energy_usage_model(energy_usage_model) self._validate_el_consumer_temporal_model(self.energy_usage_model) self._check_model_energy_usage(self.energy_usage_model) + self.consumes = consumes + self.component_type = component_type + + @property + def id(self) -> str: + return generate_id(self.name) + + @staticmethod + def check_regularity(regularity): + if isinstance(regularity, dict) and len(regularity.values()) > 0: + regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity) + return regularity def is_fuel_consumer(self) -> bool: return False diff --git a/src/libecalc/domain/infrastructure/energy_components/fuel_consumer/fuel_consumer.py b/src/libecalc/domain/infrastructure/energy_components/fuel_consumer/fuel_consumer.py index fc1540f349..85d73e254c 100644 --- a/src/libecalc/domain/infrastructure/energy_components/fuel_consumer/fuel_consumer.py +++ b/src/libecalc/domain/infrastructure/energy_components/fuel_consumer/fuel_consumer.py @@ -7,12 +7,16 @@ 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.string.string_utils import generate_id from libecalc.common.temporal_model import TemporalModel from libecalc.common.time_utils import Period from libecalc.common.variables import ExpressionEvaluator from libecalc.core.result import EcalcModelResult from libecalc.core.result.emission import EmissionResult -from libecalc.domain.infrastructure.energy_components.base.component_dto import BaseConsumer +from libecalc.domain.infrastructure.energy_components.component_validation_error import ( + ComponentValidationException, + ModelValidationError, +) from libecalc.domain.infrastructure.energy_components.fuel_model.fuel_model import FuelModel from libecalc.domain.infrastructure.energy_components.legacy_consumer.component import ( Consumer as ConsumerEnergyComponent, @@ -29,7 +33,7 @@ from libecalc.expression import Expression -class FuelConsumer(BaseConsumer, Emitter, EnergyComponent): +class FuelConsumer(Emitter, EnergyComponent): def __init__( self, name: str, @@ -44,11 +48,26 @@ def __init__( energy_usage_model: dict[Period, FuelEnergyUsageModel], consumes: Literal[ConsumptionType.FUEL] = ConsumptionType.FUEL, ): - super().__init__(name, regularity, consumes, user_defined_category, component_type, energy_usage_model, fuel) - self.fuel = self.check_fuel(fuel) + self.name = name + self.regularity = self.check_regularity(regularity) + validate_temporal_model(self.regularity) + self.user_defined_category = user_defined_category self.energy_usage_model = self.check_energy_usage_model(energy_usage_model) + self.fuel = self.validate_fuel_exist(name=self.name, fuel=fuel, consumes=consumes) self._validate_fuel_consumer_temporal_models(self.energy_usage_model, self.fuel) self._check_model_energy_usage(self.energy_usage_model) + self.consumes = consumes + self.component_type = component_type + + @property + def id(self) -> str: + return generate_id(self.name) + + @staticmethod + def check_regularity(regularity): + if isinstance(regularity, dict) and len(regularity.values()) > 0: + regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity) + return regularity def is_fuel_consumer(self) -> bool: return True @@ -114,15 +133,6 @@ def check_energy_usage_model(energy_usage_model: dict[Period, FuelEnergyUsageMod energy_usage_model = _convert_keys_in_dictionary_from_str_to_periods(energy_usage_model) return energy_usage_model - @staticmethod - def check_fuel(fuel: dict[Period, FuelType]): - """ - Make sure that temporal models are converted to Period objects if they are strings - """ - if isinstance(fuel, dict) and len(fuel.values()) > 0: - fuel = _convert_keys_in_dictionary_from_str_to_periods(fuel) - return fuel - @staticmethod def _validate_fuel_consumer_temporal_models( energy_usage_model: dict[Period, FuelEnergyUsageModel], fuel: dict[Period, FuelType] @@ -133,3 +143,23 @@ def _validate_fuel_consumer_temporal_models( @staticmethod def _check_model_energy_usage(energy_usage_model: dict[Period, FuelEnergyUsageModel]): check_model_energy_usage_type(energy_usage_model, EnergyUsageType.FUEL) + + @classmethod + def validate_fuel_exist(cls, name: str, fuel: Optional[dict[Period, FuelType]], consumes: ConsumptionType): + """ + Make sure that temporal models are converted to Period objects if they are strings, + and that fuel is set if consumption type is FUEL. + """ + if isinstance(fuel, dict) and len(fuel.values()) > 0: + fuel = _convert_keys_in_dictionary_from_str_to_periods(fuel) + if consumes == ConsumptionType.FUEL and (fuel is None or len(fuel) < 1): + msg = "Missing fuel for fuel consumer" + raise ComponentValidationException( + errors=[ + ModelValidationError( + name=name, + message=str(msg), + ) + ], + ) + return fuel diff --git a/src/libecalc/domain/infrastructure/energy_components/generator_set/generator_set_dto.py b/src/libecalc/domain/infrastructure/energy_components/generator_set/generator_set_dto.py index e54f7698ab..c93b4d98f9 100644 --- a/src/libecalc/domain/infrastructure/energy_components/generator_set/generator_set_dto.py +++ b/src/libecalc/domain/infrastructure/energy_components/generator_set/generator_set_dto.py @@ -5,13 +5,13 @@ from libecalc.application.energy.energy_component import EnergyComponent from libecalc.application.energy.energy_model import EnergyModel from libecalc.common.component_type import ComponentType +from libecalc.common.string.string_utils import generate_id from libecalc.common.temporal_model import TemporalModel from libecalc.common.time_utils import Period from libecalc.common.variables import ExpressionEvaluator from libecalc.core.models.generator import GeneratorModelSampled from libecalc.core.result import EcalcModelResult from libecalc.core.result.emission import EmissionResult -from libecalc.domain.infrastructure.energy_components.base.component_dto import BaseEquipment from libecalc.domain.infrastructure.energy_components.component_validation_error import ( ComponentValidationException, ModelValidationError, @@ -35,7 +35,7 @@ from libecalc.expression import Expression -class GeneratorSet(BaseEquipment, Emitter, EnergyComponent): +class GeneratorSet(Emitter, EnergyComponent): def __init__( self, name: str, @@ -48,13 +48,10 @@ def __init__( max_usage_from_shore: Optional[ExpressionType] = None, component_type: Literal[ComponentType.GENERATOR_SET] = ComponentType.GENERATOR_SET, ): - super().__init__( - name, - regularity, - user_defined_category, - component_type, - fuel=fuel, - ) + self.name = name + self.user_defined_category = user_defined_category + self.regularity = self.check_regularity(regularity) + validate_temporal_model(self.regularity) self.generator_set_model = self.check_generator_set_model(generator_set_model) self.fuel = self.check_fuel(fuel) self.consumers = consumers if consumers is not None else [] @@ -64,6 +61,16 @@ def __init__( self._validate_genset_temporal_models(self.generator_set_model, self.fuel) self.check_consumers() + @property + def id(self) -> str: + return generate_id(self.name) + + @staticmethod + def check_regularity(regularity): + if isinstance(regularity, dict) and len(regularity.values()) > 0: + regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity) + return regularity + def is_fuel_consumer(self) -> bool: return True diff --git a/src/libecalc/domain/infrastructure/energy_components/installation/installation.py b/src/libecalc/domain/infrastructure/energy_components/installation/installation.py index 2a4d00e30d..42bdbb777f 100644 --- a/src/libecalc/domain/infrastructure/energy_components/installation/installation.py +++ b/src/libecalc/domain/infrastructure/energy_components/installation/installation.py @@ -4,7 +4,6 @@ from libecalc.common.component_type import ComponentType from libecalc.common.string.string_utils import generate_id from libecalc.common.time_utils import Period -from libecalc.domain.infrastructure.energy_components.base.component_dto import BaseComponent from libecalc.domain.infrastructure.energy_components.consumer_system.consumer_system_dto import ConsumerSystem from libecalc.domain.infrastructure.energy_components.fuel_consumer.fuel_consumer import FuelConsumer from libecalc.domain.infrastructure.energy_components.generator_set.generator_set_dto import GeneratorSet @@ -20,7 +19,7 @@ ) -class Installation(BaseComponent, EnergyComponent): +class Installation(EnergyComponent): def __init__( self, name: str, @@ -30,7 +29,7 @@ def __init__( venting_emitters: Optional[list[YamlVentingEmitter]] = None, user_defined_category: Optional[InstallationUserDefinedCategoryType] = None, ): - super().__init__(name, regularity) + self.name = name self.hydrocarbon_export = self.convert_expression_installation(hydrocarbon_export) self.regularity = self.convert_expression_installation(regularity) self.fuel_consumers = fuel_consumers diff --git a/src/libecalc/domain/infrastructure/energy_components/legacy_consumer/component.py b/src/libecalc/domain/infrastructure/energy_components/legacy_consumer/component.py index 593176cdda..77e07a7dfc 100644 --- a/src/libecalc/domain/infrastructure/energy_components/legacy_consumer/component.py +++ b/src/libecalc/domain/infrastructure/energy_components/legacy_consumer/component.py @@ -26,7 +26,6 @@ GenericComponentResult, PumpResult, ) -from libecalc.domain.infrastructure.energy_components.base import BaseConsumer from libecalc.domain.infrastructure.energy_components.legacy_consumer.consumer_function import ( ConsumerFunction, ConsumerFunctionResult, @@ -56,7 +55,7 @@ def get_operational_settings_used_from_consumer_result( ConsumerResult = Union[ConsumerSystemResult, PumpResult, CompressorResult] -class Consumer(BaseConsumer): +class Consumer: def __init__( self, id: str, diff --git a/src/libecalc/domain/infrastructure/energy_components/pump/component_dto.py b/src/libecalc/domain/infrastructure/energy_components/pump/component_dto.py index d425ca53f0..78947f1c86 100644 --- a/src/libecalc/domain/infrastructure/energy_components/pump/component_dto.py +++ b/src/libecalc/domain/infrastructure/energy_components/pump/component_dto.py @@ -1,17 +1,61 @@ -from typing import Literal +from typing import Literal, Optional from libecalc.application.energy.energy_component import EnergyComponent from libecalc.common.component_type import ComponentType from libecalc.common.consumption_type import ConsumptionType +from libecalc.common.string.string_utils import generate_id from libecalc.common.time_utils import Period -from libecalc.domain.infrastructure.energy_components.base.component_dto import BaseConsumer +from libecalc.domain.infrastructure.energy_components.component_validation_error import ( + ComponentValidationException, + ModelValidationError, +) +from libecalc.domain.infrastructure.energy_components.utils import _convert_keys_in_dictionary_from_str_to_periods +from libecalc.dto import FuelType from libecalc.dto.models.pump import PumpModel +from libecalc.dto.types import ConsumerUserDefinedCategoryType +from libecalc.dto.utils.validators import validate_temporal_model +from libecalc.expression import Expression -class PumpComponent(BaseConsumer, EnergyComponent): +class PumpComponent(EnergyComponent): component_type: Literal[ComponentType.PUMP] = ComponentType.PUMP energy_usage_model: dict[Period, PumpModel] + def __init__( + self, + name: str, + regularity: dict[Period, Expression], + user_defined_category: dict[Period, ConsumerUserDefinedCategoryType], + component_type: ComponentType, + energy_usage_model: dict[Period, PumpModel], + consumes: Literal[ConsumptionType.FUEL, ConsumptionType.ELECTRICITY], + fuel: Optional[dict[Period, FuelType]] = None, + ): + self.name = name + self.regularity = self.check_regularity(regularity) + validate_temporal_model(self.regularity) + self.user_defined_category = user_defined_category + self.component_type = component_type + self.energy_usage_model = self.check_energy_usage_model(energy_usage_model) + self.fuel = self.validate_fuel_exist(name=self.name, fuel=fuel, consumes=consumes) + self.consumes = consumes + + @property + def id(self) -> str: + return generate_id(self.name) + + @staticmethod + def check_regularity(regularity: dict[Period, Expression]): + if isinstance(regularity, dict) and len(regularity.values()) > 0: + regularity = _convert_keys_in_dictionary_from_str_to_periods(regularity) + return regularity + + @staticmethod + def check_energy_usage_model(energy_usage_model: dict[Period, PumpModel]): + if isinstance(energy_usage_model, dict) and len(energy_usage_model.values()) > 0: + energy_usage_model = _convert_keys_in_dictionary_from_str_to_periods(energy_usage_model) + return energy_usage_model + def is_fuel_consumer(self) -> bool: return self.consumes == ConsumptionType.FUEL @@ -29,3 +73,22 @@ def get_component_process_type(self) -> ComponentType: def get_name(self) -> str: return self.name + + @classmethod + def validate_fuel_exist(cls, name: str, fuel: Optional[dict[Period, FuelType]], consumes: ConsumptionType): + """ + Make sure fuel is set if consumption type is FUEL. + """ + if isinstance(fuel, dict) and len(fuel.values()) > 0: + fuel = _convert_keys_in_dictionary_from_str_to_periods(fuel) + if consumes == ConsumptionType.FUEL and (fuel is None or len(fuel) < 1): + msg = "Missing fuel for fuel consumer" + raise ComponentValidationException( + errors=[ + ModelValidationError( + name=name, + message=str(msg), + ) + ], + ) + return fuel