3D Gravity Simulations and OpenCL

It has been a few years since I last was experimenting with OpenCL for gravity simulations.

At that time I made some mistakes with my code that resulted in not all of the objects being compared to all of the objects in the gravity calculations. For the purists this is not acceptable “How can you call it a gravity simulation if you are not using all objects in the calculations?!” The other opinion raised at the time was if it looks good enough, use the speed ups. None of the comments to my YouTube gravity videos were (in typical YouTube speak) “haha lol! looks fake! u no do gud gravity!”. So, if you are struggling with getting gravity working fast, try reducing the number of actual particle interactions.

Here is the latest updated CL kernel code….


__kernel void Gravity3DKernel(__global float4* pos, __global float4* vel, __global float4* acc, __global float4* mass, float mingravdist, float forcescalefactor, int startindex, int stopindex, int gridsize)
{
	int index=get_global_id(0)+startindex;
	float dx,dy,dz,distance,force;
	float positionx=pos[index].x;
	float positiony=pos[index].y;
	float positionz=pos[index].z;
	acc[index].x=0;
	acc[index].y=0;
	acc[index].z=0;
	for(int a=0; a<get_local_size(0); a++) {
		if (a!=index) {
				
			dx=pos[a].x-positionx;
			dy=pos[a].y-positiony;
			dz=pos[a].z-positionz;
				
			distance=sqrt(dx*dx+dy*dy+dz*dz);
			
			dx=dx/distance;
			dy=dy/distance;
			dz=dz/distance;
				
			force=1/(distance*distance+mingravdist*mingravdist)*forcescalefactor;
				
			acc[index].x+=dx*force;
			acc[index].y+=dy*force;
			acc[index].z+=dz*force;
		}
	}
	vel[index].x+=acc[index].x;
	vel[index].y+=acc[index].y;
	vel[index].z+=acc[index].z;
}


The kernel processes all of the 3d objects once using 1 of the other objects (passed as startindex).

You can call the kernel multiple times each frame/step of the simulation. If you want strictly accurate (and slow) gravity simulation results you call it inside a loop that sets startindex from 1 to the number of objects. In my experiments this is not necessary. If you want “good enough” nice looking gravity simulations then calling the kernel with as little as 10 different startindexes (use a set of objects spread out among all of them or just use a random set of 10 objects each frame).

Here is a recent 4K resolution example movie. This one used 1,000 objects out of 5,000,000 objects for the gravity calculations. The objects start by being randomly placed within an oblate spheroid. Their velocities are initialized so they are rotating around the center axis.

Jason.

3D Gravity using OpenCL

This post did have a bunch of benchmarks and times that were incorrect and have been removed. I was not comparing every particle to every other particle in the gravity calculations. That was the cause of the impossibly fast speeds I had listed.

See here for the latest kernel code and explanations.

What is left of this post is still correct. The object counts are correct and there are millions of particles being shown. It is just the actual gravity calculations that are not complete.

Previous Results

Since I originally posted the following 3D gravity movies to YouTube…

…there were questions and some skepticism in the comments so hopefully this blog post helps clarify things. That skepticism about how I could possibly calculate these millions of objects at once was well founded.

Latest Results

Here is a movie with 3,000,000 particles.

Unfortunately YouTube’s compression ruined the movie quality a bit. Mostly due to the noisy/static like nature of the millions of particles. Not enough I frames and too many P frames. Here are a series of screenshots showing what the frames looked like before the compression.

3D Gravity Simulation

3D Gravity Simulation

3D Gravity Simulation

3D Gravity Simulation

3D Gravity Simulation

3D Gravity Simulation

3D Gravity Simulation

To push it further, here is a rotating disk of 5,000,000 objects.

Again, here are some uncompressed screenshots.

3D Gravity Simulation

3D Gravity Simulation

3D Gravity Simulation

3D Gravity Simulation

3D Gravity Simulation

3D Gravity Simulation

3D Gravity Simulation

3D Gravity Simulation

3D Gravity Simulation

Try It Yourself

If use Windows you can download Visions of Chaos and see the simulations run yourself. Here are a few quick steps to get you going;

1. Open Visions of Chaos
2. Select Mode->Gravity->3D Gravity to change into the 3D Gravity mode
3. The 3D Gravity Settings dialog will appear
4. Change the number of objects to 1,000,000
5. Change the Time step to 0.02
6. Change the Size of objects to 0.2
7. Check Create AVI frames if you want to create an AVI movie from the simulation
8. Click OK

Make sure you have the latest video card drivers so the OpenCL code runs as optimal as possible. For NVidia go here and for AMD go here.

Jason.