Coverage for backend/ahuora-builder/src/ahuora_builder/property_package_manager.py: 92%

39 statements  

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

1from idaes.core.base.property_base import PhysicalParameterBlock 

2from idaes.core import FlowsheetBlock 

3from ahuora_property_packages.build_package import build_package 

4from ahuora_builder_types.flowsheet_schema import PropertyPackageType 

5from .flowsheet_manager_type import FlowsheetManager 

6from ahuora_builder_types import PropertyPackageId 

7from .methods.build_custom_package import build_custom_package 

8 

9def create_property_package( 

10 property_package_schema: PropertyPackageType, flowsheet: FlowsheetBlock 

11) -> PhysicalParameterBlock: 

12 """ 

13 Create a property package from a schema 

14 """ 

15 compounds = property_package_schema.compounds 

16 type = property_package_schema.type 

17 phases = property_package_schema.phases 

18 

19 if type == "custom": 

20 property_package = build_custom_package(property_package_schema) 

21 else: 

22 property_package = build_package(type, compounds) 

23 

24 property_package_id = property_package_schema.id if property_package_schema.id != -1 else "default" 

25 

26 flowsheet.add_component(f"PP_{property_package_id}", property_package) 

27 return property_package 

28 

29 

30 

31 

32class PropertyPackageManager: 

33 """ 

34 Manages the property packages for a flowsheet 

35 """ 

36 

37 def __init__(self, flowsheet_manager: FlowsheetManager) -> None: 

38 """ 

39 Create a new property package manager 

40 """ 

41 self._flowsheet_manager = flowsheet_manager 

42 self._property_packages: dict[PropertyPackageId, PhysicalParameterBlock] = {} 

43 

44 def load(self) -> None: 

45 """ 

46 Load the property packages from the flowsheet definition 

47 """ 

48 schema = self._flowsheet_manager.schema 

49 property_packages_schema = schema.property_packages 

50 for property_package_schema in property_packages_schema: 

51 id = property_package_schema.id 

52 if id in self._property_packages: 52 ↛ 53line 52 didn't jump to line 53 because the condition on line 52 was never true

53 raise Exception(f"Property package with id {id} already exists") 

54 

55 property_package = create_property_package( 

56 property_package_schema, self._flowsheet_manager.model.fs 

57 ) 

58 self._property_packages[id] = property_package 

59 

60 def get(self, id: PropertyPackageId) -> PhysicalParameterBlock: 

61 """ 

62 Get a property package by id 

63 """ 

64 # For backwards compatibility with other tests, use id -1 as helmholtz 

65 fs = self._flowsheet_manager.model.fs 

66 

67 if id == -1: 

68 if not hasattr(fs, "PP_default"): 

69 create_property_package( 

70 PropertyPackageType( 

71 id=-1, 

72 type="helmholtz", 

73 compounds=["h2o"], 

74 phases=["Liq"] 

75 ), 

76 fs, 

77 ) 

78 return fs.PP_default 

79 else: 

80 # get the property package by id 

81 if id not in self._property_packages: 81 ↛ 82line 81 didn't jump to line 82 because the condition on line 81 was never true

82 raise Exception(f"Property package with id {id} does not exist") 

83 return self._property_packages[id]