|
| 1 | +from libecalc.domain.process.compressor.core.train.utils.numeric_methods import ( |
| 2 | + find_root, |
| 3 | + maximize_x_given_boolean_condition_function, |
| 4 | +) |
| 5 | +from libecalc.domain.process.process_solver.boundary import Boundary |
| 6 | +from libecalc.domain.process.process_solver.solver import Solver |
| 7 | +from libecalc.domain.process.process_system.process_system import ProcessSystem |
| 8 | +from libecalc.domain.process.value_objects.fluid_stream import FluidStream |
| 9 | + |
| 10 | + |
| 11 | +class SpeedSolver(Solver): |
| 12 | + def __init__(self, boundary: Boundary, target_pressure: float): |
| 13 | + self._boundary = boundary |
| 14 | + self._target_pressure = target_pressure |
| 15 | + |
| 16 | + def solve( |
| 17 | + self, |
| 18 | + process_system: ProcessSystem, |
| 19 | + inlet_stream: FluidStream, |
| 20 | + ) -> FluidStream | None: |
| 21 | + shaft = process_system.get_shaft() |
| 22 | + |
| 23 | + def get_outlet_stream(speed: float) -> FluidStream | None: |
| 24 | + shaft.set_speed(speed) |
| 25 | + return process_system.propagate_stream(inlet_stream) |
| 26 | + |
| 27 | + maximum_speed = self._boundary.max |
| 28 | + maximum_speed_outlet_stream = get_outlet_stream(speed=maximum_speed) |
| 29 | + if maximum_speed_outlet_stream is None: |
| 30 | + # Outside capacity |
| 31 | + return maximum_speed_outlet_stream |
| 32 | + |
| 33 | + minimum_speed = self._boundary.min |
| 34 | + minimum_speed_outlet_stream = get_outlet_stream(speed=minimum_speed) |
| 35 | + if minimum_speed_outlet_stream is None: |
| 36 | + # rate is above maximum rate for minimum speed. Find the lowest minimum speed which gives a valid result |
| 37 | + minimum_speed = -maximize_x_given_boolean_condition_function( |
| 38 | + x_min=-maximum_speed, |
| 39 | + x_max=-minimum_speed, |
| 40 | + bool_func=lambda x: get_outlet_stream(speed=x) is not None, |
| 41 | + ) |
| 42 | + minimum_speed_outlet_stream = get_outlet_stream(speed=minimum_speed) |
| 43 | + |
| 44 | + if ( |
| 45 | + minimum_speed_outlet_stream.pressure_bara |
| 46 | + <= self._target_pressure |
| 47 | + <= maximum_speed_outlet_stream.pressure_bara |
| 48 | + ): |
| 49 | + # Solution 1, iterate on speed until target discharge pressure is found |
| 50 | + def f(speed: float) -> float: |
| 51 | + out = get_outlet_stream(speed=speed) |
| 52 | + assert out is not None, "What to do" |
| 53 | + return out.pressure_bara - self._target_pressure |
| 54 | + |
| 55 | + speed = find_root( |
| 56 | + lower_bound=minimum_speed, |
| 57 | + upper_bound=maximum_speed, |
| 58 | + func=f, |
| 59 | + ) |
| 60 | + return get_outlet_stream(speed=speed) |
| 61 | + elif self._target_pressure < minimum_speed_outlet_stream.pressure_bara: |
| 62 | + # Solution 2, target pressure is too low |
| 63 | + shaft.set_speed(minimum_speed) |
| 64 | + return minimum_speed_outlet_stream |
| 65 | + |
| 66 | + # Solution 3, target discharge pressure is too high |
| 67 | + shaft.set_speed(maximum_speed) |
| 68 | + return maximum_speed_outlet_stream |
0 commit comments