/*
 * sagnac_interferometer.java
 */

import com.comsol.model.*;
import com.comsol.model.util.*;

/** Model exported on May 15 2026, 09:54 by COMSOL 6.4.0.421. */
public class sagnac_interferometer {

  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");

//    Load the global parameters for the interferometer from a text file.
//    In the Model Builder window, under Global Definitions, click Parameters 1.
//    In the Settings window for Parameters, locate the Parameters section.
//    Click Load from File.
//    Browse to the model's Application Library folder and double-click the file sagnac_interferometer_parameters.txt.
//    To import content from file, use:
//    model.param().loadFile("FILENAME");
    model.param().set("Omega", "10[deg/h]", "Interferometer angular velocity");
    model.param().set("lam0", "632.8[nm]", "Vacuum wavelength");
    model.param().set("R", "10[cm]", "Ring radius");
    model.param().set("th", "3[cm]", "Out-of-plane thickness");
    model.param().set("A", "0.5*(1.5*R)*(R*sqrt(3))", "Triangle area");
    model.param().set("P", "3*(R*sqrt(3))", "Triangle perimeter");
    model.param().set("phi0", "30[deg]", "Initial ray angle");
    model.param().set("xc", "R", "Center of rotation, x-coordinate");
    model.param().set("yc", "0", "Center of rotation, y-coordinate");
    model.param().set("wm", "3[cm]", "Mirror width");
    model.param().set("hm", "2[mm]", "Mirror height");
    model.param().set("xm1", "xc+(R+hm/2)*sin(phi0)", "Lower right mirror center, x-coordinate");
    model.param().set("ym1", "yc-(R+hm/2)*cos(phi0)", "Lower right mirror center, y-coordinate");
    model.param().set("xm2", "xc+(R+hm/2)*sin(phi0)", "Upper right mirror center, x-coordinate");
    model.param().set("ym2", "yc+(R+hm/2)*cos(phi0)", "Upper right mirror center, y-coordinate");
    model.param().set("ws", "3[cm]", "Beam splitter width");
    model.param().set("hs", "2[mm]", "Beam splitter height");
    model.param().set("L0x", "cos(phi0)", "Initial ray direction, x-component");
    model.param().set("L0y", "sin(phi0)", "Initial ray direction, y-component");
    model.param().set("q0x", "-5[cm]*L0x", "Ray release, x-coordinate");
    model.param().set("q0y", "-5[cm]*L0y", "Ray release, y-coordinate");
    model.param().set("Lo", "3[cm]", "Distance to obstruction");
    model.param().set("wo", "3[cm]", "Obstruction width");
    model.param().set("ho", "2[mm]", "Obstruction height");
    model.param().set("xo", "-(Lo+ho/2)*L0x", "Center of obstruction, x-coordinate");
    model.param().set("yo", "(Lo+ho/2)*L0y", "Center of obstruction, y-coordinate");
    model.param().set("dphi", "8*pi*A/(c_const*lam0)*Omega", "Expected phase shift");
    model.param().set("zf", "4*A/(c_const*lam0)*Omega", "Expected fringe shift");
    model.param().set("dt_exp", "4*A*Omega/c_const^2", "Expected transit time difference");
    model.param().set("dL_exp", "dt_exp*c_const", "Expected path difference");
    model.param().set("nu", "c_const/lam0", "Frequency");
    model.param().set("dnu_exp", "4*A*Omega/(lam0*P)", "Expected beat frequency");
    model.param().set("SF_exp", "4*A/(lam0*P)", "Expected scale factor");
    model.param().set("Omega_t", "P*eps*c_const/(4*A)", "Threshold for cancellation error");

//    In the Model Builder window, under Component 1 (comp1), click Geometry 1.
//    In the Settings window for Geometry, locate the Units section.
//    From the Length unit list, select cm.

    model.component("comp1").geom("geom1").lengthUnit("cm");

//    . This is more convenient than the SI unit, given the dimensions of the geometry.
//    The interferometer geometry is a triangular arrangement of two mirrors and a beam splitter. The geometry also includes an obstruction to absorb the outgoing rays.
//    In the Geometry toolbar, click Rectangle.

    model.component("comp1").geom("geom1").create("r1", "Rectangle");

//    In the Settings window for Rectangle, type Bottom-Right Mirror in the Label text field.

    model.component("comp1").geom("geom1").feature("r1").label("Bottom-Right Mirror");

//    Locate the Size and Shape section.
//    In the Width text field, type wm.

    model.component("comp1").geom("geom1").feature("r1").set("size", new String[]{"wm", "1"});

//    In the Height text field, type hm.

    model.component("comp1").geom("geom1").feature("r1").set("size", new String[]{"wm", "hm"});

//    Locate the Position section.
//    From the Base list, select Center.

    model.component("comp1").geom("geom1").feature("r1").set("base", "center");

//    In the x text field, type xm1.

    model.component("comp1").geom("geom1").feature("r1").set("pos", new String[]{"xm1", "0"});

//    In the y text field, type ym1.

    model.component("comp1").geom("geom1").feature("r1").set("pos", new String[]{"xm1", "ym1"});

//    Locate the Rotation Angle section.
//    In the Rotation text field, type 30.

    model.component("comp1").geom("geom1").feature("r1").set("rot", 30);

//    In the Geometry toolbar, click Rectangle.

    model.component("comp1").geom("geom1").run("r1");
    model.component("comp1").geom("geom1").create("r2", "Rectangle");

//    In the Settings window for Rectangle, type Top-Right Mirror in the Label text field.

    model.component("comp1").geom("geom1").feature("r2").label("Top-Right Mirror");

//    Locate the Size and Shape section.
//    In the Width text field, type wm.

    model.component("comp1").geom("geom1").feature("r2").set("size", new String[]{"wm", "1"});

//    In the Height text field, type hm.

    model.component("comp1").geom("geom1").feature("r2").set("size", new String[]{"wm", "hm"});

//    Locate the Position section.
//    From the Base list, select Center.

    model.component("comp1").geom("geom1").feature("r2").set("base", "center");

//    In the x text field, type xm2.

    model.component("comp1").geom("geom1").feature("r2").set("pos", new String[]{"xm2", "0"});

//    In the y text field, type ym2.

    model.component("comp1").geom("geom1").feature("r2").set("pos", new String[]{"xm2", "ym2"});

//    Locate the Rotation Angle section.
//    In the Rotation text field, type 150.

    model.component("comp1").geom("geom1").feature("r2").set("rot", 150);

//    In the Geometry toolbar, click Rectangle.

    model.component("comp1").geom("geom1").run("r2");
    model.component("comp1").geom("geom1").create("r3", "Rectangle");

//    In the Settings window for Rectangle, type Beam Splitter in the Label text field.

    model.component("comp1").geom("geom1").feature("r3").label("Beam Splitter");

//    Locate the Size and Shape section.
//    In the Width text field, type ws.

    model.component("comp1").geom("geom1").feature("r3").set("size", new String[]{"ws", "1"});

//    In the Height text field, type hs.

    model.component("comp1").geom("geom1").feature("r3").set("size", new String[]{"ws", "hs"});

//    Locate the Position section.
//    From the Base list, select Center.

    model.component("comp1").geom("geom1").feature("r3").set("base", "center");

//    Click to expand the Layers section.
//    In the table, enter the following settings:

    model.component("comp1").geom("geom1").feature("r3").setIndex("layer", "hs/2", 0);

//    The interior boundary splits the incoming ray into reflected and refracted rays. The outer surfaces of the glass will be assigned antireflective coatings.
//    In the Geometry toolbar, click Rectangle.

    model.component("comp1").geom("geom1").run("r3");
    model.component("comp1").geom("geom1").create("r4", "Rectangle");

//    In the Settings window for Rectangle, type Obstruction in the Label text field.

    model.component("comp1").geom("geom1").feature("r4").label("Obstruction");

//    Locate the Size and Shape section.
//    In the Width text field, type wo.

    model.component("comp1").geom("geom1").feature("r4").set("size", new String[]{"wo", "1"});

//    In the Height text field, type ho.

    model.component("comp1").geom("geom1").feature("r4").set("size", new String[]{"wo", "ho"});

//    Locate the Position section.
//    From the Base list, select Center.

    model.component("comp1").geom("geom1").feature("r4").set("base", "center");

//    In the x text field, type xo.

    model.component("comp1").geom("geom1").feature("r4").set("pos", new String[]{"xo", "0"});

//    In the y text field, type yo.

    model.component("comp1").geom("geom1").feature("r4").set("pos", new String[]{"xo", "yo"});

//    Locate the Rotation Angle section.
//    In the Rotation text field, type 60.

    model.component("comp1").geom("geom1").feature("r4").set("rot", 60);

//    Click Build All Objects.

    model.component("comp1").geom("geom1").runPre("fin");

//    Click the Zoom Extents button in the Graphics toolbar.
//    In the Physics toolbar, click Moving Mesh and choose Rotating Domain.

    model.component("comp1").common().create("rot1", "RotatingDomain");
    model.component("comp1").common("rot1").selection().all();

    model.component("comp1").geom("geom1").run();

//    In the Settings window for Rotating Domain, locate the Rotation section.
//    From the Rotation type list, select Specified rotational velocity.

    model.component("comp1").common("rot1").set("rotationType", "rotationalVelocity");

//    In the \[\omega\] text field, type Omega.

    model.component("comp1").common("rot1").set("angularVelocity", "Omega");

//    Locate the Axis section.
//    Specify the \[\mathbf{r}_{\textrm{ax}}\] vector as

    model.component("comp1").common("rot1").set("rotationAxisBasePoint", new String[]{"xc", "yc", "0"});

//    Load some local variable definitions. These will be used later to interpret the results.
//    In the Model Builder window, right-click Component 1 (comp1) > Definitions and choose Variables.

    model.component("comp1").variable().create("var1");

//    Load the variable definitions from a text file.
//    In the Settings window for Variables, locate the Variables section.
//    Click Load from File.
//    Browse to the model's Application Library folder and double-click the file sagnac_interferometer_variables.txt.
//    To import content from file, use:
//    model.component("comp1").variable("var1").loadFile("FILENAME");
    model.component("comp1").variable("var1")
         .set("L1", "gop.gopop1(if(gop.pidx==1,gop.L,0))", "Optical path length, first ray");
    model.component("comp1").variable("var1")
         .set("L4", "gop.gopop1(if(gop.pidx==4,gop.L,0))", "Optical path length, fourth ray");
    model.component("comp1").variable("var1").set("dL", "L4-L1", "Optical path difference");
    model.component("comp1").variable("var1").set("dnu", "dL/P*nu", "Beat frequency");
    model.component("comp1").variable("var1").set("SF", "dnu/Omega", "Scale factor");

//    In the Model Builder window, under Component 1 (comp1), click Geometrical Optics (gop).
//    In the Settings window for Geometrical Optics, locate the Ray Release and Propagation section.
//    In the Maximum number of secondary rays text field, type 3.

    model.component("comp1").physics("gop").prop("MaximumSecondary").setIndex("MaximumSecondary", 3, 0);

//    . The Geometrical Optics interface will apply deterministic ray splitting at the beam splitter boundary. A total of three secondary rays will split off from the released ray.
//    Locate the Intensity Computation section.
//    From the Intensity computation list, select Compute intensity.

    model.component("comp1").physics("gop").prop("IntensityComputation")
         .setIndex("IntensityComputation", "ComputeIntensity", 0);

//    Locate the Additional Variables section.
//    Select the Compute optical path length checkbox.

    model.component("comp1").physics("gop").prop("ComputeOpticalPathLength")
         .setIndex("ComputeOpticalPathLength", 1, 0);

//    . The optical path difference between two of the rays will be used to compute the beat frequency of the output.
//    In the Model Builder window, under Component 1 (comp1) > Geometrical Optics (gop), click Ray Properties 1.
//    In the Settings window for Ray Properties, locate the Ray Properties section.
//    In the \[\lambda_0\] text field, type lam0.

    model.component("comp1").physics("gop").feature("op1").set("lambda0", "lam0");

//    Define some boundary conditions for the beam splitter, mirrors, and obstruction.
//    In the Physics toolbar, click Boundaries and choose Material Discontinuity.

    model.component("comp1").physics("gop").create("matd2", "MaterialDiscontinuity", 1);

//    Select Boundaries 6, 9.

    model.component("comp1").physics("gop").feature("matd2").selection().set(6, 9);

//    In the Settings window for Material Discontinuity, locate the Rays to Release section.
//    From the Release reflected rays list, select Never.

    model.component("comp1").physics("gop").feature("matd2").set("ReleaseReflectedRays", "Never");

//    In the Label text field, type ARC.

    model.component("comp1").physics("gop").feature("matd2").label("ARC");

//    In the Physics toolbar, click Boundaries and choose Material Discontinuity.

    model.component("comp1").physics("gop").create("matd3", "MaterialDiscontinuity", 1);

//    Select Boundary 8.

    model.component("comp1").physics("gop").feature("matd3").selection().set(8);

//    In the Settings window for Material Discontinuity, type Beam Splitter in the Label text field.

    model.component("comp1").physics("gop").feature("matd3").label("Beam Splitter");

//    Locate the Coatings section.
//    From the Thin dielectric films on boundary list, select Specify reflectance.

    model.component("comp1").physics("gop").feature("matd3")
         .set("ThinDielectricFilmsOnBoundary", "SpecifyReflectance");

//    In the \[R\] text field, type 0.5.

    model.component("comp1").physics("gop").feature("matd3").set("Rf", 0.5);

//    In the Physics toolbar, click Boundaries and choose Mirror.

    model.component("comp1").physics("gop").create("mir1", "Mirror", 1);

//    Select Boundaries 13, 15.

    model.component("comp1").physics("gop").feature("mir1").selection().set(13, 15);

//    In the Settings window for Mirror, type Mirrors in the Label text field.

    model.component("comp1").physics("gop").feature("mir1").label("Mirrors");

//    In the Physics toolbar, click Boundaries and choose Wall.

    model.component("comp1").physics("gop").create("wall1", "Wall", 1);

//    Select Boundary 3.

    model.component("comp1").physics("gop").feature("wall1").selection().set(3);

//    In the Settings window for Wall, type Obstruction in the Label text field.

    model.component("comp1").physics("gop").feature("wall1").label("Obstruction");

//    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 q0x.

    model.component("comp1").physics("gop").feature("relg1").setIndex("x0", "q0x", 0);

//    In the \[q_{y,0}\] text field, type q0y.

    model.component("comp1").physics("gop").feature("relg1").setIndex("x0", "q0y", 1);

//    Locate the Ray Direction Vector section.
//    Specify the \[\mathbf{L}_0\] vector as

    model.component("comp1").physics("gop").feature("relg1").set("L0", new String[]{"L0x", "L0y", "0"});

//    Use the <l>Ray Termination</l> feature to delete any rays that escape from the geometry.
//    In the Physics toolbar, click Global and choose Ray Termination.

    model.component("comp1").physics("gop").create("rt1", "RayTermination", -1);

//    In the Settings window for Ray Termination, locate the Termination Criteria section.
//    From the Spatial extents of ray propagation list, select Bounding box, from geometry.

    model.component("comp1").physics("gop").feature("rt1")
         .set("SpatialExtentsOfRayPropagation", "BoundingBoxFromGeometry");

//    Specify the refractive index in the domains. The surroundings are treated as a vacuum with <eqv>n=1</eqv>.
//    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[]{"1.5"});

//    The study will include a <l>Parametric Sweep</l> over different values of the angular velocity.
//    In the Study toolbar, click Parametric Sweep.

    model.study("std1").create("param", "Parametric");

//    In the Settings window for Parametric Sweep, locate the Study Settings section.
//    Click Add.

    model.study("std1").feature("param").setIndex("pname", "Omega", 0);
    model.study("std1").feature("param").setIndex("plistarr", "", 0);
    model.study("std1").feature("param").setIndex("punit", "rad/s", 0);
    model.study("std1").feature("param").setIndex("pname", "Omega", 0);
    model.study("std1").feature("param").setIndex("plistarr", "", 0);
    model.study("std1").feature("param").setIndex("punit", "rad/s", 0);

//    In the table, enter the following settings:

    model.study("std1").feature("param").setIndex("plistarr", "10^{range(0,0.2,5)}", 0);
    model.study("std1").feature("param").setIndex("punit", "deg/h", 0);

//    In the Model Builder window, 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");

//    From the Length unit list, select cm.

    model.study("std1").feature("rtrac").set("lunit", "cm");

//    In the Lengths text field, type 0 1.5*P.

    model.study("std1").feature("rtrac").set("llist", "0 1.5*P");

//    . Since <eqv>P</eqv> is the perimeter of the triangle, this is a sufficiently long optical path length for the rays to reach the obstruction.
//    In the Study toolbar, click Compute.

    model.study("std1").createAutoSequences("all");

    model.sol().create("sol2");
    model.sol("sol2").study("std1");
    model.sol("sol2").label("Parametric Solutions 1");

    model.batch("p1").feature("so1").set("psol", "sol2");
    model.batch("p1").run("compute");

    model.result().dataset().create("ray1", "Ray");
    model.result().dataset("ray1").set("solution", "sol2");
    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").setIndex("looplevel", 26, 1);
    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();

//    The default plot shows the ray propagation through the interferometer. The default color expression indicates the optical path length.
//    In the Model Builder window, expand the Ray Trajectories (gop) node.

    model.result("pg1").run();

//    In the Model Builder window, expand the Results > Ray Trajectories (gop) > Ray Trajectories 1 node, then click Color Expression 1.
//    In the Settings window for Color Expression, click Replace Expression in the upper-right corner of the Expression section.
//    From the menu, choose Component 1 (comp1) > Geometrical Optics > Ray statistics > gop.pidx - Ray index - 1.

    model.result("pg1").feature("rtrj1").feature("col1").set("expr", "gop.pidx");
    model.result("pg1").feature("rtrj1").feature("col1").set("descr", "Ray index");

//    In the Ray Trajectories (gop) toolbar, click Plot.

    model.result("pg1").run();

//    Create additional plots to analyze the beat frequency and scale factor.
//    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 Beat Frequency in the Label text field.

    model.result("pg2").label("Beat Frequency");

//    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);

//    Right-click Beat Frequency and choose Global.

    model.result("pg2").create("glob1", "Global");
    model.result("pg2").feature("glob1").set("markerpos", "datapoints");
    model.result("pg2").feature("glob1").set("linewidth", "preference");

//    In the Settings window for Global, click Replace Expression in the upper-right corner of the y-Axis Data section.
//    From the menu, choose Component 1 (comp1) > Definitions > Variables > dnu - Beat frequency - 1/s.

    model.result("pg2").feature("glob1").set("expr", new String[]{"dnu"});
    model.result("pg2").feature("glob1").set("descr", new String[]{"Beat frequency"});
    model.result("pg2").feature("glob1").set("unit", new String[]{"1/s"});

//    Locate the x-Axis Data section.
//    From the Axis source data list, select Outer solutions.

    model.result("pg2").feature("glob1").set("xdatasolnumtype", "outer");

//    Click to expand the Coloring and Style section.
//    Find the Line markers subsection.
//    From the Marker list, select Point.

    model.result("pg2").feature("glob1").set("linemarker", "point");

//    In the Beat Frequency toolbar, click Plot.

    model.result("pg2").run();

//    Click the x-Axis Log Scale button in the Graphics toolbar.

    model.result("pg2").set("xlog", true);

//    Click the y-Axis Log Scale button in the Graphics toolbar.

    model.result("pg2").set("ylog", true);
    model.result("pg2").run();

//    In the Model Builder window, right-click Beat Frequency and choose Duplicate.

    model.result().duplicate("pg3", "pg2");
    model.result("pg3").run();

//    In the Settings window for 1D Plot Group, type Scale Factor in the Label text field.

    model.result("pg3").label("Scale Factor");
    model.result("pg3").run();

//    In the Model Builder window, expand the Scale Factor node, then click Global 1.
//    In the Settings window for Global, click Replace Expression in the upper-right corner of the y-Axis Data section.
//    From the menu, choose Component 1 (comp1) > Definitions > Variables > SF - Scale factor - rad.

    model.result("pg3").feature("glob1").set("expr", new String[]{"SF"});
    model.result("pg3").feature("glob1").set("descr", new String[]{"Scale factor"});
    model.result("pg3").feature("glob1").set("unit", new String[]{"rad"});

//    Click Add Expression in the upper-right corner of the y-Axis Data section.
//    From the menu, choose Global definitions > Parameters > SF_exp - Expected scale factor - 1.

    model.result("pg3").feature("glob1").set("expr", new String[]{"SF", "SF_exp"});
    model.result("pg3").feature("glob1").set("descr", new String[]{"Scale factor", "Expected scale factor"});

//    In the Scale Factor toolbar, click Plot.

    model.result("pg3").run();

    model.title("Sagnac Interferometer");

    model
         .description("This is a model of a simple Sagnac interferometer consisting of two mirrors and a beam splitter arranged in a triangle. The entire modeling domain rotates; as a result, the rays propagating in opposite directions in the triangle have different optical path lengths due to the Sagnac effect. This can be used to deduce the angular velocity of the system.");

    return model;
  }

  public static void main(String[] args) {
    run();
  }

}
