/*
 * antireflective_coating_multilayer.java
 */

import com.comsol.model.*;
import com.comsol.model.util.*;

/** Model exported on May 15 2026, 09:58 by COMSOL 6.4.0.421. */
public class antireflective_coating_multilayer {

  public static Model run() {
    Model model = ModelUtil.create("Model");

//    From the File menu, choose New.
//    In the New window, click Model Wizard.
//    In the Model Wizard window, click 2D.
//    In the Select Physics tree, select Optics > Ray Optics > Geometrical Optics (gop).
//    Click Add.
//    Click Study.
//    In the Select Study tree, select Preset Studies for Selected Physics Interfaces > Ray Tracing.
//    Click Done.

    model.component().create("comp1", true);

    model.component("comp1").geom().create("geom1", 2);

    model.component("comp1").mesh().create("mesh1");

    model.component("comp1").physics().create("gop", "GeometricalOptics", "geom1");

    model.study().create("std1");
    model.study("std1").create("rtrac", "RayTracing");

//    In the Model Builder window, under Global Definitions, click Parameters 1.
//    In the Settings window for Parameters, locate the Parameters section.
//    In the table, enter the following settings:

    model.param().set("n_air", "1");
    model.param().descr("n_air", "Refractive index of air");
    model.param().set("n_glass", "1.5");
    model.param().descr("n_glass", "Refractive index of glass");
    model.param().set("n_CeF3", "1.63");
    model.param().descr("n_CeF3", "Refractive index of CeF3");
    model.param().set("n_MgF2", "1.38");
    model.param().descr("n_MgF2", "Refractive index of MgF2");
    model.param().set("n_ZrO2", "2.2");
    model.param().descr("n_ZrO2", "Refractive index of ZrO2");
    model.param().set("lam0", "550[nm]");
    model.param().descr("lam0", "Vacuum wavelength");

//    In the Geometry toolbar, click Square.

    model.component("comp1").geom("geom1").create("sq1", "Square");

//    In the Settings window for Square, click to expand the Layers section.
//    In the table, enter the following settings:

    model.component("comp1").geom("geom1").feature("sq1").setIndex("layer", 0.5, 0);

//    Click Build All Objects.

    model.component("comp1").geom("geom1").runPre("fin");
    model.component("comp1").geom("geom1").run();

//    In the Model Builder window, right-click Component 1 (comp1) > Materials and choose Blank Material.

    model.component("comp1").material().create("mat1", "Common");

//    In the Settings window for Material, locate the Material Contents section.
//    In the table, enter the following settings:

    model.component("comp1").material("mat1").propertyGroup()
         .create("RefractiveIndex", "RefractiveIndex", "Refractive_index");
    model.component("comp1").material("mat1").propertyGroup("RefractiveIndex").set("n", new String[]{"n_air"});

//    Right-click Materials and choose Blank Material.

    model.component("comp1").material().create("mat2", "Common");

//    Select Domain 1.

    model.component("comp1").material("mat2").selection().set(1);

//    In the Settings window for Material, locate the Material Contents section.
//    In the table, enter the following settings:

    model.component("comp1").material("mat2").propertyGroup()
         .create("RefractiveIndex", "RefractiveIndex", "Refractive_index");
    model.component("comp1").material("mat2").propertyGroup("RefractiveIndex").set("n", new String[]{"n_glass"});

//    In the Model Builder window, under Component 1 (comp1), click Geometrical Optics (gop).
//    In the Settings window for Geometrical Optics, locate the Intensity Computation section.
//    From the Intensity computation list, select Compute intensity and power.

    model.component("comp1").physics("gop").prop("IntensityComputation")
         .setIndex("IntensityComputation", "ComputeIntensityAndPower", 0);

//    Locate the Ray Release and Propagation section.
//    From the Wavelength distribution of released rays list, select Polychromatic, specify vacuum wavelength.

    model.component("comp1").physics("gop").prop("WavelengthDistribution")
         .setIndex("WavelengthDistribution", "PolychromaticWavelength", 0);

//    In the Maximum number of secondary rays text field, type 0.

    model.component("comp1").physics("gop").prop("MaximumSecondary").setIndex("MaximumSecondary", 0, 0);

//    Set up the quarter-quarter anti-reflective coating using two <l>Thin Dielectric Film</l> features.
//    It is useful to display the boundary normal in the <l>Graphics</l> window since this indicates the order in which the thin films are arranged. The arrow points in the direction away from the substrate, so the first layer that appears in the Model Builder is adjacent to the substrate and the last layer that appears is adjacent to the air domain.
//    In the Model Builder window, under Component 1 (comp1) > Geometrical Optics (gop), click Material Discontinuity 1.
//    In the Settings window for Material Discontinuity, locate the Advanced Settings section.
//    Select the Show boundary normal checkbox.

    model.component("comp1").physics("gop").feature("matd1").set("ShowBoundaryNormal", true);

//    Locate the Coatings section.
//    From the Thin dielectric films on boundary list, select Add layers to surface.

    model.component("comp1").physics("gop").feature("matd1")
         .set("ThinDielectricFilmsOnBoundary", "AddLayersToSurface");

//    Locate the Rays to Release section.
//    From the Release reflected rays list, select Never.

    model.component("comp1").physics("gop").feature("matd1").set("ReleaseReflectedRays", "Never");

//    . It is still possible to analyze the reflectance of the coating based on the intensity of the transmitted rays, even if no reflected rays are actually released at the boundary.
//    In the Physics toolbar, click Attributes and choose Thin Dielectric Film.

    model.component("comp1").physics("gop").feature("matd1").create("film1", "ThinDielectricFilm", 1);

//    The first layer (directly touching the glass substrate) is the CeF<sub>3</sub> layer with a refractive index of 1.63. The thickness is set to be a quarter of the wavelength in this material.
//    In the Settings window for Thin Dielectric Film, locate the Film Properties section.
//    In the \[n\] text field, type n_CeF3.

    model.component("comp1").physics("gop").feature("matd1").feature("film1").set("nf", "n_CeF3");

//    In the \[t\] text field, type lam0/(4*n_CeF3).

    model.component("comp1").physics("gop").feature("matd1").feature("film1").set("tf", "lam0/(4*n_CeF3)");

//    In the Model Builder window, click Material Discontinuity 1.
//    In the Physics toolbar, click Attributes and choose Thin Dielectric Film.

    model.component("comp1").physics("gop").feature("matd1").create("film2", "ThinDielectricFilm", 1);

//    The second layer (on top of layer 1) is the MgF<sub>2</sub> layer with a refractive index of 1.38. The thickness of this layer is also set to a quarter of the wavelength.
//    In the Settings window for Thin Dielectric Film, locate the Film Properties section.
//    In the \[n\] text field, type n_MgF2.

    model.component("comp1").physics("gop").feature("matd1").feature("film2").set("nf", "n_MgF2");

//    In the \[t\] text field, type lam0/(4*n_MgF2).

    model.component("comp1").physics("gop").feature("matd1").feature("film2").set("tf", "lam0/(4*n_MgF2)");

//    Next, set up the <l>Release From Grid</l> feature to release a number of rays of different wavelengths from 400 nm to 800 nm.
//    In the Physics toolbar, click Global and choose Release from Grid.

    model.component("comp1").physics("gop").create("relg1", "ReleaseGrid", -1);

//    In the Settings window for Release from Grid, locate the Initial Coordinates section.
//    In the \[q_{x,0}\] text field, type 0.5.

    model.component("comp1").physics("gop").feature("relg1").setIndex("x0", 0.5, 0);

//    In the \[q_{y,0}\] text field, type 1.

    model.component("comp1").physics("gop").feature("relg1").setIndex("x0", 1, 1);

//    Locate the Ray Direction Vector section.
//    Specify the \[\mathbf{L}_0\] vector as

    model.component("comp1").physics("gop").feature("relg1").set("L0", new int[]{0, -1, 0});

//    Locate the Vacuum Wavelength section.
//    From the Distribution function list, select List of values.

    model.component("comp1").physics("gop").feature("relg1").set("LDistributionFunction", "ListOfValues");

//    Click Range.
//    In the Range dialog, choose Number of values from the Entry method list.
//    In the Start text field, type 400[nm].
//    In the Stop text field, type 800[nm].
//    In the Number of values text field, type 100.
//    Click Replace.

    model.component("comp1").physics("gop").feature("relg1")
         .setIndex("lambda0vals", "range(400[nm],(800[nm]-(400[nm]))/99,800[nm])", 0);

//    Set up a <l>Ray Tracing</l> study step to compute the ray trajectories to a maximum optical path length of 1.1 m.
//    In the Model Builder window, under Study 1, click Step 1: Ray Tracing.
//    In the Settings window for Ray Tracing, locate the Study Settings section.
//    From the Time-step specification list, select Specify maximum path length.

    model.study("std1").feature("rtrac").set("timestepspec", "specifylength");

//    In the Lengths text field, type range(0,0.01,1.1).

    model.study("std1").feature("rtrac").set("llist", "range(0,0.01,1.1)");

//    In the Study toolbar, click Compute.

    model.study("std1").createAutoSequences("all");

    model.sol("sol1").runAll();

    model.result().dataset().create("ray1", "Ray");
    model.result().dataset("ray1").set("solution", "sol1");
    model.result().dataset("ray1").set("posdof", new String[]{"comp1.qx", "comp1.qy"});
    model.result().dataset("ray1").set("geom", "geom1");
    model.result().dataset("ray1").set("rgeom", "pgeom_gop");
    model.result().dataset("ray1").set("rgeomspec", "fromphysics");
    model.result().dataset("ray1").set("physicsinterface", "gop");
    model.result().create("pg1", "PlotGroup2D");
    model.result("pg1").set("data", "ray1");
    model.result("pg1").label("Ray Trajectories (gop)");
    model.result("pg1").create("rtrj1", "RayTrajectories");
    model.result("pg1").feature("rtrj1").set("linetype", "line");
    model.result("pg1").feature("rtrj1").create("col1", "Color");
    model.result("pg1").feature("rtrj1").feature("col1").set("expr", "gop.I");
    model.result("pg1").feature("rtrj1").create("filt1", "RayTrajectoriesFilter");
    model.result("pg1").run();

//    In the Results toolbar, click 1D Plot Group.

    model.result().create("pg2", "PlotGroup1D");
    model.result("pg2").run();

//    In the Settings window for 1D Plot Group, type Reflectance in the Label text field.

    model.result("pg2").label("Reflectance");

//    Locate the Data section.
//    From the Dataset list, select Ray 1.

    model.result("pg2").set("data", "ray1");

//    From the Time selection list, select Last.

    model.result("pg2").setIndex("looplevelinput", "last", 0);

//    Click to expand the Title section.
//    From the Title type list, select Manual.

    model.result("pg2").set("titletype", "manual");

//    In the Title text area, type Reflectance of Multilayer Films.

    model.result("pg2").set("title", "Reflectance of Multilayer Films");

//    Locate the Plot Settings section.
//    Select the x-axis label checkbox.

    model.result("pg2").set("xlabelactive", true);

//    In the associated text field, type Vacuum wavelength (nm).

    model.result("pg2").set("xlabel", "Vacuum wavelength (nm)");

//    Select the y-axis label checkbox.

    model.result("pg2").set("ylabelactive", true);

//    In the associated text field, type Reflectance (%).

    model.result("pg2").set("ylabel", "Reflectance (%)");

//    In the Reflectance toolbar, click More Plots and choose Ray.

    model.result("pg2").create("rtp1", "Ray1D");
    model.result("pg2").feature("rtp1").set("markerpos", "datapoints");
    model.result("pg2").feature("rtp1").set("linewidth", "preference");

//    Plot the percentage reflectance.
//    In the Settings window for Ray, locate the y-Axis Data section.
//    In the Expression text field, type 100*(gop.relg1.Q0-gop.Q)/gop.relg1.Q0.

    model.result("pg2").feature("rtp1").set("expr", "100*(gop.relg1.Q0-gop.Q)/gop.relg1.Q0");

//    Locate the x-Axis Data section.
//    From the Parameter list, select Expression.

    model.result("pg2").feature("rtp1").set("xdata", "expr");

//    In the Expression text field, type gop.lambda0.

    model.result("pg2").feature("rtp1").set("xdataexpr", "gop.lambda0");

//    From the Unit list, select nm.

    model.result("pg2").feature("rtp1").set("xdataunit", "nm");

//    Click to expand the Legends section.
//    Select the Show legends checkbox.

    model.result("pg2").feature("rtp1").set("legend", true);

//    From the Legends list, select Manual.

    model.result("pg2").feature("rtp1").set("legendmethod", "manual");

//    In the table, enter the following settings:

    model.result("pg2").feature("rtp1").setIndex("legends", "Quarter-Quarter", 0);

//    In the Reflectance toolbar, click Plot.

    model.result("pg2").run();

//    Next, to simulate a quarter-half-quarter layer, add another <l>Thin Dielectric Film</l> feature. The material of the middle layer is chosen to be Zirconium Oxide, ZrO<sub>2</sub>, with refractive index 2.2. Set the thickness to half the specified wavelength.
//    In the Model Builder window, under Component 1 (comp1) > Geometrical Optics (gop), click Material Discontinuity 1.
//    In the Physics toolbar, click Attributes and choose Thin Dielectric Film.

    model.component("comp1").physics("gop").feature("matd1").create("film3", "ThinDielectricFilm", 1);

//    This layer will sit between the other two thin layers so its node must be moved in the Model Builder.
//    Right-click Thin Dielectric Film 3 and choose Move Up.

    model.component("comp1").physics("gop").feature("matd1").feature().move("film3", 1);

//    In the Settings window for Thin Dielectric Film, locate the Film Properties section.
//    In the \[n\] text field, type n_ZrO2.

    model.component("comp1").physics("gop").feature("matd1").feature("film3").set("nf", "n_ZrO2");

//    In the \[t\] text field, type lam0/(2*n_ZrO2).

    model.component("comp1").physics("gop").feature("matd1").feature("film3").set("tf", "lam0/(2*n_ZrO2)");

//    Add another study so that the two films can be compared.
//    In the Home toolbar, click Add Study to open the Add Study window.
//    Find the Studies subsection.
//    In the Select Study tree, select Preset Studies for Selected Physics Interfaces > Ray Tracing.
//    Click Add Study in the window toolbar.

    model.study().create("std2");
    model.study("std2").create("rtrac", "RayTracing");

//    In the Home toolbar, click Add Study to close the Add Study window.
//    In the Settings window for Ray Tracing, locate the Study Settings section.
//    From the Time-step specification list, select Specify maximum path length.

    model.study("std2").feature("rtrac").set("timestepspec", "specifylength");

//    In the Lengths text field, type range(0,0.01,1.1).

    model.study("std2").feature("rtrac").set("llist", "range(0,0.01,1.1)");

//    From the Stop condition list, select No active rays remaining.

    model.study("std2").feature("rtrac").set("raystopcond", "noactive");

//    In the Study toolbar, click Compute.

    model.study("std2").createAutoSequences("all");

    model.sol("sol2").runAll();

    model.result().dataset().create("ray2", "Ray");
    model.result().dataset("ray2").set("solution", "sol2");
    model.result().dataset("ray2").set("posdof", new String[]{"comp1.qx", "comp1.qy"});
    model.result().dataset("ray2").set("geom", "geom1");
    model.result().dataset("ray2").set("rgeom", "pgeom_gop");
    model.result().dataset("ray2").set("rgeomspec", "fromphysics");
    model.result().dataset("ray2").set("physicsinterface", "gop");
    model.result().create("pg3", "PlotGroup2D");
    model.result("pg3").set("data", "ray2");
    model.result("pg3").label("Ray Trajectories (gop) 1");
    model.result("pg3").create("rtrj1", "RayTrajectories");
    model.result("pg3").feature("rtrj1").set("linetype", "line");
    model.result("pg3").feature("rtrj1").create("col1", "Color");
    model.result("pg3").feature("rtrj1").feature("col1").set("expr", "gop.I");
    model.result("pg3").feature("rtrj1").create("filt1", "RayTrajectoriesFilter");
    model.result("pg3").run();

//    The newly added <l>Thin Dielectric Film</l> feature is intended only for <l>Study 2</l> and thus it is better to disable it for <l>Study 1</l>. This can be achieved by modifying the model configurations for the study step.
//    In the Model Builder window, under Study 1, click Step 1: Ray Tracing.
//    In the Settings window for Ray Tracing, locate the Physics and Variables Selection section.
//    Select the Modify model configuration for study step checkbox.

    model.study("std1").feature("rtrac").set("useadvanceddisable", true);

//    In the tree, select Component 1 (comp1) > Geometrical Optics (gop) > Material Discontinuity 1 > Thin Dielectric Film 3.
//    Click Disable.

    model.study("std1").feature("rtrac").set("disabledphysics", new String[]{"gop/matd1/film3"});

    model.result("pg2").run();

//    In the Model Builder window, right-click Results > Reflectance > Ray 1 and choose Duplicate.

    model.result("pg2").feature().duplicate("rtp2", "rtp1");
    model.result("pg2").run();

//    In the Settings window for Ray, locate the Data section.
//    From the Dataset list, select Ray 2.

    model.result("pg2").feature("rtp2").set("data", "ray2");

//    From the Time selection list, select Last.

    model.result("pg2").feature("rtp2").setIndex("looplevelinput", "last", 0);

//    Locate the Legends section.
//    In the table, enter the following settings:

    model.result("pg2").feature("rtp2").setIndex("legends", "Quarter-Half-Quarter", 0);

//    In the Reflectance toolbar, click Plot.

    model.result("pg2").run();

    model.title("Antireflective Coating with Multiple Layers");

    model
         .description("Antireflective coatings are frequently used in optical systems to reduce the amount of stray light produced when a beam of light crosses from one medium into another medium with a different refractive index. The simplest example of an antireflective coating is a quarter-wavelength layer, in which the thickness of a monolayer dielectric film is adjusted to be one fourth of the optical wavelength. Although a single-layer coating can reduce the reflectance to zero for light at a specific wavelength and angle of incidence, the reflectance can be substantially larger for other wavelengths. One possible solution is to use a multilayer coating that gives consistently low reflectance over a wider spectral band.\n\nIn this tutorial, light crosses a boundary between two media with different refractive indices at normal incidence. The reflectance of two different multilayer coatings is compared over a wide spectral range: a quarter-quarter coating (two layers), and a quarter-half-quarter coating (three layers). The quarter-half-quarter coating is shown to have more consistently low reflectance across most of the visible spectrum.");

    return model;
  }

  public static void main(String[] args) {
    run();
  }

}
