Skip to content

small project for a n-body simulation for cuda & single threaded

License

Notifications You must be signed in to change notification settings

hwulfmeyer/N-Body-Simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

N-Body Simulation

For parallel computing on CPU & CUDA.

Simulation Setup

The classical N-body problem simulates the evolution of a system of N bodies, where the force exerted on each body arises due to its interaction with all the other bodies in the system. It is using newtons gravitational laws.

Infomaterial:

The Algorithm

The naive implementation computates each force for each object from all objects. It's called all-pairs-agorithm or brute force method and has a complexity of O(N^2), however it gives the most correct results.

See https://en.wikipedia.org/wiki/N-body_problem#General_formulation

Pseudocode calculating netforce for each body:

G = gravitational constant;
for each Body i {    
  for each Body k where k!=i {
    vec3 direction = k.position - i.position;
    double dist = norm(direction);  // L^2-Norm
    i.force += (G * k.mass * i.mass / dist^3) * direction;
  }
}

Updating position & velocity for each body:

dt = timespan;
for each Body i {    
  i.velocity += i.force / i.mass;
  i.position += dt * i.velocity;
}

With softening factor & simplification:

G = gravitational constant;
epsilon = softening factor;     // epsilon^2 > 0
for each Body i {
  i.velocity = (0,0,0);
  for each Body k {
    vec3 direction = k.position - i.position;
    double dist = norm2(direction);  // dot product (l2 norm squared)
    i.velocity += k.mass*direction  / (dist + epsilon^2)^(3/2);
  }
  i.velocity = G * i.velocity;
}
dt = timespan;
for each Body i {
  i.position += dt * i.velocity;
}

Implementation

Drawing is done with OpenGL.

Libraries (https://www.opengl.org/wiki/Related_toolkits_and_APIs)

UI: http://anttweakbar.sourceforge.net/doc/

Project Timeline

  1. Program on single threaded CPU [DONE]
  2. porting to multithreaded CPU [DONE]
  3. naive algorithm on CUDA [DONE]
  4. utilize OpenGL & Cuda inoperability [DONE]
  5. CUDA performance optimizations [DONE]
  6. create better & 3D particles/world [TODO]
  7. integrate AntTweakBar [TODO]

Algorithm Optimization/Approximation methods

https://en.wikipedia.org/wiki/Barnes%E2%80%93Hut_simulation

About

small project for a n-body simulation for cuda & single threaded

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages