n-body-sim is a simple program which simulates the movement of N bodies under the influence of their gravitational forces in two dimensional space. It uses simple O(n^2) algorithm executed on GPU, faster O(n*log n) will be implemented later.
The gravitational force used in the program is:
Usage:
n-body-sim -n nbodies [-o steps] [-i steps] [-d delta] [-s solver]
[--output-prefix prefix] [--no-update] position velocity
The flags mean following:
-n nbodies
Set the number of simulated bodies tonbodies
. This is automatically rounded to be a multiple of GPU working group size (see below).-o steps
Write an output file eachsteps
iteration. The output file is a text file containing coordinates of each body, one coordinate per line. X and Y coordinates are separated by space.-i steps
Calculate motion invariants eachsteps
iteration. Invariants include total energy and angular momentum.-d delta
A measure of time between two iterations. Smaller value will result in more accurate computations. Whendelta
is good enough, the invariants of movement will change only insignificantly.-s solver
A solver for the equation of movement. Three solvers are supported:euler
,rk2
andrk4
.rk2
andrk4
are 2nd- and 4th- order Runge-Kutta methods.euler
is the fastest among them andrk4
is the most accurate.--output-prefix prefix
The names of output files will begin with this prefix.--no-update
Do not update input files on exit (see below).
The last two mandatory arguments are paths to files containing initial
coordinates and velocities of particles in the system. These two files must
contain exactly nbodies
lines with X and Y components of a vector separated by
a space or many spaces. One set of these files is included in the examples
directory of this repository.
When the program receives SIGINT
or SIGTERM
signal (when you press ^C
or
kill
it), it overwrites position
and velocity
files with updated values
unless --no-update
option is specified.
Current restrictions: the number of bodies must be a multiple of maximal GPU
working group size (usually 256, see output of clinfo
). Wrong number of bodies
is rounded to the biggest possible number which is less than the number
specified. A good number to start the simulation with is around 10k. Also all
bodies currently have the same mass (this is because clover has a limitation on
amount of memory I can use in kernel arguments with my GPU).
Output files can be converted to png (or jpeg) pictures with gnuplot
and a
script like this:
#!/bin/sh
gnuplot << EOF
size = 45000
set terminal png size 1920,1080 enhanced font "Helvetica,20"
set xrange [-size: size]
set yrange [-size: size]
set output "$1.png"
plot "$1" with dots
EOF
You need these dependencies installed:
- OpenCL
- program-map (a small helper for loading OpenCL programs)
- Cmake for building
The building procedure is following:
mkdir build
cd build
cmake ..
make
make install