# Getting results until the point of nonconvergence in DAE system

**URL:** https://discourse.julialang.org/t/getting-results-until-the-point-of-nonconvergence-in-dae-system/130897
**Category:** Modelling & Simulations
**Tags:** question
**Created:** [July 21, 2025, 8:20am UTC](https://discourse.julialang.org/t/getting-results-until-the-point-of-nonconvergence-in-dae-system/130897 "2025-07-21T08:20:55Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![total](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@total](https://discourse.julialang.org/u/total)
#### Post date: [July 21, 2025, 8:20am UTC](https://discourse.julialang.org/t/getting-results-until-the-point-of-nonconvergence-in-dae-system/130897/1 "2025-07-21T08:20:55Z")

</div>

Hello,  
I’m trying to simulate a DAE system with Sundials IDA solver. I implemented my controlvector with callbacks and it works just fine. However, depending on the controls I run into infeasible regions and the solver does not converge (which is expected and desired). My problem is, that I can not figure out how to save the results up until that point.  
I tried try-catch, but solve only returns the final result, so the catch part never comes into play.  
Is there any kwarg I’m missing?  
I guess I could get the timepoint of non-convergence and run the simulation until that point again, but that seems very inefficient.

Thank you in advance!

```julia
SOL = nothing
try
    SOL = solve(DAE_Problem, IDA(), callback=cb, tstops=timevector; saveat=0.01)
    save_results(SOL, variables_name)
    println("Simulation completed successfully")
catch e
    @warn "Error during simulation: $(e)"
    if SOL !== nothing
        @warn "Partial results available with return code: $(SOL.retcode)"
        save_results(SOL, variables_name)
    end
end

```

---

<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: [July 21, 2025, 8:55am UTC](https://discourse.julialang.org/t/getting-results-until-the-point-of-nonconvergence-in-dae-system/130897/2 "2025-07-21T08:55:05Z")

</div>

If you don’t use saveat and instead let it save adaptively then it’ll save every point and you can use the interpolation afterwards to get it in 0.1 increments and also have the final time it was at

---

<div class="post-metadata">

### Author: ![total](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@total](https://discourse.julialang.org/u/total)
#### Post date: [July 21, 2025, 9:15am UTC](https://discourse.julialang.org/t/getting-results-until-the-point-of-nonconvergence-in-dae-system/130897/3 "2025-07-21T09:15:11Z")

</div>

thank you for the fast response.  
The problem is, that solve throws an error, and does not return any results.

I change the controls every 10 seconds, and say after 39 s the system becomes unsolvable. IDA throws an error and does not save anything. I want to access the trajectories until that point.

Cheers

---

<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: [July 21, 2025, 9:42am UTC](https://discourse.julialang.org/t/getting-results-until-the-point-of-nonconvergence-in-dae-system/130897/4 "2025-07-21T09:42:52Z")

</div>

What error is being thrown?

---

<div class="post-metadata">

### Author: ![total](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@total](https://discourse.julialang.org/u/total)
#### Post date: [July 21, 2025, 11:23am UTC](https://discourse.julialang.org/t/getting-results-until-the-point-of-nonconvergence-in-dae-system/130897/5 "2025-07-21T11:23:54Z")

</div>

The error is:

```julia
ERROR: LoadError: DomainError with -3.0253720071336128:
Exponentiation yielding a complex result requires a complex argument.

```

This error is not surprising. The model has an implicit region of feasibility, so the solver does not converge, when it is left. There is no physical solution to the problem at some point in time.  
I’m interested in the trajectories of the state variables up until that point.

To illustrate:  
Imagine a dynamic tank of water, where the control variable defines the outlet flow. Once the tank is empty, it doesn’t make any sense to have an outlet flow anymore. However, up until that point, the trajectories hold valuable information, which I want to access.

–

I just found SavingCallback and think it might solve my problem.

---

<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: [July 21, 2025, 11:29am UTC](https://discourse.julialang.org/t/getting-results-until-the-point-of-nonconvergence-in-dae-system/130897/6 "2025-07-21T11:29:44Z")

</div>

Instead of `^`, use `NaNMath.^` so it NaNs instead of errors.

---

<div class="post-metadata">

### Author: ![total](https://avatars.discourse-cdn.com/v4/letter/t/b5ac83/32.png) [@total](https://discourse.julialang.org/u/total)
#### Post date: [July 22, 2025, 6:36am UTC](https://discourse.julialang.org/t/getting-results-until-the-point-of-nonconvergence-in-dae-system/130897/7 "2025-07-22T06:36:20Z")

</div>

This didn’t work for me. But that might be my mistake.

Anyways, I solved the issue with the following code. It’s probably inefficient to always halt the integrator, but it’s still fast enough for me.

```julia
timevector_savetimes = collect(t_start:0.1:t_end)
saved_data = Vector{Tuple{Float64,Float64}}()

function save_affect!(integrator)
    t = integrator.t
    u1 = integrator.u[1]
    push!(saved_data, (t, u1))
    return nothing
end

save_cb = PresetTimeCallback(timevector_savetimes, save_affect!)

SOL = solve(DAE_Problem, IDA(), callback=CallbackSet(cb, save_cb), tstops=timevector_savetimes[1:end])

```

---

<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: [July 22, 2025, 10:21am UTC](https://discourse.julialang.org/t/getting-results-until-the-point-of-nonconvergence-in-dae-system/130897/8 "2025-07-22T10:21:12Z")

</div>

Did you add the NaNMath.jl package and import it?
