Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from math import exp
- from matplotlib import pyplot as plt
- def run_fdtd(factor):
- ke = 200
- ex = np.zeros(ke)
- hy = np.zeros(ke)
- # pulse params
- kc = int(ke / 2)
- t0 = 40
- spread = 12
- boundary_low = [0, 0]
- boundary_high = [0, 0]
- nsteps = 250
- # Dictionary to keep track of plot points
- plotting_points = [
- {'num_steps': 20, 'data_to_plot': None, 'label': ''},
- {'num_steps': 225, 'data_to_plot': None, 'label': ''},
- {'num_steps': 250, 'data_to_plot': None, 'label': 'FDTDcells'},
- ]
- # main FDTD loop
- for time_step in range(1, nsteps + 1):
- # calculate Ex field
- for k in range(1, ke):
- ex[k] = ex[k] + factor * (hy[k - 1] - hy[k])
- # put a gaussian pulse at the low end
- pulse = exp(-0.5 * ((t0 - time_step) / spread) ** 2)
- ex[kc] = pulse
- # update boundary conditions
- ex[0] = boundary_low.pop(0)
- boundary_low.append(ex[1])
- ex[ke - 1] = boundary_high.pop(0)
- boundary_high.append(ex[ke - 2])
- # calculate the Hy field
- for k in range(ke - 1):
- hy[k] = hy[k] + factor * (ex[k] - ex[k + 1])
- # store data at specific time steps for plotting
- for plotting_point in plotting_points:
- if time_step == plotting_point['num_steps']:
- plotting_point['data_to_plot'] = np.copy(ex)
- return plotting_points
- # Run simulations with factors 0.5, 1.0, 1.1, and 0.25
- factors = [0.5, 1.0, 1.1, 0.25]
- plot_data = {factor: run_fdtd(factor) for factor in factors}
- # Plotting the results
- plt.rcParams['font.size'] = 12
- fig, axs = plt.subplots(len(factors), 3, figsize=(12, 12))
- def plot_e_field(ax, data, timestep, label, factor):
- ax.plot(data, color='k', linewidth=1)
- ax.set_ylabel('E$_x$', fontsize=10)
- ax.set_xticks(np.arange(0, 199, step=20))
- ax.set_xlim(0, 199)
- ax.set_yticks(np.arange(-1.2, 1.2, step=1))
- ax.set_ylim(-1.2, 1.2)
- ax.text(100, 0.5, f'T = {timestep}', horizontalalignment='center', fontsize=9)
- ax.set_xlabel(f'{label} (Factor={factor})', fontsize=10)
- for i, (factor, plots) in enumerate(plot_data.items()):
- for j, plotting_point in enumerate(plots):
- plot_e_field(axs[i, j],
- plotting_point['data_to_plot'],
- plotting_point['num_steps'],
- plotting_point['label'],
- factor)
- plt.tight_layout()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement