Coverage for pyrc \ model \ simple.py: 22%

68 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-13 16:59 +0200

1# ------------------------------------------------------------------------------- 

2# Copyright (C) 2026 Joel Kimmich, Tim Jourdan 

3# ------------------------------------------------------------------------------ 

4# License 

5# This file is part of PyRC, distributed under GPL-3.0-or-later. 

6# ------------------------------------------------------------------------------ 

7 

8import numpy as np 

9 

10from pyrc.core.components.resistor import Resistor 

11from pyrc.core.connecting import connect_cells_with_resistors 

12from pyrc.core.inputs import InteriorBoundaryCondition, ExteriorBoundaryCondition, Radiation 

13from pyrc.core.materials import SandLimeBrick 

14from pyrc.core.network import RCNetwork 

15from pyrc.core.nodes import Node 

16from pyrc.core.resistors import CombinedResistor 

17from pyrc.core.simulation import Simulation 

18from pyrc.postprocessing.parser import FastParser 

19 

20from sympy import Symbol 

21 

22 

23class SimpleWall(RCNetwork): 

24 

25 def __init__(self, *args, layers=None, make_smiley=True, **kwargs): 

26 super().__init__(*args, **kwargs) 

27 

28 self.settings.save_folder_path = r"C:\Simulations\PyRC\demo" 

29 self.settings.weather_data_path = r"C:\Simulations\PyRC\TRY2015_523748130791_Jahr.dat" 

30 self.settings.start_date = "2022-06-01T00:00:00" 

31 self.settings.use_weather_data = True 

32 self.settings.save_all_x_seconds = 60 

33 self.settings.solve_settings.max_step = 2 

34 

35 self.material = SandLimeBrick() 

36 if layers is None: 

37 layers = (5, 1, 1) 

38 if make_smiley: 

39 layers = (layers[0], 8, 7) 

40 self.dimensions = (0.3, 4, 3.5) 

41 else: 

42 self.dimensions = (0.3, 5, 2.75) 

43 self.make_smiley = make_smiley 

44 self.layers = layers 

45 self.initial_temperature = 273.15 + 15 

46 self.inner_temperature = 273.15 + 20 

47 self.outer_temperature = 273.15 

48 

49 self.sim_objects = { 

50 "rc_objects": self.rc_objects, 

51 "rc_solution": self.rc_solution, 

52 "settings": self.settings, 

53 } 

54 

55 self.inner_htc = 4 

56 self.outer_htc = 14 

57 

58 def _create_network(self): 

59 # Create node grid 

60 nodes_matrix: np.ndarray[Node] = Node.create_grid( 

61 grid_size=self.layers, 

62 delta=self.dimensions, 

63 material=self.material, 

64 temperature=self.initial_temperature, 

65 **self.sim_objects 

66 ) 

67 

68 if self.make_smiley: 

69 for coord in [[1, 2], [2, 1], [3, 1], [4, 1], [5, 1], [6, 2], [2, 4], [2, 5], [5, 4], [5, 5]]: 

70 nodes_matrix[-1, *coord].add_internal_heat_source(Radiation, 

71 specific_power_in_w_per_meter_squared=400, 

72 area_direction=np.array((1, 0, 0))) 

73 

74 nodes: list = nodes_matrix.flatten().tolist() 

75 

76 # connect all nodes in the grid with CombinedResistor 

77 resistors: list[Resistor] = connect_cells_with_resistors(nodes) 

78 

79 # add symbol to resistor(s) 

80 resistors_to_change = [] 

81 for i in range(0, self.layers[0] - 1): 

82 resistors_to_change.extend(nodes_matrix[i, 0, 0].get_resistors_between(nodes_matrix[i + 1, 0, 0])) 

83 test_symbol = Symbol("test") 

84 for r in resistors_to_change: 

85 r._resistance = test_symbol 

86 # resistors_to_change[0].resistance 

87 

88 # add Boundary condition on two sides of the wall 

89 

90 bc_inner = InteriorBoundaryCondition( 

91 temperature=self.inner_temperature, 

92 position=np.array([-self.dimensions[0], 0, 0]), # in negative x direction 

93 heat_transfer_coefficient=self.inner_htc, 

94 **self.sim_objects 

95 ) 

96 # connect 

97 cell: Node 

98 for cell in nodes_matrix[0, :, :].flat: 

99 resistors.append(CombinedResistor(htc=self.inner_htc)) 

100 resistors[-1].connect(cell, direction=(-1, 0, 0), node_direction_points_to=bc_inner) 

101 resistors[-1].connect(bc_inner) 

102 

103 bc_outer = ExteriorBoundaryCondition( 

104 temperature=self.outer_temperature, 

105 position=np.array([self.dimensions[0], 0, 0]), # in positive x direction 

106 heat_transfer_coefficient=self.outer_htc, 

107 **self.sim_objects 

108 ) 

109 # connect 

110 cell: Node 

111 for cell in nodes_matrix[-1, :, :].flat: 

112 resistors.append(CombinedResistor(htc=self.outer_htc)) 

113 resistors[-1].connect(cell, direction=(1, 0, 0), node_direction_points_to=bc_outer) 

114 resistors[-1].connect(bc_outer) 

115 

116 self.rc_objects.set_lists(capacitors=nodes, resistors=resistors, boundaries=[bc_inner, bc_outer]) 

117 self.groups["outer"] = nodes_matrix[-1, :, :].flatten().tolist() 

118 self.groups["inner"] = nodes_matrix[0, :, :].flatten().tolist() 

119 

120 

121if __name__ == "__main__": 

122 simulation = Simulation( 

123 network_class=SimpleWall, 

124 pre_calculation_seconds=3600 * 24, 

125 t_span=(0, 3600 * 24), 

126 network_keyword_arguments={"num_cores_jacobian": 1} 

127 ) 

128 simulation.run( 

129 time_dependent_function=lambda t, *args, **kwargs: [0.218 * (1.5 + 0.5 * np.sin(2 * np.pi * t / (86400 / 4)))]) 

130 

131 parser = FastParser((simulation.network, simulation.network.pickle_path_result)) 

132 index = parser.add_node_filter("outer", return_index=True) 

133 

134 simulation.network.rc_solution.write_paraview_data( 

135 r"C:\Simulations\PyRC\demo\paraview", 

136 use_degrees_celsius=True, 

137 time_increment=5 

138 ) 

139 

140 print("Done")