# How to use DiscreteCallbacks with sundials.jl IDA solver? How to implement reinit!?

**URL:** <https://discourse.julialang.org/t/how-to-use-discretecallbacks-with-sundials-jl-ida-solver-how-to-implement-reinit/75674>\
**Category:** Numerics\
**Tags:** question, diffeq, differentialequation, diffeqcallbacks\
**Created:** [February 2, 2022, 4:34pm UTC](https://discourse.julialang.org/t/how-to-use-discretecallbacks-with-sundials-jl-ida-solver-how-to-implement-reinit/75674 "2022-02-02T16:34:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![TobiasFranz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobiasfranz/32/33428_2.png) [@TobiasFranz](https://discourse.julialang.org/u/TobiasFranz)\
**Post date:** [February 2, 2022, 4:34pm UTC](https://discourse.julialang.org/t/how-to-use-discretecallbacks-with-sundials-jl-ida-solver-how-to-implement-reinit/75674/1 "2022-02-02T16:34:32Z")

</div>

I’m solving large DAE-Systems derived from PDEs for electrochemical systems. I’m using the IDA solver because the ODE solvers (DAE formulated as ODE in mass matrix form) are not working (all of them are unstable) for my DAE System. IDA works well, but unfortunately, I could not figure out yet how to use DiscreteCallbacks in combination with the IDA solver. In another issue @ChrisRackauckas gave the advice to use reinit! :

> You can use reinit! as described above, but it hasn’t been integrated into the library for an automatic fix. DABDF2 and DFBDF still need a bit of work though, so I’d just point people to IDA for now. When those get optimized (which should be soon, we have the DAE benchmarks up and running as of this week to start doing it), then we’ll sweep back around to fix this. Until then the solvers aren’t too useful though since they are slow, which puts this at a lower priority.

Originally posted by @ChrisRackauckas in #589 (comment)

I am not sure how to implement it in my code:

```julia
param=[3E4;9000]
tstop=[30.0]

function condition(u,t,integrator)    
  t in tstop
end

function affect!(integrator)
  integrator.p[1] = 1E4
end

save_positions = (true,true)
cb = DiscreteCallback(condition, affect!, save_positions=save_positions)
prob_DAE = DAEProblem(electrolyzer_DAE,du0,u0_ss,tspan,param,differential_vars=diff_vars_bool)
sol_DAE = solve(prob_DAE,IDA(use_linesearch_ic=false),callback = cb, tstops=tstop)

```

like this it’s not working. implementation of reinit! is missing. How should reinit! be implemented?

Many thanks in advance!

---

<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:** [February 2, 2022, 4:49pm UTC](https://discourse.julialang.org/t/how-to-use-discretecallbacks-with-sundials-jl-ida-solver-how-to-implement-reinit/75674/2 "2022-02-02T16:49:06Z")

</div>

First of all, this is a repost: [Using DiscreteCallback with IDA solver for DAE systems. How to implement reinit! ? · Issue #845 · SciML/DifferentialEquations.jl · GitHub](https://github.com/SciML/DifferentialEquations.jl/issues/845) . Please only post your questions in one place.

Secondly, this is very misguided. I did not give advice to use `reinit!`. I said to another dev that `reinit!` could be used as a workaround until that issue is solved. That issue is solved, the `reinit!`s are done internally. You should not use `reinit!` like that, it will break things, don’t do it. It’s done within the package correctly which is why the issue has a linked PR and was closed.

So third, what is your actual issue? You probably answered that yourself in the first line:

> [@TobiasFranz](#):
>
> I’m solving large DAE-Systems derived from PDEs for electrochemical systems. I’m using the IDA solver because the ODE solvers (DAE formulated as ODE in mass matrix form) are not working (all of them are unstable) for my DAE System.

Almost certainly this is pointing to an issue of having a higher index DAE which is only working in IDA because the initialization got a bit lucky (which can happen with Index 2). So first question, what is the index of the DAE? Second question, how are the other methods failing? Is it at initialization? The answer is likely to stabilize the solve.

---

<div class="post-metadata">

**Author:** ![TobiasFranz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobiasfranz/32/33428_2.png) [@TobiasFranz](https://discourse.julialang.org/u/TobiasFranz)\
**Post date:** [February 2, 2022, 7:30pm UTC](https://discourse.julialang.org/t/how-to-use-discretecallbacks-with-sundials-jl-ida-solver-how-to-implement-reinit/75674/3 "2022-02-02T19:30:25Z")

</div>

Many thanks for the fast answer. I don’t know the DAE Index, I have to check it. Do you know a good way to check the index of the DAE?

The methods in mass matrix form are all failing with this error:

```julia
┌ Warning: Instability detected. Aborting
└ @ SciMLBase C:\Users\maitb\.julia\packages\SciMLBase\DXiE6\src\integrator_interface.jl:351

```

I think they are failing at initialization.

My main problem is that DiscretCallback is not working with IDA. I always get the error:

```julia
[IDA ERROR] IDACalcIC
  The linesearch algorithm failed: step too small or too many backtracks.

```

For normal simulation from initial conditions till steady-state, the IDA solver is working well.

I also found that the `DImplicitEuler()` is working for simulation till steady-state and also works for DiscreteCallback. But when the applied Discontinuity in DiscreteCallback gets too large then it takes longer and longer to get a solution. For large Discontinuities, I have to stop the simulation because it takes too long.

Many thanks for your help!

---

<div class="post-metadata">

**Author:** ![YingboMa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yingboma/32/2181_2.png) [@YingboMa](https://discourse.julialang.org/u/YingboMa)\
**Post date:** [February 2, 2022, 7:41pm UTC](https://discourse.julialang.org/t/how-to-use-discretecallbacks-with-sundials-jl-ida-solver-how-to-implement-reinit/75674/4 "2022-02-02T19:41:27Z")

</div>

Hi Tobias, do you have an MWE that I can run and reproduce this?

---

<div class="post-metadata">

**Author:** ![YingboMa](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yingboma/32/2181_2.png) [@YingboMa](https://discourse.julialang.org/u/YingboMa)\
**Post date:** [February 2, 2022, 7:47pm UTC](https://discourse.julialang.org/t/how-to-use-discretecallbacks-with-sundials-jl-ida-solver-how-to-implement-reinit/75674/5 "2022-02-02T19:47:41Z")

</div>

BTW, while we currently don’t have a utility function for checking the exact index number, you could check if your system is index 1 easily by checking the matrix pencil. Check out this blog post where it mentions matrix pencils [A Survey of Differential-Algebraic Equations (1)](https://yingboma.github.io/posts/2021_03_19_dae_intro/) .

---

<div class="post-metadata">

**Author:** ![TobiasFranz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobiasfranz/32/33428_2.png) [@TobiasFranz](https://discourse.julialang.org/u/TobiasFranz)\
**Post date:** [February 2, 2022, 8:46pm UTC](https://discourse.julialang.org/t/how-to-use-discretecallbacks-with-sundials-jl-ida-solver-how-to-implement-reinit/75674/6 "2022-02-02T20:46:16Z")

</div>

Thanks for your help! What do you mean with MWE?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [February 2, 2022, 10:21pm UTC](https://discourse.julialang.org/t/how-to-use-discretecallbacks-with-sundials-jl-ida-solver-how-to-implement-reinit/75674/7 "2022-02-02T22:21:19Z")

</div>

Minimal Working Example. A small program that allows us to reproduce your problem.

---

<div class="post-metadata">

**Author:** ![TobiasFranz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobiasfranz/32/33428_2.png) [@TobiasFranz](https://discourse.julialang.org/u/TobiasFranz)\
**Post date:** [February 3, 2022, 12:56pm UTC](https://discourse.julialang.org/t/how-to-use-discretecallbacks-with-sundials-jl-ida-solver-how-to-implement-reinit/75674/8 "2022-02-03T12:56:26Z")

</div>

The model is quite large and contains different files. Maybe I could send them to you in a private message?

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [February 3, 2022, 2:36pm UTC](https://discourse.julialang.org/t/how-to-use-discretecallbacks-with-sundials-jl-ida-solver-how-to-implement-reinit/75674/9 "2022-02-03T14:36:00Z")

</div>

Well, minimal working example means that you invest some time and effort to reduce your code to the minimal size that allows to reproduce the problem…
