TExcelTAD
Home › Calculus in Depth › The Divergence Theorem

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 EE be a simple solid region whose boundary surface SS is closed, piecewise smooth and oriented outward. Let F\mathbf{F} have continuous partial derivatives on an open region containing EE. Then

∯SF⋅dS=∭Ediv⁡F dV,div⁡F=∇⋅F=Px+Qy+Rz\oiint_S \mathbf{F} \cdot d\mathbf{S} = \iiint_E \operatorname{div} \mathbf{F}\,dV, \qquad \operatorname{div} \mathbf{F} = \nabla \cdot \mathbf{F} = P_x + Q_y + R_z

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 F\mathbf{F} into ⟨P,0,0⟩+⟨0,Q,0⟩+⟨0,0,R⟩\langle P, 0, 0 \rangle + \langle 0, Q, 0 \rangle + \langle 0, 0, R \rangle and show ∯R k⋅n dS=∭Rz dV\oiint R\,\mathbf{k} \cdot \mathbf{n}\,dS = \iiint R_z\,dV; the other two are the same argument along xx and yy. Take EE to be "zz-simple": E={(x,y,z):(x,y)∈D, u1(x,y)≤z≤u2(x,y)}E = \{(x, y, z) : (x, y) \in D,\ u_1(x, y) \le z \le u_2(x, y)\}. The Fundamental Theorem of Calculus in zz gives

∭ERz dV=∬D[R(x,y,u2(x,y))−R(x,y,u1(x,y))] dA\iiint_E R_z\,dV = \iint_D [R(x, y, u_2(x, y)) - R(x, y, u_1(x, y))]\,dA

On the surface, the top face (z=u2z = u_2, upward normal) contributes ∬DR(x,y,u2) dA\iint_D R(x, y, u_2)\,dA by the graph formula, the bottom face (downward normal) contributes −∬DR(x,y,u1) dA-\iint_D R(x, y, u_1)\,dA, and any vertical side has k⋅n=0\mathbf{k} \cdot \mathbf{n} = 0. 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 EE into tiny boxes also explains the meaning of divergence:

div⁡F(P)=lim⁡volume→01V(B)∯∂BF⋅dS\operatorname{div} \mathbf{F}(P) = \lim_{\text{volume} \to 0} \frac{1}{V(B)} \oiint_{\partial B} \mathbf{F} \cdot d\mathbf{S}

div⁡F\operatorname{div} \mathbf{F} is the outward flux per unit volume at a point: positive at a source, negative at a sink.

A check with exact arithmetic

Take F=⟨x3,y3,z3⟩\mathbf{F} = \langle x^3, y^3, z^3 \rangle and the unit ball. Then div⁡F=3(x2+y2+z2)=3ρ2\operatorname{div} \mathbf{F} = 3(x^2 + y^2 + z^2) = 3\rho^2, and in spherical coordinates the volume side is ∭3ρ2⋅ρ2sin⁡ϕ dρ dϕ dθ\iiint 3\rho^2 \cdot \rho^2 \sin\phi\,d\rho\,d\phi\,d\theta. On the unit sphere the outward unit normal is the position vector itself, so F⋅n=x4+y4+z4\mathbf{F} \cdot \mathbf{n} = x^4 + y^4 + z^4.

python · try it yourself
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 F=r/∣r∣3\mathbf{F} = \mathbf{r}/\lvert \mathbf{r} \rvert^3 (r=⟨x,y,z⟩\mathbf{r} = \langle x, y, z \rangle) has div⁡F=0\operatorname{div} \mathbf{F} = 0 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 4π4\pi (F⋅n=1/ε2\mathbf{F} \cdot \mathbf{n} = 1/\varepsilon^2 on a sphere of area 4πε24\pi\varepsilon^2), and the shell in between has zero divergence, so every enclosing surface has flux 4π4\pi. Check it on two cubes of side 2, one around the origin and off-centre, one beside it:

python · try it yourself
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 ε0\varepsilon_0, wherever the charge sits inside.

Volume of a mesh

Choose F=⟨x,y,z⟩/3\mathbf{F} = \langle x, y, z \rangle/3, whose divergence is 1. The theorem turns volume into a boundary integral: V=13∯r⋅n dSV = \frac{1}{3} \oiint \mathbf{r} \cdot \mathbf{n}\,dS. On a closed triangle mesh with outward (counter-clockwise) triangles (a,b,c)(\mathbf{a}, \mathbf{b}, \mathbf{c}) the integral over each flat triangle is exact, and the formula becomes a sum of signed tetrahedra with apex at the origin:

V=16∑a⋅(b×c)V = \frac{1}{6} \sum \mathbf{a} \cdot (\mathbf{b} \times \mathbf{c})
python · try it yourself
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 div⁡v=0\operatorname{div} \mathbf{v} = 0. Game and film fluid solvers (stable fluids, FLIP) advect velocities, then do a pressure projection: solve ∇2p=div⁡v∗\nabla^2 p = \operatorname{div} \mathbf{v}^* for a pressure pp and subtract ∇p\nabla p, which removes the divergence. On a grid, "div⁡v\operatorname{div} \mathbf{v} 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 4π4\pi.

Exercises

Exercise 1: Constant divergence

Find the outward flux of F=⟨x+y2,y−z,2z+x⟩\mathbf{F} = \langle x + y^2, y - z, 2z + x \rangle through the sphere of radius 3 centred at the origin.

Answer

144π144\pi.

Solution
div⁡F=1+1+2=4∭4 dV=4⋅43π⋅33=4⋅36π=144π\begin{aligned} \operatorname{div} \mathbf{F} &= 1 + 1 + 2 = 4 \\ \iiint 4\,dV &= 4 \cdot \tfrac{4}{3}\pi \cdot 3^3 = 4 \cdot 36\pi = 144\pi \end{aligned}

Exercise 2: A closed cylinder

Find the outward flux of F=⟨x,y,z⟩\mathbf{F} = \langle x, y, z \rangle through the closed cylinder x2+y2≤4x^2 + y^2 \le 4, 0≤z≤50 \le z \le 5 (side, top and bottom), and check the answer face by face.

Answer

60π60\pi.

Solution
div⁡F=3,volume=π⋅22⋅5=20π,flux=60πside: F⋅n=(x2+y2)/2=2,area 2π⋅2⋅5=20π→40πtop: F⋅n=z=5,area 4π→20πbottom: F⋅n=−z=0→0total 40π+20π+0=60π\begin{aligned} &\operatorname{div} \mathbf{F} = 3, \quad \text{volume} = \pi \cdot 2^2 \cdot 5 = 20\pi, \quad \text{flux} = 60\pi \\ &\text{side: } && \mathbf{F} \cdot \mathbf{n} = (x^2 + y^2)/2 = 2, && \text{area } 2\pi \cdot 2 \cdot 5 = 20\pi && \to 40\pi \\ &\text{top: } && \mathbf{F} \cdot \mathbf{n} = z = 5, && \text{area } 4\pi && \to 20\pi \\ &\text{bottom: } && \mathbf{F} \cdot \mathbf{n} = -z = 0 && && \to 0 \\ &\text{total } 40\pi + 20\pi + 0 = 60\pi \end{aligned}

Exercise 3: A spherical shell

EE is the shell 1≤∣r∣≤21 \le \lvert \mathbf{r} \rvert \le 2 and F=r/∣r∣3\mathbf{F} = \mathbf{r}/\lvert \mathbf{r} \rvert^3. What is the flux out of the outer sphere, out of the inner sphere (outward from EE, so pointing toward the origin), and in total?

Answer

4π4\pi, −4π-4\pi, total 0.

Solution

On a sphere of radius aa, F⋅(r/a)=1/a2\mathbf{F} \cdot (\mathbf{r}/a) = 1/a^2 and the area is 4πa24\pi a^2, so the flux pointing away from the origin is 4π4\pi for every aa. The outer sphere gives +4π+4\pi. On the inner sphere the normal that points out of EE points toward the origin, so it gives −4π-4\pi. The total is 0, as the divergence theorem requires because div⁡F=0\operatorname{div} \mathbf{F} = 0 throughout the shell (the origin is not in EE).

Exercise 4: Volume of a torus mesh (code)

Build a triangle mesh of the torus with R=3R = 3, b=1b = 1 from its parametrisation in Parametric Surfaces and Their Areas and compute its volume with V=16∑a⋅(b×c)V = \frac{1}{6} \sum \mathbf{a} \cdot (\mathbf{b} \times \mathbf{c}). Compare with 2π2Rb22\pi^2 Rb^2.

Answer

6π2≈59.21766\pi^2 \approx 59.2176; the mesh gives a slightly smaller value that approaches it as the grid is refined.

Solution

The torus volume is (area of the tube’s cross-section πb2)×(distance its centre travels 2πR)=2π2Rb2=2π2⋅3⋅1=6π2(\text{area of the tube's cross-section } \pi b^2) \times (\text{distance its centre travels } 2\pi R) = 2\pi^2 Rb^2 = 2\pi^2 \cdot 3 \cdot 1 = 6\pi^2. With uu before vv, ru×rv\mathbf{r}_u \times \mathbf{r}_v points outward (its zz-component is b(R+bcos⁡v)sin⁡vb(R + b\cos v)\sin v), so the triangles (a,b,c)(\mathbf{a}, \mathbf{b}, \mathbf{c}) and (a,c,d)(\mathbf{a}, \mathbf{c}, \mathbf{d}) are counter-clockwise seen from outside and the sum comes out positive.

python · try it yourself
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

F\mathbf{F} has div⁡F=−2\operatorname{div} \mathbf{F} = -2 everywhere inside a closed surface SS enclosing volume 5. The outward flux is…

  1. 10
  2. −2-2
  3. −10-10
  4. 0

A fluid solver's pressure projection makes the velocity field…

  1. curl-free
  2. divergence-free, so no cell gains or loses fluid
  3. constant
  4. a gradient field

Which field turns the divergence theorem into a formula for the volume of a solid?

  1. ⟨x,y,z⟩/3\langle x, y, z \rangle/3
  2. ⟨1,1,1⟩\langle 1, 1, 1 \rangle
  3. r/∣r∣3\mathbf{r}/\lvert \mathbf{r} \rvert^3
  4. ⟨y,z,x⟩\langle y, z, x \rangle