# [Programming] How would I simulate water pressure?

• Aediono
In summary: If the bottom square has no room for water, the pressure is the same as if it did, because it can't fill up any more. But if there is room, then the bottom square can fill up, and the pressure on the bottom square is that of the weight of the top square plus the weight of the water.In summary, the conversation discusses the need for a water simulation model for a game, based on a grid system. The model should be able to calculate pressure for each tile in real-time and handle new water and random pressure changes caused by players. Suggestions for existing packages and concepts to look into are provided, including the use of Bernoulli's
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.

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.

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:

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.

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:
Aediono said:
Anyone have any tips or concepts I could look into?
Are you familiar with the Bernoulli's principle?

jack action said:
Are you familiar with the Bernoulli's principle?

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

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

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.

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

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.

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.

## 1. What is water pressure?

Water pressure is the force that water exerts on the walls of a container or the surface it comes into contact with. It is typically measured in units of pounds per square inch (psi) or kilopascals (kPa).

## 2. How do you simulate water pressure in a programming language?

To simulate water pressure in a programming language, you can use mathematical equations to calculate the pressure at different points in a system. This involves considering factors such as the volume of water, the area of the container or surface, and the force exerted by gravity.

## 3. Can you provide an example of simulating water pressure in a programming language?

One example of simulating water pressure in a programming language is using the Navier-Stokes equations, which describe the motion of fluids. By solving these equations numerically, you can simulate the behavior of water in a system and calculate the pressure at different points.

## 4. What factors can affect water pressure in a simulation?

The factors that can affect water pressure in a simulation include the volume and density of water, the size and shape of the container or system, and the forces acting on the water, such as gravity or external pressure.

## 5. How accurate are simulations of water pressure in programming?

The accuracy of simulations of water pressure in programming depends on the complexity of the system and the precision of the mathematical models used. In general, with proper calibration and consideration of all relevant factors, simulations can provide a close approximation of real-world water pressure.

• Mechanical Engineering
Replies
5
Views
852
• Classical Physics
Replies
3
Views
905
• Mechanics
Replies
32
Views
4K
• DIY Projects
Replies
21
Views
5K
• Mechanical Engineering
Replies
20
Views
7K
• DIY Projects
Replies
39
Views
9K
• Mechanics
Replies
4
Views
1K
• Mechanics
Replies
9
Views
2K