Flow Lab

Syllabus / Module 2: Multivariable & Vector Calculus

Lesson 2.11

The Laplacian & Vector Identities

∇², the Laplacian, as 'difference from the neighborhood average'; key vector identities; the Helmholtz decomposition of a vector field.

Written byClaude Opus

🌊
Why this matters for simulation: Diffusion, viscosity and the pressure projection are all Laplacians; Helmholtz–Hodge is the heart of Stam's solver.

By the end of this lesson you can…

  • Compute the Laplacian of a scalar field and read its sign as centre value versus neighbourhood average.
  • Derive the 5-point discrete Laplacian from Taylor series and apply it to grid data by hand and in code.
  • Recognize harmonic functions, state the mean value property and maximum principle, and relax a grid toward a harmonic function with Jacobi iteration.
  • Compute the vector Laplacian and use the key identities curl grad = 0, div curl = 0 and curl curl = grad div − Δ.
  • State the Helmholtz–Hodge decomposition and split a vector field into gradient and divergence-free parts by solving a Poisson equation.
  • Explain the projection step of Stam's Stable Fluids as a Helmholtz–Hodge decomposition, with pressure as the potential.
On this page
·
  1. The Laplacian: divergence of the gradient
  2. Worked example: computing Laplacians
  3. The Laplacian as “neighbourhood average minus centre”
  4. The 5-point discrete Laplacian
  5. Worked example: applying the stencil
  6. Harmonic functions
  7. The discrete picture and Jacobi relaxation
  8. Worked example: four unknowns by hand
  9. Implement it: the stencil and Jacobi relaxation
  10. The vector Laplacian
  11. Key vector identities
  12. Worked example: checking curl curl = grad div − Δ
  13. The Helmholtz–Hodge decomposition
  14. Worked example: a decomposition by hand
  15. Stam’s projection step is Helmholtz–Hodge
  16. Implement it: the projection
  17. Key Takeaways

Open Jos Stam’s The Art of Fluid Animation to the core solver and you’ll find one time step built from four moves: add forces, diffuse, advect, project. Advection is particle tracing, which is the ODE integration you already know from 2.6 Vector Fields, Flow Lines & Streamlines. The other moves are different. Diffuse and project both come down to solving an equation built from a single operator, the Laplacian ∇2\nabla^2. This lesson closes the vector calculus module by putting together everything so far. You take the gradient (2.3 Gradient & Directional Derivatives), then the divergence (2.7 Divergence: Sources & Sinks), and the composition, ∇⋅∇\nabla\cdot\nabla, turns out to be the most important operator in mathematical physics. Then we use the identities from 2.8 Curl & Vorticity to prove the theorem that makes incompressible fluid solvers possible: every velocity field splits cleanly into a part that compresses and a part that swirls.

▶ Video The Laplacian: divergence of the gradient, and neighbour average minus centrecaptions only
Transcript
  1. This is the last lesson of the vector calculus module, and it is the one where the whole module pays off inside a fluid solver.
  2. Here is one time step of Jos Stam's stable fluids solver: add forces, diffuse, advect, and project.
  3. Two of those four steps, diffuse and project, are really the same job: solving an equation built from one operator, the Laplacian. Let's understand that operator properly.
  4. The Laplacian of a function f is the divergence of its gradient. Take the gradient, which is a vector field, then measure how much that field spreads out. In coordinates it is f x x plus f y y.
  5. Picture a hill. Its gradient points uphill, so near the summit every arrow points inward. Arrows converging means negative divergence. At a peak, the Laplacian is negative.
  6. Flip it into a valley and the gradient points outward, away from the bottom. That's a source, so the Laplacian is positive. The Laplacian measures how much a point sits below its surroundings.
  7. Here is the cleanest way to see it. Take a point on a curve and its two neighbors, a distance h to each side. Average the neighbors: that's the middle of the dashed chord.
  8. At a peak, the neighbor average sits below the center. The gap, in red, is the second derivative times h squared over two. That comes straight from Taylor's theorem.
  9. Shrink h and the gap shrinks like h squared, but the ratio settles down to the second derivative. A second derivative is a comparison between a point and its neighborhood.
  10. In two dimensions, add the x and y versions. On a grid with spacing h, each point has four neighbors: east, west, north and south.
  11. The five point Laplacian adds up the four neighbors, subtracts four times the center, and divides by h squared. Rearranged, it is four over h squared, times the neighbor average minus the center.
  12. With these numbers and h equal to one: eighteen minus eight is ten. The neighbors average four and a half, well above the center value of two, so this point is a little valley and the Laplacian is positive.
  13. Now the payoff. The heat equation says u t equals kappa times the Laplacian: every temperature drifts toward the average of its neighbors. Here is a noisy grid, with the top edge held hot.
  14. Repeatedly replace each interior value by the average of its four neighbors. This is called Jacobi relaxation. Watch the rough noise vanish in a handful of sweeps.
  15. The smooth, large scale part takes far longer. Hundreds of sweeps later the grid settles into a steady state that no longer changes.
  16. In that steady state, every value equals the average of its neighbors. The Laplacian is zero everywhere inside. A function with zero Laplacian is called harmonic.
  17. Harmonic functions are as smooth and boring as possible. The value at a point equals its average over any circle around it, so there can be no peaks or pits inside: the extremes are always on the boundary.
  18. Jacobi relaxation kills wiggles fast but crawls on smooth errors, needing roughly N squared sweeps on an N by N grid. That one fact is why Stam's solver does twenty sweeps and hopes, and why serious codes use multigrid.
  19. So: the Laplacian is the divergence of the gradient, and on a grid it is just neighbor average minus center. Next, we use it to split any velocity field into two pieces, the secret behind Stam's projection step.

The Laplacian: divergence of the gradient

For a scalar field f(x,y)f(x, y) or f(x,y,z)f(x, y, z), the Laplacian is

Δf  =  ∇2f  =  ∇⋅(∇f)  =  ∂2f∂x2+∂2f∂y2  (+ ∂2f∂z2).\Delta f \;=\; \nabla^2 f \;=\; \nabla\cdot(\nabla f) \;=\; \frac{\partial^2 f}{\partial x^2} + \frac{\partial^2 f}{\partial y^2} \;\left(+\, \frac{\partial^2 f}{\partial z^2}\right).

The two notations Δ\Delta and ∇2\nabla^2 mean the same thing. Physicists write ∇2\nabla^2 (“del squared”) as a reminder that it is ∇⋅∇\nabla\cdot\nabla. Mathematicians and numerical analysts usually write Δ\Delta. The derivation is one line: ∇f=(fx,fy,fz)\nabla f = (f_x, f_y, f_z), and the divergence of a field (P,Q,R)(P, Q, R) is Px+Qy+RzP_x + Q_y + R_z, so ∇⋅∇f=fxx+fyy+fzz\nabla\cdot\nabla f = f_{xx} + f_{yy} + f_{zz}. The Laplacian takes in a scalar field and gives back a scalar field.

What the sign means. The gradient points uphill. Near the top of a hill every gradient arrow points inward, toward the summit. Arrows converging on a point means negative divergence (a sink, in the language of 2.7 Divergence: Sources & Sinks). So at a peak, Δf<0\Delta f \lt 0. In a valley the gradient points outward, away from the bottom, so Δf>0\Delta f \gt 0. On a tilted plane the gradient is constant and Δf=0\Delta f = 0.

The divergence theorem from 2.10 Green, Stokes & the Divergence Theorem makes this precise. For a small disk DD around a point,

∬DΔf dA  =  ∮∂D∇f⋅n^ ds.\iint_D \Delta f \, dA \;=\; \oint_{\partial D} \nabla f\cdot \hat n \, ds .

The right side is the net “uphill-ness” pointing out of the disk. If the boundary circle is, on average, higher than the centre, the gradient points outward across it and the integral is positive.

Worked example: computing Laplacians

Compute Δf\Delta f for (a) f=x2+y2f = x^2 + y^2, (b) f=x2−y2f = x^2 - y^2, (c) f=e−(x2+y2)f = e^{-(x^2+y^2)} at the origin, (d) f=sin⁡xsin⁡yf = \sin x \sin y.

(a) fxx=2f_{xx} = 2 and fyy=2f_{yy} = 2, so Δf=4\Delta f = 4 everywhere. The paraboloid is a bowl: every point sits below the average of its surroundings.

(b) fxx=2f_{xx} = 2 and fyy=−2f_{yy} = -2, so Δf=0\Delta f = 0. This saddle curves up in xx and down in yy by exactly the same amount, and the two cancel.

(c) fx=−2x e−r2f_x = -2x\,e^{-r^2} and fxx=(4x2−2) e−r2f_{xx} = (4x^2 - 2)\,e^{-r^2}. By symmetry fyy=(4y2−2) e−r2f_{yy} = (4y^2 - 2)\,e^{-r^2}, so Δf=(4r2−4) e−r2\Delta f = (4r^2 - 4)\,e^{-r^2}. At the origin Δf=−4<0\Delta f = -4 \lt 0, a peak. For r>1r \gt 1 the Laplacian turns positive: out on the skirts, the surface curves upward like a bowl.

(d) fxx=−sin⁡xsin⁡yf_{xx} = -\sin x \sin y and fyy=−sin⁡xsin⁡yf_{yy} = -\sin x\sin y, so Δf=−2sin⁡xsin⁡y=−2f\Delta f = -2 \sin x \sin y = -2f. The Laplacian just multiplies this function by −2-2, so sin⁡xsin⁡y\sin x \sin y is an eigenfunction of Δ\Delta with eigenvalue −2-2. Hold on to that idea: in 8.3 Fourier Series and 9.3 Solving the Heat Equation: Separation of Variables, sines and cosines solve the heat equation precisely because the Laplacian only rescales them.

✅ Concept check

At a strict local maximum of a smooth function $f(x,y)$ (a hilltop), the Laplacian $\Delta f$ is…

The Laplacian as “neighbourhood average minus centre”

The sign rule hides an exact statement. Let fˉ(r)\bar f(r) be the average of ff over a circle of radius rr centred at a point p⃗\vec p. Expand ff in a Taylor series around p⃗\vec p (2.4 Taylor Series in One and Several Variables). The point at angle θ\theta on the circle is p⃗+r(cos⁡θ,sin⁡θ)\vec p + r(\cos\theta, \sin\theta), so

f=f(p⃗)+r(fxcos⁡θ+fysin⁡θ)+r22(fxxcos⁡2θ+2fxycos⁡θsin⁡θ+fyysin⁡2θ)+O(r3).f = f(\vec p) + r(f_x\cos\theta + f_y\sin\theta) + \frac{r^2}{2}\left(f_{xx}\cos^2\theta + 2f_{xy}\cos\theta\sin\theta + f_{yy}\sin^2\theta\right) + O(r^3).

Now average over θ∈[0,2π)\theta \in [0, 2\pi). The averages of cos⁡θ\cos\theta, sin⁡θ\sin\theta and cos⁡θsin⁡θ\cos\theta\sin\theta are all zero, and cos⁡2θ\cos^2\theta and sin⁡2θ\sin^2\theta each average to 12\tfrac12. Everything except the curvature terms drops out:

fˉ(r)  =  f(p⃗)+r24 Δf(p⃗)+O(r4)⟹Δf(p⃗)=lim⁡r→04r2(fˉ(r)−f(p⃗)).\bar f(r) \;=\; f(\vec p) + \frac{r^2}{4}\,\Delta f(\vec p) + O(r^4) \qquad\Longrightarrow\qquad \Delta f(\vec p) = \lim_{r\to 0} \frac{4}{r^2}\Big(\bar f(r) - f(\vec p)\Big).

(The r3r^3 terms also average to zero, by odd symmetry, which is why the error is O(r4)O(r^4).) In 3D the same computation over a sphere gives fˉ=f+r26Δf+⋯\bar f = f + \tfrac{r^2}{6}\Delta f + \cdots.

This is why the Laplacian shows up wherever things even out. The heat equation ut=κ Δuu_t = \kappa\,\Delta u, derived in 9.2 The Heat (Diffusion) Equation, says: a point that is colder than its neighbourhood average warms up, and one that is hotter cools down, at a rate proportional to the difference.

The 5-point discrete Laplacian

On a grid you don’t have derivatives, only values. Taylor series turns the averaging picture into a formula. In 1D, with spacing hh,

f(x±h)=f(x)±hf′(x)+h22f′′(x)±h36f′′′(x)+h424f′′′′(x)+⋯f(x \pm h) = f(x) \pm h f'(x) + \frac{h^2}{2}f''(x) \pm \frac{h^3}{6}f'''(x) + \frac{h^4}{24}f''''(x) + \cdots

Adding the two expansions cancels every odd term:

f(x+h)+f(x−h)−2f(x)=h2f′′(x)+h412f′′′′(x)+⋯⟹f′′(x)=f(x+h)−2f(x)+f(x−h)h2+O(h2).f(x+h) + f(x-h) - 2f(x) = h^2 f''(x) + \frac{h^4}{12}f''''(x) + \cdots \quad\Longrightarrow\quad f''(x) = \frac{f(x+h) - 2f(x) + f(x-h)}{h^2} + O(h^2).

Do this once in xx and once in yy, and add. Writing fCf_C for the centre value and fE,fW,fN,fSf_E, f_W, f_N, f_S for its east, west, north and south neighbours, the 5-point Laplacian is

Δhf  =  fE+fW+fN+fS−4fCh2  =  4h2(fE+fW+fN+fS4−fC).\Delta_h f \;=\; \frac{f_E + f_W + f_N + f_S - 4 f_C}{h^2} \;=\; \frac{4}{h^2}\left(\frac{f_E + f_W + f_N + f_S}{4} - f_C\right).

The second form is the averaging picture again, now with the four nearest neighbours standing in for the circle. As a picture, the stencil of weights is

1h2[11−411].\frac{1}{h^2}\begin{bmatrix} & 1 & \\ 1 & -4 & 1 \\ & 1 & \end{bmatrix}.

The error is O(h2)O(h^2): halve the grid spacing and the error drops by a factor of 4. This is a second-order method, in the sense of 4.1 Local Error, Global Error & Order. The full theory of finite-difference stencils is in 10.1 Finite Differences: Discretizing Derivatives.

Worked example: applying the stencil

(a) On a grid with h=1h = 1, a cell has value 2 and its neighbours are 3 (east), 5 (west), 4 (north) and 6 (south). Find Δhf\Delta_h f. (b) Show that the stencil is exact for f=x2+y2f = x^2 + y^2 for every hh. (c) Apply it to f=sin⁡xsin⁡yf = \sin x\sin y.

(a) Δhf=3+5+4+6−4⋅2=18−8=10\Delta_h f = 3 + 5 + 4 + 6 - 4\cdot 2 = 18 - 8 = 10. The neighbour average, 4.5, is well above the centre value 2, so this point is a small valley.

(b) In xx: (x+h)2−2x2+(x−h)2=2h2(x+h)^2 - 2x^2 + (x-h)^2 = 2h^2, so the xx part contributes 2h2/h2=22h^2/h^2 = 2. The yy part contributes another 2, giving Δhf=4=Δf\Delta_h f = 4 = \Delta f exactly. The error term involves the fourth derivatives, which are zero for a quadratic.

(c) Using sin⁡(x+h)+sin⁡(x−h)=2sin⁡xcos⁡h\sin(x+h) + \sin(x-h) = 2\sin x\cos h,

Δh(sin⁡xsin⁡y)=2cos⁡h−2h2sin⁡xsin⁡y⏟x part+2cos⁡h−2h2sin⁡xsin⁡y⏟y part=−4 (1−cos⁡h)h2sin⁡xsin⁡y=−8sin⁡2(h/2)h2 sin⁡xsin⁡y,\Delta_h(\sin x\sin y) = \underbrace{\frac{2\cos h - 2}{h^2}\sin x \sin y}_{x\text{ part}} + \underbrace{\frac{2\cos h - 2}{h^2}\sin x \sin y}_{y\text{ part}} = -\frac{4\,(1-\cos h)}{h^2}\sin x\sin y = -\frac{8\sin^2(h/2)}{h^2}\,\sin x \sin y ,

using 1−cos⁡h=2sin⁡2(h/2)1 - \cos h = 2\sin^2(h/2). Since sin⁡(h/2)≈h/2−h3/48\sin(h/2) \approx h/2 - h^3/48, the factor is 8sin⁡2(h/2)h2≈2−h26\frac{8\sin^2(h/2)}{h^2} \approx 2 - \frac{h^2}{6}. That is the exact eigenvalue −2-2 plus an O(h2)O(h^2) error, just as promised. Sines are eigenvectors of the discrete Laplacian too, which is the starting point of 10.3 Von Neumann Stability Analysis stability analysis.

✅ Concept check

On a grid with spacing $h = 0.5$, a cell has value $1$ and its four neighbours are $2, 2, 0, 1$. What is the 5-point Laplacian $\Delta_h f$ at that cell?

Harmonic functions

A function with Δu=0\Delta u = 0 is called harmonic. You have already met several:

  • linear functions ax+by+cax + by + c;
  • x2−y2x^2 - y^2 and xyxy (saddles);
  • excos⁡ye^x\cos y and exsin⁡ye^x \sin y;
  • ln⁡(x2+y2)\ln(x^2+y^2) in 2D and 1/r1/r in 3D, away from the origin.

In 2D, the real and imaginary parts of any complex-analytic function are harmonic: x2−y2x^2 - y^2 and 2xy2xy come from z2z^2, and excos⁡ye^x\cos y comes from eze^z. That’s the engine behind classical airfoil theory (12.1 Airfoils, Circulation & Lift).

Harmonic functions are as smooth and featureless as a function can be, and two properties make that precise.

The first follows from our expansion. Differentiating uˉ(r)\bar u(r) and applying the divergence theorem to the disk gives uˉ′(r)=12πr∬DrΔu dA\bar u'(r) = \frac{1}{2\pi r}\iint_{D_r}\Delta u\, dA, which is zero when uu is harmonic. So uˉ(r)\bar u(r) doesn’t depend on rr and equals its limit u(p⃗)u(\vec p). The second follows from the first: an interior maximum would be the average of values around it, all of which are smaller. That’s impossible.

Physically, harmonic functions are steady states. They give the temperature in a plate once it has stopped changing (ut=0u_t = 0 in the heat equation), the shape of a soap film stretched over a slightly bent wire, the electrostatic potential in empty space, and the velocity potential of an ideal fluid in 11.7 Potential Flow & Stream Functions. Solving Δu=0\Delta u = 0 with given boundary values is the Laplace equation, the subject of 9.6 Laplace's Equation: Rectangles & Disks.

The discrete picture and Jacobi relaxation

On a grid, "Δhu=0\Delta_h u = 0" says uC=14(uE+uW+uN+uS)u_C = \tfrac14(u_E + u_W + u_N + u_S). Every value is the average of its four neighbours. That suggests an algorithm. Hold the boundary values fixed, start from any guess inside, and repeatedly replace every interior value by the average of its neighbours, computed from the old values:

ui,j(k+1)=14(ui+1,j(k)+ui−1,j(k)+ui,j+1(k)+ui,j−1(k)).u^{(k+1)}_{i,j} = \tfrac14\left(u^{(k)}_{i+1,j} + u^{(k)}_{i-1,j} + u^{(k)}_{i,j+1} + u^{(k)}_{i,j-1}\right).

This is Jacobi relaxation. Notice that it is the same thing as u(k+1)=u(k)+h24Δhu(k)u^{(k+1)} = u^{(k)} + \tfrac{h^2}{4}\Delta_h u^{(k)}, which is a forward-Euler step of the heat equation with the largest stable time step. Relaxation is just diffusion run until nothing changes any more.

Worked example: four unknowns by hand

A 4×44\times4 grid of points has its two top-middle boundary points held at 11 and every other boundary point at 00. Find the four interior values of the discrete harmonic function, then run Jacobi from zero.

By left–right symmetry the two top interior values are equal (call them aa) and so are the two bottom ones (call them bb). The top-left interior point has neighbours 11 (above), 00 (left), aa (right) and bb (below):

a=14(1+0+a+b)  ⟹  3a−b=1.a = \tfrac14(1 + 0 + a + b) \;\Longrightarrow\; 3a - b = 1 .

The bottom-left interior point has neighbours aa (above), 00 (left), bb (right) and 00 (below):

b=14(a+0+b+0)  ⟹  a=3b.b = \tfrac14(a + 0 + b + 0) \;\Longrightarrow\; a = 3b .

Substituting gives 9b−b=19b - b = 1, so b=18b = \tfrac18 and a=38a = \tfrac38. Check the mean value property at aa: 14(1+0+38+18)=14⋅32=38\tfrac14(1 + 0 + \tfrac38 + \tfrac18) = \tfrac14\cdot\tfrac32 = \tfrac38. ✓

Jacobi from all zeros gives:

sweepaabb
000
114(1+0+0+0)=0.25\tfrac14(1+0+0+0) = 0.2500
214(1+0.25+0)=0.3125\tfrac14(1 + 0.25 + 0) = 0.312514(0.25+0)=0.0625\tfrac14(0.25 + 0) = 0.0625
314(1+0.3125+0.0625)=0.34375\tfrac14(1 + 0.3125 + 0.0625) = 0.3437514(0.3125+0.0625)=0.09375\tfrac14(0.3125 + 0.0625) = 0.09375
∞0.3750.3750.1250.125

It converges, but each sweep only lets information travel one cell. As the extreme values must, the maximum 11 and minimum 00 sit on the boundary.

◆ Interactive Relaxation lab: paint heat, then let the Laplacian average it away

Loading simulation…

Things to try in the lab:

  1. Press Relax on the smooth blobs, then load rough noise and relax again. The noise is gone in a few sweeps, while the big blobs linger for hundreds. Averaging kills short wavelengths fast and long wavelengths slowly.
  2. Tick Show Δu. Peaks of uu show up blue (Δu<0\Delta u \lt 0) and valleys red. Relax and watch Δu\Delta u fade toward zero everywhere.
  3. Set the boundary to top edge +1 or left +1, right −1 and relax until “max |h²Δu|” is tiny. You now have a harmonic function. Hover over cells: the probe shows each value equals its neighbour average. Check that the interior range never exceeds the edge range (maximum principle).
  4. Load two held points, or paint your own holds, and relax. This is the electrostatic potential of two charged electrodes.
  5. Push α above 1. The update overshoots the average, and a checkerboard pattern explodes. This is the explicit heat-equation stability limit κΔt≤h2/4\kappa\Delta t \le h^2/4, which you’ll analyse in 10.2 Explicit Methods for the Heat Equation.
✏️ Pen & paperLaplacians and harmonic functions●○○

Compute the Laplacian of each function and decide whether it is harmonic (where defined).

  1. f=x3−3xy2f = x^3 - 3xy^2
  2. f=excos⁡yf = e^{x}\cos y
  3. f=ln⁡(x2+y2)f = \ln(x^2 + y^2), for (x,y)≠(0,0)(x,y) \ne (0,0)
  4. f=x2+y2+z2f = x^2 + y^2 + z^2 in 3D
  5. f=1/r=(x2+y2+z2)−1/2f = 1/r = (x^2+y^2+z^2)^{-1/2} in 3D, for r≠0r \ne 0
Show worked solution
  1. fxx=6xf_{xx} = 6x and fyy=−6xf_{yy} = -6x, so Δf=0\Delta f = 0. Harmonic. (It is the real part of z3z^3.)
  2. fxx=excos⁡yf_{xx} = e^x\cos y and fyy=−excos⁡yf_{yy} = -e^x\cos y, so Δf=0\Delta f = 0. Harmonic.
  3. fx=2xx2+y2f_x = \dfrac{2x}{x^2+y^2}, so fxx=2(x2+y2)−2x⋅2x(x2+y2)2=2y2−2x2(x2+y2)2f_{xx} = \dfrac{2(x^2+y^2) - 2x\cdot 2x}{(x^2+y^2)^2} = \dfrac{2y^2 - 2x^2}{(x^2+y^2)^2}. By symmetry fyy=2x2−2y2(x2+y2)2f_{yy} = \dfrac{2x^2 - 2y^2}{(x^2+y^2)^2}. The sum is 00, so it is harmonic away from the origin. (This is the potential of a 2D point source.)
  4. Each second partial is 22, so Δf=6\Delta f = 6. Not harmonic: every point sits below its spherical average.
  5. fx=−x r−3f_x = -x\,r^{-3} and fxx=−r−3+3x2r−5f_{xx} = -r^{-3} + 3x^2 r^{-5}. Adding the three directions gives Δf=−3r−3+3(x2+y2+z2)r−5=−3r−3+3r−3=0\Delta f = -3r^{-3} + 3(x^2+y^2+z^2)r^{-5} = -3r^{-3} + 3r^{-3} = 0. Harmonic for r≠0r \ne 0. (This is the Newtonian and Coulomb potential.)
✏️ Pen & paperTesting the mean value property●●○
  1. Let u=x2−y2u = x^2 - y^2. Compute the average of uu over the circle of radius rr centred at (a,b)(a, b), and confirm that it equals u(a,b)u(a,b) for every rr.
  2. Repeat for f=x2+y2f = x^2 + y^2. Show that the average is f(a,b)+r2f(a,b) + r^2, and explain why this matches fˉ=f+r24Δf\bar f = f + \tfrac{r^2}{4}\Delta f exactly, with no higher-order terms.
Show worked solution
  1. Parametrize the circle as x=a+rcos⁡θx = a + r\cos\theta, y=b+rsin⁡θy = b + r\sin\theta. Then u=a2+2arcos⁡θ+r2cos⁡2θ−b2−2brsin⁡θ−r2sin⁡2θu = a^2 + 2ar\cos\theta + r^2\cos^2\theta - b^2 - 2br\sin\theta - r^2\sin^2\theta. Averaging over θ\theta, the cos⁡θ\cos\theta and sin⁡θ\sin\theta terms vanish and cos⁡2θ\cos^2\theta and sin⁡2θ\sin^2\theta both average to 12\tfrac12, so uˉ=a2−b2+r22−r22=a2−b2=u(a,b)\bar u = a^2 - b^2 + \tfrac{r^2}{2} - \tfrac{r^2}{2} = a^2 - b^2 = u(a,b). ✓ This holds for every rr.
  2. In the same way, fˉ=a2+b2+r22+r22=f(a,b)+r2\bar f = a^2 + b^2 + \tfrac{r^2}{2} + \tfrac{r^2}{2} = f(a,b) + r^2. Since Δf=4\Delta f = 4, the formula gives f+r24⋅4=f+r2f + \tfrac{r^2}{4}\cdot 4 = f + r^2. The match is exact because the remainder terms in the Taylor expansion involve third and higher derivatives, which all vanish for a quadratic.

Implement it: the stencil and Jacobi relaxation

🧪 Coding exerciseExercise: 5-point Laplacian and a Jacobi sweep
⌘/Ctrl + Enter

The vector Laplacian

The Laplacian of a vector field, in Cartesian coordinates, is taken one component at a time:

ΔF⃗=(ΔF1,  ΔF2,  ΔF3).\Delta \vec F = \big(\Delta F_1,\; \Delta F_2,\; \Delta F_3\big).

This is the operator in the viscous term νΔu⃗\nu\Delta\vec u of Navier–Stokes: each velocity component diffuses like a temperature. That’s why Stam’s code simply calls diffuse once for u and once for v.

Key vector identities

Every identity below comes from writing out components and using one fact: mixed partial derivatives commute (Clairaut’s theorem, from 2.1 Functions of Several Variables & Partial Derivatives). Here f,gf, g are scalar fields and F⃗,A⃗\vec F, \vec A vector fields, all smooth.

#IdentityIn words
1∇×(∇f)=0⃗\nabla\times(\nabla f) = \vec 0gradients never swirl
2∇⋅(∇×A⃗)=0\nabla\cdot(\nabla\times\vec A) = 0curls never pile up
3∇⋅(∇f)=Δf\nabla\cdot(\nabla f) = \Delta fdefinition of the Laplacian
4∇×(∇×F⃗)=∇(∇⋅F⃗)−ΔF⃗\nabla\times(\nabla\times\vec F) = \nabla(\nabla\cdot\vec F) - \Delta\vec F“curl curl = grad div − Laplacian”
5∇⋅(fF⃗)=∇f⋅F⃗+f ∇⋅F⃗\nabla\cdot(f\vec F) = \nabla f\cdot\vec F + f\,\nabla\cdot\vec Fproduct rule for divergence
6∇×(fF⃗)=∇f×F⃗+f ∇×F⃗\nabla\times(f\vec F) = \nabla f\times\vec F + f\,\nabla\times\vec Fproduct rule for curl
7$(\vec u\cdot\nabla)\vec u = \nabla\big(\tfrac12\vec u

Proof of 1. The xx-component of ∇×∇f\nabla\times\nabla f is ∂y(fz)−∂z(fy)=fzy−fyz=0\partial_y(f_z) - \partial_z(f_y) = f_{zy} - f_{yz} = 0, and the other two components work the same way.

Proof of 2. With ∇×A⃗=(A3,y−A2,z,  A1,z−A3,x,  A2,x−A1,y)\nabla\times\vec A = (A_{3,y} - A_{2,z},\; A_{1,z} - A_{3,x},\; A_{2,x} - A_{1,y}),

∇⋅(∇×A⃗)=(A3,yx−A2,zx)+(A1,zy−A3,xy)+(A2,xz−A1,yz)=0,\nabla\cdot(\nabla\times\vec A) = (A_{3,yx} - A_{2,zx}) + (A_{1,zy} - A_{3,xy}) + (A_{2,xz} - A_{1,yz}) = 0,

because the terms cancel in pairs. In 2D the same facts read: the scalar curl of a gradient, ∂xfy−∂yfx\partial_x f_y - \partial_y f_x, is zero, and any field of the form (ψy,−ψx)(\psi_y, -\psi_x) built from a stream function ψ\psi automatically has zero divergence.

Identity 7 matters later: it lets you write the Euler equations in terms of vorticity and derive Bernoulli’s principle (11.3 Momentum: Pressure & the Euler Equations, 11.6 Vorticity Dynamics & Circulation). Identity 4 has a nice consequence for incompressible flow. If ∇⋅u⃗=0\nabla\cdot\vec u = 0, then Δu⃗=−∇×ω⃗\Delta\vec u = -\nabla\times\vec\omega with ω⃗=∇×u⃗\vec\omega = \nabla\times\vec u, so viscosity acts by diffusing vorticity.

Worked example: checking curl curl = grad div − Δ

Verify identity 4 for F⃗=(x2y,  yz2,  xz)\vec F = (x^2 y,\; y z^2,\; x z).

Left side. First compute the curl:

∇×F⃗=(∂y(xz)−∂z(yz2),    ∂z(x2y)−∂x(xz),    ∂x(yz2)−∂y(x2y))=(−2yz,  −z,  −x2).\nabla\times\vec F = \big(\partial_y(xz) - \partial_z(yz^2),\;\; \partial_z(x^2y) - \partial_x(xz),\;\; \partial_x(yz^2) - \partial_y(x^2y)\big) = (-2yz,\; -z,\; -x^2).

Call that G⃗\vec G. Then take its curl:

∇×G⃗=(∂y(−x2)−∂z(−z),    ∂z(−2yz)−∂x(−x2),    ∂x(−z)−∂y(−2yz))=(1,  2x−2y,  2z).\nabla\times\vec G = \big(\partial_y(-x^2) - \partial_z(-z),\;\; \partial_z(-2yz) - \partial_x(-x^2),\;\; \partial_x(-z) - \partial_y(-2yz)\big) = (1,\; 2x - 2y,\; 2z).

Right side. ∇⋅F⃗=2xy+z2+x\nabla\cdot\vec F = 2xy + z^2 + x, so ∇(∇⋅F⃗)=(2y+1,  2x,  2z)\nabla(\nabla\cdot\vec F) = (2y + 1,\; 2x,\; 2z). Componentwise, ΔF⃗=(Δ(x2y),Δ(yz2),Δ(xz))=(2y,  2y,  0)\Delta\vec F = (\Delta(x^2y), \Delta(yz^2), \Delta(xz)) = (2y,\; 2y,\; 0). The difference is (1,  2x−2y,  2z)(1,\; 2x - 2y,\; 2z). ✓

✅ Concept checkselect all that apply

Select every expression that is zero for ALL smooth fields $f$ (scalar) and $\vec A$ (vector).

✏️ Pen & paperGreen's first identity and uniqueness●●○
  1. Use identity 5 to show ∇⋅(f ∇g)=∇f⋅∇g+f Δg\nabla\cdot(f\,\nabla g) = \nabla f\cdot\nabla g + f\,\Delta g.
  2. Integrate over a region Ω\Omega and apply the divergence theorem to get Green’s first identity ∭Ω(f Δg+∇f⋅∇g) dV=∮∂Ωf ∇g⋅n^ dS.\displaystyle\iiint_\Omega \big(f\,\Delta g + \nabla f\cdot\nabla g\big)\,dV = \oint_{\partial\Omega} f\,\nabla g\cdot\hat n\,dS.
  3. Suppose Δw=0\Delta w = 0 in Ω\Omega and w=0w = 0 on ∂Ω\partial\Omega. Take f=g=wf = g = w and conclude that w≡0w \equiv 0. Why does this show the Poisson problem Δϕ=ρ\Delta \phi = \rho, with ϕ\phi given on the boundary, has at most one solution?
Show worked solution
  1. Apply identity 5 with F⃗=∇g\vec F = \nabla g: ∇⋅(f∇g)=∇f⋅∇g+f ∇⋅∇g=∇f⋅∇g+fΔg\nabla\cdot(f\nabla g) = \nabla f\cdot\nabla g + f\,\nabla\cdot\nabla g = \nabla f\cdot\nabla g + f\Delta g.
  2. Integrate both sides over Ω\Omega. The divergence theorem turns the left side into ∮∂Ωf∇g⋅n^ dS\oint_{\partial\Omega} f\nabla g\cdot\hat n\,dS.
  3. With f=g=wf = g = w, the boundary term vanishes because w=0w = 0 there, and Δw=0\Delta w = 0. That leaves ∭Ω∣∇w∣2 dV=0\iiint_\Omega |\nabla w|^2\, dV = 0. The integrand is non-negative, so ∇w=0\nabla w = 0 everywhere, so ww is constant, and since it is 00 on the boundary, w≡0w \equiv 0. If ϕ1\phi_1 and ϕ2\phi_2 both solve the Poisson problem, then w=ϕ1−ϕ2w = \phi_1 - \phi_2 satisfies Δw=0\Delta w = 0 with w=0w = 0 on the boundary, so ϕ1=ϕ2\phi_1 = \phi_2. (With Neumann data ∂ϕ/∂n\partial\phi/\partial n instead, the same argument gives uniqueness up to an added constant, which is exactly the situation for pressure.)
▶ Video Helmholtz–Hodge decomposition and Stam's projection stepcaptions only
Transcript
  1. Every incompressible fluid solver hides one beautiful theorem at its core. Let's find it.
  2. After a solver adds forces and advects, its velocity field is a mess. Somewhere fluid is piling up or being created from nothing. Here, the left blob spreads out like a source, and the right blob spins.
  3. Real water can't do the first thing. An incompressible flow must have zero divergence everywhere. The spinning is fine. So we want to remove the spreading part, and only that part.
  4. Two identities make this possible. First: the curl of a gradient is always zero. A pure slope field has no swirl.
  5. Second: the divergence of a curl is always zero. A pure swirl field never piles up fluid.
  6. Both come from the same fact. Write them out and every term is a mixed partial derivative minus the same partial taken in the other order, which is zero.
  7. The Helmholtz Hodge decomposition says: any velocity field w splits, uniquely, into the gradient of a scalar potential phi, plus a field u with zero divergence.
  8. The gradient part has no curl, so it carries all of the spreading. The divergence free part carries all of the swirl. Throw away the gradient, and what's left is a legal incompressible flow.
  9. How do we find phi? Take the divergence of both sides. The divergence of u is zero by design, and the divergence of a gradient is the Laplacian.
  10. So phi solves a Poisson equation: Laplacian of phi equals the divergence of w. Solve it, subtract the gradient, and you are done.
  11. Even better, the two pieces are perpendicular, in the sense that the integral of their dot product is zero. Integrate by parts once and the divergence of u kills it.
  12. So energies add like Pythagoras. Projection is an honest orthogonal projection: it removes exactly the compressive energy and never adds any.
  13. In Fourier space the whole thing becomes linear algebra you already know. Each wave in the field has a wave vector k and an amplitude vector w hat.
  14. The part along k is compression, the gradient piece. The part perpendicular to k is pure swirl. Projection just deletes the parallel component, one wave at a time. This is exactly Stam's FFT fluid solver.
  15. Now open Stam's project routine. It computes the divergence, solves a Poisson equation with twenty Gauss Seidel sweeps, and subtracts the gradient. That is Helmholtz Hodge, line for line.
  16. And the potential it solves for is the pressure. Pressure is whatever push is needed, at every point, to keep the fluid from compressing.
  17. Recap: split the field, solve one Poisson equation for the potential, keep the divergence free part. That completes our vector calculus toolkit. Next module, we start solving differential equations themselves.

The Helmholtz–Hodge decomposition

Here is the theorem this module has been building toward.

By identities 1 and 2, the pieces have complementary jobs. ∇⋅w⃗=Δϕ\nabla\cdot\vec w = \Delta\phi, so all the divergence lives in the gradient part. And ∇×w⃗=∇×u⃗\nabla\times\vec w = \nabla\times\vec u, so all the curl lives in the divergence-free part. (On regions with holes, or with other boundary conditions, a third piece can appear: a field that is both curl-free and divergence-free, i.e. a gradient of a harmonic function. On a periodic box it is just a constant, as a quiz below explores.)

How to compute it. Take the divergence of w⃗=∇ϕ+u⃗\vec w = \nabla\phi + \vec u:

∇⋅w⃗=∇⋅∇ϕ+∇⋅u⃗⏟0⟹  Δϕ=∇⋅w⃗  \nabla\cdot\vec w = \nabla\cdot\nabla\phi + \underbrace{\nabla\cdot\vec u}_{0} \qquad\Longrightarrow\qquad \boxed{\;\Delta\phi = \nabla\cdot\vec w\;}

That is a Poisson equation for ϕ\phi. Its boundary condition comes from dotting with n^\hat n: ∂ϕ/∂n=w⃗⋅n^\partial\phi/\partial n = \vec w\cdot\hat n. Solve it, and then set u⃗=w⃗−∇ϕ\vec u = \vec w - \nabla\phi. The whole decomposition costs one Laplacian solve.

Why it’s unique, and why the pieces are orthogonal. Integrate ∇ϕ⋅u⃗\nabla\phi\cdot\vec u over Ω\Omega using identity 5 with f=ϕf = \phi and F⃗=u⃗\vec F = \vec u:

∭Ω∇ϕ⋅u⃗ dV=∮∂Ωϕ u⃗⋅n^ dS−∭Ωϕ ∇⋅u⃗ dV=0−0.\iiint_\Omega \nabla\phi\cdot\vec u\, dV = \oint_{\partial\Omega}\phi\,\vec u\cdot\hat n\, dS - \iiint_\Omega \phi\,\nabla\cdot\vec u\, dV = 0 - 0 .

So the two pieces are orthogonal in the sense of the integral inner product, and energies add like Pythagoras:

∭∣w⃗∣2 dV=∭∣∇ϕ∣2 dV+∭∣u⃗∣2 dV.\iiint |\vec w|^2\,dV = \iiint |\nabla\phi|^2\,dV + \iiint |\vec u|^2\,dV .

Uniqueness follows: if ∇ϕ1+u⃗1=∇ϕ2+u⃗2\nabla\phi_1 + \vec u_1 = \nabla\phi_2 + \vec u_2, then ∇(ϕ1−ϕ2)=u⃗2−u⃗1\nabla(\phi_1 - \phi_2) = \vec u_2 - \vec u_1 is both a gradient and divergence-free with zero normal component. Being orthogonal to itself, it is zero.

Worked example: a decomposition by hand

On the periodic square [0,2π]2[0, 2\pi]^2, decompose w⃗=(0,  2sin⁡xcos⁡y)\vec w = (0,\; 2\sin x\cos y).

The field points straight up or down everywhere, so it is not obvious that it contains either a source or a vortex. Compute its divergence:

∇⋅w⃗=∂x(0)+∂y(2sin⁡xcos⁡y)=−2sin⁡xsin⁡y.\nabla\cdot\vec w = \partial_x(0) + \partial_y(2\sin x\cos y) = -2\sin x\sin y .

Solve Δϕ=−2sin⁡xsin⁡y\Delta\phi = -2\sin x\sin y. We saw in the first worked example that Δ(sin⁡xsin⁡y)=−2sin⁡xsin⁡y\Delta(\sin x\sin y) = -2\sin x\sin y, so ϕ=sin⁡xsin⁡y\phi = \sin x\sin y works. It is periodic, and it is unique up to a constant. Then

∇ϕ=(cos⁡xsin⁡y,    sin⁡xcos⁡y),u⃗=w⃗−∇ϕ=(−cos⁡xsin⁡y,    sin⁡xcos⁡y).\nabla\phi = (\cos x\sin y,\;\; \sin x\cos y), \qquad \vec u = \vec w - \nabla\phi = (-\cos x\sin y,\;\; \sin x\cos y).

Checks. ∇⋅u⃗=sin⁡xsin⁡y−sin⁡xsin⁡y=0\nabla\cdot\vec u = \sin x\sin y - \sin x\sin y = 0. ✓ The 2D curl of ∇ϕ\nabla\phi is cos⁡xcos⁡y−cos⁡xcos⁡y=0\cos x\cos y - \cos x\cos y = 0. ✓ Orthogonality: ∇ϕ⋅u⃗=−cos⁡2xsin⁡2y+sin⁡2xcos⁡2y\nabla\phi\cdot\vec u = -\cos^2x\sin^2y + \sin^2x\cos^2y, and over the square each term integrates to π⋅π\pi\cdot\pi, so the integral is −π2+π2=0-\pi^2 + \pi^2 = 0. ✓ Energies: ∫∣w⃗∣2=4π2\int|\vec w|^2 = 4\pi^2, ∫∣∇ϕ∣2=2π2\int|\nabla\phi|^2 = 2\pi^2 and ∫∣u⃗∣2=2π2\int|\vec u|^2 = 2\pi^2, which add up correctly. ✓

So a purely vertical field is secretly half a checkerboard of sources and sinks (the cells where sin⁡xsin⁡y\sin x\sin y has extrema) and half a checkerboard of counter-rotating vortices. Load w = (0, 2 sin x cos y) in the lab below and you’ll see exactly this split.

◆ Interactive Helmholtz–Hodge lab: stir a velocity field and split it

Loading simulation…

Things to try:

  1. With source + vortex loaded, colour by divergence: the red blob appears in w⃗\vec w and ∇ϕ\nabla\phi, but u⃗\vec u is blank. Switch to curl: now ∇ϕ\nabla\phi is blank and u⃗\vec u carries it all.
  2. Read the energy line: 12∫∣w⃗∣2\tfrac12\int|\vec w|^2 equals the sum of the two parts, and ∫∇ϕ⋅u⃗=0\int\nabla\phi\cdot\vec u = 0 to machine precision.
  3. Choose Gauss–Seidel sweeps and set the slider to Stam’s default of 20. The ”% left” divergence is far from zero, and the energies no longer add up. Drag the slider to 400 and watch it converge.
  4. Pick empty and stir. Even pushing in a single straight line creates both parts: fluid shoved forward has to go somewhere, so the divergence-free part curls around into a vortex pair. That’s the classic smoke-ring shape you get in Stam’s demo.

Stam’s projection step is Helmholtz–Hodge

The incompressible Navier–Stokes equations (11.4 Viscosity & the Navier–Stokes Equations) are

∂u⃗∂t=−(u⃗⋅∇)u⃗−1ρ∇p+ν Δu⃗+f⃗,∇⋅u⃗=0.\frac{\partial\vec u}{\partial t} = -(\vec u\cdot\nabla)\vec u - \frac{1}{\rho}\nabla p + \nu\,\Delta\vec u + \vec f, \qquad \nabla\cdot\vec u = 0 .

There’s no equation for the pressure pp. It is whatever it needs to be to keep ∇⋅u⃗=0\nabla\cdot\vec u = 0. Stam’s splitting handles this by first ignoring the pressure. Starting from a divergence-free u⃗ n\vec u^{\,n}, he adds forces, advects and diffuses to get an intermediate field w⃗\vec w that is generally not divergence-free. Then he restores incompressibility by subtracting a gradient:

u⃗ n+1=w⃗−Δtρ∇p,∇⋅u⃗ n+1=0.\vec u^{\,n+1} = \vec w - \frac{\Delta t}{\rho}\nabla p, \qquad \nabla\cdot\vec u^{\,n+1} = 0 .

Compare with Helmholtz–Hodge. This is the decomposition, with ϕ=Δtρp\phi = \tfrac{\Delta t}{\rho}p. Taking the divergence gives the pressure Poisson equation Δp=ρΔt∇⋅w⃗\Delta p = \tfrac{\rho}{\Delta t}\nabla\cdot\vec w. The pressure is the potential of the compressive part of the velocity, the push needed at each point to cancel it. Stam’s project routine does exactly this, written here in NumPy for a periodic grid:

def project(u, v, h, sweeps=20):
    div = (np.roll(u, -1, 1) - np.roll(u, 1, 1) + np.roll(v, -1, 0) - np.roll(v, 1, 0)) / (2 * h)  # 1. divergence
    p = np.zeros_like(div)
    for _ in range(sweeps):                                       # 2. relax  Δp = div
        p = (np.roll(p, -1, 1) + np.roll(p, 1, 1) + np.roll(p, -1, 0) + np.roll(p, 1, 0) - h * h * div) / 4
    u -= (np.roll(p, -1, 1) - np.roll(p, 1, 1)) / (2 * h)         # 3. subtract the gradient
    v -= (np.roll(p, -1, 0) - np.roll(p, 1, 0)) / (2 * h)
    return u, v

Line for line: compute the divergence, relax a Poisson equation with the 5-point Laplacian (Stam uses Gauss–Seidel in place, written as Jacobi here), subtract the gradient. (Stam’s code folds the factors of hh and ρ/Δt\rho/\Delta t into pp, and handles walls with the boundary condition ∂p/∂n=0\partial p/\partial n = 0 instead of periodicity. His compact 5-point Laplacian also doesn’t exactly match the wide central-difference divergence and gradient, so even a fully converged solve leaves a little grid-scale divergence. The lab and the cells below use a matched stencil.) Everything in this lesson shows up here:

  • The Laplacian and its averaging form are the relaxation loop.
  • Jacobi/Gauss–Seidel slowness is why 20 sweeps leave visible divergence behind, so smoke slowly loses or gains volume. Production solvers use preconditioned conjugate gradients or multigrid.
  • Curl of gradient = 0 guarantees that subtracting ∇p\nabla p leaves the vorticity, the swirls you want to see, untouched.
  • Orthogonality guarantees that projection only ever removes kinetic energy. It can’t make the simulation blow up, which is half of why Stam’s method is “stable”.
✅ Concept check

In the projection step $\vec u^{\,n+1} = \vec w - \frac{\Delta t}{\rho}\nabla p$, what does the pressure field $p$ correspond to in the Helmholtz–Hodge decomposition $\vec w = \nabla\phi + \vec u$?

✅ Concept check

On a periodic box, you project a uniform wind $\vec w = (1, 0)$. What comes out?

Implement it: the projection

First, see the cost of Stam’s 20 sweeps for yourself. This cell projects a messy field with a relaxation Poisson solver and reports how much divergence is left after each number of sweeps. (It uses a stencil that reaches two cells, matched to the central-difference divergence, so that the converged answer is exactly divergence-free on the grid.)

🐍 PythonHow many relaxation sweeps does projection need?
⌘/Ctrl + Enter

Now write the exact version, Stam’s FFT projection.

🧪 Coding exerciseExercise: Helmholtz–Hodge by FFT
⌘/Ctrl + Enter
✏️ Pen & paperDecompose a shear plus a compression wave●●○

On the periodic square [0,2π]2[0, 2\pi]^2, let w⃗=(1+cos⁡x+sin⁡y,  0)\vec w = (1 + \cos x + \sin y,\; 0).

  1. Find ϕ\phi and u⃗\vec u in the Helmholtz–Hodge decomposition w⃗=∇ϕ+u⃗\vec w = \nabla\phi + \vec u.
  2. Check that ∇⋅u⃗=0\nabla\cdot\vec u = 0 and that w⃗\vec w and u⃗\vec u have the same curl.
  3. Verify the energy identity ∫∣w⃗∣2=∫∣∇ϕ∣2+∫∣u⃗∣2\int|\vec w|^2 = \int|\nabla\phi|^2 + \int|\vec u|^2.
  4. If w⃗\vec w were Stam’s intermediate velocity, with density ρ=1\rho = 1 and time step Δt\Delta t, what pressure would the projection compute?
Show worked solution
  1. ∇⋅w⃗=−sin⁡x\nabla\cdot\vec w = -\sin x. Solve Δϕ=−sin⁡x\Delta\phi = -\sin x: since Δ(sin⁡x)=−sin⁡x\Delta(\sin x) = -\sin x, take ϕ=sin⁡x\phi = \sin x (plus any constant). Then ∇ϕ=(cos⁡x,0)\nabla\phi = (\cos x, 0) and u⃗=w⃗−∇ϕ=(1+sin⁡y,  0)\vec u = \vec w - \nabla\phi = (1 + \sin y,\; 0). The constant wind and the shear sin⁡y\sin y both end up in u⃗\vec u.
  2. ∇⋅u⃗=∂x(1+sin⁡y)=0\nabla\cdot\vec u = \partial_x(1+\sin y) = 0. ✓ The scalar curl is ∂xw2−∂yw1=0−cos⁡y=−cos⁡y\partial_x w_2 - \partial_y w_1 = 0 - \cos y = -\cos y for w⃗\vec w, and 0−cos⁡y=−cos⁡y0 - \cos y = -\cos y for u⃗\vec u. ✓ (And ∇ϕ\nabla\phi has curl 0−0=00 - 0 = 0.)
  3. Over the square, ∫1=4π2\int 1 = 4\pi^2, ∫cos⁡2x=2π2\int\cos^2x = 2\pi^2 and ∫sin⁡2y=2π2\int\sin^2 y = 2\pi^2, and every cross term (like ∫cos⁡x\int\cos x or ∫cos⁡xsin⁡y\int\cos x\sin y) integrates to zero. So ∫∣w⃗∣2=4π2+2π2+2π2=8π2\int|\vec w|^2 = 4\pi^2 + 2\pi^2 + 2\pi^2 = 8\pi^2. Also ∫∣∇ϕ∣2=∫cos⁡2x=2π2\int|\nabla\phi|^2 = \int\cos^2 x = 2\pi^2 and ∫∣u⃗∣2=∫(1+sin⁡y)2=4π2+2π2=6π2\int|\vec u|^2 = \int(1+\sin y)^2 = 4\pi^2 + 2\pi^2 = 6\pi^2. Then 2π2+6π2=8π22\pi^2 + 6\pi^2 = 8\pi^2. ✓
  4. ϕ=Δtρp\phi = \tfrac{\Delta t}{\rho}p, so p=sin⁡xΔtp = \dfrac{\sin x}{\Delta t} plus an arbitrary constant. The compression wave cos⁡x\cos x is removed by a pressure that is high where fluid is piling up. At x=π/2x = \pi/2, w1=1+cos⁡x+sin⁡yw_1 = 1 + \cos x + \sin y is decreasing in xx: fluid arrives faster than it leaves, so the convergence is greatest there, and that is exactly where sin⁡x\sin x peaks.
✏️ Pen & paperFourier projection by hand●●●

A single Fourier mode of a 2D velocity field has wave vector k⃗=(3,4)\vec k = (3, 4) and amplitude w⃗^=(5,0)\hat{\vec w} = (5, 0).

  1. Compute the projection matrix P=I−k⃗k⃗T/∣k⃗∣2P = I - \vec k\vec k^{\mathsf T}/|\vec k|^2 and the divergence-free amplitude u⃗^=Pw⃗^\hat{\vec u} = P\hat{\vec w}.
  2. Check that u⃗^⋅k⃗=0\hat{\vec u}\cdot\vec k = 0, and that P2=PP^2 = P.
  3. Explain in one sentence why applying the projection twice in a solver does nothing the second time, and why that’s a good sanity test for your own project() code.
Show worked solution
  1. ∣k⃗∣2=25|\vec k|^2 = 25 and k⃗k⃗T=[9121216]\vec k\vec k^{\mathsf T} = \begin{bmatrix} 9 & 12 \\ 12 & 16\end{bmatrix}, so P=125[16−12−129]P = \dfrac{1}{25}\begin{bmatrix} 16 & -12 \\ -12 & 9\end{bmatrix}. Then u⃗^=P(5,0)=125(80,−60)=(3.2,  −2.4)\hat{\vec u} = P(5, 0) = \tfrac{1}{25}(80, -60) = (3.2,\; -2.4). The removed (gradient) part is w⃗^−u⃗^=(1.8,2.4)=0.6 k⃗\hat{\vec w} - \hat{\vec u} = (1.8, 2.4) = 0.6\,\vec k, parallel to k⃗\vec k as it should be.
  2. u⃗^⋅k⃗=3.2⋅3−2.4⋅4=9.6−9.6=0\hat{\vec u}\cdot\vec k = 3.2\cdot 3 - 2.4\cdot 4 = 9.6 - 9.6 = 0. ✓ For P2P^2: 1625[16⋅16+144−192−108−192−108144+81]=1625[400−300−300225]=125[16−12−129]=P\tfrac{1}{625}\begin{bmatrix} 16\cdot16 + 144 & -192 - 108 \\ -192 - 108 & 144 + 81\end{bmatrix} = \tfrac{1}{625}\begin{bmatrix} 400 & -300 \\ -300 & 225\end{bmatrix} = \tfrac{1}{25}\begin{bmatrix} 16 & -12 \\ -12 & 9\end{bmatrix} = P. ✓
  3. Once a field is divergence-free, its gradient part is zero, so projecting again removes nothing (P2=PP^2 = P). If running your project() twice changes the field noticeably, your Poisson solve hasn’t converged or your discrete divergence and gradient don’t match.

Key Takeaways

  • The Laplacian Δf=∇⋅∇f=fxx+fyy+fzz\Delta f = \nabla\cdot\nabla f = f_{xx} + f_{yy} + f_{zz} is the divergence of the gradient. It is negative at peaks and positive in valleys.
  • Precisely, Δf\Delta f is proportional to (neighbourhood average − centre value). That’s why it drives diffusion, heat flow and viscosity: things relax toward their local average.
  • The 5-point stencil (fE+fW+fN+fS−4fC)/h2(f_E + f_W + f_N + f_S - 4f_C)/h^2 is the same idea on a grid, and it is second-order accurate by Taylor’s theorem.
  • Harmonic functions (Δu=0\Delta u = 0) equal their own averages: the mean value property, no interior extremes. Jacobi relaxation finds them by repeated averaging, quickly for wiggles and slowly for smooth errors.
  • The vector Laplacian acts componentwise in Cartesian coordinates, and ∇×∇×F⃗=∇(∇⋅F⃗)−ΔF⃗\nabla\times\nabla\times\vec F = \nabla(\nabla\cdot\vec F) - \Delta\vec F.
  • curl grad = 0 and div curl = 0 (consecutive steps in grad → curl → div) both follow from equal mixed partials, and they hold exactly for matched discrete operators.
  • Helmholtz–Hodge: every field splits uniquely and orthogonally as w⃗=∇ϕ+u⃗\vec w = \nabla\phi + \vec u with ∇⋅u⃗=0\nabla\cdot\vec u = 0. Find ϕ\phi by solving Δϕ=∇⋅w⃗\Delta\phi = \nabla\cdot\vec w.
  • Stam’s projection step is exactly this decomposition, with pressure as the potential. One Poisson solve per time step makes the flow incompressible, keeps the vorticity and never adds energy.
Finished this lesson?

Mark it complete to track your progress (saved in this browser). Reading every section does this for you.

Where this leads

These later lessons build directly on this one: