https://github.com/gabyx/rsfluid
An Eulerian fluid simulation written in Rust to learn the language
https://github.com/gabyx/rsfluid
fluid-simulation rust rust-learning
Last synced: 6 months ago
JSON representation
An Eulerian fluid simulation written in Rust to learn the language
- Host: GitHub
- URL: https://github.com/gabyx/rsfluid
- Owner: gabyx
- License: mpl-2.0
- Created: 2022-12-18T16:05:43.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-16T15:35:45.000Z (almost 2 years ago)
- Last Synced: 2025-04-03T09:41:37.174Z (11 months ago)
- Topics: fluid-simulation, rust, rust-learning
- Language: Rust
- Homepage:
- Size: 382 KB
- Stars: 11
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
RsFluid
**Disclosure: The material in this repository has not been reviewed, endorsed,
or approved of by the Rust Foundation. For more information see the Rust
Foundation Trademark Policy 2023.**
A small demo project to learn Rust by implementing an Eulerian fluid simulation.
This project has been inspired by
[TenMinutesPhysics](https://matthias-research.github.io/pages/tenMinutePhysics/index.html).
The basics are easy to understand, but as always "the dog is buried in the
details" as we phrase it in German. Its probably a fact in the field of
scientific simulation that most boilerplate code is there to deal with the
beautiful (😨) boundaries of your simulation domain, data structures etc.
The simulation is implemented with the same procedure as as in
[fluid.sim](https://github.com/matthias-research/pages/blob/master/tenMinutePhysics/17-fluidSim.html)
with some additional data structures and better encapsulation to support the
computation.
# Introduction
The CLI simulator is in [src/main.rs](src/main.rs) which sets up the scene with
a [grid](src/solver/grid.rs) and hands it over to the
[timestepper](src/solver/timestepper.rs) which is responsible to integrate the
grid. Disclaimer: This design is not yet perfect but it was more to play around
with the different concepts in Rust.
You can start the simulation with
```shell
cargo run --release --bin rsfluid -- -e 10.0 -t "$timestep" --incompress-iters 100 --dim "400,200"
```
or
```shell
just run --release --bin rsfluid -- -e 10.0 -t "$timestep" --incompress-iters 100 --dim "400,200"
```
To install `cargo` use
[this help here](https://doc.rust-lang.org/cargo/getting-started/installation.html).
To create the video with `30` frames use:
```shell
./tools/create-video.sh 30
```
or
```shell
just video 30
```
# Parallel Implementation
To implement the parallel version of
[`solve_incompressibility`](src/scene/grid.rs#L330) I needed to split
`grid.cells` successively into parts with an iterator chain until ending up with
an iterator which produces stencils in the form
[`PosStencilMut`](src/scene/grid_stencil.rs#L44). This iterator can be
converted to a parallel iterator by replacing the iterator chain with the
parallel functions from `rayon`.
The following picture illustrates the topology:

The parallel version with `./create-video.sh 30 --parallel` is currently much
slower than the serial one. The parallelization is probably to fine grained to
be efficient and the `Cell` driven layout is probably not that good for cache
friendliness.
# Videos
## Video Velocity
[](https://youtu.be/qZvKNIiBiw4)
## Video Liquid
[](https://youtu.be/BxRfxUcNPv0)
## Video Pressure
[](https://youtu.be/44bBZcQKzLQ)