A 2d fluid simulator implemented with Rust + wasm-bindgen-rayon.
Check the demo here! (needs a browser that supports SharedArrayBuffer) : https://fluid-simulation-test.netlify.app/
The following is the brief description of the simulation.
- Solver is implemented in Rust and compiled to WASM.
- The simulation is parallelized by multi-threading using wasm-bindgen-rayon.
- The simulation is based on SPH method described in Particle-Based Fluid Simulation for Interactive Applications by Müller et al.
- Techniques called near-density and near-pressure are also implemented which is described in Particle-based Viscoelastic Fluid Simulation by Clavet et al. This technique is useful to realize surface tension.
- This project is inspired by Sebastian Lague's video : Coding Adventure: Simulating Fluids.
Basically, you can run the simulation locally by running the following commands.
npm install
npm run build
npm run serve
But in some environments, webpack seems to cause some errors (see this issue). In that case, you can build the repo without webpack with the following steps.
- Change
"build"inpackage.jsonlike below.- before:
"build": "cross-env RUSTUP_TOOLCHAIN=nightly wasm-pack build --target web && webpack build ./index.js --mode production -o dist --output-filename index.js && shx cp index.html dist/", - after:
"build": "cross-env RUSTUP_TOOLCHAIN=nightly wasm-pack build --target web",
- before:
- Change the code in
Cargo.tomllike below.- before:
wasm-bindgen-rayon = { version = "1.2" } - after:
wasm-bindgen-rayon = { version = "1.2", features = ["no-bundler"] }
- before:
- Change the code in
server.jslike below.- before:
app.use(express.static(__dirname + '/dist/')); - after:
app.use(express.static(__dirname));
- before:
- Remove the line
import { simd } from 'wasm-feature-detect';inindex.js
- Coding Adventure: Simulating Fluids
- This is a video that gave me an motivation to implement this project.
- Particle-Based Fluid Simulation for Interactive Applications, Müller et al. 2003
- Basically, the fluid simulation in this project is based on this paper.
- Particle-based Viscoelastic Fluid Simulation, Clavet et al. 2005
- Techniques called near density and near pressure are also implemented that is presented in this paper. These techniques are useful to realize a force like a surface tension.
- SPH implementations and articles by Lucas V. Schuermann helped me a lot.
