Every simulator you will ever trust has some bookkeeping it refuses to get wrong. A fluid solver must not create mass out of nothing, a heat solver must not leak energy, a traffic model must not spawn cars. All of these rules share one sentence: the amount of stuff inside a region changes only because of what crosses its boundary. In the last few lessons you built the pieces to say this in mathematics: the divergence measures outflow per unit volume (2.7 Divergence: Sources & Sinks), the curl measures circulation per unit area (2.8 Curl & Vorticity), and line and surface integrals add things up along curves and across surfaces (2.9 Line Integrals, Circulation & Flux). This lesson glues those pieces together into the three great integral theorems of vector calculus, and then uses them to turn a statement about boxes into a partial differential equation: the continuity equation that sits at the top of every fluid solver.
Transcript
- Green, Stokes, and the divergence theorem. Three famous theorems, and one idea underneath all of them.
- Every conservation law in physics has the same shape. The amount of stuff inside a region changes only because of what crosses its boundary.
- Today we turn that sentence into mathematics. The trick is to chop the region into tiny pieces and watch almost everything cancel.
- Start with one small square in a swirling flow. Walk counterclockwise around its four edges, adding up how much the flow pushes you along. That total is the circulation of the square.
- For a small square, the circulation is the curl at its center times its area. That is exactly what curl means: circulation per unit area.
- Now glue a second square beside it. Both are walked counterclockwise, so on the edge they share, the left square walks up while the right square walks down.
- The flow on that shared edge is the same for both squares, so their two contributions are equal and opposite. They cancel, and what's left is the circulation around the outer rectangle.
- Now chop a whole region into tiny squares, and give every one of them its own little counterclockwise loop.
- Every interior edge is shared by two squares, walked once in each direction. All of those cancel. Only the edges on the outside survive.
- Adding curl times area over all the squares builds the double integral of the curl. The surviving edges build the line integral around the boundary. That is Green's theorem.
- As the squares shrink, the staircase of surviving edges hugs the true boundary curve, and the approximation becomes exact.
- Nothing in that argument needed the squares to be flat. Tile a curved surface, like a net billowing in the wind, with tiny patches, and the same cancellation happens on every shared seam.
- That is Stokes' theorem. The flux of the curl through the surface equals the circulation around its rim.
- And here is a surprise. Puff the surface up or flatten it, but keep the same rim, and the answer never changes. Only the boundary matters.
- Now count flux instead of circulation. For a tiny box, the net flow out through its walls is the divergence times its volume.
- Stack a second box against it. Whatever leaves the left box through the shared wall enters the right box. Outflow for one is inflow for the other, so the shared wall cancels again.
- Fill a whole volume with boxes. Each interior wall is counted once as outflow and once as inflow, and only the outer walls remain.
- The total divergence inside equals the total flux out through the surface. That is the divergence theorem, also called Gauss's theorem.
- Here is why fluid people care. Take a fixed region of fluid. Its mass changes only by what flows out through its walls.
- The divergence theorem turns the wall integral into a volume integral. Now everything sits inside one integral, and it must vanish for every region we could pick.
- If an integral is zero over every region, however tiny, the integrand itself is zero. That is the continuity equation: a partial differential equation, derived from a statement about boxes. For constant density it says the velocity has zero divergence.
- Numerical methods can skip the last step and work with the boxes directly. A finite volume solver stores the amount of stuff in each cell, and moves it across the faces.
- Whatever leaves one cell enters its neighbor. So the total mass is conserved exactly, to the last digit, no matter how coarse the grid.
- Green, Stokes, and Gauss are one idea in three costumes. Add up tiny pieces, the interior cancels, and only the boundary survives. That's how integral laws become differential equations, and how finite volume solvers stay conservative.
- Next time: the divergence of the gradient, called the Laplacian. It hides inside diffusion, viscosity, and the pressure solve of every fluid simulator.
The one idea: interior pieces cancel
Recall the fundamental theorem of calculus:
The left side adds up a derivative over the inside of an interval. The right side only looks at the boundary, the two endpoints. Here’s a way to see why it works that will generalize. Chop into little pieces . On each piece, . Now add them up:
Every interior point appears twice, once with a plus sign (as the right end of one piece) and once with a minus sign (as the left end of the next). They cancel. Only the outer endpoints survive. This is called a telescoping sum, and the three theorems in this lesson are exactly this telescope in two and three dimensions:
| Theorem | “Inside” (derivative) | “Boundary” | Pieces |
|---|---|---|---|
| Fundamental theorem of calculus | endpoint values | intervals | |
| Fundamental theorem for line integrals (2.9) | at the two ends of | curve segments | |
| Green’s theorem | circulation around | little squares | |
| Stokes’ theorem | circulation around | little patches | |
| Divergence theorem | flux out through | little boxes |
The notation means “the boundary of ”. Keep this table in mind: every theorem below is one row of it.
Green’s theorem
Circulation around a tiny rectangle
Take a 2D field and a small rectangle . Walk around it counterclockwise and compute the circulation , one edge at a time:
- Bottom (going right, ): about .
- Top (going left): about .
- Right (going up, ): about .
- Left (going down): about .
Group the top with the bottom and the right with the left:
The bracket is exactly the scalar curl from 2.8 Curl & Vorticity. So, as promised there, the curl is circulation per unit area:
Gluing squares together
Now cover a region with a grid of tiny squares, each walked counterclockwise, and add up all their circulations. Look at an edge shared by two neighbouring squares. The left square walks it upward, the right square walks it downward. The field on that edge is the same for both, so the two line integrals are equal and opposite and cancel. Every interior edge cancels this way. What survives are the edges on the outside, and those, traversed in each square’s counterclockwise sense, trace out the boundary of counterclockwise. Meanwhile, the sum of (curl × area) over all the squares is a Riemann sum for a double integral (2.5 Multiple Integrals). Shrinking the squares gives:
“Region on your left” means counterclockwise around the outer edge. If has holes, the inner boundary curves are walked clockwise. That’s not a new rule: the little squares next to a hole still walk counterclockwise, and their surviving edges go clockwise around the hole.
Things to try in the explorer:
- With Pull cells apart above zero, look at any shared edge. It carries a green arrow (positive contribution) from one cell and a red arrow of the same size (negative contribution) from its neighbour. The readout shows the interior edges summing to zero, up to round-off around .
- Slide Cells per side from 1 to 24. The sum of all cell circulations always equals the boundary circulation, to the last digit, because the cancellation is exact algebra. The double integral of the curl matches too, up to quadrature error.
- Pick the Vortex with a core on the Ring region. The curl is nearly zero everywhere in the ring, so the cells contribute almost nothing. The outer boundary has circulation about , the clockwise inner boundary about , and they cancel. The circulation is trapped in the vortex core inside the hole.
- Switch to the flux form. Now each cell’s arrows show outward flux. Neighbours share each face’s flux with opposite signs, because what leaves one cell enters the next.
Worked example: both sides of Green’s theorem
Verify Green’s theorem for on the unit disk.
Inside. and , so . In polar coordinates ():
Boundary. Parametrize the circle counterclockwise: , , so and . Then
Using , the line integral is . ✓ Both sides agree.
Area from the boundary
Choose and with . Then the right side of Green’s theorem is just the area of . The symmetric choice , gives
You can measure an area by walking around it. For a polygon with vertices this becomes the shoelace formula , which graphics and mesh codes use constantly to compute cell areas and test orientation.
The flux form of Green’s theorem
Green’s theorem has a second personality. Apply it to the rotated field and you get a statement about flux across the boundary curve, with the outward unit normal:
This is the 2D divergence theorem. The tiny-box picture is the same: outflow from a small rectangle is about , and what leaves one cell through a shared face enters its neighbour.
Let and let be the unit square , with boundary walked counterclockwise.
- Compute directly, edge by edge.
- Compute and check that it agrees.
Show worked solution
-
Walk counterclockwise starting at the origin.
- Bottom, , : and , so the contribution is .
- Right, , : and , so the contribution is .
- Top, , : and , so the contribution is .
- Left, , : and , so the contribution is .
Total: .
-
, and . ✓
Use to find the area of the ellipse .
Show worked solution
Parametrize the boundary counterclockwise: , , . Then and , so
The area is . With it reduces to , as it should.
Stokes’ theorem
Nothing in the little-square argument needed the squares to be flat. Take an oriented surface in 3D, such as a soap film, a parachute, or a net billowing in the wind, and tile it with tiny curved patches. On each patch the circulation is about : the component of the curl along the patch’s normal, times its area. Neighbouring patches walk their shared seam in opposite directions, so the seams cancel and only the rim of the surface survives.
Green’s theorem is the special case of a flat surface in the -plane with .
The rim is all that matters
Here is the surprising consequence. The left side of Stokes’ theorem depends only on the boundary curve. So any two surfaces with the same rim, oriented the same way, have the same flux of curl through them. In the video, the net balloons up and flattens out while the rim stays fixed, and the answer never changes. In practice, this means you can often swap an awkward surface for a flat disk.
Worked example: hemisphere versus disk
Let and let be the upper unit hemisphere with upward normal. Verify Stokes’ theorem.
Curl.
Boundary. The rim is the unit circle in the plane . Upward normal plus the right-hand rule means we go counterclockwise seen from above: . Then and , so and the circulation is .
Surface, the easy way. By surface-independence, replace the hemisphere with the flat unit disk, whose normal is . The flux of through it is . ✓ (The hard way, with the hemisphere’s parametrization, also gives : is twice the area of the shadow the hemisphere casts on the -plane.)
Let be the part of the paraboloid with , normal pointing upward (outward from the dome). Let .
- Compute .
- Use Stokes’ theorem to find by computing a line integral instead.
- Check your answer by integrating over the flat disk , .
Show worked solution
- With , , :
- The rim is the circle in the plane , traversed counterclockwise seen from above (right-hand rule with upward normal): . On the rim , so , while has no -component. So and the circulation is . The flux of the curl through the paraboloid is .
- On the disk, and , so . The flux is . ✓ Two very different surfaces give the same answer because they share a rim.
The divergence theorem
Now count flux instead of circulation, and use boxes instead of patches. For a tiny box , the same pairing argument as the rectangle above works face by face. The flux out of the right face minus the flux in through the left face is about , and similarly in and . So
That is the definition of divergence from 2.7 Divergence: Sources & Sinks, read backwards. Fill a solid with boxes. Every interior wall is shared by two boxes, and whatever leaves one enters the other, so those fluxes cancel. Only the outer walls remain.
In words: total source strength inside = net flux out through the skin. An incompressible flow () has zero net flux through every closed surface. Whatever flows into a region must flow out.
Worked example: a cube
Verify the divergence theorem for on the unit cube .
Inside. , and
Skin. There are six faces, with outward normals .
- , : , and .
- , : .
- : , giving . And gives .
- : , giving . And gives .
The total is . ✓
Let and let be the unit sphere, oriented outward. Find .
Show worked solution
The surface integral directly is messy, so use the divergence theorem. . In spherical shells (volume element ):
Check: on the unit sphere , so . By symmetry each term has the same integral, and the average of over the unit sphere is , so . Three of them give . ✓
Let with . This is the field of a point source of unit strength (and, up to constants, gravity or the electric field of a point charge).
- Show that everywhere except the origin.
- Compute the flux of out of the sphere of radius .
- The divergence is zero, yet the flux is not. Explain why this does not contradict the divergence theorem, and find the flux out of any closed surface enclosing the origin.
Show worked solution
- Write , so . Using : Adding the and versions gives for .
- On the sphere, , so . Multiplying by the area gives a flux of , for every .
- The theorem needs to be continuously differentiable on the whole solid. This blows up at the origin, which lies inside the ball, so the theorem doesn’t apply there. All the “source” is concentrated at a single point (a Dirac delta, an infinitely sharp spike of total weight : ). For any closed surface around the origin, apply the theorem to the region between and a tiny sphere around the origin. is smooth there with zero divergence, so the outward flux through equals the outward flux through the small sphere, which is . This is Gauss’s law: the flux through a closed surface measures the total source enclosed, no matter the shape.
Verifying it numerically
Theorems like these are also great test oracles for code. If your discrete divergence and your boundary fluxes disagree, you have a bug. Here’s Green’s theorem checked numerically. Change the field and see that the two sides still match.
Now your turn, in 3D. Implement both sides of the divergence theorem for a rectangular box using the midpoint rule.
From an integral law to a PDE: the continuity equation
Here’s the payoff for fluids. Let be the density of a fluid moving with velocity . Fix a region in space, a control volume. Its mass is . The mass flux is mass per unit area per unit time, so is the rate at which mass leaves through a patch of the wall. With no sources or sinks, mass only changes by crossing the wall:
This integral conservation law is the physics. It’s true for any region, smooth or not, and it doesn’t need any derivatives of or . That will matter later, for shocks.
Now do two things.
- is fixed, so the time derivative moves inside: .
- Apply the divergence theorem to the wall integral: .
Now everything is one volume integral:
If an integral vanishes over every region, the (continuous) integrand vanishes. Suppose it were positive at some point. By continuity it would be positive on a small ball around that point, and the integral over that ball would be positive, a contradiction. Therefore:
The same recipe derives almost every PDE in this course. Pick a conserved quantity with density and flux , write “rate of change inside = −(flux out) + sources”, and apply the divergence theorem:
Then supply a constitutive law for the flux. Heat flowing down temperature gradients, (Fourier’s law), turns this into the heat equation , the subject of 9.2 The Heat (Diffusion) Equation. The operator that appears is the Laplacian, coming up next in 2.11 The Laplacian & Vector Identities. Momentum, with flux , gives the Navier–Stokes equations.
Cars on a one-lane road have density (cars per km) and flux (cars per hour passing point ). There are no on- or off-ramps.
- Write the integral conservation law for the stretch .
- Use the fundamental theorem of calculus (the 1D divergence theorem) to derive a PDE.
- If an on-ramp adds cars at rate (cars per km per hour), how does the PDE change?
- In a diffusion problem the flux is . What PDE results?
Show worked solution
- The number of cars in changes only by cars entering at and leaving at :
- , and since and are fixed, . So for every interval, which gives .
- Add to the right side of the integral law. Then .
- , i.e. : the heat (diffusion) equation. The boundary-flux picture explains why the second derivative appears: the net flux into a short interval is the difference of the gradients at its two ends.
Finite-volume preview: update cells by boundary fluxes
Numerical methods can skip the last step of the derivation and discretize the integral law directly. Chop space into cells, and store the cell average rather than point values. Applied to cell in 1D, the integral law is exact:
where is the flux through the face between cells and . The only approximation is how you estimate the face flux from nearby cell averages. With forward Euler in time:
For advection at speed , the simplest face flux is upwind: take density from the side the flow comes from, so if and if .
Now add up the update over all cells. Each face flux appears exactly twice: subtracted from the cell on its left and added to the cell on its right. The sum telescopes, just like the interior edges in Green’s theorem:
Total mass changes only by the fluxes at the domain’s ends. With periodic boundaries those match too, and mass is conserved to round-off, whatever the flux formula, grid size or time step. That’s why the method is called conservative. Accuracy is a separate question.
Worked example: one finite-volume step
Four periodic cells with hold . Take , , and the upwind flux.
The face fluxes are : , , , . By periodicity . With :
- cell 0:
- cell 1:
- cell 2:
- cell 3:
The new state is , and the total is still . The bump moved right and smeared a little. That smearing is the numerical diffusion of the upwind scheme. The method is stable when the CFL number (10.5 Wave Equation Numerics & the CFL Condition). That’s the same kind of step-size limit you met for forward Euler on stiff ODEs.
Implement it yourself:
Conservation is not automatic. It comes from writing the update in flux form. Here’s a flow whose speed varies in space, so fluid bunches up where it slows down. Mathematically, , but a scheme that discretizes only (the “advective form”) quietly drops the compression term, so it does not conserve mass in general (and here it misses the pile-up entirely):
The flux-form density piles up where the flow slows (near ), exactly as real traffic or gas would, and the total stays at . The advective form keeps forever. Its total happens to stay at because a uniform start is a fixed point of that form, but it misses the pile-up and transports as a passive label, not as mass. Both are “correct” discretizations of some PDE. Only one of them is the conservation law.
Key Takeaways
- One idea, three theorems: chop a region into tiny pieces, and the contributions from shared interior edges or faces cancel in pairs, leaving only the boundary. That’s the fundamental theorem of calculus in higher dimensions.
- Green: , with the region on your left. Its flux form, , is the 2D divergence theorem.
- Stokes: circulation around the rim equals the flux of curl through any surface spanning it (right-hand-rule orientation). Only the boundary matters.
- Divergence (Gauss): net outward flux through a closed surface equals the integral of the divergence inside. Incompressible flows have zero net flux through every closed surface.
- Use whichever side is easier. Watch the hypotheses: singularities inside the region (like a point source) break the theorems, and that is where concentrated sources hide.
- Integral law → PDE: “change inside = −(flux out)” plus the divergence theorem, for every region, gives the continuity equation , and the same recipe gives heat, diffusion and Navier–Stokes.
- Finite volume discretizes the integral law directly: update cell averages by face-flux differences. The sum telescopes, so mass is conserved exactly, and this is the discrete twin of the cancellation in Green’s theorem.