FEM4CFD - Week1
FEM4CFD - Week1
Dr Chennakesava Kadapa
Email: c.kadapa@napier.ac.uk
1
Contents
• Introduction to the course
• Poisson’s equation
• Navier-Stokes equation
• Introduction to Finite Element Method
• Introduction to the FreeFEM library
• Examples
2
Delivery mode
• Lecture
• Tutorial
• Exercise
3
References
More references at
https://github.com/chennachaos/awesome-FEM4CFD
4
Incompressible Navier-Stokes equation
Acceleration term Viscous term Pressure gradient
𝜕𝒖
𝜌 + 𝒖 ∙ ∇ 𝒖 − 𝜇∆𝒖 + 𝛻𝑝 = 𝒇
𝜕𝑡
Local Convective Body force
Acceleration Acceleration
𝛻∙𝒖 = 0
𝒖 = 𝑣𝑒𝑙𝑜𝑐𝑖𝑡𝑦
𝑝 = 𝑝𝑟𝑒𝑠𝑠𝑢𝑟𝑒
𝜌 = 𝑑𝑒𝑛𝑠𝑖𝑡𝑦
𝜇 = 𝑑𝑦𝑛𝑎𝑚𝑖𝑐 𝑣𝑖𝑠𝑐𝑜𝑠𝑖𝑡𝑦
5
Course structure
• Poisson’s equation
−𝜇∆𝑢 = 𝑓 Week 1
• Steady Advection-Diffusion equation
𝒂 ∙ ∇ 𝑢 − 𝜇∆𝑢 = 𝑓 Week 2
• Unsteady Advection-Diffusion equation
𝜕𝑢
𝜌 + 𝒂 ∙ ∇ 𝑢 − 𝜇∆𝑢 = 𝑓 Week 3
𝜕𝑡
• Stokes equation
𝜕𝒖
𝜌 − 𝜇∆𝒖 + 𝛻𝑝 = 𝒇 Week 4,5,6
𝜕𝑡
𝛻∙𝒖=0
• Navier-Stokes equation
𝜕𝒖
𝜌 + 𝒖 ∙ ∇ 𝒖 − 𝜇∆𝒖 + 𝛻𝑝 = 𝒇 Week 4,5,6
𝜕𝑡
𝛻∙𝒖=0
6
Poisson’s equation
7
Poisson’s equation
• Is an elliptic PDE
• Many applications
• Diffusion, heat transfer, electrostatics, magnetostatics
• Poisson’s equation is
−∆𝑢 = 𝑓
where 𝑢 is the (scalar) field, 𝑓 is the source term and ∆ is the Laplacian operator.
8
Poisson’s equation (Cont’d)
• We can also write as
−∇ ∙ (∇𝑢) = 𝑓
where ∇ is the gradient operator and ∇ ∙ is the divergence operator.
𝜕𝑢
𝜕𝑥
• Gradient operator (∇) ∇𝑢 =
𝜕𝑢
𝑇
𝜕 𝜕 𝜕 𝜕𝑦
∇= 𝜕𝑢
𝜕𝑥 𝜕𝑦 𝜕𝑧
𝜕𝑧
• Thus
𝜕 𝜕 𝜕 𝜕𝑢 𝜕𝑢 𝜕𝑢 𝜕 2𝑢 𝜕 2 𝑢 𝜕 2𝑢
∇ ∙ ∇𝑢 = ∙ = 2+ 2+ 2
𝜕𝑥 𝜕𝑦 𝜕𝑧 𝜕𝑥 𝜕𝑦 𝜕𝑧 𝜕𝑥 𝜕𝑦 𝜕𝑧
9
Boundary conditions
• Boundary conditions
Dirichlet boundary condition: 𝑢=𝑢
ത on Γ𝐷
10
Finite Element Method
11
What is FEM?
• Finite Element Method (FEM) is a numerical method for solving problems modelled by partial
differential equations (PDEs).
• FEM is not just for solid mechanics.
• FEM has become the de-facto numerical method for many problems in science and engineering.
• Simulation software and/or libraries based on FEM are available for
• Solid mechanics – stress analysis
• Structural dynamics
• Wave propagation
• Heat transfer
• Fluid dynamics
• Chemical diffusion
• Electromagnetics
13
History of FEM
❖ Dates back to 1940s
❖ Originally used for problems in structural mechanics
❖ 1960-1990 was the golden age of FEM
❖ Since 1990, an explosion of industrial applications of FEM
https://link.springer.com/article/10.1007/s11831-022-09740-9
14
FEM terminology
• The points are called nodes.
• The cells are called elements.
• The variables are usually stored at nodes.
Variable per cell is also possible.
• The solution variables at nodes are called as degrees of freedom (DOFs).
• The value at nodes (DOFs) can be a
• Scalar: temperature, pressure, density
• Vector: displacement, velocity
• Tensor: deformation gradient, strain, stress
15
Weak formulation
16
Weak form
• Poisson’s equation
−∇ ∙ (∇𝑢) = 𝑓
• Weighted residual statement
න 𝑤 𝑅 𝑑𝑉 = 0 Residual: 𝑅 = −∇ ∙ ∇𝑢 − 𝑓
Ω
• Splitting the integral
− න 𝑤 ∇ ∙ ∇𝑢 𝑑𝑉 = න 𝑤 𝑓 𝑑𝑉
Ω Ω
• After using the integrating by parts and divergence theorem
න ∇𝑤 ∙ ∇𝑢 𝑑𝑉 − න 𝑤 ∇𝑢 ∙ 𝒏 𝑑𝐴 = න 𝑤 𝑓 𝑑𝑉
Ω Γ Ω
• Using boundary conditions
න ∇𝑤 ∙ ∇𝑢 𝑑𝑉 = න 𝑤 𝑓 𝑑𝑉 + න 𝑤 𝑡ҧ 𝑑𝐴 Weak form
Ω Ω Γ𝑁
17
Weak form
• Weak form in compact form
න ∇𝑤 ∙ ∇𝑢 𝑑𝑉 = න 𝑤 𝑓 𝑑𝑉 + න 𝑤 𝑡ҧ 𝑑𝐴
Ω Ω Γ𝑁
• Weak form in component form
𝜕𝑤 𝜕𝑤 𝜕𝑤 𝜕𝑢 𝜕𝑢 𝜕𝑢
න ∙ 𝑑𝑉 = න 𝑤 𝑓 𝑑𝑉 + න 𝑤 𝑡ҧ 𝑑𝐴
Ω 𝜕𝑥 𝜕𝑦 𝜕𝑧 𝜕𝑥 𝜕𝑦 𝜕𝑧 Ω Γ𝑁
𝜕𝑤 𝜕𝑢 𝜕𝑤 𝜕𝑢 𝜕𝑤 𝜕𝑢
න + + 𝑑𝑉 = න 𝑤 𝑓 𝑑𝑉 + න 𝑤 𝑡ҧ 𝑑𝐴
Ω 𝜕𝑥 𝜕𝑥 𝜕𝑦 𝜕𝑦 𝜕𝑧 𝜕𝑧 Ω Γ𝑁
1D
2D
3D
18
Finite Element Method
• In the FEM, we split the domain into smaller parts, called elements.
• As the size of the elements is reduced, the approximation error is reduced.
• Elements are described by their connections to nodes.
• Discretisation
𝑛𝑒𝑛 𝑢1
𝑢2
𝑢 = 𝑁𝑖 𝑢𝑖 = 𝑁1 𝑁2 ⋯ 𝑁𝑛𝑒𝑛 ⋮ = 𝐍𝐮
𝑖=1 𝑢𝑛𝑒𝑛
𝑛𝑒𝑛
𝑤 = 𝑁𝑖 𝑤𝑖 = 𝐍𝐰
𝑖=1
● is a node.
𝜕𝑢 𝜕𝑁1 𝜕𝑁2 𝜕𝑁𝑛𝑒𝑛
⋯ 𝜕𝑥
𝑢1
𝜕𝑥 𝜕𝑥 𝜕𝑥
𝜕𝑢 𝜕𝑁1 𝜕𝑁2 𝜕𝑁𝑛𝑒𝑛 𝑢1 𝜕𝑵
∇𝑢 = 𝜕𝑦
= 𝜕𝑦 𝜕𝑦 ⋯ 𝜕𝑦 ⋮ = 𝐮
𝜕𝒙
𝜕𝑢 𝜕𝑁1 𝜕𝑁2 ⋯ 𝜕𝑁𝑛𝑒𝑛 𝑢𝑛𝑒𝑛
𝜕𝑧 𝜕𝑧 𝜕𝑧 𝜕𝑧
𝜕𝑵
∇𝑤 = 𝐰
𝜕𝒙
19
Discretised problem
• Weak form:
න ∇𝑤 ∙ ∇𝑢 𝑑𝑉 = න 𝑤 𝑓 𝑑𝑉 + න 𝑤 𝑡ҧ 𝑑𝐴
Ω Ω Γ𝑁
𝐊𝐮=𝐅
where,
𝑇
𝜕𝑵 𝜕𝑵
𝐊 = 𝐀 𝐊𝒆; 𝒆
𝐊 =න 𝑑𝑉
e=1 Ω𝑒 𝜕𝑥 𝜕𝑥
𝐅 = 𝐀 𝐅𝒆 ; 𝐅𝒆 = න 𝑵𝑇 𝑓𝑑𝑉 + න 𝑵𝑇 𝑡ҧ 𝑑𝐴
e=1 Ω𝑒 Γ𝑒𝑁
20
FEM ingredients
We need to evaluate
𝑇
𝜕𝑵 𝜕𝑵
𝐊 = 𝐀 𝐊𝒆; 𝒆
𝐊 =න 𝑑𝑉
e=1 Ω𝑒 𝜕𝑥 𝜕𝑥
𝐅 = 𝐀 𝐅𝒆 ; 𝐅𝒆 = න 𝑵𝑇 𝑓𝑑𝑉 + න 𝑵𝑇 𝑡ҧ 𝑑𝐴
e=1 Ω𝑒 Γ𝑒𝑁
Information needed:
• Mesh – nodal coordinates and connectivity
• Element – basis functions
• Stiffness matrix and force vector - numerical quadrature
• Linear system – matrix solver (LU, CG, etc..)
21
FreeFEM library
22
Introduction to FreeFEM
• An opensource library for FEM.
• Not an application/solver library like OpenFOAM.
• Library that provides all the basic FEM ingredients. • Input through scripts
• To develop custom solvers. • File extension “.edp”
• Similar to C++ syntax
✓ Well supported. • No need to compile
✓ Easy to install and use.
✓ Up-to-date tutorials. • FreeFem++.exe <inp-script-file>
23
Tutorial #1
24
Tutorial #1
File: Week1-Tutorial1.edp
25
Tutorial #1
// The triangulated domain Th
mesh Th = square(10,10); Mesh over a square
or mapped square
// Define functions
func uexact = (cosh(pi*y)-sinh(pi*y)/tanh(pi))*sin(pi*x); Functions for use
func f= 0;
26
Tutorial #1 (cont’d)
𝜕𝑤 𝜕𝑢 𝜕𝑤 𝜕𝑢
// Define the weak formulation න + 𝑑𝑉 = න 𝑤 𝑓 𝑑𝑉 + න 𝑤 𝑡ҧ 𝑑𝐴
Ω 𝜕𝑥 𝜕𝑥 𝜕𝑦 𝜕𝑦 Ω Γ𝑁
solve Poisson(u, w, solver=LU)
= int2d(Th)( // The bilinear part
dx(w)*dx(u) + dy(w)*dy(u) Weak form LHS
)
- int2d(Th)( // The right hand side
w*f
)
Weak form RHS
// The Dirichlet boundary condition
+ on(1,2,3,4, u=uexact)
; Boundary conditions
• L2 norm of error
// Error
real error = int2d(Th)(
(uexact-u)^2
);
error = sqrt(error);
28
Exercise #1
• Different number of elements
Solve the problem in Tutorial 1 using different
// The triangulated domain Th number of elements and different orders of
mesh Th = square(10,10); elements and observe the error.
mesh Th = square(20,20);
mesh Th = square(80,80);
load "Element_P3";
fespace Vh(Th, P3);
29
Neumann BCs
30
Tutorial #2
න ∇𝑤 ∙ ∇𝑢𝑑𝑉 = න 𝑤 𝑓 𝑑𝑉 + න 𝑤 𝑡ҧ 𝑑𝐴
Ω Ω Γ𝑁
31
Tutorial #2 (cont’d)
𝜕𝑤 𝜕𝑢 𝜕𝑤 𝜕𝑢
න + 𝑑𝑉 = න 𝑤 𝑓 𝑑𝑉 + න 𝑤 𝑡ҧ 𝑑𝐴
Ω 𝜕𝑥 𝜕𝑥 𝜕𝑦 𝜕𝑦 Ω Γ𝑁
32
Complex geometries
33
“square” function
// The triangulated domain Th
// unit square, starting at (0,0)
mesh Th = square(10, 10);
//display mesh
plot(Th);
34
Complex geometries – inbuilt functions
int boundary = 1; C05
C02
C01
https://doc.freefem.org/examples/mesh-generation.html
35
Complex geometries – using GMSH
load "gmsh"
mesh Th = gmshload("cylinder-gmsh-mesh.msh");
int inlet = 1;
int outlet = 2;
int topwall = 3; //display mesh
plot(Th);
int bottomwall = 4;
int cylinder = 5;
36
Exercises
37
Exercise #2
Solve the Poisson equation over the L-shaped domain.
Use the mesh from “Lshaped-gmsh-mesh.msh”.
𝑢 = 0 on all boundaries.
𝑓 = 20
38
Solution for exercise #2
39
Exercise #3
Solve the Poisson equation for the following problem.
Use the mesh from “cylinder-gmsh-mesh.msh”.
𝜕𝑢
= −2
𝜕𝑦
𝑢 = 50
𝑢 = 30
𝑢 = 10 𝑓=0
𝜕𝑢
=0
𝜕𝑦
40
Solution for exercise #3
41
42
Access to matrix and vector
// Define the weak (variational) formulation // Define the weak (variational) formulation
varf PoissonLHS(u, w) varf PoissonRHS(unused, w)
= int2d(Th)( // The bilinear part = int2d(Th)(
dx(w)*dx(u) f*w
+ dy(w)*dy(u) )
) // The Dirichlet boundary condition
// The Dirichlet boundary condition + on(1,2,3,4, u=uexact)
+ on(1,2,3,4, u=uexact) ;
;
Vh F;
//F[] is the vector associated to the function F
matrix A = PoissonLHS(Vh, Vh); //stiffness matrix F[] = PoissonRHS(0,Vh);
cout << A << endl; cout << F[] << endl;
43
More practice with FreeFEM
• FreeFEM - Getting started
https://doc.freefem.org/tutorials/poisson.html
44
45
46