Compressibility Factor Calculation (Peng-Robinson)
Calculate the compressibility factor \(Z\) of a real gas using the Peng-Robinson cubic equation of state [1]. Critical properties (\(T_c\), \(P_c\), \(\omega\)) are extracted automatically from CoolProp — no need to hardcode them per compound.
The Peng-Robinson EoS is written in reduced form as:
with:
where \(\Omega = 0.07780\), \(\Psi = 0.45724\), \(\sigma = 1 + \sqrt{2}\) and \(\varepsilon = 1 - \sqrt{2}\).
Since the equation is implicit in \(Z\), it is solved by fixed-point iteration starting from \(Z_0 \approx 1\) (ideal gas) and recalculating until \(|Z_{n+1} - Z_n| < \text{tol}\).
import numpy as np
import CoolProp.CoolProp as CP
class Zfactor:
def __init__(self, component_name, T, P):
self.component_name = component_name
self.T = T
self.P = P
def calculation_z(self, tol = 1E-4, i_max = 100):
Tc = CP.PropsSI('Tcrit', self.component_name)
Pc = CP.PropsSI('Pcrit', self.component_name)
w = CP.PropsSI('acentric', self.component_name)
# Parameters of the Peng-Robinson Equation
Ω = 0.07780
Ψ = 0.45724
σ = 1 + np.sqrt(2)
ε = 1 - np.sqrt(2)
# ==============Functions==============
Tr = lambda T: T/Tc
Pr = lambda P: P/Pc
acent_w = lambda: 0.37464 + 1.5422*w - 0.26992*w**2
α = lambda T: (1 + (acent_w())*(1 - np.sqrt(Tr(T))))**2
β = lambda T, P: (Ω*Pr(P))/Tr(T)
q = lambda T: (Ψ*α(T))/(Ω*Tr(T))
# ==============Algorithm for calculating compressibility factor==============
z_previous = 0.9999
i = 0
while 1:
z = 1 + β(self.T, self.P) - q(self.T)*β(self.T, self.P)*((z_previous - β(self.T, self.P))/((z_previous + ε*β(self.T, self.P))*(z_previous + σ*β(self.T, self.P))))
if abs(z - z_previous) < tol or i >= i_max:
break
z_previous = z
i = i + 1
return round(z, 4)
Example with methane at \(T = 300\,\text{K}\) and \(P = 5\,\text{MPa}\):
z = Zfactor("Methane", 300, 5e6).calculation_z()
print(z) # -> 0.9018
Sweep across pressure for different temperatures:

How to read the graph:
- At high \(T\) (\(400\,\text{K}\), green) methane behaves almost as an ideal gas: \(Z\) remains close to \(1\) even at \(30\,\text{MPa}\).
- At low \(T\) (\(200\,\text{K}\), red, below \(T_c \approx 190.6\,\text{K}\)) the molecules attract each other: \(Z\) drops well below \(1\) before recovering at high pressure due to excluded volume.
- The curve crosses \(Z = 1\) near the Boyle temperature, where attractive and repulsive forces cancel exactly.
Why Peng-Robinson and not another EoS:
- Much better accuracy than Van der Waals or Redlich-Kwong for hydrocarbons and non-polar mixtures.
- The Soave correction in \(\alpha(T_r, \omega)\) incorporates acentric factor \(\omega\), capturing the thermal dependence well near the critical point.
- It is the standard EoS in the oil and gas industry — most process simulators (Aspen HYSYS, Pro/II, DWSIM) use it by default.
Reference
[1] J. M. Smith, H. C. Van Ness, M. M. Abbott, M. T. Swihart. Introduction to Chemical Engineering Thermodynamics (8th ed.). McGraw-Hill, 2018.
Installation:
pip install coolprop numpy