Lecture 4
Lecture 4
Anders Logg
1 / 31
Course outline
Sunday
L1 Introduction to FEM
Monday
L2 Fundamentals of continuum mechanics (I)
L3 Fundamentals of continuum mechanics (II)
L4 Introduction to FEniCS
Tuesday
L5 Solid mechanics
L5 Static hyperelasticity in FEniCS
L5 Dynamic hyperelasticity in FEniCS
Wednesday
L5 Fluid mechanics
L5 Navier–Stokes in FEniCS
2 / 31
What is FEniCS?
3 / 31
FEniCS is an automated programming
environment for differential equations
• C++/Python library
• Initiated 2003 in Chicago
• 1000–2000 monthly downloads
• Part of Debian and Ubuntu
• Licensed under the GNU LGPL
http://fenicsproject.org/
Collaborators
Simula Research Laboratory, University of Cambridge,
University of Chicago, Texas Tech University, KTH Royal
Institute of Technology, . . .
4 / 31
FEniCS is automated FEM
• Automated generation of basis functions
• Automated evaluation of variational forms
• Automated finite element assembly
• Automated adaptive error control
5 / 31
What has FEniCS been used for?
6 / 31
Computational hemodynamics
u̇ + u · ∇u − ∇ · σ(u, p) = f
∇·u=0
# Pressure correction
a2 = inner ( grad ( q ) , k * grad ( p ) ) * dx
L2 = inner ( grad ( q ) , k * grad ( p0 ) ) * dx -
q * div ( u1 ) * dx
# Velocity correction
a3 = inner (v , u ) * dx
L3 = inner (v , u1 ) * dx + inner (v , k * grad ( p0 -
p1 ) ) * dx
material = StVenantKirchhoff ( [ mu ,
lmbda ] )
return material
10 / 31
Installation
11 / 31
Basic API
12 / 31
Hello World!
−∆u = f in Ω
u = u0 on ∂Ω
13 / 31
Solving PDEs in FEniCS
14 / 31
Deriving a variational problem for Poisson’s
equation
The simple recipe is: multiply the PDE by a test function v and
integrate over Ω:
Z Z
− (∆u)v dx = f v dx
Ω Ω
We find that: Z Z
∇u · ∇v dx = f v dx
Ω Ω
15 / 31
Variational problem for Poisson’s equation
for all v ∈ V̂
The trial space V and the test space V̂ are (here) given by
V = {v ∈ H 1 (Ω) : v = u0 on ∂Ω}
V̂ = {v ∈ H 1 (Ω) : v = 0 on ∂Ω}
16 / 31
Discrete variational problem for Poisson’s
equation
We approximate the continuous variational problem with a
discrete variational problem posed on finite dimensional
subspaces of V and V̂ :
Vh ⊂ V
V̂h ⊂ V̂
17 / 31
Canonical variational problem
18 / 31
A test problem
u(x, y) = 1 + x2 + 2y 2
19 / 31
Implementation in FEniCS
mesh = UnitSquare (6 , 4 )
V = FunctionSpace ( mesh , " Lagrange " , 1 )
u0 = Expression ( " 1 + x [ 0 ]* x [ 0 ] + 2 * x [ 1 ]* x [ 1 ] " )
bc = DirichletBC (V , u0 , u0_boundary )
f = Constant ( - 6 . 0 )
u = TrialFunction ( V )
v = TestFunction ( V )
a = inner ( grad ( u ) , grad ( v ) ) * dx
L = f * v * dx
u = Function ( V )
solve ( a = = L , u , bc )
20 / 31
Step by step: the first line
21 / 31
Step by step: creating a mesh
mesh = UnitSquare (6 , 4 )
22 / 31
Step by step: creating a function space
23 / 31
Step by step: defining expressions
Next, we define an expression for the boundary value:
help ( Expression )
24 / 31
Step by step: defining boundaries
We next define the Dirichlet boundary:
def u0_boundary ( x ) :
return x [ 0 ] < DOLFIN_EPS or \
x [ 1 ] > 1 . 0 - DOLFIN_EPS
def u0_boundary ( x ) :
return near ( x [ 0 ] , 0 . 0 ) or near ( x [ 1 ] , 1 . 0 )
25 / 31
Step by step: defining a boundary condition
bc = DirichletBC (V , u0 , u0_boundary )
Note that the above line does not yet apply the boundary
condition to all functions in the function space
26 / 31
Step by step: defining the right-hand side
or (more efficiently) as
f = Constant ( - 6 . 0 )
27 / 31
Step by step: defining variational problems
u = TrialFunction ( V )
v = TestFunction ( V )
28 / 31
Step by step: solving variational problems
u = Function ( V )
solve ( a = = L , u , bc )
29 / 31
Step by step: post-processing
The solution and the mesh may be plotted by simply calling:
plot ( u )
plot ( mesh )
interactive ()
30 / 31
The FEniCS challenge!
−∆u = f
31 / 31