FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1...

28
FEniCS Course Lecture 11: A posteriori error estimates and adaptivity Contributors Andr´ e Massing Marie Rognes Anders Logg 1 / 24

Transcript of FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1...

Page 1: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

FEniCS CourseLecture 11: A posteriori error estimates andadaptivity

ContributorsAndre MassingMarie RognesAnders Logg

1 / 24

Page 2: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

A priori estimates

If u ∈ Hk+1(Ω) and Vh = P k(Th) then

|||u− uh||| 6 Chk‖u‖Ω,k+1

‖u− uh‖L2(Ω) 6 Chk+1‖u‖Ω,k+1

2 / 24

Page 3: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

A priori estimates

If u ∈ Hk+1(Ω) and Vh = P k(Th) then

|||u− uh||| 6 Chk‖u‖Ω,k+1

‖u− uh‖L2(Ω) 6 Chk+1‖u‖Ω,k+1

10-3 10-2 10-1 100

hmax

10-11

10-10

10-9

10-8

10-7

10-6

10-5

10-4

10-3

10-2

10-1

100

L2

err

or 1

2

1

3

1

4

k = 1k = 2k = 3

2 / 24

Page 4: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

A priori estimates

If u ∈ Hk+1(Ω) and Vh = P k(Th) then

|||u− uh||| 6 Chk‖u‖Ω,k+1

‖u− uh‖L2(Ω) 6 Chk+1‖u‖Ω,k+1

10-3 10-2 10-1 100

hmax

10-8

10-7

10-6

10-5

10-4

10-3

10-2

10-1

100

H1

err

or1

1

1

2

1

3

k = 1k = 2k = 3

2 / 24

Page 5: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Mesh adaptation can yield more accurate results

with less computational resources

3 / 24

Page 6: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Mesh adaptation can yield more accurate results

with less computational resources

4 / 24

Page 7: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Mesh adaptation can yield more accurate results

with less computational resources

4 / 24

Page 8: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Mesh adaptation can yield more accurate results

with less computational resources

4 / 24

Page 9: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Adaptive error control

Estimate error

Indicate

Refine

Solve

5 / 24

Page 10: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Desired properties of error estimators

An error estimator E ∼ ‖u− uh‖ has to be

computable E = E(uh, f)

and should be

reliable |||u− uh||| 6 CE(uh, f)

efficient cE(uh, f) 6 |||u− uh|||

local E(uh, f)2 =∑T∈Th

ρT (uh, f)2

The quality of E(uh, f) is measured by the efficiency index η

η(E(uh, f)) =‖u− uh‖E(uh, f)

If η(E(uh, f))→ 1 as h→ 0, the error estimator isasymptotically exact.

6 / 24

Page 11: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Types of error estimators

• Explicit residual-based error estimators

• Implicit error estimator based on local problems

• Gradient recovery estimators

• Hierarchic error estimators

• Goal-oriented error estimators

7 / 24

Page 12: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Explicit residual based error estimators

Model problem

−∆u = f in Ω

u = 0 on ∂Ω

Residual equation

R(uh, f ; v) = (∇(u− uh),∇v) = (f,∇v)− (∇uh,∇v) ∀ v ∈ H10 (Ω)

Recall Galerkin orthogonality

R(uh, f ; vh) = 0 ∀ vh ∈ VhInterpolation operator πh : V → Vh

R(v) = (f, v − πhv)− (∇uh,∇(v − πhv))

=∑T∈Th

(f + ∆uh, v − πhv)T −∑T∈Th

(∇uh · nT , v − πh)∂T

=∑T∈Th

(f + ∆uh, v − πhv)T −∑

F∈∂F i

([∇uh · nT ], v − πh)F

8 / 24

Page 13: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Explicit residual based error estimators

Starting from

R(v) =∑T∈Th

(f + ∆uh, v − πhv)T −∑

F∈∂Fi

([∇uh · nT ], v − πh)F

and using the quasi-interpolant by Clement, which satisfies

‖v − πh‖0,T ≤ C1hT ‖v‖ω(T )

‖v − πh‖0,F ≤ C2h1/2F ‖v‖ω(F ),

one obtains

|R(v)| 6 C‖v‖1

∑T∈Th

h2T ‖f + ∆uh‖2 +

∑F∈Fi

hE‖[∇uh] · n‖2F

1/2

9 / 24

Page 14: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Explicit, residual based error estimators

Define

Element residual rT := f + ∆|TFacet residual rF := [∇uh · n]|T

Error indicators ρ2T := h2

T ‖rT ‖2T +1

2

∑F∈∂T

hF ‖rF ‖2T

Poincare inequality gives ‖v‖1 ∼ ‖∇v‖ an thus

‖u− uh‖1 6 supv∈V

(∇(u− uh),∇v)

‖v‖1= sup

v∈V

|R(v)|‖v‖1

6 C( ∑T∈Th

ρ2T

)1/2

proving the reliability of the error estimator defined by ρT T .

10 / 24

Page 15: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Goal-oriented error control

11 / 24

Page 16: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

What is goal-oriented error control?

The scientist’s viewpoint

Shear stress at vessel wall?

[Valen-Sendstad ’08]

12 / 24

Page 17: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

What is goal-oriented error control?

The mathematician’s viewpoint

Input

• PDE: find u ∈ V such that a(u, v) = L(v) ∀ v ∈ V

• Quantity of interest/Goal: M : V → R

• Tolerance: ε > 0

Challenge

Find Vh ⊂ V such that |M(u)−M(uh)| < ε where uh ∈ Vh is

determined by

a(uh, v) = L(v) ∀ v ∈ Vh

13 / 24

Page 18: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

The error measured in the goal is the residual of

the dual solution

1 Recall residual R(v) := L(v)− a(uh, v)

2 Introduce dual problem

Find z ∈ V : a∗(z, v) =M(v) ∀ v ∈ V

3 Dual solution + residual =⇒ error

M(u)−M(uh) =M(u− uh) = a∗(z, u− uh) = a(u− uh, z)

= L(z)− a(uh, z) = R(z) = R(z − zh)

4 A good dual approximation zh gives computable error

estimate ηh = r(zh)

5 Error indicators ... ?14 / 24

Page 19: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

The dual-weighted residual method for the

Poisson problem

Start with representation

|M(u)−M(uh)| = |R(u− uh, z − zh)|.

Integrate by parts as for the classical energy-norm estimategives

|M(u)−M(uh)| 6∑T∈Th

ρTωT ,

where ρT is resembles the standard element residual

ρT = ‖rT ‖T + h1/2T ‖rF ‖∂T

and ωT is a weight

ωT = ‖z − zh‖T + h1/2T ‖z − zh‖∂T

derived from the dual solution z.15 / 24

Page 20: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

What is automated goal-oriented error control?

M

F

ε

M(uh) ≈M(u)± ε

16 / 24

Page 21: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

What is automated goal-oriented error control?

M

a, L

ε

M(uh) ≈M(u)± ε

FEniCS/DOLFIN

pde = VariationalProblem(a, L, bc)

pde.solve(u, tol , M)

17 / 24

Page 22: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Automated goal-oriented adaptivity – A

complete example

from dolfin import *

# Create mesh and define function space

mesh = UnitSquareMesh(8, 8)

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

# Define boundary condition

u0 = Function(V)

bc = DirichletBC(V, u0, "x[0] < DOLFIN_EPS ||

x[0] > 1.0 - DOLFIN_EPS")

18 / 24

Page 23: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Automated goal-oriented adaptivity – A

complete example

Define variational problem:

u = TrialFunction(V)

v = TestFunction(V)

f = Expression("10*exp(-(pow(x[0] - 0.5, 2) +

pow(x[1] - 0.5, 2)) / 0.02)",

degree=1)

g = Expression("sin(5*x[0])", degree=1)

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

L = f*v*dx + g*v*ds

19 / 24

Page 24: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Automated goal-oriented adaptivity – A

complete example

Define function for the solution:

u = Function(V)

Define goal functional (quantity of interest) and tolerance:

M = u*dx

tol = 1.e-5

20 / 24

Page 25: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Automated goal-oriented adaptivity – A

complete example

Solve equation a = L with respect to u and the given boundaryconditions, such that the estimated error (measured in M) isless than tol

solver_parameters = "error_control":

"dual_variational_solver":

"linear_solver": "cg"

solve(a == L, u, bc , tol=tol , M=M,

solver_parameters=solver_parameters)

21 / 24

Page 26: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Automated goal-oriented adaptivity – A

complete exampleAlternative, more verbose version (+ illustrating how to setparameters)

problem = LinearVariationalProblem(a, L, u, bc)

solver = AdaptiveLinearVariationalSolver(problem)

solver.parameters["error_control"]["dual_variational_solver"]["linear_solver"]

= "cg"

solver.solve(tol , M)

Extract solutions on coarsest and finest mesh:

plot(u.root_node (), title="Solution on initial mesh")

plot(u.leaf_node (), title="Solution on final mesh")

interactive ()

22 / 24

Page 27: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

Useful FEniCS tools

Uniform mesh refinement:

mesh = refine(mesh)

Adaptive mesh refinement:

cell_markers = CellFunction("bool", mesh)

cell_markers.set_all(False)

for c in cells(mesh):

if <something>:

cell_markers[c] = True

mesh = refine(mesh , cell_markers)23 / 24

Page 28: FEniCS Course€¦ · 10-11 10-10 10-9 10-8 10-7 10-6 10-5 10-4 10-3 10-2 10-1 100 L 2 e r r o r 1 2 1 3 1 4 k = 1 k = 2 k = 3 2 / 24. A priori estimates If u2Hk+1() and V h = P k(T

The FEniCS challenge!

Compute the solution of the Poisson problem on the unit square with

right-hand side f = e−100(x2+y2) and homogeneous Dirichlet boundary

conditions.

Try to compute the solution as accurately as possible, using adaptive

mesh refinement. Use any one of the techniques described in the

lecture notes, or invent your own refinement strategy.

To check your answer, plot the L2 norm as function of the refinement

level. You should get an answer that approaches 8.888e-05.

Hint: Refine in the lower left corner.

The student who computes the most accurate solution will be rewarded

with an exclusive FEniCS surprise!

24 / 24