1+ import logging
2+
13from libecalc .domain .process .compressor .core .train .utils .numeric_methods import (
24 find_root ,
35 maximize_x_given_boolean_condition_function ,
46)
57from libecalc .domain .process .process_solver .boundary import Boundary
68from libecalc .domain .process .process_solver .solver import Solver
9+ from libecalc .domain .process .process_system .process_error import ProcessError
710from libecalc .domain .process .process_system .process_system import ProcessSystem
811from libecalc .domain .process .value_objects .fluid_stream import FluidStream
912
13+ logger = logging .getLogger (__name__ )
14+
1015
1116class SpeedSolver (Solver ):
1217 def __init__ (self , boundary : Boundary , target_pressure : float ):
@@ -20,24 +25,36 @@ def solve(
2025 ) -> FluidStream | None :
2126 shaft = process_system .get_shaft ()
2227
23- def get_outlet_stream (speed : float ) -> FluidStream | None :
28+ def get_outlet_stream (speed : float ) -> FluidStream :
2429 shaft .set_speed (speed )
2530 return process_system .propagate_stream (inlet_stream )
2631
2732 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
33+ try :
34+ maximum_speed_outlet_stream = get_outlet_stream (speed = maximum_speed )
35+ except ProcessError as e :
36+ logger .debug (f"No solution found for maximum speed: { maximum_speed } " , exc_info = e )
37+ return None
3238
3339 minimum_speed = self ._boundary .min
34- minimum_speed_outlet_stream = get_outlet_stream (speed = minimum_speed )
35- if minimum_speed_outlet_stream is None :
40+ try :
41+ minimum_speed_outlet_stream = get_outlet_stream (speed = minimum_speed )
42+ except ProcessError as e :
43+ logger .debug (f"No solution found for minimum speed: { minimum_speed } " , exc_info = e )
44+
3645 # rate is above maximum rate for minimum speed. Find the lowest minimum speed which gives a valid result
46+ def bool_speed_func (x ):
47+ try :
48+ get_outlet_stream (speed = x )
49+ return True
50+ except ProcessError as e :
51+ logger .debug (f"No solution found for speed: { x } " , exc_info = e )
52+ return False
53+
3754 minimum_speed = - maximize_x_given_boolean_condition_function (
3855 x_min = - maximum_speed ,
3956 x_max = - minimum_speed ,
40- bool_func = lambda x : get_outlet_stream ( speed = x ) is not None ,
57+ bool_func = bool_speed_func ,
4158 )
4259 minimum_speed_outlet_stream = get_outlet_stream (speed = minimum_speed )
4360
@@ -47,17 +64,16 @@ def get_outlet_stream(speed: float) -> FluidStream | None:
4764 <= maximum_speed_outlet_stream .pressure_bara
4865 ):
4966 # Solution 1, iterate on speed until target discharge pressure is found
50- def f (speed : float ) -> float :
51- out = get_outlet_stream (speed = speed )
67+ def root_speed_func (x : float ) -> float :
5268 # We should be able to produce an outlet stream since we adjust minimum speed above,
5369 # or exit if max speed is not enough
54- assert out is not None , "Unable to produce an outlet stream"
70+ out = get_outlet_stream ( speed = x )
5571 return out .pressure_bara - self ._target_pressure
5672
5773 speed = find_root (
5874 lower_bound = minimum_speed ,
5975 upper_bound = maximum_speed ,
60- func = f ,
76+ func = root_speed_func ,
6177 )
6278 return get_outlet_stream (speed = speed )
6379 elif self ._target_pressure < minimum_speed_outlet_stream .pressure_bara :
0 commit comments