Professional Documents
Culture Documents
Matlab Implementation of The Finite Element Method in Elasticity
Matlab Implementation of The Finite Element Method in Elasticity
1. Introduction
Unlike complex black-box commercial computer codes, this paper provides a
simple and short open-box Matlab implementation of P1 and Q1 nite elements
(FE) for the numerical solutions of linear elasticity problems. Instead of covering
all kinds of possible problems in one code, the proposed tool aims to be plain,
easy to understand, and to modify. Therefore, only simple model examples are
included to be adapted to whatever is needed in the spirit of [1]. We present an
error estimator which illustrates the accuracy of the numerical calculation.
A dierent approach is realized by the company Comsol with the nite element
package Femlab [7]. It is a tool for the solution of partial dierential equations of
many physical phenomena, including structural mechanics. A special feature is
the comfortable mesh generator and the possibility to combine dierent physical
problems. The code is written for the most part in Matlab and can be modied,
except for the mesh generator and the solver for nonlinear problems. The basis
functions in the nite element method can be chosen to be linear, quadratic, cubic
or of fourth order. The solver can use adaptivity as well.
The plan of our paper is as follows. The model problem, the Navier-Lame
equations, with general boundary conditions is described in Sect. 2; the weak
formulation and discretisation in Sect. 3. The heart of this contribution is the data
representation of the triangulation, the Dirichlet and Neumann boundary in
240
J. Alberty et al.
Sect. 4 together with the discrete space. The main steps are the assembly procedure of the stiness matrix in Sect. 5, the right-hand side in Sect. 6, and the
incorporation of the Dirichlet boundary conditions in Sect. 7. A post-processing
routine to preview the numerical solution is given in Sect. 8; in Sect. 9 an implementation of an a posteriori error estimator is presented [3], [4], [5]. Applications illustrate the usage of the new tools in Sects. 10, 11, 12, and 13 and serve as
examples for writing the user-specied Matlab routines f.m, g.m, and u d:m. The
main program is given partly in these sections and in its total in the appendices.
The programs are written for Matlab 6 but adaptation for earlier versions is
possible. For a nite element calculation it is necessary to run the main
program with (user-specied les) coordinates.dat, elements.dat,
dirichlet.dat, and neumann.dat if necessary, as well as the sub-routines
f.m, g.m, and u d:m. The graphical representation is performed with the function
show.m and the error is estimated in aposteriori.m. All les with the code
and the data for all examples can be downloaded from www.math.tuwien.ac.at/
carsten/.
2. Model Problem
The proposed Matlab program employs the nite element method to calculate
a numerical solution U which approximates the solution u to the following
d-dimensional Navier-Lame problem (P ). Let X Rd be a bounded Lipschitz
domain with polygonal boundary C. On some closed subset CD of the
boundary with positive length, we assume Dirichlet conditions while we have
Neumann boundary conditions on the (possible empty) part CN . The d components of the displacement u need not satisfy either Dirichlet- or Neumann
conditions, i.e., CD and CN may overlap. Given f 2 L2 X, M 2 L1 CD dd ,
w 2 H 1 X, and g 2 L2 CN as well as the positive parameters k and l, seek
u 2 H 1 X with
k lr div uT lDu f
in X;
k truI 2lu n g
Mu w
on CN ;
on CD ;
2
3
v : Cu dx
Z
X
f v dx
Z
CN
g v ds:
241
vh : Cuh dx
f vh dx
g vh ds:
CN
Let T denote a triangulation of X specied in Sect. 4 and let N be the set of all
nodes in T; set g1 ; . . . ; gdN u1 e1 ; u1 e2 ; . . . ; u1 ed ; . . . ; uN e1 ; uN e2 ; . . . ; uN ed
be the nodal basis of the nite dimensional space S, where N is the number of
nodes of the mesh, d the dimension of the displacement vector, and uz is the scalar
hat function of node z in the triangulation T, i.e., uz z 1 and uz y 0 for all
y 2 N with y 6 z. Up to geometric boundary conditions (involved in Sect. 7), (5)
reads
Z
gk : Cuh dx
f gk dx
g gk ds
gk : Cg dx U
X
k 1; . . . ; dN :
CN
f gk dx
PdN
g gk ds
k 1; . . . ; dN : 7
CN
The coecient matrix (global stiness matrix) A Ak 2 RdN dN and the righthand side b bk 2 RdN are dened as
Ak
Z
X
gk : Cg dx
and
bk
Z
X
f gk dx
g gk ds:
CN
The coecient matrix is sparse, symmetric and positive semidenite. The condition Muh wh on CD and Mvh 0 on CD will be incorporated in Sect. 7 via
Lagrange multipliers.
242
J. Alberty et al.
XZ
T 2T
bj
XZ
T 2T
f gj dx
XZ
ECN
9
g gj ds:
10
243
The les neumann.dat and dirichlet.dat contain the two node numbers
which bound the corresponding edge on the boundary. In this way, the sum over
E CN results in a loop over all entries in neumann.dat. The right-hand side f
and g is evaluated in Sect. 6 and the Dirichlet conditions from u d:m are discussed
in Sect. 7.
244
J. Alberty et al.
gpT ;k : CgpT ; dx
k; 1; . . . ; dK
11
and are assembled to the global stiness matrix A. This means, the entry Apq is the
sum of STIMAT k over all elements T with p pT ; k and q pT ; , which is
performed by the following Matlab commands for the two-dimensional case for
triangles and quadrilaterals.
The local stiness matrix for each element T requires an individual discussion for
triangular, tetrahedral, and quadrilateral elements.
1 0
1
@u1 =@x
11 u
A @ 22 u A:
@u2 =@y
cu @
@u2 =@x @u1 =@y
212 u
12
245
1 0
r11
k 2l
@ r22 A @ k
0
r12
10
1
k
0
11 u
k 2l 0 A@ 22 u A : Ccu:
0
l
212 u
13
14
2r12 12
From (11) and relation (14) and the fact that gpT ;k etc. are ane on T it follows
that
Z
cgpT ;k T CcgpT ; dx jT jcgpT ;k T CcgpT ; :
15
STIMAT k
T
6
X
j1
0 1
10 1
u1
u1
uk1 ;x 0 uk2 ;x 0 uk3 ;x 0
B . C
B . C
jT @ 0 uk1 ;y 0 uk2 ;y 0 uk2 ;y A@ .. A : R@ .. A
uk1 ;y uk1 ;x uk2 ;y uk2 ;x uk3 ;y uk2 ;x
u6
u6
!
uj gpT ;j
16
and hence expression (15) can be written simultaneously for all indices as
STIMAT jT jRT CR:
17
This function makes use of the fact that the gradients of the scalar basis functions
can be calculated as
0
1
0
ruk1
y2 y3
1
@ ruk A
@ y3 y1
2
2jT j
ruk3
y1 y2
1 0
x3 x2
1
x1 x3 A @ x1
x2 x1
y1
1
x2
y2
11 0
0
1
x3 A @ 1
0
y3
1
0
0 A:
1
18
246
J. Alberty et al.
19
k
k 2l
k
0
0
0
k
k
k 2l
0
0
0
0
0
0
l
0
0
0
0
0
0
l
0
10
1
11 u
0
C
B
0C
CB 22 u C
C
B
0 CB 33 u C
C : Ccu; 20
C
B
0C
CB 212 u C
A
@
0
213 u A
l
223 u
we obtain v : Cu cT vCcu. Since again gpT ;k etc. are ane on T , (15)
also holds for tetrahedral elements, but this time with C as dened in 12. Finally,
we have
c
0
12
X
!
uj gpT ;j jT
j1
uk1 ;x
B 0
B
B
B 0
B
Bu
B k1 ;y
B
@ uk1 ;z
0
uk1 ;y
0
0
uk2 ;x
0
0
uk1 ;x
uk1 ;z
0
0
uk1 ;z
0
1
u1
B . C
C
B
@ .. A
u12
0
uk2 ;y
0
0
uk3 ;x
0
uk2 ;z
0
uk2 ;y
0
uk2 ;x
uk1 ;x
uk2 ;z
uk1 ;y
uk2 ;z
0
uk3 ;y
0
0
uk4 ;x
0
0
uk4 ;y
uk3 ;z
0
uk3 ;y
0
uk3 ;x
uk4 ;y
uk4 ;x
uk2 ;x
uk3 ;z
uk3 ;x
uk4 ;z
uk2 ;y
uk3 ;z
uk3 ;y
uk4 ;z
1
0
0 C
C
C
uk4 ;z C
C
0 C
C
C
uk4 ;x A
uk4 ;y
21
and so we obtain (17), where R is the coecient matrix on the right-hand side of
(21).
247
1 0
ruk1
1
B ruk C B x1
2 C
B
B
@ ruk A @ y1
3
ruk4
z1
1
x2
y2
z2
1
x3
y3
z3
11 0
1
0
B1
x4 C
C B
y4 A @ 0
0
z4
0
0
1
0
1
0
0C
C
0A
1
22
x
x2 x1
y
y2 y1
x4 x1
y4 y1
n
x1
g
y1
23
which maps 0; 12 onto T . The shape functions on Tref 0; 12 are dened as
~
g1 n; g
~
g7 n; g
~ 1 n; g
u
;
0
~ 4 n; g
u
;
0
0
;
~ 1 n; g
u
~
g2 n; g
..
.
~
g8 n; g
0
~ 4 n; g
u
24
with
~ 1 n; g 1 n1 g;
u
~ 3 n; g ng;
u
~ 2 n; g n1 g;
u
~ 4 n; g 1 ng:
u
25
The entries of the local stiness matrix for the element T are obtained by transforming the integral in (11) onto the reference element Tref . We use the following
abbreviation for the transpose of the gradient of the transformation.
0
F
@U1
1 x;y
@ 1@x
@U2 x;y
@x
@U1
1 x;y
@y
A:
@U1
2 x;y
@y
26
248
J. Alberty et al.
We notice that for the expression gpT ;k : Cgpt; there are four dierent
possibilities depending on the zero-components of thebasis functions.
rst
In the
case we have to transform an expression of the form upT ;i0 x;y : C upT ;j0 x;y to
one depending on the variables n; g, which results in
k 2l 0
ru~i F
F T ru~j T :
27
0
l
In the second case we have to transform u
ru~i F
l
0
pT ;i x;y
ru~i F
upT ;j x;y
and obtain
0
pT ;i x;y
28
: C
upT ;j x;y
0
and obtain
0 l
F T ru~j T :
k 0
: C
0
F T ru~j T :
k 2l
In the third case we transform the expression u
ru~i F
upT ;i x;y
0
29
: C u
pT ;j x;y
and obtain
k
F T ru~j T :
0
0
l
30
Hence transforming the integration in (11) leads to four cases of the form
Z
E : ru~i T ru~j dx;
31
Tref
where E diers for each case and is the product of the three inner matrices in each
of (27)(30) and i and j depend on the corresponding shape functions ~gk and ~g as
in (24). Note that E is constant on Tref . Hence, if we dene the four matrices
0
R11
Z
R22
Z
0
1
0
1Z
1
0
~ j n; g
~ i n; g @ u
@u
dndg
@n
@n
~ j n; g
~ i n; g @ u
@u
dndg
@g
@g
i;j1;...;4
i;j1;...;4
B
1B
B 2
B
6 B 1
@
1
0
2
1B
1
B
6 @ 1
2
2 1
1 2
C
1 C
C
C; 32
2 C
A
2
1 1
2 2
2
2
1
1
1
2
1 C
C;
1A
2
33
R12 RT21
Z
0
249
1
1
1 1 1
1
~ j n; g
~ i n; g @ u
@u
1 B 1 1
1
1C
C;
dndg
B
@
1
1A
@n
4 1 1
@g
0
i;j1;...;4
1
1 1 1
1Z
34
the following Matlab routine calculates the local stiness matrix for quadrilateral
elements:
1
f gk dx
kT
1
1
1
x1
x2
x3
y1
y2 fj xS ; yS
y3
with
j : modk 1; 2 1;
35
250
J. Alberty et al.
R
The integral E g gk ds in (10) that involves the Neumann conditions is approximated with the value of g in the centre xM ; yM of the edge E with length jEj,
Z
1
g gk ds jEjgj xM ; yM
2
E
with
j : modk 1; 2 1:
36
The user-dened function g species the values of g in its rst component; its
second argument is the outward normal vector. The following Matlab commands
calculate the g-related part of (10).
1
0 1
m1
w1
B .. C
B . C
@ . Au @ .. A
md
on C;
37
wd
0 1
n
B0C
B . Cu 0
@ .. A
0
0
or
1
eT1
B .. C
@ . A u uD
eTd
or
0 1
0
@ ... Au 0:
0
251
38
The data mx and W x in (37) are provided by the user in a user-dened Matlab
program u d:m through the command line mx; W x : uD x; examples of
which are given in Sects. 10, 11, 12, and 13. For the discrete problem, this leads to
the linear system
BU w;
39
where each row of B 2 RndN contains the value of some mj 2 RdN at a node on
the boundary CD and w 2 Rn contains the corresponding values wj at this node; N
is the number of nodes. It is assumed that rang B n. In particular, all mj 0 are
not included in B.
Coupling this set of conditions through Lagrange parameters with (7) leads to the
extended system
b
U
A BT
:
40
k
w
B 0
The following Matlab instructions compute the matrix B, the vector w for the
two-dimensional case, and form the extended matrix and right-hand side:
252
J. Alberty et al.
U occupies the rst dN components. For the two dimensional case U is extracted
by
u x1 : 2 sizecoordinates; 1;
Our two-dimensional problem models the plain strain condition. It that case, the
complete stress tensor r 2 R33
sym has the form:
0
1
r11 r12 0
r @ r12 r22 0 A;
0
0 r33
2
k
r11 r
with r33 2lk
22 . It then follows for jdevrj , where devA :
P
n
trA
2 1=2
A 3 Id33 and jAj :
, the Frobenius norm, that
i;j1 Aij
jdev rj
!
1
The graphical representation shows the deformed mesh with a magnication of the
displacement. The main interest might be on the elastic shear energy density
jdevrj2 =4l and so we provide a tool to attach grey tones (or a colour bar) to the
displayed meshes. In the two-dimensional case we use the following Matlab routine:
The function show uses the variable AvS, which stores the nodal-values of rh ;
where rh is the stress calculated by the nite element method and rh is a smoother
approximation. For triangular and tetrahedral elements, rh is constant on each
element. In the case of quadrilateral elements, we use the stress in the centre of
gravity of each element; rh is dened in every node of the mesh as the mean value
of the stresses on the corresponding patch. The matrix AvS is calculated in the
function avmatrix below.
The Matlab routine trisurfelements; x;y;z;AvC; facecolor; interp
is used to draw triangulations for equal types of elements. Every row of the matrix
253
elements determines one polygon where the x-, y-, and z-coordinate of each
corner of this polygon is given by the corresponding entry in x, y, and z. (In 2dproblems, we use for the z-coordinate the same value (zero) in all mesh points.) The
values together with the options facecolor, interp determine the grey
tone of the area determined by AvC and linearly interpolated.
The following Matlab function calculates the stress tensors rh , rh and the strain
tensors C1 rh , C1 rh for triangular and quadrilateral elements. The output of this
routine is needed for the graphical representation of the solution in the function
show and for the a posteriori error calculation in the function aposteriori.
The variables AvS and AvE denote the stress and strain tensors, averaged over a
patch. The variables Sigma3, Sigma4, Eps3, and Eps4 denote the stress and
strain tensors on triangular and quadrilateral elements.
254
J. Alberty et al.
with
g2T
Z
T
serves as such an error guess; the robust error estimator from [5] is denoted by ^g
and given in brackets following the value of (41).
The stress tensors rh , rh and the strain tensors C1 rh , C1 rh for triangular and
quadrilateral elements are calculated for example in the function avmatrix
given in Sect. 8.
The following Matlab routine calculates the estimated error for triangular and/or
quadrilateral elements:
255
cos
a 1x =
a sin2x
sin2xa
0
with
x
3p=4
and
C
1
cos a 1x , C2 2k 2l=k l. The function (42) solves the Lame
equation (1) with f 0 and the boundary condition (3) on C CD (with w dened through u). This example is a common benchmark problem for linear
elasticity because it models a re-entrant corner with a typical singularity in
the stress at 0; 0. For the elasticity module E 100000 and the shear module
m 0:3 of bronze the Lame constants are k Em=1 m1 2m and l E=
21 m.
The Matlab code for the functions u d:m, u value:m and f.m is given below. It
should be noted that in this particular example, the material parameters E and m
determine the boundary condition and thus appear in u value:m.
256
J. Alberty et al.
The function show of Sect. 8 yields the deformed mesh with a displacement
multiplied by a factor of 3000 based on P1 Finite Elements and Q1 Finite
Elements for 450 degrees of freedom of Figs. 4 and 5. The grey tones visualise
jdevrh j2 =4l as described in Sect. 8 from the averaged stress eld rh . The
singularity at 0; 0 causes dierent maximal values in the visualized stress
of Figs. 4 and 5 on coarse meshes (for ner meshes the stress at that corner
increases).
The averaging a posteriori error estimate g of Sect. 9 (the reliable estimator g^ of
[5]) indicates a relative error g 15% (^
g 15%) for Q1 nite elements and
g 26% (^
g 26%) for P1 nite elements; g is not a reliable estimate but a
reasonable error guess (while ^
g is a reliable error estimator which merely lacks an
unknown universal constant).
Figure 6 shows the deformed mesh (578 degrees of freedom) displayed by the
Matlab function show with a magnifying factor 20 for the displacements. The
numerical solution shows peak stresses at the corner 0; 44 as it is expected from
the literature.
The a posteriori error estimate of Sect. 9 indicates a relative error g 16% (while
the reliable estimator of [5] results in ^
g 17%).
257
p
p
f0g 2; 3 and by 0; 1 u 0 on 2; 3 f0g which are included in the
following table:
The Matlab program yield the deformed mesh (1122 degrees of freedom) shown in
Fig. 7 with displacements magnied by a factor of 20. The example demonstrates
how the combination of triangular and quadrilateral nite elements can be
combined to describe regions with non-parallel boundaries without globally
preferring a certain mesh orientation.
The relative error is g 8% according to the a posteriori error estimate of Sect. 9
and ^
g 9% according to the reliable estimator of [5].
258
J. Alberty et al.
vious examples. The material is xed at the two holes with centres
O1 f17; 1:5; 21g and O2 f48; 1:5; 21g so that at their inner edges there is a
homogeneous Dirichlet boundary. The hole with centre O3 f20; 77; 11:5g is
lifted in z-direction by 0:1 units described by a Dirichlet boundary condition with
uD f0; 0; 0:1g there; the rest of the boundary is traction free. The describing
Matlab routines for the boundary conditions and the volume force are given in
the following table:
The deformed mesh for 6512 degrees of freedom is presented in Fig. 8, where
the displacement is magnied by a factor of 100. Again the shades of grey
visualise the square mean of the eigenvalues of the stress as calculated and
displayed by functions show and avmatrix similar to those presented in
Sect. 8 and 9.
259
Appendix A
Main Program for Two-dimensional Problems
What follows is the complete listing of the main program as it was used for
Example 11 and Example 12. It diers in line 1 in Example 10 owing to dierent
material constants E and m.
260
J. Alberty et al.
261
Appendix B
Main Program for Three-dimensional Problems
What follows is the complete listing of the main program as it was used for
Example 13.
262
J. Alberty et al.
Acknowledgments
It is our pleasure to thank Axel Hecht and Darius Zarrabi for discussions and other contributions on
the propagation of this report. The fourth author (R. K.) thankfully acknowledges the partial support
by the German research foundation at the Graduiertenkolleg 357 Eziente Algorithmen und
Mehrskalenmethoden.
263
References
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
Alberty, J., Carstensen, C., Funken, S. A.: Remarks around 50 lines of Matlab: nite element
implementation Numerical Algorithms 20, 117137 (1999).
Brenner, S. C., Scott, L. R.: The mathematical theory of nite element methods. Texts in Applied
Mathematics 15. New York: Springer 1994.
Carstensen, C., Bartels, S.: Each averaging technique yields reliable a posteriori error control in
FEM on unstructured grids, Part I: Low order conforming, nonconforming, and mixed FEM.
Math. Comp. 71, 945969 (2002).
Carstensen, C., Funken, S. A.: Averaging technique for FE-A posteriori error control in
elasticity, Part I: Conforming FEM. Comput. Methods Appl. Mech. Engrg. 190, 24832498
(2001).
Carstensen, C., Funken, S. A.: Averaging technique for FE-A posteriori error control in
elasticity. Part II: k-independent estimates. Comput. Methods Appl. Mech. Engrg. 190, 4663
4675 (2001).
Ciarlet, P. G.: The nite element method for elliptic problems. Amsterdam: North-Holland 1978.
COMSOL Group: FEMLAB: Multiphysics in Matlab. (http://www.femlab.com)
Schwarz, H. R.: Methode der Finiten Elemente. Stutgaart: Teubner 1991.
Wriggers, P. et al.: Benchmark perforated tension strip. Communication in talk at ENUMATH
Conference in Heidelberg, 1997. (See also http://www.ibnm.uni-hannover.de/Forschung/Paketantrag/Benchmarks/benchmark.html)
Jochen Alberty
Mathematisches Seminar, Bereich II,
Christian-Albrechts-Universitat zu Kiel
Ludewig-Meyn-Str. 4 24098 Kiel
Germany
e-mail: ja@numerik.uni-kiel.de
Carsten Carstensen
Institute for Applied Mathematics and
Numerical Analysis,
Vienna University of Technology
Wiedner Hauptstrae 8-10/115
A-1040 Vienna
Austria
e-mail: carsten.carstensen@tuwien.ac.at
Stefan A. Funken
Mathematisches Seminar, Bereich II,
Christian-Albrechts-Universitat zu Kiel
Ludewig-Meyn-Str. 4
24098 Kiel
Germany
e-mail: saf@numerik.uni-kiel.de
Roland Klose
Mathematisches Seminar, Bereich II,
Christian-Albrechts-Universitat zu
Kiel Ludewig-Meyn-Str. 4
24098 Kiel
Germany
e-mail: rkl@numerik.uni-kiel.de