fdtdmp.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #!/usr/bin/env python
  2. # File: fdtdmp.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 multiprocessing
  8. import matplotlib.pyplot as plt
  9. plt.style.use("classic")
  10. plt.rcParams["text.usetex"] = True
  11. plt.rcParams["pgf.texsystem"] = "pdflatex"
  12. plt.rcParams.update(
  13. {
  14. "font.family": "serif",
  15. "font.size": 8,
  16. "axes.labelsize": 10,
  17. "axes.titlesize": 10,
  18. "figure.titlesize": 10,
  19. }
  20. )
  21. def plotting(time_step, ex, hy):
  22. """plot function"""
  23. fig, (ax1, ax2) = plt.subplots(2)
  24. fig.suptitle(r"FDTD simulation of a pulse in free space after 100 time steps")
  25. ax1.plot(ex, "k", lw=1)
  26. ax1.text(100, 0.5, "T = {}".format(time_step), horizontalalignment="center")
  27. ax1.set(xlim=(0, 200), ylim=(-1.2, 1.2), ylabel=r"E$_x$")
  28. ax1.set(xticks=range(0, 220, 20), yticks=np.arange(-1, 1.2, 1))
  29. ax2.plot(hy, "k", lw=1)
  30. ax2.set(xlim=(0, 200), ylim=(-1.2, 1.2), xlabel=r"FDTD cells", ylabel=r"H$_y$")
  31. ax2.set(xticks=range(0, 220, 20), yticks=np.arange(-1, 1.2, 1))
  32. plt.subplots_adjust(bottom=0.2, hspace=0.45)
  33. plt.savefig("fdtdmp.png")
  34. def gaussian(t0, time_step, spread):
  35. return np.exp(-0.5 * ((t0 - time_step) / spread) ** 2)
  36. def electric(k, ex, hy):
  37. ex[k] = ex[k] + 0.5 * (hy[k - 1] - hy[k])
  38. def magnetic(k, ex, hy):
  39. hy[k] = hy[k] + 0.5 * (ex[k] - ex[k + 1])
  40. def main():
  41. ke = 20
  42. ex = np.zeros(ke)
  43. hy = np.zeros(ke)
  44. # Pulse parameters
  45. kc = int(ke / 2)
  46. t0 = 40
  47. spread = 12
  48. nsteps = 100
  49. for time_step in range(1, nsteps + 1):
  50. eps = []
  51. for k in range(1, ke):
  52. p = multiprocessing.Process(target = electric, args = [k, ex, hy])
  53. p.start()
  54. eps.append(p)
  55. for process in eps:
  56. process.join()
  57. ex[kc] = gaussian(t0, time_step, spread)
  58. mps = []
  59. for k in range(ke - 1):
  60. p = multiprocessing.Process(target = magnetic, args = [k, ex, hy])
  61. p.start()
  62. mps.append(p)
  63. for process in mps:
  64. process.join()
  65. plotting(time_step, ex, hy)
  66. if __name__ == "__main__":
  67. main()