From e733396c02611e156875ea5dfd6cbad9f1bb2c66 Mon Sep 17 00:00:00 2001 From: Zhao Tianqi Date: Tue, 25 Jun 2024 00:57:51 +0800 Subject: [PATCH] Fix: bug in mulliken output (#4463) * Fix: bug in mulliken output * [pre-commit.ci lite] apply automatic fixes * add test in unitcell_test_para.cpp * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- .../module_cell/test/unitcell_test_para.cpp | 267 +++++++++--------- source/module_cell/unitcell.cpp | 16 +- 2 files changed, 146 insertions(+), 137 deletions(-) diff --git a/source/module_cell/test/unitcell_test_para.cpp b/source/module_cell/test/unitcell_test_para.cpp index 11e1949a52..f8fcc899a9 100644 --- a/source/module_cell/test/unitcell_test_para.cpp +++ b/source/module_cell/test/unitcell_test_para.cpp @@ -1,29 +1,34 @@ -#include "gtest/gtest.h" -#include "gmock/gmock.h" #include "memory" -#include "module_base/mathzone.h" #include "module_base/global_variable.h" +#include "module_base/mathzone.h" #include "module_cell/unitcell.h" -#include + +#include "gmock/gmock.h" +#include "gtest/gtest.h" #include +#include #ifdef __MPI #include "mpi.h" #endif #include "prepare_unitcell.h" #ifdef __LCAO -InfoNonlocal::InfoNonlocal(){} -InfoNonlocal::~InfoNonlocal(){} +InfoNonlocal::InfoNonlocal() +{ +} +InfoNonlocal::~InfoNonlocal() +{ +} #endif Magnetism::Magnetism() { - this->tot_magnetization = 0.0; - this->abs_magnetization = 0.0; - this->start_magnetization = nullptr; + this->tot_magnetization = 0.0; + this->abs_magnetization = 0.0; + this->start_magnetization = nullptr; } Magnetism::~Magnetism() { - delete[] this->start_magnetization; + delete[] this->start_magnetization; } /************************************************ @@ -44,150 +49,154 @@ Magnetism::~Magnetism() * - read_pseudo() */ -//mock function +// mock function #ifdef __LCAO -void LCAO_Orbitals::bcast_files( - const int &ntype_in, - const int &my_rank) +void LCAO_Orbitals::bcast_files(const int& ntype_in, const int& my_rank) { - return; + return; } #endif class UcellTest : public ::testing::Test { -protected: - UcellTestPrepare utp = UcellTestLib["C1H2-Read"]; - std::unique_ptr ucell; - std::ofstream ofs; - std::string pp_dir; - std::string output; - void SetUp() - { - ofs.open("running.log"); - GlobalV::relax_new = utp.relax_new; - GlobalV::global_out_dir = "./"; - ucell = utp.SetUcellInfo(); - GlobalV::LSPINORB = false; - pp_dir = "./support/"; - GlobalV::PSEUDORCUT = 15.0; - GlobalV::DFT_FUNCTIONAL = "default"; - GlobalV::test_unitcell = 1; - GlobalV::test_pseudo_cell = 1; - GlobalV::NSPIN = 1; - GlobalV::BASIS_TYPE = "pw"; - } - void TearDown() - { - ofs.close(); - } + protected: + UcellTestPrepare utp = UcellTestLib["C1H2-Read"]; + std::unique_ptr ucell; + std::ofstream ofs; + std::string pp_dir; + std::string output; + void SetUp() + { + ofs.open("running.log"); + GlobalV::relax_new = utp.relax_new; + GlobalV::global_out_dir = "./"; + ucell = utp.SetUcellInfo(); + GlobalV::LSPINORB = false; + pp_dir = "./support/"; + GlobalV::PSEUDORCUT = 15.0; + GlobalV::DFT_FUNCTIONAL = "default"; + GlobalV::test_unitcell = 1; + GlobalV::test_pseudo_cell = 1; + GlobalV::NSPIN = 1; + GlobalV::BASIS_TYPE = "pw"; + } + void TearDown() + { + ofs.close(); + } }; #ifdef __MPI -TEST_F(UcellTest,BcastUnitcell2) +TEST_F(UcellTest, BcastUnitcell2) { - ucell->read_cell_pseudopots(pp_dir,ofs); - ucell->bcast_unitcell2(); - if(GlobalV::MY_RANK!=0) - { - EXPECT_EQ(ucell->atoms[0].ncpp.nbeta,4); - EXPECT_EQ(ucell->atoms[0].ncpp.nchi,2); - EXPECT_EQ(ucell->atoms[1].ncpp.nbeta,3); - EXPECT_EQ(ucell->atoms[1].ncpp.nchi,1); - } + ucell->read_cell_pseudopots(pp_dir, ofs); + ucell->bcast_unitcell2(); + if (GlobalV::MY_RANK != 0) + { + EXPECT_EQ(ucell->atoms[0].ncpp.nbeta, 4); + EXPECT_EQ(ucell->atoms[0].ncpp.nchi, 2); + EXPECT_EQ(ucell->atoms[1].ncpp.nbeta, 3); + EXPECT_EQ(ucell->atoms[1].ncpp.nchi, 1); + } } -TEST_F(UcellTest,BcastUnitcell) +TEST_F(UcellTest, BcastUnitcell) { - GlobalV::NSPIN=4; - ucell->bcast_unitcell(); - if(GlobalV::MY_RANK!=0) - { - EXPECT_EQ(ucell->Coordinate,"Direct"); - EXPECT_DOUBLE_EQ(ucell->a1.x,10.0); - EXPECT_EQ(ucell->atoms[0].na,1); - EXPECT_EQ(ucell->atoms[1].na,2); - } + GlobalV::NSPIN = 4; + ucell->bcast_unitcell(); + if (GlobalV::MY_RANK != 0) + { + EXPECT_EQ(ucell->Coordinate, "Direct"); + EXPECT_DOUBLE_EQ(ucell->a1.x, 10.0); + EXPECT_EQ(ucell->atoms[0].na, 1); + EXPECT_EQ(ucell->atoms[1].na, 2); + /// this is to ensure all processes have the atom label info + auto atom_labels = ucell->get_atomLabels(); + std::string atom_type1_expected = "C"; + std::string atom_type2_expected = "H"; + EXPECT_EQ(atom_labels[0], atom_type1_expected); + EXPECT_EQ(atom_labels[1], atom_type2_expected); + } } -TEST_F(UcellTest,UpdatePosTaud) +TEST_F(UcellTest, UpdatePosTaud) { - double* pos_in = new double[ucell->nat*3]; - ModuleBase::Vector3* tmp = new ModuleBase::Vector3[ucell->nat]; - ucell->set_iat2itia(); - for(int iat=0; iatnat; ++iat) - { - pos_in[iat*3] = 0.01; - pos_in[iat*3+1] = 0.01; - pos_in[iat*3+2] = 0.01; - int it, ia; - ucell->iat2iait(iat,&ia,&it); - tmp[iat] = ucell->atoms[it].taud[ia]; - } - ucell->update_pos_taud(pos_in); - for(int iat=0; iatnat; ++iat) - { - int it, ia; - ucell->iat2iait(iat,&ia,&it); - EXPECT_DOUBLE_EQ(ucell->atoms[it].taud[ia].x,tmp[iat].x+0.01); - EXPECT_DOUBLE_EQ(ucell->atoms[it].taud[ia].y,tmp[iat].y+0.01); - EXPECT_DOUBLE_EQ(ucell->atoms[it].taud[ia].z,tmp[iat].z+0.01); - } - delete[] pos_in; + double* pos_in = new double[ucell->nat * 3]; + ModuleBase::Vector3* tmp = new ModuleBase::Vector3[ucell->nat]; + ucell->set_iat2itia(); + for (int iat = 0; iat < ucell->nat; ++iat) + { + pos_in[iat * 3] = 0.01; + pos_in[iat * 3 + 1] = 0.01; + pos_in[iat * 3 + 2] = 0.01; + int it, ia; + ucell->iat2iait(iat, &ia, &it); + tmp[iat] = ucell->atoms[it].taud[ia]; + } + ucell->update_pos_taud(pos_in); + for (int iat = 0; iat < ucell->nat; ++iat) + { + int it, ia; + ucell->iat2iait(iat, &ia, &it); + EXPECT_DOUBLE_EQ(ucell->atoms[it].taud[ia].x, tmp[iat].x + 0.01); + EXPECT_DOUBLE_EQ(ucell->atoms[it].taud[ia].y, tmp[iat].y + 0.01); + EXPECT_DOUBLE_EQ(ucell->atoms[it].taud[ia].z, tmp[iat].z + 0.01); + } + delete[] pos_in; } -TEST_F(UcellTest,ReadPseudo) +TEST_F(UcellTest, ReadPseudo) { - GlobalV::global_pseudo_dir = pp_dir; - GlobalV::out_element_info = 1; - GlobalV::MIN_DIST_COEF = 0.2; - ucell->read_pseudo(ofs); - //check_structure will print some warning info - //output nonlocal file - if(GlobalV::MY_RANK==0) - { - std::ifstream ifs; - ifs.open("./C/C.NONLOCAL"); - EXPECT_TRUE(ifs.good()); - ifs.close(); - ifs.open("./H/H.NONLOCAL"); - EXPECT_TRUE(ifs.good()); - ifs.close(); - std::string command1 = "test -d C && rm -rf C"; - std::string command2 = "test -d H && rm -rf H"; - auto error1 = std::system( command1.c_str() ); - EXPECT_EQ(error1,0); - auto error2 = std::system( command2.c_str() ); - EXPECT_EQ(error2,0); - } - //read_cell_pseudopots - //bcast_unitcell2 - EXPECT_FALSE(ucell->atoms[0].ncpp.has_so); - EXPECT_FALSE(ucell->atoms[1].ncpp.has_so); - EXPECT_EQ(ucell->atoms[0].ncpp.nbeta,4); - EXPECT_EQ(ucell->atoms[0].ncpp.nchi,2); - EXPECT_EQ(ucell->atoms[1].ncpp.nbeta,3); - EXPECT_EQ(ucell->atoms[1].ncpp.nchi,1); - //cal_meshx - EXPECT_EQ(ucell->meshx,1247); - //cal_natomwfc - EXPECT_EQ(ucell->natomwfc,(1+3)*1+1*2); - //cal_nwfc - EXPECT_EQ(ucell->lmax,2); - EXPECT_EQ(ucell->lmax_ppwf,1); + GlobalV::global_pseudo_dir = pp_dir; + GlobalV::out_element_info = true; + GlobalV::MIN_DIST_COEF = 0.2; + ucell->read_pseudo(ofs); + // check_structure will print some warning info + // output nonlocal file + if (GlobalV::MY_RANK == 0) + { + std::ifstream ifs; + ifs.open("./C/C.NONLOCAL"); + EXPECT_TRUE(ifs.good()); + ifs.close(); + ifs.open("./H/H.NONLOCAL"); + EXPECT_TRUE(ifs.good()); + ifs.close(); + std::string command1 = "test -d C && rm -rf C"; + std::string command2 = "test -d H && rm -rf H"; + auto error1 = std::system(command1.c_str()); + EXPECT_EQ(error1, 0); + auto error2 = std::system(command2.c_str()); + EXPECT_EQ(error2, 0); + } + // read_cell_pseudopots + // bcast_unitcell2 + EXPECT_FALSE(ucell->atoms[0].ncpp.has_so); + EXPECT_FALSE(ucell->atoms[1].ncpp.has_so); + EXPECT_EQ(ucell->atoms[0].ncpp.nbeta, 4); + EXPECT_EQ(ucell->atoms[0].ncpp.nchi, 2); + EXPECT_EQ(ucell->atoms[1].ncpp.nbeta, 3); + EXPECT_EQ(ucell->atoms[1].ncpp.nchi, 1); + // cal_meshx + EXPECT_EQ(ucell->meshx, 1247); + // cal_natomwfc + EXPECT_EQ(ucell->natomwfc, (1 + 3) * 1 + 1 * 2); + // cal_nwfc + EXPECT_EQ(ucell->lmax, 2); + EXPECT_EQ(ucell->lmax_ppwf, 1); } #include "mpi.h" -int main(int argc, char **argv) +int main(int argc, char** argv) { - MPI_Init(&argc, &argv); - testing::InitGoogleTest(&argc, argv); + MPI_Init(&argc, &argv); + testing::InitGoogleTest(&argc, argv); - MPI_Comm_size(MPI_COMM_WORLD,&GlobalV::NPROC); - MPI_Comm_rank(MPI_COMM_WORLD,&GlobalV::MY_RANK); + MPI_Comm_size(MPI_COMM_WORLD, &GlobalV::NPROC); + MPI_Comm_rank(MPI_COMM_WORLD, &GlobalV::MY_RANK); - int result = RUN_ALL_TESTS(); - MPI_Finalize(); - return result; + int result = RUN_ALL_TESTS(); + MPI_Finalize(); + return result; } #endif diff --git a/source/module_cell/unitcell.cpp b/source/module_cell/unitcell.cpp index 6917c40660..836f3c825e 100644 --- a/source/module_cell/unitcell.cpp +++ b/source/module_cell/unitcell.cpp @@ -89,7 +89,7 @@ UnitCell::~UnitCell() #include "module_base/parallel_common.h" #ifdef __MPI -void UnitCell::bcast_unitcell(void) +void UnitCell::bcast_unitcell() { if (GlobalV::test_unitcell) ModuleBase::TITLE("UnitCell", "bcast_unitcell"); @@ -162,7 +162,7 @@ void UnitCell::bcast_unitcell(void) return; } -void UnitCell::bcast_unitcell2(void) +void UnitCell::bcast_unitcell2() { for (int i = 0; i < ntype; i++) { @@ -282,7 +282,7 @@ void UnitCell::print_cell_xyz(const std::string& fn) const } */ -void UnitCell::set_iat2itia(void) +void UnitCell::set_iat2itia() { assert(nat > 0); delete[] iat2it; @@ -348,7 +348,7 @@ std::vector UnitCell::get_atomLabels() const std::vector atomLabels(this->ntype); for (int it = 0; it < this->ntype; it++) { - atomLabels[it] = this->atom_label[it]; + atomLabels[it] = this->atoms[it].label; } return atomLabels; } @@ -1287,10 +1287,10 @@ bool UnitCell::if_atoms_can_move() const for (int ia = 0; ia < atom->na; ia++) { if (atom->mbl[ia].x || atom->mbl[ia].y || atom->mbl[ia].z) - return 1; + return true; } } - return 0; + return false; } // check if lattice vector can be changed @@ -1299,9 +1299,9 @@ bool UnitCell::if_cell_can_change() const // need to be fixed next if (this->lc[0] || this->lc[1] || this->lc[2]) { - return 1; + return true; } - return 0; + return false; } void UnitCell::setup(const std::string& latname_in,