/Fluid-Sim

An interactive particle-based fluid simulator based on smoothed particle hydrodynamics (SPH) made with C/C++ and OpenGL

Primary LanguageC++Apache License 2.0Apache-2.0


Logo

Fluid-Sim

An interactive particle-based fluid simulator made with C/C++ and OpenGL

Report Bug


Preview

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.

Details

Computational Fluid Dynamics (CFD)

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.

Force

Force

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

Force

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

Force

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.

Force

Therefore, the paper suggests a way to symmetrize the force which uses the arithmetic mean of the pressures of the interacting particles.

Force

Viscosity

Using the same principal to calculate the viscosity force yields more asymmetrical forces which have a suggested correction using velocity differences.

Force

Force

Smoothing Kernels

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

Force

Force

Force

Force

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.

Force

Force

Integration Scheme

The velocity and position are updated using the leapfrog integration scheme which is described by the following equations

Force

Installation

Linux

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

Windows

I'm currently working on releasing an executable so that you don't have to use cmake to build it on windows

Libraries Used

  • 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

License

Distributed under the Apache 2.0 License. See LICENSE for more information.

Contact

S.R. Qaiser - @pitu_dev - sc21srq@leeds.ac.uk

Project Link: https://github.com/ranaxdev/Fluid-Sim