{
 "id": "sph-fluids",
 "title": "Smoothed Particle Hydrodynamics (fluids)",
 "aliases": [
  "SPH"
 ],
 "domain": "simulation",
 "subdomain": "particle fluids",
 "stage": "simulation",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "cuda",
  "cpu"
 ],
 "year_introduced": 1992,
 "inputs": [
  "particles"
 ],
 "outputs": [
  "particles"
 ],
 "style": "both",
 "dynamic": "dynamic",
 "summary": "Meshless Lagrangian fluid method: each particle's density, pressure and viscosity forces come from kernel-weighted sums over neighbouring particles.",
 "how_it_works": "Fields are interpolated with a smoothing kernel `W(r,h)`: density is `sum_j m_j W(|x_i-x_j|,h)`, pressure force and viscosity force are kernel gradients/Laplacians of those quantities. Pressure comes from an equation of state (weakly-compressible SPH, stiff and needing tiny steps) or from a pressure solve (PCISPH, IISPH, DFSPH), which allow much larger steps. Neighbours are found with a uniform grid or spatial hash and sorted for coherence on the GPU. Interactive-graphics SPH popularised specialised kernels (poly6 for density, spiky for pressure, viscosity kernel for the Laplacian) to avoid clumping. Surface tension and boundaries are extra particle forces or boundary particles.",
 "when_to_use": "Choose it when you want a simple, meshless liquid with free surfaces, easy two-way rigid coupling and multiphase support. Prefer `position-based-fluids` for faster, more game-oriented stability, `flip-pic-fluids` for large-scale splashes with less particle-pairing noise, and `lattice-boltzmann-fluids` when a regular grid and straightforward parallelism matter more than free surfaces.",
 "cost": {
  "class": "heavy",
  "notes": "Neighbour search dominates; cost grows linearly with particles x neighbours (about 30-60 each)."
 },
 "failure_modes": [
  "Tensile instability: particles clump or pair when pressure goes negative",
  "Weakly-compressible variants force very small time steps, or fluid visibly compresses",
  "Particle deficiency at free surface and boundaries gives density and pressure errors",
  "Lots of particles (millions) needed for visually smooth surfaces; surface reconstruction adds cost and bumpiness"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1146/annurev.aa.30.090192.002551",
   "type": "paper",
   "title": "Smoothed Particle Hydrodynamics",
   "authors": [
    "J. J. Monaghan"
   ],
   "year": 1992,
   "venue": "Annual Review of Astronomy and Astrophysics",
   "url": "https://doi.org/10.1146/annurev.aa.30.090192.002551",
   "ids": {
    "doi": "10.1146/annurev.aa.30.090192.002551"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1146/annurev.aa.30.090192.002551",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1146/annurev.aa.30.090192.002551",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1146/annurev.aa.30.090192.002551",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1146/annurev.aa.30.090192.002551",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1146/annurev.aa.30.090192.002551",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "monaghan1992smoothed"
  },
  {
   "role": "implementation",
   "key": "gh:interactivecomputergraphics/splishsplash",
   "type": "code",
   "title": "InteractiveComputerGraphics/SPlisHSPlasH",
   "description": "SPlisHSPlasH is an open-source library for the physically-based simulation of fluids.",
   "authors": [
    "InteractiveComputerGraphics"
   ],
   "year": 2016,
   "venue": "GitHub",
   "url": "https://github.com/InteractiveComputerGraphics/SPlisHSPlasH",
   "ids": {
    "repo": "InteractiveComputerGraphics/SPlisHSPlasH"
   },
   "license": "MIT",
   "extra": {
    "stars": 1971,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-23T08:16:51Z",
    "language": "C++",
    "homepage": "https://splishsplash.physics-simulation.org/",
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/InteractiveComputerGraphics/SPlisHSPlasH",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/InteractiveComputerGraphics/SPlisHSPlasH",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/InteractiveComputerGraphics/SPlisHSPlasH",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/InteractiveComputerGraphics/SPlisHSPlasH",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/InteractiveComputerGraphics/SPlisHSPlasH",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/InteractiveComputerGraphics/SPlisHSPlasH",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "interactivecomputergraphics2016interactivecomputergraphics"
  }
 ],
 "presets": [
  {
   "id": "sph-fluids-live",
   "title": "Smoothed Particle Hydrodynamics — Poly6 density kernel, Tait pressure heatmap & kernel support rings",
   "runtime": "glsl-fragment",
   "params": {
    "P_KERNEL_H": 0.19,
    "P_SLOSH_SPEED": 1,
    "P_PRESSURE_VIS": 0.78,
    "P_SURFACE_ISO": 1
   },
   "basis": "original",
   "license_note": "Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).",
   "code": "const float P_KERNEL_H = 0.19; // P Kernel Radius h [0.11, 0.30, 0.01]\nconst float P_SLOSH_SPEED = 1.0; // P Slosh Speed [0.0, 2.5, 0.05]\nconst float P_PRESSURE_VIS = 0.78; // P Pressure Map [0.0, 1.5, 0.04]\nconst float P_SURFACE_ISO = 1.0; // P Fluid Threshold [0.5, 1.8, 0.04]\n\nvec2 hash21(float p) {\n  vec3 p3 = fract(vec3(p) * vec3(0.1031, 0.1030, 0.0973));\n  p3 += dot(p3, p3.yzx + 33.33);\n  return fract((p3.xx + p3.yz) * p3.zy);\n}\n\n// Poly6 SPH smoothing kernel W(r, h) ~ (1 - (r/h)^2)^3 for r < h\nfloat poly6(float r, float h) {\n  float q = r / max(h, 1e-4);\n  if (q >= 1.0) return 0.0;\n  float t = 1.0 - q * q;\n  return t * t * t;\n}\n\n// Sloshing SPH particle trajectory inside rectangular tank x in [-0.62, 0.62], y in [-0.36, 0.36]\nvec2 particlePos(int idx, float t) {\n  float fi = float(idx);\n  float colIdx = mod(fi, 7.0);\n  float rowIdx = floor(fi / 7.0);\n  vec2 h = hash21(fi * 11.7 + 4.2);\n\n  // Sloshing basin wave mode + convective circulation roll\n  float baseX = (colIdx - 3.0) * 0.172 + 0.05 * sin(t * 1.45 + rowIdx * 0.9 + h.x * 6.28);\n  float slosh = 0.12 * sin(t * 1.35) * (baseX / 0.55);\n  float splash = 0.055 * sin(t * 2.2 + colIdx * 1.15 + h.y * 6.28) * (rowIdx / 3.0);\n  float baseY = -0.28 + rowIdx * 0.115 + slosh + splash;\n\n  // Hydrostatic swirl eddy near basin center\n  float eddyAng = t * 0.95 + fi * 0.7;\n  vec2 swirl = vec2(cos(eddyAng), sin(eddyAng)) * (0.025 + 0.02 * h.x);\n\n  vec2 p = vec2(baseX, baseY) + swirl;\n  p.x = clamp(p.x, -0.58, 0.58);\n  p.y = clamp(p.y, -0.32, 0.32);\n  return p;\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 px = 1.0 / max(iResolution.y, 1.0);\n  float tAnim = iTime * max(P_SLOSH_SPEED, 0.0);\n  float hRad = clamp(P_KERNEL_H, 0.10, 0.34);\n  float presVis = clamp(P_PRESSURE_VIS, 0.0, 1.5);\n  float isoThr = clamp(P_SURFACE_ISO, 0.4, 2.0) * 0.58;\n\n  // Evaluate SPH density rho(uv) and kernel gradient grad(rho) across 28 fluid particles\n  float rho = 0.0;\n  vec2 gradRho = vec2(0.0);\n  float minDist = 1e5;\n  float ringVis = 0.0;\n  for (int i = 0; i < 28; i++) {\n    vec2 pi = particlePos(i, tAnim);\n    vec2 d = uv - pi;\n    float r = length(d);\n    minDist = min(minDist, r);\n    float w = poly6(r, hRad);\n    rho += w;\n    if (r < hRad && r > 1e-4) {\n      float q = r / hRad;\n      float dw = -6.0 * (1.0 - q * q) * (1.0 - q * q) * q / hRad;\n      gradRho += (d / r) * dw;\n    }\n    // Show kernel support radius h on 3 highlighted probe particles\n    if (i == 10 || i == 17 || i == 23) {\n      ringVis = max(ringVis, smoothstep(px * 3.0, px * 0.6, abs(r - hRad)));\n    }\n  }\n\n  // Dark lab chamber backdrop + subtle tank grid\n  vec3 col = mix(vec3(0.025, 0.036, 0.058), vec3(0.055, 0.080, 0.125), smoothstep(-0.5, 0.5, uv.y));\n  vec2 g = abs(fract(uv * 12.0 - 0.5) - 0.5);\n  col += vec3(0.015, 0.025, 0.042) * smoothstep(0.035, 0.005, min(g.x, g.y));\n\n  // Tank interior bounds\n  vec2 tankD = abs(uv) - vec2(0.64, 0.38);\n  float dTank = length(max(tankD, 0.0)) + min(max(tankD.x, tankD.y), 0.0);\n  if (dTank < 0.0) {\n    col = mix(col, vec3(0.04, 0.06, 0.10), 0.5);\n  }\n\n  // Tait equation of state pressure: p = k * ((rho / rho0)^gamma - 1)\n  float rhoNorm = rho / max(isoThr, 0.1);\n  float pressure = max(pow(max(rhoNorm, 0.0), 2.4) - 1.0, 0.0);\n\n  // Fluid surface extraction where density exceeds isosurface threshold\n  float fluidMask = smoothstep(isoThr * 0.86, isoThr * 1.06, rho) * step(dTank, 0.0);\n  if (fluidMask > 0.005) {\n    // Reconstruct pseudo-3D fluid surface normal from density gradient\n    vec3 n = normalize(vec3(-gradRho.x * 0.18, -gradRho.y * 0.18, 1.0));\n    vec3 lDir = normalize(vec3(0.45, 0.78, 0.68));\n    float diff = max(dot(n, lDir), 0.0);\n    float spec = pow(max(dot(n, normalize(lDir + vec3(0.0, 0.0, 1.0))), 0.0), 48.0);\n\n    // Rich deep-water blue -> high-pressure cyan/emerald/amber core\n    vec3 waterCol = mix(vec3(0.05, 0.34, 0.68), vec3(0.12, 0.78, 0.88), clamp(rhoNorm * 0.45, 0.0, 1.0));\n    vec3 pressCol = mix(waterCol, vec3(0.98, 0.66, 0.18), clamp(pressure * 0.28 * presVis, 0.0, 0.92));\n    // Crisp specular foam rim along free surface (low density boundary)\n    float foam = smoothstep(isoThr * 1.45, isoThr * 0.92, rho);\n    vec3 shaded = pressCol * (0.36 + 0.74 * diff) + vec3(0.92, 0.98, 1.0) * (spec * 0.85 + foam * 0.42);\n    col = mix(col, shaded, fluidMask);\n  }\n\n  // Render crisp SPH particle centers & smoothing kernel support radius rings\n  float pCore = smoothstep(0.017, 0.007, minDist);\n  col = mix(col, vec3(0.92, 0.98, 1.0), pCore * 0.72);\n  col += vec3(1.0, 0.86, 0.34) * ringVis * 0.68;\n\n  // Glass tank border\n  col = mix(col, vec3(0.68, 0.84, 0.98), smoothstep(0.009, 0.002, abs(dTank)) * 0.85);\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": "sph-fluids",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T19:04:38.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.5398,
      "std": 0.2534,
      "min": 0.1886,
      "max": 0.9596
     },
     "image": "/specimens/sph-fluids-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "position-based-fluids",
  "flip-pic-fluids",
  "mpm-snow",
  "lattice-boltzmann-fluids"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/simulation/sph-fluids.json"
 },
 "solves": [
  "particle based water simulation for games",
  "splashing liquid with free surface and no grid",
  "fluid interacts with moving objects naturally",
  "simulate honey or water as particles"
 ],
 "authority": "Monaghan, 1992",
 "url": "https://texelatlas.vercel.app/t/sph-fluids/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 20.38,
  "tier": "heavy",
  "p50_ms": 0.64,
  "p95_ms": 0.6561,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "sph-fluids-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 17.99,
   "tier": "moderate",
   "p50_ms": 0.2842,
   "p95_ms": 0.2929,
   "speedup_x": 2.25,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 12.17,
   "tier": "moderate",
   "p50_ms": 0.3504,
   "p95_ms": 0.3505,
   "speedup_x": 1.83,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}