ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
state_ovlp_test.h
Go to the documentation of this file.
1#ifndef ABACUS_LR_STATE_OVLP_TEST_H
2#define ABACUS_LR_STATE_OVLP_TEST_H
3
7#include <gtest/gtest.h>
8#include <algorithm>
9#include <cmath>
10#include <complex>
11#include <vector>
12
13// Independent contraction oracles and distribution helpers for the overlap tests.
14namespace
15{
16template <typename T>
17std::vector<T> overlap_block(const std::vector<T>& full, int bands, int start, int count)
18{
19 std::vector<T> block(count * count);
20 for (int i = 0; i < count; ++i)
21 for (int j = 0; j < count; ++j)
22 block[i * count + j] = full[(start + i) * bands + start + j];
23 return block;
24}
25
26void setup(Parallel_Blkcyclic& target, int rows, int cols, const Parallel_Blkcyclic& grid)
27{
28#ifdef __MPI
29 const int block_size = grid.nb();
30 target.set(rows, cols, block_size, grid.ictxt());
31#else
32 target.set_serial(rows, cols);
33#endif
34}
35
36template <typename T>
37std::vector<T> scatter(const std::vector<T>& full, const Parallel_Blkcyclic& layout)
38{
39 std::vector<T> local(layout.size(), T(0));
40 const int columns = layout.ncglb();
41 for (int col = 0; col < layout.ncloc(); ++col)
42 {
43 const int global_col = layout.icglb(col);
44 for (int row = 0; row < layout.nrloc(); ++row)
45 {
46 const int global_row = layout.irglb(row);
47 local[col * layout.nrloc() + row] = full[global_row * columns + global_col];
48 }
49 }
50 return local;
51}
52
53// Independent test oracles: direct formula contractions, with no BLAS/PBLAS calls.
54template <typename T>
55T conjugate(T value)
56{
57 return value;
58}
59template <>
60std::complex<double> conjugate(std::complex<double> value)
61{
62 return std::conj(value);
63}
64
65template <typename T>
66std::vector<T> reference_mo(const std::vector<T>& old, const std::vector<double>& ao,
67 const std::vector<T>& current, int naos, int bands)
68{
69 std::vector<T> result(bands * bands, T(0));
70 for (int i = 0; i < bands; ++i)
71 {
72 for (int j = 0; j < bands; ++j)
73 {
74 for (int mu = 0; mu < naos; ++mu)
75 {
76 for (int nu = 0; nu < naos; ++nu)
77 {
78 result[i * bands + j] += conjugate(old[i * naos + mu])
79 * ao[mu * naos + nu] * current[j * naos + nu];
80 }
81 }
82 }
83 }
84 return result;
85}
86
87template <typename T>
88std::vector<T> reference_projection(const std::vector<T>& previous,
89 const std::vector<T>& overlap, int no, int nv)
90{
91 const int bands = no + nv;
92 std::vector<T> result(no * nv, T(0));
93 for (int j = 0; j < no; ++j)
94 {
95 for (int b = 0; b < nv; ++b)
96 {
97 for (int i = 0; i < no; ++i)
98 {
99 for (int a = 0; a < nv; ++a)
100 {
101 result[j * nv + b] += overlap[i * bands + j] * previous[i * nv + a]
102 * conjugate(overlap[(no + a) * bands + no + b]);
103 }
104 }
105 }
106 }
107 return result;
108}
109
110template <typename T>
111std::vector<T> gather(const std::vector<T>& local, const Parallel_Blkcyclic& layout)
112{
113 const int columns = layout.ncglb();
114 const int count = layout.nrglb() * columns;
115 std::vector<T> result(count, T(0));
116 EXPECT_EQ(local.size(), layout.size());
117 for (int col = 0; col < layout.ncloc(); ++col)
118 {
119 const int global_col = layout.icglb(col);
120 for (int row = 0; row < layout.nrloc(); ++row)
121 {
122 const int global_row = layout.irglb(row);
123 result[global_row * columns + global_col] = local[col * layout.nrloc() + row];
124 }
125 }
126 Parallel_Reduce::reduce_all(result.data(), count);
127 return result;
128}
129
130template <typename T>
131std::vector<T> transform_for_test(const std::vector<T>& old, const std::vector<double>& ao,
132 const std::vector<T>& current, int naos, int bands, int start, int count, int block_size)
133{
135#ifdef __MPI
136 pc.init(naos, bands, block_size, MPI_COMM_WORLD, false);
137#else
138 pc.set_serial(naos, bands);
139#endif
142 setup(ps, naos, naos, pc);
143 setup(po, count, count, pc);
144 std::vector<T> old_rows(old.size());
145 std::vector<T> current_rows(current.size());
146 for (int i = 0; i < naos; ++i)
147 {
148 for (int band = 0; band < bands; ++band)
149 {
150 old_rows[i * bands + band] = old[band * naos + i];
151 current_rows[i * bands + band] = current[band * naos + i];
152 }
153 }
154 const std::vector<T> typed_ao(ao.begin(), ao.end());
155 const std::vector<T> local_old = scatter(old_rows, pc);
156 const std::vector<T> local_current = scatter(current_rows, pc);
157 const std::vector<T> local_ao = scatter(typed_ao, ps);
158 const std::vector<T> actual = LR::mo_overlap_dist(local_old, local_ao, local_current, pc, ps, po, start);
159 return gather(actual, po);
160}
161
162template <typename T>
163std::vector<T> project(const std::vector<T>& previous, const std::vector<T>& overlap, int no, int nv)
164{
165 const int bands = no + nv;
167#ifdef __MPI
168 po.init(bands, bands, 1, MPI_COMM_WORLD, false);
169#else
170 po.set_serial(bands, bands);
171#endif
173 setup(px, nv, no, po);
174 std::vector<T> previous_rows(previous.size());
175 for (int i = 0; i < no; ++i)
176 {
177 for (int a = 0; a < nv; ++a)
178 {
179 previous_rows[a * no + i] = previous[i * nv + a];
180 }
181 }
182 const std::vector<T> local_previous = scatter(previous_rows, px);
185 setup(pi, no, no, po);
186 setup(pv, nv, nv, po);
187 const std::vector<T> occupied = overlap_block(overlap, bands, 0, no);
188 const std::vector<T> virtuals = overlap_block(overlap, bands, no, nv);
189 const std::vector<T> local_occupied = scatter(occupied, pi);
190 const std::vector<T> local_virtuals = scatter(virtuals, pv);
191 const std::vector<T> actual = LR::project_reference_dist(local_previous, local_occupied, local_virtuals, pi, pv, px);
192 const std::vector<T> rows = gather(actual, px);
193 std::vector<T> result(previous.size());
194 for (int i = 0; i < no; ++i)
195 {
196 for (int a = 0; a < nv; ++a)
197 {
198 result[i * nv + a] = rows[a * no + i];
199 }
200 }
201 return result;
202}
203
204template <typename T>
205void check_mo(int naos, int bands, T phase, int start, int count, int block_size)
206{
207 std::vector<double> ao(naos * naos);
208 std::vector<T> old(naos * bands);
209 std::vector<T> current(naos * bands);
210 for (int i = 0; i < naos; ++i)
211 {
212 for (int j = 0; j < naos; ++j)
213 {
214 ao[i * naos + j] = (2 * i - j + 1) / 7.0;
215 }
216 for (int band = 0; band < bands; ++band)
217 {
218 old[band * naos + i] = phase * ((i + band + 1) / 11.0);
219 current[band * naos + i] = T((i - 2 * band + 2) / 13.0);
220 }
221 }
222 const std::vector<T> actual = transform_for_test(old, ao, current, naos, bands, start, count, block_size);
223 const std::vector<T> full = reference_mo(old, ao, current, naos, bands);
224 const std::vector<T> expected = overlap_block(full, bands, start, count);
225 ASSERT_EQ(actual.size(), expected.size());
226 for (std::size_t i = 0; i < actual.size(); ++i)
227 {
228 EXPECT_NEAR(std::abs(actual[i] - expected[i]), 0.0, 1e-13);
229 }
230}
231template <typename T>
232void check_projection(int no, int nv, T phase, int block_size)
233{
234 const int bands = no + nv;
236#ifdef __MPI
237 po.init(bands, bands, block_size, MPI_COMM_WORLD, false);
238#else
239 po.set_serial(bands, bands);
240#endif
242 setup(px, nv, no, po);
243 std::vector<T> overlap(bands * bands);
244 std::vector<T> previous(no * nv);
245 for (int i = 0; i < bands; ++i)
246 {
247 for (int j = 0; j < bands; ++j)
248 {
249 overlap[i * bands + j] = T((i - 2 * j + 1) / 17.0) + phase * ((i + j + 1) / 19.0);
250 }
251 }
252 for (int i = 0; i < no * nv; ++i)
253 {
254 previous[i] = phase * ((i + 1) / 23.0);
255 }
256 // Convert occupied-major X into row-major A=X^T before scattering into px.
257 std::vector<T> previous_rows(previous.size());
258 for (int i = 0; i < no; ++i)
259 {
260 for (int a = 0; a < nv; ++a)
261 {
262 previous_rows[a * no + i] = previous[i * nv + a];
263 }
264 }
265 const std::vector<T> local_previous = scatter(previous_rows, px);
268 setup(pi, no, no, po);
269 setup(pv, nv, nv, po);
270 const std::vector<T> occupied = overlap_block(overlap, bands, 0, no);
271 const std::vector<T> virtuals = overlap_block(overlap, bands, no, nv);
272 const std::vector<T> local_occupied = scatter(occupied, pi);
273 const std::vector<T> local_virtuals = scatter(virtuals, pv);
274 const std::vector<T> actual = LR::project_reference_dist(local_previous, local_occupied, local_virtuals, pi, pv, px);
275 const std::vector<T> expected = reference_projection(previous, overlap, no, nv);
276 std::vector<T> expected_rows(expected.size());
277 for (int i = 0; i < no; ++i)
278 {
279 for (int a = 0; a < nv; ++a)
280 {
281 expected_rows[a * no + i] = expected[i * nv + a];
282 }
283 }
284 const std::vector<T> local_expected = scatter(expected_rows, px);
285 ASSERT_EQ(actual.size(), local_expected.size());
286 for (std::size_t i = 0; i < actual.size(); ++i)
287 {
288 // Keep the original unit-block tolerance; larger new oracle contractions
289 // allow roundoff proportional to the independently computed result.
290 const double scaled_tolerance = 1e-12 * std::max(1.0, std::abs(local_expected[i]));
291 const double tolerance = block_size == 1 ? 1e-13 : scaled_tolerance;
292 EXPECT_NEAR(std::abs(actual[i] - local_expected[i]), 0.0, tolerance);
293 }
294}
295
296}
297
298#endif
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
int irglb(const int irloc) const
Definition parallel_blkcyc.h:88
int ncloc() const
Definition parallel_blkcyc.h:40
int set(const int nrglb, const int ncglb, const int nb, const int ictxt)
Definition parallel_blkcyc.cpp:230
int ncglb() const
Definition parallel_blkcyc.cpp:86
int nrloc() const
Definition parallel_blkcyc.h:35
int icglb(const int icloc) const
Definition parallel_blkcyc.h:93
int nb() const
Definition parallel_blkcyc.h:53
int ictxt() const
Definition parallel_blkcyc.h:130
std::size_t size() const
Definition parallel_blkcyc.h:48
int nrglb() const
Definition parallel_blkcyc.cpp:75
void set_serial(const int nrglb, const int ncglb)
Definition parallel_blkcyc.cpp:239
int init(const int nrglb, const int ncglb, const int nb, const MPI_Comm comm, const bool swap_grid)
Definition parallel_blkcyc.cpp:217
#define T
Definition exp.cpp:237
std::vector< T > project_reference_dist(const std::vector< T > &previous, const std::vector< T > &occupied_overlap, const std::vector< T > &virtual_overlap, const Parallel_Blkcyclic &pi, const Parallel_Blkcyclic &pv, const Parallel_Blkcyclic &px)
Definition state_ovlp.cpp:232
std::vector< T > mo_overlap_dist(const std::vector< T > &old_coefficients, const std::vector< T > &ao, const std::vector< T > &current_coefficients, const Parallel_Blkcyclic &pc, const Parallel_Blkcyclic &ps, const Parallel_Blkcyclic &po, const int first_band)
Definition state_ovlp.cpp:159
int4 i[880]
Definition sincos.cpp:28
void reduce_all(T &object)
reduce in all process
Definition depend_mock.cpp:14
str project
Definition conf.py:22
@ no
Definition operator.h:13
iclock::time_point start
Definition test_partition.cpp:22
const double pi
Definition test_radial.cpp:22