/*
 * tuning_fork.java
 */

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

/** Model exported on May 12 2026, 08:21 by COMSOL 6.4.0.418. */
public class tuning_fork {

  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 Structural Mechanics > Solid Mechanics (solid).
//    Click Add.
//    Click Study.
//    In the Select Study tree, select General Studies > Eigenfrequency.
//    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("solid", "SolidMechanics", "geom1");

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

//    Define parameters for the relevant dimensions and positions for the tuning fork's components.
//    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", "78[mm]");
    model.param().descr("L", "Cylinder length");
    model.param().set("Rb", "7.5[mm]");
    model.param().descr("Rb", "Base radius");
    model.param().set("Rp", "2.5[mm]");
    model.param().descr("Rp", "Prong radius");
    model.param().set("Ls", "20[mm]");
    model.param().descr("Ls", "Stem length");
    model.param().set("Rh", "4[mm]");
    model.param().descr("Rh", "Handle radius");
    model.param().set("z0", "-22.5[mm]");
    model.param().descr("z0", "Vertical displacement, handle");

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

    model.component("comp1").geom("geom1").lengthUnit("mm");

//     to better reflect the size of the fork.
//    You can build up the fork geometry efficiently using predefined geometric primitives.
//    In the Geometry toolbar, click Cone.

    model.component("comp1").geom("geom1").create("cone1", "Cone");
    model.component("comp1").geom("geom1").feature("cone1").set("specifytop", "radius");

//    In the Settings window for Cone, locate the Size and Shape section.
//    In the Bottom radius text field, type Rp.

    model.component("comp1").geom("geom1").feature("cone1").set("r", "Rp");

//    In the Height text field, type Ls.

    model.component("comp1").geom("geom1").feature("cone1").set("h", "Ls");

//    From the Specify top size using list, select Angle.

    model.component("comp1").geom("geom1").feature("cone1").set("specifytop", "angle");

//    In the Semiangle text field, type 2.

    model.component("comp1").geom("geom1").feature("cone1").set("ang", 2);

//    Locate the Position section.
//    In the x text field, type Rb.

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

//    In the z text field, type -Rb.

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

//    Locate the Axis section.
//    From the Axis type list, select Cartesian.

    model.component("comp1").geom("geom1").feature("cone1").set("axistype", "cartesian");

//    In the z text field, type -1.

    model.component("comp1").geom("geom1").feature("cone1").set("ax3", new double[]{0, 0, -1});

//    In the Geometry toolbar, click Sphere.

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

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

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

//    Locate the Position section.
//    In the x text field, type Rb.

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

//    In the z text field, type z0-Rb.

    model.component("comp1").geom("geom1").feature("sph1").set("pos", new String[]{"Rb", "0", "z0-Rb"});

//    In the Geometry toolbar, click Torus.

    model.component("comp1").geom("geom1").run("sph1");
    model.component("comp1").geom("geom1").create("tor1", "Torus");

//    In the Settings window for Torus, locate the Size and Shape section.
//    In the Major radius text field, type Rb.

    model.component("comp1").geom("geom1").feature("tor1").set("rmaj", "Rb");

//    In the Minor radius text field, type Rp.

    model.component("comp1").geom("geom1").feature("tor1").set("rmin", "Rp");

//    In the Revolution angle text field, type 180.

    model.component("comp1").geom("geom1").feature("tor1").set("angle", 180);

//    Locate the Position section.
//    In the x text field, type Rb.

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

//    Locate the Axis section.
//    From the Axis type list, select Cartesian.

    model.component("comp1").geom("geom1").feature("tor1").set("axistype", "cartesian");

//    In the z text field, type 0.

    model.component("comp1").geom("geom1").feature("tor1").set("ax3", new String[]{"0", "0", "0"});

//    In the y text field, type 1.

    model.component("comp1").geom("geom1").feature("tor1").set("ax3", new double[]{0, 1, 0});

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

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

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

    model.component("comp1").geom("geom1").run("tor1");
    model.component("comp1").geom("geom1").create("uni1", "Union");

//    In the Settings window for Union, locate the Union section.
//    Clear the Keep interior boundaries checkbox.

    model.component("comp1").geom("geom1").feature("uni1").set("intbnd", false);

//    Click in the Graphics window and then press Ctrl+A to select all objects.

    model.component("comp1").geom("geom1").feature("uni1").selection("input").set("cone1", "sph1", "tor1");

//    This completes the handle and base of the fork.
//    Add two cylinders to represent the prongs.
//    In the Geometry toolbar, click Cylinder.

    model.component("comp1").geom("geom1").run("uni1");
    model.component("comp1").geom("geom1").create("cyl1", "Cylinder");

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

    model.component("comp1").geom("geom1").feature("cyl1").set("r", "Rp");

//    In the Height text field, type L.

    model.component("comp1").geom("geom1").feature("cyl1").set("h", "L");

//    In the Geometry toolbar, click Cylinder.

    model.component("comp1").geom("geom1").run("cyl1");
    model.component("comp1").geom("geom1").create("cyl2", "Cylinder");

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

    model.component("comp1").geom("geom1").feature("cyl2").set("r", "Rp");

//    In the Height text field, type L.

    model.component("comp1").geom("geom1").feature("cyl2").set("h", "L");

//    Locate the Position section.
//    In the x text field, type 2*Rb.

    model.component("comp1").geom("geom1").feature("cyl2").set("pos", new String[]{"2*Rb", "0", "0"});

//    Use virtual geometry operations to avoid short edges and narrow regions. This will improve the mesh generation.
//    In the Geometry toolbar, click Virtual Operations and choose Ignore Edges.

    model.component("comp1").geom("geom1").run("fin");
    model.component("comp1").geom("geom1").create("ige1", "IgnoreEdges");

//    On the object fin, select Edges 22, 23, 29, 32, 33, 39, 42, 43.

    model.component("comp1").geom("geom1").feature("ige1").selection("input")
         .set("fin", 22, 23, 29, 32, 33, 39, 42, 43);

//    It might be easier to select the edges by using the <l>Selection List</l> window. To open this window, in the <l>Home</l> toolbar click <l>Windows</l> and choose <l>Selection List</l>. (If you are running the cross-platform desktop, you find <l>Windows</l> in the main menu.)
//    In the Geometry toolbar, click Build All.

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

//    Click the Go to Default View button in the Graphics toolbar.
//    The completed geometry should look as shown in the following figure:
//    In the Materials toolbar, click Add Material to open the Add Material window.
//    In the tree, select Built-in > Steel AISI 4340.
//    Click Add to Component in the window toolbar.

    model.component("comp1").material().create("mat1", "Common");
    model.component("comp1").material("mat1").propertyGroup()
         .create("Enu", "Enu", "Young's modulus and Poisson's ratio");
    model.component("comp1").material("mat1").label("Steel AISI 4340");
    model.component("comp1").material("mat1").set("family", "steel");
    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("electricconductivity", new String[]{"4.032e6[S/m]", "0", "0", "0", "4.032e6[S/m]", "0", "0", "0", "4.032e6[S/m]"});
    model.component("comp1").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.component("comp1").material("mat1").propertyGroup("def").set("heatcapacity", "475[J/(kg*K)]");
    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("density", "7850[kg/m^3]");
    model.component("comp1").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.component("comp1").material("mat1").propertyGroup("Enu").set("E", "205[GPa]");
    model.component("comp1").material("mat1").propertyGroup("Enu").set("nu", "0.28");

//    In the Materials toolbar, click Add Material to close the Add Material window.
//    In the Settings window for Mesh, locate the Physics-Controlled Mesh section.
//    From the Element size list, select Fine.

    model.component("comp1").mesh("mesh1").autoMeshSize(4);

//    In the Mesh toolbar, click More Generators and choose Free Triangular.

    model.component("comp1").mesh("mesh1").create("ftri1", "FreeTri");

//    Select Boundaries 6, 24.

    model.component("comp1").mesh("mesh1").feature("ftri1").selection().set(6, 24);

//    In the Settings window for Free Triangular, click Build Selected.

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

//    In the Mesh toolbar, click Swept.

    model.component("comp1").mesh("mesh1").create("swe1", "Sweep");

//    In the Settings window for Swept, locate the Domain Selection section.
//    From the Geometric entity level list, select Domain.

    model.component("comp1").mesh("mesh1").feature("swe1").selection().geom("geom1", 3);

//    Select Domains 1, 3.

    model.component("comp1").mesh("mesh1").feature("swe1").selection().set(1, 3);

//    Right-click Swept 1 and choose Distribution.

    model.component("comp1").mesh("mesh1").feature("swe1").create("dis1", "Distribution");

//    In the Settings window for Distribution, locate the Distribution section.
//    In the Number of elements text field, type 50.

    model.component("comp1").mesh("mesh1").feature("swe1").feature("dis1").set("numelem", 50);

//    In the Model Builder window, right-click Swept 1 and choose Build Selected.

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

//    In the Mesh toolbar, click Free Tetrahedral.

    model.component("comp1").mesh("mesh1").create("ftet1", "FreeTet");

//    In the Model Builder window, right-click Mesh 1 and choose Build All.

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

//    The meshed geometry should look like that in the figure below.
//    Set up a parametric sweep with respect to the cylinder length <eqv>L</eqv> and search for an eigenfrequency in the vicinity of 440 Hz.
//    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", "L", 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", "L", 0);
    model.study("std1").feature("param").setIndex("plistarr", "", 0);
    model.study("std1").feature("param").setIndex("punit", "m", 0);

//    In the table, enter the following settings:

    model.study("std1").feature("param").setIndex("pname", "L", 0);
    model.study("std1").feature("param").setIndex("plistarr", "range(0.078,1e-4,0.0795)", 0);

//    In the Model Builder window, click Step 1: Eigenfrequency.
//    In the Settings window for Eigenfrequency, locate the Study Settings section.
//    Select the Desired number of eigenfrequencies checkbox.

    model.study("std1").feature("eig").set("neigsactive", true);

//    In the associated text field, type 1.

    model.study("std1").feature("eig").set("neigs", 1);

//    In the Search for eigenfrequencies around shift text field, type 440.

    model.study("std1").feature("eig").set("shift", "440");

//    In the Study toolbar, click Show Default Solver.

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

//    In the Model Builder window, expand the Solution 1 (sol1) node, then click Eigenvalue Solver 1.
//    In the Settings window for Eigenvalue Solver, locate the General section.
//    In the Relative tolerance text field, type 1e-3.

    model.sol("sol1").feature("e1").set("rtol", "1e-3");

//    In the Study toolbar, click Compute.

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

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

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

    model.result().create("pg1", "PlotGroup3D");
    model.result("pg1").set("data", "dset2");
    model.result("pg1").setIndex("looplevel", 1, 0);
    model.result("pg1").setIndex("looplevel", 16, 1);
    model.result("pg1").label("Mode Shape (solid)");
    model.result("pg1").set("showlegends", false);
    model.result("pg1").create("surf1", "Surface");
    model.result("pg1").feature("surf1").set("expr", new String[]{"solid.disp"});
    model.result("pg1").feature("surf1").set("threshold", "manual");
    model.result("pg1").feature("surf1").set("thresholdvalue", 0.2);
    model.result("pg1").feature("surf1").set("colortable", "Rainbow");
    model.result("pg1").feature("surf1").set("colortabletrans", "none");
    model.result("pg1").feature("surf1").set("colorscalemode", "linear");
    model.result("pg1").feature("surf1").set("colortable", "AuroraBorealis");
    model.result("pg1").feature("surf1").create("def", "Deform");
    model.result("pg1").feature("surf1").feature("def").set("expr", new String[]{"u", "v", "w"});
    model.result("pg1").feature("surf1").feature("def").set("descr", "Displacement field");
    model.result().evaluationGroup().create("std1EvgFrq", "EvaluationGroup");
    model.result().evaluationGroup("std1EvgFrq").set("data", "dset2");
    model.result().evaluationGroup("std1EvgFrq").label("Eigenfrequencies (Study 1)");
    model.result().evaluationGroup("std1EvgFrq").create("gev1", "EvalGlobal");
    model.result().evaluationGroup("std1EvgFrq").feature("gev1").setIndex("expr", "2*pi*freq", 0);
    model.result().evaluationGroup("std1EvgFrq").feature("gev1").setIndex("unit", "rad/s", 0);
    model.result().evaluationGroup("std1EvgFrq").feature("gev1").setIndex("descr", "Angular frequency", 0);
    model.result().evaluationGroup("std1EvgFrq").feature("gev1").setIndex("expr", "imag(freq)/abs(freq)", 1);
    model.result().evaluationGroup("std1EvgFrq").feature("gev1").setIndex("unit", "1", 1);
    model.result().evaluationGroup("std1EvgFrq").feature("gev1").setIndex("descr", "Damping ratio", 1);
    model.result().evaluationGroup("std1EvgFrq").feature("gev1").setIndex("expr", "abs(freq)/imag(freq)/2", 2);
    model.result().evaluationGroup("std1EvgFrq").feature("gev1").setIndex("unit", "1", 2);
    model.result().evaluationGroup("std1EvgFrq").feature("gev1").setIndex("descr", "Quality factor", 2);
    model.result().evaluationGroup("std1EvgFrq").run();
    model.result("pg1").run();

//    To see all computed eigenfrequencies as a table, follow these steps:
//    In the Results toolbar, click Global Evaluation.

    model.result().numerical().create("gev1", "EvalGlobal");

//    In the Settings window for Global Evaluation, locate the Data section.
//    From the Dataset list, select Study 1/Parametric Solutions 1 (sol2).

    model.result().numerical("gev1").set("data", "dset2");

//    From the Table columns list, select Inner solutions.

    model.result().numerical("gev1").set("tablecols", "inner");

//    Click Replace Expression in the upper-right corner of the Expressions section.
//    From the menu, choose Component 1 (comp1) > Solid Mechanics > Global > solid.freq - Frequency - Hz.

    model.result().numerical("gev1").set("expr", new String[]{"solid.freq"});
    model.result().numerical("gev1").set("descr", new String[]{"Frequency"});
    model.result().numerical("gev1").set("unit", new String[]{"Hz"});

//    Click ^ next to Evaluate then choose New Table.

    model.result().table().create("tbl1", "Table");
    model.result().table("tbl1").comments("Global Evaluation 1");
    model.result().numerical("gev1").set("table", "tbl1");
    model.result().numerical("gev1").setResult();

//    You can see that the eigenfrequency closest to 440 Hz occurs for the cylinder length of 79.1 mm. Further fine-tuning can be performed if necessary.
//    To see the eigenmode that corresponds to this frequency, do the following:

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

//    In the Model Builder window, under Results, click Mode Shape (solid).
//    In the Settings window for 3D Plot Group, locate the Data section.
//    From the Parameter value (L (m)) list, select 0.0791.

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

//    In the Mode Shape (solid) toolbar, click Plot.

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

//    Click the Zoom Extents button in the Graphics toolbar.
//    In this figure, you can clearly see that mode is symmetric, and the displacements at the handle are very small compared to those of the prongs. This means that holding the tuning fork at the handle will dampen the vibrations negligibly.

    model.title("Tuning Fork");

    model
         .description("This example computes the fundamental eigenfrequency and eigenmode for a tuning fork. When correctly designed, the tuning fork vibrates at 440\u00a0Hz, a frequency known as standard concert pitch.");

    return model;
  }

  public static void main(String[] args) {
    run();
  }

}
