Algorithm Search⌘K

Techniqueintro

Vorticity-streamfunction formulation

Also called streamfunction-vorticity method.

Solves 2D incompressible flow for vorticity and streamfunction, which makes the velocity divergence free by construction and removes the pressure.

The vorticity-streamfunction formulation replaces the velocity and the pressure of a two-dimensional incompressible flow by two scalars: the streamfunction ψ\psi, whose derivatives give the velocity, and the vorticity ω\omega, the local rate of rotation. Any velocity computed from a streamfunction is divergence free, so the incompressibility constraint is satisfied by construction, and taking the curl of the momentum equation removes the pressure. What remains is a transport equation for ω\omega and a Poisson equation for ψ\psi. The price is that the method is natural only in two dimensions, and that the wall has two conditions on ψ\psi and none on ω\omega, so the wall vorticity must come from a formula such as Thom's.

In plain words

In a flat flow you can describe the motion by a height map of the streamfunction: the fluid always moves along the lines of equal height, faster where the lines are close together. Fluid that moves along such lines can never pile up or leave a gap, so the hardest condition of the problem holds for free. The second quantity, the spin of each small fluid parcel, is carried along by the flow and spreads out by friction. At every step the method moves the spin forward, then rebuilds the height map from the spin, and so gets the new velocity. The pressure is never needed. At a wall the spin is not known in advance; it is computed from how the height map bends next to the wall.

When it applies

  • Two-dimensional incompressible flow. In 2D the vorticity is a scalar and a single streamfunction exists on a simply connected domain. In 3D the streamfunction becomes a vector potential with three components and harder boundary conditions, and the method loses most of its advantage.
  • Simple geometry. Squares, channels and polar or body-fitted grids around a cylinder, where the Poisson equation for ψ\psi is cheap to solve with SOR, multigrid or a fast transform.
  • When the pressure is not needed inside the iteration. It can be recovered afterwards from a Poisson equation.
  • Teaching and benchmarks. The lid-driven cavity of Ghia, Ghia and Shin (1982), computed in this formulation, is still a standard benchmark for incompressible solvers.

The template

Notation. The velocity is u=∂ψ/∂yu = \partial\psi/\partial y, v=−∂ψ/∂xv = -\partial\psi/\partial x; the vorticity is ω=∂v/∂x−∂u/∂y\omega = \partial v/\partial x - \partial u/\partial y; ν\nu is the kinematic viscosity. Then ∇⋅u=ψyx−ψxy=0\nabla\cdot\mathbf{u} = \psi_{yx} - \psi_{xy} = 0, and the curl of the Navier-Stokes equations gives

ωt+u ωx+v ωy=νΔω,Δψ=−ω.\omega_t + u\,\omega_x + v\,\omega_y = \nu\Delta\omega, \qquad \Delta\psi = -\omega.

On a wall at rest ψ\psi is constant (no flow through the wall) and ∂ψ/∂n\partial\psi/\partial n is given (no slip). Both conditions are on ψ\psi, and ω\omega has none. Thom's formula supplies it: with ψw=0\psi_w = 0 on the wall, ψw+1\psi_{w+1} at the first node inside, a distance hh away, and the tangential wall speed UwU_w, a Taylor series and ωw=−∂2ψ/∂n2\omega_w = -\partial^2\psi/\partial n^2 on the wall give

ωw=−2ψw+1h2∓2Uwh,\omega_w = -\frac{2\psi_{w+1}}{h^2} \mp \frac{2U_w}{h},

with the sign of the second term set by the direction of the wall motion. For the cavity lid at y=1y = 1 moving with speed UU it is ω=−2ψi,n−1/h2−2U/h\omega = -2\psi_{i,n-1}/h^2 - 2U/h. The formula is first order in hh for ωw\omega_w.

  1. CAVITY(n, Re, Δt, U, tol)
  2. repeat
  3. 1ω on the walls ← Thom's formula from ψ next to the walls
  4. 2r ← -u ω_x - v ω_y + ν Δ_h ω at the interior nodes, central differences, u and v from ψ
  5. 3ω ← ω + Δt r at the interior nodes
  6. 4a few SOR sweeps on Δ_h ψ = -ω, with ψ = 0 on the walls
  7. 5if max|r| < tol and the Poisson residual < tol then
  8. 6return (ψ, ω)

At a steady state the time step and the number of SOR sweeps drop out: both discrete equations hold to the tolerance, so the answer depends only on the grid. Cells of size h=1/nh = 1/n give (n−1)2(n - 1)^2 unknowns for each scalar, and one step costs Θ(n2)\Theta(n^2) times the number of sweeps. Recovering the pressure. The divergence of the momentum equation gives Δp=2(uxvy−uyvx)\Delta p = 2(u_x v_y - u_y v_x), with a Neumann condition from the momentum equation at the wall; it is solved once at the end.

Classic examples

Thom's formula by hand. Let h=0.1h = 0.1, a lid speed U=1U = 1, and ψ=−0.05\psi = -0.05 at the first node below the lid. Then ωlid=−2(−0.05)/0.01−2(1)/0.1=10−20=−10\omega_{\text{lid}} = -2(-0.05)/0.01 - 2(1)/0.1 = 10 - 20 = -10. Check: ψ=U(y−1)+a(y−1)2\psi = U(y - 1) + a(y - 1)^2 has ψ=0\psi = 0 and ψy=U\psi_y = U at y=1y = 1, and ψ=−0.1+0.01a=−0.05\psi = -0.1 + 0.01a = -0.05 at y=0.9y = 0.9 gives a=5a = 5, so ω=−ψyy=−2a=−10\omega = -\psi_{yy} = -2a = -10. The formula is exact for this quadratic profile; the test file checks this in exact fractions.

The lid-driven cavity at Re = 100. The unit square, the lid moving with U=1U = 1, ν=0.01\nu = 0.01. The implementation on this page, run to steady state (tolerance 10−510^{-5}), against Ghia, Ghia and Shin (1982), who used a finer grid:

cells Δt\Delta t steps u(0.5,0.5)u(0.5, 0.5) error ψmin⁡\psi_{\min} error
16×1616 \times 16 0.02 1089 −0.17576-0.17576 0.0300 −0.090978-0.090978 0.0124
32×3232 \times 32 0.01 2183 −0.20099-0.20099 0.0048 −0.100381-0.100381 0.0030
64×6464 \times 64 0.004 5354 −0.20719-0.20719 0.0014 −0.102723-0.102723 0.0007
Ghia et al. −0.20581-0.20581 −0.103423-0.103423

The error in ψmin⁡\psi_{\min} falls by a factor of about 4 each time hh is halved, which is second order, although Thom's formula alone is first order. On the 64×6464 \times 64 grid the minimum of uu on the center line is −0.21151-0.21151 at y=0.4531y = 0.4531, against −0.21090-0.21090 at the same height in Ghia's Table I, and the center of the primary vortex is at the node (0.6094,0.7344)(0.6094, 0.7344). These are measurements.

Divergence free by construction. With central differences, the discrete velocity uij=(ψi,j+1−ψi,j−1)/(2h)u_{ij} = (\psi_{i,j+1} - \psi_{i,j-1})/(2h), vij=−(ψi+1,j−ψi−1,j)/(2h)v_{ij} = -(\psi_{i+1,j} - \psi_{i-1,j})/(2h) has a central difference divergence that is exactly zero for every ψ\psi, because the two difference operators commute. The test file checks this in exact fractions for a random ψ\psi.

Variations

  • Implicit time stepping. ADI or fully implicit steps for the vorticity equation remove the explicit limit on Δt\Delta t; a steady solver can also use Newton's method on the coupled discrete equations.
  • Streamfunction only. Eliminating ω\omega gives the fourth order equation ∂tΔψ+ψyΔψx−ψxΔψy=νΔ2ψ\partial_t\Delta\psi + \psi_y\Delta\psi_x - \psi_x\Delta\psi_y = \nu\Delta^2\psi, with both wall conditions on ψ\psi, which removes the need for a wall vorticity formula.
  • Other wall formulas. Higher order one-sided formulas use more interior nodes, for example ωw=(ψw+2−8ψw+1)/(2h2)\omega_w = (\psi_{w+2} - 8\psi_{w+1})/(2h^2) at a wall at rest, which is second order; they are more accurate but can be less stable.
  • Spectral methods. In a periodic box ψ\psi follows from ω\omega by a division in Fourier space, and the method is the standard one for 2D turbulence; see spectral methods.
  • Vortex methods. Vortex particle methods carry the vorticity on Lagrangian particles and recover the velocity by the Biot-Savart law instead of a Poisson solve on a grid.
  • 3D vorticity-vector potential and vorticity-velocity methods exist, but the boundary conditions are harder, and primitive variable methods such as the projection method are more common in 3D.

Pitfalls

  • Only 2D. The scalar streamfunction does not exist in 3D, and the vector potential needs gauge and boundary conditions of its own.
  • Wall vorticity. Thom's formula is the weak point: it is first order, it couples ω\omega on the wall to ψ\psi inside, and an iteration that updates the two with large relaxation can diverge. Updating the wall values once per step, as here, is stable in practice.
  • Explicit time step. The explicit central step needs Δt\Delta t below both the diffusion limit h2/(4ν)h^2/(4\nu) and an advection limit of the order of 2ν/∣u∣22\nu/|\mathbf{u}|^2. On the 16×1616 \times 16 cavity at Re = 100, Δt=0.02\Delta t = 0.02 converges and Δt=0.1\Delta t = 0.1 overflows (measured).
  • Cell Reynolds number. Central differences for the advection give wiggles when ∣u∣h/ν|\mathbf{u}|h/\nu is much larger than 2; upwind differences remove them but add numerical viscosity.
  • Multiply connected domains. Around an obstacle the value of ψ\psi on the obstacle is an unknown constant, fixed by the condition that the pressure is single valued.
  • The pressure is not free. Forces on walls need the pressure, which needs a separate Poisson solve with a Neumann condition.

Code

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

vorticity_streamfunction_formulation.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/vorticity-streamfunction-formulation/. The authors are listed in prov/8HPSC5.jsonl.

"""Vorticity-streamfunction method for the lid-driven cavity.

The unit square has n by n cells of size h = 1 / n. psi[i][j] and w[i][j] are the
streamfunction and the vorticity at the node (i h, j h), i along x and j along y.
u = d psi / dy, v = - d psi / dx, w = dv/dx - du/dy, so Laplace(psi) = -w. The lid
y = 1 moves with speed U; the other walls are at rest. psi = 0 on all walls.

Each step updates the wall vorticity with Thom's formula, moves the interior
vorticity one explicit step (central differences), and does a few SOR sweeps on
Laplace(psi) = -w. At a steady state both equations hold exactly, so the result
does not depend on the number of sweeps.
"""


def zeros(n):
    return [[0.0] * (n + 1) for _ in range(n + 1)]


def thom(psi, w, h, U):
    """Wall vorticity from psi = 0 on the wall, the wall velocity and a Taylor series."""
    n = len(psi) - 1
    for k in range(n + 1):
        w[k][0] = -2 * psi[k][1] / h**2                 # bottom
        w[k][n] = -2 * psi[k][n - 1] / h**2 - 2 * U / h  # lid
        w[0][k] = -2 * psi[1][k] / h**2                 # left
        w[n][k] = -2 * psi[n - 1][k] / h**2             # right


def velocity(psi, h):
    """Central differences of psi at the interior nodes; the walls keep the boundary values."""
    n = len(psi) - 1
    u, v = zeros(n), zeros(n)
    for i in range(1, n):
        for j in range(1, n):
            u[i][j] = (psi[i][j + 1] - psi[i][j - 1]) / (2 * h)
            v[i][j] = -(psi[i + 1][j] - psi[i - 1][j]) / (2 * h)
    return u, v


def vorticity_rate(psi, w, h, nu):
    """-u . grad w + nu Laplace(w) at the interior nodes."""
    n = len(psi) - 1
    r = zeros(n)
    for i in range(1, n):
        for j in range(1, n):
            u = (psi[i][j + 1] - psi[i][j - 1]) / (2 * h)
            v = -(psi[i + 1][j] - psi[i - 1][j]) / (2 * h)
            wx = (w[i + 1][j] - w[i - 1][j]) / (2 * h)
            wy = (w[i][j + 1] - w[i][j - 1]) / (2 * h)
            lap = (w[i + 1][j] + w[i - 1][j] + w[i][j + 1] + w[i][j - 1] - 4 * w[i][j]) / h**2
            r[i][j] = -u * wx - v * wy + nu * lap
    return r


def sor(psi, w, h, sweeps, omega=1.5):
    n = len(psi) - 1
    for _ in range(sweeps):
        for i in range(1, n):
            for j in range(1, n):
                g = (psi[i + 1][j] + psi[i - 1][j] + psi[i][j + 1] + psi[i][j - 1] + h * h * w[i][j]) / 4
                psi[i][j] += omega * (g - psi[i][j])


def poisson_residual(psi, w, h):
    n = len(psi) - 1
    return max(abs((psi[i + 1][j] + psi[i - 1][j] + psi[i][j + 1] + psi[i][j - 1] - 4 * psi[i][j]) / h**2
                   + w[i][j]) for i in range(1, n) for j in range(1, n))


def cavity(n, re, dt, U=1.0, tol=1e-6, maxsteps=100000, sweeps=2, omega=1.5):
    """Steady lid-driven cavity at Reynolds number re = U L / nu. Returns (psi, w, steps)."""
    h = 1.0 / n
    nu = U / re
    psi, w = zeros(n), zeros(n)
    for step in range(1, maxsteps + 1):
        thom(psi, w, h, U)                              # @line L1
        r = vorticity_rate(psi, w, h, nu)               # @line L2
        for i in range(1, n):
            for j in range(1, n):
                w[i][j] += dt * r[i][j]                 # @line L3
        sor(psi, w, h, sweeps, omega)                   # @line L4
        change = max(abs(x) for row in r for x in row)
        if not change < 1e30:
            raise OverflowError("the explicit step is unstable")
        if change < tol and poisson_residual(psi, w, h) < tol:  # @line L5
            thom(psi, w, h, U)
            return psi, w, step                         # @line L6
    raise RuntimeError("no steady state")


def centerline_u(psi, h):
    """u on the vertical line x = 1/2 (n even), from the bottom to the lid, with u = 1 at the lid."""
    n = len(psi) - 1
    u, _ = velocity(psi, h)
    col = [u[n // 2][j] for j in range(n + 1)]
    col[n] = 1.0
    return col
test_vorticity_streamfunction_formulation.py 57 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/vorticity-streamfunction-formulation/. The authors are listed in prov/8HPSC5.jsonl.

import random
from fractions import Fraction

import pytest

from vorticity_streamfunction_formulation import cavity, centerline_u, poisson_residual, thom, velocity

GHIA_U_HALF = -0.20581   # Ghia, Ghia and Shin 1982, Table I, Re = 100, u at (0.5, 0.5)
GHIA_PSI_MIN = -0.103423  # the same paper, Re = 100, primary vortex


def test_thom_is_exact_for_a_quadratic_streamfunction():
    # psi = a y^2 at the bottom (wall at rest) and psi = U (y - 1) + a (y - 1)^2 at the lid:
    # both have w = -psi_yy = -2a on the wall, and Thom's formula gives it exactly.
    n, a, U = 4, Fraction(3, 7), Fraction(5, 2)
    h = Fraction(1, n)
    psi = [[Fraction(0)] * (n + 1) for _ in range(n + 1)]
    for i in range(1, n):
        psi[i][1] = a * h * h
        psi[i][n - 1] = U * (-h) + a * h * h
    w = [[Fraction(0)] * (n + 1) for _ in range(n + 1)]
    thom(psi, w, h, U)
    for i in range(1, n):
        assert w[i][0] == -2 * a
        assert w[i][n] == -2 * a


def test_velocity_from_streamfunction_is_exactly_divergence_free():
    rng = random.Random(5)
    n = 8
    h = Fraction(1, n)
    psi = [[Fraction(rng.randint(-9, 9)) for _ in range(n + 1)] for _ in range(n + 1)]
    u, v = velocity(psi, h)
    for i in range(2, n - 1):
        for j in range(2, n - 1):
            assert (u[i + 1][j] - u[i - 1][j]) / (2 * h) + (v[i][j + 1] - v[i][j - 1]) / (2 * h) == 0


def test_cavity_re100_on_16_cells_is_near_ghia():
    psi, w, steps = cavity(16, 100.0, 0.02, tol=1e-5)
    h = 1 / 16
    assert poisson_residual(psi, w, h) < 1e-5
    col = centerline_u(psi, h)
    pmin = min(min(r) for r in psi)
    assert abs(col[8] - (-0.17576)) < 1e-4 and abs(pmin - (-0.090978)) < 1e-5  # measured
    assert abs(col[8] - GHIA_U_HALF) < 0.035 and abs(pmin - GHIA_PSI_MIN) < 0.013
    # The steady state does not depend on the number of SOR sweeps per step.
    psi4, _, steps4 = cavity(16, 100.0, 0.02, tol=1e-5, sweeps=4)
    assert max(abs(psi4[i][j] - psi[i][j]) for i in range(17) for j in range(17)) < 1e-5


def test_explicit_step_above_the_limit_blows_up():
    with pytest.raises(OverflowError):
        cavity(16, 100.0, 0.1, maxsteps=2000)

Exercises

  1. Show that ∇⋅u=0\nabla\cdot\mathbf{u} = 0 for u=ψyu = \psi_y, v=−ψxv = -\psi_x, and that the curl of the momentum equation gives the vorticity transport equation in 2D.
  2. Derive Thom's formula from the Taylor series of ψ\psi normal to the wall, and show that its error is O(h)O(h).
  3. Show that the central difference divergence of the discrete velocity above is exactly zero.
  4. On the unit cavity with h=1/2h = 1/2 there is one interior node. Write the steady discrete equations and solve them for small Re.
  5. Derive the pressure Poisson equation Δp=2(uxvy−uyvx)\Delta p = 2(u_x v_y - u_y v_x) for a divergence free 2D velocity.

References

  1. Alexander Thom, The flow past circular cylinders at low speeds, Proceedings of the Royal Society of London A 141(845) (1933) 651-669, the early hand computation of viscous flow in streamfunction and vorticity, to which the wall vorticity formula is usually credited
  2. U. Ghia, K. N. Ghia and C. T. Shin, High-Re solutions for incompressible flow using the Navier-Stokes equations and a multigrid method, Journal of Computational Physics 48(3) (1982) 387-411, the lid-driven cavity benchmark in streamfunction and vorticity; Table I (u on the vertical center line) and the primary vortex values for Re = 100
  3. Joel H. Ferziger, Milovan Perić and Robert L. Street, Computational Methods for Fluid Dynamics, 4th edition, Springer, 2020, the streamfunction-vorticity approach among the methods for incompressible flow

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • three classic examples