But How DO Fluid Simulations Work?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

A cool video idea I had, maybe it already exists, but we need a resource on mastering the 2D canvas and how to use it in both artistic and physics ways. Concepts of a fluid simulation can be applied to programming language and drawn to the canvas , but having a single resource to explain complex algorithms ON that canvas similar to p5.js is what I’m looking for

πŸ‘οΈŽ︎ 16 πŸ‘€οΈŽ︎ u/PostMaloy πŸ“…οΈŽ︎ Dec 16 2020 πŸ—«︎ replies

wow!

πŸ‘οΈŽ︎ 3 πŸ‘€οΈŽ︎ u/TheLegendofPit πŸ“…οΈŽ︎ Dec 16 2020 πŸ—«︎ replies

Nice! Was just looking for something like this

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/PostMaloy πŸ“…οΈŽ︎ Dec 16 2020 πŸ—«︎ replies

That is a great video. Ill definitely use this sometime in the future. Thank you.

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/Moondefender πŸ“…οΈŽ︎ Dec 16 2020 πŸ—«︎ replies

This is a great video. I've always been fascinated by fluid simulations.

πŸ‘οΈŽ︎ 2 πŸ‘€οΈŽ︎ u/CptDecaf πŸ“…οΈŽ︎ Dec 16 2020 πŸ—«︎ replies

This post appears to be a direct link to a video.

As a reminder, please note that posting footage of a game in a standalone thread to request feedback or show off your work is against the rules of /r/gamedev. That content would be more appropriate as a comment in the next Screenshot Saturday (or a more fitting weekly thread), where you'll have the opportunity to share 2-way feedback with others.

/r/gamedev puts an emphasis on knowledge sharing. If you want to make a standalone post about your game, make sure it's informative and geared specifically towards other developers.

Please check out the following resources for more information:

Weekly Threads 101: Making Good Use of /r/gamedev

Posting about your projects on /r/gamedev (Guide)

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/AutoModerator πŸ“…οΈŽ︎ Dec 16 2020 πŸ—«︎ replies

Is this by the same guy as the gonkee channel on youtube?

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/imavlr πŸ“…οΈŽ︎ Dec 16 2020 πŸ—«︎ replies

Pretty excellent! I’m working on a pseudo liquid sim... somewhere between soft body and fluid. It’s more node based than grid, and I feel like I’ve been intuiting how fluid dynamics have always worked. This video was really useful in helping me understand some of the broad concepts that I may have overlooked. Like, I have the concept of pressure only in so far as each node has spring forces between its nearest neighbours. And I have viscosity but only in terms of each node having a couple of friction values (linear and constant).

Oops just thinking to myself. But yeah thanks for the video. I figured that a grid based solution saves you a lot of time with nearest neighbours,and the maths to distribute pressure from the perspective of each cell is really interesting. I can see why it runs so fast!

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/HilariousCow πŸ“…οΈŽ︎ Dec 16 2020 πŸ—«︎ replies

awesome man! you should consider doing a full series of tutorials on the topic

πŸ‘οΈŽ︎ 1 πŸ‘€οΈŽ︎ u/Adam20188 πŸ“…οΈŽ︎ Dec 16 2020 πŸ—«︎ replies
Captions
fluid simulations these have always been one of the more fascinating programming feats to me how on earth is it possible that a computer with all its order logic and ones and zeros can recreate the crashing waves the rolling clouds and the swirling smoke that we see in our daily lives phenomena which seem characterized by randomness and chaos welcome to inspecto this video is the first of a series of videos exploring deep and fascinating academic concepts this time i set out to find out how exactly fluid simulations work and will be attempting to explain them hopefully at the end of the video a competent programmer would be able to create their own fluid simulation using the provided information [Music] if you've been exposed to any information regarding fluid simulations it's likely that you've heard of the navy or stokes equations the two most well known of which look like this the first shorter equation simply states that divergence of velocity equals zero what this means is that the velocities in neighboring areas of a fluid cannot be flowing towards each other or away from each other this is because that would mean that somewhere in the middle matter would either have to be created out of nothing or disappear into nothing essentially the equation makes sure that mass is conserved in the fluid the second equation states that the acceleration of a fluid is dependent on its internal forces including its pressure gradient and viscosity and also on external forces a fluid has a higher pressure gradient if there is a larger difference in pressure between its different areas and the fluid will tend to flow from the high pressure area to the low pressure area a higher viscosity means that there is a higher friction between the fluid's particles and the velocity in the fluid in one spot will more easily spread out to surrounding areas external forces just include things such as gravity and interference by objects in the fluid or surrounding walls so that was a brief explanation of the navier-stokes equations and you might agree that they seem to apply some fundamental laws of physics to fluids and are very important in the simulation of fluids however i'd like to point out that they give us the specification not the implementation this means that while the equations tell us what rules our fluid simulation have to follow they're almost useless in telling us what the simulation process actually looks like in the rest of the video we'll cover the actual simulation process and while you might be able to see some links between the navy or stokes equations and the procedures which we are implementing there are some other mathematical concepts which are much more relevant and involved [Music] there are a few different ways to represent a fluid in a computer simulation namely the simulation could either use particles which move around or a grid of stationary regions with attributes that represent the average of all the imaginary particles that would be in each grid square the levels of detail used to represent each area can also vary depending on the density of each area so that computation power is used most efficiently where it is most needed it would be impossible for this video to cover all these different methods so we'll only be looking at the type of implementation with a grid of square regions without the dynamic detail allocation all the grid squares will be the same size and stay static specifically the implementation covered in this video will be going off the 2003 paper real-time fluid dynamics for games written by your stam which was also presented by him at the 2003 game developers conference this is a very famous paper probably the most well-known and sighted resource in the field of fluid simulation for video games it presents a simple but effective fluid solver which doesn't cover every possible feature but gets the job done and will hopefully be relatively easier for you to wrap your head around we will also exclusively be dealing with 2d but the principles covered in this video can certainly be extended to 3d as well now let's return to our 2d grid each grid square will have attached to it information about the fluid that is present in that square the most important attribute of which is its velocity how fast the fluid is moving and the direction that it's moving in of course the most important feature of the fluid is arguably that it flows each grid square can also have other attributes such as density and temperature but the way that all attributes distribute themselves around the simulation area ultimately comes back to the velocity even the velocity attribute itself this video will cover the implementation of velocity and one other attribute the density and the goal of the simulation is to take in a snapshot of all the attributes at a given time then calculate how those attributes will change over many iterations and over many time steps of course these attributes can also be continuously altered throughout the simulation as an input to be interacted with the first feature that we'll go through is actually not dependent on the velocities in the fluid diffusion is simply when attributes of each part of the fluid spread into the surrounding area and the actual calculation involves making each square's value gradually become the average value of the squares surrounding it let's first consider diffusion of the density to see how this is done let's let d equal the density of the square and s equal the average of the densities of the surrounding squares and k equal the amount by which we are changing the value this amount will vary due to factors such as the time step and the viscosity of the fluid since we're trying to get the next iterations densities from the current densities let's use subscript c to represent that the value is from the current iteration and n to represent that it's from the next of course the current values are values that we already know while the next values are values which we don't know and remember we basically want to gradually make d become s one way of gradually changing the values is to start at the current density and then simply add on the difference between that and the surrounding average multiplied by k the amount this is a linear interpolation but that presents a problem when k is too large specifically larger than 1 we will actually overshoot the target value and go past it this will result in unstable changes such as density values actually going negative and jumping up and down unexpectedly we can't just cap k at 1 as well as that's unrealistic and limits how fast the simulation can go the solution is instead of just using the current values to calculate the next values we try to find the next values which when rewinded back in time results in the current values coming up with an equation to represent this we can see that it's very similar to the previous one except the current and next are swapped and it's minus instead of plus rearranging this to make dn the subject we can see that it's a hyperbolic relation and not a linear one now no matter how big k is we'll never overshoot the target value but only converge to it this is a stable way of interpolating but it presents another problem we need to know the next surrounding values for the calculation which we obviously don't know yet let's expand out the s value into what it really is the average of the surrounding d values since there are many d values and each d values expression contains the other d values this is essentially a system of simultaneous equations now we humans can solve simultaneous equations using different steps and coming up with different procedures depending what the system of equations is exactly but we need the computer to be able to handle any system of equations no matter what the values are using the same procedure every time hence we need to use an iterative solver to approximate what the solutions are and remember the solutions are the dn values the iterative solver we'll be using is called the gauss-seidel method [Music] basically for all the values that we don't know yet we assign something random to them like zero now we know the values but they're just incorrect but we can now start solving the equations using these values updating the values with the new results we get from the equations we repeat this process many times each time using the newest updated values very quickly as we repeat it more and more times the values we get converge to the true solutions for the system of equations however the gauss-seidel method does require a few conditions in order to work usually the system of equations has to have a matrix that's strictly diagonally dominant which means that the coefficients that lie on the diagonal have to have a magnitude that's larger than the sum of the magnitudes of all the other coefficients in that row this ensures that in the solved equation for each variable the variable coefficients don't add up to be more than the denominator so the values we get are controlled and don't start blowing up however going back to our density equation we can see that it already does satisfy this condition since each of the coefficients is one over four and four of them added up multiplied by k will never be larger than the denominator of one plus k so that's how we diffuse attributes in the fluid we simply repeatedly solve an equation for each square and their densities will converge to the diffuse densities that they are supposed to have advection is the movement of attributes through the fluid following its velocity for the sake of consistency we'll continue to consider the density attribute this is a relatively simple step it would be even simpler if each square's velocity vector pointed exactly into the center of another square because then we could simply move its density into the new square however the velocity vectors almost never point exactly into another square center which means that the density which moves following that vector will actually affect the four square sensors surrounding the target spot it's quite inconvenient to move density from their current positions into new positions and accurately distribute them into the surrounding squares as there can be many vectors pointing to the same spot hence we will once again trace backwards to find out where exactly its next density will come from where this resulting position is we will linear interpolate between the four surrounding square center's densities to find the target density this way each square only requires one calculation to find the position where the next density value is coming from we can simply get the position of the square and subtract its velocity from it multiplied by the delta time then to find out what square centers surround this position we can find the floor part of the coordinates or in other words the integer part to find the precise position in between those square centers we can find the fracked part or the fractional part then using a linear interplay function which looks like this we can interpolate between the four surrounding square centers density values by first individually interpolating between the two upper values and two lower values using the fractional x-coordinate then interpolating between those two results using the fractional y-coordinate and that will give the new density value for the square in question clearing divergence i'd say this is the most complicated step in the whole video so strap in to understand this step we must first understand the concepts of curl and divergence essentially in a vector field curl is when the vectors swirl around and divergence is when the vectors either point away from each other or point towards each other these two are properties universal to every vector field given any vector field we can measure and find out how much curl it has and how much divergence it has now let's think about a fluid a fluid swirls around a lot which means it has a lot of curl however if the fluid has divergence that would mean that there'd have to be material disappearing into nothing or material created out of nothing therefore we want our fluid's velocity field to have curl but not divergence however after we do all that diffusion and advection to simulate the change in the fluid's velocity field we actually end up with a velocity field which has both curl and divergence so we want to only extract out the divergence free part this is done through a process known as the helmholtz decomposition according to helmholtz's theorem also known as the fundamental theorem of vector calculus any vector fields can be expressed as the sum of two vector fields one which is free of curl and one which is free of divergence there's no direct way to compute the divergence free part so our goal is to compute the curl free part then subtract that from the original velocity field to get the divergence free part first let's calculate the divergence at each position in the field using this equation it involves a difference in x velocities across two horizontal squares plus a difference in y velocities across two vertical squares divided by the distance between those squares which is two to quickly visualize this just on one axis we can see that if the larger position has the larger velocity and the smaller position has the smaller velocity there's more stuff flowing out than flowing in and the divergence is positive vice versa there's more stuff flowing in than out and the divergence is negative after finding the divergence at every position in the field the next step uses a system of simultaneous equations which look like this where there is many p-values which all include the other p-values in their equation we can once again use the gauss-seidel method to solve for p after that we have a field of p values which are scalar values and we'll find the gradient vector field of this scalar field one of the identities of vector calculus states that the curl of such a gradient vector field is always zero indeed this is the curl free component we are looking for and we can now subtract it from the original velocity field to clear the fluid of divergence with the side effects of adding lots of swirls so those are some of the main functions which a fluid simulation can have and i chose to share them as i found them to be quite ingenious and elegant in representing real-life physics in a way which is algorithmically convenient and applicable there are many other potential features for a fluid simulation such as boundaries foaming and vorticity confinement but i won't go into those in this video this is merely an introduction to the world of fluid simulations and i hope you can walk away from this video with a better understanding of them other than that i'll see you next time
Info
Channel: Gonkee
Views: 140,336
Rating: undefined out of 5
Keywords: physics, inspecto, fluid, simulation, game, video, video game, fluid simulation, liquid
Id: qsYE1wMEMPA
Channel Id: undefined
Length: 15min 11sec (911 seconds)
Published: Wed Dec 16 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.