How to use n_value_list method in avocado

Best Python code snippet using avocado_python

sympy_transfer_matrix.py

Source:sympy_transfer_matrix.py Github

copy

Full Screen

1# -*- coding: utf-8 -*-2"""3Created on Tue May 18 16:32:48 20214@author: jo28dohe5this package is strongly influenced by the python tmm package. 6for better performance repeatedly evaluated functions can be created with a lot of math already solved. 7"""8### testing around with sp and transfer matrices9import numpy as np10import matplotlib.pyplot as plt11from matplotlib import cm 12import sympy as sp13from sympy.utilities.lambdify import lambdify14sp.init_printing()15try:16 import refractive_index as ri17except:18 from LAP_eval import refractive_index as ri19n=sp.Symbol('n')20l=sp.Symbol('l')21lamb=sp.Symbol('lamb')22n1=sp.Symbol('n1')23n2=sp.Symbol('n2')24n3=sp.Symbol('n3')25n_SiC=2.6 ### bei 800nm https://refractiveindex.info/?shelf=main&book=SiC&page=Wang-4H-o26n_SiO2=1.45 ##https://refractiveindex.info/?shelf=main&book=SiO2&page=Malitson27#n_SiC=ri.n_from_string('SiC')28n_Au=ri.n_from_string('Au') 29n_Al=ri.n_from_string('Al') 30n_MoS2_ML=ri.n_from_string('MoS2').get_n(600e-9)31n_graphene=ri.n_from_string('graphene')32d_Al=3.5e-933d_MoS2=0.615e-934d_graphene=0.35e-935d_SiO2=135e-936d_vac=2300e-937d_Au=50e-938def ref_E(n_1,n_2):39 return((n_1-n_2)/(n_1+n_2))40 41def trans_E(n_1,n_2):42 return(2*n_1/(n_1+n_2))43def P__(n,l,lamb): 44 return sp.simplify(sp.Matrix([[sp.exp(-1j*2*sp.pi*l*n/lamb),0],[0,sp.exp(+1j*2*sp.pi*l*n/lamb)]]))45def D__(n_1,n_2):46 return sp.simplify(1/trans_E(n_1,n_2)*sp.Matrix([[1,ref_E(n_1,n_2)],[ref_E(n_1,n_2),1]]))47def t_from_M__(M): 48 t=1/M[0,0]49 return(t)50 51def r_from_M__(M):52 r=M[1,0]/M[0,0]53 return(r)54def find_in_structure(d_list, distance):55 """56 d_list is list of thicknesses of layers, all of which are finite.57 distance is the distance from the front of the whole multilayer structure58 (i.e., from the start of layer 0.)59 Function returns [layer,z], where:60 * layer is what number layer you're at.61 * z is the distance into that layer.62 For large distance, layer = len(d_list), even though d_list[layer] doesn't63 exist in this case. For negative distance, return [-1, distance]64 """65 if sum(d_list) == np.inf:66 raise ValueError('This function expects finite arguments')67 if distance < 0:68 return [-1, distance]69 layer = 070 while (layer < len(d_list)) and (distance >= d_list[layer]):71 distance -= d_list[layer]72 layer += 173 return [layer, distance]74def find_in_structure_with_inf(d_list, distance):75 """76 d_list is list of thicknesses of layers [inf, blah, blah, ..., blah, inf]77 distance is the distance from the front of the whole multilayer structure78 (i.e., from the start of layer 1.)79 Function returns [layer,z], where:80 * layer is what number layer you're at,81 * z is the distance into that layer.82 For distance < 0, returns [0, distance]. So the first interface can be described as83 either [0,0] or [1,0].84 """85 86 print(d_list)87 print(distance)88 if distance < 0:89 return [0, distance]90 [layer, distance_in_layer] = find_in_structure(d_list[1:-1], distance)91 return [layer+1, distance_in_layer]92class sympy_tmm():93 94 """95 an object which corresponds to a layered structure and contains functions for optical parameters96 like Reflectance, Transmission and depth-dependant E-Field97 """98 def __init__(self,n_value_list=[n_SiC,n_graphene,1,n_Au,n_SiC],99 d_value_list=[np.inf,d_graphene,d_vac,d_Au, np.inf],100 d_parameter_list=[2],101 n_parameter_list=[1,3],102 lamb_list=np.linspace(530e-9,1000e-9,200)):103 104 assert len(n_value_list)== len(d_value_list), 'n_value_list and d_value_list must have same lenght!'105 assert len(n_value_list)>1, 'n_value_list must have more than 1 entry'106 self.lamb_list=lamb_list107 self.n_value_list=n_value_list108 self.d_value_list=d_value_list109 self.d_parameter_list=d_parameter_list110 self.n_parameter_list=n_parameter_list111 112 113 self.n_list=[]114 self.d_list=[]115 self.lamb=sp.Symbol('lamb')116 self.all_parameter_list=[self.lamb]117 118 119 for i in range(len(n_value_list)):120 if i in n_parameter_list:121 self.n_list.append(sp.Symbol('n'+str(i)))122 self.all_parameter_list.append(self.n_list[-1])123 else:124 self.n_list.append(n_value_list[i])125 if i in d_parameter_list:126 self.d_list.append(sp.Symbol('d'+str(i)))127 self.all_parameter_list.append(self.d_list[-1])128 else:129 self.d_list.append(d_value_list[i])130 131 132 133 ## initialize Transfer Matrix at last boundary:134 self.M__=D__(self.n_list[-2],self.n_list[-1])135 ## save list that contains Transfer matrices of all boundaries from last to first (for E-field calculation)136 self.M__list=[self.M__]137 138 for i in range(len(self.n_value_list)-2):139 self.M__=D__(self.n_list[-3-i],self.n_list[-2-i])*P__(self.n_list[-2-i],self.d_list[-2-i],lamb)*self.M__140 self.M__list.append(self.M__)141 print('Matrix_Multiplication_done')142 143 144 145 self.r=r_from_M__(self.M__)146 self.t=t_from_M__(self.M__)147 self.R=sp.Abs(self.r)**2148 self.T=sp.Abs(self.t)**2*self.n_value_list[-1].real/self.n_value_list[0].real149 150 ## make list of arguments which should be plugged in later151 self.lambdify_args=[self.lamb]152 for index in self.n_parameter_list:153 self.lambdify_args.append(self.n_list[index])154 for index in self.d_parameter_list:155 self.lambdify_args.append(self.d_list[index])156 157 ## lambdify functions for numpy-like evaluation158 self.lam_R=lambdify(self.lambdify_args,self.R) 159 self.lam_T=lambdify(self.lambdify_args,self.T)160 161 162 def plot_R_T(self,lamb_list,d_vac=d_vac,n_lists=()):163 params=*n_lists,d_vac164 print(params)165 plt.plot(lamb_list,self.lam_R(lamb_list,*params),label='R')166 plt.plot(lamb_list,self.lam_T(lamb_list,*params),label='T')167 plt.plot(lamb_list,self.lam_R(lamb_list,*params)+self.lam_T(lamb_list,*params),label='Sum')168 plt.grid(True)169 plt.legend()170 plt.show()171 172 def plot_R_d(self,lamb_list,d_min,d_max,n_lists,n_d=200):173 d_list=np.linspace(d_min,d_max,n_d)174 plt.figure()175 for i in range(len(d_list)):176 plt.scatter(lamb_list,np.ones(len(lamb_list))*d_list[i],c=self.lam_R(lamb_list,*n_lists,d_list[i]),177 vmin=0,vmax=1,cmap=cm.nipy_spectral)178 plt.xlim(min(lamb_list),max(lamb_list))179 plt.ylim(min(d_list),max(d_list))180 plt.show()181 182 183 def R_fit_func(self,d,lamb_list,n_lists):184 R=self.lam_R(lamb_list,*n_lists,d)185 return (R)186 187 def make_lam_E(self,d_E=0):188 189 i_layer,d_extra=find_in_structure_with_inf(self.d_value_list, d_E)190 self.E_r_l_boundary_list=[sp.Matrix([self.t,0])]191 for i in range(len(self.M__list)):192 self.E_r_l_boundary_list.append(self.M__list[i]*self.E_r_l_boundary_list[0])193 194 if self.d_list[i_layer]==np.inf:195 d_prop=-d_extra196 else:197 d_prop=self.d_list[i_layer]-d_extra198 E_r_l_pos=P__(self.n_list[i_layer],d_prop,self.lamb)*self.E_r_l_boundary_list[-1-i_layer] ## propagation from right side of i_layer_th material to position199 200 self.lam_E_r=lambdify(self.lambdify_args,E_r_l_pos[0])201 self.lam_E_l=lambdify(self.lambdify_args,E_r_l_pos[1])202 self.lam_E_ges=lambdify(self.lambdify_args,E_r_l_pos[0]+E_r_l_pos[1])203 print('object now has E_r,E_l,E_ges functions')204 return(self.lam_E_ges)205 206 207 def plot_spectrum_modification(self,lamb_list,d_E=0,n_lists=(),d_vac_min=0,d_vac_max=2300e-9):208 209 self.make_lam_E(d_E=d_E)210 plt.figure()211 d_list=np.linspace(d_vac_min,d_vac_max,200)212 for i in range(len(d_list)):213 I_laser=np.abs(self.lam_E_ges(532e-9,*n_lists,d_list[i]))**2*np.ones(len(lamb_list))214 I_emission=np.abs(self.lam_E_ges(lamb_list,*n_lists,d_list[i]))**2 215 plt.scatter(1e9*lamb_list,1e9*d_list[i]*np.ones(len(lamb_list)),216 c=I_laser*I_emission,217 vmin=-0.5,vmax=4,cmap=cm.nipy_spectral)218 219 plt.xlim(1e9*min(lamb_list),1e9*max(lamb_list))220 plt.ylim(1e9*min(d_list),1e9*max(d_list))221 plt.xlabel('wavelength in nm ')222 plt.ylabel('d in nm')223 plt.title('optical enhancement factor (Photoluminescence)')224 plt.show()225 226 def plot_spectrum_modification_to_ax(self,ax,d=0,lamb_list=[],n_lists=(),d_vac_min=0,d_vac_max=2300e-9):227 228 self.make_lam_E(d=d)229 230 d_list=np.linspace(d_vac_min,d_vac_max,200)231 for i in range(len(d_list)):232 I_laser=np.abs(self.lam_E_ges(532e-9,*n_lists,d_list[i]))**2*np.ones(len(lamb_list))233 I_emission=np.abs(self.lam_E_ges(lamb_list,*n_lists,d_list[i]))**2 234 ax.scatter(1e9*lamb_list,1e9*d_list[i]*np.ones(len(lamb_list)),235 c=I_laser*I_emission,236 vmin=0,vmax=5,cmap=cm.nipy_spectral)237 238 ax.set_xlim(1e9*min(lamb_list),1e9*max(lamb_list))239 ax.set_ylim(1e9*min(d_list),1e9*max(d_list))240 ax.set_xlabel(r'$\lambda$ in nm')241 ax.set_ylabel(r'$d$ in nm')242 ax.set_title('simulated optical enhancement factor')243 244 def plot_E_of_lamb(self,d_E=0,lamb_list=[],n_lists=(),d_vac=d_vac):245 246 247 self.make_lam_E(d_E=d_E)248 print('d_value_list: ',self.d_value_list)249 I_emission=np.abs(self.lam_E_ges(lamb_list,*n_lists,d_vac))**2250 plt.plot(1e9*lamb_list,I_emission)251 plt.xlim(1e9*min(lamb_list),1e9*max(lamb_list))252 plt.show()253 254 def plot_d_dep_E(self,d_vac=d_vac,lamb=600e-9):255 E_list=[]256 d_list=np.linspace(-100e-9,100e-9+np.sum(self.d_value_list[1:-1]))257 for d in d_list:258 self.make_lam_E(d=d)259 E_list.append(np.abs(self.lam_E_ges(lamb,n_Al.get_n(lamb),n_Au.get_n(lamb),d_vac)))260 plt.plot(d_list,E_list)261 plt.show()262 263 def E_of_d_vac(self,d_vac,lamb=532e-9):264 return(self.lam_E_ges(lamb,n_graphene.get_n(lamb),n_Au.get_n(lamb),d_vac))265 266 267 def plot_E_of_d_vac(self,lamb=532e-9):268 plt.figure()269 d_list=np.linspace(0,2300e-9,300)270 plt.plot(np.abs(self.E_of_d_vac(d_list))**2,d_list)271 plt.show()272if __name__=='__main__':273 example_tmm=sympy_tmm()274 lamb_list=np.linspace(530e-9,950e-9,200)275 n_Au_list=n_Au.get_n(lamb_list)276 n_graphene_list=n_graphene.get_n(lamb_list)277 example_tmm.plot_R_T(lamb_list,d_vac=1000e-9,n_lists=(n_graphene_list,n_Au_list))278 example_tmm.plot_E_of_lamb(lamb_list=lamb_list,d_vac=900e-9,n_lists=(n_graphene_list,n_Au_list))279 example_tmm.make_lam_E()280 example_tmm.plot_E_of_d_vac()281 example_tmm.plot_spectrum_modification(lamb_list,n_lists=(n_graphene_list,n_Au_list))282 #example_tmm.plot_E(d=d_SiO2+d_Al)283 #example_tmm.plot_R_T(600e-9,900e-9)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run avocado automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful