plasmon-modal-efficiency.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. from scipy.special import hankel2 as H2n
  4. import matplotlib.pyplot as plt
  5. import numpy as np
  6. import os
  7. import scipy.io
  8. c = 299792458.0
  9. eps_0 = 8.854187817e-12 # F/m
  10. pi = np.pi
  11. verbose = 6
  12. # r of monitor
  13. #r = 146.513e-9
  14. r = 800e-9
  15. #debug = True
  16. debug = False
  17. def read_data_mat(fname):
  18. #data = "z, dip.power, Ex, Ey, Ez, Hx, Hy, Hz, n_Au"
  19. # 0, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 "
  20. mat = scipy.io.loadmat(fname)
  21. lambd = np.reshape(mat["lambda"],(-1))
  22. dippower = np.reshape(mat["dippower"],(-1))
  23. z = np.reshape(mat["z"],(-1))
  24. n_Au = np.reshape(mat["n_fdtd"],(-1))
  25. onez = np.ones((len(z)))
  26. data1 = {}
  27. r1 = mat["mon1_E"][0,0][4][0,0]
  28. E1 = mat["mon1_E"][0,0][0]
  29. H1 = mat["mon1_H"][0,0][0]
  30. data2 = {}
  31. r2 = mat["mon2_E"][0,0][4][0,0]
  32. E2 = mat["mon2_E"][0,0][0]
  33. H2 = mat["mon2_H"][0,0][0]
  34. for i in range(len(lambd)):
  35. fdata = np.vstack(( z.astype(np.complex128), dippower[i]*onez.astype(np.complex128)
  36. ,E1[:,0,i], E1[:,1,i], E1[:,2,i]
  37. ,H1[:,0,i], H1[:,1,i], H1[:,2,i]
  38. ,n_Au[i]*onez.astype(np.complex128)
  39. ))
  40. data1[lambd[i]]=fdata
  41. fdata = np.vstack(( z.astype(np.complex128), dippower[i]*onez.astype(np.complex128)
  42. ,E2[:,0,i], E2[:,1,i], E2[:,2,i]
  43. ,H2[:,0,i], H2[:,1,i], H2[:,2,i]
  44. ,n_Au[i]*onez.astype(np.complex128)
  45. ))
  46. data2[lambd[i]]=fdata
  47. if debug: break
  48. return ((r1,data1),(r2,data2))
  49. def read_data(dirname):
  50. data = {}
  51. WLs = []
  52. for r,d,f in os.walk(dirname):
  53. for fname in f:
  54. WLs.append(fname)
  55. for fname in WLs:
  56. fdata = np.transpose(
  57. np.genfromtxt(dirname+"/"+fname, delimiter=", ",skip_header=1
  58. ,dtype=None, encoding = None
  59. , converters={0: lambda s: complex(s),
  60. 1: lambda s: complex(s),
  61. 2: lambda s: complex(s.replace('i', 'j')),
  62. 3: lambda s: complex(s.replace('i', 'j')),
  63. 4: lambda s: complex(s.replace('i', 'j')),
  64. 5: lambda s: complex(s.replace('i', 'j')),
  65. 6: lambda s: complex(s.replace('i', 'j')),
  66. 7: lambda s: complex(s.replace('i', 'j')),
  67. 8: lambda s: complex(s.replace('i', 'j'))
  68. }
  69. )
  70. )
  71. data[float(fname[2:-4])]=fdata
  72. if debug: break
  73. return data
  74. def find_nearest(array,value):
  75. idx = (np.abs(array-value)).argmin()
  76. return array[idx],idx
  77. def get_WLs_idx(WLs, data):
  78. dist = 1 #mkm
  79. mmedia = 1 # vacuum
  80. shift = 1 # one mesh step
  81. WLs_idx = []
  82. for wl in WLs:
  83. val, idx = find_nearest(data[dist][mmedia][shift][0,:],wl*1e-9)
  84. WLs_idx.append(idx)
  85. return WLs_idx
  86. # def check_field_match(data_in_air, data_in_gold,wl_idx,z_vec,kappa1,kappa2,eps2):
  87. # z = z_vec[i]*1e-9
  88. # if verbose > 8: print("z =",z)
  89. # H1_0 = H1[i]/np.exp(-kappa1[wl_idx]*z)
  90. # H2_0 = H2[i]/np.exp(-kappa2[wl_idx]*z)
  91. # E1_0 = E1[i]/np.exp(-kappa1[wl_idx]*z)
  92. # E2_0 = E2[i]/np.exp(-kappa2[wl_idx]*z)
  93. # E2_0e = E2[i]/np.exp(-kappa2[wl_idx]*z)*eps2[wl_idx]
  94. # if verbose > 8:
  95. # print("H0 air (%5.4g %+5.4gj)"%(np.real(H1_0), np.imag(H1_0)),
  96. # " from H1 (%5.4g %+5.4gj)"%(np.real(H1[i]), np.imag(H1[i])))
  97. def analyze(data,wl):
  98. # print(data[0,:]) # all z values
  99. #data = "z, dip.power, Ex, Ey, Ez, Hx, Hy, Hz, n_Au"
  100. # 0, 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 "
  101. lambd = wl
  102. omega = 2*pi*c/lambd
  103. eps_d = complex(1) # air, z>0
  104. eps_m = data[8,0]**2 # metal, z<0
  105. dip_power = data[1,0]
  106. z = data[0,:]
  107. idx_d = np.nonzero(z>1e-10)
  108. idx_0 = np.nonzero(np.logical_and(z<=1e-10, z>=-1e-10))
  109. idx_m = np.nonzero(z<-1e-10)
  110. z_d = z[idx_d]
  111. z_0 = z[idx_0]
  112. z_m = z[idx_m]
  113. if (not np.array_equal(np.hstack((z_m, z_0, z_d)), z)):
  114. print("ERROR! loosing z values!")
  115. raise
  116. Ex = data[2,:]
  117. Ex_m = data[2,idx_m][0]
  118. Ey_m = data[3,idx_m][0]
  119. Ez_m = data[4,idx_m][0]
  120. Hx_m = data[5,idx_m][0]
  121. Hy_m = data[6,idx_m][0]
  122. Hz_m = data[7,idx_m][0]
  123. E_m = np.transpose(np.array([Ex_m,Ey_m,Ez_m]))
  124. H_m = np.transpose(np.array([Hx_m,Hy_m,Hz_m]))
  125. Ex_d = data[2,idx_d][0]
  126. Ey_d = data[3,idx_d][0]
  127. Ez_d = data[4,idx_d][0]
  128. Hx_d = data[5,idx_d][0]
  129. Hy_d = data[6,idx_d][0]
  130. Hz_d = data[7,idx_d][0]
  131. E_d = np.transpose(np.array([Ex_d,Ey_d,Ez_d]))
  132. H_d = np.transpose(np.array([Hx_d,Hy_d,Hz_d]))
  133. k_0 = omega/c #air
  134. k_sp = k_0*np.sqrt(eps_d*eps_m/(eps_d+eps_m)) # eq5, supmat
  135. chi_d = np.sqrt( eps_d*k_0**2 - k_sp**2 ) # desc. after eq6c, supmat
  136. chi_m = np.sqrt( eps_m*k_0**2 - k_sp**2 ) # desc. after eq6c, supmat
  137. h_sp_d = np.exp(1j*chi_d*z_d) # eq6a, supmat
  138. e_sp_x_d = chi_d/(omega*eps_0*eps_d)*np.exp(1j*chi_d*z_d) # eq6b, supmat
  139. e_sp_z_d = k_sp/(omega*eps_0*eps_d)*np.exp(1j*chi_d*z_d) # eq6c, supmat
  140. h_sp_m = np.exp(1j*-chi_m*z_m) # eq6a, supmat
  141. e_sp_x_m = -chi_m/(omega*eps_0*eps_m)*np.exp(1j*-chi_m*z_m) # eq6b, supmat
  142. e_sp_z_m = k_sp/(omega*eps_0*eps_m)*np.exp(1j*-chi_m*z_m) # eq6c, supmat
  143. zero_m = np.zeros(len(h_sp_m))
  144. zero_d = np.zeros(len(h_sp_d))
  145. E_minus_sp_0_m = np.transpose([1j*H2n(1,k_sp*r)*e_sp_x_m,
  146. zero_m,
  147. H2n(0,k_sp*r)*e_sp_z_m]) # eq11, supmat, replace E_plus to E_minus and H1n to H2n
  148. H_minus_sp_0_m = np.transpose([zero_m,
  149. 1j*H2n(1,k_sp*r)*h_sp_m,
  150. zero_m]) # eq11, supmat, replace E_plus to E_minus and H1n to H2n
  151. E_minus_sp_0_d = np.transpose([1j*H2n(1,k_sp*r)*e_sp_x_d,
  152. zero_d,
  153. H2n(0,k_sp*r)*e_sp_z_d]) # eq11, supmat, replace E_plus to E_minus and H1n to H2n
  154. H_minus_sp_0_d = np.transpose([zero_d,
  155. 1j*H2n(1,k_sp*r)*h_sp_d,
  156. zero_d]) # eq11, supmat, replace E_plus to E_minus and H1n to H2n
  157. # E_m H_m E_d H_d
  158. N_sp_0 = (((-1)**0) * (4.0j/(omega*eps_0*k_sp))
  159. * (eps_d**2 - eps_m**2) / ((eps_m*eps_d)**(3/2)) )
  160. tmp_m = np.cross(E_minus_sp_0_m,H_m) - np.cross(E_m, H_minus_sp_0_m)
  161. radail_pojeciton_m = np.transpose(tmp_m)[0]
  162. integrand_m = (2*pi/N_sp_0)*radail_pojeciton_m*r
  163. tmp_d = np.cross(E_minus_sp_0_d,H_d) - np.cross(E_d, H_minus_sp_0_d)
  164. radail_pojeciton_d = np.transpose(tmp_d)[0]
  165. integrand_d = (2*pi/N_sp_0)*radail_pojeciton_d*r
  166. A_sp_0_m = np.trapz(integrand_m, z_m)
  167. A_sp_0_d = np.trapz(integrand_d, z_d)
  168. A_sp_0 = A_sp_0_m + A_sp_0_d
  169. return np.absolute(A_sp_0)**2
  170. # print("S from full field",np.real(np.cross(E,np.conj(H))))
  171. # print("H0 air (%5.4g %+5.4gj)"%(np.real(H1_0[wl_idx]), np.imag(H1_0[wl_idx])),
  172. # " from H1 (%5.4g %+5.4gj)"%(np.real(H1[0][wl_idx]), np.imag(H1[0][wl_idx])))
  173. # #plasmon_power = 1.0/2.0 * np.real( E1[0] * np.conj(H1[0])) # TODO check minus sign!!
  174. # plasmon_power = -1.0/2.0 * 2.0*np.pi*R * ( # TODO check minus sign!!
  175. # np.real( E1_0 * np.conj(H1_0) )
  176. # / (2.0 * np.real(kappa1))
  177. # +
  178. # np.real( E2_0 * np.conj(H1_0) )
  179. # / (2.0 * np.real(kappa2))
  180. # )* np.exp( 2.0*np.imag(k_spp)*R ) # TODO check minus sign!!
  181. # #print(np.abs(plasmon_power/ dip_power))
  182. # eta0 = plasmon_power[0]/ dip_power[0] *100
  183. # ppw = plasmon_power[0]
  184. # print("\n")
  185. # print(dirname)
  186. # print("Power: plasmon %4.3g W of dipoles %4.3g W, efficiency %5.3g%% from:"%(ppw, float(np.abs(dip_power[0])),float(np.abs( eta0))), ppw, eta0)
  187. # plt.plot(lambd*1e9, plasmon_power/ dip_power)
  188. # plt.ylim(0,0.04)
  189. # plt.xlim(550,800)
  190. # #plt.plot(lambd*1e9, np.real(eps2))
  191. # # plt.plot(lambd*1e9, np.real(k_spp))
  192. # # plt.plot(lambd*1e9, k_0)
  193. # #plt.semilogy(lambd*1e9, np.absolute(plasmon_power/ dip_power))
  194. # # # legend = []
  195. # # # legend.append(zshift[shift]+"@"+str(WLs[i])+" nm")
  196. # # # plt.legend(legend)
  197. # # # #plt.xlabel(r'THz')
  198. # plt.xlabel(r'$\lambda$, nm')
  199. # plt.ylabel(r'$P_{spp}/P_{dipole}$',labelpad=-5)
  200. # #plt.title(' R = '+str(core_r)+' nm')
  201. # plt.savefig(dirname+"_power_ratio."+file_ext)
  202. # plt.clf()
  203. # plt.close()
  204. file_ext="pdf"
  205. def main ():
  206. #dirname="bigourdan-Au-sub-dipole-W.fsp.1D.monitor_1.results"
  207. dirname="bigourdan-Au-sub-dipole-W-2mon.fsp.1D.monitor_2.results"
  208. data = read_data(dirname)
  209. filename = 'bigourdan-Au-sub-dipole-W-2mon.fsp.1D.mat'
  210. data2 = read_data_mat(filename)
  211. r,data = data2[0]
  212. if verbose > 5:
  213. print("r =",r)
  214. WLs = []
  215. A2 = []
  216. for wl in data:
  217. WLs.append(wl)
  218. A2.append(analyze(data[wl],wl))
  219. #print(WLs)
  220. WLs1 = np.array(WLs)
  221. A21 = np.array(A2)
  222. # #dirname="bigourdan-Au-sub-Cyl-dipole-W.fsp.1D.monitor_1.results"
  223. # dirname="bigourdan-Au-sub-Cyl-dipole-W-2mon.fsp.1D.monitor_2.results"
  224. # data = read_data(dirname)
  225. filename = 'bigourdan-Au-sub-Cyl-dipole-W-2mon.fsp.1D.mat'
  226. data2 = read_data_mat(filename)
  227. r,data = data2[0]
  228. WLs = []
  229. A2 = []
  230. for wl in data:
  231. WLs.append(wl)
  232. A2.append(analyze(data[wl],wl))
  233. #print(WLs)
  234. WLs2 = np.array(WLs)
  235. A22 = np.array(A2)
  236. # data = np.vstack((WLs,A2))
  237. # print(np.sort(data))
  238. plt.plot(WLs1*1e9, A21*275, color="black",label="x 275, no ant.")
  239. plt.plot(WLs2*1e9, A22, color="red", label="with antena")
  240. plt.legend()
  241. plt.xlabel(r'$\lambda$, nm')
  242. plt.ylim(0,0.5)
  243. plt.ylabel(r'$|A_{sp}|^2$',labelpad=-1)
  244. #plt.title(dirname)
  245. plt.savefig(dirname+"_A2."+file_ext)
  246. plt.clf()
  247. plt.close()
  248. main()