From 924426275e2ebc3350067e21ec0ce7d2a0e3d5e7 Mon Sep 17 00:00:00 2001 From: Andrew Davison Date: Thu, 19 Nov 2026 16:26:09 +0100 Subject: [PATCH] Fix run() with callbacks never ending on NEURON NEURON stops at the last time step before an off-grid target time, so when `time_point` is off the time grid, `t` cannot reach it and the loop does not end. A callback asking for less than one time step ahead also stalls the loop. Reported in #815. --- doc/simulation_control.txt | 4 +- pyNN/common/control.py | 10 ++++- .../scenarios/test__simulation_control.py | 39 +++++++++++++++++++ 3 files changed, 51 insertions(+), 2 deletions(-) diff --git a/doc/simulation_control.txt b/doc/simulation_control.txt index a5c48ff4..d1afd319 100644 --- a/doc/simulation_control.txt +++ b/doc/simulation_control.txt @@ -74,7 +74,9 @@ the operation at the end of each step, e.g.:: Alternatively, PyNN can take care of breaking the simulation into steps for you. :func:`run()` and :func:`run_until()` each accept an optional list of callbacks functions. Each callback should accept the current time as an argument, and -return the next time it wishes to be called. +return the next time it wishes to be called. A callback is called at most once +per time step: if it asks to be called again less than one time step later, it +will be called at the next time step. >>> def report_time(t): ... print("The time is %g" % t) diff --git a/pyNN/common/control.py b/pyNN/common/control.py index be50aedb..09001149 100644 --- a/pyNN/common/control.py +++ b/pyNN/common/control.py @@ -88,8 +88,16 @@ def run_until(time_point, callbacks=None): abs(next - callback_events[-1][0]) < simulator.state.dt: active_callbacks.append(callback_events.pop()[1]) - next = min(next, time_point) + # Advance by at least one time step, so that a callback asking for less + # than a time step ahead cannot stall the loop, and not beyond time_point. + next = min(max(next, simulator.state.t + simulator.state.dt), time_point) + t_before = simulator.state.t simulator.state.run_until(next) + if simulator.state.t <= t_before: + # Some simulators (e.g. NEURON) stop at the last time step before + # their target, so when time_point is off the time grid and less + # than one step ahead, the simulation cannot advance any further. + break callback_events.extend((callback(simulator.state.t), callback) for callback in active_callbacks) else: diff --git a/test/system/scenarios/test__simulation_control.py b/test/system/scenarios/test__simulation_control.py index 080d2a3d..5bda1e71 100644 --- a/test/system/scenarios/test__simulation_control.py +++ b/test/system/scenarios/test__simulation_control.py @@ -1,3 +1,4 @@ +import numpy as np from numpy.testing import assert_array_equal, assert_array_almost_equal, assert_allclose from .fixtures import run_with_simulators @@ -127,6 +128,44 @@ def test_run_until(sim): sim.end() +def _callback(interval, times): + def callback(t): + times.append(t) + if len(times) > 1000: + raise RuntimeError(f"run() with callbacks is not advancing (t = {t})") + return t + interval + return callback + + +@run_with_simulators("nest", "neuron", "brian2", "arbor") +def test_run_with_callbacks(sim): + sim.setup(timestep=0.1) + sim.Population(1, sim.IF_cond_exp()) + # a callback interval that is not a multiple of the time step + times = [] + sim.run(10.0, callbacks=[_callback(0.3, times)]) + assert sim.get_current_time() == pytest.approx(10.0) + assert len(times) == 35 + assert times[-1] == pytest.approx(10.0) + # a callback interval shorter than the time step: called once per step + times = [] + sim.run(1.0, callbacks=[_callback(0.05, times)]) + assert sim.get_current_time() == pytest.approx(11.0) + assert_allclose(times, np.arange(10.0, 11.05, 0.1)) + sim.end() + + +@run_with_simulators("neuron", "brian2", "arbor") # NEST does not accept off-grid times +def test_run_with_callbacks_off_grid(sim): + sim.setup(timestep=0.1) + sim.Population(1, sim.IF_cond_exp()) + times = [] + sim.run(10.07, callbacks=[_callback(1.0, times)]) + # some simulators stop at the last time step before an off-grid time, others after it + assert abs(sim.get_current_time() - 10.07) < 0.1 + sim.end() + + @run_with_simulators("nest", "neuron", "brian2", "arbor") def test_default_delay(sim):