[Programming] How would I simulate water pressure?

AI Thread Summary
The discussion revolves around creating a grid-based water simulation that accurately calculates pressure in real-time. The original poster has developed a basic program but seeks a more robust solution for simulating water physics, particularly focusing on pressure dynamics in a grid system. Key concepts include the need for a computational fluid dynamics model, with suggestions to explore existing tools like OpenFOAM, although concerns about its complexity and performance for real-time applications are noted.Participants emphasize the importance of understanding basic physics principles, such as Bernoulli's principle and the Navier-Stokes equations, to calculate pressure effectively. They suggest that while using external packages can be beneficial for learning, building a custom model may provide deeper insights into fluid mechanics. Simplifications may be necessary to achieve real-time performance, particularly in managing fluid dynamics in dead pockets within the simulation. The conversation highlights the balance between accuracy and computational efficiency in developing a functional water simulation for gaming purposes.
Aediono
Messages
6
Reaction score
0
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.

FUiOGjT.png


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.

IwM3WVD.png


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.
 
Technology news on Phys.org
This is not about programming, this is about modeling basic physics concepts.
 
Borek said:
This is not about programming, this is about modeling basic physics concepts.

Well, it got moved here. I don't really care where it goes.
 
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.
 
DrClaude said:
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.

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.
 
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.
 
anorlunda said:
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.

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.
 
Water columns.jpg


anorlunda said:
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.

+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:
  • #10
jack action said:
Are you familiar with the Bernoulli's principle?

Somewhat. How will that help me find the pressure of a given square?
 
  • #11
IMHO if it is for a simple game P=hgρ (height, gravitational acceleration, density) and Newtonian physics is all you need.
 
  • #12
Aediono said:
Somewhat. How will that help me find the pressure of a given square?
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
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
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
jack action said:
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.

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.
 
Back
Top