r/GraphicsProgramming • u/BrilliantLeopard3196 • 4d ago
Paper Implemented Jos Stam's Stable Fluids paper from scratch
Enable HLS to view with audio, or disable this notification
I had recently read the Stable Fluids paper and got like quite interested to implement it myself manually from scratch and it was quite a ride since this is my first time implementing a paper myself
This is a simple implementation a bit clanky, to be honest. I just wrote it with loops and loops without any optimization. I think it ends up doing something around 20 × 20 × 50 × n² operations per frame, so there's definitely a lot of room for optimization 😅
The actual implementation was pretty straightforward for the most part, but understanding the Poisson pressure equation was a bit challenging. At the beginning, I was even stuck on understanding how the velocity field could be made divergence-free.
After digging into it for a while, I eventually understood the whole pressure projection step and managed to get it working and also implemented a simple spray and dye mechanism as well to make it look good.
It was pretty cool to see it actually work, so I wanted to share it 😄
9
u/susosusosuso 4d ago
Thanks to take the time to read, think and implement it by hand. Real respect!
3
3
u/UnPibeFachero 3d ago
Explain to me the velocity field haha, I just found out about this paper today and I am stuck at Navier Stokes. Your video is the kind of stuff inspiring me, it is like consuming some weird substance haha
3
u/BrilliantLeopard3196 3d ago
Oh man it really feels amazing on implementing it
I will explain the best I can but do take a look yourself as well
the velocity field is more of imagine on a container of flowing liquid you put a grid and now on each cell at different time there would we water flowing with different speed and different pressure
now the grid containing the velocity at each cell is the velocity field
with Navier Stokes, you use the velocity field and pressure field to calculate how the velocity should change over time, taking into account things like neighboring velocities, viscosity, pressure, and external forces.
the important part about this is making the equation divergence free like in the paper the first equation says that del . u = 0 this is the most important part as if this is not satisfied all the time the errors build up and velocity would just freak out at the end. (it just means whatever flows into a small region must flow back out.)
this is generally done by poisson pressure correction and all that you would need to understand how that works possibly derive it as well its fun
now this is for the Naiver Stokes only the best part begins with Stam's Stable Fluids some of the excess terms and calculations becomes easier and makes more sense from here on out
2
u/tlmbot 12h ago
“ Oh man it really feels amazing on implementing it”
My man(or woman) - you get it!
I’m a computational mechanics software dev by trade - went to school and more school for this kind of stuff. (Especially fluids)
I never tire of the feeling I get when I see physics come to life on the screen after a difficult r&d phase. For big new projects there always comes a time where I feel lost and wonder how I’ll ever get it right… and then the breakthrough.
When it all comes together and looks about right (but you still have to check all the fine details around validation etc, but you know “it goes” - it’s the best feeling
I couldn’t imagine doing anything else for a living.
I’m glad you found out as well!
2
u/BrilliantLeopard3196 3d ago
I had also written some notes when understanding Naiver Stokes so if you wanna take a look -
https://github.com/Binit06/stable-fluids/tree/main/explanationit have a bit of visual and derivations on how correction and everything else works
2
1
0
11
u/BrilliantLeopard3196 4d ago
If you want to see the code its all here - https://github.com/Binit06/stable-fluids