/*
 * h_bend_waveguide_2d.java
 */

import com.comsol.model.*;
import com.comsol.model.util.*;

/** Model exported on May 15 2026, 11:21 by COMSOL 6.4.0.421. */
public class h_bend_waveguide_2d {

  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 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", 2);

    model.component("comp1").mesh().create("mesh1");

    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 Geometry toolbar, click Circle.

    model.component("comp1").geom("geom1").create("c1", "Circle");

//    In the Settings window for Circle, locate the Size and Shape section.
//    In the Radius text field, type 0.08.

    model.component("comp1").geom("geom1").feature("c1").set("r", 0.08);

//    In the Geometry toolbar, click Circle.

    model.component("comp1").geom("geom1").run("c1");
    model.component("comp1").geom("geom1").create("c2", "Circle");

//    In the Settings window for Circle, locate the Size and Shape section.
//    In the Radius text field, type 0.04.

    model.component("comp1").geom("geom1").feature("c2").set("r", 0.04);

//    In the Geometry toolbar, click Square.

    model.component("comp1").geom("geom1").run("c2");
    model.component("comp1").geom("geom1").create("sq1", "Square");

//    In the Settings window for Square, locate the Size section.
//    In the Side length text field, type 0.08.

    model.component("comp1").geom("geom1").feature("sq1").set("size", 0.08);

//    Locate the Position section.
//    In the y text field, type -0.08.

    model.component("comp1").geom("geom1").feature("sq1").set("pos", new double[]{0, -0.08});

//    In the Geometry toolbar, click Booleans and Partitions and choose Compose.

    model.component("comp1").geom("geom1").run("sq1");
    model.component("comp1").geom("geom1").create("co1", "Compose");

//    Click in the Graphics window and then press Ctrl+A to select all objects.

    model.component("comp1").geom("geom1").feature("co1").selection("input").set("c1", "c2", "sq1");

//    In the Settings window for Compose, locate the Compose section.
//    In the Set formula text field, type sq1*(c1-c2).

    model.component("comp1").geom("geom1").feature("co1").set("formula", "sq1*(c1-c2)");

//    In the Geometry toolbar, click Rectangle.

    model.component("comp1").geom("geom1").run("co1");
    model.component("comp1").geom("geom1").create("r1", "Rectangle");

//    In the Settings window for Rectangle, locate the Size and Shape section.
//    In the Width text field, type 0.04.

    model.component("comp1").geom("geom1").feature("r1").set("size", new double[]{0.04, 1});

//    In the Height text field, type 0.1.

    model.component("comp1").geom("geom1").feature("r1").set("size", new double[]{0.04, 0.1});

//    Locate the Position section.
//    In the x text field, type 0.04.

    model.component("comp1").geom("geom1").feature("r1").set("pos", new double[]{0.04, 0});

//    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, locate the Size and Shape section.
//    In the Width text field, type 0.1.

    model.component("comp1").geom("geom1").feature("r2").set("size", new double[]{0.1, 1});

//    In the Height text field, type 0.04.

    model.component("comp1").geom("geom1").feature("r2").set("size", new double[]{0.1, 0.04});

//    Locate the Position section.
//    In the x text field, type -0.1.

    model.component("comp1").geom("geom1").feature("r2").set("pos", new double[]{-0.1, 0});

//    In the y text field, type -0.08.

    model.component("comp1").geom("geom1").feature("r2").set("pos", new double[]{-0.1, -0.08});

//    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, 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);

//    Click to expand the Material Properties section.
//    In the Material properties tree, select Electromagnetic Models > Refractive Index > Refractive index, real part (n).
//    Click Add to Material.

    model.component("comp1").material("mat1").propertyGroup()
         .create("RefractiveIndex", "RefractiveIndex", "Refractive_index");

//    Locate the Material Contents section.
//    In the table, enter the following settings:

    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 Properties section.
//    In the Material properties tree, select Electromagnetic Models > Refractive Index > Refractive index, real part (n).
//    Click Add to Material.

    model.component("comp1").material("mat2").propertyGroup()
         .create("RefractiveIndex", "RefractiveIndex", "Refractive_index");

//    Locate the Material Contents section.
//    In the table, enter the following settings:

    model.component("comp1").material("mat2").propertyGroup("RefractiveIndex").set("n", new String[]{"1.44"});

//    With TE waves, only the <eqv>z</eqv> component of the electric field needs to be solved for.
//    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 Components section.
//    From the Electric field components solved for list, select Out-of-plane vector.

    model.component("comp1").physics("emw").prop("components").set("components", "outofplane");

//    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");

//    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 Physics toolbar, click Boundaries and choose Port.

    model.component("comp1").physics("emw").create("port1", "Port", 1);

//    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", 1);

//    Select Boundary 7.

    model.component("comp1").physics("emw").feature("port2").selection().set(7);

//    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");

//    In the Model Builder window, right-click Component 1 (comp1) > Mesh 1 and choose Build All.

    model.component("comp1").mesh("mesh1").run();

//    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", "PlotGroup2D");
    model.result("pg2").set("data", "dset1");
    model.result("pg2").setIndex("looplevel", 49, 0);
    model.result("pg2").create("surf1", "Surface");
    model.result("pg2").feature("surf1").set("expr", "emw.normE");
    model.result("pg2").feature("surf1").set("descr", "Electric field norm");
    model.result("pg2").set("showlegendsmaxmin", true);
    model.result("pg2").feature("surf1").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("pg2").run();

//    The default plot shows the norm of the electric field for the highest frequency, 5.2 GHz. To verify that the solution resembles the 3D version, try plotting a frequency where you expect a transmission peak.
//    In the Settings window for 2D Plot Group, locate the Data section.
//    From the Parameter value (freq (GHz)) list, select 4.225.

    model.result("pg2").setIndex("looplevel", 10, 0);

//    In the Electric Field (emw) toolbar, click Plot.

    model.result("pg2").run();

//    Finally, plot the S-parameters.

    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).
//    Next, add a study step to solve a frequency domain study sweeping over ports to extract a full S-parameter matrix.
//    In the Home toolbar, click Add Study to open the Add Study window.
//    Find the Studies subsection.
//    In the Select Study tree, select Preset Studies for Selected Physics Interfaces > Frequency Domain Source Sweep.
//    Click Add Study in the window toolbar.

    model.study().create("std2");
    model.study("std2").create("fdss", "FrequencyDomainSourceSweep");
    model.study("std2").feature("fdss").set("plotgrouparr", new String[]{"Default"});
    model.study("std2").feature("fdss").set("solnum", "auto");
    model.study("std2").feature("fdss").set("notsolnum", "auto");
    model.study("std2").feature("fdss").set("outputmap", new String[]{});
    model.study("std2").feature("fdss").set("ngenAUX", "1");
    model.study("std2").feature("fdss").set("goalngenAUX", "1");
    model.study("std2").feature("fdss").set("ngenAUX", "1");
    model.study("std2").feature("fdss").set("goalngenAUX", "1");

//    In the Home toolbar, click Add Study to close the Add Study window.
//    In the Settings window for Frequency Domain Source Sweep, locate the Study Settings section.
//    In the Frequencies text field, type 5.1[GHz].

    model.study("std2").feature("fdss").set("plist", "5.1[GHz]");

//    In the Study toolbar, click Compute.

    model.study("std2").createAutoSequences("all");

    model.component("comp1").probe("emw_port1_probe").genResult("none");

    model.sol("sol2").runAll();

    model.result().create("pg5", "PlotGroup2D");
    model.result("pg5").set("data", "dset3");
    model.result("pg5").setIndex("looplevel", 2, 0);
    model.result("pg5").setIndex("looplevel", 1, 1);
    model.result("pg5").create("surf1", "Surface");
    model.result("pg5").feature("surf1").set("expr", "emw.normE");
    model.result("pg5").feature("surf1").set("descr", "Electric field norm");
    model.result("pg5").set("showlegendsmaxmin", true);
    model.result("pg5").feature("surf1").set("colortable", "RainbowLight");
    model.result("pg5").label("Electric Field (emw) 1");
    model.result().numerical().create("gmev1", "EvalGlobalMatrix");
    model.result().numerical("gmev1").label("S-Parameter (emw)");
    model.result().numerical("gmev1").set("data", "dset3");
    model.result().numerical("gmev1").set("expr", "emw.SdB");
    model.result().numerical("gmev1").set("dataseries", "sum");
    model.result().table().create("tbl2", "Table");
    model.result().numerical("gmev1").set("table", "tbl2");
    model.result().numerical("gmev1").run();
    model.result().numerical("gmev1").setResult();
    model.result("pg5").run();

//    Review the evaluated full S-parameter matrix. Since the device is passive and reciprocal, the computed matrix is also symmetric.

    model.title("H-Bend Waveguide 2D");

    model
         .description("The transmission of a TE10\u00a0wave through a 90\u00a0degree bend in a waveguide is modeled in 2D.");

    return model;
  }

  public static void main(String[] args) {
    run();
  }

}
