# SavingCallback when using Ensemble Simulations

**URL:** <https://discourse.julialang.org/t/savingcallback-when-using-ensemble-simulations/88483>\
**Category:** General Usage\
**Tags:** differentialequation\
**Created:** [October 9, 2022, 3:00pm UTC](https://discourse.julialang.org/t/savingcallback-when-using-ensemble-simulations/88483 "2022-10-09T15:00:54Z")\
**Posts on this page:** 1\
**Showing post:** 8

<div class="post-metadata">

**Author:** ![johannesnauta](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johannesnauta/32/47434_2.png) [@johannesnauta](https://discourse.julialang.org/u/johannesnauta)\
**Post date:** [February 11, 2025, 2:20pm UTC](https://discourse.julialang.org/t/savingcallback-when-using-ensemble-simulations/88483/8 "2025-02-11T14:20:22Z")

</div>

Sorry for recovering this thread, but I am not sure how this approach works when a `remake(...)` call is to be avoided. As far as I understand, `remake` _always_ makes a deepcopy of the `prob`, yet this is something that I would like to avoid as this will create a lot of memory overhead to the point of crashes due to being out of memory.

In essence, this is related to [this thread](https://discourse.julialang.org/t/solving-ensembleproblem-efficiently-for-large-systems-memory-issues/116146), where I am using `TaskLocalValue`s in order not to deepcopy an `ODEProblem` (or `SDEProblem`). But, I cannot find any documentation on how to change the callback in-place, as to avoid using `remake`.

In essence, the code looks like

```julia
tlv_prob = TaskLocalValue{SDEProblem}(() -> deepcopy(prob))

T = Tuple{Vector{Float64}, Vector{Float64}}
saved_values = [SavedValues(Float64, T) for _ in 1:nseeds]

#/ Define prob_func that mutates the (local) SDEProblem
function prob_func(prob, i, nrepeats)
    #~ Get local problem that can be mutated safely
    localprob = tlv_prob[]
    localcallbackset = CallbackSet(savingcallback(saved_values[i]))
    #@TODO Should `remake(...)` be avoided?
    localprob = remake(localprob, callback=localcallbackset)
    return localprob
end

```

where `savingcallback(...)` is a function that defines the `SavingCallback`, which stores some (rolling) mean and variance of a stochastic trajectory. (Note that I use `CallbackSet` here, as in the full code I have multiple callbacks).  
This works fine, but I could’ve just as well avoided the `localprob` and just use `prob` instead, as `remake` initiates a deepcopy of the problem at hand, and I run out of memory sometimes.

I know that the callback of the problem is stored in `prob.kwargs.data.callback`, but I believe it is stored as an immutable struct and thus it cannot be easily changed. Is there a way, perhaps using [`replace`](https://docs.sciml.ai/ModelingToolkit/stable/examples/remake/#replace-and-remake) or something similar, to change the callback _in-place_, such that it works in conjunction with some `SavedValues`-arrays? Or can I adjust the `saved_values` somehow to deal with this?

In other words, is there a [`setp`/`setu`](https://docs.sciml.ai/ModelingToolkit/stable/examples/remake/#remake-and-setp/setu) equivalent for callbacks?

---

_[View the full topic](https://discourse.julialang.org/t/savingcallback-when-using-ensemble-simulations/88483)._
