Debian Codegraph @

python/demo/undocumented/auto-adaptive-navier-stokes/demo_auto-adaptive-navier-stokes.py

raw
1 # Copyright (C) 2010 Marie E. Rognes
2 #
3 # This file is part of DOLFIN.
4 #
5 # DOLFIN is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # DOLFIN is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Modified by Anders Logg 2011
19 #
20 # First added: 2010
21 # Last changed: 2011-10-04
22
23 from dolfin import *
24 import matplotlib.pyplot as plt
25
26
27 class Noslip(SubDomain):
28 def inside(self, x, on_boundary):
29 return (x[1] < DOLFIN_EPS or x[1] > 1.0 - DOLFIN_EPS) or \
30 (on_boundary and abs(x[0] - 1.5) < 0.1 + DOLFIN_EPS)
31
32 class Outflow(SubDomain):
33 def inside(self, x, on_boundary):
34 return x[0] > 4.0 - DOLFIN_EPS
35
36 # Use compiler optimizations
37 parameters["form_compiler"]["cpp_optimize"] = True
38
39 # Allow approximating values for points that may be generated outside
40 # of domain (because of numerical inaccuracies)
41 parameters["allow_extrapolation"] = True
42 parameters["refinement_algorithm"] = "plaza_with_parent_facets"
43
44 # Material parameters
45 nu = Constant(0.02)
46
47 # Mesh
48 mesh = Mesh("../channel_with_flap.xml.gz")
49
50 # Define function spaces (Taylor-Hood)
51 V = VectorElement("CG", mesh.ufl_cell(), 2)
52 Q = FiniteElement("CG", mesh.ufl_cell(), 1)
53 W = FunctionSpace(mesh, V * Q)
54
55 # Define unknown and test function(s)
56 (v, q) = TestFunctions(W)
57 w = Function(W)
58 (u, p) = (as_vector((w[0], w[1])), w[2])
59
60 # Prescribed pressure
61 p0 = Expression("(4.0 - x[0])/4.0", degree=2)
62
63 # Define variational forms
64 n = FacetNormal(mesh)
65 a = (nu*inner(grad(u), grad(v)) - div(v)*p + q*div(u))*dx()
66 a = a + inner(grad(u)*u, v)*dx()
67 L = - p0*dot(v, n)*ds()
68 F = a - L
69
70 # Define boundary conditions
71 bc = DirichletBC(W.sub(0), Constant((0.0, 0.0)), Noslip())
72
73 # Create boundary subdomains
74 outflow = Outflow()
75 outflow_markers = MeshFunction("size_t", mesh, mesh.topology().dim() - 1)
76 outflow_markers.set_all(1)
77 outflow.mark(outflow_markers, 0)
78
79 # Define new measure with associated subdomains
80 ds = Measure('ds', domain=mesh, subdomain_data=outflow_markers)
81
82 # Define goal
83 M = u[0]*ds(0)
84
85 # Define error tolerance (with respect to goal)
86 tol = 1.e-05
87
88 # If no more control is wanted, do:
89 # solve(F == 0, w, bc, tol=tol, M=M)
90
91 # Compute Jacobian form
92 J = derivative(F, w)
93
94 # Define variational problem
95 pde = NonlinearVariationalProblem(F, w, bc, J)
96
97 # Define solver
98 solver = AdaptiveNonlinearVariationalSolver(pde, M)
99
100 # Set reference value
101 solver.parameters["reference"] = 0.40863917;
102
103 # Solve to given tolerance
104 solver.solve(tol)
105
106 # Show solver summary
107 solver.summary();
108
109 # Show all timings
110 list_timings(TimingClear.clear, [TimingType.wall])
111
112 # Extract solutions on coarsest and finest mesh:
113 (u0, p0) = w.root_node().split()
114 plt.figure()
115 plot(p0, title="Pressure on initial mesh")
116
117 (u1, p1) = w.leaf_node().split()
118 plt.figure()
119 plot(p1, title="Pressure on final mesh")
120
121 plt.show()