| 1 |
"""Unit test for the SNES nonlinear solver""" |
| 2 |
|
| 3 |
# Copyright (C) 2012 Patrick E. Farrell |
| 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 |
# First added: 2012-10-17 |
| 21 |
# Last changed: 2016-10-26 |
| 22 |
|
| 23 |
"""Solve the Yamabe PDE which arises in the differential geometry of |
| 24 |
general relativity. http://arxiv.org/abs/1107.0360. |
| 25 |
|
| 26 |
The Yamabe equation is highly nonlinear and supports many |
| 27 |
solutions. However, only one of these is of physical relevance -- the |
| 28 |
positive solution. |
| 29 |
|
| 30 |
This unit test demonstrates the capability of the SNES solver to |
| 31 |
accept bounds on the resulting solution. The plain Newton method |
| 32 |
converges to an unphysical negative solution, while the SNES solution |
| 33 |
with {sign: nonnegative} converges to the physical positive solution. |
| 34 |
|
| 35 |
An alternative interface to SNESVI allows the user to set explicitly |
| 36 |
more complex bounds as GenericVectors or Function. |
| 37 |
""" |
| 38 |
|
| 39 |
from dolfin import * |
| 40 |
import pytest |
| 41 |
import os |
| 42 |
import numbers |
| 43 |
|
| 44 |
from dolfin_utils.test import * |
| 45 |
|
| 46 |
parameter_degree = set_parameters_fixture("form_compiler.quadrature_degree", \ |
| 47 |
[5]) |
| 48 |
parameter_backend = set_parameters_fixture("linear_algebra_backend", ["PETSc"]) |
| 49 |
|
| 50 |
@fixture |
| 51 |
def mesh(datadir): |
| 52 |
return Mesh(os.path.join(datadir, "doughnut.xml")) |
| 53 |
|
| 54 |
@fixture |
| 55 |
def V(mesh): |
| 56 |
return FunctionSpace(mesh, "CG", 1) |
| 57 |
|
| 58 |
@fixture |
| 59 |
def bcs(V): |
| 60 |
return [DirichletBC(V, 1.0, "on_boundary")] |
| 61 |
|
| 62 |
@fixture |
| 63 |
def u(V): |
| 64 |
u = Function(V) |
| 65 |
u.interpolate(Constant(-1000.0)) |
| 66 |
return u |
| 67 |
|
| 68 |
@fixture |
| 69 |
def v(V): |
| 70 |
return TestFunction(V) |
| 71 |
|
| 72 |
@fixture |
| 73 |
def F(u, v, mesh): |
| 74 |
x = SpatialCoordinate(mesh) |
| 75 |
r = sqrt(x[0]**2 + x[1]**2) |
| 76 |
rho = 1.0/r**3 |
| 77 |
|
| 78 |
return (8*inner(grad(u), grad(v))*dx + rho * inner(u**5, v)*dx \ |
| 79 |
+ (-1.0/8.0)*inner(u, v)*dx) |
| 80 |
|
| 81 |
@fixture |
| 82 |
def J(V, u, F): |
| 83 |
du = TrialFunction(V) |
| 84 |
return derivative(F, u, du) |
| 85 |
|
| 86 |
@fixture |
| 87 |
def lb(V): |
| 88 |
return interpolate(Constant(0.), V) |
| 89 |
|
| 90 |
@fixture |
| 91 |
def ub(V): |
| 92 |
return interpolate(Constant(100.), V) |
| 93 |
|
| 94 |
@fixture |
| 95 |
def newton_solver_parameters(): |
| 96 |
return{"nonlinear_solver": "newton", |
| 97 |
"newton_solver": {"linear_solver": "lu", |
| 98 |
"maximum_iterations": 100, |
| 99 |
"report": False}} |
| 100 |
|
| 101 |
@fixture |
| 102 |
def snes_solver_parameters_sign(): |
| 103 |
return {"nonlinear_solver": "snes", |
| 104 |
"snes_solver": {"linear_solver": "lu", |
| 105 |
"maximum_iterations": 100, |
| 106 |
"sign": "nonnegative", |
| 107 |
"report": True}} |
| 108 |
|
| 109 |
@fixture |
| 110 |
def snes_solver_parameters_bounds(): |
| 111 |
return {"nonlinear_solver": "snes", |
| 112 |
"snes_solver": {"linear_solver": "lu", |
| 113 |
"maximum_iterations": 100, |
| 114 |
"sign": "default", |
| 115 |
"report": True}} |
| 116 |
|
| 117 |
|
| 118 |
@skip_if_not_PETSc |
| 119 |
def test_snes_solver(F, bcs, u, snes_solver_parameters_sign, parameter_degree,\ |
| 120 |
parameter_backend): |
| 121 |
u.interpolate(Constant(-1000.0)) |
| 122 |
solve(F == 0, u, bcs, solver_parameters=snes_solver_parameters_sign) |
| 123 |
assert u.vector().min() >= 0 |
| 124 |
|
| 125 |
|
| 126 |
@skip_if_not_PETSc |
| 127 |
def test_newton_solver(F, u, bcs, newton_solver_parameters, parameter_degree,\ |
| 128 |
parameter_backend): |
| 129 |
u.interpolate(Constant(-1000.0)) |
| 130 |
solve(F == 0, u, bcs, solver_parameters=newton_solver_parameters) |
| 131 |
assert u.vector().min() < 0 |
| 132 |
|
| 133 |
|
| 134 |
@skip_if_not_PETSc |
| 135 |
@pytest.mark.skipif(not has_krylov_solver_preconditioner('amg'), |
| 136 |
reason="This test requires amg.") |
| 137 |
def test_preconditioner_interface(V, parameter_backend): |
| 138 |
"Test nonlinear solvers preconditioner interface" |
| 139 |
class Problem(NonlinearProblem): |
| 140 |
def __init__(self, V): |
| 141 |
NonlinearProblem.__init__(self) |
| 142 |
|
| 143 |
u = Function(V) |
| 144 |
u_ = TrialFunction(V) |
| 145 |
v = TestFunction(V) |
| 146 |
L = Constant(1.0)*v*dx |
| 147 |
bc = DirichletBC(V, 0, lambda x, b: b) |
| 148 |
|
| 149 |
# Nonlinear problem and its Jacobian |
| 150 |
F = (1.0+u*u)*inner(grad(u), grad(v))*dx - L |
| 151 |
J = derivative(F, u) |
| 152 |
|
| 153 |
# Preconditioner form |
| 154 |
J_pc = inner(grad(u_), grad(v))*dx |
| 155 |
|
| 156 |
assembler = SystemAssembler(J, F, bc) |
| 157 |
assembler_pc = SystemAssembler(J_pc, F, bc) |
| 158 |
|
| 159 |
self.u = u |
| 160 |
self.assembler = assembler |
| 161 |
self.assembler_pc = assembler_pc |
| 162 |
|
| 163 |
def F(self, b, x): |
| 164 |
self.assembler.assemble(b, x) |
| 165 |
|
| 166 |
def J(self, A, x): |
| 167 |
self.assembler.assemble(A) |
| 168 |
|
| 169 |
def J_pc(self, P, x): |
| 170 |
# Assemble preconditioner only once |
| 171 |
if P.empty(): |
| 172 |
self.assembler_pc.assemble(P) |
| 173 |
del self.assembler_pc |
| 174 |
|
| 175 |
class MyNewtonSolver(NewtonSolver): |
| 176 |
def converged(self, r, p, i): |
| 177 |
self._converged_called = True |
| 178 |
assert isinstance(r, GenericVector) |
| 179 |
assert isinstance(p, NonlinearProblem) |
| 180 |
assert isinstance(i, numbers.Integral) |
| 181 |
return super(MyNewtonSolver, self).converged(r, p, i) |
| 182 |
def solver_setup(self, A, J, p, i): |
| 183 |
self._solver_setup_called = True |
| 184 |
assert isinstance(A, cpp.la.GenericMatrix) |
| 185 |
assert isinstance(J, cpp.la.GenericMatrix) |
| 186 |
assert isinstance(p, NonlinearProblem) |
| 187 |
assert isinstance(i, numbers.Integral) |
| 188 |
super(MyNewtonSolver, self).solver_setup(A, J, p, i) |
| 189 |
def update_solution(self, x, dx, rp, p, i): |
| 190 |
self._update_solution_called = True |
| 191 |
assert isinstance(x, cpp.la.GenericVector) |
| 192 |
assert isinstance(dx, cpp.la.GenericVector) |
| 193 |
assert isinstance(rp, float) |
| 194 |
assert isinstance(p, NonlinearProblem) |
| 195 |
assert isinstance(i, numbers.Integral) |
| 196 |
super(MyNewtonSolver, self).update_solution(x, dx, rp, p, i) |
| 197 |
@property |
| 198 |
def check_overloads_called(self): |
| 199 |
assert getattr(self, "_converged_called", False) |
| 200 |
assert getattr(self, "_solver_setup_called", False) |
| 201 |
assert getattr(self, "_update_solution_called", False) |
| 202 |
|
| 203 |
class MyPETScNewtonSolver(MyNewtonSolver): |
| 204 |
def __init__(self): |
| 205 |
self.petsc_solver = PETScKrylovSolver() |
| 206 |
NewtonSolver.__init__(self, V.mesh().mpi_comm(), |
| 207 |
self.petsc_solver, PETScFactory.instance()) |
| 208 |
def update_solution(self, x, dx, rp, p, i): |
| 209 |
self._update_solution_called = True |
| 210 |
assert self.linear_solver() is self.petsc_solver |
| 211 |
super().update_solution(x, dx, rp, p, i) |
| 212 |
|
| 213 |
|
| 214 |
for solverclass in [NewtonSolver, MyNewtonSolver, MyPETScNewtonSolver, PETScSNESSolver]: |
| 215 |
problem = Problem(V) |
| 216 |
x = problem.u.vector() |
| 217 |
|
| 218 |
solver = solverclass() |
| 219 |
solver.parameters["linear_solver"] = "cg" |
| 220 |
solver.parameters["preconditioner"] = "amg" |
| 221 |
solver.parameters["krylov_solver"]["monitor_convergence"] = True |
| 222 |
solver.parameters["report"] = True |
| 223 |
|
| 224 |
# Check that subsequent solutions work and reuse preconditioner |
| 225 |
solver.solve(problem, x) |
| 226 |
x.zero() |
| 227 |
solver.solve(problem, x) |
| 228 |
|
| 229 |
# Check that overloading NewtonSolver members works |
| 230 |
getattr(solver, "check_overloads_called", None) |
| 231 |
|
| 232 |
|
| 233 |
@skip_if_not_PETSc |
| 234 |
def test_snes_solver_bound_vectors(F, u, bcs, J, |
| 235 |
snes_solver_parameters_bounds, |
| 236 |
lb, ub, parameter_degree, |
| 237 |
parameter_backend): |
| 238 |
u.interpolate(Constant(-1000.0)) |
| 239 |
problem = NonlinearVariationalProblem(F, u, bcs, J) |
| 240 |
problem.set_bounds(lb, ub) |
| 241 |
|
| 242 |
solver = NonlinearVariationalSolver(problem) |
| 243 |
solver.parameters.update(snes_solver_parameters_bounds) |
| 244 |
u.interpolate(Constant(-1000.0)) |
| 245 |
solver.solve() |
| 246 |
u.interpolate(Constant(-1000.0)) |
| 247 |
solver.solve() |
| 248 |
assert u.vector().min() >= 0 |
| 249 |
|
| 250 |
|
| 251 |
@skip_if_not_PETSc |
| 252 |
def test_snes_solver_bound_vectors(F, u, bcs, J, |
| 253 |
snes_solver_parameters_bounds, |
| 254 |
lb, ub, parameter_degree, |
| 255 |
parameter_backend): |
| 256 |
problem = NonlinearVariationalProblem(F, u, bcs, J) |
| 257 |
problem.set_bounds(lb, ub) |
| 258 |
|
| 259 |
solver = NonlinearVariationalSolver(problem) |
| 260 |
solver.parameters.update(snes_solver_parameters_bounds) |
| 261 |
u.interpolate(Constant(-1000.0)) |
| 262 |
solver.solve() |
| 263 |
u.interpolate(Constant(-1000.0)) |
| 264 |
solver.solve() |
| 265 |
assert u.vector().min() >= 0 |
| 266 |
|
| 267 |
|
| 268 |
@skip_if_not_PETSc |
| 269 |
def test_snes_set_from_options(): |
| 270 |
solver = PETScSNESSolver() |
| •271 |
# The default constructor of PETScSNESSolver initializes an empty jacobian matrix, as well as its preconditioner |
| 272 |
# (when running in serial). Instead, PETSc >= 3.19 requires them to be non empty upon setting from options. |
| 273 |
# We thus fetch them and set a mock size. Note that this will not be a problem in practice for users because |
| 274 |
# PETScSNESSolver::init does allocate the matrix before setting from options. |
| 275 |
(A, J, _) = solver.snes().getJacobian() |
| 276 |
A.setSizes(1, 1) |
| 277 |
J.setSizes(1, 1) |
| 278 |
PETScOptions.set("snes_atol", 1e-12) |
| 279 |
solver.set_from_options() |