# Trying my hand at fluid simulation

Aight, let's roll.

.

So fluid simulation is one of those things I've been meaning to get around to for a long time now. I've made simple particle simulations before but nothing that could simulate an actual fluid. Well, here's my opportunity!

.

The biggest issue I've had over the last couple days is the amount of information on the subject. There's a ton of literature on the subject. Like, metric tons of scientific and mathematical and computational papers out there on the science/math/simulation of fluids of all kinds. And if you're looking for a smooth introduction, almost every beginner's resource I've found says something a little different. For example:

.

So fluid simulation is one of those things I've been meaning to get around to for a long time now. I've made simple particle simulations before but nothing that could simulate an actual fluid. Well, here's my opportunity!

.

The biggest issue I've had over the last couple days is the amount of information on the subject. There's a ton of literature on the subject. Like, metric tons of scientific and mathematical and computational papers out there on the science/math/simulation of fluids of all kinds. And if you're looking for a smooth introduction, almost every beginner's resource I've found says something a little different. For example:

- GPUGems: a little daunting. I'm sure it's fantastic but holy hell just walk me through this stuff slowly please
- Stam's GDC03 paper: completely discounts pressure terms, and forces the fluid to be "incompressible." Also, apparently the algos in the paper are copyrighted so there's that . Also, this is the only source I've seen that talks about a Navier-Stokes equation for the density of a fluid field as well as one for a velocity field
- Cowboy Programming: bases itself off of Stam's paper, but makes modifications. Also points out a lot of issues with Stam's method
- WIkipedia page for Navier Stokes equations: Holy sweet mother of Jesus make the equations stop.

Okay. So let me see if I can make sense of everything. I'll write everything down as I understand it, and hope that I don't butcher everything in the process. I'm going to use this as a sort of documentation for myself, or as a way to kind of re-explain everything to myself and hope that it makes sense.

.

Let's get to it!

.# Compressibility

# of Fluids

.## So there are 2 ways to represent a fluid: as a compressible or an incompressible flow.

.

A compressible fluid is one where changes in pressure or temperature will result in a change of density. All real-world fluids are compressible to some extent, but some don't exhibit it that much. Fluids that react under a certain threshold to changes in pressure or temperature (like liquid water above freezing point and below melting point) can be approximated as an incompressible fluid. Essentially, the density of the fluid doesn't change as it moves through the fluid:

.

.# The Navier-Stokes Equations

### .

### The Navier-Stokes equations were formulated by 2 guys whose last names were Navier and Stokes (who'd have guessed it?). A bunch of other hotshot names like Maxwell and Poisson had a part in it too, from what I understand, but nobody cares, least of all me.

.

Now without further ado, here's the Navier-Stokes equation for incompressible flow:

.

.

Where u is the velocity field, p is the pressure field, rho is the uniform density, and g denotes all external forces, like gravity or magnetism. So a little explanation:

.

That leftmost differential is really what we're interested in.

.

The next term to the right is the advection term (or convection, depending on who's talking). In the same way that the velocity field moves particles and densities through a fluid, the velocity also transports itself over the velocity field. This is called self-advection and is how water currents work.

.

The next term is the diffusion term. The v term is the kinematic viscosity term, and is a constant. This term talks about a fluids aversion to movement. Fluids like molasses are extremely viscous and as a result, don't slosh around as much as, say, water. This is because the velocity field softens out or "diffuses" over the entire fluid at a much faster rate, which is why it's called the diffusion term.

.

The next term, the first one on the other side of the equals sign, is the pressure term. This is the term that talks about how areas with higher density in a fluid tend to try to push away and go somewhere where there's less pressure. This is the term that allows us to make simulations of water, because without this term, the entire fluid would lay in the bottom of the cup and ignore the pressure. Okay, that's a lie, the incompressibility constraint should take care of that, but that's the jist of it anyways.

.

There's also another constraint placed on the equation above:

.

.

This is basically saying that the amount of flow entering is equal to the amount of flow leaving. This is one of those things that's necessary for an incompressible fluid. Notice I said "amount of flow entering" instead of "amount of fluid." Imagine a water particle on the surface of a liquid. On one side it's being pressed by a ton of air and on the other side it's being pressed on by an even greater density of water. However, despite the amount of fluid contesting for the spot, the above constraint ways that as long as they are opposite and opposing, that the fluid particle will keep doing what it's doing.

.

So yeah. Pretty simple all around. I've seen a lot of variation of this thing though. For example, Stam's GDC03 paper completely chucks the pressure term out the window, and also gives a formula for the flow of density through a fluid. A lot of other places use the compressible form of the Navier-Stokes equation, and some are just like "eh. gaussian blurs and pressure looks good to me."

.# Simulating a Fluid

### .

## So there's 2 really simple ways to simulate a fluid. Well, by "simple" I mean that they're ideas that arise naturally from the definition of the Navier-Stokes equation. When you simulate something, you should always try to layout data in a way that makes the most sense and reflects the nature of the system you're trying to simulate. In this case, 2 main ideas pop out instantly:

## Fluid simulation on a discrete grid: this is where you try to approximate a liquid with a bunch of velocity cells in a 2D or 3D array. This reflects the del and laplace operators that you see in the equation very well, because it's easy to calculate those on a discrete field

## Fluid simulation with a bunch of particles: this is where you have a bunch of particles that all behave and interact in such a way that it ends up looking right. This is how fluids work in the real world (fluids aren't actually smooth fields like the mathematics suggest), just at an incredibly small scale. This type reflects well the du/dt term in the equation, because it's easy to give a particle velocity and then integrate its position

## These are really your options. I mean, you *could* be all edgy and store a pressure field but that would be strange and also it's not as easy to advect/convect a pressure field. You could store all sorts of things of things, but these are the main 2 ways.

.

I'm going to go with the first method (mainly because I tried the 2nd option first and it barely worked. Also it requires a lot of N-body-simulation-style optimizations to get working fast because it has a lot of distance functions flying everywhere.

.

So there's 2 things you need if you're going the first direction: a velocity grid and something to figure out what the pressure is for the pressure gradient portion of the equation. This is where funky things start happening.

.

Even though we're modeling an incompressible fluid, where density doesn't change, we're still going store a density field and operate on it as if it weren't. I'm not entirely sure the reason for this. There's a bunch of possibilities I'm thinking:- Theory 1: we're treating the Navier-Stokes equation as something that tries to make an incorrect fluid turn into a more correct fluid. The pressure term and diffusion term might be self-correcting in a way. Speculation, but it's a guess.
- Theory 2: The Ideal Gas Law talks about the relationship between pressure and temperature and density and a bunch of other things that don't really matter. Basically, it says that if volume and temperature and all of that other unimportant stuff remain constant then the pressure of the fluid varies directly with density of the liquid. So I'm thinking that *maybe* what people are storing as a density field is actually a pressure field and nobody's telling me. Again, just a guess.

.# Blah-Blah-Blah-LOOK PRETTY PICTURES

### .

## So this thing is getting really lengthy but I think I've cleared a lot up to myself by writing this. Strange how that works.

## .

?I'll go into gruesome details about how exactly to get past theory and implement everything, don't worry. My end goal is to eventually be able to simulate a fluid something like this:

.

[ image: CulDeVinci makes a surprise return! ]

.

Right now, though, it's looking something awful. As of right now, gravity is kinda buggy (I'm thinking instead of propagating velocities I should propagate momentums, but imma have to try that out) and advection blows up after about 10 seconds of simulation or so, so the only thing happening is fluid propagation and fluid pressure. Here's a demonstration of what a zero-gravity non-coheasive fluid might look like:

.

[media][/media]

.

Yeah, I have to admit, it looks pretty funky. Oh well. I'll write another entry when I have something that actually works and doesn't look *completely* stupid.

.

Until, then, see yah, and thanks for reading!

- Theory 1: we're treating the Navier-Stokes equation as something that tries to make an incorrect fluid turn into a more correct fluid. The pressure term and diffusion term might be self-correcting in a way. Speculation, but it's a guess.

Sign in to follow this

Followers
0

## 0 Comments

## Recommended Comments

There are no comments to display.

## Create an account or sign in to comment

You need to be a member in order to leave a comment

## Create an account

Sign up for a new account in our community. It's easy!

Register a new account## Sign in

Already have an account? Sign in here.

Sign In Now