Clocks

Many Brian objects store a clock object (always passed in the initialiser with the keyword clock=...). If no clock is specified, the program uses the global default clock. When Brian is initially imported, this is the object defaultclock, and it has a default time step of 0.1ms. In a simple script, you can override this by writing (for example):

defaultclock.dt = 1*ms

However, there are other ways to access or redefine the default clock (see functions below).

You may wish to use multiple clocks in your program. In this case, for each object which requires one, you have to pass a copy of its Clock object. The network run function automatically handles objects with different clocks, updating them all at the appropriate time according to their time steps (value of dt).

Multiple clocks can be useful, for example, for defining a simulation that runs with a very small dt, but with some computationally expensive operation running at a lower frequency.

The Clock class

class brian.Clock(dt=0.1 * msecond, t=0.0 * second, order=0, makedefaultclock=False)

An object that holds the simulation time and the time step.

Initialisation arguments:

dt
The time step of the simulation.
t
The current time of the clock.
order
If two clocks have the same time, the order of the clock is used to resolve which clock is processed first, lower orders first.
makedefaultclock
Set to True to make this clock the default clock.

The times returned by this clock are always off the form n*dt+offset for integer n and float dt and offset. For example, for a clock with dt=10*ms, setting t=25*ms will set n=2 and offset=5*ms. For a clock that uses true float values for t rather than underlying integers, use FloatClock (although see the caveats there).

In order to make sure that certain operations happen in the correct sequence, you can use the order attribute, clocks with a lower order will be processed first if the time is the same. The condition for two clocks to be considered as having the same time is abs(t1-t2)<epsilon*abs(t1), a standard test for equality of floating point values. For ordinary clocks based on integer times, the value of epsilon is 1e-14, and for float based clocks it is 1e-8.

The behaviour of clocks was changed in version 1.3 of Brian, if this is causing problems you might try using FloatClock or if that doesn’t solve the problem, NaiveClock.

Methods

reinit([t=0*second])

Reinitialises the clock time to zero (or to your specified time).

Attributes

t
dt

Current time and time step with units.

Advanced

Attributes

end

The time at which the current simulation will end, set by the Network.run() method.

Methods

tick()

Advances the clock by one time step.

set_t(t)
set_dt(dt)
set_end(end)

Set the various parameters.

get_duration()

The time until the current simulation ends.

set_duration(duration)

Set the time until the current simulation ends.

still_running()

Returns a bool to indicate whether the current simulation is still running.

For reasons of efficiency, we recommend using the methods tick(), set_duration() and still_running() (which bypass unit checking internally).

class brian.EventClock(dt=0.1 * msecond, t=0.0 * second, order=0, makedefaultclock=False)

Clock that is used for events.

Works the same as a Clock except that it is never guessed as a clock to use by NeuronGroup, etc. These clocks can be used to make multiple clock simulations without causing ambiguous clock problems.

class brian.FloatClock(dt=0.1 * msecond, t=0.0 * second, order=0, makedefaultclock=False)

Similar to a Clock except that it uses a float value of t rather than an integer based underlying value. This means that over time the values of t can drift slightly off the grid, and sometimes t/dt will be slightly less than an integer value, sometimes slightly more. This can cause problems in cases where the computation int(t/dt) is performed to extract an index value, as sometimes an index will be repeated or skipped. However, this form of clock can be used for backwards compatibility with versions of Brian before the new integer based clock was introduced, and for more flexibility than the new version allows for. Note also that the equality condition for this clock uses an epsilon of 1e-8 rather than 1e-14. See Clock for more details on this. For full backwards compatibility with older versions of Brian, use NaiveClock.

class brian.NaiveClock(dt=0.1 * msecond, t=0.0 * second, order=0, makedefaultclock=False)

Provided for backwards compatibility with older versions of Brian. Does not perform any approximate equality tests for clocks, meaning that clock processing sequence is undpredictable. Typically, users should use Clock or FloatClock.

class brian.RegularClock(dt=0.1 * msecond, t=0.0 * second, order=0, makedefaultclock=False)

Deprecated. Now the same as Clock. The old Clock class is now FloatClock.

The default clock

brian.defaultclock

The default clock object

Note that this is only the default clock object if you haven’t redefined it with the define_default_clock() function or the makedefaultclock=True option of a Clock object. A safe way to get hold of the default clock is to use the functions:

However, it is suitable for short scripts, e.g.:

defaultclock.dt = 1*ms
...
brian.define_default_clock(**kwds)

Create a new default clock

Uses the keywords of the Clock initialiser.

Sample usage:

define_default_clock(dt=1*ms)
brian.reinit_default_clock(t=0.0 * second)

Reinitialise the default clock (to zero or a specified time)

brian.get_default_clock()

Returns the default clock object.