field-nanoshell.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python
  2. # -*- coding: UTF-8 -*-
  3. #
  4. # Copyright (C) 2009-2015 Ovidio Peña Rodríguez <ovidio@bytesfall.com>
  5. # Copyright (C) 2013-2015 Konstantin Ladutenko <kostyfisik@gmail.com>
  6. #
  7. # This file is part of scattnlay
  8. #
  9. # This program is free software: you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation, either version 3 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. #
  19. # The only additional remark is that we expect that all publications
  20. # describing work using this software, or all commercial products
  21. # using it, cite the following reference:
  22. # [1] O. Pena and U. Pal, "Scattering of electromagnetic radiation by
  23. # a multilayered sphere," Computer Physics Communications,
  24. # vol. 180, Nov. 2009, pp. 2348-2354.
  25. #
  26. # You should have received a copy of the GNU General Public License
  27. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. # This test case calculates the electric field in the
  29. # XY plane, for a silver nanoshell embedded in water.
  30. # Refractive index values correspond to the wavelength
  31. # where maximum of the surface plasmon resonance (and,
  32. # hence, of electric field) is expected.
  33. from scattnlay import fieldnlay
  34. import numpy as np
  35. import time
  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. print "x =", x
  46. print "m =", m
  47. npts = 501
  48. scan = np.linspace(-4.0*x[0, 0], 4.0*x[0, 0], npts)
  49. coordX, coordY = np.meshgrid(scan, scan)
  50. coordX.resize(npts*npts)
  51. coordY.resize(npts*npts)
  52. coordZ = np.zeros(npts*npts, dtype = np.float64)
  53. coord = np.vstack((coordX, coordY, coordZ)).transpose()
  54. start_time = time.time()
  55. terms, E, H = fieldnlay(x, m, coord)
  56. elapsed_time = time.time() - start_time
  57. print "Time: ", elapsed_time
  58. Er = np.absolute(E)
  59. # |E|/|Eo|
  60. Eh = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2)
  61. result = np.vstack((coordX, coordY, coordZ, Eh)).transpose()
  62. try:
  63. import matplotlib.pyplot as plt
  64. from matplotlib import cm
  65. from matplotlib.colors import LogNorm
  66. min_tick = 0.0
  67. max_tick = 5.0
  68. edata = np.resize(Eh, (npts, npts))
  69. fig = plt.figure()
  70. ax = fig.add_subplot(111)
  71. # Rescale to better show the axes
  72. scale_x = 1000.0*np.linspace(min(coordX)*1.064/2.0/np.pi/nm, max(coordX)*1.064/2.0/np.pi/nm, npts)
  73. scale_y = 1000.0*np.linspace(min(coordY)*1.064/2.0/np.pi/nm, max(coordY)*1.064/2.0/np.pi/nm, npts)
  74. # Define scale ticks
  75. min_tick = min(min_tick, np.amin(edata))
  76. max_tick = max(max_tick, np.amax(edata))
  77. #scale_ticks = np.power(10.0, np.linspace(np.log10(min_tick), np.log10(max_tick), 6))
  78. scale_ticks = np.linspace(min_tick, max_tick, 6)
  79. # Interpolation can be 'nearest', 'bilinear' or 'bicubic'
  80. cax = ax.imshow(edata, interpolation = 'nearest', cmap = cm.jet,
  81. origin = 'lower', vmin = min_tick, vmax = max_tick,
  82. extent = (min(scale_x), max(scale_x), min(scale_y), max(scale_y)))
  83. # Add colorbar
  84. cbar = fig.colorbar(cax, ticks = [a for a in scale_ticks])
  85. cbar.ax.set_yticklabels(['%4.2g' % (a) for a in scale_ticks]) # vertically oriented colorbar
  86. pos = list(cbar.ax.get_position().bounds)
  87. fig.text(pos[0] - 0.02, 0.925, '|E|/|E$_0$|', fontsize = 14)
  88. plt.xlabel('X ( nm )')
  89. plt.ylabel('Y ( nm )')
  90. # This part draws the nanoshell
  91. # from matplotlib import patches
  92. # s1 = patches.Arc((0, 0), 2.0*x[0, 0], 2.0*x[0, 0], angle=0.0, zorder=2,
  93. # theta1=0.0, theta2=360.0, linewidth=1, color='#00fa9a')
  94. # ax.add_patch(s1)
  95. # s2 = patches.Arc((0, 0), 2.0*x[0, 1], 2.0*x[0, 1], angle=0.0, zorder=2,
  96. # theta1=0.0, theta2=360.0, linewidth=1, color='#00fa9a')
  97. # ax.add_patch(s2)
  98. # End of drawing
  99. plt.draw()
  100. plt.show()
  101. plt.clf()
  102. plt.close()
  103. finally:
  104. np.savetxt("field-nanoshell.txt", result, fmt = "%.5f")
  105. print result