JuliaGizmos / Interact.jl

Interactive widgets to play with your Julia code
Other
520 stars 75 forks source link

@manipulate changes plots when changing initial conditions of an ODEProblem #350

Closed lindnemi closed 4 years ago

lindnemi commented 4 years ago

In a jupyter notebook I am defining several ODEProblems and use Interact @manipulate to inspect the solutions. I use the variable x0 for the initial conditions. For the second problem i am reusing the variable x0 and assign different values to it. However this affects the first widget, changing the plots it displays and sometimes causing it to break with an error message.

This behaviour is very counter-intuitive since it allows to change previous outputs!

Minimal working example:

using Interact, OrdinaryDiffEq, Plots

function g!(dx, x, p, t) 
    dx .= p*x
end

x0 = [2.]

@manipulate for p = 0:0.2:2
    prob = ODEProblem(g!, x0, (0.,3.), p)
    sol = solve(prob, Tsit5())
    plot(sol, ylim=[0,20])
end

x0 = "Hallo"

Pulling the definition of x0 into the for-loop solves the issue. I was surprised that something like this can happen since i naively assumed that Interact caches the different plots.

Would it possible to avoid this behaviour or at least to display a warning, that the internal variables of the widget have been changed?

twavv commented 4 years ago

I think this is working as expected.

If you mutate variables that are defined outside of your scope, then that's going to cause issues if other things are also using those variables, both in Interact and otherwise.

I don't think there's a good way to circumvent this or warn about this because the @manipulate macro doesn't know what is going to be mutated outside of the macro (or even that, for example, g! will mutate the variable).

twavv commented 4 years ago

I'm going to close this since it's working as expected. Feel free to continue the discussion if you have more you'd like to say.