RACHFORD_RICE
This function computes vapor fraction and phase compositions by solving the classical Rachford-Rice objective for specified overall mole fractions and K-values.
The equation is:
\sum_i \frac{z_i(K_i-1)}{1 + VF(K_i-1)} = 0
Optional derivative flags allow secant, Newton, or Halley-style updates.
Excel Usage
=RACHFORD_RICE(zs, ks, use_first_derivative, use_second_derivative, guess)
zs(list[list], required): Overall mole fractions as a selected range (-).ks(list[list], required): Equilibrium K-values as a selected range (-).use_first_derivative(bool, optional, default: false): Use first derivative in solve step (Newton-Raphson).use_second_derivative(bool, optional, default: false): Use second derivative in solve step (Halley method).guess(float, optional, default: null): Initial vapor-fraction guess (-).
Returns (list[list]): 2D array containing vapor fraction and phase compositions.
Example 1: Classical Rachford-Rice solve
Inputs:
| zs | ks | ||||
|---|---|---|---|---|---|
| 0.5 | 0.3 | 0.2 | 1.685 | 0.742 | 0.532 |
Excel formula:
=RACHFORD_RICE({0.5,0.3,0.2}, {1.685,0.742,0.532})
Expected output:
| Property | Value |
|---|---|
| VF | 0.69073 |
| xs | [0.33940869696634357, 0.3650560590371706, 0.2955352439964858] |
| ys | [0.5719036543882889, 0.27087159580558057, 0.15722474980613044] |
Example 2: Newton-Raphson variant
Inputs:
| zs | ks | use_first_derivative | ||||
|---|---|---|---|---|---|---|
| 0.5 | 0.3 | 0.2 | 1.685 | 0.742 | 0.532 | true |
Excel formula:
=RACHFORD_RICE({0.5,0.3,0.2}, {1.685,0.742,0.532}, TRUE)
Expected output:
| Property | Value |
|---|---|
| VF | 0.69073 |
| xs | [0.33940869696634357, 0.3650560590371706, 0.2955352439964858] |
| ys | [0.5719036543882889, 0.27087159580558057, 0.15722474980613044] |
Example 3: Halley variant
Inputs:
| zs | ks | use_first_derivative | use_second_derivative | ||||
|---|---|---|---|---|---|---|---|
| 0.5 | 0.3 | 0.2 | 1.685 | 0.742 | 0.532 | true | true |
Excel formula:
=RACHFORD_RICE({0.5,0.3,0.2}, {1.685,0.742,0.532}, TRUE, TRUE)
Expected output:
| Property | Value |
|---|---|
| VF | 0.69073 |
| xs | [0.33940869696634357, 0.3650560590371706, 0.2955352439964858] |
| ys | [0.5719036543882889, 0.27087159580558057, 0.15722474980613044] |
Example 4: Custom initial guess
Inputs:
| zs | ks | guess | ||||
|---|---|---|---|---|---|---|
| 0.4 | 0.4 | 0.2 | 2 | 0.8 | 0.4 | 0.5 |
Excel formula:
=RACHFORD_RICE({0.4,0.4,0.2}, {2,0.8,0.4}, 0.5)
Expected output:
| Property | Value |
|---|---|
| VF | 0.518417 |
| xs | [0.26343228527928286, 0.4462708596090614, 0.2902968548930794] |
| ys | [0.5268645705585657, 0.35701668768724915, 0.11611874195723176] |
Python Code
Show Code
from chemicals.rachford_rice import Rachford_Rice_solution as chemicals_rachford_rice_solution
def rachford_rice(zs, ks, use_first_derivative=False, use_second_derivative=False, guess=None):
"""
Solve the classical Rachford-Rice flash equation.
See: https://chemicals.readthedocs.io/chemicals.rachford_rice.html#chemicals.rachford_rice.Rachford_Rice_solution
This example function is provided as-is without any representation of accuracy.
Args:
zs (list[list]): Overall mole fractions as a selected range (-).
ks (list[list]): Equilibrium K-values as a selected range (-).
use_first_derivative (bool, optional): Use first derivative in solve step (Newton-Raphson). Default is False.
use_second_derivative (bool, optional): Use second derivative in solve step (Halley method). Default is False.
guess (float, optional): Initial vapor-fraction guess (-). Default is None.
Returns:
list[list]: 2D array containing vapor fraction and phase compositions.
"""
try:
def to_list(x):
if isinstance(x, list):
out = []
for row in x:
if isinstance(row, list):
for val in row:
if val is not None and val != "":
out.append(float(val))
elif row is not None and row != "":
out.append(float(row))
return out
if x is None or x == "":
return []
return [float(x)]
z_vals = to_list(zs)
k_vals = to_list(ks)
if len(z_vals) == 0 or len(z_vals) != len(k_vals):
return "Error: zs and ks must be non-empty and have the same length"
vf, xs, ys = chemicals_rachford_rice_solution(zs=z_vals, Ks=k_vals, fprime=use_first_derivative, fprime2=use_second_derivative, guess=guess)
return [["Property", "Value"], ["VF", vf], ["xs", str(xs)], ["ys", str(ys)]]
except Exception as e:
return f"Error: {str(e)}"Online Calculator
Overall mole fractions as a selected range (-).
Equilibrium K-values as a selected range (-).
Use first derivative in solve step (Newton-Raphson).
Use second derivative in solve step (Halley method).
Initial vapor-fraction guess (-).