Skip to content

end simulation correction when simulation has callbacks#815

Open
rnwatanabe wants to merge 2 commits into
NeuralEnsemble:masterfrom
rnwatanabe:master
Open

end simulation correction when simulation has callbacks#815
rnwatanabe wants to merge 2 commits into
NeuralEnsemble:masterfrom
rnwatanabe:master

Conversation

@rnwatanabe

Copy link
Copy Markdown

Sometimes with long simulations with callbacks, the simulation never ends. Here is a small correction in the run_until() method in the control.py file that eliminates this issue.

@apdavison

Copy link
Copy Markdown
Member

Thanks for reporting the bug. I think that for backends that round run_until() to the timestep grid (NEURON, NEST, Brian), if time_point doesn't land exactly on the dt grid, t gets stuck at the last grid point below it, the guard simulator.state.t + 1e-9 < time_point never becomes false, and the loop spins forever. That matches the "long simulations with callbacks never end" symptom, since time_point tends to drift off exact grid alignment through floating-point accumulation.

The problem with your proposed solution is that it stops the callback loop up to a full timestep early, even in the normal on-grid case, so run_until ends short of time_point and any callbacks scheduled in that final timestep are dropped.

Could we instead use the same half-timestep tolerance already applied a few lines above (the -dt/2.0 check when detecting a past time_point)?

  while time_point - simulator.state.t > simulator.state.dt / 2.0:

This terminates the off-grid case (it stops once within half a timestep of the target, which is the grid-rounding tolerance) without stopping short or dropping the final-timestep callbacks.

It'd also be worth adding a regression test that reproduces the hang. The existing test_callbacks doesn't catch it because it runs on the mock backend (whose run_until() sets t exactly, with no rounding) and uses callback steps much larger than dt, so it'd need a system test on one of the rounding backends, or an off-grid time_point against a backend that rounds.

@apdavison apdavison added this to the 0.13.1 milestone Jul 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants