-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdABC.py
88 lines (71 loc) · 2.24 KB
/
fdABC.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
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
from matplotlib.text import Text
# First set up the figure, the axis, and the plot element we want to animate
fig = plt.figure()
ax = plt.axes(xlim=(0, 200), ylim=(-1, 1))
plt.title('One-Dimensional Wave with Absorbing BC')
plt.xlabel('Space')
plt.ylabel('Amplitude')
line, = ax.plot([], [], lw=1)
class FDTD:
"""1-D Finite Difference Time Domain
"""
def __init__(self):
self.Z0 = 377.5
self.dx = 1
self.N = 200
self.Ez = np.zeros(self.N)
self.Hy = np.zeros(self.N)
self.k = np.ones(self.N)
self.t = 0
self.dt = 1
#Ez is to the left of Hz
def imped(self, index, k):
self.k[index:] = k
def step(self):
# Absorbing Boundary Condition
self.Hy[-1] = self.Hy[-2]
# Update H Field
self.Hy[0:-1] += self.dt/self.dx * (self.Ez[1:] - self.Ez[0:-1]) / self.Z0
# Absorbing Boundary Condition
self.Ez[0] = self.Ez[1]
# Update E Field
self.Ez[1:] += self.dt/self.dx * (self.Hy[1:] - self.Hy[0:-1]) * self.Z0 / self.k[1:]
# Source
self.Ez[50] -= self.dt * self._gauss(self.t)
#self.Ez[0] = np.exp(-(self.t-30)*(self.t-30)/100)
self.t += self.dt
def _gauss(self, t):
return np.exp(-(t-30)*(t-30)/100)
def _sine(self, t):
periods = 3
l = 70
if (t<l):
return np.sin(t* 2 * np.pi* periods/l) * np.exp(-(t-l)*(t-l)/100)
else:
return 0
def steps(self, i):
st = 0
while st<i:
self.step()
st +=1
def state(self):
x = np.linspace(0,self.N-1,self.N)
y = self.Ez
return (x,y)
d1 = FDTD()
# initialization function: plot the background of each frame
def init():
line.set_data([], [])
return line,
# animation function. This is called sequentially
def animate(i):
d1.step()
line.set_data(*d1.state())
return line,
# call the animator. blit=True means only re-draw the parts that have changed.
anim = animation.FuncAnimation(fig, animate, init_func=init,
frames=200, interval=20, blit=True)
plt.show()