Skip to content

Commit

Permalink
check if rtol is available for python 3.8/scipy 1.8.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeolson committed Feb 9, 2024
1 parent 77c117a commit 1e7c709
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
17 changes: 11 additions & 6 deletions pyamg/krylov/tests/test_scipy.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Test scipy methods."""
import inspect
from functools import partial

import numpy as np
Expand Down Expand Up @@ -37,20 +38,24 @@ def cb(x, normb):
x0 = case['x0']
tol = case['tol']

kwargs = dict(tol=tol, restart=3, maxiter=2)

mgsres = []
_ = gmres_mgs(A, b, x0, residuals=mgsres,
tol=tol, restart=3, maxiter=2)
_ = gmres_mgs(A, b, x0, residuals=mgsres, **kwargs)

hhres = []
_ = gmres_householder(A, b, x0, residuals=hhres,
tol=tol, restart=3, maxiter=2)
_ = gmres_householder(A, b, x0, residuals=hhres, **kwargs)

scipyres = []
normb = np.linalg.norm(b)
callback = partial(cb, normb=normb)

_ = sla.gmres(A, b, x0, callback=callback, callback_type='pr_norm',
rtol=tol, atol=0, restart=3, maxiter=2)
# check if scipy gmres has rtol
kwargs['atol'] = 0
if 'rtol' in inspect.getfullargspec(sla.gmres).args:
kwargs['rtol'] = kwargs.pop(tol)

Check warning on line 56 in pyamg/krylov/tests/test_scipy.py

View check run for this annotation

Codecov / codecov/patch

pyamg/krylov/tests/test_scipy.py#L56

Added line #L56 was not covered by tests

_ = sla.gmres(A, b, x0, callback=callback, callback_type='pr_norm', **kwargs)

assert_array_almost_equal(mgsres[1:], scipyres)
assert_array_almost_equal(hhres[1:], scipyres)
11 changes: 9 additions & 2 deletions pyamg/multilevel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Generic AMG solver."""
import inspect

from warnings import warn

Expand Down Expand Up @@ -483,8 +484,14 @@ def callback_wrapper(x):
callback(x)
else:
callback_wrapper = callback

x, info = accel(A, b, x0=x0, rtol=tol, maxiter=maxiter, M=M,

# for scipy solvers, see if rtol is available
kwargs['tol'] = tol
kwargs['atol'] = 0
if 'rtol' in inspect.getfullargspec(accel).args:
kwargs['rtol'] = kwargs.pop(tol)

Check warning on line 492 in pyamg/multilevel.py

View check run for this annotation

Codecov / codecov/patch

pyamg/multilevel.py#L492

Added line #L492 was not covered by tests

x, info = accel(A, b, x0=x0, maxiter=maxiter, M=M,
callback=callback_wrapper, **kwargs)
if return_info:
return x, info
Expand Down
8 changes: 7 additions & 1 deletion pyamg/tests/test_multilevel.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
"""Test MultilevelSolver class."""
import inspect

import numpy as np
from numpy.testing import TestCase, assert_almost_equal, assert_equal
from scipy import sparse
Expand Down Expand Up @@ -53,9 +55,13 @@ def test_aspreconditioner(self):

ml = smoothed_aggregation_solver(A)

kwargs = dict(tol=1e-8, maxiter=30, atol=0)
if 'rtol' in inspect.getfullargspec(cg).args:
kwargs['rtol'] = kwargs.pop('tol')

Check warning on line 60 in pyamg/tests/test_multilevel.py

View check run for this annotation

Codecov / codecov/patch

pyamg/tests/test_multilevel.py#L60

Added line #L60 was not covered by tests

for cycle in ['V', 'W', 'F']:
M = ml.aspreconditioner(cycle=cycle)
x, info = cg(A, b, rtol=1e-8, maxiter=30, M=M)
x, info = cg(A, b, M=M, **kwargs)
# cg satisfies convergence in the preconditioner norm
assert precon_norm(b - A*x, ml) < 1e-8*precon_norm(b, ml)

Expand Down
12 changes: 6 additions & 6 deletions pyamg/util/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ def test_profile_solver(self):

opts = []
opts.append({})
opts.append({'accel': cg, 'atol': 0})

# does cg have rtol?
opts.append({'accel': cg, 'tol': 1e-10, 'atol': 0})
nextopt = dict(accel=cg, tol=1e-10, atol=0)
if 'rtol' in inspect.getfullargspec(cg).args:
opts.append({'accel': cg, 'rtol': 1e-10, 'atol': 0})
nextopt['rtol'] = nextopt.pop('tol')

Check warning on line 176 in pyamg/util/tests/test_utils.py

View check run for this annotation

Codecov / codecov/patch

pyamg/util/tests/test_utils.py#L176

Added line #L176 was not covered by tests
opts.append(nextopt)

for kwargs in opts:
residuals = profile_solver(ml, **kwargs)
Expand Down Expand Up @@ -1200,12 +1200,12 @@ def test_profile_solver(self):

opts = []
opts.append({})
opts.append({'accel': cg, 'atol': 0})

# does cg have rtol?
opts.append({'accel': cg, 'tol': 1e-10, 'atol': 0})
nextopt = dict(accel=cg, tol=1e-10, atol=0)
if 'rtol' in inspect.getfullargspec(cg).args:
opts.append({'accel': cg, 'rtol': 1e-10, 'atol': 0})
nextopt['rtol'] = nextopt.pop('tol')

Check warning on line 1207 in pyamg/util/tests/test_utils.py

View check run for this annotation

Codecov / codecov/patch

pyamg/util/tests/test_utils.py#L1207

Added line #L1207 was not covered by tests
opts.append(nextopt)

for kwargs in opts:
residuals = profile_solver(ml, **kwargs)
Expand Down

0 comments on commit 1e7c709

Please sign in to comment.