brainpy.integrators.runner.IntegratorRunner
brainpy.integrators.runner.IntegratorRunner#
- class brainpy.integrators.runner.IntegratorRunner(target, inits=None, args=None, dyn_args=None, dt=None, fun_monitors=None, monitors=None, dyn_vars=None, jit=True, numpy_mon_after_run=True, progress_bar=True)[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.integrators.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 >>> args={'a': 1., 'b': 1.}, # update arguments >>> dyn_args={'I': bp.inputs.ramp_input(0, 4, 200)}, # each time each current input >>> ) >>> runner.run(100.) >>> bp.visualize.line_plot(runner.mon.ts, runner.mon.V, plot_ids=[0, 1, 4], show=True)
(Source code, png, hires.png, pdf)
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='milstein') >>> >>> runner = bp.integrators.IntegratorRunner( >>> lorenz, >>> monitors=['x', 'y', 'z'], >>> inits=[1., 1., 1.], # initialize all variable to 1. >>> args={'p': 0.1}, >>> dt=0.01 >>> ) >>> runner.run(100.) >>> >>> 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)
Example to run an DDE integrator,
>>> import brainpy as bp >>> import brainpy.math as bm >>> import matplotlib.pyplot as plt >>> >>> # Mackey-Glass equation >>> dt = 0.01; beta=2.; gamma=1.; tau=2.; n=9.65 >>> mg_eq = lambda x, t, xdelay: (beta * xdelay(t - tau) / (1 + xdelay(t - tau) ** n) >>> - gamma * x) >>> xdelay = bm.TimeDelay(bm.asarray([1.2]), delay_len=tau, dt=dt, before_t0=lambda t: 1.2) >>> integral = bp.ddeint(mg_eq, method='rk4', state_delays={'x': xdelay}) >>> runner = bp.integrators.IntegratorRunner( >>> integral, >>> monitors=['x', ], >>> fun_monitors={'x(tau)': (lambda t, _: xdelay(t - tau))}, >>> inits=[1.2], # initialize all variable to 1. >>> args={'xdelay': xdelay}, dt=dt, >>> ) >>> runner.run(100.) >>> plt.plot(runner.mon['x'].flatten(), runner.mon['x(tau)'].flatten()) >>> plt.show()
- __init__(target, inits=None, args=None, dyn_args=None, dt=None, fun_monitors=None, monitors=None, dyn_vars=None, jit=True, numpy_mon_after_run=True, progress_bar=True)[source]#
Initialization of structural runner for integrators.
- Parameters
target (Integrator) –
monitors (sequence of str) –
fun_monitors (dict) –
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 corresponds to one set of arguments.
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.dyn_vars (dict) –
jit (bool) –
progress_bar (bool) –
numpy_mon_after_run (bool) –
Methods
__init__
(target[, inits, args, dyn_args, ...])Initialization of structural runner for integrators.
run
(duration[, start_t])