BUG: (Rare) corner case in ivp with terminating events
See original GitHub issueDescribe your issue.
I am performing several millions of integrations with solve_ivp
and I am occasionally stumbling upon what I think is an edge case that is not being handled.
The error is raised by the constructor of OdeSolution
, here:
https://github.com/scipy/scipy/blob/ece78377d322f891b1ca2d248ac82caa3411837e/scipy/integrate/_ivp/common.py#L156-L159
Specifically, I found that the last two entries in ts
are identical. Let’s see how that comes to be.
The array that is fed to the constructor of OdeSolution
is called ts
in the solve_ivp
function is ivp.py
. ts
is built by taking the old and the new timestep returned by the integration. If there’s a terminating event, ts
also is extended with the time and value of the event itself:
https://github.com/scipy/scipy/blob/ece78377d322f891b1ca2d248ac82caa3411837e/scipy/integrate/_ivp/ivp.py#L621-L630
Now, the corner case arises when the value that satisfies the event equation is the one of the bracketing values for the root finder.
Now, this seems extremely unlikely. In practice, I found this can happen when the event equation dramatically changes behavior near the termination condition.
I suspect the problem might manifest itself in the other branch of the if t_eval is None
statement.
I can open a PR to fix this bug if someone guides me in how to address it properly.
Reproducing Code Example
Code is too complex to report here.
Error message
Traceback (most recent call last):
File "/home/u20/gabrielebozzola/.cache/pypoetry/virtualenvs/kray-ZRW9TZmG-py3.8/bin/kray", line 5, in <module>
main()
File "/home/u20/gabrielebozzola/kray/kray/kray.py", line 66, in main
args.func(args)
File "/home/u20/gabrielebozzola/kray/kray/kray_ray_trace.py", line 93, in ray_trace
affine_parameters, ray = ray_trace_one(
File "/home/u20/gabrielebozzola/kray/kray/ray_tracing.py", line 288, in ray_trace_one
solution = solve_ivp(
File "/home/u20/gabrielebozzola/.cache/pypoetry/virtualenvs/kray-ZRW9TZmG-py3.8/lib/python3.8/site-packages/scipy/integrate/_ivp/ivp.py", line 666, in solve_ivp
sol = OdeSolution(ts, interpolants)
File "/home/u20/gabrielebozzola/.cache/pypoetry/virtualenvs/kray-ZRW9TZmG-py3.8/lib/python3.8/site-packages/scipy/integrate/_ivp/common.py", line 157, in __init__
raise ValueError(f"`ts` must be strictly increasing or decreasing.")
ValueError: `ts` must be strictly increasing or decreasing.
SciPy/NumPy/Python version information
1.8.0 1.22.3 sys.version_info(major=3, minor=8, micro=2, releaselevel=‘final’, serial=0)
Issue Analytics
- State:
- Created a year ago
- Comments:6 (2 by maintainers)
Are you using LSODA as your solver ? It is known that the current pyrhon interface does not handle correctly the dense output (e.g. time interpolation between each step). Since this dense output is used by Brentq to find the time at which an event is triggered, it may happen that you end up having an event which is always true in the last time step, simply because the dense output is wrong. If that’s the case, you can try other solvers, e.g. BDF or RK45 which do not suffer from this issue.
Yes, I think that this was with LSODA and the problem did not occur with RK45 and the same integration.