Variable gets updated without even being referenced. Advice on debugging this

This is a code for the function lyapunovs [link attached] in ChaosTools.jl .
The code below is a bit modified as I am using r=[1.0,1.0] here instead of r=copy(get_state(pinteg,2) for debugging purposes. I have tried r=deepcopy(get_state(pinteg,2) as well and yet this is happening . As far as I know, by using deepcopy this r should be independent and should only change in the rescale function where I am passing it.

t0 = pinteg.t
    # array to preallocate state for rescale
    r = [1.0,1.0]#deepcopy(pinteg.u[1])#copy(pinteg.u[2])
    while pinteg.t < t0 + Ttr
        step!(pinteg, Δt)
        d = λdist(pinteg)
        lt ≤ d ≤ ut || rescale!(pinteg, d/d0, r)
    end

    t0 = pinteg.t
    d = λdist(pinteg)
    d == 0 && error("Initial distance between states is zero!!!")
    rescale!(pinteg, d/d0, r)
    λ = zero(d)
    while pinteg.t < t0 + T
        d = λdist(pinteg)
        #evolve until rescaling:
        while lt ≤ d ≤ ut
            step!(pinteg, Δt)
            d = λdist(pinteg)
            pinteg.t ≥ t0 + T && break
        end
        # local lyapunov exponent is simply the relative distance of the trajectories
        a = d/d0
        λ += log(a)
        rescale!(pinteg, a, r)
    end

I define a variable r=[1.0,1.0]. However, r is getting updated when the step! function is being called. r is not being referenced as far as I can see in the step function but still somehow it is changing. This seems like odd behavior to me, maybe I am making a silly mistake. I was trying to visualize why this using the debugger and I cannot understand why this is happening. Any advice on debugging this would be super helpful.
I am attaching a gif showing this in the debugger below
2021-09-23-0930-27-online-video

Well you’re modifying it:

https://github.com/SudoMishra/ChaosTools.jl/blob/afa85ba2030c7df977c9bf2289fd0719ac7fb20d/src/chaosdetection/lyapunovs.jl#L339

and set_state! attaches it to the integrator:

https://github.com/JuliaDynamics/DynamicalSystemsBase.jl/blob/264d463dccf193870b10c6365ac6e1ac7ffc47af/src/core/continuous.jl#L186

I’m not 100% if that’s the correct set_state! method for your integrator, please do check by stepping into that call instead of over it, but that’s what I think is going on when later on step! changes the integrator state.

2 Likes