Advertisement
shadvoll

fdtd_ABC

Jun 2nd, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.26 KB | None | 0 0
  1. import numpy as np
  2. import math
  3. import matplotlib.pyplot as plt
  4. import matplotlib.animation as animation
  5. from time import sleep
  6. imp = 337.0
  7. x_steps = 100
  8. t_steps = 100
  9.  
  10. x_borders = 1.0
  11. time = 1.0
  12.  
  13. dx = x_borders/x_steps
  14. dt = time/t_steps
  15. s = dt/dx
  16.  
  17. print(s)
  18. print(dt,dx)
  19.  
  20. mid = int(x_steps/2)
  21. def init():
  22.     Ez = np.zeros(x_steps)
  23.     Hy = np.zeros(x_steps)
  24.     return Ez,Hy
  25. def solve(i):
  26.  
  27.     Hy[x_steps-1] = Hy[x_steps-2]    
  28.     for j in range(x_steps-1):
  29.         Hy[j] = Hy[j] + (Ez[j+1]-Ez[j])/imp
  30.     Ez[0] = Ez[1]    
  31.     for j in range(1,x_steps):
  32.         Ez[j] = Ez[j] +(Hy[j]-Hy[j-1])*imp
  33.     Ez[mid] = math.exp(-(i-0.5/dt)**2/(0.5/dt))
  34.  
  35.     Hy[x_steps-1] = Hy[x_steps-2]    
  36.     for j in range(x_steps-1):
  37.         Hy[j] = Hy[j] + (Ez[j+1]-Ez[j])/imp
  38.     Ez[0] = Ez[1]    
  39.     for j in range(1,x_steps):
  40.         Ez[j] = Ez[j] +(Hy[j]-Hy[j-1])*imp
  41.     Ez[mid]= math.exp(-((i+1.0)-0.5/dt)**2/(0.5/dt))
  42.  
  43.     ax.clear()
  44.     coor = np.linspace(0,x_borders,x_steps)    
  45.     ax.plot(coor,Ez)
  46.     tmp1 = dt*i
  47.     s = "%.2f" % tmp1
  48.     ax.set_title('Time '+s +' sec ' + str(i))
  49.  
  50. Ez,Hy = init()
  51. fig= plt.figure()
  52. ax = fig.add_subplot(111)
  53. # ax2 = fig.add_subplot(212)
  54. ani = animation.FuncAnimation(fig,solve,interval=1)
  55. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement