-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path2D_heat_equation_solver.py
170 lines (131 loc) · 5.07 KB
/
2D_heat_equation_solver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#!/usr/bin/env python3
# coding: utf-8
'''
Programme pour résoudre l'équation de la chaleur par la méthodes des différences finies
'''
__author__ = 'Nathan Zimniak'
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from matplotlib.animation import FuncAnimation
from mpl_toolkits import mplot3d
from mpl_toolkits.mplot3d import Axes3D
import time
start_time = time.time()
#Initialisation des constantes
Lx = 201 #Longueur spatiale
Ly = 201 #Largeur spatiale
Nbi = 100001 #Nombre d'itérations temporelles
dx = 1 #Pas spatial suivant x
dy = 1 #Pas spatial suivant y
dt = 1e-2 #Pas temporel
K = 1 #Coefficient de diffusion thermique
#Création du tableau des solutions
T0 = np.zeros((Nbi, Ly, Lx))
Tci = 0 #Condition initiale (température initiale sur toute la plaque)
Tcl1 = 100 #Condition à la limite supérieure de la plaque
Tcl2 = 100 #Condition à la limite inférieure de la plaque
Tcl3 = 100 #Condition à la limite droite de la plaque
Tcl4 = 100 #Condition à la limite gauche de la plaque
T0[:, :, :] = Tci
T0[:, :1, :] = Tcl1
T0[:, (Ly-1):, :] = Tcl2
T0[:, :, (Lx-1):] = Tcl3
T0[:, :, :1] = Tcl4
def finite_difference_method(Z):
''' Calcule la température pour chaque itération temporelle
----------
:param Z: 3D array, tableau des solutions vide
:return: Z, tableau des solutions
----------
'''
for k in range(0, Nbi-1):
for i in range(1, Ly-1):
for j in range(1, Lx-1):
Z[k + 1, i, j] = Z[k][i][j] + K * dt * ((Z[k][i+1][j] + Z[k][i-1][j] - 2*Z[k][i][j])/dx**2 + (Z[k][i][j+1] + Z[k][i][j-1] - 2*Z[k][i][j])/dy**2)
return Z
T = finite_difference_method(T0)
#Affiche le résultat
#Plot (Image 2D)
##plt.style.use('dark_background')
##plt.figure()
##X, Y = np.meshgrid(np.arange(0, Lx), np.arange(0, Ly))
##imageNbi = Nbi-1
##plt.contourf(X, Y, T[imageNbi, :, :], 100, cmap = plt.cm.inferno)
##plt.colorbar()
##plt.xlabel("x")
##plt.ylabel("y")
##plt.title("Température à t = " + str(round(imageNbi*dt,2)) + " s")
##plt.savefig('2D_Heat_Equation.png')
#Plot (Animation 2D)
##plt.style.use('dark_background')
##fig = plt.figure()
##
##def animate(k):
## k=k*100
## plt.clf()
## plt.pcolormesh(T[k, :, :], cmap = plt.cm.inferno)
## plt.colorbar()
## plt.xlabel("x")
## plt.ylabel("y")
## plt.title(f"Température à t = {k*dt:.2f} s")
## return
##
##anim = animation.FuncAnimation(fig, animate, frames = int(Nbi/100), interval = 50, repeat = True)
##
###plt.rcParams['animation.ffmpeg_path'] = 'C:\\ffmpeg\\bin\\ffmpeg.exe'
###Writer = animation.writers['ffmpeg']
###writermp4 = Writer(fps=30, bitrate=1800)
###anim.save("2D_Heat_Equation.mp4", writer=writermp4)
##writergif = animation.PillowWriter(fps=30)
##writergif.setup(fig, "2D_Heat_Equation.gif")
##anim.save("2D_Heat_Equation.gif", writer=writergif)
#Plot (Image 3D)
##plt.style.use('dark_background')
##fig = plt.figure()
##ax = plt.axes(projection = '3d')
##X, Y = np.meshgrid(np.arange(0, Lx), np.arange(0, Ly))
##imageNbi = Nbi-1
##ax.zaxis.set_rotate_label(False)
##surf = ax.plot_surface(X, Y, T[imageNbi, :, :], cmap=plt.cm.inferno)
##ax.set_xlabel("x")
##ax.set_ylabel("y")
##ax.set_zlabel("T (K)", rotation=0)
##ax.w_xaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
##ax.w_yaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
##ax.w_zaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
##ax.grid(False)
##plt.title("Température à t = " + str(round(imageNbi*dt,2)) + " s")
##plt.savefig('3D_Heat_Equation.png')
#Plot (Animation 3D)
plt.style.use('dark_background')
fig = plt.figure()
ax = plt.axes(projection = '3d')
X, Y = np.meshgrid(np.arange(0, Lx), np.arange(0, Ly))
def Animate3D(k):
k=k*100
ax.clear()
ax.set_zlim3d(0, np.max(T))
ax.zaxis.set_rotate_label(False)
ax.plot_surface(X, Y, T[k, :, :], cmap=plt.cm.inferno)
ax.set_xlabel("x")
ax.set_ylabel("y")
ax.set_zlabel("T (K)", rotation=0)
#fig.set_facecolor('black')
#ax.set_facecolor('black')
ax.w_xaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.w_yaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.w_zaxis.set_pane_color((0.0, 0.0, 0.0, 0.0))
ax.grid(False)
plt.title(f"Température à t = {k*dt:.2f} s")
ax.view_init(azim=k/100)
return
anim3D = animation.FuncAnimation(fig, Animate3D, frames = int(Nbi/100), interval = 50, blit = False, repeat = True)
##plt.rcParams['animation.ffmpeg_path'] = 'C:\\ffmpeg\\bin\\ffmpeg.exe'
##Writer = animation.writers['ffmpeg']
##writermp4 = Writer(fps=30, metadata=dict(artist='Me'), bitrate=1800)
##anim3D.save("3D_Schrodinger_Equation.mp4", writer=writermp4)
writergif = animation.PillowWriter(fps=30)
anim3D.save("3D_Heat_Equation.gif", writer=writergif)
print("%s secondes" % (time.time() - start_time))
plt.show()