---
id: "sph-fluids"
title: "Smoothed Particle Hydrodynamics (fluids)"
aliases: [SPH]
domain: "simulation"
stage: "simulation"
realtime: "realtime"
platforms: [webgl2, webgpu, desktop-gpu, cuda, cpu]
year_introduced: 1992
inputs: [particles]
outputs: [particles]
style: "both"
dynamic: "dynamic"
cost: "heavy"
authority: "Monaghan, 1992"
url: "https://texelatlas.vercel.app/t/sph-fluids/"
sources_verified_at: "2026-11-29"
---
# Smoothed Particle Hydrodynamics (fluids) (Monaghan, 1992)

> Meshless Lagrangian fluid method: each particle's density, pressure and viscosity forces come from kernel-weighted sums over neighbouring particles.

## Reach for it when

- 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

## 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 it

Choose it when you want a simple, meshless liquid with free surfaces, easy two-way rigid coupling and multiphase support. Prefer [Position Based Fluids](https://texelatlas.vercel.app/t/position-based-fluids.md) for faster, more game-oriented stability, [FLIP/PIC fluids](https://texelatlas.vercel.app/t/flip-pic-fluids.md) for large-scale splashes with less particle-pairing noise, and [Lattice Boltzmann fluids](https://texelatlas.vercel.app/t/lattice-boltzmann-fluids.md) when a regular grid and straightforward parallelism matter more than free surfaces.

## Cost

heavy — Neighbour search dominates; cost grows linearly with particles x neighbours (about 30-60 each).
Measured GPU Weight: 20.38× baseline (heavy) — 0.64 ms median / 0.6561 ms p95 on Tesla T4/PCIe/SSE2 @ 1920x1080 (opengl-timer-query).
Ada L4 scaling: 17.99× baseline (moderate) — 0.2842 ms median (2.25× faster on L4).
Ampere A100 scaling: 12.17× baseline (moderate) — 0.3504 ms median (1.83× faster on A100).

## 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

## Engine support

- blender: builtin — `Particle Properties > Physics > Fluid` (evidence: https://docs.blender.org/manual/en/5.2/physics/particles/emitter/physics/fluid.html, checked 2026-12-01)

## Dataflow

- Inputs: particles
- Outputs: particles
- Platforms: webgl2, webgpu, desktop-gpu, cuda, cpu

## Presets

### Smoothed Particle Hydrodynamics — Poly6 density kernel, Tait pressure heatmap & kernel support rings (glsl-fragment; rendered 2026-11-30)

```json
{
  "P_KERNEL_H": 0.19,
  "P_SLOSH_SPEED": 1,
  "P_PRESSURE_VIS": 0.78,
  "P_SURFACE_ISO": 1
}
```

```glsl
const float P_KERNEL_H = 0.19; // P Kernel Radius h [0.11, 0.30, 0.01]
const float P_SLOSH_SPEED = 1.0; // P Slosh Speed [0.0, 2.5, 0.05]
const float P_PRESSURE_VIS = 0.78; // P Pressure Map [0.0, 1.5, 0.04]
const float P_SURFACE_ISO = 1.0; // P Fluid Threshold [0.5, 1.8, 0.04]

vec2 hash21(float p) {
  vec3 p3 = fract(vec3(p) * vec3(0.1031, 0.1030, 0.0973));
  p3 += dot(p3, p3.yzx + 33.33);
  return fract((p3.xx + p3.yz) * p3.zy);
}

// Poly6 SPH smoothing kernel W(r, h) ~ (1 - (r/h)^2)^3 for r < h
float poly6(float r, float h) {
  float q = r / max(h, 1e-4);
  if (q >= 1.0) return 0.0;
  float t = 1.0 - q * q;
  return t * t * t;
}

// Sloshing SPH particle trajectory inside rectangular tank x in [-0.62, 0.62], y in [-0.36, 0.36]
vec2 particlePos(int idx, float t) {
  float fi = float(idx);
  float colIdx = mod(fi, 7.0);
  float rowIdx = floor(fi / 7.0);
  vec2 h = hash21(fi * 11.7 + 4.2);

  // Sloshing basin wave mode + convective circulation roll
  float baseX = (colIdx - 3.0) * 0.172 + 0.05 * sin(t * 1.45 + rowIdx * 0.9 + h.x * 6.28);
  float slosh = 0.12 * sin(t * 1.35) * (baseX / 0.55);
  float splash = 0.055 * sin(t * 2.2 + colIdx * 1.15 + h.y * 6.28) * (rowIdx / 3.0);
  float baseY = -0.28 + rowIdx * 0.115 + slosh + splash;

  // Hydrostatic swirl eddy near basin center
  float eddyAng = t * 0.95 + fi * 0.7;
  vec2 swirl = vec2(cos(eddyAng), sin(eddyAng)) * (0.025 + 0.02 * h.x);

  vec2 p = vec2(baseX, baseY) + swirl;
  p.x = clamp(p.x, -0.58, 0.58);
  p.y = clamp(p.y, -0.32, 0.32);
  return p;
}

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 px = 1.0 / max(iResolution.y, 1.0);
  float tAnim = iTime * max(P_SLOSH_SPEED, 0.0);
  float hRad = clamp(P_KERNEL_H, 0.10, 0.34);
  float presVis = clamp(P_PRESSURE_VIS, 0.0, 1.5);
  float isoThr = clamp(P_SURFACE_ISO, 0.4, 2.0) * 0.58;

  // Evaluate SPH density rho(uv) and kernel gradient grad(rho) across 28 fluid particles
  float rho = 0.0;
  vec2 gradRho = vec2(0.0);
  float minDist = 1e5;
  float ringVis = 0.0;
  for (int i = 0; i < 28; i++) {
    vec2 pi = particlePos(i, tAnim);
    vec2 d = uv - pi;
    float r = length(d);
    minDist = min(minDist, r);
    float w = poly6(r, hRad);
    rho += w;
    if (r < hRad && r > 1e-4) {
      float q = r / hRad;
      float dw = -6.0 * (1.0 - q * q) * (1.0 - q * q) * q / hRad;
      gradRho += (d / r) * dw;
    }
    // Show kernel support radius h on 3 highlighted probe particles
    if (i == 10 || i == 17 || i == 23) {
      ringVis = max(ringVis, smoothstep(px * 3.0, px * 0.6, abs(r - hRad)));
    }
  }

  // Dark lab chamber backdrop + subtle tank grid
  vec3 col = mix(vec3(0.025, 0.036, 0.058), vec3(0.055, 0.080, 0.125), smoothstep(-0.5, 0.5, uv.y));
  vec2 g = abs(fract(uv * 12.0 - 0.5) - 0.5);
  col += vec3(0.015, 0.025, 0.042) * smoothstep(0.035, 0.005, min(g.x, g.y));

  // Tank interior bounds
  vec2 tankD = abs(uv) - vec2(0.64, 0.38);
  float dTank = length(max(tankD, 0.0)) + min(max(tankD.x, tankD.y), 0.0);
  if (dTank < 0.0) {
    col = mix(col, vec3(0.04, 0.06, 0.10), 0.5);
  }

  // Tait equation of state pressure: p = k * ((rho / rho0)^gamma - 1)
  float rhoNorm = rho / max(isoThr, 0.1);
  float pressure = max(pow(max(rhoNorm, 0.0), 2.4) - 1.0, 0.0);

  // Fluid surface extraction where density exceeds isosurface threshold
  float fluidMask = smoothstep(isoThr * 0.86, isoThr * 1.06, rho) * step(dTank, 0.0);
  if (fluidMask > 0.005) {
    // Reconstruct pseudo-3D fluid surface normal from density gradient
    vec3 n = normalize(vec3(-gradRho.x * 0.18, -gradRho.y * 0.18, 1.0));
    vec3 lDir = normalize(vec3(0.45, 0.78, 0.68));
    float diff = max(dot(n, lDir), 0.0);
    float spec = pow(max(dot(n, normalize(lDir + vec3(0.0, 0.0, 1.0))), 0.0), 48.0);

    // Rich deep-water blue -> high-pressure cyan/emerald/amber core
    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));
    vec3 pressCol = mix(waterCol, vec3(0.98, 0.66, 0.18), clamp(pressure * 0.28 * presVis, 0.0, 0.92));
    // Crisp specular foam rim along free surface (low density boundary)
    float foam = smoothstep(isoThr * 1.45, isoThr * 0.92, rho);
    vec3 shaded = pressCol * (0.36 + 0.74 * diff) + vec3(0.92, 0.98, 1.0) * (spec * 0.85 + foam * 0.42);
    col = mix(col, shaded, fluidMask);
  }

  // Render crisp SPH particle centers & smoothing kernel support radius rings
  float pCore = smoothstep(0.017, 0.007, minDist);
  col = mix(col, vec3(0.92, 0.98, 1.0), pCore * 0.72);
  col += vec3(1.0, 0.86, 0.34) * ringVis * 0.68;

  // Glass tank border
  col = mix(col, vec3(0.68, 0.84, 0.98), smoothstep(0.009, 0.002, abs(dTank)) * 0.85);

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

Attribution: Original interactive WebGL2 fragment shader written for Texel Atlas (MIT).

## Lineage

- extended by [Ihmsen Unified Spray, Foam, and Bubble Whitewater Simulation](https://texelatlas.vercel.app/t/ihmsen-unified-whitewater.md)

## Sources

1. [primary] J. J. Monaghan (1992) Smoothed Particle Hydrodynamics. Annual Review of Astronomy and Astrophysics. https://doi.org/10.1146/annurev.aa.30.090192.002551 (verified via crossref on 2026-11-29)
2. [implementation] InteractiveComputerGraphics (2016) InteractiveComputerGraphics/SPlisHSPlasH. https://github.com/InteractiveComputerGraphics/SPlisHSPlasH License: MIT. (verified via github on 2026-11-29)

## Cite this entry

Texel. "Smoothed Particle Hydrodynamics (fluids)." https://texelatlas.vercel.app/t/sph-fluids/ (accessed 2026-12-01).
