Debian Codegraph @

python/demo/documented/biharmonic/demo_biharmonic.py.rst

raw
1 Biharmonic equation
2 ===================
3
4 This demo is implemented in a single Python file,
5 :download:`demo_biharmonic.py`, which contains both the variational
6 forms and the solver.
7
8 This demo illustrates how to:
9
10 * Solve a linear partial differential equation
11 * Use a discontinuous Galerkin method
12 * Solve a fourth-order differential equation
13
14 The solution for :math:`u` in this demo will look as follows:
15
16 .. image:: biharmonic_u.png
17 :scale: 75 %
18
19
20 Equation and problem definition
21 -------------------------------
22
23 The biharmonic equation is a fourth-order elliptic equation. On the
24 domain :math:`\Omega \subset \mathbb{R}^{d}`, :math:`1 \le d \le 3`,
25 it reads
26
27 .. math::
28 \nabla^{4} u = f \quad {\rm in} \ \Omega,
29
30 where :math:`\nabla^{4} \equiv \nabla^{2} \nabla^{2}` is the
31 biharmonic operator and :math:`f` is a prescribed source term. To
32 formulate a complete boundary value problem, the biharmonic equation
33 must be complemented by suitable boundary conditions.
34
35 Multiplying the biharmonic equation by a test function and integrating
36 by parts twice leads to a problem second-order derivatives, which
37 would requires :math:`H^{2}` conforming (roughly :math:`C^{1}`
38 continuous) basis functions. To solve the biharmonic equation using
39 Lagrange finite element basis functions, the biharmonic equation can
40 be split into two second-order equations (see the Mixed Poisson demo
41 for a mixed method for the Poisson equation), or a variational
42 formulation can be constructed that imposes weak continuity of normal
43 derivatives between finite element cells. The demo uses a
44 discontinuous Galerkin approach to impose continuity of the normal
45 derivative weakly.
46
47 Consider a triangulation :math:`\mathcal{T}` of the domain
48 :math:`\Omega`, where the set of interior facets is denoted by
49 :math:`\mathcal{E}_h^{\rm int}`. Functions evaluated on opposite
50 sides of a facet are indicated by the subscripts ':math:`+`' and
51 ':math:`-`'. Using the standard continuous Lagrange finite element
52 space
53
54 .. math::
55 V = \left\{v \in H^{1}_{0}(\Omega)\,:\, v \in P_{k}(K) \ \forall \ K \in \mathcal{T} \right\}
56
57 and considering the boundary conditions
58
59 .. math::
60 u &= 0 \quad {\rm on} \ \partial\Omega \\
61 \nabla^{2} u &= 0 \quad {\rm on} \ \partial\Omega
62
63 a weak formulation of the biharmonic problem reads: find :math:`u \in
64 V` such that
65
66 .. math::
67 a(u,v)=L(v) \quad \forall \ v \in V,
68
69 where the bilinear form is
70
71
72 .. math::
73 a(u, v) = \sum_{K \in \mathcal{T}} \int_{K} \nabla^{2} u \nabla^{2} v \, {\rm d}x \
74 +\sum_{E \in \mathcal{E}_h^{\rm int}}\left(\int_{E} \frac{\alpha}{h_E} [\!\![ \nabla u ]\!\!] [\!\![ \nabla v ]\!\!] \, {\rm d}s
75 - \int_{E} \left<\nabla^{2} u \right>[\!\![ \nabla v ]\!\!] \, {\rm d}s
76 - \int_{E} [\!\![ \nabla u ]\!\!] \left<\nabla^{2} v \right> \, {\rm d}s\right)
77
78 and the linear form is
79
80 .. math::
81 L(v) = \int_{\Omega} fv \, {\rm d}x
82
83 Furthermore, :math:`\left< u \right> = \frac{1}{2} (u_{+} + u_{-})`,
84 :math:`[\!\![ w ]\!\!] = w_{+} \cdot n_{+} + w_{-} \cdot n_{-}`,
85 :math:`\alpha \ge 0` is a penalty parameter and :math:`h_E` is a
86 measure of the cell size.
87
88 The input parameters for this demo are defined as follows:
89
90 * :math:`\Omega = [0,1] \times [0,1]` (a unit square)
91 * :math:`\alpha = 8.0` (penalty parameter)
92 * :math:`f = 4.0 \pi^4\sin(\pi x)\sin(\pi y)` (source term)
93
94
95 Implementation
96 --------------
97
98 This demo is implemented in the :download:`demo_biharmonic.py` file.
99
100 First, the necessary modules are imported::
101
102 import matplotlib.pyplot as plt
103 from dolfin import *
104
105 Next, some parameters for the form compiler are set::
106
107 # Optimization options for the form compiler
108 parameters["form_compiler"]["cpp_optimize"] = True
109 parameters["form_compiler"]["optimize"] = True
110
111 A mesh is created, and a quadratic finite element function space::
112
113 # Make mesh ghosted for evaluation of DG terms
114 parameters["ghost_mode"] = "shared_facet"
115
116 # Create mesh and define function space
117 mesh = UnitSquareMesh(32, 32)
118 V = FunctionSpace(mesh, "CG", 2)
119
120 A subclass of :py:class:`SubDomain <dolfin.cpp.SubDomain>`,
121 ``DirichletBoundary`` is created for later defining the boundary of
122 the domain::
123
124 # Define Dirichlet boundary
125 class DirichletBoundary(SubDomain):
126 def inside(self, x, on_boundary):
127 return on_boundary
128
129 A subclass of :py:class:`Expression
130 <dolfin.functions.expression.Expression>`, ``Source`` is created for
131 the source term :math:`f`::
132
133 class Source(UserExpression):
134 def eval(self, values, x):
135 values[0] = 4.0*pi**4*sin(pi*x[0])*sin(pi*x[1])
136
137 The Dirichlet boundary condition is created::
138
139 # Define boundary condition
140 u0 = Constant(0.0)
141 bc = DirichletBC(V, u0, DirichletBoundary())
142
143 On the finite element space ``V``, trial and test functions are
144 created::
145
146 # Define trial and test functions
147 u = TrialFunction(V)
148 v = TestFunction(V)
149
150 A function for the cell size :math:`h` is created, as is a function
151 for the average size of cells that share a facet (``h_avg``). The UFL
152 syntax ``('+')`` and ``('-')`` restricts a function to the ``('+')``
153 and ``('-')`` sides of a facet, respectively. The unit outward normal
154 to cell boundaries (``n``) is created, as is the source term ``f`` and
155 the penalty parameter ``alpha``. The penalty parameters is made a
156 :py:class:`Constant <dolfin.functions.constant.Constant>` so that it
157 can be changed without needing to regenerate code. ::
158
159 # Define normal component, mesh size and right-hand side
160 h = CellDiameter(mesh)
161 h_avg = (h('+') + h('-'))/2.0
162 n = FacetNormal(mesh)
163 f = Source(degree=2)
164
165 # Penalty parameter
166 alpha = Constant(8.0)
167
168 The bilinear and linear forms are defined::
169
170 # Define bilinear form
171 a = inner(div(grad(u)), div(grad(v)))*dx \
172 - inner(avg(div(grad(u))), jump(grad(v), n))*dS \
173 - inner(jump(grad(u), n), avg(div(grad(v))))*dS \
174 + alpha/h_avg*inner(jump(grad(u),n), jump(grad(v),n))*dS
175
176 # Define linear form
177 L = f*v*dx
178
179 A :py:class:`Function <dolfin.functions.function.Function>` is created
180 to store the solution and the variational problem is solved::
181
182 # Solve variational problem
183 u = Function(V)
184 solve(a == L, u, bc)
185
186 The computed solution is written to a file in VTK format and plotted to
187 the screen. ::
188
189 # Save solution to file
190 file = File("biharmonic.pvd")
191 file << u
192
193 # Plot solution
194 plot(u)
195 plt.show()