Smooth Particle Hydrodynamics Simulation
by Charlie Gels
Description
An N-body gravitational simulation that uses smooth particle hydrodynamics(SPH) to compute gravitational interaction forces in a particle system containing 2000 to 65,536 particles. SPH is a technique commonly used to simulate fluids by using many, many particles in close proximity to each other in order to compute the density and interaction forces between the particles in the system.
Features
- 3D Spatial Hash Data Structure
- FPS Camera to move around world
- Particle Billboarding
- Phong lighting
- SPH Solver using Leapfrog Integration Scheme (Eulerian Symplectic Integrator)
- Simulated in water-like fluid (Similar to properties of interstellar medium)
- 3D Mesh version rendering in real time with Phong shading.
- Particle Billboarding version to display computation speed. Billboards are textures used for alphamapping.
- Instancing (not demoed because it slowed performance too much)
Videos
65,536 Particle - Spatial Hash and Billboarding (~65ms per render)
- Due to large number of particles, it is hard to tell that particles are moving inward -- this is due to the interaction radius of these particles and the pipeline culling out many billboards that are overlapping.
- Spatial hash using 8000 cubes to be used for local force calculations.
8192 Particle - Spatial Hash and Billboarding (under 10ms per render)
2048 Particle - Spatial Hash and Low-Res Sphere Mesh Particles (~13ms per render)
- Due to the smaller number of particles, the resolution of the spatial has is reduced to 1000 cubes so that particles will not be isolated.
1024 Particle - No Spatial Hash (Full-Interaction) (~13ms per render)
- Notice that this system tears itself apart quickly, this is partially due to the increased number of interactions and also partially caused by imbalanced forces between gravity and viscous drag.
Lessons Learned
- Learn C++ because by the end of the project I really wanted easier Object Orientation than C can offer. This noticeably slowed down my ability to navigate my code and make small changes to tune the system parameters, etc.
- Use git!! Being able to have multiple branches holding different optimization ideas saved me lots of time. It also allowed me make changes without fear of losing my working implementation because I mangled my code beyond recovery.
- Due to the SPH algorithms have O(n^2), I wanted to try using OpenCL to accelerate my computations by moving this computations onto . I spent 2 days trying to get my OpenCL to cooperate with my hardware but I had to give up this effort for my sanity. This was really unfortunate because OpenCL was the ticket to my target number of 1Million particles :( But I learned a great deal about parrallel computing and will be trying to get OpenCL to cooperate with me in my free time.
- I also tried to implement a KD tree (similar to octree) for my spatial data structure. I was unable to get the open source KD-tree libraries to build with my existing codebase. Even though I was not able to implement the tree in time, exploring spatial data structures and their range of utility is my biggest lesson from this project.
- At one point I implemented Transform Feeback in order to export computations from my CPU into the geometry shader on the GPU, but I did not manage to implement a solution that made any noticeble difference. This solution is also eclipsed by the far superior alternative, OpenCL or CUDA.
Future Implementation Goals
Sources