cs 231 fluid simulation - computer science and …vbz/cs231f15/lecture11-15.pdf · fluid simulation...

45
CS 231 Fluid simulation

Upload: hoangthuan

Post on 29-Jul-2018

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

CS 231

Fluid simulation

Page 2: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Why Simulate Fluids?

Feature film special effects

Computer games

Medicine (e.g. blood flow in heart)

Because it’s fun

Page 3: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

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]

Page 4: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Vector Fields (Fluid Velocity)

uij = (ux,uy)

Page 5: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Navier-Stokes Equations

u=0

ut = k2u –(u)u – p + f

Incompressibility

Change in Velocity

Advection

Diffusion

Body Forces

Pressure

Page 6: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Navier-Stokes Equations

u=0

ut = k2u –(u)u – p + f

Incompressibility

Change in Velocity

Advection

Diffusion Pressure

Body Forces

Page 7: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

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

Page 8: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Scalar Field (Smoke, Dye)

cij

1.2 3.7 5.1 …

Page 9: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Diffusion

cij

Page 10: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Diffusion

1

1

1

1

-4

cijnew = cij + k Dt (ci-1j + ci+1j + cij-1 + cij+1 - 4cij)

ct = k2c

value relative

to neighbors

Page 11: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Diffusion = Blurring

Original Some Diffusion More Diffusion

Page 12: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Vector Field Diffusion

ut = k2u

… blur the x-velocity and the y-velocity

uxt = k2ux

uyt = k2uy

Two separate diffusions:

viscosity

Page 13: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Effect of Viscosity

• Each one is ten times higher

viscosity than the last

Low Medium High Very High

Page 14: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Variable Viscosity

Viscosity can vary based on position

Viscosity field k can change with temperature

Page 15: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Navier-Stokes Equations

u=0

ut = k2u –(u)u – p + f

Incompressibility

Change in Velocity

Advection

Diffusion Pressure

Body Forces

Page 16: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Advection = Pushing Stuff

Page 17: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Scalar Field Advection

ct = –(u)c

change in value advection current values

Page 18: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Vector Field Advection

ut = –(u)u

uxt = –(u)ux

uyt = –(u)uy

Two separate advections:

… push around x-velocity and y-velocity

Page 19: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Advection

Easy to code

Method stable even at large time steps

Problem: numerical inaccuracy diffuses flow

Page 20: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Diffusion/dissipation

in first order advection

Original Image After 360 degree rotationusing first order advection

Page 21: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

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

Page 22: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

error

Compensate

Forward

BackwardForward

Solution: Error Correction

Page 23: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Navier-Stokes Equations

u=0

ut = k2u –(u)u – p + f

Incompressibility

Change in Velocity

Advection

Diffusion Pressure

Body Forces

Page 24: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Divergence

High divergence Low divergence

Zero divergence

Page 25: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Enforcing Incompressibility

First do velocity diffusion and advection

Find “closest” vector field that is divergence-

free

Need to calculate divergence

Need to find and use pressure

Page 26: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Measuring Divergence

u=?

uij=(uxi+1j - ux

i-1j) + (uyij+1-u

yij-1)

?

-uyij-1

uyij+1

-uxi-1j uxi+1j

Page 27: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Pressure Term

unew = u – p

Take divergence of both sides…

unew = u – p

u = 2p

zero

Page 28: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Pressure Term

u = 2p

pnewij = pij + ( uij - (pi-1j + pi+1j + pij-1 + pij+1 - 4pij))

known unknown

?

pij-1

pij+1

pi-1j pi+1j

Page 29: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Pressure Term

unew = u – p

…and velocity is now divergence-free

Page 30: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

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

Page 31: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering
Page 32: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

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?

Page 33: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

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)

Page 34: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

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.

Page 35: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

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.

Page 36: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

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

Page 37: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

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]

Page 38: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

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]

Page 39: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

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

Page 40: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering
Page 41: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Small-scale liquid-solid

Interactions

Lake ( >1 meter) Water drops (millimeters)

What makes large water and small water behave

differently?

Surface Tension

Viscosity

Page 42: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Surface Tension

Normal (always pointing outward)

Surface Tension Force

surfF N

0 0

Page 43: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

Virtual Surface Method

Solid

Virtual

Surface

Virtual Liquid

Liquid

Air

Page 44: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering

60s 90s 120s

hydrophillic hydrophobic

Virtual Surface Method

Page 45: CS 231 Fluid simulation - Computer Science and …vbz/cs231f15/lecture11-15.pdf · Fluid Simulation Called Computational Fluid Dynamics (CFD) Many approaches from math and engineering