Debian Codegraph @

python/demo/documented/stokes-taylor-hood/demo_stokes-taylor-hood.py

raw
1 """This demo solves the Stokes equations, using quadratic elements for
2 the velocity and first degree elements for the pressure (Taylor-Hood
3 elements). The sub domains for the different boundary conditions
4 used in this simulation are computed by the demo program in
5 src/demo/mesh/subdomains."""
6
7 # Copyright (C) 2007 Kristian B. Oelgaard
8 #
9 # This file is part of DOLFIN.
10 #
11 # DOLFIN is free software: you can redistribute it and/or modify
12 # it under the terms of the GNU Lesser General Public License as published by
13 # the Free Software Foundation, either version 3 of the License, or
14 # (at your option) any later version.
15 #
16 # DOLFIN is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 # GNU Lesser General Public License for more details.
20 #
21 # You should have received a copy of the GNU Lesser General Public License
22 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
23 #
24 # Modified by Anders Logg, 2008-2009.
25 #
26 # First added: 2007-11-16
27 # Last changed: 2009-11-26
28 # Begin demo
29
30
31 import matplotlib.pyplot as plt
32 from dolfin import *
33
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
40 plt.figure()
41 plot(mesh)
42
43 plt.figure()
44 plot(sub_domains)
45
46 # Define function spaces
47 P2 = VectorElement("Lagrange", mesh.ufl_cell(), 2)
48 P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
49 TH = P2 * P1
50 W = FunctionSpace(mesh, TH)
51
52 # No-slip boundary condition for velocity
53 # x1 = 0, x1 = 1 and around the dolphin
54 noslip = Constant((0, 0))
55 bc0 = DirichletBC(W.sub(0), noslip, sub_domains, 0)
56
57 # Inflow boundary condition for velocity
58 # x0 = 1
59 inflow = Expression(("-sin(x[1]*pi)", "0.0"), degree=2)
60 bc1 = DirichletBC(W.sub(0), inflow, sub_domains, 1)
61
62 # Collect boundary conditions
63 bcs = [bc0, bc1]
64
65 # Define variational problem
66 (u, p) = TrialFunctions(W)
67 (v, q) = TestFunctions(W)
68 f = Constant((0, 0))
69 a = (inner(grad(u), grad(v)) - div(v)*p + q*div(u))*dx
70 L = inner(f, v)*dx
71
72 # Compute solution
73 w = Function(W)
74 solve(a == L, w, bcs)
75
76 # Split the mixed solution using deepcopy
77 # (needed for further computation on coefficient vector)
78 (u, p) = w.split(True)
79
80 print("Norm of velocity coefficient vector: %.15g" % u.vector().norm("l2"))
81 print("Norm of pressure coefficient vector: %.15g" % p.vector().norm("l2"))
82
83 # # Split the mixed solution using a shallow copy
84 (u, p) = w.split()
85
86 # Save solution in VTK format
87 ufile_pvd = File("velocity.pvd")
88 ufile_pvd << u
89 pfile_pvd = File("pressure.pvd")
90 pfile_pvd << p
91
92 # Plot solution
93 plt.figure()
94 plot(u, title="velocity")
95
96 plt.figure()
97 plot(p, title="pressure")
98
99 # Display plots
100 plt.show()