Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Avoid solve_ivp failure when ts is empty #14755

Merged
merged 4 commits into from
Nov 10, 2021

Conversation

Patol75
Copy link
Contributor

@Patol75 Patol75 commented Sep 21, 2021

Reference issue

Closes gh-11700
Closes gh-12100

What does this implement/fix?

While using solve_ivp, if the user provides t_eval, it is possible that execution will fail if no solution evaluations are completed before the solver exits. In particular, gh-11700 and gh-12100 respectively illustrate that the Radau solver fails and a terminal event occurs before any solution evaluation completes. I am proposing here a simple fix, taking advantage of the fact that, in Python, bool([]) is False. As a result, hstack is only executed if elements have been added to the initially empty ts list; it is initially empty only if t_eval is provided.

Additional information

None

@Patol75 Patol75 changed the title BUG: Avoid failure when ts is empty BUG: Avoid solve_ivp failure when ts is empty Sep 21, 2021
@Patol75 Patol75 changed the title BUG: Avoid solve_ivp failure when ts is empty BUG: Avoid solve_ivp failure when ts is empty Sep 21, 2021
@@ -646,7 +646,7 @@ def solve_ivp(fun, t_span, y0, method='RK45', t_eval=None, dense_output=False,
if t_eval is None:
ts = np.array(ts)
ys = np.vstack(ys).T
else:
elif ts:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth adding a test based on one of the issue reports? I suppose it might just be a small loop that would have produced an error before but no longer does?

I did notice that in one of the issues there is contention about whether or not this is a bug (I'm not an expert).

Copy link
Contributor Author

@Patol75 Patol75 Sep 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it is mainly about handling the specific case where the solver exits without having written any solution, a case that both linked issues document. Writing elif ts: instead of else: avoids calling numpy.hstack on an empty list, which otherwise yields an error. Note that this if block occurs at the very end of solve_ivp, right before the return statement. It turns out that OdeResult, which is called by the latter return, is able to handle empty solutions without any changes.

It is definitely a bug. The contention comes from a disagreement about how solve_ivp is used, but it is actually irrelevant to the issue as the error is raised if a terminal event occurs earlier than any t_eval value.

I am pushing a commit with a new test.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed a clear bug, and the test looks good to me. The new output seems good:

  message: 'A termination event occurred.'
     nfev: 11
     njev: 0
      nlu: 0
      sol: None
   status: 1
  success: True
        t: []
 t_events: [array([7.])]
        y: []
 y_events: [array([[0.41155802, 0.2420958 ]])]

@tylerjereddy tylerjereddy added the defect A clear bug or issue that prevents SciPy from being installed or used as expected label Sep 21, 2021
@Patol75
Copy link
Contributor Author

Patol75 commented Oct 14, 2021

It looks like the Linux Tests / Nightly CPython (3.1) (pull_request) test is taking a large amount of time - can someone with the right privileges stop it?

@andyfaff
Copy link
Contributor

I stopped it

@rgommers rgommers added this to the 1.8.0 milestone Nov 10, 2021
Copy link
Member

@rgommers rgommers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @Patol75

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
defect A clear bug or issue that prevents SciPy from being installed or used as expected scipy.integrate
Projects
None yet
4 participants