Skip to content

polykin.thermo.flash¤

flash2_PV ¤

flash2_PV(
    F: float,
    z: FloatVector,
    P: float,
    beta: float,
    Kcalc: Callable[
        [float, float, FloatVector, FloatVector],
        FloatVector,
    ],
    *,
    T0: float = 300.0,
    maxiter: int = 50,
    atol_inner: float = 1e-09,
    rtol_outer: float = 1e-06
) -> FlashResult

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

References

  • J.F. Boston, H.I. Britt, "A radically different formulation and solution of the single-stage flash problem", Computers & Chemical Engineering, Volume 2, Issues 2-3, 1978, p. 109-122,
PARAMETER DESCRIPTION
F

Feed mole flowrate (mol/s).

TYPE: float

z

Feed mole fractions (mol/mol).

TYPE: FloatVector

P

Pressure (Pa).

TYPE: float

beta

Vapor phase fraction (mol/mol).

TYPE: float

Kcalc

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

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

T0

Initial guess for temperature (K).

TYPE: float DEFAULT: 300.0

maxiter

Maximum number of iterations.

TYPE: int DEFAULT: 50

atol_inner

Absolute tolerance for the inner R-loop.

TYPE: float DEFAULT: 1e-09

rtol_outer

Relative tolerance for the outer volatility parameters loop.

TYPE: float DEFAULT: 1e-06

RETURNS DESCRIPTION
FlashResult

Flash result.

Source code in src/polykin/thermo/flash/vle.py
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
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
def flash2_PV(
    F: float,
    z: FloatVector,
    P: float,
    beta: float,
    Kcalc: Callable[[float, float, FloatVector, FloatVector], FloatVector],
    *,
    T0: float = 300.0,
    maxiter: int = 50,
    atol_inner: float = 1e-9,
    rtol_outer: float = 1e-6,
) -> FlashResult:
    r"""Solve a 2-phase flash problem at given pressure and vapor fraction.

    **References**

    *  J.F. Boston, H.I. Britt, "A radically different formulation and solution
       of the single-stage flash problem", Computers & Chemical Engineering,
       Volume 2, Issues 2-3, 1978, p. 109-122,

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

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

    method = "2-Phase PV Flash"
    message = ""
    success = False

    # Initial guesses
    z = z/z.sum()
    T = T0
    K = Kcalc(T, P, z, z)
    x = z/(1 + beta*(K - 1))
    y = K*x
    x /= x.sum()
    y /= y.sum()

    # Initialize volatility parameters
    Tref = 300.0
    u, Kb, A, B = _parameters_PV(T, P, x, y, beta, Kcalc, Tref, all=True)
    Kb0 = Kb

    # Outer loop
    for _ in range(maxiter):

        v_old = np.concatenate((u, [A]))

        # Inner R-loop
        if abs(beta - 0) <= eps:
            R = 0.0
        elif abs(beta - 1) <= eps:
            R = 1.0
        else:
            sol = root_brent(lambda R: _Rloop(R, u, Kb0, beta, z),
                             0.0, 1.0,
                             maxiter=maxiter,
                             tolx=atol_inner,
                             tolf=atol_inner)
            R = sol.x
            if not sol.success:
                message = f"Inner R-loop did not converge after {maxiter} iterations. Solution: {sol}."
                break

        # Compute x, y, T
        p = z/(1 - R + Kb0*R*exp(u))
        eup = exp(u)*p
        sum_p = p.sum()
        sum_eup = eup.sum()
        x = p/sum_p
        y = eup/sum_eup
        Kb = sum_p/sum_eup
        T = 1/(1/Tref + (log(Kb) - A)/B)

        # Update u, A, K
        u, A, K = _parameters_PV(T, P, x, y, beta, Kcalc, Tref, all=False, B=B)
        v_new = np.concatenate((u, [A]))

        # Check convergence
        v0 = min(vi for vi in v_new if vi > 0.0)
        if np.allclose(v_new, v_old, atol=v0*rtol_outer):
            success = True
            message = "Outer loop converged within specified tolerance."
            break

    else:
        message = f"Outer loop did not converge after {maxiter} iterations."

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

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