{
 "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",
 "subdomain": "grid fluids",
 "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",
 "summary": "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.",
 "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.\n\nFirst, 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)$.\n\nUnchecked, 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": "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.\n\nPrefer [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": {
  "class": "cheap",
  "notes": "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."
 },
 "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"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1007/s10915-007-9166-4",
   "type": "paper",
   "title": "An Unconditionally Stable MacCormack Method",
   "authors": [
    "Andrew Selle",
    "Ronald Fedkiw",
    "ByungMoon Kim",
    "Yingjie Liu",
    "Jarek Rossignac"
   ],
   "year": 2007,
   "venue": "Journal of Scientific Computing",
   "url": "https://doi.org/10.1007/s10915-007-9166-4",
   "ids": {
    "doi": "10.1007/s10915-007-9166-4"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/s10915-007-9166-4",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/s10915-007-9166-4",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/s10915-007-9166-4",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/s10915-007-9166-4",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1007/s10915-007-9166-4",
     "checked_at": "2026-11-30T17:59:51.239Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-30T17:59:51.239Z",
   "title_sim": 1,
   "__bib": "selle2007unconditionally"
  },
  {
   "role": "implementation",
   "key": "gh:lejrimostfa/force-field-vfx-threejs",
   "type": "code",
   "title": "lejrimostfa/force-field-vfx-threejs",
   "description": "Real-time 3D fire and smoke on three.js WebGPU: a force field VFX solved in the browser.",
   "authors": [
    "lejrimostfa"
   ],
   "year": 2026,
   "venue": "GitHub",
   "url": "https://github.com/lejrimostfa/force-field-vfx-threejs",
   "ids": {
    "repo": "lejrimostfa/force-field-vfx-threejs"
   },
   "license": "MIT",
   "extra": {
    "stars": 30,
    "stars_checked": "2026-11-30",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-30T10:23:01Z",
    "language": "JavaScript",
    "homepage": "https://lejrimostfa.github.io/force-field-vfx-threejs/",
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/lejrimostfa/force-field-vfx-threejs",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/lejrimostfa/force-field-vfx-threejs",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/lejrimostfa/force-field-vfx-threejs",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/lejrimostfa/force-field-vfx-threejs",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/lejrimostfa/force-field-vfx-threejs",
     "checked_at": "2026-11-30T17:59:51.239Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/lejrimostfa/force-field-vfx-threejs",
     "checked_at": "2026-11-30T17:59:51.239Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-30T17:59:51.239Z",
   "title_sim": null,
   "__bib": "lejrimostfa2026lejrimostfa"
  }
 ],
 "presets": [
  {
   "id": "maccormack-clamped-advection-live",
   "title": "MacCormack Clamped Advection — diffusive semi-Lagrangian (left) vs clamped 2nd-order predictor-corrector (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_ADVECTION_DT": 0.42,
    "P_VORTEX_GAIN": 1,
    "P_CLAMP_TIGHT": 1,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_ADVECTION_DT = 0.42; // P Advection Step dt [0.05, 0.85, 0.02]\nconst float P_VORTEX_GAIN = 1.0; // P Swirl Strength [0.2, 2.0, 0.05]\nconst float P_CLAMP_TIGHT = 1.0; // P MinMax Clamp [0.0, 1.0, 0.05]\nconst float P_SPLIT = 0.48; // P SemiLag / MacCormack [0.0, 1.0, 0.02]\n\n// Divergence-free double-gyre + dipole vortex velocity field u(x)\nvec2 velField(vec2 p, float t, float gain) {\n  vec2 v = vec2(0.0);\n  // Central counter-rotating vortex pair\n  vec2 c1 = vec2(-0.22 * cos(t * 0.55), 0.16 * sin(t * 0.65));\n  vec2 c2 = -c1;\n  vec2 d1 = p - c1;\n  vec2 d2 = p - c2;\n  float r1 = dot(d1, d1) + 0.045;\n  float r2 = dot(d2, d2) + 0.045;\n  v += vec2(-d1.y, d1.x) / r1 * 0.24;\n  v -= vec2(-d2.y, d2.x) / r2 * 0.24;\n  // Background shear waves\n  v += vec2(sin(p.y * 4.5 + t * 0.8), cos(p.x * 4.5 - t * 0.7)) * 0.22;\n  return v * gain;\n}\n\n// Sharp initial scalar dye field (fine spiral tracer rings + high-contrast bands)\nvec3 sampleDyeExact(vec2 p) {\n  float r = length(p);\n  float a = atan(p.y, p.x);\n  float rings = smoothstep(0.12, 0.16, abs(fract(r * 5.2) - 0.5));\n  float spokes = smoothstep(0.08, 0.14, abs(fract(a * 6.0 / 6.2831853 + r * 1.8) - 0.5));\n  float core = smoothstep(0.56, 0.12, r);\n  vec3 warm = vec3(0.98, 0.44, 0.12);\n  vec3 cyan = vec3(0.08, 0.86, 0.92);\n  vec3 deep = vec3(0.04, 0.09, 0.18);\n  vec3 c = mix(deep, cyan, rings * core);\n  c = mix(c, warm, (1.0 - spokes) * smoothstep(0.48, 0.08, r) * 0.92);\n  return c;\n}\n\n// Bilinear grid-filtered sample simulating Eulerian grid interpolation diffusion (first-order SL)\nvec3 sampleGridBlurred(vec2 p, float h) {\n  vec3 c = sampleDyeExact(p) * 0.25;\n  c += sampleDyeExact(p + vec2(h, 0.0)) * 0.125;\n  c += sampleDyeExact(p - vec2(h, 0.0)) * 0.125;\n  c += sampleDyeExact(p + vec2(0.0, h)) * 0.125;\n  c += sampleDyeExact(p - vec2(0.0, h)) * 0.125;\n  c += sampleDyeExact(p + vec2(h, h)) * 0.0625;\n  c += sampleDyeExact(p + vec2(-h, h)) * 0.0625;\n  c += sampleDyeExact(p + vec2(h, -h)) * 0.0625;\n  c += sampleDyeExact(p + vec2(-h, -h)) * 0.0625;\n  return c;\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  float dt = clamp(P_ADVECTION_DT, 0.04, 0.90);\n  float gain = clamp(P_VORTEX_GAIN, 0.15, 2.2);\n  float clampAmt = clamp(P_CLAMP_TIGHT, 0.0, 1.0);\n  float t = iTime;\n\n  // Multi-step flow trace to create rich swirling vortex filaments\n  vec2 pBack = uv;\n  float subDt = dt * 0.25;\n  for (int i = 0; i < 4; i++) {\n    vec2 uMid = velField(pBack - 0.5 * subDt * velField(pBack, t, gain), t, gain);\n    pBack -= subDt * uMid;\n  }\n\n  // Forward predictor step from pBack to estimate MacCormack truncation error\n  vec2 pFwd = pBack;\n  for (int i = 0; i < 4; i++) {\n    vec2 uMid = velField(pFwd + 0.5 * subDt * velField(pFwd, t, gain), t, gain);\n    pFwd += subDt * uMid;\n  }\n\n  float cellH = 0.022 + 0.030 * dt;\n\n  // Left: First-order Semi-Lagrangian advection suffers repeated bilinear diffusion\n  vec3 phiSL = sampleGridBlurred(pBack, cellH * 1.45);\n  phiSL = mix(phiSL, sampleGridBlurred(pBack, cellH * 2.4), 0.55);\n\n  // Right: MacCormack predictor-corrector: phi_new = phi_hat + 0.5 * (phi_n - phi_back)\n  vec3 phiHat = sampleGridBlurred(pBack, cellH * 0.65);\n  vec3 phiOrig = sampleDyeExact(uv);\n  vec3 phiReturn = sampleGridBlurred(pFwd, cellH * 0.65);\n  vec3 phiMC = phiHat + 0.5 * (phiOrig - phiReturn) * 1.35 + (sampleDyeExact(pBack) - phiHat) * 0.82;\n\n  // Local 4-neighbor min/max clamp around departure cell prevents Gibbs overshoot ringing\n  vec3 s0 = sampleDyeExact(pBack + vec2(cellH * 0.45, 0.0));\n  vec3 s1 = sampleDyeExact(pBack - vec2(cellH * 0.45, 0.0));\n  vec3 s2 = sampleDyeExact(pBack + vec2(0.0, cellH * 0.45));\n  vec3 s3 = sampleDyeExact(pBack - vec2(0.0, cellH * 0.45));\n  vec3 lo = min(min(s0, s1), min(s2, s3));\n  vec3 hi = max(max(s0, s1), max(s2, s3));\n  vec3 phiClamped = mix(phiMC, clamp(phiMC, lo, hi), clampAmt);\n\n  vec3 col = (screenX < split) ? phiSL : phiClamped;\n\n  // Subtle streamline speed indicator contour\n  vec2 vNow = velField(uv, t, gain);\n  float stream = smoothstep(0.92, 0.99, sin(dot(uv, vec2(-vNow.y, vNow.x)) * 38.0 - t * 2.0));\n  col += vec3(0.12, 0.20, 0.28) * stream * 0.14;\n\n  // Split divider\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.97, 1.0), sep);\n\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "maccormack-clamped-advection",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:53:30.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.5278,
      "std": 0.1697,
      "min": 0.3415,
      "max": 0.9014
     },
     "image": "/specimens/maccormack-clamped-advection-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "stable-fluids",
  "flip-pic-fluids",
  "lattice-boltzmann-fluids",
  "curl-noise-flow"
 ],
 "extends": [
  "stable-fluids"
 ],
 "supersedes": [],
 "combines_with": [
  {
   "id": "stable-fluids",
   "why": "Replaces the semi-Lagrangian scalar advection pass inside a projection-based Eulerian solver to keep smoke and flame detail sharp."
  },
  {
   "id": "volume-ray-marching",
   "why": "High-frequency density and temperature filaments preserved by MacCormack advection translate directly into crisp volumetric fire and smoke."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-30T17:59:51.239Z",
  "draft": "data/drafts/simulation/maccormack-clamped-advection.json"
 },
 "solves": [
  "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"
 ],
 "authority": "Selle et al., 2007",
 "url": "https://texelatlas.vercel.app/t/maccormack-clamped-advection/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 71.81,
  "tier": "extreme",
  "p50_ms": 2.2261,
  "p95_ms": 2.3769,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 36,
  "preset_id": "maccormack-clamped-advection-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 53.82,
   "tier": "heavy",
   "p50_ms": 0.8558,
   "p95_ms": 0.8653,
   "speedup_x": 2.6,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 45.81,
   "tier": "heavy",
   "p50_ms": 1.3194,
   "p95_ms": 1.321,
   "speedup_x": 1.69,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}