Skip to content

Commit 377c165

Browse files
author
root
committed
fix: the Nan value when rho is negative in ELF
1 parent 2e51b4d commit 377c165

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

source/module_io/write_elf.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,15 @@ void write_elf(
5555
if (nspin == 1)
5656
{
5757
for (int ir = 0; ir < rho_basis->nrxx; ++ir)
58-
{
59-
tau_TF[0][ir] = c_tf * std::pow(rho[0][ir], 5.0 / 3.0);
58+
{
59+
// Because of numerical problem, the rho may be negative, and then pow(rho, 5/3) will be NaN.
60+
// So we set tau_TF = 0 when rho < 0.
61+
if (rho[0][ir] < 0.0) {
62+
tau_TF[0][ir] = 0.0;
63+
}
64+
else {
65+
tau_TF[0][ir] = c_tf * std::pow(rho[0][ir], 5.0 / 3.0);
66+
}
6067
}
6168
}
6269
else if (nspin == 2)
@@ -65,7 +72,14 @@ void write_elf(
6572
{
6673
for (int ir = 0; ir < rho_basis->nrxx; ++ir)
6774
{
68-
tau_TF[is][ir] = 0.5 * c_tf * std::pow(2.0 * rho[is][ir], 5.0 / 3.0);
75+
// Because of numerical problem, the rho may be negative, and then pow(rho, 5/3) will be NaN.
76+
// So we set tau_TF = 0 when rho < 0.
77+
if (rho[is][ir] < 0.0) {
78+
tau_TF[is][ir] = 0.0;
79+
}
80+
else {
81+
tau_TF[is][ir] = 0.5 * c_tf * std::pow(2.0 * rho[is][ir], 5.0 / 3.0);
82+
}
6983
}
7084
}
7185
}
@@ -76,8 +90,16 @@ void write_elf(
7690
{
7791
for (int ir = 0; ir < rho_basis->nrxx; ++ir)
7892
{
79-
elf[is][ir] = (tau[is][ir] - tau_vw[is][ir] + eps) / tau_TF[is][ir];
80-
elf[is][ir] = 1. / (1. + elf[is][ir] * elf[is][ir]);
93+
// when tau_TF is small, then ELF is set to be zero.
94+
if (tau_TF[is][ir] < 1e-12)
95+
{
96+
elf[is][ir] = 0.0;
97+
}
98+
else {
99+
elf[is][ir] = (tau[is][ir] - tau_vw[is][ir] + eps) / tau_TF[is][ir];
100+
elf[is][ir] = 1. / (1. + elf[is][ir] * elf[is][ir]);
101+
}
102+
81103
}
82104
}
83105

0 commit comments

Comments
 (0)