# [Programming] How would I simulate water pressure?

1. May 14, 2017

### Aediono

Hey, this is my first time posting and I've been looking everywhere but can't find a decent water model. I hacked a program together to decently simulate water physics, but it's not nearly robust enough for what I need.

So, in this world, everything is based on a grid. Any non-wall tile can have water in it, represented by 0 for completely empty, 1 for completely full, any anything in between. Water generally moves to areas with less pressure, but also factors in velocity.

This means that each block has a water value, pressure and a velocity vector that it tries to impart on not only its neighbors, but potentially everything else in the water simulation.

Ideally, I'd be able to calculate the static pressure on every single block, but I'd have to potentially factor in everything in the same body of water. Imagine if we had a u-bend like this.

Now, the water on the right side would be putting downward pressure on the water on the left side, which would move it up until they equalize. My program simulates that in a very hacky way (essentially finding and remembering a path to a source block or a block with the highest concentration of water, and drawing from it) but I need to actually calculate pressure.

I'd also have to design it in such a way that it could handle new water, and random pressure increases caused by a player.

TLDR: Grid based water simulation, need to calculate pressure for each tile in realtime

Anyone have any tips or concepts I could look into? I'm really out ideas. Thanks a ton.

2. May 15, 2017

### Staff: Mentor

This is not about programming, this is about modeling basic physics concepts.

3. May 15, 2017

### Aediono

Well, it got moved here. I don't really care where it goes.

4. May 15, 2017

### Staff: Mentor

What you are trying to do is computational fluid dynamics. You should probably not be building such a model from scratch.

Have a look at OpenFOAM. One of the tutorials simulates a breaking dam, and I think it wouldn't be too much work to adapt it to your situation.

5. May 15, 2017

### Aediono

Thanks, I'll look into it. Hopefully the pieces I need are simple enough to where I don't need a PhD to chug through it.

6. May 15, 2017

### anorlunda

What is your objective?

If your purpose is to teach yourself about modeling of physical systems or about basic physics of liquids, then it is better to write your own rather than use an external package. Using that little cellular setup in your OP, plus Newton's Laws, plus the continuity equations, you could teach yourself a lot.

7. May 15, 2017

### Aediono

I looked into OpenFOAM. It's definitely overkill, and would never run in realtime. I only need a very small subset of its features, and need to scrap a lot of accuracy to make it run faster.

My main goal is to make a game using the ideas, not just simulate it in another program.

8. May 15, 2017

### Nidum

+1

@Aediono . You can solve for settling time for a system like the one in this diagram just using basic fluid mechanics . If you can solve problems like this then writing software for your squares game should not be too hard . Only real difficulty that I can see would be in keeping track of what is happening in those dead pockets .

Last edited: May 15, 2017
9. May 15, 2017

### jack action

Are you familiar with the Bernoulli's principle?

10. May 18, 2017

### Aediono

Somewhat. How will that help me find the pressure of a given square?

11. May 18, 2017

### Staff: Mentor

IMHO if it is for a simple game P=hgρ (height, gravitational acceleration, density) and Newtonian physics is all you need.

12. May 18, 2017

### jack action

Say you have one square on top of another. The pressure on the top square is the atmospheric pressure pushing on it (your empty squares). The pressure on the bottom square is the pressure pushing on the top square (atmosphere) plus the fluid weight of the top square, i.e. $\rho g h$, where $h$ is the height of the square. That is the static pressure. If the fluid in the top square is going down at a velocity $v$, then you must add the dynamic pressure $\frac{1}{2}\rho v^2$ to the total pressure. That is what $P + \rho g h + \frac{1}{2}\rho v^2$ means.

If you have another square below the «bottom» square, then $P$ is the total pressure of the «bottom» square plus the $\rho g h$ of the «bottom» square, plus the dynamic pressure $\frac{1}{2}\rho v^2$ exerted downward by the «bottom» square, if any.

It also works the same from side to side. Instead of $g$ you use the lateral acceleration, which I assume, in your case, is always zero.

13. May 18, 2017

### chmasy

I don't know if this would help but try a 3D program Blender its a opensource program so the source code is available to edit
it has a great physics engine in it and will simulate water.
blender.org

14. May 18, 2017

### Staff: Mentor

It all starts with the Navier-Stokes equations for incompressible fluids.
Simulating fluids in real time is problematic. You'll have to make some simplifications. Linear flow is certainly one of the assumptions you want to make - turbulence is really complicated. The article discusses some possible simplifications.

15. May 24, 2017

### Aediono

Seems simple, so I'd be able to push water 'up' a pocket after calculating the pressures there? I'm trying to figure out how I'd handle the progrmatically.