A finite-volume incompressible Navier–Stokes solver on a staggered grid, written from scratch in MATLAB and validated against Ghia, Ghia and Shin (1982). The solver diverged at a time step that both textbook stability conditions said was safe; finding out why is the part of this worth reading.
Written for ME 6434 Advanced CFD, Virginia Tech, Spring 2022 (Prof. Danesh Tafti).
Fluid sits in a square cavity. Three walls are fixed; the top wall slides at constant velocity. The question is how the u and v profiles develop as the flow reaches steady state.
The part worth reading is Why the 32×32 grid diverged, where the solver blew up at a time step that both textbook stability conditions said was safe.
📄 Technical report · Problem statement ·
Ghia et al. (1982) · Codes/lid_driven_cavity_2d.m
Non-dimensionalised on the cavity length
Staggered grid. Pressure at cell centres, velocities on cell faces. This falls out of the
continuity equation itself, writing
Discretisation. 2nd-order central differences in space, 2nd-order Adams–Bashforth in time.
Fractional step (Perot's block-LU form). Integrating an incomplete momentum equation gives an intermediate velocity that is not divergence-free; a pressure Poisson solve projects it onto the divergence-free space without changing vorticity. Written as a block LU factorisation this decouples into three steps per time step:
| Grid | 128 × 128 (32² and 64² also run for the grid study) |
| Pressure Poisson | SOR, hand-written, residual tolerance 1e−5 |
| Poisson BCs | Neumann, imposed by zeroing the wall-side coefficients rather than using ghost values |
| Steady-state criterion | L2 norm of the velocity change per step < 1e−8 |
| Time step | not fixed: chosen from three stability limits, see below |
Everything is in one MATLAB file with no toolbox dependencies. The Poisson solve is written out
rather than handed to \, because the point of the exercise was the discretisation.
Rather than hard-code Δt, the solver takes the most restrictive of three limits:
dt_max = min([ dx/uT, ... % linear CFL
2^(2/3)*C^(1/3)*(dx/uT)^(4/3), ... % non-linear CFL (Adams-Bashforth)
0.25*dx*dx/nu ]); % 2D Neumann (viscous)The middle term is not standard, and the reason it is there is the interesting part.
On the 32×32 grid the solver diverged at Δt = 0.02. It should not have:
| Condition | Limit at 32×32 |
|---|---|
| Linear CFL, |
0.0313 |
| 2D Neumann, |
0.0244 |
Δt = 0.02 is below both. The obvious conclusions (a coding error, or an unstable scheme) were both wrong.
Higher-order explicit time integrators (Adams–Bashforth, Runge–Kutta) carry a non-linear CFL-type restriction that is more stringent than the linear one for convection-dominated flow. Schneider et al. showed this numerically; Deriaz derived it from a von Neumann analysis of the 2D Burgers and Euler equations. For 2nd-order Adams–Bashforth:
At 32×32 with
That derived limit is the middle term in dt_max above, so the solver now picks a stable step on
its own at any resolution.
Same 128×128 grid, two Reynolds numbers:
| Condition | Re = 100 | Re = 1000 |
|---|---|---|
| Linear CFL | 0.0078 | 0.0078 |
| 2D Neumann (viscous) | 0.0015 | 0.0153 |
| Non-linear CFL | 0.0025 | 0.0025 |
At Re = 100 the viscous limit binds; at Re = 1000 the non-linear CFL does. The viscous limit scales
as
At Re = 100, centreline velocities against the tabulated benchmark:
The u profile matches closely. The v profile reproduces the oscillatory shape but the values agree less well, reported as found rather than tuned away.
CPU time to convergence against grid resolution follows a power law
Above the ideal ~3 (cells × steps, with steps growing as the stability limit tightens), because the SOR Poisson solve needs more sweeps per step as the grid refines: the iterative solve, not the discretisation, is what makes refinement expensive here.
Open Codes/lid_driven_cavity_2d.m, and run. Set nx, ny for
resolution and nu for Reynolds number: as committed it is 128 × 128 at nu = 0.001, i.e.
Re = 1000. The validation plots above are Re = 100, which is nu = 0.01.
No toolboxes required.
- K. Schneider, N. Kevlahan, M. Farge. Comparison of an adaptive wavelet method and nonlinearly filtered pseudospectral methods for two-dimensional turbulence. Theor. Comput. Fluid Dyn. 9 (1997) 191–206.
- E. Deriaz. Stability conditions for the numerical solution of convection-dominated problems with skew-symmetric discretizations. SIAM J. Numer. Anal. 50 (2012) 1058–1085.
- U. Ghia, K. N. Ghia, C. T. Shin. High-Re solutions for incompressible flow using the Navier–Stokes equations and a multigrid method. J. Comput. Phys. 48 (1982) 387–411.
- J. B. Perot. An analysis of the fractional step method. J. Comput. Phys. 108 (1993) 51–58.

