Advertisement
foreverfugazi

FDTD pulse hitting dielectric medium

Sep 15th, 2024
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.43 KB | Source Code | 0 0
  1. import numpy as np
  2. from math import exp
  3. from matplotlib import pyplot as plt
  4.  
  5. ke = 200
  6. ex = np.zeros(ke)
  7. hy = np.zeros(ke)
  8.  
  9. t0 = 40
  10. spread = 12
  11.  
  12. boundary_low = [0, 0]
  13. boundary_high = [0, 0]
  14.  
  15. # create dielectric profile
  16. cb = np.ones(ke)
  17. cb = 0.5 * cb
  18. cb_start = 100
  19. epsilon = 4
  20. cb[cb_start:] = 0.5 / epsilon
  21.  
  22. nsteps = 540
  23.  
  24. # dictionary to keep track of desired points for plotting
  25. plotting_points = [
  26.     {'num_steps': 100, 'data_to_plot': None, 'label': ''},
  27.     {'num_steps': 220, 'data_to_plot': None, 'label': ''},
  28.     {'num_steps': 320, 'data_to_plot': None, 'label': ''},
  29.     {'num_steps': 540, 'data_to_plot': None, 'label': 'FDTDcells'}
  30. ]
  31.  
  32. # main FDTD Loop
  33. for time_step in range(1, nsteps + 1):
  34.  
  35.     # calculate Ex field
  36.     for k in range(1, ke):
  37.         ex[k] = ex[k] + cb[k] * (hy[k - 1] - hy[k])
  38.  
  39.     # put a gaussian pulse at the low end
  40.     pulse = exp(-0.5 * ((t0 - time_step) / spread) ** 2)
  41.     ex[5] = pulse + ex[5]
  42.  
  43.     ex[0] = boundary_low.pop(0)
  44.     boundary_low.append(ex[1])
  45.  
  46.     ex[ke - 1] = boundary_high.pop(0)
  47.     boundary_high.append(ex[ke - 2])
  48.  
  49.     # calculate the Hy field
  50.     for k in range(ke - 1):
  51.         hy[k] = hy[k] + 0.5 * (ex[k] - ex[k + 1])
  52.  
  53.     # solve data at certain points for later plotting
  54.     for plotting_point in plotting_points:
  55.         if time_step == plotting_point['num_steps']:
  56.             plotting_point['data_to_plot'] = np.copy(ex)
  57.  
  58. # plot the outputs
  59. plt.rcParams['font.size'] = 12
  60. fig = plt.figure(figsize=(8, 7))
  61.  
  62. def plot_e_field(data, timestep, epsilon, cb, label):
  63.     plt.plot(data, color='k', linewidth=1)
  64.     plt.ylabel('E$_x$', fontsize=14)
  65.     plt.xticks(np.arange(0, 199, step=20))
  66.     plt.xlim(0, 199)
  67.     plt.yticks(np.arange(-0.5, 1.2, step=0.5))
  68.     plt.ylim(-0.5, 1.2)
  69.     plt.text(70, 0.5, 'T = {}'.format(timestep), horizontalalignment='center')
  70.     plt.plot((0.5 / cb - 1) / 3, 'k--', linewidth=0.75)
  71.     # math on cb above just for scaling
  72.     plt.text(170, 0.5, 'Eps = {}'.format(epsilon), horizontalalignment='center')
  73.     plt.xlabel('{}'.format(label))
  74.  
  75. # plot E field at each time step saved earlier
  76. for subplot_num, plotting_point in enumerate(plotting_points):
  77.     ax = fig.add_subplot(4, 1, subplot_num + 1)
  78.     plot_e_field(plotting_point['data_to_plot'],
  79.                  plotting_point['num_steps'], epsilon, cb,
  80.                  plotting_point['label'])
  81.  
  82. plt.subplots_adjust(bottom=0.1, hspace=0.45)
  83.  
  84. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement