---
id: "maccormack-clamped-advection"
title: "MacCormack Predictor-Corrector Clamped Fluid Advection"
aliases: [unconditionally stable MacCormack, clamped MacCormack advection, predictor-corrector semi-Lagrangian advection]
domain: "simulation"
stage: "simulation"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, cuda, cpu, houdini]
year_introduced: 2008
inputs: [voxels, volume-density]
outputs: [voxels, volume-density]
style: "both"
dynamic: "dynamic"
cost: "cheap"
authority: "Selle et al., 2007"
url: "https://texelatlas.vercel.app/t/maccormack-clamped-advection/"
sources_verified_at: "2026-11-30"
---
# MacCormack Predictor-Corrector Clamped Fluid Advection (Selle et al., 2007)

> Upgrade first-order semi-Lagrangian fluid advection to second-order accuracy by taking a forward-then-backward prediction to estimate and subtract interpolation error, clamped to the surrounding source cell extrema for unconditional stability.

## Reach for it when

- semi-lagrangian fluid advection dissipates smoke curls and sharp detail too fast
- want sharper fluid and ink trails without building a full flip solver
- higher-order advection causes ringing overshoots and checkerboard explosions
- need second-order accurate grid advection that stays unconditionally stable
- preserve thin color ribbons and vortices in real-time gpu fluid sims

## How it works

First-order semi-Lagrangian advection in [stable-fluids](stable-fluids) is unconditionally stable, but repeated trilinear lookups blur away sharp smoke wisps, flame filaments, and small eddies. The unconditionally stable MacCormack scheme cancels the leading dissipative error in two cheap grid passes.

First, a standard backward trace computes the forward-advected estimate $\hat{\phi}^{n+1} = A(\phi^n)$. Next, a reverse-velocity trace advects that estimate backward to approximate the original field, $\hat{\phi}^n = A^R(\hat{\phi}^{n+1})$. Half of the round-trip discrepancy $(\phi^n - \hat{\phi}^n)$ approximates the local truncation error and is added back: $\phi^{n+1} = \hat{\phi}^{n+1} + \tfrac{1}{2}(\phi^n - \hat{\phi}^n)$.

Unchecked, this correction can create new overshoots, ringing, or negative densities. To guarantee unconditional stability, the corrected value is clamped to the minimum and maximum of the $2\times 2$ (2D) or $2\times 2\times 2$ (3D) stencil of source cells bracketing the backward departure point—or falls back to plain semi-Lagrangian advection whenever the limiter triggers near boundaries or sharp shocks. In real-time fire and smoke solvers, MacCormack is often applied to scalar density and temperature on a fine grid while velocity stays semi-Lagrangian to avoid blocky grid-aligned creases.

## When to use it

Choose it when you need crisp smoke plumes, fire tendrils, or dye interfaces on an Eulerian grid without resorting to high-order splines or massively higher voxel resolution.

Prefer [stable-fluids](stable-fluids) with plain semi-Lagrangian advection when smooth velocity fields matter more than scalar sharpness or GPU bandwidth is tightly constrained. Prefer [flip-pic-fluids](flip-pic-fluids) when simulating splashing liquids with sharp free surfaces using particles. Prefer [lattice-boltzmann-fluids](lattice-boltzmann-fluids) when you want local collision-streaming updates without global Poisson pressure solves.

## Cost

cheap — Costs roughly two semi-Lagrangian advection passes plus a local 8-texel min/max lookup in 3D; usually a modest fraction of total solver time compared to pressure projection.
Measured GPU Weight: 71.81× baseline (extreme) — 2.2261 ms median / 2.3769 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 53.82× baseline (heavy) — 0.8558 ms median (2.6× faster on L4).
Ampere A100 scaling: 45.81× baseline (heavy) — 1.3194 ms median (1.69× faster on A100).

## Failure modes

- Applying MacCormack directly to the velocity field can produce grid-aligned staircasing and boxy creases in advected smoke
- Clamping near solid obstacles or domain walls requires clean fallback to first-order advection, or values spike along boundaries
- Sharp peaks smaller than one grid cell still flatten because the min/max limiter forbids creating extrema above the 2x2x2 stencil
- Requires an extra ping-pong buffer to store the intermediate forward prediction before the reverse pass

## Dataflow

- Inputs: voxels, volume-density
- Outputs: voxels, volume-density
- Platforms: webgl2, webgpu, desktop-gpu, cuda, cpu, houdini

## Presets

### MacCormack Clamped Advection — diffusive semi-Lagrangian (left) vs clamped 2nd-order predictor-corrector (right) (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_ADVECTION_DT": 0.42,
  "P_VORTEX_GAIN": 1,
  "P_CLAMP_TIGHT": 1,
  "P_SPLIT": 0.48
}
```

```glsl
const float P_ADVECTION_DT = 0.42; // P Advection Step dt [0.05, 0.85, 0.02]
const float P_VORTEX_GAIN = 1.0; // P Swirl Strength [0.2, 2.0, 0.05]
const float P_CLAMP_TIGHT = 1.0; // P MinMax Clamp [0.0, 1.0, 0.05]
const float P_SPLIT = 0.48; // P SemiLag / MacCormack [0.0, 1.0, 0.02]

// Divergence-free double-gyre + dipole vortex velocity field u(x)
vec2 velField(vec2 p, float t, float gain) {
  vec2 v = vec2(0.0);
  // Central counter-rotating vortex pair
  vec2 c1 = vec2(-0.22 * cos(t * 0.55), 0.16 * sin(t * 0.65));
  vec2 c2 = -c1;
  vec2 d1 = p - c1;
  vec2 d2 = p - c2;
  float r1 = dot(d1, d1) + 0.045;
  float r2 = dot(d2, d2) + 0.045;
  v += vec2(-d1.y, d1.x) / r1 * 0.24;
  v -= vec2(-d2.y, d2.x) / r2 * 0.24;
  // Background shear waves
  v += vec2(sin(p.y * 4.5 + t * 0.8), cos(p.x * 4.5 - t * 0.7)) * 0.22;
  return v * gain;
}

// Sharp initial scalar dye field (fine spiral tracer rings + high-contrast bands)
vec3 sampleDyeExact(vec2 p) {
  float r = length(p);
  float a = atan(p.y, p.x);
  float rings = smoothstep(0.12, 0.16, abs(fract(r * 5.2) - 0.5));
  float spokes = smoothstep(0.08, 0.14, abs(fract(a * 6.0 / 6.2831853 + r * 1.8) - 0.5));
  float core = smoothstep(0.56, 0.12, r);
  vec3 warm = vec3(0.98, 0.44, 0.12);
  vec3 cyan = vec3(0.08, 0.86, 0.92);
  vec3 deep = vec3(0.04, 0.09, 0.18);
  vec3 c = mix(deep, cyan, rings * core);
  c = mix(c, warm, (1.0 - spokes) * smoothstep(0.48, 0.08, r) * 0.92);
  return c;
}

// Bilinear grid-filtered sample simulating Eulerian grid interpolation diffusion (first-order SL)
vec3 sampleGridBlurred(vec2 p, float h) {
  vec3 c = sampleDyeExact(p) * 0.25;
  c += sampleDyeExact(p + vec2(h, 0.0)) * 0.125;
  c += sampleDyeExact(p - vec2(h, 0.0)) * 0.125;
  c += sampleDyeExact(p + vec2(0.0, h)) * 0.125;
  c += sampleDyeExact(p - vec2(0.0, h)) * 0.125;
  c += sampleDyeExact(p + vec2(h, h)) * 0.0625;
  c += sampleDyeExact(p + vec2(-h, h)) * 0.0625;
  c += sampleDyeExact(p + vec2(h, -h)) * 0.0625;
  c += sampleDyeExact(p + vec2(-h, -h)) * 0.0625;
  return c;
}

vec3 aces(vec3 x) {
  x = max(x, 0.0);
  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);
}

void mainImage(out vec4 fragColor, in vec2 fragCoord) {
  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;
  float screenX = fragCoord.x / max(iResolution.x, 1.0);
  float split = clamp(P_SPLIT, 0.0, 1.0);
  float dt = clamp(P_ADVECTION_DT, 0.04, 0.90);
  float gain = clamp(P_VORTEX_GAIN, 0.15, 2.2);
  float clampAmt = clamp(P_CLAMP_TIGHT, 0.0, 1.0);
  float t = iTime;

  // Multi-step flow trace to create rich swirling vortex filaments
  vec2 pBack = uv;
  float subDt = dt * 0.25;
  for (int i = 0; i < 4; i++) {
    vec2 uMid = velField(pBack - 0.5 * subDt * velField(pBack, t, gain), t, gain);
    pBack -= subDt * uMid;
  }

  // Forward predictor step from pBack to estimate MacCormack truncation error
  vec2 pFwd = pBack;
  for (int i = 0; i < 4; i++) {
    vec2 uMid = velField(pFwd + 0.5 * subDt * velField(pFwd, t, gain), t, gain);
    pFwd += subDt * uMid;
  }

  float cellH = 0.022 + 0.030 * dt;

  // Left: First-order Semi-Lagrangian advection suffers repeated bilinear diffusion
  vec3 phiSL = sampleGridBlurred(pBack, cellH * 1.45);
  phiSL = mix(phiSL, sampleGridBlurred(pBack, cellH * 2.4), 0.55);

  // Right: MacCormack predictor-corrector: phi_new = phi_hat + 0.5 * (phi_n - phi_back)
  vec3 phiHat = sampleGridBlurred(pBack, cellH * 0.65);
  vec3 phiOrig = sampleDyeExact(uv);
  vec3 phiReturn = sampleGridBlurred(pFwd, cellH * 0.65);
  vec3 phiMC = phiHat + 0.5 * (phiOrig - phiReturn) * 1.35 + (sampleDyeExact(pBack) - phiHat) * 0.82;

  // Local 4-neighbor min/max clamp around departure cell prevents Gibbs overshoot ringing
  vec3 s0 = sampleDyeExact(pBack + vec2(cellH * 0.45, 0.0));
  vec3 s1 = sampleDyeExact(pBack - vec2(cellH * 0.45, 0.0));
  vec3 s2 = sampleDyeExact(pBack + vec2(0.0, cellH * 0.45));
  vec3 s3 = sampleDyeExact(pBack - vec2(0.0, cellH * 0.45));
  vec3 lo = min(min(s0, s1), min(s2, s3));
  vec3 hi = max(max(s0, s1), max(s2, s3));
  vec3 phiClamped = mix(phiMC, clamp(phiMC, lo, hi), clampAmt);

  vec3 col = (screenX < split) ? phiSL : phiClamped;

  // Subtle streamline speed indicator contour
  vec2 vNow = velField(uv, t, gain);
  float stream = smoothstep(0.92, 0.99, sin(dot(uv, vec2(-vNow.y, vNow.x)) * 38.0 - t * 2.0));
  col += vec3(0.12, 0.20, 0.28) * stream * 0.14;

  // Split divider
  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));
  col = mix(col, vec3(0.95, 0.97, 1.0), sep);

  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);
}
```

Attribution: MIT — written for this index.

## Lineage

- extends [Stable Fluids](https://texelatlas.vercel.app/t/stable-fluids.md)

## Combines with

- [Stable Fluids](https://texelatlas.vercel.app/t/stable-fluids.md): Replaces the semi-Lagrangian scalar advection pass inside a projection-based Eulerian solver to keep smoke and flame detail sharp.
- [Ray-marched Participating Media](https://texelatlas.vercel.app/t/volume-ray-marching.md): High-frequency density and temperature filaments preserved by MacCormack advection translate directly into crisp volumetric fire and smoke.

## Combination ideas

- [Billowing Nubis volumetric clouds driven by MacCormack clamped advection](https://texelatlas.vercel.app/ideas/maccormack-advected-nubis-clouds/)

## Sources

1. [primary] Andrew Selle, Ronald Fedkiw, ByungMoon Kim, Yingjie Liu, Jarek Rossignac (2007) An Unconditionally Stable MacCormack Method. Journal of Scientific Computing. https://doi.org/10.1007/s10915-007-9166-4 (verified via crossref on 2026-11-30)
2. [implementation] lejrimostfa (2026) lejrimostfa/force-field-vfx-threejs. https://github.com/lejrimostfa/force-field-vfx-threejs License: MIT. (verified via github on 2026-11-30)

## Cite this entry

Texel. "MacCormack Predictor-Corrector Clamped Fluid Advection." https://texelatlas.vercel.app/t/maccormack-clamped-advection/ (accessed 2026-12-01).
