N-Body Simulation in Python and Cython

Distractions from GSoC

12 Jun 2016 » back to top

I’ve been a little burnt out for the past few days, when it comes to my work for Google Summer of Code; disklabel support is progressing, but it’s progressing slowly. The latest U-Boot head doesn’t seem to want to boot on my beaglebone, so I’ve had to revert to an earlier version. However, with any luck I’ll have a stable release of U-boot in pkgsrc-wip over the next couple of days.

I decided to take a break from that today, and, as a physics student, I decided that this was the perfect time to play around with scipy. This was the end result:

It’s a really simple n-body gravity simulator written in Python and Cython and using scipy’s default odeint integrator. The source can be found on github at joedavis/orbital.

My first few attempts at this were written in pure python, and struggled with even 10 particles. But after rewriting the function ode_system in cython however I was able to simulate several hundred particles in near real time on my MacBook. The above video contains 50 particles and was made using matplotlib.