field-SiAgSi.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2 = scattnlay(x, m)
  77. terms, E, H = fieldnlay(x, m, coordX, coordY, coordZ)
  78. print("Qabs = "+str(Qabs));
  79. Er = np.absolute(E)
  80. Hr = np.absolute(H)
  81. # |E|/|Eo|
  82. Eabs = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2)
  83. Eangle = np.angle(E[0, :, 0])/np.pi*180
  84. Habs= np.sqrt(Hr[0, :, 0]**2 + Hr[0, :, 1]**2 + Hr[0, :, 2]**2)
  85. Hangle = np.angle(H[0, :, 1])/np.pi*180
  86. result = np.vstack((coordX, coordY, coordZ, Eabs)).transpose()
  87. result2 = np.vstack((coordX, coordY, coordZ, Eangle)).transpose()
  88. try:
  89. import matplotlib.pyplot as plt
  90. from matplotlib import cm
  91. from matplotlib.colors import LogNorm
  92. # min_tick = 0.0
  93. # max_tick = 1.0
  94. Eabs_data = np.resize(Eabs, (npts, npts)).T
  95. Eangle_data = np.resize(Eangle, (npts, npts)).T
  96. Habs_data = np.resize(Habs, (npts, npts)).T
  97. Hangle_data = np.resize(Hangle, (npts, npts)).T
  98. fig, axs = plt.subplots(2,2)#, sharey=True, sharex=True)
  99. fig.tight_layout()
  100. # Rescale to better show the axes
  101. scale_x = np.linspace(min(coordX)*WL/2.0/np.pi/nm, max(coordX)*WL/2.0/np.pi/nm, npts)
  102. scale_z = np.linspace(min(coordZ)*WL/2.0/np.pi/nm, max(coordZ)*WL/2.0/np.pi/nm, npts)
  103. # Define scale ticks
  104. # min_tick = min(min_tick, np.amin(Eabs_data))
  105. # max_tick = max(max_tick, np.amax(Eabs_data))
  106. # scale_ticks = np.power(10.0, np.linspace(np.log10(min_tick), np.log10(max_tick), 6))
  107. # scale_ticks = np.linspace(min_tick, max_tick, 10)
  108. # Interpolation can be 'nearest', 'bilinear' or 'bicubic'
  109. axs[0,0].set_title('Eabs')
  110. cax = axs[0,0].imshow(Eabs_data, interpolation = 'nearest', cmap = cm.jet,
  111. origin = 'lower'
  112. #, vmin = min_tick, vmax = max_tick
  113. , extent = (min(scale_x), max(scale_x), min(scale_z), max(scale_z))
  114. #,norm = LogNorm()
  115. )
  116. axs[0,0].axis("image")
  117. axs[0,1].set_title('Eangle')
  118. cax = axs[0,1].imshow(Eangle_data, interpolation = 'nearest', cmap = cm.jet,
  119. origin = 'lower'
  120. #, vmin = min_tick, vmax = max_tick
  121. , extent = (min(scale_x), max(scale_x), min(scale_z), max(scale_z))
  122. #,norm = LogNorm()
  123. )
  124. axs[1,0].set_title('Habs')
  125. cax = axs[1,0].imshow(Habs_data, interpolation = 'nearest', cmap = cm.jet,
  126. origin = 'lower'
  127. #, vmin = min_tick, vmax = max_tick
  128. , extent = (min(scale_x), max(scale_x), min(scale_z), max(scale_z))
  129. #,norm = LogNorm()
  130. )
  131. axs[1,1].set_title('Hangle')
  132. cax = axs[1,1].imshow(Hangle_data, interpolation = 'nearest', cmap = cm.jet,
  133. origin = 'lower'
  134. #, vmin = min_tick, vmax = max_tick
  135. , extent = (min(scale_x), max(scale_x), min(scale_z), max(scale_z))
  136. #,norm = LogNorm()
  137. )
  138. # Add colorbar
  139. # cbar = fig.colorbar(cax, ticks = [a for a in scale_ticks])
  140. # cbar.ax.set_yticklabels(['%5.3g' % (a) for a in scale_ticks]) # vertically oriented colorbar
  141. # pos = list(cbar.ax.get_position().bounds)
  142. # fig.text(pos[0] - 0.02, 0.925, '|E|/|E$_0$|', fontsize = 14)
  143. # plt.xlabel('Z, nm')
  144. # plt.ylabel('X, nm')
  145. # This part draws the nanoshell
  146. from matplotlib import patches
  147. for m in (0,1):
  148. for n in (0,1):
  149. s1 = patches.Arc((0, 0), 2.0*core_r, 2.0*core_r, angle=0.0, zorder=2,
  150. theta1=0.0, theta2=360.0, linewidth=1, color='black')
  151. s2 = patches.Arc((0, 0), 2.0*inner_r, 2.0*inner_r, angle=0.0, zorder=2,
  152. theta1=0.0, theta2=360.0, linewidth=1, color='black')
  153. s3 = patches.Arc((0, 0), 2.0*outer_r, 2.0*outer_r, angle=0.0, zorder=2,
  154. theta1=0.0, theta2=360.0, linewidth=1, color='black')
  155. axs[m,n].add_patch(s1)
  156. axs[m,n].add_patch(s2)
  157. axs[m,n].add_patch(s3)
  158. # axs[0,0].add_patch(s1)
  159. # axs[0,0].add_patch(s2)
  160. # axs[0,0].add_patch(s3)
  161. # axs[1,0].add_patch(s1)
  162. # axs[1,0].add_patch(s2)
  163. # axs[1,0].add_patch(s3)
  164. # axs[0,1].add_patch(s1)
  165. # axs[0,1].add_patch(s2)
  166. # axs[0,1].add_patch(s3)
  167. # axs[1,1].add_patch(s1)
  168. # axs[1,1].add_patch(s2)
  169. # axs[1,1].add_patch(s3)
  170. # for m in (0,1):
  171. # for n in (0,1):
  172. # print(m)
  173. # print(n)
  174. # axs[m,n].add_patch(s1)
  175. # axs[m,n].add_patch(s2)
  176. # axs[m,n].add_patch(s3)
  177. # End of drawing
  178. plt.savefig("SiAgSi.png")
  179. plt.draw()
  180. plt.show()
  181. plt.clf()
  182. plt.close()
  183. finally:
  184. np.savetxt("field.txt", result, fmt = "%.5f")
  185. print result