# Making a callback event at a single timepoint

**URL:** https://discourse.julialang.org/t/making-a-callback-event-at-a-single-timepoint/9038
**Category:** Modelling & Simulations
**Tags:** diffeq
**Created:** [February 13, 2018, 4:34pm UTC](https://discourse.julialang.org/t/making-a-callback-event-at-a-single-timepoint/9038 "2018-02-13T16:34:06Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [February 13, 2018, 4:34pm UTC](https://discourse.julialang.org/t/making-a-callback-event-at-a-single-timepoint/9038/1 "2018-02-13T16:34:06Z")

</div>

Simple question. At a certain timepoint (e.g. t = 100) I want to make a callback, changing a parameter or variable value.  
The most intuitive way would be

```julia
function condition(u,t,integrator)
    t == 100
end

```

however the solver seems to miss it (not strange if it just takes timesteps). One could ofcourse workaround that, e.g. add a extra variable to `u` with an initial value `1`.

```julia
function condition(u,t,integrator)
    t > 100 && u[end] == 1
end

```

Then you add a line to the integrator setting `u[end] = 0`.

This however feels unnecessarily complicated. I do not know, but is there a simpler way to do this. I cannot imagine that it is a very uncommon thing that I am trying to do.

---

<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 13, 2018, 4:41pm UTC](https://discourse.julialang.org/t/making-a-callback-event-at-a-single-timepoint/9038/2 "2018-02-13T16:41:14Z")

</div>

Pass in `tstops = [100]` to make the solver stop there, allowing the condition to go off at exactly that time point.

(Note that this example was one of the ones in the video tutorial, so you may find this helpful)

[http://nbviewer.jupyter.org/github/JuliaDiffEq/DiffEqTutorials.jl/blob/master/Introduction/CallbacksAndEvents.ipynb](http://nbviewer.jupyter.org/github/JuliaDiffEq/DiffEqTutorials.jl/blob/master/Introduction/CallbacksAndEvents.ipynb)

[![](https://global.discourse-cdn.com/julialang/original/3X/d/6/d6d4984013f0743b8f698026660e1755246698f1.jpeg "Intro to solving differential equations in Julia") ](https://www.youtube.com/watch?v=KPEqYtEd-zY)

I want to make a `TimedCallback` though that does that automatically since this seems to be a pretty common question.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [February 13, 2018, 4:42pm UTC](https://discourse.julialang.org/t/making-a-callback-event-at-a-single-timepoint/9038/3 "2018-02-13T16:42:40Z")

</div>

You can use `tstops` to ensure that the integrator will stop at exactly `t = 100`. See [http://docs.juliadiffeq.org/stable/features/diffeq\_arrays.html#Example:-A-Control-Problem-1](http://docs.juliadiffeq.org/stable/features/diffeq_arrays.html#Example:-A-Control-Problem-1). You can also add a stop time dynamically while simulating using `add_tstop!(integrator,new_t)`.

---

<div class="post-metadata">

### Author: ![tkoolen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkoolen/32/1603_2.png) [@tkoolen](https://discourse.julialang.org/u/tkoolen)
#### Post date: [February 13, 2018, 4:42pm UTC](https://discourse.julialang.org/t/making-a-callback-event-at-a-single-timepoint/9038/4 "2018-02-13T16:42:58Z")

</div>

And I thought I was going to beat you.

---

<div class="post-metadata">

### Author: ![Torkel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/torkel/32/5030_2.png) [@Torkel](https://discourse.julialang.org/u/Torkel)
#### Post date: [February 13, 2018, 4:44pm UTC](https://discourse.julialang.org/t/making-a-callback-event-at-a-single-timepoint/9038/5 "2018-02-13T16:44:51Z")

</div>

Thank you guys. Yes I should probably watch that one properly, my mistake. Will do so when I get home.
