Skip to content

Commit 0eb74f6

Browse files
committed
update the ucell test
1 parent 3d057a7 commit 0eb74f6

File tree

3 files changed

+60
-53
lines changed

3 files changed

+60
-53
lines changed

source/module_cell/klist.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ void K_Vectors::set(const ModuleSymmetry::Symmetry& symm,
9494
this->nspin = (this->nspin == 4) ? 1 : this->nspin;
9595

9696
// read KPT file and generate K-point grid
97-
bool read_succesfully = this->read_kpoints(k_file_name);
97+
bool read_succesfully = this->read_kpoints(k_file_name,ucell);
9898
#ifdef __MPI
9999
Parallel_Common::bcast_bool(read_succesfully);
100100
#endif
@@ -210,7 +210,8 @@ void K_Vectors::renew(const int& kpoint_number)
210210

211211
// Read the KPT file, which contains K-point coordinates, weights, and grid size information
212212
// Generate K-point grid according to different parameters of the KPT file
213-
bool K_Vectors::read_kpoints(const std::string& fn)
213+
bool K_Vectors::read_kpoints(const std::string& fn,
214+
const UnitCell& ucell)
214215
{
215216
ModuleBase::TITLE("K_Vectors", "read_kpoints");
216217
if (GlobalV::MY_RANK != 0)
@@ -237,16 +238,16 @@ bool K_Vectors::read_kpoints(const std::string& fn)
237238
ModuleBase::WARNING_QUIT("K_Vectors", "kspacing should > 0");
238239
};
239240
// number of K points = max(1,int(|bi|/KSPACING+1))
240-
ModuleBase::Matrix3 btmp = GlobalC::ucell.G;
241+
ModuleBase::Matrix3 btmp = ucell.G;
241242
double b1 = sqrt(btmp.e11 * btmp.e11 + btmp.e12 * btmp.e12 + btmp.e13 * btmp.e13);
242243
double b2 = sqrt(btmp.e21 * btmp.e21 + btmp.e22 * btmp.e22 + btmp.e23 * btmp.e23);
243244
double b3 = sqrt(btmp.e31 * btmp.e31 + btmp.e32 * btmp.e32 + btmp.e33 * btmp.e33);
244245
int nk1
245-
= std::max(1, static_cast<int>(b1 * ModuleBase::TWO_PI / PARAM.inp.kspacing[0] / GlobalC::ucell.lat0 + 1));
246+
= std::max(1, static_cast<int>(b1 * ModuleBase::TWO_PI / PARAM.inp.kspacing[0] / ucell.lat0 + 1));
246247
int nk2
247-
= std::max(1, static_cast<int>(b2 * ModuleBase::TWO_PI / PARAM.inp.kspacing[1] / GlobalC::ucell.lat0 + 1));
248+
= std::max(1, static_cast<int>(b2 * ModuleBase::TWO_PI / PARAM.inp.kspacing[1] / ucell.lat0 + 1));
248249
int nk3
249-
= std::max(1, static_cast<int>(b3 * ModuleBase::TWO_PI / PARAM.inp.kspacing[2] / GlobalC::ucell.lat0 + 1));
250+
= std::max(1, static_cast<int>(b3 * ModuleBase::TWO_PI / PARAM.inp.kspacing[2] / ucell.lat0 + 1));
250251

251252
GlobalV::ofs_warning << " Generate k-points file according to KSPACING: " << fn << std::endl;
252253
std::ofstream ofs(fn.c_str());

source/module_cell/klist.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,8 @@ class K_Vectors
205205
* @note If the k-points type is Line mode and the symmetry flag is 1, it will quit with a warning.
206206
* @note If the number of k-points is greater than 100000, it will quit with a warning.
207207
*/
208-
bool read_kpoints(const std::string& fn); // return 0: something wrong.
208+
bool read_kpoints(const std::string& fn,
209+
const UnitCell& ucell); // return 0: something wrong.
209210

210211
/**
211212
* @brief Adds k-points linearly between special points.

source/module_cell/test/klist_test.cpp

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ Fcoef::~Fcoef()
8686
namespace GlobalC
8787
{
8888
Parallel_Kpoints Pkpoints;
89-
UnitCell ucell;
9089
} // namespace GlobalC
9190

9291
/************************************************
@@ -221,6 +220,22 @@ class KlistTest : public testing::Test
221220
ucell.nat += ucell.atoms[i].na;
222221
}
223222
}
223+
224+
void setucell()
225+
{
226+
ucell.latvec.e11 = 10.0;
227+
ucell.latvec.e12 = 0.0;
228+
ucell.latvec.e13 = 0.0;
229+
ucell.latvec.e21 = 0.0;
230+
ucell.latvec.e22 = 10.0;
231+
ucell.latvec.e23 = 0.0;
232+
ucell.latvec.e31 = 0.0;
233+
ucell.latvec.e32 = 0.0;
234+
ucell.latvec.e33 = 10.0;
235+
ucell.GT = ucell.latvec.Inverse();
236+
ucell.G = ucell.GT.Transpose();
237+
ucell.lat0 = 1.8897261254578281;
238+
}
224239
// clear ucell
225240
void ClearUcell()
226241
{
@@ -237,18 +252,6 @@ TEST_F(KlistTest, Construct)
237252
EXPECT_FALSE(kv->kc_done);
238253
EXPECT_FALSE(kv->kd_done);
239254
// just to set ucell info here, however it is used in the following tests
240-
GlobalC::ucell.latvec.e11 = 10.0;
241-
GlobalC::ucell.latvec.e12 = 0.0;
242-
GlobalC::ucell.latvec.e13 = 0.0;
243-
GlobalC::ucell.latvec.e21 = 0.0;
244-
GlobalC::ucell.latvec.e22 = 10.0;
245-
GlobalC::ucell.latvec.e23 = 0.0;
246-
GlobalC::ucell.latvec.e31 = 0.0;
247-
GlobalC::ucell.latvec.e32 = 0.0;
248-
GlobalC::ucell.latvec.e33 = 10.0;
249-
GlobalC::ucell.GT = GlobalC::ucell.latvec.Inverse();
250-
GlobalC::ucell.G = GlobalC::ucell.GT.Transpose();
251-
GlobalC::ucell.lat0 = 1.8897261254578281;
252255
}
253256

254257
TEST_F(KlistTest, MP)
@@ -292,9 +295,9 @@ TEST_F(KlistTest, MP)
292295
TEST_F(KlistTest, ReadKpointsGammaOnlyLocal)
293296
{
294297
PARAM.sys.gamma_only_local = true;
295-
std::string kfile = "KPT_GO";
298+
std::string k_file = "KPT_GO";
296299
kv->nspin = 1;
297-
kv->read_kpoints(kfile);
300+
kv->read_kpoints(k_file,ucell);
298301
ifs.open("KPT_GO");
299302
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
300303
EXPECT_THAT(str, testing::HasSubstr("Gamma"));
@@ -309,8 +312,9 @@ TEST_F(KlistTest, ReadKpointsKspacing)
309312
PARAM.input.kspacing[0] = 0.052918; // 0.52918/Bohr = 1/A
310313
PARAM.input.kspacing[1] = 0.052918; // 0.52918/Bohr = 1/A
311314
PARAM.input.kspacing[2] = 0.052918; // 0.52918/Bohr = 1/A
315+
setucell();
312316
std::string k_file = "./support/KPT3";
313-
kv->read_kpoints(k_file);
317+
kv->read_kpoints(k_file,ucell);
314318
EXPECT_EQ(kv->get_nkstot(), 343);
315319
PARAM.input.kspacing[0] = 0.0;
316320
PARAM.input.kspacing[1] = 0.0;
@@ -323,8 +327,9 @@ TEST_F(KlistTest, ReadKpointsKspacing3values)
323327
PARAM.input.kspacing[0] = 0.052918; // 0.52918/Bohr = 1/A
324328
PARAM.input.kspacing[1] = 0.06; // 0.52918/Bohr = 1/A
325329
PARAM.input.kspacing[2] = 0.07; // 0.52918/Bohr = 1/A
330+
setucell();
326331
std::string k_file = "./support/KPT3";
327-
kv->read_kpoints(k_file);
332+
kv->read_kpoints(k_file,ucell);
328333
EXPECT_EQ(kv->get_nkstot(), 210);
329334
PARAM.input.kspacing[0] = 0.0;
330335
PARAM.input.kspacing[1] = 0.0;
@@ -339,7 +344,7 @@ TEST_F(KlistTest, ReadKpointsInvalidKspacing3values)
339344
PARAM.input.kspacing[2] = 0.07; // 0.52918/Bohr = 1/A
340345
std::string k_file = "./support/KPT3";
341346
testing::internal::CaptureStdout();
342-
EXPECT_EXIT(kv->read_kpoints(k_file), ::testing::ExitedWithCode(0), "");
347+
EXPECT_EXIT(kv->read_kpoints(k_file,ucell), ::testing::ExitedWithCode(0), "");
343348
output = testing::internal::GetCapturedStdout();
344349
PARAM.input.kspacing[0] = 0.0;
345350
PARAM.input.kspacing[1] = 0.0;
@@ -350,15 +355,15 @@ TEST_F(KlistTest, ReadKpointsGamma)
350355
{
351356
std::string k_file = "./support/KPT";
352357
kv->nspin = 1;
353-
kv->read_kpoints(k_file);
358+
kv->read_kpoints(k_file,ucell);
354359
EXPECT_EQ(kv->get_nkstot(), 512);
355360
}
356361

357362
TEST_F(KlistTest, ReadKpointsMP)
358363
{
359364
std::string k_file = "./support/KPT1";
360365
kv->nspin = 1;
361-
kv->read_kpoints(k_file);
366+
kv->read_kpoints(k_file,ucell);
362367
EXPECT_EQ(kv->get_nkstot(), 512);
363368
}
364369

@@ -368,7 +373,7 @@ TEST_F(KlistTest, ReadKpointsLine)
368373
// symm_flag is required in read_kpoints for a k list
369374
std::string k_file = "./support/KPT2";
370375
kv->nspin = 1;
371-
kv->read_kpoints(k_file);
376+
kv->read_kpoints(k_file,ucell);
372377
EXPECT_EQ(kv->get_nkstot(), 122);
373378
}
374379

@@ -377,11 +382,11 @@ TEST_F(KlistTest, ReadKpointsCartesian)
377382
std::string k_file = "./support/KPT4";
378383
// Cartesian: non-spin case nspin=1
379384
kv->nspin = 1;
380-
kv->read_kpoints(k_file);
385+
kv->read_kpoints(k_file,ucell);
381386
EXPECT_EQ(kv->kvec_c.size(), 5);
382387
// spin case nspin=2
383388
kv->nspin = 2;
384-
kv->read_kpoints(k_file);
389+
kv->read_kpoints(k_file,ucell);
385390
EXPECT_EQ(kv->kvec_c.size(), 10);
386391
}
387392

@@ -392,13 +397,13 @@ TEST_F(KlistTest, ReadKpointsLineCartesian)
392397
kv->nspin = 1;
393398
kv->set_kup_and_kdw();
394399
// Read from k point file under the case of Line_Cartesian.
395-
kv->read_kpoints(k_file);
400+
kv->read_kpoints(k_file,ucell);
396401
EXPECT_EQ(kv->get_nkstot(), 51);
397402
EXPECT_EQ(kv->kvec_c.size(), 51);
398403
// Line Cartesian: spin case nspin=2
399404
kv->nspin = 2;
400405
// Read from k point file under the case of Line_Cartesian.
401-
kv->read_kpoints(k_file);
406+
kv->read_kpoints(k_file,ucell);
402407
EXPECT_EQ(kv->get_nkstot(), 51);
403408
EXPECT_EQ(kv->kvec_c.size(), 102);
404409
}
@@ -409,7 +414,7 @@ TEST_F(KlistTest, ReadKpointsDirect)
409414
kv->nspin = 1;
410415
kv->set_kup_and_kdw();
411416
// Read from k point file under the case of Direct
412-
kv->read_kpoints(k_file);
417+
kv->read_kpoints(k_file,ucell);
413418
EXPECT_EQ(kv->get_nkstot(), 6);
414419
EXPECT_TRUE(kv->kd_done);
415420
}
@@ -419,7 +424,7 @@ TEST_F(KlistTest, ReadKpointsWarning1)
419424
std::string k_file = "arbitrary_1";
420425
kv->nspin = 1;
421426
GlobalV::ofs_warning.open("klist_tmp_warning_1");
422-
EXPECT_NO_THROW(kv->read_kpoints(k_file));
427+
EXPECT_NO_THROW(kv->read_kpoints(k_file,ucell));
423428
GlobalV::ofs_warning.close();
424429
ifs.open("klist_tmp_warning_1");
425430
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
@@ -436,7 +441,7 @@ TEST_F(KlistTest, ReadKpointsWarning2)
436441
ofs.close();
437442
kv->nspin = 1;
438443
GlobalV::ofs_warning.open("klist_tmp_warning_2");
439-
EXPECT_NO_THROW(kv->read_kpoints(k_file));
444+
EXPECT_NO_THROW(kv->read_kpoints(k_file,ucell));
440445
GlobalV::ofs_warning.close();
441446
ifs.open("klist_tmp_warning_2");
442447
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
@@ -455,7 +460,7 @@ TEST_F(KlistTest, ReadKpointsWarning3)
455460
ofs.close();
456461
kv->nspin = 1;
457462
GlobalV::ofs_warning.open("klist_tmp_warning_3");
458-
EXPECT_NO_THROW(kv->read_kpoints(k_file));
463+
EXPECT_NO_THROW(kv->read_kpoints(k_file,ucell));
459464
GlobalV::ofs_warning.close();
460465
ifs.open("klist_tmp_warning_3");
461466
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
@@ -475,7 +480,7 @@ TEST_F(KlistTest, ReadKpointsWarning4)
475480
ofs.close();
476481
kv->nspin = 1;
477482
GlobalV::ofs_warning.open("klist_tmp_warning_4");
478-
EXPECT_NO_THROW(kv->read_kpoints(k_file));
483+
EXPECT_NO_THROW(kv->read_kpoints(k_file,ucell));
479484
GlobalV::ofs_warning.close();
480485
ifs.open("klist_tmp_warning_4");
481486
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
@@ -496,7 +501,7 @@ TEST_F(KlistTest, ReadKpointsWarning5)
496501
// Cartesian: non-spin case nspin=1
497502
kv->nspin = 1;
498503
GlobalV::ofs_warning.open("klist_tmp_warning_5");
499-
EXPECT_NO_THROW(kv->read_kpoints(k_file));
504+
EXPECT_NO_THROW(kv->read_kpoints(k_file,ucell));
500505
GlobalV::ofs_warning.close();
501506
ifs.open("klist_tmp_warning_5");
502507
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
@@ -518,7 +523,7 @@ TEST_F(KlistTest, ReadKpointsWarning6)
518523
kv->nspin = 1;
519524
ModuleSymmetry::Symmetry::symm_flag = 1;
520525
GlobalV::ofs_warning.open("klist_tmp_warning_6");
521-
EXPECT_NO_THROW(kv->read_kpoints(k_file));
526+
EXPECT_NO_THROW(kv->read_kpoints(k_file,ucell));
522527
GlobalV::ofs_warning.close();
523528
ifs.open("klist_tmp_warning_6");
524529
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
@@ -540,7 +545,7 @@ TEST_F(KlistTest, ReadKpointsWarning7)
540545
kv->nspin = 1;
541546
ModuleSymmetry::Symmetry::symm_flag = 1;
542547
GlobalV::ofs_warning.open("klist_tmp_warning_7");
543-
EXPECT_NO_THROW(kv->read_kpoints(k_file));
548+
EXPECT_NO_THROW(kv->read_kpoints(k_file,ucell));
544549
GlobalV::ofs_warning.close();
545550
ifs.open("klist_tmp_warning_7");
546551
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
@@ -556,14 +561,14 @@ TEST_F(KlistTest, SetKupKdown)
556561
std::string k_file = "./support/KPT4";
557562
// Cartesian: non-spin case nspin=1
558563
kv->nspin = 1;
559-
kv->read_kpoints(k_file);
564+
kv->read_kpoints(k_file,ucell);
560565
kv->set_kup_and_kdw();
561566
for (int ik = 0; ik < 5; ik++)
562567
{
563568
EXPECT_EQ(kv->isk[ik], 0);
564569
}
565570
kv->nspin = 4;
566-
kv->read_kpoints(k_file);
571+
kv->read_kpoints(k_file,ucell);
567572
kv->set_kup_and_kdw();
568573
for (int ik = 0; ik < 5; ik++)
569574
{
@@ -573,7 +578,7 @@ TEST_F(KlistTest, SetKupKdown)
573578
EXPECT_EQ(kv->isk[ik + 15], 0);
574579
}
575580
kv->nspin = 2;
576-
kv->read_kpoints(k_file);
581+
kv->read_kpoints(k_file,ucell);
577582
kv->set_kup_and_kdw();
578583
for (int ik = 0; ik < 5; ik++)
579584
{
@@ -591,7 +596,7 @@ TEST_F(KlistTest, SetAfterVC)
591596
kv->kvec_c[0].x = 0;
592597
kv->kvec_c[0].y = 0;
593598
kv->kvec_c[0].z = 0;
594-
kv->set_after_vc(PARAM.input.nspin, GlobalC::ucell.G, GlobalC::ucell.latvec);
599+
kv->set_after_vc(PARAM.input.nspin, ucell.G, ucell.latvec);
595600
EXPECT_TRUE(kv->kd_done);
596601
EXPECT_TRUE(kv->kc_done);
597602
EXPECT_DOUBLE_EQ(kv->kvec_d[0].x, 0);
@@ -611,7 +616,7 @@ TEST_F(KlistTest, PrintKlists)
611616
kv->kvec_c[0].x = 0;
612617
kv->kvec_c[0].y = 0;
613618
kv->kvec_c[0].z = 0;
614-
kv->set_after_vc(PARAM.input.nspin, GlobalC::ucell.G, GlobalC::ucell.latvec);
619+
kv->set_after_vc(PARAM.input.nspin, ucell.G, ucell.latvec);
615620
EXPECT_TRUE(kv->kd_done);
616621
kv->print_klists(GlobalV::ofs_running);
617622
GlobalV::ofs_running.close();
@@ -651,24 +656,24 @@ TEST_F(KlistTest, SetBothKvecFinalSCF)
651656
kv->kc_done = false;
652657
// case 1
653658
kv->k_nkstot = 0;
654-
kv->set_both_kvec(GlobalC::ucell.G, GlobalC::ucell.latvec, skpt);
659+
kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
655660
EXPECT_TRUE(kv->kd_done);
656661
EXPECT_TRUE(kv->kc_done);
657662
// case 2
658663
kv->k_nkstot = 1;
659664
kv->k_kword = "D";
660-
kv->set_both_kvec(GlobalC::ucell.G, GlobalC::ucell.latvec, skpt);
665+
kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
661666
EXPECT_TRUE(kv->kd_done);
662667
EXPECT_TRUE(kv->kc_done);
663668
// case 3
664669
kv->k_kword = "C";
665-
kv->set_both_kvec(GlobalC::ucell.G, GlobalC::ucell.latvec, skpt);
670+
kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
666671
EXPECT_TRUE(kv->kc_done);
667672
EXPECT_TRUE(kv->kd_done);
668673
// case 4
669674
GlobalV::ofs_warning.open("klist_tmp_warning_8");
670675
kv->k_kword = "arbitrary";
671-
kv->set_both_kvec(GlobalC::ucell.G, GlobalC::ucell.latvec, skpt);
676+
kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
672677
GlobalV::ofs_warning.close();
673678
ifs.open("klist_tmp_warning_8");
674679
std::string str((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
@@ -690,11 +695,11 @@ TEST_F(KlistTest, SetBothKvec)
690695
kv->kd_done = true;
691696
std::string skpt;
692697
PARAM.input.final_scf = false;
693-
kv->set_both_kvec(GlobalC::ucell.G, GlobalC::ucell.latvec, skpt);
698+
kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
694699
EXPECT_TRUE(kv->kc_done);
695700
kv->kc_done = true;
696701
kv->kd_done = false;
697-
kv->set_both_kvec(GlobalC::ucell.G, GlobalC::ucell.latvec, skpt);
702+
kv->set_both_kvec(ucell.G, ucell.latvec, skpt);
698703
EXPECT_TRUE(kv->kd_done);
699704
}
700705

@@ -735,7 +740,7 @@ TEST_F(KlistTest, IbzKpoint)
735740
// read KPT
736741
std::string k_file = "./support/KPT1";
737742
kv->nspin = 1;
738-
kv->read_kpoints(k_file);
743+
kv->read_kpoints(k_file,ucell);
739744
EXPECT_EQ(kv->get_nkstot(), 512);
740745
// calculate ibz_kpoint
741746
std::string skpt;
@@ -759,7 +764,7 @@ TEST_F(KlistTest, IbzKpointIsMP)
759764
// read KPT
760765
std::string k_file = "./support/KPT1";
761766
kv->nspin = 1;
762-
kv->read_kpoints(k_file);
767+
kv->read_kpoints(k_file,ucell);
763768
EXPECT_EQ(kv->get_nkstot(), 512);
764769
EXPECT_TRUE(kv->is_mp);
765770
// calculate ibz_kpoint

0 commit comments

Comments
 (0)