Advertisement
foreverfugazi

CHATGPT FTW

Sep 16th, 2024
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. import numpy as np
  2. from math import exp
  3. from matplotlib import pyplot as plt
  4.  
  5. def run_fdtd(factor):
  6.     ke = 200
  7.     ex = np.zeros(ke)
  8.     hy = np.zeros(ke)
  9.  
  10.     # pulse params
  11.     kc = int(ke / 2)
  12.     t0 = 40
  13.     spread = 12
  14.  
  15.     boundary_low = [0, 0]
  16.     boundary_high = [0, 0]
  17.  
  18.     nsteps = 250
  19.  
  20.     # Dictionary to keep track of plot points
  21.     plotting_points = [
  22.         {'num_steps': 20, 'data_to_plot': None, 'label': ''},
  23.         {'num_steps': 225, 'data_to_plot': None, 'label': ''},
  24.         {'num_steps': 250, 'data_to_plot': None, 'label': 'FDTDcells'},
  25.     ]
  26.  
  27.     # main FDTD loop
  28.     for time_step in range(1, nsteps + 1):
  29.         # calculate Ex field
  30.         for k in range(1, ke):
  31.             ex[k] = ex[k] + factor * (hy[k - 1] - hy[k])
  32.  
  33.         # put a gaussian pulse at the low end
  34.         pulse = exp(-0.5 * ((t0 - time_step) / spread) ** 2)
  35.         ex[kc] = pulse
  36.  
  37.         # update boundary conditions
  38.         ex[0] = boundary_low.pop(0)
  39.         boundary_low.append(ex[1])
  40.  
  41.         ex[ke - 1] = boundary_high.pop(0)
  42.         boundary_high.append(ex[ke - 2])
  43.  
  44.         # calculate the Hy field
  45.         for k in range(ke - 1):
  46.             hy[k] = hy[k] + factor * (ex[k] - ex[k + 1])
  47.  
  48.         # store data at specific time steps for plotting
  49.         for plotting_point in plotting_points:
  50.             if time_step == plotting_point['num_steps']:
  51.                 plotting_point['data_to_plot'] = np.copy(ex)
  52.  
  53.     return plotting_points
  54.  
  55. # Run simulations with factors 0.5, 1.0, 1.1, and 0.25
  56. factors = [0.5, 1.0, 1.1, 0.25]
  57. plot_data = {factor: run_fdtd(factor) for factor in factors}
  58.  
  59. # Plotting the results
  60. plt.rcParams['font.size'] = 12
  61. fig, axs = plt.subplots(len(factors), 3, figsize=(12, 12))
  62.  
  63. def plot_e_field(ax, data, timestep, label, factor):
  64.     ax.plot(data, color='k', linewidth=1)
  65.     ax.set_ylabel('E$_x$', fontsize=10)
  66.     ax.set_xticks(np.arange(0, 199, step=20))
  67.     ax.set_xlim(0, 199)
  68.     ax.set_yticks(np.arange(-1.2, 1.2, step=1))
  69.     ax.set_ylim(-1.2, 1.2)
  70.     ax.text(100, 0.5, f'T = {timestep}', horizontalalignment='center', fontsize=9)
  71.     ax.set_xlabel(f'{label} (Factor={factor})', fontsize=10)
  72.  
  73. for i, (factor, plots) in enumerate(plot_data.items()):
  74.     for j, plotting_point in enumerate(plots):
  75.         plot_e_field(axs[i, j],
  76.                      plotting_point['data_to_plot'],
  77.                      plotting_point['num_steps'],
  78.                      plotting_point['label'],
  79.                      factor)
  80.  
  81. plt.tight_layout()
  82. plt.show()
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement