Repository navigation
Expand file tree
/
Copy pathorb_io_test.cpp
More file actions
162 lines (146 loc) · 3.77 KB
/
Copy pathorb_io_test.cpp
File metadata and controls
162 lines (146 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <gtest/gtest.h>
#include "source_io/orb_io.h"
#ifdef __MPI
#include <mpi.h>
#endif
#include "source_base/constants.h"
#include "source_base/global_variable.h"
class OrbIOTest : public testing::Test
{
protected:
void SetUp();
void TearDown(){};
const std::string file = "../../../../tests/PP_ORB/Ti_gga_10au_100Ry_4s2p2d1f.orb";
const double tol = 1e-12;
};
void OrbIOTest::SetUp()
{
#ifdef __MPI
MPI_Comm_rank(MPI_COMM_WORLD, &GlobalV::MY_RANK);
#endif
}
TEST_F(OrbIOTest, ReadAbacusOrb)
{
std::ifstream ifs;
std::string elem;
double ecut, dr;
int nr;
std::vector<int> nzeta;
std::vector<std::vector<double>> radials;
if (GlobalV::MY_RANK == 0)
{
ifs.open(file);
}
ModuleIO::read_abacus_orb(ifs, elem, ecut, nr, dr, nzeta, radials, GlobalV::MY_RANK);
if (GlobalV::MY_RANK == 0)
{
ifs.close();
}
EXPECT_EQ(elem, "Ti");
EXPECT_DOUBLE_EQ(ecut, 100.0);
EXPECT_EQ(nr, 1001);
EXPECT_DOUBLE_EQ(dr, 0.01);
EXPECT_EQ(nzeta.size(), 4); // l from 0 to 3
EXPECT_EQ(nzeta[0], 4);
EXPECT_EQ(nzeta[1], 2);
EXPECT_EQ(nzeta[2], 2);
EXPECT_EQ(nzeta[3], 1);
EXPECT_EQ(radials.size(), 9); // 4 + 2 + 2 + 1
for(auto& radial: radials)
{
EXPECT_EQ(radial.size(), 1001);
}
EXPECT_EQ(radials[0][0], -1.581711853170e-01);
EXPECT_EQ(radials[0][4], -1.583907030513e-01);
EXPECT_EQ(radials[0][996], -4.183526380009e-05);
EXPECT_EQ(radials[0][1000], 0);
EXPECT_EQ(radials[3][0], -1.166292682541e+00);
EXPECT_EQ(radials[3][4], -1.164223359672e+00);
EXPECT_EQ(radials[3][996], -3.183325576529e-04);
EXPECT_EQ(radials[3][1000], 0);
EXPECT_EQ(radials[8][0], 0);
EXPECT_EQ(radials[8][4], 3.744878535962e-05);
EXPECT_EQ(radials[8][996], 7.495357740660e-05);
EXPECT_EQ(radials[8][1000], 0);
}
TEST_F(OrbIOTest, WriteAbacusOrb)
{
std::ifstream ifs;
std::string elem;
double ecut, dr;
int nr;
std::vector<int> nzeta;
std::vector<std::vector<double>> radials;
if (GlobalV::MY_RANK == 0)
{
ifs.open(file);
}
ModuleIO::read_abacus_orb(ifs, elem, ecut, nr, dr, nzeta, radials, GlobalV::MY_RANK);
if (GlobalV::MY_RANK == 0)
{
ifs.close();
}
const std::string ftmp = "tmp.orb";
std::ofstream ofs;
if (GlobalV::MY_RANK == 0)
{
ofs.open(ftmp);
}
ModuleIO::write_abacus_orb(ofs, elem, ecut, nr, dr, nzeta, radials, GlobalV::MY_RANK);
if (GlobalV::MY_RANK == 0)
{
ofs.close();
}
#ifdef __MPI
MPI_Barrier(MPI_COMM_WORLD);
#endif
std::ifstream ifs1;
std::string elem1;
double ecut1, dr1;
int nr1;
std::vector<int> nzeta1;
std::vector<std::vector<double>> radials1;
if (GlobalV::MY_RANK == 0)
{
ifs1.open(ftmp);
}
ModuleIO::read_abacus_orb(ifs1, elem1, ecut1, nr1, dr1, nzeta1, radials1, GlobalV::MY_RANK);
if (GlobalV::MY_RANK == 0)
{
ifs1.close();
}
EXPECT_EQ(elem, elem1);
EXPECT_DOUBLE_EQ(ecut, ecut1);
EXPECT_EQ(nr, nr1);
EXPECT_DOUBLE_EQ(dr, dr1);
EXPECT_EQ(nzeta.size(), nzeta1.size());
for (int i = 0; i < nzeta.size(); ++i)
{
EXPECT_EQ(nzeta[i], nzeta1[i]);
}
EXPECT_EQ(radials.size(), radials1.size());
for (int i = 0; i < radials.size(); ++i)
{
EXPECT_EQ(radials[i].size(), radials1[i].size());
for (int j = 0; j < radials[i].size(); ++j)
{
EXPECT_NEAR(radials[i][j], radials1[i][j], tol);
}
}
if (GlobalV::MY_RANK == 0)
{
remove(ftmp.c_str());
}
}
int main(int argc, char** argv)
{
#ifdef __MPI
MPI_Init(&argc, &argv);
#endif
testing::InitGoogleTest(&argc, argv);
int result = RUN_ALL_TESTS();
#ifdef __MPI
MPI_Finalize();
#endif
return result;
}