-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheq_python_2d.py
292 lines (205 loc) · 8.07 KB
/
eq_python_2d.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
"""
eq_python_2d.py
2D WAVE-EQUATION using explicite finite difference method
2D WAVE-EQUATION: u_{tt} = c *c* ( u_{xx} + u_{yy} )
F. Audard
"""
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
class wave2d(object):
def __init__(self,height,width,T,nx,ny,nt,c):
self.x = np.linspace(-0.5*width,0.5*width,nx)
self.y = np.linspace(-0.5*height,0.5*height,ny)
self.t = np.linspace(0,T,nt+1)
self.dx = self.x[1]-self.x[0]
self.dy = self.y[1]-self.y[0]
self.dt = self.t[1]-self.t[0]
self.xx,self.yy = np.meshgrid(self.x,self.y)
# Gamma_x squared
self.gx2 = c*self.dt/self.dx
# Gamma_y squared
self.gy2 = c*self.dt/self.dy
# 2*(1-gamma_x^2-gamma_y^2)
self.gamma = 2*(1 - self.gx2 - self.gy2)
def solve(self,ffun,gfun):
BC =1
f = ffun(self.xx,self.yy)
g = gfun(self.xx,self.yy)
u = np.zeros((nx,ny,nt+1))
# Set initial condition
u[:,:,0] = f
""" Compute first time step """
u[:,:,1] = 0.5*self.gamma*f+self.dt*g
u[1:-1,1:-1,1] += 0.5*self.gx2*(f[1:-1,2:]+f[1:-1,:-2])
u[1:-1,1:-1,1] += 0.5*self.gy2*(f[:-2,1:-1]+f[2:,1:-1])
for k in range(1,nt):
# Every point contains these terms
u[:,:,k+1] = self.gamma*u[:,:,k] - u[:,:,k-1]
# Interior points
u[1:-1,1:-1,k+1] += self.gx2*(u[1:-1,2:,k]+u[1:-1,:-2,k]) + \
self.gy2*(u[2:,1:-1,k]+u[:-2,1:-1,k])
# Dirchlet condition
# if bc['W'] is None:
if BC == 1:
# Top boundary
u[1,1:-1,k+1] += 0
# Right boundary
u[1:-1,-2,k+1] += 0
# Bottom boundary
u[-1,1:-2,k+1] += 0
# Left boundary
u[1:-1,1,k+1] += 0
# Top right corner
u[1,-2,k+1] += 0
# Bottom right corner
u[-2,-2,k+1] += 0
# Bottom left corner
u[-2,1,k+1] += 0
# Top left corner
u[1,1,k+1] += 0
# Neumann condition
elif BC ==2:
# Top boundary
u[0,1:-1,k+1] += 2*self.gy2*u[1,1:-1,k] + \
self.gx2*(u[0,2:,k]+u[0,:-2,k])
# Right boundary
u[1:-1,-1,k+1] += 2*self.gx2*u[1:-1,-2,k] + \
self.gy2*(u[2:,-1,k]+u[:-2,-1,k])
# Bottom boundary
u[-1,1:-1,k+1] += 2*self.gy2*u[-2,1:-1,k] + \
self.gx2*(u[-1,2:,k]+u[-1,:-2,k])
# Left boundary
u[1:-1,0,k+1] += 2*self.gx2*u[1:-1,1,k] + \
self.gy2*(u[2:,0,k]+u[:-2,0,k])
# Top right corner
u[0,-1,k+1] += 2*self.gx2*u[0,-2,k] + \
2*self.gy2*u[1,-1,k]
# Bottom right corner
u[-1,-1,k+1] += 2*self.gx2*u[-1,-2,k] + \
2*self.gy2*u[-2,-1,k]
# Bottom left corner
u[-1,0,k+1] += 2*self.gx2*u[-1,1,k] + \
2*self.gy2*u[-2,0,k]
# Top left corner
u[0,0,k+1] += 2*self.gx2*u[0,1,k] + \
2*self.gy2*u[1,0,k]
elif BC ==3:
# Top boundary
u[0,1:-1,k+1] += u[1,1:-1,k]
# Right boundary
u[1:-1,-1,k+1] += u[1:-1,-2,k]
# Bottom boundary
u[-1,1:-1,k+1] += u[-2,1:-1,k]
# Left boundary
u[1:-1,0,k+1] += u[1:-1,1,k]
# Top right corner
u[0,-1,k+1] += u[0,-2,k]
# Bottom right corner
u[-1,-1,k+1] += u[-1,-2,k]
# Bottom left corner
u[-1,0,k+1] += u[-1,1,k]
# Top left corner
u[0,0,k+1] += u[0,1,k]
return u
if __name__ == '__main__':
# Center domain is in center !
# . . W/,L/2
# . 0 .
# -W/,-L/2 . .
# Final time
T = 0.01
# Domain dimensions
height = 10. #2
width = 10. #4
# Wave speed
c = 343
# Number of time steps
nt = 400
# Grid points in x direction
nx = 125 # 250
# Grid points in y direction
ny = 125
# Source term position
xpos = 0.0
ypos = -0.5
# microphone term position
x_micro1 = -3.5
y_micro1 = -1.5
x_micro2 = -1.5
y_micro2 = -1.5
x_micro3 = 1.5
y_micro3 = -1.5
x_micro4 = 3.5
y_micro4 = -1.5
wave_eq = wave2d(height,width,T,nx,ny,nt,c)
# Initial value functions
f = lambda x,y: np.exp(-10*((x-xpos)**2+(y-ypos)**2))
g = lambda x,y: 0
u = wave_eq.solve(f,g)
x = wave_eq.x
y = wave_eq.y
frames = []
#fig = plt.figure(1,(16,8))
# find node arround microphone
xm1 = (round(x_micro1/(width/nx)))
ym1 = (round(y_micro1/(height/ny)))
xm2 = (round(x_micro2/(width/nx)))
ym2 = (round(y_micro2/(height/ny)))
xm3 = (round(x_micro3/(width/nx)))
ym3 = (round(y_micro3/(height/ny)))
xm4 = (round(x_micro4/(width/nx)))
ym4 = (round(y_micro4/(height/ny)))
# if positive domain we double
if xm1>0 :
xm1 +=xm1
if xm2>0 :
xm2 +=xm2
if xm3>0 :
xm3 +=xm3
if xm4>0 :
xm4 +=xm4
if ym1>0 :
ym1 +=ym1
if ym2>0 :
ym2 +=ym2
if ym3>0 :
ym3 +=ym3
if ym4>0 :
ym4 +=ym4
xm1 = abs(xm1)
ym1 = abs(ym1)
xm2 = abs(xm2)
ym2 = abs(ym2)
xm3 = abs(xm3)
ym3 = abs(ym3)
xm4 = abs(xm4)
ym4 = abs(ym4)
# Setup figure and subplots
fig = plt.figure(1,(16,8))
ax1=fig.add_subplot(1,2,1)
ax2=fig.add_subplot(1,2,2)
for k in range(nt+1):
frame = ax1.imshow(u[:,:,k],extent=[x[0],x[-1],y[0],y[-1]])
# for view position of your microphone
frame1, = ax1.plot(x_micro1,y_micro1, 'ro')
frame2, = ax1.plot(x_micro2,y_micro2, 'bo')
frame3, = ax1.plot(x_micro3,y_micro3, 'ks')
frame4, = ax1.plot(x_micro4,y_micro4, 'gs')
p1, = ax2.plot(k,u[xm1,ym1,k], 'ro')
p2, = ax2.plot(k,u[xm2,ym2,k], 'bo')
p3, = ax2.plot(k,u[xm3,ym3,k], 'ks')
p4, = ax2.plot(k,u[xm4,ym4,k], 'gs')
frames.append([frame, frame1,frame2,frame3,frame4, p1, p2, p3, p4])
# frames.append([frame, p1, p2, p3, p4])
plt.legend([p1, p2, p3, p4], ["1", "2", "3", "4"])
ani = animation.ArtistAnimation(fig,frames,interval=50,
blit=True,repeat_delay=1000)
ani.save('wave2d.mp4')
plt.show()
#Show final iteration and evolution of variable in captor
# fig2 = plt.figure(1,(16,8))
# plt.plot(k,u[xm1,ym1,k], 'ro', k,u[xm2,ym2,k], 'bo', k,u[xm3,ym3,k], 'ks', k,u[xm4,ym4,k], 'gs')
# frames.append([frame, frame1,frame2,frame3,frame4, p1, p2, p3, p4])
# frames.append([frame, p1, p2, p3, p4])
# plt.show()