Advertisement
foreverfugazi

iwannachill

Sep 15th, 2024
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.80 KB | None | 0 0
  1. import numpy as np
  2. from math import exp
  3. from matplotlib import pyplot as plt
  4.  
  5.  
  6.  
  7.  
  8. def plot_e_field(data, timestep, epsilon_high, epsilon_low, cb, label):
  9.     plt.plot(data, color='k', linewidth=1)
  10.     plt.ylabel('E$_x$', fontsize=14)
  11.     plt.xticks(np.arange(0, 199, step=20))
  12.     plt.xlim(0, 199)
  13.     plt.yticks(np.arange(-0.5, 1.2, step=0.5))
  14.     plt.ylim(-0.5, 1.2)
  15.     plt.text(70, 0.5, 'T = {}'.format(timestep), horizontalalignment='center')
  16.    
  17.     cool1 = ((0.5 / cb - 1) - (epsilon_low-1)) / (epsilon_high - epsilon_low)
  18.     cool2 = ((0.5 / cb - 1) - (epsilon_high-1)) / (epsilon_low - epsilon_high)
  19.     cool2 = cool2 * (epsilon_low / epsilon_high)
  20.     plt.plot(cool1, 'r--', linewidth=0.75)
  21.     plt.plot(cool2, 'b--', linewidth=0.75)
  22.    
  23.     plt.xlabel('{}'.format(label))
  24.  
  25.  
  26. ke = 200
  27. ex = np.zeros(ke)
  28. hy = np.zeros(ke)
  29.  
  30. t0 = 40
  31. spread = 12
  32.  
  33. boundary_low = [0, 0]
  34. boundary_high = [0, 0]
  35.  
  36. # create dielectric profile
  37. cb = np.ones(ke)
  38. cb = 0.5 * cb
  39. cb_start = 100
  40. epsilon_high = 3.4 ** 2 # (GaAs)
  41. epsilon_low = 2.9 ** 2  # (Al0.7Ga0.3As)
  42.  
  43. # DBR defined here
  44. start_dbr = 100
  45. layer_thickness = 5  # want it thicc
  46.  
  47. for i in range(start_dbr, ke, 2 * layer_thickness):
  48.     cb[i:i + layer_thickness] = 0.5 / epsilon_high
  49.     cb[i + layer_thickness:i + 2 * layer_thickness] = 0.5 / epsilon_low
  50.  
  51. nsteps = 540
  52.  
  53. # dictionary to keep track of desired points for plotting
  54. plotting_points = [
  55.     {'num_steps': 100, 'data_to_plot': None, 'label': ''},
  56.     {'num_steps': 120, 'data_to_plot': None, 'label': ''},
  57.     {'num_steps': 150, 'data_to_plot': None, 'label': ''},
  58.     {'num_steps': 180, 'data_to_plot': None, 'label': ''},
  59.     {'num_steps': 200, 'data_to_plot': None, 'label': ''},
  60.     {'num_steps': 220, 'data_to_plot': None, 'label': ''},
  61.     {'num_steps': 250, 'data_to_plot': None, 'label': ''},
  62.     {'num_steps': 280, 'data_to_plot': None, 'label': ''},
  63.     {'num_steps': 300, 'data_to_plot': None, 'label': ''},
  64.     {'num_steps': 320, 'data_to_plot': None, 'label': ''},
  65.     {'num_steps': 350, 'data_to_plot': None, 'label': ''},
  66.     {'num_steps': 380, 'data_to_plot': None, 'label': ''},
  67.     {'num_steps': 400, 'data_to_plot': None, 'label': ''},
  68.     {'num_steps': 420, 'data_to_plot': None, 'label': 'FDTDcells'}
  69. ]
  70.  
  71. # main FDTD Loop
  72. for time_step in range(1, nsteps + 1):
  73.  
  74.     # calculate Ex field
  75.     for k in range(1, ke):
  76.         ex[k] = ex[k] + cb[k] * (hy[k - 1] - hy[k])
  77.  
  78.     # put a gaussian pulse at the low end
  79.     pulse = exp(-0.5 * ((t0 - time_step) / spread) ** 2)
  80.     ex[5] = pulse + ex[5]
  81.  
  82.     ex[0] = boundary_low.pop(0)
  83.     boundary_low.append(ex[1])
  84.  
  85.     ex[ke - 1] = boundary_high.pop(0)
  86.     boundary_high.append(ex[ke - 2])
  87.  
  88.     # calculate the Hy field
  89.     for k in range(ke - 1):
  90.         hy[k] = hy[k] + 0.5 * (ex[k] - ex[k + 1])
  91.  
  92.     # solve data at certain points for later plotting
  93.     for plotting_point in plotting_points:
  94.         if time_step == plotting_point['num_steps']:
  95.             plotting_point['data_to_plot'] = np.copy(ex)
  96.  
  97. # plot the outputs in multiple figures
  98. plt.rcParams['font.size'] = 12
  99. num_plots = len(plotting_points)
  100. plots_per_figure = 3
  101. num_figures = (num_plots + plots_per_figure - 1) // plots_per_figure  # Ceiling division
  102.  
  103. for fig_num in range(num_figures):
  104.     fig = plt.figure(figsize=(8, 7))
  105.     start_index = fig_num * plots_per_figure
  106.     end_index = min(start_index + plots_per_figure, num_plots)
  107.    
  108.     for subplot_num, plotting_point in enumerate(plotting_points[start_index:end_index]):
  109.         ax = fig.add_subplot(plots_per_figure, 1, subplot_num + 1)
  110.         plot_e_field(plotting_point['data_to_plot'],
  111.                      plotting_point['num_steps'], epsilon_high, epsilon_low, cb,
  112.                      plotting_point['label'])
  113.    
  114.     plt.subplots_adjust(bottom=0.1, hspace=0.45)
  115.     plt.show()
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement