Skip to content
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
68 changes: 68 additions & 0 deletions physics/bernoullis_principle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
"""
Title: Bernoulli's Principle Implementation

Description:
This Python script implements Bernoulli's Principle, which describes the behavior of
a fluid under varying conditions of pressure, velocity, and height. Bernoulli's equation
is applied to an incompressible, frictionless fluid to calculate the unknown variable
(pressure, velocity, or height) when the others are known.

Bernoulli's Equation:
P1 + 0.5 * ρ * v1^2 + ρ * g * h1 = P2 + 0.5 * ρ * v2^2 + ρ * g * h2

Check failure on line 11 in physics/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

physics/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 physics/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

physics/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 physics/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

physics/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 physics/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

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

Where:
- P1, P2 are pressures at points 1 and 2 (in Pascals),
- v1, v2 are velocities at points 1 and 2 (in m/s),
- h1, h2 are heights at points 1 and 2 (in meters),
- ρ is the fluid density (in kg/m³, default is 1000 for water),

Check failure on line 17 in physics/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (RUF002)

physics/bernoullis_principle.py:17:3: RUF002 Docstring contains ambiguous `ρ` (GREEK SMALL LETTER RHO). Did you mean `p` (LATIN SMALL LETTER P)?
- g is the acceleration due to gravity (default is 9.81 m/s²).

The function `bernoullis_principle` calculates one unknown variable based on inputs and returns the result.

Check failure on line 20 in physics/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

physics/bernoullis_principle.py:20:89: E501 Line too long (107 > 88)
"""


def bernoullis_principle(P1, v1, h1, P2=None, v2=None, h2=None, density=1000, g=9.81):

Check failure on line 24 in physics/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

physics/bernoullis_principle.py:24:26: N803 Argument name `P1` should be lowercase

Check failure on line 24 in physics/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N803)

physics/bernoullis_principle.py:24:38: N803 Argument name `P2` should be lowercase

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there is no test file in this pull request nor any test function or class in the file physics/bernoullis_principle.py, please provide doctest for the function bernoullis_principle

Please provide return type hint for the function: bernoullis_principle. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: P1

Please provide type hint for the parameter: v1

Please provide type hint for the parameter: h1

Please provide type hint for the parameter: P2

Please provide type hint for the parameter: v2

Please provide type hint for the parameter: h2

Please provide type hint for the parameter: density

Please provide descriptive name for the parameter: g

Please provide type hint for the parameter: g

"""
Apply Bernoulli's Principle to calculate unknown variables.

Parameters:
P1 : float -> Pressure at point 1 (in Pascals)
v1 : float -> Velocity at point 1 (in m/s)
h1 : float -> Height at point 1 (in meters)
P2 : float -> Pressure at point 2 (in Pascals) (optional)
v2 : float -> Velocity at point 2 (in m/s) (optional)
h2 : float -> Height at point 2 (in meters) (optional)
density : float -> Fluid density (in kg/m^3) (default is 1000 for water)
g : float -> Acceleration due to gravity (default is 9.81 m/s²)

Returns:
- If one unknown is provided (P2, v2, or h2), the function calculates the missing value.

Check failure on line 39 in physics/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (E501)

physics/bernoullis_principle.py:39:89: E501 Line too long (92 > 88)
"""

if P2 is None:
# Calculate Pressure at point 2 (P2)
P2 = P1 + 0.5 * density * (v1**2 - v2**2) + density * g * (h1 - h2)

Check failure on line 44 in physics/bernoullis_principle.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

physics/bernoullis_principle.py:44:9: N806 Variable `P2` in function should be lowercase
return P2
elif v2 is None:
# Calculate Velocity at point 2 (v2)
v2 = ((2 * (P1 - P2 + density * g * (h1 - h2))) / density + v1**2) ** 0.5
return v2
elif h2 is None:
# Calculate Height at point 2 (h2)
h2 = h1 + (P1 - P2 + 0.5 * density * (v1**2 - v2**2)) / (density * g)
return h2
else:
return "Please provide at least one unknown (P2, v2, or h2)."


# Example Usage
# Given: P1 = 101325 Pa, v1 = 5 m/s, h1 = 10 m, v2 = 10 m/s, h2 = 5 m
P1 = 101325 # Pressure at point 1 in Pascals
v1 = 5 # Velocity at point 1 in m/s
h1 = 10 # Height at point 1 in meters
v2 = 10 # Velocity at point 2 in m/s
h2 = 5 # Height at point 2 in meters

# Calculate pressure at point 2 (P2)
P2 = bernoullis_principle(P1, v1, h1, v2=v2, h2=h2)
print(f"Pressure at point 2: {P2} Pascals")