# TerminateSteadyState callback on DiscreteProblem?

**URL:** <https://discourse.julialang.org/t/terminatesteadystate-callback-on-discreteproblem/129670>\
**Category:** Modelling & Simulations\
**Created:** [June 5, 2025, 12:59pm UTC](https://discourse.julialang.org/t/terminatesteadystate-callback-on-discreteproblem/129670 "2025-06-05T12:59:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)\
**Post date:** [June 5, 2025, 12:59pm UTC](https://discourse.julialang.org/t/terminatesteadystate-callback-on-discreteproblem/129670/1 "2025-06-05T12:59:15Z")

</div>

Hi all,

I’d like to use TerminateSteadyState from DiffEqCallbacks.jl on a DiscreteProblem, but the default test function is based on derivatives (which my model doesn’t have). I see that one can define their own test function based on the integrator - any tips on how this should be structured in order to correspond to the ‘allDerivPass’ function for continuous systems?

---

<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:** [June 5, 2025, 1:01pm UTC](https://discourse.julialang.org/t/terminatesteadystate-callback-on-discreteproblem/129670/2 "2025-06-05T13:01:06Z")

</div>

I assume you would define that as checking u\_{n+1} - u\_{n} being sufficiently small?

---

<div class="post-metadata">

**Author:** ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)\
**Post date:** [June 5, 2025, 3:47pm UTC](https://discourse.julialang.org/t/terminatesteadystate-callback-on-discreteproblem/129670/3 "2025-06-05T15:47:00Z")

</div>

Yup - I just need to use the difference in states `uprev` and `u` - I didn’t know whether there was a canned function for this, or how to build the callback appropriately.

---

<div class="post-metadata">

**Author:** ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)\
**Post date:** [June 5, 2025, 3:56pm UTC](https://discourse.julialang.org/t/terminatesteadystate-callback-on-discreteproblem/129670/4 "2025-06-05T15:56:14Z")

</div>

Looking closer, `allDerivPass` appears to be able to handle `DiscreteProblem`, but I get an error that the `FunctionMap` stepper doesn’t support derivatives, and the callback fails.

---

<div class="post-metadata">

**Author:** ![sdwfrost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sdwfrost/32/2831_2.png) [@sdwfrost](https://discourse.julialang.org/u/sdwfrost)\
**Post date:** [June 5, 2025, 7:37pm UTC](https://discourse.julialang.org/t/terminatesteadystate-callback-on-discreteproblem/129670/5 "2025-06-05T19:37:22Z")

</div>

Although `DiffEqBase.get_du!` should be implemented for `DiscreteProblem`, here’s my placeholder test function that uses `u` and `uprev`:

```julia
function allDeltaPass(integrator, abstol, reltol, min_t)
    # Early exit
    if min_t !== nothing && integrator.t < min_t
        return false
    end
    testval = integrator.u .- integrator.uprev
    return all(abs.(testval) .<= max.(abstol, reltol .* abs.(integrator.u)))
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:** [June 9, 2025, 2:27pm UTC](https://discourse.julialang.org/t/terminatesteadystate-callback-on-discreteproblem/129670/6 "2025-06-09T14:27:20Z")

</div>

That looks right. We should add that to the library as the default way for DiscreteProblem. Do you have a good test case?

> <https://github.com/SciML/DiffEqCallbacks.jl/pull/254>
>
> Fixes https://discourse.julialang.org/t/terminatesteadystate-callback-on-discret…eproblem/129670/5
