Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast nbody algorithms / solutions (with opengl/c++/??)

I'm working on a (c++, opengl) project where I need to have lots of particles which influence eachother, if I'm correct this is called a nbody problem. Does someone knows what solutions there are for algorithms like this.

I know the barnes hut algorithm and maybe I can peek around openCL, though I'm not just wondering if you maybe used other solutions.

The code which I'll create will have lots of:

for(int i = 0; i < num_particles; ++i) {
  for(int j = i+1, j < num_particles; ++j)
     dist = distance(particles[i],particles[j]);
     if(dist > limit) {....}
  }
}

Kind regards, Pollux

like image 348
pollux Avatar asked Jul 28 '10 19:07

pollux


3 Answers

Kd-trees are ideal for finding all objects (particles in this case) at a maximum distance. If the tree is balanced look ups are O(log n).

like image 180
Staffan Avatar answered Oct 15 '22 04:10

Staffan


This is where data structures like Octrees come in handy. They can reduce your O(N^2) loops to O(N*log(N)), at the expense of losing a tiny bit of accuracy.

like image 38
Justin Ardini Avatar answered Oct 15 '22 06:10

Justin Ardini


If you want to have a HUGE computation power on lot of quite simple bodies - get interested in nvidia CUDA and doing your work on GPU shader units. This can give you more performance even comparing to quad-core CPUs with multithreading

like image 34
Piotr Müller Avatar answered Oct 15 '22 06:10

Piotr Müller