oldqx.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import snlay as sn
  2. import numpy as np
  3. from scipy import interpolate
  4. from noise import pnoise1
  5. import random
  6. from tmm import coh_tmm
  7. import multiprocessing as mp
  8. import makeqx as mkq
  9. #import pyximport; pyximport.install(pyimport=True)
  10. #import mtmm as mtmm
  11. # make a materials dictionary
  12. matsdict = {
  13. 1: "./materials/gold.dat",
  14. 2: "./materials/silicon.dat",
  15. 3: "./materials/silica.dat",
  16. 4: "./materials/tio2.dat",
  17. 5: "./materials/silver.dat",
  18. }
  19. def get_nk(datafile, wavelengths):
  20. """Reads the given file and returns the n+ik complex at
  21. the given wavelength after suitable interpolation
  22. :datafile: TODO
  23. :wavelength: TODO
  24. :returns: TODO
  25. """
  26. rawdisp = np.loadtxt(datafile)
  27. f_r = interpolate.interp1d(rawdisp[:, 0], rawdisp[:, 1])
  28. f_i = interpolate.interp1d(rawdisp[:, 0], rawdisp[:, 2])
  29. return f_r(wavelengths) + 1j * f_i(wavelengths)
  30. def nk_2_eps(n):
  31. """TODO: Docstring for nk_2_epsnk_2_eps.
  32. :returns: complex epsilon given n and kappa
  33. """
  34. eps_r = n.real ** 2 - n.imag ** 2
  35. eps_i = 2 * n.real * n.imag
  36. return eps_r + 1j * eps_i
  37. def eps_2_nk(eps):
  38. """TODO: Docstring for nk_2_epsnk_2_eps.
  39. :returns: complex epsilon given n and kappa
  40. """
  41. modeps = np.abs(eps)
  42. n_r = np.sqrt(0.5 * (modeps + eps.real))
  43. n_i = np.sqrt(0.5 * (modeps - eps.real))
  44. return n_r + 1j * n_i
  45. def LL_mixing(fH, n_H, n_L):
  46. """TODO: Docstring for brugg_mixingbrugg_mixing.
  47. Given the volumne fraction of the higher index material, give the effective
  48. index of the layer
  49. :fH: volumne fraction from 0 to 1
  50. :n_H: ri of the higher index material
  51. :n_L: ri of the lower index material
  52. :returns: TODO
  53. """
  54. eH = nk_2_eps(n_H)
  55. eL = nk_2_eps(n_L)
  56. bigK = fH * (eH - 1) / (eH + 2) + (1 - fH) * (eL - 1) / (eL + 2)
  57. e_eff = (1 + 2 * bigK) / (1 - bigK)
  58. return eps_2_nk(e_eff)
  59. def make_nxdx(qx, wavelen=550, n_substrate=1.52):
  60. """TODO: Docstring for make_nxdx.
  61. :qx: TODO
  62. :n_substrate: TODO
  63. :layer_thickness: TODO
  64. :returns: TODO
  65. """
  66. num_layers = int(qx.size)
  67. d_x = [np.inf] + qx.tolist() + [np.inf]
  68. sde = np.zeros_like(qx)
  69. sde[::2] = 1.45
  70. sde[1::2] = 2.58
  71. n_x = [1.0] + sde.tolist() + [n_substrate]
  72. return d_x, n_x
  73. # def tmm_eval2(
  74. # qx,
  75. # cthick,
  76. # lam_low=400, # in nm
  77. # lam_high=1200,
  78. # lam_pts=100,
  79. # ang_low=0, # degrees
  80. # ang_high=90,
  81. # ang_pts=25,
  82. # n_subs=1.52,
  83. # ):
  84. # """TODO: Docstring for tmm_eval.
  85. # :qx: TODO
  86. # :lam_low: TODO
  87. # :#in nm
  88. # lam_high: TODO
  89. # :lam_pts: TODO
  90. # :ang_low: TODO
  91. # :#degrees
  92. # ang_high: TODO
  93. # :ang_pts: TODO
  94. # :returns: TODO
  95. # """
  96. # degree = np.pi / 180
  97. # lams = np.linspace(lam_low, lam_high, endpoint=True, num=lam_pts)
  98. # thetas = np.linspace(ang_high, ang_low, endpoint=True, num=ang_pts)
  99. # Rs = np.zeros((thetas.size, lams.size))
  100. # Rp = np.zeros((thetas.size, lams.size))
  101. # for tidx, theta in enumerate(thetas):
  102. # for lidx, lam in enumerate(lams):
  103. # d_x, n_x = make_nxdx(qx=qx, cthick=cthick, wavelen=lam, n_substrate=n_subs)
  104. # Rs[tidx, lidx] = 100 * coh_tmm(
  105. # "s", n_x, d_x, th_0=theta * degree, lam_vac=lam
  106. # )
  107. # Rp[tidx, lidx] = 100 * coh_tmm(
  108. # "p", n_x, d_x, th_0=theta * degree, lam_vac=lam
  109. # )
  110. # return Rs, Rp
  111. # def tmm_eval(
  112. # qx,
  113. # cthick,
  114. # lam_low=400, # in nm
  115. # lam_high=1200,
  116. # lam_pts=100,
  117. # ang_low=0, # degrees
  118. # ang_high=90,
  119. # ang_pts=25,
  120. # n_subs=1.52,
  121. # ):
  122. # """TODO: Docstring for tmm_eval.
  123. # :qx: TODO
  124. # :lam_low: TODO
  125. # :#in nm
  126. # lam_high: TODO
  127. # :lam_pts: TODO
  128. # :ang_low: TODO
  129. # :#degrees
  130. # ang_high: TODO
  131. # :ang_pts: TODO
  132. # :returns: TODO
  133. # """
  134. # degree = np.pi / 180
  135. # lams = np.linspace(lam_low, lam_high, endpoint=True, num=lam_pts)
  136. # thetas = np.linspace(ang_high, ang_low, endpoint=True, num=ang_pts)
  137. # Rs = np.zeros((thetas.size, lams.size))
  138. # Rp = np.zeros((thetas.size, lams.size))
  139. # for tidx, theta in enumerate(thetas):
  140. # for lidx, lam in enumerate(lams):
  141. # d_x, n_x = make_nxdx(qx=qx, cthick=cthick, wavelen=lam, n_substrate=n_subs)
  142. # Rs[tidx, lidx] = 100 * coh_tmm(
  143. # "s", n_x, d_x, th_0=theta * degree, lam_vac=lam
  144. # )
  145. # Rp[tidx, lidx] = 100 * coh_tmm(
  146. # "p", n_x, d_x, th_0=theta * degree, lam_vac=lam
  147. # )
  148. # return Rs, Rp
  149. def tmm_eval_wbk(qx, d_min , d_max, inc_ang, lam, n_subs=1.52):
  150. """TODO: Docstring for tmm_eval.
  151. :qx: TODO
  152. :returns: TODO
  153. """
  154. degree = np.pi / 180
  155. d_x, n_x = make_nxdx(qx=qx, d_min=d_min, d_max=d_max, wavelen=lam, n_substrate=n_subs)
  156. Rs = 100 * coh_tmm("s", n_x, d_x, th_0=inc_ang * degree, lam_vac=lam)
  157. # Rp = 100*coh_tmm('p',n_x,d_x, th_0=inc_ang*degree,lam_vac=lam)
  158. return Rs
  159. def tmm_eval_wsweep(
  160. qx, inc_ang, lam_low=400, lam_high=800, lam_pts=256, n_subs=1.52 # in nm
  161. ):
  162. """TODO: Docstring for tmm_eval.
  163. :qx: TODO
  164. :lam_low: TODO
  165. lam_high: TODO
  166. :lam_pts: TODO
  167. :returns: TODO
  168. """
  169. degree = np.pi / 180
  170. # lam_inv = np.linspace(1/lam_low, 1/lam_high, num=lam_pts, endpoint=True)
  171. # lams = 1.0/lam_inv
  172. lams = np.linspace(lam_low, lam_high, endpoint=True, num=lam_pts)
  173. Rs = np.zeros(lams.size)
  174. # Rp = np.zeros(lams.size)
  175. for lidx, lam in enumerate(lams):
  176. d_x, n_x = make_nxdx(qx=qx, wavelen=lam, n_substrate=n_subs)
  177. Rs[lidx] = 100 * coh_tmm("s", n_x, d_x, th_0=inc_ang * degree, lam_vac=lam)
  178. # Rp[lidx] = 100*coh_tmm('p',n_x,d_x, th_0=inc_ang*degree,lam_vac=lam)
  179. return np.mean(Rs)
  180. # def tmm_wrapper2(arg):
  181. # args, kwargs = arg
  182. # return tmm_eval_wbk(*args, **kwargs)
  183. # def tmm_lam_parallel(
  184. # q_x, cthick, inc_ang, n_par=12, lam_low=400, lam_high=1200, lam_pts=100, **kwargs
  185. # ):
  186. # jobs = []
  187. # pool = mp.Pool(n_par)
  188. # lams = np.linspace(lam_low, lam_high, endpoint=True, num=lam_pts)
  189. # for lam in lams:
  190. # jobs.append((q_x, cthick, inc_ang, lam))
  191. # arg = [(j, kwargs) for j in jobs]
  192. # answ = np.array(pool.map(tmm_wrapper2, arg))
  193. # pool.close()
  194. # return answ[:, 0], answ[:, 1]
  195. # def tmm_wrapper(arg):
  196. # args, kwargs = arg
  197. # return tmm_eval_wsweep(*args, **kwargs)
  198. # def tmm_eval_parallel(q_x, cthick, n_ang=25, n_par=10, **kwargs):
  199. # jobs = []
  200. # pool = mp.Pool(n_par)
  201. # angs = np.linspace(90, 0, endpoint=True, num=n_ang)
  202. # for ang in angs:
  203. # jobs.append((q_x, cthick, ang))
  204. # arg = [(j, kwargs) for j in jobs]
  205. # answ = np.array(pool.map(tmm_wrapper, arg))
  206. # pool.close()
  207. # return answ[:, 0, :], answ[:, 1, :]