0% found this document useful (0 votes)
28 views

FEM4CFD - Week1

This document provides an overview of a short course on finite element methods (FEM) for computational fluid dynamics (CFD). The course covers the basics of FEM and the FreeFEM library in the first week focusing on solving Poisson's equation. Later weeks cover additional equations like the steady and unsteady advection-diffusion equation, Stokes equation, and Navier-Stokes equations. The document outlines the course contents, structure, and references for further reading.

Uploaded by

usmanshahid866
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views

FEM4CFD - Week1

This document provides an overview of a short course on finite element methods (FEM) for computational fluid dynamics (CFD). The course covers the basics of FEM and the FreeFEM library in the first week focusing on solving Poisson's equation. Later weeks cover additional equations like the steady and unsteady advection-diffusion equation, Stokes equation, and Navier-Stokes equations. The document outlines the course contents, structure, and references for further reading.

Uploaded by

usmanshahid866
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

A short course on

FEM for CFD


Week 1: Basics of FEM and FreeFEM, and Poisson equation

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.

• Laplacian operator (∆)


𝜕 2𝑢 𝜕 2𝑢 𝜕 2𝑢
∆𝑢 = ∇2 𝑢 = ∇ ∙ ∇𝑢 = 2 + 2 + 2
𝜕𝑥 𝜕𝑦 𝜕𝑧

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 Γ𝐷

Neumann boundary condition: ∇𝑢 ∙ 𝒏 = 𝑡ҧ on Γ𝑁

where 𝑢 ത is the specified solution field on the Dirichlet boundary Γ𝐷


𝑡ҧ is the specified flux on the Neumann boundary Γ𝑁 .

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

Check my papers for the details. 12


Why FEM?
❖ FEM offers a flexible numerical framework with a rich set of
elements of different shapes, orders and families.
❖ Applicability to complex geometries.
❖ Different element shapes – triangle, quadrilateral,
tetrahedron, brick, pyramid, wedge.
❖ Different element families – Lagrange family, Bézier family,
Spectral elements, IGA, H-Div and H-Curl elements.
❖ Ease of extensions to higher-order approximations
❖ Ease of extension to Multiphysics problems
❖ Better accuracies compared to FVM for unstructured meshes

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:
න ∇𝑤 ∙ ∇𝑢 𝑑𝑉 = න 𝑤 𝑓 𝑑𝑉 + න 𝑤 𝑡ҧ 𝑑𝐴
Ω Ω Γ𝑁

• Using the discretization, the system of equations can be written as

𝐊𝐮=𝐅
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>

x Supports triangular and tetrahedral elements only.

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

// The finite element space defined over Th is called here Vh


fespace Vh(Th, P1); Finite element space.
Element type, order etc.
P1 is 3-noded triangle.

// Define u and w in the function space


Vh u, w;
Solution and test function

// 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

// Plot the result


plot(u);
Plot the solution
load "iovtk"
int[int] Order = [1];
string filename = "Poisson2D-square-Dirichlet.vtu";
Export solution in VTK format.
savevtk(filename, Th, u, dataname="u", order=Order); Available only as first-order elements.
27
Tutorial #1 (cont’d)
• Error
𝑒 = 𝑢𝑒𝑥𝑎𝑐𝑡 − 𝑢

• L2 norm of error

𝑒𝐿2 = න 𝑒 2 𝑑𝑉 = න (𝑢𝑒𝑥𝑎𝑐𝑡 − 𝑢)2 𝑑𝑉


Ω Ω

// 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);

• Different orders of element


// The finite element space defined over Th is called here Vh
fespace Vh(Th, P1);

fespace Vh(Th, P2);

load "Element_P3";
fespace Vh(Th, P3);

29
Neumann BCs

30
Tutorial #2

න ∇𝑤 ∙ ∇𝑢𝑑𝑉 = න 𝑤 𝑓 𝑑𝑉 + න 𝑤 𝑡ҧ 𝑑𝐴
Ω Ω Γ𝑁

31
Tutorial #2 (cont’d)
𝜕𝑤 𝜕𝑢 𝜕𝑤 𝜕𝑢
න + 𝑑𝑉 = න 𝑤 𝑓 𝑑𝑉 + න 𝑤 𝑡ҧ 𝑑𝐴
Ω 𝜕𝑥 𝜕𝑥 𝜕𝑦 𝜕𝑦 Ω Γ𝑁

// Define the weak form


solve Poisson(u, w, solver=LU)
= int2d(Th)( // The bilinear part
dx(w)*dx(u) + dy(w)*dy(u)
)
- int2d(Th)( // Source term
w*f
)
- int1d(Th,3)( // Neumann BC term
w*tbar
)
+ on(1,2,4, u=20) // Dirichlet BC
;

32
Complex geometries

33
“square” function
// The triangulated domain Th
// unit square, starting at (0,0)
mesh Th = square(10, 10);

// rectangle of size 10x5, starting at (0,0)


mesh Th = square(10, 10, [10*x, 5*y]);

// rectangle of size 5x10, starting at (-4,-2)


mesh Th = square(10, 10, [-4+5*x, -2+10*y]);

//display mesh
plot(Th);

34
Complex geometries – inbuilt functions
int boundary = 1; C05

border C01(t=0, 1){x=t; y=0.0; label=boundary;}


border C02(t=0, 1){x=1; y=0.5*t; label=boundary;}
border C03(t=0, 1){x=1.0-0.5*t; y=0.5; label=boundary;}
border C04(t=0, 1){x=0.5; y=0.5+0.5*t; label=boundary;} C04
border C05(t=0, 1){x=0.5-0.5*t; y=1.0; label=boundary;}
border C06(t=0, 1){x=0.0; y=1-t; label=boundary;}

int n = 20; C03


mesh Th = buildmesh(C01(n) + C02(n/2) + C03(n/2) + C04(n/2) + C05(n/2) + C06(n));
C06
plot(Th);

C02

C01

https://doc.freefem.org/examples/mesh-generation.html

35
Complex geometries – using GMSH
load "gmsh"

// The triangulated domain Th


mesh Th = gmshload("Lshaped-gmsh-mesh.msh");
int boundary = 1;

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;

//u[] is the vector associated to the function u


u[] = A^-1*F[];
cout << u[] << endl;

43
More practice with FreeFEM
• FreeFEM - Getting started
https://doc.freefem.org/tutorials/poisson.html

• FreeFEM – Additional examples


https://doc.freefem.org/tutorials/index.html

44
45
46

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy