-
Notifications
You must be signed in to change notification settings - Fork 0
/
fdtd.py
463 lines (273 loc) · 12.9 KB
/
fdtd.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
import numpy as np
import numba
from misc import (
timer
)
## FUNCTIONS FOR SPEEDUP CPU ===============================================
# @numba.njit
def fast_curl_y_minus(A, dirichlet=False):
B = np.copy(A)
B[1:] -= B[:-1]
if not dirichlet:
B[0] -= A[-1]
return B
# @numba.njit
def fast_curl_y_plus(A, dirichlet=False):
B = np.copy(A)
B[:-1] -= B[1:]
if not dirichlet:
B[-1] -= A[0]
return B
# @numba.njit
def fast_curl_x_plus(A, dirichlet=False):
B = np.copy(A)
B[:,:-1] -= B[:,1:]
if not dirichlet:
B[:,-1] -= A[:,0]
return B
# @numba.njit
def fast_curl_x_minus(A, dirichlet=False):
B = np.copy(A)
B[:,1:] -= B[:,:-1]
if not dirichlet:
B[:,0] -= A[:,-1]
return B
# SIM CPU ===============================================================
class Simulation:
def __init__(self,
bounds=[1,-1,1,-1],
dx=6e-3,
dy=6e-3,
pml_layers=15,
boundary_conditions={"left" :"periodic",
"right" :"periodic",
"bottom" :"pml",
"top" :"pml"}
):
#natural constants
self.c_0 = 3e8
self.eps_0 = 8.854187e-12
self.mu_0 = 4 * np.pi * 1e-7
#simulation properties
self.total_steps = 0
self.bounds = bounds
self.dx = dx
self.dy = dy
self.dt = 0.5 / (self.c_0 * np.sqrt(1/self.dx**2 + 1/self.dy**2)) #CFL
self.boundary_conditions = boundary_conditions
#sources
self.sources = []
#excitation properties
self.f_max = 1 / (100 * self.dt)
#grid size
x_max, x_min, y_max, y_min = self.bounds
self.n_x = int((x_max-x_min)/self.dx)
self.n_y = int((y_max-y_min)/self.dy)
#initialize fields
self.Ez = np.zeros((self.n_x, self.n_y))
self.Dz = np.zeros((self.n_x, self.n_y))
self.Hx = np.zeros((self.n_x, self.n_y))
self.Hy = np.zeros((self.n_x, self.n_y))
#auxiliary terms for integration
self.I_C_Ex = np.zeros((self.n_x, self.n_y))
self.I_C_Ey = np.zeros((self.n_x, self.n_y))
self.I_Dz = np.zeros((self.n_x, self.n_y))
#materials
self.materials = []
self.Eps_z = np.ones((self.n_x, self.n_y))
self.Mu_x = np.ones((self.n_x, self.n_y))
self.Mu_y = np.ones((self.n_x, self.n_y))
#pml boundary condition parameters
self.Nx_lo = pml_layers
self.Nx_hi = pml_layers
self.Ny_lo = pml_layers
self.Ny_hi = pml_layers
#setup update coefficients according to boundary conditions
self._setup_boundary_conditions()
#some values for use outside
self.E_max = 0
# misc -------------------------------------------------------------------------
def _pos_to_index(self, position):
"""
returns the index in simulation grid
to a position within bounds
"""
x, y = position
x_max, x_min, y_max, y_min = self.bounds
x_ind = min(int((x-x_min)/self.dx), self.n_x - 1)
y_ind = min(int((y-y_min)/self.dy), self.n_y - 1)
return x_ind, y_ind
# sources ----------------------------------------------------------------------
def add_source(self, Source):
Source.to_grid(self.bounds, self.Dz)
self.sources.append(Source)
# materials -------------------------------------------------------------------
def _apply_material(self, Material):
Material.to_grid(self.bounds, self.Eps_z)
M_eps_r = Material.bitmap * Material.eps_r
M_mu_r = Material.bitmap * Material.mu_r
self.Eps_z = np.where(M_eps_r >= 1, M_eps_r, self.Eps_z)
self.Mu_x = self.Mu_y = np.where(M_mu_r >= 1, M_mu_r, self.Mu_x)
self.reset()
def add_material(self, Material):
self.materials.append(Material)
self._apply_material(Material)
def _resize_materials(self):
self.Eps_z = np.ones((self.n_x, self.n_y))
self.Mu_x = np.ones((self.n_x, self.n_y))
self.Mu_y = np.ones((self.n_x, self.n_y))
for Material in self.materials:
#add Eps
self._apply_material(Material)
# M = Material.to_grid(self.bounds, self.Eps_z)
# self.Eps_z = np.where(M!=0, M, self.Eps_z)
# pml precomputations ---------------------------------------------------------
def _compute_pml_conductivity(self):
#init pml materials on 2x grid
self.Sigma_x = np.zeros((2*self.n_x, 2*self.n_y))
self.Sigma_y = np.zeros((2*self.n_x, 2*self.n_y))
#base pml conductivity
Sigma_max = self.eps_0 / (2 * self.dt)
# sigma x left side
if self.boundary_conditions["bottom"] == "pml":
for ny in range(self.Ny_lo*2):
ratio = ny / (self.Ny_lo*2)
self.Sigma_x[:, self.Ny_lo*2 - ny - 1] = Sigma_max * ratio**3
# sigma x right side
if self.boundary_conditions["top"] == "pml":
for ny in range(self.Ny_hi*2):
ratio = ny / (self.Ny_hi*2)
self.Sigma_x[:, ny + self.n_y*2 - self.Ny_hi*2] = Sigma_max * ratio**3
# sigma y bottom side
if self.boundary_conditions["left"] == "pml":
for nx in range(self.Nx_hi*2):
ratio = nx / (self.Nx_lo*2)
self.Sigma_y[self.Nx_lo*2 - nx - 1, :] = Sigma_max * ratio**3
# sigma y top side
if self.boundary_conditions["right"] == "pml":
for nx in range(self.Nx_hi*2):
ratio = nx / (self.Nx_hi*2)
self.Sigma_y[nx + self.n_x*2 - self.Nx_hi*2, :] = Sigma_max * ratio**3
def _compute_update_coeffs(self):
#update coefficients for Hx component
Sigma_Hx = self.Sigma_x[1::2, ::2]
Sigma_Hy = self.Sigma_y[1::2, ::2]
m_Hx_0 = 1 / self.dt + Sigma_Hy / (2 * self.eps_0)
self.m_Hx_1 = (1 / self.dt - Sigma_Hy / (2 * self.eps_0)) / m_Hx_0
self.m_Hx_2 = -self.c_0 / self.Mu_x / m_Hx_0
self.m_Hx_3 = -self.c_0 * self.dt / self.eps_0 * Sigma_Hx / self.Mu_x / m_Hx_0
#update coefficients for Hy component
Sigma_Hx = self.Sigma_x[::2, 1::2]
Sigma_Hy = self.Sigma_y[::2, 1::2]
m_Hy_0 = 1 / self.dt + Sigma_Hx / (2 * self.eps_0)
self.m_Hy_1 = (1 / self.dt - Sigma_Hx / (2 * self.eps_0)) / m_Hy_0
self.m_Hy_2 = -self.c_0 / self.Mu_y / m_Hy_0
self.m_Hy_3 = -self.c_0 * self.dt / self.eps_0 * Sigma_Hy / self.Mu_y / m_Hy_0
#update coefficients for Dz
Sigma_Dx = self.Sigma_x[::2, ::2]
Sigma_Dy = self.Sigma_y[::2, ::2]
m_Dz_0 = 1 / self.dt + (Sigma_Dx + Sigma_Dy) / (2 * self.eps_0) + Sigma_Dx * Sigma_Dy * self.dt / (4 * self.eps_0**2)
self.m_Dz_1 = (1 / self.dt - (Sigma_Dx + Sigma_Dy) / (2 * self.eps_0) - Sigma_Dx * Sigma_Dy * self.dt / (2 * self.eps_0)**2) / m_Dz_0
self.m_Dz_2 = self.c_0 / m_Dz_0
self.m_Dz_4 = -self.dt / self.eps_0**2 * Sigma_Dx * Sigma_Dy / m_Dz_0
# @timer
def _setup_boundary_conditions(self):
#precomputations in initialization
self._compute_pml_conductivity()
self._compute_update_coeffs()
# updating with pml ----------------------------------------------------------
# @timer
def _update_Hx_from_E(self):
#compute x component of curl of E
C_Ex = - fast_curl_y_plus(self.Ez, dirichlet=self.boundary_conditions["left"] == "dirichlet") / self.dy
#integration
self.I_C_Ex += C_Ex
#update Hx
self.Hx = self.m_Hx_1 * self.Hx + self.m_Hx_2 * C_Ex + self.m_Hx_3 * self.I_C_Ex
# @timer
def _update_Hy_from_E(self):
#compute y component of curl of E
C_Ey = fast_curl_x_plus(self.Ez, dirichlet=self.boundary_conditions["top"] == "dirichlet") / self.dx
#integration
self.I_C_Ey += C_Ey
#update Hy
self.Hy = self.m_Hy_1 * self.Hy + self.m_Hy_2 * C_Ey + self.m_Hy_3 * self.I_C_Ey
# @timer
def _update_Dz_from_H(self):
#compute z component of curl of H
C_Hz = fast_curl_x_minus(self.Hy, dirichlet=self.boundary_conditions["bottom"] == "dirichlet") / self.dy
C_Hz -= fast_curl_y_minus(self.Hx, dirichlet=self.boundary_conditions["right"] == "dirichlet") / self.dx
#integration of D field
self.I_Dz += self.Dz
#update Dz
self.Dz = self.m_Dz_1 * self.Dz + self.m_Dz_2 * C_Hz + self.m_Dz_4 * self.I_Dz
# @timer
def _update_E_from_D(self):
#update Ez
self.Ez = 1/self.Eps_z * self.Dz
# @timer
def _update_sources_D(self):
#evaluate all sources
for source in self.sources:
self.Dz += source.evaluate_on_grid(self.dt * self.total_steps)
# @timer
def update(self):
#update H
self._update_Hx_from_E()
self._update_Hy_from_E()
#update D
self._update_Dz_from_H()
#inject source
self._update_sources_D()
#calculate E
self._update_E_from_D()
#update steps
self.total_steps += 1
#update all time maximum
self.E_max = max(self.E_max, np.max(abs(self.Ez)))
#reset -----------------------------------------------------------------------
def _reset_sources(self):
#remove sources
self.sources = []
def _reset_fields(self):
#reset fields
self.Ez = np.zeros((self.n_x, self.n_y))
self.Dz = np.zeros((self.n_x, self.n_y))
self.Hx = np.zeros((self.n_x, self.n_y))
self.Hy = np.zeros((self.n_x, self.n_y))
def _reset_materials(self):
#reset materials
self.materials = []
self.Eps_z = np.ones((self.n_x, self.n_y))
self.Mu_x = np.ones((self.n_x, self.n_y))
self.Mu_y = np.ones((self.n_x, self.n_y))
def _reset_integration(self):
#auxiliary terms for integration
self.I_C_Ex = np.zeros((self.n_x, self.n_y))
self.I_C_Ey = np.zeros((self.n_x, self.n_y))
self.I_Dz = np.zeros((self.n_x, self.n_y))
def reset(self):
self._reset_fields()
self._reset_integration()
self._reset_sources()
#self._reset_materials()
#reset steps
self.total_steps = 0
#reset alltime maximum
self.E_max = 0
#reset boundary conditions
self._setup_boundary_conditions()
def resize(self, bounds):
#reset bounds
x_max, x_min, y_max, y_min = self.bounds = bounds
#reset discretization
self.n_x = int((x_max-x_min)/self.dx)
self.n_y = int((y_max-y_min)/self.dy)
#resize materials
self._resize_materials()
#reset simulation
self.reset()
#reset boundary conditions
# self._setup_boundary_conditions()
def get_Ez(self):
return self.Ez