| 1 |
Auto adaptive Poisson equation |
| 2 |
============================== |
| 3 |
|
| 4 |
This demo is implemented in a single Python file, |
| 5 |
:download:`demo_auto-adaptive-poisson.py`, which contains both the |
| 6 |
variational forms and the solver. |
| 7 |
|
| 8 |
In this demo we will use goal oriented adaptivity and error control |
| 9 |
which applies a duality technique to derive error estimates taken |
| 10 |
directly from the computed solution which then are used to weight |
| 11 |
local residuals. To this end, we derive an :math:`\textit{a |
| 12 |
posteriori}` error estimate and error indicators. We define a goal |
| 13 |
functional :math:`\mathcal{M} : V \rightarrow \mathbb{R}`, which |
| 14 |
expresses the localized physical properties of the solution of a |
| 15 |
simulation. The objective of goal oriented adaptive error control is |
| 16 |
to minimize computational work to obtain a given level of accuracy in |
| 17 |
:math:`\mathcal{M}`. |
| 18 |
|
| 19 |
We will thus illustrate how to: |
| 20 |
|
| 21 |
* Solve a linear partial differential equation with automatic adaptive |
| 22 |
mesh refinement |
| 23 |
* Define a goal functional |
| 24 |
* Use :py:class:`AdaptiveLinearVariationalSolver |
| 25 |
<dolfin.cpp.fem.AdaptiveLinearVariationalSolver>` |
| 26 |
|
| 27 |
The two solutions for u in this demo will look as follows, where the |
| 28 |
first is the unrefined while the second is the refined solution: |
| 29 |
|
| 30 |
.. image:: u_unrefined.png |
| 31 |
:scale: 75 % |
| 32 |
|
| 33 |
.. image:: u_refined.png |
| 34 |
:scale: 75 % |
| 35 |
|
| 36 |
|
| 37 |
Equation and problem definition |
| 38 |
------------------------------- |
| 39 |
|
| 40 |
The Poisson equation is the canonical elliptic partial differential |
| 41 |
equation. For a domain :math:`\Omega \subset \mathbb{R}^n` with |
| 42 |
boundary :math:`\partial \Omega = \Gamma_{D} \cup \Gamma_{N}`, the |
| 43 |
Poisson equation with particular boundary conditions reads: |
| 44 |
|
| 45 |
.. math:: |
| 46 |
|
| 47 |
- \nabla^{2} u &= f \quad {\rm in} \ \Omega, \\ |
| 48 |
u &= 0 \quad {\rm on} \ \Gamma_{D}, \\ |
| 49 |
\nabla u \cdot n &= g \quad {\rm on} \ \Gamma_{N}. \\ |
| 50 |
|
| 51 |
Here, :math:`f` and :math:`g` are input data and n denotes the outward |
| 52 |
directed boundary normal. The variational form of Poisson equation |
| 53 |
reads: find :math:`u \in V` such that |
| 54 |
|
| 55 |
.. math:: |
| 56 |
|
| 57 |
a(u, v) = L(v) \quad \forall \ v \in \hat{V}, |
| 58 |
|
| 59 |
which we will call the continous primal problem, where :math:`V`, |
| 60 |
:math:`\hat{V}` are the trial- and test spaces and |
| 61 |
|
| 62 |
.. math:: |
| 63 |
|
| 64 |
a(u, v) &= \int_{\Omega} \nabla u \cdot \nabla v \, {\rm d} x, \\ |
| 65 |
L(v) &= \int_{\Omega} f v \, {\rm d} x + \int_{\Gamma_{N}} g v \, {\rm d} s. |
| 66 |
|
| 67 |
The expression :math:`a(u, v)` is the bilinear form and :math:`L(v)` |
| 68 |
is the linear form. It is assumed that all functions in :math:`V` |
| 69 |
satisfy the Dirichlet boundary conditions (:math:`u = 0 \ {\rm on} \ |
| 70 |
\Gamma_{D}`). |
| 71 |
|
| 72 |
The above definitions is that of the continuous problem. In the actual |
| 73 |
computer implementation we use a descrete formulation which reads: |
| 74 |
find :math:`u \in V_h` such that |
| 75 |
|
| 76 |
.. math:: |
| 77 |
|
| 78 |
a(u_h, v) = L(v) \quad \forall \ v \in \hat{V}_h. |
| 79 |
|
| 80 |
We will refer to the above equation as the discrete primal |
| 81 |
problem. Here, :math:`V_h` and :math:`\hat{V_h}` are finite |
| 82 |
dimensional subspaces. |
| 83 |
|
| 84 |
The weak residual is defined as |
| 85 |
|
| 86 |
.. math:: |
| 87 |
|
| 88 |
r(v) = L(v) - a(u_h, v). |
| 89 |
|
| 90 |
By the Galerkin orthogonality, we have |
| 91 |
|
| 92 |
.. math:: |
| 93 |
|
| 94 |
r(v) = L(v) - a(u_h, v) = a(u_h, v) - a(u_h, v) = 0\,\, \forall v \in \hat{V}_{h}, |
| 95 |
|
| 96 |
which means that the residual vanishes for all functions in |
| 97 |
:math:`\hat{V}_{h}`. This property is used further in the derivation |
| 98 |
of the error estimates. wish to compute a solution :math:`u_h` to the |
| 99 |
discrete primal problem defined above such that for a given tolerance |
| 100 |
:math:`\mathrm{TOL}` we have |
| 101 |
|
| 102 |
.. math:: |
| 103 |
|
| 104 |
\eta = \left| \mathcal{M}(u_h) - \mathcal{u_h} \right| \leq \mathrm{TOL}. |
| 105 |
|
| 106 |
Next we derive an :math:`\textit{a posteriori}` error estimate by |
| 107 |
defining the discrete dual variational problem: find :math:`z_h \in |
| 108 |
V_h^*` such that |
| 109 |
|
| 110 |
.. :math:: |
| 111 |
|
| 112 |
a^*(z_h,v) = \mathcal{v}, \quad \forall v \in \hat{V}_h^*. |
| 113 |
|
| 114 |
Here :math:`V^*, \hat{V}_h^*` are the dual trial- and test spaces and |
| 115 |
:math:`a^* : V^* \times \hat{V}^* \rightarrow \mathbb{R}` is the |
| 116 |
adjoint of :math:`a` such that :math:`a^*(v,w) = a(w,v)`. We find that |
| 117 |
|
| 118 |
.. math:: |
| 119 |
|
| 120 |
\mathcal{M}(u - \mathcal{u_h}) = a^*(z, u - u_h) = a(u - u_h, z) = a(u,z) - a(u_h,z) = L(z) - a(u_h,z) = r(z) |
| 121 |
|
| 122 |
and by Galerkin orthogonality we have :math:`r(z) = r(z - v_h)\,\, |
| 123 |
\forall v_h \in \hat{V}_h`. Note that the residual vanishes if |
| 124 |
:math:`z \in \hat{V}_h^*` and has to either be approximated in a |
| 125 |
higher order element space or one may use an extrapolation. The choice |
| 126 |
of goal functional depends on what quantity you are interested in. |
| 127 |
Here, we take the goal functional to be defined as |
| 128 |
|
| 129 |
.. math:: |
| 130 |
|
| 131 |
\mathcal{M}(u) = \int_{\Omega} u dx. |
| 132 |
|
| 133 |
|
| 134 |
We use :math:`D\ddot{o}rfler` marking as the mesh marking procedure. |
| 135 |
|
| 136 |
In this demo, we shall consider the following definitions of the input |
| 137 |
functions, the domain, and the boundaries: |
| 138 |
|
| 139 |
* :math:`\Omega = [0,1] \times [0,1]\,` (a unit square) |
| 140 |
* :math:`\Gamma_{D} = \{(0, y) \cup (1, y) \subset \partial \Omega\}\,` (Dirichlet boundary) |
| 141 |
* :math:`\Gamma_{N} = \{(x, 0) \cup (x, 1) \subset \partial \Omega\}\,` (Neumann boundary) |
| 142 |
* :math:`g = \sin(5x)\,` (normal derivative) |
| 143 |
* :math:`f = 10\exp(-((x - 0.5)^2 + (y - 0.5)^2) / 0.02)\,` (source term) |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
Implementation |
| 148 |
-------------- |
| 149 |
|
| 150 |
This description goes through the implementation (in |
| 151 |
:download:`demo_auto-adaptive-poisson.py`) of a solver for the above |
| 152 |
described Poisson equation step-by-step. |
| 153 |
|
| 154 |
First, the necessary modules are imported:: |
| 155 |
|
| 156 |
import matplotlib.pyplot as plt |
| 157 |
from dolfin import * |
| 158 |
|
| 159 |
We begin by defining a mesh of the domain and a finite element |
| 160 |
function space V relative to this mesh. We used the built-in mesh |
| 161 |
provided by the class :py:class:`UnitSquareMesh |
| 162 |
<dolfin.cpp.mesh.UnitSquareMesh>`. In order to create a mesh |
| 163 |
consisting of 8 x 8 squares with each square divided into two |
| 164 |
triangles, we do as follows:: |
| 165 |
|
| 166 |
# Create mesh and define function space |
| 167 |
mesh = UnitSquareMesh(8, 8) |
| 168 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 169 |
|
| 170 |
The second argument to :py:class:`FunctionSpace |
| 171 |
<dolfin.cpp.function.FunctionSpace>`, "Lagrange", is the finite |
| 172 |
element family, while the third argument specifies the polynomial |
| 173 |
degree. Thus, in this case, our space V consists of first-order, |
| 174 |
continuous Lagrange finite element functions (or in order words, |
| 175 |
continuous piecewise linear polynomials). |
| 176 |
|
| 177 |
Next, we want to consider the Dirichlet boundary condition. In our |
| 178 |
case, we want to say that the points (x, y) such that x = 0 or x = 1 |
| 179 |
are inside on the inside of :math:`\Gamma_D`. (Note that because of |
| 180 |
rounding-off errors, it is often wise to instead specify :math:`x < |
| 181 |
\epsilon` or :math:`x > 1 - \epsilon` where :math:`\epsilon` is a |
| 182 |
small number (such as machine precision).) :: |
| 183 |
|
| 184 |
# Define boundary condition |
| 185 |
u0 = Function(V) |
| 186 |
bc = DirichletBC(V, u0, "x[0] < DOLFIN_EPS || x[0] > 1.0 - DOLFIN_EPS") |
| 187 |
|
| 188 |
Next, we want to express the variational problem. First, we need to |
| 189 |
specify the trial function u and the test function v, both living in |
| 190 |
the function space V. We do this by defining a |
| 191 |
:py:class:`TrialFunction <dolfin.functions.function.TrialFunction>` |
| 192 |
and a :py:class:`TestFunction |
| 193 |
<dolfin.functions.function.TestFunction>` on the previously defined |
| 194 |
:py:class:`FunctionSpace <dolfin.cpp.function.FunctionSpace>` V. |
| 195 |
|
| 196 |
Further, the source f and the boundary normal derivative g are |
| 197 |
involved in the variational forms, and hence we must specify |
| 198 |
these. Both f and g are given by simple mathematical formulas, and can |
| 199 |
be easily declared using the :py:class:`Expression |
| 200 |
<dolfin.cpp.function.Expression>` class. Note that the strings |
| 201 |
defining f and g use C++ syntax since, for efficiency, DOLFIN will |
| 202 |
generate and compile C++ code for these expressions at run-time. |
| 203 |
|
| 204 |
With these ingredients, we can write down the bilinear form a and the |
| 205 |
linear form L (using UFL operators). In summary, this reads:: |
| 206 |
|
| 207 |
# Define variational problem |
| 208 |
u = TrialFunction(V) |
| 209 |
v = TestFunction(V) |
| 210 |
f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", |
| 211 |
degree=1) |
| 212 |
g = Expression("sin(5*x[0])", degree=1) |
| 213 |
a = inner(grad(u), grad(v))*dx() |
| 214 |
L = f*v*dx() + g*v*ds() |
| 215 |
|
| 216 |
Now, we have specified the variational forms and can consider the |
| 217 |
solution of the variational problem. First, we need to define a |
| 218 |
:py:class:`Function <dolfin.cpp.function.Function>` u to represent the |
| 219 |
solution. (Upon initialization, it is simply set to the zero |
| 220 |
function.) A Function represents a function living in a finite element |
| 221 |
function space. :: |
| 222 |
|
| 223 |
# Define function for the solution |
| 224 |
u = Function(V) |
| 225 |
|
| 226 |
Then define the goal functional:: |
| 227 |
|
| 228 |
# Define goal functional (quantity of interest) |
| 229 |
M = u*dx() |
| 230 |
|
| 231 |
Next we specify the error tolerance for when the refinement shall stop:: |
| 232 |
|
| 233 |
# Define error tolerance |
| 234 |
tol = 1.e-5 |
| 235 |
|
| 236 |
Now, we have specified the variational forms and can consider the |
| 237 |
solution of the variational problem. First, we define the |
| 238 |
:py:class:`LinearVariationalProblem |
| 239 |
<dolfin.cpp.fem.LinearVariationalProblem>` function with the arguments |
| 240 |
a, L, u and bc. Next we send this problem to the |
| 241 |
:py:class:`AdaptiveLinearVariationalSolver |
| 242 |
<dolfin.cpp.fem.AdaptiveLinearVariationalSolver>` together with the |
| 243 |
goal functional. Note that one may also choose several adaptations in |
| 244 |
the error control. At last we solve the problem with the defined |
| 245 |
tolerance:: |
| 246 |
|
| 247 |
# Solve equation a = L with respect to u and the given boundary |
| 248 |
# conditions, such that the estimated error (measured in M) is less |
| 249 |
# than tol |
| 250 |
problem = LinearVariationalProblem(a, L, u, bc) |
| 251 |
solver = AdaptiveLinearVariationalSolver(problem, M) |
| 252 |
solver.parameters["error_control"]["dual_variational_solver"]["linear_solver"] = "cg" |
| 253 |
solver.parameters["error_control"]["dual_variational_solver"]["symmetric"] = True |
| 254 |
solver.solve(tol) |
| 255 |
|
| 256 |
solver.summary() |
| 257 |
|
| 258 |
# Plot solution(s) |
| 259 |
plt.figure() |
| 260 |
plot(u.root_node(), title="Solution on initial mesh") |
| 261 |
plt.figure() |
| 262 |
plot(u.leaf_node(), title="Solution on final mesh") |
| 263 |
plt.show() |