Debian Codegraph @

python/demo/documented/navier-stokes/documentation.rst

raw
1 .. Documentation for the incompressible Navier-Stokes demo from DOLFIN.
2
3 .. _demo_pde_navier_stokes_python_documentation:
4
5 Incompressible Navier-Stokes equations
6 ======================================
7
8 This demo is implemented in a single Python file,
9 :download:`demo_navier-stokes.py`, which contains both the variational
10 forms and the solver.
11
12 .. include:: ../common.txt
13
14 Implementation
15 --------------
16
17 This demo is implemented in the :download:`demo_navier-stokes.py` file.
18
19 First, the :py:mod:`dolfin` module is imported:
20
21 .. code-block:: python
22
23 from dolfin import *
24
25 For the parallel case, we turn off log messages from processes other than
26 the the root process to avoid excessive output:
27
28 .. code-block:: python
29
30 # Print log messages only from the root process in parallel
31 parameters["std_out_all_processes"] = False;
32
33 We then load the mesh for the L-shaped domain from file:
34
35 .. code-block:: python
36
37 # Load mesh from file
38 mesh = Mesh("../lshape.xml.gz")
39
40 We next define a pair of function spaces :math:`V` and :math:`Q` for
41 the velocity and pressure, and trial and test functions on these
42 spaces:
43
44 .. code-block:: python
45
46 # Define function spaces (P2-P1)
47 V = VectorFunctionSpace(mesh, "Lagrange", 2)
48 Q = FunctionSpace(mesh, "Lagrange", 1)
49
50 # Define trial and test functions
51 u = TrialFunction(V)
52 p = TrialFunction(Q)
53 v = TestFunction(V)
54 q = TestFunction(Q)
55
56 The time step, the length of the time interval, and the kinematic
57 viscosity are defined by:
58
59 .. code-block:: python
60
61 # Set parameter values
62 dt = 0.01
63 T = 3
64 nu = 0.01
65
66 The time-dependent pressure boundary condition can be defined using
67 the :py:class:`Expression <dolfin.functions.expression.Expression>`
68 class:
69
70 .. code-block:: python
71
72 # Define time-dependent pressure boundary condition
73 p_in = Expression("sin(3.0*t)", t=0.0, degree=2)
74
75 Note that the variable ``t`` is not automatically updated during
76 time-stepping, so we must remember to manually update the value of the
77 current time in each time step.
78
79 We may now define the boundary conditions for the velocity and
80 pressure. We define one no-slip boundary condition for the velocity
81 and a pair of boundary conditions for the pressure at the inflow and
82 outflow boundaries:
83
84 .. code-block:: python
85
86 # Define boundary conditions
87 noslip = DirichletBC(V, (0, 0),
88 "on_boundary && \
89 (x[0] < DOLFIN_EPS | x[1] < DOLFIN_EPS | \
90 (x[0] > 0.5 - DOLFIN_EPS && x[1] > 0.5 - DOLFIN_EPS))")
91 inflow = DirichletBC(Q, p_in, "x[1] > 1.0 - DOLFIN_EPS")
92 outflow = DirichletBC(Q, 0, "x[0] > 1.0 - DOLFIN_EPS")
93 bcu = [noslip]
94 bcp = [inflow, outflow]
95
96
97 We collect the boundary conditions in the two lists ``bcu`` and
98 ``bcp`` so that we may easily iterate over them below when we apply
99 the boundary conditions. This makes it easy to add new boundary
100 conditions or use this demo program to solve the Navier-Stokes
101 equations on other geometries.
102
103 We next define the functions and the coefficients that will be used
104 below:
105
106 .. code-block:: python
107
108 # Create functions
109 u0 = Function(V)
110 u1 = Function(V)
111 p1 = Function(Q)
112
113 # Define coefficients
114 k = Constant(dt)
115 f = Constant((0, 0))
116
117 Note that one may use the time step ``dt`` directly in the
118 form. However, by using the :py:class:`Constant
119 <dolfin.functions.constant.Constant>` class, we may freely change the
120 size of the time step without triggering regeneration of code.
121
122 The next step is now to define the variational problems for the three
123 steps of Chorin's method. We do this by defining a pair of bilinear
124 and linear forms for each step:
125
126 .. code-block:: python
127
128 # Tentative velocity step
129 F1 = (1/k)*inner(u - u0, v)*dx + inner(grad(u0)*u0, v)*dx + \
130 nu*inner(grad(u), grad(v))*dx - inner(f, v)*dx
131 a1 = lhs(F1)
132 L1 = rhs(F1)
133
134 # Pressure update
135 a2 = inner(grad(p), grad(q))*dx
136 L2 = -(1/k)*div(u1)*q*dx
137
138 # Velocity update
139 a3 = inner(u, v)*dx
140 L3 = inner(u1, v)*dx - k*inner(grad(p1), v)*dx
141
142 Since the bilinear forms do not depend on any coefficients that change
143 during time-stepping, the corresponding matrices remain constant. We
144 may therefore assemble these before the time-stepping begins:
145
146 .. code-block:: python
147
148 # Assemble matrices
149 A1 = assemble(a1)
150 A2 = assemble(a2)
151 A3 = assemble(a3)
152
153 # Use amg preconditioner if available
154 prec = "amg" if has_krylov_solver_preconditioner("amg") else "default"
155
156 During time-stepping, we will store the solution in VTK format
157 (readable by MayaVi and Paraview). We therefore create a pair of files
158 that can be used to store the solution. Specifying the ``.pvd`` suffix
159 signals that the solution should be stored in VTK format:
160
161 .. code-block:: python
162
163 # Create files for storing solution
164 ufile = File("results/velocity.pvd")
165 pfile = File("results/pressure.pvd")
166
167 The time-stepping loop is now implemented as follows:
168
169 .. code-block:: python
170
171 # Time-stepping
172 t = dt
173 while t < T + DOLFIN_EPS:
174
175 # Update pressure boundary condition
176 p_in.t = t
177
178 We remember to update the current time for the time-dependent pressure
179 boundary value.
180
181 For each of the three steps of Chorin's method, we assemble the
182 right-hand side, apply boundary conditions and solve a linear
183 system. Note the different use of preconditioners. Incomplete LU
184 factorization is used for the computation of the tentative velocity
185 and the velocity update, while algebraic multigrid is used for the
186 pressure equation if available:
187
188 .. code-block:: python
189
190 # Compute tentative velocity step
191 begin("Computing tentative velocity")
192 b1 = assemble(L1)
193 [bc.apply(A1, b1) for bc in bcu]
194 solve(A1, u1.vector(), b1, "bicgstab", "default")
195 end()
196
197 # Pressure correction
198 begin("Computing pressure correction")
199 b2 = assemble(L2)
200 [bc.apply(A2, b2) for bc in bcp]
201 [bc.apply(p1.vector()) for bc in bcp]
202 solve(A2, p1.vector(), b2, "bicgstab", prec)
203 end()
204
205 # Velocity correction
206 begin("Computing velocity correction")
207 b3 = assemble(L3)
208 [bc.apply(A3, b3) for bc in bcu]
209 solve(A3, u1.vector(), b3, "bicgstab", "default")
210 end()
211
212 Note the use of ``begin`` and ``end``; these improve the readability
213 of the output from the program by adding indentation to diagnostic
214 messages.
215
216 At the end of the time-stepping loop, we plot the solution, store the
217 solution to file, and update values for the next time step:
218
219 .. code-block:: python
220
221 # Plot solution
222 plot(p1, title="Pressure", rescale=True)
223 plot(u1, title="Velocity", rescale=True)
224
225 # Save to file
226 ufile << u1
227 pfile << p1
228
229 # Move to next time step
230 u0.assign(u1)
231 t += dt
232
233 Finally, we call the ``interactive`` function to signal that the plot
234 window should be kept open and allow us to interact with (zoom,
235 rotate) the solution.
236
237 Complete code
238 -------------
239
240 .. literalinclude:: demo_navier-stokes.py
241 :start-after: # Begin demo