|
| 1 | +from collections.abc import Sequence |
| 2 | +from typing import Final |
| 3 | + |
| 4 | +from libecalc.domain.component_validation_error import DomainValidationException |
| 5 | +from libecalc.domain.process.process_solver.float_constraint import FloatConstraint |
| 6 | +from libecalc.domain.process.process_solver.outlet_pressure_solver import OutletPressureSolver |
| 7 | +from libecalc.domain.process.process_solver.pressure_control.downstream_choke import ( |
| 8 | + DownstreamChokePressureControlStrategy, |
| 9 | +) |
| 10 | +from libecalc.domain.process.process_solver.pressure_control.upstream_choke import UpstreamChokePressureControlStrategy |
| 11 | +from libecalc.domain.process.process_solver.process_runner import Configuration |
| 12 | +from libecalc.domain.process.process_solver.solver import Solution |
| 13 | +from libecalc.domain.process.process_solver.solvers.speed_solver import SpeedConfiguration |
| 14 | +from libecalc.domain.process.value_objects.fluid_stream import FluidStream |
| 15 | + |
| 16 | + |
| 17 | +class MultiPressureSolver: |
| 18 | + """Tries to find the shaft speed satisfying N ordered pressure targets, one per segment. Segments share a |
| 19 | + single physical shaft. Each segment's runner covers a disjoint sub-sequence of the stream propagation chain. |
| 20 | +
|
| 21 | + Independent OutletPressureSolver per segment (sequential) against its own pressure target. This gives the |
| 22 | + speed each segment would require if unconstrained. The segment requiring the highest speed is the binding |
| 23 | + constraint. At binding speed, non-binding segments produce more pressure than needed and must have it reduced |
| 24 | + by their pressure-control strategy. |
| 25 | +
|
| 26 | + All segments are re-run in sequence at binding_speed. Anti-surge is applied first. If a segment's outlet |
| 27 | + still exceeds its target, the pressure-control strategy reduces it. The outlet of each segment feeds the inlet |
| 28 | + of the next. For all but one segment, speed changed from the individual evaluations. Therefore, recirculation |
| 29 | + is re-evaluated from scratch. |
| 30 | + """ |
| 31 | + |
| 32 | + def __init__( |
| 33 | + self, |
| 34 | + segments: list[OutletPressureSolver], |
| 35 | + ) -> None: |
| 36 | + if len(segments) < 2: |
| 37 | + raise DomainValidationException("MultiPressureSolver requires at least 2 segments.") |
| 38 | + shaft_ids = {segment.shaft_id for segment in segments} |
| 39 | + if len(shaft_ids) != 1: |
| 40 | + raise DomainValidationException("All segments must share the same shaft_id.") |
| 41 | + self._shaft_id: Final = next(iter(shaft_ids)) |
| 42 | + self._validate_pressure_control_placement(segments) |
| 43 | + self._segments: Final = segments |
| 44 | + |
| 45 | + @staticmethod |
| 46 | + def _validate_pressure_control_placement(segments: list[OutletPressureSolver]) -> None: |
| 47 | + """Upstream choke is only valid on the first segment; downstream choke only on the last.""" |
| 48 | + for i, segment in enumerate(segments): |
| 49 | + strategy = segment.pressure_control_strategy |
| 50 | + is_first = i == 0 |
| 51 | + is_last = i == len(segments) - 1 |
| 52 | + if isinstance(strategy, UpstreamChokePressureControlStrategy) and not is_first: |
| 53 | + raise DomainValidationException( |
| 54 | + f"UpstreamChokePressureControlStrategy is only valid for the first segment " |
| 55 | + f"(segment {i} of {len(segments)})." |
| 56 | + ) |
| 57 | + if isinstance(strategy, DownstreamChokePressureControlStrategy) and not is_last: |
| 58 | + raise DomainValidationException( |
| 59 | + f"DownstreamChokePressureControlStrategy is only valid for the last segment " |
| 60 | + f"(segment {i} of {len(segments)})." |
| 61 | + ) |
| 62 | + |
| 63 | + @staticmethod |
| 64 | + def _extract_speed_configuration(solution: Solution[Sequence[Configuration]]) -> SpeedConfiguration: |
| 65 | + """Find speed configuration in a sequence of configurations.""" |
| 66 | + for config in solution.configuration: |
| 67 | + if isinstance(config.value, SpeedConfiguration): |
| 68 | + return config.value |
| 69 | + raise DomainValidationException("No SpeedConfiguration found in solution.") |
| 70 | + |
| 71 | + def find_solution( |
| 72 | + self, |
| 73 | + pressure_targets: list[FloatConstraint], |
| 74 | + inlet_stream: FluidStream, |
| 75 | + ) -> Solution[Sequence[Configuration]]: |
| 76 | + if len(pressure_targets) != len(self._segments): |
| 77 | + raise DomainValidationException( |
| 78 | + f"Number of pressure targets ({len(pressure_targets)}) must match " |
| 79 | + f"number of segments ({len(self._segments)})." |
| 80 | + ) |
| 81 | + |
| 82 | + speed_configurations: list[SpeedConfiguration] = [] |
| 83 | + current_inlet = inlet_stream |
| 84 | + for segment, target in zip(self._segments, pressure_targets): |
| 85 | + solution_for_segment = segment.find_solution(pressure_constraint=target, inlet_stream=current_inlet) |
| 86 | + speed_configurations.append(self._extract_speed_configuration(solution_for_segment)) |
| 87 | + segment.runner.apply_configurations(solution_for_segment.configuration) |
| 88 | + current_inlet = segment.runner.run(inlet_stream=current_inlet) |
| 89 | + |
| 90 | + shaft_config = Configuration( |
| 91 | + simulation_unit_id=self._shaft_id, |
| 92 | + value=max(speed_configurations), |
| 93 | + ) |
| 94 | + all_configurations: dict = {self._shaft_id: shaft_config} |
| 95 | + |
| 96 | + current_inlet = inlet_stream |
| 97 | + overall_success = True |
| 98 | + |
| 99 | + for segment, target in zip(self._segments, pressure_targets): |
| 100 | + segment.runner.apply_configuration(shaft_config) |
| 101 | + |
| 102 | + segment.anti_surge_strategy.reset() |
| 103 | + anti_surge_solution = segment.anti_surge_strategy.apply(inlet_stream=current_inlet) |
| 104 | + segment.runner.apply_configurations(anti_surge_solution.configuration) |
| 105 | + for config in anti_surge_solution.configuration: |
| 106 | + all_configurations[config.simulation_unit_id] = config |
| 107 | + |
| 108 | + outlet = segment.runner.run(inlet_stream=current_inlet) |
| 109 | + |
| 110 | + if outlet.pressure_bara > target: |
| 111 | + pressure_control_solution = segment.pressure_control_strategy.apply( |
| 112 | + target_pressure=target, |
| 113 | + inlet_stream=current_inlet, |
| 114 | + ) |
| 115 | + for config in pressure_control_solution.configuration: |
| 116 | + all_configurations[config.simulation_unit_id] = config |
| 117 | + segment.runner.apply_configurations(pressure_control_solution.configuration) |
| 118 | + outlet = segment.runner.run(inlet_stream=current_inlet) |
| 119 | + if not pressure_control_solution.success: |
| 120 | + overall_success = False |
| 121 | + elif outlet.pressure_bara < target: |
| 122 | + overall_success = False |
| 123 | + |
| 124 | + current_inlet = outlet |
| 125 | + |
| 126 | + return Solution( |
| 127 | + success=overall_success, |
| 128 | + configuration=list(all_configurations.values()), |
| 129 | + ) |
0 commit comments