# run function¶

(Shortest import: from brian2 import run)

brian2.core.magic.run(duration, report=None, report_period=10 * second, namespace=None, level=0)[source]

Runs a simulation with all “visible” Brian objects for the given duration. Calls collect() to gather all the objects, the simulation can be stopped by calling the global stop() function.

In order to avoid bugs, this function will occasionally raise MagicError when the intent of the user is not clear. See the notes to MagicNetwork for more details on this point. If you persistently see this error, then Brian is not able to safely guess what you intend to do, and you should use a Network object and call Network.run explicitly.

Parameters

duration : Quantity

The amount of simulation time to run for. If the network consists of new objects since the last time run() was called, the start time will be reset to 0. If run() is called twice or more without changing the set of objects, the second and subsequent runs will start from the end time of the previous run. To explicitly reset the time to 0, do magic_network.t = 0*second.

report : {None, ‘stdout’, ‘stderr’, ‘graphical’, function}, optional

How to report the progress of the simulation. If None, do not report progress. If stdout or stderr is specified, print the progress to stdout or stderr. If graphical, Tkinter is used to show a graphical progress bar. Alternatively, you can specify a callback function(elapsed, complete) which will be passed the amount of time elapsed (in seconds) and the fraction complete from 0 to 1.

report_period : Quantity

How frequently (in real time) to report progress.

profile : bool, optional

Whether to record profiling information (see Network.profiling_info). Defaults to False.

namespace : dict-like, optional

A namespace in which objects which do not define their own namespace will be run. If not namespace is given, the locals and globals around the run function will be used.

level : int, optional

How deep to go down the stack frame to look for the locals/global (see namespace argument). Only necessary under particular circumstances, e.g. when calling the run function as part of a function call or lambda expression. This is used in tests, e.g.: assert_raises(MagicError, lambda: run(1*ms, level=3)).

Raises

MagicError

Error raised when it was not possible for Brian to safely guess the intended use. See MagicNetwork for more details.