Curl and Divergence
Curl and divergence are the two derivatives of a vector field. The curl measures how much a field rotates around each point, the divergence how much it spreads out from it. They appear in every theorem that follows, in the equations of fluid flow and electromagnetism, and in the pressure-projection step that keeps simulated smoke and water from gaining or losing volume. This lesson defines both with the operator, proves the two key identities, interprets them physically and builds a working incompressibility projection. For a gentler start, see Divergence and Curl.
Definitions
Treat the del operator as a formal vector. For on with partial derivatives:
The curl is a vector field, the divergence a scalar field. A 2D field is treated as , so its curl is , the integrand of Green's theorem. Combining them gives the Laplacian .
Two identities
Theorem 1. If has continuous second partial derivatives, .
Proof. The first component is by Clairaut's theorem, and likewise for the other two. ∎
Theorem 2. If has continuous second partials, .
Proof. , the terms cancel in pairs by Clairaut. ∎
Theorem 1 gives a test: if somewhere, is not conservative. Conversely, if is defined on all of (or any simply connected region) and , then is conservative; the proof uses Stokes' theorem. Theorem 2 gives another test: if , then is not the curl of any field.
import sympy as sp
x, y, z = sp.symbols('x y z')
def curl(F): return [sp.diff(F[2], y) - sp.diff(F[1], z), sp.diff(F[0], z) - sp.diff(F[2], x), sp.diff(F[1], x) - sp.diff(F[0], y)]
def div(F): return sp.diff(F[0], x) + sp.diff(F[1], y) + sp.diff(F[2], z)
def grad(f): return [sp.diff(f, v) for v in (x, y, z)]
F = [y**2 * z, x * sp.sin(z), sp.exp(x) * y]
print("curl F =", curl(F))
print("div F =", div(F))
f = sp.Function('f')(x, y, z) # an arbitrary smooth function
G = [sp.Function(n)(x, y, z) for n in 'PQR'] # an arbitrary smooth field
print("curl(grad f) =", [sp.simplify(c) for c in curl(grad(f))])
print("div(curl G) =", sp.simplify(div(curl(G))))curl F = [-x*cos(z) + exp(x), y**2 - y*exp(x), -2*y*z + sin(z)]
div F = 0
curl(grad f) = [0, 0, 0]
div(curl G) = 0
The identities hold for completely general and , since SymPy treats mixed partials as equal. The example field happens to be incompressible () because each component does not depend on its own variable, yet its curl is not zero, so it is not conservative.
What they measure
Curl as rotation. Put a tiny paddle wheel in a fluid with velocity field . It spins fastest when its axle points along , and its angular speed is . For a rigid rotation the curl is , twice the angular velocity. A field with is called irrotational.
Divergence as net outflow. is the limit of . Positive divergence is a source, negative a sink, zero means the flow is incompressible: as much enters every tiny region as leaves it. The next example checks this flux interpretation with a small cube and also computes divergence on a sampled grid, as a simulation would.
import numpy as np
F = lambda x, y, z: np.array([x**2 * y, np.sin(y) + z, x * z**2])
div_exact = lambda x, y, z: 2 * x * y + np.cos(y) + 2 * x * z
p, h = np.array([1.0, 0.5, -1.0]), 1e-3 # flux out of a cube of side h around p
flux = 0.0
for axis in range(3):
e = np.zeros(3); e[axis] = h / 2
flux += (F(*(p + e))[axis] - F(*(p - e))[axis]) * h**2 # face-centre values × face area
print("flux / volume ≈", round(flux / h**3, 6), " exact div =", round(float(div_exact(*p)), 6))
g = np.linspace(-1, 1, 41) # the same field sampled on a grid
X, Y, Z = np.meshgrid(g, g, g, indexing="ij")
P, Q, R = F(X, Y, Z)
d = g[1] - g[0]
div_grid = np.gradient(P, d, axis=0) + np.gradient(Q, d, axis=1) + np.gradient(R, d, axis=2)
err = np.abs(div_grid - div_exact(X, Y, Z))[1:-1, 1:-1, 1:-1].max()
print("max interior error of np.gradient divergence:", round(float(err), 6))flux / volume ≈ -0.122417 exact div = -0.122417
max interior error of np.gradient divergence: 0.000417
Green's theorem in vector form
With the unit tangent and the outward unit normal of the boundary curve of a plane region , Green's theorem can be written in two ways:
The first lifts to Stokes' theorem on surfaces, the second to the Divergence Theorem on solids.
In software: pressure projection
Real-time fluid solvers (smoke, water and fire in games and film) store a velocity grid . After moving and pushing the fluid, is no longer divergence-free, so the solver projects it: by the Helmholtz decomposition with , and taking divergence gives a Poisson equation . Solve for the pressure , then subtract its gradient: . On a periodic grid the Poisson solve is one FFT, because each Fourier mode turns into multiplication by .
import numpy as np
N = 64
x = np.arange(N) * 2 * np.pi / N
X, Y = np.meshgrid(x, x, indexing="ij")
swirl = np.array([-np.sin(Y), np.sin(X)]) # div = 0
source = np.array([-np.sin(X) * np.cos(2*Y), -2 * np.cos(X) * np.sin(2*Y)]) # = ∇(cos x cos 2y)
u = swirl + source
k = np.fft.fftfreq(N, d=1 / N) # integer wavenumbers
KX, KY = np.meshgrid(k, k, indexing="ij")
K2 = KX**2 + KY**2; K2[0, 0] = 1
uh = np.fft.fft2(u)
div_hat = 1j * (KX * uh[0] + KY * uh[1])
print("max |div u| before:", round(float(np.abs(np.fft.ifft2(div_hat)).max()), 6))
ph = div_hat / -K2 # ∇²p = div u
w = np.real(np.fft.ifft2(uh - np.array([1j * KX * ph, 1j * KY * ph]))) # w = u − ∇p
div_w = np.real(np.fft.ifft2(1j * (KX * np.fft.fft2(w[0]) + KY * np.fft.fft2(w[1]))))
print("max |div w| after below 1e-12:", bool(np.abs(div_w).max() < 1e-12))
print("w equals the swirl part:", bool(np.allclose(w, swirl)))max |div u| before: 5.0
max |div w| after below 1e-12: True
w equals the swirl part: True
The source part has divergence (maximum size 5); projection removes exactly that gradient part and leaves the swirl. Other places curl and divergence appear:
- Electromagnetism and physics engines: Maxwell's equations are statements about div and curl of and .
- Procedural animation: "curl noise" takes the curl of a noise field to get a divergence-free velocity field (Theorem 2), so particles swirl like smoke without bunching up or thinning out.
- Game AI: a steering field built as a gradient has zero curl, so agents never circle endlessly; adding a curl component makes them orbit.
- Image processing: divergence of an image gradient is the Laplacian, used for edge detection and Poisson image blending.
Exercises
Exercise 1: Compute curl and div
Find and for .
Answer
, .
Solution
Exercise 2: Conservative?
Is conservative on ? If so, find a potential.
Answer
Yes; .
Solution
Exercise 3: Not a curl
Show that is not the curl of any vector field.
Answer
, but every curl has divergence 0.
Solution
If , then by Theorem 2. But , a contradiction.
Exercise 4: Paddle wheel speed (code)
A fluid flows with velocity . Compute by hand, then use SymPy to confirm it and print the angular speed of a small paddle wheel.
Answer
; angular speed .
Solution
import sympy as sp
x, y, z = sp.symbols('x y z')
P, Q, R = -3*y + z, 3*x, 0
c = sp.Matrix([sp.diff(R, y) - sp.diff(Q, z), sp.diff(P, z) - sp.diff(R, x), sp.diff(Q, x) - sp.diff(P, y)])
print(list(c), c.norm() / 2, round(float(c.norm() / 2), 6))[0, 1, 6] sqrt(37)/2 3.041381
Test yourself
Which statement is true for every smooth function ?
A velocity field has everywhere. The flow is…
- irrotational
- conservative
- incompressible
- constant
In a fluid solver's pressure projection, what is subtracted from the velocity field?
- The gradient of the pressure , where
- The curl of the velocity
- The divergence itself
- A constant vector