Dolfin User Manual
Dolfin User Manual
www.fenics.org
Visit http://www.fenics.org/ for the latest version of this manual.
Send comments and suggestions to [email protected].
Contents
1 Introduction 13
1.3 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2 Quickstart 15
3
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
3 Linear algebra 25
3.4 Preconditioning . . . . . . . . . . . . . . . . . . . . . . . . . . 27
4 The mesh 29
5 Functions 33
5.1.1 Representation . . . . . . . . . . . . . . . . . . . . . . 34
5.1.2 Evaluation . . . . . . . . . . . . . . . . . . . . . . . . . 34
5.1.3 Assignment . . . . . . . . . . . . . . . . . . . . . . . . 35
5.1.5 Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
4
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
8 Nonlinear solver 53
5
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
9 Input/output 57
9.2.2 VTK . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
9.2.3 OpenDX . . . . . . . . . . . . . . . . . . . . . . . . . . 61
9.2.5 MATLAB . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.2.6 Tecplot . . . . . . . . . . . . . . . . . . . . . . . . . . 62
9.2.7 GiD . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63
6
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
12 Solvers 77
12.1.1 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
12.1.2 Performance . . . . . . . . . . . . . . . . . . . . . . . . 79
12.1.3 Limitations . . . . . . . . . . . . . . . . . . . . . . . . 79
12.2 Convection–diffusion . . . . . . . . . . . . . . . . . . . . . . . 80
12.2.1 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
12.2.2 Performance . . . . . . . . . . . . . . . . . . . . . . . . 81
12.2.3 Limitations . . . . . . . . . . . . . . . . . . . . . . . . 81
12.3.1 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
12.3.2 Performance . . . . . . . . . . . . . . . . . . . . . . . . 82
7
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
12.3.3 Limitations . . . . . . . . . . . . . . . . . . . . . . . . 82
12.4 Elasticity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
12.4.1 Usage . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
12.4.2 Performance . . . . . . . . . . . . . . . . . . . . . . . . 83
12.4.3 Limitations . . . . . . . . . . . . . . . . . . . . . . . . 83
A Reference elements 87
B Installation 97
8
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
D License 109
9
About this manual
Intended audience
This manual is written both for the beginning and the advanced user. There
is also some useful information for developers. More advanced topics are
treated at the end of the manual or in the appendix.
Typographic conventions
# ./configure
# make
Commands are written in the dialect of the bash shell. For other shells,
such as tcsh, appropriate translations may be needed.
11
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Contact
12
Chapter 1
Introduction
1.3 Overview
13
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
14
Chapter 2
Quickstart
This chapter demonstrates how to get started with DOLFIN, including down-
loading and installing the latest version of DOLFIN, and solving Poisson’s
equation. These topics are discussed in more detail elsewhere in this manual.
In particular, see Appendix B for detailed installation instructions and Chap-
ter 7 for a detailed discussion of how to solve partial differential equations
with DOLFIN.
The latest version of DOLFIN can be found on the FEniCS web page:
http://www.fenics.org/
15
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
# ./configure
# make
# make install
Make sure that you download the latest release (which is not 0.1.0).
Note that you may need to be root on your system to perform the last
step. Since DOLFIN depends on a number of other packages, including the
linear algebra package PETSc and the form compiler FFC, you may also need
download and install these packages before you can compile DOLFIN. (See
Appendix B for detailed instructions.)
Let’s say that we want to solve Poisson’s equation on the unit square Ω =
(0, 1)×(0, 1) with homogeneous Dirichlet boundary conditions on the bound-
ary Γ0 = {(x, y) ∈ ∂Ω : x = 1}, homogeneous Neumann boundary condi-
tions on the remaining part of the boundary and right-hand side given by
f = x sin(y),
with (V̂h , Vh ) a pair of suitable function spaces (the test and trial spaces).
The bilinear form a : V̂h × Vh → R is given by
Z
a(v, U ) = ∇v · ∇U dx (2.5)
Ω
16
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
v = BasisFunction(element)
U = BasisFunction(element)
f = Function(element)
a = dot(grad(v), grad(U))*dx
L = v*f*dx
The example is given here for piecewise linear finite elements in two dimen-
sions, but other choices are available, including arbitrary order Lagrange
elements in two and three dimensions.
To compile the pair of forms (a, L), now call the form compiler on the
command-line as follows:
# ffc Poisson.form
This generates the file Poisson.h which implements the forms in C++ for
inclusion in your DOLFIN program.
17
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Having compiled the variational problem (2.4) with FFC, it is now easy to
implement a solver for Poisson’s equation. We first discuss the implementa-
tion line by line and then present the complete program. The source code
for this example is available in the directory src/demo/pde/poisson/ of the
DOLFIN source tree.
At the beginning of our C++ program, which we write in a text file named
main.cpp, we must first include the header file dolfin.h, which gives our
program access to the DOLFIN class library. In addition, we include the
header file Poisson.h generated by the form compiler. Since all classes in
the DOLFIN class library are defined within the namespace dolfin, we also
specify that we want to work within this namespace:
#include <dolfin.h>
#include ‘‘Poisson.h’’
int main()
{
// Write your program here
return 0;
}
18
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
19
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Next, we initialize the pair of bilinear and linear forms that we have previ-
ously compiled with FFC:
Poisson::BilinearForm a;
Poisson::LinearForm L(f);
We may now define a PDE from the pair of forms, the mesh and the boundary
conditions:
To solve the PDE, we now just need to call the solve function as follows:
Function U = pde.solve();
File file(‘‘poisson.pvd’’);
file << U;
#include <dolfin.h>
#include "Poisson.h"
int main()
{
// Right-hand side
20
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
// Boundary condition
class : public BoundaryCondition
{
void eval(BoundaryValue& value, const Point& p, unsigned int i)
{
if ( std::abs(p.x - 1.0) < DOLFIN_EPS )
value = 0.0;
}
} bc;
// Set up problem
UnitSquare mesh(16, 16);
Poisson::BilinearForm a;
Poisson::LinearForm L(f);
PDE pde(a, L, mesh, bc);
// Compute solution
Function U = pde.solve();
return 0;
}
The easiest way to compile the program is to create a Makefile that tells
the standard Unix command make how to build the program. The following
21
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
DEST = dolfin-poisson
OBJECTS = main.o
all: $(DEST)
install:
clean:
-rm -f *.o core *.core $(OBJECTS) $(DEST)
$(DEST): $(OBJECTS)
$(LINK) -o $@ $(OBJECTS) $(CFLAGS) $(LIBS)
.cpp.o:
$(CXX) $(CFLAGS) -c $<
With the Makefile in place, we just need to type make to compile the pro-
gram, generating the executable as the file dolfin-poisson.
# ./dolfin-poisson
Computing mesh connectivity:
Found 289 vertices
Found 512 cells
[...]
Krylov solver (gmres, ilu) converged in 21 iterations.
Saved function [...] to file poisson.pvd in VTK format.
22
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
23
Chapter 3
Linear algebra
DOLFIN does not have its own linear algebra routines, but instead uses the
external package PETSc [11] for linear algebra functionality.
PETSc is a suite of data structures and routines for the scalable (parallel)
solution of scientific applications modeled by partial differential equations.
It employs the MPI standard for all message-passing communication.
For convenience DOLFIN provides wrappers for some of the most common
linear algebra functionality. For more advanced usage, DOLFIN provides
direct access to the PETSc pointers to be used with the standard PETSc
interface.
The matrix class Matrix provides wrappers for initializing a sequential sparse
matrix, or a sequential sparse matrix in block compressed row format. For
parallel matrices the PETSc interface has to be used directly.
The code for initializing a sequential sparse M × N matrix takes the form:
25
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
uint M = 100;
uint N = 100;
Matrix A(M,N);
Vector b(M);
Further, wrappers for some basic linear algebra functionality, such as matrix
vector multiplication, norms etc., are provided with an intentionally simple
interface, for example, matrix vector multiplication is defined by:
Vector Ax;
A.mult(b,Ax);
For more advanced operations, a pointer to the PETSc matrix and vector is
accessed by mat() and vec() respectively.
The class VirtualMatrix enables the use of Krylov subspace methods for
linear systems Ax = b, without having to explicitly store the matrix A. All
that is needed is that the user-defined VirtualMatrix implements multi-
plication with vectors. Note that the multiplication operator needs to be
defined in terms of PETSc data structures (Vec), since it will be called from
PETSc.
26
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Vec x;
GMRES solver;
solver.solve(A,x,b);
The real components of the eigenvalues are returned in the vector er and
the complex components of the eigenvalues are returned in the vector ec.
The procedure for computing the eigenvalues of a matrix is computationally
intensive and should only be used for relatively small matrices.
3.4 Preconditioning
27
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
To change the default preconditioner in the DOLFIN GMRES solver, edit the
constructor of the GMRES class. For example, to choose the ILU preconditioner
for GMRES we write
PC pc;
KSPGetPC(ksp, &pc);
PCSetType(pc, PCILU);
PCSetType(pc, PCHYPRE );
PCHYPRESetType(pc,"boomeramg");
28
Chapter 4
The mesh
29
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
30
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
The following example illustrates how to iterate over the Cells of a Mesh to
mark some Cells for refinement, before refining the Mesh:
31
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
// Refine mesh
mesh.refine();
It is also possible to directly mark all Cells for refinement to refine the Mesh
uniformly:
32
Chapter 5
Functions
I Developer’s note: Since this chapter was written, the Function class has
seen a number of improvements which are not covered here. Chapter needs
to be updated.
33
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
5.1.1 Representation
5.1.2 Evaluation
Function f;
Mesh mesh;
Function f;
Mesh mesh;
34
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
5.1.3 Assignment
Function f;
Function g = f;
35
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Note that picking a component or sub function creates a new Function that
shares data with the original Function.
5.1.5 Output
File file(‘‘solution.pvd’’);
file << U;
36
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
X
N
v= v i φi , (5.1)
i=1
where {φi }Ni=1 is the global basis of the finite element space defined by the
Mesh and the FiniteElement, and the nodal values {vi }N i=1 are given by the
values of a Vector.
Note that a discrete Function may not be evaluated at arbitrary points (only
at each Vertex of a Mesh).
Vector x;
Function f(x);
If possible, DOLFIN will then automatically try to determine the Mesh and
the FiniteElement.
Vector x;
Mesh mesh;
37
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Vector x;
Mesh mesh;
FiniteElement element;
Vector& x = u.vector();
Mesh& mesh = u.mesh();
FiniteElement& element = u.element();
Vector x;
Mesh mesh;
FiniteElement element;
Function f(x);
f.attach(mesh);
f.attach(element);
38
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Vector x;
Mesh mesh;
Function f(x, mesh);
Poisson::BilinearForm a;
In this example, the Function f represents a function in the trial space for
the BilinearForm a.
39
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Source f;
Source() : Function(3) {}
Source f;
40
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Function f(source);
41
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
MeshSize h;
Note that the current Cell is only available during assembly and has no
meaning otherwise. It is thus not possible to write the Function h to file,
since the current Cell is not available when evaluating a Function at any
given Vertex. Furthermore, note that the current Cell is not available when
creating a Function from a function pointer.
42
Chapter 6
43
Chapter 7
45
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
where we employ tensor notation so that the double index i means summation
from i = 1, ..., d, and L(·) : V̂ → R is a linear form acting on the test space
V̂ , defined by Z
L(v) = vf dx. (7.4)
Ω
For this problem we typically use V = V̂ = H01 (Ω), with H01 (Ω) the standard
Sobolev space of square integrable functions with also their first derivatives
square integrable (in the Lebesgue sense), with the functions being zero on
the boundary (in the sense of traces).
Finite element basis functions in DOLFIN are defined using FIAT, which
supports the generation of arbitrary order Lagrange finite elements on lines,
triangles, and tetrahedra. Upcoming versions of FIAT will also support Her-
mite and nonconforming elements as well as H(div) and H(curl) elements
such as Raviart-Thomas and Nedelec.
46
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
In the language of FFC, with Vh = V̂h the space of piecewise linear Lagrange
finite elements on a tetrahedral mesh, (7.5) is defined as:
v = BasisFunction(element)
u = BasisFunction(element)
f = Function(element)
a = v.dx(i)*u.dx(i)*dx
L = v*f*dx
where *dx signifies integration over the domain Ω, and the finite element
space is constructed using FIAT. DOLFIN is not communicating directly
with FIAT, but only through FFC in the definition of the variational form in
the .form file.
# ffc Poisson.form
generates a file Poisson.h, containing classes for the bilinear form a(·, ·) and
the linear form L(·), and classes for the finite element spaces Vh and V̂h .
The element matrices and vectors for a given cell may be factored into two
tensors, with one tensor depending on the geometry of the cell, and the
47
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
For efficiency in the computation of the element matrices and vectors, FFC
precomputes the tensors that are independent of the geometry of a certain
cell.
The class FEM automates the assembly algorithm, constructing a linear system
of equations from a PDE, given in the form of a variational problem (7.2),
with a bilinear form a(·, ·) and a linear form L(·).
Poisson::BilinearForm a;
Poisson::LinearForm L(f);
Mesh mesh;
Mat A;
Vec b;
FEM::assemble(a,L,A,b,mesh);
In the assemble() function the element matrices and vectors are computed
by calling the function eval() in the classes Bilinearform and Linearform.
The eval() functions at a certain element in the assembly algorithm take as
argument an AffineMap object, describing the mapping from the reference
element to the actual element, by computing the Jacobian J of the mapping
(also J −1 and det(J) are computed).
48
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
The boundary conditions are specified by defining a new subclass of the class
BoundaryCondition, which we here will name MyBC:
MyBC bc;
FEM::assemble(a,L,A,b,mesh,bc);
49
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
For example, the form file Heat.form for the heat equation discretized in
time with the implicit Euler method, takes the form:
a = v*U1*dx + k*v.dx(i)*U1.dx(i)*dx
L = v*U0*dx + v*f*dx
50
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Heat::BilinearForm a(k);
Heat::LinearForm L(U0, f, k);
51
Chapter 8
Nonlinear solver
F (x) = 0 (8.1)
To use the nonlinear solver, a nonlinear function must be defined. The non-
linear solver is then initialised with this function and a solution computed.
To solve a nonlinear problem, the user must defined a class which . The class
should be derived from the DOLFINclass NonlinearFunction. The class
should contain the necessary functions to form the function F (u) and the
Jacobian matrix J = ∂F/∂u. The precise form of the user defined class will
depend on the PDE being solved and the numerical method. The structu of
a user defined class MyNonlinearFunction is shown below.
53
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
// Constructor
MyNonlinearFunction() : NonlinearFunction(){}
// Compute F(u)
void F(Vector& b, const Vector& x)
{
// Insert F(u) into the vector b
}
// Compute J
void J(Matrix& A, const Vector& x)
{
// Insert the Jacobian into the matrix A
}
dolfin::uint size()
{
// Return the dimension of the Jacobian matrix
}
dolfin::uint nzsize()
{
// Return the maximum number of zeroes per row of J
}
private:
// Pointers to objects with which F(u) is defined
};
54
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
55
Chapter 9
Input/output
Thus, if file is a File and object is an object of some class that can be
written to file, then the object can be written to file as follows:
Similarly, if object is an object of a class that can be read from file, then
data can be read from file (overwriting any previous data held by the object)
57
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
as follows:
The format (type) of a file is determined by its filename suffix, if not otherwise
specified. Thus, the following code creates a File for reading/writing data
in DOLFIN XML format:
File file(‘‘data.xml’’);
A complete list of file formats and corresponding file name suffixes is given
in Table 9.1.
Alternatively, the format of a file may be explicitly defined. One may thus
create a file named data.xml for reading/writing data in GNU Octave for-
mat:
58
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
I Developer’s note: Some of the file formats are partly broken after changing
the linear algebra backend to PETSc. (Do grep FIXME in src/kernel/io/.)
Table 9.2: Matrix of supported combinations of classes and file formats for
input/output in DOLFIN.
In this section, we give some pointers to each of the file formats supported by
DOLFIN. For detailed information, we refer to the respective user manual
of each format/program.
DOLFIN XML is the native format of DOLFIN. As the name says, data
is stored in XML ASCII format. This has the advantage of being a robust
and human-readable format, and if the files are compressed there is little
overhead in terms of file size compared to a binary format.
59
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Mesh mesh;
File in(‘‘mesh.xml.gz’’);
in >> mesh;
File out(‘‘mesh.xml’’);
out << mesh;
on the command-line.
There is currently no visualization tool that can read DOLFIN XML files,
so the main purpose of this format is to save and transfer data.
9.2.2 VTK
Data saved in VTK format [13] can be visualized using various packages. The
powerful and freely available ParaView [10] is recommended. Alternatively,
VTK data can be visualized in MayaVi [5], which is recommended for quality
vector PostScript output. Time-dependent data is handled automatically in
the VTK format.
Function u;
File out(‘‘data.pvd’’);
out << u;
60
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
The sample code produces the file data.pvd, which can be read by Par-
aView. The file data.pvd contains a list of files which contain the results
computed by DOLFIN. For the above example, these files would be named
dataXXX.vtu, where XXX is a counter which is incremented each time the
function is saved. If the function u was to be saved three times, the files
data000000.vtu
data000001.vtu
data000002.vtu
9.2.3 OpenDX
GNU Octave [7] is a free clone of MATLAB that can be used to visualize
solutions computed in DOLFIN, using the commands pdemesh, pdesurf
61
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
and pdeplot. These commands are normally not part of GNU Octave but
are provided by DOLFIN in the subdirectory src/utils/octave/ of the
DOLFIN source tree. These commands require the external program ivview
included in the open source distribution of Open Inventor [8]. (Debian users
install the package inventor-clients.)
octave:1> poisson
9.2.5 MATLAB
Since MATLAB [4] is not free, users are encouraged to use GNU Octave
whenever possible. That said, data is visualized in much the same way
in MATLAB as in GNU Octave, using the MATLAB commands pdemesh,
pdesurf and pdeplot.
9.2.6 Tecplot
62
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
9.2.7 GiD
GiD [2] is a proprietary visualization tool. The GiD format is not actively
maintained and may be removed in future versions of DOLFIN (if there is
not sufficient interest to maintain the format).
DOLFIN supplies a script for easy conversion between different file formats.
The script is named dolfin-convert and can be found in the directory
src/utils/convert/ of the DOLFIN source tree. The only supported file
formats are currently the Medit .mesh format (which can be generated by
TetGen), the .gmsh format (generated by Gmsh) and the DOLFIN XML
mesh format:
With some effort, DOLFIN can be expanded with new file formats. Any con-
tributions are welcome. If you wish to contribute to DOLFIN, then adding
a new file format (or improving upon an existing file format) is a good place
to start. Take a look at one of the current formats in the subdirectory
src/kernel/io/ of the DOLFIN source tree to get a feeling for how to de-
sign the file format, or ask at [email protected] for directions.
63
Chapter 10
Log messages can be generated using the function dolfin info() available
in the dolfin namespace:
which works similarly to the standard C library function printf. The fol-
lowing examples illustrate the usage of dolfin info():
65
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Note the use of dolfin::cout and dolfin::endl from the dolfin names-
pace, corresponding to the standard standard std::cout and std::endl in
namespace std. If log messages are directed to standard output (see below),
then dolfin::cout and std::cout may be mixed freely.
Use with caution for large objects. For a Matrix, calling disp() will displays
all matrix entries.
66
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
dolfin_warning(message);
dolfin_error(message);
In addition to displaying the given string message, the macro dolfin error()
also displays information about the location of the code that generated the
error (file, function name and line number). Once an error is encountered,
the program is stopped.
Note that in order to pass formatting strings and additional arguments to
warnings or errors, the variations dolfin error1(), dolfin error2() and
so on must be used, as illustrated by the following examples:
dolfin_debug(message);
Note that in order to pass formatting strings and additional arguments with
debug messages, the variations dolfin debug1(), dolfin debug2() and so
on, depending on the number of arguments, must be used.
67
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
dolfin assert(check);
Note that assertions are only active when compiling DOLFIN and your
program with DEBUG defined (configure option --enable-debug or compiler
flag -DDEBUG). Otherwise, the macro dolfin assert() expands to nothing,
meaning that liberal use of assertions does not affect performance, since as-
sertions are only present during development and debugging.
The two functions dolfin begin() and dolfin end() available in the dolfin
name space can be used to notify the DOLFIN log system about the begin-
ning and end of a task:
void dolfin_begin();
void dolfin_end();
68
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
These functions enable the DOLFIN log system to display messages, warn-
ings and errors hierarchically, by automatically indenting the output pro-
duced between calls to dolfin begin() and dolfin end(). A program may
contain an arbitrary number of nested tasks.
The DOLFIN log system provides the class Progress for simple creation of
progress sessions. A progress session automatically displays the progress of
a computation using a progress bar.
69
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
p = i;
}
Progress p(‘‘Time-stepping’’);
while ( t < T )
{
...
p = t / T;
}
The progress bar created by the progress session will only be updated if the
progress has changed significantly since the last update (by default at least
10%). The amount of change needed for an update can be controlled using
the parameter ‘‘progress step’’:
By default, the DOLFIN log system directs messages to standard output (the
terminal). Other options include directing messages to a curses interface or
turning of messages completely. To specify the output destination, use the
function dolfin output() available in the dolfin namespace:
70
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
It is possible to switch the DOLFIN log system on or off using the function
dolfin log() available in the dolfin namespace. This function accepts as
argument a bool, specifying whether or not messages should be directed
to the current output destination. This function can be useful to suppress
excessive logging, for example when calling a function that generates log
messages multiple times:
GMRES gmres;
while ( ... )
{
...
dolfin_log(false);
gmres.solve(A, x, b);
dolfin_log(true);
...
}
71
Chapter 11
I Developer’s note: Since this chapter was written, the DOLFIN parame-
ter system has been completely redesigned and now supports localization of
parameters to objects or hierarchies of objects. Chapter needs to be updated.
To retrieve the value of a parameter, use the function get() available in the
dolfin namespace:
This function accepts as argument a string key and returns the value of the
parameter matching the given key. An error message is printed through the
73
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
log system if there is no parameter with the given key in the database.
The value of the parameter is automatically cast to the correct type when
assigning the value of get() to a variable, as illustrated by the following
examples:
Note that there is a small cost associated with accessing the value of a pa-
rameter, so if the value of a parameter is to be used multiple times, then it
should be retrieved once and stored in a local variable as illustrated by the
following example:
To modify the value of a parameter, use the function set() available in the
dolfin namespace:
This function accepts as arguments a string key together with the corre-
sponding value. The value type should match the type of parameter that is
74
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
being modified. An error message is printed through the log system if there
is no parameter with the given key in the database.
set(‘‘tolerance’’, 0.01);
set(‘‘number of samples’’, 10);
set(‘‘solve dual problem’’, true);
set(‘‘file name’’, ‘‘solution.xml’’);
Note that changing the values of parameters using set() does not change
the values of already retrieved parameters; it only changes the values of
parameters in the database. Thus, the value of a parameter must be changed
before using a component that is controlled by the parameter in question.
To add a parameter to the database, use the function add() available in the
dolfin namespace:
This function accepts two arguments: a unique key identifying the new pa-
rameter and the value of the new parameter.
add(‘‘tolerance’’, 0.01);
add(‘‘number of samples’’, 10);
add(‘‘solve dual problem’’, true);
add(‘‘file name’’, ‘‘solution.xml’’);
75
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
The following code illustrates how to save the current database of parameters
to a file in DOLFIN XML format:
File file(‘‘parameters.xml’’);
file << ParameterSystem::parameters;
The following code illustrates how to load a set of parameters into the current
database of parameters from a file in DOLFIN XML format:
File file(‘‘parameters.xml’’);
file >> ParameterSystem::parameters;
<dolfin xmlns:dolfin=’’http://www.fenics.org/dolfin/’’>
<parameters>
<parameter name=’’tolerance’’ type=’’real’’ value=’’0.01’’/>
<parameter name=’’number of samples’’ type=’’int’’ value=’’10’’/>
<parameter name=’’solve dual problem’’ type=’’bool’’ value=’’false’’/>
<parameter name=’’file name’’ type=’’string’’ value=’’solution.xml’’/>
</parameters>
</dolfin>
76
Chapter 12
Solvers
1. Poisson
2. Convection-Diffusion
3. Navier-Stokes
4. Elasticity
77
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
FIXME:List solvers, then present in detail, include lots of nice images with
solver output
−∆u = f in Ω,
u = gD on Γ1 , (12.1)
−∂n u = 0 on Γ2
R R
Ω
∇u · ∇v dx = Ω
f v dx ∀v. (12.2)
The boundary conditions are enforced strongly and thus don’t appear in the
variational formulation.
12.1.1 Usage
78
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
int main()
{
Mesh mesh("mesh.xml.gz");
MyFunction f;
MyBC bc;
PoissonSolver::solve(mesh, f, bc);
return 0;
}
Where “f” is a Function specifying the right-hand side of the equation and
“bc” is a BoundaryCondition.
12.1.2 Performance
The solver is an illustrative example and performance has not been a goal.
It uses a GMRES linear solver, where a multi-grid linear solver would give
optimal performance.
12.1.3 Limitations
The solver is meant to be the simplest example solver, and therefore some
simplifications have been made. Typically the general form of Poisson’s equa-
tion includes a diffusion coefficient which has been omitted here.
79
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
12.2 Convection–diffusion
u̇ + b · ∇u − ∇ · (a∇u) = f in Ω × (0, T ],
u = gD on Γ1 × (0, T ],
(12.3)
−∂n u = 0 on Γ2 × (0, T ],
u(·, 0) = u0 in Ω,
where the convection is given by the vector b = b(x, t) and the diffusion is
given by a = a(x, t).
FIXME:Stabilized convection-diffusion
12.2.1 Usage
// Solve convection-diffusion
void solve();
80
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
int main()
{
dolfin_output("curses");
Mesh mesh("dolfin.xml.gz");
Convection w;
Source f;
MyBC bc;
ConvectionDiffusionSolver::solve(mesh, w, f, bc);
return 0;
}
12.2.2 Performance
There are no particular performance issues with the solver. GMRES is used
for solving the linear system.
12.2.3 Limitations
81
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
12.3.1 Usage
12.3.2 Performance
12.3.3 Limitations
12.4 Elasticity
82
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
u = x − X,
u̇ − v = 0 in Ω0 ,
v̇ − ∇ · σ = f in Ω0 ,
σ = E(u) = E(∇u> + ∇u)
E = λtr()I + 2µ,
v(0, ·) = v 0 , u(0, ·) = u0 in Ω0 ,
u = gD on Γ1 × (0, T ],
−∂n u = 0 on Γ2 × (0, T ]
R R
Ω
v̇w dx = Ω
−σ(u)(v) + f w dx, ∀w. (12.4)
12.4.1 Usage
12.4.2 Performance
12.4.3 Limitations
83
Bibliography
85
Appendix A
Reference elements
The reference triangle (Figure A.1) is defined by the following three vertices:
v 0 = (0, 0),
v 1 = (1, 0), (A.1)
v 2 = (0, 1).
The edges of the reference triangle are ordered following the convention that
edge ei should be opposite to vertex v i for i = 0, 1, 2, with the vertices of
each edge ordered to give a counter-clockwise orientation of the triangle in
the plane:
e0 : (v 1 , v 2 ),
e1 : (v 2 , v 0 ), (A.2)
e2 : (v 0 , v 1 ).
87
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
x1
v2
v 0 = (0, 0)
v 1 = (1, 0)
PSfrag replacements
v 2 = (0, 1)
x0
v0 v1
v2
e1 e0
PSfrag replacements
v0 e2 v1
Figure A.2: Ordering of mesh entities (vertices and edges) for the reference
triangle.
88
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
The reference tetrahedron (Figure A.3) is defined by the following four ver-
tices:
v0 = (0, 0, 0),
v1 = (1, 0, 0),
(A.3)
v2 = (0, 1, 0),
v4 = (0, 0, 1).
The faces of the reference tetrahedron are ordered following the convention
that face f i should be opposite to vertex v i for i = 0, 1, 2, 3, with the vertices
of each face ordered to give a counter-clockwise orientation of each face as
seen from the outside of the tetrahedron and the first vertex of face f i given
by vertex v i+1 mod 4 :
f0 : (v 1 , v 3 , v 2 ),
f1 : (v 2 , v 3 , v 0 ),
(A.4)
f2 : (v 3 , v 1 , v 0 ),
f3 : (v 0 , v 1 , v 2 ).
The edges of the reference tetrahedron are ordered following the convention
that edges e0 , e1 , e2 should correspond to the edges of the reference triangle.
Edges e3 , e4 , e5 all ending up at vertex v 3 are ordered based on their first
vertex:
e0 : (v 1 , v 2 ),
e1 : (v 2 , v 0 ),
e2 : (v 0 , v 1 ),
(A.5)
e3 : (v 0 , v 3 ),
e4 : (v 1 , v 3 ),
e5 : (v 2 , v 3 ).
89
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
faces by identifying an edge on a face with the opposite vertex on the face:
f0 : (e5 , e0 , e4 ),
f1 : (e3 , e1 , e5 ),
(A.6)
f2 : (e2 , e3 , e4 ),
f3 : (e0 , e1 , e2 ).
Note that the ordering of edges on f 3 is the same as the ordering of edges
on the reference triangle. Also note that the internal ordering of vertices
on edges does not always follow the orientation of the face (which is not
possible).
The local and global orderings of degrees of freedom or nodes are obtained
by associating each node with a mesh entity, locally and globally.
90
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
x2
v3
v 0 = (0, 0, 0)
v 1 = (1, 0, 0)
v 2 = (0, 1, 0)
v 3 = (0, 0, 1)
PSfrag replacements
v2 x1
v0
v1 x0
91
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
v3
PSfrag replacements e3 e5
1
f
f0
f2 e4
v2
e1
v0
f3
e0
2
e
v1
Figure A.4: Ordering of mesh entities (vertices, edges, faces) for the reference
tetrahedron.
92
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
With each mesh entity, there can be associated zero or more nodes and the
nodes are ordered locally and globally based on the topological dimension of
the mesh entity with which they are associated. Thus, any nodes associated
with vertices are ordered first and nodes associated with cells last.
If more than one node is associated with a single mesh entity, the internal
ordering of the nodes associated with the mesh entity becomes important, in
particular for edges and faces, where the nodes of two adjacent cells sharing
a common edge or face must line up.
For edges containing more than one node, the nodes are ordered in the di-
rection from the first vertex (ve0 ) of the edge to the second vertex (ve1 ) of the
edge as in Figure A.5.
ve1
PSfrag replacements
0
ve0
93
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Depending on the orientation of any given cell, an edge on the cell may be
aligned or not aligned with the corresponding edge on the reference cell if the
vertices of the cell are mapped to the reference cell. We define the alignment
of an edge with respect to a cell to be 0 if the edge is aligned with the
orientation of the reference cell and 1 otherwise.
If two cells share a common edge and the edge is aligned with one of the cells
and not the other, we must reverse the order in which the local nodes are
mapped to global nodes on one of the two cells. As a convention, the order
is kept if the alignment is 0 and reversed if the alignment is 1.
For faces containing more than one node, the ordering of nodes is nested
going from the first to the third vertex and in each step going from the first
to the second vertex as in Figure A.6.
There are six different ways for a face to be aligned on a tetrahedron; there are
three ways to pick the first edge of the face, and once the first edge is picked,
there are two ways to pick the second edge. To define an alignment of faces as
an integer between 0 and 5, we compare the ordering of edges on a face with
the ordering of edges on the corresponding face on the reference tetrahedron.
If the first edge of the face matches the first edge on the corresponding face
on the reference tetrahedron and also the second edge matches the second
edge on the reference tetrahedron, then the alignment is 0. If only the first
94
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
vf2
PSfrag replacements 5
3 4
0 1 2
vf0 vf1
95
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
96
Appendix B
Installation
The source code of DOLFIN is portable and should compile on any Unix
system, although it is developed mainly under GNU/Linux (in particular
Debian GNU/Linux). DOLFIN can be compiled under Windows through
Cygwin [1]. Questions, bug reports and patches concerning the installation
should be directed to the DOLFIN mailing list at the address
DOLFIN must currently be compiled directly from source, but effort is under-
way to provide precompiled Debian packages of DOLFIN and other FEniCS
components.
97
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
libraries, you need to install FIAT and FFC if you want to define your own
variational forms.
Installing Libxml2
Libxml2 is a library used by DOLFIN to parse XML data files. Libxml2 can
be obtained from
http://xmlsoft.org/
Packages are available for most Linux distributions. For Debian users, the
package to install is libxml2-dev.
Installing PETSc
PETSc is a library for the solution of linear and nonlinear systems, function-
ing as the backend for the DOLFIN linear algebra classes. DOLFIN depends
on PETSc version 2.3.1 (or 2.3.0), which can be obtained from
http://www-unix.mcs.anl.gov/petsc/petsc-2/
Follow the installation instructions on the PETSc web page. Normally, you
should only have to perform the following simple steps in the PETSc source
directory:
# export PETSC_DIR=‘pwd‘
# ./config/configure.py --with-clanguage=cxx --with-shared=1
# make all
98
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Installing FFC
DOLFIN uses the FEniCS Form Compiler FFC to process variational forms.
FFC can be obtained from
http://www.fenics.org/
Follow the installation instructions given in the FFC manual. FFC follows
the standard for Python packages, which means that normally you should
only have to perform the following simple step in the FFC source directory:
Note that FFC depends on FIAT , which in turn depends on the Python pack-
ages Numeric (Debian package python-numeric) and LinearAlgebra (Debian
package python-numeric-ext). Refer to the FFC manual for further details.
http://www.fenics.org/
99
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
If you want the very latest version of DOLFIN, there is also a version named
dolfin-cvs-current.tar.gz which provides a snapshot of the current CVS
version of DOLFIN, updated automatically from the CVS repository each
hour. This version may contain features not yet present in the latest release,
but may also be less stable and even not work at all.
# ./configure
# make
followed by an optional
# make install
The configure script will check for a number of libraries and try to figure out
how compile DOLFIN against these libraries. The configure script accepts a
collection of optional arguments that can be used to control the compilation
process. A few of these are listed below. Use the command
# ./configure --help
100
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
# ./configure.local
# make
# make install
101
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
After compiling the DOLFIN library according to the instructions above, you
may want to try one of the demo programs in the subdirectory src/demo/
of the DOLFIN source tree. Just enter the directory containing the demo
program you want to compile and type make. You may also compile all demo
programs at once using the command
# make demo
Whether you are writing your own Makefiles or using an automated build
system such as GNU Autotools or BuildSystem, it is straightforward to com-
pile a program against DOLFIN. The necessary include and library paths
can be obtained through the script dolfin-config which is automatically
generated during the compilation of DOLFIN and installed in the bin sub-
directory of the <path> specified with --prefix. Assuming this directory is
in your executable path (environment variable PATH), the include path for
building DOLFIN can be obtained from the command
# dolfin-config --cflags
and the path to DOLFIN libraries can be obtained from the command
# dolfin-config --libs
Examples of how to write a proper Makefile are provided with each of the
example programs in the subdirectory src/demo/ in the DOLFIN source
tree.
102
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
In preparation.
DOLFIN can be used under Windows using Cygwin, which provides a Linux-
like environment. The installation process is largely the same as under
GNU/Linux. This section addresses some Cygwin specific issues.
To use DOLFIN under Cygwin, the Cygwin development tools must be in-
stalled. Instructions for installing PETSc under Cygwin can be found on
the PETSc web page. Installation of FFC and FIAT is the same as under
GNU/Linux. The Python package Numeric is not available as a Cygwin
package and must be installed manually. To compile DOLFIN, the Cygwin
package libxml2-devel must be installed. DOLFIN does not link correctly
with the curses libraries of Cygwin, so under Cygwin, DOLFIN can be built
by:
# ./configure --disable-curses
# make
Due to problems with the latest GCC compilers provided by Cygwin (GCC
3.4.4), compiling DOLFIN will lead to fatal errors. These problems can be
avoided by using GCC version 3.3.3 provided by Cygwin.
103
Appendix C
Contributing code
If you have created a new module, fixed a bug somewhere, or have made a
small change which you want to contribute to DOLFIN, then the best way to
do so is to send us your contribution in the form of a patch. A patch is a file
which describes how to transform a file or directory structure into another.
The patch is built by comparing a version which both parties have against
the modified version which only you have.
The tool used to create a patch is called diff and the tool used to apply
the patch is called patch. These tools are free software and are standard on
most Unix systems.
Here’s an example of how it works. Start from the latest release of DOLFIN,
which we here assume is release 0.1.0. You then have a directory structure
under dolfin-0.1.0 where you have made modifications to some files which
you think could be useful to other users.
105
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
# make clean
# mv dolfin-0.1.0 dolfin-0.1.0-mod
4. You should now have two DOLFIN directory structures in your current
directory:
# ls
dolfin-0.1.0
dolfin-0.1.0-mod
# gzip dolfin-<identifier>-<date>.patch
106
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
Patch files should be sent to the DOLFIN mailing list at the address
Let’s say that a patch has been built relative to DOLFIN release 0.1.0. The
following description then shows how to apply the patch to a clean version
of release 0.1.0.
1. Unpack the version of DOLFIN which the patch is built relative to:
# ls
dolfin-0.1.0
dolfin-<identifier>-<date>.patch
# cd dolfin-0.1.0
107
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
The option -p1 strips the leading directory from the filename references
in the patch, to match the fact that we are applying the patch from
inside the directory. Another useful option to patch is --dry-run
which can be used to test the patch without actually applying it.
108
Appendix D
License
DOLFIN is licensed under the GNU General Public License (GPL) version
2, included verbatim below.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation’s software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.
109
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author’s protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors’ reputations.
110
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
111
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
112
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients’ exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
113
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
114
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
115
Index
117
DOLFIN User Manual Hoffman, Jansson, Logg, Wells
NewtonSolver, 55
nonlinear solver, 53
NonlinearFunction, 53
object, 57
output destination, 70
parameters, 73
ParaView, 36
partial differential equations, 45
patch, 105, 107
PETSc, 98
Poisson’s equation, 16, 78
post-processing, 57
pre-processing, 57
progress bar, 69
quickstart, 15
reference tetrahedron, 89
reference triangle, 87
source code, 99
tasks, 68
typographic conventions, 11
user-defined functions, 39
warnings, 66
XML, 59, 76
118