-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
Conversation
solve_ivp
failure when ts is empty
solve_ivp
failure when ts is emptysolve_ivp
failure when ts
is empty
@@ -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: |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 ]])]
It looks like the |
I stopped it |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks @Patol75
Reference issue
Closes gh-11700
Closes gh-12100
What does this implement/fix?
While using
solve_ivp
, if the user providest_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 theRadau
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([])
isFalse
. As a result,hstack
is only executed if elements have been added to the initially emptyts
list; it is initially empty only ift_eval
is provided.Additional information
None