scientific computing i - tum

39
Scientific Computing I Module 7: An Introduction to Finite Element Methods Michael Bader Lehrstuhl Informatik V Winter 2017/2018

Upload: others

Post on 23-Feb-2022

2 views

Category:

Documents


0 download

TRANSCRIPT

Scientific Computing I

Module 7: An Introduction to Finite Element Methods

Michael BaderLehrstuhl Informatik V

Winter 2017/2018

Part I: Introduction to Finite Element Methods

The Model Problem

FEM Main IngredientsWeak Forms and Weak SolutionsTest and Shape Functions

Assembling the System of EquationsExample Problem: 1D PoissonStiffness Matrix for Linear Nodal Basis Functions

A Road to Theory

Time-Dependent Problems

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 2

The Model Problem: Poisson Equation

• 2D Poisson Equation on unit square:

∂2

∂x2 u(x , y) +∂2

∂y2 u(x , y) = f (x , y) in Ω = (0,1)2

• Dirichlet boundary conditions:

u(x , y) = g(x , y) on ∂Ω

• for general operator L instead of ∆: similar concept• new for FEM: compute a function to approximate u(x , y)

(instead of approximate values at certain grid points)

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 3

Finite Elements – Main Ingredients

1. solve weak form of PDE to reduce regularity properties

u′′ = f −→∫

vu′′ dx =

∫vf dx for “all” v

→ allows additional weak solutions (esp. after integration by parts)2. compute a function as numerical solution→ search in a function space Wh:

uh =∑

j

ujϕj (x), spanϕ1, . . . , ϕJ = Wh

3. find weak solutions of simple form:for example piecewise linear functions and choose basis functions withlocal support (“hat functions”)→ leads to system of linear equations

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 4

Weak Form and Weak Solutions

The weak form is obtained by 3 steps:1. integrate over the domain⇒ from infinitely many equations (strong form has to hold on every point(x , y)), only one new is obtained

2. multiply with (any) test function v (from a certain function space V )⇒ again, infinitely many equations

3. (optional) do “integration by parts”,i.e. use divergence theorem to “transfer” derivatives from solution u totest functions v⇒ reduce smoothness assumptions on u

⇒ “real solution” u also solves the weak formhowever, additional, weak solutions are now allowed

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 5

Weak Form of the Poisson Equation

• Poisson equation with Dirichlet conditions:

−∆u = f in Ω,u = 0 on δΩ

• weak form:−∫

Ω

∆u · v dΩ =

∫Ω

f · v dΩ ∀v ∈ V

• apply divergence theorem:

−∫

Ω

∆u · v dΩ =

∫Ω

∇u · ∇v dΩ−∫∂Ω

∇u · v ds

• choose functions v such that v = 0 on ∂Ω:∫Ω

∇u · ∇v dΩ =

∫Ω

f · v dΩ ∀v ∈ V

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 6

Weak Form of the Poisson Equation (2)

• Poisson equation with Dirichlet conditions:

−∆u = f in Ω,u = 0 on δΩ

• transformed into weak form:∫Ω

∇u · ∇v dΩ =

∫Ω

f · v dΩ ∀v ∈ V

• weaker requirements for a solution u:twice differentiabale→ first derivative integrable

• 〈∇u,∇v〉 a bilinear form (i.e. depends on 2 functions u and v );often written as:

a (u, v) = 〈f , v〉 ∀v ∈ V

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 7

A Bit More Motivation for the Weak FormFEM and the Residual Equation

• for the PDE ∆u = f , the residual is defined as r(x) := f (x) − ∆u(x)!

= 0• in the weak form (multiply with test function an integrate) we obtain∫

v(x) r(x) =

∫v(x)

(f (x) − ∆u(x)

)dx = 0 for all v

• remember that∫

f (x)g(x) dx is the standard scalar product on functions

• hence: the residual of the weak solution has to be orthogonal to all test functions(and recall all connections between “orthogonal” and “shortest distance”)

FEM vs. Linear Algebra• consider the system of linear equations Ax = b• now imagine a “weak form” with scalar product: demand vT Ax = vT b for all v• if v = eT

i = (0, . . . 0, 1, 0 . . . , 0), then eTi Ax = eT

i b tests a single equation (i-th row)• if you use all unit vectors ei as test functions, only an exact x is also a weak solution• you may use fewer test functions (and maybe different ones) to allow “weak” solutions• for the PDE weak form, you cannot use all test function: these would be infinitely many

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 8

Test and Shape Functions

• search for solution functions uh of the form

uh =∑

j

ujϕj (x)

the ϕj (x) are typically called shape or ansatz functions• the basis functions ϕj (x) build a vector space

(i.e., a function space) Wh

spanϕ1, . . . , ϕJ = Wh

• insert into weak formulation∫∇(∑

j

ujϕj (x)

)· ∇v dx =

∫f · v dx ∀v ∈ V

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 9

Test and Shape Functions (2)

• choose a basis ψi of the test space Vh

• then: if all basis functions ψi satisfy∫∇(∑

j

ujϕj (x)

)· ∇ψi dx =

∫f · ψi dx ∀ψi

then all v ∈ Vh satisfy the equation• the ψi are therefore often called test functions• we obtain a system of equations for unknowns uj :

one equation for each test function ψi

• Vh is often chosen to be identical to Wh (Ritz-Galerkin method)→ we then have as many equations as unknowns

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 10

Example: Nodal Basis

ϕi (x) :=

1h (x − xi−1) xi−1 < x < xi1h (xi+1 − x) xi < x < xi+1

0 otherwise

0,60,4 0,80,20

1

1

0,4

0,2

x

0

0,8

0,6

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 11

Assembling the System of Equations

• start from system of equations∫∇(∑

j

ujϕj (x)

)· ∇ψi dx =

∫f · ψi dx ∀ψi

• ∆ is a linear operator⇒ turn into usual form of linear system of equations∑j

(∫∇ϕj (x) · ∇ψi (x) dx︸ ︷︷ ︸

=:Aij

)uj =

∫f · ψi dx ∀ψi

• aim: make matrix A sparse→ most Aij = 0• approach: use local basis functions on a discretisation grid• ψj , ϕj zero everywhere except in grid cells adjacent to grid point xj

• Aij = 0, if ψi and ϕj don’t overlap

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 12

Example Problem: 1D Poisson

• in 1D: −u′′(x) = f (x) on Ω = (0,1),hom. Dirichlet boundary cond.: u(0) = u(1) = 0

• weak form: ∫ 1

0u′(x) · v ′(x) dx =

∫ 1

0f (x) · v(x) dx ∀v ∈ V

• computational grid:xi = ih, (for i = 1, . . . ,n − 1); mesh size h = 1/n

• Vh = Wh: piecewise linear functions(on intervals [xi , xi+1])

• weak form reads:∫ 1

0u′(x) · ϕ′i (x) dx =

∫ 1

0f (x) · ϕi (x) dx ∀i = 1, . . . ,n − 1

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 13

Nodal Basis

ϕi (x) :=

1h (x − xi−1) xi−1 < x < xi1h (xi+1 − x) xi < x < xi+1

0 otherwise

0,60,4 0,80,20

1

1

0,4

0,2

x

0

0,8

0,6

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 14

Nodal Basis – Derivatives and IntegralsCompute derivative of basis functions:

ϕ′i (x) :=

1h xi−1 < x < xi

− 1h xi < x < xi+1

0 otherwise

Compute integrals of weak form:

•∫ϕ′i (x)ϕ′i (x) dx =

∫ xi

xi−1

1h· 1

hdx +

∫ xi+1

xi

(−1

h

)(−1

h

)dx =

2h

•∫ϕ′i (x)ϕ′i+1(x) dx =

∫ xi+1

xi

(−1

h

)1h

dx = −1h

•∫ϕ′i (x)ϕ′i−1(x) dx = −1

h(same computation)

•∫ϕ′i (x)ϕ′j (x) dx = 0 for all j 6= i , i ± 1

(supports of basis functions do not overlap!)

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 15

Nodal Basis – System of Equations

• stiffness matrix:

1h

2 −1

−1 2. . .

. . . . . . −1−1 2

• right hand sides (assume f (x) = α ∈ R):∫ 1

0f (x) · ϕi (x) dx =

∫ 1

0α · ϕi (x) dx = αh

• system of equations very similar to finite differences• note: for other operators L: same approach!• exercise: compute right-hand-side vector, if f (x) :=

∑fjφj (x)

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 16

Outlook: A Road to Theory

• weak formulation is equivalent to variational approach:solution u minimises an energy functional

• best approximation property:∥∥∥u − uFEh

∥∥∥a≤ inf

v∈Vh

‖u − v‖a

in terms of the norm induced by the bilinear form a (energy norm)• thus: error bounded by interpolation error

(in energy norm)

(details in lecture Numerical Programming II . . . )

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 17

Time-Dependent Problems

Example: 1D Heat Equation• ut = uxx + f on domain Ω = [0,1] for t ∈ [0, tend]

• formulate weak form∫ut · v dx =

∫uxx · v dx +

∫f · v dx

∂∂t

(∫u · v dx

)=

∫uxx · v dx +

∫f · v dx

• apply discretisation in space using finite element method:

∂∂t (Mhuh) = Ahuh + fh

Mh: mass matrix, Ah: stiffness matrix, uh = uh(t)

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 18

Time-Dependent Problems (2)Solve a system of ordinary differential equations:

• after spatial discretisation (Mh constant):∂∂t Mh (uh) = Ahuh + fh

• uh a vector of time-dependent coefficient functions:

uh = (u1(t), . . . ,ui (t), . . . ,un(t))T

• e.g., with forward Euler: Mh

(u(n+1)

h − u(n)h

)= Ahu(n)

h + fh

Outlook: how to deal with inversion of mass matrix?• approximate Mh by a simpler matrix (diagonal matrix, e.g.)→ mass lumping

• choose orthogonal basis functions→ mass matrix diagonal• choose entirely element-local basis functions (“discontinuous

Galerkin”)→ mass matrix block-diagonal

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 19

Part II: Implementation of Finite Element Methods

Element Stiffness MatricesElement-Oriented Computation of Stiffness MatricesExample: 1D PoissonExample: 2D PoissonTypical Workflow

Reference Elements and Stiffness MatricesElement-Oriented Computation on Unstructured MeshesAccumulation of Global Stiffness MatrixSimple Example: 1D PoissonOutlook: Extension to 2D and 3DOutlook: Further Components and Aspects of FEM

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 20

Element Stiffness Matrices

Finite Element methods are often used for complicated meshes:

Questions:• how to set up the system of equations efficiently?• is it possible to use stencil notation??

⇒ Switch to element stiffness matrices!

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 21

Element Stiffness Matrices (2)

• domain Ω subdivided into finite elements Ω(k):

Ω = Ω(1) ∪ Ω(2) ∪ · · · ∪ Ω(n)

• observation: basis functions are defined element-wisely

• use:∫ b

a f (x) dx =∫ c

a f (x) dx +∫ b

c f (x) dx• element-wise evaluation of the integrals:∫

Ω

∇u · ∇v dx =∑

k

∫Ω(k)

∇u · ∇v dx∫Ω

f · v dx =∑

k

∫Ω(k)

f · v dx

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 22

Element Stiffness Matrices (3)

• leads to local stiffness matrices for each element:∫Ω(k)

∇φj · ∇φi dx

︸ ︷︷ ︸=:A(k)

ij

• and respective element systems:

A(k)x = b(k)

• accumulate to obtain global system:∑k

A(k)

︸ ︷︷ ︸=:A

x =∑

k

b(k)

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 23

Element Stiffness Matrices (4)

Some comments on notation:• assume: 1D problem, n elements (i.e. intervals), linear nodal basis• in each element only two basis functions are non-zero!

• hence, almost all A(k)ij are zero:

A(k)ij =

∫Ω(k)

∇φj · ∇φi dx

• only 2× 2 elements of A(k) are non-zero• therefore convention to omit a-priori zero columns/rows⇒ leaves only unknowns that are in Ω(k)

⇒ notation: write this matrix as A(k) (element stiffness matrix)

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 24

Example: 1D Poisson

• Ω = [0,1] splitted into Ω(k) = [xk , xk+1]

• nodal basis; leads to element stiffness matrix:

A(k) =

(1 −1−1 1

)• consider case with only three unknowns (i.e., 3 basis functions):

A(1) + A(2) =

1 −1 0−1 1 00 0 0

+

0 0 00 1 −10 −1 1

• in stencil notation:

[−1 1∗ ] + [ 1∗ − 1 ] → [−1 2 − 1 ]

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 25

Example: 2D Poisson• −∆u = f on domain Ω = [0,1]2

• splitted into Ω(i,j) = [xi , xi+1]× [xj , xj+1]• bilinear basis functions

ϕij (x , y) = ϕi (x)ϕj (y)

• “pagoda” functions

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 26

Example: 2D Poisson (2)

• leads to element stiffness matrix:

A(k) =

2 − 1

2 − 12 −1

− 12 2 −1 − 1

2

− 12 −1 2 − 1

2

−1 − 12 − 1

2 2

• accumulation leads to 9-point stencil −1 −1 −1

−1 8 −1−1 −1 −1

• exercise at home: compute A(k) and assemble global matrix

(and compare with stencil)

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 27

Typical Workflow

1. choose grid:• quadratic or cubic cells• triangles (structured, unstructured)• tetrahedra, etc.

2. set up basis functions for each element Ω(k);for example, at all nodes xi ∈ Ω(k)

ϕi (xi ) = 1ϕi (xj ) = 0 for all j 6= i

3. for element stiffness matrix, compute all

A(k)ij =

∫Ω(k)

∇ϕj · ∇ϕi dx

4. accumulate global stiffness matrix A (and rhs)

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 28

Element-Oriented Computation on UnstructuredMeshesRecall: FEM often used for complicated meshes:

(Re-)computation of element matrices required for each element?• left grid: no (up to scaling with mesh size)• right grid: yes?

⇒ requires a closer look at the computation of element matricesMichael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 29

Element Stiffness Matrices – Notation Revisited

• recall notation for local stiffness matrices A(k)ij :

A(k)ij =

∫Ω(k)

∇φj (x) · ∇φi (x) dx

⇒ which basis functions φi , φj belong to element k?• switch to element-local numbering of basis functions:

A(k)µν =

∫Ω(k)

∇φ(k)ν (x) · ∇φ(k)

µ (x) dx

where φ(k)µ (x) = φi (x) and φ(k)

ν = φj (x) in Ω(k)

• requires a mapping φ(k)µ → φi (and φ(k)

ν → φj ),i.e., a function ι(k) with i = ι(k)(µ) and j = ι(k)(ν)

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 30

Accumulation of Global Stiffness Matrix• note the different dimensions of A(k) and A(k):

A(k) omits zero rows and columns of A(k)

• formulated via a projection matrix:

A(k) =(P(k)

)T A(k)P(k) with P(k)µ,i :=

1 if i = ι(k)(µ)0 otherwise

• recall computation of global stiffness matrix:

A :=∑

k

A(k) =∑

k

(P(k)

)T A(k)P(k)

• hence, for each element:1. compute element stiffness matrix A(k)

2. determine position of matrix elements A(k) in A(k)

→ based on mapping ι(k)(µ)

3. add non-zero elements of A(k) =(P(k)

)T A(k)P(k) to global matrix A

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 31

Element Stiffness Matrices and MeshesRecall our initial question:

(Re-)computation of A(k) required for each element?• left grid: no (up to scaling with mesh size)• right grid: yes?

⇒ efficient computation of A(k) via reference elements

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 32

Simple Example: 1D PoissonConsider FEM for 1D Poisson on an adaptive grid:

• grid points given as xk , not necessarily equidistant;define elements Ω(k) = [xk , xk+1]

• piecewise linear basis functions (“hat functions”) φk (x);φk (x) non-zero only on two elements: Ω(k−1) and Ω(k)

• mapping local to global indices: ι(k)(1) := k and ι(k)(2) := k + 1

• wanted: A(k)µ,ν :=

∫Ω(k)

∂∂x φ

(k)ν (x) ∂∂x φ

(k)µ (x) dx for every element k

Introduce reference element Ωref with linear basis functions Φµ:

Φ1(ξ) := ξ Φ2(ξ) := 1− ξ ξ ∈ Ωref := [0,1]

and a mapping x = χ(k)(ξ) for each Ω(k) defined as

χ(k)(ξ) := xk + ξ(xk+1 − xk )

such that φ(k)µ (x) = φ

(k)µ

(χ(k)(ξ)

):= Φµ(ξ) for x ∈ Ω(k).

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 33

Simple Example: 1D Poisson (2)We need to compute

A(k)µ,ν :=

xk+1∫xk

∂xφ(k)ν (x)

∂xφ(k)µ (x) dx =

χ(k)(1)∫χ(k)(0)

∂xφ(k)ν (x)

∂xφ(k)µ (x) dx

Compare integration by substitution:g(b)∫g(a)

f (x) dx =b∫a

f (g(t))g′(t) dt

A(k)µ,ν =

1∫0

∂xφ(k)ν

(χ(k)(ξ)

) ∂∂xφ(k)µ

(χ(k)(ξ)

)(xk+1 − xk ) dξ.

Note that:• ∂

∂ξχk (ξ) = (xk+1 − xk ) corresponds to the factor g′(t) in the substitutionrule

• we still have to deal with the partial derivatives in x

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 34

Simple Example: 1D Poisson (3)We require the chain rule, ∂f

∂x = ∂f∂g

∂g∂x , to obtain:

∂xφ(k)µ

(χ(k)(ξ)

)=

∂xΦµ(ξ) =

∂ξΦµ(ξ)

∂ξ

∂x

As x = χ(k)(ξ) := xk + ξ(xk+1 − xk ), we have ξ =x − xk

xk+1 − xkand thus:

∂ξΦµ(ξ)

∂ξ

∂x=

∂ξΦµ(ξ)

1xk+1 − xk

Finally, we get:

A(k)µ,ν =

1∫0

∂xφ(k)ν

(χ(k)(ξ)

) ∂∂xφ(k)µ

(χ(k)(ξ)

)(xk+1 − xk ) dξ

=

1∫0

∂ξΦν(ξ)

∂ξΦµ(ξ)

1(xk+1 − xk )2 (xk+1 − xk ) dξ.

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 35

Simple Example: 1D Poisson (4)Altogether, element stiffness matrices are computed as:

A(k)µ,ν =

1xk+1 − xk

1∫0

∂ξΦν(ξ)

∂ξΦµ(ξ) dξ

With Φ1(ξ) = ξ and Φ2(ξ) = 1− ξ, we obtain that

A(k) =1

xk+1 − xk

(1 −1−1 1

)Observations:

• the integral values only depend on the reference element Ωref = [0,1] andthe reference basis functions Φµ, Φν

• all other element matrices are obtained by simple scaling• we did not use that Φµ, Φν are linear functions;

same computation for more complicated basis functions!

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 36

Outlook: Extension to 2D and 3DFor 2D, 3D, etc., we require the multidimensional substitution rule:∫

Ω(k)

f (x , y , z) dΩ =

∫Ωref

f (x(ξ, η, ζ), y(ξ, η, ζ), z(ξ, η, ζ)) |J | dξ dη dζ

where |J | is the determinant of the Jacobian:

|J | =

∣∣∣∣∣∣∣∣∂x∂ξ

∂x∂η

∂x∂ζ

∂y∂ξ

∂y∂η

∂y∂ζ

∂z∂ξ

∂z∂η

∂z∂ζ

∣∣∣∣∣∣∣∣and also the multidimensional chain rule.

Result:• similar, element-wise computation of stiffness matrices via reference

elements• |J | instead of factor 1

xk+1−xk; more complicated, in general

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 37

Outlook: Automatised Finite Element AssemblyExample: FeniCS Project, https://fenicsproject.org/ Numerical Programming 2

Compute a FEM-Solution of the 2D Poisson Equation:

from fenics import *

# Create mesh and define function space

mesh = UnitSquareMesh(8, 8)

V = FunctionSpace(mesh, ’P’, 1)

# Define boundary condition (omitted)

# Define variational problem

u = TrialFunction(V)

v = TestFunction(V)

f = Constant(-6.0)

a = dot(grad(u), grad(v))*dx

L = f*v*dx

# Compute solution

u = Function(V)

solve(a == L, u, bc)

Source: https://fenicsproject.org/pub/tutorial/html/._ftut1004.html#___sec19

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 38

Outlook: Further Components and Aspects of FEM

Additional components and “features” of Finite Element Methods:• mesh generation:

structured/unstructured meshes consisting of triangles, tetrahedra,rectangles, cuboids, quadrilaterals, hexahedrals, prisms, . . .

• higher-order basis functions:polynomials of higher degree in various dimensions; “nodal” vs. “modal”basis functions, “spectral” elements, . . .

• quadrature rules for integration:Gaussian quadrature, e.g., to compute integrals of weak forms;resp. choice of quadrature points for certain elements?

• solving the system of equations:e.g., set up a global system of equations explicitly, or work directly onelement representation?

. . . and many more . . .

Michael Bader | Scientific Computing I | Introduction to Finite Element Methods | Winter 2017/2018 39