Debian Codegraph @

python/demo/documented/stokes-mini/demo_stokes-mini.py

raw
1 """This demo solves the Stokes equations, using linear elements
2 enriched with a bubble for the velocity and linear elements for the
3 pressure (Mini elements). The sub domains for the different boundary
4 conditions 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 # Begin demo
25
26
27 import matplotlib.pyplot as plt
28 from dolfin import *
29
30 # Load mesh and subdomains
31 mesh = Mesh("../dolfin_fine.xml.gz")
32 sub_domains = MeshFunction("size_t", mesh, "../dolfin_fine_subdomains.xml.gz")
33
34 # Build function spaces on Mini element
35 P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
36 B = FiniteElement("Bubble", mesh.ufl_cell(), mesh.topology().dim() + 1)
37 V = VectorElement(NodalEnrichedElement(P1, B))
38 Q = P1
39 W = FunctionSpace(mesh, V * Q)
40
41 # No-slip boundary condition for velocity
42 bc0 = DirichletBC(W.sub(0), (0, 0), sub_domains, 0)
43
44 # Inflow boundary condition for velocity
45 inflow = Expression(("-sin(x[1]*pi)", "0.0"), degree=2)
46 bc1 = DirichletBC(W.sub(0), inflow, sub_domains, 1)
47
48 # Collect boundary conditions
49 bcs = [bc0, bc1]
50
51 # Define variational problem
52 u, p = TrialFunctions(W)
53 v, q = TestFunctions(W)
54 f = Constant((0, 0))
55 a = (inner(grad(u), grad(v)) - div(v)*p - q*div(u))*dx
56 L = inner(f, v)*dx
57
58 # Compute solution
59 w = Function(W)
60 solve(a == L, w, bcs)
61
62 # Split the mixed solution using deepcopy
63 # (needed for further computation on coefficient vector)
64 u, p = w.split(True)
65
66 print("Norm of velocity coefficient vector: %.15g" % u.vector().norm("l2"))
67 print("Norm of pressure coefficient vector: %.15g" % p.vector().norm("l2"))
68
69 # Split the mixed solution using a shallow copy
70 u, p = w.split()
71
72 # Save solution in VTK format
73 ufile_pvd = File("velocity.pvd")
74 ufile_pvd << u
75 pfile_pvd = File("pressure.pvd")
76 pfile_pvd << p
77
78 # Plot solution
79 plt.figure()
80 plot(u, title="velocity")
81
82 plt.figure()
83 plot(p, title="pressure")
84
85 # Display plots
86 plt.show()