forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeeds_of_gas_molecules.py
106 lines (82 loc) · 3.84 KB
/
speeds_of_gas_molecules.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
94
95
96
97
98
99
100
101
102
103
104
105
106
r"""
The root-mean-square, average and most probable speeds of gas molecules are
derived from the Maxwell-Boltzmann distribution. The Maxwell-Boltzmann
distribution is a probability distribution that describes the distribution of
speeds of particles in an ideal gas.
The distribution is given by the following equation:
.. math:: f(v) = \left(\frac{M}{2 \pi RT}\right)^{\frac{3}{2}} \cdot 4 \pi v^2
\cdot e^{-\frac{Mv^2}{2RT}}
where:
* :math:`f(v)` is the fraction of molecules with a speed :math:`v`
* :math:`M` is the molar mass of the gas in kg/mol
* :math:`R` is the gas constant
* :math:`T` is the absolute temperature
More information about the Maxwell-Boltzmann distribution can be found here:
https://en.wikipedia.org/wiki/Maxwell%E2%80%93Boltzmann_distribution
The average speed can be calculated by integrating the Maxwell-Boltzmann distribution
from 0 to infinity and dividing by the total number of molecules. The result is:
.. math:: v_{avg} = \sqrt{\frac{8RT}{\pi M}}
The most probable speed is the speed at which the Maxwell-Boltzmann distribution
is at its maximum. This can be found by differentiating the Maxwell-Boltzmann
distribution with respect to :math:`v` and setting the result equal to zero.
The result is:
.. math:: v_{mp} = \sqrt{\frac{2RT}{M}}
The root-mean-square speed is another measure of the average speed
of the molecules in a gas. It is calculated by taking the square root
of the average of the squares of the speeds of the molecules. The result is:
.. math:: v_{rms} = \sqrt{\frac{3RT}{M}}
Here we have defined functions to calculate the average and
most probable speeds of molecules in a gas given the
temperature and molar mass of the gas.
"""
# import the constants R and pi from the scipy.constants library
from scipy.constants import R, pi
def avg_speed_of_molecule(temperature: float, molar_mass: float) -> float:
"""
Takes the temperature (in K) and molar mass (in kg/mol) of a gas
and returns the average speed of a molecule in the gas (in m/s).
Examples:
>>> avg_speed_of_molecule(273, 0.028) # nitrogen at 273 K
454.3488755020387
>>> avg_speed_of_molecule(300, 0.032) # oxygen at 300 K
445.52572733919885
>>> avg_speed_of_molecule(-273, 0.028) # invalid temperature
Traceback (most recent call last):
...
Exception: Absolute temperature cannot be less than 0 K
>>> avg_speed_of_molecule(273, 0) # invalid molar mass
Traceback (most recent call last):
...
Exception: Molar mass should be greater than 0 kg/mol
"""
if temperature < 0:
raise Exception("Absolute temperature cannot be less than 0 K")
if molar_mass <= 0:
raise Exception("Molar mass should be greater than 0 kg/mol")
return (8 * R * temperature / (pi * molar_mass)) ** 0.5
def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float:
"""
Takes the temperature (in K) and molar mass (in kg/mol) of a gas
and returns the most probable speed of a molecule in the gas (in m/s).
Examples:
>>> mps_speed_of_molecule(273, 0.028) # nitrogen at 273 K
402.65620701908966
>>> mps_speed_of_molecule(300, 0.032) # oxygen at 300 K
394.836895549922
>>> mps_speed_of_molecule(-273, 0.028) # invalid temperature
Traceback (most recent call last):
...
Exception: Absolute temperature cannot be less than 0 K
>>> mps_speed_of_molecule(273, 0) # invalid molar mass
Traceback (most recent call last):
...
Exception: Molar mass should be greater than 0 kg/mol
"""
if temperature < 0:
raise Exception("Absolute temperature cannot be less than 0 K")
if molar_mass <= 0:
raise Exception("Molar mass should be greater than 0 kg/mol")
return (2 * R * temperature / molar_mass) ** 0.5
if __name__ == "__main__":
import doctest
doctest.testmod()