/*
 * hexagonal_grating.java
 */

import com.comsol.model.*;
import com.comsol.model.util.*;

/** Model exported on May 12 2026, 10:01 by COMSOL 6.4.0.418. */
public class hexagonal_grating {

  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 3D.
//    In the Select Physics tree, select Radio Frequency > Electromagnetic Waves, Frequency Domain (emw).
//    Click Add.
//    Click Study.
//    In the Select Study tree, select General Studies > Frequency Domain.
//    Click Done.

    model.component().create("comp1", true);

    model.component("comp1").geom().create("geom1", 3);
    model.component("comp1").geom("geom1").geomRep("comsol");

    model.component("comp1").mesh().create("mesh1");
    model.component("comp1").mesh("mesh1").contribute("geom/detail", true);

    model.component("comp1").physics().create("emw", "ElectromagneticWaves", "geom1");

    model.study().create("std1");
    model.study("std1").create("freq", "Frequency");
    model.study("std1").feature("freq").set("solnum", "auto");
    model.study("std1").feature("freq").set("notsolnum", "auto");
    model.study("std1").feature("freq").set("outputmap", new String[]{});
    model.study("std1").feature("freq").set("ngenAUX", "1");
    model.study("std1").feature("freq").set("goalngenAUX", "1");
    model.study("std1").feature("freq").set("ngenAUX", "1");
    model.study("std1").feature("freq").set("goalngenAUX", "1");

//    First add some parameters that defines the geometry and the incident electric field.
//    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("wl0", "1[um]");
    model.param().descr("wl0", "Center wavelength");
    model.param().set("lda0", "wl0");
    model.param().descr("lda0", "Wavelength");
    model.param().set("f0", "c_const/lda0");
    model.param().descr("f0", "Frequency");
    model.param().set("a", "wl0/2");
    model.param().descr("a", "Hexagon side length");
    model.param().set("h0", "3*wl0");
    model.param().descr("h0", "Air height");
    model.param().set("a1", "a/2");
    model.param().descr("a1", "Sphere radius");
    model.param().set("theta", "pi/3");
    model.param().descr("theta", "Elevation angle");
    model.param().set("phi", "12[deg]");
    model.param().descr("phi", "Azimuth angle");

//    Notice that the azimuth angle <c>phi</c> above measures the angle for the wave vector of the incident wave from the <eqv>x</eqv>-axis.
//    Here, <c>c_const</c> is a predefined COMSOL constant for the speed of light in vacuum.
//    In the Model Builder window, under Study 1, click Step 1: Frequency Domain.
//    In the Settings window for Frequency Domain, locate the Study Settings section.
//    In the Frequencies text field, type f0.

    model.study("std1").feature("freq").set("plist", "f0");

//    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 µm.

    model.component("comp1").geom("geom1").lengthUnit("\u00b5m");

//    The geometry consists of an extruded hexagon, with a semisphere removed from it at the bottom.
//    In the Geometry toolbar, click Work Plane.

    model.component("comp1").geom("geom1").create("wp1", "WorkPlane");
    model.component("comp1").geom("geom1").feature("wp1").set("unite", true);

//    In the Settings window for Work Plane, click Go to Plane Geometry.
//    In the Model Builder window, click Plane Geometry.
//    In the Work Plane toolbar, click Polygon.

    model.component("comp1").geom("geom1").feature("wp1").geom().create("pol1", "Polygon");
    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1").set("source", "table");

//    In the Settings window for Polygon, locate the Coordinates section.
//    In the table, enter the following settings:

    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1").setIndex("table", "a", 0, 0);
    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1").setIndex("table", 0, 0, 1);
    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1").setIndex("table", "a/2", 1, 0);
    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1")
         .setIndex("table", "sqrt(3)/2*a", 1, 1);
    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1").setIndex("table", "-a/2", 2, 0);
    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1")
         .setIndex("table", "sqrt(3)/2*a", 2, 1);
    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1").setIndex("table", "-a", 3, 0);
    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1").setIndex("table", 0, 3, 1);
    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1").setIndex("table", "-a/2", 4, 0);
    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1")
         .setIndex("table", "-sqrt(3)/2*a", 4, 1);
    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1").setIndex("table", "a/2", 5, 0);
    model.component("comp1").geom("geom1").feature("wp1").geom().feature("pol1")
         .setIndex("table", "-sqrt(3)/2*a", 5, 1);

//    In the Model Builder window, right-click Geometry 1 and choose Extrude.

    model.component("comp1").geom("geom1").run("wp1");
    model.component("comp1").geom("geom1").feature().create("ext1", "Extrude");

//    In the Settings window for Extrude, locate the Distances section.
//    In the table, enter the following settings:

    model.component("comp1").geom("geom1").feature("ext1").setIndex("distance", "h0", 0);

//    In the Geometry toolbar, click Sphere.

    model.component("comp1").geom("geom1").run("ext1");
    model.component("comp1").geom("geom1").create("sph1", "Sphere");

//    In the Settings window for Sphere, locate the Size section.
//    In the Radius text field, type a1.

    model.component("comp1").geom("geom1").feature("sph1").set("r", "a1");

//    In the Geometry toolbar, click Booleans and Partitions and choose Difference.

    model.component("comp1").geom("geom1").run("sph1");
    model.component("comp1").geom("geom1").create("dif1", "Difference");

//    Select the object ext1.

    model.component("comp1").geom("geom1").feature("dif1").selection("input").set("ext1");

//    In the Settings window for Difference, locate the Difference section.
//    Select the Activate Selection toggle button for Objects to subtract.
//    Select the object sph1.

    model.component("comp1").geom("geom1").feature("dif1").selection("input2").set("sph1");

//    Click Build All Objects.

    model.component("comp1").geom("geom1").runPre("fin");

//    Click the Go to Default View button in the Graphics toolbar.
//    Click the Wireframe Rendering button in the Graphics toolbar.

    model.component("comp1").view("view1").set("renderwireframe", true);

    model.component("comp1").geom("geom1").run();

//    In the Materials toolbar, click Add Material to open the Add Material window.
//    In the tree, select Built-in > Air.
//    Click Add to Component in the window toolbar.

    model.component("comp1").material().create("mat1", "Common");
    model.component("comp1").material("mat1").propertyGroup("def").func().create("eta", "Piecewise");
    model.component("comp1").material("mat1").propertyGroup("def").func().create("Cp", "Piecewise");
    model.component("comp1").material("mat1").propertyGroup("def").func().create("rho", "Analytic");
    model.component("comp1").material("mat1").propertyGroup("def").func().create("k", "Piecewise");
    model.component("comp1").material("mat1").propertyGroup("def").func().create("cs", "Analytic");
    model.component("comp1").material("mat1").propertyGroup("def").func().create("an1", "Analytic");
    model.component("comp1").material("mat1").propertyGroup("def").func().create("an2", "Analytic");
    model.component("comp1").material("mat1").propertyGroup()
         .create("RefractiveIndex", "RefractiveIndex", "Refractive index");
    model.component("comp1").material("mat1").propertyGroup()
         .create("NonlinearModel", "NonlinearModel", "Nonlinear model");
    model.component("comp1").material("mat1").propertyGroup().create("idealGas", "idealGas", "Ideal gas");
    model.component("comp1").material("mat1").propertyGroup("idealGas").func().create("Cp", "Piecewise");
    model.component("comp1").material("mat1").label("Air");
    model.component("comp1").material("mat1").set("family", "air");
    model.component("comp1").material("mat1").propertyGroup("def").func("eta").set("arg", "T");
    model.component("comp1").material("mat1").propertyGroup("def").func("eta")
         .set("pieces", new String[][]{{"200.0", "1600.0", "-8.38278E-7+8.35717342E-8*T^1-7.69429583E-11*T^2+4.6437266E-14*T^3-1.06585607E-17*T^4"}});
    model.component("comp1").material("mat1").propertyGroup("def").func("eta").set("argunit", "K");
    model.component("comp1").material("mat1").propertyGroup("def").func("eta").set("fununit", "Pa*s");
    model.component("comp1").material("mat1").propertyGroup("def").func("Cp").set("arg", "T");
    model.component("comp1").material("mat1").propertyGroup("def").func("Cp")
         .set("pieces", new String[][]{{"200.0", "1600.0", "1047.63657-0.372589265*T^1+9.45304214E-4*T^2-6.02409443E-7*T^3+1.2858961E-10*T^4"}});
    model.component("comp1").material("mat1").propertyGroup("def").func("Cp").set("argunit", "K");
    model.component("comp1").material("mat1").propertyGroup("def").func("Cp").set("fununit", "J/(kg*K)");
    model.component("comp1").material("mat1").propertyGroup("def").func("rho")
         .set("expr", "pA*0.02897/R_const[K*mol/J]/T");
    model.component("comp1").material("mat1").propertyGroup("def").func("rho").set("args", new String[]{"pA", "T"});
    model.component("comp1").material("mat1").propertyGroup("def").func("rho").set("fununit", "kg/m^3");
    model.component("comp1").material("mat1").propertyGroup("def").func("rho")
         .set("argunit", new String[]{"Pa", "K"});
    model.component("comp1").material("mat1").propertyGroup("def").func("rho")
         .set("plotaxis", new String[]{"off", "on"});
    model.component("comp1").material("mat1").propertyGroup("def").func("rho")
         .set("plotfixedvalue", new String[]{"101325", "273.15"});
    model.component("comp1").material("mat1").propertyGroup("def").func("rho")
         .set("plotargs", new String[][]{{"pA", "101325", "101325"}, {"T", "273.15", "293.15"}});
    model.component("comp1").material("mat1").propertyGroup("def").func("k").set("arg", "T");
    model.component("comp1").material("mat1").propertyGroup("def").func("k")
         .set("pieces", new String[][]{{"200.0", "1600.0", "-0.00227583562+1.15480022E-4*T^1-7.90252856E-8*T^2+4.11702505E-11*T^3-7.43864331E-15*T^4"}});
    model.component("comp1").material("mat1").propertyGroup("def").func("k").set("argunit", "K");
    model.component("comp1").material("mat1").propertyGroup("def").func("k").set("fununit", "W/(m*K)");
    model.component("comp1").material("mat1").propertyGroup("def").func("cs")
         .set("expr", "sqrt(1.4*R_const[K*mol/J]/0.02897*T)");
    model.component("comp1").material("mat1").propertyGroup("def").func("cs").set("args", new String[]{"T"});
    model.component("comp1").material("mat1").propertyGroup("def").func("cs").set("fununit", "m/s");
    model.component("comp1").material("mat1").propertyGroup("def").func("cs").set("argunit", new String[]{"K"});
    model.component("comp1").material("mat1").propertyGroup("def").func("cs")
         .set("plotfixedvalue", new String[]{"273.15"});
    model.component("comp1").material("mat1").propertyGroup("def").func("cs")
         .set("plotargs", new String[][]{{"T", "273.15", "373.15"}});
    model.component("comp1").material("mat1").propertyGroup("def").func("an1").set("funcname", "alpha_p");
    model.component("comp1").material("mat1").propertyGroup("def").func("an1")
         .set("expr", "-1/rho(pA,T)*d(rho(pA,T),T)");
    model.component("comp1").material("mat1").propertyGroup("def").func("an1").set("args", new String[]{"pA", "T"});
    model.component("comp1").material("mat1").propertyGroup("def").func("an1").set("fununit", "1/K");
    model.component("comp1").material("mat1").propertyGroup("def").func("an1")
         .set("argunit", new String[]{"Pa", "K"});
    model.component("comp1").material("mat1").propertyGroup("def").func("an1")
         .set("plotaxis", new String[]{"off", "on"});
    model.component("comp1").material("mat1").propertyGroup("def").func("an1")
         .set("plotfixedvalue", new String[]{"101325", "273.15"});
    model.component("comp1").material("mat1").propertyGroup("def").func("an1")
         .set("plotargs", new String[][]{{"pA", "101325", "101325"}, {"T", "273.15", "373.15"}});
    model.component("comp1").material("mat1").propertyGroup("def").func("an2").set("funcname", "muB");
    model.component("comp1").material("mat1").propertyGroup("def").func("an2").set("expr", "0.6*eta(T)");
    model.component("comp1").material("mat1").propertyGroup("def").func("an2").set("args", new String[]{"T"});
    model.component("comp1").material("mat1").propertyGroup("def").func("an2").set("fununit", "Pa*s");
    model.component("comp1").material("mat1").propertyGroup("def").func("an2").set("argunit", new String[]{"K"});
    model.component("comp1").material("mat1").propertyGroup("def").func("an2")
         .set("plotfixedvalue", new String[]{"200"});
    model.component("comp1").material("mat1").propertyGroup("def").func("an2")
         .set("plotargs", new String[][]{{"T", "200", "1600"}});
    model.component("comp1").material("mat1").propertyGroup("def").set("thermalexpansioncoefficient", "");
    model.component("comp1").material("mat1").propertyGroup("def").set("molarmass", "");
    model.component("comp1").material("mat1").propertyGroup("def").set("bulkviscosity", "");
    model.component("comp1").material("mat1").propertyGroup("def")
         .set("thermalexpansioncoefficient", new String[]{"alpha_p(pA,T)", "0", "0", "0", "alpha_p(pA,T)", "0", "0", "0", "alpha_p(pA,T)"});
    model.component("comp1").material("mat1").propertyGroup("def").set("molarmass", "0.02897[kg/mol]");
    model.component("comp1").material("mat1").propertyGroup("def").set("bulkviscosity", "muB(T)");
    model.component("comp1").material("mat1").propertyGroup("def")
         .set("relpermeability", new String[]{"1", "0", "0", "0", "1", "0", "0", "0", "1"});
    model.component("comp1").material("mat1").propertyGroup("def")
         .set("relpermittivity", new String[]{"1", "0", "0", "0", "1", "0", "0", "0", "1"});
    model.component("comp1").material("mat1").propertyGroup("def").set("dynamicviscosity", "eta(T)");
    model.component("comp1").material("mat1").propertyGroup("def").set("ratioofspecificheat", "1.4");
    model.component("comp1").material("mat1").propertyGroup("def")
         .set("electricconductivity", new String[]{"0[S/m]", "0", "0", "0", "0[S/m]", "0", "0", "0", "0[S/m]"});
    model.component("comp1").material("mat1").propertyGroup("def").set("heatcapacity", "Cp(T)");
    model.component("comp1").material("mat1").propertyGroup("def").set("density", "rho(pA,T)");
    model.component("comp1").material("mat1").propertyGroup("def")
         .set("thermalconductivity", new String[]{"k(T)", "0", "0", "0", "k(T)", "0", "0", "0", "k(T)"});
    model.component("comp1").material("mat1").propertyGroup("def").set("soundspeed", "cs(T)");
    model.component("comp1").material("mat1").propertyGroup("def").addInput("temperature");
    model.component("comp1").material("mat1").propertyGroup("def").addInput("pressure");
    model.component("comp1").material("mat1").propertyGroup("RefractiveIndex")
         .set("n", new String[]{"1", "0", "0", "0", "1", "0", "0", "0", "1"});
    model.component("comp1").material("mat1").propertyGroup("NonlinearModel").set("BA", "def.gamma-1");
    model.component("comp1").material("mat1").propertyGroup("idealGas").func("Cp").label("Piecewise 2");
    model.component("comp1").material("mat1").propertyGroup("idealGas").func("Cp").set("arg", "T");
    model.component("comp1").material("mat1").propertyGroup("idealGas").func("Cp")
         .set("pieces", new String[][]{{"200.0", "1600.0", "1047.63657-0.372589265*T^1+9.45304214E-4*T^2-6.02409443E-7*T^3+1.2858961E-10*T^4"}});
    model.component("comp1").material("mat1").propertyGroup("idealGas").func("Cp").set("argunit", "K");
    model.component("comp1").material("mat1").propertyGroup("idealGas").func("Cp").set("fununit", "J/(kg*K)");
    model.component("comp1").material("mat1").propertyGroup("idealGas").set("Rs", "R_const/Mn");
    model.component("comp1").material("mat1").propertyGroup("idealGas").set("heatcapacity", "Cp(T)");
    model.component("comp1").material("mat1").propertyGroup("idealGas").set("ratioofspecificheat", "1.4");
    model.component("comp1").material("mat1").propertyGroup("idealGas").set("molarmass", "0.02897[kg/mol]");
    model.component("comp1").material("mat1").propertyGroup("idealGas").addInput("temperature");
    model.component("comp1").material("mat1").propertyGroup("idealGas").addInput("pressure");
    model.component("comp1").material("mat1").materialType("nonSolid");

//    In the Materials toolbar, click Add Material to close the Add Material window.
//    In the Settings window for Electromagnetic Waves, Frequency Domain, locate the Analysis Methodology section.
//    From the Methodology options list, select Robust.

    model.component("comp1").physics("emw").prop("AnalysisMethodology").set("MethodologyOptions", "Robust");

//    In the Physics toolbar, click Domains and choose Periodic Structure.

    model.component("comp1").physics("emw").create("ps1", "PeriodicStructure", 3);

//    In the Settings window for Periodic Structure, locate the Port Handling section.
//    Clear the Add listener port checkbox.

    model.component("comp1").physics("emw").feature("ps1").set("addListenerPort", false);

//    , as the bottom boundary should use the default <l>Perfect Electric Conductor 1</l> node.
//    Locate the Port Mode Settings section.
//    In the \[\alpha_{1}\] text field, type theta.

    model.component("comp1").physics("emw").feature("ps1").set("alpha1_inc", "theta");

//    In the \[\alpha_{2}\] text field, type phi.

    model.component("comp1").physics("emw").feature("ps1").set("alpha2_inc", "phi");

//    , as this angle is measured from the <l>Reference Direction</l> subnode of the <l>Periodic Structure</l>.
//    In the Model Builder window, click Periodic Structure 1.
//    Locate the Port Handling section.
//    From the Diffraction order specification list, select From current parameters.

    model.component("comp1").physics("emw").feature("ps1")
         .set("DiffractionOrderSpecification", "FromCurrentParameters");

//    , as no angle sweep will be done in this example model.
//    Click Add Diffraction Orders.

    model.component("comp1").physics("emw").feature("ps1").feature("fpc1")
         .set("splitPeriodicConditionSelections", true);
    model.component("comp1").physics("emw").feature("ps1").feature("fpc2")
         .set("splitPeriodicConditionSelections", true);
    model.component("comp1").physics("emw").feature("ps1").feature("fpc3")
         .set("splitPeriodicConditionSelections", true);
    model.component("comp1").physics("emw").feature("ps1").feature("fpc1")
         .set("splitPeriodicConditionSelections", false);
    model.component("comp1").physics("emw").feature("ps1").feature("fpc2")
         .set("splitPeriodicConditionSelections", false);
    model.component("comp1").physics("emw").feature("ps1").feature("fpc3")
         .set("splitPeriodicConditionSelections", false);
    model.component("comp1").physics("emw").feature("ps1").feature("pport1").runCommand("addDiffractionOrders");

//    In the Model Builder window, under Component 1 (comp1), click Mesh 1.
//    In the Settings window for Mesh, locate the Electromagnetic Waves, Frequency Domain (emw) section.
//    From the Maximum mesh element size control parameter list, select User defined.

    model.component("comp1").physics("emw").prop("MeshControl").set("SizeControlParameter", "UserDefined");

//    In the Maximum element size in free space text field, type wl0/6.

    model.component("comp1").physics("emw").prop("MeshControl")
         .set("PhysicsControlledMeshMaximumElementSize", "wl0/6");

//    Click Build All.

    model.component("comp1").mesh("mesh1").run();

//    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", "wl0", 0);
    model.study("std1").feature("param").setIndex("plistarr", "", 0);
    model.study("std1").feature("param").setIndex("punit", "m", 0);
    model.study("std1").feature("param").setIndex("pname", "wl0", 0);
    model.study("std1").feature("param").setIndex("plistarr", "", 0);
    model.study("std1").feature("param").setIndex("punit", "m", 0);

//    From the list in the <l>Parameter name</l> column, choose <l>lda0 (Wavelength)</l>.
//    In the table, enter the following settings:

    model.study("std1").feature("param").setIndex("pname", "lda0", 0);

//    Click Range.
//    In the Range dialog, type 0.9[um] in the Start text field.
//    In the Step text field, type 0.01[um].
//    In the Stop text field, type 1.1[um].
//    Click Replace.

    model.study("std1").feature("param").setIndex("plistarr", "range(0.9[um],0.01[um],1.1[um])", 0);

//    In the Settings window for Parametric Sweep, locate the Study Settings section.
//    In the table, enter the following settings:

    model.study("std1").feature("param").setIndex("punit", "um", 0);

//    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.component("comp1").probe("emw_pport1_probe").genResult("none");

    model.batch("p1").run("compute");

    model.result().create("pg2", "PlotGroup3D");
    model.result("pg2").set("data", "dset2");
    model.result("pg2").setIndex("looplevel", 1, 0);
    model.result("pg2").setIndex("looplevel", 21, 1);
    model.result("pg2").create("mslc1", "Multislice");
    model.result("pg2").feature("mslc1").set("expr", new String[]{"emw.normE"});
    model.result("pg2").set("showlegendsmaxmin", true);
    model.result("pg2").feature("mslc1").set("colortable", "RainbowLight");
    model.result("pg2").label("Electric Field (emw)");
    model.result().create("pg3", "PlotGroup1D");
    model.result("pg3").set("data", "dset2");
    model.result("pg3").create("glob1", "Global");
    model.result("pg3").feature("glob1").set("unit", new String[]{"", "", "", "", "", ""});
    model.result("pg3").feature("glob1")
         .set("expr", new String[]{"emw.S11dB", "emw.S31dB", "emw.S41dB", "emw.S51dB", "emw.S61dB", "emw.S71dB"});
    model.result("pg3").feature("glob1").set("descr", new String[]{"S11", "S31", "S41", "S51", "S61", "S71"});
    model.result("pg3").label("S-Parameter (emw)");
    model.result("pg3").feature("glob1").set("titletype", "none");
    model.result("pg3").feature("glob1").set("xdata", "expr");
    model.result("pg3").set("ylabelactive", true);
    model.result("pg3").set("ylabel", "S-parameter (dB)");
    model.result("pg3").feature("glob1").set("xdataexpr", "lda0");
    model.result("pg3").feature("glob1").set("xdataunit", "um");
    model.result("pg3").feature("glob1").set("markerpos", "datapoints");
    model.result("pg3").feature("glob1").set("xdatasolnumtype", "all");
    model.result().create("pg4", "SmithGroup");
    model.result("pg4").set("data", "dset2");
    model.result("pg4").create("rgr1", "ReflectionGraph");
    model.result("pg4").feature("rgr1").set("unit", new String[]{""});
    model.result("pg4").feature("rgr1").set("expr", new String[]{"emw.S11"});
    model.result("pg4").feature("rgr1").set("descr", new String[]{"S11"});
    model.result("pg4").label("Smith Plot (emw)");
    model.result("pg4").feature("rgr1").set("titletype", "manual");
    model.result("pg4").feature("rgr1").set("title", "Reflection Graph: S-parameter, Color: Frequency (GHz)");
    model.result("pg4").feature("rgr1").set("linemarker", "point");
    model.result("pg4").feature("rgr1").set("markerpos", "datapoints");
    model.result("pg4").feature("rgr1").create("col1", "Color");
    model.result("pg4").feature("rgr1").feature("col1").set("expr", "emw.freq/1e9");
    model.result("pg4").feature("rgr1").feature("col1").set("colortable", "Spectrum");
    model.result().create("pg5", "PlotGroup3D");
    model.result("pg5").set("data", "dset2");
    model.result("pg5").label("Electric Field, Logarithmic (emw)");
    model.result("pg5").selection().geom("geom1", 3);
    model.result("pg5").selection().set(1);
    model.result("pg5").set("applyselectiontodatasetedges", true);
    model.result("pg5").create("surf1", "Surface");
    model.result("pg5").feature("surf1").set("expr", "emw.normE");
    model.result("pg5").feature("surf1").create("sel1", "Selection");
    model.result("pg5").feature("surf1").feature("sel1").selection().set(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12);
    model.result("pg5").feature("surf1").set("colortable", "Dipole");
    model.result("pg5").feature("surf1").set("colorscalemode", "logarithmic");
    model.result("pg5").feature("surf1").create("tran1", "Transparency");
    model.result("pg5").feature("surf1").feature("tran1").set("transparency", 0.7);

    model.component("comp1").view("view2").camera().set("position", new double[]{-3, -4, 3});
    model.component("comp1").view("view2").camera().set("up", new double[]{0, 0, 1});
    model.component("comp1").view().create("view3", "geom1");
    model.component("comp1").view("view3").camera().set("position", new double[]{-3.85, -5.13, 4.5});
    model.component("comp1").view("view3").set("environmentmap", "Indoor");
    model.component("comp1").view("view3").camera().set("zoomanglefull", 48.224999999999994);

    model.result("pg5").set("view", "view3");
    model.result().create("pg6", "PlotGroup1D");
    model.result("pg6").label("Polarization Plot (emw)");
    model.result("pg6").set("data", "dset2");
    model.result("pg6").set("titletype", "manual");
    model.result("pg6").set("title", "Polarization states, Color: Phase (Radians)");
    model.result("pg6").setIndex("looplevelinput", "manual", 1);
    model.result("pg6").setIndex("looplevel", "1", 0);
    model.result("pg6").setIndex("looplevel", "1", 1);
    model.result("pg6").create("plz1", "Polarization");
    model.result("pg6").feature("plz1").set("normalization", "allorders");
    model.result("pg6").feature("plz1").set("linestyle", "dotted");
    model.result("pg6").feature("plz1").set("linewidth", 2);
    model.result("pg6").feature("plz1").set("display", "3");
    model.result("pg6").feature("plz1").create("col1", "Color");
    model.result("pg6").feature("plz1").feature("col1").set("colortable", "Cyclic");
    model.result("pg6").feature("plz1").feature("col1").set("colorlegend", true);
    model.result("pg6").feature("plz1").set("legend", true);
    model.result("pg6").feature("plz1").set("legendmethod", "manual");
    model.result("pg6").feature("plz1").setIndex("legends", "Input field", 0);
    model.result("pg6").create("plz2", "Polarization");
    model.result("pg6").feature("plz2").set("normalization", "allorders");
    model.result("pg6").feature("plz2").set("linestyle", "solid");
    model.result("pg6").feature("plz2").set("linewidth", 2);
    model.result("pg6").feature("plz2").set("display", "2");
    model.result("pg6").feature("plz2").create("col1", "Color");
    model.result("pg6").feature("plz2").feature("col1").set("colortable", "Cyclic");
    model.result("pg6").feature("plz2").feature("col1").set("colorlegend", false);
    model.result("pg6").feature("plz2").set("legend", true);
    model.result("pg6").feature("plz2").set("legendmethod", "manual");
    model.result("pg6").feature("plz2").setIndex("legends", "Reflection", 0);
    model.result("pg6").create("plz3", "Polarization");
    model.result("pg6").feature("plz3").set("normalization", "allorders");
    model.result("pg6").feature("plz3").set("linestyle", "solid");
    model.result("pg6").feature("plz3").set("linewidth", 2);
    model.result("pg6").feature("plz3").set("display", "0");
    model.result("pg6").feature("plz3").create("col1", "Color");
    model.result("pg6").feature("plz3").feature("col1").set("colortable", "Cyclic");
    model.result("pg6").feature("plz3").feature("col1").set("colorlegend", false);
    model.result("pg6").create("plz4", "Polarization");
    model.result("pg6").feature("plz4").set("normalization", "allorders");
    model.result("pg6").feature("plz4").set("linestyle", "solid");
    model.result("pg6").feature("plz4").set("linewidth", 2);
    model.result("pg6").feature("plz4").set("display", "1");
    model.result("pg6").feature("plz4").create("col1", "Color");
    model.result("pg6").feature("plz4").feature("col1").set("colortable", "Cyclic");
    model.result("pg6").feature("plz4").feature("col1").set("colorlegend", false);
    model.result("pg6").create("plz5", "Polarization");
    model.result("pg6").feature("plz5").label("Propagation Limit, Reflection Side");
    model.result("pg6").feature("plz5").set("planewave", "manual");
    model.result("pg6").feature("plz5")
         .set("xexpr", "emw.aAxisR*cos(emw.thetaAxisR)-j*emw.bAxisR*sin(emw.thetaAxisR)");
    model.result("pg6").feature("plz5")
         .set("yexpr", "emw.aAxisR*sin(emw.thetaAxisR)+j*emw.bAxisR*cos(emw.thetaAxisR)");
    model.result("pg6").feature("plz5").set("normalization", "manual");
    model.result("pg6").feature("plz5").set("plotscale", "1");
    model.result("pg6").feature("plz5").set("centerpos", "user");
    model.result("pg6").feature("plz5").set("xcenter", "emw.mOriginR");
    model.result("pg6").feature("plz5").set("ycenter", "emw.nOriginR");
    model.result("pg6").feature("plz5").set("showrotdir", false);
    model.result("pg6").feature("plz5").set("showorder", false);
    model.result("pg6").feature("plz5").set("legend", true);
    model.result("pg6").feature("plz5").set("legendmethod", "manual");
    model.result("pg6").feature("plz5").setIndex("legends", "Propagation limit, reflection side", 0);
    model.result("pg6").feature("plz5").active(true);
    model.result("pg2").run();

//    Add Arrow Surface plots showing the wave vector directions for the incident field, the reflected field and the diffracted fields. Notice that the diffracted fields come in pairs, where each pair have the same wave vector. Thus, only two wave vectors for the diffraction orders need to be added in this case.
//    Right-click Electric Field (emw) and choose Arrow Surface.

    model.result("pg2").create("arws1", "ArrowSurface");
    model.result("pg2").feature("arws1").set("evaluationsettings", "parent");

//    In the Settings window for Arrow Surface, click Replace Expression in the upper-right corner of the Expression section.
//    From the menu, choose Component 1 (comp1) > Electromagnetic Waves, Frequency Domain > Ports > Wave vectors > emw.kIncx_1,...,emw.kIncz_1 - Incident wave vector.

    model.result("pg2").feature("arws1").set("expr", new String[]{"emw.kIncx_1", "emw.kIncy_1", "emw.kIncz_1"});
    model.result("pg2").feature("arws1").set("descr", "Incident wave vector");

//    Locate the Expression section.
//    Select the Description checkbox.

    model.result("pg2").feature("arws1").set("descractive", true);

//    In the associated text field, type Incident wave (red).

    model.result("pg2").feature("arws1").set("descr", "Incident wave (red)");

//    Locate the Arrow Positioning section.
//    In the Number of arrows text field, type 12.

    model.result("pg2").feature("arws1").set("arrowcount", 12);

//    Locate the Coloring and Style section.
//    Select the Scale factor checkbox.

    model.result("pg2").feature("arws1").set("scaleactive", true);

//    In the associated text field, type 3e-8.

    return model;
  }

  public static Model run2(Model model) {

    model.result("pg2").feature("arws1").set("scale", "3e-8");

//    Right-click Arrow Surface 1 and choose Selection.

    model.result("pg2").feature("arws1").create("sel1", "Selection");

//    Select Boundary 4.

    model.result("pg2").feature("arws1").feature("sel1").selection().set(4);

//    , to select only the port boundary.

    model.result("pg2").run();

//    In the Model Builder window, right-click Results > Electric Field (emw) > Arrow Surface 1 and choose Duplicate.

    model.result("pg2").feature().duplicate("arws2", "arws1");
    model.result("pg2").run();

//    In the Settings window for Arrow Surface, click Replace Expression in the upper-right corner of the Expression section.
//    From the menu, choose Component 1 (comp1) > Electromagnetic Waves, Frequency Domain > Ports > Wave vectors > emw.kModex_1,...,emw.kModez_1 - Port mode wave vector, port 1.

    model.result("pg2").feature("arws2").set("expr", new String[]{"emw.kModex_1", "emw.kModey_1", "emw.kModez_1"});

//    Locate the Coloring and Style section.
//    From the Color list, select Blue.

    model.result("pg2").feature("arws2").set("color", "blue");

//    Locate the Expression section.
//    In the Description text field, type Reflected wave (blue).

    model.result("pg2").feature("arws2").set("descr", "Reflected wave (blue)");

//    Right-click Arrow Surface 2 and choose Duplicate.

    model.result("pg2").feature().duplicate("arws3", "arws2");
    model.result("pg2").run();

//    In the Settings window for Arrow Surface, click Replace Expression in the upper-right corner of the Expression section.
//    From the menu, choose Component 1 (comp1) > Electromagnetic Waves, Frequency Domain > Ports > Wave vectors > emw.kModex_3,...,emw.kModez_3 - Port mode wave vector, port 3.

    model.result("pg2").feature("arws3").set("expr", new String[]{"emw.kModex_3", "emw.kModey_3", "emw.kModez_3"});

//    Locate the Coloring and Style section.
//    From the Color list, select Green.

    model.result("pg2").feature("arws3").set("color", "green");

//    Locate the Expression section.
//    In the Description text field, type Mode m = -1, n = -1 (green).

    model.result("pg2").feature("arws3").set("descr", "Mode m = -1, n = -1 (green)");

//    Right-click Arrow Surface 3 and choose Duplicate.

    model.result("pg2").feature().duplicate("arws4", "arws3");
    model.result("pg2").run();

//    In the Settings window for Arrow Surface, click Replace Expression in the upper-right corner of the Expression section.
//    From the menu, choose Component 1 (comp1) > Electromagnetic Waves, Frequency Domain > Ports > Wave vectors > emw.kModex_5,...,emw.kModez_5 - Port mode wave vector, port 5.

    model.result("pg2").feature("arws4").set("expr", new String[]{"emw.kModex_5", "emw.kModey_5", "emw.kModez_5"});

//    Locate the Coloring and Style section.
//    From the Color list, select Yellow.

    model.result("pg2").feature("arws4").set("color", "yellow");

//    Locate the Expression section.
//    In the Description text field, type Mode m = 0, n = -1 (yellow).

    model.result("pg2").feature("arws4").set("descr", "Mode m = 0, n = -1 (yellow)");
    model.result("pg2").run();

//    Select the wavelength closest to the critical wavelength for the mode <eqv>m</eqv> = 0, <eqv>n</eqv> = -1.
//    In the Model Builder window, click Electric Field (emw).
//    In the Settings window for 3D Plot Group, locate the Data section.
//    From the Parameter value (lda0 (um)) list, select 1.01.

    model.result("pg2").set("looplevel", new int[]{1, 12});

//    In the Electric Field (emw) toolbar, click Plot.

    model.result("pg2").run();

//    Click the Zoom Extents button in the Graphics toolbar.

    model.result("pg3").run();

//    In the Model Builder window, expand the Results > S-Parameter (emw) node, then click Global 1.
//    In the Settings window for Global, locate the y-Axis Data section.
//    In the table, enter the following settings:

    model.result("pg3").feature("glob1").setIndex("expr", "abs(emw.S11)^2", 0);
    model.result("pg3").feature("glob1").setIndex("unit", 1, 0);
    model.result("pg3").feature("glob1").setIndex("descr", "Reflectance", 0);
    model.result("pg3").feature("glob1").setIndex("expr", "abs(emw.S31)^2", 1);
    model.result("pg3").feature("glob1").setIndex("unit", 1, 1);
    model.result("pg3").feature("glob1").setIndex("descr", "m = -1, n = -1, in-plane", 1);
    model.result("pg3").feature("glob1").setIndex("expr", "abs(emw.S41)^2", 2);
    model.result("pg3").feature("glob1").setIndex("unit", 1, 2);
    model.result("pg3").feature("glob1").setIndex("descr", "m = -1, n = -1, out-of-plane", 2);
    model.result("pg3").feature("glob1").setIndex("expr", "abs(emw.S51)^2", 3);
    model.result("pg3").feature("glob1").setIndex("unit", 1, 3);
    model.result("pg3").feature("glob1").setIndex("descr", "m = 0, n = -1, in-plane", 3);
    model.result("pg3").feature("glob1").setIndex("expr", "abs(emw.S61)^2", 4);
    model.result("pg3").feature("glob1").setIndex("unit", 1, 4);
    model.result("pg3").feature("glob1").setIndex("descr", "m = 0, n = -1, out-of-plane", 4);
    model.result("pg3").feature("glob1").setIndex("expr", "abs(emw.S71)^2", 5);
    model.result("pg3").feature("glob1").setIndex("unit", 1, 5);
    model.result("pg3").feature("glob1").setIndex("descr", "m = 0, n = 0, orthogonal", 5);

//    Note that port name 2 represents the hidden port on the transmission side. Thus, that port name is not used in the S-parameter variable names above.
//    Locate the x-Axis Data section.
//    From the Axis source data list, select Outer solutions.

    model.result("pg3").feature("glob1").set("xdatasolnumtype", "outer");

//    Select the Description checkbox.

    model.result("pg3").feature("glob1").set("xdatadescractive", true);

//    Click to expand the Legends section.
//    From the Legends list, select Manual.

    model.result("pg3").feature("glob1").set("legendmethod", "manual");

//    In the table, enter the following settings:

    model.result("pg3").feature("glob1").setIndex("legends", "Reflectance", 0);
    model.result("pg3").feature("glob1").setIndex("legends", "m = -1, n = -1, in-plane", 1);
    model.result("pg3").feature("glob1").setIndex("legends", "m = -1, n = -1, out-of-plane", 2);
    model.result("pg3").feature("glob1").setIndex("legends", "m = 0, n = -1, in-plane", 3);
    model.result("pg3").feature("glob1").setIndex("legends", "m = 0, n = -1, out-of-plane", 4);
    model.result("pg3").feature("glob1").setIndex("legends", "m = 0, n = 0, orthogonal", 5);
    model.result("pg3").run();

//    In the Model Builder window, click S-Parameter (emw).
//    In the Settings window for 1D Plot Group, click to expand the Title section.
//    From the Title type list, select None.

    model.result("pg3").set("titletype", "none");

//    Locate the Plot Settings section.
//    In the y-axis label text field, type Diffraction efficiency.

    model.result("pg3").set("ylabel", "Diffraction efficiency");

//    Locate the Legend section.
//    From the Position list, select Upper left.

    model.result("pg3").set("legendpos", "upperleft");

//    In the S-Parameter (emw) toolbar, click Plot.

    model.result("pg3").run();
    model.result("pg6").run();

//    In the Model Builder window, click Polarization Plot (emw).
//    In the Settings window for 1D Plot Group, locate the Legend section.
//    From the Layout list, select Outside graph axis area.

    model.result("pg6").set("legendlayout", "outside");

//    From the Position list, select Bottom.

    model.result("pg6").set("legendposoutside", "bottom");

//    In the Number of rows text field, type 2.

    model.result("pg6").set("legendrowcount", 2);

//    In the Polarization Plot (emw) toolbar, click Plot.

    model.result("pg6").run();

//    This plot shows the polarization ellipses for the modes for TE polarization.
//    Now, repeat the simulation for an incoming wave with p-polarization (the electric field polarized in the plane of incidence).
//    In the Model Builder window, under Component 1 (comp1) > Electromagnetic Waves, Frequency Domain (emw), click Periodic Structure 1.
//    In the Settings window for Periodic Structure, locate the Port Mode Settings section.
//    From the list, select Transverse magnetic (TM).

    model.component("comp1").physics("emw").feature("ps1").set("LinearPol", "TM");

//    In the Home toolbar, click Compute.

    model.study("std1").createAutoSequences("all");

    model.component("comp1").probe("emw_pport1_probe").genResult("none");

    model.batch("p1").run("compute");

    model.result("pg2").run();

//    In the Settings window for 3D Plot Group, locate the Data section.
//    From the Parameter value (lda0 (um)) list, select 1.01.
//    Click the Zoom Extents button in the Graphics toolbar.

    model.result("pg3").run();

//    In the Model Builder window, click S-Parameter (emw).
//    In the S-Parameter (emw) toolbar, click Plot.

    model.result("pg3").run();

//    Finally, take a look at the polarization states when the input wave is p-polarized.

    model.result("pg6").run();

//    In the Model Builder window, click Polarization Plot (emw).
//    In the Polarization Plot (emw) toolbar, click Plot.

    model.result("pg6").run();

//    Also in this case, the polarization is elliptical for the different modes.
//    In the Results toolbar, click Configurations and choose Preferred Units.

    model.result().configuration().create("prfu1", "PreferredUnits");

//    In the Settings window for Preferred Units, locate the Units section.
//    Click Add Physical Quantity.
//    In the Physical Quantity dialog, type plane in the text field.
//    In the tree, select General > Plane angle (rad).
//    Click OK.

    model.result().configuration("prfu1")
         .setIndex("quantityunits", new String[]{"planeangle", "Plane angle", "rad", "rad"}, 0);

//    In the Settings window for Preferred Units, locate the Units section.
//    In the table, enter the following settings:

    model.result().configuration("prfu1").setIndex("quantityunits", "\u00b0", 0, 3);

//    In the Results toolbar, click 1D Plot Group.

    model.result().create("pg7", "PlotGroup1D");
    model.result("pg7").run();

//    In the Settings window for 1D Plot Group, type Diffraction Angles (emw) in the Label text field.

    model.result("pg7").label("Diffraction Angles (emw)");

//    Locate the Data section.
//    From the Dataset list, select Study 1/Parametric Solutions 1 (sol2).

    model.result("pg7").set("data", "dset2");

//    Right-click Diffraction Angles (emw) and choose Global.

    model.result("pg7").create("glob1", "Global");
    model.result("pg7").feature("glob1").set("markerpos", "datapoints");
    model.result("pg7").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) > Electromagnetic Waves, Frequency Domain > Ports > Elevation angle, by order > All expressions in this group.

    model.result("pg7").feature("glob1").set("descr", new String[]{});
    model.result("pg7").feature("glob1").set("unit", new String[]{});
    model.result("pg7").feature("glob1")
         .set("expr", new String[]{"emw.alpha1R_0_0", "emw.alpha1R_0_n1", "emw.alpha1R_n1_n1"});

//    Locate the x-Axis Data section.
//    From the Axis source data list, select Outer solutions.

    model.result("pg7").feature("glob1").set("xdatasolnumtype", "outer");

//    From the Parameter list, select Expression.

    model.result("pg7").feature("glob1").set("xdata", "expr");

//    In the Expression text field, type lda0.

    model.result("pg7").feature("glob1").set("xdataexpr", "lda0");

//    Click to expand the Legends section.
//    Find the Include subsection.
//    Clear the Solution checkbox.

    model.result("pg7").feature("glob1").set("autosolution", false);

//    Right-click Global 1 and choose Duplicate.

    model.result("pg7").feature().duplicate("glob2", "glob1");
    model.result("pg7").run();

//    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) > Electromagnetic Waves, Frequency Domain > Ports > Azimuth angle, by order > All expressions in this group.

    model.result("pg7").feature("glob2").set("descr", new String[]{});
    model.result("pg7").feature("glob2").set("unit", new String[]{});
    model.result("pg7").feature("glob2")
         .set("expr", new String[]{"emw.alpha2R_0_0", "emw.alpha2R_0_n1", "emw.alpha2R_n1_n1"});

//    Click to expand the Coloring and Style section.
//    Find the Line style subsection.
//    From the Line list, select None.

    model.result("pg7").feature("glob2").set("linestyle", "none");

//    From the Color list, select Cycle (reset).

    model.result("pg7").feature("glob2").set("linecolor", "cyclereset");

//    Find the Line markers subsection.
//    From the Marker list, select Cycle.

    model.result("pg7").feature("glob2").set("linemarker", "cycle");

//    In the Diffraction Angles (emw) toolbar, click Plot.

    model.result("pg7").run();
    model.result("pg7").run();

//    In the Model Builder window, click Diffraction Angles (emw).
//    In the Settings window for 1D Plot Group, click to expand the Title section.
//    From the Title type list, select None.

    model.result("pg7").set("titletype", "none");

//    Locate the Plot Settings section.
//    Select the y-axis label checkbox.

    model.result("pg7").set("ylabelactive", true);

//    In the associated text field, type Diffraction angles (degrees).

    model.result("pg7").set("ylabel", "Diffraction angles (degrees)");

//    Locate the Legend section.
//    From the Layout list, select Outside graph axis area.

    model.result("pg7").set("legendlayout", "outside");

//    From the Position list, select Bottom.

    model.result("pg7").set("legendposoutside", "bottom");

//    In the Number of rows text field, type 3.

    model.result("pg7").set("legendrowcount", 3);

//    In the Diffraction Angles (emw) toolbar, click Plot.

    model.result("pg7").run();

//    Notice that the wavelength dependence for the elevation and azimuth angles is very small for <eqv>m</eqv> = <eqv>n</eqv> = 0, whereas there is some wavelength dependence for the higher-order modes.

    model.result("pg2").run();

    model.title("Hexagonal Grating");

    model
         .description("A plane wave is incident on a reflecting hexagonal grating. The grating cell consists of a protruding semisphere. The scattering coefficients for the different diffraction orders are calculated for a few different wavelengths.");

    return model;
  }

  public static void main(String[] args) {
    Model model = run();
    run2(model);
  }

}
