An interactive particle-based fluid simulator made with C/C++ and OpenGL
Report Bug
fluid_demo.mp4
This is an interactive 3D particle-based fluid simulator based on smoothed particle hydrodynamics, featuring fluid interaction with a tank container and adjustable parameters such as mass, density, gravity, viscosity and different smoothing kernels. Based on the paper by Matthias Müller et al. at the department of Computer Science, Federal Institute of Technology Zürich (ETHZ), Switzerland, published in 2003 at the Eurographics/SIGGRAPH Symposium on Computer Animation.
The solution is a work-around to the computational fluid dynamics problem of performance as producing approximations to the Navier-Stokes equations is computationally expensive and the visualization aspect is generally done as a second step, but this implementation provides live computation of the fluid behavior by approximating the shallow water equations in discretised grid-space (Euler).
The basic principal is to distribute field quantities in a local neighborhood of each particle using radial symmetrical smoothing kernels, which respect the Navier-Stokes principals, conservation of mass and momentum. The below is a simplified version of the equations for incompressible fluids.
Where ρ
is density, v
is a velocity field, g
is an external force density field and μ
is viscosity.
Because the derivative of the velocity field is essentially the derivative of the particle velocity, we don't need to approximate the conservation of mass anymore and can focus on the second equation, which has the components pressure, external force and viscosity. According to SPH, a scalar quantity A
is interpolated at location r
by a weighted sum of contributions from all particles as shown below
Here, j
iterates over all particles, m_j
is the mass of particle j
, r_j
its position, ρ_j
the density and A_j
the field quantity at r_j
. The function W
is a smoothing kernel with radius h
. Using this, we can approximate the different required forces such as internal pressure
Where p
, the pressure force is computed using p=k(ρ-ρ_0)
where k
is the gas constant and ρ_0
is the rest density. However, this force is not symmetric and the fluid will behave irregularly.
Therefore, the paper suggests a way to symmetrize the force which uses the arithmetic mean of the pressures of the interacting particles.
Using the same principal to calculate the viscosity force yields more asymmetrical forces which have a suggested correction using velocity differences.
This implementation experimented with the Poly6
, Spiky
and Viscosity
kernels which have different effects on the behavior of the fluid in terms of stability, accuracy and speed.
The W_poly6
and W_spiky
can be interchanged for internal pressure and are shown below
And for the viscosity force kernel W_viscosity
, we need the second laplacian which requires a kernel that doesn't resolve negative forces that increase relative velocities.
The velocity and position are updated using the leapfrog integration scheme which is described by the following equations
Dependencies include OpenGL 4.50
, libdl
, glfw3
, pthread
, glm
git clone https://github.com/ranaxdev/Fluid-Sim/
cd Fluid-Sim
mkdir Release
cd Release
cmake -DCMAKE_BUILD_TYPE=Release ../
make
I'm currently working on releasing an executable so that you don't have to use cmake
to build it on windows
-
GLAD : OpenGL Loading Library
-
GLM : OpenGL Mathematics
-
GLFW : A multi-platform library for OpenGL, OpenGL ES, Vulkan, window and input
-
Dear ImGui : Bloat-free Graphical User interface for C++ with minimal dependencies
Distributed under the Apache 2.0 License. See LICENSE
for more information.
S.R. Qaiser - @pitu_dev - sc21srq@leeds.ac.uk
Project Link: https://github.com/ranaxdev/Fluid-Sim