Coverage for pyrc \ validation \ conduction \ analytic.py: 0%

33 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 

8# Script for validation 

9# 

10# Calculation of the solution of stationary heat conduction in different materials 

11# 

12# Analytic solution 

13# 

14import numpy as np 

15 

16from pyrc.visualization.plot import LinePlot 

17 

18 

19def line_heat_conduction( 

20 range_x: list | np.ndarray | tuple, 

21 thickness: float | int, 

22 delta_temperature: float | int, 

23 lower_temperature: float | int = 0, 

24) -> np.ndarray: 

25 """ 

26 

27 Parameters 

28 ---------- 

29 range_x : list | np.ndarray | tuple 

30 The x values to calculate the temperature for. 

31 thickness : float | int 

32 The thickness of the material in m. 

33 delta_temperature : float | int 

34 The temperature difference in K. 

35 lower_temperature : float | int, optional 

36 The lower temperature in K or °C. 

37 

38 Returns 

39 ------- 

40 np.ndarray : 

41 The temperature values for the given range_x through the material. 

42 """ 

43 range_x = np.array(range_x) 

44 

45 return lower_temperature + range_x / thickness * delta_temperature 

46 

47 

48def major_temperatures( 

49 walls: list, 

50 lower_temperature: float | int, 

51 upper_temperature: float | int, 

52): 

53 """ 

54 

55 Parameters 

56 ---------- 

57 walls : list 

58 Tuples defining the walls in ascending temperature order. 

59 Each tuple consists of: 

60 1. thickness in m 

61 2. thermal conductivity coefficient in W/m/K 

62 lower_temperature : float | int 

63 The lower temperature in K 

64 upper_temperature : float | int 

65 The upper temperature in K 

66 

67 Returns 

68 ------- 

69 list : 

70 The temperatures between and at start/beginning of the walls. 

71 """ 

72 

73 if len(walls) == 1: 

74 return [lower_temperature, upper_temperature] 

75 

76 # calculate heat transfer coefficient k 

77 k = get_k([t / tc for t, tc in walls]) 

78 

79 # specific heat flux 

80 spec_heat_flux = k * (upper_temperature - lower_temperature) 

81 

82 result = [lower_temperature] 

83 for i, wall in enumerate(walls): 

84 s, conductivity = wall 

85 result.append(result[-1] + spec_heat_flux * s / conductivity) 

86 

87 return result 

88 

89 

90def get_k(resistance_terms: list): 

91 """ 

92 Calculates k. 

93 

94 Parameters 

95 ---------- 

96 resistance_terms : list 

97 The resistance terms. E.g.: thickness/thermal_conductivity or 1/alpha. 

98 

99 Returns 

100 ------- 

101 float : 

102 k in W/K/m/m 

103 """ 

104 return 1 / sum(resistance_terms) 

105 

106 

107def make_temp_data( 

108 walls: list, 

109 lower_temperature: float | int, 

110 upper_temperature: float | int, 

111 number_points: int, 

112) -> tuple[np.ndarray, np.ndarray]: 

113 temps = major_temperatures(walls, lower_temperature, upper_temperature) 

114 

115 thickness_list = [j[0] for j in walls] 

116 x_borders = np.cumsum([0, *thickness_list]) 

117 x_values = x_borders 

118 y_values = np.array(temps) 

119 # number_points 

120 # total_thickness = sum(thickness_list) 

121 # increment = total_thickness / (number_points - 1) 

122 # x_values = np.array([n*increment for n in range(number_points)]) 

123 # 

124 # y_values = np.zeros_like(x_values, dtype=float) 

125 # 

126 # current_temp = lower_temperature 

127 # for i in range(0, len(temps)-1): 

128 # mask = (x_values<x_borders[i+1]) & (x_values >= x_borders[i]) 

129 # y_values[mask] = line_heat_conduction( 

130 # x_values[mask] - x_borders[i], 

131 # thickness_list[i], 

132 # delta_temperature=temps[i], 

133 # lower_temperature=current_temp 

134 # ) 

135 # current_temp = y_values[mask][-1] 

136 

137 return x_values, y_values 

138 

139 

140def run_conduction_analytic(plot_data=True): 

141 data = make_temp_data( 

142 [ 

143 (0.1, 1), 

144 (0.2, 3), 

145 (0.5, 10), 

146 (0.1, 0.3), 

147 (0.1, 30), 

148 ], 

149 -11, 

150 20, 

151 number_points=300, 

152 ) 

153 

154 if plot_data: 

155 plot = LinePlot(x=data[0], ys=data[1], y_title="Temperature / °C") 

156 plot.plot() 

157 # plot.save("/home/kimmichjl/testplot.png") 

158 plot.save(r"C:\Simulations\PyRC\debug\conduction_analytic.png") 

159 else: 

160 return data[0], data[1] 

161 

162 

163if __name__ == "__main__": 

164 run_conduction_analytic(True)