| 1 |
.. Documentation for the DOLFIN Stokes problem with Taylor-Hood elements demo |
| 2 |
|
| 3 |
.. _demo_pde_stokes-taylor-hood_python_documentation: |
| 4 |
|
| 5 |
Stokes equations with Taylor-Hood elements |
| 6 |
========================================== |
| 7 |
|
| 8 |
This demo is implemented in a single Python file, |
| 9 |
:download:`demo_stokes-taylor-hood.py`, which contains both the |
| 10 |
variational form and the solver. |
| 11 |
|
| 12 |
.. include:: ../common.txt |
| 13 |
|
| 14 |
Implementation |
| 15 |
-------------- |
| 16 |
|
| 17 |
First, the :py:mod:`dolfin` module is imported: |
| 18 |
|
| 19 |
.. code-block:: python |
| 20 |
|
| 21 |
from dolfin import * |
| 22 |
|
| 23 |
In this example, different boundary conditions are prescribed on |
| 24 |
different parts of the boundaries. This information must be made |
| 25 |
available to the solver. One way of doing this, is to tag the |
| 26 |
different sub-regions with different (integer) labels. DOLFIN provides |
| 27 |
a class :py:class:`MeshFunction <dolfin.cpp.mesh.MeshFunction>` which |
| 28 |
is useful for these types of operations: instances of this class |
| 29 |
represent functions over mesh entities (such as over cells or over |
| 30 |
facets). Mesh and mesh functions can be read from file in the |
| 31 |
following way: |
| 32 |
|
| 33 |
.. code-block:: python |
| 34 |
|
| 35 |
# Load mesh and subdomains |
| 36 |
mesh = Mesh("../dolfin_fine.xml.gz") |
| 37 |
sub_domains = MeshFunction("size_t", mesh, "../dolfin_fine_subdomains.xml.gz") |
| 38 |
|
| 39 |
Next, we define a :py:class:`FunctionSpace |
| 40 |
<dolfin.functions.functionspace.FunctionSpace>` built on a mixed |
| 41 |
finite element ``TH`` which consists of continuous |
| 42 |
piecewise quadratics and continuous piecewise |
| 43 |
linears. (This mixed finite element space is known as the Taylor–Hood |
| 44 |
elements and is a stable, standard element pair for the Stokes |
| 45 |
equations.) |
| 46 |
|
| 47 |
.. code-block:: python |
| 48 |
|
| 49 |
# Define function spaces |
| 50 |
P2 = VectorElement("Lagrange", mesh.ufl_cell(), 2) |
| 51 |
P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) |
| 52 |
TH = P2 * P1 |
| 53 |
W = FunctionSpace(mesh, TH) |
| 54 |
|
| 55 |
Now that we have our mixed function space and marked subdomains |
| 56 |
defining the boundaries, we define boundary conditions: |
| 57 |
|
| 58 |
.. code-block:: python |
| 59 |
|
| 60 |
# No-slip boundary condition for velocity |
| 61 |
# x1 = 0, x1 = 1 and around the dolphin |
| 62 |
noslip = Constant((0, 0)) |
| 63 |
bc0 = DirichletBC(W.sub(0), noslip, sub_domains, 0) |
| 64 |
|
| 65 |
# Inflow boundary condition for velocity |
| 66 |
# x0 = 1 |
| 67 |
inflow = Expression(("-sin(x[1]*pi)", "0.0"), degree=2) |
| 68 |
bc1 = DirichletBC(W.sub(0), inflow, sub_domains, 1) |
| 69 |
|
| 70 |
# Collect boundary conditions |
| 71 |
bcs = [bc0, bc1] |
| 72 |
|
| 73 |
Here, we have given four arguments in the call to |
| 74 |
:py:class:`DirichletBC <dolfin.cpp.fem.DirichletBC>`. The first |
| 75 |
specifies the :py:class:`FunctionSpace |
| 76 |
<dolfin.cpp.function.FunctionSpace>`. Since we have a |
| 77 |
mixed function space, we write |
| 78 |
``W.sub(0)`` for the velocity component of the space, and |
| 79 |
``W.sub(1)`` for the pressure componnen of the space. |
| 80 |
The second argument specifies the value on the Dirichlet |
| 81 |
boundary. The two last ones specifies the marking of the subdomains; |
| 82 |
``sub_domains`` contains the subdomain markers and the number given as |
| 83 |
the last argument is the subdomain index. |
| 84 |
|
| 85 |
The bilinear and linear forms corresponding to the weak mixed |
| 86 |
formulation of the Stokes equations are defined as follows: |
| 87 |
|
| 88 |
.. code-block:: python |
| 89 |
|
| 90 |
# Define variational problem |
| 91 |
(u, p) = TrialFunctions(W) |
| 92 |
(v, q) = TestFunctions(W) |
| 93 |
f = Constant((0, 0)) |
| 94 |
a = (inner(grad(u), grad(v)) - div(v)*p + q*div(u))*dx |
| 95 |
L = inner(f, v)*dx |
| 96 |
|
| 97 |
To compute the solution we use the bilinear and linear forms, and the |
| 98 |
boundary condition, but we also need to create a :py:class:`Function |
| 99 |
<dolfin.cpp.function.Function>` to store the solution(s). The (full) |
| 100 |
solution will be stored in w, which we initialize using the mixed |
| 101 |
function space ``W``. The actual |
| 102 |
computation is performed by calling solve with the arguments ``a``, |
| 103 |
``L``, ``w`` and ``bcs``. The separate components ``u`` and ``p`` of |
| 104 |
the solution can be extracted by calling the :py:meth:`split |
| 105 |
<dolfin.functions.function.Function.split>` function. Here we use an |
| 106 |
optional argument True in the split function to specify that we want a |
| 107 |
deep copy. If no argument is given we will get a shallow copy. We want |
| 108 |
a deep copy for further computations on the coefficient vectors. |
| 109 |
|
| 110 |
.. code-block:: python |
| 111 |
|
| 112 |
# Compute solution |
| 113 |
w = Function(W) |
| 114 |
solve(a == L, w, bcs) |
| 115 |
|
| 116 |
# Split the mixed solution using deepcopy |
| 117 |
# (needed for further computation on coefficient vector) |
| 118 |
(u, p) = w.split(True) |
| 119 |
|
| 120 |
We may be interested in the :math:`L^2` norms of u and p, they can be |
| 121 |
calculated and printed by writing |
| 122 |
|
| 123 |
.. code-block:: python |
| 124 |
|
| 125 |
print("Norm of velocity coefficient vector: %.15g" % u.vector().norm("l2")) |
| 126 |
print("Norm of pressure coefficient vector: %.15g" % p.vector().norm("l2")) |
| 127 |
|
| 128 |
One can also split functions using shallow copies (which is enough |
| 129 |
when we just plotting the result) by writing |
| 130 |
|
| 131 |
.. code-block:: python |
| 132 |
|
| 133 |
# Split the mixed solution using a shallow copy |
| 134 |
(u, p) = w.split() |
| 135 |
|
| 136 |
Finally, we can store to file and plot the solutions. |
| 137 |
|
| 138 |
.. code-block:: python |
| 139 |
|
| 140 |
# Save solution in VTK format |
| 141 |
ufile_pvd = File("velocity.pvd") |
| 142 |
ufile_pvd << u |
| 143 |
pfile_pvd = File("pressure.pvd") |
| 144 |
pfile_pvd << p |
| 145 |
|
| 146 |
# Plot solution |
| 147 |
plot(u) |
| 148 |
plot(p) |
| 149 |
interactive() |
| 150 |
|
| 151 |
Complete code |
| 152 |
------------- |
| 153 |
|
| 154 |
.. literalinclude:: demo_stokes-taylor-hood.py |
| 155 |
:start-after: # Begin demo |