Skip to content

Commit

Permalink
Merge pull request #2307 from Ericgig/fix.scipy1.12.rtol.v4
Browse files Browse the repository at this point in the history
Update to fix scipy1.12 deprecation warnings
  • Loading branch information
Ericgig committed Jan 26, 2024
2 parents 1692198 + c1320df commit e4ba3bd
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
18 changes: 16 additions & 2 deletions qutip/cy/stochastic.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ import scipy.sparse as sp
from scipy.sparse.linalg import LinearOperator
from scipy.linalg.cython_blas cimport zaxpy, zdotu, zdotc, zcopy, zdscal, zscal
from scipy.linalg.cython_blas cimport dznrm2 as raw_dznrm2
import scipy
from packaging.version import parse as _parse_version


SCIPY_GT_1_12 = _parse_version(scipy.__version__) >= _parse_version("1.12")


cdef int ZERO=0
cdef double DZERO=0
Expand Down Expand Up @@ -1214,6 +1220,7 @@ cdef class SSESolver(StochasticSolver):
cdef object cpcd_ops
cdef object imp
cdef double tol, imp_t
cdef dict imp_options

def set_data(self, sso):
L = sso.LH
Expand All @@ -1230,6 +1237,9 @@ cdef class SSESolver(StochasticSolver):
self.tol = sso.tol
self.imp = LinearOperator( (self.l_vec,self.l_vec),
matvec=self.implicit_op, dtype=complex)
self.imp_options = {"tol": sso.tol, "atol": 1e-12}
if SCIPY_GT_1_12:
self.imp_options["rtol"] = self.imp_options.pop("tol")

def implicit_op(self, vec):
cdef np.ndarray[complex, ndim=1] out = np.zeros(self.l_vec, dtype=complex)
Expand Down Expand Up @@ -1610,7 +1620,7 @@ cdef class SSESolver(StochasticSolver):
# scipy function only take np array, not memoryview
self.imp_t = t
spout, check = sp.linalg.bicgstab(self.imp, dvec, x0=guess,
tol=self.tol, atol=1e-12)
**self.imp_options)
cdef int i
copy(spout, out)

Expand All @@ -1622,6 +1632,7 @@ cdef class SMESolver(StochasticSolver):
cdef object c_ops
cdef int N_root
cdef double tol
cdef dict imp_options

def set_data(self, sso):
L = sso.LH
Expand All @@ -1636,6 +1647,9 @@ cdef class SMESolver(StochasticSolver):
if sso.solver_code in [MILSTEIN_IMP_SOLVER, TAYLOR1_5_IMP_SOLVER]:
self.tol = sso.tol
self.imp = sso.imp
self.imp_options = {"tol": sso.tol, "atol": 1e-12}
if SCIPY_GT_1_12:
self.imp_options["rtol"] = self.imp_options.pop("tol")

cdef void _normalize_inplace(self, complex[::1] vec):
_normalize_rho(vec)
Expand Down Expand Up @@ -1878,7 +1892,7 @@ cdef class SMESolver(StochasticSolver):
# np.ndarray to memoryview is OK but not the reverse
# scipy function only take np array, not memoryview
spout, check = sp.linalg.bicgstab(self.imp(t, data=1), dvec, x0=guess,
tol=self.tol, atol=1e-12)
**self.imp_options)
cdef int i
copy(spout,out)

Expand Down
22 changes: 22 additions & 0 deletions qutip/steadystate.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ def out(*args, **kwargs):
return out


def _rename_kwargs(function, names_pairs):
"""
Return a wrapped version of `function` that rename any keyword
arguments from the first value of the pair, to the second.
"""
@functools.wraps(function)
def out(*args, **kwargs):
for old, new in names_pairs:
if old in kwargs:
kwargs[new] = kwargs.pop(old)
return function(*args, **kwargs)
return out


# From SciPy 1.4 onwards we need to pass the `callback_type='legacy'` argument
# to gmres to maintain the same behaviour we used to have. Since this should
# be the default behaviour, we use that in the main code and just "eat" the
Expand All @@ -69,6 +83,14 @@ def out(*args, **kwargs):
gmres = _eat_kwargs(gmres, ['callback_type'])


# From SciPy 1.12, the `tol` keyword argument to iterative solvers was renamed
# to `rtol`.
if _parse_version(scipy.__version__) >= _parse_version("1.12"):
gmres = _rename_kwargs(gmres, [('tol', 'rtol')])
lgmres = _rename_kwargs(lgmres, [('tol', 'rtol')])
bicgstab = _rename_kwargs(bicgstab, [('tol', 'rtol')])


def _empty_info_dict():
def_info = {'perm': [], 'solution_time': None,
'residual_norm': None,
Expand Down

0 comments on commit e4ba3bd

Please sign in to comment.