field-SiAgSi-flow.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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. from scattnlay import scattnlay
  34. import numpy as np
  35. import cmath
  36. def get_index(array,value):
  37. idx = (np.abs(array-value)).argmin()
  38. return idx
  39. #Ec = np.resize(Ec, (npts, npts)).T
  40. def GetFlow(scale_x, scale_z, Ec, Hc, a, b, nmax):
  41. # Initial position
  42. flow_x = [a]
  43. flow_z = [b]
  44. for n in range(0, nmax):
  45. #Get the next position
  46. #1. Find Poynting vector and normalize it
  47. x_pos = flow_x[-1]
  48. z_pos = flow_z[-1]
  49. x_idx = get_index(scale_x, x_pos)
  50. z_idx = get_index(scale_z, z_pos)
  51. S=np.cross(Ec[npts*z_idx+x_idx], np.conjugate(Hc[npts*z_idx+x_idx]) ).real
  52. Snorm=S/np.linalg.norm(S)
  53. #2. Evaluate displacement = half of the discrete and new position
  54. dpos = abs(scale_z[0]-scale_z[1])/4.0
  55. dx = dpos*Snorm[0]
  56. dz = dpos*Snorm[2]
  57. x_pos = x_pos+dx
  58. z_pos = z_pos+dz
  59. #3. Save result
  60. flow_x.append(x_pos)
  61. flow_z.append(z_pos)
  62. return flow_x, flow_z
  63. epsilon_Si = 13.64 + 0.047j
  64. epsilon_Ag = -28.05 + 1.525j
  65. # epsilon_Si = 2.0 + 0.047j
  66. # epsilon_Ag = -2.0 + 1.525j
  67. # air = 1
  68. # epsilon_Si = air*2
  69. # epsilon_Ag = air*2
  70. index_Si = np.sqrt(epsilon_Si)
  71. index_Ag = np.sqrt(epsilon_Ag)
  72. print(index_Si)
  73. print(index_Ag)
  74. # # Values for 800 nm, taken from http://refractiveindex.info/
  75. # index_Si = 3.69410 + 0.0065435j
  76. # index_Ag = 0.18599 + 4.9886j
  77. WL=800 #nm
  78. core_width = 17.74 #nm Si
  79. inner_width = 23.31 #nm Ag
  80. outer_width = 22.95 #nm Si
  81. core_r = core_width
  82. inner_r = core_r+inner_width
  83. outer_r = inner_r+outer_width
  84. # n1 = 1.53413
  85. # n2 = 0.565838 + 7.23262j
  86. nm = 1.0
  87. x = np.ones((1, 3), dtype = np.float64)
  88. x[0, 0] = 2.0*np.pi*core_r/WL
  89. x[0, 1] = 2.0*np.pi*inner_r/WL
  90. x[0, 2] = 2.0*np.pi*outer_r/WL
  91. m = np.ones((1, 3), dtype = np.complex128)
  92. m[0, 0] = index_Si/nm
  93. m[0, 1] = index_Ag/nm
  94. m[0, 2] = index_Si/nm
  95. print "x =", x
  96. print "m =", m
  97. npts = 241
  98. factor=2.2
  99. scan = np.linspace(-factor*x[0, 2], factor*x[0, 2], npts)
  100. coordX, coordZ = np.meshgrid(scan, scan)
  101. coordX.resize(npts*npts)
  102. coordZ.resize(npts*npts)
  103. coordY = np.zeros(npts*npts, dtype = np.float64)
  104. coord = np.vstack((coordX, coordY, coordZ)).transpose()
  105. terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2 = scattnlay(x, m)
  106. terms, E, H = fieldnlay(x, m, coord)
  107. Er = np.absolute(E)
  108. Hr = np.absolute(H)
  109. # |E|/|Eo|
  110. Eabs = np.sqrt(Er[0, :, 0]**2 + Er[0, :, 1]**2 + Er[0, :, 2]**2)
  111. Ec = E[0, :, :]
  112. Hc = H[0, :, :]
  113. Eangle = np.angle(E[0, :, 0])/np.pi*180
  114. P=[]
  115. for n in range(0, len(E[0])):
  116. P.append(np.linalg.norm( np.cross(Ec[n], np.conjugate(Hc[n]) ).real/2 ))
  117. Habs= np.sqrt(Hr[0, :, 0]**2 + Hr[0, :, 1]**2 + Hr[0, :, 2]**2)
  118. Hangle = np.angle(H[0, :, 1])/np.pi*180
  119. try:
  120. import matplotlib.pyplot as plt
  121. from matplotlib import cm
  122. from matplotlib.colors import LogNorm
  123. min_tick = 0.0
  124. max_tick = 1.0
  125. # Eabs_data = np.resize(P, (npts, npts)).T
  126. Eabs_data = np.resize(Eabs, (npts, npts)).T
  127. # Eangle_data = np.resize(Eangle, (npts, npts)).T
  128. # Habs_data = np.resize(Habs, (npts, npts)).T
  129. # Hangle_data = np.resize(Hangle, (npts, npts)).T
  130. fig, ax = plt.subplots(1,1)#, sharey=True, sharex=True)
  131. #fig.tight_layout()
  132. # Rescale to better show the axes
  133. scale_x = np.linspace(min(coordX)*WL/2.0/np.pi/nm, max(coordX)*WL/2.0/np.pi/nm, npts)
  134. scale_z = np.linspace(min(coordZ)*WL/2.0/np.pi/nm, max(coordZ)*WL/2.0/np.pi/nm, npts)
  135. # Define scale ticks
  136. min_tick = min(min_tick, np.amin(Eabs_data))
  137. max_tick = max(max_tick, np.amax(Eabs_data))
  138. #max_tick = 5
  139. # scale_ticks = np.power(10.0, np.linspace(np.log10(min_tick), np.log10(max_tick), 6))
  140. scale_ticks = np.linspace(min_tick, max_tick, 11)
  141. # Interpolation can be 'nearest', 'bilinear' or 'bicubic'
  142. ax.set_title('Eabs')
  143. cax = ax.imshow(Eabs_data, interpolation = 'nearest', cmap = cm.jet,
  144. origin = 'lower'
  145. , vmin = min_tick, vmax = max_tick
  146. , extent = (min(scale_x), max(scale_x), min(scale_z), max(scale_z))
  147. #,norm = LogNorm()
  148. )
  149. ax.axis("image")
  150. # Add colorbar
  151. cbar = fig.colorbar(cax, ticks = [a for a in scale_ticks])
  152. cbar.ax.set_yticklabels(['%5.3g' % (a) for a in scale_ticks]) # vertically oriented colorbar
  153. pos = list(cbar.ax.get_position().bounds)
  154. fig.text(pos[0] - 0.02, 0.925, '|E|/|E$_0$|', fontsize = 14)
  155. plt.xlabel('Z, nm')
  156. plt.ylabel('X, nm')
  157. # This part draws the nanoshell
  158. from matplotlib import patches
  159. s1 = patches.Arc((0, 0), 2.0*core_r, 2.0*core_r, angle=0.0, zorder=2,
  160. theta1=0.0, theta2=360.0, linewidth=1, color='black')
  161. s2 = patches.Arc((0, 0), 2.0*inner_r, 2.0*inner_r, angle=0.0, zorder=2,
  162. theta1=0.0, theta2=360.0, linewidth=1, color='black')
  163. s3 = patches.Arc((0, 0), 2.0*outer_r, 2.0*outer_r, angle=0.0, zorder=2,
  164. theta1=0.0, theta2=360.0, linewidth=1, color='black')
  165. ax.add_patch(s1)
  166. ax.add_patch(s2)
  167. ax.add_patch(s3)
  168. from matplotlib.path import Path
  169. #import matplotlib.patches as patches
  170. flow_total = 21
  171. for flow in range(0,flow_total):
  172. flow_x, flow_z = GetFlow(scale_x, scale_z, Ec, Hc,
  173. min(scale_x)+flow*(scale_x[-1]-scale_x[0])/(flow_total-1),
  174. min(scale_z), npts*12)
  175. verts = np.vstack((flow_z, flow_x)).transpose().tolist()
  176. codes = [Path.CURVE4]*len(verts)
  177. #codes = [Path.LINETO]*len(verts)
  178. codes[0] = Path.MOVETO
  179. path = Path(verts, codes)
  180. patch = patches.PathPatch(path, facecolor='none', lw=1, edgecolor='white')
  181. ax.add_patch(patch)
  182. plt.savefig("SiAgSi-flow.png")
  183. plt.draw()
  184. plt.show()
  185. plt.clf()
  186. plt.close()
  187. finally:
  188. print("Qabs = "+str(Qabs));
  189. #