# run function

(Shortest import: `from brian2 import run)`

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

  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, ‘text’, ‘stdout’, ‘stderr’, function}, optional

    > How to report the progress of the simulation. If `None`, do not report progress. If `'text'` or `'stdout'` is specified, print the progress to stdout. If `'stderr'` is specified, print the progress to stderr. Alternatively, you can specify a callback `callable(elapsed, completed, start, duration)` which will be passed the amount of time elapsed as a `Quantity`, the fraction `completed` from 0.0 to 1.0, the `start` time of the simulation as a `Quantity` and the total duration of the simulation (in biological time) as a `Quantity`. The function will always be called at the beginning and the end (i.e. for fractions 0.0 and 1.0), regardless of the `report_period`.

    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 `None` (which will use the value set by `set_device`, if any).

    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.

  See also

  `Network.run`, `MagicNetwork`, `collect()`, `start_scope()`, `stop()`