brainpy.IntegratorRunner#

class brainpy.IntegratorRunner(target, inits=None, dt=None, monitors=None, dyn_vars=None, jit=True, numpy_mon_after_run=True, progress_bar=True, args=None, dyn_args=None, fun_monitors=None)[source]#

Structural runner for numerical integrators in brainpy.

Examples

Example to run an ODE integrator,

>>> import brainpy as bp
>>> import brainpy.math as bm
>>> a=0.7; b=0.8; tau=12.5
>>> dV = lambda V, t, w, I: V - V * V * V / 3 - w + I
>>> dw = lambda w, t, V, a, b: (V + a - b * w) / tau
>>> integral = bp.odeint(bp.JointEq([dV, dw]), method='exp_auto')
>>>
>>> runner = bp.IntegratorRunner(
>>>          integral,  # the simulation target
>>>          monitors=['V', 'w'],  # the variables to monitor
>>>          inits={'V': bm.random.rand(10),
>>>                 'w': bm.random.normal(size=10)},  # the initial values
>>> )
>>> runner.run(100.,
>>>            args={'a': 1., 'b': 1.},  # update arguments
>>>            dyn_args={'I': bp.inputs.ramp_input(0, 4, 100)},  # each time each current input
>>> )
>>> bp.visualize.line_plot(runner.mon.ts, runner.mon.V, plot_ids=[0, 1, 4], show=True)

(Source code, png, hires.png, pdf)

../../../_images/brainpy-IntegratorRunner-1.png

Example to run an SDE intragetor,

>>> import brainpy as bp
>>> import brainpy.math as bm
>>> # stochastic Lorenz system
>>> sigma=10; beta=8 / 3; rho=28
>>> g = lambda x, y, z, t, p: (p * x, p * y, p * z)
>>> f = lambda x, y, z, t, p: [sigma * (y - x), x * (rho - z) - y, x * y - beta * z]
>>> lorenz = bp.sdeint(f, g, method='milstein2')
>>>
>>> runner = bp.IntegratorRunner(
>>>   lorenz,
>>>   monitors=['x', 'y', 'z'],
>>>   inits=[1., 1., 1.], # initialize all variable to 1.
>>>   dt=0.01
>>> )
>>> runner.run(100., args={'p': 0.1},)
>>>
>>> import matplotlib.pyplot as plt
>>> fig = plt.figure()
>>> ax = fig.gca(projection='3d')
>>> plt.plot(runner.mon.x.squeeze(), runner.mon.y.squeeze(), runner.mon.z.squeeze())
>>> ax.set_xlabel('x')
>>> ax.set_xlabel('y')
>>> ax.set_xlabel('z')
>>> plt.show()

(Source code, png, hires.png, pdf)

../../../_images/brainpy-IntegratorRunner-2.png
__init__(target, inits=None, dt=None, monitors=None, dyn_vars=None, jit=True, numpy_mon_after_run=True, progress_bar=True, args=None, dyn_args=None, fun_monitors=None)[source]#

Initialization of structural runner for integrators.

Parameters
  • target (Integrator) – The target to run.

  • monitors (sequence of str) – The variables to monitor.

  • fun_monitors (dict) – The monitors with callable functions. .. deprecated:: 2.3.1

  • inits (sequence, dict) – The initial value of variables. With this parameter, you can easily control the number of variables to simulate. For example, if one of the variable has the shape of 10, then all variables will be an instance of brainpy.math.Variable with the shape of \((10,)\).

  • args (dict) –

    The equation arguments to update. Note that if one of the arguments are heterogeneous (i.e., a tensor), it means we should run multiple trials. However, you can set the number of the elements in the variables so that each pair of variables can correspond to one set of arguments.

    Deprecated since version 2.3.1: Will be removed after version 2.4.0.

  • dyn_args (dict) –

    The dynamically changed arguments. This means this argument can control the argument dynamically changed. For example, if you want to inject a time varied currents into the HH neuron model, you can pack the currents into this dyn_args argument.

    Deprecated since version 2.3.1: Will be removed after version 2.4.0.

  • dt (float, int) –

  • dyn_vars (dict) –

  • jit (bool) –

  • progress_bar (bool) –

  • numpy_mon_after_run (bool) –

Methods

__init__(target[, inits, dt, monitors, ...])

Initialization of structural runner for integrators.

cpu()

Move all variable into the CPU device.

cuda()

Move all variables into the GPU device.

load_state_dict(state_dict[, warn])

Copy parameters and buffers from state_dict into this module and its descendants.

load_states(filename[, verbose])

Load the model states.

nodes([method, level, include_self])

Collect all children nodes.

register_implicit_nodes(*nodes[, node_cls])

register_implicit_vars(*variables, ...)

run(duration[, start_t, eval_time, args, ...])

The running function.

save_states(filename[, variables])

Save the model states.

state_dict()

Returns a dictionary containing a whole state of the module.

to(device)

Moves all variables into the given device.

tpu()

Move all variables into the TPU device.

train_vars([method, level, include_self])

The shortcut for retrieving all trainable variables.

tree_flatten()

Flattens the object as a PyTree.

tree_unflatten(aux, dynamic_values)

New in version 2.3.1.

unique_name([name, type_])

Get the unique name for this object.

vars([method, level, include_self, ...])

Collect all variables in this node and the children nodes.

Attributes

name

Name of the model.