Skip to content

oscarmuya/fluid-simulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fluid Simulation in Rust

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.

Core Concepts & Model Choice

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.

Governing Equations

The fluid dynamics are based on the Navier-Stokes equations for an incompressible fluid ($\rho$ is constant).

Momentum Equation

$$\frac{\partial \mathbf{u}}{\partial t} + (\mathbf{u} \cdot \nabla) \mathbf{u} = -\frac{1}{\rho} \nabla p + \nu \nabla^2 \mathbf{u} + \mathbf{f}$$

Incompressibility Constraint

$$\nabla \cdot \mathbf{u} = 0$$

Projection Method

To enforce the constraint, I use a Projection Method implemented on the grid:

  1. Particle-to-Grid Mapping: Particle data (like divergence sources) is interpolated onto the grid structure.
  2. 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.
  3. Velocity Projection: Particle velocities are corrected using the solved pressure gradient: $$\mathbf{u}^{n+1} = \mathbf{u}^* - \frac{\Delta t}{\rho} \nabla p$$

Implementation Details

  • Data Flow: The main loop in src/main.rs orchestrates 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.

How to Run

Prerequisites

  • Rust toolchain installed.

Build

For the fastest execution, compile in release mode:

cargo build --release

Execution

The 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-simulation

Further details on simulation parameters, domain size, and output formats should be checked within src/main.rs and src/physics.rs.

About

Fluid simulation in Rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages