# Order of ran cells changes output, using DifferentialEquations

**URL:** <https://discourse.julialang.org/t/order-of-ran-cells-changes-output-using-differentialequations/122547>\
**Category:** Pluto\
**Created:** [November 12, 2024, 2:15pm UTC](https://discourse.julialang.org/t/order-of-ran-cells-changes-output-using-differentialequations/122547 "2024-11-12T14:15:00Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![cmdenis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmdenis/32/211517_2.png) [@cmdenis](https://discourse.julialang.org/u/cmdenis)\
**Post date:** [November 12, 2024, 2:15pm UTC](https://discourse.julialang.org/t/order-of-ran-cells-changes-output-using-differentialequations/122547/1 "2024-11-12T14:15:01Z")

</div>

Hello,

I’m running into a strange issue while using `Pluto` and `DifferentialEquations`.

In a nutshell: I get a systematically different output depending on the order I run the cells in my notebook.

I’ve managed to boil it down to this MWE:

```julia
# [Cell 1]
using DifferentialEquations, Plots

# [Cell 2]
times = 0:0.01:500

# [Cell 3]
begin # Add callback event
	condition(u, t, integrator) = (t == 250.);
	affect!(integrator) = (integrator.p[1] = 2);
	cb = DiscreteCallback(condition, affect!);
end

# [Cell 4]
# Define parameters
testP = [1.];

# [Cell 5]
# Define initial conditions
u0 = [1.];

# [Cell 6]
# Define problem
prob = ODEProblem(f, u0, (times[1], times[end]), testP, callback = cb);

# [Cell 7]
# Integrate system
sol = solve(prob, Euler(), dt = 0.001, tstops = [250.]);

# [Cell 8]
plot(sol)

```

If I launch this notebook, I get this plot, which evaluates everything properly:

 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/8/6828ed884bb528579ac99e6230a68745a9f44d5e.png)

But if I evaluate `[Cell 7]` after this, then I get:

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

Which seems to ignore the callback that changes a parameter in the integration. Not sure what is going on… Is it a problem on my end? Or is this something else?

I’m running Julia `Version 1.11.1 (2024-10-16)`, `Pluto v0.20.3` and `DifferentialEquations v7.14.0` (and `Plots v1.40.8`, although the same is produced with `CairoMakie`)

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![cmdenis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmdenis/32/211517_2.png) [@cmdenis](https://discourse.julialang.org/u/cmdenis)\
**Post date:** [November 12, 2024, 2:16pm UTC](https://discourse.julialang.org/t/order-of-ran-cells-changes-output-using-differentialequations/122547/2 "2024-11-12T14:16:38Z")

</div>

Not sure if this will work, but here is the notebook:

[glitch\_pluto.jl](https://discourse.julialang.org/uploads/short-url/6W79b7lLHiqMEyBHk61wpAADH8L.jl) (106.4 KB)

---

<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:** [November 12, 2024, 2:17pm UTC](https://discourse.julialang.org/t/order-of-ran-cells-changes-output-using-differentialequations/122547/3 "2024-11-12T14:17:31Z")

</div>

> [@cmdenis](#):
>
> Which seems to ignore the callback that changes a parameter in the integration. Not sure what is going on… Is it a problem on my end? Or is this something else?

Your affect! modifies the parameter, so when you run it again it’s still modified since you directly modified the reference in `p`.

---

<div class="post-metadata">

**Author:** ![cmdenis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmdenis/32/211517_2.png) [@cmdenis](https://discourse.julialang.org/u/cmdenis)\
**Post date:** [November 12, 2024, 2:21pm UTC](https://discourse.julialang.org/t/order-of-ran-cells-changes-output-using-differentialequations/122547/4 "2024-11-12T14:21:33Z")

</div>

Right ok, I see, that makes sense.

However, on `Pluto`’s side, shouldn’t it produce the same results every time?

---

<div class="post-metadata">

**Author:** ![Sevi](https://avatars.discourse-cdn.com/v4/letter/s/c67d28/32.png) [@Sevi](https://discourse.julialang.org/u/Sevi)\
**Post date:** [November 12, 2024, 2:37pm UTC](https://discourse.julialang.org/t/order-of-ran-cells-changes-output-using-differentialequations/122547/5 "2024-11-12T14:37:16Z")

</div>

Pluto has no internal state of the notebook _unless_ you work with mutable objects 😅 That’s pretty subtle, but I guess it makes sense (?)  
And allows one to “hack” the notebook a bit by creating constructs that _do_ depend on cell order.

So modifying a `Vector` for example will persist if the cell defining (and thus “resetting”) the `Vector` is not re-run explicitly.

In Pluto’s internal dependency graph, running `[Cell 7]` does not trigger `[Cell 4]` because that code does not depend on anything from `[Cell 7]`, but the object that was created in `[Cell 4]` (the parameter vector) can be modified and re-used without problem.

One solution to your problem might be to either use an immutable type for your parameters (not sure if that’s possible with the callback) or making sure that a new parameter vector is generated when you run the `solve` cell. Something like this perhaps:

```julia
make_problem() = ODEProblem(...)
...

sol = solve(make_problem(), ...)

```

Then every time, the `solve` cell is re-run, it re-generates a new problem. But make sure to not re-use the same object in the problem again, so this probably still won’t work (there is only one `testP` object which will be re-used all the time)

```julia
make_problem() = ODEProblem(..., testP, ...)

```

and might need to be this (I’m sure there are nicer ways to write it though, depends on what you need):

```julia
make_problem() = ODEProblem(..., [1.], )

```

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [November 12, 2024, 3:07pm UTC](https://discourse.julialang.org/t/order-of-ran-cells-changes-output-using-differentialequations/122547/6 "2024-11-12T15:07:42Z")

</div>

> [@cmdenis](#):
>
> However, on `Pluto`’s side, shouldn’t it produce the same results every time?

Pluto.jl cannot track arbitrary dependencies between cells (and if you think about it this also doesn’t make because you ran into ambiguous situations very quickly - find an example below). Pluto generally tracks assignments to variables and nothing else. Since you `affect!` modifies only the value inside something a variable refers to, Pluto.jl has no chance of seeing this dependency.

In general you should keep things, that mutate inside the cell that defines what is being mutated to keep things consistent. I agree that it is a bit difficult to see in your scenario.

> **Example: Ambigous cell order**
>
> ```julia
> # Cell 1
> var = [1,2]
> 
> # Cell 2
> var[1] += 3
> 
> # Cell 3
> var[1] *= 2
> 
> # Cell 4
> var # what should this print?!
> 
> ```
> 
> As you can see the order of Cells 2 and 3 is completely ambiguous.

---

<div class="post-metadata">

**Author:** ![cmdenis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cmdenis/32/211517_2.png) [@cmdenis](https://discourse.julialang.org/u/cmdenis)\
**Post date:** [November 14, 2024, 8:44pm UTC](https://discourse.julialang.org/t/order-of-ran-cells-changes-output-using-differentialequations/122547/7 "2024-11-14T20:44:22Z")

</div>

That all makes sense to me, thanks!

I ended up wrapping some parts of my code inside of functions to make it more predictable and benefit from the updating of Pluto when changing stuff around.
