FreeFEM Documentation on GitHub

stars - forks

# Learning by Examples

The FreeFEM language is typed, polymorphic and reentrant with macro generation.

Every variable must be typed and declared in a statement, that is separated from the next by a semicolon ;.

The FreeFEM language is a C++ idiom with something that is more akin to LaTeX.

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 matrix instead of problem).

The Development Cycle: Edit–Run/Visualize–Revise

Many examples and tutorials are given there after and in the examples section. It is better to study them and learn by example.

If you are a beginner in the finite element method, you may also have to read a book on variational formulations.

The development cycle includes the following steps:

Modeling: From strong forms of PDE to weak forms, one must know the variational formulation to use FreeFEM; one should also have an eye on the reusability of the variational formulation so as to keep the same internal matrices; a typical example is the time dependent heat equation with an implicit time scheme: the internal matrix can be factorized only once and FreeFEM can be taught to do so.

Programming: Write the code in FreeFEM language using a text editor such as the one provided in your integrated environment.

Run: Run the code (here written in file mycode.edp). That can also be done in terminal mode by :

1 FreeFem++ mycode.edp


Visualization: Use the keyword plot directly in mycode.edp to display functions while FreeFEM is running. Use the plot-parameter wait=1 to stop the program at each plot.

Debugging: A global variable debug (for example) can help as in wait=true to wait=false.

 1 bool debug = true;
2
3 border a(t=0, 2.*pi){x=cos(t); y=sin(t); label=1;};
4 border b(t=0, 2.*pi){x=0.8+0.3*cos(t); y=0.3*sin(t); label=2;};
5
6 plot(a(50) + b(-30), wait=debug); //plot the borders to see the intersection
7 //so change (0.8 in 0.3 in b)
8 //if debug == true, press Enter to continue
9
10 mesh Th = buildmesh(a(50) + b(-30));
11 plot(Th, wait=debug); //plot Th then press Enter
12
13 fespace Vh(Th,P2);
14 Vh f = sin(pi*x)*cos(pi*y);
15 Vh g = sin(pi*x + cos(pi*y));
16
17 plot(f, wait=debug); //plot the function f
18 plot(g, wait=debug); //plot the function g


Changing debug to false will make the plots flow continuously. Watching the flow of graphs on the screen (while drinking coffee) can then become a pleasant experience.

Error management

Error messages are displayed in the console window. They are not always very explicit because of the template structure of the C++ code (we did our best!). Nevertheless they are displayed at the right place. For example, if you forget parenthesis as in:

1 bool debug = true;
2 mesh Th = square(10,10;
3 plot(Th);


then you will get the following message from FreeFEM:

 1 2 : mesh Th = square(10,10;
2 Error line number 2, in file bb.edp, before  token ;
3 parse error
4 current line = 2
5 syntax error
6 current line = 2
7 Compile error : syntax error
8 line number :2, ;
9 error Compile error : syntax error
10 line number :2, ;
11 code = 1 mpirank: 0


If you use the same symbol twice as in:

1 real aaa = 1;
2 real aaa;


then you will get the message:

1 2 : real aaa; The identifier aaa exists
2       the existing type is <Pd>
3       the new  type is <Pd>


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, just as you would do in C++.

The following example works:

1 ...
2 fespace Vh(Th, P1);
3 Vh u;
4 cout << u;
5 matrix A = a(Vh, Vh);
6 cout << A;


Another trick is to comment in and out by using // as in C++. For example:

1 real aaa =1;
2 // real aaa;

Table of content