Skip to content

Create bernoullis_principle.py #11916

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions searches/bernoullis_principle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
"""
Title: Bernoulli's Principle

Description:
This algorithm implements Bernoulli's Principle to calculate the unknown pressure
at a second point in a fluid system using the Bernoulli equation. Bernoulli's equation
states that for an incompressible, frictionless fluid, the total mechanical energy
(remains constant) as the fluid flows.

The equation used:
P1 + 0.5 * ρ * v1^2 + ρ * g * h1 = P2 + 0.5 * ρ * v2^2 + ρ * g * h2

Check failure on line 11 in searches/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

searches/bernoullis_principle.py:11:12: RUF002 Docstring contains ambiguous `ρ` (GREEK SMALL LETTER RHO). Did you mean `p` (LATIN SMALL LETTER P)?

Check failure on line 11 in searches/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

searches/bernoullis_principle.py:11:23: RUF002 Docstring contains ambiguous `ρ` (GREEK SMALL LETTER RHO). Did you mean `p` (LATIN SMALL LETTER P)?

Check failure on line 11 in searches/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

searches/bernoullis_principle.py:11:47: RUF002 Docstring contains ambiguous `ρ` (GREEK SMALL LETTER RHO). Did you mean `p` (LATIN SMALL LETTER P)?

Check failure on line 11 in searches/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

searches/bernoullis_principle.py:11:58: RUF002 Docstring contains ambiguous `ρ` (GREEK SMALL LETTER RHO). Did you mean `p` (LATIN SMALL LETTER P)?

Where:
- P1 and P2 are the pressures at point 1 and point 2 (Pascals)
- v1 and v2 are the fluid velocities at point 1 and point 2 (m/s)
- h1 and h2 are the heights at point 1 and point 2 (m)
- ρ is the density of the fluid (kg/m^3)

Check failure on line 17 in searches/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

searches/bernoullis_principle.py:17:3: RUF002 Docstring contains ambiguous `ρ` (GREEK SMALL LETTER RHO). Did you mean `p` (LATIN SMALL LETTER P)?
- g is the gravitational constant (9.81 m/s^2)

This algorithm solves for P2, the unknown pressure at point 2.

Example:
>>> bernoullis_principle(10, 101325, 0, 15, 5)
100732.64
"""


def bernoullis_principle(
velocity1: float,
pressure1: float,
height1: float,
velocity2: float,
height2: float,
density: float = 1.225,
) -> float:
"""
Calculate the unknown pressure at a second point in a fluid system using Bernoulli's equation.

Check failure on line 37 in searches/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

searches/bernoullis_principle.py:37:89: E501 Line too long (98 > 88)

Parameters:
velocity1 (float): velocity at point 1 in m/s
pressure1 (float): pressure at point 1 in Pascals
height1 (float): height at point 1 in meters
velocity2 (float): velocity at point 2 in m/s
height2 (float): height at point 2 in meters
density (float): density of the fluid in kg/m^3 (default is 1.225, for air)

Returns:
float: Pressure at point 2 in Pascals

Example:
>>> bernoullis_principle(density=1000, velocity=5, height=10, pressure=101325)
144413.5
>>> bernoullis_principle(density=500, velocity=10, height=5, pressure=0)
25000.0
>>> bernoullis_principle(density=997, velocity=0, height=0, pressure=101325)
101325.0
"""
g = 9.81 # gravitational constant in m/s^2

# Bernoulli's equation to solve for pressure at point 2
pressure2 = (
pressure1
+ 0.5 * density * (velocity1**2)
+ density * g * height1
- 0.5 * density * (velocity2**2)
- density * g * height2
)

return round(pressure2, 2)


# Example usage
if __name__ == "__main__":
# Example doctest
import doctest

doctest.testmod()

# Manual test
pressure_at_point_2 = bernoullis_principle(10, 101325, 0, 15, 5)
print(f"Pressure at point 2: {pressure_at_point_2} Pascals")
Loading