fdtd.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python
  2. # File: fdtd.py
  3. # Name: D.Saravanan
  4. # Date: 16/05/2022
  5. """ Script for 1-dimensional fdtd with gaussian pulse as source """
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. plt.style.use("classic")
  9. plt.rcParams["text.usetex"] = True
  10. plt.rcParams["pgf.texsystem"] = "pdflatex"
  11. plt.rcParams.update(
  12. {
  13. "font.family": "serif",
  14. "font.size": 8,
  15. "axes.labelsize": 10,
  16. "axes.titlesize": 10,
  17. "figure.titlesize": 10,
  18. }
  19. )
  20. def plotting(time_step, ex, hy):
  21. """plot function"""
  22. fig, (ax1, ax2) = plt.subplots(2)
  23. fig.suptitle(r"FDTD simulation of a pulse in free space after 100 time steps")
  24. ax1.plot(ex, "k", lw=1)
  25. ax1.text(100, 0.5, "T = {}".format(time_step), horizontalalignment="center")
  26. ax1.set(xlim=(0, 200), ylim=(-1.2, 1.2), ylabel=r"E$_x$")
  27. ax1.set(xticks=range(0, 220, 20), yticks=np.arange(-1, 1.2, 1))
  28. ax2.plot(hy, "k", lw=1)
  29. ax2.set(xlim=(0, 200), ylim=(-1.2, 1.2), xlabel=r"FDTD cells", ylabel=r"H$_y$")
  30. ax2.set(xticks=range(0, 220, 20), yticks=np.arange(-1, 1.2, 1))
  31. plt.subplots_adjust(bottom=0.2, hspace=0.45)
  32. plt.savefig("fdtd.png")
  33. def main():
  34. ke = 201
  35. ex = np.zeros(ke)
  36. hy = np.zeros(ke)
  37. # Pulse parameters
  38. kc = int(ke / 2)
  39. t0 = 40
  40. spread = 12
  41. nsteps = 100
  42. for time_step in range(1, nsteps + 1):
  43. ex[1:ke] = ex[1:ke] + 0.5 * (hy[0:ke-1] - hy[1:ke])
  44. ex[kc] = np.exp(-0.5 * ((t0 - time_step) / spread) ** 2)
  45. hy[0:ke-1] = hy[0:ke-1] + 0.5 * (ex[0:ke-1] - ex[1:ke])
  46. plotting(time_step, ex, hy)
  47. if __name__ == "__main__":
  48. main()