Debian Codegraph @

python/demo/documented/mixed-poisson/demo_mixed-poisson.py.rst

raw
1
2 .. _demo_mixed_poisson:
3
4 Mixed formulation for Poisson equation
5 ======================================
6
7 This demo is implemented in a single Python file,
8 :download:`demo_mixed-poisson.py`, which contains both the variational
9 forms and the solver.
10
11 This demo illustrates how to solve Poisson equation using a mixed
12 (two-field) formulation. In particular, it illustrates how to
13
14 * Use mixed and non-continuous finite element spaces
15 * Set essential boundary conditions for subspaces and H(div) spaces
16 * Define a (vector-valued) expression using additional geometry information
17
18
19 Equation and problem definition
20 -------------------------------
21
22 An alternative formulation of Poisson equation can be formulated by
23 introducing an additional (vector) variable, namely the (negative)
24 flux: :math:`\sigma = \nabla u`. The partial differential equations
25 then read
26
27 .. math::
28 \sigma - \nabla u &= 0 \quad {\rm in} \ \Omega, \\
29 \nabla \cdot \sigma &= - f \quad {\rm in} \ \Omega,
30
31 with boundary conditions
32
33 .. math::
34 u = u_0 \quad {\rm on} \ \Gamma_{D}, \\
35 \sigma \cdot n = g \quad {\rm on} \ \Gamma_{N}.
36
37 The same equations arise in connection with flow in porous media, and
38 are also referred to as Darcy flow.
39
40 After multiplying by test functions :math:`\tau` and :math:`v`,
41 integrating over the domain, and integrating the gradient term by
42 parts, one obtains the following variational formulation: find
43 :math:`\sigma \in \Sigma` and :math:`v \in V` satisfying
44
45 .. math::
46 \int_{\Omega} (\sigma \cdot \tau + \nabla \cdot \tau \ u) \ {\rm d} x
47 &= \int_{\Gamma} \tau \cdot n \ u \ {\rm d} s
48 \quad \forall \ \tau \in \Sigma, \\
49
50 \int_{\Omega} \nabla \cdot \sigma v \ {\rm d} x
51 &= - \int_{\Omega} f \ v \ {\rm d} x
52 \quad \forall \ v \in V.
53
54 Here :math:`n` denotes the outward pointing normal vector on the
55 boundary. Looking at the variational form, we see that the boundary
56 condition for the flux (:math:`\sigma \cdot n = g`) is now an
57 essential boundary condition (which should be enforced in the function
58 space), while the other boundary condition (:math:`u = u_0`) is a
59 natural boundary condition (which should be applied to the variational
60 form). Inserting the boundary conditions, this variational problem can
61 be phrased in the general form: find :math:`(\sigma, u) \in \Sigma_g
62 \times V` such that
63
64 .. math::
65
66 a((\sigma, u), (\tau, v)) = L((\tau, v))
67 \quad \forall \ (\tau, v) \in \Sigma_0 \times V
68
69 where the variational forms :math:`a` and :math:`L` are defined as
70
71 .. math::
72
73 a((\sigma, u), (\tau, v)) &=
74 \int_{\Omega} \sigma \cdot \tau + \nabla \cdot \tau \ u
75 + \nabla \cdot \sigma \ v \ {\rm d} x \\
76 L((\tau, v)) &= - \int_{\Omega} f v \ {\rm d} x
77 + \int_{\Gamma_D} u_0 \tau \cdot n \ {\rm d} s
78
79 and :math:`\Sigma_g = \{ \tau \in H({\rm div}) \text{ such that } \tau \cdot n|_{\Gamma_N} = g \}`
80 and :math:`V = L^2(\Omega)`.
81
82 To discretize the above formulation, two discrete function spaces
83 :math:`\Sigma_h \subset \Sigma` and :math:`V_h \subset V` are needed
84 to form a mixed function space :math:`\Sigma_h \times V_h`. A stable
85 choice of finite element spaces is to let :math:`\Sigma_h` be the
86 Brezzi-Douglas-Marini elements of polynomial order :math:`k` and let
87 :math:`V_h` be discontinuous elements of polynomial order :math:`k-1`.
88
89 We will use the same definitions of functions and boundaries as in the
90 demo for Poisson's equation. These are:
91
92 * :math:`\Omega = [0,1] \times [0,1]` (a unit square)
93 * :math:`\Gamma_{D} = \{(0, y) \cup (1, y) \in \partial \Omega\}`
94 * :math:`\Gamma_{N} = \{(x, 0) \cup (x, 1) \in \partial \Omega\}`
95 * :math:`u_0 = 0`
96 * :math:`g = \sin(5x)` (flux)
97 * :math:`f = 10\exp(-((x - 0.5)^2 + (y - 0.5)^2) / 0.02)` (source term)
98
99 With the above input the solution for :math:`u` and :math:`\sigma` will look as
100 follows:
101
102 .. image:: mixed-poisson_u.png
103 :scale: 75 %
104 :align: center
105
106 .. image:: mixed-poisson_sigma.png
107 :scale: 75 %
108 :align: center
109
110
111 Implementation
112 --------------
113
114 This demo is implemented in the :download:`demo_mixed-poisson.py`
115 file.
116
117 First, the required modules are imported::
118
119 from dolfin import *
120 import matplotlib.pyplot as plt
121
122 Then, we need to create a :py:class:`Mesh <dolfin.cpp.Mesh>` covering
123 the unit square. In this example, we will let the mesh consist of 32 x
124 32 squares with each square divided into two triangles::
125
126 # Create mesh
127 mesh = UnitSquareMesh(32, 32)
128
129 .. index::
130 pair: FunctionSpace; Brezzi-Douglas-Marini
131 pair: FunctionSpace; Discontinous Lagrange
132
133 Next, we need to build the function space. ::
134
135 # Define finite elements spaces and build mixed space
136 BDM = FiniteElement("BDM", mesh.ufl_cell(), 1)
137 DG = FiniteElement("DG", mesh.ufl_cell(), 0)
138 W = FunctionSpace(mesh, BDM * DG)
139
140 The second argument to :py:class:`FunctionSpace
141 <dolfin.functions.functionspace.FunctionSpace>` specifies underlying
142 finite element, here mixed element obtained by ``*`` operator.
143
144 .. math::
145
146 W = \{ (\tau, v) \ \text{such that} \ \tau \in BDM, v \in DG \}.
147
148 Next, we need to specify the trial functions (the unknowns) and the
149 test functions on this space. This can be done as follows::
150
151 # Define trial and test functions
152 (sigma, u) = TrialFunctions(W)
153 (tau, v) = TestFunctions(W)
154
155 In order to define the variational form, it only remains to define the
156 source function :math:`f`. This is done just as for the :ref:`Poisson
157 demo <demo_poisson_equation>`::
158
159 # Define source function
160 f = Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2)
161
162 We are now ready to define the variational forms a and L. Since,
163 :math:`u_0 = 0` in this example, the boundary term on the right-hand
164 side vanishes. ::
165
166 # Define variational form
167 a = (dot(sigma, tau) + div(tau)*u + div(sigma)*v)*dx
168 L = - f*v*dx
169
170 It only remains to prescribe the boundary condition for the
171 flux. Essential boundary conditions are specified through the class
172 :py:class:`DirichletBC <dolfin.fem.bcs.DirichletBC>` which takes three
173 arguments: the function space the boundary condition is supposed to be
174 applied to, the data for the boundary condition, and the relevant part
175 of the boundary.
176
177 We want to apply the boundary condition to the first subspace of the
178 mixed space. Subspaces of a mixed :py:class:`FunctionSpace
179 <dolfin.functions.functionspace.FunctionSpace>` can be accessed
180 by the method :py:func:`sub
181 <dolfin.functions.functionspace.FunctionSpace.sub>`. In our case,
182 this reads ``W.sub(0)``. (Do *not* use the separate space ``BDM`` as
183 this would mess up the numbering.)
184
185 Next, we need to construct the data for the boundary condition. An
186 essential boundary condition is handled by replacing degrees of
187 freedom by the degrees of freedom evaluated at the given data. The
188 :math:`BDM` finite element spaces are vector-valued spaces and hence
189 the degrees of freedom act on vector-valued objects. The effect is
190 that the user is required to construct a :math:`G` such that :math:`G
191 \cdot n = g`. Such a :math:`G` can be constructed by letting :math:`G
192 = g n`. In particular, it can be created by subclassing the
193 :py:class:`Expression <dolfin.functions.expression.Expression>`
194 class. Overloading the ``eval_cell`` method (instead of the usual
195 ``eval``) allows us to extract more geometry information such as the
196 facet normals. Since this is a vector-valued expression, we also need
197 to overload the ``value_shape`` method.
198
199 .. index::
200 single: Expression; (in Mixed Poisson demo)
201
202 ::
203
204 # Define function G such that G \cdot n = g
205 class BoundarySource(UserExpression):
206 def __init__(self, mesh, **kwargs):
207 self.mesh = mesh
208 super().__init__(**kwargs)
209 def eval_cell(self, values, x, ufc_cell):
210 cell = Cell(self.mesh, ufc_cell.index)
211 n = cell.normal(ufc_cell.local_facet)
212 g = sin(5*x[0])
213 values[0] = g*n[0]
214 values[1] = g*n[1]
215 def value_shape(self):
216 return (2,)
217
218 G = BoundarySource(mesh, degree=2)
219
220 Specifying the relevant part of the boundary can be done as for the
221 Poisson demo (but now the top and bottom of the unit square is the
222 essential boundary): ::
223
224 # Define essential boundary
225 def boundary(x):
226 return x[1] < DOLFIN_EPS or x[1] > 1.0 - DOLFIN_EPS
227
228 Now, all the pieces are in place for the construction of the essential
229 boundary condition: ::
230
231 bc = DirichletBC(W.sub(0), G, boundary)
232
233 To compute the solution we use the bilinear and linear forms, and the
234 boundary condition, but we also need to create a :py:class:`Function
235 <dolfin.functions.function.Function>` to store the solution(s). The
236 (full) solution will be stored in the ``w``, which we initialise using
237 the :py:class:`FunctionSpace
238 <dolfin.functions.functionspace.FunctionSpace>` ``W``. The actual
239 computation is performed by calling :py:func:`solve
240 <dolfin.fem.solving.solve>`. The separate components ``sigma`` and
241 ``u`` of the solution can be extracted by calling the :py:func:`split
242 <dolfin.functions.function.Function.split>` function. Finally, we plot
243 the solutions to examine the result. ::
244
245 # Compute solution
246 w = Function(W)
247 solve(a == L, w, bc)
248 (sigma, u) = w.split()
249
250 # Plot sigma and u
251 plt.figure()
252 plot(sigma)
253
254 plt.figure()
255 plot(u)
256
257 plt.show()