Skip to content

Commit 21d3acf

Browse files
authored
Merge pull request #1 from Glebfra/add-van-der-waals-equation
Added Van-der-Waals equation of state for gasses
2 parents 14ca726 + 66aac2c commit 21d3acf

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

physics/vander_vaals_gas_law.py

+190
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
"""
2+
The van der Waals equation, named for its originator,
3+
the Dutch physicist Johannes Diderik van der Waals,
4+
is an equation of state that extends the ideal gas
5+
law to include the non-zero size
6+
of gas molecules and the interactions between them
7+
(both of which depend on the specific substance).
8+
9+
As a result the equation is able to model the phase
10+
change from liquid to gas, and vice versa.
11+
It also produces simple analytic expressions for the
12+
properties of real substances that shed light on their
13+
behavior.
14+
15+
( Description was taken from https://en.wikipedia.org/wiki/Van_der_Waals_equation )
16+
17+
---------------------
18+
| (p+a/V^2)(V-bv)=vRT |
19+
---------------------
20+
! p - Pressure (Pa)
21+
! V - Volume (m^3)
22+
! v - Amount of gas (mol)
23+
! R - Universal gas constant
24+
! T Absolute temperature (K)
25+
! a, b - Parameters
26+
"""
27+
28+
R = 8.314462618
29+
30+
# Taken from https://ru.wikipedia.org/wiki/Уравнение_Ван-дер-Ваальса
31+
CONSTANTS = {
32+
"nitrogen": {"a": 0.1370, "b": 38.7e-6},
33+
"ammonia": {"a": 0.4225, "b": 37.1e-6},
34+
"argon": {"a": 0.1355, "b": 32.0e-6},
35+
"oxygen": {"a": 0.1382, "b": 31.9e-6},
36+
}
37+
38+
39+
def system_pressure(
40+
quantity: float, temperature: float, volume: float, a: float, b: float
41+
) -> float:
42+
"""
43+
Gets the system pressure from other 2 parameters
44+
---------------------
45+
| p=(vRT)/(V-bv)-a/V^2 |
46+
---------------------
47+
48+
>>> system_pressure(1, 300, 1, 0.1382, 31.9e-6)
49+
2494.2801573455995
50+
>>> system_pressure(1, 100, 1, 0.1382, 31.9e-6)
51+
831.3345857818664
52+
>>> system_pressure(1, 300, -1, 0.1382, 31.9e-6)
53+
Traceback (most recent call last):
54+
...
55+
ValueError: Please provide the positive values
56+
"""
57+
58+
if temperature < 0 or volume < 0:
59+
raise ValueError("Please provide the positive values")
60+
return (quantity * R * temperature) / (volume - quantity * b) - a / (volume**2)
61+
62+
63+
def system_temperature(
64+
quantity: float, pressure: float, volume: float, a: float, b: float
65+
) -> float:
66+
"""
67+
Gets the system temperature from other 2 parameters
68+
---------------------
69+
| T = 1/(vR)*(p+a/V^2)(V-bv) |
70+
---------------------
71+
72+
>>> system_temperature(1, 300, 1, 0.1382, 31.9e-6)
73+
36.09717661628195
74+
>>> system_temperature(1, 100, 1, 0.1382, 31.9e-6)
75+
12.04347294491859
76+
>>> system_temperature(1, 300, -1, 0.1382, 31.9e-6)
77+
Traceback (most recent call last):
78+
...
79+
ValueError: Please provide the positive values
80+
"""
81+
82+
if pressure < 0 or volume < 0:
83+
raise ValueError("Please provide the positive values")
84+
return 1 / (quantity * R) * (pressure + a / volume**2) * (volume - quantity * b)
85+
86+
87+
def critical_temperature(a: float, b: float) -> float:
88+
"""
89+
Calculate the critical temperature from two parameters for each gas
90+
---------------------
91+
| T_c=8a/(27bR) |
92+
---------------------
93+
94+
>>> critical_temperature(0.1382, 31.9e-6)
95+
154.3865270378366
96+
"""
97+
98+
return 8 * a / (27 * b * R)
99+
100+
101+
def critical_volume(b: float) -> float:
102+
"""
103+
Calculate the critical volume from one parameter for each gas
104+
---------------------
105+
| V_c=3b |
106+
---------------------
107+
108+
>>> critical_volume(31.9e-6)
109+
9.570000000000001e-05
110+
"""
111+
112+
return 3 * b
113+
114+
115+
def critical_pressure(a: float, b: float) -> float:
116+
"""
117+
Calculate the critical pressure from two parameters for each gas
118+
---------------------
119+
| p_c=a/(27b^2) |
120+
---------------------
121+
122+
>>> critical_pressure(0.1382, 31.9e-6)
123+
5029941.253052267
124+
"""
125+
126+
return a / (27 * b**2)
127+
128+
129+
def critical_coefficient(a: float, b: float) -> float:
130+
"""
131+
Calculate the critical coefficient from two parameters for each gas
132+
---------------------
133+
| k_c=(R*T_c)/(p_c*V_c) |
134+
---------------------
135+
136+
>>> critical_coefficient(0.1382, 31.9e-6)
137+
2.6666666666666665
138+
"""
139+
140+
return (
141+
R * critical_temperature(a, b) / (critical_pressure(a, b) * critical_volume(b))
142+
)
143+
144+
145+
def given_volume(volume: float, b: float) -> float:
146+
"""
147+
Calculate the given volume from one parameter for each gas and volume
148+
---------------------
149+
| φ = V / V_c |
150+
---------------------
151+
152+
>>> given_volume(1, 31.9e-6)
153+
10449.32079414838
154+
"""
155+
156+
return volume / critical_volume(b)
157+
158+
159+
def given_pressure(pressure: float, a: float, b: float) -> float:
160+
"""
161+
Calculate the given pressure from two parameters for each gas and pressure
162+
---------------------
163+
| π = p / p_c |
164+
---------------------
165+
166+
>>> given_pressure(1, 0.1382, 31.9e-6)
167+
1.9880947901591899e-07
168+
"""
169+
170+
return pressure / critical_pressure(a, b)
171+
172+
173+
def given_temperature(temperature: float, a: float, b: float) -> float:
174+
"""
175+
Calculate the given temperature from two parameters for each gas and temperature
176+
---------------------
177+
| τ = T / T_c |
178+
---------------------
179+
180+
>>> given_temperature(1, 0.1382, 31.9e-6)
181+
0.006477249143346057
182+
"""
183+
184+
return temperature / critical_temperature(a, b)
185+
186+
187+
if __name__ == "__main__":
188+
from doctest import testmod
189+
190+
testmod()

0 commit comments

Comments
 (0)