Coverage for backend/ahuora-builder/src/ahuora_builder/custom/valve_pressure_changer.py: 85%

27 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-05-13 02:47 +0000

1from idaes.models.unit_models.pressure_changer import PressureChangerData 

2from .inverted import add_inverted, initialise_inverted 

3from idaes.core import declare_process_block_class 

4import pyomo.environ as pyo 

5 

6@declare_process_block_class("ValvePressureChanger") 

7class ValvePressureChangerData(PressureChangerData): 

8 """ 

9 Custom Pressure Changer model that includes inverted deltaP property. 

10 """ 

11 

12 def build(self, *args, **kwargs): 

13 """ 

14 Build method for the ValvePressureChangerData class. 

15 This method initializes the control volume and sets up the model. 

16 """ 

17 super().build(*args, **kwargs) 

18 

19 # add deltaP_inverted as a property 

20 add_inverted(self, "deltaP") 

21 

22 def initialize_build( 

23 self,*args,**kwargs, 

24 ): 

25 """ 

26 Initialization method for the ValvePressureChangerData class. 

27 

28 Args: 

29 state_args (dict): Arguments to be passed to the state block 

30 solver (str): Solver to use for initialization 

31 optarg (dict): Solver arguments dictionary 

32 """ 

33 initialise_inverted(self, "deltaP") 

34 

35 super().initialize_build(*args, **kwargs) 

36 

37 def diagnose(self) -> list[tuple[pyo.Component, str]]: 

38 """ 

39 Help for diagnosing common problems with the valve model. Checks for common issues such as: 

40 - Weird stuff happening with temperature and pressure (unexpected increases) 

41 - Outlet pressure being very low (valve can't meet the target conditions) 

42 """ 

43 problems = [] 

44 inlet_temp = pyo.value(self.control_volume.properties_in[0].temperature) or 0 

45 outlet_temp = pyo.value(self.control_volume.properties_out[0].temperature) or 0 

46 inlet_pressure = pyo.value(self.control_volume.properties_in[0].pressure) or 0 

47 outlet_pressure = pyo.value(self.control_volume.properties_out[0].pressure) or 0 

48 inlet_vapor_frac = pyo.value(self.control_volume.properties_in[0].vapor_frac) or -1 

49 outlet_vapor_frac = pyo.value(self.control_volume.properties_out[0].vapor_frac) or -1 

50 

51 if inlet_temp < outlet_temp: 51 ↛ 52line 51 didn't jump to line 52 because the condition on line 51 was never true

52 problems.append( 

53 ( 

54 self.control_volume.properties_out[0].temperature, 

55 f"Outlet temperature ({outlet_temp:.2f} K) is more than inlet temperature ({inlet_temp:.2f} K). Valves can only decrease temperature. Check your formulation of the upstream problem." 

56 ) 

57 ) 

58 

59 if inlet_pressure < outlet_pressure: 59 ↛ 60line 59 didn't jump to line 60 because the condition on line 59 was never true

60 problems.append( 

61 ( 

62 self.control_volume.properties_out[0].pressure, 

63 f"Outlet pressure ({outlet_pressure:.2f} Pa) is greater than inlet pressure ({inlet_pressure:.2f} Pa). Valves can only decrease pressure. Check your formulation of the upstream problem." 

64 ) 

65 ) 

66 if outlet_pressure < 1000: 66 ↛ 79line 66 didn't jump to line 79 because the condition on line 66 was always true

67 problems.append( 

68 ( 

69 self.control_volume.properties_out[0].pressure, 

70 f"""Outlet pressure ({outlet_pressure:.2f} Pa) is very low. 

71 If you are targeting an outlet condition, e.g vapor fraction or temperature, 

72 the valve may not be able to reach these conditions. Try specifying a  

73 pressure drop or ratio instead.  

74 {f"Outlet Vapor fraction is {outlet_vapor_frac:.2f}, no more cooling from phase change" 

75 if outlet_vapor_frac >= 0.95 and inlet_vapor_frac < 0.95 else ""} 

76 """ 

77 ) 

78 ) 

79 return problems