0% found this document useful (0 votes)
151 views

Fenics Tutorial

This document discusses implementing finite element models in FEniCS. It begins by introducing FEniCS as an automated programming environment for differential equations. It then provides instructions on installing FEniCS and describes a first solver for Poisson's equation with Dirichlet boundary conditions as an example. Finally, it walks through implementing this Poisson solver step-by-step in FEniCS, from defining the mesh and function space to solving the variational problem and post-processing the solution.

Uploaded by

Leo Costa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
151 views

Fenics Tutorial

This document discusses implementing finite element models in FEniCS. It begins by introducing FEniCS as an automated programming environment for differential equations. It then provides instructions on installing FEniCS and describes a first solver for Poisson's equation with Dirichlet boundary conditions as an example. Finally, it walks through implementing this Poisson solver step-by-step in FEniCS, from defining the mesh and function space to solving the variational problem and post-processing the solution.

Uploaded by

Leo Costa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 126

Implementing finite element models in FEniCS

Patrick Farrell

Oxford

May 2017

P. E. Farrell (Oxford) Practical I May 2017 1 / 20


FEniCS is an automated programming environment for
differential equations

I C++/Python library
I Began in 2003
I Thousands of downloads/month
I Developers all over the world
I Licensed under the GNU LGPL

http://fenicsproject.org/

P. E. Farrell (Oxford) Practical I May 2017 2 / 20


Installing FEniCS

To do the exercises for this course, you need:


I Docker [http://www.docker.com/products/docker-toolbox]
I FEniCS installed via fenicsproject run
I Paraview [http://www.paraview.org/download]
I A text editor, e.g. vim or [http://www.sublimetext.com]

P. E. Farrell (Oxford) Practical I May 2017 3 / 20


A first solver: Poisson

Consider Poisson’s equation with Dirichlet boundary conditions:

−∆u = f in Ω
u = 0 on ∂Ω

Poisson’s equation is ubiquitous in physical applications, and often arises


as a subproblem of a more complex solver.

P. E. Farrell (Oxford) Practical I May 2017 4 / 20


From PDE to variational problem
We multiply the PDE by a test function v and integrate over Ω:
Z Z
− (∆u)v dx = f v dx
Ω Ω

Then integrate by parts and set v = 0 on the Dirichlet boundary:


Z Z Z
∂u
− (∆u)v dx = ∇u · ∇v dx − v ds
Ω Ω ∂Ω ∂n
| {z }
=0

In weak form, the equation is: find u ∈ H01 (Ω) such that
Z Z
∇u · ∇v dx = f v dx
Ω Ω

for all v ∈ H01 (Ω).


P. E. Farrell (Oxford) Practical I May 2017 5 / 20
From continuous (i) to discrete (ii) problem

Choose finite dimensional subspaces of V = H01 (Ω):

Vh ⊂ V

with e.g. piecewise linear finite elements.

Galerkin projection: find uh ∈ Vh such that


Z Z
∇uh · ∇vh dx = f vh dx
Ω Ω

for all vh ∈ Vh .

P. E. Farrell (Oxford) Practical I May 2017 6 / 20


A test problem

We construct a test problem for which we can easily check the answer. We
first define the exact solution by

u(x, y) = 1 + x2 + 2y 2

We insert this into Poisson’s equation:

f = −∆u = −∆(1 + x2 + 2y 2 ) = −(2 + 4) = −6


and we know that u = 1 + x2 + 2y 2 on the boundary of Ω.

This technique is called the method of manufactured solutions (MMS).

P. E. Farrell (Oxford) Practical I May 2017 7 / 20


Poisson solver in FEniCS: implementation
from dolfin import *

mesh = UnitSquareMesh(32, 32)


(x, y) = SpatialCoordinate(mesh)

element = FiniteElement("Lagrange", mesh.ufl_cell(), 1)


V = FunctionSpace(mesh, element)

u = Function(V)
v = TestFunction(V)

f = Constant(-6.0)
g = 1 + x**2 + 2*y**2
bc = DirichletBC(V, g, DomainBoundary())

F = inner(grad(u), grad(v))*dx - f*v*dx

solve(F == 0, u, bc)
File("poisson.pvd") << u
P. E. Farrell (Oxford) Practical I May 2017 8 / 20
Step by step: the first line

The first line of a FEniCS program usually begins with


from dolfin import *

This imports key classes like UnitSquareMesh, FunctionSpace,


Function and so forth, from the FEniCS user interface (DOLFIN).

P. E. Farrell (Oxford) Practical I May 2017 9 / 20


Step by step: creating a mesh

Next, we create a mesh of our domain Ω:


mesh = UnitSquareMesh(32, 32)

defines a (triangular) mesh with 32 elements along each edge.

Other useful classes for creating meshes include UnitIntervalMesh,


UnitCubeMesh, UnitCircleMesh, UnitSphereMesh, RectangleMesh
and BoxMesh.

Complex geometries should be built in dedicated mesh generation tools and


imported:
mesh = Mesh("complexmesh.xdmf")

Mesh generation is a huge subject in its own right.

P. E. Farrell (Oxford) Practical I May 2017 10 / 20


Step by step: creating a function space
The following lines create our discrete function space on Ω:
element = FiniteElement("Lagrange", triangle, 1)
V = FunctionSpace(mesh, element)

The first argument specifies the family of element, while the third
argument is the degree of the basis functions on the element.

Other types of elements include


I "Discontinuous Lagrange",
I "Brezzi-Douglas-Marini",
I "Raviart-Thomas",
I "Crouzeix-Raviart",
I "Nedelec 1st kind H(curl)".
See help(FiniteElement) for a list.
P. E. Farrell (Oxford) Practical I May 2017 11 / 20
Step by step: defining expressions

Next, we define an expression for the boundary value:


(x, y) = SpatialCoordinate(mesh)
g = 1 + x**2 + 2*y**2

Another way:
g = Expression("1 + x[0]*x[0] + 2*x[1]*x[1]",
element=V.ufl_element())

This is compiled to C++.

The Expression class is very flexible and can be used to create complex
user-defined expressions: looking up information from a database or the
internet, solving a local problem for material parameters, etc.

P. E. Farrell (Oxford) Practical I May 2017 12 / 20


Step by step: defining a boundary condition

The following code defines a Dirichlet boundary condition:


bc = DirichletBC(V, g, DomainBoundary())

This boundary condition states that a function in the function space


defined by V should be equal to g on the domain defined by
DomainBoundary().

Note that the above line does not yet apply the boundary condition to all
functions in the function space. (It gets enforced strongly during solve.)

P. E. Farrell (Oxford) Practical I May 2017 13 / 20


Step by step: more about defining domains
For a Dirichlet boundary condition, a simple domain can be defined by a C++
string
"on_boundary" # The entire boundary, same as DomainBoundary()

Alternatively, domains can be defined by subclassing SubDomain


class Boundary(SubDomain):
def inside(self, x, on_boundary):
return on_boundary # same as DomainBoundary()

Other examples:
"near(x[0], 0.0)"
"near(x[0], 0.0) || near(x[1], 1.0)"

There are many more possibilities, see


help(SubDomain)
help(DirichletBC)
P. E. Farrell (Oxford) Practical I May 2017 14 / 20
Step by step: defining the right-hand side

The right-hand side f = −6 may be defined as follows:


f = Expression("-6")

or (more efficiently) as
f = Constant(-6.0)

Using a Constant means that the intermediate C++ code won’t be


regenerated when the value changes.

P. E. Farrell (Oxford) Practical I May 2017 15 / 20


Step by step: defining variational problems

Variational problems are defined in terms of solution and test functions:


u = Function(V)
v = TestFunction(V)

We now have all the objects we need in order to specify the form:
F = inner(grad(u), grad(v))*dx - f*v*dx

Here dx is a type of class Measure that means ”integrate over the whole
volume”. There are other measures: for example, ds means ”integrate
over exterior facets”.

P. E. Farrell (Oxford) Practical I May 2017 16 / 20


Step by step: solving variational problems

Once a variational problem has been defined, it may be solved by calling


the solve function:
solve(F == 0, u, bc)

Nice!

P. E. Farrell (Oxford) Practical I May 2017 17 / 20


Step by step: post-processing

For postprocessing in paraview, store the solution in PVD format:


File("poisson.pvd") << u

P. E. Farrell (Oxford) Practical I May 2017 18 / 20


Poisson solver in FEniCS: implementation
from dolfin import *

mesh = UnitSquareMesh(32, 32)


(x, y) = SpatialCoordinate(mesh)

element = FiniteElement("Lagrange", mesh.ufl_cell(), 1)


V = FunctionSpace(mesh, element)

u = Function(V)
v = TestFunction(V)

f = Constant(-6.0)
g = 1 + x**2 + 2*y**2
bc = DirichletBC(V, g, DomainBoundary())

F = inner(grad(u), grad(v))*dx - f*v*dx

solve(F == 0, u, bc)
File("poisson.pvd") << u
P. E. Farrell (Oxford) Practical I May 2017 19 / 20
FEniCS 01 Challenge!

I Run the code and look at the solution in paraview.


I Compute the norms of the error with
print errornorm(g, u, ’L2’)
print errornorm(g, u, ’H1’)

I Try using quadratic Lagrange elements instead of linear elements.


What happens to the error?
I Formulate a problem on Ω = [0, 1]3 whose solution you know using
the method of manufactured solutions.
Change the code to solve this problem.
I More advanced question: what solvers would have optimal complexity
for this problem?

P. E. Farrell (Oxford) Practical I May 2017 20 / 20


L-shaped domains: meshing, singularities and adaptivity

Patrick Farrell

Oxford

May 2017

P. E. Farrell (Oxford) Practical II May 2017 1/8


Constructing the L-shaped domain

Three classes of domains:


I Trivial: use built-in classes (BoxMesh, RectangleMesh, etc)
I Complex: use dedicated mesh generator (aircraft, engine, etc)
I Intermediate: use mshr, package for constructive solid geometry

P. E. Farrell (Oxford) Practical II May 2017 2/8


Constructive solid geometry

Constructive solid geometry expresses a domain with two ingredients:

Geometric primitives: Operations on those primitives:


I rectangle/box I union
I circle/sphere I intersection
I cylinder/cone I set difference
I polygon/polyhedron I rotation
I ... I scaling
I translation

P. E. Farrell (Oxford) Practical II May 2017 3/8


Constructing the L-shaped domain

We want to construct

Ω = [−1, 1]2 \ [0, 1] × [0, −1].


We can do this with
from dolfin import *
from mshr import *

square = Rectangle(Point(-1, -1), Point(1, 1))


cutout = Rectangle(Point(+0, -1), Point(1, 0))
domain = square - cutout
mesh = generate_mesh(domain, 50)

P. E. Farrell (Oxford) Practical II May 2017 4/8


Constructing the L-shaped domain

P. E. Farrell (Oxford) Practical II May 2017 5/8


Constructing the L-shaped domain

We can make a sequence of meshes with


from mshr import *

square = Rectangle(Point(-1, -1), Point(1, 1))


cutout = Rectangle(Point(+0, -1), Point(1, 0))
domain = square - cutout

for mesh_size in [50, 100, 200]:


mesh = generate_mesh(domain, mesh_size)
# ... solve PDE here

P. E. Farrell (Oxford) Practical II May 2017 6/8


Defining the boundary data
We want to construct
2 2
g(r, θ) = r 3 sin θ.
3
We can do this with
from math import atan2
class BoundaryData(Expression):
def eval(self, values, x):
r = sqrt(x[0]**2 + x[1]**2)
theta = atan2(x[1], x[0])

# atan2 gives output in [-pi, pi];


# change to [0, 2*pi]
if theta < 0: theta += 2*pi

values[0] = r**(2.0/3.0) * sin((2.0/3.0) * theta)

g = BoundaryData(degree=5)

P. E. Farrell (Oxford) Practical II May 2017 7/8


FEniCS 02 Challenge!

Solve the Laplace equation with the given boundary data.

Examine the order of convergence as the mesh is refined


(mesh size = 50, 100, 200).

P. E. Farrell (Oxford) Practical II May 2017 8/8


Natural boundary conditions

Patrick Farrell

Oxford

May 2017

P. E. Farrell (Oxford) Practical III May 2017 1/7


Two kinds of boundary conditions

Two kinds of boundary conditions:


Essential boundary conditions
Enforced in the definition of the trial space, e.g.

V = {u ∈ H 1 (Ω) : u|Γ = g}.

Natural boundary conditions


Implied by the weak form of the problem.

P. E. Farrell (Oxford) Practical III May 2017 2/7


Poisson with Robin boundary conditions

Consider Poisson’s equation with a Robin boundary condition:

−∆u = f in Ω
αu + ∇u · n = 0 on ∂Ω

P. E. Farrell (Oxford) Practical III May 2017 3/7


Poisson with Robin boundary conditions

Consider Poisson’s equation with a Robin boundary condition:

−∆u = f in Ω
αu + ∇u · n = 0 on ∂Ω

Integrating by parts, we find


Z Z Z
∇u · ∇v dx − (∇u · n)v ds = f v dx
Z Ω Z∂Ω Z Ω

=⇒ ∇u · ∇v dx + αuv ds = f v dx
Ω ∂Ω Ω

P. E. Farrell (Oxford) Practical III May 2017 3/7


Integrating over surfaces

The surface integral term can be implemented with the ds measure:

Z Z Z
∇u · ∇v dx + αuv ds = f v dx
Ω ∂Ω Ω

F = (
inner(grad(u), grad(v))*dx
+ inner(alpha*u, v)*ds
- inner(f, v)*dx
)

P. E. Farrell (Oxford) Practical III May 2017 4/7


Imposing mixed boundary conditions

Consider Poisson’s equation with mixed boundary conditions:

−∆u = f in Ω
αu + ∇u · n = 0 on ∂Ω|x = 0
u=g on ∂Ω|x > 0

Two ingredients need to be modified: the Dirichlet condition, and the


surface measure.
bc = DirichletBC(V, g, "x[0] > 0 && on_boundary")

P. E. Farrell (Oxford) Practical III May 2017 5/7


Integrating over subdomains
To integrate over subsets of geometric entities, we need to color them.
# Color the facets of the mesh
colors = FacetFunction("size_t", mesh)
colors.set_all(0) # default to zero
CompiledSubDomain("x[0] == 0").mark(colors, 1)

# Visualise the colors


File("colors.pvd") << colors

# Create the measure


ds = Measure("ds", subdomain_data=colors)

# Integrate over facets with color 1


F = ... + inner(alpha*u, v)*ds(1)

P. E. Farrell (Oxford) Practical III May 2017 6/7


FEniCS 03 Challenge!

Solve the problem

−∆u = 1 in Ω = [0, 1]2


u + ∇u · n = 0 on ∂Ω|x = 0
u = 0 on ∂Ω|x > 0 .

P. E. Farrell (Oxford) Practical III May 2017 7/7


Semilinear PDEs

Patrick Farrell

Oxford

May 2017

P. E. Farrell (Oxford) Practical IV May 2017 1/8


Nonlinear PDEs

The Poisson equation models stationary heat distribution. If we take


energy emitted via black-body radiation, we get a Stefan-Boltzmann
boundary condition:

−∆u = f in Ω
∇u · n = β(c4 − u4 ) on ∂Ω,

where c is the temperature of the surrounding medium.

This equation is semilinear.

P. E. Farrell (Oxford) Practical IV May 2017 2/8


Classification of PDEs

Linear PDEs
The coefficients do not depend on the solution.

P. E. Farrell (Oxford) Practical IV May 2017 3/8


Classification of PDEs

Linear PDEs
The coefficients do not depend on the solution.

Semilinear PDEs
The coefficients of the highest-order derivatives do not depend on the
solution.

P. E. Farrell (Oxford) Practical IV May 2017 3/8


Classification of PDEs

Linear PDEs
The coefficients do not depend on the solution.

Semilinear PDEs
The coefficients of the highest-order derivatives do not depend on the
solution.

Quasilinear PDEs
The coefficients of the highest-order derivatives depend on lower-order
derivatives of the solution.

P. E. Farrell (Oxford) Practical IV May 2017 3/8


Classification of PDEs

Linear PDEs
The coefficients do not depend on the solution.

Semilinear PDEs
The coefficients of the highest-order derivatives do not depend on the
solution.

Quasilinear PDEs
The coefficients of the highest-order derivatives depend on lower-order
derivatives of the solution.

Fully nonlinear PDEs


All coefficients can depend on all derivatives of the solution.

P. E. Farrell (Oxford) Practical IV May 2017 3/8


Poisson with Stefan-Boltzmann

Integrating by parts, we find


Z Z Z
∇u · ∇v dx − (∇u · n)v ds = f v dx
ZΩ Z∂Ω Ω Z
=⇒ ∇u · ∇v dx − β(c4 − u4 )v ds = f v dx
Ω ∂Ω Ω

P. E. Farrell (Oxford) Practical IV May 2017 4/8


Poisson with Stefan-Boltzmann

Integrating by parts, we find


Z Z Z
∇u · ∇v dx − (∇u · n)v ds = f v dx
ZΩ Z∂Ω Ω Z
=⇒ ∇u · ∇v dx − β(c4 − u4 )v ds = f v dx
Ω ∂Ω Ω

F = (
inner(grad(u), grad(v))*dx
- inner(beta*(c**4 - u**4), v)*ds
- inner(f, v)*dx
)

P. E. Farrell (Oxford) Practical IV May 2017 4/8


Newton–Kantorovich method

The main algorithm for solving nonlinear equations:


Newton–Kantorovich algorithm

I Apply boundary conditions to u.


I While not converged:
I Solve: find δu ∈ V0 such that

F 0 (u; v, δu) = −F (u; v) ∀ v ∈ V̂

I Set u = u + δu.

P. E. Farrell (Oxford) Practical IV May 2017 5/8


Properties of Newton–Kantorovich
Some facts about Newton–Kantorovich:

Computational challenge
Main cost: solving the linearized system.

P. E. Farrell (Oxford) Practical IV May 2017 6/8


Properties of Newton–Kantorovich
Some facts about Newton–Kantorovich:

Computational challenge
Main cost: solving the linearized system.

Fast convergence
Converges quadratically if close to a regular solution.

P. E. Farrell (Oxford) Practical IV May 2017 6/8


Properties of Newton–Kantorovich
Some facts about Newton–Kantorovich:

Computational challenge
Main cost: solving the linearized system.

Fast convergence
Converges quadratically if close to a regular solution.

Divergence
Can diverge if initialized far from a solution.

P. E. Farrell (Oxford) Practical IV May 2017 6/8


Properties of Newton–Kantorovich
Some facts about Newton–Kantorovich:

Computational challenge
Main cost: solving the linearized system.

Fast convergence
Converges quadratically if close to a regular solution.

Divergence
Can diverge if initialized far from a solution.

Multiple solutions
Can converge to different solutions from different initial guesses.
P. E. Farrell (Oxford) Practical IV May 2017 6/8
Using Newton’s method in FEniCS

On entry: u is the initial guess.

solve(F == 0, u, bcs)

On exit: u is the solution.

P. E. Farrell (Oxford) Practical IV May 2017 7/8


Using Newton’s method in FEniCS

On entry: u is the initial guess.

solve(F == 0, u, bcs)

On exit: u is the solution.

Setting a good initial guess is crucial for convergence!


u.interpolate(Constant(1))
solve(F == 0, u, bcs)

P. E. Farrell (Oxford) Practical IV May 2017 7/8


FEniCS 04 Challenge!

Solve the problem

−∆u = 1000x(1 − x)y(1 − y) in Ω = [0, 1]2


∇u · n = (0.54 − u4 ) on ∂Ω.

Hint:

(x, y) = SpatialCoordinate(mesh)

P. E. Farrell (Oxford) Practical IV May 2017 8/8


Continuation for nonlinear problems

Patrick Farrell

Oxford

May 2017

P. E. Farrell (Oxford) Practical V May 2017 1/6


p-Laplace equation

Consider a Poisson-type equation where the diffusivity depends on the


solution itself:
−∇ · (γ(u)∇u) = f in Ω
u=g on ∂Ω

where
1
γ(u) = (2 + |∇u|2 )(p−2)/2
2
This particular choice of γ defines the p-Laplace equation.

P. E. Farrell (Oxford) Practical V May 2017 2/6


Variational formulation

Multiplying with a test function and integrating by parts yields


Z Z
F (u; v) = ∇v · γ(u)∇u dx − f v dx.
Ω Ω

This can be written as


p = Constant(5.0)
epsilon = Constant(1.0e-5)

gamma = (epsilon**2 + 0.5 * inner(grad(u), grad(u)))**((p-2)/2)


F = inner(grad(v), gamma * grad(u))*dx - inner(f, v)*dx

P. E. Farrell (Oxford) Practical V May 2017 3/6


Continuation

Continuation is an extremely powerful algorithm for solving difficult


nonlinear problems.

Idea: construct a good initial guess by solving an easier problem.

Continuation
I Solve the problem for easy p (here, p = 2).
I While not finished:
I Use solution for p as initial guess for p = p + ∆p.
I Increment p.

P. E. Farrell (Oxford) Practical V May 2017 4/6


To do continuation in FEniCS, update the parameter in a loop and solve:

F = ...

for p_val in [2, 3, 4, 5]:


p.assign(p_val)
solve(F == 0, u, bc)

P. E. Farrell (Oxford) Practical V May 2017 5/6


FEniCS 05 Challenge!

Solve the p-Laplace equation on [0, 1]2 with p = 5, f = 1, g = 0,  = 10−5


(i) by tackling the problem directly from the initial guess u = 0;
(ii) via continuation from p = 2.

Compare the number of Newton iterations required by both approaches.

Hint for (i):

solve(F == 0, u, bc, solver_parameters={"newton_solver": {"maximum_iterations": 100}})

P. E. Farrell (Oxford) Practical V May 2017 6/6


Time-dependent linear PDEs: the heat equation

Patrick Farrell

Oxford

May 2017

P. E. Farrell (Oxford) Practical VI May 2017 1 / 13


The heat equation

We will solve the simplest extension of the Poisson problem into the time
domain, the heat equation:
∂u
− ∆u = f in Ω for t > 0
∂t
u = g on ∂Ω for t > 0
u = u0 in Ω at t = 0

The solution u = u(x, t), the right-hand side f = f (x, t) and the boundary
value g = g(x, t) may vary in space (x = (x0 , x1 , ...)) and time (t). The
initial value u0 is a function of space only.

P. E. Farrell (Oxford) Practical VI May 2017 2 / 13


Time-discretization of the heat equation

There are many discretizations in time, each with different stability and
efficiency properties. We will implement BDF2, a multistep scheme. Given
∂u
= h(u, t),
∂t
we will solve for un with
4 1 2
un − un−1 + un−2 = ∆th(un , tn ).
3 3 3

With h(u, t) = f (t) + ∆u, we find


2 2 4 1
un − ∆t∆un = ∆tf (tn ) + un−1 − un−2
3 3 3 3

P. E. Farrell (Oxford) Practical VI May 2017 3 / 13


Time-discretization of the heat equation

Algorithm?

I Start with u0 and choose a timestep ∆t > 0.


I For n = 1, 2, . . ., solve for un :
2 2 4 1
un − ∆t∆un = ∆tf (tn ) + un−1 − un−2
3 3 3 3

P. E. Farrell (Oxford) Practical VI May 2017 4 / 13


Time-discretization of the heat equation

Initialization
To use a multistep method you need several past solutions, but we are
only given one initial condition.

Solution: use another scheme of the same order to compute the first few
solutions. We will use Crank-Nicolson. We will solve for un with
un + un−1 tn + tn−1
un − un−1 = ∆th( , ).
2 2

With h(u, t) = f (t) + ∆u, we find

∆t tn + tn−1 ∆t
un − ∆un = ∆tf ( ) + un−1 + ∆un−1 .
2 2 2

P. E. Farrell (Oxford) Practical VI May 2017 5 / 13


Time-discretization of the heat equation

Algorithm

I Start with u0 and choose a timestep ∆t > 0.


I Solve for u1 :
∆t t1 + t0 ∆t
u1 − ∆u1 = ∆tf ( ) + u0 + ∆u0 .
2 2 2
I For n = 2, . . ., solve for un :
2 2 4 1
un − ∆t∆un = ∆tf (tn ) + un−1 − un−2 .
3 3 3 3

P. E. Farrell (Oxford) Practical VI May 2017 6 / 13


Variational problem for the heat equation

The semi-discretized BDF2 step for un is


2 2 4 1
un − ∆t∆un = ∆tf (tn ) + un−1 − un−2 .
3 3 3 3

We also need to discretise this in space. With the finite element method,
this yields the variational problem: find un ∈ Vh such that
Z Z
2
un v dx + ∆t ∇un · ∇v dx =
Ω 3
Z Ω Z Z
2 4 1
∆t f (tn )v dx + un−1 v dx − un−2 v dx
3 Ω 3 Ω 3 Ω

for all v ∈ V̂h .

P. E. Farrell (Oxford) Practical VI May 2017 7 / 13


Manifolds
For fun, let’s solve the problem on a two-dimensional manifold in
three-dimensional space.

This is a Gray’s Klein bottle.

P. E. Farrell (Oxford) Practical VI May 2017 8 / 13


Implementation

# Solve the heat equation with BDF2 and Crank-Nicolson

from dolfin import *


from decimal import Decimal

mesh = Mesh("klein.xml.gz")
V = FunctionSpace(mesh, "CG", 1)

u = Function(V) # u_n
u_prevs = [Function(V), Function(V)] # u_{n-1}, u_{n-2}

v = TestFunction(V)

P. E. Farrell (Oxford) Practical VI May 2017 9 / 13


Implementation

# Initial condition
g = interpolate(Expression("sin(x[2])*cos(x[1])", degree=2), V)

T = Decimal("1.0") # final time


t = Decimal("0.0") # current time we are solving for
h = Decimal("0.02") # timestep size
dt = Constant(float(h)) # for use in the form
ntimestep = 0 # number of timesteps solved

u.assign(g) # assign initial guess for solver


u_prevs[0].assign(g) # assign initial condition to u_0

P. E. Farrell (Oxford) Practical VI May 2017 10 / 13


Implementation

def rhs(u, v):


return -inner(grad(u), grad(v))*dx

F_cn = (
u*v*dx
- u_prevs[0]*v*dx
- dt*rhs(0.5*u + 0.5*u_prevs[0], v)
)

F_bdf = (
u*v*dx
- 4.0/3.0 * u_prevs[0]*v*dx
+ 1.0/3.0 * u_prevs[1]*v*dx
- 2.0/3.0 * dt*rhs(u, v)
)

P. E. Farrell (Oxford) Practical VI May 2017 11 / 13


Implementation
output = File("heat.pvd")
output << (u, float(t))
while True:
# Update the time we’re solving for
t += h; print "Solving for time: ", float(t)

# check if we have enough initial data for BDF2


if ntimestep < 1:
solve(F_cn == 0, u)
else:
solve(F_bdf == 0, u)

# Now cycle the variables


u_prevs[1].assign(u_prevs[0])
u_prevs[0].assign(u)

ntimestep += 1
output << (u, float(t))
if t >= T: break
P. E. Farrell (Oxford) Practical VI May 2017 12 / 13
FEniCS 06 Challenge!

Run the code and look at the solution.

Change the code to use BDF3. (Initialize with two steps of


Crank-Nicolson.)

Then change the code to use BDF4. (Initialize with Crank-Nicolson and
BDF3.)

P. E. Farrell (Oxford) Practical VI May 2017 13 / 13


Mixed problems: the Stokes equations

Patrick Farrell

Oxford

May 2017

P. E. Farrell (Oxford) Practical VII May 2017 1 / 11


P. E. Farrell (Oxford) Practical VII May 2017 2 / 11
George Stokes

I Born in Sligo, Ireland


I Lucasian Professor of Mathematics,
Cambridge, 1849–1903
I Member of Parliament, 1887–1892
I President of the Royal Society,
1885–1890
I Fundamental contributions to
mathematics and physics.
I No one knows he is Irish.

P. E. Farrell (Oxford) Practical VII May 2017 3 / 11


The Stokes equations
We consider the stationary Stokes equations: find the velocity u and the
pressure p such that

−∇ · (2ν(u) − p I) = f in Ω
∇ · u = 0 in Ω
1
∇u + (∇u)T and with boundary conditions

where (u) = 2

u = 0 on ∂ΩD
−(2ν − p I) · n = p0 n on ∂ΩN

If viscosity ν varies with u (or p),

ν = ν(u)

this is a nonlinear system of partial differential equations.


P. E. Farrell (Oxford) Practical VII May 2017 4 / 11
The Stokes equations: variational formulation
If u ∈ V and p ∈ Q, then w = (u, p) ∈ V × Q = W .

Step 1
Multiply by test functions (v, q) ∈ W and integrate first equation by parts:
Z Z Z
2ν(u) · ∇v dx− p∇ · v dx − (2ν(u) − p I) · n · v ds = 0
Ω ZΩ ∂Ω

∇ · u q dx = 0

P. E. Farrell (Oxford) Practical VII May 2017 5 / 11


The Stokes equations: variational formulation
If u ∈ V and p ∈ Q, then w = (u, p) ∈ V × Q = W .

Step 2
Add the equations and incorporate the boundary conditions: find
(u, p) ∈ W = V0 × Q such that
Z Z Z Z
2ν(u) · ∇v dx − p∇ · v dx − ∇ · u q dx + p0 v · n ds = 0
Ω Ω Ω ∂ΩN

for all (v, q) ∈ W = V0 × Q where V0 = {v ∈ V such that v|∂ΩD = 0}.

P. E. Farrell (Oxford) Practical VII May 2017 5 / 11


Step by step: creating mixed function spaces

To create a mixed function space, first make a mixed element:


V = VectorElement("CG", triangle, 2)
Q = FiniteElement("CG", triangle, 1)
W = FunctionSpace(mesh, MixedElement([V, Q]))

You can define functions on mixed spaces and split into components:
w = Function(W)
(u, p) = split(w)

... and arguments:


y = TestFunction(W)
(v, q) = split(y)

Choice of mixed function space is crucial.

P. E. Farrell (Oxford) Practical VII May 2017 6 / 11


Step by step: defining a boundary condition on a subspace

The subspaces of W can be retrieved using sub:


W0 = W.sub(0)

The following code defines a homogenous Dirichlet (boundary) condition


on the first subspace at the part where x0 = 0.
bc = DirichletBC(W.sub(0), (0, 0), "near(x[0], 0.0)")

P. E. Farrell (Oxford) Practical VII May 2017 7 / 11


Stokes: defining the variational form
Given
w = Function(W)
(u, p) = split(w)
(v, q) = split(TestFunction(W))
p0 = ...; nu = ...; n = ...

we can define the form F with


epsilon = sym(grad(u))
F = (
2*nu*inner(epsilon, grad(v))*dx
- div(u)*q*dx
- div(v)*p*dx
+ p0*dot(v, n)*ds
)

dx: integration over cells; ds: integration over exterior (boundary) facets.
P. E. Farrell (Oxford) Practical VII May 2017 8 / 11
Stokes implementation
# Define mesh and geometry
mesh = Mesh("dolphin.xml")
n = FacetNormal(mesh)

# Define Taylor--Hood function space W


V = VectorElement("CG", triangle, 2)
Q = FiniteElement("CG", triangle, 1)
W = FunctionSpace(mesh, MixedElement([V, Q]))

# Define Function and TestFunction(s)


w = Function(W); (u, p) = split(w)
(v, q) = split(TestFunction(W))

# Define viscosity and bcs


nu = Constant(0.2)
p0 = Expression("1.0-x[0]", degree=1)
bcs = DirichletBC(W.sub(0), (0.0, 0.0), "on_boundary && !(near(x[0], 0.0) || near(x[0], 1.0))")

# Define variational form


epsilon = sym(grad(u))
F = (2*nu*inner(epsilon, grad(v)) - div(u)*q - div(v)*p)*dx + p0*dot(v,n)*ds

# Solve problem
solve(F == 0, w, bcs)

# Plot solutions
(u, p) = w.split()
File("velocity.pvd") << u
File("pressure.pvd") << p

P. E. Farrell (Oxford) Practical VII May 2017 9 / 11


A confusion: two kinds of splits

There are two kinds of splits.

This creates a view:


(u, p) = split(w)

u and p point to parts of w’s memory. Use this in your mixed form.

This creates an independent copy:


(u, p) = w.split()

u and p are independent of w. Use this to plot.

P. E. Farrell (Oxford) Practical VII May 2017 10 / 11


FEniCS 07 Challenge!

Solve the Stokes problem on Ω defined by the dolphin.xml mesh, defined


by the following data

−∇ · (2ν(u) − p I) = 0 in Ω
∇ · u = 0 in Ω
−(2ν(u) − p I) · n = p0 n on ∂ΩN = {(x0 , x1 )| x0 = 0 or x0 = 1}
p 0 = 1 − x0
u = 0 on ∂ΩD = ∂Ω\∂ΩN

I Compute and plot the solutions for a constant viscosity ν = 0.2.


I Use this solution as initial guess for the nonlinear viscosity

ν = ν(u) = 0.5(∇u : ∇u)1/(2(k−1)) , k = 4.

Compute and plot the solutions.


P. E. Farrell (Oxford) Practical VII May 2017 11 / 11
Hyperelasticity

Patrick Farrell

Oxford

May 2017

P. E. Farrell (Oxford) Practical VIII May 2017 1/8


Static hyperelasticity

−∇ · P = B in Ω
u=g on ΓD
P ·n=T on ΓN

I u is the displacement (vector-valued)


I P = P (u) is the first Piola–Kirchhoff stress tensor
I B is a given body force per unit volume
I g is a given boundary displacement
I T is a given boundary traction

P. E. Farrell (Oxford) Practical VIII May 2017 2/8


Variational problem

Multiply by a test function v ∈ V̂ and integrate by parts:


Z Z Z
− ∇ · (P · v) dx = P : ∇v dx − (P · n) · v ds
Ω Ω ∂Ω
Note that v = 0 on ΓD and P · n = T on ΓN

Find u ∈ V such that


Z Z Z
P : ∇v dx = B · v dx + T · v ds
Ω Ω ΓN

for all v ∈ V̂ .

P. E. Farrell (Oxford) Practical VIII May 2017 3/8


Stress–strain relations

I F = I + ∇u is the deformation gradient


I C = F > F is the right Cauchy–Green tensor
I E = 12 (C − I) is the Green–Lagrange strain tensor
I W = W (E) is the strain energy density
∂W
I Sij = ∂Eij is the second Piola–Kirchhoff stress tensor
I P = F S is the first Piola–Kirchhoff stress tensor
St. Venant–Kirchhoff strain energy function:
λ
W (E) = (tr(E))2 + µ tr(E 2 )
2

P. E. Farrell (Oxford) Practical VIII May 2017 4/8


Useful FEniCS tools (I)

Loading a mesh from a file:


mesh = Mesh("whatever.xml")

Vector-valued function spaces:


V = VectorFunctionSpace(mesh, "Lagrange", 1)

Vector-valued Constants:
g = Constant((0, 0, -9.81)) # accel. due to gravity

P. E. Farrell (Oxford) Practical VIII May 2017 5/8


Useful FEniCS tools (II)

Defining subdomains/boundaries:
MyBoundary = CompiledSubDomain("near(x[0], 0.0) && on_boundar

Marking boundaries:
my_boundary_1 = MyBoundary1()
my_boundary_2 = MyBoundary2()
boundaries = FacetFunction("size_t", mesh)
boundaries.set_all(0)
my_boundary_1.mark(boundaries, 1)
my_boundary_2.mark(boundaries, 2)
ds = Measure("ds", subdomain_data=boundaries)
F = ...*ds(0) + ...*ds(1)

P. E. Farrell (Oxford) Practical VIII May 2017 6/8


Useful FEniCS tools (III)

Computing derivatives of expressions:


I = Identity(3)
F = I + grad(u)
C = F.T * F
...
E = variable(...)
W = ...
S = diff(W, E)
P = F*S

Computing functionals of a solution:


J = assemble(u[0]*dx) / assemble(Constant(1)*dx(mesh))

P. E. Farrell (Oxford) Practical VIII May 2017 7/8


FEniCS 08 Challenge!

Compute the deflection of a regular 10 × 2 LEGO brick. Use the


St. Venant–Kirchhoff model and assume that the LEGO brick is made of
PVC plastic. The LEGO brick is subject to gravity of size g = −9.81 m/s2
and a downward traction of size 5000 N/m2 at its right surface.

g = −9.81m/s2

T = 5000 N/m2

Compute the average value of the displacement in the z-direction.

P. E. Farrell (Oxford) Practical VIII May 2017 8/8


The obstacle problem

Patrick Farrell

Oxford

May 2017

P. E. Farrell (Oxford) Practical IX May 2017 1/6


Obstacle problems

Suppose an elastic membrane is attached to a flat wire frame which


encloses a region Ω of the plane. Suppose this membrane is subject to a
distributed load f (x, y). Then the equilibrium position z = u(x, y) satisfies

−∇2 u = f,
u = 0 on ∂Ω.

P. E. Farrell (Oxford) Practical IX May 2017 2/6


Obstacle problems

Suppose an elastic membrane is attached to a flat wire frame which


encloses a region Ω of the plane. Suppose this membrane is subject to a
distributed load f (x, y). Then the equilibrium position z = u(x, y) satisfies

−∇2 u = f,
u = 0 on ∂Ω.

Now suppose that an obstacle is placed underneath the membrane. The


obstacle z = ψ(x, y) is continuous, differentiable and ψ|∂Ω ≤ 0. The task
is to find a region R and solution u such that u coincides with ψ on R,
and u satisfies the PDE on Ω \ R.

P. E. Farrell (Oxford) Practical IX May 2017 2/6


Obstacle problem

P. E. Farrell (Oxford) Practical IX May 2017 3/6


Obstacle problem

P. E. Farrell (Oxford) Practical IX May 2017 3/6


Variational formulation

Let

Kψ = {v ∈ H01 (Ω) | v ≥ φ}.


Then the variational formulation is a variational inequality: find u ∈ Kψ
such that
Z Z
∇u · ∇(v − u) ≥ f (v − u) ∀ v ∈ Kψ .
Ω Ω

To solve this, we will reformulate the problem as a nonsmooth rootfinding


problem.

P. E. Farrell (Oxford) Practical IX May 2017 4/6


Nonsmooth reformulation

Step 1

−∇2 u ≥ f
u ≥ ψ.

P. E. Farrell (Oxford) Practical IX May 2017 5/6


Nonsmooth reformulation

Step 1

−∇2 u ≥ f
u ≥ ψ.

Step 2

−∇2 u − λ = f
u − ψ ≥ 0, λ ≥ 0, λ(u − ψ) = 0.

P. E. Farrell (Oxford) Practical IX May 2017 5/6


Nonsmooth reformulation

Step 1

−∇2 u ≥ f
u ≥ ψ.

Step 2

−∇2 u − λ = f
u − ψ ≥ 0, λ ≥ 0, λ(u − ψ) = 0.

Step 3

−∇2 u − λ = f
λ − max(λ − (u − ψ), 0) = 0.

P. E. Farrell (Oxford) Practical IX May 2017 5/6


FEniCS 09 Challenge!

Solve the Poisson obstacle problem with Ω = [−1, 1]2 , f = −10 and

−0.2 if x ∈ [−1, −0.5),



−0.4 if x ∈ [−0.5, 0),
ψ(x, y) =


 −0.6 if x ∈ [0, 0.5),

−0.8 if x ∈ [0.5, 1].

P. E. Farrell (Oxford) Practical IX May 2017 6/6


Eigenvalue problems

Patrick Farrell

Oxford

May 2017

P. E. Farrell (Oxford) Practical X May 2017 1/9


Eigenvalue problems

We seek eigenvalues of the Laplacian with Dirichlet boundary conditions.


Find u 6= 0, λ ∈ R such that

−∆u = λu in Ω,
u = 0 on ∂Ω.

P. E. Farrell (Oxford) Practical X May 2017 2/9


Variational formulation

As usual, we multiply by a test function and integrate by parts: find


0 6= u ∈ H01 (Ω), λ ∈ R such that
Z Z
∇u · ∇v dx = λ uv dx ∀ v ∈ H01 (Ω).
Ω Ω
To solve this, we will assemble matrices corresponding to the two bilinear
forms.

P. E. Farrell (Oxford) Practical X May 2017 3/9


TestFunctions and TrialFunctions
Consider the code

a = assemble(inner(grad(u), grad(v))*dx)

Three cases:

P. E. Farrell (Oxford) Practical X May 2017 4/9


TestFunctions and TrialFunctions
Consider the code

a = assemble(inner(grad(u), grad(v))*dx)

Three cases:

If u is a Function and v is a Function, a ∈ R.

P. E. Farrell (Oxford) Practical X May 2017 4/9


TestFunctions and TrialFunctions
Consider the code

a = assemble(inner(grad(u), grad(v))*dx)

Three cases:

If u is a Function and v is a TestFunction, a ∈ Rn , with


Z
ai = ∇u · ∇φi .

P. E. Farrell (Oxford) Practical X May 2017 4/9


TestFunctions and TrialFunctions
Consider the code

a = assemble(inner(grad(u), grad(v))*dx)

Three cases:

If u is a TrialFunction and v is a TestFunction, a ∈ Rn×n , with


Z
aij = ∇φi · ∇φj .

P. E. Farrell (Oxford) Practical X May 2017 4/9


Assembling a matrix

u = TrialFunction(V)
v = TestFunction(V)

dummy = inner(Constant(1), v)*dx


bc = DirichletBC(V, 0, "on_boundary")

a = inner(grad(u), grad(v))*dx
asm = SystemAssembler(a, dummy, bc)
A = PETScMatrix(); asm.assemble(A)

P. E. Farrell (Oxford) Practical X May 2017 5/9


Assembling a matrix

b = inner(u, v)*dx
asm = SystemAssembler(b, dummy) # no bc
B = PETScMatrix(); asm.assemble(B)
bc.zero(B)

P. E. Farrell (Oxford) Practical X May 2017 6/9


Constructing the eigensolver

solver = SLEPcEigenSolver(A, B)
solver.parameters["solver"] = "krylov-schur"
solver.parameters["spectrum"] = "target magnitude"
solver.parameters["problem_type"] = "gen_hermitian"
solver.parameters["spectral_transform"] = "shift-and-invert"
solver.parameters["spectral_shift"] = 10.
solver.solve(1)

P. E. Farrell (Oxford) Practical X May 2017 7/9


Fetching the data

eigenmodes = File("eigenmodes.pvd")
eigenfunction = Function(V, name="Eigenfunction")
for i in range(solver.get_number_converged()):
(r, _, rv, _) = solver.get_eigenpair(i)

eigenfunction.vector().zero()
eigenfunction.vector().axpy(1, rv)
eigenmodes << eigenfunction

P. E. Farrell (Oxford) Practical X May 2017 8/9


FEniCS 10 Challenge!

Solve the eigenvalue problem for the Laplacian on the L-shaped domain.

Plot the first eigenmode. Does it look familiar?

P. E. Farrell (Oxford) Practical X May 2017 9/9


PDE-constrained optimisation problems

Patrick Farrell

Oxford

May 2017

P. E. Farrell (Oxford) Practical XI May 2017 1 / 13


What is PDE-constrained optimisation?

Optimisation problems where at least one constraint is a partial differential


equation.
Applications

I Shape and topology optimisation (e.g. optimal shape of a wing)


I Data assimilation (e.g. weather prediction)
I Inverse problems (e.g. petroleum exploration)
I ...

P. E. Farrell (Oxford) Practical XI May 2017 2 / 13


Hello World of PDE-constrained optimisation

We will solve an optimisation problem involving the Poisson equation:

Z Z
1 2 α
min (u − ud ) dx + m2 dx
u,m 2 Ω 2 Ω
subject to
−∆u = m in Ω
u = 0 on ∂Ω

This problem can be physically interpreted as: find the heating/cooling


term m for which u best approximates the desired heat distribution ud .

The regularisation term in the functional ensures existence and uniqueness


for α > 0.
P. E. Farrell (Oxford) Practical XI May 2017 3 / 13
The canonical abstract form

min J (u, m)
u,m

subject to:
F(u, m) = 0,

with
I the objective functional J .
I the parameter m.
I the PDE operator F with solution u ∈ U, parametrised by m ∈ M.

P. E. Farrell (Oxford) Practical XI May 2017 4 / 13


Oneshot solution strategy

We form the Lagrangian L:

L(u, λ, m) = J (u, m) + λ∗ F (u, m)

Optimality conditions (Karush-Kuhn-Tucker): ∇L = 0 at an optimum:

dL dL dL
= 0, = 0, = 0.
du dλ dm

Oneshot approach: solve these three (coupled, often nonlinear) PDEs


together.

P. E. Farrell (Oxford) Practical XI May 2017 5 / 13


Comments on oneshot approach

The oneshot approach can be extremely fast, but very difficult to


converge. (Very difficult to ensure convergence of Newton’s method, and
to solve the resulting linear systems.)

Oneshot approaches are mainly employed for steady PDEs; for


time-dependent PDEs the reduced approach is usually much faster.

For help in implementing the reduced approach with FEniCS, see


dolfin-adjoint: http://dolfin-adjoint.org

P. E. Farrell (Oxford) Practical XI May 2017 6 / 13


Forming the Lagrangian

Z Z Z
1 2 α 2
L(u, λ, m) = (u − ud ) dx + m dx + ∇λ · ∇u − λm dx
2 Ω 2 Ω Ω

Z = VectorFunctionSpace(mesh, "Lagrange", 1, dim=3)


z = Function(Z)
(u, lmbd, m) = split(z)

L = (0.5*inner(u-ud, u-ud)*dx
+ 0.5*alpha*inner(m, m)*dx
+ inner(grad(u), grad(lmbda))*dx
- m*lmbda*dx)

P. E. Farrell (Oxford) Practical XI May 2017 7 / 13


KKT conditions

−∇2 u = m
−∇2 λ = u − ud
αm = λ

F = derivative(L, z, TestFunction(Z))

bcs = [DirichletBC(Z.sub(0), 0, "on_boundary"),


DirichletBC(Z.sub(1), 0, "on_boundary")]
solve(F == 0, z, bcs)

P. E. Farrell (Oxford) Practical XI May 2017 8 / 13


FEniCS 11 Challenge, part A!

Solve the mother problem on Ω = [0, 1]2 with α = 10−7 , and


(
1 if (x, y) ∈ [0, 0.5]2
ud (x, y) =
0 otherwise .

Then vary the strength of regularisation parameter over


10−3 , 10−4 , . . . , 10−9 , and plot the solutions.

P. E. Farrell (Oxford) Practical XI May 2017 9 / 13


FEniCS 11 Challenge, part B!

We consider the following optimal control problem, after Ito and Kunisch:
1 α
min ku − ud k2L2 (Ω) + kmk2L2 (Ω)
u,m 2 2
2 3
subject to −∇ u + u − u = 0 on Ω,
∇u · n = m on Γ.
This Ginzburg-Landau PDE arises in superconductivity. Solve this optimal
control problem with Ω = [0, 1] × [0, 2], α = 10−7 , ud = 3.

P. E. Farrell (Oxford) Practical XI May 2017 10 / 13


Course accreditation

Choose a

I nonlinear BVP I variational inequality


I time-dependent IBVP I eigenvalue problem
I optimisation problem

that we haven’t done in the course.

P. E. Farrell (Oxford) Practical XI May 2017 11 / 13


Course accreditation

Write a report (ten to twenty pages) describing


(1) Introduction and motivation
(2) Strong statement of the problem
(3) Variational statement of the problem
(4) FEniCS implementation
(5) Results

Submit the report (.pdf) and code (.zip) by 3 July.

P. E. Farrell (Oxford) Practical XI May 2017 12 / 13


Possible ideas

Sources of inspiration:
(1) Your research (preferred)!
(2) The PDE coffee table book.
(3) SIAM News.

P. E. Farrell (Oxford) Practical XI May 2017 13 / 13

You might also like