| 1 |
"""Unit tests for parts of the PETSc interface not tested via the |
| 2 |
GenericFoo interface |
| 3 |
|
| 4 |
""" |
| 5 |
|
| 6 |
# Copyright (C) 2015-2017 Garth N. Wells |
| 7 |
# |
| 8 |
# This file is part of DOLFIN. |
| 9 |
# |
| 10 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 11 |
# it under the terms of the GNU Lesser General Public License as published by |
| 12 |
# the Free Software Foundation, either version 3 of the License, or |
| 13 |
# (at your option) any later version. |
| 14 |
# |
| 15 |
# DOLFIN is distributed in the hope that it will be useful, |
| 16 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 |
# GNU Lesser General Public License for more details. |
| 19 |
# |
| 20 |
# You should have received a copy of the GNU Lesser General Public License |
| 21 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 22 |
|
| 23 |
from dolfin import (UnitSquareMesh, TrialFunction, TestFunction, |
| 24 |
MPI, FunctionSpace, assemble, Constant, dx, |
| 25 |
parameters, has_petsc) |
| 26 |
|
| 27 |
if has_petsc() : |
| 28 |
from dolfin import (PETScVector, PETScMatrix, PETScNestMatrix, |
| 29 |
PETScLUSolver, PETScKrylovSolver) |
| 30 |
|
| 31 |
from dolfin_utils.test import (skip_if_not_PETSc, |
| 32 |
skip_if_not_petsc4py, |
| 33 |
pushpop_parameters) |
| 34 |
|
| 35 |
|
| 36 |
@skip_if_not_PETSc |
| 37 |
def test_vector(): |
| 38 |
"Test PETScVector interface" |
| 39 |
|
| 40 |
prefix = "my_vector_" |
| 41 |
x = PETScVector(MPI.comm_world) |
| 42 |
x.set_options_prefix(prefix) |
| 43 |
|
| 44 |
assert x.get_options_prefix() == prefix |
| 45 |
x.init(300) |
| 46 |
assert x.get_options_prefix() == prefix |
| 47 |
|
| 48 |
|
| 49 |
@skip_if_not_PETSc |
| 50 |
def test_krylov_solver_norm_type(): |
| 51 |
"""Check setting of norm type used in testing for convergence by |
| 52 |
PETScKrylovSolver |
| 53 |
|
| 54 |
""" |
| 55 |
|
| 56 |
norm_type = (PETScKrylovSolver.norm_type.default_norm, |
| 57 |
PETScKrylovSolver.norm_type.natural, |
| 58 |
PETScKrylovSolver.norm_type.preconditioned, |
| 59 |
PETScKrylovSolver.norm_type.none, |
| 60 |
PETScKrylovSolver.norm_type.unpreconditioned) |
| 61 |
|
| 62 |
for norm in norm_type: |
| 63 |
# Solve a system of equations |
| 64 |
mesh = UnitSquareMesh(4, 4) |
| 65 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 66 |
u, v = TrialFunction(V), TestFunction(V) |
| 67 |
a = u*v*dx |
| 68 |
L = Constant(1.0)*v*dx |
| 69 |
A, b = assemble(a), assemble(L) |
| 70 |
|
| 71 |
solver = PETScKrylovSolver("cg") |
| 72 |
solver.parameters["maximum_iterations"] = 2 |
| 73 |
solver.parameters["error_on_nonconvergence"] = False |
| 74 |
solver.set_norm_type(norm) |
| 75 |
solver.set_operator(A) |
| 76 |
solver.solve(b.copy(), b) |
| 77 |
solver.get_norm_type() |
| 78 |
|
| 79 |
if norm is not PETScKrylovSolver.norm_type.default_norm: |
| 80 |
assert solver.get_norm_type() == norm |
| 81 |
|
| 82 |
|
| 83 |
@skip_if_not_PETSc |
| 84 |
def test_krylov_solver_options_prefix(pushpop_parameters): |
| 85 |
"Test set/get PETScKrylov solver prefix option" |
| 86 |
|
| 87 |
# Set backend |
| 88 |
parameters["linear_algebra_backend"] = "PETSc" |
| 89 |
|
| 90 |
# Prefix |
| 91 |
prefix = "test_foo_" |
| 92 |
|
| 93 |
# Create solver and set prefix |
| 94 |
solver = PETScKrylovSolver() |
| 95 |
solver.set_options_prefix(prefix) |
| 96 |
|
| 97 |
# Check prefix (pre solve) |
| 98 |
assert solver.get_options_prefix() == prefix |
| 99 |
|
| 100 |
# Solve a system of equations |
| 101 |
mesh = UnitSquareMesh(4, 4) |
| 102 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 103 |
u, v = TrialFunction(V), TestFunction(V) |
| 104 |
a, L = u*v*dx, Constant(1.0)*v*dx |
| 105 |
A, b = assemble(a), assemble(L) |
| 106 |
solver.set_operator(A) |
| 107 |
solver.solve(b.copy(), b) |
| 108 |
|
| 109 |
# Check prefix (post solve) |
| 110 |
assert solver.get_options_prefix() == prefix |
| 111 |
|
| 112 |
|
| 113 |
@skip_if_not_PETSc |
| 114 |
def test_options_prefix(pushpop_parameters): |
| 115 |
"Test set/get prefix option for PETSc objects" |
| 116 |
|
| 117 |
def run_test(A, init_function): |
| 118 |
# Prefix |
| 119 |
prefix = "test_foo_" |
| 120 |
|
| 121 |
# Set prefix |
| 122 |
A.set_options_prefix(prefix) |
| 123 |
|
| 124 |
# Get prefix (should be empty since vector has been initialised) |
| 125 |
# assert not A.get_options_prefix() |
| 126 |
|
| 127 |
# Initialise vector |
| 128 |
init_function(A) |
| 129 |
|
| 130 |
# Check prefix |
| 131 |
assert A.get_options_prefix() == prefix |
| 132 |
|
| 133 |
# Try changing prefix post-intialisation (should throw error) |
| 134 |
# with pytest.raises(RuntimeError): |
| 135 |
# A.set_options_prefix("test") |
| 136 |
|
| 137 |
# Test vector |
| 138 |
def init_vector(x): |
| 139 |
x.init(100) |
| 140 |
x = PETScVector(MPI.comm_world) |
| 141 |
run_test(x, init_vector) |
| 142 |
|
| 143 |
# Test matrix |
| 144 |
def init_matrix(A): |
| 145 |
mesh = UnitSquareMesh(12, 12) |
| 146 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 147 |
u, v = TrialFunction(V), TestFunction(V) |
| 148 |
assemble(u*v*dx, tensor=A) |
| 149 |
A = PETScMatrix() |
| 150 |
run_test(A, init_matrix) |
| 151 |
|
| 152 |
# FIXME: Need to straighten out the calls to PETSc |
| 153 |
# FooSetFromOptions calls in the solver wrapers |
| 154 |
|
| 155 |
# Test solvers |
| 156 |
# def init_solver(A, solver): |
| 157 |
# A = PETScMatrix() |
| 158 |
# init_matrix(A) |
| 159 |
# solver.set_operator(A) |
| 160 |
|
| 161 |
# Test Krylov solver |
| 162 |
# solver = PETScKrylovSolver() |
| 163 |
# run_test(solver, init_solver) |
| 164 |
|
| 165 |
# Test KY solver |
| 166 |
# solver = PETScLUSolver() |
| 167 |
# run_test(solver, init_solver) |
| 168 |
|
| 169 |
@skip_if_not_PETSc |
| 170 |
def test_nest_matrix(pushpop_parameters): |
| 171 |
|
| 172 |
# Create Matrices and insert into nest |
| 173 |
A00 = PETScMatrix() |
| 174 |
A01 = PETScMatrix() |
| 175 |
A10 = PETScMatrix() |
| 176 |
mesh = UnitSquareMesh(12, 12) |
| 177 |
V = FunctionSpace(mesh, "Lagrange", 2) |
| 178 |
Q = FunctionSpace(mesh, "Lagrange", 1) |
| 179 |
u, v = TrialFunction(V), TestFunction(V) |
| 180 |
p, q = TrialFunction(Q), TestFunction(Q) |
| 181 |
assemble(u*v*dx, tensor=A00) |
| 182 |
assemble(p*v*dx, tensor=A01) |
| 183 |
assemble(u*q*dx, tensor=A10) |
| 184 |
|
| 185 |
AA = PETScNestMatrix([A00, A01, A10, None]) |
| 186 |
|
| 187 |
# Create compatible RHS Vectors and insert into nest |
| 188 |
u = PETScVector() |
| 189 |
p = PETScVector() |
| 190 |
x = PETScVector() |
| 191 |
|
| 192 |
A00.init_vector(u, 1) |
| 193 |
A01.init_vector(p, 1) |
| 194 |
AA.init_vectors(x, [u, p]) |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
@skip_if_not_petsc4py |
| 199 |
def test_lu_cholesky(): |
| 200 |
"""Test that PETScLUSolver selects LU or Cholesky solver based on |
| 201 |
symmetry of matrix operator. |
| 202 |
|
| 203 |
""" |
| 204 |
|
| 205 |
from petsc4py import PETSc |
| 206 |
|
| 207 |
mesh = UnitSquareMesh(MPI.comm_world, 12, 12) |
| 208 |
V = FunctionSpace(mesh, "Lagrange", 1) |
| 209 |
u, v = TrialFunction(V), TestFunction(V) |
| 210 |
A = PETScMatrix(mesh.mpi_comm()) |
| 211 |
assemble(Constant(1.0)*u*v*dx, tensor=A) |
| 212 |
|
| 213 |
# Check that solver type is LU |
| 214 |
solver = PETScLUSolver(mesh.mpi_comm(), A, "petsc") |
| 215 |
pc_type = solver.ksp().getPC().getType() |
| 216 |
assert pc_type == "lu" |
| 217 |
|
| 218 |
# Set symmetry flag |
| 219 |
A.mat().setOption(PETSc.Mat.Option.SYMMETRIC, True) |
| 220 |
|
| 221 |
# Check symmetry flags |
| 222 |
symm = A.mat().isSymmetricKnown() |
| 223 |
assert symm[0] == True |
| 224 |
assert symm[1] == True |
| 225 |
|
| 226 |
# Check that solver type is Cholesky since matrix has now been |
| 227 |
# marked as symmetric |
| 228 |
solver = PETScLUSolver(mesh.mpi_comm(), A, "petsc") |
| 229 |
pc_type = solver.ksp().getPC().getType() |
| 230 |
assert pc_type == "cholesky" |
| 231 |
|
| 232 |
# Re-assemble, which resets symmetry flag |
| 233 |
assemble(Constant(1.0)*u*v*dx, tensor=A) |
| 234 |
solver = PETScLUSolver(mesh.mpi_comm(), A, "petsc") |
| 235 |
pc_type = solver.ksp().getPC().getType() |
| 236 |
assert pc_type == "lu" |