Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

14. The lid-driven cavity

School of Engineering, Nazarbayev University

A new incompressible Navier–Stokes code is often tested first on a square box [0,L]2[0, L]^2 whose left, right, and bottom walls are stationary while the top lid slides horizontally at speed UlidU_{\mathrm{lid}}: the 2-D lid-driven cavity. The geometry is simple, but the flow has singular corners at the lid, a primary vortex and counter-rotating corner eddies, of which Moffatt showed in 1964 that there is in principle an infinite sequence. In 2-D planar geometry, taking the curl ∇×(⋅)\nabla \times (\cdot) of the momentum equation removes −∇p-\nabla p identically (∇×∇p≡0\nabla \times \nabla p \equiv \mathbf{0}) and enforces ∇⋅u=0\nabla \cdot \mathbf{u} = 0 automatically via a scalar streamfunction ψ(x,y)\psi(x, y). In this lecture we derive the ω\omega–ψ\psi formulation and Thom’s (1933) 2nd-order no-slip wall vorticity boundary condition, accelerate every elliptic step to O(Nlog⁡N)O(N \log N) via elliptic_multigrid.poisson_2d_fast_sine, verify navier_stokes.lid_driven_cavity_2d against Ghia, Ghia & Shin (1982) (constants.GHIA_RE100_BENCHMARK), and compare Re=100\mathrm{Re} = 100 vs. Re=400\mathrm{Re} = 400 vortex structures.

Learning objectives

  1. Derive the 2-D vorticity–streamfunction (ω\omega–ψ\psi) Navier–Stokes system ∂tω+u∂xω+v∂yω=ν∇2ω\partial_t \omega + u\partial_x \omega + v\partial_y \omega = \nu \nabla^2 \omega coupled to −∇2ψ=ω-\nabla^2 \psi = \omega with (u,v)=(∂yψ,−∂xψ)(u, v) = (\partial_y \psi, -\partial_x \psi).

  2. Derive and verify Thom’s (1933) 2nd-order wall vorticity formula ωwall=−2(ψadj+huwall∥)/h2\omega_{\mathrm{wall}} = -2(\psi_{\mathrm{adj}} + h u_{\mathrm{wall}}^{\parallel})/h^2 enforcing tangential no-slip on solid walls.

  3. Verify navier_stokes.lid_driven_cavity_2d at Re=100\mathrm{Re} = 100 point-by-point against the 17-point Ghia, Ghia & Shin (1982) centerline tables (constants.GHIA_RE100_BENCHMARK) and core strength ψmin⁡≈−0.1034\psi_{\min} \approx -0.1034.

  4. Contrast viscous-dominated (Re=100\mathrm{Re} = 100) vs. inertia-convected (Re=400\mathrm{Re} = 400) primary vortex migration and secondary bottom-corner Moffatt eddies (ψ>0\psi > 0).

Time: about 60 min

1. Physical Principles: Vorticity–Streamfunction (ω\omega–ψ\psi) & Thom’s Wall Formula

In 2-D incompressible flow (∂xu+∂yv=0\partial_x u + \partial_y v = 0), defining the scalar streamfunction ψ(x,y)\psi(x, y) and out-of-plane vorticity ω(x,y)\omega(x, y) via:

u=∂ψ∂y,v=−∂ψ∂x,ω=∂v∂x−∂u∂y=−(∂2ψ∂x2+∂2ψ∂y2)  ⟹  −∇2ψ=ω(1.1)u = \frac{\partial \psi}{\partial y}, \qquad v = -\frac{\partial \psi}{\partial x}, \qquad \omega = \frac{\partial v}{\partial x} - \frac{\partial u}{\partial y} = -\left(\frac{\partial^2 \psi}{\partial x^2} + \frac{\partial^2 \psi}{\partial y^2}\right) \implies -\nabla^2 \psi = \omega \tag{1.1}

satisfies continuity ∂x(∂yψ)+∂y(−∂xψ)=0\partial_x(\partial_y \psi) + \partial_y(-\partial_x \psi) = 0 identically. Taking ∂x(y-mom)−∂y(x-mom)\partial_x(\text{y-mom}) - \partial_y(\text{x-mom}) eliminates −∇p-\nabla p (∂x∂yp−∂y∂xp=0\partial_x \partial_y p - \partial_y \partial_x p = 0) and yields the scalar advection–diffusion equation characterized solely by the cavity Reynolds number Re=UlidL/ν\mathrm{Re} = U_{\mathrm{lid}} L / \nu:

∂ω∂t+u∂ω∂x+v∂ω∂y=ν∇2ω,Re=UlidLν(1.2)\frac{\partial \omega}{\partial t} + u\frac{\partial \omega}{\partial x} + v\frac{\partial \omega}{\partial y} = \nu \nabla^2 \omega, \qquad \mathrm{Re} = \frac{U_{\mathrm{lid}} L}{\nu} \tag{1.2}

1.1 Where Does Vorticity Come From? Thom’s (1933) No-Slip Wall Condition.

Impermeable cavity walls form a closed streamline ψ∣∂Ω=0\psi|_{\partial\Omega} = 0 (homogeneous Dirichlet for poisson_2d_fast_sine), yet ω\omega has no independent physical boundary value, no-slip tangential shear at the walls generates all vorticity in the flow. Taylor-expanding ψ(x,L−h)\psi(x, L - h) one spacing hh below the moving top lid (y=Ly = L, where ψ=0\psi = 0 and ∂yψ=u=+Ulid\partial_y \psi = u = +U_{\mathrm{lid}}):

ψi,N−2=ψi,N−1⏟0−h∂ψ∂y∣y=L⏟Ulid+h22∂2ψ∂y2∣y=L⏟−ωi,N−1+O(h3)⟹ωi,N−1=−2(ψi,N−2+hUlid)h2(1.3)\psi_{i, N-2} = \underbrace{\psi_{i, N-1}}_{0} - h\underbrace{\left.\frac{\partial \psi}{\partial y}\right|_{y=L}}_{U_{\mathrm{lid}}} + \frac{h^2}{2}\underbrace{\left.\frac{\partial^2 \psi}{\partial y^2}\right|_{y=L}}_{-\omega_{i, N-1}} + O(h^3) \quad \Longrightarrow \quad \omega_{i, N-1} = -\frac{2\left(\psi_{i, N-2} + h U_{\mathrm{lid}}\right)}{h^2} \tag{1.3}

Setting Uwall=0U_{\mathrm{wall}} = 0 on the stationary left, right, and bottom walls immediately gives ωwall=−2ψadj/h2\omega_{\mathrm{wall}} = -2\psi_{\mathrm{adj}} / h^2.


Demonstration 1: Fast Sine O(N log N) Streamfunction Inversion & Thom's 2nd-Order Wall Vorticity
================================================================================================
DST-I streamfunction max |psi_dst - psi_exact| (65x65) : 8.0358e-04
Thom top-wall vorticity max |omega_Thom - omega_exact| : 1.5849e-02 (exact peak |omega_wall| = 19.739)
Thom wall error 33 -> 65 -> 129 : [0.063336, 0.015849, 0.003963] (observed order = 1.999)

What Demonstration 1 shows. Every time step of lid_driven_cavity_2d solves −∇h2ψ=ω-\nabla_h^2 \psi = \omega exactly in O(Nlog⁡N)O(N \log N) floating-point operations via 2-D Discrete Sine Transform (poisson_2d_fast_sine), avoiding iterative SOR inner loops while Thom’s formula injects wall vorticity at O(h2)O(h^2) accuracy.


2. Benchmark Verification Against Ghia, Ghia & Shin (1982) at Re=100\mathrm{Re} = 100

In 1982, U. Ghia, K. N. Ghia, and C. T. Shin published coupled-multigrid 129×129129 \times 129 cavity solutions (J. Comput. Phys. 48, 387–411) that are still the standard reference for this flow (constants.GHIA_RE100_BENCHMARK). We run navier_stokes.lid_driven_cavity_2d on a 49×4949 \times 49 grid at Re=100\mathrm{Re} = 100 (constants.reynolds_number(1.0, 1.0, 0.01) == 100.0) and compare every single vertical-centerline u(0.5,y)u(0.5, y) and horizontal-centerline v(x,0.5)v(x, 0.5) point.


Demonstration 2: Re = 100 Lid-Driven Cavity vs Ghia, Ghia & Shin (1982) Benchmark
=================================================================================
49x49 Re=100 Cavity (steps=3634, d|omega|/dt=9.97e-07):
  Primary vortex core psi_min : -0.10207 vs Ghia (1982) -0.10342 (rel diff = 1.31%)
  Max |u(0.5, y) - u_Ghia|    : 0.00335
  Max |v(x, 0.5) - v_Ghia|    : 0.00297
<Figure size 1210x473 with 2 Axes>

Figure 14.1. Lid-driven cavity at Re=100\mathrm{Re} = 100 on a 49×4949 \times 49 grid (lines) and the values tabulated by Ghia, Ghia and Shin (1982) (symbols). (a) Horizontal velocity along the vertical centerline. (b) Vertical velocity along the horizontal centerline.

What Demonstration 2 shows. The centerline profiles at Re=100\mathrm{Re} = 100 are not symmetric: the top moving lid drags a sharp forward shear layer (u→+1u \to +1 near y=1y = 1), which falls downward along the right wall (vmin⁡≈−0.245v_{\min} \approx -0.245 at x≈0.81x \approx 0.81) and returns more gently upward along the left wall (vmax⁡≈+0.175v_{\max} \approx +0.175 at x≈0.23x \approx 0.23), and the computed profiles differ from the 34 tabulated values of Ghia, Ghia and Shin by at most 0.004.


3. Mesh-Refinement Convergence Toward Ghia’s Continuum Limit

We verify monotone grid convergence of both the primary vortex circulation ψmin⁡\psi_{\min} and the centerline L2L_2 discrepancy against GHIA_RE100_BENCHMARK across N∈{21,33,45}N \in \{21, 33, 45\}.


Demonstration 3: Grid-Refinement Convergence at Re = 100 (21x21 -> 33x33 -> 45x45)
==================================================================================
  Grid N |  h = 1/(N-1) |     psi_min |  |psi_min + 0.10342| |  Ghia u-profile L2 diff
--------------------------------------------------------------------------------------
      21 |      0.05000 |    -0.09530 |              0.00812 |                 0.01496
      33 |      0.03125 |    -0.10038 |              0.00304 |                 0.00467
      45 |      0.02273 |    -0.10179 |              0.00163 |                 0.00194

What Demonstration 3 shows. Both ∣ψmin⁡−(−0.10342)∣|\psi_{\min} - (-0.10342)| and the 17-point Ghia centerline L2L_2 error decrease from 21×21→33×33→45×4521 \times 21 \to 33 \times 33 \to 45 \times 45 as wall boundary layers are progressively resolved.


4. Re=100\mathrm{Re} = 100 vs. Re=400\mathrm{Re} = 400 & Corner Moffatt Eddies

What happens physically when we quadruple inertia from Re=100\mathrm{Re} = 100 to Re=400\mathrm{Re} = 400 on a 49×4949 \times 49 mesh?

  1. Inviscid Core Migration: At higher Re\mathrm{Re}, vorticity advects around closed streamlines into a core of nearly uniform vorticity whose center (arg⁡min⁡ψ)(\arg\min \psi) migrates downward and leftward from (0.625,0.750)(0.625, 0.750) toward the center of the cavity (0.5,0.5)(0.5, 0.5).

  2. Moffatt Corner Vortices (ψ>0\psi > 0): Adverse pressure gradients along the decelerating walls separate the boundary layer before reaching the stagnant bottom corners, spinning up counter-rotating secondary eddies (ψmax⁡>0\psi_{\max} > 0, counterclockwise vs. the clockwise ψ<0\psi < 0 primary vortex) whose strength grows strongly from Re=100\mathrm{Re} = 100 to Re=400\mathrm{Re} = 400.


Demonstration 4: Vortex Core Migration & Secondary Corner Moffatt Eddies (Re=100 vs Re=400)
===========================================================================================
Re = 100 : core (x, y) = (0.625, 0.750), psi_min = -0.10207, corner Moffatt psi_max = 1.622e-05
Re = 400 : core (x, y) = (0.562, 0.604), psi_min = -0.10510, corner Moffatt psi_max = 6.330e-04
<Figure size 1210x495 with 4 Axes>

Figure 14.2. Streamfunction in the cavity on a 49×4949 \times 49 grid. (a) Re=100\mathrm{Re} = 100. (b) Re=400\mathrm{Re} = 400. The point marks the center of the primary vortex; the dashed contours show the secondary eddies in the lower corners, where ψ>0\psi > 0.

Inside the algorithm

What Demonstration 4 shows. Between Re=100\mathrm{Re} = 100 and Re=400\mathrm{Re} = 400 the secondary eddy in the bottom right corner (ψ>0\psi > 0, dashed contours) becomes 39 times stronger (ψmax⁡=1.6×10−5\psi_{\max} = 1.6 \times 10^{-5} and 6.3×10−46.3 \times 10^{-4}), and the core of the primary vortex moves from (0.625,0.750)(0.625, 0.750) to (0.562,0.604)(0.562, 0.604). Both runs use the 49×4949 \times 49 grid and are continued until the vorticity changes by less than 10-6 per unit time; on this grid the corner eddies are resolved by only a few cells, so their strength is approximate. Below we inspect navier_stokes.lid_driven_cavity_2d and check that central differences of ψ\psi give a velocity field whose discrete divergence vanishes to rounding error at the interior vertices.

Check against the library


Inside the Algorithm: navier_stokes.lid_driven_cavity_2d & Exact Mixed-Difference Cancellation
==============================================================================================
def lid_driven_cavity_2d(
    n_grid: int = 49,
    Re: float = 100.0,
    U_lid: float = 1.0,
    L: float = 1.0,
    dt: float | None = None,
    max_steps: int = 4500,
    tol: float = 1e-6,
) -> dict:
    """Solve steady 2-D incompressible Navier-Stokes lid-driven cavity via DST-accelerated vorticity-streamfunction."""
    if n_grid < 9 or Re <= 0.0 or U_lid <= 0.0 or L <= 0.0:
        raise ValueError("Require n_grid >= 9 and Re, U_lid, L > 0.")
    h = float(L) / (int(n_grid) - 1)
    nu = float(U_lid) * float(L) / float(Re)
    x = np.linspace(0.0, float(L), int(n_grid))
    y = np.linspace(0.0, float(L), int(n_grid))

    if dt is None:
        dt_diff = 0.22 * (h**2) / nu
        dt_conv = 0.35 * h / float(U_lid)
        dt_use = min(dt_diff, dt_conv)
    else:
        dt_use = float(dt)

    psi = np.zeros((n_grid, n_grid), dtype=float)
    omega = np.zeros((n_grid, n_grid), dtype=float)

    step = 0
    diff_norm = 1.0
    for step in range(1, max_steps + 1):
        # Thom's 2nd-order no-slip wall vorticity boundary conditions (top wall y=L moves at +U_lid):
        omega[0, :] = -2.0 * psi[1, :] / (h**2)
        omega[-1, :] = -2.0 * psi[-2, :] / (h**2)
        omega[:, 0] = -2.0 * psi[:, 1] / (h**2)
        omega[:, -1] = -2.0 * (psi[:, -2] + h * float(U_lid)) / (h**2)

        u_in = (psi[1:-1, 2:] - psi[1:-1, :-2]) / (2.0 * h)
        v_in = -(psi[2:, 1:-1] - psi[:-2, 1:-1]) / (2.0 * h)

        # Central advection and viscous Laplacian of the interior vorticity:
        dom_dx = (omega[2:, 1:-1] - omega[:-2, 1:-1]) / (2.0 * h)
        dom_dy = (omega[1:-1, 2:] - omega[1:-1, :-2]) / (2.0 * h)
        lap_om = (
            omega[2:, 1:-1]
            + omega[:-2, 1:-1]
            + omega[1:-1, 2:]
            + omega[1:-1, :-2]
            - 4.0 * omega[1:-1, 1:-1]
        ) / (h**2)

        omega_new_in = omega[1:-1, 1:-1] + dt_use * (-(u_in * dom_dx + v_in * dom_dy) + nu * lap_om)
        diff_norm = float(np.max(np.abs(omega_new_in - omega[1:-1, 1:-1])) / dt_use)
        omega[1:-1, 1:-1] = omega_new_in

        # Exact direct DST-I solve of -lap_h(psi) = omega on interior nodes:
        psi[1:-1, 1:-1] = poisson_2d_fast_sine(omega[1:-1, 1:-1], dx=h, dy=h)
        if diff_norm < tol and step > 50:
            break

    u = np.zeros_like(psi)
    v = np.zeros_like(psi)
    u[1:-1, 1:-1] = (psi[1:-1, 2:] - psi[1:-1, :-2]) / (2.0 * h)
    v[1:-1, 1:-1] = -(psi[2:, 1:-1] - psi[:-2, 1:-1]) / (2.0 * h)
    u[:, -1] = float(U_lid)

    mid = n_grid // 2
    return {
        "x": x,
        "y": y,
        "u": u,
        "v": v,
        "psi": psi,
        "vorticity": omega,
        "u_vertical_centerline": u[mid, :],
        "v_horizontal_centerline": v[:, mid],
        "steps": step,
        "residual_rate": diff_norm,
        "psi_min": float(np.min(psi)),
    }

Max interior |du/dx + dv/dy| from streamfunction : 1.360e-15
Max interior |-lap_h(psi) - omega| (DST-I exact) : 9.037e-14

Exercises

Worked solutions are in 14_solutions.ipynb. The cell below the list is a short self-check to start from.

  1. Verify a result. Evaluate Thom’s bottom-wall vorticity formula ωThom(x,0)=−2ψ(x,h)/h2\omega_{\mathrm{Thom}}(x, 0) = -2\psi(x, h)/h^2 across N∈{17,33,65,129}N \in \{17, 33, 65, 129\} on two analytical streamfunctions satisfying ψ(x,0)=u(x,0)=0\psi(x, 0) = u(x, 0) = 0: (a) symmetric ψA(x,y)=sin⁡2(πy)sin⁡(πx)\psi_A(x,y) = \sin^2(\pi y)\sin(\pi x) (∂y3ψ∣0=0\partial_y^3\psi|_0 = 0) and (b) cubic-shear ψB(x,y)=y2(1−y)sin⁡(πx)\psi_B(x,y) = y^2(1 - y)\sin(\pi x) (∂y3ψ∣0=−6sin⁡πx≠0\partial_y^3\psi|_0 = -6\sin\pi x \neq 0). Verify observed convergence orders pA≈2.0p_A \approx 2.0 and pB≈1.0p_B \approx 1.0 from Taylor’s remainder theorem.

  2. Find how it fails. Verify that navier_stokes.lid_driven_cavity_2d raises ValueError for invalid inputs (n_grid=5, Re=0.0, U_lid=-1.0), and demonstrate on n_grid=33, Re=50 (ν=0.02,h=1/32\nu = 0.02, h = 1/32) that automatic time-stepping (dt=None) converges stably while forcing dt=0.04 (>h2/(4ν)≈0.0122> h^2/(4\nu) \approx 0.0122) blows up.

  3. Implement it yourself. Implement my_lid_cavity_step(psi, omega, h, nu, dt, U_lid) in pure NumPy using elliptic_multigrid.poisson_2d_fast_sine, evolve 150 steps from rest on n_grid=25, Re=100 at the exact auto dt = min(0.22*h^2/nu, 0.35*h/U_lid), and verify < 1e-13 agreement with navier_stokes.lid_driven_cavity_2d(n_grid=25, Re=100, max_steps=150, tol=1e-12).


Self-check for the exercises
============================
h = 0.03125 | max |omega_Thom - omega_exact| = 6.33359e-02
h = 0.01562 | max |omega_Thom - omega_exact| = 1.58493e-02
All checks passed.