Setting or Modifying Mesh Data
You can modify the mesh object of a meshing sequence via the data() method. Using this method you access a temporary object (MeshData) storing mesh data. When you use the data() method the first time the MeshData object is empty. You can fill it with mesh data by using various set methods or by transferring mesh data from the mesh of the meshing sequence. Call the method data().createMesh to construct a complete mesh from the MeshData object and store it in the meshing sequence. If the geometry is not empty, the new mesh is checked to ensure that it matches the geometry. Thus, to create an arbitrary mesh, you need to create an empty geometry sequence and a corresponding empty meshing sequence and construct the mesh on the empty meshing sequence.
To set the mesh vertices, use
model.component(<ctag>).mesh(<tag>).data().setVertex(double[][]);
where each column of the input matrix contains the coordinates of a mesh vertex.
To set the elements of a specific type, use
model.component(<ctag>).mesh(<tag>).data().setElem(type, int[][]);
where each column of input element matrix contains the mesh vertex indices of an element’s corners.
If you want to specify the geometric entity number for the elements of a specific type, use
model.component(<ctag>).mesh(<tag>).data().setElemEntity(type, int[]);
The MeshData object has the same access methods as the meshing sequence.
int model.component(<ctag>).mesh(<tag>.data().getNumVertex();
double[][] model.component(<ctag>).mesh(<tag>).data().getVertex();
String[] model.component(<ctag>).mesh(<tag>).data().getTypes();
int model.component(<ctag>).mesh(<tag>).data().getNumElem(type);
int[][] model.component(<ctag>).mesh(<tag>).data().getElem(type);
int[] model.component(<ctag>).mesh(<tag>).data().getElemEntity(type);
It is also possible to fill the MeshData object with mesh data from the mesh of a meshing sequence. To transfer the mesh from the current meshing sequence into the MeshData object, use
model.component(<ctag>).mesh(<tag>).data().transferMesh();
To transfer the mesh from another meshing sequence, specified by mtag, into the MeshData object, use
model.component(<ctag>).mesh(<tag>).data().transferMesh(mtag);
To clear the MeshData object, use
model.component(<ctag>).mesh(<tag>).data().clearData();
To create a complete mesh from the MeshData object and store it in the sequence, use
model.component(<ctag>).mesh(<tag>).data().createMesh();
This method uses several properties when creating a complete mesh from the specified mesh data. To set a property, use
model.component(<ctag>).mesh(<tag>).data().set(property, <value>);
To get a property, use
model.component(<ctag>).mesh(<tag>).data().getType(property);
Running the createMesh() method from provided, possibly incomplete, mesh data is equivalent to importing possibly incomplete mesh data from file. See Importing Externally Generated Mesh Data in the COMSOL Multiphysics Reference Manual for more information.
The following properties are available.
Examples of Setting or Modifying Mesh Data
The following examples create a triangular mesh on a square, extracts the vertices and the triangles. Then the vertices are transformed and inserted into a new meshing sequence.
Code for Use with Java
Model model = ModelUtil.create("Model");
model.component().create("comp1");
 
model.component("comp1").geom().create("geom1", 2);
MeshSequence m = model.component("comp1").mesh().create("mesh1", "geom1");
 
// Create a rectangle and a mesh
model.component("comp1").geom("geom1").create("r1", "Rectangle");
m.create("ftri1", "FreeTri");
m.run();
 
double[][] vtx = m.getVertex();
int[][] tri = m.getElem("tri");
 
// Transform x coordinates
for (int k=0; k<vtx[0].length; k++)
  vtx[0][k] *= 0.5;
 
// Create a new geometry and mesh
model.component("comp1").geom().create("geom2", 2);
MeshSequence m2 = model.component("comp1").mesh().create("mesh2", "geom2");
 
// Insert vertices and triangles and create mesh
m2.data().setElem("tri", tri);
m2.data().setVertex(vtx);
m2.data().createMesh();
Code for Use with MATLAB
model = ModelUtil.create('Model');
model.component.create('comp1');
 
model.component('comp1').geom.create('geom1', 2);
m = model.component('comp1').mesh.create('mesh1', 'geom1');
 
% Create a rectangle and a mesh
model.component('comp1').geom('geom1').create('r1', 'Rectangle');
m.create('ftri1', 'FreeTri');
m.run;
 
vtx = m.getVertex;
tri = m.getElem('tri');
 
% Transform x coordinates
vtx(1,:) = vtx(1,:)*0.5;
 
% Create a new geometry and mesh
model.component('comp1').geom.create('geom2', 2);
m2 = model.component('comp1').mesh.create('mesh2', 'geom2');
 
% Insert vertices and triangles and create mesh
m2.data.setElem('tri', tri);
m2.data.setVertex(vtx);
m2.data.createMesh;