CALFEM Mesh Module Manual
CALFEM Mesh Module Manual
0–1
Introduction
The functions in this module makes it possible to generate a two dimensional mesh with
small effort. It also allows extraction and manipulation of the mesh with operations such
as degree of freedom extraction, mesh merging, conversion between quadrilateral and tri-
angular elements, improvement and control of mesh quality etc.
Mesh properties
Meshing involves the division of a physical domain, into smaller domains - elements -
described by nodes. The connection to the physical space is achieved trough the global
nodal coordinate matrix Coord. This matrix is a list of global coordinates for each node in
the mesh. In each node a certain number of degrees of freedom are present. A key feature
that characterize the mesh is the connectivity matrix, the global degree of freedom matrix
Edof associating the degrees of freedom of each element to the global degree of freedom
matrix Dof . The later being a list of global nodal degrees of freedom.
Figure 1: Example of nodal coordinates, and degrees of freedom, elements using triangular
elements and two degrees of freedom per node.
0–1 Introduction
Mesh generation
The mesh geometry
In order to generate these matrices for a large number of elements, the physical domain to
be meshed has to be defined systematically. In this module the mesh geometry is defined
using the basic concepts of vertices, segments, and surfaces.
2.5
22 2(15) 3
11 6(15) 6 3(5)
2
0.5 5(5)
0 5 4(5) 4
−0.5
0 0.5 1 1.5 2 2.5 3 3.5 4
Figure 2: A surface description of a domain. The domain is divided into two surfaces using
seven segments and six vertices.
Figure 2 showing a domain with two surfaces, seven segments and six vertices (surface
numbers are shown in a square and segment numbers are shown underlined). The basic
mesh geometry for this case is defined by the matrices
6 1
0. 2.
1 2
4. 2. " #
2 3
4. 0. 7 6 1 2
Vertices =
Segments = 3 4 Surfaces = (1)
3. 0.
4 5 7 3
4 5
3. 1.
5 6
0. 1.
5 2
Introduction 0–2
The vertices matrix is a coordinate list. The numbers (followed by a dot) on each row are
the coordinates of the vertices, numbered in the order shown in Figure 2. The segment
matrix is a vertices list, with the vertex numbers of the segments on each row. Finally, the
surface matrix is a segment list with rows containing segment numbers for each surface.
Using the strMeshgen function with the surface defined domain, the matrices in (1),
as input variables and also specifying 15 elements on segment 1, 5 elements on segment
2, etc, yields the mesh in Figure 3. The unstructured meshing approach is conducted in
2.5
1.5
1
y
0.5
−0.5
0 0.5 1 1.5 2 2.5 3 3.5 4
x
a similar fashion. The figure 4 illustrates a boundary defined domain. The matrices in
(1) are (accordingly) slightly changed, meaning that segment 7 is dropped in the segments
matrix and that the surface matrix will be reading,
h i
Surfaces = 1 2 3 4 5 6 . (2)
Using the unstrMeshgen function with the modified geometry matrices and an information
about the approximate element size generates the mesh shown in Figure 5
0–3 Introduction
2.5
22 2 3 3 4
1.5 1 4
11 8 8 5
0.5 7 5
0 7 6 6
−0.5
0 0.5 1 1.5 2 2.5 3 3.5 4
2.5
1.5
1
y
0.5
−0.5
0 0.5 1 1.5 2 2.5 3 3.5 4
x
Introduction 0–4
More meshing tools
Functions for displaying basic mesh geometry, extracting degrees of freedom, manipulating
the mesh, etc are described next.
1.5
2
4
3(5) 1(5) 0.5
y
1
0
2(5) 3
−0.5
−1
Figure 6: Illustration of using mesh of a triangle to generate a half circle using stitchM esh.
The geomdraw2 (left) displays the input given strMeshgen and by apply stitchM esh twice
on the triangle to generate the half circle (right).
Curved segments
The structured meshing is a four/eight point mapping from a computational domain on the
physical domain. The four point assumes straight segments while eight points associates
to each segment an additional point added to the point list, and a curve is interpolated
from these three points. The number of the point is added to a separate list, the segment
property list. Figure 6 illustrates the curved segments of a hole in a square shape and the
surface description used for this.
0–5 Introduction
Extraction functions
The vertices mentioned earlier are examples of points needed in order to define the mesh.
However, these are only a subset of the point list generally needed in a finite element
calculation. Loading points, constraint points etc also have to be defined and included in
the point list.
Knowing particular degrees of freedom at various points, segments, and surfaces and
also knowing element numbers at surfaces in the mesh are essential in finite element anal-
ysis. For example if a point load should be applied at a specific point, this point has to
be defined as a fixed point in the mesh and the degrees of freedom at that node has to be
known for the load application. Another example, if a boundary condition is to be applied
at a part of the boundary in a particular direction, these degrees of freedom need to be
known and extracted from a pre defined segment associated with that part of the boundary.
Or if a model contain more than one material definition, the mesh has to be characterized
with different surfaces for each material. The element numbers of these elements need to be
extracted, thus making it possible to assign a certain material property to these elements.
These are a few example of what a meshing tool should provide in order to avoid the very
tedious work of finding the correct degree of freedom in a long list of numbers.
The meshing tools available for these operations are the extraction functions working
on points, segments, and surfaces. The functions are extrPoint, extrSeg, and extrSurf.
The output from the two first functions are the degrees of freedom of specified points and
segments respectively. The second also yields the point numbers of the segments. The
third function yields the element numbers and the point numbers of specified surfaces.
Introduction 0–6
Manual pages
The functions available are described in detail in this section. Further information as func-
tional dependencies within the package is obtained by typing at command prompt help
function name. A general feature for all but geomdraw2 is the place holder meshdb vari-
able that can replace input and output.
Meshing Functions
NOTE:
Before generating the mesh, use geomdraw2 to validate that the proper settings are
done.
Purpose:
Displays the basic geometry for the mesh in order to establish correct basic properties
for the strMeshgen and unstrMeshgen.
Syntax:
[ totarea noelements] = geomdraw2(vertices,segments, surfaces,segp,mp)
Description:
The physical domain is characterized by specification of (convex) (sub)domain, sur-
face. The surface(s) is defined by listing it’s consecutive enclosing segments, listed
in a matrix similar to Edof , denoted here as surf aces. The coordinates of vertices
is in a format similar to Coord, the vertices in segments is specified by the row index
of the starting and the end vertex in the vertex list,
x1 y1 vertex1 vertex2
x2 y2
vertex2 vertex3
vertices =
x3 y3 segments =
vertex3 vertex4
.. .. .. ..
. . . .
xn v n vertexm−1 vertexm
The surface element topology matrix by row wise specify each elements segments
index,
segment1 segment2 ... segmentn
. . .. ..
surfaces = .. .. . .
segmentk−n segmentk−n+1 ... segmentk
The segment property list, segpr, contains the properties associated with each seg-
ment, where bias and curve row is optional,
seed1 seed2 . . . seedk
segpr =
curve1 curve2 . . . curvek
bias1 bias2 . . . biask
The seed is the number cells on interior segment, the curve is the index of the vertex
associated to each segment used for the interpolation and thus gives three points
representation, where 0 index indicates linear segment. The bias is an asymmetric
distribution of the points on the segment, range (−1, 1) where positive is clustering
at end point.
The mp describes the mesh properties, which affects the Edof and Coord, for the
boundary domain specification mp = [dpn nen], where dpn is the degrees of freedom
per node and nen the number of element nodes. The output totarea and noelements
gives the area of the domain respective estimated number of elements from mesh
generation.
Purpose:
Generates a structured mesh.
Syntax:
[coord edof dofs meshdb]=strMeshgen(vertices,segments,surfaces,segp,mp)
[meshdb]=strMeshgen(vertices,segments,surfaces,segp,mp)
Description:
The mesh is generated by mapping a regular grid into specified sub domain. The
procedure in dividing the domain restrict the segpr. Therefore seed assumes the
same seed value for all triangular surfaces, while quadrilaterals same seed on opposite
segments of the surface. The bias works only for quadrilateral surfaces. If higher
order surfaces than quadrilateral is used, the seed is the mean value of the seed
vector. See coordxtr for further details of the definitions of edof , dof s, coord and
geomdraw2 for vertices, segments, surf aces, mp and segpr. meshdb is a place
holder of all relevant data for the mesh.
Example:
The example from figure 1 in introduction,
See also:
geomdraw2, DBextract
Purpose:
Generate unstructured mesh using randomized incremental triangulation algorithm
with delaunay condition enforced.
Syntax:
[coord edof dofs meshdb]=unstrMeshgen(vertices,segments,surfaces,segp,mp)
[meshdb]=unstrMeshgen(vertices,segments,surfaces,segp,mp)
Description:
The delaunay mesh is a mesh satisfy the condition that given any triangle element
in mesh, its circumcircle contain no interior nodes of the mesh. For quadrilateral
element input (nen = 4) the mesh is computed by obtaining the triangular mesh
for the twice of the element area and then apply convertM esh to obtain the final
mesh. See coordxtr for further details of the definitions of edof , dof s, coord and
geomdraw2 for vertices, segments, surf aces, mp and segpr.
Example:
Unstructured mesh generation of domain presented in Figure 2,
See also:
geomdraw2, convertMesh
Purpose:
Is used to merge two meshes.
Syntax:
[ coord edofs dofs meshdb ] = mergeMesh(coord1 ,edofs1 ,dofs1 ,coord2 ,edofs2 ,dofs2 )
[ meshdb ] = mergeMesh(meshdb1 ,meshdb2 )
Description:
The boundary vertices are identified and the global topology matrix for nodes, global
coordinates are remapped to the unique points in the mesh. See coordxtr for further
details of the definitions of edof , dof s, coord.
Example:
An alternative mesh generation of Figure 3,
Note:
Using extract function upon merged mesh may require a redefinition of vertices and
segments.
See also:
stitchMesh,
Purpose:
Is used to refine the mesh by subdivision each element.
Syntax:
[ coord edofs dofs meshdb ] = refineMesh(coord,edofs,dofs)
[ meshdb ] = refineMesh(meshdb)
Description:
The mesh elements are divided into four sub elements, by adding vertices on the
midpoint of each segments and one in the interior. See coordxtr for further details
of the definitions of edof , dof s, coord.
Example:
A modification of mesh generation from input for figure 5,
Note:
This routine is the fastest meshing routine in this package, recommended to be used
whenever convergence study is performed.
See also:
convertMesh
Purpose:
Improve the quality of mesh by redistribute position of interior nodes, called smooth-
ing.
Syntax:
[ coord disp meshdb ] = smoothMesh(coord,edofs,dofs)
[ meshdb ] = smoothMesh(meshdb)
Description:
The smoothing is performed by average over nodes from the surrounding neighbour
cells. See coordxtr for further details of the definitions of edof , dof s, coord and
geomdraw2 for vertices and segments.
Example:
A modification of mesh generation from input for Figure 5,
See also:
convertMesh
Purpose:
A routine to convert between quadrilateral and triangular mesh.
Syntax:
[ coord edofs dofs meshdb ] = convertMesh(coord,edofs)
[ meshdb ] = convertMesh(meshdb)
Description:
The conversion from quadrilateral mesh into triangular is performed by dividing
the quadrilateral by the edge of shortest distance between diagonal vertex. The
conversion from triangular into quadrilateral are performed by collapsing eligible
triangles and remaining triangles are divided into three sub quadrilaterals by adding
a point in the center of the element. This routine work as a ref ineM esh followed
by correctM esh. See coordxtr for further details of the definitions of edof , dof s,
coord and geomdraw2 for vertices and segments.
Example:
A modification of mesh generation from input for figure 5,
Note:
By repeating this operation the command act as a refinement on the mesh with
the important difference to ref ineM esh that edge flipping is allowed (i.e. delaunay
condition ) to alter and hence the function correct low quality mesh and improving
the q factor.
See also:
refineMesh
Purpose:
To extract dofs and vertex row index to vertices from the mesh along a given segment
list.
Syntax:
[ dof vertex ] = extrSeg(segidx,segments,segpr,vertices,dofs,coord,ndof,d))
[ dof vertex ] = extrSeg(segidx,segments,vertices,dofs,coord,ndof,d))
[ dof vertex ] = extrSeg(segidx,meshdb,ndof)
Description:
The list of segments row indices is matched to vertices over mesh along a search stripe
of width d. Identical numbers are removed from the list and it is returned sorted.
See coordxtr for further details of the definitions of dof s, coord and geomdraw2 for
vertices and segments.
Example:
Figure 3 revisited, dof’s for segment 1 and 2 are extracted,
Note:
The segments can be redefined after mesh generation, segments in this context is
differentiated from the previous defined by allowing any line segment between any
vertices and vertices list can be any coordinate list. Note that if curve is used then
segpr must be inserted as well. The ndof tells which global degree of freedom of the
node to be chosen, if omitted all to given node is listed.
See also:
extrPoint, extrSurf
Purpose:
To extract dofs and vertex row index from the mesh at vertices defined.
Syntax:
[ dof vertex ] = extrPoint(vertidx,vertices,coord,dofs,ndof)
[ dof vertex ] = extrPoint(vertidx,meshdb,ndof)
Description:
The vertex row index list vertidx is matched to vertices over mesh. The ndof tells
which global degree of freedom of the node to be chosen, if omitted all to given
node is listed. See coordxtr for further details of the definitions of dof s, coord and
geomdraw2 for vertices.
Example:
Figure 3 revisited, dof’s for vertex 1 and 2 are extracted,
Note:
The vertices can even be interior points, in other words, vertices is differentiated
from the previous defined by allowing any point.
See also:
extrSeg,extrSurf
Manual pages 0 – 10
Manual pages extrSurf
Purpose:
extracts row index of edof and row index for Coord for given surface row index.
Syntax:
[ el np ] = extrSurf(meshdb,surfidx)
Description:
Given row index list of surf aces, surf idx, the associated row index list el for edof s
and nodes np in coord extracted.
Example:
The geometry of Figure 1 revisited,
0 – 11 Manual pages
DBextract Manual pages
Purpose:
To extract coord,edofs,dofs and nen from meshdata db or any other specified tag
name.
Syntax:
[ coord edofs dofs nen] = DBextract(meshdb)
[ var ] = DBextract(meshdb,’var’)
Description:
The use of datatype struct as place holder of data is a convenient object, the actual
information about its contents can be further pursued in the help files. See coordxtr
for further details of the definitions of edof , dof s, coord.
Example:
The geometry of Figure 1 revisited,
Tip:
In command prompt, type the variable name meshdb to list its contents.
Manual pages 0 – 12
Manual pages correctMesh
Purpose:
To improve the quality of the mesh.
Syntax:
[ coord edofs dof disp meshdb ] = correctMesh(coord,edof,dofs,sel,disp)
[ meshdb ] = correctMesh(meshdb)
Description:
The sel list is a sub set of the row index list to edof s to which quality is improved.
For the triangle mesh delaunay condition is enforced, see unstrM eshgen, and for
quadrilateral illegal elements such as non-convex is fixed. See coordxtr for further
details of the definitions of edof , dof s, coord
Example:
Tip:
sel from extractBoundarydof s provides the non-convex elements for quadrilaterals.
See also:
extractBoundaryDofs
0 – 13 Manual pages
checkMesh Manual pages
Purpose:
Compute the quality of the mesh.
Syntax:
[ qe qn J] = checkMesh(coord,edof,dofsPerNode)
[ qe qn J] = checkMesh(meshdb)
Description:
Computes the q factor for given mesh and the Jacobian of each element. The qe
factor is a element property, the qn factor for the nodes are the averaged q factor
over all surrounding elements. The J gives the area of the domain. See coordxtr for
further details of the definitions of edof and coord
Example:
Manual pages 0 – 14
Manual pages stitchMesh
Purpose:
Merge two mesh by joining two segments by translation, rotation and scaling.
Syntax:
[ qe qn J] = stitchMesh(meshdb1 ,SegID1 ,meshdb2 ,SegID2 )
[ qe qn J] = checkMesh(meshdb)
Description:
The mesh meshdb1 is translated from starting point for segment SegID1 to the
starting point of segment SegID2 for meshdb2 , then scaled and rotated to align with
meshdb2 , finally the mesh is merged by mergeM esh.
Example:
The mesh generation of figure 6,
See also:
translateMesh,rotateMesh,scaleMesh
0 – 15 Manual pages
Examples
L-shape console
For the figure 2, surface domain description, the following input defines two surfaces defined
by
At this point one can either use strMeshgen leading to figure 3 or unstrMeshgen which
gives the result in figure 8, which can be compared to figure 4.
The smoothing smoothMesh was combined with the DBextract in one command, this can
generally be achieved for all commands apart from geomdraw2 .
2.5
1.5
1
y
0.5
−0.5
0 0.5 1 1.5 2 2.5 3 3.5 4
x
Figure 7: A surface description of a domain. The domain is divided into two surfaces using
seven segments and six vertices.
0–1 Examples
A square shape with hole
w=sqrt(2)/6;
vertices = [ 0 0 ; 1/3 0 ; 2/3 0 ; 1 0 ; ...
0 1/3 ; 1/3 1/3 ; 2/3 1/3 ; 1 1/3 ; ...
0 2/3 ; 1/3 2/3 ; 2/3 2/3 ; 1 2/3 ; ...
0 1 ; 1/3 1 ; 2/3 1; 1 1 ; ...
1/2 (1/2-w) ; (1/2+w) 1/2; 1/2 (1/2+w) ; (1/2-w) 1/2];
% define segments
segments =[ 1 2; 2 3; 3 4; 1 5; 2 6; 3 7; 4 8; ...
5 6; 6 7; 7 8; 5 9; 6 10; 7 11; 8 12; ...
9 10; 10 11; 11 12;9 13; 10 14; 11 15; 12 16; ...
13 14; 14 15; 15 16];
% define surfaces
surfaces=[ 1 5 8 4 ; 2 6 9 5 ; 3 7 10 6;8 12 15 11; ...
10 14 17 13; 15 19 22 18;16 20 23 19;17 21 24 20];
% assign seed, curve
seed=ones(1,24)*4;curve=zeros(1,24);
curve([ 9 13 16 12])=[ 17 18 19 20];segpr=[seed;curve];
% mesh properties
mp=[ 2 3];
db=smoothMesh(convertMesh(strMeshgen(vertices,segments,surfaces,segpr,mp)));
% plot mesh
[coord edof dof nen]=DBextract(db);
[ex ey]=coordxtr(edof,coord,dof,nen);eldraw2(ex,ey,[1 4 1]);
0.9
0.8
0.7
0.6
0.5
y
0.4
0.3
0.2
0.1
0
0 0.2 0.4 0.6 0.8 1
x
Figure 8: A surface description of a domain with a hole, figure 6. The domain is divided
into 8 surfaces using and triangulated followed by conversion to quadrilateral elements,
convertMesh.
The above is essential the same as previous section but with convertMesh added to illustrate
further the convenience by using nested calls.
Examples 0–2
Extraction and meshdb manipulation
This section serves the purpose to illustrate that vertices and segments can be redefined
any time after the mesh generation arbitrary. By using the mesh data db from example
”A square with hole” together with redefinition of vertices and segments, stitchMesh under
the mesh/utilities allows more complex regions to be build,
2.5
1.5
1
y
0.5
−0.5
0 0.5 1 1.5 2 2.5 3 3.5 4
x
0–3 Examples
Now we seek the dof’s in a direction not defined in geometry for db, on the line between
the point (0, 1) and (1/2, 1). To achieve this, the recreate vertices and segments is redefined
and extrSeg is called.
ans =
212
214
216
218
220
256
258
Examples 0–4
Quality improvement of mesh
This module offers two functions to improve the quality of the mesh, with q-factor (q)
as a measure, one by adjusting the coordinates (smoothM esh), another by rearrange the
topology (correctM esh). By starting from section ”L-shaped console”,
>> db1=smoothMesh(correctMesh(smoothMesh(db)));
>> db1=checkMesh(db1);db=checkMesh(db);
>> subplot(221);hist(db.q);
>> subplot(222);hist(db1.q);
>> subplot(223);[ex ey]=coordxtr(db.edofs,db.coords,db.dofs,db.nen);eldraw2(ex,ey,[1 4
>> subplot(224);[ex ey]=coordxtr(db1.edofs,db1.coords,db1.dofs,db1.nen);eldraw2(ex,ey,
The figure 10 show the improvement, this is a significant result and altogether an useful
tool in creating high quality mesh.
40 80
30 60
20 40
10 20
0 0
0.4 0.6 0.8 1 0.4 0.6 0.8 1
2 2
1 1
y
0 0
0 1 2 3 4 0 1 2 3 4
x x
Figure 10: Improvement of mesh from ”L-shaped console” section, using checkM esh,
correctM esh and smoothM esh.
0–5 Examples
Advanced example using vertices2segment
To gain further insight in how to use the mesh generator to generate input, the section ”A
square with a hole” is remade using the vertices2segment from mesh/dependicies.
Examples 0–6