Chapter 2
Conservation of Mass
Block the tip of a syringe full of water with your finger and push the plunger: it barely moves. With air in the syringe, the plunger slides right in. Water hardly gives way when you squeeze it; even a hundred times the pressure of the air around us shrinks it by only about 0.5%. So when water flows into one place, the same amount has to flow out somewhere. The same rule is at work when a wide river runs into a narrow gorge and speeds up: for the same amount of water to pass in the same time, it has to move faster where the river is narrow. This chapter writes that simple rule as one equation, the term highlighted in the equation bar.
What flows in flows out
Draw a small box in the water. It has no walls; it is just an outline in your mind, and water crosses it freely. Over one second, write down in a ledger how much water comes in and goes out through each side. If water cannot be squeezed, the amount inside the box can neither grow nor shrink, so what came in must equal what went out. The balance is zero.
The exceptions are places where water is added or taken away from outside: a spring where water wells up from below, or a drain where it runs away. Seen only as a flow on the plane, water appears at a source and disappears at a sink. In the experiment below, pick a source, a sink or a swirl, and move the box. On each side, orange arrows show water going out and sky-blue arrows show water coming in. The ledger at the top left adds up what came in, what went out and the balance.
Side note
This flow is not a simulation: it is drawn from formulas and solves no equation of motion. The source is not a single point but spreads over a small patch with a radius of about 0.1. The flow is on a plane, so it measures water by area instead of volume: each number in the ledger is the area of water that crossed the sides in one second. Particles appear where the source gives water and vanish where the sink takes it, so they stay evenly spread everywhere.
At first the box sits away from the source. Water comes in through the side facing the source and goes out through the other three. In equals out, so the balance is 0: water passes through the box, but none piles up inside. Move the box over the source, and water only goes out, through all four sides. The balance becomes +0.20, the amount the source gives every second. Make the box bigger and the balance stays at +0.20: nearly all of the source’s water already wells up inside, and the new area only lets water pass through. A sink is the reverse: water only comes in, and the balance is −0.20. In the swirl, water crosses the box, yet wherever you put the box, the balance is 0.
The balance of the ledger: divergence
Now shrink the box onto a single point. The balance divided by the box’s area settles to one value as the box gets smaller. That value is the divergence at the point. Take a small box wide and tall, and write the velocity as . What goes out through the right side minus what comes in through the left side depends on how changes across the box:
The top and bottom sides add times the area in the same way. Add the two and divide by the area , and you get the divergence:
Where the divergence is positive, more water goes out than comes in, as at the source. Where it is negative, more comes in than goes out: a sink. Where it is zero, what comes in goes out. In the experiment, the source’s divergence lives only inside the small orange glow and is as good as zero outside it. The swirl’s divergence is zero everywhere.
Going the other way, the balance of a big box is the sum of the balances of the small boxes that make it up. Where two neighbouring boxes share a side, what leaves one enters the other, so the two cancel, and only the water crossing the outer outline remains. So the balance of a closed outline, the water that goes out minus the water that comes in, equals the sum of all the divergence inside it. In the equation below, is the unit vector that stands at right angles to the outline and points outward.
That is why the balance stayed the same when the box grew. The divergence lives only around the source, so any box that holds the whole source measures what the source gives, whatever its size. A box that holds only part of it measures that share, and a box without it measures zero.
Side note
If the source were a single point, its divergence would be zero everywhere except at that point, and still every box holding the point, large or small, would measure the same amount: all of the divergence would be packed into one point. This experiment spreads the source over a small patch, so that any box can weigh the share it holds.
Now we can write mass conservation as an equation. In a fluid of constant density with no sources or sinks, what flows into any point must flow out:
This is the second equation that stands beside the Navier–Stokes equation. The first says how a flow changes; this one says what every flow must obey.
Side note
When the density can change, mass conservation reads as follows: the mass inside any box changes only by the mass that crosses its sides.
If is constant, the first term vanishes, and dividing by leaves only the zero divergence. The assumption holds well for liquids. Air counts too, as long as it moves slower than about a third of the speed of sound (some 100 m/s): its density then changes by only a few percent. This book keeps constant throughout.
Faster through the narrow part
Apply the ledger to a channel. Treat the stretch between two cross-sections as one box. No water crosses the walls, so all the water that comes in through section 1 in a second must go out through section 2. The amount that passes a section in a second is the flow rate, and it is the section’s area times the mean speed . So the flow rate is the same at both sections:
In a flow on a plane, the area of a section is the channel’s width (with a depth of 1). The experiment below is the narrowing channel from chapter 1. Each of its two sections shows its width , its mean speed and their product , the flow rate. Squeeze sets how many times wider the inlet is than the throat, as in chapter 1, and Section 2 moves the second section.
Side note
As in chapter 1, formulas chosen to conserve the water’s volume draw this channel; it solves no equation of motion. The water moves downstream equally fast at every height across a section, and it slides along the walls instead of stopping at them. Real water sticks to the walls and runs fastest in the middle (chapter 4). Written with the mean speed, the equation still holds.
The water speeds up as it enters the throat and slows down again where the channel widens. Even so, is 0.054 at both sections, always. It stays so wherever you move section 2, and when you narrow the throat further. The speed grows in the same proportion as the width shrinks:
The shaded bands of water all have the same area, so in the throat, where the channel is narrow, they stretch out. The particles move apart along the channel there and close in across it, yet a patch of any given area holds as many of them as in the wide part: nowhere does the water crowd together or thin out.
Mass conservation tells you how much faster the water must go. It does not tell you what pushes it to go faster. A parcel of water needs a force to speed up, and that force comes from differences in pressure. Chapter 3 takes up that force.
A fake fluid without its projection
A simulator has to obey this condition too. At every step, this book’s engine adds the stirring force and carries the flow along, and the velocity field that comes out usually has a divergence that is not zero. So every step goes through a stage that removes the divergence, called the pressure projection. Chapter 3 shows how pressure does that. In the experiment below, you can switch this stage off and on.
Side note
Even with the projection on, the divergence is not exactly zero: the pressure comes from an iterative solve, which leaves a tiny remainder. In this experiment, while the water is being stirred, what remains is about a thousandth of the divergence with the projection off. In a browser without WebGPU, a recorded video plays instead, and the video cannot switch the projection.
Stir with the projection off, and the particles crowd together ahead of the stroke in a bright band, while behind it they drain away and thin out. This is a flow where what comes in does not go out. Turn on the divergence layer: the crowded front edge is sky blue (negative divergence), and the patch just behind it, where the particles are draining away, is orange (positive divergence). In this fake fluid, water piles up in one place and drains out of another, which cannot happen in water of constant density.
Switch the projection on and stir the same way, and the water pushed ahead flows around to both sides and rolls into a pair of swirls. The particles stay evenly spread as they turn, and the divergence layer stays dark. The swirls you stirred up in chapter 0 took their shape from this stage too.
In one sentence
In a fluid of constant density, whatever flows into a small box must flow out, so the balance of that ledger per unit area, the divergence , is zero everywhere.
Something to think about
A wide river flowing at 1 m/s runs into a gorge half as wide. If the depth stays the same, how fast does the water flow in the gorge?
Show answer
At 2 m/s on average. With the same depth, the cross-section’s area halves, and for the flow rate to stay the same, the speed must double. Mass conservation only tells you how much faster the water goes; the push that speeds it up comes from differences in pressure (chapter 3).
Something to think about
In the fake fluid without its projection, is the divergence positive or negative where particles crowd together into a bright band?
Show answer
Negative. More water flows into that spot than out of it, so particles pile up there. Where particles thin out, more water goes out than comes in, and the divergence is positive. That is why the experiment’s divergence layer paints the front edge sky blue and the patch behind it orange.