def simulate(beta, nu, p0, S0, I0, V_T): p = lambda t: p0 if 6 <= t <= 6 + V_T else 0 solver = odespy.Euler(f, f_args=(beta, nu, p)) solver.set_initial_condition([S0, I0, 0, 0]) dt = 0.5 # t counts days T = 60 N = int(T / dt) t = np.linspace(0, T, N + 1) u, t = solver.solve(t) I = u[:, 1] return I.max()
def get_solver(self, func): """ Returns the solver method from odespy package. Args: func: function function with ODE system. Returns: an instance of odeSolver """ if self.solverMethod is solverMethod.LSODA: solver = odespy.Lsoda(func) elif self.solverMethod is solverMethod.LSODAR: solver = odespy.Lsodar(func) elif self.solverMethod is solverMethod.LSODE: solver = odespy.Lsode(func) elif self.solverMethod is solverMethod.HEUN: solver = odespy.Heun(func) elif self.solverMethod is solverMethod.EULER: solver = odespy.Euler(func) elif self.solverMethod is solverMethod.RK4: solver = odespy.RK4(func) elif self.solverMethod is solverMethod.DORMAN_PRINCE: solver = odespy.DormandPrince(func) elif self.solverMethod is solverMethod.RKFehlberg: solver = odespy.RKFehlberg(func) elif self.solverMethod is solverMethod.Dopri5: solver = odespy.Dopri5(func) elif self.solverMethod is solverMethod.Dop853: solver = odespy.Dop853(func) elif self.solverMethod is solverMethod.Vode: solver = odespy.Vode(func) elif self.solverMethod is solverMethod.AdamsBashforth2: solver = odespy.AdamsBashforth2(func, method='bdf') elif self.solverMethod is solverMethod.Radau5: solver = odespy.Radau5(func) elif self.solverMethod is solverMethod.AdamsBashMoulton2: solver = odespy.AdamsBashMoulton2(func) # update default parameters solver.nsteps = SolverConfigurations.N_STEPS solver.atol = SolverConfigurations.ABSOLUTE_TOL solver.rtol = SolverConfigurations.RELATIVE_TOL return solver
import odespy import numpy as np import sys I0 = 1 S0 = 1500. period = 360 beta = lambda t: 0.0003 * np.sin(np.pi / period * t)**6 nu = 1. / 10 mu = 1. / 100 rho = 1. / 150 p = lambda t: 0.01 * np.sin(np.pi / period * (t - 50))**6 p = lambda t: 0.0 solver = odespy.Euler(f, f_args=(beta, nu, p, mu, rho)) solver.set_initial_condition([S0, I0, 0, 0]) dt = 0.5 # t counts days T = 1400 N = int(T / dt) t = np.linspace(0, T, N + 1) u, t = solver.solve(t) S, I, R, V = u[:, 0], u[:, 1], u[:, 2], u[:, 3] from matplotlib.pyplot import * plot(t, S, 'r-', t, I, 'b-', t, R, 'g-', t, V, 'y-') legend(['S', 'I', 'R', 'V']) savefig('tmppng') savefig('tmp.pdf') show()
return [-beta(t)*S*I, beta(t)*S*I - nu*I, nu*I] import odespy import numpy as np import sys # beta = 0.0005; nu = 0.1; I0 = 1 # beta = 0.0001; nu = 0.1; I0 = 1 and 50 beta = lambda t: 0.0005 if t < 12 else 0.00005 nu = 0.1 I0 = 1 S0 = 1500. solver = odespy.Euler(f, f_args=(beta, nu)) solver.set_initial_condition([S0, I0, 0]) dt = 0.5 # t counts days T = 60 N = int(T/dt) t = np.linspace(0, T, N+1) u, t = solver.solve(t) S, I, R = u[:,0], u[:,1], u[:,2] from matplotlib.pyplot import * plot(t, S, 'r-', t, I, 'b-', t, R, 'g-') legend(['S', 'I', 'R']) savefig('tmppng'); savefig('tmp.pdf') show()
rtol = 1E-5 adams_or_bdf = 'bdf' import odespy solvers = [ odespy.AdamsBashMoulton2(problem), odespy.AdamsBashMoulton3(problem), odespy.AdamsBashforth2(problem), odespy.AdamsBashforth3(problem), odespy.AdamsBashforth4(problem), odespy.AdaptiveResidual(problem, solver='Euler'), odespy.Backward2Step(problem), odespy.BackwardEuler(problem), odespy.Dop853(problem, rtol=rtol, atol=atol), odespy.Dopri5(problem, rtol=rtol, atol=atol), odespy.Euler(problem), odespy.Heun(problem), odespy.Leapfrog(problem), odespy.LeapfrogFiltered(problem), odespy.MidpointImplicit(problem), odespy.MidpointIter(problem, max_iter=10, eps_iter=1E-7), odespy.RK2(problem), odespy.RK3(problem), odespy.RK4(problem), odespy.RKFehlberg(problem, rtol=rtol, atol=atol), odespy.SymPy_odefun(problem), odespy.ThetaRule(problem), odespy.Trapezoidal(problem), odespy.Vode(problem, rtol=rtol, atol=atol, adams_or_bdf=adams_or_bdf), ]