| 1 |
Mixed formulation for Poisson equation (C++) |
| 2 |
============================================ |
| 3 |
|
| 4 |
This demo illustrates how to solve Poisson equation using a mixed |
| 5 |
(two-field) formulation. In particular, it illustrates how to |
| 6 |
|
| 7 |
* Use mixed and non-continuous finite element spaces |
| 8 |
* Set essential boundary conditions for subspaces and H(div) spaces |
| 9 |
* Define a (vector-valued) expression using additional geometry information |
| 10 |
|
| 11 |
|
| 12 |
Equation and problem definition |
| 13 |
------------------------------- |
| 14 |
|
| 15 |
An alternative formulation of Poisson equation can be formulated by |
| 16 |
introducing an additional (vector) variable, namely the (negative) |
| 17 |
flux: :math:`\sigma = \nabla u`. The partial differential equations |
| 18 |
then read |
| 19 |
|
| 20 |
.. math:: |
| 21 |
\sigma - \nabla u &= 0 \quad {\rm in} \ \Omega, \\ |
| 22 |
\nabla \cdot \sigma &= - f \quad {\rm in} \ \Omega, |
| 23 |
|
| 24 |
with boundary conditions |
| 25 |
|
| 26 |
.. math:: |
| 27 |
u = u_0 \quad {\rm on} \ \Gamma_{D}, \\ |
| 28 |
\sigma \cdot n = g \quad {\rm on} \ \Gamma_{N}. |
| 29 |
|
| 30 |
The same equations arise in connection with flow in porous media, and |
| 31 |
are also referred to as Darcy flow. |
| 32 |
|
| 33 |
After multiplying by test functions :math:`\tau` and :math:`v`, |
| 34 |
integrating over the domain, and integrating the gradient term by |
| 35 |
parts, one obtains the following variational formulation: find |
| 36 |
:math:`\sigma \in \Sigma` and :math:`v \in V` satisfying |
| 37 |
|
| 38 |
.. math:: |
| 39 |
\int_{\Omega} (\sigma \cdot \tau + \nabla \cdot \tau \ u) \ {\rm d} x |
| 40 |
&= \int_{\Gamma} \tau \cdot n \ u \ {\rm d} s |
| 41 |
\quad \forall \ \tau \in \Sigma, \\ |
| 42 |
|
| 43 |
\int_{\Omega} \nabla \cdot \sigma v \ {\rm d} x |
| 44 |
&= - \int_{\Omega} f \ v \ {\rm d} x |
| 45 |
\quad \forall \ v \in V. |
| 46 |
|
| 47 |
Here :math:`n` denotes the outward pointing normal vector on the |
| 48 |
boundary. Looking at the variational form, we see that the boundary |
| 49 |
condition for the flux (:math:`\sigma \cdot n = g`) is now an |
| 50 |
essential boundary condition (which should be enforced in the function |
| 51 |
space), while the other boundary condition (:math:`u = u_0`) is a |
| 52 |
natural boundary condition (which should be applied to the variational |
| 53 |
form). Inserting the boundary conditions, this variational problem can |
| 54 |
be phrased in the general form: find :math:`(\sigma, u) \in \Sigma_g |
| 55 |
\times V` such that |
| 56 |
|
| 57 |
.. math:: |
| 58 |
|
| 59 |
a((\sigma, u), (\tau, v)) = L((\tau, v)) |
| 60 |
\quad \forall \ (\tau, v) \in \Sigma_0 \times V |
| 61 |
|
| 62 |
where the variational forms :math:`a` and :math:`L` are defined as |
| 63 |
|
| 64 |
.. math:: |
| 65 |
|
| 66 |
a((\sigma, u), (\tau, v)) &= |
| 67 |
\int_{\Omega} \sigma \cdot \tau + \nabla \cdot \tau \ u |
| 68 |
+ \nabla \cdot \sigma \ v \ {\rm d} x \\ |
| 69 |
L((\tau, v)) &= - \int_{\Omega} f v \ {\rm d} x |
| 70 |
+ \int_{\Gamma_D} u_0 \tau \cdot n \ {\rm d} s |
| 71 |
|
| 72 |
and :math:`\Sigma_g = \{ \tau \in H({\rm div}) \text{ such that } \tau \cdot n|_{\Gamma_N} = g \}` |
| 73 |
and :math:`V = L^2(\Omega)`. |
| 74 |
|
| 75 |
To discretize the above formulation, two discrete function spaces |
| 76 |
:math:`\Sigma_h \subset \Sigma` and :math:`V_h \subset V` are needed |
| 77 |
to form a mixed function space :math:`\Sigma_h \times V_h`. A stable |
| 78 |
choice of finite element spaces is to let :math:`\Sigma_h` be the |
| 79 |
Brezzi-Douglas-Marini elements of polynomial order :math:`k` and let |
| 80 |
:math:`V_h` be discontinuous elements of polynomial order :math:`k-1`. |
| 81 |
|
| 82 |
We will use the same definitions of functions and boundaries as in the |
| 83 |
demo for Poisson's equation. These are: |
| 84 |
|
| 85 |
* :math:`\Omega = [0,1] \times [0,1]` (a unit square) |
| 86 |
* :math:`\Gamma_{D} = \{(0, y) \cup (1, y) \in \partial \Omega\}` |
| 87 |
* :math:`\Gamma_{N} = \{(x, 0) \cup (x, 1) \in \partial \Omega\}` |
| 88 |
* :math:`u_0 = 0` |
| 89 |
* :math:`g = \sin(5x)` (flux) |
| 90 |
* :math:`f = 10\exp(-((x - 0.5)^2 + (y - 0.5)^2) / 0.02)` (source term) |
| 91 |
|
| 92 |
With the above input the solution for :math:`u` and :math:`\sigma` will look as |
| 93 |
follows: |
| 94 |
|
| 95 |
.. image:: ../mixed-poisson_u.png |
| 96 |
:scale: 75 |
| 97 |
:align: center |
| 98 |
|
| 99 |
.. image:: ../mixed-poisson_sigma.png |
| 100 |
:scale: 75 |
| 101 |
:align: center |
| 102 |
|
| 103 |
|
| 104 |
Implementation |
| 105 |
-------------- |
| 106 |
|
| 107 |
The implementation is split in two files, a form file containing the definition |
| 108 |
of the variational forms expressed in UFL and the solver which is implemented |
| 109 |
in a C++ file. |
| 110 |
|
| 111 |
Running this demo requires the files: :download:`main.cpp`, |
| 112 |
:download:`MixedPoisson.ufl` and :download:`CMakeLists.txt`. |
| 113 |
|
| 114 |
|
| 115 |
UFL form file |
| 116 |
^^^^^^^^^^^^^ |
| 117 |
|
| 118 |
The UFL file is implemented in :download:`MixedPoisson.ufl`, and the |
| 119 |
explanation of the UFL file can be found at :doc:`here <MixedPoisson.ufl>`. |
| 120 |
|
| 121 |
|
| 122 |
C++ program |
| 123 |
^^^^^^^^^^^ |
| 124 |
|
| 125 |
The solver is implemented in the :download:`main.cpp` file. |
| 126 |
|
| 127 |
At the top we include the DOLFIN header file and the generated header |
| 128 |
file containing the variational forms. For convenience we also |
| 129 |
include the DOLFIN namespace. |
| 130 |
|
| 131 |
.. code-block:: cpp |
| 132 |
|
| 133 |
#include <dolfin.h> |
| 134 |
#include "MixedPoisson.h" |
| 135 |
|
| 136 |
using namespace dolfin; |
| 137 |
|
| 138 |
Then follows the definition of the coefficient functions (for |
| 139 |
:math:`f` and :math:`G`), which are derived from the DOLFIN |
| 140 |
:cpp:class:`Expression` class. |
| 141 |
|
| 142 |
.. code-block:: cpp |
| 143 |
|
| 144 |
// Source term (right-hand side) |
| 145 |
class Source : public Expression |
| 146 |
{ |
| 147 |
void eval(Array<double>& values, const Array<double>& x) const |
| 148 |
{ |
| 149 |
double dx = x[0] - 0.5; |
| 150 |
double dy = x[1] - 0.5; |
| 151 |
values[0] = 10*exp(-(dx*dx + dy*dy) / 0.02); |
| 152 |
} |
| 153 |
}; |
| 154 |
|
| 155 |
// Boundary source for flux boundary condition |
| 156 |
class BoundarySource : public Expression |
| 157 |
{ |
| 158 |
public: |
| 159 |
|
| 160 |
BoundarySource(const Mesh& mesh) : Expression(2), mesh(mesh) {} |
| 161 |
|
| 162 |
void eval(Array<double>& values, const Array<double>& x, |
| 163 |
const ufc::cell& ufc_cell) const |
| 164 |
{ |
| 165 |
dolfin_assert(ufc_cell.local_facet >= 0); |
| 166 |
|
| 167 |
Cell cell(mesh, ufc_cell.index); |
| 168 |
Point n = cell.normal(ufc_cell.local_facet); |
| 169 |
|
| 170 |
const double g = sin(5*x[0]); |
| 171 |
values[0] = g*n[0]; |
| 172 |
values[1] = g*n[1]; |
| 173 |
} |
| 174 |
|
| 175 |
private: |
| 176 |
|
| 177 |
const Mesh& mesh; |
| 178 |
|
| 179 |
}; |
| 180 |
|
| 181 |
|
| 182 |
Then follows the definition of the essential boundary part of the |
| 183 |
boundary of the domain, which is derived from the |
| 184 |
:cpp:class:`SubDomain` class. |
| 185 |
|
| 186 |
.. code-block:: cpp |
| 187 |
|
| 188 |
// Sub domain for essential boundary condition |
| 189 |
class EssentialBoundary : public SubDomain |
| 190 |
{ |
| 191 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 192 |
{ |
| 193 |
return x[1] < DOLFIN_EPS or x[1] > 1.0 - DOLFIN_EPS; |
| 194 |
} |
| 195 |
}; |
| 196 |
|
| 197 |
Inside the ``main()`` function we first create the ``mesh`` and then |
| 198 |
we define the (mixed) function space for the variational |
| 199 |
formulation. We also define the bilinear form ``a`` and linear form |
| 200 |
``L`` relative to this function space. |
| 201 |
|
| 202 |
.. code-block:: cpp |
| 203 |
|
| 204 |
int main() |
| 205 |
{ |
| 206 |
// Create mesh |
| 207 |
auto mesh = std::make_shared<UnitSquareMesh>(32, 32); |
| 208 |
|
| 209 |
// Construct function space |
| 210 |
auto W = std::make_shared<MixedPoisson::FunctionSpace>(mesh); |
| 211 |
MixedPoisson::BilinearForm a(W, W); |
| 212 |
MixedPoisson::LinearForm L(W); |
| 213 |
|
| 214 |
Then we create the source (:math:`f`) and assign it to the linear form. |
| 215 |
|
| 216 |
.. code-block:: cpp |
| 217 |
|
| 218 |
// Create source and assign to L |
| 219 |
auto f = std::make_shared<Source>(); |
| 220 |
L.f = f; |
| 221 |
|
| 222 |
It only remains to prescribe the boundary condition for the |
| 223 |
flux. Essential boundary conditions are specified through the class |
| 224 |
:cpp:class:`DirichletBC` which takes three arguments: the function |
| 225 |
space the boundary condition is supposed to be applied to, the data |
| 226 |
for the boundary condition, and the relevant part of the boundary. |
| 227 |
|
| 228 |
We want to apply the boundary condition to the first subspace of the |
| 229 |
mixed space. This space can be accessed through the `sub` member |
| 230 |
function of the :cpp:class:`FunctionSpace` class. |
| 231 |
|
| 232 |
Next, we need to construct the data for the boundary condition. An |
| 233 |
essential boundary condition is handled by replacing degrees of |
| 234 |
freedom by the degrees of freedom evaluated at the given data. The |
| 235 |
:math:`BDM` finite element spaces are vector-valued spaces and hence |
| 236 |
the degrees of freedom act on vector-valued objects. The effect is |
| 237 |
that the user is required to construct a :math:`G` such that :math:`G |
| 238 |
\cdot n = g`. Such a :math:`G` can be constructed by letting :math:`G |
| 239 |
= g n`. This is what the derived expression class ``BoundarySource`` |
| 240 |
defined above does. |
| 241 |
|
| 242 |
.. code-block:: cpp |
| 243 |
|
| 244 |
// Define boundary condition |
| 245 |
auto G = std::make_shared<BoundarySource>(*mesh); |
| 246 |
auto boundary = std::make_shared<EssentialBoundary>(); |
| 247 |
DirichletBC bc(W->sub(0), G, boundary); |
| 248 |
|
| 249 |
To compute the solution we use the bilinear and linear forms, and the |
| 250 |
boundary condition, but we also need to create a :cpp:class:`Function` |
| 251 |
to store the solution(s). The (full) solution will be stored in the |
| 252 |
:cpp:class:`Function` ``w``, which we initialise using the |
| 253 |
:cpp:class:`FunctionSpace` ``W``. The actual computation is performed |
| 254 |
by calling ``solve``. |
| 255 |
|
| 256 |
.. code-block:: cpp |
| 257 |
|
| 258 |
// Compute solution |
| 259 |
Function w(W); |
| 260 |
solve(a == L, w, bc); |
| 261 |
|
| 262 |
Now, the separate components ``sigma`` and ``u`` of the solution can |
| 263 |
be extracted by taking components. These can then be written to XDMF |
| 264 |
files. |
| 265 |
|
| 266 |
.. code-block:: cpp |
| 267 |
|
| 268 |
// Extract sub functions (function views) |
| 269 |
Function& sigma = w[0]; |
| 270 |
Function& u = w[1]; |
| 271 |
|
| 272 |
XDMFFile("sigma.xdmf").write(sigma); |
| 273 |
XDMFFile("u.xdmf").write(u); |
| 274 |
|
| 275 |
return 0; |
| 276 |
} |