Debian Codegraph @

python/test/unit/la/test_mg_solver.py

raw
1 """Unit tests for geometric multigrid via PETSc"""
2
3 # Copyright (C) 2016 Patrick E. Farrell and Garth N. Wells
4 #
5 # This file is part of DOLFIN.
6 #
7 # DOLFIN is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # DOLFIN is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
19
20
21 from dolfin import *
22 import pytest
23 from dolfin_utils.test import (skip_if_not_PETSc,
24 skip_if_not_petsc4py,
25 pushpop_parameters)
26
27 @pytest.mark.skip # See https://bitbucket.org/fenics-project/dolfin/issues/938
28 @skip_if_not_petsc4py
29 def test_mg_solver_laplace(pushpop_parameters):
30
31 parameters["linear_algebra_backend"] = "PETSc"
32
33 # Create meshes and function spaces
34 meshes = [UnitSquareMesh(N, N) for N in [16, 32, 64]]
35 V = [FunctionSpace(mesh, "Lagrange", 1) for mesh in meshes]
36
37 # Create variational problem on fine grid
38 u, v = TrialFunction(V[-1]), TestFunction(V[-1])
39 a = dot(grad(u), grad(v))*dx
40 L = v*dx
41 bc = DirichletBC(V[-1], Constant(0.0), "on_boundary")
42 A, b = assemble_system(a, L, bc)
43
44 # Create collection of PETSc DM objects
45 dm_collection = PETScDMCollection(V)
46
47 # Create PETSc Krylov solver and set operator
48 solver = PETScKrylovSolver()
49 solver.set_operator(A)
50
51 # Set PETSc solver type
52 PETScOptions.set("ksp_type", "richardson")
53 PETScOptions.set("pc_type", "mg")
54
55 # Set PETSc MG type and levels
56 PETScOptions.set("pc_mg_levels", len(V))
57 PETScOptions.set("pc_mg_galerkin")
58
59 # Set smoother
60 PETScOptions.set("mg_levels_ksp_type", "chebyshev")
61 PETScOptions.set("mg_levels_pc_type", "jacobi")
62
63 # Set tolerance and monitor residual
64 PETScOptions.set("ksp_monitor_true_residual")
65 PETScOptions.set("ksp_atol", 1.0e-12)
66 PETScOptions.set("ksp_rtol", 1.0e-12)
67 solver.set_from_options()
68
69 # Get fine grid DM and attach fine grid DM to solver
70 solver.set_dm(dm_collection.get_dm(-1))
71 solver.set_dm_active(False)
72
73 # Solve
74 x = PETScVector()
75 solver.solve(x, b)
76
77 # Check multigrid solution against LU solver solution
78 solver = LUSolver(A)
79 x_lu = Vector()
80 solver.solve(x_lu, b)
81 assert round((x - x_lu).norm("l2"), 10) == 0
82
83 # Clear all PETSc options
84 from petsc4py import PETSc
85 opts = PETSc.Options()
86 for key in opts.getAll():
87 opts.delValue(key)
88
89
90 @skip_if_not_petsc4py
91 def xtest_mg_solver_stokes(pushpop_parameters):
92
93 parameters["linear_algebra_backend"] = "PETSc"
94
95 mesh0 = UnitCubeMesh(2, 2, 2)
96 mesh1 = UnitCubeMesh(4, 4, 4)
97 mesh2 = UnitCubeMesh(8, 8, 8)
98
99 Ve = VectorElement("CG", mesh0.ufl_cell(), 2)
100 Qe = FiniteElement("CG", mesh0.ufl_cell(), 1)
101 Ze = MixedElement([Ve, Qe])
102
103 Z0 = FunctionSpace(mesh0, Ze)
104 Z1 = FunctionSpace(mesh1, Ze)
105 Z2 = FunctionSpace(mesh2, Ze)
106 W = Z2
107
108 # Boundaries
109 def right(x, on_boundary): return x[0] > (1.0 - DOLFIN_EPS)
110 def left(x, on_boundary): return x[0] < DOLFIN_EPS
111 def top_bottom(x, on_boundary):
112 return x[1] > 1.0 - DOLFIN_EPS or x[1] < DOLFIN_EPS
113
114 # No-slip boundary condition for velocity
115 noslip = Constant((0.0, 0.0, 0.0))
116 bc0 = DirichletBC(W.sub(0), noslip, top_bottom)
117
118 # Inflow boundary condition for velocity
119 inflow = Expression(("-sin(x[1]*pi)", "0.0", "0.0"), degree=2)
120 bc1 = DirichletBC(W.sub(0), inflow, right)
121
122 # Collect boundary conditions
123 bcs = [bc0, bc1]
124
125 # Define variational problem
126 (u, p) = TrialFunctions(W)
127 (v, q) = TestFunctions(W)
128 f = Constant((0.0, 0.0, 0.0))
129 a = inner(grad(u), grad(v))*dx + div(v)*p*dx + q*div(u)*dx
130 L = inner(f, v)*dx
131
132 # Form for use in constructing preconditioner matrix
133 b = inner(grad(u), grad(v))*dx + p*q*dx
134
135 # Assemble system
136 A, bb = assemble_system(a, L, bcs)
137
138 # Assemble preconditioner system
139 P, btmp = assemble_system(b, L, bcs)
140
141 spaces = [Z0, Z1, Z2]
142 dm_collection = PETScDMCollection(spaces)
143
144 solver = PETScKrylovSolver()
145 solver.set_operators(A, P)
146
147 PETScOptions.set("ksp_type", "gcr")
148 PETScOptions.set("pc_type", "mg")
149 PETScOptions.set("pc_mg_levels", 3)
150 PETScOptions.set("pc_mg_galerkin")
151 PETScOptions.set("ksp_monitor_true_residual")
152
153 PETScOptions.set("ksp_atol", 1.0e-10)
154 PETScOptions.set("ksp_rtol", 1.0e-10)
155 solver.set_from_options()
156
157 from petsc4py import PETSc
158
159 ksp = solver.ksp()
160
161 ksp.setDM(dm_collection.dm())
162 ksp.setDMActive(False)
163
164 x = PETScVector()
165 solver.solve(x, bb)
166
167 # Check multigrid solution against LU solver
168 solver = LUSolver(A)
169 x_lu = Vector()
170 solver.solve(x_lu, bb)
171 assert round((x - x_lu).norm("l2"), 10) == 0
172
173 # Clear all PETSc options
174 opts = PETSc.Options()
175 for key in opts.getAll():
176 opts.delValue(key)