Python/physics/archimedes_principle_of_buoyant_force.py
pre-commit-ci[bot] bc8df6de31
[pre-commit.ci] pre-commit autoupdate (#11322)
* [pre-commit.ci] pre-commit autoupdate

updates:
- [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.2.2...v0.3.2)
- [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0)

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
2024-03-13 07:52:41 +01:00

63 lines
1.9 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Calculate the buoyant force of any body completely or partially submerged in a static
fluid. This principle was discovered by the Greek mathematician Archimedes.
Equation for calculating buoyant force:
Fb = ρ * V * g
https://en.wikipedia.org/wiki/Archimedes%27_principle
"""
# Acceleration Constant on Earth (unit m/s^2)
g = 9.80665 # Also available in scipy.constants.g
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 the object (m^3)
gravity: Acceleration from gravity. Gravitational force on the system,
The default is Earth Gravity
returns:
the buoyant force on an object in Newtons
>>> archimedes_principle(fluid_density=500, volume=4, gravity=9.8)
19600.0
>>> archimedes_principle(fluid_density=997, volume=0.5, gravity=9.8)
4885.3
>>> archimedes_principle(fluid_density=997, volume=0.7)
6844.061035
>>> archimedes_principle(fluid_density=997, volume=-0.7)
Traceback (most recent call last):
...
ValueError: Impossible object volume
>>> archimedes_principle(fluid_density=0, volume=0.7)
Traceback (most recent call last):
...
ValueError: Impossible fluid density
>>> archimedes_principle(fluid_density=997, volume=0.7, gravity=0)
0.0
>>> archimedes_principle(fluid_density=997, volume=0.7, gravity=-9.8)
Traceback (most recent call last):
...
ValueError: Impossible gravity
"""
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
doctest.testmod()