efficiency-plasmon-plot.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/env python3
  2. # -*- coding: UTF-8 -*-
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. c = 299792458
  6. pi = np.pi
  7. verbose = 6
  8. def read_data(dirname, distance, zshift):
  9. media = [1,2] # 1 - positive zshift, 2 - negative (need to add a minus sign for real shift).
  10. #min_mesh_step = 2.5 #nm
  11. data = []
  12. data.append([])
  13. for x in distance:
  14. data.append([])
  15. data[x].append([])
  16. for m in media:
  17. data[x].append([])
  18. for z in zshift:
  19. monitor_name = "mon_x"+str(x)+"mkm_media"+str(m)+"_zshift"+z+"nm"
  20. data[x][m].append(
  21. np.transpose(
  22. np.genfromtxt(dirname+"/"+monitor_name+".txt", delimiter=", ",skip_header=1
  23. ,dtype=None, encoding = None
  24. , converters={0: lambda s: complex(s),
  25. 1: lambda s: complex(s),
  26. 2: lambda s: complex(s.replace('i', 'j')),
  27. 3: lambda s: complex(s.replace('i', 'j')),
  28. 4: lambda s: complex(s.replace('i', 'j')),
  29. 5: lambda s: complex(s.replace('i', 'j')),
  30. 6: lambda s: complex(s.replace('i', 'j')),
  31. 7: lambda s: complex(s.replace('i', 'j')),
  32. 8: lambda s: complex(s.replace('i', 'j'))
  33. }
  34. )
  35. )
  36. )
  37. return data
  38. def find_nearest(array,value):
  39. idx = (np.abs(array-value)).argmin()
  40. return array[idx],idx
  41. def get_WLs_idx(WLs, data):
  42. dist = 1 #mkm
  43. mmedia = 1 # vacuum
  44. shift = 1 # one mesh step
  45. WLs_idx = []
  46. for wl in WLs:
  47. val, idx = find_nearest(data[dist][mmedia][shift][0,:],wl*1e-9)
  48. WLs_idx.append(idx)
  49. return WLs_idx
  50. def check_field_match(data_in_air, data_in_gold,wl_idx,z_vec,kappa1,kappa2,eps2):
  51. H1 = data_in_air[:,6,wl_idx]
  52. H2 = data_in_gold[:,6,wl_idx]
  53. E1 = data_in_air[:,4,wl_idx]
  54. E2 = data_in_gold[:,4,wl_idx]
  55. for i in range(len(z_vec)):
  56. z = z_vec[i]*1e-9
  57. if verbose > 8: print("z =",z)
  58. H1_0 = H1[i]/np.exp(-kappa1[wl_idx]*z)
  59. H2_0 = H2[i]/np.exp(-kappa2[wl_idx]*z)
  60. E1_0 = E1[i]/np.exp(-kappa1[wl_idx]*z)
  61. E2_0 = E2[i]/np.exp(-kappa2[wl_idx]*z)
  62. E2_0e = E2[i]/np.exp(-kappa2[wl_idx]*z)*eps2[wl_idx]
  63. if verbose > 8:
  64. print("H0 air (%5.4g %+5.4gj)"%(np.real(H1_0), np.imag(H1_0)),
  65. " from H1 (%5.4g %+5.4gj)"%(np.real(H1[i]), np.imag(H1[i])))
  66. print("H0 gold (%5.4g %+5.4gj)"%(np.real(H2_0), np.imag(H2_0)),
  67. " from H2 (%5.4g %+5.4gj)"%(np.real(H2[i]), np.imag(H2[i])))
  68. print("E0 air (%5.4g %+5.4gj)"%(np.real(E1_0), np.imag(E1_0)),
  69. " from E1 (%5.4g %+5.4gj)"%(np.real(E1[i]), np.imag(E1[i])))
  70. print("E0*eps2 (%5.4g %+5.4gj)"%(np.real(E2_0e), np.imag(E2_0e)),
  71. " from E2 (%5.4g %+5.4gj)"%(np.real(E2[i]), np.imag(E2[i])))
  72. print("E0 gold (%5.4g %+5.4gj)"%(np.real(E2_0), np.imag(E2_0)))
  73. def analyze(data, dist, z_vec, wl_idx):
  74. ''' dist in mkm!!!
  75. '''
  76. #data = [dist][mmedia][shift] "lambda, dip.power, Ex, Ey, Ez, Hx, Hy, Hz, n_Au"
  77. # 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 "
  78. data_in_air = np.array(data[dist][1])
  79. data_in_gold = np.array(data[dist][2])
  80. lambd = data_in_air[0][0,:]
  81. omega = 2*pi*c/lambd
  82. dip_power = data_in_air[0][1,:]
  83. Ex = data_in_air[0,2,0]
  84. Ey = data_in_air[0,3,0]
  85. Ez = data_in_air[0,4,0]
  86. Hx = data_in_air[0,5,0]
  87. Hy = data_in_air[0,6,0]
  88. Hz = data_in_air[0,7,0]
  89. E = np.array([Ex,Ey,Ez])
  90. H = np.array([Hx,Hy,Hz])
  91. print("S from full field",np.real(np.cross(E,np.conj(H))))
  92. eps1 = complex(1)
  93. n_Au = data_in_air[0][8,:]
  94. eps2 = n_Au**2
  95. k_0 = omega/c #air
  96. k_spp = k_0*np.sqrt(eps1*eps2/(eps1+eps2))
  97. kappa1= np.sqrt(k_spp**2 - eps1*k_0**2)
  98. kappa2= np.sqrt(k_spp**2 - eps2*k_0**2)
  99. check_field_match(data_in_air, data_in_gold,wl_idx,z_vec,kappa1,kappa2,eps2)
  100. H1 = data_in_air[:,6]
  101. E1 = data_in_air[:,4]
  102. z = z_vec[0]*1e-9
  103. if verbose > 5: print("Using data from air monitor at z =",z)
  104. H1_0 = H1[0]/np.exp(-kappa1*z)
  105. E1_0 = E1[0]/np.exp(-kappa1*z)
  106. E2_0 = E1[0]/eps2
  107. if verbose > 5:
  108. print("H0 air (%5.4g %+5.4gj)"%(np.real(H1_0[wl_idx]), np.imag(H1_0[wl_idx])),
  109. " from H1 (%5.4g %+5.4gj)"%(np.real(H1[0][wl_idx]), np.imag(H1[0][wl_idx])))
  110. print("E0 air (%5.4g %+5.4gj)"%(np.real(E1_0[wl_idx]), np.imag(E1_0[wl_idx])),
  111. " from E1 (%5.4g %+5.4gj)"%(np.real(E1[0][wl_idx]), np.imag(E1[0][wl_idx])))
  112. print("E0 gold (%5.4g %+5.4gj)"%(np.real(E2_0[wl_idx]), np.imag(E2_0[wl_idx])), " from E1")
  113. R = dist*1e-6
  114. print("R =",R)
  115. #plasmon_power = 1.0/2.0 * np.real( E1[0] * np.conj(H1[0])) # TODO check minus sign!!
  116. plasmon_power = -1.0/2.0 * 2.0*np.pi*R * ( # TODO check minus sign!!
  117. np.real( E1_0 * np.conj(H1_0) )
  118. / (2.0 * np.real(kappa1))
  119. +
  120. np.real( E2_0 * np.conj(H1_0) )
  121. / (2.0 * np.real(kappa2))
  122. )* np.exp( 2.0*np.imag(k_spp)*R ) # TODO check minus sign!!
  123. #print(np.abs(plasmon_power/ dip_power))
  124. eta0 = plasmon_power[0]/ dip_power[0] *100
  125. ppw = plasmon_power[0]
  126. print("\n")
  127. print(dirname)
  128. 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)
  129. plt.plot(lambd*1e9, plasmon_power/ dip_power)
  130. plt.ylim(0,1.0)
  131. #plt.plot(lambd*1e9, np.real(eps2))
  132. # plt.plot(lambd*1e9, np.real(k_spp))
  133. # plt.plot(lambd*1e9, k_0)
  134. #plt.semilogy(lambd*1e9, np.absolute(plasmon_power/ dip_power))
  135. # # legend = []
  136. # # legend.append(zshift[shift]+"@"+str(WLs[i])+" nm")
  137. # # plt.legend(legend)
  138. # # #plt.xlabel(r'THz')
  139. plt.xlabel(r'$\lambda$, nm')
  140. plt.ylabel(r'$P_{spp}/P_{dipole}$',labelpad=-5)
  141. #plt.title(' R = '+str(core_r)+' nm')
  142. plt.savefig(dirname+"_power_ratio."+file_ext)
  143. plt.clf()
  144. plt.close()
  145. file_ext="pdf"
  146. #dirname="template-dipole-on-sphere-on-surf-z.fsp.results"
  147. #dirname="Au-JC-R100-Au-JC.fsp.results"
  148. #dirname="Au-McPeak-R100-Si-Green.fsp.results"
  149. #dirname="Au-McPeak-R100-Au-McPeak.fsp.results"
  150. #dirname="Au-McPeak-R0.fsp.results"
  151. #dirname="Au-McPeak-R100-Si-Green-1500.fsp.results"
  152. #dirname="Au-McPeak-R100-Si-Green-1500-l.fsp.results"
  153. dirname="Au-McPeak-R50-Si-Green-1500-l.fsp.results"
  154. def main ():
  155. distance = [1,2,3,4,5,6,7,8,9,10] #mkm
  156. zshift = ["5","20","200","400","600"]
  157. z_vec = [int(val) for val in zshift]
  158. data = read_data(dirname, distance, zshift)
  159. #WLs=[300,350,400,450,600,700,800]
  160. #WLs=[600,700, 800, 450]
  161. WLs=[800]#, 450]
  162. WLs_idx = get_WLs_idx(WLs, data)
  163. dist = 10 #mkm
  164. wl_idx = WLs_idx[0]
  165. analyze(data, dist, z_vec, wl_idx)
  166. # legend = []
  167. # for shift in range(len(zshift)):
  168. # for i in range(len(WLs)):
  169. # pl_data = []
  170. # idx = WLs_idx[i]
  171. # legend.append(zshift[shift]+"@"+str(WLs[i])+" nm")
  172. # for dist in distance:
  173. # pl_data.append(np.absolute(data[dist][mmedia][shift][2,idx]*np.sqrt(dist)))
  174. # print(len(pl_data))
  175. # plt.semilogy(distance, pl_data,marker="o")
  176. # plt.legend(legend)
  177. # # #plt.xlabel(r'THz')
  178. # plt.xlabel(r'Monitor R, $\mu$m')
  179. # plt.ylabel(r'$Abs(E_x) \sqrt{R}$',labelpad=-5)
  180. # # plt.title(' r = '+str(core_r))
  181. # plt.savefig(dirname+"_WLs."+file_ext)
  182. # plt.clf()
  183. # plt.close()
  184. main()