# Handling prematurely terminated ODE integration in parameter estimation

**URL:** https://discourse.julialang.org/t/handling-prematurely-terminated-ode-integration-in-parameter-estimation/103514
**Category:** Modelling & Simulations
**Tags:** optimization, differentialequation
**Created:** [September 4, 2023, 4:02pm UTC](https://discourse.julialang.org/t/handling-prematurely-terminated-ode-integration-in-parameter-estimation/103514 "2023-09-04T16:02:06Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Neodym](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Neodym](https://discourse.julialang.org/u/Neodym)
#### Post date: [September 4, 2023, 4:02pm UTC](https://discourse.julialang.org/t/handling-prematurely-terminated-ode-integration-in-parameter-estimation/103514/1 "2023-09-04T16:02:06Z")

</div>

Hi,

I’m trying to estimate parameters of an ODE model using `DifferentialEquations`, `SciMLSensitivity`, and `Optimization` packages. In particular, I’ve found that `Ipopt` works quite well for my problem.  
Sometimes, though, the ODE cannot be solved up to the final time during the course of the optimization. That is, the ODE solver terminates somewhere in the middle such that the residual / loss cannot be computed.

At the moment, an exception is raised since some arithmetics doesn’t work out (due to different vector dimensions). This crashes the optimizer as well.

Is there a general practice how to handle this problem with derivative-based optimizers?  
For example, can I signal the optimizer to try a different step size in its line search or can I let it abort and return the best point encountered so far?

---

<div class="post-metadata">

### Author: ![isaacsas](https://avatars.discourse-cdn.com/v4/letter/i/f6c823/32.png) [@isaacsas](https://discourse.julialang.org/u/isaacsas)
#### Post date: [September 4, 2023, 5:33pm UTC](https://discourse.julialang.org/t/handling-prematurely-terminated-ode-integration-in-parameter-estimation/103514/2 "2023-09-04T17:33:37Z")

</div>

Have you looked at [Handling Divergent and Unstable Trajectories · SciMLSensitivity.jl](https://docs.sciml.ai/SciMLSensitivity/stable/tutorials/training_tips/divergence/)?

---

<div class="post-metadata">

### Author: ![Neodym](https://avatars.discourse-cdn.com/v4/letter/n/ba8739/32.png) [@Neodym](https://discourse.julialang.org/u/Neodym)
#### Post date: [September 5, 2023, 6:30am UTC](https://discourse.julialang.org/t/handling-prematurely-terminated-ode-integration-in-parameter-estimation/103514/3 "2023-09-05T06:30:21Z")

</div>

Thanks for the pointer, I’ve tried that. Unfortunately, Ipopt doesn’t like it:

```julia
EXIT: Invalid number in NLP function or derivative detected.

```

---

<div class="post-metadata">

### Author: ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)
#### Post date: [September 5, 2023, 10:25pm UTC](https://discourse.julialang.org/t/handling-prematurely-terminated-ode-integration-in-parameter-estimation/103514/4 "2023-09-05T22:25:30Z")

</div>

That just means you didn’t do it correctly. You need to make sure you capture the solution, check the retcode, and change the loss calculation instead of letting it error. Do you have an MWE?
