Simulation control

The update schedule

When a simulation is run, the operations are done in the following order by default:

  1. Update every NeuronGroup, this typically performs an integration time step for the differential equations defining the neuron model.
  2. Check the threshold condition and propagate the spikes to the target neurons.
  3. Update every Synapses, this may include updating the state of targeted NeuronGroup objects
  4. Reset all neurons that spiked.
  5. Call all user-defined operations and state monitors.

The user-defined operations and state monitors can be placed at other places in this schedule, by using the keyword when. The values can be start, before_groups, after_groups, middle, before_connections, after_connections, before_resets, after_resets or end (default: end). For example, to call a function f at the beginning of every timestep:

def f():

or to record the value of a state variable just before the resets:


Basic simulation control

The simulation is run simply as follows:


where 1000 ms is the duration of the run. It can be stopped during the simulation with the instruction stop(), and the network can be reinitialised with the instruction reinit(). The run() function also has some options for reporting the progress of the simulation as it runs, for example this will print out the elapsed time, percentage of the simulation this is complete, and an estimate of the remaining time every 10s:

run(100*second, report='text')

When the run() function is called, Brian looks for all relevant objects in the namespace (groups, connections, monitors, user operations), and runs them. In complex scripts, the user might want to run only selected objects. In that case, there are two options. The first is to create a Network object (see next section). The second is to use the forget() function on objects you want to exclude from being used. These can then be later added back using the recall() function.

Users of ipython may also want to make use of the clear() function which removes all Brian objects and deletes their data. This is useful because ipython keeps persistent references to these objects which stops memory from being freed.

The Network class

A Network object holds a collection of objets that can be run, i.e., objects with class NeuronGroup, Connection, SpikeMonitor, StateMonitor (or subclasses) or any user-defined operation with the decorator network_operation(). Thoses objects can then be simulated. Example:

G = NeuronGroup(...)
C = Connection(...)
net = Network(G,C)*second)

You can also pass lists of objects. The simulation can be controlled with the methods stop and reinit.

The MagicNetwork object

When run(), reinit() and stop() are called, they act on the “magic network” (the network consisting of all relevant objects such as groups, connections, monitors and user operations). This “magic network” can be explicitly constructed using the MagicNetwork object:

G = NeuronGroup(...)
C = Connection(...)
net = MagicNetwork()*second)