Saving Simulation and Truncating Values in Memory

I have a long simulation. I’m trying to find a way to save the values of my simulation to disk and truncate the values held in memory.

My guess is to use a callback. So far I have:

  using MAT

  function save_and_clear(u, t, integ)
        outFile = matopen("$(t)_X.mat","w");
          write(outFile,"u",u);
        close(outFile);

    end

    cb = FunctionCallingCallback(save_and_clear; funcat = [1000, 2000, 3000])

    @time sim = solve(prob, Tsit5(), maxiters=1e10, saveat=SaveTimes, reltol = reltolVal, callback = cb);

This is sort of working. I’m getting the values at t = 1000, 2000, 3000. What I want are all the values from t \in [0, 1000), t \in [1000, 2000) and t \in [2000, 3000). How I access those values? I was guessing they were in integrator but I can’t find them. Furthermore, once I save, t \in [0, 1000), how do I remove them from memory?

thanks!

If you instead do FunctionCallingCallback(save_and_clear), it will use the default func_everystep = true and thus fire at every step, thus saving at every step.

If you also save integ.k you can also rebuild the interpolant if you wanted.

Note for low memory you want to do:

@time sim = solve(prob, Tsit5(), maxiters=1e10, save_on=false, reltol = reltolVal, callback = cb);

I.e. do all saving manually.

In order to do this, I created two callback functions —

    function save_func(u,t,integ)
      return u     
    end
    saved_values = SavedValues(Float64, Matrix{Float64});
    collection_cb = SavingCallback(save_func, saved_values, saveat = SaveTimes);

    diskTimes = collect.([25000.0:25000.0:75000.0]);
    condition(u, t, integrator) = t ∈ diskTimes[1]
    function save_to_disk_and_clear_cb!(integ)
        outFile = matopen("$(integ.t)_X.mat","w");
          write(outFile,"tme",integ.opts.callback.discrete_callbacks[1].affect!.saved_values.t);
          #write(outFile,"V",integ.opts.callback.discrete_callbacks[1].affect!.saved_values.saveval[:,1]);
          #write(outFile,"conds",integ.opts.callback.discrete_callbacks[1].affect!.saved_values.saveval[:,14:20]);
          write(outFile,"sol",integ.opts.callback.discrete_callbacks[1].affect!.saved_values.saveval);
        close(outFile);
        empty!(integ.opts.callback.discrete_callbacks[1].affect!.saved_values.t)
        empty!(integ.opts.callback.discrete_callbacks[1].affect!.saved_values.saveval)
    end
    saveToDisk_cb = DiscreteCallback(condition, save_to_disk_and_clear_cb!)

    cbset = CallbackSet(collection_cb,saveToDisk_cb);

I did this while keeping save_on = false as chris suggested.

You could also have just done that with only a single FunctionCallingCallback

the second line should be return copy(u)