From c0b0b128b7ad4a5a75ed866bc7c114c3cf7a89ef Mon Sep 17 00:00:00 2001 From: Kavienan J <45987371+kavienanj@users.noreply.github.com> Date: Sun, 30 Oct 2022 15:56:46 +0530 Subject: [PATCH] Add Ideal Gas Law for physics (#6503) * add physics ideal gas law * run pre commit * Update physics/ideal_gas_law.py Suggestion #1 Co-authored-by: Caeden * Update physics/ideal_gas_law.py Suggestion #2 Co-authored-by: Caeden * run pre commit * Update volume return line sugesstion Co-authored-by: Caeden Perelli-Harris * Add suggestions * Apply suggestions from code review Co-authored-by: Caeden Perelli-Harris Co-authored-by: Caeden Co-authored-by: Christian Clauss --- physics/ideal_gas_law.py | 59 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 physics/ideal_gas_law.py diff --git a/physics/ideal_gas_law.py b/physics/ideal_gas_law.py new file mode 100644 index 000000000..805da47b0 --- /dev/null +++ b/physics/ideal_gas_law.py @@ -0,0 +1,59 @@ +""" +The ideal gas law, also called the general gas equation, is the +equation of state of a hypothetical ideal gas. It is a good approximation +of the behavior of many gases under many conditions, although it has +several limitations. It was first stated by Benoît Paul Émile Clapeyron +in 1834 as a combination of the empirical Boyle's law, Charles's law, +Avogadro's law, and Gay-Lussac's law.[1] The ideal gas law is often written +in an empirical form: + ------------ + | PV = nRT | + ------------ +P = Pressure (Pa) +V = Volume (m^3) +n = Amount of substance (mol) +R = Universal gas constant +T = Absolute temperature (Kelvin) + +(Description adapted from https://en.wikipedia.org/wiki/Ideal_gas_law ) +""" + +UNIVERSAL_GAS_CONSTANT = 8.314462 # Unit - J mol-1 K-1 + + +def pressure_of_gas_system(moles: float, kelvin: float, volume: float) -> float: + """ + >>> pressure_of_gas_system(2, 100, 5) + 332.57848 + >>> pressure_of_gas_system(0.5, 273, 0.004) + 283731.01575 + >>> pressure_of_gas_system(3, -0.46, 23.5) + Traceback (most recent call last): + ... + ValueError: Invalid inputs. Enter positive value. + """ + if moles < 0 or kelvin < 0 or volume < 0: + raise ValueError("Invalid inputs. Enter positive value.") + return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume + + +def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: + """ + >>> volume_of_gas_system(2, 100, 5) + 332.57848 + >>> volume_of_gas_system(0.5, 273, 0.004) + 283731.01575 + >>> volume_of_gas_system(3, -0.46, 23.5) + Traceback (most recent call last): + ... + ValueError: Invalid inputs. Enter positive value. + """ + if moles < 0 or kelvin < 0 or pressure < 0: + raise ValueError("Invalid inputs. Enter positive value.") + return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure + + +if __name__ == "__main__": + from doctest import testmod + + testmod()