field-SiAgSi.py 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. # E-k plane, for an spherical Si-Ag-Si nanoparticle. Core radius is 17.74 nm,
  29. # inner layer 23.31nm, outer layer 22.95nm. Working wavelength is 800nm, we use
  30. # silicon epsilon=13.64+i0.047, silver epsilon= -28.05+i1.525
  31. import scattnlay
  32. from scattnlay import fieldnlay
  33. import numpy as np
  34. # epsilon_Si = 13.64 + 0.047j
  35. # epsilon_Ag = -28.05 + 1.525j
  36. epsilon_Si = 2.0 + 0.047j
  37. epsilon_Ag = -2.0 + 1.525j
  38. index_Si = epsilon_Si*epsilon_Si
  39. index_Ag = epsilon_Ag*epsilon_Ag
  40. WL=800 #nm
  41. core_width = 17.74 #nm Si
  42. inner_width = 23.31 #nm Ag
  43. outer_width = 22.95 #nm Si
  44. core_r = core_width
  45. inner_r = core_r+inner_width
  46. outer_r = inner_r+outer_width
  47. # n1 = 1.53413
  48. # n2 = 0.565838 + 7.23262j
  49. nm = 1.0
  50. x = np.ones((1, 3), dtype = np.float64)
  51. x[0, 0] = 2.0*np.pi*core_r/WL
  52. x[0, 1] = 2.0*np.pi*inner_r/WL
  53. x[0, 2] = 2.0*np.pi*outer_r/WL
  54. m = np.ones((1, 3), dtype = np.complex128)
  55. m[0, 0] = index_Si
  56. m[0, 1] = index_Ag
  57. m[0, 2] = index_Si
  58. print "x =", x
  59. print "m =", m
  60. npts = 28
  61. scan = np.linspace(-2.0*x[0, 2], 2.0*x[0, 2], npts)
  62. coordX, coordZ = np.meshgrid(scan, scan)
  63. coordX.resize(npts*npts)
  64. coordZ.resize(npts*npts)
  65. coordY = np.zeros(npts*npts, dtype = np.float64)
  66. coord = np.vstack((coordX, coordY, coordZ)).transpose()
  67. terms, E, H = fieldnlay(x, m, coord)
  68. Er = np.absolute(E)
  69. # |E|/|Eo|
  70. Eh = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2)
  71. result = np.vstack((coordX, coordY, coordZ, Eh)).transpose()
  72. try:
  73. import matplotlib.pyplot as plt
  74. from matplotlib import cm
  75. from matplotlib.colors import LogNorm
  76. min_tick = 0.1
  77. max_tick = 1.0
  78. edata = np.resize(Eh, (npts, npts))
  79. fig = plt.figure()
  80. ax = fig.add_subplot(111)
  81. # Rescale to better show the axes
  82. scale_x = np.linspace(min(coordX)*1.064/2.0/np.pi/nm, max(coordX)*1.064/2.0/np.pi/nm, npts)
  83. scale_z = np.linspace(min(coordZ)*1.064/2.0/np.pi/nm, max(coordZ)*1.064/2.0/np.pi/nm, npts)
  84. # Define scale ticks
  85. min_tick = min(min_tick, np.amin(edata))
  86. max_tick = max(max_tick, np.amax(edata))
  87. # scale_ticks = np.power(10.0, np.linspace(np.log10(min_tick), np.log10(max_tick), 6))
  88. scale_ticks = np.linspace(min_tick, max_tick, 6)
  89. # Interpolation can be 'nearest', 'bilinear' or 'bicubic'
  90. cax = ax.imshow(edata, interpolation = 'nearest', cmap = cm.jet,
  91. origin = 'lower', vmin = min_tick, vmax = max_tick,
  92. extent = (min(scale_x), max(scale_x), min(scale_z), max(scale_z))
  93. #,norm = LogNorm()
  94. )
  95. # Add colorbar
  96. cbar = fig.colorbar(cax, ticks = [a for a in scale_ticks])
  97. cbar.ax.set_yticklabels(['%3.1e' % (a) for a in scale_ticks]) # vertically oriented colorbar
  98. pos = list(cbar.ax.get_position().bounds)
  99. fig.text(pos[0] - 0.02, 0.925, '|E|/|E$_0$|', fontsize = 14)
  100. plt.xlabel('X')
  101. plt.ylabel('Z')
  102. # This part draws the nanoshell
  103. # from matplotlib import patches
  104. # s1 = patches.Arc((0, 0), 2.0*x[0, 0], 2.0*x[0, 0], angle=0.0, zorder=2,
  105. # theta1=0.0, theta2=360.0, linewidth=1, color='#00fa9a')
  106. # ax.add_patch(s1)
  107. # s2 = patches.Arc((0, 0), 2.0*x[0, 1], 2.0*x[0, 1], angle=0.0, zorder=2,
  108. # theta1=0.0, theta2=360.0, linewidth=1, color='#00fa9a')
  109. # ax.add_patch(s2)
  110. # End of drawing
  111. plt.draw()
  112. plt.show()
  113. plt.clf()
  114. plt.close()
  115. finally:
  116. np.savetxt("field.txt", result, fmt = "%.5f")
  117. print result