From The FEniCS project

Getting started

This all looks good, but where do I start? This is a good question and the answer depends on your interest.

You may be interested in using a particular component, like calling FIAT or SyFi to tabulate finite element basis functions, or you may be interested in using FFC as a code generator. However, you are most likely reading this because you want to solve differential equations and that's what we will concentrate on here - using FEniCS as a problem-solving environment (PSE) for differential equations.

DOLFIN functions as the main user interface of FEniCS and is available both in the form of a C++ library and as a Python module. DOLFIN depends on the other components of FEniCS as backends for specific tasks. For example, FIAT provides DOLFIN with finite element basis functions, FFC generates code for working with finite element variational formulations and Viper provides built-in plotting.

Installation

The first thing you need to do is to install the software. For a minimal installation, you need to install DOLFIN and UFC, but it is highly recommended that you also install FFC, FIAT, Instant and Viper. All these packages can be found in the Download section.

If you are lucky and are running Debian or a Debian-based system like Ubuntu, you may install all required packages through the package manager:

apt-get install fenics

This will install DOLFIN and all the other packages.

If you cannot use the prebuilt packages, you need to download the source code and build the packages manually. This is most easily done using Dorsal. Simply download Dorsal and type

./dorsal.sh

to automatically download and install all FEniCS components and their dependencies.

For more detailed build instructions, refer to the manuals of each package and don't be afraid to ask for help on one of the mailing lists (Contact section).

Solving Poisson's equation

If installation went smoothly and you have installed DOLFIN and its dependencies, you are now ready to solve a differential equation. The Python script listed below demonstrates how to solve Poisson's equation on the unit square and plot the solution. To run this script, save it to a file and name it demo.py. Then simply run

python demo.py

If all goes well, you should see the following plot of the computed solution.

Solving Poisson's equation

In this script, we first create a mesh of the unit square and a function space on that mesh. We use standard piecewise linears but many other families of finite elements are available, including arbitrary order continuous and discontinuous Lagrange elements, BDM elements and RT elements.

We then define a subset of the boundary (a SubDomain) that we use to specify a Dirichlet boundary condition.

Solving Poisson's equation is then just a matter of writing down the variational problem, computing and plotting the solution. We also store the solution to a VTK file for postprocessing in MayaVi or Paraview.

The script reads as follows:

from dolfin import *

# Create mesh and define function space
mesh = UnitSquare(32, 32)
V = FunctionSpace(mesh, "CG", 1)

# Define Dirichlet boundary (x = 0 or x = 1)
class DirichletBoundary(SubDomain):
    def inside(self, x, on_boundary):
        return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS

# Define boundary condition
u0 = Constant(0.0)
bc = DirichletBC(V, u0, DirichletBoundary())

# Define variational problem
v = TestFunction(V)
u = TrialFunction(V)
f = Expression(("500.0 * exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)"))
a = dot(grad(v), grad(u))*dx
L = v*f*dx

# Compute solution
problem = VariationalProblem(a, L, bc)
u = problem.solve()

# Plot solution
plot(u)

# Save solution in VTK format
file = File("poisson.pvd")
file << u

# Hold plot
interactive()

Don't worry if this looks too simple. DOLFIN provides a simple solver for linear variational problems (which just assembles a linear system and solves it) but for more complicated problems, you may want to assemble specific matrices and vectors yourself and use these as building blocks in an iterative procedure.

For a C++ implementation of the above example, take a look at the quickstart chapter in the DOLFIN manual.

Personal tools