~johan-hake/dolfin/general-rk-solver

5841 by Johannes Ring
Change license from LGPL 2.1 to LGPL v3 or later.
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
5922.1.3 by Johannes Ring
Remove extra spaces in license header.
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5841 by Johannes Ring
Change license from LGPL 2.1 to LGPL v3 or later.
13
# GNU Lesser General Public License for more details.
14
#
15
# You should have received a copy of the GNU Lesser General Public License
5922.1.3 by Johannes Ring
Remove extra spaces in license header.
16
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
5959.1.26 by Anders Logg
Updates in Python demos for new variational problem interface
17
#
18
# Modified by Anders Logg 2011
19
#
20
# First added:  2010
6312.1.17 by Anders Logg
Rename fine --> leaf_node, coarse --> root_node, fixing bug 842100
21
# Last changed: 2011-10-04
5475 by Anders Logg
Hand-merge gigantic patch from Marie (excellent work btw). Now passes
22
23
from dolfin import *
24
import time
25
26
class Noslip(SubDomain):
27
    def inside(self, x, on_boundary):
28
        return (x[1] < DOLFIN_EPS or x[1] > 1.0 - DOLFIN_EPS) or \
29
               (on_boundary and abs(x[0] - 1.5) < 0.1 + DOLFIN_EPS)
30
31
class Outflow(SubDomain):
32
    def inside(self, x, on_boundary):
33
        return x[0] > 4.0 - DOLFIN_EPS
34
6242 by Marie E. Rognes
Use "cpp_optimize" = True in auto-adaptive-navier-stokes demo.
35
# Use compiler optimizations
36
parameters["form_compiler"]["cpp_optimize"] = True
37
38
# Allow approximating values for points that may be generated outside
39
# of domain (because of numerical inaccuracies)
5959.2.1 by Marie E. Rognes
Take 1 on getting unified Adaptive*VariationalSolver in place with new
40
parameters["allow_extrapolation"] = True
5664 by Marie E. Rognes
Recompile form files with UFC_DEV set and various minos fixes. Looks
41
5475 by Anders Logg
Hand-merge gigantic patch from Marie (excellent work btw). Now passes
42
# Material parameters
43
nu = Constant(0.02)
44
45
# Mesh
7352.1.201 by Anders Logg
Use common mesh files for C++ and Python demos. Files not yet moved
46
mesh = Mesh("../channel_with_flap.xml.gz")
5475 by Anders Logg
Hand-merge gigantic patch from Marie (excellent work btw). Now passes
47
48
# Define function spaces (Taylor-Hood)
49
V = VectorFunctionSpace(mesh, "CG", 2)
50
Q = FunctionSpace(mesh, "CG", 1)
51
W = V * Q
52
53
# Define unknown and test function(s)
54
(v, q) = TestFunctions(W)
5959.1.44 by Marie E. Rognes
Set Jacobian in auto-adaptive-navier-stokes demo and simplify some
55
w = Function(W)
56
(u, p) = (as_vector((w[0], w[1])), w[2])
5475 by Anders Logg
Hand-merge gigantic patch from Marie (excellent work btw). Now passes
57
58
# Prescribed pressure
59
p0 = Expression("(4.0 - x[0])/4.0")
60
61
# Define variational forms
62
n = FacetNormal(mesh)
7352.2.12 by Martin Sandve Alnaes
Use dx() etc. syntax in adaptive python demos.
63
a = (nu*inner(grad(u), grad(v)) - div(v)*p + q*div(u))*dx()
64
a = a + inner(grad(u)*u, v)*dx()
65
L = - p0*dot(v, n)*ds()
5475 by Anders Logg
Hand-merge gigantic patch from Marie (excellent work btw). Now passes
66
F = a - L
67
68
# Define boundary conditions
69
bc = DirichletBC(W.sub(0), Constant((0.0, 0.0)), Noslip())
70
5993.1.5 by Marie E. Rognes
Add solve(eq, u, bcs, tol, M) to Python interface. Tested for both
71
# Create boundary subdomains
5750 by Marie E. Rognes
Update auto-adaptive navier-stokes demo to use attached
72
outflow = Outflow()
7225 by Johan Hake
Change name of std::size_t type in Python interface:
73
outflow_markers = MeshFunction("size_t", mesh, mesh.topology().dim() - 1)
5750 by Marie E. Rognes
Update auto-adaptive navier-stokes demo to use attached
74
outflow_markers.set_all(1)
75
outflow.mark(outflow_markers, 0)
5921.1.8 by Marie E. Rognes
Update demos.
76
77
# Define new measure with associated subdomains
5959.2.1 by Marie E. Rognes
Take 1 on getting unified Adaptive*VariationalSolver in place with new
78
ds = Measure("ds")[outflow_markers]
5921.1.8 by Marie E. Rognes
Update demos.
79
5993.1.5 by Marie E. Rognes
Add solve(eq, u, bcs, tol, M) to Python interface. Tested for both
80
# Define goal
5959.1.44 by Marie E. Rognes
Set Jacobian in auto-adaptive-navier-stokes demo and simplify some
81
M = u[0]*ds(0)
5959.2.1 by Marie E. Rognes
Take 1 on getting unified Adaptive*VariationalSolver in place with new
82
5993.1.5 by Marie E. Rognes
Add solve(eq, u, bcs, tol, M) to Python interface. Tested for both
83
# Define error tolerance (with respect to goal)
84
tol = 1.e-05
85
86
# If no more control is wanted, do:
7085.1.10 by Marie E. Rognes
Some more demo clean-ups.
87
# solve(F == 0, w, bc, tol=tol, M=M)
5993.1.5 by Marie E. Rognes
Add solve(eq, u, bcs, tol, M) to Python interface. Tested for both
88
6046.1.25 by Anders Logg
Demo update for new interface
89
# Compute Jacobian form
90
J = derivative(F, w)
91
5993.1.5 by Marie E. Rognes
Add solve(eq, u, bcs, tol, M) to Python interface. Tested for both
92
# Define variational problem
6046.1.25 by Anders Logg
Demo update for new interface
93
pde = NonlinearVariationalProblem(F, w, bc, J)
5993.1.5 by Marie E. Rognes
Add solve(eq, u, bcs, tol, M) to Python interface. Tested for both
94
5959.2.1 by Marie E. Rognes
Take 1 on getting unified Adaptive*VariationalSolver in place with new
95
# Define solver
7085.1.6 by Marie E. Rognes
Get Python going again.
96
solver = AdaptiveNonlinearVariationalSolver(pde, M)
5959.2.1 by Marie E. Rognes
Take 1 on getting unified Adaptive*VariationalSolver in place with new
97
98
# Set reference value
99
solver.parameters["reference"] = 0.40863917;
5475 by Anders Logg
Hand-merge gigantic patch from Marie (excellent work btw). Now passes
100
101
# Solve to given tolerance
7085.1.6 by Marie E. Rognes
Get Python going again.
102
solver.solve(tol)
5993.1.5 by Marie E. Rognes
Add solve(eq, u, bcs, tol, M) to Python interface. Tested for both
103
7085.1.10 by Marie E. Rognes
Some more demo clean-ups.
104
# Show solver summary
105
solver.summary();
106
107
# Show all timings
6494.2.1 by Marie E. Rognes
Use list_timings instead of summary in auto-adaptive-navier-stokes
108
list_timings()
6159.1.2 by Marie E. Rognes
Redefine fine() in function_post.i.
109
6159.1.5 by Marie E. Rognes
Undo renaming fine_shared_ptr -> _fine etc, and use Johan's patent for
110
# Extract solutions on coarsest and finest mesh:
6312.1.17 by Anders Logg
Rename fine --> leaf_node, coarse --> root_node, fixing bug 842100
111
(u0, p0) = w.root_node().split()
112
(u1, p1) = w.leaf_node().split()
6159.1.5 by Marie E. Rognes
Undo renaming fine_shared_ptr -> _fine etc, and use Johan's patent for
113
plot(p0, title="Pressure on initial mesh")
114
plot(p1, title="Pressure on final mesh")
6159.1.2 by Marie E. Rognes
Redefine fine() in function_post.i.
115
interactive()