Divergence and Curl
Divergence and curl are two derivatives of a vector field. Divergence measures how much the field spreads out from a point, like a source or sink. Curl measures how much it swirls around a point. Fluid simulations in games and films are built around them: an incompressible fluid has zero divergence, and its curl is the visible swirl.
The del operator
Treat as a vector of derivative operators. Applied in three different ways, it gives the three basic derivatives of vector calculus:
| Operation | Input | Output | Formula |
|---|---|---|---|
| Gradient | scalar field | vector field | |
| Divergence | vector field | scalar field | |
| Curl | vector field | vector field |
Here . The curl is the cross product determinant from Dot and Cross Products with in the middle row. In 2D, with , the divergence is and the scalar curl is (the -component of the 3D curl).
Divergence: sources and sinks
Imagine as the velocity of a fluid. Draw a tiny box around a point. The divergence is the net rate at which fluid flows out of the box, per unit volume:
- : a source, more flows out than in (fluid is being created, or gas is expanding).
- : a sink, fluid is draining or being compressed.
- everywhere: the field is incompressible (divergence-free). What flows in must flow out.
For the divergence is everywhere: every point is a little source. For the vortex it is : the fluid just circles.
Curl: rotation
Put a tiny paddle wheel in the fluid. The curl is a vector along the wheel's axis when it spins fastest, with length twice the local angular velocity of the fluid. For the vortex the curl is : it rotates counter-clockwise about the -axis, like a rigid body with angular speed 1. A field with zero curl everywhere is irrotational.
Two things are surprising at first:
- A field can curve without having curl. circles the origin but has zero curl away from the origin, because the outer layers move slower in just the right way.
- A field can move in straight lines and still have curl. The shear flow (a river faster at the top) has curl : a paddle wheel in it spins.
import sympy as sp
x, y, z = sp.symbols('x y z')
def div(F):
return sp.simplify(sum(sp.diff(Fi, v) for Fi, v in zip(F, (x, y, z))))
def curl(F):
P, Q, R = F
return [sp.simplify(e) for e in (sp.diff(R, y) - sp.diff(Q, z), sp.diff(P, z) - sp.diff(R, x), sp.diff(Q, x) - sp.diff(P, y))]
fields = {
"source <x, y, z> ": (x, y, z),
"vortex <-y, x, 0> ": (-y, x, 0),
"shear <y, 0, 0> ": (y, 0, 0),
"swirl <-y, x, 0>/r^2": (-y / (x**2 + y**2), x / (x**2 + y**2), 0),
"mixed <xy, yz, zx> ": (x*y, y*z, z*x),
}
for name, F in fields.items():
print(f"{name}: div = {div(F)}, curl = {curl(F)}")source <x, y, z> : div = 3, curl = [0, 0, 0]
vortex <-y, x, 0> : div = 0, curl = [0, 0, 2]
shear <y, 0, 0> : div = 0, curl = [0, 0, -1]
swirl <-y, x, 0>/r^2: div = 0, curl = [0, 0, 0]
mixed <xy, yz, zx> : div = x + y + z, curl = [-y, -z, -x]
Two identities
For any smooth fields:
Both follow from the equality of mixed partials (Clairaut). The first explains why conservative (gradient) fields have no circulation, as in line integrals. The second gives a recipe for building incompressible flow: take the curl of anything. Also worth knowing: is the Laplacian , which appears in heat flow, diffusion, image blurring and the pressure solve below.
import sympy as sp
x, y, z = sp.symbols('x y z')
f = sp.exp(x) * sp.sin(y) + x**2 * z**3
F = sp.Matrix([sp.sin(y * z), x**3 * z, sp.exp(x * y)])
grad = sp.Matrix([sp.diff(f, v) for v in (x, y, z)])
curl = lambda G: sp.Matrix([sp.diff(G[2], y) - sp.diff(G[1], z),
sp.diff(G[0], z) - sp.diff(G[2], x),
sp.diff(G[1], x) - sp.diff(G[0], y)])
div = lambda G: sp.diff(G[0], x) + sp.diff(G[1], y) + sp.diff(G[2], z)
print("curl(grad f) =", list(sp.simplify(curl(grad))))
print("div(curl F) =", sp.simplify(div(curl(F))))
print("Laplacian f =", sp.simplify(div(grad)))curl(grad f) = [0, 0, 0]
div(curl F) = 0
Laplacian f = 2*z*(3*x**2 + z**2)
On a grid: what a fluid solver does
A fluid simulation stores velocities on a grid and computes divergence and curl with finite differences (np.gradient uses central differences). Each frame, a "projection" step removes the divergence so the fluid stays incompressible: it solves a Poisson equation for a pressure and subtracts from the velocity. The curl ("vorticity") is used for visual effects such as vorticity confinement, which re-injects swirl lost to numerical smoothing.
import numpy as np
n = 101
xs = np.linspace(-1, 1, n)
X, Y = np.meshgrid(xs, xs, indexing="xy")
h = xs[1] - xs[0]
# a swirl plus an outward push: u = <-y + 0.3x, x + 0.3y>
U, V = -Y + 0.3 * X, X + 0.3 * Y
dU_dy, dU_dx = np.gradient(U, h) # np.gradient returns d/d(row), d/d(col) = d/dy, d/dx
dV_dy, dV_dx = np.gradient(V, h)
div = dU_dx + dV_dy
curl = dV_dx - dU_dy
print("divergence: min", round(float(div.min()), 6), "max", round(float(div.max()), 6), " (exact 0.6)")
print("curl : min", round(float(curl.min()), 6), "max", round(float(curl.max()), 6), " (exact 2)")divergence: min 0.6 max 0.6 (exact 0.6)
curl : min 2.0 max 2.0 (exact 2)
For a field that is linear in and , central differences are exact, so the grid values match the formulas. For real simulation data the finite differences are approximations, and their error shrinks as the grid gets finer.
In software
- Fluid simulation. "Stable fluids" solvers used in games and VFX alternate advection, adding forces, and the divergence-removing projection. Smoke, fire and water in real-time engines are built on this loop.
- Curl noise. Particle effects often move particles along the curl of a smooth noise field. Because , the flow is incompressible: particles swirl naturally without bunching up or thinning out.
- Graphics and imaging. The Laplacian is used for edge detection, sharpening, mesh smoothing and Poisson image editing (seamless cloning).
- Physics. Maxwell's equations are statements about the divergence and curl of the electric and magnetic fields.
Exercises
Exercise 1: Compute divergence and curl
For , find and .
Answer
, and .
Solution
Exercise 2: Source or sink?
Is the point a source or a sink of ? What about ?
Answer
, which is 4 at and 1 at : both are sources.
Solution
everywhere, so every point is a source.
Exercise 3: Find the constant
For which value of is divergence-free? Is it then also curl-free?
Answer
. Yes, its curl is , so it is both (it is the gradient of a harmonic function).
Solution
With , for , and .
Exercise 4: Vorticity on a grid (code)
Sample on a grid over , compute the scalar curl with np.gradient, and compare its value at the centre with the exact formula.
Answer
Exact , which is at ; the grid value agrees to about 4 decimals.
Solution
.
import numpy as np
xs = np.linspace(0, 2, 201)
h = xs[1] - xs[0]
X, Y = np.meshgrid(xs, xs, indexing="xy")
U, V = np.sin(Y), np.cos(X)
dU_dy, _ = np.gradient(U, h)
_, dV_dx = np.gradient(V, h)
curl = dV_dx - dU_dy
print(round(float(curl[100, 100]), 6), round(float(-np.sin(1) - np.cos(1)), 6))-1.38175 -1.381773
Test yourself
A fluid's velocity field has divergence 0 everywhere. The fluid is…
- at rest
- incompressible
- irrotational
- flowing in circles
What is the curl of any gradient field ?
- The zero vector
- itself
- It depends on
Why do particle effects use curl noise?
- It is faster than ordinary noise
- The curl of a field is divergence-free, so particles swirl without clumping
- It removes all rotation
- It makes particles move in straight lines