Skip to content

Commit 3dbd515

Browse files
authored
refactor: remove compressor stage dto (#1133)
* refactor: remove compressor stage dto
1 parent 5bd0ad3 commit 3dbd515

17 files changed

Lines changed: 727 additions & 907 deletions

src/libecalc/domain/process/compressor/core/utils.py

Lines changed: 0 additions & 78 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
from .stage import CompressorStage, InterstagePressureControl
1+
from .stage import InterstagePressureControl
Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from libecalc.common.fixed_speed_pressure_control import FixedSpeedPressureControl
2-
from libecalc.domain.process.value_objects.chart.compressor.compressor_chart_dto import CompressorChart
32

43

54
class InterstagePressureControl:
@@ -10,32 +9,3 @@ def __init__(
109
):
1110
self.upstream_pressure_control = upstream_pressure_control
1211
self.downstream_pressure_control = downstream_pressure_control
13-
14-
15-
class CompressorStage:
16-
"""Special case for multiple streams model."""
17-
18-
def __init__(
19-
self,
20-
compressor_chart: CompressorChart,
21-
inlet_temperature_kelvin: float,
22-
pressure_drop_before_stage: float,
23-
remove_liquid_after_cooling: bool,
24-
stream_reference: list[str] | None = None,
25-
interstage_pressure_control: InterstagePressureControl | None = None,
26-
control_margin: float = 0.0,
27-
):
28-
if inlet_temperature_kelvin < 0:
29-
raise ValueError("inlet_temperature_kelvin must be greater than or equal to 0")
30-
if pressure_drop_before_stage < 0:
31-
raise ValueError("pressure_drop_before_stage must be greater than or equal to 0")
32-
if not (0 <= control_margin <= 1):
33-
raise ValueError("control_margin must be between 0 and 1")
34-
35-
self.compressor_chart = compressor_chart
36-
self.inlet_temperature_kelvin = inlet_temperature_kelvin
37-
self.pressure_drop_before_stage = pressure_drop_before_stage
38-
self.remove_liquid_after_cooling = remove_liquid_after_cooling
39-
self.control_margin = control_margin
40-
self.stream_reference = stream_reference
41-
self.interstage_pressure_control = interstage_pressure_control

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

Lines changed: 76 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from libecalc.common.energy_usage_type import EnergyUsageType
88
from libecalc.common.errors.exceptions import InvalidResourceException
99
from libecalc.common.fixed_speed_pressure_control import FixedSpeedPressureControl
10+
from libecalc.common.serializable_chart import SingleSpeedChartDTO, VariableSpeedChartDTO
1011
from libecalc.common.temporal_model import TemporalModel
1112
from libecalc.common.time_utils import Period, define_time_model_for_period
1213
from libecalc.common.units import Unit
@@ -51,21 +52,25 @@
5152
from libecalc.domain.process.compressor.core.train.single_speed_compressor_train_common_shaft import (
5253
SingleSpeedCompressorTrainCommonShaft,
5354
)
54-
from libecalc.domain.process.compressor.core.train.stage import CompressorTrainStage
55+
from libecalc.domain.process.compressor.core.train.stage import CompressorTrainStage, UndefinedCompressorStage
5556
from libecalc.domain.process.compressor.core.train.types import FluidStreamObjectForMultipleStreams
5657
from libecalc.domain.process.compressor.core.train.variable_speed_compressor_train_common_shaft import (
5758
VariableSpeedCompressorTrainCommonShaft,
5859
)
5960
from libecalc.domain.process.compressor.core.train.variable_speed_compressor_train_common_shaft_multiple_streams_and_pressures import (
6061
VariableSpeedCompressorTrainCommonShaftMultipleStreamsAndPressures,
6162
)
62-
from libecalc.domain.process.compressor.core.utils import map_compressor_train_stage_to_domain
6363
from libecalc.domain.process.compressor.dto import (
64-
CompressorStage,
6564
InterstagePressureControl,
6665
)
6766
from libecalc.domain.process.pump.pump import PumpModel
67+
from libecalc.domain.process.value_objects.chart.compressor import (
68+
SingleSpeedCompressorChart,
69+
VariableSpeedCompressorChart,
70+
)
71+
from libecalc.domain.process.value_objects.chart.compressor.chart_creator import CompressorChartCreator
6872
from libecalc.domain.process.value_objects.chart.compressor.compressor_chart_dto import CompressorChart
73+
from libecalc.domain.process.value_objects.chart.generic import GenericChartFromDesignPoint, GenericChartFromInput
6974
from libecalc.domain.process.value_objects.fluid_stream.fluid_factory import FluidFactoryInterface
7075
from libecalc.domain.process.value_objects.fluid_stream.fluid_model import FluidModel
7176
from libecalc.domain.regularity import Regularity
@@ -250,6 +255,52 @@ def validate_increasing_pressure(
250255
)
251256

252257

258+
def _create_compressor_chart(
259+
chart_dto: CompressorChart,
260+
) -> SingleSpeedCompressorChart | VariableSpeedCompressorChart | None:
261+
if isinstance(chart_dto, SingleSpeedChartDTO):
262+
return SingleSpeedCompressorChart(chart_dto)
263+
elif isinstance(chart_dto, VariableSpeedChartDTO):
264+
return VariableSpeedCompressorChart(chart_dto)
265+
elif isinstance(chart_dto, GenericChartFromDesignPoint):
266+
return CompressorChartCreator.from_rate_and_head_design_point(
267+
design_actual_rate_m3_per_hour=chart_dto.design_rate_actual_m3_per_hour,
268+
design_head_joule_per_kg=chart_dto.design_polytropic_head_J_per_kg,
269+
polytropic_efficiency=chart_dto.polytropic_efficiency_fraction,
270+
)
271+
elif isinstance(chart_dto, GenericChartFromInput):
272+
return None
273+
else:
274+
raise NotImplementedError(f"Compressor chart type: {chart_dto.typ} has not been implemented.")
275+
276+
277+
def _create_compressor_train_stage(
278+
compressor_chart,
279+
inlet_temperature_kelvin: float,
280+
remove_liquid_after_cooling: bool,
281+
pressure_drop_ahead_of_stage: float | None = None,
282+
interstage_pressure_control: InterstagePressureControl | None = None,
283+
control_margin: float = 0.0,
284+
) -> CompressorTrainStage:
285+
if isinstance(compressor_chart, GenericChartFromInput):
286+
return UndefinedCompressorStage(
287+
polytropic_efficiency=compressor_chart.polytropic_efficiency_fraction,
288+
inlet_temperature_kelvin=inlet_temperature_kelvin,
289+
remove_liquid_after_cooling=remove_liquid_after_cooling,
290+
pressure_drop_ahead_of_stage=pressure_drop_ahead_of_stage,
291+
)
292+
compressor_chart = _create_compressor_chart(chart_dto=compressor_chart)
293+
if control_margin > 0 and compressor_chart is not None:
294+
compressor_chart = compressor_chart.get_chart_adjusted_for_control_margin(control_margin)
295+
return CompressorTrainStage(
296+
compressor_chart=compressor_chart,
297+
inlet_temperature_kelvin=inlet_temperature_kelvin,
298+
remove_liquid_after_cooling=remove_liquid_after_cooling,
299+
pressure_drop_ahead_of_stage=pressure_drop_ahead_of_stage,
300+
interstage_pressure_control=interstage_pressure_control,
301+
)
302+
303+
253304
class CompressorModelMapper:
254305
def __init__(self, resources: Resources, reference_service: ReferenceService, configuration: YamlValidator):
255306
self._reference_service = reference_service
@@ -333,23 +384,22 @@ def _create_simplified_variable_speed_compressor_train(self, model: YamlSimplifi
333384
# The stages are pre defined, known
334385
yaml_stages = train_spec.stages
335386
stages = [
336-
CompressorStage(
387+
_create_compressor_train_stage(
337388
inlet_temperature_kelvin=convert_temperature_to_kelvin(
338389
[stage.inlet_temperature],
339390
input_unit=Unit.CELSIUS,
340391
)[0],
341392
compressor_chart=self._get_compressor_chart(stage.compressor_chart),
342-
pressure_drop_before_stage=0,
393+
pressure_drop_ahead_of_stage=0,
343394
control_margin=0,
344395
remove_liquid_after_cooling=True,
345396
)
346397
for stage in yaml_stages
347398
]
348-
stages_mapped = [map_compressor_train_stage_to_domain(stage_dto) for stage_dto in stages]
349399

350400
return CompressorTrainSimplifiedKnownStages(
351401
fluid_factory=fluid_factory,
352-
stages=stages_mapped,
402+
stages=stages,
353403
energy_usage_adjustment_constant=model.power_adjustment_constant,
354404
energy_usage_adjustment_factor=model.power_adjustment_factor,
355405
calculate_max_rate=model.calculate_max_rate,
@@ -358,21 +408,20 @@ def _create_simplified_variable_speed_compressor_train(self, model: YamlSimplifi
358408
else:
359409
# The stages are unknown, not defined
360410
compressor_chart_reference = train_spec.compressor_chart
361-
stage = CompressorStage(
411+
stage = _create_compressor_train_stage(
362412
compressor_chart=self._get_compressor_chart(compressor_chart_reference),
363413
inlet_temperature_kelvin=convert_temperature_to_kelvin(
364414
[train_spec.inlet_temperature],
365415
input_unit=Unit.CELSIUS,
366416
)[0],
367-
pressure_drop_before_stage=0,
417+
pressure_drop_ahead_of_stage=0,
368418
remove_liquid_after_cooling=True,
369419
# control_margin=0, # mypy needs this?
370420
)
371-
stage_mapped = map_compressor_train_stage_to_domain(stage)
372421

373422
return CompressorTrainSimplifiedUnknownStages(
374423
fluid_factory=fluid_factory,
375-
stage=stage_mapped,
424+
stage=stage,
376425
energy_usage_adjustment_constant=model.power_adjustment_constant,
377426
energy_usage_adjustment_factor=model.power_adjustment_factor,
378427
calculate_max_rate=model.calculate_max_rate,
@@ -391,7 +440,7 @@ def _create_variable_speed_compressor_train(
391440
# The stages are pre defined, known
392441
stages_data = train_spec.stages
393442

394-
stages: list[CompressorStage] = []
443+
stages: list[CompressorTrainStage] = []
395444
for stage in stages_data:
396445
control_margin = convert_control_margin_to_fraction(
397446
stage.control_margin,
@@ -401,26 +450,25 @@ def _create_variable_speed_compressor_train(
401450
compressor_chart = self._get_compressor_chart(stage.compressor_chart)
402451

403452
stages.append(
404-
CompressorStage(
453+
_create_compressor_train_stage(
405454
compressor_chart=compressor_chart,
406455
inlet_temperature_kelvin=convert_temperature_to_kelvin(
407456
[stage.inlet_temperature],
408457
input_unit=Unit.CELSIUS,
409458
)[0],
410459
remove_liquid_after_cooling=True,
411-
pressure_drop_before_stage=stage.pressure_drop_ahead_of_stage,
460+
pressure_drop_ahead_of_stage=stage.pressure_drop_ahead_of_stage,
412461
control_margin=control_margin,
413462
)
414463
)
415-
mapped_stages = [map_compressor_train_stage_to_domain(stage_dto) for stage_dto in stages]
416464
pressure_control = _pressure_control_mapper(model)
417465
fluid_factory = _create_fluid_factory(fluid_model)
418466
if fluid_factory is None:
419467
raise ValueError("Fluid model is required for compressor train")
420468

421469
return VariableSpeedCompressorTrainCommonShaft(
422470
fluid_factory=fluid_factory,
423-
stages=mapped_stages,
471+
stages=stages,
424472
energy_usage_adjustment_constant=model.power_adjustment_constant,
425473
energy_usage_adjustment_factor=model.power_adjustment_factor,
426474
calculate_max_rate=model.calculate_max_rate, # type: ignore[arg-type]
@@ -436,23 +484,22 @@ def _create_single_speed_compressor_train(
436484

437485
train_spec = model.compressor_train
438486

439-
stages: list[CompressorStage] = [
440-
CompressorStage(
487+
stages: list[CompressorTrainStage] = [
488+
_create_compressor_train_stage(
441489
compressor_chart=self._get_compressor_chart(stage.compressor_chart),
442490
inlet_temperature_kelvin=convert_temperature_to_kelvin(
443491
[stage.inlet_temperature],
444492
input_unit=Unit.CELSIUS,
445493
)[0],
446494
remove_liquid_after_cooling=True,
447-
pressure_drop_before_stage=stage.pressure_drop_ahead_of_stage,
495+
pressure_drop_ahead_of_stage=stage.pressure_drop_ahead_of_stage,
448496
control_margin=convert_control_margin_to_fraction(
449497
stage.control_margin,
450498
YAML_UNIT_MAPPING[stage.control_margin_unit],
451499
),
452500
)
453501
for stage in train_spec.stages
454502
]
455-
stages_mapped = [map_compressor_train_stage_to_domain(stage_dto) for stage_dto in stages]
456503
pressure_control = _pressure_control_mapper(model)
457504
maximum_discharge_pressure = model.maximum_discharge_pressure
458505
if maximum_discharge_pressure and pressure_control != FixedSpeedPressureControl.DOWNSTREAM_CHOKE:
@@ -468,7 +515,7 @@ def _create_single_speed_compressor_train(
468515

469516
return SingleSpeedCompressorTrainCommonShaft(
470517
fluid_factory=fluid_factory,
471-
stages=stages_mapped,
518+
stages=stages,
472519
pressure_control=pressure_control,
473520
maximum_discharge_pressure=maximum_discharge_pressure,
474521
energy_usage_adjustment_constant=model.power_adjustment_constant,
@@ -516,7 +563,7 @@ def _create_variable_speed_compressor_train_multiple_streams_and_pressures(
516563
[stage_config.inlet_temperature],
517564
input_unit=Unit.CELSIUS,
518565
)[0]
519-
pressure_drop_before_stage = stage_config.pressure_drop_ahead_of_stage
566+
pressure_drop_ahead_of_stage = stage_config.pressure_drop_ahead_of_stage
520567
control_margin = stage_config.control_margin
521568
control_margin_unit = stage_config.control_margin_unit
522569
control_margin_fraction = convert_control_margin_to_fraction(
@@ -550,16 +597,13 @@ def _create_variable_speed_compressor_train_multiple_streams_and_pressures(
550597
)
551598

552599
stages.append(
553-
map_compressor_train_stage_to_domain(
554-
CompressorStage(
555-
compressor_chart=compressor_chart,
556-
inlet_temperature_kelvin=inlet_temperature_kelvin,
557-
pressure_drop_before_stage=pressure_drop_before_stage,
558-
remove_liquid_after_cooling=True,
559-
control_margin=control_margin_fraction,
560-
stream_reference=stream_references_this_stage,
561-
interstage_pressure_control=interstage_pressure_control,
562-
)
600+
_create_compressor_train_stage(
601+
compressor_chart=compressor_chart,
602+
inlet_temperature_kelvin=inlet_temperature_kelvin,
603+
pressure_drop_ahead_of_stage=pressure_drop_ahead_of_stage,
604+
remove_liquid_after_cooling=True,
605+
control_margin=control_margin_fraction,
606+
interstage_pressure_control=interstage_pressure_control,
563607
)
564608
)
565609

0 commit comments

Comments
 (0)