New in Wolfram
Mathematica
8: CUDA and OpenCL Support
◄
previous
|
next
►
Software Development
Simulate Thousands of Particles in Real Time
Simulates the
-body problem for 8000 bodies in real time.
In[1]:=
X
srcf = FileNameJoin[{$OpenCLLinkPath, "SupportFiles", "NBody.cl"}];
In[2]:=
X
NBody = OpenCLFunctionLoad[{srcf}, "nbody_sim", {{"Float4", _, "Input"}, {"Float4", _, "Input"}, _Integer, "Float", "Float", {"Local", "Float"}, {"Float4", _, "Output"}, {"Float4", _, "Output"}}, 256];
In[3]:=
X
numParticles = 8192; deltaT = 0.01; epsSqrt = 1.0; pos = OpenCLMemoryLoad[ RandomReal[Sqrt[numParticles], {numParticles, 4}], "Float4"]; vel = OpenCLMemoryLoad[RandomReal[5, {numParticles, 4}], "Float4"]; newPos = OpenCLMemoryAllocate["Float4", {numParticles, 4}]; newVel = OpenCLMemoryAllocate["Float4", {numParticles, 4}];
In[4]:=
X
colors = Map[ColorData["CherryTones"], Range[0, 1, 1/numParticles]] //. RGBColor -> List;
In[5]:=
X
Graphics3D[{AbsolutePointSize[2], Point[ Dynamic[Refresh[ NBody[pos, vel, numParticles, deltaT, epsSqrt, 256*4, newPos, newVel, numParticles]; NBody[newPos, newVel, numParticles, deltaT, epsSqrt, 256*4, pos, vel, numParticles]; Take[#, 3] & /@ OpenCLMemoryGet[pos], UpdateInterval -> 0]], VertexColors -> colors]}, Boxed -> False, RotationAction -> "Clip"]
Play Animation
»
Stop Animation
»