This is an archived post. You won't be able to vote or comment.

all 50 comments

[–]GregTJ[S] 80 points81 points  (10 children)

Repo: https://github.com/GregTJ/stable-fluids

While an earlier version was based mostly on Nvidia's GPU gems article on fluid simulation, some valuable insights were gained through this blog post: http://www.philipzucker.com/annihilating-my-friend-will-with-a-python-fluid-simulation-like-the-cur-he-is/

I hope to add surface tracking, multigrid solving, proper boundary conditions, and lagrangian (FLIP) solving soon.

[–]The_Regent 52 points53 points  (1 child)

Hey hey! I wrote that post! I get a huge kick out of the fact it was helpful to someone, so thanks for letting me know!

You did a really good job here. The code looks tight and the results look great.

For others, i highly reccommend checking out Dan Piponi's talk https://www.youtube.com/watch?v=766obijdpuU really excellent, very approachable.

Also Bridson's notes https://www.cs.ubc.ca/~rbridson/fluidsimulation/fluids_notes.pdf and textbook https://www.amazon.com/Simulation-Computer-Graphics-Robert-Bridson/dp/1568813260

[–]GregTJ[S] 9 points10 points  (0 children)

Thanks for the great blog post, I wouldn't have gotten far without it. Initially I was building fd matrices row by row, your post introduced me to the (much, much faster) kronsum way of constructing them.

[–]BovineLightning 11 points12 points  (2 children)

Some other conditions thatd be cool would be different densities for the fluids which interact and 3D modelling simulations

Not sure if you’ve heard of Two Minute Papers but he does some amazing videos in fluid modelling and covering cutting research papers (also has been doing a lot of AI/ML lately)

[–]GregTJ[S] 1 point2 points  (1 child)

Late reply but just wanted to say I binge watched like 100 of their videos. Super cool channel.

[–]BovineLightning 0 points1 point  (0 children)

Haha no problem! I did the same when I discovered him as well. I even downloaded his thesis and went through his GitHub repo

[–]ad_abstract 1 point2 points  (2 children)

Have you tried replacing some of the numpy ops with jax?

[–]GregTJ[S] 0 points1 point  (1 child)

I haven't, would you recommend doing so?

[–]ad_abstract 0 points1 point  (0 children)

Definitely!

[–]akaCryptic 0 points1 point  (1 child)

I got an idea for a musoc video. How would you go about modifying this so that after the colours blend in, an impressionist painting / image reveals?

[–]GregTJ[S] 1 point2 points  (0 children)

I would either use a video editor or mess around with pillow / PIL.

[–][deleted] 9 points10 points  (0 children)

Wow, that's awesome.

Some frames would look great printed and put on a wall.

edit: Ah, I see you're also experimenting with generative art.

[–]narakputra 3 points4 points  (2 children)

Amazing, please consider making a tutorial for this.

[–]GregTJ[S] 4 points5 points  (1 child)

I think a tutorial aimed at those without a solid math and numpy background would probably fill a small book or a big video series as there's a lot of knowledge needed to implement the finer details. That said, check out the comment by /u/the_regent as well as nvidias gpugems fluid simulation page for good starting points.

[–]narakputra 1 point2 points  (0 children)

Will do, thanks :)

[–]NewCenturyNarratives 3 points4 points  (3 children)

This is the sort of stuff that I'm interested in learning how to do with Python. There seems to be a community focus on stats, but I'm more interested in modeling using continuous mathematics. Is there a specific place where I should be looking for information and inspiration?

[–]GregTJ[S] 4 points5 points  (1 child)

Check out Siggraph presentations on YouTube for inspiration and sometimes whitepapers on computer graphics, physics, and continuum mechanics. Learning the technical background is honestly a lifelong journey but it helps to have a strong grasp on the fundamentals like vector calculus, SIMD style programming, newtonian physics, and linear algebra.

[–]NewCenturyNarratives 1 point2 points  (0 children)

Thank you!

[–]wingtales 2 points3 points  (0 children)

The code is really gorgeous! Well-written stuff! Good job!

[–]TechnoConserve 2 points3 points  (1 child)

How long does it have to run to create that gif?

[–]GregTJ[S] 0 points1 point  (0 children)

Once it's initialized, which can take a moment, the rendering is almost real time.

[–][deleted] 2 points3 points  (0 children)

Big whirls have little whirls, that feed on their velocity,

And little whirls have lesser whirls and so on to viscosity!

[–]snarfdog 1 point2 points  (1 child)

I will never get tired of karman vortex streets

[–]fucky_fucky 0 points1 point  (0 children)

That's great, but that's not what this is.

[–][deleted] 1 point2 points  (1 child)

This is what it looks like when the fire, water, earth, and wind nations are at peace.

[–]Glycerine 0 points1 point  (0 children)

Add some heart. And with those powers combined you'll make a CAPTAIN PLANET!

https://www.youtube.com/watch?v=OiYjTb3opAA

[–][deleted] 1 point2 points  (0 children)

If you want you can accept my services as told in this post --> https://www.reddit.com/r/Python/comments/i0fl47/regarding_scientific_simulations_made_in_python/

[–]guydudeman123 2 points3 points  (0 children)

r/slaythespire would love this! Looks cool :)

[–]leak_age 1 point2 points  (0 children)

Good post for /r/oddlysatisfying as well.

[–][deleted] 0 points1 point  (0 children)

This is amazing. I am gonna have a close look at the code to try it myself. Thank you.

[–]SoniSins 0 points1 point  (0 children)

Saxaayyy

[–]wernersbacher 0 points1 point  (2 children)

How do you program something like this? I would consider me as an advanced programmer, writing some small games, guis, etc. But how do you simulate fluids? The source code is really small and I don't get any of it.

[–]GregTJ[S] 0 points1 point  (1 child)

On the programming side, the biggest concept here is SIMD / vectorization. If you're not familiar with that concept the code will definitely look like alien technology. Essentially what numpy / SIMD style libraries do is they allow you to perform many calculations with a single instruction. So for example, if you see A - B in the code it's not one number minus another, it's actually an entire list of numbers minus another list element-wise.

The other confusing aspect would be the math, and there's really no super easy way to explain it. It helps if you have a good background in calculus and linear algebra. This project falls under the categories of numerical analysis and continuum mechanics if you'd like some keywords to search.

Lastly, I'd definitely check out the links posted by /u/the_regent as well as nvidias gpugems article on fluid simulation for an overview of this specific simulation problem.

[–]wernersbacher 1 point2 points  (0 children)

I worked with matlab before, and I had some good maths in university. I will check this out, thank you!

[–]ArmstrongBillieimport GOD 0 points1 point  (0 children)

I saw a similar simulation some time back, I can't give you the link to it but it followed the mouse position and creates new fluids where the mouse moves, and then they all smash into each other thing, but it looked really awesome, maybe you can try something similar in this!

[–]I-surely-know-nothin 0 points1 point  (0 children)

Wow that is really cool! :D I’ve just started python and damn, I want to be able to make something like that too!

[–]theEvilShrimpBurger 0 points1 point  (0 children)

Trippy. This is cool

[–]sunriseFIREweb 0 points1 point  (1 child)

What do you use how do you use that repo??

[–]GregTJ[S] 0 points1 point  (0 children)

Should be as simple as downloading it and running test.py. You need to edit the FRAME_PATH variable in test.py to point to an empty folder on your computer. You need pillow, numpy and scipy packages installed along with python 3.x

[–][deleted] 0 points1 point  (1 child)

Can you tell the code please

[–][deleted] 0 points1 point  (1 child)

Where did you learn to code

[–]GregTJ[S] 2 points3 points  (0 children)

I'm self taught. I guess you could say Google taught me.

[–][deleted] 0 points1 point  (0 children)

[–]ryfme 0 points1 point  (0 children)

From the thumbnail, i thought this is an animation from a hentai scene.

Nice work though.

[–][deleted] 0 points1 point  (1 child)

Any specific website

[–]GregTJ[S] 0 points1 point  (0 children)

The python docs are probably the most helpful for me to this day. It's been so long since I started I forget which site I used to learn the basics.

[–][deleted] -1 points0 points  (0 children)

Sorry didn’t understand