Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion source/source_cell/klist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,19 @@ void K_Vectors::normalize_wk(const int& degspin)
{
sum += this->wk[ik];
}
assert(sum > 0.0);

// If sum of weights is zero or very small, set equal weights
if (sum < 1e-10)
{
ModuleBase::WARNING("K_Vectors::normalize_wk",
"Sum of k-point weights is zero or very small. "
"Setting equal weights for all k-points.");
for (int ik = 0; ik < nkstot; ik++)
{
this->wk[ik] = 1.0 / double(nkstot);
}
sum = 1.0;
}

for (int ik = 0; ik < nkstot; ik++)
{
Expand Down
3 changes: 2 additions & 1 deletion source/source_cell/klist.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ class K_Vectors
* @return void
*
* @note This function should only be called by the master process (MY_RANK == 0).
* @note The function assumes that the sum of the weights of the k-points is greater than 0.
* @note If the sum of the weights is zero or very small (< 1e-10), the function will set equal weights for all
* k-points and issue a warning. This allows calculations like get_wf to proceed with zero-weight k-points.
* @note The function first normalizes the weights so that their sum is 1, and then scales them by the degeneracy of
* spin.
*/
Expand Down
26 changes: 26 additions & 0 deletions source/source_cell/test/klist_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,32 @@ TEST_F(KlistTest, NormalizeWk)
EXPECT_DOUBLE_EQ(kv->wk[1], 1.0);
}

TEST_F(KlistTest, NormalizeWkZeroWeights)
{
// Test that zero weights are handled correctly
kv->nspin = 1;
kv->set_nkstot(3);
kv->set_nks(3);
kv->renew(kv->get_nkstot());
kv->wk[0] = 0.0;
kv->wk[1] = 0.0;
kv->wk[2] = 0.0;
int deg = 2;

// Should not crash and should set equal weights
kv->normalize_wk(deg);

// Each k-point should have weight = deg / nkstot = 2 / 3
EXPECT_NEAR(kv->wk[0], 2.0 / 3.0, 1e-10);
EXPECT_NEAR(kv->wk[1], 2.0 / 3.0, 1e-10);
EXPECT_NEAR(kv->wk[2], 2.0 / 3.0, 1e-10);

// Sum should equal deg
double sum = kv->wk[0] + kv->wk[1] + kv->wk[2];
EXPECT_NEAR(sum, 2.0, 1e-10);
}


TEST_F(KlistTest, UpdateUseIBZ)
{
kv->nspin = 1;
Expand Down
24 changes: 24 additions & 0 deletions tests/03_NAO_multik/32_NO_KP_get_wf_zero_weight/INPUT
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
INPUT_PARAMETERS
suffix autotest

#calculation scf
#out_wfc_lcao 1

calculation get_wf
symmetry -1
nbands 2
nspin 2
pseudo_dir ../../PP_ORB
orbital_dir ../../PP_ORB
latname sc
ecutwfc 25.0
basis_type lcao
scf_thr 1e-10
read_file_dir ./

out_wfc_norm 1
out_wfc_re_im 1

bx 2
by 2
bz 2
6 changes: 6 additions & 0 deletions tests/03_NAO_multik/32_NO_KP_get_wf_zero_weight/KPT
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
K_POINTS
3
Direct
0.0000 0.0000 0.0000 0.0
0.3333 0.0000 0.0000 0.0
0.6667 0.0000 0.0000 0.0
3 changes: 3 additions & 0 deletions tests/03_NAO_multik/32_NO_KP_get_wf_zero_weight/README
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Test the 'calculation get_wf' option with zero-weight k-points.
This test verifies that ABACUS can handle k-points with zero weights
by automatically assigning equal weights and issuing a warning.
19 changes: 19 additions & 0 deletions tests/03_NAO_multik/32_NO_KP_get_wf_zero_weight/STRU
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#This is the atom file containing all the information
#about the lattice structure.

ATOMIC_SPECIES
H 1.0008 H_ONCV_PBE-1.0.upf

NUMERICAL_ORBITAL
H_gga_6au_60Ry_2s1p.orb

LATTICE_CONSTANT
10.0 #Lattice constant

ATOMIC_POSITIONS
Cartesian #Cartesian(Unit is LATTICE_CONSTANT)
H #Name of element
0.0 #Magnetic for this element.
2 #Number of atoms
0.00 0.00 -0.0661400 0 0 0 #x,y,z, move_x, move_y, move_z
0.00 0.00 0.0661400 0 0 0 #x,y,z, move_x, move_y, move_z
19 changes: 19 additions & 0 deletions tests/03_NAO_multik/32_NO_KP_get_wf_zero_weight/result.ref
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
wfi1s1k1.cube -11.77152988
wfi1s1k1im.cube 0
wfi1s1k1re.cube -11.77169544
wfi1s1k2.cube -3.754629032
wfi1s1k2im.cube -5.256421773e-15
wfi1s1k2re.cube -3.754662163
wfi1s1k3.cube -3.754629032
wfi1s1k3im.cube -2.592407863e-14
wfi1s1k3re.cube -3.754662163
wfi1s2k1.cube -11.77152988
wfi1s2k1im.cube 0
wfi1s2k1re.cube -15.5263576
wfi1s2k2.cube -3.754629032
wfi1s2k2im.cube 8.143533574e-14
wfi1s2k2re.cube -7.509324327
wfi1s2k3.cube -3.754629032
wfi1s2k3im.cube -3.563679367e-14
wfi1s2k3re.cube -7.509324327
totaltimeref 0.22
14 changes: 14 additions & 0 deletions tests/03_NAO_multik/32_NO_KP_get_wf_zero_weight/wfk1s1_nao.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1 (index of k points)
0 0 0
2 (number of bands)
10 (number of orbitals)
1 (band)
-7.51324209e-01 (Ry)
3.33333333e-01 (Occupations)
-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
-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
2 (band)
4.36966415e-01 (Ry)
0.00000000e+00 (Occupations)
-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
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
14 changes: 14 additions & 0 deletions tests/03_NAO_multik/32_NO_KP_get_wf_zero_weight/wfk1s2_nao.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
4 (index of k points)
0 0 0
2 (number of bands)
10 (number of orbitals)
1 (band)
-7.51324209e-01 (Ry)
3.33333333e-01 (Occupations)
-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
-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
2 (band)
4.36966415e-01 (Ry)
0.00000000e+00 (Occupations)
-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
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
14 changes: 14 additions & 0 deletions tests/03_NAO_multik/32_NO_KP_get_wf_zero_weight/wfk2s1_nao.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
2 (index of k points)
0.33333333 0 0
2 (number of bands)
10 (number of orbitals)
1 (band)
-7.50769326e-01 (Ry)
3.33333333e-01 (Occupations)
-5.37427336e-01 -1.05559980e-18 -3.71097795e-02 5.89767804e-19 -1.53531997e-02 -2.52000064e-19 -6.10794919e-17 -2.98003376e-04 4.28540990e-17 -6.70070819e-20
-5.37427336e-01 1.15844342e-18 -3.71097795e-02 -5.70793079e-19 1.53531997e-02 -1.99414845e-19 7.22531527e-17 -2.98003376e-04 -2.77072393e-17 7.08987808e-20
2 (band)
4.40705472e-01 (Ry)
0.00000000e+00 (Occupations)
-1.74611098e+00 1.79777627e-18 4.85703522e-01 2.96309663e-18 -9.76081786e-03 1.02686625e-19 2.79195633e-16 -7.11522605e-04 1.78966954e-16 -2.29212014e-18
1.74611098e+00 -1.30843780e-18 -4.85703522e-01 3.69605802e-18 -9.76081786e-03 -1.72277732e-18 -1.27636301e-16 7.11522605e-04 -9.62680330e-17 1.00089304e-19
14 changes: 14 additions & 0 deletions tests/03_NAO_multik/32_NO_KP_get_wf_zero_weight/wfk2s2_nao.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
5 (index of k points)
0.33333333 0 0
2 (number of bands)
10 (number of orbitals)
1 (band)
-7.50769326e-01 (Ry)
3.33333333e-01 (Occupations)
-5.37427336e-01 -1.06901834e-18 -3.71097795e-02 9.88948368e-19 -1.53531997e-02 1.80111026e-20 1.14141119e-17 -2.98003376e-04 -3.06324474e-17 -1.14953806e-19
-5.37427336e-01 1.17059117e-18 -3.71097795e-02 -6.82945997e-19 1.53531997e-02 1.67973109e-19 -5.00148318e-17 -2.98003376e-04 9.69124493e-18 4.66316044e-20
2 (band)
4.40705472e-01 (Ry)
0.00000000e+00 (Occupations)
-1.74611098e+00 -2.08369602e-18 4.85703522e-01 -1.37343732e-17 -9.76081786e-03 1.67507422e-18 -3.24174080e-17 -7.11522605e-04 -2.44161562e-16 -1.32000126e-18
1.74611098e+00 -7.08124980e-19 -4.85703522e-01 -1.23075988e-17 -9.76081786e-03 2.63869504e-18 -1.40848625e-16 7.11522605e-04 1.21290669e-16 1.14340373e-18
14 changes: 14 additions & 0 deletions tests/03_NAO_multik/32_NO_KP_get_wf_zero_weight/wfk3s1_nao.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
3 (index of k points)
0.66666667 0 0
2 (number of bands)
10 (number of orbitals)
1 (band)
-7.50769326e-01 (Ry)
3.33333333e-01 (Occupations)
-5.37427336e-01 1.25616529e-18 -3.71097795e-02 -5.11002849e-19 -1.53531997e-02 2.62263916e-19 -7.47922807e-17 2.98003376e-04 4.23522015e-17 1.46891825e-19
-5.37427336e-01 -1.25520762e-18 -3.71097795e-02 6.78972343e-19 1.53531997e-02 3.53226637e-19 7.22078030e-17 2.98003376e-04 -2.67636433e-17 -1.69280472e-19
2 (band)
4.40705472e-01 (Ry)
0.00000000e+00 (Occupations)
-1.74611098e+00 -4.13533757e-18 4.85703522e-01 -4.94201126e-18 -9.76081786e-03 1.06240055e-18 2.77206952e-16 7.11522605e-04 1.61957571e-16 2.65671499e-18
1.74611098e+00 2.68898237e-18 -4.85703522e-01 -7.21715533e-18 -9.76081786e-03 5.06512961e-19 -1.24025501e-16 -7.11522605e-04 -9.08141901e-17 4.33505600e-20
14 changes: 14 additions & 0 deletions tests/03_NAO_multik/32_NO_KP_get_wf_zero_weight/wfk3s2_nao.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
6 (index of k points)
0.66666667 0 0
2 (number of bands)
10 (number of orbitals)
1 (band)
-7.50769326e-01 (Ry)
3.33333333e-01 (Occupations)
-5.37427336e-01 8.61417023e-19 -3.71097795e-02 -6.23082852e-19 -1.53531997e-02 2.44581316e-21 1.16838265e-17 2.98003376e-04 -2.92860654e-17 8.50798608e-20
-5.37427336e-01 -9.02644351e-19 -3.71097795e-02 5.33082486e-19 1.53531997e-02 -1.02404229e-19 -5.05065573e-17 2.98003376e-04 1.04755659e-17 -9.40792559e-20
2 (band)
4.40705472e-01 (Ry)
0.00000000e+00 (Occupations)
-1.74611098e+00 2.39469645e-18 4.85703522e-01 1.09268415e-17 -9.76081786e-03 -1.48128168e-18 -3.40426592e-17 7.11522605e-04 -2.47663622e-16 3.17776521e-18
1.74611098e+00 3.42509704e-19 -4.85703522e-01 1.00219566e-17 -9.76081786e-03 -9.76641093e-19 -1.37890471e-16 -7.11522605e-04 1.27874741e-16 -1.32051002e-18
1 change: 1 addition & 0 deletions tests/03_NAO_multik/CASES_CPU.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
30_NO_KP_S2_eminus
31_NO_KP_nupdown0
32_NO_KP_get_wf
32_NO_KP_get_wf_zero_weight
33_NO_KP_OW
34_NO_KP_MU
35_NO_KP_MU_nscf
Expand Down
Loading