FLASH_INNER_LOOP

This function solves the inner flash step for vapor fraction and phase compositions using overall mole fractions and K-values.

It supports automatic method selection or explicit algorithm choice among available solvers.

The core equation solved is the Rachford-Rice balance:

\sum_i \frac{z_i(K_i-1)}{1 + VF(K_i-1)} = 0

Excel Usage

=FLASH_INNER_LOOP(zs, ks, inner_loop_method, guess, check)
  • zs (list[list], required): Overall mole fractions as a selected range (-).
  • ks (list[list], required): Equilibrium K-values as a selected range (-).
  • inner_loop_method (str, optional, default: null): Solver method name, or blank for automatic selection.
  • guess (float, optional, default: null): Initial vapor-fraction guess (-).
  • check (bool, optional, default: false): Whether to validate K-values for a positive-composition solution.

Returns (list[list]): 2D array containing vapor fraction and phase compositions.

Example 1: Automatic method selection

Inputs:

zs ks
0.5 0.3 0.2 1.685 0.742 0.532

Excel formula:

=FLASH_INNER_LOOP({0.5,0.3,0.2}, {1.685,0.742,0.532})

Expected output:

Property Value
VF 0.69073
xs [0.3394086969663436, 0.3650560590371706, 0.2955352439964858]
ys [0.571903654388289, 0.27087159580558057, 0.15722474980613044]
Example 2: Rachford-Rice secant method

Inputs:

zs ks inner_loop_method
0.5 0.3 0.2 1.685 0.742 0.532 Rachford-Rice (Secant)

Excel formula:

=FLASH_INNER_LOOP({0.5,0.3,0.2}, {1.685,0.742,0.532}, "Rachford-Rice (Secant)")

Expected output:

Property Value
VF 0.69073
xs [0.33940869696634357, 0.3650560590371706, 0.2955352439964858]
ys [0.5719036543882889, 0.27087159580558057, 0.15722474980613044]
Example 3: Li-Johns-Ahmadi method option

Inputs:

zs ks inner_loop_method
0.45 0.35 0.2 1.7 0.75 0.5 Li-Johns-Ahmadi

Excel formula:

=FLASH_INNER_LOOP({0.45,0.35,0.2}, {1.7,0.75,0.5}, "Li-Johns-Ahmadi")

Expected output:

Property Value
VF 0.5
xs [0.33333333333333337, 0.39999999999999997, 0.26666666666666666]
ys [0.5666666666666668, 0.3, 0.13333333333333333]
Example 4: Provided initial guess

Inputs:

zs ks guess
0.5 0.3 0.2 1.685 0.742 0.532 0.6

Excel formula:

=FLASH_INNER_LOOP({0.5,0.3,0.2}, {1.685,0.742,0.532}, 0.6)

Expected output:

Property Value
VF 0.69073
xs [0.3394086969663436, 0.3650560590371706, 0.29553524399648573]
ys [0.571903654388289, 0.27087159580558057, 0.1572247498061304]

Python Code

Show Code
from chemicals.flash_basic import flash_inner_loop as chemicals_flash_inner_loop

def flash_inner_loop(zs, ks, inner_loop_method=None, guess=None, check=False):
    """
    Solve flash inner-loop vapor fraction and phase compositions from overall composition and K-values.

    See: https://chemicals.readthedocs.io/chemicals.flash_basic.html#chemicals.flash_basic.flash_inner_loop

    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 (-).
        inner_loop_method (str, optional): Solver method name, or blank for automatic selection. Valid options: Analytical, Rachford-Rice Secant, Rachford-Rice Newton-Raphson, Rachford-Rice Halley, Rachford-Rice NumPy, Leibovici and Nichita 2, Rachford-Rice Polynomial, Li-Johns-Ahmadi, Leibovici and Neoschil. Default is None.
        guess (float, optional): Initial vapor-fraction guess (-). Default is None.
        check (bool, optional): Whether to validate K-values for a positive-composition solution. Default is False.

    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"

        method_val = None if inner_loop_method in (None, "") else inner_loop_method
        vf, xs, ys = chemicals_flash_inner_loop(zs=z_vals, Ks=k_vals, method=method_val, guess=guess, check=check)
        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 (-).
Solver method name, or blank for automatic selection.
Initial vapor-fraction guess (-).
Whether to validate K-values for a positive-composition solution.