Search code examples
pythonscipyodeint

'MemoryError' when calling scipy.odeint twice


I use scipy.odeint to solve a list of ode equations. In my code the solver will be called several times(for each calling a 40500*10001 array will be generated), at the first time of its calling the solver works just well, but then the MemoryError arises at the second calling. Can anyone help me with this problem?

fcn is a subroutine that returns a (0,40500) array, rho is a (0,40500) length array

for iraman in (0,30):
    ...  
    time = np.linspace(0,1.0e-11,10001)
    solve = odeint(fcn,rho,time,rtol= tol, atol = tol, mxstep=5, mxords=10)
    for istep in range(1, nsteps+1):
        t = time[istep]
        rho = solve[istep]
        ....  

Solution

  • solve is an array of 64 bit floating point values, so it takes 3 gigabytes of memory. In the second call, another 3 gigabytes is allocated by odeint for the solution. This will eventually be assigned to solve, and the previously allocated 3 gigabytes will be recovered by the python interpreter, but there is a time when both the old and new arrays exist, which requires 6 gigabytes.

    That's why I made the flippant comment: "Here's a nickel, kid. Get yourself some more memory."