# Diffeq trigger callback from inside f

**URL:** <https://discourse.julialang.org/t/diffeq-trigger-callback-from-inside-f/67499>\
**Category:** General Usage\
**Tags:** diffeq\
**Created:** [September 1, 2021, 12:42pm UTC](https://discourse.julialang.org/t/diffeq-trigger-callback-from-inside-f/67499 "2021-09-01T12:42:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)\
**Post date:** [September 1, 2021, 12:42pm UTC](https://discourse.julialang.org/t/diffeq-trigger-callback-from-inside-f/67499/1 "2021-09-01T12:42:50Z")

</div>

I am looking to improve the performance of my ODE. Since the expensive operation `g(u,p)` (which involves inverting matrices) has to be done anyway to calculate `du/dt`, I’d like not to have to reevaluate it at every step for the callback condition too. Is there a clean way of triggering the callback from `f`? Maybe by storing additional values?

```julia
function f(du,u,p,t)
    x = g(u,p)
    du = h(u,x,p)
end

function condition(u,t,integrator)
   x = g(u,p)
   somecheck(x)
end
affect!(integrator) = ...
cb = Callback(condition, affect!)

u0 = ...
prob = ODEProblem(f, u0, cb, tspan, pars)
solve(prob)

```

---

<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 1, 2021, 10:49pm UTC](https://discourse.julialang.org/t/diffeq-trigger-callback-from-inside-f/67499/2 "2021-09-01T22:49:05Z")

</div>

Modify a parameter and check in the callback if that parameter is true?

---

<div class="post-metadata">

**Author:** ![levasco](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/levasco/32/17539_2.png) [@levasco](https://discourse.julialang.org/u/levasco)\
**Post date:** [September 2, 2021, 4:01pm UTC](https://discourse.julialang.org/t/diffeq-trigger-callback-from-inside-f/67499/3 "2021-09-02T16:01:53Z")

</div>

Yup that’s probably a good approach. Thanks!
