From 1cd5494a54280c6d2e29ad6af3108d60f5acf3b7 Mon Sep 17 00:00:00 2001 From: H Rohitha Aiswarya Date: Wed, 9 Oct 2024 11:25:26 +0530 Subject: [PATCH 1/2] 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. --- searches/bernoullis_principle.py | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 searches/bernoullis_principle.py diff --git a/searches/bernoullis_principle.py b/searches/bernoullis_principle.py new file mode 100644 index 000000000000..d68edb6652fb --- /dev/null +++ b/searches/bernoullis_principle.py @@ -0,0 +1,68 @@ +""" +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 + +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) +- 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. + + 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") From 080b658381faba133d50dd25b986f43f6b69e86a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Oct 2024 05:56:25 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- searches/bernoullis_principle.py | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/searches/bernoullis_principle.py b/searches/bernoullis_principle.py index d68edb6652fb..10a92d19447e 100644 --- a/searches/bernoullis_principle.py +++ b/searches/bernoullis_principle.py @@ -2,9 +2,9 @@ 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 +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: @@ -24,8 +24,15 @@ 100732.64 """ -def bernoullis_principle(velocity1: float, pressure1: float, height1: float, - velocity2: float, height2: float, density: float = 1.225) -> float: + +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. @@ -39,7 +46,7 @@ def bernoullis_principle(velocity1: float, pressure1: float, height1: float, Returns: float: Pressure at point 2 in Pascals - + Example: >>> bernoullis_principle(density=1000, velocity=5, height=10, pressure=101325) 144413.5 @@ -51,8 +58,13 @@ def bernoullis_principle(velocity1: float, pressure1: float, height1: float, 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) + pressure2 = ( + pressure1 + + 0.5 * density * (velocity1**2) + + density * g * height1 + - 0.5 * density * (velocity2**2) + - density * g * height2 + ) return round(pressure2, 2) @@ -61,6 +73,7 @@ def bernoullis_principle(velocity1: float, pressure1: float, height1: float, if __name__ == "__main__": # Example doctest import doctest + doctest.testmod() # Manual test