lfield.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # Copyright (C) 2009-2015 Ovidio Peña Rodríguez <ovidio@bytesfall.com>
  5. #
  6. # This file is part of python-scattnlay
  7. #
  8. # This program is free software: you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation, either version 3 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU General Public License for more details.
  17. #
  18. # The only additional remark is that we expect that all publications
  19. # describing work using this software, or all commercial products
  20. # using it, cite the following reference:
  21. # [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by
  22. # a multilayered sphere," Computer Physics Communications,
  23. # vol. 180, Nov. 2009, pp. 2348-2354.
  24. #
  25. # You should have received a copy of the GNU General Public License
  26. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  27. # This test case calculates the electric field along the
  28. # X, Y and Z axes, for an spherical silver nanoparticle
  29. # embedded in glass.
  30. # Refractive index values correspond to a wavelength of
  31. # 400 nm. Maximum of the surface plasmon resonance (and,
  32. # hence, of electric field) is expected under those
  33. # conditions.
  34. from scattnlay import fieldnlay
  35. import numpy as np
  36. n1 = 1.53413
  37. n2 = 0.565838 + 7.23262j
  38. nm = 1.3205
  39. x = np.ones((1, 2), dtype = np.float64)
  40. x[0, 0] = 2.0*np.pi*nm*0.05/1.064
  41. x[0, 1] = 2.0*np.pi*nm*0.06/1.064
  42. m = np.ones((1, 2), dtype = np.complex128)
  43. m[0, 0] = n1/nm
  44. m[0, 1] = n2/nm
  45. nc = 1001
  46. coordX = np.zeros((nc, 3), dtype = np.float64)
  47. coordY = np.zeros((nc, 3), dtype = np.float64)
  48. coordZ = np.zeros((nc, 3), dtype = np.float64)
  49. scan = np.linspace(-4.0*x[0, 1], 4.0*x[0, 1], nc)
  50. one = np.ones(nc, dtype = np.float64)
  51. coordX[:, 0] = scan
  52. coordY[:, 1] = scan
  53. coordZ[:, 2] = scan
  54. terms, Ex, Hx = fieldnlay(x, m, coordX)
  55. terms, Ey, Hy = fieldnlay(x, m, coordY)
  56. terms, Ez, Hz = fieldnlay(x, m, coordZ)
  57. Exr = np.absolute(Ex)
  58. Eyr = np.absolute(Ey)
  59. Ezr = np.absolute(Ez)
  60. # |E|/|Eo|
  61. Exh = np.sqrt(Exr[0, :, 0]**2 + Exr[0, :, 1]**2 + Exr[0, :, 2]**2)
  62. Eyh = np.sqrt(Eyr[0, :, 0]**2 + Eyr[0, :, 1]**2 + Eyr[0, :, 2]**2)
  63. Ezh = np.sqrt(Ezr[0, :, 0]**2 + Ezr[0, :, 1]**2 + Ezr[0, :, 2]**2)
  64. result = np.vstack((scan, Exh, Eyh, Ezh)).transpose()
  65. try:
  66. import matplotlib.pyplot as plt
  67. fig = plt.figure()
  68. ax = fig.add_subplot(111)
  69. ax.errorbar(result[:, 0], one, fmt = 'k')
  70. ax.errorbar(result[:, 0], result[:, 1], fmt = 'r', label = 'X axis')
  71. ax.errorbar(result[:, 0], result[:, 2], fmt = 'g', label = 'Y axis')
  72. ax.errorbar(result[:, 0], result[:, 3], fmt = 'b', label = 'Z axis')
  73. ax.legend()
  74. plt.xlabel('X|Y|Z')
  75. plt.ylabel('|E|/|E$_0$|')
  76. plt.draw()
  77. plt.show()
  78. finally:
  79. np.savetxt("lfield.txt", result, fmt = "%.5f")
  80. print result