Skip to content

Commit 52207d5

Browse files
committed
simplify lle
1 parent 20d574f commit 52207d5

File tree

2 files changed

+27
-24
lines changed

2 files changed

+27
-24
lines changed

thermosteam/equilibrium/lle.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from .equilibrium import Equilibrium
1313
from ..exceptions import NoEquilibrium
1414
from .binary_phase_fraction import phase_fraction
15-
from .tangential_plane_stability import lle_tangential_plan_analysis
15+
from .tangential_plane_stability import lle_tangential_plane_analysis
1616
from scipy.optimize import shgo, differential_evolution
1717
import flexsolve as flx
1818
import numpy as np
@@ -121,28 +121,22 @@ def pseudo_equilibrium(K, phi, z, T, n, f_gamma, gamma_args, inner_loop_options,
121121
# Light weight
122122
def solve_lle_mol(gamma, z, T, P, sample=None, phi=1):
123123
n = z.size
124-
stability = lle_tangential_plan_analysis(gamma, z, T, P, sample=sample)
124+
stability = lle_tangential_plane_analysis(gamma, z, T, P, samples=sample)
125125
if stability.unstable:
126126
y = stability.candidate
127127
y[y < 1e-64] = 1e-64
128-
if stability.sample_unstable:
129-
phi = min(0.99 * (z / y).min(), phi)
130-
else:
131-
phi = 0.99 * (z / y).min()
128+
phi = 0.99 * (z / y).min()
132129
x = z - phi * y
133130
x /= x.sum()
134131
K = gamma(y, T) / gamma(x, T)
135132
else:
136133
return z
137134
K[K <= 0] = 1e-9
138-
try:
139-
mol = pseudo_equilibrium(
140-
K, phi, z, T, n, gamma.f, gamma.args,
141-
LLE.pseudo_equilibrium_inner_loop_options,
142-
LLE.pseudo_equilibrium_outer_loop_options,
143-
)
144-
except:
145-
breakpoint()
135+
mol = pseudo_equilibrium(
136+
K, phi, z, T, n, gamma.f, gamma.args,
137+
LLE.pseudo_equilibrium_inner_loop_options,
138+
LLE.pseudo_equilibrium_outer_loop_options,
139+
)
146140
if mol.any():
147141
return mol / mol.sum()
148142
else:
@@ -357,22 +351,32 @@ def solve_lle_liquid_mol(self, z, T, lle_chemicals, single_loop):
357351
K = self._K
358352
phi = self._phi
359353
x0 = z / (1. + phi * (K - 1.))
354+
x0 /= x0.sum()
360355
sample = x0
361356
else:
362357
sample = None
363-
stability = lle_tangential_plan_analysis(gamma, z, T, 101325, sample=sample)
358+
stability = lle_tangential_plane_analysis(gamma, z, T, 101325, sample=sample)
364359
if stability.unstable:
365360
y = stability.candidate
366-
y[y < 1e-64] = 1e-64
367-
if stability.sample_unstable:
368-
phi = min(0.99 * (z / y).min(), phi)
369-
else:
370-
phi = 0.99 * (z / y).min()
361+
y[y < 1e-32] = 1e-32
362+
phi = 0.999 * (z / y).min()
371363
x = z - phi * y
372364
x /= x.sum()
373365
K = gamma(y, T) / gamma(x, T)
374366
else:
375-
return z
367+
indices = np.argsort(z * np.array([i.MW for i in lle_chemicals]))
368+
x = z.copy()
369+
y = z.copy()
370+
a = indices[-1]
371+
b = indices[-2]
372+
x[a] = 0.99
373+
y[a] = 1e-3
374+
x[b] = 1e-3
375+
y[b] = 0.99
376+
x /= x.sum()
377+
y /= y.sum()
378+
K = gamma(y, T) / gamma(x, T)
379+
phi = 0.5
376380
if single_loop:
377381
f_gamma = gamma.f
378382
gamma_args = gamma.args

thermosteam/equilibrium/tangential_plane_stability.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ class StabilityReport(NamedTuple):
1919
unstable: bool
2020
candidate: np.ndarray
2121
tpd: float
22-
sample_unstable: bool = False
2322

2423
def edge_points_simplex_masked(z: np.ndarray,
2524
points_per_edge: int = 12,
@@ -90,7 +89,7 @@ def edge_points_simplex_masked(z: np.ndarray,
9089
return np.array(pts)
9190

9291
# Light weight
93-
def lle_tangential_plan_analysis(gamma, z, T, P, sample=None):
92+
def lle_tangential_plane_analysis(gamma, z, T, P, sample=None):
9493
MW = np.array([i.MW for i in gamma.chemicals])
9594
logfz = np.log(z * gamma(z, T, P) + 1e-30)
9695

@@ -148,7 +147,7 @@ def objective(w, T, P, logfz, softmax=False):
148147
return StabilityReport(
149148
unstable=best_val < 0 and np.abs(best_result - z).sum() > 1e-9,
150149
candidate=best_result,
151-
tpd=best_val
150+
tpd=best_val,
152151
)
153152

154153
class TangentPlaneStabilityAnalysis:

0 commit comments

Comments
 (0)