Get and Set Methods for Accessing Properties
The get and set methods are used to access and assign, respectively, property values in the different parts of the model object. To assign individual elements of a vector or matrix, the setIndex method is used. The property values can be of the basic data types: String, int, double, and boolean, as well as vectors or matrices of these types (1D or 2D arrays).
The get, set, and create methods (described in the previous section) are also accessible from the model tree by right-clicking and selecting Copy as Code to Clipboard.
The get Methods
The family of get methods is used to retrieve the values of properties. For example, the getDouble method can be used to retrieve the value of the predefined element size property hauto for a mesh and store it in a variable hv:
double hv = model.mesh("mesh1").feature("size").getDouble("hauto");
See the section “Example Code” on page 33 below for more information on the property hauto.
The syntax for the family of get methods for the basic data types is summarized in the following table:
All arrays are returned as copies of the data; writing to a retrieved array does not change the data in the model object. To change the contents of an array in the model object, use one of the methods set or setIndex.
Automatic type conversion is attempted from the property type to the requested return type.
The set Method
The syntax for assignment using the set method is exemplified by this line of code, which sets the title of a plot group pg1:
model.result("pg1").set("title", "Temperature T in Kelvin");
The first argument is a string with the name of the property, in the above example "title". The second argument is the value and can be a basic type as indicated by the table below.
Using the set method for an object returns the object itself. This allows you to append multiple calls to set as follows:
model.result("pg1").set("edgecolor", "black").set("edges", "on");
The previous line of code assigns values to both the edgecolor and edges properties of the plot group pg1 and is equivalent to the two lines:
model.result("pg1").set("edgecolor", "black");
model.result("pg1").set("edges", "on");
In this case, the set method returns a plot group object.
Automatic type conversion is attempted from the input value type to the property type. For example, consider a model parameter a that is just a decimal number with no unit. Its value can be set with the statement:
model.param().set("a", "7.54");
where the value "7" is a string. In this case, the following syntax is also valid:
model.param().set("a",7.54);
The setIndex Method
The setIndex method is used to assign a value to a 1D or 2D array element at a position given by one or two indices (starting from index 0).
The following line illustrates using setIndex with one index:
model.physics("c").feature("cfeq1").setIndex("f", "2.5", 0);
The following line illustrates using setIndex with two indices:
model.physics("c").feature("cfeq1").setIndex("c", "-0.1", 0, 1);
For the setIndex method in general, use one of these alternatives to set the value of a single element:
setIndex(String name,String value,int index);
setIndex(String name,String value,int index1,int index2);
The name argument is a string with the name of the property. The value argument is a string representation of the value. The indices start at 0, for example:
setIndex(name,value,2);
sets the third element of the property name to value.
The setIndex method returns an object of the same type, which means that setIndex methods can be appended just like the set method.
If the index points beyond the current size of the array, then the array is extended as needed before the element at index is set. The values of any newly created intermediate elements are undefined.
The method setIndex and set can both be used to assign values in ragged arrays. For example, consider a ragged array with 2 rows. The code statements:
setIndex(name,new String[]{"1","2","3"},0);
setIndex(name,new String[]{"4","5"},1);
sets the first and second row of the array and are equivalent to the single statement:
set("name",new String[][]{{"1","2","3"},{"4","5"}});
Methods Associated with Set and Get Methods
For object types for which the set, setIndex, and get methods can be used, the following additional methods are available, exemplified by the case of a Heat Transfer in Solids physics interface:
// String[] properties();
String[] props = model.component("comp1").physics("ht").feature("solid1").properties();
returns the names of all available properties,
// boolean hasProperty(String name);
boolean b = model.component("comp1").physics("ht").feature("solid1").hasProperty("k_mat");
returns true if the feature has the named property,
// String[] getAllowedPropertyValues(String name);
String[] vals = model.component("comp1").physics("ht").feature("solid1").getAllowedPropertyValues("k_mat");
returns the allowed values for named properties, if it is a finite set.
Example Code
The following code block can be used to warn an application’s user of excessive simulation times based on the element size:
if (model.mesh("mesh1").feature("size").getDouble("hauto") <= 3) {
  exp_time = "Solution times may be more than 10 minutes for finer element   sizes.";
}
In the above example, getDouble is used to retrieve the value of the property hauto, which corresponds to the Element Size parameter Predefined in the Settings window of the Size node under the Mesh node. This setting is available when the Sequence type is set to User-controlled mesh, in the Settings window of the Mesh node.
The following line of code retrieves an array of strings corresponding to the legends of a 1D point graph.
String[] legends = model.results("pg3").feature("ptgr1").getStringArray("legends");
The figure below shows an example of a vector of legends in the Settings window of the corresponding Point Graph.
The following line of code sets the Dataset dset1 for the Plot Group pg1:
model.result("pg1").set("data", "dset1");
The following lines of code set the anisotropic diffusion coefficient for a Poisson’s equation problem on a block geometry.
model.geom("geom1").create("blk1", "Block");
with(model.geom("geom1").feature("blk1"));
set("size", new String[]{"10", "1", "1"});
endwith();
model.geom("geom1").run();
with(model.physics("c").feature("cfeq1"));
setIndex("c", "-0.1", 0, 1);
setIndex("c", "-0.2", 0, 6);
setIndex("f", "2.5", 0);
endwith();
The 3-by-3 diffusion coefficient matrix indices follow column-first ordering.
The code below sets the global parameter L to a fixed value.
model.param().set("L", "10[cm]");
The code below sets the material link index to the string variable alloy, defined under the Declarations node.
model.material("matlnk1").set("link", alloy);
The code below sets the coordinates of a cut point dataset cpt1 to the values of the 1D array samplecoords[].
with(model.result().dataset("cpt1"));
set("pointx", samplecoords[0]);
set("pointy", samplecoords[1]);
set("pointz", samplecoords[2]);
endwith();
The code below sets the components of a deformation plot.
with(model.result("pg7").feature("surf1").feature("def"));
setIndex("expr", withstru, 0);
setIndex("expr", withstrv, 1);
setIndex("expr", withstrw, 2);
endwith();
The code below sets the title and color legend of a plot group pg2 and then regenerates the plot.
with(model.result("pg2"));
  set("titletype", "auto");
endwith();
with(model.result("pg2").feature("surf1"));
  set("colorlegend", "on");
endwith();
model.result("pg2").run();