Best practice for creating indefinite simulations

Hi :wave:

I’d really appreciate your thoughts on best practice for creating an indefinite simulation.

I recently published Solving the Schrödinger equation 01 where I managed to create a simulation that goes on indefinitely. What I observed is that everything starts off great but then quite quickly slows down a lot.

I created another notebook that’s simpler in order to more clearly demonstrate what’s going on:

I am only ever updating two variables, but from the look of things in the dev tools the memory usage keeps going up. I feel like I must be missing something quite basic but it is eluding me.

Thanks for taking the time to look at my post :blush: .

Matt

Maybe look at https://observablehq.com/@mbostock/animation-loops.

I suspect that the Dygraph instance you create each time psi changes is the source of your memory leak. One option would be to render the chart yourself, say using D3, either to Canvas or SVG. Another option would be to use the update effect pattern to update the chart, rather than rendering a new chart from scratch for each frame. The latter technique is described in Learn D3: Animation.

1 Like

Thanks so much for replying @severo and @mbostock.

It turns out that Dygraph has a nice update function which I used in conjunction with the update pattern from Learn D3: animation - perfect!

Thanks again

:blush:

Matt

2 Likes