# OrdinaryDiffEq.jl: DiscreteCallback at last step

**URL:** https://discourse.julialang.org/t/ordinarydiffeq-jl-discretecallback-at-last-step/46795
**Category:** General Usage
**Tags:** question
**Created:** [September 17, 2020, 5:26pm UTC](https://discourse.julialang.org/t/ordinarydiffeq-jl-discretecallback-at-last-step/46795 "2020-09-17T17:26:05Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)
#### Post date: [September 17, 2020, 5:26pm UTC](https://discourse.julialang.org/t/ordinarydiffeq-jl-discretecallback-at-last-step/46795/1 "2020-09-17T17:26:06Z")

</div>

Is there an easy way to determine whether a `DiscreteCallback` is called after the last step of an integration, i.e. just before the integration terminates? I would like to call such a callback using my chosen `condition(u,t,integrator)` but also when this step is the last.

---

<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 17, 2020, 5:56pm UTC](https://discourse.julialang.org/t/ordinarydiffeq-jl-discretecallback-at-last-step/46795/2 "2020-09-17T17:56:44Z")

</div>

you can do `other_condition || t == integrator.prob.tspan[2]`

---

<div class="post-metadata">

### Author: ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)
#### Post date: [September 17, 2020, 6:13pm UTC](https://discourse.julialang.org/t/ordinarydiffeq-jl-discretecallback-at-last-step/46795/3 "2020-09-17T18:13:18Z")

</div>

Thanks, Chris!

> [@ChrisRackauckas](#):
>
> t == integrator.prob.tspan[2]

That’s along the lines of what I’ve tried (and used in the past, if I remember correctly). However, I get

```julia
julia> using OrdinaryDiffEq, DiffEqCallbacks

julia> ode = ODEProblem((u,p,t) -> -u, 1.0, (0.0, 1.0))
ODEProblem with uType Float64 and tType Float64. In-place: false
timespan: (0.0, 1.0)
u0: 1.0

julia> cb = DiscreteCallback(
           (u, t, integrator) -> t == integrator.prob.tspan[2],
           (integrator) -> println("Last step"))
DiscreteCallback{var"#3#5",var"#4#6",typeof(DiffEqBase.INITIALIZE_DEFAULT)}(var"#3#5"(), var"#4#6"(), DiffEqBase.INITIALIZE_DEFAULT, Bool[1, 1])

julia> solve(ode, Tsit5(), callback=cb);
ERROR: type ODEIntegrator has no field prob

```

I’m using

- Julia v1.5.1
- DiffEqCallbacks v2.14.1
- OrdinaryDiffEq v5.42.8

---

<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 17, 2020, 6:19pm UTC](https://discourse.julialang.org/t/ordinarydiffeq-jl-discretecallback-at-last-step/46795/4 "2020-09-17T18:19:54Z")

</div>

`integrator.sol.prob.tspan[2]`

---

<div class="post-metadata">

### Author: ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)
#### Post date: [September 17, 2020, 6:22pm UTC](https://discourse.julialang.org/t/ordinarydiffeq-jl-discretecallback-at-last-step/46795/5 "2020-09-17T18:22:11Z")

</div>

Thanks! 🤦‍♂️I’ve forgotten the `.sol`. At least I don’t feel completely stupid since we made the same error at first 😉
