The Divergence Theorem
The divergence theorem says that the total outward flux of a vector field through a closed surface equals the integral of its divergence over the solid inside. Flux is measured on the boundary; divergence measures sources and sinks in the interior; the theorem says the books balance. It is the 3D partner of the flux form of Green's theorem, and it is the reason conservation laws can be written either for a whole volume or point by point.
The statement
Let be a simple solid region whose boundary surface is closed, piecewise smooth and oriented outward. Let have continuous partial derivatives on an open region containing . Then
The left side is a flux (Surface Integrals); the right side is a triple integral of the divergence from Curl and Divergence. The survey version is in Green's, Stokes' and Divergence Theorems.
Why it is true (sketch)
Split into and show ; the other two are the same argument along and . Take to be "-simple": . The Fundamental Theorem of Calculus in gives
On the surface, the top face (, upward normal) contributes by the graph formula, the bottom face (downward normal) contributes , and any vertical side has . The two sides match. More complicated solids are cut into simple pieces; the flux through each internal cut is counted twice with opposite normals and cancels.
Cutting into tiny boxes also explains the meaning of divergence:
is the outward flux per unit volume at a point: positive at a source, negative at a sink.
A check with exact arithmetic
Take and the unit ball. Then , and in spherical coordinates the volume side is . On the unit sphere the outward unit normal is the position vector itself, so .
import sympy as sp
rho, phi, th = sp.symbols('rho phi theta', nonnegative=True)
volume_side = sp.integrate(3 * rho**2 * rho**2 * sp.sin(phi), (rho, 0, 1), (phi, 0, sp.pi), (th, 0, 2*sp.pi))
x, y, z = sp.sin(phi)*sp.cos(th), sp.sin(phi)*sp.sin(th), sp.cos(phi)
F_dot_n = x**4 + y**4 + z**4 # F · position on the unit sphere
surface_side = sp.integrate(sp.expand(F_dot_n * sp.sin(phi)), (phi, 0, sp.pi), (th, 0, 2*sp.pi))
print("∭ div F dV =", volume_side)
print("∯ F · dS =", sp.simplify(surface_side))∭ div F dV = 12*pi/5
∯ F · dS = 12*pi/5
Gauss's law: a source hidden inside
The inverse-square field () has everywhere except the origin, where it is undefined. The theorem therefore says the flux out of any closed surface not enclosing the origin is 0. For a surface that encloses it, cut out a tiny sphere around the origin: the flux through that sphere is ( on a sphere of area ), and the shell in between has zero divergence, so every enclosing surface has flux . Check it on two cubes of side 2, one around the origin and off-centre, one beside it:
import numpy as np
def cube_flux(center, n=400):
s = (np.arange(n) + 0.5) * 2 / n - 1 # midpoints on [-1, 1]
A, B = np.meshgrid(s, s)
dA, total = (2 / n) ** 2, 0.0
for axis in range(3):
for side in (-1, 1):
p = [None, None, None]
p[axis] = np.full_like(A, center[axis] + side)
others = [i for i in range(3) if i != axis]
p[others[0]], p[others[1]] = center[others[0]] + A, center[others[1]] + B
r3 = (p[0]**2 + p[1]**2 + p[2]**2) ** 1.5
total += np.sum(side * p[axis] / r3) * dA # F · (outward normal)
return total
print("cube around the origin:", round(cube_flux((0.5, 0.3, 0.2)) / np.pi, 4), "π")
print("cube beside the origin:", round(cube_flux((3.0, 0.0, 0.0)) / np.pi, 4) + 0.0, "π")cube around the origin: 4.0 π
cube beside the origin: 0.0 π
In physics this is Gauss's law: the electric flux out of a closed surface is the enclosed charge divided by , wherever the charge sits inside.
Volume of a mesh
Choose , whose divergence is 1. The theorem turns volume into a boundary integral: . On a closed triangle mesh with outward (counter-clockwise) triangles the integral over each flat triangle is exact, and the formula becomes a sum of signed tetrahedra with apex at the origin:
import numpy as np
def uv_sphere(nlat, nlon):
tris = []
th = np.linspace(0, np.pi, nlat + 1)
ph = np.linspace(0, 2 * np.pi, nlon + 1)
P = lambda i, j: np.array([np.sin(th[i]) * np.cos(ph[j]), np.sin(th[i]) * np.sin(ph[j]), np.cos(th[i])])
for i in range(nlat):
for j in range(nlon):
a, b, c, d = P(i, j), P(i + 1, j), P(i + 1, j + 1), P(i, j + 1)
tris += [(a, b, c), (a, c, d)] # counter-clockwise seen from outside
return tris
def mesh_volume(tris):
return sum(np.dot(a, np.cross(b, c)) for a, b, c in tris) / 6
for n in (8, 32, 128):
print(f"{n:>3} x {2*n:<3} sphere mesh: {mesh_volume(uv_sphere(n, 2 * n)):.6f}")
print(f"4π/3 : {4 * np.pi / 3:.6f}") 8 x 16 sphere mesh: 3.926596
32 x 64 sphere mesh: 4.171996
128 x 256 sphere mesh: 4.187739
4π/3 : 4.188790
The triangles at the poles are degenerate (two corners coincide) and contribute 0, which is harmless. Flipping the winding of every triangle flips the sign of the result, which is how tools detect inside-out meshes.
In software
- Fluid simulation. An incompressible fluid has . Game and film fluid solvers (stable fluids, FLIP) advect velocities, then do a pressure projection: solve for a pressure and subtract , which removes the divergence. On a grid, " at a cell" is computed as the net flux through its six faces divided by its volume, the limit formula above made discrete.
- Finite-volume methods. CFD and weather codes store averages per cell and update them by face fluxes, so whatever leaves one cell enters its neighbour and mass is conserved to rounding error.
- Mesh tools. Volume, centre of mass and inertia tensors of closed meshes (physics engines, 3D-printing slicers, CAD) all come from divergence-theorem sums over triangles like the one above.
- Graphics. Photon and light "conservation" arguments, and the inside/outside test via generalised winding numbers, are flux integrals of the inverse-square field: a point is inside a closed mesh exactly when the flux is .
Exercises
Exercise 1: Constant divergence
Find the outward flux of through the sphere of radius 3 centred at the origin.
Answer
.
Solution
Exercise 2: A closed cylinder
Find the outward flux of through the closed cylinder , (side, top and bottom), and check the answer face by face.
Answer
.
Solution
Exercise 3: A spherical shell
is the shell and . What is the flux out of the outer sphere, out of the inner sphere (outward from , so pointing toward the origin), and in total?
Answer
, , total 0.
Solution
On a sphere of radius , and the area is , so the flux pointing away from the origin is for every . The outer sphere gives . On the inner sphere the normal that points out of points toward the origin, so it gives . The total is 0, as the divergence theorem requires because throughout the shell (the origin is not in ).
Exercise 4: Volume of a torus mesh (code)
Build a triangle mesh of the torus with , from its parametrisation in Parametric Surfaces and Their Areas and compute its volume with . Compare with .
Answer
; the mesh gives a slightly smaller value that approaches it as the grid is refined.
Solution
The torus volume is . With before , points outward (its -component is ), so the triangles and are counter-clockwise seen from outside and the sum comes out positive.
import numpy as np
R, b, n = 3.0, 1.0, 200
u = np.linspace(0, 2 * np.pi, n + 1)
U, V = np.meshgrid(u, u, indexing="ij")
P = np.stack([(R + b * np.cos(V)) * np.cos(U), (R + b * np.cos(V)) * np.sin(U), b * np.sin(V)], axis=-1)
a, bb, c, d = P[:-1, :-1], P[1:, :-1], P[1:, 1:], P[:-1, 1:]
vol = (np.einsum('ijk,ijk->ij', a, np.cross(bb, c)) + np.einsum('ijk,ijk->ij', a, np.cross(c, d))).sum() / 6
print(round(float(vol), 4), round(6 * np.pi**2, 4))59.1981 59.2176
Test yourself
has everywhere inside a closed surface enclosing volume 5. The outward flux is…
- 10
- 0
A fluid solver's pressure projection makes the velocity field…
- curl-free
- divergence-free, so no cell gains or loses fluid
- constant
- a gradient field
Which field turns the divergence theorem into a formula for the volume of a solid?