Learning Objectives
By the end of this section you will be able to:
- State the continuity equation in its general form and explain every symbol.
- Derive it from scratch by doing mass bookkeeping on a tiny control volume.
- Read the incompressible form as a geometric statement about the divergence of a vector field.
- Verify continuity numerically with the divergence theorem, in plain Python and in PyTorch.
- Diagnose whether a given flow is compressible, incompressible, a source, or a sink — by inspection.
The Bookkeeping Question
One question, no equations yet: if I draw a tiny box in the middle of a flowing river, and I count every drop of water that crosses the wall of the box per second, what must the total come to?
For an ordinary river — incompressible water, no taps, no drains — the answer is obviously zero. Every drop that enters one wall must leave through another, because the water inside the box has nowhere to hide. It cannot be compressed. It cannot be destroyed. So the books must balance.
That single sentence — the books must balance — is the continuity equation. Everything that follows is just translating it into the language of partial derivatives so we can apply it at every point of the fluid simultaneously, not just on one box.
The big idea, before any math
The continuity equation is a conservation law for mass. It says: if the density inside a region is changing, that change must be accounted for by mass crossing the region's boundary. Mass cannot appear or disappear without a flux to explain it.
Intuition: A Tiny Glass Box
Imagine an infinitesimally small transparent box sitting in a stream. Call it a control volume. Its sides are , , . It does not move. Water flows freely through its faces.
Three things can happen inside this box, and only three:
1. Mass flows in
Water crosses one face heading into the box. We count this as positive inflow.
2. Mass flows out
Water crosses some face heading away. We count this as positive outflow.
3. Density inside changes
The fluid already in the box gets denser or thinner: density ticks up or down.
Conservation of mass says these three are not independent. The change in what is inside must equal inflow minus outflow:
This is the same sentence a shopkeeper says when balancing a cash register at the end of the day. It is also the continuity equation. The only thing we are about to do is translate "mass flowing in minus out" into the calculus expression .
Flux, Divergence, and the Theorem That Links Them
Before we can write the continuity equation we need two ideas: flux (how much mass crosses a surface per second), and divergence (the local source rate of a vector field).
Flux
For a fluid moving with velocity and density , the mass flux is the vector (units: kg/m²/s). It points in the direction mass is moving, and its magnitude says how much mass per second is crossing a unit area perpendicular to it.
The total mass crossing a surface per second is the surface integral:
where is the outward unit normal. The dot product picks out only the component of velocity that actually crosses the surface — fluid moving along doesn't count.
Divergence
The divergence of a vector field at a point measures the net outflow rate per unit volume at that point:
Picture a tiny sphere around the point. If , more is flowing out than in — the point acts like a source. If , more is flowing in — a sink. If zero, the books balance locally.
The Divergence Theorem
The bridge between the two is the divergence theorem of Gauss:
The total flux through the boundary of any region equals the integral of the divergence over the region. This is the multivariable cousin of the Fundamental Theorem of Calculus, and it is the engine that turns "mass bookkeeping on a box" into a differential equation at every point.
Deriving the Continuity Equation
We now translate "rate of mass accumulating = mass flowing in minus out" into symbols. Take a fixed region of space, with boundary .
Left-hand side: rate of mass accumulating
The total mass inside at time is the volume integral of density:
The rate at which that mass changes is just its time derivative:
We pulled the derivative inside the integral because is fixed in space — it doesn't move with the fluid, so its limits of integration don't depend on .
Right-hand side: net mass entering
The mass crossing the boundary per second is the surface integral of mass flux. Outflow is positive when the velocity points along the outward normal. So the net inflow is the negative of the outflow:
Apply the divergence theorem
The right-hand side is begging for the divergence theorem applied to the vector field :
Set accumulation equal to inflow
Combining the last three statements, becomes:
or, moving both integrands to one side,
Shrink the region to a point
The crucial step. This integral identity must hold for any choice of — any shape, any size, anywhere in the fluid. The only way that's possible is if the integrand is itself zero everywhere. (If it were positive at a point, we'd pick a tiny ball around that point and break the equation.) That gives us the differential form:
Local rate of density change + Net outflow of mass per unit volume = 0
Read it left to right: if the density at a point is going up, the divergence of mass flux at that point must be negative — mass must be piling in faster than it leaves. If density is going down, mass is escaping. If density is steady, the books balance pointwise.
The Incompressible Form
For liquids like water, density barely changes — call it constant. Plugging in :
- The time-derivative term vanishes: .
- The constant slides out of the divergence: .
- Dividing through by the (non-zero) constant leaves a beautifully clean equation.
For water, blood, oil at modest speeds — the velocity field is divergence-free.
Geometrically: at every point in the fluid, the velocity field has no sources and no sinks. Whatever flows in must flow out — at every point, all at once. This is one of the cleanest equations in physics, and it constrains the velocity field of a Navier-Stokes flow in a profound way: of the three velocity components , only two are actually independent, because ties them together.
When you cannot assume incompressibility
For gases at high speed (Mach > 0.3), shock waves, sound waves, atmospheric science, plasma physics, and combustion, density changes matter and you must keep the full term. The incompressible shortcut is a wonderful, almost-universally-correct approximation for liquids and slow gas flows — but it is a shortcut, not a law of physics.
Interactive: Watch Mass Flow Through a Box
Drag the dashed amber box anywhere in the field. Move the divergence slider and the rotation slider . The four numbers on the box show the flux through each face — red is outward, blue is inward — and the panel on the right keeps a running ledger.
Try these experiments — they make the math concrete:
- Click Pure Rotation. Watch every face have non-zero flux, yet the net out always pinches back to zero. That is the continuity equation for .
- Click Source. Notice every face now has positive outflux (red). The net is positive, equal to — exactly what the divergence theorem predicts. Compare the "NET OUT" row to the "2α · Area" row in the panel.
- Drag the box around in the source case. Net outflux changes with where you put the box? No — only with the box's area. That's because divergence is uniform here.
- Set Spiral (Mixed). The flow looks complicated but the bookkeeping is the same: net out = 2α · Area, independent of ω. Rotation does not violate continuity.
Worked Example: Spinning vs Exploding Fluids
Let's compute the divergence (and net outflux through a unit square) of two specific fields by hand. They look similar, but one is incompressible and the other isn't.
| Field | Components | What it looks like |
|---|---|---|
| Field A (Rotation) | Counter-clockwise solid-body rotation about the origin | |
| Field B (Outflow) | Radial expansion away from the origin |
We'll compute analytically, then verify with a surface integral on the unit square . Try it yourself first before expanding the solution.
Expand: full hand calculation (try it yourself first!)
Field A: rotation
Step 1 — divergence. With and :
The field is everywhere divergence-free. The continuity equation is satisfied at every single point.
Step 2 — flux through the unit square. The outward normal on the right edge (x = 1) is , so . Integrate over :
By similar work on the other three faces:
- Left face (x = 0, normal = (−1, 0)): — but wait, on x = 0 the field component u = −y still, so this is .
- Top face (y = 1, normal = (0, 1)): .
- Bottom face (y = 0, normal = (0, −1)): .
Net: . The fluid spins around but the books are balanced — consistent with the pointwise divergence being zero.
Field B: outflow
Step 1 — divergence. Now , :
Divergence is the constant 2 everywhere. Every point of space is acting as a uniform mass source.
Step 2 — flux through the unit square.
- Right face (x = 1): .
- Left face (x = 0): .
- Top face (y = 1): .
- Bottom face (y = 0): .
Net: .
Check against divergence theorem: The two answers match exactly.
The Punchline
Field A passes the continuity test (∇·v = 0). Field B fails it uniformly — every point is a source of mass at rate 2. If you saw Field B in a real flow, you'd know mass is being created from nothing somewhere, which is physically impossible: in a real fluid you'd have to add a source term (a tap, an injector, a chemical reaction) to the right-hand side of the continuity equation.
Plain Python: Checking Continuity Numerically
Algebra is reassuring. Numbers are more reassuring. We'll now compute both sides of the divergence theorem — the pointwise divergence and the net flux through the boundary — in plain Python with finite differences, and confirm they match for two test fields.
PyTorch: Continuity via Autograd
Finite differences are honest but noisy. Autograd is exact (to floating-point precision) and scales effortlessly. Better still, in 2D we can construct a divergence-free field automatically using a stream function: pick any scalar ψ(x, y), set , and the divergence is identically zero — guaranteed by the equality of mixed partials.
What the Equation Really Says
Putting it all together: the continuity equation is the local form of mass conservation. It bridges two scales:
Integral (bulk) form
"The rate of mass accumulating inside a region equals the rate mass flows in across its boundary."
Differential (pointwise) form
"At every point, the rate density changes is balanced by the divergence of mass flux."
These are the same statement, written at two different zoom levels. The divergence theorem is the optical instrument that switches between them.
Three ways to read the incompressible form
| Reading | Statement | Useful when... |
|---|---|---|
| Volumetric | Fluid parcels conserve their volume as they move. | Tracking individual fluid elements (Lagrangian view). |
| Geometric | The velocity field has no sources or sinks. | Visualizing streamlines or doing CFD post-processing. |
| Algebraic | u_x + v_y + w_z = 0 ties the three velocity components. | Numerically integrating the Navier-Stokes equations. |
Applications
🚿 Pipe flow
For incompressible flow in a pipe, continuity says A₁v₁ = A₂v₂. A pipe that narrows must speed the fluid up — the basis for nozzles, venturis, and your garden hose.
🌬️ Atmospheric science
Weather models cannot use the incompressible shortcut. The full continuity equation links wind, temperature, and pressure changes — it's what propagates pressure systems across continents.
❤️ Hemodynamics
Blood flow through arteries is treated as incompressible. When an artery narrows (stenosis), continuity demands the velocity rises — and ultrasound exploits this to see the blockage.
⚡ Electromagnetism
Maxwell's ∇·B = 0 is literally a continuity equation for magnetic field lines — there are no magnetic monopoles, so field lines never start or end. Same math, different physics.
🎮 Fluid simulation
Realistic water and smoke in games and films are made divergence-free at each frame by solving a Poisson equation — that projection step is the continuity equation imposed numerically.
🤖 ML & normalizing flows
Continuous normalizing flows model probability density as it transports through a velocity field. The same continuity equation governs how a probability cloud reshapes during sampling.
Common Pitfalls
| Pitfall | What goes wrong | Fix |
|---|---|---|
| Forgetting the density factor | Writing for a compressible flow. | Always keep the full unless ρ is genuinely constant. |
| Confusing divergence and curl | Treating a rotating flow as compressible because it ‘looks’ like it’s moving sideways. | Rotation gives non-zero curl but zero divergence. Solid-body rotation passes continuity. |
| Sign errors on the outward normal | Adding +u on the left face instead of −u — flux comes out double or zero. | Always evaluate v · n with n pointing outward. The minus on left/bottom faces is the whole point. |
| Mixing Eulerian and Lagrangian frames | Computing ‘rate of mass change’ inside a moving fluid parcel using ∂ρ/∂t. | For a moving region, use the material derivative Dρ/Dt and the Reynolds transport theorem. |
| Assuming a source term doesn't exist | In combustion, evaporation, or reactive flows, mass really is created or destroyed locally. | Add a source term S on the RHS: . |
Summary
The continuity equation is the mathematical statement that mass is conserved at every point of a fluid. It takes the simplest possible accounting principle — "what flows in must either accumulate or flow out" — and translates it into a partial differential equation that holds at every point simultaneously.
Key Takeaways
- General form: — local mass conservation.
- Incompressible form: — when density is constant, the velocity field is divergence-free.
- The divergence theorem is the bridge between the bulk (surface-integral) statement and the pointwise (differential) statement.
- Rotation ≠ compressibility: a fluid can swirl as violently as it likes and still satisfy continuity, as long as no mass appears or disappears.
- In 2D, the stream function automatically generates divergence-free velocity fields — a trick used across physics and deep learning whenever an incompressibility constraint matters.
Coming Next: Section 4 examines viscosity and the stress tensor — the physical origin of the term in the momentum equation, and the reason real fluids resist being deformed.