CS 231 Fluid simulation
Why Simulate Fluids? Feature film special effects Computer games Medicine (e.g. blood flow in heart) Because it s fun
Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering Graphics favors finite difference Introduced fast and stable methods to graphics [Stam 1999]
Vector Fields (Fluid Velocity) u ij = (u x,u y )
Navier-Stokes Equations u=0 Incompressibility u t = k 2 u (u )u p + f Diffusion Change in Velocity Advection Pressure Body Forces
Navier-Stokes Equations u=0 Incompressibility u t = k 2 u (u )u p + f Diffusion Change in Velocity Advection Pressure Body Forces
Fluid Grids All values live on regular grids Need scalar and vector fields Scalar fields: amount of smoke or dye Vector fields: fluid velocity Subtract adjacent quantities to approximate derivatives
Scalar Field (Smoke, Dye) 1.2 3.7 5.1 c ij
Diffusion c ij
Diffusion c t = k 2 c 1 value relative to neighbors 1-4 1 1 c ij new = c ij + k Dt (c i-1j + c i+1j + c ij-1 + c ij+1-4c ij )
Diffusion = Blurring Original Some Diffusion More Diffusion
Vector Field Diffusion u t = k 2 u Two separate diffusions: viscosity u x t = k 2 u x u y t = k 2 u y blur the x-velocity and the y-velocity
Effect of Viscosity Low Medium High Very High Each one is ten times higher viscosity than the last
Variable Viscosity Viscosity can vary based on position Viscosity field k can change with temperature
Navier-Stokes Equations u=0 Incompressibility u t = k 2 u (u )u p + f Diffusion Change in Velocity Advection Pressure Body Forces
Advection = Pushing Stuff
Scalar Field Advection c t = (u )c change in value advection current values
Vector Field Advection u t = (u )u Two separate advections: u x t = (u )u x u y t = (u )u y push around x-velocity and y-velocity
Advection Easy to code Method stable even at large time steps Problem: numerical inaccuracy diffuses flow
Diffusion/dissipation in first order advection Original Image After 360 degree rotation using first order advection
Solution: Error Correction 1) Perform forward advection 2) Do backward advection from new position 3) Compute error and take correction step 4) Do forward advection from corrected position
Solution: Error Correction Forward Compensate Forward Backward error
Navier-Stokes Equations u=0 Incompressibility u t = k 2 u (u )u p + f Diffusion Change in Velocity Advection Pressure Body Forces
Divergence High divergence Low divergence Zero divergence
Enforcing Incompressibility First do velocity diffusion and advection Find closest vector field that is divergencefree Need to calculate divergence Need to find and use pressure
Measuring Divergence u=? u y ij+1 -u x? i-1j u x i+1j -u y ij-1 u ij =(u x i+1j - u x i-1j) + (u y ij+1-u y ij-1)
Pressure Term u new = u p Take divergence of both sides u new = u p zero u = 2 p
Pressure Term u = 2 p known unknown p ij+1 p i-1j? p i+1j p ij-1 p new ij = p ij + ( u ij - (p i-1j + p i+1j + p ij-1 + p ij+1-4p ij ))
Pressure Term u new = u p and velocity is now divergence-free
Fluid Simulator 1) Diffuse velocity 2) Advect velocity 3) Add body forces (e.g. gravity) 4) Pressure projection 5) Diffuse dye/smoke 6) Advect dye/smoke
Where is the Water? Water is often modeled as a fluid with a free surface The only thing we re missing so far is how to track where the water is: Voxelized: which cells are fluid vs. empty? Better: where does air/water interface cut through grid lines?
Marker Particles Simplest approach is to use marker particles Sample fluid volume with particles At each time step, mark grid cells containing any marker particles as Fluid, rest as Empty or Solid Move particles in incompressible grid velocity field (interpolating as needed)
Rendering Marker Particles Need to wrap a surface around the particles E.g. blobbies, or Zhu & Bridson 05 Problem: rendered surface has bumpy detail that the fluid solver doesn t have access to The simulation can t animate that detail properly if it can t see it. Result: looks fine for splashy, noisy surfaces, but fails on smooth, glassy surfaces.
Improvement Sample (implicit) surface function on simulation grid Even just union of spheres is reasonable if we smooth and render from the grid Issues, must make sure that each particle always shows up on grid or droplets can flicker in and out of existence Or: you must delete stray particles But still not perfect for flat fluid surfaces.
Level Sets Idea: drop the particles all together, and just sample the implicit surface function on the grid In particular, best behaved type of implicit surface function is signed distance
Computing Signed Distance Many algorithms exist Simplest and most robust (and accurate enough for animation) are geometric in nature Our example algorithm is based on a Fast Sweeping method [Tsai 02]
Level Sets Surface is exactly where =0 We need to evolve on the grid The surface ( =0) moves at the velocity of the fluid (any fluid particle with =0 should continue with =0) See e.g. the book [Osher&Fedkiw 02]
Reinitializing One problem is that advecting this way doesn t preserve signed distance property Eventually gets badly distorted Causes problems particularly for extrapolation, also for accuracy in general Reinitialize: recompute distance to surface Ideally shouldn t change surface at all, just values in the volume
Small-scale liquid-solid Interactions What makes large water and small water behave differently? Surface Tension Viscosity Lake ( >1 meter) Water drops (millimeters)
Surface Tension Fsurf N Normal (always pointing outward) Surface Tension Force 0 0
Virtual Surface Method Air Liquid Virtual Liquid Virtual Surface Solid
Virtual Surface Method 60 90 120 s s hydrophillic hydrophobic s