MacCormack Predictor-Corrector Clamped Fluid Advection
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.
Also known as unconditionally stable MacCormack, clamped MacCormack advection, predictor-corrector semi-Lagrangian advection
In the family Simulation & animation, grid fluids
Where it runs
Built inOfficial add-onPluginNone foundNot researched
| Engine | Support | How |
|---|---|---|
| three.js r187 | None found | No built-in Eulerian fluid solver or MacCormack predictor-corrector clamped advection pass. |
| Babylon.js 9.38.0 | None found | |
| Godot 4.7 | None found | No built-in Eulerian grid fluid solver or MacCormack clamped advection compute pass. |
| Unity | None found | No built-in Eulerian grid fluid solver or MacCormack clamped advection pass in URP/HDRP. |
| Unreal 5.7 | None found | Niagara Fluids Grid2D/Grid3D ships semi-Lagrangian and FLIP advection stages rather than a built-in clamped MacCormack advection module. |
| Blender 5.2 | None found | |
| WebGPU W3C | None found | |
| Metal | None found |
How it works
First-order semi-Lagrangian advection in 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 φ̂n+1 = A(φn). Next, a reverse-velocity trace advects that estimate backward to approximate the original field, φ̂n = AR(φ̂n+1). Half of the round-trip discrepancy (φn - φ̂n) approximates the local truncation error and is added back: φn+1 = φ̂n+1 + 1/2(φn - φ̂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× 2 (2D) or 2× 2× 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 choose it
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
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 with plain semi-Lagrangian advection when smooth velocity fields matter more than scalar sharpness or GPU bandwidth is tightly constrained. Prefer FLIP/PIC fluids when simulating splashing liquids with sharp free surfaces using particles. Prefer Lattice Boltzmann fluids when you want local collision-streaming updates without global Poisson pressure solves.
Field marks
- 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
Presets
MacCormack Clamped Advection — diffusive semi-Lagrangian (left) vs clamped 2nd-order predictor-corrector (right)
{
"P_ADVECTION_DT": 0.42,
"P_VORTEX_GAIN": 1,
"P_CLAMP_TIGHT": 1,
"P_SPLIT": 0.48
}Show GLSL (114 lines)
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.
Measured GPU weight & timing
| Preset | GPU | Weight / p50 / p95 | Method |
|---|---|---|---|
| maccormack-clamped-advection-live | Tesla T4 1920x1080 | 71.8× baseline (extreme) · 2.23 ms / 2.38 ms | opengl-timer-query · 36 frames · GPU Weight Atlas · Colab T4 sweep |
| maccormack-clamped-advection-live | NVIDIA L4 1920x1080 | 53.8× baseline (heavy) · 0.856 ms / 0.865 ms · 2.6× faster on L4 | opengl-timer-query · Cross-architecture validation |
| maccormack-clamped-advection-live | NVIDIA A100-SXM4-40GB 1920x1080 | 45.8× baseline (heavy) · 1.32 ms / 1.32 ms · 1.7× faster on A100 | opengl-timer-query · Cross-architecture validation |
Lineage
Crosses
- Billowing Nubis volumetric clouds driven by MacCormack clamped advection: Advecting cloud moisture and cumulus anvil density with second-order MacCormack clamped advection keeps curling thunderhead edges razor-crisp over minutes of live weather evolution, eliminating the syrupy blurring of standard semi-Lagrangian wind scroll.
- Stable Fluids: Replaces the semi-Lagrangian scalar advection pass inside a projection-based Eulerian solver to keep smoke and flame detail sharp.
- Ray-marched Participating Media: High-frequency density and temperature filaments preserved by MacCormack advection translate directly into crisp volumetric fire and smoke.
Sources
- primaryAndrew Selle, Ronald Fedkiw, ByungMoon Kim, Yingjie Liu, Jarek Rossignac (2007). An Unconditionally Stable MacCormack Method. Journal of Scientific Computing.Verified via crossref · 2026-11-30doi:10.1007/s10915-007-9166-4
- implementationlejrimostfa (2026). lejrimostfa/force-field-vfx-threejs. Real-time 3D fire and smoke on three.js WebGPU: a force field VFX solved in the browser.GitHub API · license MIT · 2026-11-30