def test_odeint_sparse_jacobian(): # Test the sparse linear solver def func(t, y, rhs): rhs[:] = c.dot(y) return 0 def jac(t, y, fy, J): J[:,:] = c return 0 def sparse_jac(t, y, fy, J): J.data = c_sparse.data J.indicies = c.indicies J.indptr = c.indptr return 0 y0 = np.ones(4) t = np.array([0, 5, 10, 100]) # Use the full Jacobian. sol1 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='dense') # Use the sparse Jacobian. sol2 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, jacfn=sparse_jac, nnz=c_sparse.nnz, linsolver='sparse') assert_allclose(sol1.values.y, sol2.values.y, atol=1e-12, err_msg="sol1 != sol2")
def _do_problem(self, problem): t = arange(0.0, problem.stop_t, 0.05) for method in ['bdf', 'admo', 'rk5', 'rk8']: sol = odeint(problem.f, t, problem.z0, method=method) assert_( problem.verify(sol.values.y, sol.values.t), msg="Method {} on Problem {}. Printout {}".format( method, problem.__class__.__name__, False #problem.verify(sol.values.y, sol.values.t, True) ))
def _do_problem(self, problem): t = arange(0.0, problem.stop_t, 0.05) for method in ['bdf', 'admo', 'rk5', 'rk8']: sol = odeint(problem.f, t, problem.z0, method=method) assert_(problem.verify(sol.values.y, sol.values.t), msg="Method {} on Problem {}. Printout {}".format(method, problem.__class__.__name__, False #problem.verify(sol.values.y, sol.values.t, True) ) )
def intger(Si,Ei,Ii,Ri,ti,T,h,beta,sigma,gamma,mov,qp=0,tci=None, movfunct = 'sawtooth'): # Movility function shape if movfunct == 'sawtooth': def f(t): return signal.sawtooth(t) elif movfunct == 'square': def f(t): return signal.square(t) if qp ==0: def e_fun(t): return(1) elif qp == -1: if tci is None: def e_fun(t): return(mov) else: def e_fun(t): if t<tci: return(1) else: return(mov) else: if tci is None: def e_fun(t): return((1-mov)/2*(f(np.pi / qp * t - np.pi))+(1+mov)/2) else: def e_fun(t): if t<tci: return(1) else: return((1-mov)/2*(f(np.pi / qp * t - np.pi))+(1+mov)/2) def model_SEIR(t,y,ydot): S0 = y[0] E0 = y[1] I0 = y[2] R0 = y[3] N=S0+E0+I0+R0 ydot[0] = -beta * I0 * S0 /N * e_fun(t) ydot[1] = beta * I0 * S0 /N * e_fun(t) - sigma * E0 ydot[2] = sigma * E0 - (gamma * I0) ydot[3] = gamma * I0 t=np.arange(ti,T+h,h) y0 = [Si, Ei, Ii, Ri] soln = odeint(model_SEIR, t, y0) t=soln[1][0] soln=soln[1][1] S = soln[:, 0] E = soln[:, 1] I = soln[:, 2] R = soln[:, 3] # Return info just per day tout = range(int(T)+1) idx=np.searchsorted(t,tout) Sout = S[idx] Eout = E[idx] Iout = I[idx] Rout = R[idx] return({"S":Sout,"E":Eout,"I":Iout,"R":Rout,"t":tout})
def test_odeint_banded_jacobian(): # Test the use of the `Dfun`, `ml` and `mu` options of odeint default bdf # solver def func(t, y, rhs): rhs[:] = c.dot(y) return 0 def jac(t, y, J): J[:,:] = c return 0 def bjac1_rows(t, y, J): J[:,:] = cband1 return jac def bjac2_rows(t, y, J): # define BAND_ELEM(A,i,j) ((A->cols)[j][(i)-(j)+(A->s_mu)]) J[:,:] = cband2 return jac y0 = np.ones(4) t = np.array([0, 5, 10, 100]) # Use the full Jacobian. sol1 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, jacfn=jac) # Use the full Jacobian. sol2 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='dense') # Use the banded Jacobian. sol3 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, jacfn=bjac1_rows, lband=2, uband=1, linsolver='band') # Use the banded Jacobian. sol4 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, jacfn=bjac2_rows, lband=1, uband=1, linsolver='band') # Use the banded Jacobian. sol5 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, lband=2, uband=1, linsolver='band') # Use the banded Jacobian. sol6 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, lband=1, uband=1, linsolver='band') # Use the diag Jacobian. sol7 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='diag') #use lapack versions: # Use the full Jacobian. sol2a = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='lapackdense') # Use the banded Jacobian. sol3a = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, jacfn=bjac1_rows, lband=2, uband=1, linsolver='lapackband') # Use the banded Jacobian. sol4a = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, jacfn=bjac2_rows, lband=1, uband=1, linsolver='lapackband') # Use the banded Jacobian. sol5a = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, lband=2, uband=1, linsolver='lapackband') # Use the banded Jacobian. sol6a = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, lband=1, uband=1, linsolver='lapackband') #finish with some other solvers sol10 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='spgmr') sol11 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='spbcg') sol12 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='sptfqmr') assert_allclose(sol1.values.y, sol2.values.y, atol=1e-12, err_msg="sol1 != sol2") assert_allclose(sol1.values.y, sol3.values.y, atol=1e-12, err_msg="sol1 != sol3") assert_allclose(sol1.values.y, sol4.values.y, atol=1e-12, err_msg="sol1 != sol4") assert_allclose(sol1.values.y, sol5.values.y, atol=1e-12, err_msg="sol1 != sol5") assert_allclose(sol1.values.y, sol6.values.y, atol=1e-12, err_msg="sol1 != sol6") assert_allclose(sol1.values.y, sol7.values.y, atol=1e-12, err_msg="sol1 != sol7") assert_allclose(sol1.values.y, sol2a.values.y, atol=1e-12, err_msg="sol1 != sol2a") assert_allclose(sol1.values.y, sol3a.values.y, atol=1e-12, err_msg="sol1 != sol3a") assert_allclose(sol1.values.y, sol4a.values.y, atol=1e-12, err_msg="sol1 != sol4a") assert_allclose(sol1.values.y, sol5a.values.y, atol=1e-12, err_msg="sol1 != sol5a") assert_allclose(sol1.values.y, sol6a.values.y, atol=1e-12, err_msg="sol1 != sol6a") assert_allclose(sol1.values.y, sol10.values.y, atol=1e-12, err_msg="sol1 != sol10") assert_allclose(sol1.values.y, sol11.values.y, atol=1e-12, err_msg="sol1 != sol11") assert_allclose(sol1.values.y, sol12.values.y, atol=1e-12, err_msg="sol1 != sol12")
def integr(self,t0,T,h,E0init=False): #integrator function that star form t0 and finish with T with h as #timestep. If there aren't inital values in [t0,T] function doesn't #start. Or it's start if class object is initialze. if(not isinstance(self.S, np.ndarray)): #pass if object is initalized if(E0init): E_as0=self.mu*(self.I_as) E_sy0=self.mu*(self.I_mi+self.I_se+self.I_cr) else: E_as0=self.E_as E_sy0=self.E_sy S0=self.S I_as0=self.I_as I_mi0=self.I_mi I_se0=self.I_se I_cr0=self.I_cr H_in0=self.H_in H_cr0=self.H_cr H_out0=self.H_out V0=self.V D0=self.D B0=self.B R0=self.R I0=self.I CV0=self.CV CH0=self.CH ACV0=self.ACV ACH0=self.ACH print(self.H_out) self.t=np.arange(t0,T+h,h) elif((min(self.t)<=t0) & (t0<=max(self.t))): #Condition over exiting time in already initialized object #Search fot initial time idx=np.searchsorted(self.t,t0) #set initial condition S0=self.S[idx] E_as0=self.E_as E_sy0=self.E_sy I_as0=self.I_as[idx] I_mi0=self.I_mi[idx] I_se0=self.I_se[idx] I_cr0=self.I_cr[idx] H_in0=self.H_in[idx] H_cr0=self.H_cr[idx] H_out0=self.H_out[idx] V0=self.V[idx] D0=self.D[idx] B0=self.B[idx] R0=self.R[idx] I0=self.I[idx] CV0=self.CV[idx] CH0=self.CH[idx] ACV0=self.ACV[idx] ACH0=self.ACH[idx] #set time grid self.t=np.arange(self.t[idx],T+h,h) else: return() # dim=self.S.shape[0] def model_SEIR_graph(t,y,ydot): ydot[0]=self.dS(t,y[0],y[1],y[2],y[3],y[4],y[11],y[13]) ydot[1]=self.dE_as(t,y[0],y[1],y[2],y[3],y[4]) ydot[2]=self.dE_sy(t,y[0],y[1],y[2],y[3],y[4]) ydot[3]=self.dI_as(t,y[1],y[3]) ydot[4]=self.dI_mi(t,y[2],y[4]) ydot[5]=self.dI_se(t,y[2],y[5],y[7],y[8],y[9]) ydot[6]=self.dI_cr(t,y[2],y[6],y[7],y[8],y[9]) ydot[7]=self.dH_in(t,y[5],y[7],y[8],y[9]) ydot[8]=self.dH_cr(t,y[6],y[7],y[8],y[9],y[10]) ydot[9]=self.dH_out(t,y[7],y[9],y[10]) ydot[10]=self.dV(t,y[8],y[10]) ydot[11]=self.dD(t,y[5],y[6],y[7],y[8],y[9],y[10],y[11]) ydot[12]=self.dB(t,y[11]) ydot[13]=self.dR(t,y[3],y[4],y[9],y[13]) ydot[14]=self.dI(t,y[1],y[2]) ydot[15]=self.dCV(t,y[6],y[7],y[8],y[9],y[10],y[15]) ydot[16]=self.dCH(t,y[5],y[7],y[8],y[9],y[16]) ydot[17]=self.dACH(t,y[15]) ydot[18]=self.dACV(t,y[16]) initcond = np.array([S0,E_as0,E_sy0,I_as0,I_mi0,I_se0,I_cr0, H_in0,H_cr0,H_out0,V0,D0,B0,R0,I0,CV0,CH0,ACV0,ACH0]) # initcond = initcond.reshape(4*dim) sol = odeint(model_SEIR_graph, self.t, initcond,method='admo') self.t=sol.values.t # soln=np.transpose(np.array(soln[1][1])) # self.S=sol.values.y[:,0] self.E_as=sol.values.y[:,1] self.E_sy=sol.values.y[:,2] self.I_as=sol.values.y[:,3] self.I_mi=sol.values.y[:,4] self.I_se=sol.values.y[:,5] self.I_cr=sol.values.y[:,6] self.H_in=sol.values.y[:,7] self.H_cr=sol.values.y[:,8] self.H_out=sol.values.y[:,9] self.V=sol.values.y[:,10] self.D=sol.values.y[:,11] self.B=sol.values.y[:,12] self.R=sol.values.y[:,13] self.I=sol.values.y[:,14] self.CV=sol.values.y[:,15] self.CH=sol.values.y[:,16] self.ACV=sol.values.y[:,17] self.ACH=sol.values.y[:,18] return(sol)
import pkg_resources print(pkg_resources.get_distribution("scikits.odes").version) #od.test() t0, tT = 1, 500 # considered interval y0 = np.array([0.5, 0.5]) # initial condition def van_der_pol(t, y, ydot): """ we create rhs equations for the problem""" ydot[0] = y[1] ydot[1] = 1000 * (1.0 - y[0]**2) * y[1] - y[0] num = 200 t_n = np.linspace(t0, tT, num) solution = odeint(van_der_pol, t_n, y0) plt.plot(solution.values.t, solution.values.y[:, 0], label='Van der Pol oscillator') plt.show() print(solution.values.y) solution = od.ode('cvode', van_der_pol, old_api=False).solve(np.linspace(t0, 500, 200), y0) plt.plot(solution.values.t, solution.values.y[:, 0], label='Van der Pol oscillator') plt.show()
def test_odeint_banded_jacobian(): # Test the use of the `Dfun`, `ml` and `mu` options of odeint default bdf # solver def func(t, y, rhs): rhs[:] = c.dot(y) return 0 def jac(t, y, J): J[:,:] = c return 0 def bjac1_rows(t, y, J): J[:,:] = cband1 return jac def bjac2_rows(t, y, J): # define BAND_ELEM(A,i,j) ((A->cols)[j][(i)-(j)+(A->s_mu)]) J[:,:] = cband2 return jac y0 = np.ones(4) t = np.array([0, 5, 10, 100]) # Use the full Jacobian. sol1 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, jacfn=jac) # Use the full Jacobian. sol2 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='dense') # Use the banded Jacobian. sol3 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, jacfn=bjac1_rows, lband=2, uband=1, linsolver='band') # Use the banded Jacobian. sol4 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, jacfn=bjac2_rows, lband=1, uband=1, linsolver='band') # Use the banded Jacobian. sol5 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, lband=2, uband=1, linsolver='band') # Use the banded Jacobian. sol6 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, lband=1, uband=1, linsolver='band') # Use the diag Jacobian. sol7 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='diag') #use lapack versions: if TEST_LAPACK: # Use the full Jacobian. sol2a = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='lapackdense') # Use the banded Jacobian. sol3a = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, jacfn=bjac1_rows, lband=2, uband=1, linsolver='lapackband') # Use the banded Jacobian. sol4a = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, jacfn=bjac2_rows, lband=1, uband=1, linsolver='lapackband') # Use the banded Jacobian. sol5a = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, lband=2, uband=1, linsolver='lapackband') # Use the banded Jacobian. sol6a = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, lband=1, uband=1, linsolver='lapackband') #finish with some other solvers sol10 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='spgmr') sol11 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='spbcg') sol12 = odeint(func, t, y0, atol=1e-13, rtol=1e-11, max_steps=10000, linsolver='sptfqmr') assert_allclose(sol1.values.y, sol2.values.y, atol=1e-12, err_msg="sol1 != sol2") assert_allclose(sol1.values.y, sol3.values.y, atol=1e-12, err_msg="sol1 != sol3") assert_allclose(sol1.values.y, sol4.values.y, atol=1e-12, err_msg="sol1 != sol4") assert_allclose(sol1.values.y, sol5.values.y, atol=1e-12, err_msg="sol1 != sol5") assert_allclose(sol1.values.y, sol6.values.y, atol=1e-12, err_msg="sol1 != sol6") assert_allclose(sol1.values.y, sol7.values.y, atol=1e-12, err_msg="sol1 != sol7") assert_allclose(sol1.values.y, sol10.values.y, atol=1e-12, err_msg="sol1 != sol10") assert_allclose(sol1.values.y, sol11.values.y, atol=1e-12, err_msg="sol1 != sol11") assert_allclose(sol1.values.y, sol12.values.y, atol=1e-12, err_msg="sol1 != sol12") if TEST_LAPACK: assert_allclose(sol1.values.y, sol2a.values.y, atol=1e-12, err_msg="sol1 != sol2a") assert_allclose(sol1.values.y, sol3a.values.y, atol=1e-12, err_msg="sol1 != sol3a") assert_allclose(sol1.values.y, sol4a.values.y, atol=1e-12, err_msg="sol1 != sol4a") assert_allclose(sol1.values.y, sol5a.values.y, atol=1e-12, err_msg="sol1 != sol5a") assert_allclose(sol1.values.y, sol6a.values.y, atol=1e-12, err_msg="sol1 != sol6a")
def integr(self, t0, T, h, E0init=False): #integrator function that star form t0 and finish with T with h as #timestep. If there aren't inital values in [t0,T] function doesn't #start. Or it's start if class object is initialze. if scikitsimport: if (len(self.S.shape) == 1): #pass if object is initalized S0 = self.S if (E0init): E0 = self.mu * self.I else: E0 = self.E I0 = self.I R0 = self.R self.t = np.arange(t0, T + h, h) elif ((min(self.t) <= t0) & (t0 <= max(self.t))): #Condition over exiting time in already initialized object #Search fot initial time idx = np.searchsorted(self.t, t0) #set initial condition S0 = self.S[:, idx] E0 = self.E[:, idx] I0 = self.I[:, idx] R0 = self.R[:, idx] #set time grid self.t = np.arange(self.t[idx], T + h, h) else: return () dim = self.S.shape[0] def model_SEIR_graph(t, y, ydot): ydot[0:dim] = self.dSdt(t, y[0:dim], y[2 * dim:3 * dim]) ydot[dim:2 * dim] = self.dEdt(t, y[0:dim], y[2 * dim:3 * dim], y[dim:2 * dim]) ydot[2 * dim:3 * dim] = self.dIdt(t, y[dim:2 * dim], y[2 * dim:3 * dim]) ydot[3 * dim:4 * dim] = self.dRdt(t, y[2 * dim:3 * dim]) initcond = np.concatenate((S0, E0, I0, R0)) # initcond = initcond.reshape(4*dim) soln = odeint(model_SEIR_graph, self.t, initcond) self.t = soln[1][0] soln = np.transpose(np.array(soln[1][1])) self.S = soln[0:dim, :] self.E = soln[dim:2 * dim, :] self.I = soln[2 * dim:3 * dim, :] self.R = soln[3 * dim:4 * dim, :] else: print("Scikit couldn't be imported. Using RK4 instead") return self.integr_RK4(t0, T, h, E0init)
""" import numpy as np # The second order differential equation for the angle `theta` of a # pendulum acted on by gravity with friction can be written:: b = 0.25 c = 5.0 def pend(t, y, out): theta, omega = y out[:] = [omega, -b*omega - c*np.sin(theta)] y0 = [np.pi - 0.1, 0.0] t = np.linspace(0, 10, 101) from scikits.odes.odeint import odeint sol = odeint(pend, t, y0) import matplotlib.pyplot as plt plt.figure() plt.plot(sol.values.t, sol.values.y[:, 0], 'b', label='$\\theta(t)$') plt.plot(sol.values.t, sol.values.y[:, 1], 'g', label='$\\omega(t)$') plt.legend(loc='best') plt.xlabel('t') plt.grid() #plt.show() # now the same with a class method class pendulum(object): def __init__(self, b=0.25, c=5.0): self.b = b self.c = c
# The second order differential equation for the angle `theta` of a # pendulum acted on by gravity with friction can be written:: b = 0.25 c = 5.0 def pend(t, y, out): theta, omega = y out[:] = [omega, -b * omega - c * np.sin(theta)] y0 = [np.pi - 0.1, 0.0] t = np.linspace(0, 10, 101) from scikits.odes.odeint import odeint sol = odeint(pend, t, y0) import matplotlib.pyplot as plt plt.figure() plt.plot(sol.values.t, sol.values.y[:, 0], 'b', label='$\\theta(t)$') plt.plot(sol.values.t, sol.values.y[:, 1], 'g', label='$\\omega(t)$') plt.legend(loc='best') plt.xlabel('t') plt.grid() #plt.show() # now the same with a class method class pendulum(object): def __init__(self, b=0.25, c=5.0):