field-SiAgSi.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 python-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. # E-k plane, for an spherical Si-Ag-Si nanoparticle. Core radius is 17.74 nm,
  30. # inner layer 23.31nm, outer layer 22.95nm. Working wavelength is 800nm, we use
  31. # silicon epsilon=13.64+i0.047, silver epsilon= -28.05+i1.525
  32. import scattnlay
  33. from scattnlay import fieldnlay
  34. from scattnlay import scattnlay
  35. import numpy as np
  36. import cmath
  37. epsilon_Si = 13.64 + 0.047j
  38. epsilon_Ag = -28.05 + 1.525j
  39. # epsilon_Si = 2.0 + 0.047j
  40. # epsilon_Ag = -2.0 + 1.525j
  41. # air = 1
  42. # epsilon_Si = air*2
  43. # epsilon_Ag = air*2
  44. index_Si = np.sqrt(epsilon_Si)
  45. index_Ag = np.sqrt(epsilon_Ag)
  46. # # Values for 800 nm, taken from http://refractiveindex.info/
  47. # index_Si = 3.69410 + 0.0065435j
  48. # index_Ag = 0.18599 + 4.9886j
  49. WL=800 #nm
  50. core_width = 17.74 #nm Si
  51. inner_width = 23.31 #nm Ag
  52. outer_width = 22.95 #nm Si
  53. core_r = core_width
  54. inner_r = core_r+inner_width
  55. outer_r = inner_r+outer_width
  56. # n1 = 1.53413
  57. # n2 = 0.565838 + 7.23262j
  58. nm = 1.0
  59. x = np.ones((1, 3), dtype = np.float64)
  60. x[0, 0] = 2.0*np.pi*core_r/WL
  61. x[0, 1] = 2.0*np.pi*inner_r/WL
  62. x[0, 2] = 2.0*np.pi*outer_r/WL
  63. m = np.ones((1, 3), dtype = np.complex128)
  64. m[0, 0] = index_Si/nm
  65. m[0, 1] = index_Ag/nm
  66. m[0, 2] = index_Si/nm
  67. print "x =", x
  68. print "m =", m
  69. npts = 281
  70. factor=2.5
  71. scan = np.linspace(-factor*x[0, 2], factor*x[0, 2], npts)
  72. coordX, coordZ = np.meshgrid(scan, scan)
  73. coordX.resize(npts*npts)
  74. coordZ.resize(npts*npts)
  75. coordY = np.zeros(npts*npts, dtype = np.float64)
  76. coord = np.vstack((coordX, coordY, coordZ)).transpose()
  77. terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2 = scattnlay(x, m)
  78. terms, E, H = fieldnlay(x, m, coord)
  79. print("Qabs = "+str(Qabs));
  80. Er = np.absolute(E)
  81. Hr = np.absolute(H)
  82. # |E|/|Eo|
  83. Eabs = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2)
  84. Eangle = np.angle(E[0, :, 0])/np.pi*180
  85. Habs= np.sqrt(Hr[0, :, 0]**2 + Hr[0, :, 1]**2 + Hr[0, :, 2]**2)
  86. Hangle = np.angle(H[0, :, 1])/np.pi*180
  87. result = np.vstack((coordX, coordY, coordZ, Eabs)).transpose()
  88. result2 = np.vstack((coordX, coordY, coordZ, Eangle)).transpose()
  89. try:
  90. import matplotlib.pyplot as plt
  91. from matplotlib import cm
  92. from matplotlib.colors import LogNorm
  93. # min_tick = 0.0
  94. # max_tick = 1.0
  95. Eabs_data = np.resize(Eabs, (npts, npts)).T
  96. Eangle_data = np.resize(Eangle, (npts, npts)).T
  97. Habs_data = np.resize(Habs, (npts, npts)).T
  98. Hangle_data = np.resize(Hangle, (npts, npts)).T
  99. fig, axs = plt.subplots(2,2)#, sharey=True, sharex=True)
  100. fig.tight_layout()
  101. # Rescale to better show the axes
  102. scale_x = np.linspace(min(coordX)*WL/2.0/np.pi/nm, max(coordX)*WL/2.0/np.pi/nm, npts)
  103. scale_z = np.linspace(min(coordZ)*WL/2.0/np.pi/nm, max(coordZ)*WL/2.0/np.pi/nm, npts)
  104. # Define scale ticks
  105. # min_tick = min(min_tick, np.amin(Eabs_data))
  106. # max_tick = max(max_tick, np.amax(Eabs_data))
  107. # scale_ticks = np.power(10.0, np.linspace(np.log10(min_tick), np.log10(max_tick), 6))
  108. # scale_ticks = np.linspace(min_tick, max_tick, 10)
  109. # Interpolation can be 'nearest', 'bilinear' or 'bicubic'
  110. axs[0,0].set_title('Eabs')
  111. cax = axs[0,0].imshow(Eabs_data, interpolation = 'nearest', cmap = cm.jet,
  112. origin = 'lower'
  113. #, vmin = min_tick, vmax = max_tick
  114. , extent = (min(scale_x), max(scale_x), min(scale_z), max(scale_z))
  115. #,norm = LogNorm()
  116. )
  117. axs[0,0].axis("image")
  118. axs[0,1].set_title('Eangle')
  119. cax = axs[0,1].imshow(Eangle_data, interpolation = 'nearest', cmap = cm.jet,
  120. origin = 'lower'
  121. #, vmin = min_tick, vmax = max_tick
  122. , extent = (min(scale_x), max(scale_x), min(scale_z), max(scale_z))
  123. #,norm = LogNorm()
  124. )
  125. axs[1,0].set_title('Habs')
  126. cax = axs[1,0].imshow(Habs_data, interpolation = 'nearest', cmap = cm.jet,
  127. origin = 'lower'
  128. #, vmin = min_tick, vmax = max_tick
  129. , extent = (min(scale_x), max(scale_x), min(scale_z), max(scale_z))
  130. #,norm = LogNorm()
  131. )
  132. axs[1,1].set_title('Hangle')
  133. cax = axs[1,1].imshow(Hangle_data, interpolation = 'nearest', cmap = cm.jet,
  134. origin = 'lower'
  135. #, vmin = min_tick, vmax = max_tick
  136. , extent = (min(scale_x), max(scale_x), min(scale_z), max(scale_z))
  137. #,norm = LogNorm()
  138. )
  139. # Add colorbar
  140. # cbar = fig.colorbar(cax, ticks = [a for a in scale_ticks])
  141. # cbar.ax.set_yticklabels(['%5.3g' % (a) for a in scale_ticks]) # vertically oriented colorbar
  142. # pos = list(cbar.ax.get_position().bounds)
  143. # fig.text(pos[0] - 0.02, 0.925, '|E|/|E$_0$|', fontsize = 14)
  144. # plt.xlabel('Z, nm')
  145. # plt.ylabel('X, nm')
  146. # This part draws the nanoshell
  147. from matplotlib import patches
  148. for m in (0,1):
  149. for n in (0,1):
  150. s1 = patches.Arc((0, 0), 2.0*core_r, 2.0*core_r, angle=0.0, zorder=2,
  151. theta1=0.0, theta2=360.0, linewidth=1, color='black')
  152. s2 = patches.Arc((0, 0), 2.0*inner_r, 2.0*inner_r, angle=0.0, zorder=2,
  153. theta1=0.0, theta2=360.0, linewidth=1, color='black')
  154. s3 = patches.Arc((0, 0), 2.0*outer_r, 2.0*outer_r, angle=0.0, zorder=2,
  155. theta1=0.0, theta2=360.0, linewidth=1, color='black')
  156. axs[m,n].add_patch(s1)
  157. axs[m,n].add_patch(s2)
  158. axs[m,n].add_patch(s3)
  159. # axs[0,0].add_patch(s1)
  160. # axs[0,0].add_patch(s2)
  161. # axs[0,0].add_patch(s3)
  162. # axs[1,0].add_patch(s1)
  163. # axs[1,0].add_patch(s2)
  164. # axs[1,0].add_patch(s3)
  165. # axs[0,1].add_patch(s1)
  166. # axs[0,1].add_patch(s2)
  167. # axs[0,1].add_patch(s3)
  168. # axs[1,1].add_patch(s1)
  169. # axs[1,1].add_patch(s2)
  170. # axs[1,1].add_patch(s3)
  171. # for m in (0,1):
  172. # for n in (0,1):
  173. # print(m)
  174. # print(n)
  175. # axs[m,n].add_patch(s1)
  176. # axs[m,n].add_patch(s2)
  177. # axs[m,n].add_patch(s3)
  178. # End of drawing
  179. plt.savefig("SiAgSi.png")
  180. plt.draw()
  181. plt.show()
  182. plt.clf()
  183. plt.close()
  184. finally:
  185. np.savetxt("field.txt", result, fmt = "%.5f")
  186. print result