Skip to content

N-body simulation (movement under the influence of gravitational forces).

License

Notifications You must be signed in to change notification settings

shamazmazum/n-body-sim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

N-Body-Sim

Build Status

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: $\vec{F} = G \frac{m_1 m_2}{({\left|\vec{r} \right|}^2 + \epsilon )^{3/2}} \vec{r}$ where ε=1 and G=100.

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 to nbodies. This is automatically rounded to be a multiple of GPU working group size (see below).
  • -o steps Write an output file each steps 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 each steps 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. When delta 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 and rk4. rk2 and rk4 are 2nd- and 4th- order Runge-Kutta methods. euler is the fastest among them and rk4 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

Installation

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

About

N-body simulation (movement under the influence of gravitational forces).

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published