Repository navigation
Expand file tree
/
Copy pathunitcell_test_para.cpp
More file actions
288 lines (276 loc) · 8.21 KB
/
Copy pathunitcell_test_para.cpp
File metadata and controls
288 lines (276 loc) · 8.21 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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#define private public
#include "source_io/module_parameter/parameter.h"
#undef private
#include "memory"
#include "source_base/global_variable.h"
#include "source_base/mathzone.h"
#include "source_cell/unitcell.h"
#include "source_estate/read_pseudo.h"
#include <valarray>
#include <vector>
#ifdef __MPI
#include "mpi.h"
#endif
#include "prepare_unitcell.h"
#include "../update_cell.h"
#include "../bcast_cell.h"
#ifdef __LCAO
InfoNonlocal::InfoNonlocal()
{
}
InfoNonlocal::~InfoNonlocal()
{
}
#endif
Magnetism::Magnetism()
{
this->tot_mag = 0.0;
this->abs_mag = 0.0;
this->start_mag = nullptr;
}
Magnetism::~Magnetism()
{
delete[] this->start_mag;
}
#define private public
#include "source_io/module_parameter/parameter.h"
#undef private
/************************************************
* unit test of class UnitCell
***********************************************/
/**
* - Tested Functions:
* - UpdatePosTaud
* - update_pos_tau(double* pos)
* - update_pos_taud(const double* pos)
* - bcast_atoms_tau() is also called in the above function, which calls Atom::bcast_atom with many
* atomic info in addition to tau
* - BcastUnitcell
* - bcast basic info of unitcell and basic info of atoms
* - BcastUnitcell2
* - calls bcast_atoms2() to bcast atomic pseudo info
* - ReadPseudo
* - read_pseudo()
*/
// mock function
#ifdef __LCAO
void LCAO_Orbitals::bcast_files(const int& ntype_in, const int& my_rank)
{
return;
}
#endif
class UcellTest : public ::testing::Test
{
protected:
UcellTestPrepare utp = UcellTestLib["C1H2-Read"];
std::unique_ptr<UnitCell> ucell;
std::ofstream ofs;
std::string pp_dir;
std::string output;
void SetUp()
{
ofs.open("running.log");
PARAM.input.relax_new = utp.relax_new;
PARAM.sys.global_out_dir = "./";
ucell = utp.SetUcellInfo();
PARAM.input.lspinorb = false;
pp_dir = "./support/";
PARAM.input.pseudo_rcut = 15.0;
PARAM.input.dft_functional = "default";
PARAM.input.test_pseudo_cell = 1;
PARAM.input.nspin = 1;
PARAM.input.basis_type = "pw";
}
void TearDown()
{
ofs.close();
}
};
#ifdef __MPI
TEST_F(UcellTest, BcastUnitcell)
{
PARAM.input.nspin = 4;
unitcell::bcast_unitcell(*ucell);
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, BcastLattice)
{
unitcell::bcast_Lattice(ucell->lat);
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, BcastMagnitism)
{
unitcell::bcast_magnetism(ucell->magnet, ucell->ntype);
PARAM.input.nspin = 4;
if (GlobalV::MY_RANK != 0)
{
EXPECT_DOUBLE_EQ(ucell->magnet.start_mag[0], 0.0);
EXPECT_DOUBLE_EQ(ucell->magnet.start_mag[1], 0.0);
for (int i = 0; i < 3; ++i)
{
EXPECT_DOUBLE_EQ(ucell->magnet.ux_[i], 0.0);
}
}
}
TEST_F(UcellTest, UpdatePosTau)
{
double* pos_in = new double[ucell->nat * 3];
ucell->set_iat2itia();
std::fill(pos_in, pos_in + ucell->nat * 3, 0);
for (int iat = 0; iat < ucell->nat; ++iat)
{
int it, ia;
ucell->iat2iait(iat, &ia, &it);
for (int ik = 0; ik < 3; ++ik)
{
ucell->atoms[it].mbl[ia][ik] = true;
pos_in[iat * 3 + ik] = (iat * 3 + ik) / (ucell->nat * 3.0) * (ucell->lat.lat0);
}
}
unitcell::update_pos_tau(ucell->lat,pos_in,ucell->ntype,ucell->nat,ucell->atoms);
for (int iat = 0; iat < ucell->nat; ++iat)
{
int it, ia;
ucell->iat2iait(iat, &ia, &it);
for (int ik = 0; ik < 3; ++ik)
{
EXPECT_DOUBLE_EQ(ucell->atoms[it].tau[ia][ik],
(iat*3+ik)/(ucell->nat*3.0));
}
}
delete[] pos_in;
}
TEST_F(UcellTest, UpdatePosTaud_pointer)
{
double* pos_in = new double[ucell->nat * 3];
ModuleBase::Vector3<double>* tmp = new ModuleBase::Vector3<double>[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];
}
unitcell::update_pos_taud(ucell->lat,pos_in,ucell->ntype,
ucell->nat,ucell->atoms);
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[] tmp;
delete[] pos_in;
}
//test update_pos_taud with ModuleBase::Vector3<double> version
TEST_F(UcellTest, UpdatePosTaud_Vector3)
{
ModuleBase::Vector3<double>* pos_in = new ModuleBase::Vector3<double>[ucell->nat];
ModuleBase::Vector3<double>* tmp = new ModuleBase::Vector3<double>[ucell->nat];
ucell->set_iat2itia();
for (int iat = 0; iat < ucell->nat; ++iat)
{
for (int ik = 0; ik < 3; ++ik)
{
pos_in[iat][ik] = 0.01;
}
int it=0;
int ia=0;
ucell->iat2iait(iat, &ia, &it);
tmp[iat] = ucell->atoms[it].taud[ia];
}
unitcell::update_pos_taud(ucell->lat,pos_in,ucell->ntype,
ucell->nat,ucell->atoms);
for (int iat = 0; iat < ucell->nat; ++iat)
{
int it, ia;
ucell->iat2iait(iat, &ia, &it);
for (int ik = 0; ik < 3; ++ik)
{
EXPECT_DOUBLE_EQ(ucell->atoms[it].taud[ia][ik], tmp[iat][ik] + 0.01);
}
}
delete[] tmp;
delete[] pos_in;
}
TEST_F(UcellTest, ReadPseudo)
{
PARAM.input.pseudo_dir = pp_dir;
PARAM.input.out_element_info = true;
elecstate::read_pseudo(ofs, *ucell);
// 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
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)
{
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);
int result = RUN_ALL_TESTS();
MPI_Finalize();
return result;
}
#endif