Coverage for backend/django/core/auxiliary/models/RecycleData.py: 96%

42 statements  

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

1from django.db import models 

2from flowsheetInternals.unitops.models.SimulationObject import SimulationObject 

3from .PropertyInfo import PropertyInfo 

4 

5from core.managers import AccessControlManager, AllFlowsheetStatesManager 

6from .FlowsheetHistoryModel import FlowsheetHistoryModel 

7from flowsheetInternals.unitops.services.edit_operations.recorder import ( 

8 tracked_bulk_create, 

9) 

10 

11 

12class RecycleData(FlowsheetHistoryModel, models.Model): 

13 """ 

14 Recycle data for a recycle block simulation object 

15 Stores which tear object is connected to the recycle block 

16 """ 

17 

18 flowsheet_state = models.ForeignKey( 

19 "FlowsheetState", on_delete=models.CASCADE, related_name="RecycleDatas" 

20 ) 

21 # parent recycle block 

22 simulationObject = models.OneToOneField( 

23 SimulationObject, 

24 on_delete=models.CASCADE, 

25 related_name="recycleData", 

26 null=True, 

27 ) 

28 # tear object, makes properties of this object enabled 

29 tearObject = models.OneToOneField( 

30 SimulationObject, 

31 on_delete=models.SET_NULL, 

32 related_name="recycleConnection", 

33 null=True, 

34 ) 

35 

36 created_at = models.DateTimeField(auto_now_add=True) 

37 objects = AccessControlManager() 

38 all_states = AllFlowsheetStatesManager() 

39 

40 def update(self, tear_object: SimulationObject | None) -> None: 

41 """ 

42 Update the tear object's properties to be enabled 

43 """ 

44 prev_tear = self.tearObject 

45 self.tearObject = tear_object 

46 self.save() 

47 

48 # reevaluate enabled properties 

49 if prev_tear is not None: 

50 # cleanup internal controls 

51 for prop in prev_tear.properties.containedProperties.all(): 

52 for prop_value in prop.values.all(): 

53 if ( 53 ↛ 58line 53 didn't jump to line 58 because the condition on line 53 was never true

54 prop_value.is_control_set_point() 

55 and not prop_value.is_externally_controlled() 

56 ): 

57 # delete this control 

58 prop_value.controlSetPoint.delete() 

59 

60 prev_tear.reevaluate_properties_enabled() 

61 if tear_object is not None and prev_tear != tear_object: 

62 tear_object.reevaluate_properties_enabled() 

63 

64 # delete old property connections 

65 self.propertyConnections.all().delete() 

66 # create new property connections for the new tear object 

67 recycle_properties = [] 

68 if tear_object is not None: 

69 # hardcoded for now 

70 # default to composition and mass flow fixed for stream tear guesses 

71 state_vars = { 

72 "mole_frac_comp": True, 

73 "flow_mass": True, 

74 "temperature": False, 

75 "pressure": False, 

76 } 

77 for prop in tear_object.properties.containedProperties.filter( 

78 key__in=state_vars 

79 ): 

80 recycle_properties.append( 

81 RecycleProperty( 

82 recycleData=self, 

83 propertyInfo=prop, 

84 fixed=state_vars[prop.key], 

85 flowsheet_state=self.flowsheet_state, 

86 ) 

87 ) 

88 

89 tracked_bulk_create(RecycleProperty.objects, recycle_properties) 

90 

91 def clear(self) -> None: 

92 """ 

93 Clear the tear object 

94 """ 

95 self.update(None) 

96 

97 

98class RecycleProperty(FlowsheetHistoryModel, models.Model): 

99 """ 

100 Used to store "fix var and remove the equality constraint" 

101 at the the tear object. Also contributes towards access 

102 levels for frontend and idaes_factory (meaning, is this property editable?) 

103 """ 

104 

105 flowsheet_state = models.ForeignKey( 

106 "FlowsheetState", on_delete=models.CASCADE, related_name="RecycleProperties" 

107 ) 

108 recycleData = models.ForeignKey( 

109 RecycleData, 

110 on_delete=models.CASCADE, 

111 related_name="propertyConnections", 

112 null=True, 

113 ) 

114 # this relation may need to become PropertyValue eventually 

115 propertyInfo = models.OneToOneField( 

116 PropertyInfo, 

117 on_delete=models.CASCADE, 

118 related_name="recycleConnection", 

119 null=True, 

120 ) 

121 fixed = models.BooleanField( 

122 default=False 

123 ) # if true, fix var and remove the equality constraint across the tear 

124 

125 created_at = models.DateTimeField(auto_now_add=True) 

126 objects = AccessControlManager() 

127 all_states = AllFlowsheetStatesManager()