1#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_RI_BENCHMARK_RI_BENCHMARK_HPP
2#define ABACUS_SOURCE_LCAO_MODULE_LR_RI_BENCHMARK_RI_BENCHMARK_HPP
23 template <
typename TR>
27 for (
const auto& it2 : Cs_ao.at(0))
29 assert(it2.second.shape.size() == 3);
30 naos += it2.second.shape[2];
37 template <
typename TK>
44 std::vector<TK> psi_slice(wfc_ks.
get_nk() * nb * nw);
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());
54 template <
typename TK,
typename TR>
66 for (
auto& c1 : Cs_ao)
68 const int& iat1 = c1.first;
69 const int& it1 = ucell.
iat2it[iat1];
70 const int& nw1 = ucell.
atoms[it1].
nw;
73 for (
auto& c2 : c1.second)
75 const int& iat2 = c2.first.first;
76 const int& it2 = ucell.
iat2it[iat2];
77 const int& nw2 = ucell.
atoms[it2].
nw;
80 const auto& tensor_ao = c2.second;
81 const size_t& nabf = tensor_ao.shape[0];
82 assert(tensor_ao.shape.size() == 3);
84 std::vector<TK> psi_a1 = occ_first ?
slice_psi(wfc_ks, 0, nocc, iw1, nw1)
85 :
slice_psi(wfc_ks, nocc, nvirt, iw1, nw1);
86 std::vector<TK> psi_a2 = occ_first ?
slice_psi(wfc_ks, nocc, nvirt, iw2, nw2)
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)
92 const auto ptr = &tensor_ao(iabf, 0, 0);
93 std::vector<TK> tmp(nw1 * (occ_first ? nvirt : nocc));
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);
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);
113 template <
typename TK,
typename TR>
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);
124 assert(Vs_shape.size() == 2);
126 const int& npairs = Cs_shape[1] * Cs_shape[2];
127 std::vector<TK> Amat_full(npairs * npairs, 0.0);
129 for (
auto& itv1 : Vs)
131 const int& iat1 = itv1.first;
132 for (
auto& itv2 : itv1.second)
134 const int& iat2 = itv2.first.first;
135 const auto& tensor_v = itv2.second;
136 for (
auto& itca2 : Cs_a.at(iat1))
138 const int& iat3 = itca2.first.first;
139 const auto& tensor_ca = itca2.second;
140 for (
auto& itcb2 : Cs_b.at(iat2))
142 const int& iat4 = itcb2.first.first;
143 const auto& tensor_cb = itcb2.second;
144 const int& nabf1 = tensor_v.shape[0];
145 const int& nabf2 = tensor_v.shape[1];
146 assert(tensor_ca.shape[0] == nabf1);
147 assert(tensor_cb.shape[0] == nabf2);
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, tensor_ca.ptr(), npairs, tmp.data(), nabf1, 1.0, Amat_full.data(), npairs);
157 template <
typename TK>
161 for (
auto& it1 : Cs_mo)
163 const int& iat1 = it1.first;
164 for (
auto& it2 : it1.second)
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;
179 template <
typename TK,
typename TR>
184 for (
auto& it1 : Cs_a_mo)
186 const int& iat1 = it1.first;
187 for (
auto& it2 : it1.second)
189 const int& iat2 = it2.first.first;
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))
196 const int& iat3 = it3.first.first;
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);
203 if (CV.count(iat2) && CV.at(iat2).count({ iat3, {0, 0, 0} }))
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);
210 RI::Tensor<TK> tmp({ nabf2, tensor_c.shape[1], tensor_c.shape[2] });
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;
219 template <
typename TK,
typename TR>
221 const TLRIX<TK>& Cs_bX,
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)
229 const int& iat1 = itv1.first;
230 for (
auto& itv2 : itv1.second)
232 const int& iat2 = itv2.first.first;
233 const auto& tensor_v = itv2.second;
234 for (
auto& itca2 : Cs_a.at(iat1))
236 const int& iat3 = itca2.first.first;
237 const auto& tensor_ca = itca2.second;
238 for (
auto& itcb2 : Cs_bX.at(iat2))
240 const int& iat4 = itcb2.first.first;
241 const auto& vector_cb = itcb2.second;
242 const int& nabf1 = tensor_v.shape[0];
243 const int& nabf2 = tensor_v.shape[1];
244 assert(tensor_ca.shape[0] == nabf1);
245 assert(vector_cb.size() == nabf2);
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, tensor_ca.ptr(), npairs, tmp.data(), 1, 1.0, AX, 1);
255 template <
typename TK>
257 const TLRIX<TK>& Cs_bX,
263 const int& iat1 = it1.first;
264 for (
auto& it2 : it1.second)
266 const int& iat2 = it2.first.first;
267 const auto& tensor_cv = it2.second;
268 const int& npairs = tensor_cv.shape[1] * tensor_cv.shape[2];
269 for (
auto& it3 : Cs_bX.at(iat2))
271 const int& iat3 = it3.first.first;
272 const auto& vector_cx = it3.second;
273 const int& nabf = tensor_cv.shape[0];
274 assert(vector_cx.size() == nabf);
275 container::BlasConnector::gemv(
'N', npairs, nabf, scale, tensor_cv.ptr(), npairs, vector_cx.data(), 1, 1.0, AX, 1);
281 template <
typename FPTYPE>
284 std::vector<FPTYPE> bands;
285 std::vector<FPTYPE> bands_final;
290 for (
int i = 0;i < 6;++i) { std::getline(ifs, tmp); }
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);
296 if (occ < 0.1) { ++ivirt; }
297 if (ivirt == nvirt) {
break; }
299 ncore = bands.size() - nocc - nvirt;
300 std::cout <<
"bands_final:" << std::endl;
301 for (
int i = ncore;i < bands.size();++i)
303 bands_final.push_back(bands[i]);
304 std::cout << bands[i] <<
" ";
306 std::cout << std::endl;
311 template <
typename TK>
318 while (ifs.peek() != EOF)
320 std::getline(ifs, tmp);
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)
326 ifs >> tmp >> tmp >> tmp >> tmp >> tmp >> tmp;
327 for (
int ib = nbands_last; ib < nbands_file;++ib)
330 if (ib >= ncore && ib < ncore + nbands)
332 for (
int is = 0;is < wfc_ks.
get_nk();++is)
334 wfc_ks(is, ib - ncore, iw) = std::stod(tmp);
339 std::getline(ifs, tmp);
340 std::getline(ifs, tmp);
341 nbands_last = nbands_file;
344 std::cout <<
"wfc_gs_read_from_aims:" << std::endl;
345 for (
int ib = 0;ib < nbands;++ib)
347 for (
int iw = 0;iw < nbasis;++iw)
349 std::cout << wfc_ks(0, ib, iw) <<
" ";
351 std::cout << std::endl;
355 template <
typename TR>
358 for (
auto& tmp1 : Vs1)
360 const int& iat1 = tmp1.first;
361 for (
auto& tmp2 : tmp1.second)
363 const int& iat2 = tmp2.first.first;
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)
370 for (
int j = 0;j < t1.shape[1];++j)
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; }
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)
385 std::vector<TLRI<TR>> Ds_split;
386 for (
const auto& D : Ds)
389 const int nbasis = std::sqrt(D.size());
391 for (
int iat1 = 0;iat1 < ucell.
nat;++iat1)
393 const int& it1 = ucell.
iat2it[iat1];
394 const size_t& nw1 = aims_nbasis[it1];
396 for (
int iat2 = 0;iat2 < ucell.
nat;++iat2)
398 const int& it2 = ucell.
iat2it[iat2];
399 const size_t& nw2 = aims_nbasis[it2];
401 for (
int i = 0;i < nw1;++i)
403 for (
int j = 0;j < nw2;++j)
405 D_split[iat1][{iat2, { 0,0,0 }}](i, j) = D[(iw1_start + i)*nbasis+(iw2_start + j)] * 0.5;
410 assert(iw2_start == nbasis);
413 assert(iw1_start == nbasis);
414 Ds_split.push_back(D_split);
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
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