Repository navigation
Expand file tree
/
Copy pathesolver_dp_test.cpp
More file actions
177 lines (154 loc) · 4.34 KB
/
Copy pathesolver_dp_test.cpp
File metadata and controls
177 lines (154 loc) · 4.34 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
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "for_test.h"
#define private public
#define protected public
#include "../esolver_dp.h"
#undef private
/************************************************
* unit tests of class ESolver_DP
***********************************************/
/**
* - Tested Functions:
* - ESolver_DP::init()
* - ESolver_DP::run()
* - ESolver_DP::cal_energy()
* - ESolver_DP::cal_force()
* - ESolver_DP::cal_stress()
* - ESolver_DP::post_process()
* - ESolver_DP::type_map()
*/
class ESolverDPTest : public ::testing::Test
{
protected:
void SetUp() override
{
// Initialize variables before each test
esolver = new ModuleESolver::ESolver_DP("./support/case_1.pb");
ucell.iat2it = new int[2];
ucell.iat2it[0] = 0;
ucell.iat2it[1] = 1;
ucell.iat2ia = new int[2];
ucell.iat2ia[0] = 0;
ucell.iat2ia[1] = 1;
ucell.nat = 2;
ucell.ntype = 2;
ucell.atoms = new Atom[2];
ucell.atoms[0].na = 1;
ucell.atoms[1].na = 1;
ucell.atoms[0].taud.resize(1, ModuleBase::Vector3<double>(0.0, 0.0, 0.0));
ucell.atoms[1].taud.resize(1, ModuleBase::Vector3<double>(0.0, 0.0, 0.0));
ucell.atom_label.resize(ucell.ntype);
ucell.atom_label[0] = "Cu";
ucell.atom_label[1] = "Al";
esolver->before_all_runners(ucell, inp);
}
void TearDown() override
{
// Clean up after each test
delete esolver;
delete[] ucell.atoms;
}
ModuleESolver::ESolver_DP* esolver;
Input_para inp;
UnitCell ucell;
};
// Test the Init() funciton case 1
TEST_F(ESolverDPTest, InitCase1)
{
// Check the initialized variables
EXPECT_DOUBLE_EQ(esolver->dp_potential, 0.0);
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
EXPECT_DOUBLE_EQ(esolver->dp_virial(i, j), 0.0);
}
}
for (int i = 0; i < ucell.nat; ++i)
{
for (int j = 0; j < 3; ++j)
{
EXPECT_DOUBLE_EQ(esolver->dp_force(i, j), 0.0);
}
}
EXPECT_EQ(esolver->atype[0], 0);
EXPECT_EQ(esolver->atype[1], 0);
}
// Test the Run() funciton WARNING_QUIT
TEST_F(ESolverDPTest, RunWarningQuit)
{
int istep = 0;
testing::internal::CaptureStdout();
EXPECT_EXIT(esolver->runner(ucell, istep), ::testing::ExitedWithCode(1), "");
std::string output = testing::internal::GetCapturedStdout();
EXPECT_THAT(output, testing::HasSubstr("Please recompile with -D__DPMD"));
}
// Test the cal_energy() funciton
TEST_F(ESolverDPTest, CalEnergy)
{
double etot = 0.0;
esolver->dp_potential = 9.8;
etot = esolver->cal_energy();
// Check the results
EXPECT_DOUBLE_EQ(etot, 9.8);
}
// Test the cal_Force() funciton
TEST_F(ESolverDPTest, CalForce)
{
ModuleBase::matrix force(ucell.nat, 3);
for (int i = 0; i < ucell.nat; ++i)
{
for (int j = 0; j < 3; ++j)
{
esolver->dp_force(i, j) = 3.0 * i + j;
}
}
esolver->cal_force(ucell, force);
// Check the results
for (int i = 0; i < ucell.nat; ++i)
{
for (int j = 0; j < 3; ++j)
{
EXPECT_DOUBLE_EQ(force(i, j), 3.0 * i + j);
}
}
}
// Test the cal_Stress() funciton
TEST_F(ESolverDPTest, CalStress)
{
ModuleBase::matrix stress(3, 3);
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
esolver->dp_virial(i, j) = 3.0 * i + j;
}
}
esolver->cal_stress(ucell, stress);
// Check the results
for (int i = 0; i < 3; ++i)
{
for (int j = 0; j < 3; ++j)
{
EXPECT_DOUBLE_EQ(stress(i, j), 3.0 * i + j);
}
}
}
// Test the postprocess() funciton
TEST_F(ESolverDPTest, Postprocess)
{
esolver->dp_potential = 9.8;
// Check the results
GlobalV::ofs_running.open("log");
esolver->after_all_runners(ucell);
GlobalV::ofs_running.close();
std::string expected_output
= "\n --------------------------------------------\n !FINAL_ETOT_IS 133.3358404000000235 eV\n "
"--------------------------------------------\n\n\n";
std::ifstream ifs("log");
std::string output((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
ifs.close();
std::remove("log");
EXPECT_EQ(expected_output, output);
}