r/Simulated • • 1d ago

Research Simulation Real-time 2D fluid sim in Python

Enable HLS to view with audio, or disable this notification

I wanted to understand fluid simulation, so I built a 2D one in Python. About 9,000 particles, you can grab the fluid with the mouse, lift it and drop it.

My first version ran at about 10 FPS. The big problems were:

  • Every particle was checked against every other one, roughly 80 million pair checks per step. I put the particles in a uniform grid with cells the size of the smoothing radius, so a particle only looks at the 3x3 cells around it.
  • The loops were plain Python. Moving them to Numba helped a lot.
  • I switched from computing pressure forces directly to Position Based Fluids. It predicts positions, then nudges crowded particles apart over a few iterations, and takes velocity from how far they moved. It stays stable at much larger time steps than the force-based version I started with.

This was inspired by Sebastian Lague Coding Adventure. I made this in python and optimized it further.

Here's the Repo and a small article for understanding

This was my very first optimization project and also in graphics, it's still a little rusty, so if you see something wrong in the kernels or have ideas for making it faster, I’d like to hear it.

2 Upvotes

1 comment sorted by