Skip to content

Commit 08a5e4f

Browse files
authored
Fix: no k weights for get_wf (#6859)
1 parent f96f14e commit 08a5e4f

File tree

15 files changed

+197
-2
lines changed

15 files changed

+197
-2
lines changed

source/source_cell/klist.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,19 @@ void K_Vectors::normalize_wk(const int& degspin)
566566
{
567567
sum += this->wk[ik];
568568
}
569-
assert(sum > 0.0);
569+
570+
// If sum of weights is zero or very small, set equal weights
571+
if (sum < 1e-10)
572+
{
573+
ModuleBase::WARNING("K_Vectors::normalize_wk",
574+
"Sum of k-point weights is zero or very small. "
575+
"Setting equal weights for all k-points.");
576+
for (int ik = 0; ik < nkstot; ik++)
577+
{
578+
this->wk[ik] = 1.0 / double(nkstot);
579+
}
580+
sum = 1.0;
581+
}
570582

571583
for (int ik = 0; ik < nkstot; ik++)
572584
{

source/source_cell/klist.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,8 @@ class K_Vectors
273273
* @return void
274274
*
275275
* @note This function should only be called by the master process (MY_RANK == 0).
276-
* @note The function assumes that the sum of the weights of the k-points is greater than 0.
276+
* @note If the sum of the weights is zero or very small (< 1e-10), the function will set equal weights for all
277+
* k-points and issue a warning. This allows calculations like get_wf to proceed with zero-weight k-points.
277278
* @note The function first normalizes the weights so that their sum is 1, and then scales them by the degeneracy of
278279
* spin.
279280
*/

source/source_cell/test/klist_test.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,32 @@ TEST_F(KlistTest, NormalizeWk)
727727
EXPECT_DOUBLE_EQ(kv->wk[1], 1.0);
728728
}
729729

730+
TEST_F(KlistTest, NormalizeWkZeroWeights)
731+
{
732+
// Test that zero weights are handled correctly
733+
kv->nspin = 1;
734+
kv->set_nkstot(3);
735+
kv->set_nks(3);
736+
kv->renew(kv->get_nkstot());
737+
kv->wk[0] = 0.0;
738+
kv->wk[1] = 0.0;
739+
kv->wk[2] = 0.0;
740+
int deg = 2;
741+
742+
// Should not crash and should set equal weights
743+
kv->normalize_wk(deg);
744+
745+
// Each k-point should have weight = deg / nkstot = 2 / 3
746+
EXPECT_NEAR(kv->wk[0], 2.0 / 3.0, 1e-10);
747+
EXPECT_NEAR(kv->wk[1], 2.0 / 3.0, 1e-10);
748+
EXPECT_NEAR(kv->wk[2], 2.0 / 3.0, 1e-10);
749+
750+
// Sum should equal deg
751+
double sum = kv->wk[0] + kv->wk[1] + kv->wk[2];
752+
EXPECT_NEAR(sum, 2.0, 1e-10);
753+
}
754+
755+
730756
TEST_F(KlistTest, UpdateUseIBZ)
731757
{
732758
kv->nspin = 1;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
INPUT_PARAMETERS
2+
suffix autotest
3+
4+
#calculation scf
5+
#out_wfc_lcao 1
6+
7+
calculation get_wf
8+
symmetry -1
9+
nbands 2
10+
nspin 2
11+
pseudo_dir ../../PP_ORB
12+
orbital_dir ../../PP_ORB
13+
latname sc
14+
ecutwfc 25.0
15+
basis_type lcao
16+
scf_thr 1e-10
17+
read_file_dir ./
18+
19+
out_wfc_norm 1
20+
out_wfc_re_im 1
21+
22+
bx 2
23+
by 2
24+
bz 2
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
K_POINTS
2+
3
3+
Direct
4+
0.0000 0.0000 0.0000 0.0
5+
0.3333 0.0000 0.0000 0.0
6+
0.6667 0.0000 0.0000 0.0
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Test the 'calculation get_wf' option with zero-weight k-points.
2+
This test verifies that ABACUS can handle k-points with zero weights
3+
by automatically assigning equal weights and issuing a warning.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#This is the atom file containing all the information
2+
#about the lattice structure.
3+
4+
ATOMIC_SPECIES
5+
H 1.0008 H_ONCV_PBE-1.0.upf
6+
7+
NUMERICAL_ORBITAL
8+
H_gga_6au_60Ry_2s1p.orb
9+
10+
LATTICE_CONSTANT
11+
10.0 #Lattice constant
12+
13+
ATOMIC_POSITIONS
14+
Cartesian #Cartesian(Unit is LATTICE_CONSTANT)
15+
H #Name of element
16+
0.0 #Magnetic for this element.
17+
2 #Number of atoms
18+
0.00 0.00 -0.0661400 0 0 0 #x,y,z, move_x, move_y, move_z
19+
0.00 0.00 0.0661400 0 0 0 #x,y,z, move_x, move_y, move_z
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
wfi1s1k1.cube -11.77152988
2+
wfi1s1k1im.cube 0
3+
wfi1s1k1re.cube -11.77169544
4+
wfi1s1k2.cube -3.754629032
5+
wfi1s1k2im.cube -5.256421773e-15
6+
wfi1s1k2re.cube -3.754662163
7+
wfi1s1k3.cube -3.754629032
8+
wfi1s1k3im.cube -2.592407863e-14
9+
wfi1s1k3re.cube -3.754662163
10+
wfi1s2k1.cube -11.77152988
11+
wfi1s2k1im.cube 0
12+
wfi1s2k1re.cube -15.5263576
13+
wfi1s2k2.cube -3.754629032
14+
wfi1s2k2im.cube 8.143533574e-14
15+
wfi1s2k2re.cube -7.509324327
16+
wfi1s2k3.cube -3.754629032
17+
wfi1s2k3im.cube -3.563679367e-14
18+
wfi1s2k3re.cube -7.509324327
19+
totaltimeref 0.22
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
1 (index of k points)
2+
0 0 0
3+
2 (number of bands)
4+
10 (number of orbitals)
5+
1 (band)
6+
-7.51324209e-01 (Ry)
7+
3.33333333e-01 (Occupations)
8+
-5.37247451e-01 0.00000000e+00 -3.61675036e-02 0.00000000e+00 -1.54180993e-02 0.00000000e+00 -6.00760481e-17 0.00000000e+00 4.95327447e-17 0.00000000e+00
9+
-5.37247451e-01 0.00000000e+00 -3.61675036e-02 0.00000000e+00 1.54180993e-02 0.00000000e+00 7.40355316e-17 0.00000000e+00 -2.60742120e-17 0.00000000e+00
10+
2 (band)
11+
4.36966415e-01 (Ry)
12+
0.00000000e+00 (Occupations)
13+
-1.75242688e+00 0.00000000e+00 4.89257747e-01 0.00000000e+00 -1.47270024e-02 0.00000000e+00 2.73797359e-16 0.00000000e+00 5.21506844e-17 0.00000000e+00
14+
1.75242688e+00 0.00000000e+00 -4.89257747e-01 0.00000000e+00 -1.47270024e-02 0.00000000e+00 -1.13418218e-16 0.00000000e+00 -1.40732906e-16 0.00000000e+00
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
4 (index of k points)
2+
0 0 0
3+
2 (number of bands)
4+
10 (number of orbitals)
5+
1 (band)
6+
-7.51324209e-01 (Ry)
7+
3.33333333e-01 (Occupations)
8+
-5.37247451e-01 0.00000000e+00 -3.61675036e-02 0.00000000e+00 -1.54180993e-02 0.00000000e+00 2.25946347e-17 0.00000000e+00 -3.23946428e-17 0.00000000e+00
9+
-5.37247451e-01 0.00000000e+00 -3.61675036e-02 0.00000000e+00 1.54180993e-02 0.00000000e+00 -4.69322153e-17 0.00000000e+00 9.89372825e-18 0.00000000e+00
10+
2 (band)
11+
4.36966415e-01 (Ry)
12+
0.00000000e+00 (Occupations)
13+
-1.75242688e+00 0.00000000e+00 4.89257747e-01 0.00000000e+00 -1.47270024e-02 0.00000000e+00 -2.23164913e-16 0.00000000e+00 -1.71182391e-16 0.00000000e+00
14+
1.75242688e+00 0.00000000e+00 -4.89257747e-01 0.00000000e+00 -1.47270024e-02 0.00000000e+00 -2.19012650e-16 0.00000000e+00 1.56688230e-16 0.00000000e+00

0 commit comments

Comments
 (0)