I am using the Sci Py zombie code as an example.
The problem with ODEint is I am integrating over long times (in this case from 0 to 500, but for other projects up to thousands of years) and it does this integration in one go. I would like to implement a long output timescale, but a small integration timescale and change the number of samples generated to 1, but continuously update the timescale so that I can write a new point to a file per loop. i.e I do not want it to integrate from 0 to 500 in one go, but rather loop over a changing timescale. Can this be implemented?
# zombie apocalypse modeling
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import odeint
plt.ion()
plt.rcParams['figure.figsize'] = 10, 8
P = 0 # birth rate
d = 0.0001 # natural death percent (per day)
B = 0.0095 # transmission percent (per day)
G = 0.0001 # resurect percent (per day)
A = 0.0001 # destroy percent (per day)
# solve the system dy/dt = f(y, t)
def f(y, t):
Si = y[0]
Zi = y[1]
Ri = y[2]
# the model equations (see Munz et al. 2009)
f0 = P - B*Si*Zi - d*Si
f1 = B*Si*Zi + G*Ri - A*Si*Zi
f2 = d*Si + A*Si*Zi - G*Ri
return [f0, f1, f2]
# initial conditions
S0 = 500. # initial population
Z0 = 0 # initial zombie population
R0 = 0 # initial death population
y0 = [S0, Z0, R0] # initial condition vector
t = np.linspace(0, 50, 1000) # time grid
# solve the DEs
soln = odeint(f, y0, t)
S = soln[:, 0]
Z = soln[:, 1]
R = soln[:, 2]
Why not just do that?
for k in range(50):
t = np.linspace(k, k+1, 20)
sol = odeint(f, y0, t)
y0 = sol[-1] #initial value for next segment
S,Z,R = sol.T
# output to file, other post-processing