TExcelTAD
Home › Calculus Essentials › Divergence and Curl

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 ∇=⟨∂/∂x,∂/∂y,∂/∂z⟩\nabla = \langle \partial/\partial x, \partial/\partial y, \partial/\partial z \rangle 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 ∇f\nabla f scalar field vector field ⟨fx,fy,fz⟩\langle f_x, f_y, f_z \rangle
Divergence ∇⋅F\nabla \cdot F vector field scalar field ∂P/∂x+∂Q/∂y+∂R/∂z\partial P/\partial x + \partial Q/\partial y + \partial R/\partial z
Curl ∇×F\nabla \times F vector field vector field ⟨Ry−Qz,Pz−Rx,Qx−Py⟩\langle R_y - Q_z, P_z - R_x, Q_x - P_y \rangle

Here F=⟨P,Q,R⟩F = \langle P, Q, R \rangle. The curl is the cross product determinant from Dot and Cross Products with ∇\nabla in the middle row. In 2D, with F=⟨P,Q⟩F = \langle P, Q \rangle, the divergence is Px+QyP_x + Q_y and the scalar curl is Qx−PyQ_x - P_y (the zz-component of the 3D curl).

Divergence: sources and sinks

Imagine FF 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:

  • div⁡F>0\operatorname{div} F > 0: a source, more flows out than in (fluid is being created, or gas is expanding).
  • div⁡F<0\operatorname{div} F < 0: a sink, fluid is draining or being compressed.
  • div⁡F=0\operatorname{div} F = 0 everywhere: the field is incompressible (divergence-free). What flows in must flow out.

For F=⟨x,y⟩F = \langle x, y \rangle the divergence is 1+1=21 + 1 = 2 everywhere: every point is a little source. For the vortex ⟨−y,x⟩\langle -y, x \rangle it is 0+0=00 + 0 = 0: 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 F=⟨−y,x,0⟩F = \langle -y, x, 0 \rangle the curl is ⟨0,0,2⟩\langle 0, 0, 2 \rangle: it rotates counter-clockwise about the zz-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. F=⟨−y,x⟩/(x2+y2)F = \langle -y, x \rangle / (x^2 + y^2) 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 F=⟨y,0⟩F = \langle y, 0 \rangle (a river faster at the top) has curl ⟨0,0,−1⟩\langle 0, 0, -1 \rangle: a paddle wheel in it spins.
python · try it yourself
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:

∇×(∇f)=0the curl of a gradient is zero∇⋅(∇×F)=0the divergence of a curl is zero\begin{aligned} \nabla \times (\nabla f) &= 0 && \text{the curl of a gradient is zero} \\ \nabla \cdot (\nabla \times F) &= 0 && \text{the divergence of a curl is zero} \end{aligned}

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: ∇⋅(∇f)=fxx+fyy+fzz\nabla \cdot (\nabla f) = f_{xx} + f_{yy} + f_{zz} is the Laplacian ∇2f\nabla^2 f, which appears in heat flow, diffusion, image blurring and the pressure solve below.

python · try it yourself
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 ∇2p=∇⋅u\nabla^2 p = \nabla \cdot u for a pressure pp and subtracts ∇p\nabla p from the velocity. The curl ("vorticity") is used for visual effects such as vorticity confinement, which re-injects swirl lost to numerical smoothing.

python · try it yourself
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 xx and yy, 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 div⁡(curl⁡)=0\operatorname{div}(\operatorname{curl}) = 0, the flow is incompressible: particles swirl naturally without bunching up or thinning out.
  • Graphics and imaging. The Laplacian ∇2\nabla^2 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 F=⟨x2y,2yz,xz2⟩F = \langle x^2y, 2yz, xz^2 \rangle, find ∇⋅F\nabla \cdot F and ∇×F\nabla \times F.

Answer

∇⋅F=2xy+2z+2xz\nabla \cdot F = 2xy + 2z + 2xz, and ∇×F=⟨−2y,−z2,−x2⟩\nabla \times F = \langle -2y, -z^2, -x^2 \rangle.

Solution
div⁡=∂(x2y)∂x+∂(2yz)∂y+∂(xz2)∂z=2xy+2z+2xzcurl⁡=⟨Ry−Qz,Pz−Rx,Qx−Py⟩=⟨0−2y, 0−z2, 0−x2⟩=⟨−2y,−z2,−x2⟩\begin{aligned} \operatorname{div} &= \frac{\partial(x^2y)}{\partial x} + \frac{\partial(2yz)}{\partial y} + \frac{\partial(xz^2)}{\partial z} = 2xy + 2z + 2xz \\ \operatorname{curl} &= \langle R_y - Q_z, P_z - R_x, Q_x - P_y \rangle \\ &= \langle 0 - 2y,\ 0 - z^2,\ 0 - x^2 \rangle = \langle -2y, -z^2, -x^2 \rangle \end{aligned}

Exercise 2: Source or sink?

Is the point (1,−1)(1, -1) a source or a sink of F=⟨x3,y⟩F = \langle x^3, y \rangle? What about (0,0)(0, 0)?

Answer

div⁡F=3x2+1\operatorname{div} F = 3x^2 + 1, which is 4 at (1,−1)(1, -1) and 1 at (0,0)(0, 0): both are sources.

Solution

div⁡F=∂(x3)/∂x+∂(y)/∂y=3x2+1≥1>0\operatorname{div} F = \partial(x^3)/\partial x + \partial(y)/\partial y = 3x^2 + 1 \ge 1 > 0 everywhere, so every point is a source.

Exercise 3: Find the constant

For which value of aa is F=⟨ax+y,x−3y,2z⟩F = \langle ax + y, x - 3y, 2z \rangle divergence-free? Is it then also curl-free?

Answer

a=1a = 1. Yes, its curl is ⟨0,0,0⟩\langle 0, 0, 0 \rangle, so it is both (it is the gradient of a harmonic function).

Solution
div⁡F=a−3+2=a−1=0⇒a=1curl⁡F=⟨0−0, 0−0, 1−1⟩=0\begin{aligned} \operatorname{div} F &= a - 3 + 2 = a - 1 = 0 \quad\Rightarrow\quad a = 1 \\ \operatorname{curl} F &= \langle 0 - 0,\ 0 - 0,\ 1 - 1 \rangle = 0 \end{aligned}

With a=1a = 1, F=∇φF = \nabla\varphi for φ=x2/2+xy−3y2/2+z2\varphi = x^2/2 + xy - 3y^2/2 + z^2, and ∇2φ=1−3+2=0\nabla^2\varphi = 1 - 3 + 2 = 0.

Exercise 4: Vorticity on a grid (code)

Sample F=⟨sin⁡y,cos⁡x⟩F = \langle \sin y, \cos x \rangle on a 201×201201 \times 201 grid over [0,2]×[0,2][0, 2] \times [0, 2], compute the scalar curl with np.gradient, and compare its value at the centre (1,1)(1, 1) with the exact formula.

Answer

Exact curl⁡=−sin⁡x−cos⁡y\operatorname{curl} = -\sin x - \cos y, which is −sin⁡1−cos⁡1≈−1.3818-\sin 1 - \cos 1 \approx -1.3818 at (1,1)(1, 1); the grid value agrees to about 4 decimals.

Solution

Qx−Py=∂(cos⁡x)/∂x−∂(sin⁡y)/∂y=−sin⁡x−cos⁡yQ_x - P_y = \partial(\cos x)/\partial x - \partial(\sin y)/\partial y = -\sin x - \cos y.

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

  1. at rest
  2. incompressible
  3. irrotational
  4. flowing in circles

What is the curl of any gradient field ∇f\nabla f?

  1. ∇2f\nabla^2 f
  2. The zero vector
  3. ∇f\nabla f itself
  4. It depends on ff

Why do particle effects use curl noise?

  1. It is faster than ordinary noise
  2. The curl of a field is divergence-free, so particles swirl without clumping
  3. It removes all rotation
  4. It makes particles move in straight lines