Skip to content

Commit

Permalink
Merge pull request #2313 from Ericgig/bug.warnings_stochastic
Browse files Browse the repository at this point in the history
Fix warnings tests in test_stochastic
  • Loading branch information
Ericgig committed Jan 30, 2024
2 parents 846db23 + 9e00d1f commit c3a1916
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions doc/changes/2313.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add too small step warnings in fixed dt SODE solver
11 changes: 8 additions & 3 deletions qutip/solver/sode/rouchon.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import warnings
from qutip import unstack_columns, stack_columns
from qutip.core import data as _data
from ..stochastic import StochasticSolver
Expand Down Expand Up @@ -97,12 +98,16 @@ def set_state(self, t, state0, generator):

def integrate(self, t, copy=True):
delta_t = (t - self.t)
dt = self.options["dt"]
if delta_t < 0:
raise ValueError("Stochastic integration need increasing times")
elif delta_t == 0:
return self.t, self.state, np.zeros()
elif delta_t < 0.5 * dt:
warnings.warn(
f"Step under minimum step ({dt}), skipped.",
RuntimeWarning
)
return self.t, self.state, np.zeros(len(self.sc_ops))

dt = self.options["dt"]
N, extra = np.divmod(delta_t, dt)
N = int(N)
if extra > 0.5 * dt:
Expand Down
11 changes: 8 additions & 3 deletions qutip/solver/sode/sode.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import numpy as np
import warnings
from . import _sode
from ..integrator.integrator import Integrator
from ..stochastic import StochasticSolver, SMESolver
Expand Down Expand Up @@ -135,12 +136,16 @@ def __init__(self, rhs, options):

def integrate(self, t, copy=True):
delta_t = t - self.t
dt = self.options["dt"]
if delta_t < 0:
raise ValueError("Stochastic integration time")
elif delta_t == 0:
raise ValueError("Integration time, can't be negative.")
elif delta_t < 0.5 * dt:
warnings.warn(
f"Step under minimum step ({dt}), skipped.",
RuntimeWarning
)
return self.t, self.state, np.zeros(self.N_dw)

dt = self.options["dt"]
N, extra = np.divmod(delta_t, dt)
N = int(N)
if extra > 0.5 * dt:
Expand Down
23 changes: 16 additions & 7 deletions qutip/tests/solver/test_stochastic.py
Original file line number Diff line number Diff line change
Expand Up @@ -348,23 +348,32 @@ def func(t, A, W):

def test_deprecation_warnings():
with pytest.warns(FutureWarning, match=r'map_func'):
ssesolve(qeye(2), basis(2), [0, 1e-5], [qeye(2)], map_func=None)
ssesolve(qeye(2), basis(2), [0, 0.01], [qeye(2)], map_func=None)

with pytest.warns(FutureWarning, match=r'progress_bar'):
ssesolve(qeye(2), basis(2), [0, 1e-5], [qeye(2)], progress_bar=None)
ssesolve(qeye(2), basis(2), [0, 0.01], [qeye(2)], progress_bar=None)

with pytest.warns(FutureWarning, match=r'nsubsteps'):
ssesolve(qeye(2), basis(2), [0, 1e-5], [qeye(2)], nsubsteps=None)
ssesolve(qeye(2), basis(2), [0, 0.01], [qeye(2)], nsubsteps=None)

with pytest.warns(FutureWarning, match=r'map_func'):
ssesolve(qeye(2), basis(2), [0, 1e-5], [qeye(2)], map_func=None)
ssesolve(qeye(2), basis(2), [0, 0.01], [qeye(2)], map_func=None)

with pytest.warns(FutureWarning, match=r'store_all_expect'):
ssesolve(qeye(2), basis(2), [0, 1e-5], [qeye(2)], store_all_expect=1)
ssesolve(qeye(2), basis(2), [0, 0.01], [qeye(2)], store_all_expect=1)

with pytest.warns(FutureWarning, match=r'store_measurement'):
ssesolve(qeye(2), basis(2), [0, 1e-5], [qeye(2)], store_measurement=1)
ssesolve(qeye(2), basis(2), [0, 0.01], [qeye(2)], store_measurement=1)

with pytest.raises(TypeError) as err:
ssesolve(qeye(2), basis(2), [0, 1e-5], [qeye(2)], m_ops=1)
ssesolve(qeye(2), basis(2), [0, 0.01], [qeye(2)], m_ops=1)
assert '"m_ops" and "dW_factors"' in str(err.value)


@pytest.mark.parametrize("method", ["euler", "rouchon"])
def test_small_step_warnings(method):
with pytest.warns(RuntimeWarning, match=r'under minimum'):
smesolve(
qeye(2), basis(2), [0, 0.0000001], [qeye(2)],
options={"method": method}
)

0 comments on commit c3a1916

Please sign in to comment.