| 1 |
Biharmonic equation (C++) |
| 2 |
========================= |
| 3 |
|
| 4 |
This demo illustrates how to: |
| 5 |
|
| 6 |
* Solve a linear partial differential equation |
| 7 |
* Use a discontinuous Galerkin method |
| 8 |
* Solve a fourth-order differential equation |
| 9 |
|
| 10 |
The solution for :math:`u` in this demo will look as follows: |
| 11 |
|
| 12 |
.. image:: ../biharmonic_u.png |
| 13 |
:scale: 75 % |
| 14 |
|
| 15 |
Equation and problem definition |
| 16 |
------------------------------- |
| 17 |
|
| 18 |
The biharmonic equation is a fourth-order elliptic equation. On the |
| 19 |
domain :math:`\Omega \subset \mathbb{R}^{d}`, :math:`1 \le d \le 3`, |
| 20 |
it reads |
| 21 |
|
| 22 |
.. math:: |
| 23 |
\nabla^{4} u = f \quad {\rm in} \ \Omega, |
| 24 |
|
| 25 |
where :math:`\nabla^{4} \equiv \nabla^{2} \nabla^{2}` is the |
| 26 |
biharmonic operator and :math:`f` is a prescribed source term. To |
| 27 |
formulate a complete boundary value problem, the biharmonic equation |
| 28 |
must be complemented by suitable boundary conditions. |
| 29 |
|
| 30 |
Multiplying the biharmonic equation by a test function and integrating |
| 31 |
by parts twice leads to a problem second-order derivatives, which |
| 32 |
would requires :math:`H^{2}` conforming (roughly :math:`C^{1}` |
| 33 |
continuous) basis functions. To solve the biharmonic equation using |
| 34 |
Lagrange finite element basis functions, the biharmonic equation can |
| 35 |
be split into two second-order equations (see the Mixed Poisson demo |
| 36 |
for a mixed method for the Poisson equation), or a variational |
| 37 |
formulation can be constructed that imposes weak continuity of normal |
| 38 |
derivatives between finite element cells. The demo uses a |
| 39 |
discontinuous Galerkin approach to impose continuity of the normal |
| 40 |
derivative weakly. |
| 41 |
|
| 42 |
Consider a triangulation :math:`\mathcal{T}` of the domain |
| 43 |
:math:`\Omega`, where the set of interior facets is denoted by |
| 44 |
:math:`\mathcal{E}_h^{\rm int}`. Functions evaluated on opposite |
| 45 |
sides of a facet are indicated by the subscripts ':math:`+`' and |
| 46 |
':math:`-`'. Using the standard continuous Lagrange finite element |
| 47 |
space |
| 48 |
|
| 49 |
.. math:: |
| 50 |
V = \left\{v \in H^{1}_{0}(\Omega)\,:\, v \in P_{k}(K) \ \forall \ K \in \mathcal{T} \right\} |
| 51 |
|
| 52 |
and considering the boundary conditions |
| 53 |
|
| 54 |
.. math:: |
| 55 |
u &= 0 \quad {\rm on} \ \partial\Omega \\ |
| 56 |
\nabla^{2} u &= 0 \quad {\rm on} \ \partial\Omega |
| 57 |
|
| 58 |
a weak formulation of the biharmonic problem reads: find :math:`u \in |
| 59 |
V` such that |
| 60 |
|
| 61 |
.. math:: |
| 62 |
a(u,v)=L(v) \quad \forall \ v \in V, |
| 63 |
|
| 64 |
where the bilinear form is |
| 65 |
|
| 66 |
.. math:: |
| 67 |
a(u, v) = \sum_{K \in \mathcal{T}} \int_{K} \nabla^{2} u \nabla^{2} v \, {\rm d}x \ |
| 68 |
+\sum_{E \in \mathcal{E}_h^{\rm int}}\left(\int_{E} \frac{\alpha}{h_E} [\!\![ \nabla u ]\!\!] [\!\![ \nabla v ]\!\!] \, {\rm d}s |
| 69 |
- \int_{E} \left<\nabla^{2} u \right>[\!\![ \nabla v ]\!\!] \, {\rm d}s |
| 70 |
- \int_{E} [\!\![ \nabla u ]\!\!] \left<\nabla^{2} v \right> \, {\rm d}s\right) |
| 71 |
|
| 72 |
and the linear form is |
| 73 |
|
| 74 |
.. math:: |
| 75 |
L(v) = \int_{\Omega} fv \, {\rm d}x |
| 76 |
|
| 77 |
Furthermore, :math:`\left< u \right> = \frac{1}{2} (u_{+} + u_{-})`, :math:`[\!\![ |
| 78 |
w ]\!\!] = w_{+} \cdot n_{+} + w_{-} \cdot n_{-}`, :math:`\alpha \ge |
| 79 |
0` is a penalty parameter and :math:`h_E` is a measure of the cell size. |
| 80 |
|
| 81 |
The input parameters for this demo are defined as follows: |
| 82 |
|
| 83 |
* :math:`\Omega = [0,1] \times [0,1]` (a unit square) |
| 84 |
* :math:`\alpha = 8.0` (penalty parameter) |
| 85 |
* :math:`f = 4.0 \pi^4\sin(\pi x)\sin(\pi y)` (source term) |
| 86 |
|
| 87 |
|
| 88 |
Implementation |
| 89 |
-------------- |
| 90 |
|
| 91 |
The implementation is split in two files, a form file containing the |
| 92 |
definition of the variational forms expressed in UFL and the solver |
| 93 |
which is implemented in a C++ file. |
| 94 |
|
| 95 |
Running this demo requires the files: :download:`main.cpp`, |
| 96 |
:download:`Biharmonic.ufl` and :download:`CMakeLists.txt`. |
| 97 |
|
| 98 |
UFL form file |
| 99 |
^^^^^^^^^^^^^ |
| 100 |
|
| 101 |
The UFL file is implemented in :download:`Biharmonic.ufl`, and the |
| 102 |
explanation of the UFL file can be found at :doc:`here |
| 103 |
<Biharmonic.ufl>`. |
| 104 |
|
| 105 |
|
| 106 |
C++ program |
| 107 |
^^^^^^^^^^^ |
| 108 |
|
| 109 |
The DOLFIN interface and the code generated from the UFL input is |
| 110 |
included, and the DOLFIN namespace is used: |
| 111 |
|
| 112 |
.. code-block:: cpp |
| 113 |
|
| 114 |
#include <dolfin.h> |
| 115 |
#include "Biharmonic.h" |
| 116 |
|
| 117 |
using namespace dolfin; |
| 118 |
|
| 119 |
A class ``Source`` is defined for the function :math:`f`, with the |
| 120 |
function ``Expression::eval`` overloaded: |
| 121 |
|
| 122 |
.. code-block:: cpp |
| 123 |
|
| 124 |
// Source term |
| 125 |
class Source : public Expression |
| 126 |
{ |
| 127 |
public: |
| 128 |
|
| 129 |
void eval(Array<double>& values, const Array<double>& x) const |
| 130 |
{ |
| 131 |
values[0] = 4.0*std::pow(DOLFIN_PI, 4)* |
| 132 |
std::sin(DOLFIN_PI*x[0])*std::sin(DOLFIN_PI*x[1]); |
| 133 |
} |
| 134 |
|
| 135 |
}; |
| 136 |
|
| 137 |
A boundary subdomain is defined, which in this case is the entire |
| 138 |
boundary: |
| 139 |
|
| 140 |
.. code-block:: cpp |
| 141 |
|
| 142 |
// Sub domain for Dirichlet boundary condition |
| 143 |
class DirichletBoundary : public SubDomain |
| 144 |
{ |
| 145 |
bool inside(const Array<double>& x, bool on_boundary) const |
| 146 |
{ return on_boundary; } |
| 147 |
}; |
| 148 |
|
| 149 |
The main part of the program is begun, and a mesh is created with 32 |
| 150 |
vertices in each direction: |
| 151 |
|
| 152 |
.. code-block:: cpp |
| 153 |
|
| 154 |
int main() |
| 155 |
{ |
| 156 |
// Make mesh ghosted for evaluation of DG terms |
| 157 |
parameters["ghost_mode"] = "shared_facet"; |
| 158 |
|
| 159 |
// Create mesh |
| 160 |
auto mesh = std::make_shared<UnitSquareMesh>(32, 32); |
| 161 |
|
| 162 |
The source function, a function for the cell size and the penalty term |
| 163 |
are declared: |
| 164 |
|
| 165 |
.. code-block:: cpp |
| 166 |
|
| 167 |
// Create functions |
| 168 |
auto f = std::make_shared<Source>(); |
| 169 |
auto alpha = std::make_shared<Constant>(8.0); |
| 170 |
|
| 171 |
A function space object, which is defined in the generated code, is |
| 172 |
created: |
| 173 |
|
| 174 |
.. code-block:: cpp |
| 175 |
|
| 176 |
// Create function space |
| 177 |
auto V = std::make_shared<Biharmonic::FunctionSpace>(mesh); |
| 178 |
|
| 179 |
The Dirichlet boundary condition on :math:`u` is constructed by |
| 180 |
defining a :cpp:class:`Constant` which is equal to zero, defining the |
| 181 |
boundary (``DirichletBoundary``), and using these, together with |
| 182 |
``V``, to create ``bc``: |
| 183 |
|
| 184 |
.. code-block:: cpp |
| 185 |
|
| 186 |
// Define boundary condition |
| 187 |
auto u0 = std::make_shared<Constant>(0.0); |
| 188 |
auto boundary = std::make_shared<DirichletBoundary>(); |
| 189 |
DirichletBC bc(V, u0, boundary); |
| 190 |
|
| 191 |
Using the function space ``V``, the bilinear and linear forms are |
| 192 |
created, and function are attached: |
| 193 |
|
| 194 |
.. code-block:: cpp |
| 195 |
|
| 196 |
// Define variational problem |
| 197 |
Biharmonic::BilinearForm a(V, V); |
| 198 |
Biharmonic::LinearForm L(V); |
| 199 |
a.alpha = alpha; L.f = f; |
| 200 |
|
| 201 |
A :cpp:class:`Function` is created to hold the solution and the |
| 202 |
problem is solved: |
| 203 |
|
| 204 |
.. code-block:: cpp |
| 205 |
|
| 206 |
// Compute solution |
| 207 |
Function u(V); |
| 208 |
solve(a == L, u, bc); |
| 209 |
|
| 210 |
The solution is then written to a file in VTK format: |
| 211 |
|
| 212 |
.. code-block:: cpp |
| 213 |
|
| 214 |
// Save solution in VTK format |
| 215 |
File file("biharmonic.pvd"); |
| 216 |
file << u; |
| 217 |
|
| 218 |
return 0; |
| 219 |
} |