/*
 * h_bend_waveguide_3d.java
 */

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

/** Model exported on May 13 2026, 08:21 by COMSOL 6.4.0.419. */
public class h_bend_waveguide_3d {

  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");

//    Define the study frequency ahead of performing any frequency-dependent operation such as building mesh. The physics-controlled mesh uses the highest frequency value in the specified range.
//    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 range(4[GHz],25[MHz],5.2[GHz]).

    model.study("std1").feature("freq").set("plist", "range(4[GHz],25[MHz],5.2[GHz])");

//    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("l_wg", "10[cm]");
    model.param().descr("l_wg", "Waveguide length");
    model.param().set("w_wg", "4[cm]");
    model.param().descr("w_wg", "Waveguide width");
    model.param().set("h_wg", "2[cm]");
    model.param().descr("h_wg", "Waveguide height");

//    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 Circle.

    model.component("comp1").geom("geom1").feature("wp1").geom().create("c1", "Circle");

//    In the Settings window for Circle, locate the Size and Shape section.
//    In the Radius text field, type w_wg.

    model.component("comp1").geom("geom1").feature("wp1").geom().feature("c1").set("r", "w_wg");

//    In the Work Plane toolbar, click Circle.

    model.component("comp1").geom("geom1").feature("wp1").geom().run("c1");
    model.component("comp1").geom("geom1").feature("wp1").geom().create("c2", "Circle");

//    In the Settings window for Circle, locate the Size and Shape section.
//    In the Radius text field, type 2*w_wg.

    model.component("comp1").geom("geom1").feature("wp1").geom().feature("c2").set("r", "2*w_wg");

//    In the Sector angle text field, type 90.

    model.component("comp1").geom("geom1").feature("wp1").geom().feature("c2").set("angle", 90);

//    Locate the Rotation Angle section.
//    In the Rotation text field, type -90.

    model.component("comp1").geom("geom1").feature("wp1").geom().feature("c2").set("rot", -90);

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

    model.component("comp1").geom("geom1").feature("wp1").geom().run("c2");
    model.component("comp1").geom("geom1").feature("wp1").geom().create("dif1", "Difference");

//    Select the object c2.

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

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

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

//    In the Work Plane toolbar, click Rectangle.

    model.component("comp1").geom("geom1").feature("wp1").geom().run("dif1");
    model.component("comp1").geom("geom1").feature("wp1").geom().create("r1", "Rectangle");

//    In the Settings window for Rectangle, locate the Size and Shape section.
//    In the Width text field, type w_wg.

    model.component("comp1").geom("geom1").feature("wp1").geom().feature("r1").set("size", new String[]{"w_wg", "1"});

//    In the Height text field, type l_wg.

    model.component("comp1").geom("geom1").feature("wp1").geom().feature("r1")
         .set("size", new String[]{"w_wg", "l_wg"});

//    Locate the Position section.
//    In the xw text field, type w_wg.

    model.component("comp1").geom("geom1").feature("wp1").geom().feature("r1").set("pos", new String[]{"w_wg", "0"});

//    In the Work Plane toolbar, click Rectangle.

    model.component("comp1").geom("geom1").feature("wp1").geom().run("r1");
    model.component("comp1").geom("geom1").feature("wp1").geom().create("r2", "Rectangle");

//    In the Settings window for Rectangle, locate the Size and Shape section.
//    In the Width text field, type l_wg.

    model.component("comp1").geom("geom1").feature("wp1").geom().feature("r2").set("size", new String[]{"l_wg", "1"});

//    In the Height text field, type w_wg.

    model.component("comp1").geom("geom1").feature("wp1").geom().feature("r2")
         .set("size", new String[]{"l_wg", "w_wg"});

//    Locate the Position section.
//    In the xw text field, type -l_wg.

    model.component("comp1").geom("geom1").feature("wp1").geom().feature("r2").set("pos", new String[]{"-l_wg", "0"});

//    In the yw text field, type -2*w_wg.

    model.component("comp1").geom("geom1").feature("wp1").geom().feature("r2")
         .set("pos", new String[]{"-l_wg", "-2*w_wg"});

//    In the Work Plane toolbar, click Build All.

    model.component("comp1").geom("geom1").feature("wp1").geom().run("r2");

//    In the Model Builder window, right-click Component 1 (comp1) > Geometry 1 > Work Plane 1 (wp1) and choose Extrude.

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

//    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", "h_wg", 0);

//    Click Build All Objects.

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

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

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

//    In the Model Builder window, under Component 1 (comp1) > Electromagnetic Waves, Frequency Domain (emw), click Wave Equation, Electric 1.
//    In the Settings window for Wave Equation, Electric, locate the Electric Displacement Field section.
//    From the Electric displacement field model list, select Refractive index.

    model.component("comp1").physics("emw").feature("wee1").set("DisplacementFieldModel", "RefractiveIndex");

//    In the Physics toolbar, click Boundaries and choose Port.

    model.component("comp1").physics("emw").create("port1", "Port", 2);

//    Select Boundary 1.

    model.component("comp1").physics("emw").feature("port1").selection().set(1);

//    In the Settings window for Port, locate the Port Properties section.
//    From the Type of port list, select Rectangular.

    model.component("comp1").physics("emw").feature("port1").set("PortType", "Rectangular");

//    For the first port, wave excitation is <l>on</l> by default.
//    In the Physics toolbar, click Boundaries and choose Port.

    model.component("comp1").physics("emw").create("port2", "Port", 2);

//    Select Boundary 15.

    model.component("comp1").physics("emw").feature("port2").selection().set(15);

//    In the Settings window for Port, locate the Port Properties section.
//    From the Type of port list, select Rectangular.

    model.component("comp1").physics("emw").feature("port2").set("PortType", "Rectangular");

//    The default boundary condition is perfect electric conductor, which is fine for all exterior boundaries except the ports. The software automatically imposes continuity on interior boundaries.
//    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, type Air in the Label text field.

    model.component("comp1").material("mat1").label("Air");

//    Select Domains 1, 3.

    model.component("comp1").material("mat1").selection().set(1, 3);

//    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"});

//    Right-click Materials and choose Blank Material.

    model.component("comp1").material().create("mat2", "Common");

//    In the Settings window for Material, type Silica Glass in the Label text field.

    model.component("comp1").material("mat2").label("Silica Glass");

//    Select Domain 2.

    model.component("comp1").material("mat2").selection().set(2);

//    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[]{"1.44"});

//    In the Model Builder window, right-click Component 1 (comp1) > Mesh 1 and choose Build All.

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

//    If you look closely at the mesh, you can see that it is indeed a bit finer in the bend than elsewhere.
//    In the Study toolbar, click Compute.

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

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

    model.sol("sol1").runAll();

    model.result().create("pg2", "PlotGroup3D");
    model.result("pg2").set("data", "dset1");
    model.result("pg2").setIndex("looplevel", 49, 0);
    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", "dset1");
    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.S21dB"});
    model.result("pg3").feature("glob1").set("descr", new String[]{"S11", "S21"});
    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", "freq");
    model.result("pg3").feature("glob1").set("xdataunit", "GHz");
    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", "dset1");
    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", "dset1");
    model.result("pg5").label("Electric Field, Logarithmic (emw)");
    model.result("pg5").selection().geom("geom1", 3);
    model.result("pg5").selection().set(1, 2, 3);
    model.result("pg5").set("applyselectiontodatasetedges", true);

    model.component("comp1").measure().selection().geom(2);
    model.component("comp1").measure().selection().set(2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 16);
    model.component("comp1").measure().selection().geom(2);
    model.component("comp1").measure().selection().set(1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16);

    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(2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 16);
    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.85);
    model.result("pg5").feature("surf1").create("mtrl1", "MaterialAppearance");
    model.result("pg5").feature("surf1").feature("mtrl1").set("appearance", "custom");
    model.result("pg5").feature("surf1").feature("mtrl1").set("family", "aluminumpolished");
    model.result("pg5").feature("surf1").set("expr", "1");
    model.result("pg5").create("surf2", "Surface");
    model.result("pg5").feature("surf2").set("expr", "emw.normE");
    model.result("pg5").feature("surf2").create("sel1", "Selection");
    model.result("pg5").feature("surf2").feature("sel1").selection().set(6, 12);
    model.result("pg5").feature("surf2").set("colortable", "Dipole");
    model.result("pg5").feature("surf2").set("colorscalemode", "logarithmic");
    model.result("pg5").feature("surf2").create("tran1", "Transparency");
    model.result("pg5").feature("surf2").feature("tran1").set("transparency", 0.3);
    model.result("pg5").create("surf3", "Surface");
    model.result("pg5").feature("surf3").set("expr", "emw.normE");
    model.result("pg5").feature("surf3").create("sel1", "Selection");
    model.result("pg5").feature("surf3").feature("sel1").selection().set(1, 15);
    model.result("pg5").feature("surf3").set("colortable", "Dipole");
    model.result("pg5").feature("surf3").set("colorscalemode", "logarithmic");
    model.result("pg5").feature("surf3").create("tran1", "Transparency");
    model.result("pg5").feature("surf3").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[]{-0.3, -0.4, 0.3});
    model.component("comp1").view("view3").set("environmentmap", "Indoor");
    model.component("comp1").view("view3").camera().set("zoomanglefull", 31.6);

    model.result("pg5").set("view", "view3");
    model.result("pg2").run();

//    The default plot shows the distribution of the electric field norm on slices of the waveguide, for the highest frequency in the sweep. Note the wave pattern in the bend and the rectangular input section. This indicates standing waves caused by reflections in the bend. In contrast, the pattern beyond the bend is independent of the <eqv>y</eqv>-coordinate, showing that the output port does a good job of transmitting the wave.
//    An S-parameter plot gives you a quantitative measure of how much of the wave is transmitted and reflected at different frequencies.

    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, click to expand the Coloring and Style section.
//    Find the Line markers subsection.
//    From the Marker list, select Cycle.

    model.result("pg3").feature("glob1").set("linemarker", "cycle");

//    From the Positioning list, select Interpolated.

    model.result("pg3").feature("glob1").set("markerpos", "interp");

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

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

//    In the Model Builder window, under Results, click Smith Plot (emw).

    model.result("pg5").set("applyselectiontodatasetedges", true);
    model.result("pg5").run();

//    In the Model Builder window, click Electric Field, Logarithmic (emw).

    model.result("pg2").set("applyselectiontodatasetedges", false);
    model.result("pg2").run();

//    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 (freq (GHz)) list, select 5.1.

    model.result("pg2").setIndex("looplevel", 45, 0);

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

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

//    The standing wave pattern still remains in the bend, but at this frequency it is almost completely gone in the input section.
//    For an alternative view, you can plot the instantaneous value of the electric field inside the waveguide. Only the <eqv>z</eqv> component will be substantially nonzero. For a better view, add also deformation. Replace the Multislice with a single horizontal slice plot.

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

//    In the Model Builder window, expand the Electric Field (emw) node.
//    Right-click Multislice 1 and choose Delete.

    model.result("pg2").feature().remove("mslc1");
    model.result("pg2").run();

//    Right-click Electric Field (emw) and choose Slice.

    model.result("pg2").create("slc1", "Slice");
    model.result("pg2").feature("slc1").set("evaluationsettings", "parent");

//    In the Settings window for Slice, click Replace Expression in the upper-right corner of the Expression section.
//    From the menu, choose Component 1 (comp1) > Electromagnetic Waves, Frequency Domain > Electric > Electric field - V/m > emw.Ez - Electric field, z-component.

    model.result("pg2").feature("slc1").set("expr", "emw.Ez");
    model.result("pg2").feature("slc1").set("descr", "Electric field, z-component");

//    Locate the Plane Data section.
//    From the Plane list, select XY-planes.

    model.result("pg2").feature("slc1").set("quickplane", "xy");

//    From the Entry method list, select Coordinates.

    model.result("pg2").feature("slc1").set("quickzmethod", "coord");

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

    model.result("pg2").feature("slc1").set("colortable", "Wave");

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

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

//    The Wave color table looks its best using a symmetric range. You can also play with a deformed shape plot to make the waves appear more clearly.
//    Click to expand the Range section.
//    Locate the Coloring and Style section.
//    From the Scale list, select Linear symmetric.

    model.result("pg2").feature("slc1").set("colorscalemode", "linearsymmetric");

//    Right-click Slice 1 and choose Deformation.

    model.result("pg2").feature("slc1").create("def1", "Deform");
    model.result("pg2").run();

//    In the Settings window for Deformation, click Replace Expression in the upper-right corner of the Expression section.
//    From the menu, choose Component 1 (comp1) > Electromagnetic Waves, Frequency Domain > Electric > emw.Ex,emw.Ey,emw.Ez - Electric field.

    model.result("pg2").feature("slc1").feature("def1").set("expr", new String[]{"emw.Ex", "emw.Ey", "emw.Ez"});
    model.result("pg2").feature("slc1").feature("def1").set("descr", "Electric field");

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

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

//    The remaining instructions show you how to systematically solve with one port active at a time, and save the results in the Touchstone format.
//    In the Model Builder window, under Component 1 (comp1), click Electromagnetic Waves, Frequency Domain (emw).
//    In the Settings window for Electromagnetic Waves, Frequency Domain, locate the Port Sweep Settings section.
//    Select the Use manual port sweep checkbox.

    model.component("comp1").physics("emw").prop("PortSweepSettings").set("useSweep", true);

//    Click Configure Sweep Settings.

    model.param("default").setShowInParamSel(true);
    model.param().set("PortName", "1");

    model.study("std1").create("param1", "Parametric");
    model.study("std1").feature("param1").set("pname", "PortName");
    model.study("std1").feature("param1").set("plistarr", "1 2");

//    . By clicking the <l>Configure Sweep Settings</l> button, all necessary port sweep settings such as sweep parameter and parametric study step will be automatically added. It is necessary to run the parametric sweep with port names to get a full S-parameter matrix.
//    Select the Export Touchstone file checkbox.

    model.component("comp1").physics("emw").prop("PortSweepSettings").set("ExportTouchstone", true);

//    Click the <l>Browse</l> button and select a file to which you want to export the results in the Touchstone format. If the file does not exist, it will be created.
//    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_sweep_probe").genResult("none");

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

    model.result().create("pg6", "PlotGroup3D");
    model.result("pg6").set("data", "dset3");
    model.result("pg6").setIndex("looplevel", 49, 0);
    model.result("pg6").setIndex("looplevel", 2, 1);
    model.result("pg6").create("mslc1", "Multislice");
    model.result("pg6").feature("mslc1").set("expr", new String[]{"emw.normE"});
    model.result("pg6").set("showlegendsmaxmin", true);
    model.result("pg6").feature("mslc1").set("colortable", "RainbowLight");
    model.result("pg6").label("Electric Field (emw) 1");
    model.result().create("pg7", "PlotGroup1D");
    model.result("pg7").set("data", "dset3");
    model.result("pg7").create("glob1", "Global");
    model.result("pg7").feature("glob1").set("unit", new String[]{"", "", "", ""});
    model.result("pg7").feature("glob1")
         .set("expr", new String[]{"emw.S11dB", "emw.S12dB", "emw.S21dB", "emw.S22dB"});
    model.result("pg7").feature("glob1").set("descr", new String[]{"S11", "S12", "S21", "S22"});
    model.result("pg7").label("S-Parameter (emw) 1");
    model.result("pg7").feature("glob1").set("titletype", "none");
    model.result("pg7").feature("glob1").set("xdata", "expr");
    model.result("pg7").set("ylabelactive", true);
    model.result("pg7").set("ylabel", "S-parameter (dB)");
    model.result("pg7").feature("glob1").set("xdataexpr", "freq");
    model.result("pg7").feature("glob1").set("xdataunit", "GHz");
    model.result("pg7").feature("glob1").set("markerpos", "datapoints");
    model.result("pg7").feature("glob1").set("xdatasolnumtype", "all");
    model.result("pg7").feature("glob1").active(true);
    model.result().create("pg8", "SmithGroup");
    model.result("pg8").set("data", "dset3");
    model.result("pg8").create("rgr1", "ReflectionGraph");
    model.result("pg8").feature("rgr1").set("unit", new String[]{"", ""});
    model.result("pg8").feature("rgr1").set("expr", new String[]{"emw.S11", "emw.S22"});
    model.result("pg8").feature("rgr1").set("descr", new String[]{"S11", "S22"});
    model.result("pg8").label("Smith Plot (emw) 1");
    model.result("pg8").feature("rgr1").set("titletype", "manual");
    model.result("pg8").feature("rgr1").set("title", "Reflection Graph: S-parameter, Color: Frequency (GHz)");
    model.result("pg8").feature("rgr1").set("linemarker", "point");
    model.result("pg8").feature("rgr1").set("markerpos", "datapoints");
    model.result("pg8").feature("rgr1").create("col1", "Color");
    model.result("pg8").feature("rgr1").feature("col1").set("expr", "emw.freq/1e9");
    model.result("pg8").feature("rgr1").feature("col1").set("colortable", "Spectrum");
    model.result().create("pg9", "PlotGroup3D");
    model.result("pg9").set("data", "dset3");
    model.result("pg9").label("Electric Field, Logarithmic (emw) 1");
    model.result("pg9").selection().geom("geom1", 3);
    model.result("pg9").selection().set(1, 2, 3);
    model.result("pg9").set("applyselectiontodatasetedges", true);

    model.component("comp1").measure().selection().geom(2);
    model.component("comp1").measure().selection().set(2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 16);
    model.component("comp1").measure().selection().geom(2);
    model.component("comp1").measure().selection().set(1, 2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 15, 16);

    model.result("pg9").create("surf1", "Surface");
    model.result("pg9").feature("surf1").set("expr", "emw.normE");
    model.result("pg9").feature("surf1").create("sel1", "Selection");
    model.result("pg9").feature("surf1").feature("sel1").selection().set(2, 3, 4, 5, 7, 8, 9, 10, 11, 13, 14, 16);
    model.result("pg9").feature("surf1").set("colortable", "Dipole");
    model.result("pg9").feature("surf1").set("colorscalemode", "logarithmic");
    model.result("pg9").feature("surf1").create("tran1", "Transparency");
    model.result("pg9").feature("surf1").feature("tran1").set("transparency", 0.85);
    model.result("pg9").feature("surf1").create("mtrl1", "MaterialAppearance");
    model.result("pg9").feature("surf1").feature("mtrl1").set("appearance", "custom");
    model.result("pg9").feature("surf1").feature("mtrl1").set("family", "aluminumpolished");
    model.result("pg9").feature("surf1").set("expr", "1");
    model.result("pg9").create("surf2", "Surface");
    model.result("pg9").feature("surf2").set("expr", "emw.normE");
    model.result("pg9").feature("surf2").create("sel1", "Selection");
    model.result("pg9").feature("surf2").feature("sel1").selection().set(6, 12);
    model.result("pg9").feature("surf2").set("colortable", "Dipole");
    model.result("pg9").feature("surf2").set("colorscalemode", "logarithmic");
    model.result("pg9").feature("surf2").create("tran1", "Transparency");
    model.result("pg9").feature("surf2").feature("tran1").set("transparency", 0.3);
    model.result("pg9").create("surf3", "Surface");
    model.result("pg9").feature("surf3").set("expr", "emw.normE");
    model.result("pg9").feature("surf3").create("sel1", "Selection");
    model.result("pg9").feature("surf3").feature("sel1").selection().set(1, 15);
    model.result("pg9").feature("surf3").set("colortable", "Dipole");
    model.result("pg9").feature("surf3").set("colorscalemode", "logarithmic");
    model.result("pg9").feature("surf3").create("tran1", "Transparency");
    model.result("pg9").feature("surf3").feature("tran1").set("transparency", 0.7);

    model.component("comp1").view().create("view4", "geom1");
    model.component("comp1").view("view4").camera().set("position", new double[]{-0.3, -0.4, 0.3});
    model.component("comp1").view("view4").set("environmentmap", "Indoor");
    model.component("comp1").view("view4").camera().set("zoomanglefull", 31.6);

    model.result("pg9").set("view", "view4");
    model.result("pg6").run();

//    The Touchstone file should now contain the complete output from the model. The new solution dataset contains two frequency sweeps, one for each port.

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

//    As you can see, after performing the parametric sweep over the ports, the S-parameter plot you created previously is empty. To restore the plot, you need to change the dataset and specify the inner parameter - that is, the frequency - as the quantity to display along the horizontal axis.
//    In the Model Builder window, under Results > S-Parameter (emw), click Global 1.
//    In the Settings window for Global, locate the Data section.
//    From the Dataset list, select Probe Solution 2 (sol1).

    model.result("pg3").feature("glob1").set("data", "dset2");

//    To verify the reciprocity of the waveguide, you can add the S-parameters S12dB and S22dB to the Expressions table and change the parameter selection for PortName:
//    Click Add Expression in the upper-right corner of the y-Axis Data section.
//    From the menu, choose Component 1 (comp1) > Electromagnetic Waves, Frequency Domain > Ports > S-parameter, dB - dB > emw.S12dB - S12.

    model.result("pg3").feature("glob1").set("expr", new String[]{"emw.S11dB", "emw.S21dB", "emw.S12dB"});
    model.result("pg3").feature("glob1").set("descr", new String[]{"S11", "S21", "S12"});

//    Click Add Expression in the upper-right corner of the y-Axis Data section.
//    From the menu, choose Component 1 (comp1) > Electromagnetic Waves, Frequency Domain > Ports > S-parameter, dB - dB > emw.S22dB - S22.

    model.result("pg3").feature("glob1")
         .set("expr", new String[]{"emw.S11dB", "emw.S21dB", "emw.S12dB", "emw.S22dB"});
    model.result("pg3").feature("glob1").set("descr", new String[]{"S11", "S21", "S12", "S22"});

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

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

//    In the Model Builder window, under Results, click Smith Plot (emw).
//    In the Settings window for Smith Plot Group, locate the Data section.
//    From the Dataset list, select Study 1/Parametric Solutions 1 (sol2).

    model.result("pg4").set("data", "dset3");

//    From the Parameter selection (PortName) list, select Last.

    model.result("pg4").setIndex("looplevelinput", "last", 1);
    model.result("pg4").run();

//    In the Model Builder window, expand the Smith Plot (emw) node, then click Reflection Graph 1.
//    In the Settings window for Reflection Graph, click Replace Expression in the upper-right corner of the Expression section.
//    From the menu, choose Component 1 (comp1) > Electromagnetic Waves, Frequency Domain > Ports > S-parameter > emw.S22 - S22.

    model.result("pg4").feature("rgr1").set("expr", new String[]{"emw.S22"});
    model.result("pg4").feature("rgr1").set("descr", new String[]{"S22"});
    model.result("pg4").feature("rgr1").set("unit", new String[]{"1"});

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

    model.result("pg4").run();
    model.result("pg8").run();
    model.result("pg8").run();

//    In the Model Builder window, expand the Smith Plot (emw) 1 node, then click Reflection Graph 1.
//    In the Settings window for Reflection Graph, click to expand the Coloring and Style section.

    return model;
  }

  public static Model run2(Model model) {
//    Find the Line markers subsection.
//    From the Marker list, select Cycle.

    model.result("pg8").feature("rgr1").set("linemarker", "cycle");

//    From the Positioning list, select Interpolated.

    model.result("pg8").feature("rgr1").set("markerpos", "interp");

//    Finish by verifying the reciprocity of the waveguide on the Smith plot.

    model.title("H-Bend Waveguide 3D");

    model
         .description("The transmission of a TE10\u00a0wave through a 90\u00a0degree bend in a waveguide is modeled in 3D.");

    return model;
  }

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

}
