Mesh Creation Functions
Several mesh features are discussed, with examples in this section:
Mesh Sizing Properties
The Size attribute provides a number of input properties that can control the mesh element size, such as the following properties:
These properties are available both globally and locally. The following examples are included: Creating a 2D Mesh with Triangular Elements and Creating a 2D Mesh with Quadrilateral Elements. Also discussed is The Free Meshing Method.
There are several predefined settings that can be used to set a suitable combination of values for many properties. To select one of these settings, use the property hauto and pass an integer from 1 to 9 as its value to describe the mesh resolution:
Creating a 2D Mesh with Triangular Elements
Generate a triangular mesh of a unit square:
model = ModelUtil.create('Model');
 
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1',2);
geom1.feature.create('r1','Rectangle');
 
mesh1 = comp1.mesh.create('mesh1');
ftri1 = mesh1.feature.create('ftri1','FreeTri');
mesh1.run;
 
mphmesh(model)
Figure 3-1: Default mesh on a unit square.
The default size feature is generated with the property hauto set to 5, that is:
mesh1.feature('size').set('hauto',5);
To override this behavior, set hauto to another integer. Override this by setting specific size properties, for example, making the mesh finer than the default by specifying a maximum element size of 0.02:
mesh1.feature('size').set('hmax',0.02);
mesh1.run;
 
mphmesh(model)
This value corresponds to 1/50 of the largest axis-parallel distance, whereas the default value is 1/15.
Figure 3-2: Fine mesh (maximum element size = 0.02).
Sometimes a nonuniform mesh is desirable. Make a mesh that is denser on the left side by specifying a smaller maximum element size only on the edge segment to the left (edge number 1):
mesh1.feature('size').set('hauto',5);
 
size1 = ftri1.feature.create('size1','Size');
size1.set('hmax',0.02);
size1.selection.geom('geom1',1);
size1.selection.set(1);
 
mesh1.run
 
mphmesh(model)
.
Figure 3-3: Refined mesh on boundary 1(maximum element size = 0.02).
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1',2);
geom1.feature.create('r1','Rectangle');
mesh1 = comp1.mesh.create('mesh1');
ftri1 = mesh1.feature.create('ftri1','FreeTri');
mesh1.run;
mphmesh(model)
mesh1.feature('size').set('hauto',5);
mesh1.feature('size').set('hmax',0.02);
mesh1.run;
mphmesh(model)
mesh1.feature('size').set('hauto',5);
size1 = ftri1.feature.create('size1','Size');
size1.set('hmax',0.02);
size1.selection.geom('geom1',1);
size1.selection.set(1);
mesh1.run
mphmesh(model)
The Free Meshing Method
The default method to generate free triangle meshes in 2D is based on an advancing front algorithm. To switch to a Delaunay algorithm use the value del for the method property. Start by creating a geometry:
model = ModelUtil.create('Model');
 
comp1 = model.component.create('comp1', true);
 
geom1 = comp1.geom.create('geom1',2);
geom1.feature.create('r1','Rectangle');
 
c1 = geom1.feature.create('c1','Circle');
c1.set('r','0.5');
 
co1 = geom1.feature.create('co1','Compose');
co1.selection('input').set({'c1' 'r1'});
co1.set('formula','r1-c1');
 
geom1.runAll;
 
mesh1 = comp1.mesh.create('mesh1');
 
ftri1 = mesh1.feature.create('ftri1','FreeTri');
ftri1.set('method','del');
 
mesh1.run;
 
mphmesh(model,'mesh1')
Figure 3-4: Mesh created with the Delaunay method.
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1',2);
geom1.feature.create('r1','Rectangle');
c1 = geom1.feature.create('c1','Circle');
c1.set('r','0.5');
co1 = geom1.feature.create('co1','Compose');
co1.selection('input').set({'c1' 'r1'});
co1.set('formula','r1-c1');
geom1.runAll;
mesh1 = comp1.mesh.create('mesh1');
ftri1 = mesh1.feature.create('ftri1','FreeTri');
ftri1.set('method','del');
mesh1.run;
mphmesh(model,'mesh1')
Creating a 2D Mesh with Quadrilateral Elements
To create an unstructured quadrilateral mesh on a unit circle, enter:
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
 
geom1 = comp1.geom.create('geom1',2);
 
geom1.feature.create('c1','Circle');
 
mesh1 = comp1.mesh.create('mesh1');
 
mesh1.feature.create('ftri1','FreeQuad');
 
mesh1.run;
 
mphmesh(model)
Figure 3-5: Free quad mesh.
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1',2);
geom1.feature.create('c1','Circle');
mesh1 = comp1.mesh.create('mesh1');
mesh1.feature.create('ftri1','FreeQuad');
mesh1.run;
mphmesh(model)
Creating Structured Meshes
To create a structured quadrilateral mesh in 2D, use the Map operation. This operation uses a mapping technique to create the quadrilateral mesh.
Map in the COMSOL Multiphysics Programming Reference Manual
Use the EdgeGroup attribute to group the edges (boundaries) into four edge groups, one for each edge of the logical mesh. To control the edge element distribution use the Distribution attribute, which determines the overall mesh density.
Creating a Structured Quadrilateral Mesh
Create a structured quadrilateral mesh on a geometry where the domains are bounded by more than four edges:
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1',2);
geom1.feature.create('r1','Rectangle');
r2 = geom1.feature.create('r2','Rectangle');
r2.set('pos',[1 0]);
c1 = geom1.feature.create('c1','Circle');
c1.set('r','0.5');
c1.set('pos',[1.1 -0.1]);
dif1 = geom1.feature.create('dif1', 'Difference');
dif1.selection('input').set({'r1' 'r2'});
dif1.selection('input2').set({'c1'});
geom1.run('dif1');
 
mesh1 = comp1.mesh.create('mesh1');
 
map1 = mesh1.feature.create('map1','Map');
 
eg1 = map1.feature.create('eg1', 'EdgeGroup');
eg1.selection.set(1);
eg1.selection('edge1').set([1 3]);
eg1.selection('edge2').set(2);
eg1.selection('edge3').set(8);
eg1.selection('edge4').set(4);
 
eg2 = map1.feature.create('eg2', 'EdgeGroup');
eg2.selection.set(2);
eg2.selection('edge1').set(4);
eg2.selection('edge2').set([6 9 10]);
eg2.selection('edge3').set(7);
eg2.selection('edge4').set(5);
 
mesh1.run;
mphmesh(model);
Figure 3-6: Structured quadrilateral mesh (right) and its underlying geometry.
The left-hand side plot in Figure 3-6 is obtained with this command:
mphgeom(model, 'geom1', 'edgelabels','on')
The EdgeGroup attributes specify that the four edges enclosing domain 1 are boundaries 1 and 3; boundary 2; boundary 8; and boundary 4. For domain 2 the four edges are boundary 4; boundary 5; boundary 7; and boundaries 9, 10, and 6.
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1',2);
geom1.feature.create('r1','Rectangle');
r2 = geom1.feature.create('r2','Rectangle');
r2.set('pos',[1 0]);
c1 = geom1.feature.create('c1','Circle');
c1.set('r','0.5');
c1.set('pos',[1.1 -0.1]);
dif1 = geom1.feature.create('dif1', 'Difference');
dif1.selection('input').set({'r1' 'r2'});
dif1.selection('input2').set({'c1'});
geom1.run('dif1');
mesh1 = comp1.mesh.create('mesh1');
map1 = mesh1.feature.create('map1','Map');
eg1 = map1.feature.create('eg1', 'EdgeGroup');
eg1.selection.set(1);
eg1.selection('edge1').set([1 3]);
eg1.selection('edge2').set(2);
eg1.selection('edge3').set(8);
eg1.selection('edge4').set(4);
eg2 = map1.feature.create('eg2', 'EdgeGroup');
eg2.selection.set(2);
eg2.selection('edge1').set(4);
eg2.selection('edge2').set([6 9 10]);
eg2.selection('edge3').set(7);
eg2.selection('edge4').set(5);
mesh1.run;
mphmesh(model);
mphgeom(model, 'geom1', 'edgelabels','on')
Building a Mesh Incrementally
To build meshes in a step-by-step fashion, create selections for the parts of the geometry that you want to mesh in each step, as in this example:
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
 
geom1 = comp1.geom.create('geom1',2);
geom1.feature.create('r1','Rectangle');
geom1.feature.create('c1','Circle');
uni1 = geom1.feature.create('uni1', 'Union');
uni1.selection('input').set({'c1' 'r1'});
geom1.runCurrent;
del1 = geom1.feature.create('del1', 'Delete');
del1.selection('input').init(1);
del1.selection('input').set('uni1', 8);
geom1.run('del1');
 
mesh1 = comp1.mesh.create('mesh1');
 
dis1 = mesh1.feature.create('dis1', 'Distribution');
dis1.selection.set([2 4]);
dis1.set('type', 'predefined');
dis1.set('method', 'geometric');
dis1.set('elemcount', 20);
dis1.set('reverse', 'on');
dis1.set('elemratio', 20);
 
dis2 = mesh1.feature.create('dis2', 'Distribution');
dis2.selection.set([1 3]);
dis2.set('type', 'predefined');
dis2.set('method', 'geometric');
dis2.set('elemcount', 20);
dis2.set('elemratio', 20);
 
map1 = mesh1.feature.create('map1','Map');
map1.selection.geom('geom1', 2);
map1.selection.set(2);
mesh1.feature.create('frt1','FreeTri');
 
mesh1.run;
 
mphmesh(model)
The final mesh is in Figure 3-7. Note the effect of the Distribution feature, with which the distribution of vertex elements along geometry edges can be controlled.
Figure 3-7: Incrementally generated mesh (right).
The left-hand side plot in Figure 3-7 is obtained with this command:
mphgeom(model, 'geom1', 'edgelabels','on')
To replace the structured quad mesh by an unstructured quad mesh, delete the Map feature and replace it by a FreeQuad feature:
mesh1.feature.remove('map1');
mesh1.run('dis1');
fq1 = mesh1.feature.create('fq1', 'FreeQuad');
fq1.selection.geom('geom1', 2).set(2);
mesh1.run;
Thus, to get the FreeQuad feature before the FreeTri feature, the dis1 feature needs to be made the current feature by building it with the run method. Alternatively, parts of a mesh can be selectively removed by using the Delete feature. For example, to remove the structured mesh from domain 2 (along with the adjacent edge mesh on edges 3 and 4), and replace it with an unstructured quad mesh, enter these commands:
del1 = mesh1.feature.create('del1','Delete');
del1.selection.geom('geom1', 2).set(2);
del1.set('deladj','on');
frq1 = mesh1.feature.create('frq1','FreeQuad');
frq1.selection.geom('geom1', 2).set(2);
mesh1.run;
 
For further details on the various commands and their properties see the COMSOL Multiphysics Programming Reference Manual.
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1',2);
geom1.feature.create('r1','Rectangle');
geom1.feature.create('c1','Circle');
uni1 = geom1.feature.create('uni1', 'Union');
uni1.selection('input').set({'c1' 'r1'});
geom1.runCurrent;
del1 = geom1.feature.create('del1', 'Delete');
del1.selection('input').init(1);
del1.selection('input').set('uni1', 8);
geom1.run('del1');
mesh1 = comp1.mesh.create('mesh1');
dis1 = mesh1.feature.create('dis1', 'Distribution');
dis1.selection.set([2 4]);
dis1.set('type', 'predefined');
dis1.set('method', 'geometric');
dis1.set('elemcount', 20);
dis1.set('reverse', 'on');
dis1.set('elemratio', 20);
dis2 = mesh1.feature.create('dis2', 'Distribution');
dis2.selection.set([1 3]);
dis2.set('type', 'predefined');
dis2.set('method', 'geometric');
dis2.set('elemcount', 20);
dis2.set('elemratio', 20);
map1 = mesh1.feature.create('map1','Map');
map1.selection.geom('geom1', 2);
map1.selection.set(2);
mesh1.feature.create('frt1','FreeTri');
mesh1.run;
mphmesh(model);
mphgeom(model, 'geom1', 'edgelabels','on')
mesh1.feature.remove('map1');
mesh1.run('dis1');
fq1 = mesh1.feature.create('fq1', 'FreeQuad');
fq1.selection.geom('geom1', 2).set(2);
mesh1.run;
del1 = mesh1.feature.create('del1','Delete');
del1.selection.geom('geom1', 2).set(2);
del1.set('deladj','on');
frq1 = mesh1.feature.create('frq1','FreeQuad');
frq1.selection.geom('geom1', 2).set(2);
mesh1.run;
mphmesh(model);
Revolving a Mesh by Sweeping
Create 3D volume meshes by extruding and revolving face meshes with the Sweep feature. Depending on the 2D mesh type, the 3D meshes can be hexahedral (brick) meshes or prism meshes.
Create and visualize a revolved prism mesh as follows:
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
 
geom1 = comp1.geom.create('geom1', 3);
wp1 = geom1.feature.create('wp1', 'WorkPlane');
wp1.set('planetype', 'quick');
wp1.set('quickplane', 'xy');
c1 = wp1.geom.feature.create('c1', 'Circle');
c1.set('pos', [2, 0]);
rev1 = geom1.feature.create('rev1', 'Revolve');
rev1.set('angle2', '60').set('angle1', -60);
rev1.selection('input').set({'wp1'});
geom1.run('rev1');
 
mesh1 = comp1.mesh.create('mesh1');
mesh1.feature.create('ftri1', 'FreeTri');
mesh1.feature('ftri1').selection.geom(2);
mesh1.feature('ftri1').selection.set(2);
mesh1.runCurrent;
 
swe1 = mesh1.feature.create('swe1', 'Sweep');
swe1.selection.geom(3);
swe1.selection.add(1);
 
mesh1.run;
mphmesh(model)
To obtain a torus, leave the angles property unspecified; the default value gives a complete revolution.
Figure 3-8: 3D prism mesh created with the Sweep feature.
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 3);
wp1 = geom1.feature.create('wp1', 'WorkPlane');
wp1.set('planetype', 'quick');
wp1.set('quickplane', 'xy');
c1 = wp1.geom.feature.create('c1', 'Circle');
c1.set('pos', [2, 0]);
rev1 = geom1.feature.create('rev1', 'Revolve');
rev1.set('angle2', '60').set('angle1', -60);
rev1.selection('input').set({'wp1'});
geom1.run('rev1');
mesh1 = comp1.mesh.create('mesh1');
mesh1.feature.create('ftri1', 'FreeTri');
mesh1.feature('ftri1').selection.geom(2);
mesh1.feature('ftri1').selection.set(2);
mesh1.runCurrent;
swe1 = mesh1.feature.create('swe1', 'Sweep');
swe1.selection.geom(3);
swe1.selection.add(1);
mesh1.run;
mphmesh(model)
Extruding a Mesh by Sweeping
To generate a 3D prism mesh from the same 2D mesh by extrusion and then to plot it, enter these commands:
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 3);
wp1 = geom1.feature.create('wp1', 'WorkPlane');
wp1.set('planetype', 'quick');
wp1.set('quickplane', 'xy');
c1 = wp1.geom.feature.create('c1', 'Circle');
c1.set('pos', [2, 0]);
ext1 = geom1.feature.create('ext1', 'Extrude');
ext1.selection('input').set({'wp1'});
geom1.runAll;
 
mesh1 = comp1.mesh.create('mesh1');
 
ftri1 = mesh1.feature.create('ftri1', 'FreeTri');
ftri1.selection.geom('geom1', 2);
ftri1.selection.set(3);
 
dis1 = mesh1.feature.create('dis1', 'Distribution');
dis1.selection.set(1);
dis1.set('type', 'predefined');
dis1.set('elemcount', 20);
dis1.set('elemratio', 100);
 
swe1 = mesh1.feature.create('swe1', 'Sweep');
swe1.selection('sourceface').geom('geom1', 2);
swe1.selection('targetface').geom('geom1', 2);
 
mesh1.run;
mphmesh(model);
The result is shown in Figure 3-9. With the properties elemcount and elemratio the number and distribution of mesh element layers is controlled in the extruded direction.
Distribution in the COMSOL Multiphysics Programming Reference Manual or at the MATLAB prompt: mphdoc(model.mesh,'Distribution')
Figure 3-9: Extruded 3D prism mesh.
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 3);
wp1 = geom1.feature.create('wp1', 'WorkPlane');
wp1.set('planetype', 'quick');
wp1.set('quickplane', 'xy');
c1 = wp1.geom.feature.create('c1', 'Circle');
c1.set('pos', [2, 0]);
ext1 = geom1.feature.create('ext1', 'Extrude');
ext1.selection('input').set({'wp1'});
geom1.runAll;
mesh1 = comp1.mesh.create('mesh1');
ftri1 = mesh1.feature.create('ftri1', 'FreeTri');
ftri1.selection.geom('geom1', 2);
ftri1.selection.set(3);
dis1 = mesh1.feature.create('dis1', 'Distribution');
dis1.selection.set(1);
dis1.set('type', 'predefined');
dis1.set('elemcount', 20);
dis1.set('elemratio', 100);
swe1 = mesh1.feature.create('swe1', 'Sweep');
swe1.selection('sourceface').geom('geom1', 2);
swe1.selection('targetface').geom('geom1', 2);
mesh1.run;
mphmesh(model);
Combining Unstructured and Structured Meshes
By specifying selections for the meshing operations, swept meshing can also be combined with free meshing. In this case, start by free meshing domain 2, then sweep the resulting surface mesh through domain 1, as in this example:
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 3);
cone1 = geom1.feature.create('cone1', 'Cone');
cone1.set('r', 0.3).set('h', 1).set('ang', 9);
cone1.set('pos', [ 0 0.5 0.5]).set('axis', [-1 0 0]);
geom1.feature.create('blk1', 'Block');
 
mesh1 = comp1.mesh.create('mesh1');
 
ftet1 = mesh1.feature.create('ftet1', 'FreeTet');
ftet1.selection.geom('geom1', 3);
ftet1.selection.set(2);
 
swe1 = mesh1.feature.create('swe1', 'Sweep');
swe1.selection('sourceface').geom('geom1', 2);
swe1.selection('targetface').geom('geom1', 2);
 
mesh1.run;
mphmesh(model);
Figure 3-10: Combined structured/unstructured mesh.
The left-hand side plot in Figure 3-10 is obtained with this command:
mphgeom(model,'geom1','facemode','off','facelabels','on')
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 3);
cone1 = geom1.feature.create('cone1', 'Cone');
cone1.set('r', 0.3).set('h', 1).set('ang', 9);
cone1.set('pos', [ 0 0.5 0.5]).set('axis', [-1 0 0]);
geom1.feature.create('blk1', 'Block');
mesh1 = comp1.mesh.create('mesh1');
ftet1 = mesh1.feature.create('ftet1', 'FreeTet');
ftet1.selection.geom('geom1', 3);
ftet1.selection.set(2);
swe1 = mesh1.feature.create('swe1', 'Sweep');
swe1.selection('sourceface').geom('geom1', 2);
swe1.selection('targetface').geom('geom1', 2);
mesh1.run;
mphmesh(model);
mphgeom(model,'geom1','facemode','off','facelabels','on')
Creating Boundary Layer Meshes
For 2D and 3D geometries it is also possible to create boundary layer meshes using the BndLayer feature. A boundary layer mesh is a mesh with dense element distribution in the normal direction along specific boundaries. This type of mesh is typically used for fluid flow problems to resolve the thin boundary layers along the no-slip boundaries. In 2D, a layered quadrilateral mesh is used along the specified no-slip boundaries. In 3D, a layered prism mesh or hexahedral mesh is used depending on whether the corresponding boundary layer boundaries contain a triangular or a quadrilateral mesh.
If starting with an empty mesh, the boundary-layer mesh uses free meshing to create the initial mesh before inserting boundary layers into the mesh. This generates a mesh with triangular and quadrilateral elements in 2D and tetrahedral and prism elements in 3D. The following example illustrates this in 2D:
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
 
geom1 = comp1.geom.create('geom1', 2);
r1 = geom1.feature.create('r1', 'Rectangle');
r1.set('size', [10, 5]);
c1 = geom1.feature.create('c1', 'Circle');
c1.set('pos', [3.5 2.5]);
dif1 = geom1.feature.create('dif1', 'Difference');
dif1.selection('input').set({'r1'});
dif1.selection('input2').set({'c1'});
geom1.runAll;
 
mesh1 = comp1.mesh.create('mesh1');
 
bl1 = mesh1.feature.create('bl1', 'BndLayer');
bl1.feature.create('blp1', 'BndLayerProp');
bl1.feature('blp1').selection.set([2 3 5 6 7 8]);
 
mesh1.run;
mphmesh(model);
Figure 3-11: Boundary layer mesh based on an unstructured triangular mesh.
It is also possible to insert boundary layers in an existing mesh. Use the following meshing sequence with the geometry sequence from the previous example:
bl1.active(false);
 
fq1 = mesh1.feature.create('fq1', 'FreeQuad');
fq1.selection.set(1);
mesh1.run;
mphmesh(model)
 
bl1 = mesh1.feature.create('bl2', 'BndLayer');
bl1.feature.create('blp2', 'BndLayerProp');
bl1.feature('blp2').selection.set([2 3 5 6 7 8]);
mesh1.run;
mphmesh(model);
Figure 3-12: Initial unstructured quad mesh (left) and resulting boundary layer mesh (right).
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 2);
r1 = geom1.feature.create('r1', 'Rectangle');
r1.set('size', [10, 5]);
c1 = geom1.feature.create('c1', 'Circle');
c1.set('pos', [3.5 2.5]);
dif1 = geom1.feature.create('dif1', 'Difference');
dif1.selection('input').set({'r1'});
dif1.selection('input2').set({'c1'});
geom1.runAll;
mesh1 = comp1.mesh.create('mesh1');
bl1 = mesh1.feature.create('bl1', 'BndLayer');
bl1.feature.create('blp1', 'BndLayerProp');
bl1.feature('blp1').selection.set([2 3 5 6 7 8]);
mesh1.run;
mphmesh(model);
bl1.active(false);
fq1 = mesh1.feature.create('fq1', 'FreeQuad');
fq1.selection.set(1);
mesh1.run;
mphmesh(model)
bl1 = mesh1.feature.create('bl2', 'BndLayer');
bl1.feature.create('blp2', 'BndLayerProp');
bl1.feature('blp2').selection.set([2 3 5 6 7 8]);
mesh1.run;
mphmesh(model);
Refining Meshes
Given a mesh consisting only of simplex elements (lines, triangles, and tetrahedra) you can create a finer mesh using the feature Refine. Enter this command to refine the mesh:
mesh1.feature.create('ref1', 'Refine');
By specifying the property tri, either as a row vector of element numbers or a 2-row matrix, the elements to be refined can be controlled. In the latter case, the second row of the matrix specifies the number of refinements for the corresponding element.
The refinement method is controlled by the property rmethod. In 2D, its default value is regular, corresponding to regular refinement, in which each specified triangular element is divided into four triangles of the same shape. Setting rmethod to longest gives longest edge refinement, where the longest edge of a triangle is bisected. Some triangles outside the specified set might also be refined in order to preserve the triangulation and its quality.
In 3D, the default refinement method is longest, while regular refinement is only implemented for uniform refinements. In 1D, the function always uses regular refinement, where each element is divided into two elements of the same shape.
For stationary or eigenvalue PDE problems you can use adaptive mesh refinement at the solver stage with the adaption solver step. See Adaption in the COMSOL Multiphysics Programming Reference Manual.
Copying Boundary Meshes
Use the CopyEdge feature in 2D and the CopyFace feature in 3D to copy a mesh between boundaries.
The following example demonstrates how to copy a mesh between two boundaries in 3D and then create a swept mesh on the domain:
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
 
geom1 = comp1.geom.create('geom1', 3);
wp1 = geom1.feature.create('wp1', 'WorkPlane');
wp1.set('planetype', 'quick');
wp1.set('quickplane', 'xy');
c1 = wp1.geom.feature.create('c1', 'Circle');
c1.set('r', 0.5).set('pos', [1, 0]);
rev1 = geom1.feature.create('rev1', 'Revolve');
rev1.set('angle1', 0).set('angle2', 180);
rev1.selection('input').set({'wp1'});
geom1.run('wp1');
 
mesh1 = comp1.mesh.create('mesh1');
 
size1 = mesh1.feature.create('size1', 'Size');
size1.selection.geom('geom1', 1);
size1.selection.set(18);
size1.set('hmax', '0.06');
 
ftri1 = mesh1.feature.create('ftri1', 'FreeTri');
ftri1.selection.geom('geom1', 2);
ftri1.selection.set(10);
 
cpf1 = mesh1.feature.create('cpf1', 'CopyFace');
cpf1.selection('source').geom('geom1', 2);
cpf1.selection('destination').geom('geom1', 2);
cpf1.selection('source').set(10);
cpf1.selection('destination').set(1);
 
sw1 = mesh1.feature.create('sw1', 'Sweep');
sw1.selection('sourceface').geom('geom1', 2);
sw1.selection('targetface').geom('geom1', 2);
 
mesh1.run;
mphmesh(model);
The algorithm automatically determines how to orient the source mesh on the target boundary. The result is shown in Figure 3-13.
Figure 3-13: Prism element obtained with the CopyFace and Sweep features.
To explicitly control the orientation of the copied mesh, use the EdgeMap attribute. The command sequence:
em1 = cpf1.feature.create('em1', 'EdgeMap');
em1.selection('srcedge').set(18);
em1.selection('dstedge').set(2);
mesh1.feature.remove('sw1');
mesh1.feature.create('ftet1', 'FreeTet');
 
mesh1.run;
mphmesh(model);
copies the mesh between the same boundaries as in the previous example, but now the orientation of the source mesh on the target boundary is different. The domain is then meshed by the free mesh, resulting in the mesh in Figure 3-14. In this case it is not possible to create a swept mesh on the domain because the boundary meshes do not match in the sweeping direction.
Figure 3-14: Free tetrahedral mesh after the use of the CopyFace feature.
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 3);
wp1 = geom1.feature.create('wp1', 'WorkPlane');
wp1.set('planetype', 'quick');
wp1.set('quickplane', 'xy');
c1 = wp1.geom.feature.create('c1', 'Circle');
c1.set('r', 0.5).set('pos', [1, 0]);
rev1 = geom1.feature.create('rev1', 'Revolve');
rev1.set('angle1', 0).set('angle2', 180);
rev1.selection('input').set({'wp1'});
geom1.run('wp1');
mesh1 = comp1.mesh.create('mesh1');
size1 = mesh1.feature.create('size1', 'Size');
size1.selection.geom('geom1', 1);
size1.selection.set(18);
size1.set('hmax', '0.06');
ftri1 = mesh1.feature.create('ftri1', 'FreeTri');
ftri1.selection.geom('geom1', 2);
ftri1.selection.set(10);
cpf1 = mesh1.feature.create('cpf1', 'CopyFace');
cpf1.selection('source').geom('geom1', 2);
cpf1.selection('destination').geom('geom1', 2);
cpf1.selection('source').set(10);
cpf1.selection('destination').set(1);
sw1 = mesh1.feature.create('sw1', 'Sweep');
sw1.selection('sourceface').geom('geom1', 2);
sw1.selection('targetface').geom('geom1', 2);
mesh1.run;
mphmesh(model);
em1 = cpf1.feature.create('em1', 'EdgeMap');
em1.selection('srcedge').set(18);
em1.selection('dstedge').set(2);
mesh1.feature.remove('sw1');
mesh1.feature.create('ftet1', 'FreeTet');
mesh1.run;
mphmesh(model);
Converting Mesh Elements
Use the Convert feature to convert meshes containing quadrilateral, hexahedral, or prism elements into triangular meshes and tetrahedral meshes. In 2D, the function splits each quadrilateral element into either two or four triangles. In 3D, it converts each prism into three tetrahedral elements and each hexahedral element into five, six, or 28 tetrahedral elements. To control the method used to convert the elements, use the property splitmethod.
For additional properties supported, see Convert in the COMSOL Multiphysics Programming Reference Manual or at the MATLAB prompt: mphdoc(model.mesh,'Convert')
This example demonstrates how to convert a quad mesh into a triangle mesh:
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 2);
geom1.feature.create('c1', 'Circle');
geom1.feature.create('r1', 'Rectangle');
int1 = geom1.feature.create('int1', 'Intersection');
int1.selection('input').set({'c1' 'r1'});
mesh1 = comp1.mesh.create('mesh1', 'geom1');
mesh1.feature.create('fq1', 'FreeQuad');
mesh1.runCurrent;
mphmesh(model);
mesh1.feature.create('conv1', 'Convert');
mesh1.run;
mphmesh(model);
The result is illustrated in the Figure 3-15:
Figure 3-15: Mesh using free quad elements (left) and converted mesh from quad to triangle (right).
Code for use with MATLAB®
model = ModelUtil.create('Model');
comp1 = model.component.create('comp1', true);
geom1 = comp1.geom.create('geom1', 2);
geom1.feature.create('c1', 'Circle');
geom1.feature.create('r1', 'Rectangle');
int1 = geom1.feature.create('int1', 'Intersection');
int1.selection('input').set({'c1' 'r1'});
mesh1 = comp1.mesh.create('mesh1', 'geom1');
mesh1.feature.create('fq1', 'FreeQuad');
mesh1.runCurrent;
mphmesh(model);
mesh1.feature.create('conv1', 'Convert');
mesh1.run;
mphmesh(model);