// COMSOL 6.4 Java API method body // Continue 3D parallel-plate capacitor model. // Existing model variable is provided. // 1. Identify existing component. String cp = "comp1"; String[] existingComps = model.component().tags(); if (existingComps.length > 0) { cp = existingComps[0]; } else { model.component().create(cp, true); } model.component(cp).label("3D Parallel Plate Capacitor"); // 2. Global parameters. model.param().set("capL", "100[mm]"); model.param().set("capW", "100[mm]"); model.param().set("capGap", "10[mm]"); model.param().set("capVolt", "100[V]"); model.param().set("capEpsR", "1"); // 3. Reuse or create 3D geometry. String gt = "geom1"; String[] geoms = model.component(cp).geom().tags(); if (geoms.length > 0) { gt = geoms[0]; } else { model.component(cp).geom().create(gt, 3); } if (model.component(cp).geom(gt).getSDim() != 3) { throw new RuntimeException( "Existing geometry is not 3D; no changes made to it." ); } model.component(cp).geom(gt).lengthUnit("mm"); String blk = "capAir"; if (!java.util.Arrays.asList( model.component(cp).geom(gt).feature().tags() ).contains(blk)) { model.component(cp).geom(gt).create(blk, "Block"); } model.component(cp).geom(gt).feature(blk) .label("Air dielectric volume"); model.component(cp).geom(gt).feature(blk) .set("size", new String[]{ "capL", "capW", "capGap" }); model.component(cp).geom(gt).feature(blk) .set("base", "center"); model.component(cp).geom(gt).feature(blk) .set("pos", new String[]{ "0[mm]", "0[mm]", "capGap/2" }); model.component(cp).geom(gt).run(); // 4. Apply air material. String mt = "capAirMat"; if (!java.util.Arrays.asList( model.component(cp).material().tags() ).contains(mt)) { model.component(cp).material().create(mt, "Common"); } model.component(cp).material(mt).label("Air dielectric"); model.component(cp).material(mt).selection().all(); model.component(cp).material(mt).propertyGroup("def") .set("relpermittivity", new String[]{ "capEpsR", "0", "0", "0", "capEpsR", "0", "0", "0", "capEpsR" }); // 5. Electrode boundary selections. String sTop = "capTop"; String sBot = "capBottom"; String[] selectionTags = model.component(cp).selection().tags(); if (!java.util.Arrays.asList(selectionTags).contains(sTop)) { model.component(cp).selection().create(sTop, "Box"); } if (!java.util.Arrays.asList( model.component(cp).selection().tags() ).contains(sBot)) { model.component(cp).selection().create(sBot, "Box"); } model.component(cp).selection(sTop).label("Upper plate"); model.component(cp).selection(sBot).label("Lower plate"); model.component(cp).selection(sTop).geom(gt, 2); model.component(cp).selection(sBot).geom(gt, 2); for (String tag : new String[]{sTop, sBot}) { model.component(cp).selection(tag) .set("entitydim", 2); model.component(cp).selection(tag) .set("condition", "inside"); model.component(cp).selection(tag) .set("xmin", -51); model.component(cp).selection(tag) .set("xmax", 51); model.component(cp).selection(tag) .set("ymin", -51); model.component(cp).selection(tag) .set("ymax", 51); } model.component(cp).selection(sTop) .set("zmin", 9.99); model.component(cp).selection(sTop) .set("zmax", 10.01); model.component(cp).selection(sBot) .set("zmin", -0.01); model.component(cp).selection(sBot) .set("zmax", 0.01); // 6. Electrostatics physics. String pt = "es"; if (!java.util.Arrays.asList( model.component(cp).physics().tags() ).contains(pt)) { model.component(cp).physics() .create(pt, "Electrostatics", gt); } model.component(cp).physics(pt) .label("Electrostatics"); if (!java.util.Arrays.asList( model.component(cp).physics(pt).feature().tags() ).contains("capPot")) { model.component(cp).physics(pt) .create("capPot", "ElectricPotential", 2); } model.component(cp).physics(pt).feature("capPot") .label("Positive electrode 100 V"); model.component(cp).physics(pt).feature("capPot") .selection().named(sTop); model.component(cp).physics(pt).feature("capPot") .set("V0", "capVolt"); if (!java.util.Arrays.asList( model.component(cp).physics(pt).feature().tags() ).contains("capGnd")) { model.component(cp).physics(pt) .create("capGnd", "Ground", 2); } model.component(cp).physics(pt).feature("capGnd") .label("Ground electrode 0 V"); model.component(cp).physics(pt).feature("capGnd") .selection().named(sBot); // 7. Generate three-dimensional mesh. String meshTag = "mesh1"; if (!java.util.Arrays.asList( model.component(cp).mesh().tags() ).contains(meshTag)) { model.component(cp).mesh().create(meshTag); } model.component(cp).mesh(meshTag) .label("Capacitor 3D mesh"); model.component(cp).mesh(meshTag).autoMeshSize(4); model.component(cp).mesh(meshTag).run(); // 8. Stationary study. String studyTag = "std1"; if (!java.util.Arrays.asList( model.study().tags() ).contains(studyTag)) { model.study().create(studyTag); } model.study(studyTag) .label("Stationary Electrostatics"); if (!java.util.Arrays.asList( model.study(studyTag).feature().tags() ).contains("stat")) { model.study(studyTag) .create("stat", "Stationary"); } model.study(studyTag).feature("stat") .activate(pt, true); // 9. Electric field magnitude visualization. String pgE = "capField3D"; if (!java.util.Arrays.asList( model.result().tags() ).contains(pgE)) { model.result().create(pgE, "PlotGroup3D"); } model.result(pgE) .label("Electric Field Magnitude (V/m)"); if (!java.util.Arrays.asList( model.result(pgE).feature().tags() ).contains("slc1")) { model.result(pgE).feature() .create("slc1", "Slice"); } model.result(pgE).feature("slc1") .set("expr", pt + ".normE"); // 10. Electric potential visualization. String pgV = "capPotential3D"; if (!java.util.Arrays.asList( model.result().tags() ).contains(pgV)) { model.result().create(pgV, "PlotGroup3D"); } model.result(pgV) .label("Electric Potential (V)"); if (!java.util.Arrays.asList( model.result(pgV).feature().tags() ).contains("slc1")) { model.result(pgV).feature() .create("slc1", "Slice"); } model.result(pgV).feature("slc1") .set("expr", "V"); // 11. Analytical verification reference. model.param().set( "capEReference", "capVolt/capGap" ); model.param().set( "capCReference", "epsilon0_const*capL*capW/capGap" ); // 12. Completion diagnostics. System.out.println("COMSOL capacitor setup completed."); System.out.println("Component: " + cp); System.out.println("Geometry: " + gt); System.out.println("Physics: " + pt); System.out.println("Study: " + studyTag); System.out.println("3D geometry and mesh built."); System.out.println("Upper electrode: 100 V"); System.out.println("Lower electrode: 0 V"); System.out.println("Expected electric field: 10000 V/m"); System.out.println("Expected capacitance: 8.854 pF"); System.out.println("No solver run was requested.");