Advertisement
mirosh111000

Мірошниченко_Артем_ПМ-11_Лр№7_Мет_комп_експ

May 5th, 2024
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.85 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. # Параметри пластини
  5. Lx = 100
  6. Ly = 100
  7. time_max = 10
  8. T1 = 0
  9. T2 = 1
  10. D = 10
  11. dt = 0.001
  12.  
  13. # Параметри джерела
  14. rect_x = Lx // 4
  15. rect_y = Ly // 4
  16. rect_width = Lx // 2
  17. rect_height = Ly // 2
  18.  
  19. T = np.ones((Ly + 2, Lx + 2)) * T1
  20. T[rect_y:rect_y + rect_height, rect_x:rect_x + rect_width] = T2
  21.  
  22. def calc_next(T, D, dt):
  23.     TempMas = np.copy(T)
  24.     for j in range(1, Ly + 1):
  25.         for i in range(1, Lx + 1):
  26.             TempMas[j, i] = T[j, i] + D * dt * (
  27.                 T[j, i + 1] + T[j, i - 1] + T[j + 1, i] + T[j - 1, i] - 4 * T[j, i]
  28.             )
  29.     return TempMas
  30.  
  31.  
  32. T_list = []
  33. time_points = []  
  34. diff_history = []
  35. temperature_history = []
  36. t = dt
  37. while t <= time_max:
  38.     T = calc_next(T, D, dt)
  39.     average_temp = np.mean(T)
  40.     temperature_history.append(average_temp)  
  41.     time_points.append(t)
  42.     diff = np.mean(T**2) - average_temp**2
  43.     diff_history.append(diff)
  44.     if round(t * 1000) % 1000 == 0 or t == dt:
  45.         T_list.append(np.copy(T))
  46.     t += dt
  47.  
  48.  
  49. fig, axs = plt.subplots(nrows=len(T_list)//3+1, ncols=3, figsize=(15, 5*(len(T_list)//3)+1))
  50. for idx, t_value in enumerate(np.linspace(0, time_max, len(T_list))):
  51.     ax = axs[idx//3, idx%3]
  52.     im = ax.imshow(T_list[idx], cmap='inferno', interpolation='nearest', vmin=T1, vmax=T2)
  53.     ax.set_title(fr"$Time: {round(t_value, 6)}$")
  54.     fig.colorbar(im, ax=ax)
  55.     ax.set_xticks([])
  56.     ax.set_yticks([])
  57.     ax.grid(False)
  58.  
  59.  
  60. plt.figure(figsize=(10, 5))
  61. plt.plot(time_points, temperature_history, color='red')
  62. plt.xlabel(fr'$Час$')
  63. plt.ylabel(fr'$Середня\ температура$')
  64. plt.title(fr'$Залежність\ середньої\ температури\ від\ часу$')
  65. plt.grid(True)
  66. plt.show()
  67.  
  68.  
  69. plt.figure(figsize=(10, 5))
  70. plt.plot(time_points, diff_history)
  71. plt.xlabel(fr'$Час$')
  72. plt.ylabel(fr'$<T^2>-<T>^2$')
  73. plt.title(fr"$Залежність\ <T^2>-<T>^2\ від\ часу$")
  74. plt.grid(True)
  75. plt.tight_layout()
  76. plt.show()
  77.  
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86. T = np.ones((Ly + 2, Lx + 2)) * T1
  87. T[rect_y:rect_y + rect_height, rect_x:rect_x + rect_width] = T2
  88.  
  89. def calc_next2(T, D, dt):
  90.     TempMas = np.copy(T)
  91.     for j in range(1, Ly + 1):
  92.         for i in range(1, Lx + 1):
  93.             if not (rect_y <= j < rect_y + rect_height and rect_x <= i < rect_x + rect_width):
  94.                 TempMas[j, i] = T[j, i] + D * dt * (T[j, i + 1] + T[j, i - 1] + T[j + 1, i] + T[j - 1, i] - 4 * T[j, i])
  95.     return TempMas
  96.  
  97. T_list = []
  98. time_points = []
  99. diff_history = []
  100. temperature_history = []
  101. t = dt
  102. while t <= time_max:
  103.     T = calc_next2(T, D, dt)
  104.     average_temp = np.mean(T)
  105.     temperature_history.append(average_temp)  
  106.     time_points.append(t)
  107.     diff = np.mean(T**2) - np.mean(T)**2
  108.     diff_history.append(diff)
  109.     if round(t * 1000) % 1000 == 0 or t == dt:
  110.         T_list.append(T)
  111.     t += dt
  112.  
  113.    
  114.    
  115. fig, axs = plt.subplots(nrows=len(T_list)//3+1, ncols=3, figsize=(15, 5*(len(T_list)//3+1)))
  116. for idx, t_value in enumerate(np.linspace(0, time_max, len(T_list))):
  117.     ax = axs[idx//3, idx%3]
  118.     im = ax.imshow(T_list[idx], cmap='inferno', interpolation='nearest', vmin=T1, vmax=T2)
  119.     ax.set_title(fr"$Time: {round(t_value, 6)}$")
  120.     fig.colorbar(im, ax=ax)
  121.     ax.set_xticks([])
  122.     ax.set_yticks([])
  123.     ax.grid(False)
  124.  
  125.    
  126.    
  127. plt.figure(figsize=(10, 5))
  128. plt.plot(time_points, temperature_history)
  129. plt.xlabel(fr'$Час$')
  130. plt.ylabel(fr'$Середня\ температура$')
  131. plt.title(fr"$Залежність\ середньої\ температури\ від\ часу$")
  132. plt.grid(True)
  133.  
  134. plt.figure(figsize=(10, 5))
  135. plt.plot(time_points, diff_history)
  136. plt.xlabel(fr'$Час$')
  137. plt.ylabel(fr'$<T^2>-<T>^2$')
  138. plt.title(fr"$Залежність\ <T^2>-<T>^2 \ від\ часу$")
  139. plt.grid(True)
  140.  
  141. plt.tight_layout()
  142. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement