Skip to content

Ant13731/fluid-simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

49 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fluid Simulation from Scratch

Vortex shedding in a wind tunnel.

Vortex shedding in a wind tunnel (divergence view).

Features

  • Reasonably fast 2D fluid simulation on the GPU. See the python-numpy and unity-cpu branches for alternative implementations.
  • Interactive objects: fans, heaters, coolers, smoke machines
  • Fully configurable fluid interactions (ex. configurable gravity, diffusion, vorticity confinement strength, buoyancy, fan speed, enable/disable simulation effects, etc.)
  • View shaders: the simulation can be viewed through the lens of normal smoke, velocity divergence, or a grid of velocity-based arrows
  • Simple interface with scene presets (use ` to swap scenes)

Algorithm

This simulation follows the Navier-Stokes incompressible fluids equations:

$\frac{\partial{\vec{u}}}{\partial{t}} + \vec{u} \cdot \nabla\vec{u} + \frac{1}{\rho}\nabla p = \vec{f} + \nu\nabla \cdot \nabla\vec{u}\ \nabla \cdot \vec{u} = 0$

where:

  • $\vec{u}$ represents the fluid's velocity
  • $t$ for time
  • $\vec{f}$ is used for external forces acting on the fluid
  • $\rho$ is a constant used for density
  • $p$ is pressure
  • $\nu$ for kinematic viscosity

In other words, at each time step:

  • Send user input to compute shader
  • Update solid-cell pixel-maps (including brush-added fans, heaters, and smoke-generating vents)
  • Apply forces (buoyancy, interactive “solid” cells)
  • Vorticity confinement (promote tiny swirls)
  • Resolve pressure so the liquid is incompressible
  • Self-advect
  • Advect other properties (smoke, temperature, etc.)
  • Diffuse and decay
  • Update display shaders

We use a MAC grid to offset velocity components from actual cells of fluid:

Usage

Open/import the project into Unity. The default Unity Scene should include a SimulationCanvas object, but in case it does not load, create a new empty object and attach the FluidSimulationGPU script.

Controls

General Program Controls

Keypress Action
SPACE Pause/play
CTRL+SPACE or -> Move one frame forward and pause
ESC Clears everything
SHIFT+ESC Clears everything except solids
TAB Switch view mode
` Switch scene

Interactions

These buttons work alongside mouse movement.

Keypress Action
R Clears everything (brush)
SHIFT+R Clears everything except solids (brush)
Z Adds smoke (density)
X Adds velocity
C Adds solid
V Adds heat source
SHIFT+V Adds heat sink
B Adds density source (constant)
SHIFT+B Adds density sink (constant)
CTRL+letter Removes what the above letters added
W Add upwards velocity source (hollow fan)
A Add left velocity source (hollow fan)
S Add down velocity source (hollow fan)
D Add right velocity source (hollow fan)
SHIFT+letter Adds one layer of pixels directly opposite the fan
CTRL+letter Removes hollow fans in a given direction

Details

File Organization

  • FluidSimulationGPU.cs acts as the orchestrator for all other scripts and shaders
  • Simulation.compute is the main engine as a compute shader. All physics-related objects are placed in 2D grids (RWTexture2D objects) - kernels are GPU functions that act on these grids on a per-cell basis.
  • View.shader paints the canvas with one layer of pixels. Non-fluid objects and fluid objects are interpolated together so that smoke appears on top of the other objects, but there is really only one pixel plane. Future work could include separating these layers for a more straightforward shader architecture.
  • SimulationConfig.cs contains all important configuration options (which can be modified within the Unity editor)

Future Ideas

  • A 2D platformer game where the character needs to blow dust to see (invisible) platforms. The dust will use the air simulation, and we can add wind/fans to make things more difficult
  • 3D simulations
  • Add in moving objects
  • Find a better pressure projection algorithm (right now we use Jacobi, but Gauss-Seidel or Conjugate Gradient would converge faster)

References

Images

Scenes

Convection of hot and cool air.

Convection with one heater.

Car in a wind tunnel.

Truck in a wind tunnel.

Circular vortices in opposite directions.

Unity-CPU branch

Velocity vectors for each cell (debug view)

Smoke simulation from the unity-cpu branch.

Divergence view.

Python-Numpy branch

Smoke simulation from the python-numpy branch.

About

A GPU-based fluid simulation from scratch

Resources

Stars

Watchers

Forks