lfield.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. x = np.ones((1, 1), dtype = np.float64)
  37. x[0, 0] = 1.
  38. m = np.ones((1, 1), dtype = np.complex128)
  39. m[0, 0] = (0.05 + 2.070j)/1.46
  40. nc = 1001
  41. coordX = np.zeros((nc, 3), dtype = np.float64)
  42. coordY = np.zeros((nc, 3), dtype = np.float64)
  43. coordZ = np.zeros((nc, 3), dtype = np.float64)
  44. scan = np.linspace(-3.0*x[0, 0], 3.0*x[0, 0], nc)
  45. one = np.ones(nc, dtype = np.float64)
  46. coordX[:, 0] = scan
  47. coordY[:, 1] = scan
  48. coordZ[:, 2] = scan
  49. terms, Ex, Hx = fieldnlay(x, m, coordX)
  50. terms, Ey, Hy = fieldnlay(x, m, coordY)
  51. terms, Ez, Hz = fieldnlay(x, m, coordZ)
  52. Exr = np.absolute(Ex)
  53. Eyr = np.absolute(Ey)
  54. Ezr = np.absolute(Ez)
  55. # |E|/|Eo|
  56. Exh = np.sqrt(Exr[0, :, 0]**2 + Exr[0, :, 1]**2 + Exr[0, :, 2]**2)
  57. Eyh = np.sqrt(Eyr[0, :, 0]**2 + Eyr[0, :, 1]**2 + Eyr[0, :, 2]**2)
  58. Ezh = np.sqrt(Ezr[0, :, 0]**2 + Ezr[0, :, 1]**2 + Ezr[0, :, 2]**2)
  59. result = np.vstack((scan, Exh, Eyh, Ezh)).transpose()
  60. try:
  61. import matplotlib.pyplot as plt
  62. fig = plt.figure()
  63. ax = fig.add_subplot(111)
  64. ax.errorbar(result[:, 0], one, fmt = 'k')
  65. ax.errorbar(result[:, 0], result[:, 1], fmt = 'r', label = 'X axis')
  66. ax.errorbar(result[:, 0], result[:, 2], fmt = 'g', label = 'Y axis')
  67. ax.errorbar(result[:, 0], result[:, 3], fmt = 'b', label = 'Z axis')
  68. ax.legend()
  69. plt.xlabel('X|Y|Z')
  70. plt.ylabel('|E|/|Eo|')
  71. plt.draw()
  72. plt.show()
  73. finally:
  74. np.savetxt("lfield.txt", result, fmt = "%.5f")
  75. print result