forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ideal_gas_law.py
93 lines (78 loc) · 2.98 KB
/
ideal_gas_law.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"""
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
def temperature_of_gas_system(moles: float, volume: float, pressure: float) -> float:
"""
>>> temperature_of_gas_system(2, 100, 5)
30.068090996146232
>>> temperature_of_gas_system(11, 5009, 1000)
54767.66101807144
>>> temperature_of_gas_system(3, -0.46, 23.5)
Traceback (most recent call last):
...
ValueError: Invalid inputs. Enter positive value.
"""
if moles < 0 or volume < 0 or pressure < 0:
raise ValueError("Invalid inputs. Enter positive value.")
return pressure * volume / (moles * UNIVERSAL_GAS_CONSTANT)
def moles_of_gas_system(kelvin: float, volume: float, pressure: float) -> float:
"""
>>> moles_of_gas_system(100, 5, 10)
0.06013618199229246
>>> moles_of_gas_system(110, 5009, 1000)
5476.766101807144
>>> moles_of_gas_system(3, -0.46, 23.5)
Traceback (most recent call last):
...
ValueError: Invalid inputs. Enter positive value.
"""
if kelvin < 0 or volume < 0 or pressure < 0:
raise ValueError("Invalid inputs. Enter positive value.")
return pressure * volume / (kelvin * UNIVERSAL_GAS_CONSTANT)
if __name__ == "__main__":
from doctest import testmod
testmod()