field.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 in the
  28. # XY plane, 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. npts = 1001
  41. scan = np.linspace(-3.0*x[0, 0], 3.0*x[0, 0], npts)
  42. coordX, coordY = np.meshgrid(scan, scan)
  43. coordX.resize(npts*npts)
  44. coordY.resize(npts*npts)
  45. coordZ = np.zeros(npts*npts, dtype = np.float64)
  46. coord = np.vstack((coordX, coordY, coordZ)).transpose()
  47. terms, E, H = fieldnlay(x, m, coord)
  48. Er = np.absolute(E)
  49. # |E|/|Eo|
  50. Eh = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2)
  51. result = np.vstack((coordX, coordY, coordZ, Eh)).transpose()
  52. try:
  53. import matplotlib.pyplot as plt
  54. from matplotlib import cm
  55. from matplotlib.colors import LogNorm
  56. min_tick = 0.1
  57. max_tick = 1.0
  58. edata = np.resize(Eh, (npts, npts))
  59. fig = plt.figure()
  60. ax = fig.add_subplot(111)
  61. # Rescale to better show the axes
  62. scale_x = np.linspace(min(coordX), max(coordX), npts)
  63. scale_y = np.linspace(min(coordY), max(coordY), npts)
  64. # Define scale ticks
  65. min_tick = max(0.5, min(min_tick, np.amin(edata)))
  66. max_tick = max(max_tick, np.amax(edata))
  67. scale_ticks = np.power(10.0, np.linspace(np.log10(min_tick), np.log10(max_tick), 6))
  68. # Interpolation can be 'nearest', 'bilinear' or 'bicubic'
  69. cax = ax.imshow(edata, interpolation = 'bicubic', cmap = cm.afmhot,
  70. origin = 'lower', vmin = min_tick, vmax = max_tick,
  71. extent = (min(scale_x), max(scale_x), min(scale_y), max(scale_y)),
  72. norm = LogNorm())
  73. # Add colorbar
  74. cbar = fig.colorbar(cax, ticks = [a for a in scale_ticks])
  75. cbar.ax.set_yticklabels(['%3.1e' % (a) for a in scale_ticks]) # vertically oriented colorbar
  76. pos = list(cbar.ax.get_position().bounds)
  77. fig.text(pos[0] - 0.02, 0.925, '|E|/|E$_0$|', fontsize = 14)
  78. plt.xlabel('X')
  79. plt.ylabel('Y')
  80. plt.draw()
  81. plt.show()
  82. plt.clf()
  83. plt.close()
  84. finally:
  85. np.savetxt("field.txt", result, fmt = "%.5f")
  86. print result