/*
 * mbb_beam_optimization.java
 */

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

/** Model exported on May 15 2026, 07:50 by COMSOL 6.4.0.421. */
public class mbb_beam_optimization {

  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 Structural Mechanics > Solid Mechanics (solid).
//    Click Add.
//    Click Study.
//    In the Select Study tree, select Preset Studies for Selected Physics Interfaces > Optimization > Topology Optimization, Stationary.
//    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("solid", "SolidMechanics", "geom1");

    model.study().create("std1");
    model.study("std1").create("stat", "Stationary");
    model.study("std1").create("topo", "TopologyOptimization");

    model.component("comp1").common().create("dtopo1", "DensityTopology");
    model.component("comp1").common("dtopo1").selection().all();

    model.component("comp1").material().create("toplnk1", "TopologyLink");
    model.component("comp1").material("toplnk1").set("topologySource", "dtopo1");
    model.component("comp1").material("toplnk1").selection().all();

    model.component("comp1").physics("solid").prop("ShapeProperty").set("order_displacement", 1);

    model.study("std1").feature("topo").setIndex("optobj", "comp1.solid.Ws_tot", 0);
    model.study("std1").feature("topo").set("objectivescaling", "init");
    model.study("std1").feature("topo").setIndex("constraintExpression", "comp1.dtopo1.theta_avg", 0);
    model.study("std1").feature("topo").setIndex("constraintUbound", "0.5", 0);

//    In the Model Builder window, expand the Component 1 (comp1) > Definitions node, then click Global Definitions > Parameters 1.
//    In the Settings window for Parameters, locate the Parameters section.
//    In the table, enter the following settings:

    model.param().set("a", "3[m]");
    model.param().descr("a", "Beam half width");
    model.param().set("b", "1[m]");
    model.param().descr("b", "Beam height");
    model.param().set("L1", "0.1[m]");
    model.param().descr("L1", "Support width");
    model.param().set("meshsz", "6[cm]");
    model.param().descr("meshsz", "Mesh size");
    model.param().set("volfrac", "0.5");
    model.param().descr("volfrac", "Maximum volume fraction");
    model.param().set("beta", "1");
    model.param().descr("beta", "Projection slope");

//    In the Geometry toolbar, click Rectangle.

    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 a.

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

//    In the Height text field, type b.

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

//    In the Geometry toolbar, click Point.

    model.component("comp1").geom("geom1").run("r1");
    model.component("comp1").geom("geom1").create("pt1", "Point");

//    In the Settings window for Point, locate the Point section.
//    In the y text field, type L1.

    model.component("comp1").geom("geom1").feature("pt1").setIndex("p", "L1", 1);

//    In the Geometry toolbar, click Point.

    model.component("comp1").geom("geom1").run("pt1");
    model.component("comp1").geom("geom1").create("pt2", "Point");

//    In the Settings window for Point, locate the Point section.
//    In the x text field, type a-L1/2.

    model.component("comp1").geom("geom1").feature("pt2").setIndex("p", "a-L1/2", 0);

//    In the y text field, type b.

    model.component("comp1").geom("geom1").feature("pt2").setIndex("p", "b", 1);

//    In the Geometry toolbar, click Build All.

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

//    In the Geometry toolbar, click Selections and choose Box Selection.

    model.component("comp1").geom("geom1").create("boxsel1", "BoxSelection");

//    In the Settings window for Box Selection, type Load Boundary in the Label text field.

    model.component("comp1").geom("geom1").feature("boxsel1").label("Load Boundary");

//    Locate the Geometric Entity Level section.
//    From the Level list, select Boundary.

    model.component("comp1").geom("geom1").feature("boxsel1").set("entitydim", 1);

//    Locate the Box Limits section.
//    In the x minimum text field, type a-L1/1.999.

    model.component("comp1").geom("geom1").feature("boxsel1").set("xmin", "a-L1/1.999");

//    In the y minimum text field, type b*0.999.

    model.component("comp1").geom("geom1").feature("boxsel1").set("ymin", "b*0.999");

//    Locate the Output Entities section.
//    From the Include entity if list, select Entity inside box.

    model.component("comp1").geom("geom1").feature("boxsel1").set("condition", "inside");

//    In the Geometry toolbar, click Selections and choose Box Selection.

    model.component("comp1").geom("geom1").run("boxsel1");
    model.component("comp1").geom("geom1").create("boxsel2", "BoxSelection");

//    In the Settings window for Box Selection, type Sliding Support in the Label text field.

    model.component("comp1").geom("geom1").feature("boxsel2").label("Sliding Support");

//    Locate the Geometric Entity Level section.
//    From the Level list, select Boundary.

    model.component("comp1").geom("geom1").feature("boxsel2").set("entitydim", 1);

//    Locate the Box Limits section.
//    In the x maximum text field, type a*0.001.

    model.component("comp1").geom("geom1").feature("boxsel2").set("xmax", "a*0.001");

//    In the y maximum text field, type L1*1.001.

    model.component("comp1").geom("geom1").feature("boxsel2").set("ymax", "L1*1.001");

//    Locate the Output Entities section.
//    From the Include entity if list, select Entity inside box.

    model.component("comp1").geom("geom1").feature("boxsel2").set("condition", "inside");

//    In the Geometry toolbar, click Selections and choose Box Selection.

    model.component("comp1").geom("geom1").run("boxsel2");
    model.component("comp1").geom("geom1").create("boxsel3", "BoxSelection");

//    In the Settings window for Box Selection, type Symmetry/Roller in the Label text field.

    model.component("comp1").geom("geom1").feature("boxsel3").label("Symmetry/Roller");

//    Locate the Geometric Entity Level section.
//    From the Level list, select Boundary.

    model.component("comp1").geom("geom1").feature("boxsel3").set("entitydim", 1);

//    Locate the Box Limits section.
//    In the x minimum text field, type a*0.999.

    model.component("comp1").geom("geom1").feature("boxsel3").set("xmin", "a*0.999");

//    Locate the Output Entities section.
//    From the Include entity if list, select Entity inside box.

    model.component("comp1").geom("geom1").feature("boxsel3").set("condition", "inside");

//    In the Geometry toolbar, click Build All.

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

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

    model.material().create("mat1", "Common", "");
    model.material("mat1").propertyGroup().create("Enu", "Enu", "Young's modulus and Poisson's ratio");
    model.material("mat1").propertyGroup("Enu").func().create("int1", "Interpolation");
    model.material("mat1").propertyGroup("Enu").func().create("int2", "Interpolation");
    model.material("mat1").propertyGroup().create("Murnaghan", "Murnaghan", "Murnaghan");
    model.material("mat1").propertyGroup()
         .create("ElastoplasticModel", "ElastoplasticModel", "Elastoplastic material model");
    model.material("mat1").propertyGroup("ElastoplasticModel").func().create("int1", "Interpolation");
    model.material("mat1").propertyGroup().create("Ludwik", "Ludwik", "Ludwik");
    model.material("mat1").propertyGroup("Ludwik").func().create("int1", "Interpolation");
    model.material("mat1").propertyGroup().create("JohnsonCook", "JohnsonCook", "Johnson-Cook");
    model.material("mat1").propertyGroup().create("Swift", "Swift", "Swift");
    model.material("mat1").propertyGroup().create("Voce", "Voce", "Voce");
    model.material("mat1").propertyGroup("Voce").func().create("int1", "Interpolation");
    model.material("mat1").propertyGroup().create("HockettSherby", "HockettSherby", "Hockett-Sherby");
    model.material("mat1").propertyGroup("HockettSherby").func().create("int1", "Interpolation");
    model.material("mat1").propertyGroup().create("ArmstrongFrederick", "ArmstrongFrederick", "Armstrong-Frederick");
    model.material("mat1").propertyGroup("ArmstrongFrederick").func().create("int1", "Interpolation");
    model.material("mat1").propertyGroup().create("Norton", "Norton", "Norton");
    model.material("mat1").propertyGroup().create("Garofalo", "Garofalo", "Garofalo (hyperbolic sine)");
    model.material("mat1").propertyGroup()
         .create("ChabocheViscoplasticity", "ChabocheViscoplasticity", "Chaboche viscoplasticity");
    model.material("mat1").label("Structural steel");
    model.material("mat1").set("family", "custom");
    model.material("mat1")
         .set("customspecular", new double[]{0.7843137254901961, 0.7843137254901961, 0.7843137254901961});
    model.material("mat1").set("diffuse", "custom");
    model.material("mat1")
         .set("customdiffuse", new double[]{0.6666666666666666, 0.6666666666666666, 0.6666666666666666});
    model.material("mat1").set("ambient", "custom");
    model.material("mat1")
         .set("customambient", new double[]{0.6666666666666666, 0.6666666666666666, 0.6666666666666666});
    model.material("mat1").set("noise", true);
    model.material("mat1").set("fresnel", 0.9);
    model.material("mat1").set("roughness", 0.3);
    model.material("mat1").set("diffusewrap", 0);
    model.material("mat1").set("reflectance", 0);
    model.material("mat1").propertyGroup("def").set("lossfactor", "0.02");
    model.material("mat1").propertyGroup("def")
         .set("relpermeability", new String[]{"1", "0", "0", "0", "1", "0", "0", "0", "1"});
    model.material("mat1").propertyGroup("def").set("heatcapacity", "475[J/(kg*K)]");
    model.material("mat1").propertyGroup("def")
         .set("thermalconductivity", new String[]{"44.5[W/(m*K)]", "0", "0", "0", "44.5[W/(m*K)]", "0", "0", "0", "44.5[W/(m*K)]"});
    model.material("mat1").propertyGroup("def")
         .set("electricconductivity", new String[]{"4.032e6[S/m]", "0", "0", "0", "4.032e6[S/m]", "0", "0", "0", "4.032e6[S/m]"});
    model.material("mat1").propertyGroup("def")
         .set("relpermittivity", new String[]{"1", "0", "0", "0", "1", "0", "0", "0", "1"});
    model.material("mat1").propertyGroup("def")
         .set("thermalexpansioncoefficient", new String[]{"12.3e-6[1/K]", "0", "0", "0", "12.3e-6[1/K]", "0", "0", "0", "12.3e-6[1/K]"});
    model.material("mat1").propertyGroup("def").set("density", "7850[kg/m^3]");
    model.material("mat1").propertyGroup("Enu").func("int1").set("funcname", "E");
    model.material("mat1").propertyGroup("Enu").func("int1")
         .set("table", new String[][]{{"293.15", "200e9"}, {"793.15", "166.6e9"}});
    model.material("mat1").propertyGroup("Enu").func("int1").set("extrap", "linear");
    model.material("mat1").propertyGroup("Enu").func("int1").set("fununit", new String[]{"Pa"});
    model.material("mat1").propertyGroup("Enu").func("int1").set("argunit", new String[]{"K"});
    model.material("mat1").propertyGroup("Enu").func("int2").set("funcname", "nu");
    model.material("mat1").propertyGroup("Enu").func("int2")
         .set("table", new String[][]{{"293.15", "0.30"}, {"793.15", "0.315"}});
    model.material("mat1").propertyGroup("Enu").func("int2").set("extrap", "linear");
    model.material("mat1").propertyGroup("Enu").func("int2").set("fununit", new String[]{"1"});
    model.material("mat1").propertyGroup("Enu").func("int2").set("argunit", new String[]{"K"});
    model.material("mat1").propertyGroup("Enu").set("E", "E(T)");
    model.material("mat1").propertyGroup("Enu").set("nu", "nu(T)");
    model.material("mat1").propertyGroup("Enu").addInput("temperature");
    model.material("mat1").propertyGroup("Murnaghan").set("l", "-3.0e11[Pa]");
    model.material("mat1").propertyGroup("Murnaghan").set("m", "-6.2e11[Pa]");
    model.material("mat1").propertyGroup("Murnaghan").set("n", "-7.2e11[Pa]");
    model.material("mat1").propertyGroup("ElastoplasticModel").func("int1").set("funcname", "a");
    model.material("mat1").propertyGroup("ElastoplasticModel").func("int1")
         .set("table", new String[][]{{"600", "1"}, {"1100", "0.1"}, {"1643", "0"}});
    model.material("mat1").propertyGroup("ElastoplasticModel").func("int1").set("fununit", new String[]{"1"});
    model.material("mat1").propertyGroup("ElastoplasticModel").func("int1").set("argunit", new String[]{"K"});
    model.material("mat1").propertyGroup("ElastoplasticModel").set("sigmags", "350[MPa]*a(T)");
    model.material("mat1").propertyGroup("ElastoplasticModel").set("Et", "1.045[GPa]*a(T)");
    model.material("mat1").propertyGroup("ElastoplasticModel").set("Ek", "1.045[GPa]*a(T)");
    model.material("mat1").propertyGroup("ElastoplasticModel").set("sigmagh", "1.050[GPa]*epe*a(T)");
    model.material("mat1").propertyGroup("ElastoplasticModel")
         .set("Hillcoefficients", new String[]{"0[m^2*s^4/kg^2]", "0[m^2*s^4/kg^2]", "0[m^2*s^4/kg^2]", "0[m^2*s^4/kg^2]", "0[m^2*s^4/kg^2]", "0[m^2*s^4/kg^2]"});
    model.material("mat1").propertyGroup("ElastoplasticModel")
         .set("ys", new String[]{"0[N/m^2]", "0[N/m^2]", "0[N/m^2]", "0[N/m^2]", "0[N/m^2]", "0[N/m^2]"});
    model.material("mat1").propertyGroup("ElastoplasticModel").addInput("temperature");
    model.material("mat1").propertyGroup("ElastoplasticModel").addInput("effectiveplasticstrain");
    model.material("mat1").propertyGroup("Ludwik").func("int1").set("funcname", "a");
    model.material("mat1").propertyGroup("Ludwik").func("int1")
         .set("table", new String[][]{{"600", "1"}, {"1100", "0.1"}, {"1643", "0"}});
    model.material("mat1").propertyGroup("Ludwik").func("int1").set("fununit", new String[]{"1"});
    model.material("mat1").propertyGroup("Ludwik").func("int1").set("argunit", new String[]{"K"});
    model.material("mat1").propertyGroup("Ludwik").set("k_lud", "560[MPa]*a(T)");
    model.material("mat1").propertyGroup("Ludwik").set("n_lud", "0.61");
    model.material("mat1").propertyGroup("Ludwik").addInput("temperature");
    model.material("mat1").propertyGroup("JohnsonCook").label("Johnson-Cook");
    model.material("mat1").propertyGroup("JohnsonCook").set("k_jcook", "560[MPa]");
    model.material("mat1").propertyGroup("JohnsonCook").set("n_jcook", "0.61");
    model.material("mat1").propertyGroup("JohnsonCook").set("C_jcook", "0.12");
    model.material("mat1").propertyGroup("JohnsonCook").set("epet0_jcook", "1[1/s]");
    model.material("mat1").propertyGroup("JohnsonCook").set("m_jcook", "0.6");
    model.material("mat1").propertyGroup("Swift").set("e0_swi", "0.021");
    model.material("mat1").propertyGroup("Swift").set("n_swi", "0.2");
    model.material("mat1").propertyGroup("Voce").func("int1").set("funcname", "a");
    model.material("mat1").propertyGroup("Voce").func("int1")
         .set("table", new String[][]{{"600", "1"}, {"1100", "0.1"}, {"1643", "0"}});
    model.material("mat1").propertyGroup("Voce").func("int1").set("fununit", new String[]{"1"});
    model.material("mat1").propertyGroup("Voce").func("int1").set("argunit", new String[]{"K"});
    model.material("mat1").propertyGroup("Voce").set("sigma_voc", "249[MPa]*a(T)");
    model.material("mat1").propertyGroup("Voce").set("beta_voc", "9.3");
    model.material("mat1").propertyGroup("Voce").addInput("temperature");
    model.material("mat1").propertyGroup("HockettSherby").label("Hockett-Sherby");
    model.material("mat1").propertyGroup("HockettSherby").func("int1").set("funcname", "a");
    model.material("mat1").propertyGroup("HockettSherby").func("int1")
         .set("table", new String[][]{{"600", "1"}, {"1100", "0.1"}, {"1643", "0"}});
    model.material("mat1").propertyGroup("HockettSherby").func("int1").set("fununit", new String[]{"1"});
    model.material("mat1").propertyGroup("HockettSherby").func("int1").set("argunit", new String[]{"K"});
    model.material("mat1").propertyGroup("HockettSherby").set("sigma_hoc", "684[MPa]*a(T)");
    model.material("mat1").propertyGroup("HockettSherby").set("m_hoc", "3.9");
    model.material("mat1").propertyGroup("HockettSherby").set("n_hoc", "0.85");
    model.material("mat1").propertyGroup("HockettSherby").addInput("temperature");
    model.material("mat1").propertyGroup("ArmstrongFrederick").label("Armstrong-Frederick");
    model.material("mat1").propertyGroup("ArmstrongFrederick").func("int1").set("funcname", "a");
    model.material("mat1").propertyGroup("ArmstrongFrederick").func("int1")
         .set("table", new String[][]{{"600", "1"}, {"1100", "0.1"}, {"1643", "0"}});
    model.material("mat1").propertyGroup("ArmstrongFrederick").func("int1").set("fununit", new String[]{"1"});
    model.material("mat1").propertyGroup("ArmstrongFrederick").func("int1").set("argunit", new String[]{"K"});
    model.material("mat1").propertyGroup("ArmstrongFrederick").set("Ck", "2.070[GPa]*a(T)");
    model.material("mat1").propertyGroup("ArmstrongFrederick").set("gammak", "8.0");
    model.material("mat1").propertyGroup("ArmstrongFrederick").addInput("temperature");
    model.material("mat1").propertyGroup("Norton").set("A_nor", "1.2e-15[1/s]");
    model.material("mat1").propertyGroup("Norton").set("sigRef_nor", "1[MPa]");
    model.material("mat1").propertyGroup("Norton").set("n_nor", "4.5");
    model.material("mat1").propertyGroup("Garofalo").set("A_gar", "1e-6[1/s]");
    model.material("mat1").propertyGroup("Garofalo").set("sigRef_gar", "100[MPa]");
    model.material("mat1").propertyGroup("Garofalo").set("n_gar", "4.6");
    model.material("mat1").propertyGroup("ChabocheViscoplasticity").set("A_cha", "1[1/s]");
    model.material("mat1").propertyGroup("ChabocheViscoplasticity").set("sigRef_cha", "490[MPa]");
    model.material("mat1").propertyGroup("ChabocheViscoplasticity").set("n_cha", "9");

//    In the Materials toolbar, click Add Material to close the Add Material window.
//    In the Model Builder window, under Component 1 (comp1) > Materials, click Topology Link 1 (toplnk1).
//    In the Settings window for Topology Link, locate the Link Settings section.
//    From the Material list, select Structural steel (mat1).

    model.component("comp1").material("toplnk1").set("link", "mat1");

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

    model.component("comp1").physics("solid").create("roll1", "Roller", 1);

//    In the Settings window for Roller, locate the Boundary Selection section.
//    From the Selection list, select Symmetry/Roller.

    model.component("comp1").physics("solid").feature("roll1").selection().named("geom1_boxsel3");

//    In the Physics toolbar, click Boundaries and choose Prescribed Displacement.

    model.component("comp1").physics("solid").create("disp1", "Displacement1", 1);

//    In the Settings window for Prescribed Displacement, locate the Boundary Selection section.
//    From the Selection list, select Sliding Support.

    model.component("comp1").physics("solid").feature("disp1").selection().named("geom1_boxsel2");

//    Locate the Prescribed Displacement section.
//    From the Displacement in y direction list, select Prescribed.

    model.component("comp1").physics("solid").feature("disp1").setIndex("Direction", "prescribed", 1);

//    This is effectively a roller condition along the <eqv>x</eqv>-axis, but it is applied on a vertical boundary to avoid bending stiffness.
//    In the Physics toolbar, click Boundaries and choose Boundary Load.

    model.component("comp1").physics("solid").create("bndl1", "BoundaryLoad", 1);

//    In the Settings window for Boundary Load, locate the Boundary Selection section.
//    From the Selection list, select Load Boundary.

    model.component("comp1").physics("solid").feature("bndl1").selection().named("geom1_boxsel1");

//    Locate the Force section.
//    From the Load type list, select Total force.

    model.component("comp1").physics("solid").feature("bndl1").set("forceType", "TotalForce");

//    Since this is a linear problem, the magnitude of the applied force does not affect the optimal topology.
//    Specify the \[\mathbf{F}_{\mathrm{tot}}\] vector as

    model.component("comp1").physics("solid").feature("bndl1").set("force", new String[]{"0", "-100[kN]", "0"});

//    In the Model Builder window, right-click Component 1 (comp1) > Mesh 1 and choose Edit Physics-Induced Sequence.

    model.component("comp1").mesh("mesh1").automatic(false);

//    In the Model Builder window, under Component 1 (comp1) > Mesh 1, click Size.
//    In the Settings window for Size, locate the Element Size section.
//    Click the Custom button.

    model.component("comp1").mesh("mesh1").feature("size").set("custom", true);

//    Locate the Element Size Parameters section.
//    In the Maximum element size text field, type meshsz.

    model.component("comp1").mesh("mesh1").feature("size").set("hmax", "meshsz");

//    Click Build All.

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

//    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("volfrac", "0.5", "Maximum volume fraction");
    model.param().set("volfrac", "0.5");
    model.param().descr("volfrac", "Maximum volume fraction");
    model.param().set("beta", "1", "Projection slope");
    model.param().set("beta", "1");
    model.param().descr("beta", "Projection slope");

//    Here we have also defined the maximum volume fraction.
//    Add a density topology feature, which can be used to distinguish between void and solid regions. This variable is coupled back to the <l>Solid Mechanics</l> interface via the Young's modulus. The filter radius should not be smaller than the mesh element size, so the default will work, but a fixed value has to be chosen to make the result of the optimization mesh independent.
//    In the Model Builder window, under Component 1 (comp1) > Topology Optimization, click Density Model 1 (dtopo1).
//    In the Settings window for Density Model, locate the Projection section.
//    From the Projection type list, select Hyperbolic tangent projection.

    model.component("comp1").common("dtopo1").set("projectionType", "TanhProjection");

//    In the \[\beta\] text field, type beta.

    model.component("comp1").common("dtopo1").set("beta", "beta");

//    Locate the Control Variable Initial Value section.
//    In the \[\theta_0\] text field, type volfrac.

    model.component("comp1").common("dtopo1").set("theta_0", "volfrac");

//    In the Model Builder window, click Study 1.
//    In the Settings window for Study, type Optimization in the Label text field.

    model.study("std1").label("Optimization");

//    Locate the Study Settings section.
//    Clear the Generate default plots checkbox.

    model.study("std1").setGenPlots(false);

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

//     twice.

    model.study("std1").feature("param").setIndex("pname", "b", 1);
    model.study("std1").feature("param").setIndex("plistarr", "", 1);
    model.study("std1").feature("param").setIndex("punit", "m", 1);
    model.study("std1").feature("param").setIndex("pname", "b", 1);
    model.study("std1").feature("param").setIndex("plistarr", "", 1);
    model.study("std1").feature("param").setIndex("punit", "m", 1);

//    In the table, enter the following settings:

    model.study("std1").feature("param").setIndex("pname", "beta", 0);
    model.study("std1").feature("param").setIndex("plistarr", "1 8", 0);
    model.study("std1").feature("param").setIndex("punit", "", 0);
    model.study("std1").feature("param").setIndex("pname", "meshsz", 1);
    model.study("std1").feature("param").setIndex("plistarr", "6 3", 1);
    model.study("std1").feature("param").setIndex("punit", "cm", 1);

//    Click to expand the Advanced Settings section.
//    Select the Reuse solution from previous step checkbox.

    model.study("std1").feature("param").set("reusesol", true);

//    <s>b</s> equal to one corresponds to no projection, so this effectively initializes an optimization with projection on a fine mesh with the solution to the optimization problem on a coarse without projection.
//    Initialize the solution object with control variable and its filtered version, so that a suitable plot can be set up.
//    In the Study toolbar, click Get Initial Value.

    model.study("std1").createAutoSequences("sol");
    model.study("std1").createAutoSequences("jobs");

    model.sol().create("sol2");
    model.sol("sol2").study("std1");
    model.sol("sol2").label("Parametric Solutions 1");

    model.batch("p1").feature("so1").set("psol", "sol2");
    model.batch("p1").getInitialValue();

//    In the Model Builder window, expand the Results node.
//    Set up a <l>Mirror</l> dataset which can be used for plotting the whole geometry rather than just the left half.
//    In the Model Builder window, expand the Results > Datasets node.
//    Right-click Results > Datasets and choose More 2D Datasets > Mirror 2D.

    model.result().dataset().create("mir1", "Mirror2D");

//    In the Settings window for Mirror 2D, locate the Axis Data section.
//    In row Point 1, set X to a.

    model.result().dataset("mir1").setIndex("genpoints", "a", 0, 0);

//    In row Point 2, set X to a.

    model.result().dataset("mir1").setIndex("genpoints", "a", 1, 0);

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

    model.result().dataset("mir1").set("data", "dset2");

//    Right-click Mirror 2D 1 and choose Duplicate.

    model.result().dataset().duplicate("mir2", "mir1");

//    In the Settings window for Mirror 2D, locate the Data section.
//    From the Dataset list, select Optimization/Solution 1 (sol1).

    model.result().dataset("mir2").set("data", "dset1");

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

    model.result().create("pg1", "PlotGroup2D");
    model.result("pg1").run();

//    In the Settings window for 2D Plot Group, locate the Data section.
//    From the Dataset list, select Mirror 2D 1.

    model.result("pg1").set("data", "mir1");

//    In the Label text field, type Topology.

    model.result("pg1").label("Topology");

//    Right-click Topology and choose Surface.

    model.result("pg1").create("surf1", "Surface");
    model.result("pg1").feature("surf1").set("evaluationsettings", "parent");

//    In the Settings window for Surface, locate the Expression section.
//    In the Expression text field, type if(mir1side,dtopo1.theta_c,dtopo1.theta_f).

    model.result("pg1").feature("surf1").set("expr", "if(mir1side,dtopo1.theta_c,dtopo1.theta_f)");

//    Select the Description checkbox.

    model.result("pg1").feature("surf1").set("descractive", true);

//    In the associated text field, type theta_c (left) and theta_f (right).

    model.result("pg1").feature("surf1").set("descr", "theta_c (left) and theta_f (right)");

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

    model.result("pg1").feature("surf1").set("colortable", "GrayScale");

//    From the Color table transformation list, select Reverse.

    model.result("pg1").feature("surf1").set("colortabletrans", "reverse");
    model.result("pg1").run();

//    In the Model Builder window, right-click Topology and choose Duplicate.

    model.result().duplicate("pg2", "pg1");
    model.result("pg2").run();

//    In the Settings window for 2D Plot Group, locate the Data section.
//    From the Dataset list, select Mirror 2D 2.

    model.result("pg2").set("data", "mir2");

//    In the Label text field, type Topology 2.

    model.result("pg2").label("Topology 2");
    model.result("pg2").run();

//    In the Model Builder window, expand the Topology 2 node, then click Surface 1.
//    In the Settings window for Surface, locate the Expression section.
//    In the Expression text field, type if(mir1side,dtopo1.theta_f,dtopo1.theta).

    model.result("pg2").feature("surf1").set("expr", "if(mir1side,dtopo1.theta_f,dtopo1.theta)");

//    In the Description text field, type theta_f (left) and theta (right).

    model.result("pg2").feature("surf1").set("descr", "theta_f (left) and theta (right)");

//    Add the <l>Topology Optimization</l> study step, select a solver, limit the number of iterations and switch on plotting while solving.
//    In the Model Builder window, under Optimization, click Topology Optimization.
//    In the Settings window for Topology Optimization, locate the Optimization Solver section.
//    In the Maximum number of iterations text field, type 50.

    model.study("std1").feature("topo").set("mmamaxiter", 50);

//    Locate the Constraints section.
//    In the table, enter the following settings:

    model.study("std1").feature("topo").setIndex("constraintExpression", "comp1.dtopo1.theta_avg", 0);
    model.study("std1").feature("topo").setIndex("constraintLbound", "", 0);
    model.study("std1").feature("topo").setIndex("constraintUbound", "volfrac", 0);

//    Click to expand the Output section.
//    Select the Plot checkbox.

    model.study("std1").feature("topo").set("plot", true);

//    In the table, enter the following settings:

    model.study("std1").feature("topo").setIndex("plotgrouparr", "pg2", 0);

//    In general it is best to scale the objective function with the initial value, but in this case the scale is close enough to 1 that it is unnecessary.
//    In the Study toolbar, click Compute.

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

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

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

    model.study("std1").feature("topo").set("probewindow", "");

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

//    Plotting the projected field gives a sharper and fairer picture of the topology, as seen by the <l>Solid Mechanics</l> interface.

    model.result("pg1").run();
    model.result("pg1").feature("surf1").set("titletype", "none");
    model.result("pg1").feature("surf1").set("colorlegend", false);

    model.view("view2").set("showgrid", false);

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

    model.view("view2").set("showgrid", true);

    model.result("pg1").run();
    model.result("pg1").feature("surf1").set("titletype", "auto");
    model.result("pg1").feature("surf1").set("colorlegend", true);
    model.result("pg2").run();
    model.result("pg2").feature("surf1").set("titletype", "none");
    model.result("pg2").feature("surf1").set("colorlegend", false);

    model.view("view3").set("showgrid", false);

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

    model.view("view3").set("showgrid", true);

    model.result("pg2").run();
    model.result("pg2").feature("surf1").set("titletype", "auto");
    model.result("pg2").feature("surf1").set("colorlegend", true);
    model.result("pg2").feature("surf1").set("expr", "solid.E");
    model.result("pg2").feature("surf1").set("descractive", false);
    model.result("pg2").feature("surf1").set("colortable", "RainbowLight");
    model.result("pg2").feature("surf1").set("colortabletrans", "none");
    model.result("pg2").run();
    model.result("pg2").run();

    return model;
  }

  public static Model run2(Model model) {
    model.result("pg2").feature("surf1").set("expr", "if(mir1side,dtopo1.theta_f,dtopo1.theta)");
    model.result("pg2").feature("surf1").set("descractive", true);
    model.result("pg2").feature("surf1").set("descr", "theta_f (left) and theta (right)");
    model.result("pg2").feature("surf1").set("colortable", "GrayScale");
    model.result("pg2").feature("surf1").set("colortabletrans", "reverse");
    model.result("pg2").run();

    model.title("Topology Optimization of an MBB Beam");

    model
         .description("This example demonstrates topology optimization with the Optimization Module in the context of the Messerschmitt\u2013B\u00f6lkow\u2013Blohm beam benchmark problem.");

    return model;
  }

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

}
