field-SiAgSi.py 7.3 KB

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