Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import math
- import matplotlib.pyplot as plt
- import matplotlib.animation as animation
- from time import sleep
- imp = 377
- x_steps = 100
- t_steps = 100
- x_borders = 1.0
- time = 1.0
- dx = x_borders/x_steps
- dt = time/t_steps
- s = dt/dx
- print(s)
- print(dt,dx)
- mid = int(x_steps/2)
- def init():
- Ez = np.zeros(x_steps)
- Hy = np.zeros(x_steps)
- return Ez,Hy
- def solve(i):
- if i > 148:
- sleep(5)
- Ez[0] = 0
- Hy[x_steps-1] = 0
- for j in range(x_steps-1):
- Hy[j] = Hy[j] + (Ez[j+1]-Ez[j])/imp
- for j in range(1,x_steps):
- Ez[j] = Ez[j] + (Hy[j]-Hy[j-1])*imp
- # if i*dt < 3:
- Ez[mid] += math.exp(-(i - 0.1/dt )**2/(0.5/dt))
- ax.clear()
- ax2.clear()
- coor = np.linspace(0,x_borders,x_steps)
- ax.plot(coor,Ez)
- ax2.plot(coor,Hy)
- # ax.set_ylim([-2,2])
- # ax2.set_ylim()
- tmp1 = dt*i
- s = "%.2f" % tmp1
- # print(s)
- ax.set_title('Time '+s +' sec ' + str(i))
- ax2.set_title('Magnetic')
- # fig.title('FDTD')
- # fig.ylabel('Magnitude')
- # ax.xlabel('Coordinates')
- # ax.xlim(time)
- pause = False
- Ez,Hy = init()
- fig= plt.figure()
- ax = fig.add_subplot(211)
- ax2 = fig.add_subplot(212)
- ani = animation.FuncAnimation(fig,solve,interval=1)
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement