[SciPy-User] help speeding up a Runge-Kuta algorithm (cython, f2py, ...)

Jim Vickroy jim.vickroy at noaa.gov
Fri Aug 3 13:15:13 EDT 2012


On 8/3/2012 11:02 AM, Ryan Krauss wrote:
> I need help speeding up some code I wrote to perform a Runge-Kuta
> integration.  I need to do the integration as part of a real-time
> control algorithm, so it needs to be fairly fast.
> scipy.integrate.odeint does too much error checking to be fast enough.
>   My pure Python version was just a little too slow, so I tried coding
> it up in Cython.  I have only used Cython once before, so I don't know
> if I did it correctly (the .pyx file is attached).
>
> The code runs just fine, but there is almost no speed up.  I think the
> core issue is that my dxdt_runge_kuta function gets called about 4000
> times per second, so most of my overhead is in the function calls (I
> think).  I am running my real-time control algorithm at 500 Hz and I
> need at least 2 Runge-Kuta integration steps per real-time steps for
> numeric stability.  And the Runge-Kuta algorithm needs to evaluate the
> derivative 4 times per times step.  So, 500 Hz * 2 * 4 = 4000 calls
> per second.
>
> I also tried coding this up in fortran and using f2py, but I am
> getting a type mismatch error I don't understand.  I have a function
> that declares its return values as double precision:
>
> double precision function dzdt(x,voltage)
>
> and I declare the variable I want to store the returned value in to
> also be double precision:
>
> double precision F,z,vel,accel,zdot1,zdot2,zdot3,zdot4
>
> zdot1 = dzdt(x_prev,volts)
>
> but some how it is not happy.

I'm not much of a Fortran programmer and I may misunderstand the above, 
but have you tried adding dzdt to your double precision declaration?



>
>
> My C skills are pretty weak (the longer I use Python, the more C I
> forget, and I didn't know that much to start with).  I started looking
> into Boost as well as using f2py on C code, but I got stuck.
>
>
> Can anyone either make my Cython or Fortran approaches work or point
> me in a different direction?
>
> Thanks,
>
> Ryan
>
>
> _______________________________________________
> SciPy-User mailing list
> SciPy-User at scipy.org
> http://mail.scipy.org/mailman/listinfo/scipy-user

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20120803/96ce5bff/attachment.html>


More information about the SciPy-User mailing list