field-Ag-flow.py 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. x_pos = flow_x[-1]
  45. z_pos = flow_z[-1]
  46. x_idx = get_index(scale_x, x_pos)
  47. z_idx = get_index(scale_z, z_pos)
  48. S=np.cross(Ec[npts*z_idx+x_idx], Hc[npts*z_idx+x_idx].conjugate()).real
  49. #if (np.linalg.norm(S)> 1e-4):
  50. Snorm_prev=S/np.linalg.norm(S)
  51. Snorm_prev=Snorm_prev.real
  52. for n in range(0, nmax):
  53. #Get the next position
  54. #1. Find Poynting vector and normalize it
  55. x_pos = flow_x[-1]
  56. z_pos = flow_z[-1]
  57. x_idx = get_index(scale_x, x_pos)
  58. z_idx = get_index(scale_z, z_pos)
  59. Epoint = Ec[npts*z_idx+x_idx]
  60. Hpoint = Hc[npts*z_idx+x_idx]
  61. S=np.cross(Epoint, Hpoint.conjugate())
  62. #if (np.linalg.norm(S)> 1e-4):
  63. Snorm=S.real/np.linalg.norm(S)
  64. #Snorm=Snorm.real
  65. #2. Evaluate displacement = half of the discrete and new position
  66. dpos = abs(scale_z[0]-scale_z[1])/2.0
  67. dx = dpos*Snorm[0];
  68. dz = dpos*Snorm[2];
  69. x_pos = x_pos+dx
  70. z_pos = z_pos+dz
  71. #3. Save result
  72. flow_x.append(x_pos)
  73. flow_z.append(z_pos)
  74. return flow_x, flow_z
  75. # # a)
  76. #WL=400 #nm
  77. #core_r = WL/20.0
  78. #epsilon_Ag = -2.0 + 10.0j
  79. # # b)
  80. #WL=400 #nm
  81. #core_r = WL/20.0
  82. #epsilon_Ag = -2.0 + 1.0j
  83. # c)
  84. WL=354 #nm
  85. core_r = WL/20.0
  86. epsilon_Ag = -2.0 + 0.28j
  87. # d)
  88. #WL=367 #nm
  89. #core_r = WL/20.0
  90. #epsilon_Ag = -2.71 + 0.25j
  91. index_Ag = np.sqrt(epsilon_Ag)
  92. # n1 = 1.53413
  93. # n2 = 0.565838 + 7.23262j
  94. nm = 1.0
  95. x = np.ones((1, 2), dtype = np.float64)
  96. x[0, 0] = 2.0*np.pi*core_r/WL/4.0*3.0
  97. x[0, 1] = 2.0*np.pi*core_r/WL
  98. m = np.ones((1, 2), dtype = np.complex128)
  99. m[0, 0] = index_Ag/nm
  100. m[0, 1] = index_Ag/nm
  101. print "x =", x
  102. print "m =", m
  103. npts = 281
  104. factor=3
  105. scan = np.linspace(-factor*x[0, 0], factor*x[0, 0], npts)
  106. coordX, coordZ = np.meshgrid(scan, scan)
  107. coordX.resize(npts*npts)
  108. coordZ.resize(npts*npts)
  109. coordY = np.zeros(npts*npts, dtype = np.float64)
  110. coord = np.vstack((coordX, coordY, coordZ)).transpose()
  111. #coord = np.vstack((coordY, coordX, coordZ)).transpose()
  112. terms, Qext, Qsca, Qabs, Qbk, Qpr, g, Albedo, S1, S2 = scattnlay(x, m)
  113. terms, E, H = fieldnlay(x, m, coord)
  114. P = np.array(map(lambda n: np.linalg.norm(np.cross(E[0][n], H[0][n].conjugate())).real, range(0, len(E[0]))))
  115. Ec = E[0, :, :]
  116. Hc = H[0, :, :]
  117. try:
  118. import matplotlib.pyplot as plt
  119. from matplotlib import cm
  120. from matplotlib.colors import LogNorm
  121. # min_tick = 0.0
  122. # max_tick = 1.0
  123. Eabs_data = np.resize(P, (npts, npts)).T
  124. #Eabs_data = np.resize(Eabs, (npts, npts)).T
  125. #Eabs_data = np.resize(Eangle, (npts, npts)).T
  126. #Eabs_data = np.resize(Habs, (npts, npts)).T
  127. #Eabs_data = np.resize(Hangle, (npts, npts)).T
  128. fig, ax = plt.subplots(1, 1)#, sharey=True, sharex=True)
  129. #idxs = np.where(np.abs(coordX) < 1e-10)
  130. #print H[0, idxs][0, :, 1]
  131. #axs[0].errorbar(coordZ[idxs]*WL/2.0/np.pi/nm, P[idxs], fmt = 'r', label = 'Poynting vector')
  132. #axs[0].errorbar(coordZ[idxs]*WL/2.0/np.pi/nm, np.linalg.norm(E[0, idxs][0], axis = 1), fmt = 'g', label = 'E')
  133. # axs[0].errorbar(coordZ[idxs]*WL/2.0/np.pi/nm, np.linalg.norm(H[0, idxs][0], axis = 1), fmt = 'b', label = 'H')
  134. # axs[0].errorbar(coordZ[idxs]*WL/2.0/np.pi/nm, H[0, idxs][0, :, 1].real, fmt = 'k', label = 'H.r')
  135. # axs[0].errorbar(coordZ[idxs]*WL/2.0/np.pi/nm, H[0, idxs][0, :, 1].imag, fmt = 'b', label = 'H.i')
  136. #axs[0].errorbar(coordZ[idxs]*WL/2.0/np.pi/nm, H[0, idxs][0, :, 0].real, fmt = 'b', label = 'Px')
  137. #axs[0].errorbar(coordZ[idxs]*WL/2.0/np.pi/nm, H[0, idxs][0, :, 1].real, fmt = 'k', label = 'Py')
  138. #axs[0].errorbar(coordZ[idxs]*WL/2.0/np.pi/nm, H[0, idxs][0, :, 2].real, fmt = 'b', label = 'Pz')
  139. #axs[0].legend()
  140. #fig.tight_layout()
  141. # Rescale to better show the axes
  142. scale_x = np.linspace(min(coordX)*WL/2.0/np.pi/nm, max(coordX)*WL/2.0/np.pi/nm, npts)
  143. scale_z = np.linspace(min(coordZ)*WL/2.0/np.pi/nm, max(coordZ)*WL/2.0/np.pi/nm, npts)
  144. # Define scale ticks
  145. min_tick = np.amin(Eabs_data)
  146. max_tick = np.amax(Eabs_data)
  147. # scale_ticks = np.power(10.0, np.linspace(np.log10(min_tick), np.log10(max_tick), 6))
  148. scale_ticks = np.linspace(min_tick, max_tick, 11)
  149. # Interpolation can be 'nearest', 'bilinear' or 'bicubic'
  150. ax.set_title(r'$Re(E \times H^*)$')
  151. cax = ax.imshow(Eabs_data, interpolation = 'nearest', cmap = cm.jet,
  152. origin = 'lower'
  153. #, vmin = min_tick, vmax = max_tick
  154. , extent = (min(scale_x), max(scale_x), min(scale_z), max(scale_z))
  155. #,norm = LogNorm()
  156. )
  157. ax.axis("image")
  158. # # Add colorbar
  159. cbar = fig.colorbar(cax, ticks = [a for a in scale_ticks])
  160. cbar.ax.set_yticklabels(['%5.3g' % (a) for a in scale_ticks]) # vertically oriented colorbar
  161. # pos = list(cbar.ax.get_position().bounds)
  162. # fig.text(pos[0] - 0.02, 0.925, '|E|/|E$_0$|', fontsize = 14)
  163. plt.xlabel('Z, nm')
  164. plt.ylabel('X, nm')
  165. # This part draws the nanoshell
  166. from matplotlib import patches
  167. s1 = patches.Arc((0, 0), 2.0*core_r, 2.0*core_r, angle=0.0, zorder=2,
  168. theta1=0.0, theta2=360.0, linewidth=1, color='black')
  169. ax.add_patch(s1)
  170. from matplotlib.path import Path
  171. #import matplotlib.patches as patches
  172. flow_total = 39
  173. for flow in range(0,flow_total):
  174. flow_x, flow_z = GetFlow(scale_x, scale_z, Ec, Hc,
  175. min(scale_x)+flow*(scale_x[-1]-scale_x[0])/(flow_total-1),
  176. min(scale_z),
  177. #0.0,
  178. npts*16)
  179. verts = np.vstack((flow_z, flow_x)).transpose().tolist()
  180. #codes = [Path.CURVE4]*len(verts)
  181. codes = [Path.LINETO]*len(verts)
  182. codes[0] = Path.MOVETO
  183. path = Path(verts, codes)
  184. patch = patches.PathPatch(path, facecolor='none', lw=1, edgecolor='white')
  185. ax.add_patch(patch)
  186. # # Start powerflow lines in the middle of the image
  187. # flow_total = 131
  188. # for flow in range(0,flow_total):
  189. # flow_x, flow_z = GetFlow(scale_x, scale_z, Ec, Hc,
  190. # min(scale_x)+flow*(scale_x[-1]-scale_x[0])/(flow_total-1),
  191. # 15.0, #min(scale_z),
  192. # npts*6)
  193. # verts = np.vstack((flow_z, flow_x)).transpose().tolist()
  194. # #codes = [Path.CURVE4]*len(verts)
  195. # codes = [Path.LINETO]*len(verts)
  196. # codes[0] = Path.MOVETO
  197. # path = Path(verts, codes)
  198. # patch = patches.PathPatch(path, facecolor='none', lw=1, edgecolor='white')
  199. # ax.add_patch(patch)
  200. plt.savefig("Ag-flow.png")
  201. plt.draw()
  202. plt.show()
  203. plt.clf()
  204. plt.close()
  205. finally:
  206. print("Qabs = "+str(Qabs));
  207. #