Coverage for backend/idaes_factory/adapters/property_package_adapter.py: 81%
36 statements
« prev ^ index » next coverage.py v7.10.7, created at 2025-11-06 23:27 +0000
« prev ^ index » next coverage.py v7.10.7, created at 2025-11-06 23:27 +0000
1from common.models.idaes.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
7from ..idaes_factory_context import IdaesFactoryContext
8from ..queryset_lookup import get_all_ports, get_property_package, get_property, get_index
10class PropertyPackageAdapter(ValueAdapter):
11 def __init__(self, package_name: PropertyPackageKey = ""):
12 self.package_name = package_name
14 def serialise(self, ctx: IdaesFactoryContext, unit_model: SimulationObject):
15 # TODO: This needs to figure out which compounds is in the stream is using, and then use that property
16 # package (add it to the flowsheet if it's not already there)
17 # and then return the id of the property package
18 port_keys = unit_model.schema.propertyPackagePorts[self.package_name]
19 ports = get_all_ports(unit_model, keys=port_keys)
20 package_type = get_property_package(unit_model, self.package_name).propertyPackage
21 if package_type is None: 21 ↛ 22line 21 didn't jump to line 22 because the condition on line 21 was never true
22 if ctx.require_variables_fixed:
23 raise ValueError(f"Property package {self.package_name} is not defined for unit model {unit_model.componentName}")
24 else:
25 return -1 # if we are just trying a simple test scenario, we can return -1 to indicate that the property package is not defined
27 # set of compounds in the stream
28 compounds = set()
29 for port in ports:
30 stream: SimulationObject = ctx.get_simulation_object(port.stream_id)
31 if stream is not None: 31 ↛ 29line 31 didn't jump to line 29 because the condition on line 31 was always true
32 property_set = stream.properties
33 mole_frac_comp = get_property(property_set, "mole_frac_comp")
34 for value in mole_frac_comp.values.all():
35 compounds.add(get_index(value, "compound").key)
37 if len(compounds) == 0:
38 if ctx.require_variables_fixed: 38 ↛ 39line 38 didn't jump to line 39 because the condition on line 38 was never true
39 raise ValueError(f"No compounds found in stream {port_keys} for unit model {unit_model.componentName}")
40 else:
41 return -1
43 # check if property package is already in the flowsheet
44 for package in ctx.property_packages:
45 if package.type == package_type and set(package.compounds) == compounds: 45 ↛ 44line 45 didn't jump to line 44 because the condition on line 45 was always true
46 return package.id
48 # create a new property package
49 id = len(ctx.property_packages)
50 ctx.property_packages.append(PropertyPackageType(
51 id=id,
52 type=package_type,
53 compounds=list(compounds),
54 phases=["Liq", "Vap"]
55 ))
57 return id