Skip to content

Commit 1cd5494

Browse files
Create bernoullis_principle.py
- Implemented Bernoulli's Principle to calculate unknown pressure in a fluid system. - This function computes the pressure at a second point using the velocities, pressures, and heights of two points. - Type hints added for function parameters and return value. - Included example and documentation for clarity. - Doctests and an example provided for easy verification.
1 parent e9e7c96 commit 1cd5494

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

searches/bernoullis_principle.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""
2+
Title: Bernoulli's Principle
3+
4+
Description:
5+
This algorithm implements Bernoulli's Principle to calculate the unknown pressure
6+
at a second point in a fluid system using the Bernoulli equation. Bernoulli's equation
7+
states that for an incompressible, frictionless fluid, the total mechanical energy
8+
(remains constant) as the fluid flows.
9+
10+
The equation used:
11+
P1 + 0.5 * ρ * v1^2 + ρ * g * h1 = P2 + 0.5 * ρ * v2^2 + ρ * g * h2
12+
13+
Where:
14+
- P1 and P2 are the pressures at point 1 and point 2 (Pascals)
15+
- v1 and v2 are the fluid velocities at point 1 and point 2 (m/s)
16+
- h1 and h2 are the heights at point 1 and point 2 (m)
17+
- ρ is the density of the fluid (kg/m^3)
18+
- g is the gravitational constant (9.81 m/s^2)
19+
20+
This algorithm solves for P2, the unknown pressure at point 2.
21+
22+
Example:
23+
>>> bernoullis_principle(10, 101325, 0, 15, 5)
24+
100732.64
25+
"""
26+
27+
def bernoullis_principle(velocity1: float, pressure1: float, height1: float,
28+
velocity2: float, height2: float, density: float = 1.225) -> float:
29+
"""
30+
Calculate the unknown pressure at a second point in a fluid system using Bernoulli's equation.
31+
32+
Parameters:
33+
velocity1 (float): velocity at point 1 in m/s
34+
pressure1 (float): pressure at point 1 in Pascals
35+
height1 (float): height at point 1 in meters
36+
velocity2 (float): velocity at point 2 in m/s
37+
height2 (float): height at point 2 in meters
38+
density (float): density of the fluid in kg/m^3 (default is 1.225, for air)
39+
40+
Returns:
41+
float: Pressure at point 2 in Pascals
42+
43+
Example:
44+
>>> bernoullis_principle(density=1000, velocity=5, height=10, pressure=101325)
45+
144413.5
46+
>>> bernoullis_principle(density=500, velocity=10, height=5, pressure=0)
47+
25000.0
48+
>>> bernoullis_principle(density=997, velocity=0, height=0, pressure=101325)
49+
101325.0
50+
"""
51+
g = 9.81 # gravitational constant in m/s^2
52+
53+
# Bernoulli's equation to solve for pressure at point 2
54+
pressure2 = (pressure1 + 0.5 * density * (velocity1 ** 2) + density * g * height1
55+
- 0.5 * density * (velocity2 ** 2) - density * g * height2)
56+
57+
return round(pressure2, 2)
58+
59+
60+
# Example usage
61+
if __name__ == "__main__":
62+
# Example doctest
63+
import doctest
64+
doctest.testmod()
65+
66+
# Manual test
67+
pressure_at_point_2 = bernoullis_principle(10, 101325, 0, 15, 5)
68+
print(f"Pressure at point 2: {pressure_at_point_2} Pascals")

0 commit comments

Comments
 (0)