I have a solver that can exchange between scipy.integrate.odeand scipy.integrate.odeint. Here is the code.
def f(y,s,C,u,v):
y0 = y[0]
y1 = y[1]
y2 = y[2]
y3 = y[3]
dy = np.zeros_like(y)
dy[0] = y1
dy[2] = y3
C = C.subs({u:y0,v:y2})
dy[1] = -C[0,0][0]*dy[0]**2\
-2*C[0,0][1]*dy[0]*dy[2]\
-C[0,1][1]*dy[2]**2
dy[3] = -C[1,0][0]*dy[0]**2\
-2*C[1,0][1]*dy[0]*dy[2]\
-C[1,1][1]*dy[2]**2
return dy
def solve(C,u0,s0,s1,ds,solver=None):
from sympy.abc import u,v
if solver == None:
s = np.arange(s0,s1+ds,ds)
print 'Running solver ...'
return sc.odeint(f,u0,s,args=(C,u,v))
else:
r = sc.ode(f).set_integrator(solver)
r.set_f_params(C,u,v)
r.set_initial_value(u0)
y = []
print 'Running solver ...'
while r.successful() and r.t <= s1:
r.integrate(r.t + ds)
y.append(r.y)
return np.array(y)
The problem I am experiencing is the following. If I decide to use the solver from scipy.integrate.odeint, then the parameters fshould be specified in the order indicated in the code. However, if I decide to use solvers from scipy.integrate.ode, I need to change the order of the function parameters f(y,s,C,u,v)to f(s,y,C,u,v), otherwise I will get an error
TypeError: 'float' object has no attribute '__getitem__'
If I do this, it will scipy.integrate.odeintgenerate the same error for f, which is defined as f(s,y,C,u,v). How can I work with unified fregardless of the order of parameters?
Edit:
To summarize the problem up:
scipy.integrate.ode , f f(s,y,C,u,v), scipy.integrate.odeint , f f(y,s,C,u,v). , ?
:
Scipy - 0.16.0