0

I am designing the classical Runge-Kutta scheme (RK4) for a large number of coupled equations in Python 2.7. Since there is going to be over a hundred coupled 1st order equations the for-loops will be hell large and I am looking for some optimization hints.

1. When calculating a vector of returned variables for RK coefficients is it better to...

  • Prealocate a numpy array and fill it or
  • Use list.append for each variable and numpy.array(list) at the end?

2. The coupled equations obviously have coefficients. Is it better to...

  • Insert them into the function called in RK4 steps (i.e. redefine them each time the function evaluation is called) or
  • Label them as global variables?
Victor Pira
  • 1,132
  • 1
  • 10
  • 28
  • 1
    The best optimization would be to use `scipy.integrate.odeint` instead of implementing the method yourself... I don't think 2 will make a difference for the running time. For 1, the best option is better but you really should try to form the right-hand side vector by array-wise numpy operations, instead of looping through the array. –  Dec 13 '16 at 15:16
  • @zaq: I don't want to use the odeint cause I have nonlinear system highly sensitive to input parameters so I don't want to use any "blackboxes". I am sorry but I don't follow your third sentence ("For 1..."). Should it be "the first"? – Victor Pira Dec 14 '16 at 10:19
  • 1
    `lsoda` which is behind `odeint` is a well tested and well documented "black box". Set the relative (and absolute) error margins, since the default values are rather high. Make several runs with different error settings to document how stable the result is. – Lutz Lehmann Dec 14 '16 at 11:32
  • Compare also with https://stackoverflow.com/questions/29617089/implement-pseudo-spectral-method-with-rk4-in-python and https://stackoverflow.com/questions/29803342/error-in-rk4-algorithm-in-python/29804905 for the use of pseudo-spectral methods to treat the linear terms separately and thus get better numerical integration in the non-linear part. – Lutz Lehmann Dec 14 '16 at 11:41

0 Answers0