Language Element Examples
The Java® programming language is used to write COMSOL methods, which means that Java® statements and syntax in general can be used. This section contains simple examples of some of the most common language elements. For more information and examples, see the Application Programming Guide and the Programming Reference Manual.
Unary and Binary Operators in the Model Object
The table below describes the unary and binary operators that can be used when accessing a model object, such as when defining material properties and boundary conditions, and in results, expressions used for postprocessing and visualization.
Unary and Binary Operators in Methods (Java® Syntax)
The table below describes the most important unary and binary operators used in Java® code in methods.
Accessing a Variable in the Declarations Node
Variables defined in the Declarations node are available as global variables in a method and need no further declarations.
Built-in Elementary Math Functions
Elementary math functions used in methods rely on the Java® math library. Some examples:
Math.sin(double)
Math.cos(double)
Math.random()
Math.PI
The IF Statement
if(a<b) {
  alert(toString(a));
} else {
  alert(toString(b));
}
The For Statement
// Iterate i from 1 to N:
int N=10;
for (int i = 1; i <= N; i++) {
// Do something
}
The While Statement
double t=0,h=0.1,tend=10;
while(t<tend) {
//do something with t
t=t+h;
}
The With Statement
// Set the global parameter L to a fixed value
with(model.param());
set("L", "10[cm]");
endwith();
The code above is equivalent to:
model.param().set("L", "10[cm]");
The with command is specific to the COMSOL API and not part of the standard Java® programming language.
Accessing a Global Parameter
You would typically use the Editor Tools window for generating code for setting the value of a global parameter. While in the Method Editor, right-click the parameter and select Set.
To set the value of the global parameter L to 10 cm:
model.param().set("L", "10[cm]");
To get the global parameter L and store it in a double variable Length:
double Length=model.param().evaluate("L");
The evaluation is in this case with respect to the base Unit System defined in the model tree root node.
To return the unit of the parameter L, if any, use:
String Lunit=model.param().evaluateUnit("L");
To write the value of a double to a global parameter, you need to convert it to a string. The reason is that global parameters are model expressions and may contain units.
Multiply the value of the variable Length with 2 and write the result to the parameter L including the unit of cm.
Length=2*Length;
model.param().set("L", toString(Length)+"[cm]");
To return the value of a parameter in a different unit than the base Unit System, use:
double Length_real = model.param().evaluate("L","cm");
If the parameter is complex valued, the real and imaginary part can be returned as a double vector of length 2:
double[] realImag = model.param().evaluateComplex("Ex","V/m");
Comparing Strings
Comparing string values in Java® has to be done with .equals() and not with the == operator. This is due to the fact that the == operator compares whether the strings are the same objects and does not consider their values. The below code demonstrates string comparisons:
boolean streq=false;
String a="string A";
String b="string B";
streq=a.equals(b);
// In this case streq==false
 
streq=(a==b);
// In this case streq==false
 
b="string A";
streq=a.equals(b);
// In this case streq==true
Alerts and Messages
The methods alert, confirm, and request display a dialog with a text string and optional user input. The following example uses confirm to ask the user if a direct or an iterative solver should be used in an application. Based on the answer, the alert function is then used to show the estimated memory requirement for the selected solver type in a message dialog:
String answer = confirm("Which solver do you want to use?", "Solver Selection","Direct", "Iterative");
if(answer.equals("Direct")) {
alert("Using the direct solver will require about 4GB of memory when solving.");
} else {
alert("Using the iterative solver will require about 2GB of memory when solving.");
}