From bdc7cbbecd5e6b34c342cf8e0efa8ccb2da3a9aa Mon Sep 17 00:00:00 2001 From: DIvkov575 Date: Thu, 13 Oct 2022 22:56:57 -0400 Subject: [PATCH 1/6] Added archimedes principle (physics) --- physics/archimedes_principle.py | 46 +++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 physics/archimedes_principle.py diff --git a/physics/archimedes_principle.py b/physics/archimedes_principle.py new file mode 100644 index 000000000000..2b4e3b9d30ef --- /dev/null +++ b/physics/archimedes_principle.py @@ -0,0 +1,46 @@ +""" +Calculates buoyant force on object submerged within static fluid. +Discovered by greek mathematician, Archimedes. The principle is named after him. + +Equation for calculating buoyant force: +Fb = ρ * V * g + +Source: +- https://en.wikipedia.org/wiki/Archimedes%27_principle +""" + + +# Acceleration Constant on Earth (unit m/s^2) +g = 9.80665 + + +def archimedes_principle(fluid_density: float, volume: float, gravity: float = g) -> float: + """ + Args: + fluid_density: density of fluid (kg/m^3) + volume: volume of object / liquid being displaced by object + gravity: Acceleration from gravity. Gravitational force on system. Default is Earth Gravity + returns: + buoyant force on object in Newtons + + >>> archimedes_principle(fluid_density=997, volume=0.5, gravity=9.8) + 4885.3 + >>> archimedes_principle(fluid_density=997, volume=0.7) + 6844.061035 + """ + + if fluid_density <= 0: + raise ValueError("Impossible fluid density") + if volume < 0: + raise ValueError("Impossible Object volume") + if gravity <= 0: + raise ValueError("Impossible Gravity") + + return fluid_density * gravity * volume + + +if __name__ == "__main__": + import doctest + + # run doctest + doctest.testmod() From 6f70b4288c1d5ecce90625b2f277d43968525374 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 14 Oct 2022 02:58:43 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/archimedes_principle.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/physics/archimedes_principle.py b/physics/archimedes_principle.py index 2b4e3b9d30ef..7ec997ad5c8f 100644 --- a/physics/archimedes_principle.py +++ b/physics/archimedes_principle.py @@ -14,7 +14,9 @@ g = 9.80665 -def archimedes_principle(fluid_density: float, volume: float, gravity: float = g) -> float: +def archimedes_principle( + fluid_density: float, volume: float, gravity: float = g +) -> float: """ Args: fluid_density: density of fluid (kg/m^3) From 7540a77dbe5b490cebf58258f0bb55f48b6b9792 Mon Sep 17 00:00:00 2001 From: DIvkov575 Date: Thu, 13 Oct 2022 23:00:14 -0400 Subject: [PATCH 3/6] reformated --- physics/archimedes_principle.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/physics/archimedes_principle.py b/physics/archimedes_principle.py index 2b4e3b9d30ef..7ec997ad5c8f 100644 --- a/physics/archimedes_principle.py +++ b/physics/archimedes_principle.py @@ -14,7 +14,9 @@ g = 9.80665 -def archimedes_principle(fluid_density: float, volume: float, gravity: float = g) -> float: +def archimedes_principle( + fluid_density: float, volume: float, gravity: float = g +) -> float: """ Args: fluid_density: density of fluid (kg/m^3) From 235dc753efbf3a3c4202cceb696f53b9840821f0 Mon Sep 17 00:00:00 2001 From: DIvkov575 Date: Thu, 13 Oct 2022 23:14:22 -0400 Subject: [PATCH 4/6] reformatted archimedes principles --- physics/archimedes_principle.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/physics/archimedes_principle.py b/physics/archimedes_principle.py index 7ec997ad5c8f..6ecfc65e7461 100644 --- a/physics/archimedes_principle.py +++ b/physics/archimedes_principle.py @@ -21,7 +21,8 @@ def archimedes_principle( Args: fluid_density: density of fluid (kg/m^3) volume: volume of object / liquid being displaced by object - gravity: Acceleration from gravity. Gravitational force on system. Default is Earth Gravity + gravity: Acceleration from gravity. Gravitational force on system, + Default is Earth Gravity returns: buoyant force on object in Newtons From e0a7cf8d500886749375667b65aa5681a37aae45 Mon Sep 17 00:00:00 2001 From: DIvkov575 Date: Fri, 14 Oct 2022 21:58:48 -0400 Subject: [PATCH 5/6] added bernoullis_equations.py --- physics/bernoullis_equation.py | 137 +++++++++++++++++++++++++++++++++ 1 file changed, 137 insertions(+) create mode 100644 physics/bernoullis_equation.py diff --git a/physics/bernoullis_equation.py b/physics/bernoullis_equation.py new file mode 100644 index 000000000000..bfdb0e5bf5e1 --- /dev/null +++ b/physics/bernoullis_equation.py @@ -0,0 +1,137 @@ +""" + + +Source: +- https://courses.lumenlearning.com/suny-osuniversityphysics/chapter/14-6-bernoullis-equation/ +""" + +# Acceleration Constant on Earth (unit m/s^2) +g = 9.80665 + + +def bernoulli_static_equation( + fluid_density: float, h: float, gravity: float = g, initial_pressure: float = 0 +) -> float: + """ + Bernoulli's equation for static fluids. + Calculate pressure from static fluid at given depth. + + Pascals law, "a pressure change at any point in a confined incompressible fluid, + is transmitted throughout the fluid such that the same change occurs everywhere" + According to Pascals law, the pressure will be the same at a given depth, + regardless of whether this is a submerged object or a force on container wall. + + This function models: + P (pressure) = h (depth) * ρ (density) * g (gravity) + P = hρg + + In addition to this equation being used independently to calculate pressure at a depth, + It is a component of bernoulli's principle. + + + Args: + fluid_density: Density of fluid within which pressure is being calculated + h: depth at which pressure is being calculated. + gravity: Acceleration from gravity. Default is Earth Gravity + initial_pressure: represents any external pressure acting on system (Pa) + returns: + Pressure at given depth in Pa (Pascals) + + + >>> bernoulli_static_equation(fluid_density=997, h=5) + 48886.15025 + + >>> bernoulli_static_equation(fluid_density=997, h=5, gravity=9.8) + 48853.0 + + >>> bernoulli_static_equation(fluid_density=997, h=5, gravity=9.8, initial_pressure=3.7) + 48856.7 + """ + + if fluid_density <= 0: + raise ValueError("Impossible fluid density") + if h < 0: + raise ValueError("Impossible depth") + if gravity <= 0: + raise ValueError("Impossible Gravity") + + return fluid_density * h * gravity + initial_pressure + + +def bernoulli_fluid_equation( + fluid_density: float, fluid_velocity: float, initial_pressure: float = 0 +) -> float: + """ + Calculates pressure/fluids-kinetic-energy - work of a dynamic non-compressible fluid through a tube + + Bernoulli's Equation: + W = P1 + 0.5*ρ*v^2 + + Args: + fluid_density: Density of fluid within which pressure is being calculated + fluid_velocity: Velocity at which the fluid is travelling at + initial_pressure: represents any external pressure acting on system (Pa) + Returns: + Pressure of system in Pa (Pascals) + + >>> bernoulli_fluid_equation(fluid_density=997, fluid_velocity=0.7) + 244.26499999999996 + >>> bernoulli_fluid_equation(fluid_density=997, fluid_velocity=0.7, initial_pressure=2) + 246.26499999999996 + """ + + if fluid_density <= 0: + raise ValueError("Impossible fluid density") + + return 0.5 * fluid_density * fluid_velocity ** 2 + initial_pressure + + +def bernoulli_full_equation( + fluid_density: float, + fluid_velocity: float = 0, + h: float = 0, + gravity: float = g, + initial_pressure: float = 0, +) -> float: + """ + Bernoulli's equation represents the relation of (Pressure, KE, and GPE) of a non-compressible, + frictionless fluid within a container. + + P1 + 0.5*ρ*v1^2 + ρgh1 = P2 + 0.5*ρ*v2^2 + ρgh2 + or + P + 0.5*ρ*v^2 + ρgh = constant + + Equation is often rearranged to solve for different variables + + Args: + fluid_density: Density of fluid within which pressure is being calculated + fluid_velocity: Velocity at which the fluid is travelling at + h: height difference between two pressures being compared + gravity: Acceleration from gravity. Default is Earth Gravity + initial_pressure: represents any external pressure acting on system (Pa), Default = 0 + Returns: + Pressure of system in Pa (Pascals) + + + >>> bernoulli_full_equation(fluid_density=997, fluid_velocity=0.2, h=5, gravity=9.8, initial_pressure=7) + 48879.94 + >>> bernoulli_full_equation(fluid_density=997, fluid_velocity=0.2, h=5, gravity=9.8) + 48872.94 + >>> bernoulli_full_equation(fluid_density=997, fluid_velocity=0.2, h=5) + 48906.09025 + >>> bernoulli_full_equation(fluid_density=997, h=9) + 87995.07045 + """ + + return ( + bernoulli_fluid_equation(fluid_density=fluid_density, fluid_velocity=fluid_velocity) + + bernoulli_static_equation(fluid_density=fluid_density, h=h, gravity=gravity) + + initial_pressure + ) + + +if __name__ == '__main__': + import doctest + + # run doctest + doctest.testmod() From a2c0e987ab78c0a7da96a9fc3fe0ac5abc9d1b53 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 15 Oct 2022 02:01:18 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- physics/bernoullis_equation.py | 94 +++++++++++++++++----------------- 1 file changed, 48 insertions(+), 46 deletions(-) diff --git a/physics/bernoullis_equation.py b/physics/bernoullis_equation.py index bfdb0e5bf5e1..34a98e36163d 100644 --- a/physics/bernoullis_equation.py +++ b/physics/bernoullis_equation.py @@ -62,28 +62,28 @@ def bernoulli_fluid_equation( fluid_density: float, fluid_velocity: float, initial_pressure: float = 0 ) -> float: """ - Calculates pressure/fluids-kinetic-energy - work of a dynamic non-compressible fluid through a tube - - Bernoulli's Equation: - W = P1 + 0.5*ρ*v^2 - - Args: - fluid_density: Density of fluid within which pressure is being calculated - fluid_velocity: Velocity at which the fluid is travelling at - initial_pressure: represents any external pressure acting on system (Pa) - Returns: - Pressure of system in Pa (Pascals) - - >>> bernoulli_fluid_equation(fluid_density=997, fluid_velocity=0.7) - 244.26499999999996 - >>> bernoulli_fluid_equation(fluid_density=997, fluid_velocity=0.7, initial_pressure=2) - 246.26499999999996 + Calculates pressure/fluids-kinetic-energy - work of a dynamic non-compressible fluid through a tube + + Bernoulli's Equation: + W = P1 + 0.5*ρ*v^2 + + Args: + fluid_density: Density of fluid within which pressure is being calculated + fluid_velocity: Velocity at which the fluid is travelling at + initial_pressure: represents any external pressure acting on system (Pa) + Returns: + Pressure of system in Pa (Pascals) + + >>> bernoulli_fluid_equation(fluid_density=997, fluid_velocity=0.7) + 244.26499999999996 + >>> bernoulli_fluid_equation(fluid_density=997, fluid_velocity=0.7, initial_pressure=2) + 246.26499999999996 """ if fluid_density <= 0: raise ValueError("Impossible fluid density") - return 0.5 * fluid_density * fluid_velocity ** 2 + initial_pressure + return 0.5 * fluid_density * fluid_velocity**2 + initial_pressure def bernoulli_full_equation( @@ -94,43 +94,45 @@ def bernoulli_full_equation( initial_pressure: float = 0, ) -> float: """ - Bernoulli's equation represents the relation of (Pressure, KE, and GPE) of a non-compressible, - frictionless fluid within a container. - - P1 + 0.5*ρ*v1^2 + ρgh1 = P2 + 0.5*ρ*v2^2 + ρgh2 - or - P + 0.5*ρ*v^2 + ρgh = constant - - Equation is often rearranged to solve for different variables - - Args: - fluid_density: Density of fluid within which pressure is being calculated - fluid_velocity: Velocity at which the fluid is travelling at - h: height difference between two pressures being compared - gravity: Acceleration from gravity. Default is Earth Gravity - initial_pressure: represents any external pressure acting on system (Pa), Default = 0 - Returns: - Pressure of system in Pa (Pascals) - - - >>> bernoulli_full_equation(fluid_density=997, fluid_velocity=0.2, h=5, gravity=9.8, initial_pressure=7) - 48879.94 - >>> bernoulli_full_equation(fluid_density=997, fluid_velocity=0.2, h=5, gravity=9.8) - 48872.94 - >>> bernoulli_full_equation(fluid_density=997, fluid_velocity=0.2, h=5) - 48906.09025 - >>> bernoulli_full_equation(fluid_density=997, h=9) - 87995.07045 + Bernoulli's equation represents the relation of (Pressure, KE, and GPE) of a non-compressible, + frictionless fluid within a container. + + P1 + 0.5*ρ*v1^2 + ρgh1 = P2 + 0.5*ρ*v2^2 + ρgh2 + or + P + 0.5*ρ*v^2 + ρgh = constant + + Equation is often rearranged to solve for different variables + + Args: + fluid_density: Density of fluid within which pressure is being calculated + fluid_velocity: Velocity at which the fluid is travelling at + h: height difference between two pressures being compared + gravity: Acceleration from gravity. Default is Earth Gravity + initial_pressure: represents any external pressure acting on system (Pa), Default = 0 + Returns: + Pressure of system in Pa (Pascals) + + + >>> bernoulli_full_equation(fluid_density=997, fluid_velocity=0.2, h=5, gravity=9.8, initial_pressure=7) + 48879.94 + >>> bernoulli_full_equation(fluid_density=997, fluid_velocity=0.2, h=5, gravity=9.8) + 48872.94 + >>> bernoulli_full_equation(fluid_density=997, fluid_velocity=0.2, h=5) + 48906.09025 + >>> bernoulli_full_equation(fluid_density=997, h=9) + 87995.07045 """ return ( - bernoulli_fluid_equation(fluid_density=fluid_density, fluid_velocity=fluid_velocity) + bernoulli_fluid_equation( + fluid_density=fluid_density, fluid_velocity=fluid_velocity + ) + bernoulli_static_equation(fluid_density=fluid_density, h=h, gravity=gravity) + initial_pressure ) -if __name__ == '__main__': +if __name__ == "__main__": import doctest # run doctest