# solving the gravitational equations properly

09/08/12 18:22

Using Sverre Aarseth’s Nbody0 code

So having figured that we have to be more sophisticated than using simple Runge-Kutta techniques, we had to go back to look at people who do this problem properly. In the past I worked with Sverre Aarseth who is very much the grand master of this game. Much to my delight when I searched for him on the web he is still going strong (in Maths and Astronomy Cambridge there seems to be some kind of rule that people don’t retire).

One of Sverre’s key characteristics was that he could put the most fiendishly complicated into clean computer code. [For example he redefines the coordinate system for tight binaries using renormalisation group techniques so the tight binaries are calculated in effect analytically with the other bodies providing a small “far-field” perturbation. Also normal bodies will use the nearest 20 or so neighbours to update the “near-field forces” often and the remaining bodies contribute to a far-field that is updated less often. [Some of these habits I try to emulate for the big optimisation problems but Sverre isn’t someone you actually compete with…]. All that is quite wonderful of course but it does mean that most of the code he (very generously) publishes is very complex and a bit over-kill for what is intended to be an app about moving images.

Fortunately one of his codes (the most simple, nBody0.f) is both short and easy to read. It also does all we need in that it has adaptive time steps for each body and is pretty accurate as it uses analytic differentiation of the force equations to obtain high order accuracy. In Newton’s Playground we add repulsive forces between the bodies to prevent them getting too close and so that they effectively bounce off each other. Hence I needed to go back in time to remember how to do differentiation. Unfortunately nBody0.f is in FORTRAN and it is a long time since I have written in FORTRAN. However as it is short and clean it wasn’t too difficult to convert to C and replace the old Runge-Kutta with something more finely tuned to the gravitational nBody problem.

Using Sverre’s algorithms, everything becomes much more stable and we can grow the system so that it can comfortably deal with up to 50-bodies with real-time animation.

DW

One of Sverre’s key characteristics was that he could put the most fiendishly complicated into clean computer code. [For example he redefines the coordinate system for tight binaries using renormalisation group techniques so the tight binaries are calculated in effect analytically with the other bodies providing a small “far-field” perturbation. Also normal bodies will use the nearest 20 or so neighbours to update the “near-field forces” often and the remaining bodies contribute to a far-field that is updated less often. [Some of these habits I try to emulate for the big optimisation problems but Sverre isn’t someone you actually compete with…]. All that is quite wonderful of course but it does mean that most of the code he (very generously) publishes is very complex and a bit over-kill for what is intended to be an app about moving images.

Fortunately one of his codes (the most simple, nBody0.f) is both short and easy to read. It also does all we need in that it has adaptive time steps for each body and is pretty accurate as it uses analytic differentiation of the force equations to obtain high order accuracy. In Newton’s Playground we add repulsive forces between the bodies to prevent them getting too close and so that they effectively bounce off each other. Hence I needed to go back in time to remember how to do differentiation. Unfortunately nBody0.f is in FORTRAN and it is a long time since I have written in FORTRAN. However as it is short and clean it wasn’t too difficult to convert to C and replace the old Runge-Kutta with something more finely tuned to the gravitational nBody problem.

*[Note that we do the heavy calculations in C rather than objective-C, as it cuts out a lot of messaging overhead and is perhaps 4-5 times faster. The fact that you can mix C with objective-C is one of the really nice aspects of that language].*

Using Sverre’s algorithms, everything becomes much more stable and we can grow the system so that it can comfortably deal with up to 50-bodies with real-time animation.

DW