Skip to content

Commit 5c29851

Browse files
committed
minor improvement for speed
1 parent 52207d5 commit 5c29851

File tree

2 files changed

+44
-34
lines changed

2 files changed

+44
-34
lines changed

thermosteam/equilibrium/lle.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,12 @@ def solve_lle_liquid_mol(self, z, T, lle_chemicals, single_loop):
358358
stability = lle_tangential_plane_analysis(gamma, z, T, 101325, sample=sample)
359359
if stability.unstable:
360360
y = stability.candidate
361-
y[y < 1e-32] = 1e-32
362-
phi = 0.999 * (z / y).min()
363-
x = z - phi * y
364-
x /= x.sum()
365-
K = gamma(y, T) / gamma(x, T)
361+
if not stability.stable_sample:
362+
y[y < 1e-32] = 1e-32
363+
phi = 0.999 * (z / y).min()
364+
x = z - phi * y
365+
x /= x.sum()
366+
K = gamma(y, T) / gamma(x, T)
366367
else:
367368
indices = np.argsort(z * np.array([i.MW for i in lle_chemicals]))
368369
x = z.copy()

thermosteam/equilibrium/tangential_plane_stability.py

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class StabilityReport(NamedTuple):
1919
unstable: bool
2020
candidate: np.ndarray
2121
tpd: float
22+
stable_sample: bool
2223

2324
def edge_points_simplex_masked(z: np.ndarray,
2425
points_per_edge: int = 12,
@@ -103,23 +104,26 @@ def objective(w, T, P, logfz, softmax=False):
103104
if sample is None:
104105
best_val = np.inf
105106
best_result = None
107+
stable_sample = False
106108
else:
107109
best_result = sample
108110
best_val = objective(sample, *args)
109-
result = minimize(
110-
objective,
111-
best_result,
112-
method="L-BFGS-B",
113-
options=dict(maxiter=20),
114-
args=(*args, True),
115-
)
116-
value = result.fun
117-
if value < best_val:
118-
w = result.x
119-
w = np.exp(w - np.max(w)) # Softmax for unconstrained optimization
120-
w /= w.sum()
121-
best_result = w
122-
best_val = value
111+
stable_sample = best_val < 0 and np.abs(best_result - z).sum() > 1e-9
112+
if not stable_sample:
113+
result = minimize(
114+
objective,
115+
best_result,
116+
method="L-BFGS-B",
117+
options=dict(maxiter=20),
118+
args=(*args, True),
119+
)
120+
value = result.fun
121+
if value < best_val:
122+
w = result.x
123+
w = np.exp(w - np.max(w)) # Softmax for unconstrained optimization
124+
w /= w.sum()
125+
best_result = w
126+
best_val = value
123127
w = z * MW
124128
w /= w.sum()
125129
samples = edge_points_simplex_masked(w)
@@ -148,6 +152,7 @@ def objective(w, T, P, logfz, softmax=False):
148152
unstable=best_val < 0 and np.abs(best_result - z).sum() > 1e-9,
149153
candidate=best_result,
150154
tpd=best_val,
155+
stable_sample=stable_sample,
151156
)
152157

153158
class TangentPlaneStabilityAnalysis:
@@ -176,23 +181,26 @@ def __call__(self, z, T, P, reference_phase='l', potential_phase='L', sample=Non
176181
if sample is None:
177182
best_val = np.inf
178183
best_result = None
184+
stable_sample = False
179185
else:
180186
best_result = sample
181187
best_val = objective(sample, *args)
182-
result = minimize(
183-
objective,
184-
best_result,
185-
method="L-BFGS-B",
186-
options=dict(maxiter=20),
187-
args=(*args, True),
188-
)
189-
value = result.fun
190-
if value < best_val:
191-
w = result.x
192-
w = np.exp(w - np.max(w)) # Softmax for unconstrained optimization
193-
w /= w.sum()
194-
best_result = w
195-
best_val = value
188+
stable_sample = best_val < 0 and np.abs(best_result - z).sum() > 1e-9
189+
if not stable_sample:
190+
result = minimize(
191+
objective,
192+
best_result,
193+
method="L-BFGS-B",
194+
options=dict(maxiter=20),
195+
args=(*args, True),
196+
)
197+
value = result.fun
198+
if value < best_val:
199+
w = result.x
200+
w = np.exp(w - np.max(w)) # Softmax for unconstrained optimization
201+
w /= w.sum()
202+
best_result = w
203+
best_val = value
196204
MW = self.MW
197205
w = z * MW
198206
w /= w.sum()
@@ -221,7 +229,8 @@ def __call__(self, z, T, P, reference_phase='l', potential_phase='L', sample=Non
221229
return StabilityReport(
222230
unstable=best_val < 0 and np.abs(best_result - z).sum() > 1e-9,
223231
candidate=best_result,
224-
tpd=best_val
232+
tpd=best_val,
233+
stable_sample=stable_sample,
225234
)
226235

227236

0 commit comments

Comments
 (0)