Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Van der Waals equation of state #11359

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
190 changes: 190 additions & 0 deletions physics/vander_vaals_gas_law.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
"""
The van der Waals equation, named for its originator,
the Dutch physicist Johannes Diderik van der Waals,
is an equation of state that extends the ideal gas
law to include the non-zero size
of gas molecules and the interactions between them
(both of which depend on the specific substance).

As a result the equation is able to model the phase
change from liquid to gas, and vice versa.
It also produces simple analytic expressions for the
properties of real substances that shed light on their
behavior.

( Description was taken from https://en.wikipedia.org/wiki/Van_der_Waals_equation )

---------------------
| (p+a/V^2)(V-bv)=vRT |
---------------------
! p - Pressure (Pa)
! V - Volume (m^3)
! v - Amount of gas (mol)
! R - Universal gas constant
! T Absolute temperature (K)
! a, b - Parameters
"""

R = 8.314462618

# Taken from https://ru.wikipedia.org/wiki/Уравнение_Ван-дер-Ваальса
CONSTANTS = {
"nitrogen": {"a": 0.1370, "b": 38.7e-6},
"ammonia": {"a": 0.4225, "b": 37.1e-6},
"argon": {"a": 0.1355, "b": 32.0e-6},
"oxygen": {"a": 0.1382, "b": 31.9e-6},
}


def system_pressure(
quantity: float, temperature: float, volume: float, a: float, b: float
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace a and b with self-documentating variable names.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when quantity is zero? When quantity is negative?

) -> float:
"""
Gets the system pressure from other 2 parameters
---------------------
| p=(vRT)/(V-bv)-a/V^2 |
---------------------

>>> system_pressure(1, 300, 1, 0.1382, 31.9e-6)
2494.2801573455995
>>> system_pressure(1, 100, 1, 0.1382, 31.9e-6)
831.3345857818664
>>> system_pressure(1, 300, -1, 0.1382, 31.9e-6)
Traceback (most recent call last):
...
ValueError: Please provide the positive values
"""

if temperature < 0 or volume < 0:
raise ValueError("Please provide the positive values")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
raise ValueError("Please provide the positive values")
raise ValueError("Please provide non-negative values")

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a test for both values being zero.

return (quantity * R * temperature) / (volume - quantity * b) - a / (volume**2)


def system_temperature(
quantity: float, pressure: float, volume: float, a: float, b: float
) -> float:
"""
Gets the system temperature from other 2 parameters
---------------------
| T = 1/(vR)*(p+a/V^2)(V-bv) |
---------------------

>>> system_temperature(1, 300, 1, 0.1382, 31.9e-6)
36.09717661628195
>>> system_temperature(1, 100, 1, 0.1382, 31.9e-6)
12.04347294491859
>>> system_temperature(1, 300, -1, 0.1382, 31.9e-6)
Traceback (most recent call last):
...
ValueError: Please provide the positive values
"""

if pressure < 0 or volume < 0:
raise ValueError("Please provide the positive values")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-negative

return 1 / (quantity * R) * (pressure + a / volume**2) * (volume - quantity * b)


def critical_temperature(a: float, b: float) -> float:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Single letter variable names went out of style in the 1970’s.

"""
Calculate the critical temperature from two parameters for each gas
---------------------
| T_c=8a/(27bR) |
---------------------

>>> critical_temperature(0.1382, 31.9e-6)
154.3865270378366
"""

return 8 * a / (27 * b * R)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please replace b & R with self-documentating names



def critical_volume(b: float) -> float:
"""
Calculate the critical volume from one parameter for each gas
---------------------
| V_c=3b |
---------------------

>>> critical_volume(31.9e-6)
9.570000000000001e-05
"""

return 3 * b


def critical_pressure(a: float, b: float) -> float:
"""
Calculate the critical pressure from two parameters for each gas
---------------------
| p_c=a/(27b^2) |
---------------------

>>> critical_pressure(0.1382, 31.9e-6)
5029941.253052267
"""

return a / (27 * b**2)


def critical_coefficient(a: float, b: float) -> float:
"""
Calculate the critical coefficient from two parameters for each gas
---------------------
| k_c=(R*T_c)/(p_c*V_c) |
---------------------

>>> critical_coefficient(0.1382, 31.9e-6)
2.6666666666666665
"""

return (
R * critical_temperature(a, b) / (critical_pressure(a, b) * critical_volume(b))
)


def given_volume(volume: float, b: float) -> float:
"""
Calculate the given volume from one parameter for each gas and volume
---------------------
| φ = V / V_c |
---------------------

>>> given_volume(1, 31.9e-6)
10449.32079414838
"""

return volume / critical_volume(b)


def given_pressure(pressure: float, a: float, b: float) -> float:
"""
Calculate the given pressure from two parameters for each gas and pressure
---------------------
| π = p / p_c |
---------------------

>>> given_pressure(1, 0.1382, 31.9e-6)
1.9880947901591899e-07
"""

return pressure / critical_pressure(a, b)


def given_temperature(temperature: float, a: float, b: float) -> float:
"""
Calculate the given temperature from two parameters for each gas and temperature
---------------------
| τ = T / T_c |
---------------------

>>> given_temperature(1, 0.1382, 31.9e-6)
0.006477249143346057
"""

return temperature / critical_temperature(a, b)


if __name__ == "__main__":
from doctest import testmod

testmod()