When you do some juggeling with numbers on time-dependent problems it is always nice to have a time-dependent means of displaying the results. And animations or videos are the right thing!
As a little example we will examine how to animate the gravitational multi-body problem of some objects. We will use Python, NumPy, Cairo and mencoder to get this "fancy" video:
We start by splitting the problem into four sub-problems:
The mathematics of non-relativistic gravity is rather simple. For each point in time that is investigated, we have to determine the forces exerted onto each planet by all the other ones. Using the simple equations of motion we can update each body's acceleration, speed and position.
To do so we use the NumPy module in order to do the math in two dimensions comfortably with vectors/arrays.
Instead of using complete visualizations modules like matplotlib or mayavi we can "plot" our own data using Cairo. In order to get snapshots that will be concatenated to a video sequence Cairo is rather more convenient: We do not have to worry about rescalings of the axes, labels or how to fade away the old image such that each planet gets a trace. But we do have to worry about poorly chosen parameters resulting in the planet leaving the ImageSurface.
At first we start off with a partially transparent black rectangle that will produce some nice fading trace. Then we draw each planet in front of this rectangle and save the image with a serial number.
In the control loop we simply run through a desired number of points in time. And at each of those points we make sure that the gravitational forces exerted on each planet are regarded before drawing an arc at the new position.
Converting a set of images into a video is rather trivial when ones knows the correct parameters to the video converter of ones choice.