Coverage for backend/pinch_service/OpenPinch/src/lib/schema.py: 100%

109 statements  

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

1from pydantic import BaseModel 

2from typing import Any, List, Optional, Dict 

3from .enums import MainOptionsPropKeys, TurbineOptionsPropKeys, StreamType 

4 

5class ValueWithUnit(BaseModel): 

6 value: float | None 

7 units: str 

8 

9# Hot or Cold Utility 

10class HeatUtility(BaseModel): 

11 name: str 

12 heat_flow: float | ValueWithUnit 

13 

14# Pinch Temperature/s 

15class TempPinch(BaseModel): 

16 cold_temp: Optional[float] | Optional[ValueWithUnit] = None 

17 hot_temp: Optional[float] | Optional[ValueWithUnit] = None 

18 

19# Individual Target Summary 

20class TargetResults(BaseModel): 

21 name: str 

22 degree_of_integration: Optional[float] | Optional[ValueWithUnit] = None 

23 Qh: float | ValueWithUnit 

24 Qc: float | ValueWithUnit 

25 Qr: float | ValueWithUnit 

26 utility_cost: float | ValueWithUnit = None 

27 row_type: str = None 

28 hot_utilities: List[HeatUtility] 

29 cold_utilities: List[HeatUtility] 

30 temp_pinch: TempPinch 

31 work_target: float | ValueWithUnit = None 

32 turbine_efficiency_target: float | ValueWithUnit = None 

33 area: float | ValueWithUnit = None 

34 num_units: float = None 

35 capital_cost: float = None 

36 total_cost: float = None 

37 exergy_sources: float | ValueWithUnit = None 

38 exergy_sinks: float | ValueWithUnit = None 

39 ETE: float = None 

40 exergy_req_min: float | ValueWithUnit = None 

41 exergy_des_min: float | ValueWithUnit = None 

42 

43# Coordinate 

44class DataPoint(BaseModel): 

45 x: float 

46 y: float 

47 

48# Line Segment 

49class Segment(BaseModel): 

50 title: Optional[str] = None 

51 colour: Optional[int] = None 

52 arrow: Optional[str] = None 

53 data_points: Optional[List[DataPoint]] = None 

54 

55# Individual Graph 

56class Graph(BaseModel): 

57 segments: Optional[List[Segment]] = [] 

58 type: str 

59 

60 class Config: 

61 use_enum_values = True 

62 

63class GraphSet(BaseModel): 

64 name: str = "GraphSet" 

65 graphs: List[Graph] 

66 

67# Aggregate request type 

68class TargetResponse(BaseModel): 

69 name: str = 'Site' 

70 targets: List[TargetResults] 

71 graphs: Dict[str, GraphSet] = None 

72 

73# Single Stream Instance 

74class StreamSchema(BaseModel): 

75 zone: str 

76 name: str 

77 t_supply: float | ValueWithUnit 

78 t_target: float | ValueWithUnit 

79 heat_flow: float | ValueWithUnit 

80 dt_cont: float | ValueWithUnit 

81 htc: float | ValueWithUnit 

82 active: Optional[bool] = True 

83 

84# Single Utility Instance 

85class UtilitySchema(BaseModel): 

86 name: str 

87 type: StreamType 

88 t_supply: float | ValueWithUnit 

89 t_target: float | ValueWithUnit 

90 heat_flow: Optional[float] | Optional[ValueWithUnit] 

91 dt_cont: float | ValueWithUnit 

92 htc: float | ValueWithUnit 

93 price: float | ValueWithUnit 

94 active: Optional[bool] = True 

95 

96 class Config: 

97 use_enum_values = True 

98 

99# Zone relationships and nested tree structure 

100class ZoneTreeSchema(BaseModel): 

101 name: str 

102 type: str 

103 children: Optional[List["ZoneTreeSchema"]] = [] 

104 

105# Turbine Option Dict 

106class TurbineOption(BaseModel): 

107 key: TurbineOptionsPropKeys 

108 value: Any 

109 

110 class Config: 

111 use_enum_values = True 

112 

113# Primary Options 

114class Options(BaseModel): 

115 main: List[MainOptionsPropKeys] 

116 # graphs: List[GraphOptionsPropKeys] 

117 turbine: List[TurbineOption] 

118 

119 class Config: 

120 use_enum_values = True 

121 

122# Complete Request 

123class TargetRequest(BaseModel): 

124 streams: List[StreamSchema] 

125 utilities: Optional[List[UtilitySchema]] = [] 

126 options: Optional[Options] = None 

127 zone_tree: Optional[ZoneTreeSchema] = None 

128 

129 

130 

131 

132 

133# Test required schema 

134class THSchema(BaseModel): 

135 T: List[float] 

136 H_hot: Optional[List[float]] = None 

137 H_cold: Optional[List[float]] = None 

138 H_net: Optional[List[float]] = None 

139 H_hot_net: Optional[List[float]] = None 

140 H_cold_net: Optional[List[float]] = None 

141 

142class ProblemTableDataSchema(BaseModel): 

143 name: str 

144 data: THSchema 

145 

146class GetInputOutputData(BaseModel): 

147 plant_profile_data: List[ProblemTableDataSchema] 

148 streams: List[StreamSchema] 

149 utilities: Optional[List[UtilitySchema]] = [] 

150 options: Optional[Options] = None