question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: (Rare) corner case in ivp with terminating events

See original GitHub issue

Describe 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:open
  • Created a year ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
laurent90gitcommented, Sep 23, 2022

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.

0reactions
Sbozzolocommented, Sep 23, 2022

Are you using LSODA as your solver ? It is known that the curre t pyrgon interface does not handle correctly the dense output (e.g. tile 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Corner case - Wikipedia
In engineering, a corner case (or pathological case) involves a problem or situation that occurs only outside normal operating parameters—specifically one that ...
Read more >
The most error prone FPGA corner cases
Cycle related corner cases are probably the worst and main reason for undetected bugs on many FPGAs.
Read more >
An Overview of MOOS-IvP and a Users Guide to the IvP Helm
The IvP Helm is written as a MOOS application where MOOS is a set of Open Source ... In the rare case that...
Read more >
What Is an Edge Case? Definition and Examples in a Product
Corner cases are a type of edge case, but are a bit more complex. An edge case occurs when a user finds themselves...
Read more >
qandamaster307.xml - Chegg
... -equilibrium-price-output-events--graphs-clearl-q23780090 0.8 2017-09-23 ... y-k-constant-represents-flow-near-corner-find-equation-streamline-q23780557 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found