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

Ryan Krauss ryanlists@gmail....
Fri Aug 3 12:02:42 CDT 2012


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.


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: runge_kuta.pyx
Type: application/octet-stream
Size: 2939 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/scipy-user/attachments/20120803/eeaf8911/attachment.obj 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: runge_kuta_f.f
Type: application/octet-stream
Size: 2057 bytes
Desc: not available
Url : http://mail.scipy.org/pipermail/scipy-user/attachments/20120803/eeaf8911/attachment-0001.obj 


More information about the SciPy-User mailing list