Skip to content

Commit 2fd78d6

Browse files
committed
refactor: remove double mapping pump model
1 parent d6f019b commit 2fd78d6

7 files changed

Lines changed: 61 additions & 167 deletions

File tree

src/libecalc/domain/process/pump/factory.py

Lines changed: 0 additions & 67 deletions
This file was deleted.

src/libecalc/domain/process/pump/pump.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
from __future__ import annotations
22

33
from abc import abstractmethod
4-
from typing import Literal
54

65
import numpy as np
76
from numpy.typing import NDArray
8-
from pydantic import ConfigDict
9-
from pydantic.dataclasses import dataclass
107
from scipy.interpolate import interp1d
118

12-
from libecalc.common.energy_model_type import EnergyModelType
139
from libecalc.common.list.adjustment import transform_linear
1410
from libecalc.common.logger import logger
15-
from libecalc.common.serializable_chart import SingleSpeedChartDTO, VariableSpeedChartDTO
1611
from libecalc.common.units import Unit, UnitConstants
1712
from libecalc.domain.process.core.results import PumpModelResult
1813
from libecalc.domain.process.value_objects.chart import SingleSpeedChart, VariableSpeedChart
@@ -90,15 +85,6 @@ def _calculate_power(
9085
)
9186

9287

93-
@dataclass(config=ConfigDict(arbitrary_types_allowed=True))
94-
class PumpModelDTO:
95-
energy_usage_adjustment_constant: float
96-
energy_usage_adjustment_factor: float
97-
chart: SingleSpeedChartDTO | VariableSpeedChartDTO
98-
head_margin: float
99-
typ: Literal[EnergyModelType.PUMP_MODEL] = EnergyModelType.PUMP_MODEL
100-
101-
10288
class PumpSingleSpeed(PumpModel):
10389
def __init__(
10490
self,

src/libecalc/presentation/yaml/domain/reference_service.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from libecalc.domain.infrastructure.energy_components.generator_set import GeneratorSetModel
55
from libecalc.domain.infrastructure.energy_components.legacy_consumer.tabulated import TabularEnergyFunction
66
from libecalc.domain.process.compressor.dto.model_types import CompressorModelTypes
7-
from libecalc.domain.process.pump.pump import PumpModelDTO
7+
from libecalc.domain.process.pump.pump import PumpModel
88
from libecalc.dto import FuelType
99

1010

@@ -24,6 +24,6 @@ def get_generator_set_model(self, reference: str) -> GeneratorSetModel: ...
2424

2525
def get_compressor_model(self, reference: str) -> CompressorModelTypes: ...
2626

27-
def get_pump_model(self, reference: str) -> PumpModelDTO: ...
27+
def get_pump_model(self, reference: str) -> PumpModel: ...
2828

2929
def get_tabulated_model(self, reference: str) -> TabularEnergyFunction: ...

src/libecalc/presentation/yaml/mappers/consumer_function_mapper.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
VariableSpeedCompressorTrainMultipleStreamsAndPressures,
4141
)
4242
from libecalc.domain.process.compressor.dto.model_types import CompressorModelTypes
43-
from libecalc.domain.process.pump.factory import create_pump_model
4443
from libecalc.domain.regularity import Regularity
4544
from libecalc.domain.time_series_flow_rate import TimeSeriesFlowRate
4645
from libecalc.domain.time_series_variable import TimeSeriesVariable
@@ -300,7 +299,7 @@ def _map_tabular(
300299
def _map_pump(
301300
self, model: YamlEnergyUsageModelPump, consumes: ConsumptionType, period: Period
302301
) -> PumpConsumerFunction:
303-
energy_model = self.__references.get_pump_model(model.energy_function)
302+
pump_model = self.__references.get_pump_model(model.energy_function)
304303
period_regularity, period_evaluator = self._period_subsets[period]
305304
if consumes != ConsumptionType.ELECTRICITY:
306305
raise InvalidConsumptionType(actual=ConsumptionType.ELECTRICITY, expected=consumes)
@@ -333,7 +332,6 @@ def _map_pump(
333332
)
334333
discharge_pressure = ExpressionTimeSeriesPressure(time_series_expression=discharge_pressure_expression)
335334

336-
pump_model = create_pump_model(pump_model_dto=energy_model)
337335
return PumpConsumerFunction(
338336
power_loss_factor=power_loss_factor,
339337
pump_function=pump_model,
@@ -631,7 +629,7 @@ def _map_pump_system(
631629
pumps = []
632630
for pump in model.pumps:
633631
pump_model = self.__references.get_pump_model(pump.chart)
634-
pumps.append(ConsumerSystemComponent(name=pump.name, facility_model=create_pump_model(pump_model)))
632+
pumps.append(ConsumerSystemComponent(name=pump.name, facility_model=pump_model))
635633

636634
operational_settings: list[ConsumerSystemOperationalSettingExpressions] = []
637635
for operational_setting in model.operational_settings:

src/libecalc/presentation/yaml/mappers/facility_input.py

Lines changed: 45 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
from libecalc.domain.infrastructure.energy_components.legacy_consumer.tabulated import TabularEnergyFunction
1313
from libecalc.domain.process.compressor.dto import CompressorSampled as CompressorTrainSampledDTO
1414
from libecalc.domain.process.dto import EnergyModel
15-
from libecalc.domain.process.pump.pump import PumpModelDTO
15+
from libecalc.domain.process.pump.pump import PumpSingleSpeed, PumpVariableSpeed
16+
from libecalc.domain.process.value_objects.chart import SingleSpeedChart, VariableSpeedChart
1617
from libecalc.domain.resource import Resource, Resources
1718
from libecalc.presentation.yaml.file_context import FileContext, FileMark
1819
from libecalc.presentation.yaml.mappers.energy_model_factory import EnergyModelFactory
@@ -116,64 +117,62 @@ def _create_compressor_train_sampled_dto_model_data(
116117

117118
def _create_pump_model_single_speed_dto_model_data(
118119
resource: Resource, facility_data: YamlPumpChartSingleSpeed, **kwargs
119-
) -> PumpModelDTO:
120+
) -> PumpSingleSpeed:
120121
chart_data = get_single_speed_chart_data(resource=resource)
121-
122-
chart = SingleSpeedChartDTO(
123-
speed_rpm=chart_data.speed,
124-
efficiency_fraction=convert_efficiency_to_fraction(
125-
efficiency_values=chart_data.efficiency,
126-
input_unit=YAML_UNIT_MAPPING[facility_data.units.efficiency],
127-
),
128-
rate_actual_m3_hour=convert_rate_to_am3_per_hour(
129-
rate_values=chart_data.rate, input_unit=YAML_UNIT_MAPPING[facility_data.units.rate]
130-
),
131-
polytropic_head_joule_per_kg=convert_head_to_joule_per_kg(
132-
head_values=chart_data.head, input_unit=YAML_UNIT_MAPPING[facility_data.units.head]
133-
),
134-
)
135-
136-
head_margin = facility_data.head_margin
137-
138-
return PumpModelDTO(
139-
chart=chart,
140-
energy_usage_adjustment_constant=_get_adjustment_constant(facility_data),
141-
energy_usage_adjustment_factor=_get_adjustment_factor(facility_data),
142-
head_margin=head_margin,
143-
)
144-
145-
146-
def _create_pump_chart_variable_speed_dto_model_data(
147-
resource: Resource, facility_data: YamlPumpChartVariableSpeed, **kwargs
148-
) -> PumpModelDTO:
149-
curves_data = chart_curves_as_resource_to_dto_format(resource=resource)
150-
151-
curves: list[ChartCurveDTO] = [
152-
ChartCurveDTO(
153-
speed_rpm=curve.speed,
122+
pump_chart = SingleSpeedChart(
123+
SingleSpeedChartDTO(
124+
speed_rpm=chart_data.speed,
154125
rate_actual_m3_hour=convert_rate_to_am3_per_hour(
155-
rate_values=curve.rate,
156-
input_unit=YAML_UNIT_MAPPING[facility_data.units.rate],
126+
rate_values=chart_data.rate, input_unit=YAML_UNIT_MAPPING[facility_data.units.rate]
157127
),
158128
polytropic_head_joule_per_kg=convert_head_to_joule_per_kg(
159-
head_values=curve.head,
160-
input_unit=YAML_UNIT_MAPPING[facility_data.units.head],
129+
head_values=chart_data.head, input_unit=YAML_UNIT_MAPPING[facility_data.units.head]
161130
),
162131
efficiency_fraction=convert_efficiency_to_fraction(
163-
efficiency_values=curve.efficiency,
132+
efficiency_values=chart_data.efficiency,
164133
input_unit=YAML_UNIT_MAPPING[facility_data.units.efficiency],
165134
),
166135
)
167-
for curve in curves_data
168-
]
136+
)
137+
return PumpSingleSpeed(
138+
pump_chart=pump_chart,
139+
energy_usage_adjustment_constant=_get_adjustment_constant(facility_data),
140+
energy_usage_adjustment_factor=_get_adjustment_factor(facility_data),
141+
head_margin=facility_data.head_margin,
142+
)
169143

170-
head_margin = facility_data.head_margin
171144

172-
return PumpModelDTO(
173-
chart=VariableSpeedChartDTO(curves=curves),
145+
def _create_pump_chart_variable_speed_dto_model_data(
146+
resource: Resource, facility_data: YamlPumpChartVariableSpeed, **kwargs
147+
) -> PumpVariableSpeed:
148+
curves_data = chart_curves_as_resource_to_dto_format(resource=resource)
149+
pump_chart = VariableSpeedChart(
150+
VariableSpeedChartDTO(
151+
curves=[
152+
ChartCurveDTO(
153+
speed_rpm=curve.speed,
154+
rate_actual_m3_hour=convert_rate_to_am3_per_hour(
155+
rate_values=curve.rate,
156+
input_unit=YAML_UNIT_MAPPING[facility_data.units.rate],
157+
),
158+
polytropic_head_joule_per_kg=convert_head_to_joule_per_kg(
159+
head_values=curve.head,
160+
input_unit=YAML_UNIT_MAPPING[facility_data.units.head],
161+
),
162+
efficiency_fraction=convert_efficiency_to_fraction(
163+
efficiency_values=curve.efficiency,
164+
input_unit=YAML_UNIT_MAPPING[facility_data.units.efficiency],
165+
),
166+
)
167+
for curve in curves_data
168+
]
169+
)
170+
)
171+
return PumpVariableSpeed(
172+
pump_chart=pump_chart,
174173
energy_usage_adjustment_constant=_get_adjustment_constant(facility_data),
175174
energy_usage_adjustment_factor=_get_adjustment_factor(facility_data),
176-
head_margin=head_margin,
175+
head_margin=facility_data.head_margin,
177176
)
178177

179178

src/libecalc/presentation/yaml/yaml_entities.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from libecalc.domain.infrastructure.energy_components.legacy_consumer.tabulated import TabularEnergyFunction
2020
from libecalc.domain.process.compressor.dto.model_types import CompressorModelTypes
2121
from libecalc.domain.process.dto.base import EnergyModel
22-
from libecalc.domain.process.pump.pump import PumpModelDTO
22+
from libecalc.domain.process.pump.pump import PumpModel
2323
from libecalc.domain.resource import Resource
2424
from libecalc.dto import FuelType
2525
from libecalc.presentation.yaml.domain.reference_service import InvalidReferenceException, ReferenceService
@@ -197,9 +197,9 @@ def get_compressor_model(self, reference: str) -> CompressorModelTypes:
197197
raise InvalidReferenceException("compressor model", reference)
198198
return model # noqa
199199

200-
def get_pump_model(self, reference: str) -> PumpModelDTO:
200+
def get_pump_model(self, reference: str) -> PumpModel:
201201
model = self._get_model_reference(reference, "compressor model")
202-
if not isinstance(model, PumpModelDTO):
202+
if not isinstance(model, PumpModel):
203203
raise InvalidReferenceException("pump model", reference)
204204
return model
205205

tests/libecalc/input/mappers/test_consumer_chart.py

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
from libecalc.common.errors.exceptions import InvalidResourceException
44
from libecalc.common.serializable_chart import SingleSpeedChartDTO
55
from libecalc.common.units import Unit
6-
from libecalc.domain.process.pump.pump import PumpModelDTO
6+
from libecalc.domain.process.pump.pump import PumpModel, PumpSingleSpeed, PumpVariableSpeed
7+
from libecalc.domain.process.value_objects.chart import SingleSpeedChart
78
from libecalc.presentation.yaml.mappers.facility_input import (
89
_create_pump_model_single_speed_dto_model_data,
910
)
@@ -86,44 +87,21 @@ def pump_chart():
8687
class TestSingleSpeedChart:
8788
def test_valid_with_speed(self, pump_chart, chart_resource_with_speed):
8889
"""Test that speed can be specified. Note: 1.0 and 1 is considered equal."""
89-
pump_model_dto = _create_pump_model_single_speed_dto_model_data(
90+
pump_model = _create_pump_model_single_speed_dto_model_data(
9091
resource=chart_resource_with_speed,
9192
facility_data=pump_chart,
9293
)
93-
assert pump_model_dto == PumpModelDTO(
94-
chart=SingleSpeedChartDTO(
95-
rate_actual_m3_hour=[6.0, 6.0],
96-
polytropic_head_joule_per_kg=[
97-
Unit.POLYTROPIC_HEAD_METER_LIQUID_COLUMN.to(Unit.POLYTROPIC_HEAD_JOULE_PER_KG)(x)
98-
for x in [7.0, 7.0]
99-
],
100-
efficiency_fraction=[0.08, 0.08],
101-
speed_rpm=5.0,
102-
),
103-
energy_usage_adjustment_factor=1,
104-
energy_usage_adjustment_constant=0,
105-
head_margin=0,
106-
)
94+
95+
assert pump_model.pump_chart.speed == 5.0
10796

10897
def test_valid_without_speed(self, pump_chart, chart_resource_without_speed):
109-
pump_model_dto = _create_pump_model_single_speed_dto_model_data(
98+
pump_model = _create_pump_model_single_speed_dto_model_data(
11099
resource=chart_resource_without_speed,
111100
facility_data=pump_chart,
112101
)
113-
assert pump_model_dto == PumpModelDTO(
114-
chart=SingleSpeedChartDTO(
115-
rate_actual_m3_hour=[6.0, 6.0],
116-
polytropic_head_joule_per_kg=[
117-
Unit.POLYTROPIC_HEAD_METER_LIQUID_COLUMN.to(Unit.POLYTROPIC_HEAD_JOULE_PER_KG)(x)
118-
for x in [7.0, 7.0]
119-
],
120-
efficiency_fraction=[0.08, 0.08],
121-
speed_rpm=1,
122-
),
123-
energy_usage_adjustment_constant=0,
124-
energy_usage_adjustment_factor=1,
125-
head_margin=0,
126-
)
102+
103+
# Speed set to 1.0 if header not found
104+
assert pump_model.pump_chart.speed == 1.0
127105

128106
def test_invalid_unequal_speed(self, pump_chart, chart_resource_unequal_speed):
129107
with pytest.raises(InvalidResourceException) as exception_info:

0 commit comments

Comments
 (0)