Getting and Setting Mesh Data
The function mphmeshstats also returns the mesh data such as element coordinates. Use the function with two output variables to get the mesh data. Enter:
[meshstats,meshdata] = mphmeshstats(model)
where meshdata is a MATLAB structure with the following fields:
types, which contains the element type;
vertex, which contains the mesh vertex coordinates;
elem, which contains the element data information;
elementity, which contains the element entity information for each element type.
Extract and Create Mesh Information
A mesh can be manually created based on a grid generated in MATLAB. However, before inserting this mesh into the model, a default coarse mesh is generated to get the mesh information, which enables you to understand the requested mesh structure to use with the createMesh method. Then a complete mesh can be constructed and stored in the meshing sequence. If the geometry is not empty, the new mesh is checked to ensure that it matches the geometry. In other words, to create an arbitrary mesh, an empty geometry sequence and a corresponding empty meshing sequence need to be created and the mesh is then constructed on the empty meshing sequence.
Start by creating a 2D model containing a square, and mesh it with triangles:
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 2);
geom1.feature.create('sq1', 'Square');
geom1.run;
mesh1 = comp1.mesh.create('mesh1');
mesh1.feature.create('ftri1', 'FreeTri');
mesh1.feature('size').set('hmax', 0.5);
mesh1.run('ftri1');
mphmesh(model);
To get the mesh data information, enter:
[meshstats,meshdata] = mphmeshstats(model);
 
meshdata =
         types: {'edg' 'tri' 'vtx'}
vertex: [2×12 double]
elem: {[2×8 int32] [3×14 int32] [0 5 7 11]}
elementity: {[8×1 int32] [14×1 int32] [4×1 int32]}
The mesh node coordinates are stored in the vertex field:
vtx = meshdata.vertex
 
vtx =
0     0   0.302   0.5   0.351   0   0.631   1   0.673   0.5     1  1
0   0.5   0.302     0   0.639   1   0.363   0   0.672     1   0.5  1
In the elem field the element information is retrieved, such as the node indices (using a 0 based) connected to the elements:
tri = meshdata.elem{2}
tri =
2 2 2 1 2 2 3 8 9 8 10 8 8 8
1 0 4 4 3 6 7 4 5 9 6 6 10 11
0 3 1 5 6 4 6 6 4 4 7 10 11 9
In the above command, notice that element number 1 is connected to nodes 0, 1, and 2, and element number 2 is connected to nodes 2, 0, and 3.
Then create manually a mesh using a data distribution generated in MATLAB. Enter the command:
[x,y] = meshgrid([0 0.5 1], [0 0.5 1]);
coord = [x(:) y(:)]';
The node distribution obtained with this command corresponds to the mesh in Figure 3-18.
Figure 3-18: Mesh with elements (bold) and nodes (italic) indices.
Table 3-1 lists the nodes and element connectivity in the mesh.
To create the elements and nodes connectivity information use the command:
new_tri(:,1)=[0;3;4];
new_tri(:,2)=[0;1;4];
new_tri(:,3)=[1;4;5];
new_tri(:,4)=[1;2;5];
new_tri(:,5)=[3;6;7];
new_tri(:,6)=[3;4;7];
new_tri(:,7)=[4;7;8];
new_tri(:,8)=[4;5;8];
Assign the element information, node coordinates, and elements connectivity information, into a new mesh. Use the method createMesh to create the new mesh:
comp2 = model.component.create('comp2', true);
comp2.geom.create('geom2',2);
mesh2 = comp2.mesh.create('mesh2');
mesh2.data.setElem('tri',new_tri)
mesh2.data.setVertex(coord)
mesh2.data.createMesh
mphmesh(model,'mesh2');
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 2);
geom1.feature.create('sq1', 'Square');
geom1.run;
mesh1 = comp1.mesh.create('mesh1');
mesh1.feature.create('ftri1', 'FreeTri');
mesh1.feature('size').set('hmax', 0.5);
mesh1.run('ftri1');
mphmesh(model);
[meshstats,meshdata] = mphmeshstats(model);
vtx = meshdata.vertex
tri = meshdata.elem{2}
[x,y] = meshgrid([0 0.5 1], [0 0.5 1]);
coord = [x(:) y(:)]';
new_tri(:,1)=[0;3;4];
new_tri(:,2)=[0;1;4];
new_tri(:,3)=[1;4;5];
new_tri(:,4)=[1;2;5];
new_tri(:,5)=[3;6;7];
new_tri(:,6)=[3;4;7];
new_tri(:,7)=[4;7;8];
new_tri(:,8)=[4;5;8];
comp2 = model.component.create('comp2', true);
comp2.geom.create('geom2',2);
mesh2 = comp2.mesh.create('mesh2');
mesh2.data.setElem('tri',new_tri);
mesh2.data.setVertex(coord);
mesh2.data.createMesh;
mphmesh(model,'mesh2');