Repository navigation
Expand file tree
/
Copy pathnumerical_basis_test.cpp
More file actions
207 lines (181 loc) · 7.05 KB
/
Copy pathnumerical_basis_test.cpp
File metadata and controls
207 lines (181 loc) · 7.05 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
#include "source_base/math_sphbes.h"
#include "source_base/matrix3.h"
#include "source_base/vector3.h"
#include "source_io/numerical_basis_jyjy.h"
#include "gtest/gtest.h"
using namespace NumericalBasis;
using ModuleBase::Sphbes;
/***********************************************************
* Unit test of class "TwoCenterIntegrator"
***********************************************************/
/**
* Tested functions:
*
* - indexgen
* generates a vector of composite index (itype, iatom, l, m)
* from given number of atoms and maximum angular momentum
*
* - cal_overlap_Sq (static function)
* computes <jy|jy> and <jy|-\nabla^2|jy> with two-center integration
*
*/
class NumericalBasisTest : public ::testing::Test
{
protected:
void SetUp();
void TearDown();
};
void NumericalBasisTest::SetUp()
{
}
void NumericalBasisTest::TearDown()
{
}
TEST_F(NumericalBasisTest, indexgen)
{
const std::vector<int> natom = {1, 2, 3};
const std::vector<int> lmax = {2, 1, 2};
const auto index = indexgen(natom, lmax);
size_t idx = 0;
for (size_t it = 0; it < natom.size(); ++it)
{
for (int ia = 0; ia != natom[it]; ++ia)
{
for (int l = 0; l <= lmax[it]; ++l)
{
for (int M = 0; M < 2 * l + 1; ++M)
{
// convert the "abacus M" to the conventional m
const int m = (M % 2 == 0) ? -M / 2 : (M + 1) / 2;
EXPECT_EQ(index[idx], std::make_tuple(int(it), ia, l, m));
++idx;
}
}
}
}
}
TEST_F(NumericalBasisTest, cal_overlap_Sq)
{
const int lmax = 3;
const int nbes = 7;
const double rcut = 7.0;
const ModuleBase::Matrix3 latvec(30.0, 0.0, 0.0, 0.0, 30.0, 0.0, 0.0, 0.0, 30.0);
// atom-0 and 1 have overlap with each other, but neither of them
// has overlap with atom-2
std::vector<std::vector<ModuleBase::Vector3<double>>> tau_cart;
tau_cart.emplace_back();
tau_cart[0].emplace_back(0.0, 0.0, 0.0);
tau_cart[0].emplace_back(1.0, 1.0, 1.0);
tau_cart[0].emplace_back(0.0, 0.0, 15.0);
const std::vector<int> natom = {int(tau_cart[0].size())};
const std::vector<int> lmax_v = {lmax};
const auto index = indexgen(natom, lmax_v);
const int nao = index.size();
// zeros of sperical Bessel functions
std::vector<double> zeros(nbes * (lmax + 1));
ModuleBase::Sphbes::sphbes_zeros(lmax, nbes, zeros.data(), true);
// <jy|jy> and <jy|-\nabla^2|jy>
const double S_thr = 1e-5;
const double T_thr = 1e-3;
const auto S = cal_overlap_Sq('S', lmax, nbes, rcut, tau_cart, latvec, index);
const auto T = cal_overlap_Sq('T', lmax, nbes, rcut, tau_cart, latvec, index);
int t1, a1, l1, m1, t2, a2, l2, m2; // values will be updated in the loop
for (int i = 0; i < nao; ++i)
{
std::tie(t1, a1, l1, m1) = index[i];
for (int j = 0; j < nao; ++j)
{
std::tie(t2, a2, l2, m2) = index[j];
for (int q1 = 0; q1 < nbes; ++q1)
{
for (int q2 = 0; q2 < nbes; ++q2)
{
if (i == j)
{
if (q1 == q2)
{
// diagonal elements have analytical results
double S_ref
= std::pow(rcut, 3) * 0.5 * std::pow(Sphbes::sphbesj(l1 + 1, zeros[l1 * nbes + q1]), 2);
EXPECT_NEAR(S(i, i, q1, q1).real(), S_ref, S_thr);
EXPECT_EQ(S(i, i, q1, q1).imag(), 0);
double T_ref
= 0.5 * rcut
* std::pow(zeros[l1 * nbes + q1] * Sphbes::sphbesj(l1 + 1, zeros[l1 * nbes + q1]), 2);
EXPECT_NEAR(T(i, i, q1, q1).real(), T_ref, T_thr);
EXPECT_EQ(T(i, i, q1, q1).imag(), 0);
}
else
{
// off-diagonal elements should be zero due to orthogonality
EXPECT_NEAR(S(i, i, q1, q2).real(), 0, S_thr);
EXPECT_EQ(S(i, i, q1, q2).imag(), 0);
EXPECT_NEAR(T(i, i, q1, q2).real(), 0, T_thr);
EXPECT_EQ(T(i, i, q1, q2).imag(), 0);
}
}
if ((a1 == 2) != (a2 == 2))
{
// atom-2 has no overlap with atom-0/1
EXPECT_EQ(S(i, j, q1, q2).real(), 0);
EXPECT_EQ(S(i, j, q1, q2).imag(), 0);
EXPECT_EQ(T(i, j, q1, q2).real(), 0);
EXPECT_EQ(T(i, j, q1, q2).imag(), 0);
}
if (a1 != a2 && a1 != 2 && a2 != 2)
{
// overlap between atom-0 and atom-1 orbitals should be non-zero
EXPECT_NE(S(i, j, q1, q2).real(), 0);
EXPECT_EQ(S(i, j, q1, q2).imag(), 0);
EXPECT_NE(T(i, j, q1, q2).real(), 0);
EXPECT_EQ(T(i, j, q1, q2).imag(), 0);
}
}
}
}
}
// verifies that the calculated matrix is invariant w.r.t. translation
// across the cell boundary
tau_cart[0][0].x -= 0.5;
tau_cart[0][1].x -= 0.5;
tau_cart[0][2].x -= 0.5;
const auto S_wrap = cal_overlap_Sq('S', lmax, nbes, rcut, tau_cart, latvec, index);
for (int i = 0; i < nao; ++i)
{
std::tie(t1, a1, l1, m1) = index[i];
for (int j = 0; j < nao; ++j)
{
std::tie(t2, a2, l2, m2) = index[j];
for (int q1 = 0; q1 < nbes; ++q1)
{
for (int q2 = 0; q2 < nbes; ++q2)
{
EXPECT_EQ(S(i, j, q1, q2), S_wrap(i, j, q1, q2));
}
}
}
}
}
TEST_F(NumericalBasisTest, neighbor_vec)
{
const ModuleBase::Vector3<double> d0(2.5, 0.0, 0.0);
const ModuleBase::Matrix3 latvec(3.0, 0.0, 0.0, 0.0, 4.0, 0.0, 0.0, 0.0, 5.0);
const double r1 = 2.0;
auto d1 = neighbor_vec(d0, latvec, r1);
EXPECT_EQ(d1.size(), 1);
EXPECT_EQ(d1[0], ModuleBase::Vector3<double>(-0.5, 0.0, 0.0));
const double r2 = 3.6;
auto d2 = neighbor_vec(d0, latvec, r2);
EXPECT_EQ(d2.size(), 3);
EXPECT_EQ(d2[0], ModuleBase::Vector3<double>(-3.5, 0.0, 0.0));
EXPECT_EQ(d2[1], ModuleBase::Vector3<double>(-0.5, 0.0, 0.0));
EXPECT_EQ(d2[2], ModuleBase::Vector3<double>(2.5, 0.0, 0.0));
const double r3 = 4.1;
auto d3 = neighbor_vec(d0, latvec, r3);
EXPECT_EQ(d3.size(), 5);
EXPECT_EQ(d3[0], ModuleBase::Vector3<double>(-3.5, 0.0, 0.0));
EXPECT_EQ(d3[1], ModuleBase::Vector3<double>(-0.5, -4.0, 0.0));
EXPECT_EQ(d3[2], ModuleBase::Vector3<double>(-0.5, 0.0, 0.0));
EXPECT_EQ(d3[3], ModuleBase::Vector3<double>(-0.5, 4.0, 0.0));
EXPECT_EQ(d3[4], ModuleBase::Vector3<double>(2.5, 0.0, 0.0));
}