From 00cbeb8ce48acf798a65a15c0d0c563e3b7b75f7 Mon Sep 17 00:00:00 2001 From: kavienanj Date: Sat, 1 Oct 2022 13:32:17 +0530 Subject: [PATCH 1/8] add physics ideal gas law --- physics/ideal_gas_law.py | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 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 000000000000..a3d6b3437b5d --- /dev/null +++ b/physics/ideal_gas_law.py @@ -0,0 +1,66 @@ +""" +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 + +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 + """ + if moles < 0 or kelvin < 0 or volume < 0: + raise Exception('Invalid inputs. Enter positive value.') + pressure: float = moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume + return pressure + +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 + """ + if moles < 0 or kelvin < 0 or pressure < 0: + raise Exception('Invalid inputs. Enter positive value.') + volume: float = moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure + return volume + +if __name__ == "__main__": + from doctest import testmod + + testmod() + + # Example 1 + example_1_volume = 5 + example_1_moles = 2 + example_1_kelvin = 100 + print(f"Pressure(P) of a gas system with V = {example_1_volume} [m^3], n = {example_1_moles} [mol], T = {example_1_kelvin} [K]:") + print(f"{pressure_of_gas_system(example_1_moles, example_1_kelvin, example_1_volume)} [Pa]") + + print() + + # Example 2 + example_2_pressure = 0.004 + example_2_moles = 0.5 + example_2_kelvin = 273 + print(f"Volume(V) of a gas system with P = {example_2_pressure} [Pa], n = {example_2_moles} [mol], T = {example_2_kelvin} [K]:") + print(f"{volume_of_gas_system(example_2_moles, example_2_kelvin, example_2_pressure)} [m^3]") From 049ef9817d0acf35eb1189d799b50ab0c7a74f7b Mon Sep 17 00:00:00 2001 From: kavienanj Date: Sun, 2 Oct 2022 08:23:19 +0530 Subject: [PATCH 2/8] run pre commit --- physics/ideal_gas_law.py | 41 +++++++++++++++++++++++++++------------- 1 file changed, 28 insertions(+), 13 deletions(-) diff --git a/physics/ideal_gas_law.py b/physics/ideal_gas_law.py index a3d6b3437b5d..4dc2922ebfae 100644 --- a/physics/ideal_gas_law.py +++ b/physics/ideal_gas_law.py @@ -1,10 +1,10 @@ """ -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 +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 | @@ -20,6 +20,7 @@ UNIVERSAL_GAS_CONSTANT = 8.314462 + def pressure_of_gas_system(moles: float, kelvin: float, volume: float) -> float: """ >>> pressure_of_gas_system(2, 100, 5) @@ -28,10 +29,11 @@ def pressure_of_gas_system(moles: float, kelvin: float, volume: float) -> float: 283731.01575 """ if moles < 0 or kelvin < 0 or volume < 0: - raise Exception('Invalid inputs. Enter positive value.') + raise Exception("Invalid inputs. Enter positive value.") pressure: float = moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume return pressure + def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: """ >>> volume_of_gas_system(2, 100, 5) @@ -40,21 +42,28 @@ def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: 283731.01575 """ if moles < 0 or kelvin < 0 or pressure < 0: - raise Exception('Invalid inputs. Enter positive value.') + raise Exception("Invalid inputs. Enter positive value.") volume: float = moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure return volume + if __name__ == "__main__": from doctest import testmod testmod() - + # Example 1 example_1_volume = 5 example_1_moles = 2 example_1_kelvin = 100 - print(f"Pressure(P) of a gas system with V = {example_1_volume} [m^3], n = {example_1_moles} [mol], T = {example_1_kelvin} [K]:") - print(f"{pressure_of_gas_system(example_1_moles, example_1_kelvin, example_1_volume)} [Pa]") + example_1_pressure = pressure_of_gas_system( + example_1_moles, example_1_kelvin, example_1_volume + ) + print( + f"""Pressure(P) of a gas system with V = {example_1_volume} [m^3], + n = {example_1_moles} [mol], T = {example_1_kelvin} [K]:""" + ) + print(f"{example_1_pressure} [Pa]") print() @@ -62,5 +71,11 @@ def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: example_2_pressure = 0.004 example_2_moles = 0.5 example_2_kelvin = 273 - print(f"Volume(V) of a gas system with P = {example_2_pressure} [Pa], n = {example_2_moles} [mol], T = {example_2_kelvin} [K]:") - print(f"{volume_of_gas_system(example_2_moles, example_2_kelvin, example_2_pressure)} [m^3]") + examle_2_volume = volume_of_gas_system( + example_2_moles, example_2_kelvin, example_2_pressure + ) + print( + f"""Volume(V) of a gas system with P = {example_2_pressure} [Pa], + n = {example_2_moles} [mol], T = {example_2_kelvin} [K]:""" + ) + print(f"{examle_2_volume} [m^3]") From 63b2f12f7e07ffd1ece7d482c8e3182a40ad1a2b Mon Sep 17 00:00:00 2001 From: Kavienan J <45987371+kavienanj@users.noreply.github.com> Date: Sun, 2 Oct 2022 17:42:02 +0530 Subject: [PATCH 3/8] Update physics/ideal_gas_law.py Suggestion #1 Co-authored-by: Caeden --- physics/ideal_gas_law.py | 1 + 1 file changed, 1 insertion(+) diff --git a/physics/ideal_gas_law.py b/physics/ideal_gas_law.py index 4dc2922ebfae..c4d127697172 100644 --- a/physics/ideal_gas_law.py +++ b/physics/ideal_gas_law.py @@ -79,3 +79,4 @@ def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: n = {example_2_moles} [mol], T = {example_2_kelvin} [K]:""" ) print(f"{examle_2_volume} [m^3]") + From 6b9a10d00789188fc0b08cafd4f8edd09a73686a Mon Sep 17 00:00:00 2001 From: Kavienan J <45987371+kavienanj@users.noreply.github.com> Date: Sun, 2 Oct 2022 17:42:18 +0530 Subject: [PATCH 4/8] Update physics/ideal_gas_law.py Suggestion #2 Co-authored-by: Caeden --- physics/ideal_gas_law.py | 1 - 1 file changed, 1 deletion(-) diff --git a/physics/ideal_gas_law.py b/physics/ideal_gas_law.py index c4d127697172..7731a06b66eb 100644 --- a/physics/ideal_gas_law.py +++ b/physics/ideal_gas_law.py @@ -33,7 +33,6 @@ def pressure_of_gas_system(moles: float, kelvin: float, volume: float) -> float: pressure: float = moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume return pressure - def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: """ >>> volume_of_gas_system(2, 100, 5) From 3bff0df89645d1a79ce75547c11459d40319245b Mon Sep 17 00:00:00 2001 From: kavienanj Date: Sun, 2 Oct 2022 17:51:16 +0530 Subject: [PATCH 5/8] run pre commit --- physics/ideal_gas_law.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/ideal_gas_law.py b/physics/ideal_gas_law.py index 7731a06b66eb..4dc2922ebfae 100644 --- a/physics/ideal_gas_law.py +++ b/physics/ideal_gas_law.py @@ -33,6 +33,7 @@ def pressure_of_gas_system(moles: float, kelvin: float, volume: float) -> float: pressure: float = moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume return pressure + def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: """ >>> volume_of_gas_system(2, 100, 5) @@ -78,4 +79,3 @@ def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: n = {example_2_moles} [mol], T = {example_2_kelvin} [K]:""" ) print(f"{examle_2_volume} [m^3]") - From c56aa57e03747183cf49ff556278c232f02d46ec Mon Sep 17 00:00:00 2001 From: Kavienan J <45987371+kavienanj@users.noreply.github.com> Date: Mon, 17 Oct 2022 10:14:57 +0530 Subject: [PATCH 6/8] Update volume return line sugesstion Co-authored-by: Caeden Perelli-Harris --- physics/ideal_gas_law.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/physics/ideal_gas_law.py b/physics/ideal_gas_law.py index 4dc2922ebfae..288e23cac4b9 100644 --- a/physics/ideal_gas_law.py +++ b/physics/ideal_gas_law.py @@ -44,7 +44,7 @@ def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: if moles < 0 or kelvin < 0 or pressure < 0: raise Exception("Invalid inputs. Enter positive value.") volume: float = moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure - return volume + return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure if __name__ == "__main__": From e59078c0f119f78e695ea250854febeaea5c6fa4 Mon Sep 17 00:00:00 2001 From: Kavienan J <45987371+kavienanj@users.noreply.github.com> Date: Mon, 17 Oct 2022 10:30:52 +0530 Subject: [PATCH 7/8] Add suggestions --- physics/ideal_gas_law.py | 34 ++-------------------------------- 1 file changed, 2 insertions(+), 32 deletions(-) diff --git a/physics/ideal_gas_law.py b/physics/ideal_gas_law.py index 288e23cac4b9..476dedb50627 100644 --- a/physics/ideal_gas_law.py +++ b/physics/ideal_gas_law.py @@ -18,7 +18,7 @@ (Description adapted from https://en.wikipedia.org/wiki/Ideal_gas_law ) """ -UNIVERSAL_GAS_CONSTANT = 8.314462 +UNIVERSAL_GAS_CONSTANT = 8.314462 # Unit - J mol-1 K-1 def pressure_of_gas_system(moles: float, kelvin: float, volume: float) -> float: @@ -30,8 +30,7 @@ def pressure_of_gas_system(moles: float, kelvin: float, volume: float) -> float: """ if moles < 0 or kelvin < 0 or volume < 0: raise Exception("Invalid inputs. Enter positive value.") - pressure: float = moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume - return pressure + return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: @@ -43,7 +42,6 @@ def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: """ if moles < 0 or kelvin < 0 or pressure < 0: raise Exception("Invalid inputs. Enter positive value.") - volume: float = moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure @@ -51,31 +49,3 @@ def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: from doctest import testmod testmod() - - # Example 1 - example_1_volume = 5 - example_1_moles = 2 - example_1_kelvin = 100 - example_1_pressure = pressure_of_gas_system( - example_1_moles, example_1_kelvin, example_1_volume - ) - print( - f"""Pressure(P) of a gas system with V = {example_1_volume} [m^3], - n = {example_1_moles} [mol], T = {example_1_kelvin} [K]:""" - ) - print(f"{example_1_pressure} [Pa]") - - print() - - # Example 2 - example_2_pressure = 0.004 - example_2_moles = 0.5 - example_2_kelvin = 273 - examle_2_volume = volume_of_gas_system( - example_2_moles, example_2_kelvin, example_2_pressure - ) - print( - f"""Volume(V) of a gas system with P = {example_2_pressure} [Pa], - n = {example_2_moles} [mol], T = {example_2_kelvin} [K]:""" - ) - print(f"{examle_2_volume} [m^3]") From 2bb34421b39ad399269db6813ba92e4d829e77cc Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Sun, 30 Oct 2022 11:21:32 +0100 Subject: [PATCH 8/8] Apply suggestions from code review Co-authored-by: Caeden Perelli-Harris --- physics/ideal_gas_law.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/physics/ideal_gas_law.py b/physics/ideal_gas_law.py index 476dedb50627..805da47b0079 100644 --- a/physics/ideal_gas_law.py +++ b/physics/ideal_gas_law.py @@ -27,9 +27,13 @@ def pressure_of_gas_system(moles: float, kelvin: float, volume: float) -> float: 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 Exception("Invalid inputs. Enter positive value.") + raise ValueError("Invalid inputs. Enter positive value.") return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume @@ -39,9 +43,13 @@ def volume_of_gas_system(moles: float, kelvin: float, pressure: float) -> float: 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 Exception("Invalid inputs. Enter positive value.") + raise ValueError("Invalid inputs. Enter positive value.") return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure