Skip to content

Commit 69600dd

Browse files
author
abacus_fixer
committed
test(klist): fix broken semantics exposed by the nspin->spin_mult rename
After the rename of K_Vectors::nspin -> spin_mult, two kinds of latent misuses in the tests became visible and are corrected here: 1. In klist_test_para.cpp, set() calls had been passing kv->spin_mult as the nspin_in argument. That was only working by accident (both sides happened to be 1). Since set() takes the *physical* nspin (1/2/4, which it then maps to spin_mult internally), replace the member argument with explicit physical-nspin literals, and remove the now- redundant pre-set of the member (set() overwrites it anyway). 2. In klist_test.cpp (SetKupKdown test), the non-collinear case had been written as kv->spin_mult = 4, which is not a legal spin_mult value (only 1 and 2 are). K_Vectors maps nspin=4 -> 1 (non- collinear does not double the k-point list); the 4 was only masking a coincidentally matching renew(nkstot*4) size and a set_kup_and_kdw default branch that did nothing. Change it to the correct mapped value (1), add case A/B/C comments explaining the physical spin source for each case, and remove the bogus isk[ik+5/10/15] checks that relied on the accidentally oversized container. All production call sites already pass the physical nspin correctly (inp.nspin / inp_->nspin), so no source changes are needed there.
1 parent 4fffc2f commit 69600dd

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

source/source_cell/test/klist_test.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -632,22 +632,26 @@ TEST_F(KlistTest, SetKupKdown)
632632
const std::string kmesh_type = "gamma";
633633
const double koffset[3] = {0.0, 0.0, 0.0};
634634
std::string k_file = "./support/KPT4";
635+
636+
// case A: physical nspin=1 -> spin_mult=1 (no doubling).
635637
kv->spin_mult = 1;
636638
kv->read_kpoints(ucell, k_file, gamma_only_local, kspacing, kmesh_type, koffset, GlobalV::ofs_running);
637639
kv->set_kup_and_kdw(GlobalV::ofs_running);
638640
for (int ik = 0; ik < 5; ik++)
639641
{
640642
EXPECT_EQ(kv->isk[ik], 0);
641643
}
642-
kv->spin_mult = 4;
644+
645+
// case B: physical nspin=4 (non-collinear) maps to spin_mult=1 at
646+
// K_Vectors::set() time; non-collinear does not double the k-point list,
647+
// so the correct spin_mult is still 1. We bypass set() here, so set the
648+
// mapped value directly.
649+
kv->spin_mult = 1;
643650
kv->read_kpoints(ucell, k_file, gamma_only_local, kspacing, kmesh_type, koffset, GlobalV::ofs_running);
644651
kv->set_kup_and_kdw(GlobalV::ofs_running);
645652
for (int ik = 0; ik < 5; ik++)
646653
{
647654
EXPECT_EQ(kv->isk[ik], 0);
648-
EXPECT_EQ(kv->isk[ik + 5], 0);
649-
EXPECT_EQ(kv->isk[ik + 10], 0);
650-
EXPECT_EQ(kv->isk[ik + 15], 0);
651655
}
652656
kv->spin_mult = 2;
653657
kv->read_kpoints(ucell, k_file, gamma_only_local, kspacing, kmesh_type, koffset, GlobalV::ofs_running);

source/source_cell/test/klist_test_para.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,8 @@ TEST_F(KlistParaTest, Set)
183183
symm.analy_sys(ucell.lat, ucell.st, ucell.atoms, GlobalV::ofs_running, 1e-6, 1, "scf", cal_symm_repr);
184184
// read KPT
185185
std::string k_file = "./support/KPT1";
186-
// set klist
187-
kv->spin_mult = 1;
186+
// note: do NOT pre-set kv->spin_mult here; set() takes the physical
187+
// nspin as input and performs the 4->1 mapping internally.
188188
if (GlobalV::NPROC == 4)
189189
{
190190
GlobalV::KPAR = 2;
@@ -207,7 +207,7 @@ TEST_F(KlistParaTest, Set)
207207
const double kspacing[3] = {0.0, 0.0, 0.0};
208208
const std::string kmesh_type = "gamma";
209209
const double koffset[3] = {0.0, 0.0, 0.0};
210-
kv->set(ucell, symm, k_file, kv->spin_mult, ucell.G, ucell.latvec, GlobalV::ofs_running, use_ibz, global_out_dir, gamma_only_local, kspacing, kmesh_type, koffset);
210+
kv->set(ucell, symm, k_file, /*nspin_in*/ 1, ucell.G, ucell.latvec, GlobalV::ofs_running, use_ibz, global_out_dir, gamma_only_local, kspacing, kmesh_type, koffset);
211211
EXPECT_EQ(kv->get_nkstot(), 35);
212212
EXPECT_EQ(kv->get_nkstot_full(), 512);
213213
EXPECT_GT(kv->get_nkstot_full(), kv->get_nkstot());
@@ -307,8 +307,8 @@ TEST_F(KlistParaTest, SetAfterVC)
307307
symm.analy_sys(ucell.lat, ucell.st, ucell.atoms, GlobalV::ofs_running, 1e-6, 1, "scf", cal_symm_repr);
308308
// read KPT
309309
std::string k_file = "./support/KPT1";
310-
// set klist
311-
kv->spin_mult = 1;
310+
// note: do NOT pre-set kv->spin_mult here; set() takes the physical
311+
// nspin as input and performs the 4->1 mapping internally.
312312
if (GlobalV::NPROC == 4)
313313
{
314314
GlobalV::KPAR = 1;
@@ -331,7 +331,7 @@ TEST_F(KlistParaTest, SetAfterVC)
331331
const double kspacing[3] = {0.0, 0.0, 0.0};
332332
const std::string kmesh_type = "gamma";
333333
const double koffset[3] = {0.0, 0.0, 0.0};
334-
kv->set(ucell, symm, k_file, kv->spin_mult, ucell.G, ucell.latvec, GlobalV::ofs_running, use_ibz, global_out_dir, gamma_only_local, kspacing, kmesh_type, koffset);
334+
kv->set(ucell, symm, k_file, /*nspin_in*/ 1, ucell.G, ucell.latvec, GlobalV::ofs_running, use_ibz, global_out_dir, gamma_only_local, kspacing, kmesh_type, koffset);
335335
EXPECT_EQ(kv->get_nkstot(), 35);
336336
EXPECT_TRUE(kv->kc_done);
337337
EXPECT_TRUE(kv->kd_done);

0 commit comments

Comments
 (0)