Skip to content

Commit

Permalink
Merge pull request #126 from PKU-NIP-Lab/changes
Browse files Browse the repository at this point in the history
fix bugs
  • Loading branch information
chaoming0625 authored Mar 27, 2022
2 parents e871ed8 + 74cbfd1 commit 3d6b84f
Show file tree
Hide file tree
Showing 6 changed files with 186 additions and 14 deletions.
2 changes: 1 addition & 1 deletion brainpy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-

__version__ = "2.1.2"
__version__ = "2.1.3"


try:
Expand Down
19 changes: 11 additions & 8 deletions brainpy/datasets/chaotic_systems.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def modified_lu_chen_series(duration, dt=0.001, a=36, c=20, b=3, d1=1, d2=0., ta
eq.x[:] = inits['x']
eq.y[:] = inits['y']
eq.z[:] = inits['z']
runner = dyn.DSRunner(eq, monitors=['x', 'y', 'z'], dt=dt, progress_bar=False,
runner = dyn.DSRunner(eq,
monitors=['x', 'y', 'z'],
dt=dt, progress_bar=False,
numpy_mon_after_run=numpy_mon)
runner.run(duration)
return {'ts': runner.mon.ts,
Expand Down Expand Up @@ -167,19 +169,20 @@ def mackey_glass_series(duration, dt=0.1, beta=2., gamma=1., tau=2., n=9.65,
assert isinstance(inits, (bm.ndarray, jnp.ndarray))

rng = bm.random.RandomState(seed)
xdelay = bm.TimeDelay(inits, tau, dt=dt)
xdelay.data = inits + 0.2 * (rng.random((xdelay.num_delay_step,) + inits.shape) - 0.5)
xdelay = bm.TimeDelay(inits, tau, dt=dt, interp_method='round')
xdelay.data.value = inits + 0.2 * (rng.random((xdelay.num_delay_step,) + inits.shape) - 0.5)

@ddeint(method=method, state_delays={'x': xdelay})
@ddeint(method=method,
state_delays={'x': xdelay})
def mg_eq(x, t):
return beta * xdelay(t - tau) / (1 + xdelay(t - tau) ** n) - gamma * x
xtau = xdelay(t - tau)
return beta * xtau / (1 + xtau ** n) - gamma * x

runner = IntegratorRunner(mg_eq,
inits={'x': inits},
monitors=['x'],
fun_monitors={'x(tau)': lambda t, dt: xdelay(t - tau)},
progress_bar=progress_bar,
dt=dt,
fun_monitors={'x(tau)': lambda t, _: xdelay(t - tau)},
progress_bar=progress_bar, dt=dt,
numpy_mon_after_run=numpy_mon)
runner.run(duration)
return {'ts': runner.mon.ts,
Expand Down
12 changes: 10 additions & 2 deletions brainpy/integrators/dde/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,23 @@ def __call__(self, *args, **kwargs):
delay.update(new_dvars[key])
elif isinstance(delay, bm.TimeDelay):
delay.update(kwargs['t'] + dt, new_dvars[key])
raise ValueError('Unknown delay variable.')
else:
raise ValueError('Unknown delay variable. We only supports '
'brainpy.math.LengthDelay, brainpy.math.TimeDelay, '
'brainpy.math.NeutralDelay. '
f'While we got {delay}')

# update state delay variables
for key, delay in self.state_delays.items():
if isinstance(delay, bm.LengthDelay):
delay.update(dict_vars[key])
elif isinstance(delay, bm.TimeDelay):
delay.update(kwargs['t'] + dt, dict_vars[key])
raise ValueError('Unknown delay variable.')
else:
raise ValueError('Unknown delay variable. We only supports '
'brainpy.math.LengthDelay, brainpy.math.TimeDelay, '
'brainpy.math.NeutralDelay. '
f'While we got {delay}')

return new_vars

Expand Down
10 changes: 8 additions & 2 deletions brainpy/integrators/dde/explicit_rk.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-

from typing import Dict
import brainpy.math as bm

from brainpy.integrators.constants import F, DT
from brainpy.integrators.dde.base import DDEIntegrator
from brainpy.integrators.ode import common
Expand Down Expand Up @@ -102,11 +105,14 @@ class Ralston2(ExplicitRKIntegrator):


class RK2(ExplicitRKIntegrator):
def __init__(self, f, beta=2 / 3, var_type=None, dt=None, name=None, show_code=False):
def __init__(self, f, beta=2 / 3, var_type=None, dt=None, name=None,
state_delays: Dict[str, bm.TimeDelay] = None,
neutral_delays: Dict[str, bm.NeutralDelay] = None):
self.A = [(), (beta,)]
self.B = [1 - 1 / (2 * beta), 1 / (2 * beta)]
self.C = [0, beta]
super(RK2, self).__init__(f=f, var_type=var_type, dt=dt, name=name, show_code=show_code)
super(RK2, self).__init__(f=f, var_type=var_type, dt=dt, name=name,
state_delays=state_delays, neutral_delays=neutral_delays)


register_dde_integrator('rk2', RK2)
Expand Down
155 changes: 155 additions & 0 deletions brainpy/integrators/dde/tests/test_explicit_rk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# -*- coding: utf-8 -*-


import unittest

import brainpy as bp
import brainpy.math as bm


class TestExplicitRKStateDelay(unittest.TestCase):
def test_euler(self):
xdelay = bm.TimeDelay(bm.zeros((1,)), 1., before_t0=-1., interp_method='round')

@bp.ddeint(method='euler', state_delays={'x': xdelay})
def equation(x, t, ):
return -xdelay(t - 1)

runner = bp.integrators.IntegratorRunner(equation, monitors=['x'])
runner.run(20.)

bp.visualize.line_plot(runner.mon.ts, runner.mon['x'], show=True)

def test_midpoint(self):
xdelay = bm.TimeDelay(bm.zeros((1,)), 1., before_t0=-1., interp_method='round')

@bp.ddeint(method='midpoint', state_delays={'x': xdelay})
def equation(x, t, ):
return -xdelay(t - 1)

runner = bp.integrators.IntegratorRunner(equation, monitors=['x'])
runner.run(20.)

bp.visualize.line_plot(runner.mon.ts, runner.mon['x'], show=True)

def test_heun2(self):
xdelay = bm.TimeDelay(bm.zeros((1,)), 1., before_t0=-1., interp_method='round')

@bp.ddeint(method='heun2', state_delays={'x': xdelay})
def equation(x, t, ):
return -xdelay(t - 1)

runner = bp.integrators.IntegratorRunner(equation, monitors=['x'])
runner.run(20.)

bp.visualize.line_plot(runner.mon.ts, runner.mon['x'], show=True)

def test_ralston2(self):
xdelay = bm.TimeDelay(bm.zeros((1,)), 1., before_t0=-1., interp_method='round')

@bp.ddeint(method='ralston2', state_delays={'x': xdelay})
def equation(x, t, ):
return -xdelay(t - 1)

runner = bp.integrators.IntegratorRunner(equation, monitors=['x'])
runner.run(20.)

bp.visualize.line_plot(runner.mon.ts, runner.mon['x'], show=True)

def test_rk2(self):
xdelay = bm.TimeDelay(bm.zeros((1,)), 1., before_t0=-1., interp_method='round')

@bp.ddeint(method='rk2',
state_delays={'x': xdelay})
def equation(x, t, ):
return -xdelay(t - 1)

runner = bp.integrators.IntegratorRunner(equation, monitors=['x'])
runner.run(20.)

bp.visualize.line_plot(runner.mon.ts, runner.mon['x'], show=True)

def test_rk3(self):
xdelay = bm.TimeDelay(bm.zeros((1,)), 1., before_t0=-1., interp_method='round')

@bp.ddeint(method='rk3', state_delays={'x': xdelay})
def equation(x, t, ):
return -xdelay(t - 1)

runner = bp.integrators.IntegratorRunner(equation, monitors=['x'])
runner.run(20.)

bp.visualize.line_plot(runner.mon.ts, runner.mon['x'], show=True)

def test_heun3(self):
xdelay = bm.TimeDelay(bm.zeros((1,)), 1., before_t0=-1., interp_method='round')

@bp.ddeint(method='heun3', state_delays={'x': xdelay})
def equation(x, t, ):
return -xdelay(t - 1)

runner = bp.integrators.IntegratorRunner(equation, monitors=['x'])
runner.run(20.)

bp.visualize.line_plot(runner.mon.ts, runner.mon['x'], show=True)

def test_ralston3(self):
xdelay = bm.TimeDelay(bm.zeros((1,)), 1., before_t0=-1., interp_method='round')

@bp.ddeint(method='ralston3', state_delays={'x': xdelay})
def equation(x, t, ):
return -xdelay(t - 1)

runner = bp.integrators.IntegratorRunner(equation, monitors=['x'])
runner.run(20.)

bp.visualize.line_plot(runner.mon.ts, runner.mon['x'], show=True)

def test_ssprk3(self):
xdelay = bm.TimeDelay(bm.zeros((1,)), 1., before_t0=-1., interp_method='round')

@bp.ddeint(method='ssprk3', state_delays={'x': xdelay})
def equation(x, t, ):
return -xdelay(t - 1)

runner = bp.integrators.IntegratorRunner(equation, monitors=['x'])
runner.run(20.)

bp.visualize.line_plot(runner.mon.ts, runner.mon['x'], show=True)

def test_rk4(self):
xdelay = bm.TimeDelay(bm.zeros((1,)), 1., before_t0=-1., interp_method='round')

@bp.ddeint(method='rk4', state_delays={'x': xdelay})
def equation(x, t, ):
return -xdelay(t - 1)

runner = bp.integrators.IntegratorRunner(equation, monitors=['x'])
runner.run(20.)

bp.visualize.line_plot(runner.mon.ts, runner.mon['x'], show=True)

def test_ralston4(self):
xdelay = bm.TimeDelay(bm.zeros((1,)), 1., before_t0=-1., interp_method='round')

@bp.ddeint(method='ralston4', state_delays={'x': xdelay})
def equation(x, t, ):
return -xdelay(t - 1)

runner = bp.integrators.IntegratorRunner(equation, monitors=['x'])
runner.run(20.)

bp.visualize.line_plot(runner.mon.ts, runner.mon['x'], show=True)

def test_rk4_38rule(self):
xdelay = bm.TimeDelay(bm.zeros((1,)), 1., before_t0=-1., interp_method='round')

@bp.ddeint(method='rk4_38rule', state_delays={'x': xdelay})
def equation(x, t, ):
return -xdelay(t - 1)

runner = bp.integrators.IntegratorRunner(equation, monitors=['x'])
runner.run(20.)

bp.visualize.line_plot(runner.mon.ts, runner.mon['x'], show=True)

2 changes: 1 addition & 1 deletion brainpy/nn/nodes/RC/nvar.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def init_state(self, num_batch=1):
if self.store is None:
self.store = bm.Variable(state)
else:
self.store._value = state
self.store._value = state.value

def forward(self, ff, fb=None, **shared_kwargs):
all_parts = []
Expand Down

0 comments on commit 3d6b84f

Please sign in to comment.