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

FF Basic Tutorial

The document discusses modeling transport phenomena during acupuncture using the FreeFem++ solver. It introduces FreeFem++, describes how to write the first FreeFem++ code, and outlines numerical examples. FreeFem++ is an open source PDE solver that uses the finite element method. It can automatically generate meshes and solve problems by defining them variationally.

Uploaded by

Eriselda Goga
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)
29 views

FF Basic Tutorial

The document discusses modeling transport phenomena during acupuncture using the FreeFem++ solver. It introduces FreeFem++, describes how to write the first FreeFem++ code, and outlines numerical examples. FreeFem++ is an open source PDE solver that uses the finite element method. It can automatically generate meshes and solve problems by defining them variationally.

Uploaded by

Eriselda Goga
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/ 70

Modeling anda PDEs

FreeFem++, simulation onTutorial


solver : Basic transport
phenomena during acupuncture
Yannick Deleuze

Yannick Deleuze
Laboratoire Jacques-Louis Lions, Sorbonne Universités, UPMC Univ Paris 06, France
Scientific Computing and Cardiovascular Lab, Department of ESOE, National Taiwan University
Center of Advanced Study in Theoretical Sciences, National Taiwan University

CASTS-LJLL Workshop
Under the supervision
on Applied Mathematics of
and Mathematical Sciences
Marc Thiriet May
and 28, 2013 Tony W.H. Sheu
Outline

Introduction

First FreeFem++ first code

Numerical examples
Outline

Introduction
FreeFem++

First FreeFem++ first code

Numerical examples
FreeFem++

FreeFem++ is an open source partial differential equation solver. It is


developped in the Laboratory Jacques-Louis Lions (LJLL) of University Pierre
et Marie Curie (Paris, France) to solve PDEs.
I finite elements method, including discontinuous FE spaces;
I 2D/3D
I built on C++, syntax from C++ adapted to mathematics
I automatic mesh generator (2D/3D);
I load, save mesh;
I unstructured meshes;
I mesh adaptation (2D);
I problem definition using variational formulation
I fast linear solvers;
I mpi tools for parallel computing.

Goal
Present a basic introduction to FreeFem++ for beginners with FreeFem++.
Outline

Introduction
FreeFem++

First FreeFem++ first code

Numerical examples
FreeFem++

I Download the current FreeFem++ from Frédéric Hecht website :


http://www.freefem.org/ff++/
I Full documentation and examples at
http://www.freefem.org/ff++/ftp/freefem++doc.pdf
I FreeFem++-cs, an integrated environment with graphical interface , from
Antoine Le Hyaric website :
http://www.ann.jussieu.fr/~lehyaric/ffcs/index.htm
I Taiwan Society for Industrial and Applied Mathematics (TWSIAM)
FreeFem++ activity group :
http://homepage.ntu.edu.tw/~twhsheu/twsiamff++/freefem.html
I Tutorial website :
http://www.ljll.math.upmc.fr/deleuze/freefem.html
FreeFem++ Documentation

Third Edition, Version 3.20

http://www.freefem.org/ff++

F. Hecht, O. Pironneau
A. Le Hyaric, K. Ohtsuka

Keep FreeFem++ documentation close by.


Laboratoire Jacques-Louis Lions, Université Pierre et Marie Curie, Paris
Start using FreeFem++

Installation of FreeFem++ is really easy on Windows and MacOS platforms.


On Linux, the software needs to be compiled from source code.

FreeFem++ scripts can be written in any text editor and saved to a ”.edp” file.
Lauching an ’.edp’ file depends on your operating system.

I recommend to start using FreeFem++ with FreeFem++-cs, an integrated


environment for FreeFem++ (Windows, OS X, Linux). It provides a text editor
with FreeFem++ syntax and highlighting of FreeFem++ compilation errors, a
text output and visualisation. Developed by Antoine Lehyaric (LJLL).
http://www.ann.jussieu.fr/~lehyaric/ffcs/index.htm
Main idea of a FE code using FreeFem++

Main steps to a PDE using the FEM


I define the domain and generate the mesh.
I define the finite element space (basis functions) for the unknowns and test
functions.
I set the problem : in FreeFem++, a problem must be given in it’s weak
form.
I solve the problem.
I visualise the solution.
Workflow of a FreeFem++ script

Generate the mesh Th


mesh Th = ... ;

Define the finite element space Vh


fespace Vh (Th, P1);

Define the variational problem P


problem P(u,v) = a(u,v) - l(f,v) + (Dirichlet boundary condition);

solve the problem P


P;

visualise the solution u


plot(u);
FreeFem++ syntax

1 x , y , z // C a r t e s i a n c o o r d i n a t e s
2 N . x , N . y , N . z // Normal v e c t o r components
3 i n t k = 1 0 ; // i n t e g e r
4 r e a l a = 2 . 5 ; // r e a l
5 b o o l b=(a <3.) ; / b o o l e a n
6 r e a l [ i n t ] a r r a y ( k ) ; // a r r a y o f k e l e m e n t s
7 a r r a y [ ] [ 5 ] ; // 6 t h v a l u e o f t h e a r r a y
8 mesh Th ; // 2d mesh
9 mesh3 Th3 // 3d mesh
10 f e s p a c e Vh ( Th , P1 ) ; // f i n i t e e l e m e n t s p a c e
11 Vh u=x ; // f i n i t e e l e m e n t f u n c t i o n
12 Vh3<complex> uc = x+ 1 . i ∗y ; // c o m p l e x f i n i t e e l e m e n t f u n c t i o n
13 f e s p a c e Xh ( Th , [ P2 , P2 , P1 ] ) ;
14 Xh [ u1 , u2 , p ] ; // a v e c t o r i a l f i n i t e e l e m e n t f u n c t i o n o r a r r a y
15 u [ ] ; // t h e a r r a y a s s o c i a t e d t o FE f u n c t i o n u
16 u ( 1 . , 0 . 1 , 3 . ) ; // v a l u e o f u a t p o i n t ( 1 . , 0 . 1 , 3 . )
17 u [ ] . max ; // max o f t h e a r r a y u
18 u [ ] . min ; // max o f t h e a r r a y u
19 u [ ] . sum ; //sum o f a l l e l e m e n t s o f u
20 u [ ] . l 1 ; // l 1 norm o f u
21 u [ ] . l 2 ; // l 2 norm o f u
22 u [ ] . l i n f t y ; // l i n f i n i t y norm o f u
23 macro d i v ( u , v ) ( dx ( u )+dy ( v ) ) // EOM
24 macro Grad ( u ) [ dx ( u ) , dy ( u ) ] // EOM
25 f u n c f=x+y ; // f u n c t i o n
26 f u n c r e a l f ( i n t i , r e a l a ) { . . . . . ; r e t u r n i+a ; }
27 v a r f a ( [ u1 , u2 , p ] , [ v1 , v2 , q ] )= i n t 2 d ( . . . ) + on ( . . )
28 m a t r i x A = a ( Vh , Vh , s o l v e r=UMFPACK) ; // r i g i d m a t r i c o f t h e p r o b l e m
29 r e a l [ i n t ] b=a ( 0 , V3h ) ; // r i g h t hand s i d e
30 u [ ] =Aˆ−1∗b ; // s o l v i n g u = Aˆ−1∗b
Outline

Introduction

First FreeFem++ first code


Model problem
Finite element method
FreeFem++ implementation
Finite element spaces and discrete problem

Numerical examples
Outline

Introduction

First FreeFem++ first code


Model problem
Finite element method
FreeFem++ implementation
Finite element spaces and discrete problem

Numerical examples
Laplace equation

An elastic membrane Ω is attached to a rigid support ∂Ω, and a force f (x) is


exerted on the surface. The vertical membrane displacement u(x) is obtained
by solving Poisson’s equation:

−∇2 u = f , in Ω
u|∂Ω = g.
Outline

Introduction

First FreeFem++ first code


Model problem
Finite element method
FreeFem++ implementation
Finite element spaces and discrete problem

Numerical examples
Integration by parts

Let Ω be an open compact domain of Rd with a smooth boundary ∂Ω. Let


u, v ∈ C 2 (Ω̄). Then

1. Gauss-Ostrogradsky theorem (divergence theorem)


Z Z
div U dx = U.n dΓ
Ω ∂Ω

2. Integration by parts
Z Z Z
∂v ∂u
u dx = − v dx + u v dΓ
Ω ∂xi Ω ∂xi ∂Ω

3. Green’s first identity


Z Z Z
∇2 u v dx = − ∇u · ∇v dx + (∇u · n) v dΓ
Ω Ω ∂Ω

4. Green’s second identity


Z Z Z
∇2 u v dx − u ∇2 v dx = ((∇u · n) v − (∇v · n) u) dΓ
Ω Ω ∂Ω
Weak form

First, we derive the weak form of the equation.


We multiply the Laplace’s equation by a smooth test function v and integrate
over the entire domain. We apply Green’s identity and it gives
Z Z Z
∇u · ∇v dx − (∇u · n) v dΓ = f v dx.
Ω ∂Ω Ω

Due to the Dirichlet boundary conditions we choose v such that v|∂Ω = 0. Let
Vϕ = {w ∈ H 1 (Ω)|w|∂Ω = ϕ}, then the problem becomes

find u ∈ Vg such that ∀v ∈ V0


Z Z
∇u.∇v dx − fv dx = 0
Ω Ω

R R
a(u, v ) = Ω
∇u.∇v is the bilinear form and l(v ) = Ω
fv is the linear form.
Finite element method

Freefem++ uses the most common finite elements for the discretisation of the
continuous problem.
Consider the continuous piecewise polynomial of degree one (P1 ) finite element
dizcretization on the triangulation Th of Ωh .
The discrete problem becomes

find uh ∈ Vgh such that ∀vh ∈ V0h


Z Z
∇uh .∇vh − fvh = 0
Ω Ω
Outline

Introduction

First FreeFem++ first code


Model problem
Finite element method
FreeFem++ implementation
Finite element spaces and discrete problem

Numerical examples
Meshing

It order to solve the problem on a computer, we need to decretize it. The first
step of the discretisation is to triangulate the physical domain Ω into the
computational domain Th. We can use the keyword square to triangulate the
domain and generate the mesh. We need to provide a number N of vertex
placed on the borders.
square
1 i n t nn =10;
2 mesh Th = s q u a r e ( nn , nn ) ;
3 p l o t ( Th , w a i t =1 , p s=” m e s h s q u a r e . e p s ” ) ;

rectangle
1 r e a l x0 =0 , x1 =10; r e a l l x=x1−x0 ;
2 r e a l y0 =0 , y1 =5; r e a l l y=y1−y0 ;
3 mesh Th1=s q u a r e ( nn , 2 ∗ nn , [ x0+(x1−x0 ) ∗x , y0+(y1−y0 ) ∗ y ] ) ;
4 p l o t ( Th1 , w a i t =1 , p s=” m e s h r e c t a n g l e . e p s ” ) ;

Note that we use the keyword plot to plot the mesh and the keyword wait to
stop the program at each plot.
Meshing : borders

Figure : Square mesh generated with Figure : Rectangle mesh generated with
square square
Meshing : borders

In FreeFem++, the geometry of the domain can be easily defined by an


analytic description of boundaries by pieces and the mesh is automatically
generated. Each piece of boundary is described by it’s parametric equation.
The borders are declared with the type border. The keyword label defines a
group of boundaries. The label type can be either an integer or a name. The
mesh is then generated by the keyword buildmesh. For is border, we need to
provide a number of vertex to be placed on it. The border has to be well
defined in the counterclockwise direction. The borders can not cross each
others.

1 b o r d e r c1 ( t=x0 , x1 ) { x=t ; y=y0 ; l a b e l = 1 ; } ;


2 b o r d e r c2 ( t=y0 , y1 ) { x=x1 ; y=t ; l a b e l = 2 ; } ;
3 b o r d e r c3 ( t=x1 , x0 ) { x=t ; y=y1 ; l a b e l = 3 ; } ;
4 b o r d e r c4 ( t=y1 , y0 ) { x=x0 ; y=t ; l a b e l = 4 ; } ;
5 mesh Th2= b u i l d m e s h ( c1 ( l x ∗N/ 2 )+c2 ( l y ∗N/ 2 )+c3 ( l x ∗N/ 2 )+c4 ( l y ∗N/ 2 ) ) ;
6 p l o t ( Th2 , w a i t =1 , p s=” mesh . e p s ” ) ;

We use the keyword ps to save the plot in an eps file.


Meshing : borders

Figure : Mesh generated with border and build mesh


Meshing : borders

1 i n t c i r c l e =0;
2 b o r d e r c ( t =0 ,2∗ p i ) { x=1+3∗ c o s ( t ) ; y=−1+3∗ s i n ( t ) ; l a b e l= c i r c l e ; }
3 mesh Th3=b u i l d m e s h ( c ( 1 0 0 ) ) ;
4 p l o t ( Th3 , w a i t =1 , p s=” c i r c l e . e p s ” , cmm=” C i r c l e mesh ” ) ;

Circle mesh

Figure : Mesh generated with border and buildmesh


Meshing : ranunculoid

Ranunculoid mesh
Meshing : save & load

Use savemesh and readmesh to save or load a mesh file ”.msh”

1 s a v e m e s h ( Th2 , ” m e s h f i l e . msh” ) ;

1 mesh Th3 = r e a d m e s h ( ” m e s h f i l e . msh” ) ;


Outline

Introduction

First FreeFem++ first code


Model problem
Finite element method
FreeFem++ implementation
Finite element spaces and discrete problem

Numerical examples
Finite Element Spaces

The FEM approximates all function


u(x, y ) ' u0 Φ0 (x, y ) + ... + uM−1 ΦM−1 (x, y ) with finite element basis functions
Φk (x, y ).

FreeFem++ handles Lagrangian finite elements (P0,P1,P2,P3,P4), bubble


elements (P1b,P2b), discontinuous P1, Raviart-Thomas, ... (see full
documentation).

fespace defines the the discrete FE space for the unknowns and test functions;
n o
Vh(Th, P1) = w (x, y )|w (x, y ) = ΣM k=1 wk Φk (x, y ), wk ∈ R

Declare the finite element space


1 f e s p a c e Vh ( Th , P1 ) ; // P1 FE s p a c e

Declare the unknown and the test functions


1 Vh u , v ; // unkown and t e s t f u n c t i o n .
Finite Element Spaces

P0 piecewise constant discontinuous finite element. The degrees of


freedom are the barycenter element value.
P1 piecewise linear continuous finite element. The degrees of
freedom are the vertices values.
P2 piecewise continuous quadratic finite element. The degrees of
freedom are the vertices values and the middle point of each
edge.
P3 piecewise continuous cubic finite element. (need : load
”Element P3”)
P4 piecewise continuous quartic finite element. (need : load
”Element P4”)
P1b piecewise linear continuous plus bubble. [1]
P2b piecewise quadratic continuous plus bubble. [1]
P1dc piecewise linear discontinuous finite element.
P2dc piecewise P2 discontinuous finite element.
For full list, refer to FreeFem++ documentation.
[1] Brezzi, F. & Russo, A. Choosing Bubbles for Advection-Diffusion Problems. Mathematical Models and Methods in Applied Sciences

04, 571587 (1994). (The residual-free bubbles technique interprets the stabilization parameter as the mean value of the solution of a

differential equation defined at the element level.)


Discrete variational form

In FreeFem++, the problem can be declared with the keyword


problem P(u,v) = a(u,v) - l(f,v) + (Dirichlet BC);
The bilinear form and the linear form should not be written under the same
integral.
Z
∇u.∇v → int2d(Th)( dx(u)*dx(v) + dy(u)*dy(v) )

Z
fv → int2d(Th)(f*v )

where dx(u) = ∂u
∂x
∂u
, dy(u) = ∂y . The keyword on defines the Dirichlet boundary
condition on the borders 1, 2, 3 and 4.

1 f u n c f =1; // r i g h t hand s i d e f u n c t i o n
2 f u n c g =0; // boundary c o n d i t i o n f u n c t i o n
3
4 // definion of the problem
5 problem l a p l a c e (u , v ) =
6 i n t 2 d ( Th ) ( dx ( u ) ∗ dx ( v ) + dy ( u ) ∗ dy ( v ) ) // b i l i n e a r form
7 − i n t 2 d ( Th ) ( f ∗ v ) // l i n e a r form
8 + on ( 1 , 2 , 3 , 4 , u=g ) ; // b o u n d a r y c o n d i t i o n form
Solve the problem

FreeFem++ solves the linear problem of the form


Z
ΣM−1
j=0 Aij uj − Fi = 0, i = 1, ..., M − 1, Fi = f Φi

FreeFem++ automatically builds the associate matrix and the second member
vector.

1 laplace ;

FreeFem++ solves elliptic equation. The user should specify it’s own
algorithms to solve parabolic and hyperbolic problems.

The user can specify the solver to solve the linear problem associated.
FreeFem++ provides a large variety of linear direct and iterative solvers (LU,
Cholesky, Crout, CG, GMRES, multi-frontal method UMFPACK, MUMPS
(MUltifrontal Massively Parallel sparse direct Solver), SuperLU, ...).
Visualization

Use the keyword plot to plot the solution with useful options :
I ps= string to save the plot in a eps file;
I cmm = string to add comment
I if value= 1 plot the value of isolines
I if fill=1 color fill between isolines
I if wait=1 stop the program at this plot

1 p l o t ( u , p s=” L a p l a c e . e p s ” , v a l u e =1 , w a i t =1 , f i l l =1 , cmm=” S o l u t i o n u o f
t h e L a p l a c e e q u a t i o n i n \Omega” ) ;
Outline

Introduction

First FreeFem++ first code

Numerical examples
Heat equation
Convection
Stationary Stokes problem
Convection-Diffusion Equation
Outline

Introduction

First FreeFem++ first code

Numerical examples
Heat equation
Time discretization
Spatial discretization
Practical implementation
Convection
Stationary Stokes problem
Convection-Diffusion Equation
Heat problem

Freefem++ is able to solve time indecent problems but it is the user’s


responsibility to provide the algorithms.
 ∂u
− ∇ · (κ∇u) = 0 in (0, T ) × Ω
 ∂t


u|t=0 = U0 in Ω

 ∇u · n = UN on (0, T ) × ΓN
u = UD on (0, T ) × ΓD

Time discretization

First, let us consider a time discretization of the heat equation (35) . Time is
discretize with finite difference schemes such as
θ - scheme [Raviart-Thomas 1998]
We discretize time (0, T ) in M − 1 intervals ∆t = T
M−1
and M points (tm )M−1
m=0
such that U(tm , x) = U m (x).

∂U U n+1 − U n
≈ = θF (U n+1 ) + (1 − θ)F (U n )
∂t ∆t
I Euler explicit (θ = 0)
I Euler implicit (θ = 1)
I Crank-Nicolson (θ = 12 )
Time discretization

Let us choose the Euler implicit quadrature. The heat problem in its
semi-discrete form becomes

u n+1 − u n
− ∇ · (κ∇u n+1 ) = 0
∆t
Exercise : Implement the Crank-Nicolson scheme or any higher order scheme of
your choice.
Spatial discretization

We multiply the Laplace’s equation by a smooth test function v and integrate


over the entire domain. We apply Green’s identity and it gives
Z Z Z Z
1 n+1 1 n
u v+ κ∇u n+1 ∇v − κ(∇u n+1 · n) v − u v =0
Ω ∆t Ω ∂Ω Ω ∆t
Due to the Dirichlet boundary conditions we choose v such that v|∂Ω = 0 on
ΓD and we use the Neumann boundary condition on ΓN
Z Z Z Z
1 n+1 1 n
u v+ κ∇u n+1 ∇v − κ UN v − u v =0
Ω ∆t Ω ∂ΓN Ω ∆t

We can rewrite it, find u n+1 ∈ V such that ∀v ∈ V0

a(u, v ) = l(v )

with
1 n+1
κ∇u n+1 ∇v the bilinear form,
R R
I a(u, v ) = Ω ∆t
u v+ Ω
R R 1 n
I and l(v ) = − ∂ΓN
κ UN v − Ω ∆t u v the linear form.
Heat equation on the unit square

Let’s consider the problem


 ∂u
 ∂t − ∇ · (κ∇u) = 0 in (0, T ) × Ω,
u = sin(π x) sin(π y ) in Ω,
 |t=0
u = 0 on (0, T ) × ΓD = ∂Ω.

Then, the exact solution is


2
u(x, y , t) = sin(π x) sin(π y ) e−2π t
Generating mesh

square mesh

1 i n t CD=10; // c o u l d be a n y t h i n g
2 r e a l x0 =0 , x1 =1;
3 r e a l y0 =0 , y1 =1;
4 i n t nn =30;
5 mesh Ths=s q u a r e ( nn , nn , [ x0+(x1−x0 ) ∗x , y0+(y1−y0 ) ∗ y ] ) ;
6 i n t [ i n t ] l a b e l s =[1 ,CD, 2 , CD, 3 , CD, 4 , CD ] ; // c h a n g e t h e l a b e l s from
1 , 2 , 3 , 4 t o CD
7 Ths=c h a n g e ( Ths , l a b e l= l a b e l s ) ;
8 p l o t ( Ths , w a i t =1 , p s=” s q u a r e . e p s ” , cmm=” s q u a r e mesh ” ) ;
Defining the problem

Parameters
1 i n t i ; // t i m e l o o p i t e r a t o r
2 i n t M=10; // t i m e i t e r a t i o n s
3 r e a l d t = 0 . 0 0 0 0 1 ; // t i m e s t e p

Finite element space


1 f e s p a c e Vhs ( Ths , P1 ) ;
2 Vhs u , u0 , v ; // unknow and t e s t f u n c t i o n

Initial condition
1 u = s i n ( p i ∗x ) ∗ s i n ( p i ∗y ) ;

Variational problem
1 r e a l kappa= 1 ;
2 macro F ( u , v ) kappa ∗( dx ( u ) ∗ dx ( v )+dy ( u ) ∗ dy ( v ) ) //eom
3 p r o b l e m h e a t ( u , v , i n i t =i )
4 = i n t 2 d ( Ths ) ( u∗ v / d t )
5 + i n t 2 d ( Ths ) ( F ( u , v ) )
6 − i n t 2 d ( Ths ) ( u0∗ v / d t )
7 + on (CD, u=0)
8 ;
Solving the system

Time loop
1 f o r ( i =0; i <M; i ++) {
2 u0=u ; // u p d a t e from p r e v i o u s s t e p t i m e
3 h e a t ; // s o l v e t h e l i n e a r s y s t e m
4 p l o t ( u , w a i t =0 , f i l l =1 , v a l u e =1 ,cmm=” s o l u t i o n a t t i m e ”
+i ∗ dt , v i s o=v i s o ) ; // p l o t t h e s o l u t i o n a t e a c h
step time
5 }
Visualize the solution

Visualize the solution


1 r e a l t i m e= M∗ d t ; // c u r r e n t t i m e
2 p l o t ( u , v a l u e=t r u e , w a i t =1 , f i l l =t r u e , cmm=” s o l u t i o n a t t i m e ”+
time , v i s o=v i s o ) ;
Heat equation with Dirichlet and Neumann boundary conditions
Exercise: Now, let’s consider the problem
 ∂u
− ∇ · (κ∇u) = 0 in (0, T ) × Ω,
 ∂t


u|t=0 = UD = 20 in Ω,

 ∇u · n = UN = −1 on (0, T ) × ΓN ,
u = UD = 20 on (0, T ) × ΓD .

Circle with circular hole mesh

!_N

!_D

Figure : Ω

1D integration on the border Γ


Z
∇u · nv → int1d(Th)( ( dx(u)*N.x + dy(u)*N.y ) v )
Γ
Outline

Introduction

First FreeFem++ first code

Numerical examples
Heat equation
Convection
Characteristic-Galerkin method
Operators
Solvers
Input-Output
Practical implementation
Stationary Stokes problem
Convection-Diffusion Equation
Convection

Consider the convection equation in Ω × (0, T )


∂u
+ a · ∇u = f , u|∂Ω = 0
∂t

Characteristic-Galerkin method
The convection equation can be discretised as [See FreeFem++
documentation]
u n+1 (x) − u n (X n )
= f n (x).
∆t

I Euler scheme: X n = x − an (x) ∆t


I Second order Runge-Kutta: X n = x − an (x − an (x) ∆t
2
) ∆t
The problem becomes: find uhn+1 ∈ Vh0 such that ∀vh ∈ Vh0 :
Z Z Z
1 n+1 1 n
uh vh − uh ◦ X n vh − fvh = 0
Ω dt Ω dt Ω
Convection

Find uhn+1 ∈ Vh0 such that ∀vh ∈ Vh0 :


Z Z Z
1 n+1 1 n n
uh vh − uh (X ) vh − fvh = 0
Ω dt Ω dt Ω

FreeFem++ provides an interpolation operator


convect(an , −∆t, u n ) ≈ u n (X n ) = u n (x − an (x) ∆t) such that the code for the
problem is

1 p r o b l e m c o n v e c t ( u , w)
2 = i n t 2 d ( Th ) ( 1/ d t ∗u∗w)
3 − i n t 2 d ( Th ) ( 1 / d t ∗ c o n v e c t ( [ a1 , a2 ] , − dt , up ) ∗w)
4 − i n t 2 d ( Th ) ( f ∗w) ;
Operators

FreeFem++ provides many operators


I dx(u), dy(u), dxx(u), dyy(u), dxy(u)
I convect(a,dt,u)
I sin(u), exp(u), ...
I int1d(u), int2d(u)
but you can make your own
I macro div(u,v) ( dx(u)+dy(v) ) //EOM
Exercise : Write macros to enhance your codes.
Solvers

The user can provide the solver to solve the linear problem associated.
FreeFem++ provides a large variety of linear direct and iterative solvers (LU,
Cholesky, Crout, CG, GMRES, UMFPACK, MUMPS, SuperLU, ...).
A useful option is init. If init 6= 0, than the decomposition of the stiff matrix
associated to the problem is reused.
Exercise :
I Change the solver used.
I Use the init parameter to save computational time.
Input-Output in FreeFem++
The user can import/export data from FreeFem++ such as meshes files,
postscript images, text files. Other format are handle with FreeFem++ to
interact with third parties softwares.
Terminal
The syntax write/read in the terminal is similar to C++ with the keywords cin,
cout, <<, >>, endl.
1 i n t nn ;
2 c o u t << ” number o f n o d e s on t h e b o r d e r s nn= ” << e n d l ; // w r i t i n g i n
the terminal
3 c i n >> nn ; // r e a d v a l u e from t h e t e r m i n a l
4 mesh Tht=s q u a r e ( nn , nn ) ;

Files
To write/read a file, first the user need to declare a variable with ofstream
(output) or ifstream (input). Then, the syntax for input and output in a file
remains the same.
1 i f s t r e a m f i n ( ” d a t a . t x t ” ) ; // d e c l a r e an i n p u t from f i l e
2 r e a l ff , fg ;
3 f i n >> f f ; // r e a d s t h e 1 s r t v a l u e from t h e f i l e
4 f i n >> f g ; // r e a d s t h e 2 nd v a l u e from t h e f i l e
5 f u n c f= f f ;
6 f u n c g=f g ;
Input-Output in FreeFem++

Mesh
To import/export mesh the function read mesh and save mesh are available.
Refer to the documentation for full list of format one can import in
FreeFem++.
1 s a v e m e s h ( Tht , ” m e s h f i l e . msh” ) ; // e x p o r t mesh
2 mesh Th = r e a d m e s h ( ” m e s h f i l e . msh” ) ; // i m p o r t mesh

Finite element function (array of value)


To import/export the solution of a finite element function we use the following
syntax
1 {
2 ofstream fout (” s o l u t i o n . txt ”) ;
3 f o u t << u [ ] ; // e x p o r t t h e s o l u t i o n
4 } // u s e b r a c k e t t o c l o s e t o f i l e ( c a l l t h e d e s t r u c t o r )

1 Vh u 4 p l o t ;
2 { ifstream fu (” s o l u t i o n . txt ”) ;
3 f u >> u 4 p l o t [ ] ; } // i m p o r t a FE s o l u t i o n
4 p l o t ( u 4 p l o t , p s=” L a p l a c e . e p s ” ) ; // e x p o r t image

Exercise : use input/output functionalities in your codes.


Mesh file

9 8 8
0 0 4
0.5 0 1
1 0 2
0 0.5 4
0.5 0.5 0
N b V e r t i c e s NbElements NbBorderElem
1 0.5 2
// c o o r d i n a t e s o f v e r t i c e s
0 1 4
x1 y1 l a b e l // 1 s r t v e r t i c e
0.5 1 3
x2 y2 l a b e l // 2 nd v e r t i c e
1 1 3
...
1 2 5 0
// e l e m e n t w i t h 3 v e r t i c e s 1 , 2 , 3
1 5 4 0
v1 v2 v3 r e g i o n
2 3 6 0
v1 v2 v4 r e g i o n
2 6 5 0
v3 v4 v5 r e g i o n
4 5 8 0
...
4 8 7 0
// b o u n d a r y e l e m e n t i s a s e g m e n t
5 6 9 0
of 2 v e r t i c e s
5 9 8 0
v1 v2 l a b e l
1 2 1
v2 v3 l a b e l
2 3 1
...
3 6 2
6 9 2
8 7 3
9 8 3
4 1 4
7 4 4
FE solution file

9
6 . 2 5 e−62 6 . 2 5 e−32 6 . 2 5 e−62 6 . 2 5 e−32
0.0625
6 . 2 5 e−32 6 . 2 5 e−62 6 . 2 5 e−32 6 . 2 5 e−62

NbVertices
u (1) u (2) u (3)
u (4) u (5) . . .
... u( NbVertices )
Pure convection
Pure convection

Generating the mesh


1 r e a l Lx = 1 0 ;
2 r e a l Ly = 2 0 ;
3
4 r e a l [ i n t ] A ( 2 ) , B ( 2 ) , C ( 2 ) , D( 2 ) , E ( 2 ) , F ( 2 ) , G ( 2 ) , H( 2 ) , I ( 2 ) ;
5
6 A = [ 0 , 0 ] ; B=[Lx , 0 ] ; C=[Lx , Ly ] ; D=[ Lx / 2 . , Ly ] ;
7 E=[ Lx / 2 . , Ly / 4 . ] ; F= [ 0 , Ly / 4 . ] ;
8 G=[ Lx / 4 . , Ly / 8 . ] ; H=[3∗ Lx / 4 . , Ly / 8 . ] ; I =[3∗ Lx / 4 . , 7 ∗ Ly / 8 . ] ;
9
10 border l 1 ( t =0 ,1) { x=(1− t ) ∗A[ 0 ] + t ∗B [ 0 ] ; y=(1− t ) ∗A[ 1 ] + t ∗B [ 1 ] ; l a b e l =0;}
11 border l 2 ( t =0 ,1) { x=(1− t ) ∗B[ 0 ] + t ∗C [ 0 ] ; y=(1− t ) ∗B[ 1 ] + t ∗C [ 1 ] ; l a b e l =0;}
12 border l 3 ( t =0 ,1) { x=(1− t ) ∗C[ 0 ] + t ∗D [ 0 ] ; y=(1− t ) ∗C[ 1 ] + t ∗D [ 1 ] ; l a b e l =0;}
13 border l 4 ( t =0 ,1) { x=(1− t ) ∗D[ 0 ] + t ∗E [ 0 ] ; y=(1− t ) ∗D[ 1 ] + t ∗E [ 1 ] ; l a b e l =0;}
14 border l 5 ( t =0 ,1) { x=(1− t ) ∗E [ 0 ] + t ∗F [ 0 ] ; y=(1− t ) ∗E [ 1 ] + t ∗F [ 1 ] ; l a b e l =0;}
15 border l 6 ( t =0 ,1) { x=(1− t ) ∗F [ 0 ] + t ∗A [ 0 ] ; y=(1− t ) ∗F [ 1 ] + t ∗A [ 1 ] ; l a b e l =0;}
16
17 b o r d e r i 1 ( t =0 ,1) { x=(1− t ) ∗G[ 0 ] + t ∗H [ 0 ] ; y=(1− t ) ∗G[ 1 ] + t ∗H [ 1 ] ; l a b e l =0;}
18 b o r d e r i 2 ( t =0 ,1) { x=(1− t ) ∗H[ 0 ] + t ∗ I [ 0 ] ; y=(1− t ) ∗H[ 1 ] + t ∗ I [ 1 ] ; l a b e l =0;}
19
20 i n t nn =4;
21 mesh Th=b u i l d m e s h ( l 1 ( nn∗Lx )+l 2 ( nn∗Ly )+l 3 ( Lx / 2 . ∗ nn )+l 4 ( 3 . ∗ Ly / 4 . ∗ nn )+
l 5 ( Lx / 2 . ∗ nn )+l 6 ( Ly / 4 . ∗ nn )+i 1 ( Lx / 2 . ∗ nn )+i 2 ( Ly ∗ 6 . / 8 . ∗ nn ) ) ;
22
23 p l o t ( Th , w a i t =1 , p s=” p u r e c o n v e c t . e p s ” ) ;
Pure convection

Generating the velocity field


1 f u n c f y=x−Lx / 2 ;
2 f e s p a c e Uh ( Th , P1b ) ;
3 f e s p a c e Vh ( Th , P1 ) ;
4 Uh a1 , a2 , a1h , a2h ;
5 Vh p , ph ;
6
7 s o l v e S t o k e s ( [ a1 , a2 , p ] , [ a1h , a2h , ph ] )
8 = i n t 2 d ( Th ) ( dx ( a1 ) ∗ dx ( a1h )+dy ( a1 ) ∗ dy ( a1h ) )
9 + i n t 2 d ( Th ) ( dx ( a2 ) ∗ dx ( a2h )+dy ( a2 ) ∗ dy ( a2h ) )
10 + i n t 2 d ( Th ) ( dx ( p ) ∗ a1h+dy ( p ) ∗ a2h )
11 + i n t 2 d ( Th ) ( dx ( a1 ) ∗ph+dy ( a2 ) ∗ph )
12 − i n t 2 d ( Th ) ( f y ∗ a2h )
13 + on ( 0 , a1 =0 , a2 =0) ; Figure :
14 Velocity field
15 p l o t ( [ a1 , a2 ] , p s=” v e l o c i t y . e p s ” , w a i t =1) ; a = [a2 , a2 ]
Pure convection

u(10)

1 Vh u = ( s q r t ( ( x−Lx / 2 . ) ˆ2+(y−Ly / 1 6 . ) ˆ 2 )<Ly / 3 2 . ) ;


2 r e a l masse0 = i n t 2 d ( Th ) ( u ) ; // i n i t i a l mass
3 c o u t << ” i n i t i a l mass M0=” << masse0 << e n d l ;
4 p l o t ( u , n b i s o =40 , f i l l =1 , w a i t =1) ;
5
6 real t i m e = 0 ; // c u r r e n t t i m e
7 real d t =1; // t i m e s t e p
8 real i t m a x =30; // max i t e r a t i o n s
9 real [ i n t ] i t p l o t ( i t m a x ) , masse ( i t m a x ) ;
10
11 for ( int i t =0; i t <i t m a x ; i t ++){ // t i m e i t e r a t i o n
12 itplot [ it ] = it ;
13 u = c o n v e c t ( [ a1 , a2 ] , −dt , u ) ;
14 t i m e += d t ;
15 p l o t ( u , n b i s o =40 , f i l l =1) ;
16 c o u t << ” Masse0 = ” << masse0 Figure :
17 << ” ; Masse ( t ) = ” << masse [ i t ] << e n d l ; Velocity field
18 p l o t ( u , n b i s o =60 , f i l l =1 , cmm=”u ( ”+t i m e+” ) ” , a = [a2 , a2 ]
p s=” u t i m e=”+t i m e+” . e p s ” ) ;
19 }
Outline

Introduction

First FreeFem++ first code

Numerical examples
Heat equation
Convection
Stationary Stokes problem
Governing equations
Variational formulation
Practical implementation
Convection-Diffusion Equation
Stationary Stokes equations

We consider the pseudo-compressible Stokes equations on the smooth spatial


domain Ω :
1 2
− ∇ u + ∇p = f,
Re
∇.u + εp = 0,
u = g on Γ1 ,
u = 0 on Γ2 ,
p = 0 on Γ3 ,
∇u · n = 0 on Γ3 .

where u = (u1 , u2 ) is the velocity vector and p the pressure. ∇2 , here, stands
for the vector Laplacian. In Cartesian coordinantes, ∇2 u = (∇2 u1 , ∇2 u2 ).
Variational problem

To solve a problem in FreeFem++, let us write the variational formulation of


the problem. We multiply the first equation by a smooth test function
v = (v1 , v2 ), we get after applying the Green’s formula
Z Z Z Z Z
1 1
∇ui ∇vi − ∇ui · n vi − p∂i vi + p ni vi − fi vi = 0.
Re Ω Re ∂Ω Ω ∂Ω Ω

And we multiply the second equation by a smooth function q and integrate


over Ω. We get Z Z
∇.u q + εpq = 0.
Ω Ω

We consider the space Vφ = (w , r ) ∈ [H01 (Ω)]2 × L2 (Ω)|w|Γ1 = φ, w|Γ2 = 0




and choose (u, p) ∈ Vg and (v , q) ∈ V0 . The variational form reduces to


Z Z Z
1
∇ui ∇vi − p∂i vi − fi vi = 0, i = 1, 2,
Re Ω Ω Ω
Variational problem
The problem is to find (u, p) ∈ Vg such that for all (v , q) ∈ V0 . The variational
form reduces to
Z Z
1
(∇u1 ∇v1 + ∇u2 ∇v2 ) − p(∂x v1 + ∂y v2 )
Re Ω
Z Z Z Ω
+ (∂x u1 + ∂y u2 ) q + εpq − (f1 v1 + f2 v2 ) = 0
Ω Ω Ω

1 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 // F i n i t e E l e m e n t S p a c e s
3 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
4 f e s p a c e Xh ( Th , P2 ) ;
5 f e s p a c e Mh( Th , P1 ) ;
6 Xh u2 , v2 ;
7 Xh u1 , v1 ;
8 Mh p , q ;
9
10 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
11 // S t o k e s
12 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
13 f u n c g=4∗y∗(1−y ) ;
14
15 s o l v e S t o k e s ( [ u1 , u2 , p ] , [ v1 , v2 , q ] , s o l v e r=UMFPACK) =
16 // I m p l e m e n t t h e s t o k e s p r o b l e m
17 + on ( 1 , u1=g , u2 =0)
18 + on ( 2 , 4 , u1 =0 , u2=0)
19 + on ( 3 , p=0) ;
Implementation of the Stokes problem

1 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
2 // F i n i t e E l e m e n t S p a c e s
3 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
4 f e s p a c e Xh ( Th , P2 ) ;
5 f e s p a c e Mh( Th , P1 ) ;
6 Xh u2 , v2 ;
7 Xh u1 , v1 ;
8 Mh p , q ;
9
10
11 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
12 // S t o k e s
13 //−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−
14 f u n c g=4∗y∗(1−y ) ; // ∗( c o s ( 0 . 1 ∗ i ) +2) ; // p u l s a t i v e f l o w
15
16 s o l v e S t o k e s ( [ u1 , u2 , p ] , [ v1 , v2 , q ] , s o l v e r=C r o u t ) =
17 i n t 2 d ( Th ) ( ( dx ( u1 ) ∗ dx ( v1 ) + dy ( u1 ) ∗ dy ( v1 )
18 + dx ( u2 ) ∗ dx ( v2 ) + dy ( u2 ) ∗ dy ( v2 ) )
19 + p∗q ∗ ( 0 . 0 0 0 0 0 1 )
20 + p∗ dx ( v1 )+ p∗ dy ( v2 )
21 + dx ( u1 ) ∗q+ dy ( u2 ) ∗q
22 )
23 + on ( 1 , u1=g , u2 =0)
24 + on ( 2 , 4 , u1 =0 , u2=0)
25 + on ( 3 , p=0) ;
26
27 p l o t ( c o e f = 0 . 2 ,cmm=” [ u1 , u2 ] and p ” , p , [ u1 , u2 ] , w a i t =1) ;
Outline

Introduction

First FreeFem++ first code

Numerical examples
Heat equation
Convection
Stationary Stokes problem
Convection-Diffusion Equation
Governing equation
Variational formulation
Practical implementation
Convection-Diffusion Equation
The convection-diffusion equation describes the physical phenomena where
particles or other physical quantities are transferred inside a physical system
due to two processes: diffusion and convection.
∂φ
+ ∇.(uφ) − ∇.(ν∇φ) = 0,
∂t
φ|t=0 = φ0 ,
φ = φD on ΓD
∇φ.~n = u.~nφ on ΓN .
where u = (u1 , u2 ) is the velocity vector of a fluid vector, such that ∇.u = 0 in
Ω and ν is the diffusion coefficient.

In most common situation, the diffusion coefficient is constant and the velocity
field describes an incompressible flow i.e. ∇.u = 0. Then the problem simplifies
to
∂φ
+ u.∇φ − ν∇2 φ = 0,
∂t
φ|t=0 = φ0 ,
φ = φD on ΓD
∇φ.~n = u.~nφ on ΓN .
Diffusion-convection of a pollutant in a lake

∂φ
+ u.∇φ − ν∇2 φ
IsoValue
-0.489928

= 0, -0.444094
-0.39826
-0.352426
-0.306592

∂t -0.260758
-0.214923
-0.169089
-0.123255
-0.0774214
-0.0315874

φ0 ,
0.0142466

φ|t=0 = 0.0600806
0.105915
0.151749
0.197583
0.243417
0.289251
0.335085
0.380919

∇φ.~n = 0 on Γ. Vec Value


0
0.120959
0.241917
0.362876
0.483835
0.604793
0.725752
0.846711
0.967669
1.08863
1.20959
1.33055
rho FF++ err.l2=0.48317 1.4515
IsoValue 1.57246
-0.0925147 1.69342
0.0461494 1.81438
0.138592
0.231035 1.93534
0.323478 2.0563
0.41592 2.17726
0.508363 2.29821
0.600806
0.693249
0.785691
0.878134
0.970577
1.06302
1.15546
1.2479
1.34035
1.43279
1.52523
1.61768
1.84878

Figure : Flow potential Φ and vector


fields u = ∇Φ in a model lake with a river
inlet and a river outlet. A pollutant will
be released on one side of the lake and
transported by the water flow.
Figure : φ0
Diffusion-convection of particles

We model the the evolution of the density of particles in a Stokes flow. The the
evolution of the density of pollutant φ is described by the following problem :
find the numerical solution φh ∈ Vh such that ∀vh ∈ Vh
Z
∂φh
vh − u.∇vh φh + ν∇φh .∇vh = 0, (1)
Ω ∂t

where u = (u1 , u2 ) is the velocity of the fluid and ν is the diffusivity of the
particles.
Diffusion-convection of particles in a Stokes flow

1 // / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
2 // D e f i n i n g t h e t i m e p r o b l e m
3 // / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
4 r e a l nu = 0 . 0 1 ; // d i f u s i v i t y
5 r e a l d t = 0 . 0 1 ; // t i m e s t e p
6 i n t M=300; // t i m e i t e r a t i o n s
7 i n t i t e =0; // t i m e i t e r a t i o n
8
9 // b i l i n e a r form
10 macro a t ( p h i h , vh ) ( 1 . / d t ∗ p h i h ∗ vh + nu ∗ ( dx ( p h i h ) ∗ dx ( vh ) + dy ( p h i h
) ∗ dy ( vh ) ) + ( u1 ∗ dx ( p h i h ) + u2 ∗ dy ( p h i h ) ) ∗ vh ) //eom
11 // l i n e a r form
12 macro l ( vh ) ( 1 . / d t ∗ p h i p ∗ vh ) //eom
13
14 p r o b l e m CD ( p h i , w , i n i t = i t e ) =
15 i n t 2 d ( Th ) ( a t ( p h i , w) )
16 −i n t 2 d ( Th ) ( l (w) ) ;

Ex1 : Write the loop in time.


Ex2 : Write a Cranck-Nicolson scheme in time.
Diffusion-convection of particles in a Stokes flow

1 // / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
2 // S o l v i n g t h e p r o b l e m
3 // / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
4 // i n i t i a l c o n d i t i o n : p o l l u t i o n
5 f u n c i n i t p h i = 2∗ e xp ( −500∗(( x −0.5) ˆ2+(y −0.5) ˆ 2 ) ) ;
6 p h i= i n i t p h i ;
7 p l o t ( p h i , f i l l =1 , v a l u e =1 , w a i t =1 , cmm=” p h i ( 0 ) ” ) ;
8
9 f o r ( i t e =0; i t e <M; i t e ++){
10 p h i p=p h i ;
11 CD ;
12 p l o t ( p h i , w a i t =0 , f i l l =1 , cmm=” p h i ( ”+ i t e ∗ d t+” ) ” ) ;

Ex : Reduce the diffusivity. What do you observe ?


Exercise : convect operator and solver

FreeFem++ provides an interpolation operator convect for the ∂t u + ((u.∇)u).


We can rewrite the problem giving the discretisation in time :
find φn+1
h ∈ Vh such that ∀vh ∈ Vh:
Z
1 n+1
(φh − φnh ◦ X n ).vh + ∇φn+1 n+1
h .∇vh − u.∇φh vh = 0
Ω dt

The term φnh ◦ X n will be computed by the operator convect.

1 macro g r a d ( u ) [ dx ( u ) , dy ( u ) ] //eom
2
3 p r o b l e m CDconvect ( p h i , w) =
4 i n t 2 d ( Th ) ( 1/ d t ∗ p h i ∗w + nu∗ g r a d ( p h i ) ’ ∗ g r a d (w) )
5 − i n t 2 d ( Th ) ( 1 / d t ∗ c o n v e c t ( [ u1 , u2 ] , − dt , p h i p ) ∗w)
6 ;
Exercise : different meshes

1. Use different meshes provided in the website to compute the


convection-diffusion of particles in a stokes flow in different situation.
2. Change the value of the diffusivity to observe different results. Be sure you
computation are stable using more grid points or the Characterisitic
method.

Figure : Aneurysm Figure : Bifurcation Figure : Stenosis

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