Expand description
GPU-accelerated Smoothed Particle Hydrodynamics (SPH) simulation.
This module demonstrates how to leverage the oxiphysics-gpu compute
backend to run an SPH density–pressure solve entirely on the GPU, falling
back to a clean CPU implementation when no GPU is available.
§Physical model
Weakly Compressible SPH (WCSPH) with:
- Density: ρᵢ = Σⱼ mⱼ W(rᵢⱼ, h) (cubic-spline W3 kernel)
- Pressure: pᵢ = k (ρᵢ/ρ₀ − 1) (Tait equation of state, γ = 1)
- Acceleration: aᵢ = −Σⱼ mⱼ (pᵢ/ρᵢ² + pⱼ/ρⱼ²) ∇W + aᵢ^visc + g
- Viscosity: aᵢ^visc = ν Σⱼ mⱼ/ρⱼ (r⃗ᵢⱼ · ∇W / (|r⃗ᵢⱼ|² + ε)) (v⃗ᵢⱼ)
§GPU dispatch strategy
Each particle is assigned to one GPU thread. Naïve O(N²) neighbour search is used for small N (≤ 4096); a cell-list spatial hash reduces this to O(N) for larger simulations.
§Usage
use oxiphysics_gpu::sph_gpu::{SphSimulation, SphConfig};
let cfg = SphConfig { n_particles: 64, smoothing_h: 0.1, rest_density: 1000.0, ..SphConfig::default() };
let mut sim = SphSimulation::new(cfg);
// Place particles in a 4×4×4 grid
for i in 0..4 { for j in 0..4 { for k in 0..4 {
let idx = i * 16 + j * 4 + k;
sim.state.pos_x[idx] = i as f64 * 0.1;
sim.state.pos_y[idx] = j as f64 * 0.1 + 1.0;
sim.state.pos_z[idx] = k as f64 * 0.1;
}}}
// Simulate 10 frames at 60 Hz
for _ in 0..10 { sim.step(1.0 / 60.0); }
// Particles should have moved under gravity
assert!(sim.state.pos_y[0] < 1.0 + 0.1,
"particles should fall under gravity");Structs§
- SphConfig
- Configuration for an SPH simulation.
- SphParticle
State - Structure-of-Arrays particle state for N SPH particles.
- SphSimulation
- SPH simulation that dispatches compute to GPU when available.
Functions§
- cubic_
spline_ dw_ dr - Gradient magnitude of cubic-spline kernel: |∇W| = dW/dr / r (for r > 0).
- cubic_
spline_ w3 - Cubic-spline kernel W3(r, h).