# Sol.interp and sol.k DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372>\
**Category:** Modelling & Simulations\
**Created:** [May 16, 2024, 6:22pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372 "2024-05-16T18:22:37Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [May 16, 2024, 6:22pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372/1 "2024-05-16T18:22:37Z")

</div>

Hello, please help me understand what it is `sol.interp`, `sol.k` and is there any way not to save them? I found that they take up quite a lot of RAM, which is why ubuntu 24.04 freezes.

Thank you for your helps!

Code:

```julia
using StaticArrays, DifferentialEquations

function FHN2_try3(u, p ,t)
    x1, y1, x2, y2, z= u
    ϵ, a, g, k, σ, α, k1, k2 = p

    I(ϕ_i) = g * (1.0/(1.0 + exp(k*(cos(σ/2) - cos(ϕ_i - α - σ/2)))))
    ρz = k1 + k2 * z ^ 2

    ϕ2 = atan(y2, x2)
    ϕ1 = atan(y1, x1)

    dx1dt = (x1 - x1 ^ 3 / 3 - y1 + I(ϕ2) + ρz * (x2 - x1) ) / ϵ
    dy1dt = x1 - a
    dx2dt = (x2 - x2 ^ 3 / 3 - y2 + I(ϕ1) + ρz * (x1 - x2) ) / ϵ
    dy2dt = x2 - a
    dzdt = x1 - x2
    return SVector(dx1dt, dy1dt, dx2dt, dy2dt, dzdt)
end

function FHN2_try3_params()
    ϵ = 0.01; a = -1.01;
    g = 0.1; k = 50.0; σ = 50.0 * pi / 180; α = 160.0 * pi / 180;
    k1 = 0.0; k2 = 0.0
    return [ϵ, a, g, k, σ, α, k1, k2]
end

function jac_FHN(u, p, t)
    x1, y1, x2, y2, z = u
    ϵ, a, g, k, σ, α, k1, k2 = p

    pz = k1 + k2 * z^2

    exp_ϕ2 = exp( k * ( cos(σ/2) - cos( α + σ/2 - atan(y2, x2) ) ) )
    sin_ϕ2 = sin( α + σ/2 - atan(y2, x2) )
    exp_ϕ1 = exp( k * ( cos(σ/2) - cos( α + σ/2 - atan(y1, x1) ) ) )
    sin_ϕ1 = sin( α + σ/2 - atan(y1, x1) )
    
    x1x1 = (1.0 - x1^2 - pz) / ϵ
    x1y1 = -1.0/ϵ
    x1x2 = (-g * k * y2 * exp_ϕ2 * sin_ϕ2) / ( x2^2 * (1 + y2^2 / x2^2 ) * ( exp_ϕ2 + 1.0 )^2 ) + k1 + k2 * z^2
    x1x2 = x1x2 / ϵ
    x1y2 = ( g * k * x2 * exp_ϕ2 * sin_ϕ2 ) / ( (x2^2 + y2^2) * ( 1.0 + exp_ϕ2 )^2 )
    x1y2 = x1y2 / ϵ
    x1z = 2.0 * k2 * z * ( -x1 + x2 ) / ϵ
    #-----------------------------------------------
    y1x1 = 1.0;
    y1y1 = 0.0; y1x2 = 0.0; y1y2 = 0.0; y1z = 0.0;
    #----------------------------------------------
    x2x2 = (1.0 - x2^2 - pz) / ϵ
    x2y2 = -1.0/ϵ
    x2x1 = (-g * k * y1 * exp_ϕ1 * sin_ϕ1) / ( x1^2 * (1 + y1^2 / x1^2 ) * ( exp_ϕ1 + 1.0 )^2 ) + k1 + k2 * z^2
    x2x1 = x2x1 / ϵ
    x2y1 = ( g * k * x1 * exp_ϕ1 * sin_ϕ1 ) / ( (x1^2 + y1^2) * ( 1.0 + exp_ϕ1 )^2 )
    x2y1 = x2y1 / ϵ
    x2z = 2.0 * k2 * z * ( -x2 + x1 ) / ϵ
    #----------------------------------------------
    y2x2 = 1.0;
    y2y2 = 0.0; y2x1 = 0.0; y2y1 = 0.0; y2z = 0.0;
    #----------------------------------------------
    zx1 = 1.0; zy1 = 0.0; zx2 = -1.0; zy2 = 0.0; zz = 0.0;
    return SMatrix{5,5}(x1x1, y1x1, x2x1, y2x1, zx1,
                        x1y1, y1y1, x2y1, y2y1, zy1,
                        x1x2, y1x2, x2x2, y2x2, zx2,
                        x1y2, y1y2, x2y2, y2y2, zy2,
                        x1z, y1z, x2z, y2z, zz)
end

parameters = FHN2_try3_params()
tspan = (0.0, 10000.0)
parameters[7] = 0.09
parameters[8] = 75.74
u0 = [-0.9859005363852416, -0.635253572091177, -1.0345181027025165, -0.636382088705782, 0.0011285166148596525] 
u0 = SVector{5}(u0)
prob = ODEProblem(FHN2_try3, u0, tspan, parameters)
alg = Vern9();

sol = solve(prob, alg, adaptive = false, dt = 0.001, maxiters = 1e7) #true, abstol = abs_tolerance, reltol = rel_tolerance, maxiters = max_iters);

sol_alg = sol.alg
sol_alg_choice = sol.alg_choice
sol_dense = sol.dense
sol_errors = sol.errors
sol_interp = sol.interp
sol_k = sol.k
sol_prob = sol.prob
sol_retcode = sol.retcode
sol_stats = sol.stats
sol_t = sol.t
sol_tslocation = sol.tslocation
sol_u = sol.u
sol_u_analytic = sol.u_analytic

varinfo()

```

---

<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:** [May 16, 2024, 11:05pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372/2 "2024-05-16T23:05:31Z")

</div>

Do you need dense output? If you don’t, then just `saveat`?

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [May 21, 2024, 12:02pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372/3 "2024-05-21T12:02:02Z")

</div>

Thanks, I’ll try to use `saveat`. I don’t understand what `dense output` is used for, so I can’t tell if I need it.

---

<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:** [May 21, 2024, 12:08pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372/4 "2024-05-21T12:08:51Z")

</div>

If you don’t know what points you will need until after a solution, saying doing some analysis and playing around in the REPL, or generating new interpolations on-demand (rootfinding and such), then you’d want the dense continuous output. If you know what values you want, just use `saveat` and directly compute those. That will be cheaper since then it doesn’t have to save the whole continuous output.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [May 21, 2024, 12:18pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372/5 "2024-05-21T12:18:19Z")

</div>

After the solution has been received, I analyze the time series. I define spikes in the time series, the extreme events and calculate the time intervals between extreme events.  
I tried using `saveat` and it didn’t seem to affect the result.  
Thank you for your help

---

<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:** [May 21, 2024, 12:19pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372/6 "2024-05-21T12:19:52Z")

</div>

> [@Sergey\_Novak](#):
>
> After the solution has been received, I analyze the time series. I define spikes in the time series, the extreme events and calculate the time intervals between extreme events.  
> I tried using `saveat` and it didn’t seem to affect the result.

That kind of post analysis should probably be done on the continuous time dense version unless you have a very dense `saveat` save (which would take more memory than the continuous one). Though if you don’t have strict accuracy constraints then having a less dense `saveat` would be a way to cut memory.

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [May 21, 2024, 12:22pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372/7 "2024-05-21T12:22:35Z")

</div>

one approach to look into here would be putting the analysis in a callback. writing the code to work well in a callback is a little tricky, but doing so would likely allow you to skip saving entirely

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [May 21, 2024, 12:29pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372/8 "2024-05-21T12:29:04Z")

</div>

I need to calculate the trajectory for quite a long time, for example time = 5000000 , so that later, build a probability density function for time intervals between exteme events. Therefore, I try to use the accuracy as high as possible. I am using the Verne9() and abstol, reltol = 1e-14

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [May 21, 2024, 12:29pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372/9 "2024-05-21T12:29:55Z")

</div>

In my case, this will not work. I need a complete time series.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [May 21, 2024, 12:37pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372/10 "2024-05-21T12:37:04Z")

</div>

I tried to calculate the solution by parts. Then I noticed that starting from a certain time in the time diagram, the spikes look too discrete. For example part of time series

 ![image](https://global.discourse-cdn.com/julialang/original/3X/e/3/e36d438ec6977c27be1ccd72f39b553f64dca654.png)

Code for calculate solution by parts:

```julia
using StaticArrays, DifferentialEquations, Statistics, JLD2 

function clear_work_space(sol, len_sol, ttr)
    sol = nothing
    len_sol = nothing
    ttr = nothing
    return sol, len_sol, ttr
end

t_truncate(t) = floor(Int64, t / 2)

function save_data(iteration, data, path_to_save)
    file_name = "$(iteration)_sol_x1.jld"
    full_path = path_to_save * file_name
    jldsave(full_path; data)
end

function first_iteration(u0, parameters, tspan, integrator_setting, path_to_save)

    prob = ODEProblem(FHN2_try3, u0, tspan, parameters)
    sol = solve(prob, integrator_setting.alg, adaptive = true,
    abstol = integrator_setting.abs_tol, reltol = integrator_setting.rel_tol,
    maxiters = integrator_setting.max_iters);
    last_point = sol[end]
    println("last point: $(last_point)"); flush(stdout)
    len_sol = length(sol.t)

    ttr = t_truncate(len_sol); tend = len_sol
    data_x1 = [sol[1, ttr:tend], sol.t[ttr:tend]]
    sol, len_sol, ttr = clear_work_space(sol, len_sol, ttr)
    GC.gc()

    save_data(1, data_x1, path_to_save)
    return last_point
end

function calculate_timeseris(u0_start, parameters, integrator_setting,
    t_point, count_iteration, path_to_save)

    global tspan = (0.0, t_point)
    global u0 = u0_start

    u0 = first_iteration(u0, parameters, tspan, integrator_setting, path_to_save)
    println("----------------------------------------------"); flush(stdout)
    println("");flush(stdout)

    for iteration in range(2, count_iteration)

        println("ITERATION: $(iteration)")

        tspan = (t_point*(iteration-1), t_point*iteration)
        prob = ODEProblem(FHN2_try3, u0, tspan, parameters)
        println("u0: $(u0)"); flush(stdout)
        sol = solve(prob, integrator_setting.alg, adaptive = true,
        abstol = integrator_setting.abs_tol, reltol = integrator_setting.rel_tol,
        maxiters = integrator_setting.max_iters);
        u0 = sol[end]

        data_x1 = [sol[1, :], sol.t]
        sol = nothing; GC.gc();
        save_data(iteration, data_x1, path_to_save)

        println("last point: $(u0)"); flush(stdout)
        println("----------------------------------------------"); flush(stdout)
        println("");flush(stdout)
    end
end

alg = Vern9()
max_iters = 1e8
abs_tol = 1e-14; rel_tol = 1e-14
integrator_setting = (alg = alg, abs_tol = abs_tol, rel_tol = rel_tol, max_iters = max_iters)

path_to_save = "/home/sergey/MEGA/dynamical-systems/FHN_Korotkov/data/timeseries_k2_75_72/"
parameters = FHN2_try3_params()
parameters[7] = 0.09
parameters[8] = 75.72

u0_start = [1.7, 0.7, -1.4, 0.35, 0.7 - 0.35]
u0_start = SVector{5}(u0_start)

t_point = 50000.0
count_iteration = 30

calculate_timeseris(u0_start, parameters, integrator_setting,
    t_point, count_iteration, path_to_save)

```

---

<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:** [May 21, 2024, 4:47pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372/11 "2024-05-21T16:47:45Z")

</div>

If you need to decrease the memory, putting the analysis into a callback as Oscar describes would be a good way to go.

---

<div class="post-metadata">

**Author:** ![Sergey\_Novak](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sergey_novak/32/37716_2.png) [@Sergey\_Novak](https://discourse.julialang.org/u/Sergey_Novak)\
**Post date:** [May 22, 2024, 3:04pm UTC](https://discourse.julialang.org/t/sol-interp-and-sol-k-differentialequations-jl/114372/12 "2024-05-22T15:04:23Z")

</div>

Thank you  
I’ll try to save only local minimums and maximums using an callback
