Debian Codegraph @

python/demo/documented/stokes-iterative/demo_stokes-iterative.py.rst

raw
1
2 .. _demo_pde_iterative_stokes_python_documentation:
3
4 Stokes equations with an iterative solver
5 =========================================
6
7 This demo is implemented in a single Python file,
8 :download:`demo_stokes-iterative.py`, which contains both the
9 variational forms and the solver.
10
11 This demo illustrates how to:
12
13 * Read mesh and subdomains from file
14 * Use mixed function spaces
15
16
17 Equation and problem definition
18 -------------------------------
19
20 Strong formulation
21 ^^^^^^^^^^^^^^^^^^
22
23 .. math::
24 - \nabla \cdot (\nabla u + p I) &= f \quad {\rm in} \ \Omega, \\
25 \nabla \cdot u &= 0 \quad {\rm in} \ \Omega. \\
26
27 .. note::
28 The sign of the pressure has been flipped from the classical
29 definition. This is done in order to have a symmetric (but not
30 positive-definite) system of equations rather than a
31 non-symmetric (but positive-definite) system of equations.
32
33 A typical set of boundary conditions on the boundary :math:`\partial
34 \Omega = \Gamma_{D} \cup \Gamma_{N}` can be:
35
36 .. math::
37 u &= u_0 \quad {\rm on} \ \Gamma_{D}, \\
38 \nabla u \cdot n + p n &= g \, \quad\;\; {\rm on} \ \Gamma_{N}. \\
39
40
41 Weak formulation
42 ^^^^^^^^^^^^^^^^
43
44 The Stokes equations can easily be formulated in a mixed variational
45 form; that is, a form where the two variables, the velocity and the
46 pressure, are approximated simultaneously. Using the abstract
47 framework, we have the problem: find :math:`(u, p) \in W` such that
48
49 .. math::
50 a((u, p), (v, q)) = L((v, q))
51
52 for all :math:`(v, q) \in W`, where
53
54 .. math::
55
56 a((u, p), (v, q))
57 &= \int_{\Omega} \nabla u \cdot \nabla v
58 - \nabla \cdot v \ p
59 + \nabla \cdot u \ q \, {\rm d} x, \\
60 L((v, q))
61 &= \int_{\Omega} f \cdot v \, {\rm d} x
62 + \int_{\partial \Omega_N} g \cdot v \, {\rm d} s. \\
63
64 The space :math:`W` should be a mixed (product) function space
65 :math:`W = V \times Q`, such that :math:`u \in V` and :math:`q \in Q`.
66
67
68 Domain and boundary conditions
69 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
70
71 In this demo, we shall consider the following definitions of the input
72 functions, the domain, and the boundaries:
73
74 * :math:`\Omega = [0,1]\times[0,1] \backslash {\rm dolphin}` (a unit
75 cube)
76 * :math:`\Gamma_D =`
77 * :math:`\Gamma_N =`
78 * :math:`u_0 = (- \sin(\pi x_1), 0.0)` for :math:`x_0 = 1` and
79 :math:`u_0 = (0.0, 0.0)` otherwise
80 * :math:`f = (0.0, 0.0)`
81 * :math:`g = (0.0, 0.0)`
82
83
84 Implementation
85 --------------
86
87 The Stokes equations as formulated above result in a system of linear
88 equations that is not positive-definite. Standard iterative linear
89 solvers typically fail to converge for such systems. Some care must
90 therefore be taken in preconditioning the systems of
91 equations. Moreover, not all of the linear algebra backends support
92 this. We therefore start by checking that either "PETSc" or "Tpetra"
93 (from Trilinos) is available. We also try to pick MINRES Krylov
94 subspace method which is suitable for symmetric indefinite problems.
95 If not available, costly QMR method is choosen. ::
96
97 from dolfin import *
98
99 # Test for PETSc or Tpetra
100 if not has_linear_algebra_backend("PETSc") and not has_linear_algebra_backend("Tpetra"):
101 info("DOLFIN has not been configured with Trilinos or PETSc. Exiting.")
102 exit()
103
104 if not has_krylov_solver_preconditioner("amg"):
105 info("Sorry, this demo is only available when DOLFIN is compiled with AMG "
106 "preconditioner, Hypre or ML.")
107 exit()
108
109 if has_krylov_solver_method("minres"):
110 krylov_method = "minres"
111 elif has_krylov_solver_method("tfqmr"):
112 krylov_method = "tfqmr"
113 else:
114 info("Default linear algebra backend was not compiled with MINRES or TFQMR "
115 "Krylov subspace method. Terminating.")
116 exit()
117
118 Next, we define the mesh (a :py:class:`UnitCubeMesh
119 <dolfin.cpp.UnitCubeMesh>`) and a mixed finite element ``TH``. Then
120 we build a :py:class:`FunctionSpace
121 <dolfin.functions.functionspace.FunctionSpace>` on this element.
122 (This mixed finite element space is known as the Taylor--Hood elements
123 and is a stable, standard element pair for the Stokes equations.) ::
124
125 # Load mesh
126 mesh = UnitCubeMesh.create(16, 16, 16, CellType.Type.hexahedron)
127
128 # Build function space
129 P2 = VectorElement("Lagrange", mesh.ufl_cell(), 2)
130 P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
131 TH = P2 * P1
132 W = FunctionSpace(mesh, TH)
133
134 Next, we define the boundary conditions. ::
135
136 # Boundaries
137 def right(x, on_boundary): return x[0] > (1.0 - DOLFIN_EPS)
138 def left(x, on_boundary): return x[0] < DOLFIN_EPS
139 def top_bottom(x, on_boundary):
140 return x[1] > 1.0 - DOLFIN_EPS or x[1] < DOLFIN_EPS
141
142 # No-slip boundary condition for velocity
143 noslip = Constant((0.0, 0.0, 0.0))
144 bc0 = DirichletBC(W.sub(0), noslip, top_bottom)
145
146 # Inflow boundary condition for velocity
147 inflow = Expression(("-sin(x[1]*pi)", "0.0", "0.0"), degree=2)
148 bc1 = DirichletBC(W.sub(0), inflow, right)
149
150 # Collect boundary conditions
151 bcs = [bc0, bc1]
152
153 The bilinear and linear forms corresponding to the weak mixed
154 formulation of the Stokes equations are defined as follows: ::
155
156 # Define variational problem
157 (u, p) = TrialFunctions(W)
158 (v, q) = TestFunctions(W)
159 f = Constant((0.0, 0.0, 0.0))
160 a = inner(grad(u), grad(v))*dx + div(v)*p*dx + q*div(u)*dx
161 L = inner(f, v)*dx
162
163
164 We can now use the same :py:class:`TrialFunctions
165 <dolfin.functions.function.TrialFunction>` and
166 :py:class:`TestFunctions <dolfin.functions.function.TestFunction>` to
167 define the preconditioner matrix. We first define the form
168 corresponding to the expression for the preconditioner (given in the
169 initial description above): ::
170
171 # Form for use in constructing preconditioner matrix
172 b = inner(grad(u), grad(v))*dx + p*q*dx
173
174 Next, we want to assemble the matrix corresponding to the bilinear
175 form and the vector corresponding to the linear form of the Stokes
176 equations. Moreover, we want to apply the specified boundary
177 conditions to the linear system. However, :py:func:`assembling
178 <dolfin.fem.assembling.assemble>` the matrix and vector and applying a
179 :py:func:`DirichletBC <dolfin.fem.bcs.DirichletBC>` separately will
180 possibly result in a non-symmetric system of equations. Instead, we
181 can use the :py:func:`assemble_system
182 <dolfin.fem.assembling.assemble_system>` function to assemble both the
183 matrix ``A``, the vector ``bb``, and apply the boundary conditions
184 ``bcs`` in a symmetric fashion: ::
185
186 # Assemble system
187 A, bb = assemble_system(a, L, bcs)
188
189 We do the same for the preconditioner matrix ``P`` using the linear
190 form ``L`` as a dummy form: ::
191
192 # Assemble preconditioner system
193 P, btmp = assemble_system(b, L, bcs)
194
195 Next, we specify the iterative solver we want to use, in this case a
196 :py:class:`KrylovSolver <dolfin.cpp.KrylovSolver>`. We associate the
197 left-hand side matrix ``A`` and the preconditioner matrix ``P`` with
198 the solver by calling :py:func:`solver.set_operators
199 <dolfin.cpp.GenericLinearSolver.set_operators>`. ::
200
201 # Create Krylov solver and AMG preconditioner
202 solver = KrylovSolver(krylov_method, "amg")
203
204 # Associate operator (A) and preconditioner matrix (P)
205 solver.set_operators(A, P)
206
207 We are now almost ready to solve the linear system of equations. It
208 remains to specify a :py:class:`Vector <dolfin.cpp.Vector>` for
209 storing the result. For easy manipulation later, we can define a
210 :py:class:`Function <dolfin.functions.function.Function>` and use the
211 vector associated with this Function. The call to
212 :py:func:`solver.solve <dolfin.cpp.KrylovSolver.solve>` then looks as
213 follows ::
214
215 # Solve
216 U = Function(W)
217 solver.solve(U.vector(), bb)
218
219 Finally, we can play with the result in different ways: ::
220
221 # Get sub-functions
222 u, p = U.split()
223
224 # Save solution in VTK format
225 ufile_pvd = File("velocity.pvd")
226 ufile_pvd << u
227 pfile_pvd = File("pressure.pvd")
228 pfile_pvd << p