Coding Adventure: Simulating Fluids

1,702,839
0
Published 2023-10-08
Let's try to convince a bunch of particles to behave (at least somewhat) like water.
Written in C# and HLSL, and running inside the Unity engine.

Source code:
github.com/SebLague/Fluid-Sim

If you'd like to support me in creating more videos like this, you can do so here:
www.patreon.com/SebastianLague
ko-fi.com/sebastianlague

Resources:
matthias-research.github.io/pages/publications/sca…
www.ligum.umontreal.ca/Clavet-2005-PVFS/pvfs.pdf
sph-tutorial.physics-simulation.org/pdf/SPH_Tutori…
web.archive.org/web/20140725014123/https://docs.nv…

Music:
github.com/SebLague/Misc-Project-Info/blob/main/Co…

Chapters:
0:00 Intro
0:27 Gravity and Collisions
2:31 Smoothed Particles
3:55 Calculating Density
7:53 The Interpolation Equation
11:58 Gradient Calculations
15:38 The Pressure Force
19:19 Trying to Make it Work...
22:05 Optimizing Particle Lookups
26:31 Spatial Grid Code
27:48 Position Predictions
30:32 Mouse Force
33:22 Artificial Viscosity
36:46 Pressure Problems
39:51 Bugs
41:06 Parallel Sorting
43:22 Some Tests and Experiments
44:47 The Third Dimension
47:23 Outro

All Comments (21)
  • @SebastianLague
    Hi everyone, I hope you enjoy the video! This is a topic I’ve been wanting to tackle for ages, but have always found a bit intimidating to get started with. So I’m happy to have finally dived in! Let me know if you have any suggestions for improving it, or ideas for future projects. In other news, I’ve recently created a Ko-Fi page where it’s possible to support my work with a one-off contribution (as opposed to the monthly pledges on Patreon). No pressure of course, but if that’s something you’d like and are able to do, you can find the page here ko-fi.com/sebastianlague
  • @clavetsi
    The near-density trick was something I came up with for my master's thesis 18 years ago, and nobody mentioned that they were using it before today. It took a long time, but it was worth it in the end because I got my name in one of best videos on youtube. Thank you Sebastian.
  • @Danidev
    Super cool video! We need part 2 👁👄👁
  • @jucom756
    I love how one of the bugs you try to fix is just emergent surface tension.
  • @michellinzer4881
    He is the Bob Ross of Coding. He is like: „Lets put a happy little rekursive non-linear differencial equation of sixt order here.“ I love it!
  • @fxzn
    The worst part about Sebastian Lague videos is that they end.
  • @batimius
    Sebastian Lague never disappoints. I am astonished by how much a single man can accomplish, and at the same time, how much he can teach others. The 48 minutes that this video lasted felt more like 10 because of how enjoyable and informative it was. I can't wait to see how the finished product will look like. Amazing work!
  • @lordesfairgenug
    Good throwback to my old times as a molecular dynamics scientist :) Heating up of your simulation: This is normal and happens for different reasons, the biggest contribution is certainly that your forces are cut off and discretized. There is a difference for incoming and outgoing particles (whenever particles bounce, they heat up a little bit, because not everything is perfectly symmetrical over time). The only way to solve this with 100% correct physics is to go via action integral solutions, which is absolutely not feasible for a simulation of that size, that is why even physicists don't use that for molecular dynamics (MD). The easy and cheap way here to go is to introduce a thermostat, which for example every 100 or 10k frames readjusts the temperature, by shifting the speed of each particle a bit more in the direction of the desired speed. In your case you don't need to care about real temperature, just pick an average speed that you are aiming for, which means you still set the temperature but don't know the value in terms of K or °C. Incompressibility: Be happy you don't have it. You don't want it. It is not a good thing for discrete particle simulations. This is something for totally different methods like finite volume/element simulations, density functional and such things. If you have discrete particles exchanging forces, don't try incompressibility. It is unphysical and it only works in more abstract and less close to real physics. Even in these fields it ends up with singularities and problems everywhere, ask people who try to model turbulence ;-) Behavior at the boundaries: This is no surprise. You have this behavior because the particles at the edges do not have any force pulling on them from the other side. They only have forces pulling on them from the inside and along the boundary but not from outside. This increases their local density there. In MD simulations there are several solutions for this. Often simulations have periodic boundary conditions, then this problem is mostly gone. In your case this is not a solution. You can have a background field that is outside of the box, which is calculated from what is happening inside the box, fast fourier transform is used to calculate that cheaply, this basically means that you copy your box abstractly into each direction, so the particles feel long distance forces from all sides. This is really important for MD simulation to get proper bulk behavior of materials, but in your case this is absolutely not necessary as you do not care if your fluid has a realistic volume viscosity, density, temperature / pressure relation etc. pp. You don't care about physical correctness, so you don't need to go that costly route (especially coding it from scratch). Another solution is to not have "neutral" walls. Your walls can have forces too, which results in the walls being sticky or repulsive, changing the behavior. The easiest solution would be to put ghost particles on the walls, which just emit the same forces as your other particles, but do not receive anything and do not move ever. This is often used in simulation of pores and confined materials. The ghost particles will certainly imprint a structure on the wall (and pull real particles into that structure), which you might not desire. In that case a homogenous wall force might be more what you are looking for, that is basically the same thing, but smooth, so that particles do not stick to certain points on the wall, but just still glide, without higher local density.
  • @user-rb7vs5bx6b
    This is probably the only channel where I can watch an almost hour long video and not get bored for a tiny second
  • @HeisenbergFam
    Sebastian comes back every once in a while like a Santa to bless us with heavenly vibe
  • The real insane part of these videos isn’t the coding is the visualisation of the coding at the same time to make it so that non coders can understand clearly, I love these videos
  • @KipIngram
    I personally would love to see a "deep dive walkthrough" on you porting this to the GPU. It seems like a great vehicle for introducing a lot of people to GPU programming. You mostly did that part "off screen" here - those details would be hugely interesting, I think.
  • It's so fun to watch these as a physicist! On the one hand, if feels like you get just enough of the physics to follow along with your project, and I, on the other hand, get just enough of the programming to follow along with your video. It's also so cool to see you implementing these ideas. One of my favorite youtube series for sure.
  • @LHInstrumentals
    Developing these prototypes requires a lot of work. The fact you go the extra mile to create visual graphs on what is currently taking place using the Editor itself and no editing tricks is ridiculous, you take a great deal of pride in your work! You are a very smart man!
  • @AngeTheGreat
    This is extremely cool! There's something about the combination of particle-based systems and fluid simulations that is fascinating. I ran into a lot of the same problems while trying to implement a particle-based fluid visualization where the underlying simulation is not actually particle based. A lot of the techniques you showed would definitely be applicable to this problem and I'll look into them a bit more. My solution was just giving up on trying to match the density with the particles and instead make the particles act more like dust that is pulled and pushed by the fluid's motion. It works alright but suffers from its own set of issues... Anyway, great video as always!
  • @Maxy.waxyyy
    I love how the water particles simulate adhesion and cohesion naturally… so cool… (33:05)
  • @alexbroaddus2380
    The way you timed up the music with the waves at 45:58 was so clean 🤌 love the production quality you put into these video
  • @thecakecraft7724
    Sebastian didn’t end his video with “cheers”. My conclusion is that he’s been kidnapped and had to send a secret message that only we’d understand something was weird about. The chessbots must have finally got him.
  • @Oziji
    An absolutely classic Lague video
  • @chimpana
    I did a compsci degree at a decent university many years back... this video is astounding quality and right up there in terms of lecturer quality... and it's free!