|
| 1 | +#include "gtest/gtest.h" |
| 2 | +#include "gmock/gmock.h" |
| 3 | +#include<streambuf> |
| 4 | +#ifdef __MPI |
| 5 | +#include "mpi.h" |
| 6 | +#endif |
| 7 | + |
| 8 | +/************************************************ |
| 9 | + * unit test of Atom_pseudo |
| 10 | + ***********************************************/ |
| 11 | + |
| 12 | +/** |
| 13 | + * - Tested Functions: |
| 14 | + * - Atom() |
| 15 | + * - constructor of class Atom |
| 16 | + * - ~Atom() |
| 17 | + * - deconstructor of class Atom |
| 18 | + * - print_Atom() |
| 19 | + * - print atomic info to file |
| 20 | + * - set_index() |
| 21 | + * - index between numerical atomic orbtials and quantum numbers L,m |
| 22 | + * - bcast_atom() |
| 23 | + * - bcast basic atomic info to all processes |
| 24 | + * - bcast_atom2() |
| 25 | + * - bcast norm-conserving pseudopotential info to all processes |
| 26 | + */ |
| 27 | + |
| 28 | +#define private public |
| 29 | +#include "module_cell/read_pp.h" |
| 30 | +#include "module_cell/pseudo_nc.h" |
| 31 | +#include "module_cell/atom_pseudo.h" |
| 32 | +#include "module_cell/atom_spec.h" |
| 33 | + |
| 34 | +class AtomSpecTest : public testing::Test |
| 35 | +{ |
| 36 | +protected: |
| 37 | + Atom atom; |
| 38 | + Pseudopot_upf upf; |
| 39 | + std::ofstream ofs; |
| 40 | + std::ifstream ifs; |
| 41 | +}; |
| 42 | + |
| 43 | +TEST_F(AtomSpecTest, PrintAtom) |
| 44 | +{ |
| 45 | + ofs.open("tmp_atom_info"); |
| 46 | + atom.label = "C"; |
| 47 | + atom.type = 1; |
| 48 | + atom.na = 2; |
| 49 | + atom.nwl = 2; |
| 50 | + atom.Rcut = 1.1; |
| 51 | + atom.nw = 14; |
| 52 | + atom.stapos_wf = 0; |
| 53 | + atom.mass = 12.0; |
| 54 | + delete[] atom.tau; |
| 55 | + atom.tau = new ModuleBase::Vector3<double>[atom.na]; |
| 56 | + atom.tau[0].x = 0.2; |
| 57 | + atom.tau[0].y = 0.2; |
| 58 | + atom.tau[0].z = 0.2; |
| 59 | + atom.tau[1].x = 0.4; |
| 60 | + atom.tau[1].y = 0.4; |
| 61 | + atom.tau[1].z = 0.4; |
| 62 | + atom.print_Atom(ofs); |
| 63 | + ofs.close(); |
| 64 | + ifs.open("tmp_atom_info"); |
| 65 | + std::string str((std::istreambuf_iterator<char>(ifs)),std::istreambuf_iterator<char>()); |
| 66 | + EXPECT_THAT(str, testing::HasSubstr("label = C")); |
| 67 | + EXPECT_THAT(str, testing::HasSubstr("type = 1")); |
| 68 | + EXPECT_THAT(str, testing::HasSubstr("na = 2")); |
| 69 | + EXPECT_THAT(str, testing::HasSubstr("nwl = 2")); |
| 70 | + EXPECT_THAT(str, testing::HasSubstr("Rcut = 1.1")); |
| 71 | + EXPECT_THAT(str, testing::HasSubstr("nw = 14")); |
| 72 | + EXPECT_THAT(str, testing::HasSubstr("stapos_wf = 0")); |
| 73 | + EXPECT_THAT(str, testing::HasSubstr("mass = 12")); |
| 74 | + EXPECT_THAT(str, testing::HasSubstr("atom_position(cartesian) Dimension = 2")); |
| 75 | + ifs.close(); |
| 76 | + remove("tmp_atom_info"); |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +TEST_F(AtomSpecTest, SetIndex) |
| 81 | +{ |
| 82 | + ifs.open("./support/C.upf"); |
| 83 | + GlobalV::PSEUDORCUT = 15.0; |
| 84 | + upf.read_pseudo_upf201(ifs); |
| 85 | + atom.ncpp.set_pseudo_nc(upf); |
| 86 | + ifs.close(); |
| 87 | + EXPECT_TRUE(atom.ncpp.has_so); |
| 88 | + atom.nw = 0; |
| 89 | + atom.nwl = 2; |
| 90 | + delete[] atom.l_nchi; |
| 91 | + atom.l_nchi = new int[atom.nwl]; |
| 92 | + atom.l_nchi[0] = 2; |
| 93 | + atom.nw += atom.l_nchi[0]; |
| 94 | + atom.l_nchi[1] = 4; |
| 95 | + atom.nw += 3*atom.l_nchi[1]; |
| 96 | + atom.set_index(); |
| 97 | + EXPECT_EQ(atom.iw2l[13],1); |
| 98 | + EXPECT_EQ(atom.iw2n[13],3); |
| 99 | + EXPECT_EQ(atom.iw2m[13],2); |
| 100 | + EXPECT_EQ(atom.iw2_ylm[13],3); |
| 101 | + EXPECT_TRUE(atom.iw2_new[11]); |
| 102 | +} |
| 103 | + |
| 104 | +#ifdef __MPI |
| 105 | +TEST_F(AtomSpecTest, BcastAtom) |
| 106 | +{ |
| 107 | + GlobalV::test_atom = 1; |
| 108 | + if(GlobalV::MY_RANK==0) |
| 109 | + { |
| 110 | + atom.label = "C"; |
| 111 | + atom.type = 1; |
| 112 | + atom.na = 2; |
| 113 | + atom.nwl = 2; |
| 114 | + atom.Rcut = 1.1; |
| 115 | + atom.nw = 14; |
| 116 | + atom.stapos_wf = 0; |
| 117 | + atom.mass = 12.0; |
| 118 | + delete[] atom.tau; |
| 119 | + atom.tau = new ModuleBase::Vector3<double>[atom.na]; |
| 120 | + atom.tau[0].x = 0.2; |
| 121 | + atom.tau[0].y = 0.2; |
| 122 | + atom.tau[0].z = 0.2; |
| 123 | + atom.tau[1].x = 0.4; |
| 124 | + atom.tau[1].y = 0.4; |
| 125 | + atom.tau[1].z = 0.4; |
| 126 | + } |
| 127 | + atom.bcast_atom(); |
| 128 | + if(GlobalV::MY_RANK!=0) |
| 129 | + { |
| 130 | + EXPECT_EQ(atom.label,"C"); |
| 131 | + EXPECT_EQ(atom.type,1); |
| 132 | + EXPECT_EQ(atom.na,2); |
| 133 | + EXPECT_EQ(atom.nwl,2); |
| 134 | + EXPECT_DOUBLE_EQ(atom.Rcut,1.1); |
| 135 | + EXPECT_EQ(atom.nw,14); |
| 136 | + EXPECT_EQ(atom.stapos_wf,0); |
| 137 | + EXPECT_DOUBLE_EQ(atom.mass,12.0); |
| 138 | + EXPECT_DOUBLE_EQ(atom.tau[0].x,0.2); |
| 139 | + EXPECT_DOUBLE_EQ(atom.tau[1].z,0.4); |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +TEST_F(AtomSpecTest, BcastAtom2) |
| 144 | +{ |
| 145 | + if(GlobalV::MY_RANK==0) |
| 146 | + { |
| 147 | + ifs.open("./support/C.upf"); |
| 148 | + GlobalV::PSEUDORCUT = 15.0; |
| 149 | + upf.read_pseudo_upf201(ifs); |
| 150 | + atom.ncpp.set_pseudo_nc(upf); |
| 151 | + ifs.close(); |
| 152 | + EXPECT_TRUE(atom.ncpp.has_so); |
| 153 | + } |
| 154 | + atom.bcast_atom2(); |
| 155 | + if(GlobalV::MY_RANK!=0) |
| 156 | + { |
| 157 | + EXPECT_EQ(atom.ncpp.nbeta,6); |
| 158 | + EXPECT_EQ(atom.ncpp.nchi,3); |
| 159 | + EXPECT_DOUBLE_EQ(atom.ncpp.rho_atc[0],8.7234550809E-01); |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +int main(int argc, char **argv) |
| 164 | +{ |
| 165 | + MPI_Init(&argc, &argv); |
| 166 | + testing::InitGoogleTest(&argc, argv); |
| 167 | + |
| 168 | + MPI_Comm_size(MPI_COMM_WORLD,&GlobalV::NPROC); |
| 169 | + MPI_Comm_rank(MPI_COMM_WORLD,&GlobalV::MY_RANK); |
| 170 | + int result = RUN_ALL_TESTS(); |
| 171 | + |
| 172 | + MPI_Finalize(); |
| 173 | + |
| 174 | + return result; |
| 175 | +} |
| 176 | +#endif |
| 177 | +#undef private |
0 commit comments