|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | + |
| 4 | +import pytest |
| 5 | +pytest.skip('Test cannot pass in github action.', allow_module_level=True) |
| 6 | +import unittest |
| 7 | + |
| 8 | +import brainpy as bp |
| 9 | +import brainpy.math as bm |
| 10 | +import matplotlib.pyplot as plt |
| 11 | + |
| 12 | +block = False |
| 13 | + |
| 14 | + |
| 15 | +class FitzHughNagumoModel(bp.dyn.DynamicalSystem): |
| 16 | + def __init__(self, method='exp_auto'): |
| 17 | + super(FitzHughNagumoModel, self).__init__() |
| 18 | + |
| 19 | + # parameters |
| 20 | + self.a = 0.7 |
| 21 | + self.b = 0.8 |
| 22 | + self.tau = 12.5 |
| 23 | + |
| 24 | + # variables |
| 25 | + self.V = bm.Variable(bm.zeros(1)) |
| 26 | + self.w = bm.Variable(bm.zeros(1)) |
| 27 | + self.Iext = bm.Variable(bm.zeros(1)) |
| 28 | + |
| 29 | + # functions |
| 30 | + def dV(V, t, w, Iext=0.): |
| 31 | + dV = V - V * V * V / 3 - w + Iext |
| 32 | + return dV |
| 33 | + |
| 34 | + def dw(w, t, V, a=0.7, b=0.8): |
| 35 | + dw = (V + a - b * w) / self.tau |
| 36 | + return dw |
| 37 | + |
| 38 | + self.int_V = bp.odeint(dV, method=method) |
| 39 | + self.int_w = bp.odeint(dw, method=method) |
| 40 | + |
| 41 | + def update(self, tdi): |
| 42 | + t, dt = tdi['t'], tdi['dt'] |
| 43 | + self.V.value = self.int_V(self.V, t, self.w, self.Iext, dt) |
| 44 | + self.w.value = self.int_w(self.w, t, self.V, self.a, self.b, dt) |
| 45 | + self.Iext[:] = 0. |
| 46 | + |
| 47 | + |
| 48 | +class TestBifurcation1D(unittest.TestCase): |
| 49 | + def test_bifurcation_1d(self): |
| 50 | + bp.math.enable_x64() |
| 51 | + |
| 52 | + @bp.odeint |
| 53 | + def int_x(x, t, a=1., b=1.): |
| 54 | + return bp.math.sin(a * x) + bp.math.cos(b * x) |
| 55 | + |
| 56 | + pp = bp.analysis.PhasePlane1D( |
| 57 | + model=int_x, |
| 58 | + target_vars={'x': [-bp.math.pi, bp.math.pi]}, |
| 59 | + resolutions=0.1 |
| 60 | + ) |
| 61 | + pp.plot_vector_field() |
| 62 | + pp.plot_fixed_point(show=True) |
| 63 | + |
| 64 | + bf = bp.analysis.Bifurcation1D( |
| 65 | + model=int_x, |
| 66 | + target_vars={'x': [-bp.math.pi, bp.math.pi]}, |
| 67 | + target_pars={'a': [0.5, 1.5], 'b': [0.5, 1.5]}, |
| 68 | + resolutions={'a': 0.1, 'b': 0.1} |
| 69 | + ) |
| 70 | + bf.plot_bifurcation(show=False) |
| 71 | + plt.show(block=block) |
| 72 | + plt.close() |
| 73 | + bp.math.disable_x64() |
| 74 | + |
| 75 | + def test_bifurcation_2d(self): |
| 76 | + bp.math.enable_x64() |
| 77 | + |
| 78 | + model = FitzHughNagumoModel() |
| 79 | + bif = bp.analysis.Bifurcation2D( |
| 80 | + model=model, |
| 81 | + target_vars={'V': [-3., 3.], 'w': [-1, 3.]}, |
| 82 | + target_pars={'Iext': [0., 1.]}, |
| 83 | + resolutions={'Iext': 0.1} |
| 84 | + ) |
| 85 | + bif.plot_bifurcation() |
| 86 | + bif.plot_limit_cycle_by_sim() |
| 87 | + plt.show(block=block) |
| 88 | + |
| 89 | + # bp.math.disable_x64() |
0 commit comments