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

30 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-06-23 21:51 +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 inlet_flow = pyo.value(self.control_volume.properties_in[0].flow_mol) or 0 

51 

52 if inlet_temp + 0.00001 < outlet_temp: 

53 problems.append( 

54 ( 

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

56 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." 

57 ) 

58 ) 

59 

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

61 problems.append( 

62 ( 

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

64 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." 

65 ) 

66 ) 

67 if outlet_pressure < 1000: 

68 problems.append( 

69 ( 

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

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

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

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

74 pressure drop or ratio instead.  

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

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

77 """ 

78 ) 

79 ) 

80 if inlet_flow <= 0.0001: 80 ↛ 81line 80 didn't jump to line 81 because the condition on line 80 was never true

81 problems.append( 

82 ( 

83 self.control_volume.properties_in[0].flow_mol, 

84 f"Inlet flow ({inlet_flow:.2f} mol/s) is very low." 

85 ) 

86 ) 

87 return problems