Skip to content

Commit adb231a

Browse files
authored
Test: add UT for class atom_pseudo (#1919)
1 parent e5ee79a commit adb231a

File tree

4 files changed

+122
-7
lines changed

4 files changed

+122
-7
lines changed

source/module_cell/test/CMakeLists.txt

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
remove_definitions(-D__LCAO)
21
remove_definitions(-D__DEEPKS)
32
remove_definitions(-D__CUDA)
43
remove_definitions(-D__ROCM)
54

5+
install(DIRECTORY support DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
6+
67
AddTest(
78
TARGET cell_read_pp
89
LIBS ${math_libs} base device
@@ -16,6 +17,20 @@ AddTest(
1617

1718
)
1819

20+
AddTest(
21+
TARGET cell_atom_pseudo
22+
LIBS ${math_libs} base device
23+
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 ../../src_parallel/parallel_reduce.cpp ../../src_parallel/parallel_kpoints.cpp ../../src_parallel/parallel_global.cpp ../../src_parallel/parallel_common.cpp ../../module_io/output.cpp
24+
)
25+
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}
32+
)
33+
1934
AddTest(
2035
TARGET klist_test
2136
LIBS ${math_libs} base device
@@ -25,6 +40,3 @@ AddTest(
2540
../../module_io/output.cpp ../module_symmetry/symm_other.cpp
2641
../module_symmetry/symmetry_basic.cpp ../module_symmetry/symmetry.cpp
2742
)
28-
29-
install(DIRECTORY support DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
30-
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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_pseudo
15+
* - ~Atom_pseudo
16+
*/
17+
18+
#define private public
19+
#include "module_cell/read_pp.h"
20+
#include "module_cell/pseudo_nc.h"
21+
#include "module_cell/atom_pseudo.h"
22+
23+
class AtomPseudoTest : public testing::Test
24+
{
25+
protected:
26+
std::unique_ptr<Pseudopot_upf> upf{new Pseudopot_upf};
27+
std::unique_ptr<pseudo_nc> ncpp{new pseudo_nc};
28+
std::unique_ptr<Atom_pseudo> atom_pseudo{new Atom_pseudo};
29+
};
30+
31+
TEST_F(AtomPseudoTest, SetDSo)
32+
{
33+
std::ifstream ifs;
34+
ifs.open("./support/C.upf");
35+
GlobalV::PSEUDORCUT = 15.0;
36+
upf->read_pseudo_upf201(ifs);
37+
atom_pseudo->set_pseudo_nc(*upf);
38+
ifs.close();
39+
EXPECT_EQ(atom_pseudo->nh,14);
40+
EXPECT_TRUE(atom_pseudo->has_so);
41+
ModuleBase::ComplexMatrix d_so_in(atom_pseudo->nh*2,atom_pseudo->nh*2);
42+
int nproj = 6;
43+
int nproj_soc = 4;
44+
bool has_so = 1;
45+
GlobalV::NSPIN = 4;
46+
atom_pseudo->set_d_so(d_so_in,nproj,nproj_soc,has_so);
47+
EXPECT_NEAR(atom_pseudo->d_so(0,0,0).real(),1e-8,1e-7);
48+
EXPECT_NEAR(atom_pseudo->d_so(0,0,0).imag(),1e-8,1e-7);
49+
GlobalV::LSPINORB = 1;
50+
atom_pseudo->set_d_so(d_so_in,nproj,nproj_soc,has_so);
51+
EXPECT_NEAR(atom_pseudo->d_so(0,0,0).real(),1e-8,1e-7);
52+
EXPECT_NEAR(atom_pseudo->d_so(0,0,0).imag(),1e-8,1e-7);
53+
}
54+
55+
#ifdef __MPI
56+
TEST_F(AtomPseudoTest, BcastAtomPseudo)
57+
{
58+
if(GlobalV::MY_RANK==0)
59+
{
60+
std::ifstream ifs;
61+
ifs.open("./support/C.upf");
62+
GlobalV::PSEUDORCUT = 15.0;
63+
upf->read_pseudo_upf201(ifs);
64+
atom_pseudo->set_pseudo_nc(*upf);
65+
ifs.close();
66+
}
67+
atom_pseudo->bcast_atom_pseudo();
68+
if(GlobalV::MY_RANK!=0)
69+
{
70+
EXPECT_EQ(atom_pseudo->nbeta,6);
71+
EXPECT_EQ(atom_pseudo->nchi,3);
72+
EXPECT_DOUBLE_EQ(atom_pseudo->rho_atc[0],8.7234550809E-01);
73+
}
74+
}
75+
76+
int main(int argc, char **argv)
77+
{
78+
MPI_Init(&argc, &argv);
79+
testing::InitGoogleTest(&argc, argv);
80+
81+
MPI_Comm_size(MPI_COMM_WORLD,&GlobalV::NPROC);
82+
MPI_Comm_rank(MPI_COMM_WORLD,&GlobalV::MY_RANK);
83+
int result = RUN_ALL_TESTS();
84+
85+
MPI_Finalize();
86+
87+
return result;
88+
}
89+
#endif
90+
#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_pseudo
12+
break
13+
done

source/module_cell/test/klist_test.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
#include<iostream>
12
#include "gtest/gtest.h"
3+
#define private public
4+
#include "../klist.h"
25
#include "module_elecstate/magnetism.h"
36
#include "module_hamilt_pw/hamilt_pwdft/VNL_in_pw.h"
47
#include "module_cell/atom_pseudo.h"
@@ -10,7 +13,6 @@
1013
#include "src_parallel/parallel_kpoints.h"
1114
#include "module_io/berryphase.h"
1215
#include "module_orbital/ORB_gaunt_table.h"
13-
#include<iostream>
1416

1517
bool berryphase::berry_phase_flag=0;
1618

@@ -55,8 +57,6 @@ namespace GlobalC
5557
* - setup kup and kdw after vc (different spin cases)
5658
*/
5759

58-
#define private public
59-
#include "module_cell/klist.h"
6060

6161
class KlistTest : public testing::Test
6262
{

0 commit comments

Comments
 (0)