0% found this document useful (0 votes)
86 views11 pages

Bake, Shake or Break - and Other Applications For The FEM: A) 1D Quadrature

The programming project will implement Gaussian quadrature for 1D, 2D and 3D finite element integration. Students will write MATLAB functions to evaluate integrals using Gaussian quadrature rules in 1D over an interval, 2D over a triangle, and 3D over a tetrahedron. The functions take the integration limits/points and a function handle and return the approximated integral.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views11 pages

Bake, Shake or Break - and Other Applications For The FEM: A) 1D Quadrature

The programming project will implement Gaussian quadrature for 1D, 2D and 3D finite element integration. Students will write MATLAB functions to evaluate integrals using Gaussian quadrature rules in 1D over an interval, 2D over a triangle, and 3D over a tetrahedron. The functions take the integration limits/points and a function handle and return the approximated integral.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Bake, shake or break - and other applications for the FEM

Programming project in TMA4220 - part 1

by Geir Bogfjellmo and Anne Kværnø


TMA4220 - Numerical solution of partial differential equations using the finite element method

The programming project will be split into two parts. This is the first part and is mandatory for
all students. It is an introduction into the finite element method and is designed to build up a solid
code base for part 2, where you will actually solve larger, real life problems. For the second part
you will have to choose one of several tasks to implement.

1 Gaussian quadrature

At the heart of every finite element code, lies the evaluation of an integral. This integral may be of
varying complexity depending on the problem at hand, and many of these integrals does not even
have a known analytical solution. Some integrals are possible to solve analytically, but of such
computational complexity that it is impractical to do so. As such, one often refers to numerical
integration schemes to do the core integration. One popular integration scheme is the Gaussian
quadrature.
In one dimension the gauss quadrature takes the form
Z 1 Nq
X
g(z)dz ≈ ρq g(zq ),
−1 q=1

where Nq is the number of integration points, zq are the Gaussian quadrature points and ρq are the
associated Gaussian weights.
This extends to higher dimensions by
Z Nq
X
g(z)dz ≈ ρq g(z q ),
Ω̂ q=1

and specifying the vector quadrature points z q as well as integrating over a suitable reference
domain Ω̂ (i.e. squares or triangles in 2D, tetrahedra or cubes in 3D).

a) 1D quadrature

Write a matlab function I = quadrature1D(a,b,Nq,g). With the following arguments:


I ∈R value of the integral
a ∈R integration start
b ∈R integration end
Nq ∈ [1, 4] number of integration points
g :R→R function pointer∗
verify that the function evaluates correctly by comparing with the analytical solution of the integral
Z 2
ex dx
1
Nq zq ρq
1
1-point-rule 2√ 1
1 3 1
2-point-rule 2 − √6 2
1 3 1
2 + √6 2
1 15 5
2 − 10 18
1 4
3-point-rule 2√ 9
1 15 5
2√ + 10 √ 18

1
2 − √525+70
70 √
30 18− 30
72

4-point-rule 1
2 − √525−70
70 √
30 18+ 30
72

1
2 + √525−70
70 √
30 18+ 30
72

1
2 + 525+7070
30 18− 30
72

Table 1: 1D gauss quadrature

b) 2D quadrature

Using all numerical quadratures, it is important to first map the function to the referance domain.
In one dimension, this is the interval ζ ∈ [0, 1]. In higher dimensions, we often map to barycentric
coordinates (or area coordinates as they are known in 2D). The gauss points are then given as
triplets in this coordinate system. The area coordinates are defined by
A1
ζ1 =
A
A2
ζ2 =
A
A3
ζ3 =
A
where A1 , A2 and A3 are the area of the triangles depicted in figure 1 and A is the total area of the
triangle. Note that these do not form a linear independent basis as ζ1 + ζ2 + ζ3 = 1.

Nq (ζ1 , ζ2 , ζ3 ) ρ
1-point rule (1/3, 1/3, 1/3) 1
(1/2, 1/2, 0) 1/3
3-point rule (1/2, 0, 1/2) 1/3
(0, 1/2, 1/2) 1/3
(1/3, 1/3, 1/3) -9/16
4-point rule (3/5, 1/5, 1/5) 25/48
(1/5, 3/5, 1/5) 25/48
(1/5, 1/5, 3/5) 25/48

Table 2: 2D gauss quadrature

Write a matlab function I = quadrature2D(p1,p2,p3,Nq,g). With the following argu-


ments:
Hint: An easy way of mapping barycentric coordinates ζ to physical coordinates x is by x =
ζ1 p1 + ζ2 p2 + ζ3 p3 , where pi , i = 1, 2, 3 is the corner points of the triangle.
Figure 1: Barycentric coordinates in two dimensions

I ∈R value of the integral


p1 ∈ R2 first corner point of the triangle
p2 ∈ R2 second corner point of the triangle
p3 ∈ R2 third corner point of the triangle
Nq ∈ {1, 3, 4} number of integration points
g : R2 → R function pointer∗
verify that the function evaluates correctly by comparing with the analytical solution of the integral
ZZ
log(x + y) dx dy,

where Ω is the triangle defined by the corner points (1, 0), (3, 1) and (3, 2).

c) 3D quadrature

The extension of the barycentric coordinates to 3 dimensions and tetrahedral elements, should be
straightforward. The integration schemes can be found in the following table
Write a matlab function I = quadrature3D(p1,p2,p3,p4,Nq,g). With the following
arguments:
Nq (ζ1 , ζ2 , ζ3 , ζ4 ) ρ
1-point rule (1/4, 1/4, 1/4, 1/4) 1
(α, β, β, β) 1/4
4-point rule (β, α, β, β) 1/4
(β, β, α, β) 1/4
(β, β, β, α) 1/4
(1/4, 1/4, 1/4, 1/4) -4/5
(1/2, 1/6, 1/6, 1/6) 9/20
5-point rule (1/6, 1/2, 1/6, 1/6) 9/20
(1/6, 1/6, 1/2, 1/6) 9/20
(1/6, 1/6, 1/6, 1/2) 9/20
√ √
1 3 5 1 5
α= 4 + 20 , β= 4 − 20

Table 3: 3D gauss quadrature

I ∈R value of the integral


p1 ∈ R3 first corner point of the tetrahedron
p2 ∈ R3 second corner point of the tetrahedron
p3 ∈ R3 third corner point of the tetrahedron
p4 ∈ R3 fourth corner point of the tetrahedron
Nq ∈ {1, 4, 5} number of integration points
g : R3 → R function pointer∗
verify that the function evaluates correctly by comparing with the analytical solution of the integral
ZZZ
ex dx dy dz,

where Ω is the tetrahedron defined by the corner points (0, 0, 0), (0, 2, 0), (0, 0, 2) and (2, 0, 0).

(*) A function pointer in matlab is a variable which represents a function instead of the usual
numerical values. In its simplest form it is declared as

f = @(x) xˆ2 + 1

which would cause the variable f to contain a pointer to the function f (x) = x2 + 1. The function
can then be evaluated using one of two methods

y = f(4);
y = feval(f,4);

both of which should yield the same result y = 17. A function may take in several arguments,
i.e. f (x, y) = x2 + y 2 may be declared as

f = @(x,y) xˆ2 + yˆ2

again the evaluation of the function is straightforward


y = f(2,2);
y = feval(f,2,2);

Provided that the actual function body is capable of vector or matrix operations, then the input
arguments may be of vector or matrix form. The syntax remains unchanged by this. You may also
use variables in the function declaration, i.e.

a = 2;
f = @(x) x*a

will result in a function f which is doubling its input argument (even if a is changed at a later
point).
2 Poisson in 2 dimensions

We are going to solve the two-dimensional Poisson problem, given by

∇2 u(x, y) = −f (x, y) (1)


u(x, y)|r=1 = 0,

with f given in polar coordinates as

f (r, θ) = −8π cos 2πr2 + 16π 2 r2 sin 2πr2


 

on the domain Ω given by the unit disk, i.e. Ω = (x, y) : x2 + y 2 ≤ 1 .




a) Analytical solution

Verify that the following expression is in fact a solution to the problem (1)

u(x, y) = sin 2π(x2 + y 2 ) .



(2)

b) Weak formulation

Show that the problem can be rewritten as

a(u, v) = l(v), ∀v ∈ X.

with the bilinear functional a and the linear functional l given by


ZZ
a(u, v) = ∇u · ∇v dx dy,
ZΩZ
l(v) = f v dx dy.

What is the definition of the space X?

c) Galerkin projection

Instead of searching for a solution u in the entire space X we are going to be looking for a
solution in a much smaller space Xh ⊂ X. Let Ω be discretized into M triangles such that our
computational domain is the union of all of these Ω = ∪M k=1 Kk . Each triangle Kk is then defined
by its three corner nodes xi . For each of these nodes there corresponds one basis function. The
space Xh is then defined by

Xh = {v ∈ X : v|Kk ∈ P1 (Kk ), 1 ≤ k ≤ M }
for which the basis functions {ϕi }ni=1 satisfy

Xh = span{ϕi }ni=1 ϕj (xi ) = δij


P uh ∈ Xh , it is then possible to write
and δij is the Kronecker delta. By searching for a solution
this as a weighted sum of the basis functions, i.e. uh = ni=1 uih ϕi (x, y).
Show that the problem ”Find uh ∈ Xh such that a(uh , v) = l(v) ∀v ∈ Xh ” is equivalent to the
following problem
Find u such that
Au = f (3)
with

A = [Aij ] = [a(ϕi , ϕj )]
u = [uih ]
f = [fi ] = [l(ϕi )].

d) Implementation

We are now going to actually solve the system (3). First we are going to take a look at the triangula-
tion {Kk }. From the webpage http://wiki.math.ntnu.no/tma4220/2013h/start
you may download the mesh generators. For organization purposes you might want to keep them
in a separate directory and see the matlab addpath command.
The function getDisk is generating the unit disk Ω. Plot at least three meshes of different
sizes using the mesh generated from this function. You may want to check the matlab function
trimesh or triplot.

e) Stiffness matrix

Build the stiffness matrix A. You may choose if you perform the integration analytically or by
Gaussian quadrature.
The matrix A should now be singular. Verify this in your code and explain why this is the case.

f) Right hand side

Build the right hand side vector f in the same manner as A. Here you might need to resort to
Gaussian quadrature.

g) Boundary conditions

Implement the homogeneous dirichlet boundary conditions. Describe what method you used for
this and how you did it.

h) Verification

Solve the system (3) and verify that you are getting (approximately) the same result as the analyt-
ical solution (2).
3 Neumann boundary conditions

We are going to change to boundary conditions of our problem to

∇2 u(x, y) = −f (x, y) (4)


u(x, y)|∂ΩD = 0,

∂u(x, y)
= g(x, y),
∂n ∂ΩN

with the source term f and exact solution u given as above, and g as

g(r, θ) = 4πr cos 2πr2 .



(5)

defined on ∂ΩD = x2 + y 2 = 1, y < 0 , and the neumann



The dirichlet boundary condition
 is
boundary condition as ∂ΩN = x2 + y 2 = 1, y > 0 shown in figure 2.

Figure 2: Dirichlet and Neumann boundary conditions

a) Boundary condition

Verify that (5) is a solution to (4) at the boundary

b) Variational formulation

How does a(·, ·) and l(·) change with the introduction of Neumann boundary conditions?
c) Gauss quadrature

The neumann boundary condition is given as an integral and should be evaluated using Gaussian
quadrature. Modify your quadrature methods from task 1 to solve line integrals in two dimensions,
i.e. the method signature in I = quadrature1D(a,b,Nq,g) should change to a ∈ R2 and
b ∈ R2 .

d) Implementation

Change your code from task 2 to solve this new boundary value problem. How does your solution
in the interior compare to the one you got in task 2? How does your solution at the boundary
compare?
4 Moving into 3 dimensions

Figure 3: Sample 3d shape

a) The Poisson in 3d

We are now going to solve the problem


∇2 u = −f
u|∂Ω = 0
in three dimensions, meaning that we are looking for a solution u(x, y, z).
Generate a mesh, using the function getSphere from the downloaded mesh generators. This
will give you three variables which will describe the nodal points, the tetrahedral elements and the
index of the boundary nodes. These should be familiar from task 2 as the only difference is that
spatial coordinates have one more component, as well as the elements require one more index to
describe.
Modify your code from task 2 to deal with tetrahedral elements in three dimensions. Use the
following f
f (r, φ, θ) = −12π cos(2πr2 ) + 16π 2 r2 sin(2πr2 )
and homogeneous Dirichlet boundary conditions (uD = 0).

b) Volume visualization

Plot the domain Ω (i.e. the ball). Note that you will not be required to plot every element, as most
will be hidden on the inside of the domain. See the matlab function TriRep or tetramesh for
functionality relating to this.
Plot your solution using isosurfaces. Note that the matlab function isosurface requires your
data to be structured, which it currently is not. You will have to post process the data to get it on
the desired form. Read up on TriScatteredInterp for this.

c) GLview (optional)

GLview is a dedicated commercial visualising program to display the results from finite element
analysis. It is available to all NTNU students through progdist. While it is possible to display 2D-
results in GLview, the advantage of using a dedicated visualiser really is apparent when trying to
draw 3D volumetric results.
Download and install GLview inova from progdist and write a .vtf output file to view your
results. See the attached writeVTF function in the grids.zip file.

d) Neumann boundary condition

Change the problem definition to use Neumann boundary conditions for the top half of the sphere
(i.e. z > 0) and Dirichlet for the bottom part. The neumann condition is given in (5).

You might also like