I am working on a simulation of neural circuits. The circuit consists of eight segments, and I solve the activity of each segment with differential equations. The differential equation is
dE[i]/dt = f(b[i] * E[i]) (i = 0, 1, ... 7)
and b[i] is changed when E[i] cuts the threshold from below to above. To implement this, I built the following code using scipy's solve_ivp.
seg_number = 8
t0 = 0
tend = 2e5
x0 = [0]*seg_number
def gen_event(i):
def event(t, x):
return x[i] - threshold
event.terminal = True
event.direction = 1
return event
events = [gen_event(i) for i in range(seg_number)]
def func(t, x): #differential equation
dx = []
for i in range(seg_number):
・・・
return(dx)
while True:
orbit = solve_ivp(func, (t0, tend), x0, events = events)
if orbit.status == 1:
・・・ # update b[i]
t0 = orbit.t[-1]
x0 = orbit.y[:, -1].copy()
else:
break
However, in this code, when b[i] and the initial conditions are changed and the differential equations are solved again, the initial condition is detected as an event, causing an infinite loop. If I add a small amount to the initial conditions, the solution proceeds, but I don't think this is a good solution.
x0 = orbit.y[:, -1].copy() + 1e-12