ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
ri_benchmark.hpp
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_RI_BENCHMARK_RI_BENCHMARK_HPP
2#define ABACUS_SOURCE_LCAO_MODULE_LR_RI_BENCHMARK_RI_BENCHMARK_HPP
3
4#include <algorithm>
5#include <dirent.h>
6#include "ri_benchmark.h"
8#include "source_psi/psi.h"
10namespace RI_Benchmark
11{
12 // std::cout << "the size of Cs:" << std::endl;
13 // for (auto& it1: Cs)
14 // {
15 // for (auto& it2: it1.second)
16 // {
17 // std::cout << "iat1=" << it1.first << ", iat2= " << it2.first.first << std::endl;
18 // auto& ts = it2.second.shape;
19 // std::cout << "Tensor shape:" << ts[0] << " " << ts[1] << " " << ts[2] << std::endl; // abf, nw1, nw2
20 // }
21 // }
22
23 template <typename TR>
24 inline int count_nao_from_Cs(const TLRI<TR>& Cs_ao)
25 {
26 int naos = 0;
27 for (const auto& it2 : Cs_ao.at(0))
28 {
29 assert(it2.second.shape.size() == 3);
30 naos += it2.second.shape[2];
31 }
32 return naos;
33 }
34
37 template <typename TK>
38 inline std::vector<TK> slice_psi(const psi::Psi<TK>& wfc_ks,
39 const int& ibstart,
40 const int& nb,
41 const int& iwstart,
42 const int& nw)
43 {
44 std::vector<TK> psi_slice(wfc_ks.get_nk() * nb * nw);
45 int i = 0;
46 for (int ik = 0; ik < wfc_ks.get_nk(); ++ik)
47 for (int ib = ibstart; ib < ibstart + nb; ++ib)
48 for (int iw = iwstart; iw < iwstart + nw; ++iw)
49 psi_slice[i++] = wfc_ks(ik, ib, iw);
50 assert(i == psi_slice.size());
51 return psi_slice;
52 }
53
54 template <typename TK, typename TR>
56 const TLRI<TR>& Cs_ao,
57 const psi::Psi<TK>& wfc_ks,
58 const int& nocc,
59 const int& nvirt,
60 const int& occ_first)
61 {
62 // assert(wfc_ks.get_nk() == 1); // currently only gamma-only is supported
63 assert(nocc + nvirt <= wfc_ks.get_nbands());
64 TLRI<TK> Cs_mo;
65 int iw1 = 0;
66 for (auto& c1 : Cs_ao)
67 {
68 const int& iat1 = c1.first;
69 const int& it1 = ucell.iat2it[iat1];
70 const int& nw1 = ucell.atoms[it1].nw;
71 assert(iw1 == ucell.get_iat2iwt()[iat1]);
72 int iw2 = 0;
73 for (auto& c2 : c1.second)
74 {
75 const int& iat2 = c2.first.first;
76 const int& it2 = ucell.iat2it[iat2];
77 const int& nw2 = ucell.atoms[it2].nw;
78 assert(iw2 == ucell.get_iat2iwt()[iat2]);
79
80 const auto& tensor_ao = c2.second;
81 const size_t& nabf = tensor_ao.shape[0];
82 assert(tensor_ao.shape.size() == 3); // abf, nw1, nw2
83
84 std::vector<TK> psi_a1 = occ_first ? slice_psi(wfc_ks, 0, nocc, iw1, nw1) //occ
85 : slice_psi(wfc_ks, nocc, nvirt, iw1, nw1); // virt
86 std::vector<TK> psi_a2 = occ_first ? slice_psi(wfc_ks, nocc, nvirt, iw2, nw2) //virt
87 : slice_psi(wfc_ks, 0, nocc, iw2, nw2); // occ
88
89 Cs_mo[c1.first][c2.first] = RI::Tensor<TK>({ nabf, (std::size_t)nocc, (std::size_t)nvirt });
90 for (int iabf = 0; iabf < nabf; ++iabf)
91 {
92 const auto ptr = &tensor_ao(iabf, 0, 0);
93 std::vector<TK> tmp(nw1 * (occ_first ? nvirt : nocc));
94 // caution: Cs are row-major (ia2 contiguous)
95 if (occ_first)
96 {
97 container::BlasConnector::gemm('T', 'N', nvirt, nw1, nw2, 1.0, psi_a2.data(), nw2, ptr, nw2, 0.0, tmp.data(), nvirt);
98 container::BlasConnector::gemm('N', 'N', nvirt, nocc, nw1, 1.0, tmp.data(), nvirt, psi_a1.data(), nw1, 0.0, &Cs_mo[c1.first][c2.first](iabf, 0, 0), nvirt);
99 }
100 else
101 {
102 container::BlasConnector::gemm('T', 'N', nw1, nocc, nw2, 1.0, ptr, nw2, psi_a2.data(), nw2, 0.0, tmp.data(), nw1);
103 container::BlasConnector::gemm('T', 'N', nvirt, nocc, nw1, 1.0, psi_a1.data(), nw1, tmp.data(), nw1, 0.0, &Cs_mo[c1.first][c2.first](iabf, 0, 0), nvirt);
104 }
105 }
106 iw2 += nw2;
107 }
108 iw1 += nw1;
109 }
110 return Cs_mo;
111 }
112
113 template <typename TK, typename TR>
114 std::vector<TK> cal_Amat_full(const TLRI<TK>& Cs_a,
115 const TLRI<TK>& Cs_b,
116 const TLRI<TR>& Vs)
117 {
118 assert(Cs_a.size() > 0);
119 assert(Cs_b.size() > 0);
120 assert(Vs.size() > 0);
121 auto& Cs_shape = Cs_a.at(0).begin()->second.shape;
122 auto& Vs_shape = Vs.at(0).begin()->second.shape;
123 assert(Cs_shape.size() == 3); // abf, nocc, nvirt
124 assert(Vs_shape.size() == 2); // abf, abf
125
126 const int& npairs = Cs_shape[1] * Cs_shape[2];
127 std::vector<TK> Amat_full(npairs * npairs, 0.0);
128
129 for (auto& itv1 : Vs)
130 {
131 const int& iat1 = itv1.first;
132 for (auto& itv2 : itv1.second)
133 {
134 const int& iat2 = itv2.first.first;
135 const auto& tensor_v = itv2.second; // (nabf2, nabf1), T
136 for (auto& itca2 : Cs_a.at(iat1))
137 {
138 const int& iat3 = itca2.first.first;
139 const auto& tensor_ca = itca2.second; // (nvirt*nocc, nabf1), N
140 for (auto& itcb2 : Cs_b.at(iat2))
141 {
142 const int& iat4 = itcb2.first.first;
143 const auto& tensor_cb = itcb2.second; // (nvirt*nocc, nabf2), T
144 const int& nabf1 = tensor_v.shape[0];
145 const int& nabf2 = tensor_v.shape[1];
146 assert(tensor_ca.shape[0] == nabf1); //abf1
147 assert(tensor_cb.shape[0] == nabf2); //abf2
148 std::vector<TK> tmp(npairs * nabf1);
149 container::BlasConnector::gemm('T', 'T', nabf1, npairs, nabf2, 1.0, tensor_v.ptr(), nabf2, tensor_cb.ptr(), npairs, 0.0, tmp.data(), nabf1);
150 container::BlasConnector::gemm('N', 'N', npairs, npairs, nabf1, 2.0/*Hartree to Ry*/, tensor_ca.ptr(), npairs, tmp.data(), nabf1, 1.0, Amat_full.data(), npairs);
151 }
152 }
153 }
154 }
155 return Amat_full;
156 }
157 template <typename TK>
158 TLRIX<TK> cal_CsX(const TLRI<TK>& Cs_mo, const TK* X)
159 {
160 TLRIX<TK> CsX;
161 for (auto& it1 : Cs_mo)
162 {
163 const int& iat1 = it1.first;
164 for (auto& it2 : it1.second)
165 {
166 const int& iat2 = it2.first.first;
167 auto& tensor_c = it2.second;
168 const int& nabf = tensor_c.shape[0];
169 const int& npairs = tensor_c.shape[1] * tensor_c.shape[2];
170 std::vector<TK> CX(nabf);
171 for (int iabf = 0;iabf < nabf;++iabf)
172 CX[iabf] = container::BlasConnector::dot(npairs, &tensor_c(iabf, 0, 0), 1, X, 1);
173 CsX[iat1][it2.first] = CX;
174 }
175 }
176 return CsX;
177 }
178
179 template <typename TK, typename TR>
180 TLRI<TK> cal_CV(const TLRI<TK>& Cs_a_mo,
181 const TLRI<TR>& Vs)
182 {
183 TLRI<TK> CV;
184 for (auto& it1 : Cs_a_mo) //the atom on which ABFs locate
185 {
186 const int& iat1 = it1.first;
187 for (auto& it2 : it1.second)
188 {
189 const int& iat2 = it2.first.first;
190 // const auto& Rc=it2.first.second;
191 auto& tensor_c = it2.second;
192 const int& nabf1 = tensor_c.shape[0];
193 const int& npairs = tensor_c.shape[1] * tensor_c.shape[2];
194 for (auto& it3 : Vs.at(iat1))
195 {
196 const int& iat3 = it3.first.first;
197 // const auto& Rv=it3.first.second;
198 const auto& tensor_v = it3.second;
199 assert(nabf1 == tensor_v.shape[0]);
200 const size_t& nabf2 = tensor_v.shape[1];
201 std::vector<TK> tmp(nabf2 * npairs);
202 // const auto& Rcv = (Rv - Rc) % period;
203 if (CV.count(iat2) && CV.at(iat2).count({ iat3, {0, 0, 0} })) // add-up, sum over iat1
204 {
205 auto& tensor_cv = CV.at(iat2).at({ iat3, {0, 0, 0} });
206 container::BlasConnector::gemm('N', 'T', npairs, nabf2, nabf1, 1.0, tensor_c.ptr(), npairs, tensor_v.ptr(), nabf2, 1.0, tensor_cv.ptr(), npairs);
207 }
208 else
209 {
210 RI::Tensor<TK> tmp({ nabf2, tensor_c.shape[1], tensor_c.shape[2] }); // (nabf2, nocc, nvirt)
211 container::BlasConnector::gemm('N', 'T', npairs, nabf2, nabf1, 1.0, tensor_c.ptr(), npairs, tensor_v.ptr(), nabf2, 0.0, tmp.ptr(), npairs);
212 CV[iat2][{iat3, { 0, 0, 0 }}] = tmp;
213 }
214 }
215 }
216 }
217 return CV;
218 }
219 template <typename TK, typename TR>
220 void cal_AX(const TLRI<TK>& Cs_a,
221 const TLRIX<TK>& Cs_bX,
222 const TLRI<TR>& Vs,
223 TK* AX,
224 const double& scale)
225 {
226 const int& npairs = Cs_a.at(0).begin()->second.shape[1] * Cs_a.at(0).begin()->second.shape[2];
227 for (auto& itv1 : Vs)
228 {
229 const int& iat1 = itv1.first;
230 for (auto& itv2 : itv1.second)
231 {
232 const int& iat2 = itv2.first.first;
233 const auto& tensor_v = itv2.second; // (nabf2, nabf1), T
234 for (auto& itca2 : Cs_a.at(iat1))
235 {
236 const int& iat3 = itca2.first.first;
237 const auto& tensor_ca = itca2.second; // (nvirt*nocc, nabf1), N
238 for (auto& itcb2 : Cs_bX.at(iat2))
239 {
240 const int& iat4 = itcb2.first.first;
241 const auto& vector_cb = itcb2.second; // (nvirt*nocc, nabf2), T
242 const int& nabf1 = tensor_v.shape[0];
243 const int& nabf2 = tensor_v.shape[1];
244 assert(tensor_ca.shape[0] == nabf1); //abf1
245 assert(vector_cb.size() == nabf2); //abf2
246 std::vector<TK> tmp(nabf1);
247 container::BlasConnector::gemv('T', nabf1, nabf2, 1.0, tensor_v.ptr(), nabf2, vector_cb.data(), 1, 0.0, tmp.data(), 1);
248 container::BlasConnector::gemv('N', npairs, nabf1, scale/*Hartree to Ry; singlet*/, tensor_ca.ptr(), npairs, tmp.data(), 1, 1.0, AX, 1);
249 }
250 }
251 }
252 }
253 }
254
255 template <typename TK>
256 void cal_AX(const TLRI<TK>& CV,
257 const TLRIX<TK>& Cs_bX,
258 TK* AX,
259 const double& scale)
260 {
261 for (auto& it1 : CV)
262 {
263 const int& iat1 = it1.first;
264 for (auto& it2 : it1.second)
265 {
266 const int& iat2 = it2.first.first;
267 const auto& tensor_cv = it2.second; // (nabf, nocc, nvirt)
268 const int& npairs = tensor_cv.shape[1] * tensor_cv.shape[2];
269 for (auto& it3 : Cs_bX.at(iat2))
270 {
271 const int& iat3 = it3.first.first;
272 const auto& vector_cx = it3.second; // (nabf)
273 const int& nabf = tensor_cv.shape[0];
274 assert(vector_cx.size() == nabf); //abf on at2
275 container::BlasConnector::gemv('N', npairs, nabf, scale/*Hartree to Ry; singlet*/, tensor_cv.ptr(), npairs, vector_cx.data(), 1, 1.0, AX, 1);
276 }
277 }
278 }
279 }
280
281 template <typename FPTYPE>
282 std::vector<FPTYPE> read_aims_ebands(const std::string& file, const int nocc, const int nvirt, int& ncore)
283 {
284 std::vector<FPTYPE> bands;
285 std::vector<FPTYPE> bands_final;
286 std::ifstream ifs;
287 ifs.open(file);
288 std::string tmp;
289 FPTYPE ene, occ;
290 for (int i = 0;i < 6;++i) { std::getline(ifs, tmp); } // skip the first 6 lines
291 int ivirt = 0;
292 while (ifs.peek() != EOF) {
293 ifs >> tmp >> occ >> ene >> tmp;
294 std::cout << "occ=" << occ << ", ene=" << ene << std::endl;
295 bands.push_back(ene * 2);//Hartree to Ry
296 if (occ < 0.1) { ++ivirt; }
297 if (ivirt == nvirt) { break; }
298 }
299 ncore = bands.size() - nocc - nvirt;
300 std::cout << "bands_final:" << std::endl;
301 for (int i = ncore;i < bands.size();++i)
302 {
303 bands_final.push_back(bands[i]);
304 std::cout << bands[i] << " ";
305 }
306 std::cout << std::endl;
307 return bands_final;
308 }
309
311 template <typename TK>
312 void read_aims_eigenvectors(psi::Psi<TK>& wfc_ks, const std::string& file, const int ncore, const int nbands, const int nbasis)
313 {
314 std::ifstream ifs;
315 ifs.open(file);
316 std::string tmp;
317 int nbands_last = 0;
318 while (ifs.peek() != EOF)
319 {
320 std::getline(ifs, tmp); //the first line
321 std::stringstream ss(tmp);
322 while (std::getline(ss, tmp, ' ')) {};
323 int nbands_file = std::stoi(tmp);
324 for (int iw = 0;iw < nbasis;++iw)
325 {
326 ifs >> tmp >> tmp >> tmp >> tmp >> tmp >> tmp; //useless cols
327 for (int ib = nbands_last; ib < nbands_file;++ib)
328 {
329 ifs >> tmp;
330 if (ib >= ncore && ib < ncore + nbands)
331 {
332 for (int is = 0;is < wfc_ks.get_nk();++is)
333 { //only for gamma_only and spin degenerate
334 wfc_ks(is, ib - ncore, iw) = std::stod(tmp);
335 }
336 }
337 }
338 }
339 std::getline(ifs, tmp); // the interval line between two blocks
340 std::getline(ifs, tmp); // the interval line between two blocks
341 nbands_last = nbands_file;
342 }
343 // output wfc
344 std::cout << "wfc_gs_read_from_aims:" << std::endl;
345 for (int ib = 0;ib < nbands;++ib)
346 {
347 for (int iw = 0;iw < nbasis;++iw)
348 {
349 std::cout << wfc_ks(0, ib, iw) << " ";
350 }
351 std::cout << std::endl;
352 }
353 }
354
355 template < typename TR>
356 bool compare_Vs(const TLRI<TR>& Vs1, const TLRI<TR>& Vs2, const double thr)
357 {
358 for (auto& tmp1 : Vs1)
359 {
360 const int& iat1 = tmp1.first;
361 for (auto& tmp2 : tmp1.second)
362 {
363 const int& iat2 = tmp2.first.first;
364 const RI::Tensor<TR>& t1 = tmp2.second;
365 const RI::Tensor<TR>& t2 = Vs2.at(iat1).at({ iat2, {0,0,0} });
366 if (t1.shape[0] != t2.shape[0]) { return false; }
367 if (t1.shape[1] != t2.shape[1]) { return false; }
368 for (int i = 0;i < t1.shape[0];++i)
369 {
370 for (int j = 0;j < t1.shape[1];++j)
371 {
372 if (std::abs(t1(i, j) - t2(i, j)) > thr) { std::cout << "element (" << i << ", " << j << ") are differernt: " << t1(i, j) << ", " << t2(i, j) << std::endl;return false; }
373 }
374 }
375 }
376 }
377 return true;
378 }
379 // since ucell.nw is updated from aims_nbasis, this function is abandoned 25-05-23
380 template <typename TR>
381 std::vector<TLRI<TR>> split_Ds(const std::vector<std::vector<TR>>& Ds, const std::vector<int>& aims_nbasis, const UnitCell& ucell) // vector index: ispin
382 {
383 // Due to the hard-coded constructor of module_dm::DensityMatrix, singlet-triplet with nspin=2 cannot use DM_trans with size 1
384 // if(Ds.size()>1) { throw std::runtime_error("split_Ds only supports gamma-only spin-1 Ds now."); }
385 std::vector<TLRI<TR>> Ds_split;
386 for (const auto& D : Ds)
387 {
388 TLRI<TR> D_split;
389 const int nbasis = std::sqrt(D.size());
390 int iw1_start = 0;
391 for (int iat1 = 0;iat1 < ucell.nat;++iat1)
392 {
393 const int& it1 = ucell.iat2it[iat1];
394 const size_t& nw1 = aims_nbasis[it1];
395 int iw2_start = 0;
396 for (int iat2 = 0;iat2 < ucell.nat;++iat2)
397 {
398 const int& it2 = ucell.iat2it[iat2];
399 const size_t& nw2 = aims_nbasis[it2];
400 D_split[iat1][{iat2, { 0,0,0 }}] = RI::Tensor<TR>({ nw1, nw2 });
401 for (int i = 0;i < nw1;++i)
402 {
403 for (int j = 0;j < nw2;++j)
404 {
405 D_split[iat1][{iat2, { 0,0,0 }}](i, j) = D[(iw1_start + i)*nbasis+(iw2_start + j)] * 0.5; // consistent with split_m2D_ktoR
406 }
407 }
408 iw2_start += nw2;
409 }
410 assert(iw2_start == nbasis);
411 iw1_start += nw1;
412 }
413 assert(iw1_start == nbasis);
414 Ds_split.push_back(D_split);
415 }
416 return Ds_split;
417 }
418}
419
420#endif // ABACUS_SOURCE_LCAO_MODULE_LR_RI_BENCHMARK_RI_BENCHMARK_HPP
int nw
number of local orbitals (l,n,m) of this type
Definition atom_spec.h:36
Definition rpa_lri_coeff_v1.h:11
Provide the basic information about unitcell.
Definition unitcell.h:16
Atom * atoms
Definition unitcell.h:165
int nat
total number of atoms of all species in unitcell
Definition unitcell.h:155
std::vector< int > iat2it
iat==>it, distinguish a atom belong to which type
Definition unitcell.h:156
const int * get_iat2iwt() const
Get iat2iwt.
Definition unitcell.h:64
Definition psi.h:37
const int & get_nbands() const
Definition psi.cpp:337
const int & get_nk() const
Definition psi.cpp:331
Definition operator_ri_hartree.h:8
std::vector< TK > slice_psi(const psi::Psi< TK > &wfc_ks, const int &ibstart, const int &nb, const int &iwstart, const int &nw)
slice the psi from wfc_ks of all the k-points, some bands, some basis functions
Definition ri_benchmark.hpp:38
void cal_AX(const TLRI< TK > &Cs_a, const TLRIX< TK > &Cs_bX, const TLRI< TR > &Vs, TK *AX, const double &scale)
Definition ri_benchmark.hpp:220
TLRI< TK > cal_Cs_mo(const UnitCell &ucell, const TLRI< TR > &Cs_ao, const psi::Psi< TK > &wfc_ks, const int &nocc, const int &nvirt, const int &occ_first)
Definition ri_benchmark.hpp:55
TLRIX< TK > cal_CsX(const TLRI< TK > &Cs_mo, const TK *X)
Definition ri_benchmark.hpp:158
std::vector< TLRI< TR > > split_Ds(const std::vector< std::vector< TR > > &Ds, const std::vector< int > &aims_nbasis, const UnitCell &ucell)
Definition ri_benchmark.hpp:381
std::vector< TK > cal_Amat_full(const TLRI< TK > &Cs_a, const TLRI< TK > &Cs_b, const TLRI< TR > &Vs)
Definition ri_benchmark.hpp:114
TLRI< TK > cal_CV(const TLRI< TK > &Cs_a_mo, const TLRI< TR > &Vs)
Definition ri_benchmark.hpp:180
std::vector< FPTYPE > read_aims_ebands(const std::string &file, const int nocc, const int nvirt, int &ncore)
Definition ri_benchmark.hpp:282
int count_nao_from_Cs(const TLRI< TR > &Cs_ao)
Definition ri_benchmark.hpp:24
bool compare_Vs(const TLRI< TR > &Vs1, const TLRI< TR > &Vs2, const double thr)
Definition ri_benchmark.hpp:356
void read_aims_eigenvectors(psi::Psi< TK > &wfc_ks, const std::string &file, const int ncore, const int nbands, const int nbasis)
read the eigenvectors from FHI-aims, only for gamma_only and spin degenerate
Definition ri_benchmark.hpp:312
file(GLOB ATen_CORE_SRCS "*.cpp") set(ATen_CPU_SRCS $
Definition CMakeLists.txt:1
std::map< int, std::map< TAC, RI::Tensor< T > > > TLRI
Definition test_write_ri_cv.cpp:12