Skip to content

polykin.thermo.flash¤

flash2_PT ¤

flash2_PT(
    F: float,
    z: FloatVector,
    P: float,
    T: float,
    Kcalc: Callable[
        [float, float, FloatVector, FloatVector],
        FloatVector,
    ],
    *,
    beta0: float | None = None,
    maxiter: int = 50,
    atol_inner: float = 1e-09,
    rtol_outer: float = 1e-06,
    alpha_outer: float = 1.0
) -> FlashResult

Solve a 2-phase flash problem at given temperature and pressure.

References

  • M.L. Michelsen and J. Mollerup, "Thermodynamic models: Fundamentals and computational aspects", Tie-Line publications, 2nd edition, 2007.
PARAMETER DESCRIPTION
F

Feed mole flowrate (mol/s).

TYPE: float

z

Feed mole fractions (mol/mol).

TYPE: FloatVector

T

Temperature (K).

TYPE: float

P

Pressure (Pa).

TYPE: float

Kcalc

Function to calculate K-values, with signature Kcalc(T, P, x, y).

TYPE: Callable[[float, float, FloatVector, FloatVector], FloatVector]

beta0

Initial guess for vapor phase fraction.

TYPE: float | None DEFAULT: None

maxiter

Maximum number of iterations.

TYPE: int DEFAULT: 50

atol_inner

Absolute tolerance for the inner beta-loop.

TYPE: float DEFAULT: 1e-09

rtol_outer

Relative tolerance for the outer K-values loop.

TYPE: float DEFAULT: 1e-06

alpha_outer

Relaxation factor for the outer K-values loop.

TYPE: float DEFAULT: 1.0

RETURNS DESCRIPTION
FlashResult

Flash result.

Source code in src/polykin/thermo/flash/vle.py
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
def flash2_PT(
    F: float,
    z: FloatVector,
    P: float,
    T: float,
    Kcalc: Callable[[float, float, FloatVector, FloatVector], FloatVector],
    *,
    beta0: float | None = None,
    maxiter: int = 50,
    atol_inner: float = 1e-9,
    rtol_outer: float = 1e-6,
    alpha_outer: float = 1.0,
) -> FlashResult:
    r"""Solve a 2-phase flash problem at given temperature and pressure.

    **References**

    *  M.L. Michelsen and J. Mollerup, "Thermodynamic models: Fundamentals and
       computational aspects", Tie-Line publications, 2nd edition, 2007.

    Parameters
    ----------
    F : float
        Feed mole flowrate (mol/s).
    z : FloatVector
        Feed mole fractions (mol/mol).
    T : float
        Temperature (K).
    P : float
        Pressure (Pa).
    Kcalc : Callable[[float, float, FloatVector, FloatVector], FloatVector]
        Function to calculate K-values, with signature `Kcalc(T, P, x, y)`.
    beta0 : float | None
        Initial guess for vapor phase fraction.
    maxiter : int
        Maximum number of iterations.    
    atol_inner : float
        Absolute tolerance for the inner beta-loop.
    rtol_outer : float
        Relative tolerance for the outer K-values loop.
    alpha_outer : float
        Relaxation factor for the outer K-values loop.

    Returns
    -------
    FlashResult
        Flash result.
    """

    # Initial guesses
    z = z/z.sum()
    x = z
    y = z
    beta = np.nan
    K = Kcalc(T, P, z, z)

    success = False
    for _ in range(maxiter):

        # Find beta
        sol = solve_Rachford_Rice(K, z, beta0, maxiter, atol_inner)
        beta = sol.beta
        if not sol.success:
            warnings.warn(
                f"Inner Rachford-Rice loop did not converge after {maxiter} iterations.\n"
                f"Solution: {sol}.",
                ConvergenceWarning)

        # Compute x, y
        x = z/(1 + beta*(K - 1))
        y = K*x
        x /= x.sum()
        y /= y.sum()

        # Update K
        K_old = K.copy()
        K_new = Kcalc(T, P, x, y)

        # Check convergence
        k0 = min(k for k in K_new if k > 0)
        if np.allclose(K_new, K_old, atol=k0*rtol_outer):
            success = True
            break
        else:
            K = exp(alpha_outer*log(K_new) + (1 - alpha_outer)*log(K_old))
            beta0 = beta

    else:
        warnings.warn(
            f"Outer loop did not converge after {maxiter} iterations.",
            ConvergenceWarning)

    # Overall balance
    V = F*beta
    L = F - V

    return FlashResult(success, T, P, F, L, V, beta, z, x, y, K)