Ansys Learning Forum › Forums › Discuss Simulation › Photonics › Optimization script writing › Reply To: Optimization script writing
Dear Bailin,
Â
Thank you for reaching out to us!
Â
I am not exactly sure what you want to optimize and what your settings are (if for example you use 2D or 3D grating, if you use more than one input angle for the excitation, and so on).
Â
An optimization example of a 2D grating that the target is to get 0.2 transmission for s-polarized light if the wavelength is smaller than or equal to 500nm target wavelength and 0.5 transmission if the wavelength is greater than 500nm in a specific diffraction order (in this example it is the -1 diffraction order), could be the following:
Â
lambda_target=500e-9; # wavelength to be used as a condition
target_lower = 0.2; # diffraction efficiency for wavelengths smaller than lambda_target
target_upper = 0.5; # diffraction efficiency for wavelengths greater than lambda_target
Â
lambda=grating.lambda;
Ts=grating.Ts_grating; # result that we need to optimize
target_order = -1; # the diffraction order that we are interested in optimizing the power
Â
n = grating.n;
target_order_index = find(n, target_order);
Â
fom=0;
Â
for (i=1:length(lambda)) {
   if (lambda(i)<=lambda_target) {
       fom=fom+abs(Ts(i,1,target_order_index,1)-target_lower);
   } else {
       fom=fom+abs(Ts(i,1,target_order_index,1)-target_upper);
   }
}
Â
Â
where the result that has been used is the grating_orders result and the fom type is set to minimize:
Â
Please keep in mind that the above optimization script can also be used for simulations that run for multiple wavelengths (more than 1 frequency points in the excitation tab of RCWA). In this case the optimization will try to minimize the difference from the target efficiencies for all wavelengths, i.e. the sum of the differences for all wavelengths will be minimized.
Â
I hope this helps.
Â
Best regards,
Afroditi