BE Getfem Laplacian Py
BE Getfem Laplacian Py
Applied Mathematics
A. Ammar, F. Deluzet, A. Estana, M. Fournié, N. Renon
1 Problem
The goal of the work is to implement the approximation by finite element method of the Laplacian
equation with source term f given on a square domain Ω = [0, 1] × [0, 1] with Dirichlet boundary condition :
−∆u = f, (x, y) ∈ Ω,
u = g, (x, y) ∈ ∂Ω.
2 Introduction to GetFEM++
GetFEM++ is basically a generic C++ finite element library which aims to offer the widest range of
finite element methods and elementary matrix computations for the approximation of linear or non-linear
problems, possibly in hybrid form and possibly coupled. The dimension of the problem is arbitrary and
may be a parameter of the problem. GetFEM++ offers a description of models in the form of bricks whose
objective is to enable reusability of the approximations made. The system of bricks, is used to assemble
components such as standard models (elasticity in small and large deformations, Helmholtz problem, scalar
elliptic problem ...), components representing the boundary conditions (Neumann, Dirichlet, Fourier-Robin,
contact, friction...), also components representing constraints (incompressibility, removing rigid motions ...)
and coupling components for coupled models. A documentation of the library can be found to the address
http ://getfem.org/userdoc/index.html
GetFEM++ proposed three interfaces (with Scilab, Matlab and Python) that allow to use of the main
features of the software without the need of C++ programming and allowing graphical post-processing. For
that work, we are going to use the Python interface. The documentation can be found to the address
http ://getfem.org/python/index.html
GetFEM++ has only a (very) experimental meshing procedure (and produces regular meshes). It is
therefore often necessary to import meshes. The formats currently supported are Gid, Gmsh and Emc2. For
that work, we consider simple domain then the generation of regular meshes is trivial under the library,
however we are going to use the most popular free software Gmsh.
System environment.
We suggest to use Linux system. Then you have to open a Terminal to manage everything. To move into
your workspace you have the commands ls, cd and so on. Getfem library, Pyhton modules (numpy), Gmsh
and Paraview softwares necessary to do the BE are already installed on your system.
At school, all variables environments can be load with the commands (in a terminal) :
module load anaconda3/2023
conda init
open a new terminal
module load anaconda3/2023
module load getfem/5.4.2-mumps
module load gmsh/4.10.0
Perhaps there are slight differences in the call for example module load getfem/5.4.2 instead of
module load getfem, ... use tabulation key on the keyboard to obtain the list of available modules.
The python interface is available via a python module getfem.py. In order to use the interface you have
to load it (under python) with import getfem
To use spyder, the editor must be called after loading the variable environment in the same terminal.
• First issue :
It is possible to create simple structured meshes or unstructured meshes for simple geometries
(see getfem.Mesh(’generate’, mesher-object mo, scalar h)). For this example, we just consider some
nodes associated to a regular mesh whose nodes are {xi=0,···10,j=0··· ,10 } :
Next step corresponds to save the mesh using the File Menu. If the script is called square.geo you
must recover on the disk the corresponding mesh called square.msh. Becarful, a simple clic into the left
menu on Save can create a good mesh but Getfem can have some trouble to use it. To be more efficient, you
can use Export, you select the format *.msh, you specify the name of the output with the good extension
.msh and Save. The last pop-up Menu asks you which format-version you want. Select Version 2 ASCII
and save to create on the disk the mesh square.msh (see the following figures)
An other
issue corresponds to uncomment the three lines to create directly on the disk the mesh with the good name
#Mesh.Algorithm = 6;
#Mesh 2;
#Save "square.msh";
Finally, in the Terminal (where Python turns with Getfem import) you can use the following command
to import the mesh using
m=gf.Mesh(’import’,’gmsh’,’square.msh’)
Notice that in the construction of the mesh you have given 4 flags 10-20-30-40 to identify the left-down-
right-up boundary parts. These integers of your choice into the (geo-file) will be used in the assembling step.
You will have to use exactly the same integer for a given location.
The first instruction builds a new MeshFem object, the second argument specifies that this object will
be used to interpolate scalar fields (since the unknown u is a scalar field). The second instruction assigns
the P2 FEM to every convex (each basis function is a polynomial of degree 2). As P2 is a polynomial FEM,
you can view the expression of its basis functions on the reference convex :
The integration method will be used to compute the various integrals on each element : here we choose
to perform exact computations (no quadrature formula), which is possible since the geometric transfor-
mation of these convexes from the reference convex is linear (this is true for all simplexes, and this is
also true for the parallelepipeds of our regular mesh, but it is not true for general quadrangles), and the
chosen FEM is polynomial. Hence it is possible to analytically integrate every basis function/product of
basis functions/gradients/etc. There are many alternative FEM methods and integration methods (see User
Documentation).
Note however that in the general case, approximate integration methods are a better choice than exact
integration methods.
• If you have used Gmsh then this step is already done during the construction of the mesh. In our
example, you have given 4 flags with the integers 10, 20, 30, 40 that play the same rule as 42 in
previous presentation (42 is used for all the boundary contrary to the present case where we split the
boundary into 4 parts).
Let us build a problem with an easy exact solution. To construct such problem, we can choice u(x, y) =
x(x − 1) − y(y − 1) and compute ∆u. We verify that −∆u = 0. So, to have a PDE with that solution, we
can consider the following PDE (that we are going to solve numerically) :
−∆u = 0 on Ω = [0; 1] × [0; 1],
(of course you know the corresponding exact solution u(x, y) = x(x − 1) − y(y − 1)).
Now, we are going to use step by step the finite element library. We start with an empty real model
(a model is either ’real’ or ’complex’). And we declare that u is an unknown of the system on the finite
element method mf by
Now, we add a generic elliptic brick, which handles −∇ · (A : ∇u) = . . . problems, where A can be a
scalar field, a matrix field, or an order 4 tensor field. By default, A = 1. Then there are at least two manner
to add it on our main variable u with
or instead of this command we can use the more interesting one directly inspired by the weak
formulation, see the Documentation for more details
md.add_linear_term(mim, ’Grad_u:Grad_Test_u’)
Next we add a Dirichlet condition on the domain boundary
These lines define a data of the model which represents the value of the Dirichlet condition.
These lines add a Dirichlet condition to the variable u on the boundary number 42 (or on the bondaries
identify by the numbers 1, 2, 3, 4). The Dirichlet condition is imposed with Lagrange multipliers. A MeshFem
argument is also required, as the Dirichlet condition u = g is imposed in a weak form
Z Z
u(x)v(x) = g(x)v(x) ∀v
Γ Γ
The source term is equal to 0 in our example, so it is not necessary to uncomment these commands.
Then export solution under a specific format. The most popular in the VTK format. The output file
can be load using Paraview software.
# export computed solution using vtk format (you can load it with Paraview)
mf.export_to_vtk(’u.vtk’,u,’Computed solution’)
# export computed solution using gmsh format (you can load it with Gmsh)
mf.export_to_pos(’u.pos’,u,’Computed solution’)
4 TODO for the BE
The goal is to understand all commands used to construct the mesh (*.geo) and to implement the finite
element method under Pytrhon using Getfem Library.
You don’t have to give a report and no mark will be associated to the BE on Getfem
(BE1 and BE2). However, if you arrive to understand the manipulations and results that you
obtain, it is a good training for the final classical exam. Making tests can give you the ans-
wers to questions that you have. Don’t hesitate to discuss with the other students and teachers.
• Solve the same PDE on the same domain (square) with Dirichlet boundary conditions
# Errors
L2error = gf.compute(mf, u-g, ’L2 norm’, mim)
H1error = gf.compute(mf, u-g, ’H1 norm’, mim)
print ’Error in L2 norm : ’, L2error
print ’Error in H1 norm : ’, H1error
# export data into vtk-format and view with external Paraview
mfu.export_to_vtk(’sol.vtk’, g,’Exact solution’,u,’Computed solution’)
# or export data into gmsh-format and view with external gmsh
mfu.export_to_pos(’sol.pos’, g,’Exact solution’,u,’Computed solution’)
Make some tests for different meshes where the size of the mesh h tends to zero (use the most interesting
mesher). Plot on a same figure the errors according to h in log-log representation. What do you observe ? Is
it in accordance with the results given in course (see slides) ?
• Change the finite element for example use P4 and observe the number of unknowns that you have
comparing to the P1 or P2 cases.
• Compare the accuracy of the solution for a given mesh using 2 different families of finite element P1
and P2 for example (keep the same mesh).
• Instead of triangular mesh create a quadrangular mesh using the syntax ’FEM_QK(2,2)’ under Get-
fem or create a quadrangular mesh under gmsh before import. For the same number of nodes between a
triangular mesh and a quadrangular mesh coupled with P1 or Q1 , what is the most accurate ?
• If you impose Dirichlet boundary conditions with multiplier or with penalization, which is the most
accurate ?