Coverage for backend/django/idaes_factory/adapters/property_package_adapter.py: 87%

83 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-03-26 20:57 +0000

1from ahuora_builder_types.flowsheet_schema import PropertyPackageType 

2from flowsheetInternals.unitops.models import SimulationObject, Port 

3from .property_info_adapter import ValueAdapter 

4from core.auxiliary.enums.unitOpGraphics import ConType 

5from common.config_types import PropertyPackageKey 

6from core.auxiliary.models.CustomPropertyPackage import CustomPropertyPackage 

7from ahuora_builder_types.custom_package_schema import ( 

8 CustomCompoundPropertiesSchema, 

9 CustomPropertyPackagePropertiesSchema, 

10) 

11from ..idaes_factory_context import IdaesFactoryContext 

12from ..queryset_lookup import ( 

13 get_all_ports, 

14 get_property_package, 

15 get_property, 

16 get_index, 

17) 

18 

19 

20class PropertyPackageAdapter(ValueAdapter): 

21 def __init__(self, package_name: PropertyPackageKey = ""): 

22 self.package_name = package_name 

23 

24 def serialise(self, ctx: IdaesFactoryContext, unit_model: SimulationObject): 

25 # TODO: This needs to figure out which compounds is in the stream is using, and then use that property 

26 # package (add it to the flowsheet if it's not already there) 

27 # and then return the id of the property package 

28 port_keys = unit_model.schema.propertyPackagePorts[self.package_name] 

29 ports = get_all_ports(unit_model, keys=port_keys) 

30 package = get_property_package(unit_model, self.package_name) 

31 package_type = package.propertyPackage 

32 if package_type is None: 32 ↛ 33line 32 didn't jump to line 33 because the condition on line 32 was never true

33 if ctx.require_variables_fixed: 

34 raise ValueError( 

35 f"Property package {self.package_name} is not defined for unit model {unit_model.componentName}" 

36 ) 

37 else: 

38 return ( 

39 -1 

40 ) # if we are just trying a simple test scenario, we can return -1 to indicate that the property package is not defined 

41 

42 # set of compounds in the stream 

43 compounds = set() 

44 for port in ports: 

45 stream: SimulationObject = ctx.get_simulation_object(port.stream_id) 

46 if stream is not None: 46 ↛ 44line 46 didn't jump to line 44 because the condition on line 46 was always true

47 property_set = stream.properties 

48 mole_frac_comp = get_property(property_set, "mole_frac_comp") 

49 for value in mole_frac_comp.values.all(): 

50 compounds.add(get_index(value, "compound").key) 

51 

52 if len(compounds) == 0: 

53 if ctx.require_variables_fixed: 53 ↛ 54line 53 didn't jump to line 54 because the condition on line 53 was never true

54 raise ValueError( 

55 f"No compounds found in stream {port_keys} for unit model {unit_model.componentName}" 

56 ) 

57 else: 

58 return -1 

59 

60 # check if property package is already in the flowsheet 

61 for existing_package in ctx.property_packages: 

62 if ( 

63 existing_package.type == package_type 

64 and set(existing_package.compounds) == compounds 

65 ): 

66 return existing_package.id 

67 

68 # create a new property package 

69 id = len(ctx.property_packages) 

70 if package_type == "custom": 

71 package_def = serialise_custom_package(package.customPackage, id) 

72 else: 

73 package_def = PropertyPackageType( 

74 id=id, 

75 type=package_type, 

76 compounds=list(compounds), 

77 phases=["Liq", "Vap"], 

78 ) 

79 

80 ctx.property_packages.append(package_def) 

81 

82 return id 

83 

84 

85def serialise_custom_package(package: CustomPropertyPackage, id): 

86 if package is None: 86 ↛ 87line 86 didn't jump to line 87 because the condition on line 86 was never true

87 raise ValueError("Custom property package is not set") 

88 (compounds, custom_compound_properties) = serialise_custom_compound_properties( 

89 package 

90 ) 

91 custom_package_properties = serialise_custom_package_properties(package) 

92 custom_kappa_values = serialise_custom_kappa_values(package) 

93 return PropertyPackageType( 

94 id=id, 

95 type="custom", 

96 compounds=compounds, 

97 phases=["Liq", "Vap"], 

98 custom_compound_properties=custom_compound_properties, 

99 custom_package_properties=custom_package_properties, 

100 custom_kappa_values=custom_kappa_values, 

101 ) 

102 

103 

104def serialise_custom_package_properties(package: CustomPropertyPackage): 

105 properties = package.properties.all() 

106 property_dict = {} 

107 for prop in properties: 

108 if prop.value is None: 108 ↛ 109line 108 didn't jump to line 109 because the condition on line 108 was never true

109 raise ValueError( 

110 f"Custom property package property {prop.package_property_key} is not set" 

111 ) 

112 property_dict[prop.package_property_key] = float(prop.value) 

113 

114 return CustomPropertyPackagePropertiesSchema.model_validate(property_dict) 

115 

116 

117def serialise_custom_kappa_values( 

118 package: CustomPropertyPackage, 

119) -> dict[str, dict[str, float]]: 

120 kappas = package.kappas.all() 

121 compounds = package.compounds.all() 

122 compound_names = {compound.id: compound.name for compound in compounds} 

123 kappa_dict = {} 

124 for kappa in kappas: 

125 compound1_name = compound_names[kappa.compound1_id] 

126 compound2_name = compound_names[kappa.compound2_id] 

127 if compound1_name not in kappa_dict: 127 ↛ 129line 127 didn't jump to line 129 because the condition on line 127 was always true

128 kappa_dict[compound1_name] = {} 

129 kappa_dict[compound1_name][compound2_name] = kappa.value 

130 return kappa_dict 

131 

132 

133def serialise_custom_compound_properties( 

134 package: CustomPropertyPackage, 

135) -> tuple[list[str], list[CustomCompoundPropertiesSchema]]: 

136 compounds = package.compounds.all() 

137 compound_names = [] 

138 compound_properties = [] 

139 for compound in compounds: 

140 properties = compound.properties.all() 

141 property_dict = {} 

142 for prop in properties: 

143 if prop.value is None: 143 ↛ 144line 143 didn't jump to line 144 because the condition on line 143 was never true

144 raise ValueError( 

145 f"Custom compound property {prop.compound_property_key} for compound {compound.name} is not set" 

146 ) 

147 property_dict[prop.compound_property_key] = float(prop.value) 

148 compound_properties.append( 

149 CustomCompoundPropertiesSchema.model_validate(property_dict) 

150 ) 

151 compound_names.append(compound.name) 

152 return (compound_names, compound_properties)