This repository contains my high-performance, grid-coupled particle simulation written entirely in Rust. The design leverages Rust's strengths for performance-critical numerical computation while maintaining safety.
This implementation utilizes a hybrid approach combining grid-based advection/pressure projection with particle tracking for transport and material properties.
- Particle Tracking (
src/particle.rs): Individual fluid elements are represented as particles. Their properties (position, velocity) are tracked directly through time integration. - Grid Structure (
src/physics.rs): A background grid is used for solving implicit equations, specifically for the pressure Poisson problem, ensuring the divergence-free constraint is met efficiently. - Memory Management (
src/pool.rs): We use a custom object/particle pool to manage memory allocation and deallocation efficiently, avoiding heap fragmentation common in high-iteration simulations.
The fluid dynamics are based on the Navier-Stokes equations for an incompressible fluid (
To enforce the constraint, I use a Projection Method implemented on the grid:
- Particle-to-Grid Mapping: Particle data (like divergence sources) is interpolated onto the grid structure.
-
Pressure Poisson Equation (PPE): The pressure
$p$ is solved on the grid to eliminate divergence:$$\nabla^2 p = \frac{1}{\Delta t} (\nabla \cdot \mathbf{u}^*)$$ This is solved iteratively, likely via a Conjugate Gradient solver. -
Velocity Projection: Particle velocities are corrected using the solved pressure gradient:
$$\mathbf{u}^{n+1} = \mathbf{u}^* - \frac{\Delta t}{\rho} \nabla p$$
- Data Flow: The main loop in
src/main.rsorchestrates the steps: 1) Advect particles, 2) Map particle data to the grid, 3) Solve PPE on the grid, 4) Update particle velocities via grid projection. - Performance: The use of Rust ensures high performance, especially within the particle update loops and the iterative solver.
- Rust toolchain installed.
For the fastest execution, compile in release mode:
cargo build --releaseThe simulation is executed from the compiled binary. Configuration details are handled within the main function:
# Executes the simulation with default parameters set in src/main.rs
./target/release/fluid-simulationFurther details on simulation parameters, domain size, and output formats should be checked within src/main.rs and src/physics.rs.