Skip to content

Commit 55abd63

Browse files
Add Bernoulli's Principle Implementation
- Implemented Bernoulli's Principle to calculate unknown variables (pressure, velocity, or height) based on given inputs. - Added Python function that uses Bernoulli's equation for an incompressible, frictionless fluid to perform these calculations. - The function allows for fluid density and gravitational acceleration to be adjusted, making it versatile for different scenarios. - Includes detailed documentation explaining Bernoulli's Principle, the equation used, and the inputs/outputs of the function. - Provided an example use case demonstrating how the function works in practice.
1 parent e9e7c96 commit 55abd63

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

physics/bernoullis_principle.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
Title: Bernoulli's Principle Implementation
3+
4+
Description:
5+
This Python script implements Bernoulli's Principle, which describes the behavior of
6+
a fluid under varying conditions of pressure, velocity, and height. Bernoulli's equation
7+
is applied to an incompressible, frictionless fluid to calculate the unknown variable
8+
(pressure, velocity, or height) when the others are known.
9+
10+
Bernoulli's Equation:
11+
P1 + 0.5 * ρ * v1^2 + ρ * g * h1 = P2 + 0.5 * ρ * v2^2 + ρ * g * h2
12+
13+
Where:
14+
- P1, P2 are pressures at points 1 and 2 (in Pascals),
15+
- v1, v2 are velocities at points 1 and 2 (in m/s),
16+
- h1, h2 are heights at points 1 and 2 (in meters),
17+
- ρ is the fluid density (in kg/m³, default is 1000 for water),
18+
- g is the acceleration due to gravity (default is 9.81 m/s²).
19+
20+
The function `bernoullis_principle` calculates one unknown variable based on inputs and returns the result.
21+
"""
22+
23+
def bernoullis_principle(P1, v1, h1, P2=None, v2=None, h2=None, density=1000, g=9.81):
24+
"""
25+
Apply Bernoulli's Principle to calculate unknown variables.
26+
27+
Parameters:
28+
P1 : float -> Pressure at point 1 (in Pascals)
29+
v1 : float -> Velocity at point 1 (in m/s)
30+
h1 : float -> Height at point 1 (in meters)
31+
P2 : float -> Pressure at point 2 (in Pascals) (optional)
32+
v2 : float -> Velocity at point 2 (in m/s) (optional)
33+
h2 : float -> Height at point 2 (in meters) (optional)
34+
density : float -> Fluid density (in kg/m^3) (default is 1000 for water)
35+
g : float -> Acceleration due to gravity (default is 9.81 m/s²)
36+
37+
Returns:
38+
- If one unknown is provided (P2, v2, or h2), the function calculates the missing value.
39+
"""
40+
41+
if P2 is None:
42+
# Calculate Pressure at point 2 (P2)
43+
P2 = P1 + 0.5 * density * (v1**2 - v2**2) + density * g * (h1 - h2)
44+
return P2
45+
elif v2 is None:
46+
# Calculate Velocity at point 2 (v2)
47+
v2 = ((2 * (P1 - P2 + density * g * (h1 - h2))) / density + v1**2)**0.5
48+
return v2
49+
elif h2 is None:
50+
# Calculate Height at point 2 (h2)
51+
h2 = h1 + (P1 - P2 + 0.5 * density * (v1**2 - v2**2)) / (density * g)
52+
return h2
53+
else:
54+
return "Please provide at least one unknown (P2, v2, or h2)."
55+
56+
57+
# Example Usage
58+
# Given: P1 = 101325 Pa, v1 = 5 m/s, h1 = 10 m, v2 = 10 m/s, h2 = 5 m
59+
P1 = 101325 # Pressure at point 1 in Pascals
60+
v1 = 5 # Velocity at point 1 in m/s
61+
h1 = 10 # Height at point 1 in meters
62+
v2 = 10 # Velocity at point 2 in m/s
63+
h2 = 5 # Height at point 2 in meters
64+
65+
# Calculate pressure at point 2 (P2)
66+
P2 = bernoullis_principle(P1, v1, h1, v2=v2, h2=h2)
67+
print(f"Pressure at point 2: {P2} Pascals")

0 commit comments

Comments
 (0)