• Embed Doc
  • Readcast
  • Collections
  • CommentGo Back
 
Fluid Flow for the Rest of Us: Tutorial of the Marker and Cell Method inComputer Graphics
David Cline David Cardon Parris K. Egbert
Brigham Young University
Abstract
Understanding how fluid is modeled for computer graphics can bea challenge. This is especially true for students who have not takencourses in vector calculus and differential equations, or are rustyin these subjects. Beginning students tend to get bogged down inthe notation of the Navier-Stokes equations and the inevitable dif-ficulties that arise when trying to discretize them. Despite this fact,we will attempt to show that, given a little instruction, building acomplete fluid simulator is actually fairly straightforward.
1 Introduction
In the fall of 2001 I attempted to implement a fluid simulator, withonly limited success. In retrospect, I can see that there were reallytwo reasons for my lack of success. First, my math background hadnot prepared me for the notation of the Navier-Stokes equations,and second, the implementation details of the fluid flow systemsdescribed in the literature were scattered over a lot of “previouswork”.A couple of years later, in the Winter of 2004, a friend of mine,Dave Cardon, implemented a liquid simulator as a class project.While he was better prepared for the task than I was, he still foundit difficult to ferret out all of the details needed to implement a fluidsimulator.After both of our experiences, we realized that implementing afluid simulator is really not that difficult once all the details havebeen tracked down. It was understanding the notation and track-ing down the details that was the hard part. At that point, we de-cided to write what we wish we’d had to begin with, a detailedtutorial describing both the Navier-Stokes equations, and some of the most recent advances in fluid simulation for computer graphics.To that end, we have written this paper, which is organized as fol-lows: First, the mathematical notation used by the Navier-Stokesequations is described. Then the Navier-Stokes equations are pre-sented, giving what we hope is an intuitive explanation of each of the terms. Next, we describe a fluid simulator similar to the onedescribed in [Foster and Fedkiw 2001]. After the basic simulatorhas been described, we show how to extend it to achieve differenteffects, including making the fluid respond to moving objects as in[FosterandFedkiw2001], thesimulationoffluidswithvariablevis-cosity [Carlson et al. 2002], viscoelastic fluid simulation [Goktekinet al. 2004], and the simulation of smoke [Fedkiw et al. 2001]. Fi-nally, we discuss how to track the surface of a fluid using
level sets
.
2 Understanding the Navier-Stokes Equa-tions
2.1 Fluid Flow as a Velocity Field
In this paper we model fluid flow as a
velocity field 
, a vectorfield that defines the motion of a fluid at a set of points in space.
cline@rivit.cs.byu.edu, the dave@byu.edu, egbert@cs.byu.edu
Throughout our discussions, we will use the term
u
to refer tothis velocity field. To simulate a flowing fluid, we evolve
u
overtime, and move marker particles (or some other representation of the fluid, such as a level set) through space as dictated by
u
. Sincewe cannot store velocity information at every point in space, veloc-ities are stored at discrete grid points, and velocities between thesesamples are found by interpolation. The rules used to evolve
u
arebased on the Navier-Stokes equations, which will be presented insection 2.3.
2.2 Mathematical Notation
In this section, we introduce the mathematical notation used to de-fine the Navier-Stokes equations. In our descriptions we will use
bold
to indicate vector quantities, and
itallics
to indicate scalarquantities. For each term, we will try to give an intuitive expla-nation of what means, and then explain how we evaluate it numer-ically in our application. Although the terms described in this sec-tion may appear challenging, keep in mind that in every case theyboil down to adding or subtracting a few values from a small neigh-borhood in the grid.
The partial derivative
(
∂ 
)
.
The derivative of a function of onevariable
(
 x
)
can be written
d f dx
. If a function is defined over multi-ple variables, such as
g
(
 x
,
 y
,
 z
)
, it has a
partial derivative
, or simply
 partial
, for each variable over which the function is defined. Todistinguish the partial from the standard derivative, a stylized “d” isused in the notation. For example, the partial of 
g
with respect to
y
is written
∂ 
g
∂ 
 y
, and is defined as the limit of a
central difference
:
∂ 
g
(
 x
,
 y
,
 z
)
∂ 
 y
=
lim
h
0
g
(
 x
,
y
+
h
,
z
)
g
(
 x
,
y
h
,
z
)
2
h
This is almost identical to the definition of the derivative found inany Calculus text. One way to evaluate the partial on a discretegrid is to drop the limit from the above expression and use an
h
of one grid cell width. The denominator of the expression can also beignored for our application, leaving the central difference
∂ 
g
(
 x
,
 y
,
 z
)
∂ 
 y
=
g
(
 x
,
y
+
1
,
z
)
g
(
 x
,
y
1
,
z
)
(1)Besides the central difference, it is sometimes convenient to use a
 forward 
or a
backward 
difference to evaluate the partial:
∂ 
g
(
 x
,
 y
,
 z
)
∂ 
 y
=
g
(
 x
,
y
+
1
,
z
)
g
(
 x
,
y
,
z
)
(2)
∂ 
g
(
 x
,
 y
,
 z
)
∂ 
 y
=
g
(
 x
,
y
,
z
)
g
(
 x
,
y
1
,
z
)
(3)Sometimes we want to take the partial of one of the compo-nents of a vector-valued function. We will use subscripts todenote the different components of the function. For example,
∂ 
u
 x
∂ 
 y
=
u
 x
(
 x
,
 y
+
1
,
 z
)
u
 x
(
 x
,
 y
1
,
 z
)
is the partial derivative of the
 
 x
component of 
u
in the
y
direction evaluated using central differ-ences.
The gradient operator
(
)
.
The gradient operator is a vector of partial derivatives. In three dimensions, it is defined
=
∂ ∂ 
 x
,
∂ ∂ 
 y
,
∂ ∂ 
 z
.
The gradient of a scalar field,
g
, is a vector field that tells how thescalar field changes spatially. In our application, we will calculatethe gradient of the pressure field using
backward 
differences:
g
(
 x
,
 y
,
 z
) = (
g
(
 x
,
 y
,
 z
)
g
(
 x
1
,
 y
,
 z
)
,
g
(
 x
,
 y
,
 z
)
g
(
 x
,
 y
1
,
 z
)
,
g
(
 x
,
 y
,
 z
)
g
(
 x
,
 y
,
 z
1
) )
(4)
The divergence of a vector field
(
·
)
.
Since the gradient op-erator is just a vector of partial derivatives, it can be used in a dotproduct with a vector field. For instance, consider the vector field
u
. The dot product of the gradient operator with the vector field
u
produces the scalar field
·
u
=
∂ 
u
 x
∂ 
 x
+
∂ 
u
 y
∂ 
 y
+
∂ 
u
 z
∂ 
 z
where
u
 x
,
u
 y
and
u
 z
are the
x
,
y
and
z
components of 
u
. This useof the gradient operator is called the
divergence
of a vector fieldbecause it describes the net flow out of or into points in the vectorfield. Rather than finding the divergence of points within the vectorfield, however, we will use
forward 
differences to find the net flowout of or into a small cube within the vector field:
·
u
(
 x
,
 y
,
 z
) = (
u
 x
(
 x
+
1
,
 y
,
 z
)
u
 x
(
 x
,
 y
,
 z
)) +(
u
 y
(
 x
,
 y
+
1
,
 z
)
u
 y
(
 x
,
 y
,
 z
)) +(
u
 z
(
 x
,
 y
,
 z
+
1
)
u
 z
(
 x
,
 y
,
 z
))
(5)
The Laplacian operator,
(
2
)
.
The Laplacian operator is thedot product of two gradient operators. This dot product producesa scalar quantity which is the sum of the
x
,
y
and
z
second partials(the derivatives of the derivatives):
2
=
·
=
∂ 
2
∂ 
 x
2
+
∂ 
2
∂ 
 y
2
+
∂ 
2
∂ 
 z
2
.
Applying
2
to a scalar field produces another scalar field thatroughly describes how much values in the original field differ fromtheir neighborhood average. This property becomes readily appar-ent when we look at the terms of equation 6. To evaluate the lapla-cian, we subtracta
backward difference
froma
 forwarddifference
atthe point of interest. This formulation keeps the calculation withina neighborhood of three grid cells:
2
g
(
 x
,
 y
,
 z
) =
g
(
 x
+
1
,
 y
,
 z
) +
g
(
 x
1
,
 y
,
 z
) +
g
(
 x
,
 y
+
1
,
 z
) +
g
(
 x
,
 y
1
,
 z
) +
g
(
 x
,
 y
,
 z
+
1
) +
g
(
 x
,
 y
,
 z
1
)
6
g
(
 x
,
 y
,
 z
)
.
(6)To apply
2
to a vector field, we apply the operator to each vectorcomponent separately, resulting in a vector field that gives the dif-ference between vectors in the original field and their neighborhoodaverage:
2
u
(
 x
,
 y
,
 z
) = (
2
u
 x
(
 x
,
 y
,
 z
)
,
2
u
 y
(
 x
,
 y
,
 z
)
,
2
u
 z
(
 x
,
 y
,
 z
) )
.
(7)
2.3 The Navier-Stokes Equations for IncompressibleFlow
the Navier-Stokes equations are a set of two differential equationsthat describe the velocity field of a fluid,
u
, over time. They arecalled
differential
equations because they specify the derivatives of the velocity field rather than the velocity field itself. The first of theequations arises because we are simulating
incompressible
fluids,and is given by
·
u
=
0
.
(8)Simply put, this equation says that the amount of fluid flowing intoany volume in space must be equal to the amount flowing out. Notethat this is just the divergence of the velocity field. During simula-tion, we will solve a pressure term to satisfy equation 8.The second of the Navier-Stokes equations is a little more com-plicated. It specifies how
u
changes over time, and is given by
∂ 
u
∂ 
=
(
·
u
)
u
1
ρ
 p
+
ν 
2
u
+
F
.
(9)The second equation accounts for the motion of the fluid throughspace, alongwithanyinternalorexternalforcesthatactonthefluid.The terms of this equation can be described as follows:
∂ 
u
∂ 
Thederivativeofvelocitywithrespecttotime.
This will be calculated at all grid points contain-ing fluid during each time step of the simulation.
(
·
u
)
u The convection term.
This term arises becauseof the conservation of momentum. Since the ve-locity field is sampled at fixed spatial locations,the momentum of the fluid must be moved or“convected” through space along with the fluiditself. In practice, we will use a “backward par-ticle trace” method to solve this term.
1
ρ
 p
The pressure term.
This term captures forcesgenerated by pressure differences within thefluid.
ρ
is the density of the fluid, which is al-ways set to one in our simulations, and
p
is thepressure. Since we are simulating incompress-ible fluids, the pressure term will be coupledwith equation 8 to make sure the flow remainsincompressible.
ν 
2
u The viscosity term.
In thick fluids, frictionforces cause the velocity of the fluid to movetoward the neighborhood average. The viscos-ity term captures this relationship using the
2
operator. The variable
ν 
is called the
kinematicviscosity
of the fluid, and it determines the thick-ness of the fluid. The higher the value of 
ν 
, thethicker the fluid.
F External force.
Any external forces, such asgravity or contact forces with objects are in-cluded in this term. In practice, the externalforce term allows us to modify or set velocitiesin the grid to any desired value as one of the sim-ulation steps. The fluid then moves naturally, asthough external forces have acted on it.
 
3 The MAC Grid
This section describes how to discretize the velocity and pressurefields used in the Navier-Stokes equations. We use the staggeredMarker And Cell (MAC) grid first published in [Harlow and Welch1965], which has become one of the most popular ways to simulatefluid flow in computer graphics.The MAC grid method discretizes space into cubical cells withwidth
h
. Each cell has a pressure,
p
, defined at its center. It also hasa velocity,
u
= (
u
 x
,
u
 y
,
u
 z
)
, but the components of the velocity areplaced at the centers of three of the cell faces,
u
 x
on the x-min face,
u
 y
on the y-min face, and
u
 z
on the z-min face, as shown in figure1. Staggering the position of the velocity in this manner tends toproduce more stable simulations than storing the velocity at the cellcenters.Inadditiontospatialgridcells, asetofmarkerparticles(pointsinspace) is used to represent the fluid volume. As the simulation pro-gresses, the marker particles are moved through the velocity fieldand then used to determine which cells contain fluid.
u    x   u    z   u    y    p   h   
 y    x    z   
Figure 1: A MAC grid cell. Velocity components,
u
 x
,
u
 y
and
u
 z
, arestored on the minimal faces of the cell. Pressure,
p
, is stored at thecell center.
3.1 Putting the MAC Grid in a Hashtable
One of the main disadvantages to using a fixed MAC grid structureis that
u
is not defined outside the grid bounds. To get around thisproblem, we use a dynamic grid. Grid cells are created as needed,and destroyed when no longer in use. The cells are stored in a hashtable which is keyed by the cell coordinates. We use the hash func-tion presented in [Worley 1996] and used by [Steele et al. 2004]:
hash
=
541
 x
+
79
 y
+
31
 z
.
(10)[Teschner et al. 2003] define a similar hash function that could alsobe used. Using a hash table version of the MAC grid frees the fluidto go anywhere in the environment, and reduces memory require-ments so that the number of grid cells is proportional to the amountof fluid being simulated rather than the entire volume of the simu-lation space.Despite the trouble to which we have gone to make the simu-lation space unbounded, we allow simulations to specify explicitbounds on where the fluid can go. This makes it easy to definewalls for the fluid to collide against, and prevents fluid particlesfrom flying off into infinity.
4 Simulation
This section describes our basic simulation method, which is essen-tially the same as the one presented in [Foster and Fedkiw 2001],except that we use a dynamic MAC grid instead of a static one. Fig-ure 2 shows several screen shots captured from the basic simulatordescribed in this section.Figure 2: Screen shots from the basic fluid simulator. The upperleft image shows the grid cells that define the velocity field. Inthe other images, the fluid particles that track the liquid volumeare shown. The particles are rendered in OpenGL as billboards(pictures of spheres) rather than actual 3D spheres for efficiency.
4.1 The Basic Simulator
The MAC grid method simulates fluid flow by periodically updat-ing the velocity field. Marker particles are moved through the ve-locity field to keep track of where the fluid is in space. The actualsequence that our simulator uses to do this is shown in figure 3.The remainder of this section will detail each of the steps in thealgorithm.
Step 1. Calculate the time step.
To avoid unnecessary compu-tation, we would like to use a time step that is as large as possible,butifitistoolong, theflowmaylookunnaturalorbecomeunstable.We use a heuristic called the CFL condition to determine what
to use. For our application, the CFL condition states that
shouldbe small enough so that the maximum motion in the velocity fieldis less than the width of a grid cell. In other words,
h
/
|
u
max
|
where
h
is the width of a grid cell and
u
max
is the maximum velocityin
u
.In practice, the strict CFL condition can be relaxed somewhat aslong as there is a big enough buffer zone around the cells containingfluid. [Foster and Fedkiw 2001] and [Enright et al. 2002] got awaywith time steps as much as five times larger than dictated by theCFL condition. Our implementation finds
by scaling the CFLtime step by a user-specified constant,
cfl
:
=
cfl
h
|
u
max
|
.
(11)
is then clamped to lie between user-specified min and max timesteps.
Step 2. Update the grid.
Before the velocity field can be ad-vanced, the list of cells containing fluid must be updated, and a
buffer zone
of air must be created around the fluid so that the fluid
of 00

Leave a Comment

You must be to leave a comment.
Submit
Characters: ...
You must be to leave a comment.
Submit
Characters: ...