@@ -19,6 +19,7 @@ class StabilityReport(NamedTuple):
1919 unstable : bool
2020 candidate : np .ndarray
2121 tpd : float
22+ stable_sample : bool
2223
2324def 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
153158class 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