# Catalyst Jump model and callbacks

**URL:** <https://discourse.julialang.org/t/catalyst-jump-model-and-callbacks/82967>\
**Category:** Modelling & Simulations\
**Tags:** question\
**Created:** [June 18, 2022, 12:24pm UTC](https://discourse.julialang.org/t/catalyst-jump-model-and-callbacks/82967 "2022-06-18T12:24:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jae0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jae0/32/37277_2.png) [@jae0](https://discourse.julialang.org/u/jae0)\
**Post date:** [June 18, 2022, 12:24pm UTC](https://discourse.julialang.org/t/catalyst-jump-model-and-callbacks/82967/1 "2022-06-18T12:24:47Z")

</div>

Hello, this is a question that looks related to the following:

> [@Certain callback does not seem to have an effect when solving JumpProblem](https://discourse.julialang.org/t/certain-callback-does-not-seem-to-have-an-effect-when-solving-jumpproblem/46415):
>
> I am trying to solve a jump problem. As a part of it, mid simulation, I change a parameter. This is basically an input being triggered into the system. This does not seem to have much effect though, and I am unsure why: using DifferentialEquations, Catalyst my\_network = @reaction\_network begin v0 + hill(σ,v,K,n), ∅ → σ deg, (σ,iσ) → ∅ (kB,kD), σ ↔ iσ input, iσ → σ end v0 v K n kB kD deg input; v0 = 0.01; v = 0.25; K = 5.; n = 2.; kB = 1.; kD = 1.; deg = 0.01; input = 0.; p = [v…

I am hoping to perturb the system state at multiple times using Catalyst / DiscreteProblem / JumpProblem.

I have found the above answer as well as the remarks in the tutorial ([https://diffeq.sciml.ai/stable/tutorials/discrete\_stochastic\_example/#Continuous-Time-Jump-Processes-and-Gillespie-Methods](https://diffeq.sciml.ai/stable/tutorials/discrete_stochastic_example/#Continuous-Time-Jump-Processes-and-Gillespie-Methods)) and thought I understood them. But evidently I do not. Here is a minimal example.

# this is a basic model and works fine

using Catalyst, DifferentialEquations  
using Plots, StatsPlots

rs = @reaction\_network begin  
g, X → 2X  
l, X → 0  
end g l

dprob = DiscreteProblem(rs, [1000.0], (0.0, 100.0), ( 1.0, 1.0 ) )  
jprob = JumpProblem(rs, dprob, Direct(), save\_positions=(false, false))  
jsol = solve(jprob, SSAStepper(), saveat=0.1)  
plot(jsol,lw=2,title=“Jump solution”)

# Trying to add a callback to affect u has no effect

condition20(u, t, integrator) = t==20.0  
function affect20!(integrator)  
integrator.u[1] = 100  
reset\_jumps!(integrator)  
# reset\_aggregated\_jumps!(integrator)  
end

jsol = solve(jprob, SSAStepper(), saveat=0.1, callback=DiscreteCallback( condition20, affect20! ) )  
plot(jsol,lw=2,title=“Jump solution with perturbation at time=20”)

I have also tried PresetTimeCallback ( as I have multiple perturbations that I am trying to simulate). No joy there either.

Would anyone have a hint as to how I can implement such perturbations in a Jump model?

Thanks!  
Jae

---

<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:** [June 18, 2022, 12:42pm UTC](https://discourse.julialang.org/t/catalyst-jump-model-and-callbacks/82967/2 "2022-06-18T12:42:36Z")

</div>

Hello,  
Your callback activates when `t==20.0`. However, it is not certain the simulation will actually stop at that timestep (e.g. it might simulate a jump at time 19.67, and then the next one at time 21.23). Have you tried adding `tstops=[20.0]` to your `solve(...)` command? That might help.

Also, you can you ` to format code to make it look prettier: [Discourse Guide: Code Formatting - Meta - Stonehearth Discourse](https://discourse.stonehearth.net/t/discourse-guide-code-formatting/30587)

---

<div class="post-metadata">

**Author:** ![jae0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jae0/32/37277_2.png) [@jae0](https://discourse.julialang.org/u/jae0)\
**Post date:** [June 18, 2022, 12:48pm UTC](https://discourse.julialang.org/t/catalyst-jump-model-and-callbacks/82967/3 "2022-06-18T12:48:37Z")

</div>

Joy has been restored! Thank you!

---

<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 18, 2022, 12:48pm UTC](https://discourse.julialang.org/t/catalyst-jump-model-and-callbacks/82967/4 "2022-06-18T12:48:52Z")

</div>

> [@Torkel](#):
>
> Your callback activates when `t==20.0`. However, it is not certain the simulation will actually stop at that timestep (e.g. it might simulate a jump at time 19.67, and then the next one at time 21.23). Have you tried adding `tstops=[20.0]` to your `solve(...)` command? That might help.

And on this note, if you want a discrete callback that hits specific times, you may want to use `PresetTimeCallback` as the docs describe.

[https://diffeq.sciml.ai/stable/features/callback\_functions/#PresetTimeCallback](https://diffeq.sciml.ai/stable/features/callback_functions/#PresetTimeCallback)

---

<div class="post-metadata">

**Author:** ![jae0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jae0/32/37277_2.png) [@jae0](https://discourse.julialang.org/u/jae0)\
**Post date:** [June 18, 2022, 12:52pm UTC](https://discourse.julialang.org/t/catalyst-jump-model-and-callbacks/82967/5 "2022-06-18T12:52:18Z")

</div>

Yes! I was hoping that would work! And thank you for your dev and teaching material. Very appreciated!

---

<div class="post-metadata">

**Author:** ![jae0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jae0/32/37277_2.png) [@jae0](https://discourse.julialang.org/u/jae0)\
**Post date:** [June 18, 2022, 1:31pm UTC](https://discourse.julialang.org/t/catalyst-jump-model-and-callbacks/82967/6 "2022-06-18T13:31:27Z")

</div>

Joy in stasis … use of PresetTimeCallback not quite understood:

Another minimal example. Same as before but with a callback using PresetTimeCallback with callback times: cbtimes and :

```julia
using Catalyst, DifferentialEquations 

 rs = @reaction_network begin
   g, X --> 2X
   l, X --> 0
 end g l  

 dprob = DiscreteProblem(rs, [1000.0], (0.0, 100.0), ( 1.0, 1.0 ) )
 jprob = JumpProblem(rs, dprob, Direct(), save_positions=(false, false))
 cbtimes = [20.0, 30.0]
 affectpresets!(integrator) = integrator.u[1] += 1000
  
 jsol = solve(jprob, SSAStepper(), saveat=0.1, tstops=cbtimes, callback=PresetTimeCallback( cbtimes, affectpresets! ) )

```

errors out as:

```julia
type or paste code hERROR: type SSAIntegrator has no field tdir
Stacktrace:
  [1] getproperty
    @ ./Base.jl:42 [inlined]
  [2] (::DiffEqCallbacks.var"#62#65"{typeof(SciMLBase.INITIALIZE_DEFAULT), Bool, Vector{Float64}, typeof(affectpresets!)})(c::DiscreteCallback{DiffEqCallbacks.var"#60#63"{Vector{Float64}}, DiffEqCallbacks.var"#61#64"{typeof(affectpresets!)}, DiffEqCallbacks.var"#62#65"{typeof(SciMLBase.INITIALIZE_DEFAULT), Bool, Vector{Float64}, typeof(affectpresets!)}, ...

```

I am not sure how to debug this one … any more feedback would be appreciated!

---

<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 18, 2022, 1:38pm UTC](https://discourse.julialang.org/t/catalyst-jump-model-and-callbacks/82967/7 "2022-06-18T13:38:09Z")

</div>

Oh that’s a bug. SSAIntegrator is a bit of a weird one and doesn’t have all of the normal fields. It’s missing a field `tdir` for whether it’s integrating forward or backwards in time, and `PresetTimeCallback` wants to use this to know if it’s in the right direction. Can you open an issue on DiffEqJump.jl? This should be simple to fix.

---

<div class="post-metadata">

**Author:** ![jae0](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jae0/32/37277_2.png) [@jae0](https://discourse.julialang.org/u/jae0)\
**Post date:** [June 18, 2022, 1:46pm UTC](https://discourse.julialang.org/t/catalyst-jump-model-and-callbacks/82967/8 "2022-06-18T13:46:57Z")

</div>

Ok will do. Thx!
