<<

Notes

! To recap, using eta for depth=h+H: D! = "!# $ u Dt Du = "g#h Dt ! We!re currently working on the () part

cs533d-term1-2005 1 cs533d-term1-2005 2

Advection Exploiting Lagrangian view

! Let!s discretize just the material derivative ! We want to stick an Eulerian grid for now, (advection equation): but somehow exploit the fact that Dq q + u! "q = 0 or = 0 • If we know q at some point x at time t, we just t Dt follow a particle through the flow starting at x ! For a Lagrangian scheme this is trivial: just to see where that value of q ends up move the particle that stores q, don!t change the value of q at all q(x(t + !t),t + !t) = q(x(t),t) q(x(t),t) = q(x0,0) dx = u(x), x(t) = x0 dt

cs533d-term1-2005 3 cs533d-term1-2005 4 Looking backwards Semi-Lagrangian method

! Problem with tracing particles - we want values at grid ! Developed in weather prediction, going back to nodes at the end of the step the 50!s • Particles could end up anywhere ! Also dubbed “stable fluids” in graphics ! But… we can look backwards in time (reinvention by Stam #99) q = q x(t ! "t),t ! "t ijk ( ) ! To find new value of q at a grid point, trace dx particle backwards from grid point (with velocity = u(x), x(t) = xijk dt u) for -"t and interpolate from old values of q ! Same formulas as before - but new interpretation ! Two questions • To get value of q at a grid point, follow a particle backwards • How do we trace? through flow to wherever it started • How do we interpolate?

cs533d-term1-2005 5 cs533d-term1-2005 6

Tracing Tracing: 1st order

! The errors we make in tracing backwards ! We!re at grid node (i,j,k) at position xijk aren!t too big a deal ! Trace backwards through flow for -"t • We don!t compound them - stability isn!t an x = x ! "t u issue old ijk ijk • How accurate we are in tracing doesn!t effect • Note - using u value at grid point (what we know shape of q much, just location already) like Forward Euler. ! Whether we get too much blurring, oscillations, or ! Then can get new q value (with interpolation) a nice result is really up to interpolation n +1 n ! Thus look at “Forward” Euler and RK2 qijk = q (xold ) n = q (xijk ! "tuijk )

cs533d-term1-2005 7 cs533d-term1-2005 8

Interpolation Linear interpolation

! “First” order accurate: nearest neighbour ! Error in linear interpolation is proportional to 2 • Just pick q value at grid node closest to xold " q !x 2 • Doesn!t work for slow fluid (small time steps) -- "x 2 nothing changes! • When we divide by grid spacing to put in terms of ! Modified PDE ends up something like… advection equation, drops to zero!th order accuracy Dq " 2q = k(!t)!x 2 ! “Second” order accurate: linear or bilinear (2D) Dt "x 2 • Ends up first order in advection equation • We have numerical viscosity, things will smear out • Still fast, easy to handle boundary conditions… • For reasonable time steps, k looks like 1/"t ~ 1/"x • How well does it work? ! [Equivalent to 1st order upwind for CFL "t] ! In practice, too much smearing for inviscid fluids

cs533d-term1-2005 9 cs533d-term1-2005 10

Nice properties of lerping Cubic interpolation

! Linear interpolation is completely stable ! To fix the problem of excessive smearing, • Interpolated value of q must lie between the go to higher order old values of q on the grid ! E.g. use cubic splines • Thus with each time step, max(q) cannot • Finding interpolating C2 cubic spline is a little increase, and min(q) cannot decrease painful, an alternative is the ! Thus we end up with a fully stable • C1 Catmull-Rom (cubic Hermite) spline algorithm - take "t as big as you want ! [derive] • Great for interactive applications ! Introduces overshoot problems • Also simplifies whole issue of picking time • Stability isn!t so easy to guarantee anymore steps

cs533d-term1-2005 11 cs533d-term1-2005 12 Min-mod limited Catmull-Rom Back to Shallow Water

! See Fedkiw, Stam, Jensen #01 ! So we can now handle advection of both ! Trick is to check if either slope at the endpoints water depth and each component of water of the interval has the wrong sign velocity • If so, clamp the slope to zero ! Left with the and gradient • Still use cubic Hermite formulas with more reliable slopes terms !" $ !u !w' = #"& + ) ! This has same stability guarantee as linear !t % !x !z ( interpolation !u !h • But in smoother parts of flow, higher order accurate = #g • Called “high resolution” !t !x ! Still has issues with boundary conditions… !w !h = #g cs533d-term1-2005 13 !t !z cs533d-term1-2005 14

Staggered grid Spatial Discretization

! We like central differences - more ! So on the MAC grid: accurate, unbiased ! So natural to use a staggered grid for % u 1 # u 1 w 1 # w 1 ( !"ij i+ 2, j i# 2, j i, j + 2 i, j# 2 velocity and height variables = #"ij ' + * • Called MAC grid after the Marker-and-Cell !t & $x $z ) method (Harlow and Welch #65) that !u 1 h # h introduced it i+ 2, j = #g i+1, j i, j ! Heights at cell centres !t $x

! u-velocities at x-faces of cells !wi, j + 1 hi, j +1 # hi, j 2 = #g ! w-velocities at z-faces of cells !t $z

cs533d-term1-2005 15 cs533d-term1-2005 16

Solving Full Equations Operator Splitting

! Let!s now solve the full incompressible ! Generally a bad idea to treat Euler or Navier-Stokes equations incompressible flow as conservation laws ! We!ll first avoid interfaces with constraints (e.g. free surfaces) ! Instead: split equations up into easy ! Think smoke chunks, just like Shallow Water

ut + u! "u = 0 2 ut = #" u

ut = g 1 ut + $ "p = 0 (" ! u = 0) cs533d-term1-2005 17 cs533d-term1-2005 18

Time integration Advection boundary conditions

! Don!t mix the steps at all - 1st order accurate ! But first, one last issue u(1) = advect(un ,!t) ! Semi-Lagrangian procedure may need to (2) (1) 2 (2) interpolate from values of u outside the domain, u = u + "!t# u or inside solids u(3) = u(2) + !tg • Outside: no correct answer. Extrapolating from nearest point on domain is fine, or assuming some n +1 (3) 1 u = u $ !t % #p far-field velocity perhaps ! We!ve already seen how to do the advection step • Solid walls: velocity should be velocity of wall (e.g.zero) ! Often can ignore the second step (viscosity) ! Technically only normal component of velocity needs to be ! Let!s focus for now on the last step (pressure) taken from wall, in absence of viscosity the tangential component may be better extrapolated from the fluid

cs533d-term1-2005 19 cs533d-term1-2005 20 Continuous pressure Pressure boundary conditions

! Before we discretize in space, last step is ! Issue of what to do for p and u at to take u(3), figure out the pressure p that boundaries in pressure solve n+1 makes u incompressible: ! Think in terms of control volumes: • Want ! " un +1 = 0 subtract pn from u on boundary so that u(3) t 1 p 0 • Plug in pressure update formula: ! " ( # $ % ! ) = integral of u•n is zero • Rearrange: t 1 p u(3) ! " (# $ ! ) = ! " ! So at closed boundary we end up with • Solve this Poisson problem (often density is n +1 constant and you can rescale p by it, also "t) u ! n = 0 ! Make this assumption from now on: 2 (3) #p ! p = ! " u un +1 ! n = u(3) ! n " un +1 = u(3) # !p #n cs533d-term1-2005 21 cs533d-term1-2005 22

Pressure BC’s cont’d. Approximate projection

! At closed wall boundary have two choices: ! Can now directly discretize Poisson equation on a grid # " 2 p " 2 p " 2 p& • Set u•n=0 first, then solve for p with $p/$n=0, 2 p (! ) = % 2 + 2 + 2 ( don!t update velocity at boundary ijk $ "x "y "z ' p * 2p + p p * 2p + p p * 2p + p ) i+1 jk ijk i*1 jk + ij +1k ijk ij*1k + ijk+1 ijk ijk*1 • Or simply solve for p with $p/$n=u•n and +x 2 +y 2 +z2 update u•n at boundary with -$p/$n # "u "v "w& (! , u) = % + + ( ijk "x "y "z • Equivalent, but the second option make sense $ ' ijk u * u v * v w * w in the continuous setting, and generally keeps ) i+1 jk i*1 jk + ij +1k ij*1k + ijk+1 ijk*1 you more honest 2+x 2+y 2+z - pi+1 jk * pi*1 jk pij +1k * pij*1k pijk+1 * pijk*1 0 (!p) ) / , , 2 ! At open (or free-surface) boundaries, no ijk . 2+x 2+y 2+z 1 constraint on u•n, so typically pick p=0 ! Central differences - 2nd order, no bias

cs533d-term1-2005 23 cs533d-term1-2005 24 Issues Exact projection (1st try)

! On the plus side: simple grid, simple discretization, ! Connection becomes exact in limit for smooth u… ! But it doesn!t work • use the discrete divergence as a hard • Divergence part of equation can!t “see” high frequency constraint to enforce, pressure p turns out to compression waves be the Lagrange multipliers… • Left with high frequency oscillatory error • Need to filter this out - smooth out velocity field before ! Or let!s just follow the route before, but subtracting off pressure gradient • Filtering introduces more numerical viscosity, eliminates features discretize divergence and gradient first on coarse grids First try: use centred differences as before ! Also: doesn!t exactly make u incompressible • • Measuring divergence of result gives nonzero • u and p all “live” on same grid: uijk, pijk ! So let!s look at exactly enforcing the incompressibility This is called a “collocated” scheme constraint •

cs533d-term1-2005 25 cs533d-term1-2005 26

Exact collocated projection Problems

! " un +1 = 0 ! So want ( )ijk ! Pressure problem decouples into 8 independent un +1 # un +1 v n +1 # v n +1 w n +1 # w n +1 subproblems i+1 jk i#1 jk + ij +1k ij#1k + ijk+1 ijk#1 = 0 2$x 2$y 2$z ! “Checkerboard” instability • Divergence still doesn!t see high-frequency ! Update with discrete gradient of p un +1 = u(3) ! "p compression waves # p ! p p ! p p ! p & n +1 (3) i+1 jk i!1 jk ij +1k ij!1k ijk+1 ijk!1 ! Really want to avoid differences over 2 grid uijk = uijk !% , , ( $ 2"x 2"y 2"z ' points, but still want centred ! Plug in update formula to solve for p ! Thus use a staggered MAC grid, as with shallow pi+2 jk ! 2pijk + pi!2 jk pij +2k ! 2pijk + pij!2k pijk+2 ! 2pijk + pijk!2 2 + 2 + 2 = water 4"x 4"y 4"z u(3) ! u(3) v(3) ! v(3) w(3) ! w(3) i+1 jk i!1 jk + ij +1k ij!1k + ijk+1 ijk!1 2"x 2"y 2"z

cs533d-term1-2005 27 cs533d-term1-2005 28

Staggered grid Exact staggered projection

! Pressure p lives in centre of cell, pijk ! Do it discretely as before, but now want ! ! " un +1 = 0 u lives in centre of x-faces, ui+1/2,j,k ( )ijk n +1 n +1 n +1 n +1 n +1 n +1 ! v in centre of y-faces, vi,j+1/2,k u # u v # v w # w i+1/ 2 jk i#1/ 2 jk + ij +1/ 2k ij#1/ 2k + ijk+1/ 2 ijk#1/ 2 = 0 ! w in centre of z-faces, wi,j,k+1/2 $x $y $z ! Whenever we need to take a difference (grad p or div u) result is where it should be ! And update is p ! p un +1 = u(3) ! i+1 jk ijk i+1/ 2 jk i+1/ 2 jk "x ! Works beautifully with “stair-step” boundaries n +1 (3) pij +1k ! pijk Not so simple to generalize to other boundary vij +1/ 2k = vij +1/ 2k ! • "y geometry n +1 (3) pijk+1 ! pijk wijk+1/ 2 = wijk+1/ 2 ! cs533d-term1-2005 29 "z cs533d-term1-2005 30

(Continued) Pressure solve simplified

! Plugging in to solve for p ! Assume for simplicity that "x="y="z=h ! Then we can actually rescale pressure (again - already took in density and "t) to get pi+1 jk ! 2pijk + pi!1 jk pij +1k ! 2pijk + pij!1k pijk+1 ! 2pijk + pijk!1 2 + 2 + 2 = "x "y "z 6pijk ! pi+1 jk ! pi!1 jk ! pij +1k ! pij!1k ! pijk+1 ! pijk!1 = (3) (3) (3) (3) (3) (3) (3) (3) (3) (3) (3) (3) ui+1/ 2 jk ! ui!1/ 2 jk vij +1/ 2k ! vij!1/ 2k wijk+1/ 2 ! wijk!1/ 2 + + !ui+1/ 2 jk + ui!1/ 2 jk ! vij +1/ 2k + vij!1/ 2k ! wijk+1/ 2 + wijk!1/ 2 "x "y "z

! At boundaries where p is known, replace (say) pi+1jk with known value, move to right-hand side (be careful to ! This is for all i,j,k: gives a linear system to scale if not zero!)

solve -Ap=d ! At boundaries where (say) $p/$y=v, replace pij+1k with pijk+v (so finite difference for $p/$y is correct at boundary)

cs533d-term1-2005 31 cs533d-term1-2005 32 Solving the Linear System How to solve it

! So we!re left with the problem of efficiently ! Direct Gaussian Elimination does not work well • This is a large sparse matrix - will end up with lots of fill-in (new finding p nonzeros) ! Luckily, linear system Ap=-d is symmetric ! If domain is square with uniform boundary conditions, positive definite can use FFT • Fourier modes are eigenvectors of the matrix A, everything ! Incredibly well-studied A, lots of work out works out there on how to do it fast ! But in general, will need to go to iterative methods • Luckily - have a great starting guess! Pressure from previous time step [appropriately rescaled]

cs533d-term1-2005 33 cs533d-term1-2005 34

Convergence Conjugate Gradient

! Need to know when to stop iterating ! Standard iterative method for solving ! Ideally - when error is small symmetric positive definite systems ! But if we knew the error, we!d know the solution ! ! We can measure the residual for Ap=b: it!s just r=b-Ap For a fairly exhaustive description, read • Related to the error: Ae=r • “An Introduction to the Conjugate Gradient ! So check if norm(r)

cs533d-term1-2005 35 cs533d-term1-2005 36 Plain vanilla CG

! r=b-Ap (p is initial guess) ! "=rTr, check if already solved ! s=r (first search direction) ! Loop: • t=As • #= "/(sTt) (optimum step size) • x+= #s, r-= #t, check for convergence T • "new=r r

• $= "new /" • s=r+ $s (updated search direction)

• "="new

cs533d-term1-2005 37