Skip to content

Commit dc5234f

Browse files
jmmshnthe-hampel
authored andcommitted
raise if dichotomy fails
1 parent bb9567a commit dc5234f

File tree

1 file changed

+21
-5
lines changed

1 file changed

+21
-5
lines changed

python/triqs_dft_tools/sumk_dft.py

+21-5
Original file line numberDiff line numberDiff line change
@@ -2073,11 +2073,27 @@ def F_optimize(mu):
20732073
# check for lowercase matching for the method variable
20742074
if method.lower() == "dichotomy":
20752075
mpi.report("\nsumk calc_mu: Using dichtomy adjustment to find chemical potential\n")
2076-
self.chemical_potential = dichotomy.dichotomy(function=F_bisection,
2077-
x_init=self.chemical_potential, y_value=density,
2078-
precision_on_y=precision, delta_x=delta, max_loops=max_loops,
2079-
x_name="Chemical Potential", y_name="Total Density",
2080-
verbosity=3)[0]
2076+
result = dichotomy.dichotomy(function=F_bisection,
2077+
x_init=self.chemical_potential,
2078+
y_value=density,
2079+
precision_on_y=precision,
2080+
delta_x=delta,
2081+
max_loops=max_loops,
2082+
x_name="Chemical Potential",
2083+
y_name="Total Density",
2084+
verbosity=3)
2085+
if result[0] is None:
2086+
raise ValueError(
2087+
f"\nsumk calc_mu: Failed to find chemical potential using dichotomy method.\n"
2088+
f" Target total density: {density}\n"
2089+
f" Starting guess for chemical potential: {self.chemical_potential}\n"
2090+
f" Step size (delta): {delta}, Precision required: {precision}\n"
2091+
f" Maximum iterations allowed: {max_loops}\n"
2092+
f" The method could not converge: no value of mu found such that |F(mu) - {density}| < {precision}.\n"
2093+
f" Suggestion:\n"
2094+
f" Adjust the initial guess or increase delta."
2095+
)
2096+
self.chemical_potential = result[0]
20812097
elif method.lower() == "newton":
20822098
mpi.report("\nsumk calc_mu: Using Newton method to find chemical potential\n")
20832099
self.chemical_potential = newton(func=F_optimize,

0 commit comments

Comments
 (0)