Coverage for backend/django/flowsheetInternals/unitops/config/objects/simple_header_config.py: 96%

37 statements  

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

1from common.config_types import * 

2from common.config_utils import * 

3from ..spec_helpers import * 

4 

5class SimpleHeaderPortListAdapter(PortListAdapter): 

6 """ 

7 Handles the serialisation and reloading of the ports of a header. 

8 Includes separate handling for condensate_outlet port. 

9 """ 

10 def __init__(self): 

11 pass 

12 

13 def serialise(self, ctx, unit_model): 

14 portList = {} 

15 

16 # Handle inlets 

17 inlets = [ i for i in unit_model.ports.filter(direction=ConType.Inlet, stream__isnull=False)] 

18 

19 # Sort regular inlets by index 

20 inlets.sort(key=lambda x: x.index if hasattr(x, 'index') else 0) 

21 

22 # Add regular inlets 

23 for i, inlet in enumerate(inlets): 

24 portList[f"inlet_{i+1}"] = { 

25 "id": inlet.pk, 

26 "properties": serialise_stream(ctx, inlet.stream, is_inlet=True) 

27 } 

28 

29 # Handle outlets 

30 outlets = unit_model.ports.filter(direction=ConType.Outlet, stream__isnull=False) 

31 

32 # Separate special outlets 

33 condensate_outlet = None 

34 vent_outlet = None 

35 regular_outlets = [] 

36 

37 for outlet in outlets: 

38 if outlet.key == "outlet_condensate": 

39 condensate_outlet = outlet 

40 elif outlet.key == "outlet_vent": 

41 vent_outlet = outlet 

42 else: 

43 regular_outlets.append(outlet) 

44 

45 # Sort regular outlets 

46 regular_outlets.sort(key=lambda x: x.index if hasattr(x, 'index') else 0) 

47 # Add regular outlets 

48 for i, outlet in enumerate(regular_outlets): 

49 portList[f"outlet_{i+1}"] = { 

50 "id": outlet.pk, 

51 "properties": serialise_stream(ctx, outlet.stream, is_inlet=False) 

52 } 

53 

54 # Add vent outlet if it exists (named "vent" not "outlet_N") 

55 if vent_outlet: 55 ↛ 62line 55 didn't jump to line 62 because the condition on line 55 was always true

56 portList["outlet_vent"] = { 

57 "id": vent_outlet.pk, 

58 "properties": serialise_stream(ctx, vent_outlet.stream, is_inlet=False) 

59 } 

60 

61 # Add condensate outlet 

62 if condensate_outlet: 62 ↛ 68line 62 didn't jump to line 68 because the condition on line 62 was always true

63 portList["outlet_condensate"] = { 

64 "id": condensate_outlet.pk, 

65 "properties": serialise_stream(ctx, condensate_outlet.stream, is_inlet=False) 

66 } 

67 

68 return portList 

69 

70class HeaderOutletsAdapter(ValueAdapter): 

71 def serialise(self, ctx, unit_model): 

72 return ValueArgSchema(value=len(get_all_ports(unit_model, "outlet"))) 

73 

74class HeaderInletsAdapter(ValueAdapter): 

75 def serialise(self, ctx, unit_model): 

76 return ValueArgSchema(value=len(get_all_ports(unit_model, "inlet"))) 

77 

78simple_header_config: ObjectType = ObjectType( 

79 displayType="Simple Header", 

80 displayName="Simple Header", 

81 ports={ 

82 "outlet_condensate": PortType( 

83 many=False, # Always exactly 1, cannot increase 

84 displayName="Condensate", 

85 type=ConType.Outlet, 

86 default=1, 

87 streamName="Condensate S", 

88 streamOffset=3, 

89 ), 

90 "outlet_vent": PortType( 

91 many=False, 

92 displayName="Vent Outlet", 

93 type=ConType.Outlet, 

94 default=1, # default number of outlets 

95 streamName="Vent S", 

96 streamOffset=3, 

97 ), 

98 "inlet": PortType( 

99 many=True, 

100 displayName="Inlet", 

101 type=ConType.Inlet, 

102 default=1, # default number of inlets 

103 minimum=1, # minimum number of inlets 

104 streamName="S", 

105 streamOffset=3, 

106 

107 ), 

108 "outlet": PortType( 

109 many=True, 

110 displayName="Outlet", 

111 type=ConType.Outlet, 

112 default=2, # default number of outlets 

113 minimum=1, # minimum number of outlets 

114 streamName="S", 

115 streamOffset=3, 

116 ), 

117 }, 

118 propertyPackagePorts={ 

119 "": ["inlet", "outlet", "outlet_vent", "outlet_condensate"] 

120 }, 

121 graphicObject={ 

122 "width": 25, 

123 "height": 800, 

124 "autoHeight": True 

125 }, 

126 indexSets=["splitter_fraction"], 

127 properties=PropertiesType({ 

128 "split_flow": PropertyType( 

129 displayName="Flow", 

130 type="numeric", 

131 unitType="molarflow", 

132 indexSets=["splitter_fraction"], 

133 ), 

134 "outlet_vent.flow_mol": PropertyType( 

135 displayName="Vent Flow", 

136 type="numeric", 

137 unitType="molarflow", 

138 ), 

139 "heat_loss": PropertyType( 

140 displayName="Heat Loss", 

141 type="numeric", 

142 unitType="heatflow", 

143 value=0, 

144 ), 

145 "pressure_loss": PropertyType( 

146 displayName="Pressure Drop", 

147 type="numeric", 

148 unitType="pressure", 

149 value=0, 

150 ), 

151 # Read-only properties 

152 "total_flow_mass": PropertyType( 

153 displayName="Mass Flow", 

154 type="numeric", 

155 unitType="massflow", 

156 ), 

157 "total_flow_mol": PropertyType( 

158 displayName="Molar Flow", 

159 type="numeric", 

160 unitType="molarflow", 

161 ), 

162 "balance_flow_mol": PropertyType( 

163 displayName="Balance Flow", 

164 type="numeric", 

165 unitType="molarflow", 

166 ), 

167 "temperature": PropertyType( 

168 displayName="Temperature", 

169 type="numeric", 

170 unitType="temperature", 

171 ), 

172 "degree_of_superheat": PropertyType( 

173 displayName="Degree of Superheat", 

174 type="numeric", 

175 unitType="deltaTemperature", 

176 ), 

177 "pressure": PropertyType( 

178 displayName="Pressure", 

179 type="numeric", 

180 unitType="pressure", 

181 ), 

182 "vapor_frac": PropertyType( 

183 displayName="Vapor Fraction", 

184 type="numeric", 

185 unitType="ratio", 

186 ), 

187 "enth_mass": PropertyType( 

188 displayName="Mass Specific Enthalpy", 

189 type="numeric", 

190 unitType="massEnthalpy", 

191 ), 

192 "enth_mol": PropertyType( 

193 displayName="Molar Specific Enthalpy", 

194 type="numeric", 

195 unitType="molarEnthalpy", 

196 ), 

197 }), 

198 splitter_fraction_name="Outlet", 

199 propertySetGroups={ 

200 "default": { 

201 "type": "stateVars", 

202 "displayName": "Properties", 

203 "stateVars": ("split_flow", "heat_loss", "pressure_loss"), 

204 } 

205 }, 

206 keyProperties=[ 

207 "pressure", 

208 "temperature", 

209 "degree_of_superheat", 

210 "enth_mass", 

211 "vapor_frac", 

212 ], 

213 idaes_adapter=UnitModelAdapter( 

214 args=ArgAdapter({ 

215 "property_package": PropertyPackageAdapter(), 

216 "num_inlets": HeaderInletsAdapter(), 

217 "num_outlets": HeaderOutletsAdapter(), 

218 }), 

219 properties=SerialisePropertiesAdapter(), 

220 ports=SimpleHeaderPortListAdapter() 

221 ), 

222)