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 :

1FreeFem++ 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.

 1bool debug = true;
2
3border a(t=0, 2.*pi){x=cos(t); y=sin(t); label=1;};
4border b(t=0, 2.*pi){x=0.8+0.3*cos(t); y=0.3*sin(t); label=2;};
5
6plot(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
10mesh Th = buildmesh(a(50) + b(-30));
11plot(Th, wait=debug); //plot Th then press Enter
12
13fespace Vh(Th,P2);
14Vh f = sin(pi*x)*cos(pi*y);
15Vh g = sin(pi*x + cos(pi*y));
16
17plot(f, wait=debug); //plot the function f
18plot(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:

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


then you will get the following message from FreeFEM:

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


If you use the same symbol twice as in:

1real aaa = 1;
2real aaa;


then you will get the message:

12 : 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...
2fespace Vh(Th, P1);
3Vh u;
4cout << u;
5matrix A = a(Vh, Vh);
6cout << A;


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

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

Table of content