The n-bodies problem
There’s been some activity in the #haskell IRC channel recently towards coming up with a better solution to the n-bodies problem. In this problem, you’re given a set of bodies, and are asked to simulate Newtonian gravity on them. Most of the solutions I’ve seen go past have involved some sort of crazy state monad based stuff, reading from and writing to STUArrays and other imperative ideas. So, I thought I’d try and solve the problem using a pure functional style instead.
I’ve recently been playing with Reactive a lot, initially for my work, but also just because it’s so damned cool. Reactive is a library for Haskell that lets you describe time varying values in a purely functional way. That is, out goes the concept of state, and in comes the concept of describing exactly what is going on.
Back to n-bodies
In order to simulate newtonian physics, we need the bodies’ mass,the positions of the bodies, and the velocities of the bodies:
That was pretty straight forward, lets get down to the core of the problem – simulating gravity:
Well, that was surprisingly easy! The
accelBetween function describes the newtonian gravity equation just as the maths does, and summing up the acceleration due to all the other planets in the system is fairly straightforward.
What we’ve not seen yet though, is any of that lovely functional reactive programming I was talking about. We know now how to compute the acceleration affecting any one body at any one time, but what we want to know is the acceleration affecting a body at *any* time. To do this, we’re going to need to move from doing computations on
Bodys, to instead doing computation on
withOthers function here is just like map, but it passes in all the other values in the list in a second argument to the function.
bodyAccels computes a continuous acceleration function for all the bodies in the system. For each body, it runs the
computeAccels function, giving it all other bodies in the system as its second argument. Crucially,
liftA2 allows us to do this in the
Behavior Applicative, so we are no longer computing it on rigid, static bodies, but instead, on all the positions and velocities the bodies in the system will ever have (isn’t lazyness great!).
Finally, we can get from these accelerations down to the velocities, and then positions of the bodies using integration on the acceleration:
I’m sure you can imagine what the bodyPos function looks like. You may wonder what the
dt in here is talking about. This is an unfortunate effect of not being able to mathematically integrate arbitrary functions. Instead, we must use euler integration, and that requires us to provide times at which to take samples. The
dt argument is an event which ticks reasonably fast, and progresses our simulation:
So, now we are able to combine all our efforts together, and solve the whole n-bodies problem:
A lot of assumptions are made about how we must write programs. Often, even beautiful mathematical problems end up described as horrible state-full masses of code that obscure what it is we’re trying to compute. I’ve presented a solution to the n-bodies problem using the Reactive library to get a handle on time in a purely functional setting, it turned out that this was rather beautiful!
Full code for my solution can be found below.