| 1 |
"""Unit tests for the KrylovSolver interface""" |
| 2 |
|
| 3 |
# Copyright (C) 2014 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 |
import pytest |
| 21 |
from dolfin import * |
| 22 |
from dolfin_utils.test import skip_if_not_PETSc, skip_in_parallel, pushpop_parameters |
| 23 |
|
| 24 |
|
| 25 |
@skip_if_not_PETSc |
| 26 |
def test_krylov_samg_solver_elasticity(pushpop_parameters): |
| 27 |
"Test PETScKrylovSolver with smoothed aggregation AMG" |
| 28 |
|
| 29 |
# Set backend |
| 30 |
parameters["linear_algebra_backend"] = "PETSc" |
| 31 |
|
| 32 |
def build_nullspace(V, x): |
| 33 |
"""Function to build null space for 2D elasticity""" |
| 34 |
|
| 35 |
# Create list of vectors for null space |
| 36 |
nullspace_basis = [x.copy() for i in range(3)] |
| 37 |
|
| 38 |
# Build translational null space basis |
| 39 |
V.sub(0).dofmap().set(nullspace_basis[0], 1.0) |
| 40 |
V.sub(1).dofmap().set(nullspace_basis[1], 1.0) |
| 41 |
|
| 42 |
# Build rotational null space basis |
| 43 |
V.sub(0).set_x(nullspace_basis[2], -1.0, 1) |
| 44 |
V.sub(1).set_x(nullspace_basis[2], 1.0, 0) |
| 45 |
|
| 46 |
for x in nullspace_basis: |
| 47 |
x.apply("insert") |
| 48 |
|
| 49 |
null_space = VectorSpaceBasis(nullspace_basis) |
| 50 |
null_space.orthonormalize() |
| 51 |
return null_space |
| 52 |
|
| 53 |
def amg_solve(N, method): |
| 54 |
# Elasticity parameters |
| 55 |
E = 1.0e9 |
| 56 |
nu = 0.3 |
| 57 |
mu = E/(2.0*(1.0 + nu)) |
| 58 |
lmbda = E*nu/((1.0 + nu)*(1.0 - 2.0*nu)) |
| 59 |
|
| 60 |
# Stress computation |
| 61 |
def sigma(v): |
| 62 |
return 2.0*mu*sym(grad(v)) + lmbda*tr(sym(grad(v)))*Identity(2) |
| 63 |
|
| 64 |
# Define problem |
| 65 |
mesh = UnitSquareMesh(N, N) |
| 66 |
V = VectorFunctionSpace(mesh, 'Lagrange', 1) |
| 67 |
bc = DirichletBC(V, Constant((0.0, 0.0)), |
| 68 |
lambda x, on_boundary: on_boundary) |
| 69 |
u = TrialFunction(V) |
| 70 |
v = TestFunction(V) |
| 71 |
|
| 72 |
# Forms |
| 73 |
a, L = inner(sigma(u), grad(v))*dx, dot(Constant((1.0, 1.0)), v)*dx |
| 74 |
|
| 75 |
# Assemble linear algebra objects |
| 76 |
A, b = assemble_system(a, L, bc) |
| 77 |
|
| 78 |
# Create solution function |
| 79 |
u = Function(V) |
| 80 |
|
| 81 |
# Create near null space basis and orthonormalize |
| 82 |
null_space = build_nullspace(V, u.vector()) |
| 83 |
|
| 84 |
# Attached near-null space to matrix |
| 85 |
as_backend_type(A).set_near_nullspace(null_space) |
| 86 |
|
| 87 |
# Test that basis is orthonormal |
| 88 |
assert null_space.is_orthonormal() |
| 89 |
|
| 90 |
# Create PETSC smoothed aggregation AMG preconditioner, and |
| 91 |
# create CG solver |
| 92 |
pc = PETScPreconditioner(method) |
| 93 |
solver = PETScKrylovSolver("cg", pc) |
| 94 |
|
| 95 |
# Set matrix operator |
| 96 |
solver.set_operator(A) |
| 97 |
|
| 98 |
# Compute solution and return number of iterations |
| 99 |
return solver.solve(u.vector(), b) |
| 100 |
|
| 101 |
# Set some multigrid smoother parameters |
| 102 |
PETScOptions.set("mg_levels_ksp_type", "chebyshev") |
| 103 |
PETScOptions.set("mg_levels_pc_type", "jacobi") |
| 104 |
|
| 105 |
# Improve estimate of eigenvalues for Chebyshev smoothing |
| 106 |
PETScOptions.set("mg_levels_esteig_ksp_type", "cg") |
| 107 |
PETScOptions.set("mg_levels_ksp_chebyshev_esteig_steps", 50) |
| 108 |
|
| 109 |
# Build list of smoothed aggregation preconditioners |
| 110 |
methods = ["petsc_amg"] |
| 111 |
# if "ml_amg" in PETScPreconditioner.preconditioners(): |
| 112 |
# methods.append("ml_amg") |
| 113 |
|
| 114 |
# Test iteration count with increasing mesh size for each |
| 115 |
# preconditioner |
| 116 |
for method in methods: |
| 117 |
for N in [8, 16, 32, 64]: |
| 118 |
print("Testing method '{}' with {} x {} mesh".format(method, N, N)) |
| 119 |
niter = amg_solve(N, method) |
| 120 |
assert niter < 18 |
| 121 |
|
| 122 |
|
| 123 |
@skip_if_not_PETSc |
| 124 |
def test_krylov_reuse_pc(): |
| 125 |
"Test preconditioner re-use with PETScKrylovSolver" |
| 126 |
|
| 127 |
# Define problem |
| 128 |
mesh = UnitSquareMesh(8, 8) |
| 129 |
V = FunctionSpace(mesh, 'Lagrange', 1) |
| 130 |
bc = DirichletBC(V, Constant(0.0), lambda x, on_boundary: on_boundary) |
| 131 |
u = TrialFunction(V) |
| 132 |
v = TestFunction(V) |
| 133 |
|
| 134 |
# Forms |
| 135 |
a, L = inner(grad(u), grad(v))*dx, dot(Constant(1.0), v)*dx |
| 136 |
|
| 137 |
A, P = PETScMatrix(), PETScMatrix() |
| 138 |
b = PETScVector() |
| 139 |
|
| 140 |
# Assemble linear algebra objects |
| 141 |
assemble(a, tensor=A) |
| 142 |
assemble(a, tensor=P) |
| 143 |
assemble(L, tensor=b) |
| 144 |
|
| 145 |
# Apply boundary conditions |
| 146 |
bc.apply(A) |
| 147 |
bc.apply(P) |
| 148 |
bc.apply(b) |
| 149 |
|
| 150 |
# Create Krysolv solver and set operators |
| 151 |
solver = PETScKrylovSolver("gmres", "bjacobi") |
| 152 |
solver.set_operators(A, P) |
| 153 |
|
| 154 |
# Solve |
| 155 |
x = PETScVector() |
| 156 |
num_iter_ref = solver.solve(x, b) |
| 157 |
|
| 158 |
# Change preconditioner matrix (bad matrix) and solve (PC will be |
| 159 |
# updated) |
| 160 |
a_p = u*v*dx |
| 161 |
assemble(a_p, tensor=P) |
| 162 |
bc.apply(P) |
| 163 |
x = PETScVector() |
| 164 |
num_iter_mod = solver.solve(x, b) |
| 165 |
assert num_iter_mod > num_iter_ref |
| 166 |
|
| 167 |
# Change preconditioner matrix (good matrix) and solve (PC will be |
| 168 |
# updated) |
| 169 |
a_p = a |
| 170 |
assemble(a_p, tensor=P) |
| 171 |
bc.apply(P) |
| 172 |
x = PETScVector() |
| 173 |
num_iter = solver.solve(x, b) |
| 174 |
assert num_iter == num_iter_ref |
| 175 |
|
| 176 |
# Change preconditioner matrix (bad matrix) and solve (PC will not |
| 177 |
# be updated) |
| 178 |
solver.set_reuse_preconditioner(True) |
| 179 |
a_p = u*v*dx |
| 180 |
assemble(a_p, tensor=P) |
| 181 |
bc.apply(P) |
| 182 |
x = PETScVector() |
| 183 |
num_iter = solver.solve(x, b) |
| 184 |
assert num_iter == num_iter_ref |
| 185 |
|
| 186 |
# Update preconditioner (bad PC, will increase iteration count) |
| 187 |
solver.set_reuse_preconditioner(False) |
| 188 |
x = PETScVector() |
| 189 |
num_iter = solver.solve(x, b) |
| 190 |
assert num_iter == num_iter_mod |
| 191 |
|
| 192 |
|
| 193 |
def test_krylov_tpetra(): |
| 194 |
if not has_linear_algebra_backend("Tpetra"): |
| 195 |
return |
| 196 |
|
| 197 |
mesh = UnitCubeMesh(10, 10, 10) |
| 198 |
Q = FunctionSpace(mesh, "CG", 1) |
| 199 |
v = TestFunction(Q) |
| 200 |
u = TrialFunction(Q) |
| 201 |
a = dot(grad(u), grad(v))*dx |
| 202 |
L = v*dx |
| 203 |
|
| 204 |
def bound(x): |
| 205 |
return x[0] == 0 |
| 206 |
|
| 207 |
bc = DirichletBC(Q, Constant(0.0), bound) |
| 208 |
|
| 209 |
A = TpetraMatrix() |
| 210 |
b = TpetraVector() |
| 211 |
assemble(a, A) |
| 212 |
assemble(L, b) |
| 213 |
bc.apply(A) |
| 214 |
bc.apply(b) |
| 215 |
|
| 216 |
mp = MueluPreconditioner() |
| 217 |
mlp = mp.parameters |
| 218 |
mlp['verbosity'] = 'extreme' |
| 219 |
mlp.add("max_levels", 10) |
| 220 |
mlp.add("coarse:_max_size", 10) |
| 221 |
mlp.add("coarse:_type", "KLU2") |
| 222 |
mlp.add("multigrid_algorithm", "sa") |
| 223 |
mlp.add("aggregation:_type", "uncoupled") |
| 224 |
mlp.add("aggregation:_min_agg_size", 3) |
| 225 |
mlp.add("aggregation:_max_agg_size", 7) |
| 226 |
|
| 227 |
pre_paramList = Parameters("smoother:_pre_params") |
| 228 |
pre_paramList.add("relaxation:_type", "Symmetric Gauss-Seidel") |
| 229 |
pre_paramList.add("relaxation:_sweeps", 1) |
| 230 |
pre_paramList.add("relaxation:_damping_factor", 0.6) |
| 231 |
mlp.add("smoother:_pre_type", "RELAXATION") |
| 232 |
mlp.add(pre_paramList) |
| 233 |
|
| 234 |
post_paramList = Parameters("smoother:_post_params") |
| 235 |
post_paramList.add("relaxation:_type", "Symmetric Gauss-Seidel") |
| 236 |
post_paramList.add("relaxation:_sweeps", 1) |
| 237 |
post_paramList.add("relaxation:_damping_factor", 0.9) |
| 238 |
mlp.add("smoother:_post_type", "RELAXATION") |
| 239 |
mlp.add(post_paramList) |
| 240 |
|
| 241 |
solver = BelosKrylovSolver("cg", mp) |
| 242 |
solver.parameters['relative_tolerance'] = 1e-8 |
| 243 |
solver.parameters['monitor_convergence'] = False |
| 244 |
solver.parameters['belos'].add("Maximum_Iterations", 150) |
| 245 |
|
| 246 |
solver.set_operator(A) |
| 247 |
|
| 248 |
u = TpetraVector() |
| 249 |
n_iter = solver.solve(u, b) |
| 250 |
|
| 251 |
# Number of iterations should be around 15 |
| 252 |
assert n_iter < 50 |