Search code examples
pythonscipydifferential-equationsodeint

Inhomogeneous shape of an array when using odeint


I am trying to qolve system of equations describing some polymerization process using odeint. To do so, I need to use coefficients, that depends on one of variables i am solving for. When I try to run the code:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
T = 25+298.15
p = 1
omega0= 0.3
def kp(x):
    return (3.3*10**5+3.8*10**6*np.exp((-5.3*omega0*(1-x))/(1-omega0*x)))*np.exp((-1.88*10**3+(0.096+0.11*omega0*(1-x)*p/(1-omega0*x)))/T)
k_sd = 9.3*10**6
c_nu = 13
k_td = 10**9
Crd = 100
def nu(x):
    return np.exp(c_nu*x)
def kt(x):
    return 1/(k_sd**(-1)+nu(x)/k_td)+Crd*(1-x)*kp(x)
f = 0.64
kd = 1.58*10**(-4)
parameters = (f, kd)
IC = [0.05, 3.5, 0, 0, 0, 0]
def omega(c):
    return c*86/1000
def X(a):
    return (omega(IC[1])-omega(a))/(omega(IC[1])-omega(c)*omega(IC[1]))
t = np.linspace(0,500,10001)
def model_basic(y, t, f, kd):
    I, M, Y0, Y1, Q0, Q2 = y
    dydt = [-kd*I, 
            -kp(X(M))*M*Y0,
            2*f*kd*I-2*kt(X(M))*Y0*Y0,
            kp(X(M))*M*Y0-2*kt(X(M))*Y0*Y1,
            2*f*kd*I-kt(X(M))*Y0*Y0/2,
            kp(X(M))*M*Y0+2*kp(X(M))*M*Y1+kt(X(M))*Y1*Y1]
    return dydt
sol_2 = odeint(model_basic, IC, t, parameters, hmax = 0.1)

I recieve the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-45-bec4df54cef7> in <module>
      9             kp(X(M))*M*Y0+2*kp(X(M))*M*Y1+kt(X(M))*Y1*Y1]
     10     return dydt
---> 11 sol_2 = odeint(model_basic, IC, t, parameters, hmax = 0.1)

C:\eclipse\miniConda\lib\site-packages\scipy\integrate\odepack.py in odeint(func, y0, t, args, Dfun, col_deriv, full_output, ml, mu, rtol, atol, tcrit, h0, hmax, hmin, ixpr, mxstep, mxhnil, mxordn, mxords, printmessg, tfirst)
    239     t = copy(t)
    240     y0 = copy(y0)
--> 241     output = _odepack.odeint(func, y0, t, args, Dfun, col_deriv, ml, mu,
    242                              full_output, rtol, atol, tcrit, h0, hmax, hmin,
    243                              ixpr, mxstep, mxhnil, mxordn, mxords,

ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (6,) + inhomogeneous part.

Other topics on similar problem were always related to incorrect initial value lists. I cheked my code and didn't found any inconsistencies between initial values and function model_basic that i pass to odeint. Can please anyone help me with it?


Solution

  • Copying your code into a clean work space, I get as first error that in def X(a) the variable c is not defined. If you have that lying around from somewhere else in your session, and that previous c is a list, then you get the error you reported.