在ODE中使用事件的时间安排

时间:2018-09-10 16:59:27

标签: python scipy ode

(这是与Scipy ODE time steps going backward相关的后续问题)

我有一个方程组,我正在尝试使用scipy的solve_ivp求解。这是最小的工作代码:

import numpy as np
from scipy.integrate import solve_ivp

def synapse(t, t0):
    tau_1 = 5.3
    tau_2 = 0.05
    tau_rise = (tau_1 * tau_2) / (tau_1 - tau_2)
    B = ((tau_2 / tau_1)**(tau_rise / tau_1) - (tau_2 / tau_1)**(tau_rise / tau_2)) ** -1
    return B*(np.exp(-(t - t0) / tau_1) - np.exp(-(t - t0) / tau_2))

def alpha_m(v, vt):
    return -0.32*(v - vt -13)/(np.exp(-1*(v-vt-13)/4)-1)

def beta_m(v, vt):
    return 0.28 * (v - vt - 40) / (np.exp((v- vt - 40) / 5) - 1)

def alpha_h(v, vt):
    return 0.128 * np.exp(-1 * (v - vt - 17) / 18)

def beta_h(v, vt):
    return  4 / (np.exp(-1 * (v - vt - 40) / 5) + 1)

def alpha_n(v, vt):
    return -0.032*(v - vt - 15)/(np.exp(-1*(v-vt-15)/5) - 1)

def beta_n(v, vt):
    return 0.5* np.exp(-1*(v-vt-10)/40)

def event(t,X):
    return X[0] + 20
event.terminal = False
event.direction = +1

def f(t, X):
    V = X[0]
    m = X[1]
    h = X[2]
    n = X[3]

    last_inputspike = inputspike[inputspike.searchsorted(t, side='right') - 1 ]
    last_t_event = -100 #Not sure what to put here

    g_syn_in = synapse(t, last_inputspike)
    g_syn_spike = synapse(t, last_t_event)
    syn = 0.5 * g_syn_in * (V - 0) + 0.2 * g_syn_spike * (V + 70)

    dVdt = - 50*m**3*h*(V-60) - 10*n**4*(V+100) - syn - 0.1*(V + 70)
    dmdt = alpha_m(V, -45)*(1-m) - beta_m(V, -45)*m
    dhdt = alpha_h(V, -45)*(1-h) - beta_h(V, -45)*h
    dndt = alpha_n(V, -45)*(1-n) - beta_n(V, -45)*n
    return [dVdt, dmdt, dhdt, dndt]


# Define the spike events:
nbr_spike = 20
beta = 100
first_spike_date = 500

np.random.seed(0)
inputspike = np.cumsum( np.random.exponential(beta, size=nbr_spike) ) + first_spike_date
inputspike = np.insert(inputspike, 0, -1e4)  # set a very old spike at t=-1e4
                           # it is a hack in order to set a t0  for t<first_spike_date (model settle time)
                           # so that `synapse(t, t0)` can be called regardless of t
                           # synapse(t, -1e4) = 0  for t>0

# Solve:
t_start = 0.0
t_end = 2000

X_start = [-70, 0, 1,0]

sol = solve_ivp(f, [t_start, t_end], X_start, method='BDF', max_step=1, vectorized=True, events=event)
print(sol.message)

我想检测是否存在尖峰(定义为V> 20),并以类似的方式通过更改syn使尖峰的定时影响ODE中的g_syn_spike随机输入会影响它。

本质上,我想知道是否有可能,以及如何在给定的求解器迭代中访问sol.t_events的最后一个值?

1 个答案:

答案 0 :(得分:0)

我一直在寻找一种在连续微分方程系统中模拟离散事件的方法。对这种不连续性进行建模并不是一件容易的事,并且有两个(最近)可以帮助您应对的软件包:

assimulo-https://pypi.org/project/Assimulo/

simupy-https://pypi.org/project/simpy/

(不是简单的,这仅适用于离散系统)

如果您已经找到其他解决方案,希望对您有所帮助,