# Callback at saveat times

**URL:** <https://discourse.julialang.org/t/callback-at-saveat-times/95331>\
**Category:** General Usage\
**Created:** [February 28, 2023, 1:33pm UTC](https://discourse.julialang.org/t/callback-at-saveat-times/95331 "2023-02-28T13:33:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [February 28, 2023, 1:33pm UTC](https://discourse.julialang.org/t/callback-at-saveat-times/95331/1 "2023-02-28T13:33:37Z")

</div>

I have read most of the documentation on Callbacks, but I still have an issue, that does not appear to be covered.

Here is a fragment of a longer section from an UODE:

```julia
    λ8 = (T8_11 + T8_22 + T8_33)/2

    # λ9 = tr(σ⋅γd)
    λ9 = (T6_11 + T6_22 + T6_33) / 2f0

    # Run the integrity basis through a neural network
    model_inputs = [λ1;λ2;λ3;λ4;λ5;λ6;λ7;λ8;λ9]
    g1,g2,g3,g4,g5,g6,g7,g8,g9 = model_univ(model_inputs, model_weights)
    # tst that this code is being executed. Plot should change. The code was indeed executing,
    # and the solution did not change from its initial value. This must imply that the nonlinearity
    # has very little effect.
    #g1,g2,g3,g4,g5,g6,g7,g8,g9 = 0., 0., 0., 0., 0., 0., 0., 0., 0.

    # Save g1 through g9 per epoch. Once trained
    #println("g1->g9: $t, $g1, $g2, $g3, $g4, $g5, $g6, $g7, $g8, $g9")

    if dct[:captureG]
        coef = [t, g1,g2,g3,g4,g5,g6,g7,g8,g9]
        push!(tdnn_coefs, coef)
        trace = [t, λ1, λ2, λ3, λ4, λ5, λ5, λ6, λ7, λ8, λ9]
        push!(tdnn_traces, trace)
    end

    # Tensor combining layer
    F11 = g1 + g2*σ11 + g3*γd11 + g4*T4_11 + g5*T5_11 + g6*T6_11 + g7*T7_11 + g8*T8_11 + g9*T9_11
    F22 = g1 + g2*σ22 + g3*γd22 + g4*T4_22 + g5*T5_22 + g6*T6_22 + g7*T7_22 + g8*T8_22 + g9*T9_22

```

The driver function is called at fixed time intervals time steps. I wish to capture `coef` and `trace` at saveat. Is there a simple flag I could test inside the function to determine whether or not I am at one of the `saveat` times? I do not wish to introduce additional interpolations into the problem. Thanks!

---

<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:** [March 1, 2023, 8:28am UTC](https://discourse.julialang.org/t/callback-at-saveat-times/95331/2 "2023-03-01T08:28:10Z")

</div>

> [@erlebach](#):
>
> Is there a simple flag I could test inside the function to determine whether or not I am at one of the `saveat` times?

the probability you will be at a saveat time is effectively zero. Did you mean `tstop` times?

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [March 1, 2023, 11:57am UTC](https://discourse.julialang.org/t/callback-at-saveat-times/95331/3 "2023-03-01T11:57:02Z")

</div>

I was thinking `saveat` times. Ok, here are my thoughts. I run a solver with `saveat=0.2` since the solution is saved at those times, I assume that `tstops` is a superset of `saveat` times.

I would like to print out auxiliary variables, calculated from derived functions of the solution, at the same saved times. Alter actively, I would like this auxiliary data every nth time level computed from the `saveat` times.

In reality, I am inside an UODE, which is quite complex, where the NN outputs the coefficiens of a tensor base expansion. this expansion is added to the RHS of the ODE system

---

<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:** [March 1, 2023, 1:24pm UTC](https://discourse.julialang.org/t/callback-at-saveat-times/95331/4 "2023-03-01T13:24:53Z")

</div>

> [@erlebach](#):
>
> I was thinking `saveat` times. Ok, here are my thoughts. I run a solver with `saveat=0.2` since the solution is saved at those times, I assume that `tstops` is a superset of `saveat` times.

`saveat` and `tstops` are normally distinct. The solver almost never steps to a `saveat` time.

> [@erlebach](#):
>
> I would like to print out auxiliary variables, calculated from derived functions of the solution, at the same saved times. Alter actively, I would like this auxiliary data every nth time level computed from the `saveat` times.

FunctionCallingCallback and SavingCallback can be helpful for these kinds of things.

---

<div class="post-metadata">

**Author:** ![erlebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/erlebach/32/12973_2.png) [@erlebach](https://discourse.julialang.org/u/erlebach)\
**Post date:** [March 1, 2023, 2:34pm UTC](https://discourse.julialang.org/t/callback-at-saveat-times/95331/5 "2023-03-01T14:34:41Z")

</div>

Will check them out again. Thanks.
