Skip to content

Commit 4b16d5a

Browse files
jmmshnthe-hampel
authored andcommitted
raise if dichotomy fails
1 parent 4e8231e commit 4b16d5a

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
@@ -2083,11 +2083,27 @@ def F_optimize(mu):
20832083
# check for lowercase matching for the method variable
20842084
if method.lower() == "dichotomy":
20852085
mpi.report("\nsumk calc_mu: Using dichtomy adjustment to find chemical potential\n")
2086-
self.chemical_potential = dichotomy.dichotomy(function=F_bisection,
2087-
x_init=self.chemical_potential, y_value=density,
2088-
precision_on_y=precision, delta_x=delta, max_loops=max_loops,
2089-
x_name="Chemical Potential", y_name="Total Density",
2090-
verbosity=3)[0]
2086+
result = dichotomy.dichotomy(function=F_bisection,
2087+
x_init=self.chemical_potential,
2088+
y_value=density,
2089+
precision_on_y=precision,
2090+
delta_x=delta,
2091+
max_loops=max_loops,
2092+
x_name="Chemical Potential",
2093+
y_name="Total Density",
2094+
verbosity=3)
2095+
if result[0] is None:
2096+
raise ValueError(
2097+
f"\nsumk calc_mu: Failed to find chemical potential using dichotomy method.\n"
2098+
f" Target total density: {density}\n"
2099+
f" Starting guess for chemical potential: {self.chemical_potential}\n"
2100+
f" Step size (delta): {delta}, Precision required: {precision}\n"
2101+
f" Maximum iterations allowed: {max_loops}\n"
2102+
f" The method could not converge: no value of mu found such that |F(mu) - {density}| < {precision}.\n"
2103+
f" Suggestion:\n"
2104+
f" Adjust the initial guess or increase delta."
2105+
)
2106+
self.chemical_potential = result[0]
20912107
elif method.lower() == "newton":
20922108
mpi.report("\nsumk calc_mu: Using Newton method to find chemical potential\n")
20932109
self.chemical_potential = newton(func=F_optimize,

0 commit comments

Comments
 (0)