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 , whose derivatives give the velocity, and the vorticity , 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 and a Poisson equation for . The price is that the method is natural only in two dimensions, and that the wall has two conditions on and none on , 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 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 , ; the vorticity is ; is the kinematic viscosity. Then , and the curl of the Navier-Stokes equations gives
On a wall at rest is constant (no flow through the wall) and is given (no slip). Both conditions are on , and has none. Thom's formula supplies it: with on the wall, at the first node inside, a distance away, and the tangential wall speed , a Taylor series and on the wall give
with the sign of the second term set by the direction of the wall motion. For the cavity lid at moving with speed it is . The formula is first order in for .
CAVITY(n, Re, Δt, U, tol)repeat- 1
ω on the walls ← Thom's formula from ψ next to the walls - 2
r ← -u ω_x - v ω_y + ν Δ_h ω at the interior nodes, central differences, u and v from ψ - 3
ω ← ω + Δt r at the interior nodes - 4
a few SOR sweeps on Δ_h ψ = -ω, with ψ = 0 on the walls - 5
if max|r| < tol and the Poisson residual < tol then - 6
return (ψ, ω)
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 give unknowns for each scalar, and one step costs times the number of sweeps. Recovering the pressure. The divergence of the momentum equation gives , 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 , a lid speed , and at the first node below the lid. Then . Check: has and at , and at gives , so . 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 , . The implementation on this page, run to steady state (tolerance ), against Ghia, Ghia and Shin (1982), who used a finer grid:
| cells | steps | error | error | |||
|---|---|---|---|---|---|---|
| 0.02 | 1089 | 0.0300 | 0.0124 | |||
| 0.01 | 2183 | 0.0048 | 0.0030 | |||
| 0.004 | 5354 | 0.0014 | 0.0007 | |||
| Ghia et al. |
The error in falls by a factor of about 4 each time is halved, which is second order, although Thom's formula alone is first order. On the grid the minimum of on the center line is at , against at the same height in Ghia's Table I, and the center of the primary vortex is at the node . These are measurements.
Divergence free by construction. With central differences, the discrete velocity , has a central difference divergence that is exactly zero for every , because the two difference operators commute. The test file checks this in exact fractions for a random .
Variations
- Implicit time stepping. ADI or fully implicit steps for the vorticity equation remove the explicit limit on ; a steady solver can also use Newton's method on the coupled discrete equations.
- Streamfunction only. Eliminating gives the fourth order equation , with both wall conditions on , which removes the need for a wall vorticity formula.
- Other wall formulas. Higher order one-sided formulas use more interior nodes, for example at a wall at rest, which is second order; they are more accurate but can be less stable.
- Spectral methods. In a periodic box follows from 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 on the wall to 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 below both the diffusion limit and an advection limit of the order of . On the cavity at Re = 100, converges and overflows (measured).
- Cell Reynolds number. Central differences for the advection give wiggles when is much larger than 2; upwind differences remove them but add numerical viscosity.
- Multiply connected domains. Around an obstacle the value of 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.
# 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
- Show that for , , and that the curl of the momentum equation gives the vorticity transport equation in 2D.
- Derive Thom's formula from the Taylor series of normal to the wall, and show that its error is .
- Show that the central difference divergence of the discrete velocity above is exactly zero.
- On the unit cavity with there is one interior node. Write the steady discrete equations and solve them for small Re.
- Derive the pressure Poisson equation for a divergence free 2D velocity.
References
- 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
- 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
- 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.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag 8HPSC5 Edit this page History Report an error Markdown