-
Notifications
You must be signed in to change notification settings - Fork 9
refactor: introduce upstream choke pressure control strategy #1427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
frodehk
merged 2 commits into
main
from
refactor/introduce-upstream-pressure-control-strategy
Mar 13, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/libecalc/domain/process/process_solver/pressure_control/upstream_choke.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| from dataclasses import dataclass | ||
|
|
||
| from libecalc.domain.process.compressor.core.train.utils.common import PRESSURE_CALCULATION_TOLERANCE | ||
| from libecalc.domain.process.entities.process_units.choke import Choke | ||
| from libecalc.domain.process.process_solver.boundary import Boundary | ||
| from libecalc.domain.process.process_solver.float_constraint import FloatConstraint | ||
| from libecalc.domain.process.process_solver.pressure_control.pressure_control_strategy import PressureControlStrategy | ||
| from libecalc.domain.process.process_solver.search_strategies import RootFindingStrategy | ||
| from libecalc.domain.process.process_solver.solvers.downstream_choke_solver import ChokeConfiguration | ||
| from libecalc.domain.process.process_solver.solvers.upstream_choke_solver import UpstreamChokeSolver | ||
| from libecalc.domain.process.process_system.process_system import ProcessSystem | ||
| from libecalc.domain.process.value_objects.fluid_stream import FluidStream | ||
|
|
||
|
|
||
| @dataclass | ||
| class UpstreamChokeRunner: | ||
| def __init__(self, process_system: ProcessSystem, upstream_choke: Choke): | ||
| self._process_system = process_system | ||
| self._upstream_choke = upstream_choke | ||
|
|
||
| def run(self, inlet_stream: FluidStream, upstream_delta_pressure: float) -> FluidStream: | ||
| self._upstream_choke.set_pressure_change(pressure_change=upstream_delta_pressure) | ||
| return self._process_system.propagate_stream(inlet_stream=inlet_stream) | ||
|
|
||
|
|
||
| class UpstreamChokePressureControlStrategy(PressureControlStrategy): | ||
| """ | ||
| Meet target outlet pressure at fixed speed by applying upstream choking when needed. | ||
|
|
||
| The strategy models pressure control by reducing suction pressure (upstream choking). | ||
| It computes a safe ΔP search interval from the inlet stream pressure at solve time, | ||
| then uses a root-finding solver to find the ΔP that makes outlet pressure match the target. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| runner: "UpstreamChokeRunner", | ||
| root_finding_strategy: RootFindingStrategy, | ||
| ): | ||
| self._runner = runner | ||
| self._root_finding_strategy = root_finding_strategy | ||
|
|
||
| def apply(self, target_pressure: FloatConstraint, inlet_stream: FluidStream) -> bool: | ||
| # Use a small margin to avoid evaluating exactly at the physical/numerical extremes: | ||
| # ΔP = 0 (no choke) and ΔP = inlet_pressure (zero/negative suction pressure). | ||
| delta_pressure_boundary = Boundary( | ||
| min=PRESSURE_CALCULATION_TOLERANCE, | ||
| max=inlet_stream.pressure_bara - PRESSURE_CALCULATION_TOLERANCE, | ||
| ) | ||
|
|
||
| solver = UpstreamChokeSolver( | ||
| root_finding_strategy=self._root_finding_strategy, | ||
| target_pressure=target_pressure.value, | ||
| delta_pressure_boundary=delta_pressure_boundary, | ||
| ) | ||
|
|
||
| def choke_func(config: ChokeConfiguration) -> FluidStream: | ||
| # The runner is responsible for interpreting upstream ΔP as reduced suction pressure | ||
| # seen by the downstream process system. | ||
| return self._runner.run( | ||
| inlet_stream=inlet_stream, | ||
| upstream_delta_pressure=config.delta_pressure, | ||
| ) | ||
|
|
||
| solution = solver.solve(choke_func) | ||
|
|
||
| # Re-run with the chosen configuration to leave the choke/process state configured. | ||
| outlet_stream_after_choke = choke_func(solution.configuration) | ||
|
|
||
| return outlet_stream_after_choke.pressure_bara == target_pressure | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| import pytest | ||
|
|
||
| from libecalc.domain.process.entities.shaft import VariableSpeedShaft | ||
| from libecalc.domain.process.process_solver.boundary import Boundary | ||
| from libecalc.domain.process.process_system.compressor_stage_process_unit import CompressorStageProcessUnit | ||
| from libecalc.domain.process.process_system.process_unit import ProcessUnitId, create_process_unit_id | ||
| from libecalc.domain.process.value_objects.fluid_stream import FluidService, FluidStream | ||
|
|
||
|
|
||
| class SpeedCompressorStage(CompressorStageProcessUnit): | ||
| """ | ||
| Test double that makes speed->pressure mapping deterministic: | ||
|
|
||
| outlet_pressure = inlet_pressure + shaft_speed | ||
|
|
||
| The capacity-related methods are implemented with wide limits to avoid | ||
| interfering with tests that focus on solver orchestration. | ||
| """ | ||
|
|
||
| def __init__(self, shaft: VariableSpeedShaft, fluid_service: FluidService): | ||
| self._id = create_process_unit_id() | ||
| self._shaft = shaft | ||
| self._fluid_service = fluid_service | ||
|
|
||
| def get_id(self) -> ProcessUnitId: | ||
| return self._id | ||
|
|
||
| def get_speed_boundary(self) -> Boundary: | ||
| return Boundary(min=200.0, max=600.0) | ||
|
|
||
| def get_maximum_standard_rate(self, inlet_stream: FluidStream) -> float: | ||
| # "Infinite" capacity for test purposes | ||
| return 1e30 | ||
|
|
||
| def get_minimum_standard_rate(self, inlet_stream: FluidStream) -> float: | ||
| # "No minimum" for test purposes | ||
| return 0.0 | ||
|
|
||
| def propagate_stream(self, inlet_stream: FluidStream) -> FluidStream: | ||
| speed = self._shaft.get_speed() | ||
| return self._fluid_service.create_stream_from_standard_rate( | ||
| fluid_model=inlet_stream.fluid_model, | ||
| pressure_bara=inlet_stream.pressure_bara + speed, | ||
| standard_rate_m3_per_day=inlet_stream.standard_rate_sm3_per_day, | ||
| temperature_kelvin=inlet_stream.temperature_kelvin, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def speed_compressor_stage_factory(fluid_service): | ||
| def create(shaft: VariableSpeedShaft): | ||
| return SpeedCompressorStage(shaft=shaft, fluid_service=fluid_service) | ||
|
|
||
| return create |
67 changes: 67 additions & 0 deletions
67
.../process/process_solver/pressure_control/test_upstream_choke_pressure_control_strategy.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| from libecalc.domain.process.process_solver.float_constraint import FloatConstraint | ||
| from libecalc.domain.process.process_solver.pressure_control.upstream_choke import ( | ||
| UpstreamChokePressureControlStrategy, | ||
| UpstreamChokeRunner, | ||
| ) | ||
|
|
||
|
|
||
| def test_upstream_choke_strategy_baseline_below_target_does_not_choke( | ||
| simple_process_unit_factory, | ||
| process_system_factory, | ||
| stream_factory, | ||
| choke_factory, | ||
| root_finding_strategy, | ||
| ): | ||
| """ | ||
| Does not apply upstream choking when baseline outlet pressure is already below the target. | ||
| """ | ||
| upstream_choke = choke_factory() | ||
| process_system = process_system_factory( | ||
| process_units=[upstream_choke, simple_process_unit_factory(pressure_multiplier=1)], | ||
| ) | ||
|
|
||
| runner = UpstreamChokeRunner(process_system=process_system, upstream_choke=upstream_choke) | ||
| strategy = UpstreamChokePressureControlStrategy( | ||
| runner=runner, | ||
| root_finding_strategy=root_finding_strategy, | ||
| ) | ||
|
|
||
| inlet_stream = stream_factory(standard_rate_m3_per_day=1000, pressure_bara=50) | ||
| target_pressure = FloatConstraint(70.0, abs_tol=1e-12) | ||
|
|
||
| success = strategy.apply(target_pressure=target_pressure, inlet_stream=inlet_stream) | ||
| assert success is False | ||
|
|
||
| # Ensure choke is not applied (baseline-run uses delta_pressure=0.0) | ||
| outlet_stream_no_choke = runner.run(inlet_stream=inlet_stream, upstream_delta_pressure=0.0) | ||
| assert outlet_stream_no_choke.pressure_bara < target_pressure | ||
|
|
||
|
|
||
| def test_upstream_choke_strategy_baseline_above_target_chokes_to_target( | ||
| simple_process_unit_factory, | ||
| process_system_factory, | ||
| stream_factory, | ||
| choke_factory, | ||
| root_finding_strategy, | ||
| ): | ||
| """Applies upstream choking when baseline outlet pressure is above the target, so outlet meets target.""" | ||
| upstream_choke = choke_factory() | ||
| process_system = process_system_factory( | ||
| process_units=[upstream_choke, simple_process_unit_factory(pressure_multiplier=1)], | ||
| ) | ||
|
|
||
| runner = UpstreamChokeRunner(process_system=process_system, upstream_choke=upstream_choke) | ||
| strategy = UpstreamChokePressureControlStrategy( | ||
| runner=runner, | ||
| root_finding_strategy=root_finding_strategy, | ||
| ) | ||
|
|
||
| inlet_stream = stream_factory(standard_rate_m3_per_day=1000, pressure_bara=100) | ||
| target_pressure = FloatConstraint(70.0, abs_tol=1e-12) | ||
|
|
||
| success = strategy.apply(target_pressure=target_pressure, inlet_stream=inlet_stream) | ||
| assert success is True | ||
|
|
||
| # Confirm outlet is at target with the choke state set by the strategy. | ||
| outlet_stream_after = process_system.propagate_stream(inlet_stream=inlet_stream) | ||
| assert outlet_stream_after.pressure_bara == target_pressure |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.