Coverage for backend/idaes_service/solver/unit_model_manager.py: 92%

20 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2025-11-06 23:27 +0000

1from .methods.load_unit_model import add_unit_model_to_flowsheet 

2from .methods.adapter_library import UnitModelConstructor, AdapterLibrary 

3from .flowsheet_manager_type import FlowsheetManager 

4from common.models.idaes import UnitModelId, UnitModelSchema 

5from idaes.core import UnitModelBlock 

6 

7 

8 

9class UnitModelManager: 

10 """ 

11 Manages the unit models in the flowsheet 

12 """ 

13 

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

15 """ 

16 Create a new unit model manager 

17 """ 

18 self.flowsheet_manager = flowsheet_manager 

19 self._unit_models: dict[UnitModelId, UnitModelBlock] = {} 

20 

21 def load(self) -> None: 

22 """ 

23 load all unit models from the schema into the flowsheet 

24 """ 

25 schema = self.flowsheet_manager.schema 

26 

27 for unit_model_def in schema.unit_models: 

28 unit_model_type = unit_model_def.type 

29 if unit_model_type not in AdapterLibrary: 29 ↛ 30line 29 didn't jump to line 30 because the condition on line 29 was never true

30 raise Exception( 

31 f"Unit model type '{unit_model_type}' is not in the adapter library." 

32 ) 

33 adapter_constructor: UnitModelConstructor = AdapterLibrary[unit_model_type] 

34 self.load_from_def(unit_model_def, adapter_constructor) 

35 

36 def load_from_def( 

37 self, unit_model_def: UnitModelSchema, adapter_constructor: UnitModelConstructor 

38 ) -> None: 

39 """ 

40 Deserialise a unit model from a JSON object into a unit model adapter 

41 """ 

42 # add the unit model to the flowsheet 

43 unit_model = add_unit_model_to_flowsheet( 

44 unit_model_def, adapter_constructor, self.flowsheet_manager 

45 ) 

46 

47 # store a reference to the adapter 

48 self._unit_models[unit_model_def.id] = unit_model