Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion doc/simulation_control.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 9 additions & 1 deletion pyNN/common/control.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
39 changes: 39 additions & 0 deletions test/system/scenarios/test__simulation_control.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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):
Expand Down
Loading