Advertisement
shadvoll

fdtd

Jun 2nd, 2018
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 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 = 377
  7. x_steps = 100
  8. t_steps = 100
  9. x_borders = 1.0
  10. time = 1.0
  11.  
  12. dx = x_borders/x_steps
  13. dt = time/t_steps
  14. s = dt/dx
  15. print(s)
  16. print(dt,dx)
  17.  
  18. mid = int(x_steps/2)
  19. def init():
  20.     Ez = np.zeros(x_steps)
  21.     Hy = np.zeros(x_steps)
  22.     return Ez,Hy
  23. def solve(i):
  24.     if i > 148:
  25.         sleep(5)
  26.     Ez[0] = 0
  27.     Hy[x_steps-1] = 0
  28.     for j in range(x_steps-1):
  29.         Hy[j] = Hy[j] + (Ez[j+1]-Ez[j])/imp
  30.     for j in range(1,x_steps):
  31.         Ez[j] = Ez[j] + (Hy[j]-Hy[j-1])*imp
  32.     # if i*dt < 3:
  33.     Ez[mid] += math.exp(-(i - 0.1/dt )**2/(0.5/dt))
  34.     ax.clear()
  35.     ax2.clear()
  36.     coor = np.linspace(0,x_borders,x_steps)    
  37.     ax.plot(coor,Ez)
  38.     ax2.plot(coor,Hy)
  39.     # ax.set_ylim([-2,2])
  40.     # ax2.set_ylim()
  41.     tmp1 = dt*i
  42.     s = "%.2f" % tmp1
  43.     # print(s)
  44.     ax.set_title('Time '+s +' sec ' + str(i))
  45.     ax2.set_title('Magnetic')
  46.     # fig.title('FDTD')
  47.     # fig.ylabel('Magnitude')
  48.     # ax.xlabel('Coordinates')
  49.     # ax.xlim(time)
  50. pause = False
  51.  
  52. Ez,Hy = init()
  53. fig= plt.figure()
  54. ax = fig.add_subplot(211)
  55. ax2 = fig.add_subplot(212)
  56. ani = animation.FuncAnimation(fig,solve,interval=1)
  57. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement