Sie sind auf Seite 1von 34

FreeFem++

LSR, RB, Dic 2013

Outline
Introduction and history
Instructions
Structure of simple programs
Mesh generation
Definition of FE space
Definition of variational form of the problem
Solution
Result analysis
Examples
LSR, RB, Dic 2013

Intro (I)
FreeFem++ is the implementation of a language dedicated
to the Finite Element Method.
It enables to solve PDE relatively easily.
It is a free, open-source software package for 2-D / 3-D
finite element computations.
Platforms: Linux, Windows, MacOS X.
Written in C++, and much of the syntax is similar to that of
C++.
Authors: F. Hecht, O. Pironneau, A. Le Hyaric (Universite
Pierre et Marie Curie, Laboratoire Jacques-Louis Lions).

LSR, RB, Dic 2013

Authors and contributors


F. Hecht professor at LJLL.
O. Pironneau professor of numerical analysis at the
university of Paris VI and at LJLL.
A. Le Hyaric research engineer from CNRS at LJLL.
J. Maurice - Post-Doct at LJLL.
K. Ohtsuka - professor at the Hiroshima Kokusai Gakuin
Univerisity, Japan. Expert in fracture dynamics, modelling
and computing.

LSR, RB, Dic 2013

Intro (II)
The project evolved from MacFem, Pcfem, written in Pascal,
to C (FreeFem version 3.4), which already offered mesh
adaptation on a single mesh. A thorough rewriting in C++
led to FreeFem++.
FreeFem++ includes:
Mesh generation and input
A wide range of finite elements and the ability to add new
elements
A number of integrated linear solvers, including CG,
GMRES, UMFPACK
Visualization tools
LSR, RB, Dic 2013

Instructions
; at line end is always required
No ; means instruction continues in the following line
Comments:
//From now on, only this line is commented
/*
All what is written here is commented,
also in different lines
*/

LSR, RB, Dic 2013

Instructions: variables declaration


Integers: int nnn=10;
Reals: real T0=300.0;
real time=0.0, Tmin=T0/2.0;
Vectors: int[int] mmm (10); //Vector of 10 integer elements
real[int] rrr (10); //Vector of 10 real elements
rrr[0]=1.0; //Vector indexing starts from 0!
rrr[1]=2.0; ...
FE space variables (1 value for each element, strictly correlated
to the mesh): Vh TTT, TTT0=T0;
LSR, RB, Dic 2013

Instructions: if statement
if (CONDITION1)
{BLOCK of calculations;}
else if (CONDITION2)
{BLOCK of calculations;}
else
{BLOCK of calculations;}

Boolean conditions:
(CONDITION1 && CONDITION2)
(CONDITION1 || CONDITION2)

LSR, RB, Dic 2013

Instructions: loops
For loops:
for (INITIALIZATION; CONDITION; CHANGE)
{ BLOCK of calculations;}

While loops:
while (CONDITION)
{ BLOCK of calculations or change of control variables;}

LSR, RB, Dic 2013

Instructions: input
To read from the screen:
cin>>variableName;
To read from a file
{
ifstream infile(''filename.dat''); //also .txt format is ok
infile>>variableName; //variableName[] if it is a FE space
variable
}

LSR, RB, Dic 2013

10

Instructions: output
To print to the screen:
cout<<Text=<<variableName<<endl;
To write to a file
{
ofstream outfile(''filename.dat''); //also .txt format is ok
outfile<<variableName1<< <<variableName2<<endl;
//variableName[] if it is a FE space variable
}
Use ofstream outfile(''filename'',append); if you want to add
lines to an already open file, e.g. inside a loop.
LSR, RB, Dic 2013

11

Instructions: plots
To produce 2D plots:
plot(TTT, fill=true, value=true, wait=true, eps=figname.eps");

TTT: FE space variables to be plotted (also the mesh name is


valid)
fill: to fill the area with color (if true, or 1) or plot only isolines
(if false, or 0); only isolines are more accurate.
value: to see also the automatic legend (if true)
wait: to wait for click or enter before proceeding with the
program execution, after producing the plot
eps: to save the plot in .eps format
LSR, RB, Dic 2013

12

Instructions: compute integrals


1D integrals:
length=int1d(meshname,bordername)(1.0);
flux=int1d(meshname,bordername)((T-Tamb)*h);

2D integrals:
area=int2d(meshname)(1.0);
Tave=int2d(meshname)(T)/area;
RelErr= sqrt(int2d(meshname)((T-Told)^2))/
sqrt(int2d(meshname)(T^2));

LSR, RB, Dic 2013

13

Instructions: region definition


int region1=meshname(xin,yin).region;
int region2=meshname(xin,yin).region;
fespace Vh(meshname,P1);
Vh kappa;

kappa=k1*(region==region1)+k2*(region== region2);
//boolean instruction
plot(kappa, wait=1, value=1, fill=1); //plot to check

LSR, RB, Dic 2013

14

FreeFem++ vs. FreeFem++-cs


FreeFem++-cs (gui) brings
the following extra
features:
Script writing
Plot area
Graphical shell:
area
(FreeFem++ is a
command-line program).
Inline documentation
(manual, examples,
reference card), very useful
from a new-user point of
Command line (error reporting, print on screen)
view.
Available options explicitly
listed in standard menus.
LSR, RB, Dic 2013

15

FreeFem++-cs
Bold black: all standard FreeFem++ keywords.
Blue: user-defined objects. User defined types are in bold face.
Character strings are in dark blue.
Green: macros and include files.
Bold red: spelling mistakes, syntax errors, undefined objects,
macros and include files containing errors.
Purple: comments.

Once you have a coherent program, you can run it just by clicking
on a button (no need to save it) and if FreeFem++ produces an
error the problematic line will be highlighted.
FreeFem++ script file extension: .edp
LSR, RB, Dic 2013

16

Notes
Any version of FreeFem++ does not crash if you do not use
bad instructions
Read carefully the error reporting printed in the command line!!!
The location and type of error is always correctly reported, for
common errors
FreeFem++ can also be run in background mode (no -cs interface)

LSR, RB, Dic 2013

17

Structure of FreeFem++
simple programs
1. Build a mesh
2. Declare the finite element space and test and trial
functions from that space
3. Write the variational forms / inner products involved in
the problem and construct the problem statement
4. Solve the problem
5. Analyze the results (plots, error calculations, etc.)
Lets try to build, step-by-step, the simple program
corresponding to the solution of Poisson equation on a simple
domain
LSR, RB, Dic 2013

18

Lets start!

LSR, RB, Dic 2013

19

1. Build the mesh


To build a square mesh on
use:

we can simply

int n=10;
mesh Th=square(n,n);
Or (more flexible):
int n=10, m=10;
real x0=0.0, x1=1.0;
real y0=0.0, y1=1.0;
mesh Th=square(n, m, [x0+(x1x0)x, y0+(y1y0)y]);

LSR, RB, Dic 2013

20

1. Build the mesh:


more mesh generation

Triangulation of W is automatically generated using 20 points on


border a
The domain is assumed to be on the left side of the boundary
which is implicitly oriented by the parametrization
Check plotting the borders only and looking at the arrows direction
LSR, RB, Dic 2013

21

1. Build the mesh:


Complex domains & refinements
Holes can be added in the computational domain

!
Refinement of the mesh is done by increasing the number
of points on the boundary (inner vertices are determined
by the density of the points on boundaries)
Mesh adaptation adaptmesh
LSR, RB, Dic 2013

22

1. Build the mesh:


labels
The keyword label can be added to define a group of
boundaries for later use (boundary conditions for instance)

Boundaries can be referred to by name or by label (or by


internal progressive number)
LSR, RB, Dic 2013

23

2. Define FE space (I)


We will use P1 elements for uh and vh:
Defines Vh to be the space
of continuous functions
which are affine in x, y on
each triangle Th

Now functions can be declared in Vh

LSR, RB, Dic 2013

24

2. Define FE space (II)

LSR, RB, Dic 2013

25

3. Write variational form


and problem statement

The variational problem can be easily written as:

BC (no BCadiabatic)
LSR, RB, Dic 2013

26

4. Solve the problem


Solve a problem just calling it by name (insert it into a
loop to proceed with time if needed for transient problems)

5. Analyze results: plot solution

LSR, RB, Dic 2013

27

5. Analyze results:
Mesh refinement
N=10

N=100

N=1000

LSR, RB, Dic 2013

28

Example 1

LSR, RB, Dic 2013

29

Example 2

LSR, RB, Dic 2013

30

Example 3

LSR, RB, Dic 2013

31

Example 4

LSR, RB, Dic 2013

32

Example 5

LSR, RB, Dic 2013

33

Additional infos
Softare official website (for free download):
http://www.freefem.org/
http://www.ljll.math.upmc.fr/lehyaric/ffcs/install.php (for
the GUI installation)
Download the full user manual from:
http://www.freefem.org/ff++/ftp/freefem++doc.pdf
Find the full user manual in the FreeFem++ installation
directory:
MainDisk:\FreeFem++\freefem++doc.pdf
LSR, RB, Dic 2013

34