To illustrate with an example, let us explain how FreeFem++ solves the Poisson’s equation: for a given function f(x,y), find a function u(x,y) satisfying
Here ∂Ω is the boundary of the bounded open set Ω ⊂The following is a FreeFem++ program which computes u when f(x,y) = xy and Ω is the unit disk. The boundary C = ∂Ω is
Note that in FreeFem++ the domain Ω is assumed to described by its boundary that is on the left side of its boundary oriented by the parameter. As illustrated in Fig. 2.2, we can see the isovalue of u by using plot (see line 13 below).
Note that the qualifier solver=LU is not required and by default a multi-frontal LU would have been used. Note also that the lines containing clock are equally not required. Finally note how close to the mathematics FreeFem++ input language is. Line 8 and 9 correspond to the mathematical variational equation
for all v which are in the finite element space Vh and zero on the boundary C.
Exercise : Change P1 into P2 and run the program.
This first example shows how FreeFem++ executes with no effort all the usual steps required by the
finite element method (FEM). Let us go through them one by one.
1st line: the boundary Γ is described analytically by a parametric equation for x and for y. When
Γ = ∑
j=0JΓ
j then each curve Γj, must be specified and crossings of Γj are not allowed except at end points
.
The keyword “label” can be added to define a group of boundaries for later use (boundary conditions for instance). Hence the circle could also have been described as two half circle with the same label:
Boundaries can be referred to either by name ( Gamma1 for example) or by label ( C here) or even by its
internal number here 1 for the first half circle and 2 for the second (more examples are in Section 5.8).
2nd line: the triangulation h of Ω is automatically generated by buildmesh(C(50)) using 50 points
on C as in Fig. 2.1.
The domain is assumed to be on the left side of the boundary which is implicitly oriented by the parametrization. So an elliptic hole can be added by
If by mistake one had written
then the inside of the ellipse would be triangulated as well as the outside.
Automatic mesh generation is based on the Delaunay-Voronoi algorithm. Refinement of the mesh are done by increasing the number of points on Γ, for example, buildmesh(C(100)), because inner vertices are determined by the density of points on the boundary. Mesh adaptation can be performed also against a given function f by calling adaptmesh(Th,f).
Now the name h (Th in FreeFem++ ) refers to the family {Tk}k=1,
,nt of triangles shown in figure 2.1.
Traditionally h refers to the mesh size, nt to the number of triangles in
h and nv to the number of
vertices, but it is seldom that we will have to use them explicitly. If Ω is not a polygonal domain, a “skin”
remains between the exact domain Ω and its approximation Ωh = ∪k=1ntT
k. However, we notice that all
corners of Γh = ∂Ωh are on Γ.
3rd line: A finite element space is, usually, a space of polynomial functions on elements, triangles here
only, with certain matching properties at edges, vertices etc. Here fespace Vh(Th,P1) defines
Vh to be the space of continuous functions which are affine in x,y on each triangle of Th.
As it is a linear vector space of finite dimension, basis can be found. The canonical basis is
made of functions, called the hat functions ϕk which are continuous piecewise affine and are
equal to 1 on one vertex and 0 on all others. A typical hat function is shown on figure 2.4
1.
Then
![]() | (2.3) |
where M is the dimension of Vh, i.e. the number of vertices. The wk are called the degree of freedom of w and M the number of the degree of freedom.
It is said also that the nodes of this finite element method are the vertices.
Currently FreeFem++ implements the following elements , (see section 6 for the full description)
P0 piecewise constant,
P1 continuous piecewise linear,
P2 continuous piecewise quadratic,
RT0 Raviart-Thomas piecewise constant,
P1nc piecewise linear non-conforming,
P1dc piecewise linear discontinuous,
P2dc piecewise quadratic discontinuous,
P1b piecewise linear continuous plus bubble,
P2b piecewise quadratic continuous plus bubble.
...
To get the full list do in a unix terminal, in directory examples++-tutorial do
The user can add other elements fairly easily if required.
Step3: Setting the problem
4th line: Vh u,v declares that u and v are approximated as above, namely
![]() | (2.4) |
5th line: the right hand side f is defined analytically using the keyword func.
7th–9th lines: defines the bilinear form of equation (2.1) and its Dirichlet boundary conditions (2.2).
This variational formulation is derived by multiplying (2.1) by v(x,y) and integrating the result over
Ω:
Vh u,v; solve Poisson(u,v) =
and (2.5) is written with dx(u) = ∂u∕∂x, dy(u) = ∂u∕∂y and
The other way is to define the problem and then we solve it, write :
Step4: Solution and visualization
6th line: The current time in seconds is stored into the real-valued variable cpu.
7th line The problem is solved.
11th line: The visualization is done as illustrated in Fig. 2.2 (see Section 7.1 for zoom, postscript and other commands).
12th line: The computing time (not counting graphics) is written on the console Notice the C++-like
syntax; the user needs not study C++ for using FreeFem++ , but it helps to guess what is allowed in the
language.
Access to matrices and vectors
Internally FreeFem++ will solve a linear system of the type
The matrix A = (Aij) is called stiffness matrix .
If the user wants to access A directly he can do so by using (see section 6.10 page 282 for details)
The vector F in (2.7) can also be constructed manually
The problem can then be solved at the level of algebra by
Note 2.1 Here uand Fare finite element function, and u[]and F[]give the array of value associated ( u[]≡ (ui)i=0,…,M-1 and F[]≡ (Fi)i=0,…,M-1). So we have
The linear system (2.7) is solved by UMFPACK unless another option is mentioned specifically as in
Vh u,v; problem Poisson(u,v,solver=CG) = int2d(...
meaning that Poisson is declared only here and when it is called (by simply writing Poisson; ) then (2.7) will be solved by the Conjugate Gradient method.
The language of FreeFem++ is typed, polymorphic and reentrant with macro generation (see 9.12). Every variable must be typed and declared in a statement each statement separated from the next by a semicolon ”;”. The syntax is that of C++ by default augmented with something that is more akin to TEX. For the specialist, one key guideline is that FreeFem++ rarely generates an internal finite element array; this was adopted for speed and consequently FreeFem++ could be hard to beat in terms of execution speed, except for the time lost in the interpretation of the language (which can be reduced by a systematic usage of varf and matrices instead of problem.
An integrated environment is provided with FreeFem++ written by A. Le Hyaric; Many examples and tutorials are also given along with this documentation and it is best to study them and learn by example. Explanations for some of these examples are given in this book in the next chapter. If you are a FEM beginner, you also may want to read a book on variational formulations.
The development cycle will have the following steps:
% FreeFem++ mycode.edp
Note, the name of the command FreeFem++ may depend on your installation.
Changing debug to false will make the plots flow continuously; drinking coffee and watching the flow of graph on the screen can then become a pleasant experience.
Error messages are displayed in the console window. They are not always very explicit because of the template structure of the C++ code, sorry! Nevertheless they are displayed at the right place. For example, if you forget parenthesis as in
then you will get the following message from FreeFem++,
If you use the same symbol twice as in
then you will get the message
If you find that the program isn’t doing what you want you may also use cout to display in text format on the console window the value of variables.
The following example works:
Another trick is to comment in and out by using the“ //” as in C++. For example