Coverage for teaser/data/output/buildingelement_output.py: 94%

47 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-04-29 16:01 +0000

1"""This module contains function to save building element classes.""" 

2 

3import teaser.logic.utilities as utilities 

4import warnings 

5import collections 

6import json 

7 

8 

9def save_type_element(element, data_class): 

10 """Save information about building element to json. 

11 

12 Saves typical building elements according to their construction 

13 year and their construction type in the json file for type building 

14 elements. If the Project parent is set, it automatically saves it to 

15 the file given in Project.data. Alternatively you can specify a path to 

16 a file of TypeBuildingElements. If this file does not exist, 

17 a new file is created. 

18 

19 Parameters 

20 ---------- 

21 element : BuildingElement() 

22 Instance of BuildingElement or inherited Element of TEASER 

23 

24 data_class : DataClass() 

25 DataClass containing the bindings for TypeBuildingElement and 

26 Material (typically this is the data class stored in prj.data, 

27 but the user can individually change that. 

28 

29 """ 

30 add_to_json = True 

31 

32 warning_text = ( 

33 "Construction Type and building age " 

34 "group already exist in this json, consider revising " 

35 "your inputs. The Element is NOT saved into json" 

36 ) 

37 

38 check_str = "{}_{}_{}".format( 

39 type(element).__name__, element.building_age_group, element.construction_data 

40 ) 

41 

42 if check_str in data_class.element_bind.keys(): 

43 warnings.warn(warning_text) 

44 add_to_json = False 

45 return 

46 

47 if add_to_json is True: 

48 data_class.element_bind[check_str] = collections.OrderedDict() 

49 

50 _set_basic_data_json( 

51 element=element, wall_out=data_class.element_bind[check_str] 

52 ) 

53 

54 _set_layer_data_json( 

55 element=element, wall_out=data_class.element_bind[check_str] 

56 ) 

57 

58 with open(utilities.get_full_path(data_class.path_tb), "w") as file: 

59 file.write( 

60 json.dumps(data_class.element_bind, indent=4, separators=(",", ": ")) 

61 ) 

62 

63 

64def delete_type_element(element, data_class): 

65 """Delete typical element in json. 

66 

67 Deletes typical building elements according to their construction 

68 year and their construction type in the the json file for type building 

69 elements. If the Project parent is set, it automatically saves it to 

70 the file given in Project.data. Alternatively you can specify a path to 

71 a file of TypeBuildingElements. If this file does not exist, 

72 a new file is created. 

73 

74 Parameters 

75 ---------- 

76 element : BuildingElement() 

77 Instance of BuildingElement or inherited Element of TEASER 

78 data_class : DataClass() 

79 DataClass containing the bindings for TypeBuildingElement and 

80 Material (typically this is the data class stored in prj.data, 

81 but the user can individually change that.) 

82 

83 """ 

84 check_str = "{}_{}_{}".format( 

85 type(element).__name__, element.building_age_group, element.construction_data 

86 ) 

87 

88 del data_class.element_bind[check_str] 

89 

90 with open(utilities.get_full_path(data_class.path_tb), "w") as file: 

91 file.write( 

92 json.dumps(data_class.element_bind, indent=4, separators=(",", ": ")) 

93 ) 

94 

95 

96def _set_basic_data_json(element, wall_out): 

97 """Set basic data of building element. 

98 

99 Helper function. 

100 

101 Parameters 

102 ---------- 

103 element : BuildingElement() 

104 Instance of BuildingElement or inherited Element of TEASER 

105 wall_out: dictionary 

106 Dictionary with information about walls. 

107 

108 """ 

109 wall_out["building_age_group"] = element.building_age_group 

110 wall_out["construction_data"] = element.construction_data 

111 wall_out["inner_radiation"] = element.inner_radiation 

112 wall_out["inner_convection"] = element.inner_convection 

113 

114 if type(element).__name__ == "Window": 

115 

116 wall_out["outer_radiation"] = element.outer_radiation 

117 wall_out["outer_convection"] = element.outer_convection 

118 wall_out["g_value"] = element.g_value 

119 wall_out["a_conv"] = element.a_conv 

120 wall_out["shading_g_total"] = element.shading_g_total 

121 wall_out["shading_max_irr"] = element.shading_max_irr 

122 

123 elif ( 

124 type(element).__name__ == "OuterWall" 

125 or type(element).__name__ == "Rooftop" 

126 or type(element).__name__ == "Door" 

127 ): 

128 

129 wall_out["outer_radiation"] = element.outer_radiation 

130 wall_out["outer_convection"] = element.outer_convection 

131 

132 

133def _set_layer_data_json(element, wall_out): 

134 """Set layer data of building element. 

135 

136 Helper function. 

137 

138 Parameters 

139 ---------- 

140 element : BuildingElement() 

141 Instance of BuildingElement or inherited Element of TEASER 

142 wall_out: dictionary 

143 Dictionary with information about walls. 

144 

145 """ 

146 layer_dict = collections.OrderedDict() 

147 for layer in element.layer: 

148 

149 layer_dict[layer.id] = collections.OrderedDict() 

150 layer_dict[layer.id]["thickness"] = layer.thickness 

151 layer_dict[layer.id]["material"] = collections.OrderedDict() 

152 layer_dict[layer.id]["material"]["name"] = layer.material.name 

153 layer_dict[layer.id]["material"]["material_id"] = layer.material.material_id 

154 

155 wall_out["layer"] = layer_dict