Skip to content

Commit bd71395

Browse files
authored
Test: add UT for class Atom (#1923)
* Test: add UT for class Atom * Update atom_spec_test.cpp
1 parent 0463de3 commit bd71395

File tree

4 files changed

+212
-9
lines changed

4 files changed

+212
-9
lines changed

source/module_cell/test/CMakeLists.txt

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ AddTest(
2323
SOURCES atom_pseudo_test.cpp ../atom_pseudo.cpp ../pseudo_nc.cpp ../read_pp.cpp ../read_pp_upf201.cpp ../read_pp_upf100.cpp ../read_pp_vwr.cpp ../read_pp_blps.cpp ../../module_io/output.cpp
2424
)
2525

26-
install(FILES bcast_atom_pseudo_test.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
27-
find_program(BASH bash)
28-
29-
add_test(NAME cell_bcast_atom_pseudo_test
30-
COMMAND ${BASH} bcast_atom_pseudo_test.sh
31-
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
26+
AddTest(
27+
TARGET cell_atom_spec
28+
LIBS ${math_libs} base device
29+
SOURCES atom_spec_test.cpp ../atom_spec.cpp ../atom_pseudo.cpp ../pseudo_nc.cpp ../read_pp.cpp ../read_pp_upf201.cpp ../read_pp_upf100.cpp ../read_pp_vwr.cpp ../read_pp_blps.cpp ../../module_io/output.cpp
3230
)
3331

3432
AddTest(
@@ -46,12 +44,22 @@ AddTest(
4644
SOURCES parallel_kpoints_test.cpp ../../module_base/global_variable.cpp ../../module_base/parallel_global.cpp ../../module_base/parallel_common.cpp ../parallel_kpoints.cpp
4745
)
4846

47+
install(FILES bcast_atom_pseudo_test.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
48+
install(FILES bcast_atom_spec_test.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
4949
install(FILES parallel_kpoints_test.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
50-
5150
find_program(BASH bash)
5251

52+
add_test(NAME cell_bcast_atom_pseudo_test
53+
COMMAND ${BASH} bcast_atom_pseudo_test.sh
54+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
55+
)
56+
57+
add_test(NAME cell_bcast_atom_spec_test
58+
COMMAND ${BASH} bcast_atom_spec_test.sh
59+
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
60+
)
61+
5362
add_test(NAME parallel_kpoints_test
5463
COMMAND ${BASH} parallel_kpoints_test.sh
5564
WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
5665
)
57-

source/module_cell/test/atom_pseudo_test.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,13 @@
1212
/**
1313
* - Tested Functions:
1414
* - Atom_pseudo
15+
* - constructor of class Atom_pseudo
1516
* - ~Atom_pseudo
17+
* - deconstructor of class Atom_pseudo
18+
* - set_d_so
19+
* - set spin-orbital info from pseudopotential
20+
* - bcast_atom_pseudo
21+
* - bcast upf201 pp info to other processes
1622
*/
1723

1824
#define private public
@@ -24,7 +30,6 @@ class AtomPseudoTest : public testing::Test
2430
{
2531
protected:
2632
std::unique_ptr<Pseudopot_upf> upf{new Pseudopot_upf};
27-
std::unique_ptr<pseudo_nc> ncpp{new pseudo_nc};
2833
std::unique_ptr<Atom_pseudo> atom_pseudo{new Atom_pseudo};
2934
};
3035

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
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
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
3+
np=`cat /proc/cpuinfo | grep "cpu cores" | uniq| awk '{print $NF}'`
4+
echo "nprocs in this machine is $np"
5+
6+
for i in 4;do
7+
if [[ $i -gt $np ]];then
8+
continue
9+
fi
10+
echo "TEST in parallel, nprocs=$i"
11+
mpirun -np $i ./cell_atom_spec
12+
break
13+
done

0 commit comments

Comments
 (0)