Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 46 Ā· Computational Fluid Dynamics

An incompressible flow solver: the lid-driven cavity

Build a 2D incompressible Navier-Stokes solver with the streamfunction-vorticity method, run the lid-driven cavity at Re = 100 and check it with grid refinement.

The previous lesson used a model equation. Real flows add two difficulties: the nonlinear advection term, and the incompressibility constraint āˆ‡Ā·u = 0, which is not an evolution equation for any variable. Pressure is whatever it needs to be to keep the velocity divergence-free. Finding it is the central difficulty of incompressible CFD. This lesson shows the cleanest way around the problem in two dimensions, the streamfunction-vorticity formulation, and uses it on the most famous benchmark in the field.

A square cavity with a moving lid, a large clockwise vortex, and small corner vortices
The lid-driven cavity: a box of fluid, a sliding top wall and a pattern of vortices.

What you will learn

  • How the streamfunction ψ and vorticity ω remove the pressure and enforce āˆ‡Ā·u = 0 automatically.
  • How to implement a time-marching solver with wall vorticity boundary conditions.
  • How the primitive-variable "projection" method handles pressure.
  • How to check a solution by grid refinement.

Key terms

  • Streamfunction (ψ): a scalar with u = āˆ‚Ļˆ/āˆ‚y and v = -āˆ‚Ļˆ/āˆ‚x; lines of constant ψ are streamlines.
  • Vorticity (ω): ω = āˆ‚v/āˆ‚x - āˆ‚u/āˆ‚y, the local spin of the fluid.
  • Poisson equation: āˆ‡Ā²Ļˆ = -ω, an elliptic equation that links the two.
  • Projection method: advance the velocity without the pressure, then project it onto a divergence-free field.
  • Grid refinement study: compute on successively finer meshes and watch the answer converge.

Removing the pressure

In two dimensions, define ψ by u = āˆ‚Ļˆ/āˆ‚y, v = -āˆ‚Ļˆ/āˆ‚x. Then āˆ‚u/āˆ‚x + āˆ‚v/āˆ‚y = āˆ‚Ā²Ļˆ/āˆ‚xāˆ‚y - āˆ‚Ā²Ļˆ/āˆ‚yāˆ‚x = 0: incompressibility is satisfied identically. Take the curl of the momentum equations (āˆ‚/āˆ‚x of the y-equation minus āˆ‚/āˆ‚y of the x-equation); the pressure cancels, and the vorticity equation remains:

āˆ‚Ļ‰/āˆ‚t + u āˆ‚Ļ‰/āˆ‚x + v āˆ‚Ļ‰/āˆ‚y = ν āˆ‡Ā²Ļ‰

Vorticity is advected and diffused like any scalar. The velocity is tied to it through the definition of ω, which gives āˆ‡Ā²Ļˆ = -ω.

The algorithm for each time step is:

  1. Set ω on the walls from ψ (boundary condition below).
  2. Compute u and v from ψ by central differences.
  3. Advance ω in the interior with an explicit step: advection, then diffusion.
  4. Solve the Poisson equation āˆ‡Ā²Ļˆ = -ω for the new ψ (here with a few Jacobi sweeps per step).
  5. Repeat until ω stops changing (a steady state).

Wall vorticity (Thom's formula)

On a fixed wall u = v = 0, so ψ is constant (we take 0). Expanding ψ one cell into the fluid and using ψ_n = 0 at the wall: ψ_1 = ½ ψ_nn Ī”n², so ω_wall = -ψ_nn = -2 ψ_1/Ī”n². On the moving lid the tangential speed U adds a term: ω_lid = -2 ψ_1/Ī”n² - 2U/Ī”n. These are first-order accurate boundary conditions and they are the most delicate part of the method.

Stability

The explicit step needs Ī”t ≤ Ī”x²/(4ν) (the diffusion limit in two dimensions) and Ī”t ≤ Ī”x/U (the advection limit). The code below picks a time step that satisfies both with a safety factor.

The primitive-variable alternative

In three dimensions there is no streamfunction, so codes use the velocity u and pressure p directly. In Chorin's projection method (1968), each step has three parts:

  1. Advance the velocity with advection and viscosity but without pressure, to get a provisional field u*.
  2. Solve the pressure Poisson equation āˆ‡Ā²p = (1/Ī”t) āˆ‡Ā·u*.
  3. Correct: u^{n+1} = u* - Ī”t āˆ‡p, which is divergence-free.

Staggered grids (pressure at cell centers, velocities at faces, the MAC scheme of Harlow and Welch) avoid the "checkerboard" pressure that appears when both are stored at the same points. SIMPLE and its relatives (Patankar and Spalding, 1972) do the same job in steady finite-volume codes. In all of them, the pressure Poisson solve dominates the cost.

Common misconceptions

  • "Pressure is a transported quantity." In incompressible flow it is a Lagrange multiplier: it adjusts instantly everywhere to maintain āˆ‡Ā·u = 0.
  • "Boundary conditions are details." In the vorticity method the wall formula carries the entire effect of the walls on the flow.
  • "Converged residuals mean an accurate answer." Convergence tells you the iteration is done. Accuracy needs grid refinement.

Try it

Python Lid-driven cavity at Re = 100

Press Run (or Ctrl + Enter) to see the result here.

Program input Ā· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Grid refinement study

Press Run (or Ctrl + Enter) to see the result here.

Program input Ā· what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The numbers come from running these programs. The benchmark value quoted is the commonly cited reference level for this problem, which you should check against the original paper (Ghia, Ghia and Shin, 1982) before citing it.

Check your understanding

Quick check Ā· 5 questions

of 5 answered You scored / 5 Ā· perfect!

  1. 1. What does the streamfunction ψ guarantee?

    With u = āˆ‚Ļˆ/āˆ‚y and v = -āˆ‚Ļˆ/āˆ‚x, the divergence vanishes identically.
  2. 2. Which equation links ψ to the vorticity?

    Since ω = āˆ‚v/āˆ‚x - āˆ‚u/āˆ‚y = -āˆ‡Ā²Ļˆ, the streamfunction satisfies a Poisson equation with the vorticity as source.
  3. 3. What is Thom's formula used for?

    ω_wall = -2ψ_1/Ī”n² (plus -2U/Ī”n on a moving wall) closes the vorticity equation at the boundary.
  4. 4. In Chorin's projection method, what is the last step?

    The correction u = u* - Ī”t āˆ‡p makes the velocity divergence-free.
  5. 5. Why is a grid refinement study necessary?

    Only comparing solutions on finer and finer meshes shows how close the answer is to the continuous solution.

Summary

  • In 2D, ψ removes pressure and incompressibility; the vorticity equation is advection plus diffusion, and āˆ‡Ā²Ļˆ = -ω closes it.
  • Wall vorticity from Thom's formula carries the no-slip condition; time steps obey Ī”t ≤ Ī”x²/(4ν) and Ī”t ≤ Ī”x/U.
  • Primitive-variable codes use projection (Chorin) or SIMPLE, with a pressure Poisson equation.
  • Always check solutions with a grid refinement study and a benchmark.

Next: Chapter 8 review