| 1 |
Poisson equation (C++) |
| 2 |
====================== |
| 3 |
|
| 4 |
This demo illustrates how to: |
| 5 |
|
| 6 |
* Solve a linear partial differential equation |
| 7 |
* Create and apply Dirichlet boundary conditions |
| 8 |
* Define Expressions |
| 9 |
* Define a FunctionSpace |
| 10 |
* Create a SubDomain |
| 11 |
|
| 12 |
The solution for :math:`u` in this demo will look as follows: |
| 13 |
|
| 14 |
.. image:: ../poisson_u.png |
| 15 |
:scale: 75 % |
| 16 |
|
| 17 |
|
| 18 |
Equation and problem definition |
| 19 |
------------------------------- |
| 20 |
|
| 21 |
The Poisson equation is the canonical elliptic partial differential |
| 22 |
equation. For a domain :math:`\Omega \subset \mathbb{R}^n` with |
| 23 |
boundary :math:`\partial \Omega = \Gamma_{D} \cup \Gamma_{N}`, the |
| 24 |
Poisson equation with particular boundary conditions reads: |
| 25 |
|
| 26 |
.. math:: |
| 27 |
- \nabla^{2} u &= f \quad {\rm in} \ \Omega, \\ |
| 28 |
u &= 0 \quad {\rm on} \ \Gamma_{D}, \\ |
| 29 |
\nabla u \cdot n &= g \quad {\rm on} \ \Gamma_{N}. \\ |
| 30 |
|
| 31 |
Here, :math:`f` and :math:`g` are input data and :math:`n` denotes the |
| 32 |
outward directed boundary normal. The most standard variational form |
| 33 |
of Poisson equation reads: find :math:`u \in V` such that |
| 34 |
|
| 35 |
.. math:: |
| 36 |
a(u, v) = L(v) \quad \forall \ v \in V, |
| 37 |
|
| 38 |
where :math:`V` is a suitable function space and |
| 39 |
|
| 40 |
.. math:: |
| 41 |
a(u, v) &= \int_{\Omega} \nabla u \cdot \nabla v \, {\rm d} x, \\ |
| 42 |
L(v) &= \int_{\Omega} f v \, {\rm d} x |
| 43 |
+ \int_{\Gamma_{N}} g v \, {\rm d} s. |
| 44 |
|
| 45 |
The expression :math:`a(u, v)` is the bilinear form and :math:`L(v)` |
| 46 |
is the linear form. It is assumed that all functions in :math:`V` |
| 47 |
satisfy the Dirichlet boundary conditions (:math:`u = 0 \ {\rm on} \ |
| 48 |
\Gamma_{D}`). |
| 49 |
|
| 50 |
In this demo, we shall consider the following definitions of the input |
| 51 |
functions, the domain, and the boundaries: |
| 52 |
|
| 53 |
* :math:`\Omega = [0,1] \times [0,1]` (a unit square) |
| 54 |
* :math:`\Gamma_{D} = \{(0, y) \cup (1, y) \subset \partial \Omega\}` (Dirichlet boundary) |
| 55 |
* :math:`\Gamma_{N} = \{(x, 0) \cup (x, 1) \subset \partial \Omega\}` (Neumann boundary) |
| 56 |
* :math:`g = \sin(5x)` (normal derivative) |
| 57 |
* :math:`f = 10\exp(-((x - 0.5)^2 + (y - 0.5)^2) / 0.02)` (source term) |
| 58 |
|
| 59 |
|
| 60 |
Implementation |
| 61 |
-------------- |
| 62 |
|
| 63 |
The implementation is split in two files: a form file containing the |
| 64 |
definition of the variational forms expressed in UFL and a C++ file |
| 65 |
containing the actual solver. |
| 66 |
|
| 67 |
Running this demo requires the files: :download:`main.cpp`, |
| 68 |
:download:`Poisson.ufl` and :download:`CMakeLists.txt`. |
| 69 |
|
| 70 |
|
| 71 |
UFL form file |
| 72 |
^^^^^^^^^^^^^ |
| 73 |
|
| 74 |
The UFL file is implemented in :download:`Poisson.ufl`, and the |
| 75 |
explanation of the UFL file can be found at :doc:`here <Poisson.ufl>`. |
| 76 |
|
| 77 |
|
| 78 |
C++ program |
| 79 |
^^^^^^^^^^^ |
| 80 |
|
| 81 |
The main solver is implemented in the :download:`main.cpp` file. |
| 82 |
|
| 83 |
At the top we include the DOLFIN header file and the generated header |
| 84 |
file "Poisson.h" containing the variational forms for the Poisson |
| 85 |
equation. For convenience we also include the DOLFIN namespace. |
| 86 |
|
| 87 |
.. code-block:: cpp |
| 88 |
|
| 89 |
#include <dolfin.h> |
| 90 |
#include "Poisson.h" |
| 91 |
|
| 92 |
using namespace dolfin; |
| 93 |
|
| 94 |
Then follows the definition of the coefficient functions (for |
| 95 |
:math:`f` and :math:`g`), which are derived from the |
| 96 |
:cpp:class:`Expression` class in DOLFIN |
| 97 |
|
| 98 |
.. code-block:: cpp |
| 99 |
|
| 100 |
// Source term (right-hand side) |
| 101 |
class Source : public Expression |
| 102 |
{ |
| 103 |
void eval(Array<double>& values, const Array<double>& x) const |
| 104 |
{ |
| 105 |
double dx = x[0] - 0.5; |
| 106 |
double dy = x[1] - 0.5; |
| 107 |
values[0] = 10*exp(-(dx*dx + dy*dy) / 0.02); |
| 108 |
} |
| 109 |
}; |
| 110 |
|
| 111 |
// Normal derivative (Neumann boundary condition) |
| 112 |
class dUdN : public Expression |
| 113 |
{ |
| 114 |
void eval(Array<double>& values, const Array<double>& x) const |
| 115 |
{ |
| 116 |
values[0] = sin(5*x[0]); |
| 117 |
} |
| 118 |
}; |
| 119 |
|
| 120 |
The ``DirichletBoundary`` is derived from the :cpp:class:`SubDomain` |
| 121 |
class and defines the part of the boundary to which the Dirichlet |
| 122 |
boundary condition should be applied. |
| 123 |
|
| 124 |
.. code-block:: cpp |
| 125 |
|
| 126 |
// Sub domain for Dirichlet boundary condition |
| 127 |
class DirichletBoundary : public SubDomain |
| 128 |
{ |
| 129 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 130 |
{ |
| 131 |
return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS; |
| 132 |
} |
| 133 |
}; |
| 134 |
|
| 135 |
Inside the ``main`` function, we begin by defining a mesh of the |
| 136 |
domain. As the unit square is a very standard domain, we can use a |
| 137 |
built-in mesh provided by the :cpp:class:`UnitSquareMesh` factory. In |
| 138 |
order to create a mesh consisting of 32 x 32 squares with each square |
| 139 |
divided into two triangles, and the finite element space (specified in |
| 140 |
the form file) defined relative to this mesh, we do as follows |
| 141 |
|
| 142 |
.. code-block:: cpp |
| 143 |
|
| 144 |
int main() |
| 145 |
{ |
| 146 |
// Create mesh and function space |
| 147 |
auto mesh = std::make_shared<Mesh>( |
| 148 |
UnitSquareMesh::create({{32, 32}}, CellType::Type::triangle)); |
| 149 |
auto V = std::make_shared<Poisson::FunctionSpace>(mesh); |
| 150 |
|
| 151 |
Now, the Dirichlet boundary condition (:math:`u = 0`) can be created |
| 152 |
using the class :cpp:class:`DirichletBC`. A :cpp:class:`DirichletBC` |
| 153 |
takes three arguments: the function space the boundary condition |
| 154 |
applies to, the value of the boundary condition, and the part of the |
| 155 |
boundary on which the condition applies. In our example, the function |
| 156 |
space is ``V``, the value of the boundary condition (0.0) can |
| 157 |
represented using a :cpp:class:`Constant`, and the Dirichlet boundary |
| 158 |
is defined by the class :cpp:class:`DirichletBoundary` listed |
| 159 |
above. The definition of the Dirichlet boundary condition then looks |
| 160 |
as follows: |
| 161 |
|
| 162 |
.. code-block:: cpp |
| 163 |
|
| 164 |
// Define boundary condition |
| 165 |
auto u0 = std::make_shared<Constant>(0.0); |
| 166 |
auto boundary = std::make_shared<DirichletBoundary>(); |
| 167 |
DirichletBC bc(V, u0, boundary); |
| 168 |
|
| 169 |
Next, we define the variational formulation by initializing the |
| 170 |
bilinear and linear forms (:math:`a`, :math:`L`) using the previously |
| 171 |
defined :cpp:class:`FunctionSpace` ``V``. Then we can create the |
| 172 |
source and boundary flux term (:math:`f`, :math:`g`) and attach these |
| 173 |
to the linear form. |
| 174 |
|
| 175 |
.. code-block:: cpp |
| 176 |
|
| 177 |
// Define variational forms |
| 178 |
Poisson::BilinearForm a(V, V); |
| 179 |
Poisson::LinearForm L(V); |
| 180 |
auto f = std::make_shared<Source>(); |
| 181 |
auto g = std::make_shared<dUdN>(); |
| 182 |
L.f = f; |
| 183 |
L.g = g; |
| 184 |
|
| 185 |
Now, we have specified the variational forms and can consider the |
| 186 |
solution of the variational problem. First, we need to define a |
| 187 |
:cpp:class:`Function` ``u`` to store the solution. (Upon |
| 188 |
initialization, it is simply set to the zero function.) Next, we can |
| 189 |
call the ``solve`` function with the arguments ``a == L``, ``u`` and |
| 190 |
``bc`` as follows: |
| 191 |
|
| 192 |
.. code-block:: cpp |
| 193 |
|
| 194 |
// Compute solution |
| 195 |
Function u(V); |
| 196 |
solve(a == L, u, bc); |
| 197 |
|
| 198 |
The function ``u`` will be modified during the call to solve. A |
| 199 |
:cpp:class:`Function` can be saved to a file. Here, we output the |
| 200 |
solution to a ``VTK`` file (specified using the suffix ``.pvd``) for |
| 201 |
visualisation in an external program such as Paraview. |
| 202 |
|
| 203 |
.. code-block:: cpp |
| 204 |
|
| 205 |
// Save solution in VTK format |
| 206 |
File file("poisson.pvd"); |
| 207 |
file << u; |
| 208 |
|
| 209 |
return 0; |
| 210 |
} |