ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
ri_2d_comm.hpp
Go to the documentation of this file.
1//=======================
2// AUTHOR : Peize Lin
3// DATE : 2022-08-17
4//=======================
5
6#ifndef RI_2D_COMM_HPP
7#define RI_2D_COMM_HPP
8#include "ri_2d_comm.h"
9#include "ri_util.h"
11#include "source_base/timer.h"
14#include <Comm/Comm_Assemble/Comm_Assemble.h>
15#include <Comm/example/Communicate_Map-1.h>
16#include <Comm/example/Communicate_Map-2.h>
17#include <RI/comm/example/Communicate_Map_Period.h>
18#include <RI/comm/mix/Communicate_Tensors_Map.h>
19#include <RI/global/Global_Func-2.h>
20
21#include <cmath>
22#include <string>
23#include <stdexcept>
24
25#ifdef _OPENMP
26#include <omp.h>
27#endif
28
30inline RI::Tensor<std::complex<double>> tensor_conj(const RI::Tensor<std::complex<double>>& t)
31{
33 for (int i = 0; i < t.data->size(); ++i) {
34 (*r.data)[i] = std::conj((*t.data)[i]);
35 }
36 return r;
37}
38template<typename Tdata, typename Tmatrix>
39auto RI_2D_Comm::split_m2D_ktoR(const UnitCell& ucell,
40 const K_Vectors & kv,
41 const std::vector<const Tmatrix*>&mks_2D,
42 const Parallel_Blkcyclic & pv,
43 const int nspin,
44 const bool spgsym)
45-> std::vector<std::map<TA,std::map<TAC,RI::Tensor<Tdata>>>>
46{
47 ModuleBase::TITLE("RI_2D_Comm","split_m2D_ktoR");
48 ModuleBase::timer::start("RI_2D_Comm", "split_m2D_ktoR");
49 const TC period = RI_Util::get_Born_vonKarmen_period(kv);
50 std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>> mRs_a2D
51 = (period == TC{1, 1, 1})
52 ? RI_2D_Comm::split_m2D_ktoR_gamma<Tdata, Tmatrix>(ucell, mks_2D, pv, nspin)
53 : RI_2D_Comm::split_m2D_ktoR_k<Tdata, Tmatrix>(ucell, kv, mks_2D, pv, nspin, spgsym);
54 ModuleBase::timer::end("RI_2D_Comm", "split_m2D_ktoR");
55 return mRs_a2D;
56}
57
58template<typename Tdata, typename Tmatrix>
60 const std::vector<const Tmatrix*>& mks_2D,
61 const Parallel_Blkcyclic& pv,
62 const int nspin)
63-> std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>>
64{
65 ModuleBase::TITLE("RI_2D_Comm","split_m2D_ktoR_gamma");
66 ModuleBase::timer::start("RI_2D_Comm", "split_m2D_ktoR_gamma");
67
68 const std::map<int,int> nspin_k = {{1,1}, {2,2}, {4,1}};
69 const double SPIN_multiple = std::map<int, double>{ {1,0.5}, {2,1}, {4,1} }.at(nspin); // why?
70 const TC cell = {0, 0, 0};
71
72 std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>> mRs_a2D(nspin);
73
74 #ifdef _OPENMP
75 // pre-init all outer maps mRs_a2D[is_b][iat] to avoid concurrent std::map rebalancing
76 for (int is_b = 0; is_b < nspin; ++is_b)
77 for (int iat0 = 0; iat0 < ucell.nat; ++iat0)
78 mRs_a2D[is_b][iat0];
79
80 std::vector<omp_lock_t> locks(ucell.nat);
81 for (omp_lock_t& l : locks)
82 omp_init_lock(&l);
83 #endif
84
85 for (int is_k = 0; is_k < nspin_k.at(nspin); ++is_k)
86 {
87 using Tdata_m = typename Tmatrix::value_type;
89 = RI_Util::Vector_to_Tensor<Tdata_m>(*mks_2D[is_k], pv.ncloc(), pv.nrloc());
90 const Tdata_m frac = RI::Global_Func::convert<Tdata_m>(SPIN_multiple);
91 RI::Tensor<Tdata> mR_2D = RI::Global_Func::convert<Tdata>(mk_2D * frac);
92
93 #ifdef _OPENMP
94 #pragma omp parallel for schedule(dynamic)
95 #endif
96 for (int iwt0_2D = 0; iwt0_2D != mR_2D.shape[0]; ++iwt0_2D)
97 {
98 const int iwt0 = ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)
99 ? pv.icglb(iwt0_2D)
100 : pv.irglb(iwt0_2D);
101 int iat0, iw0_b, is0_b;
102 std::tie(iat0, iw0_b, is0_b) = RI_2D_Comm::get_iat_iw_is_block(ucell, iwt0);
103 const int it0 = ucell.iat2it[iat0];
104 for (int iwt1_2D = 0; iwt1_2D != mR_2D.shape[1]; ++iwt1_2D)
105 {
106 const int iwt1 = ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)
107 ? pv.irglb(iwt1_2D)
108 : pv.icglb(iwt1_2D);
109 int iat1, iw1_b, is1_b;
110 std::tie(iat1, iw1_b, is1_b) = RI_2D_Comm::get_iat_iw_is_block(ucell, iwt1);
111 const int it1 = ucell.iat2it[iat1];
112
113 const int is_b = RI_2D_Comm::get_is_block(is_k, is0_b, is1_b);
114 #ifdef _OPENMP
115 omp_set_lock(&locks[iat0]);
116 #endif
117 RI::Tensor<Tdata>& mR_a2D = mRs_a2D[is_b][iat0][{iat1, cell}];
118 if (mR_a2D.empty())
119 {
120 mR_a2D = RI::Tensor<Tdata>(
121 {static_cast<size_t>(ucell.atoms[it0].nw),
122 static_cast<size_t>(ucell.atoms[it1].nw)});
123 }
124 mR_a2D(iw0_b, iw1_b) = mR_2D(iwt0_2D, iwt1_2D);
125 #ifdef _OPENMP
126 omp_unset_lock(&locks[iat0]);
127 #endif
128 }
129 }
130 }
131
132 #ifdef _OPENMP
133 for (omp_lock_t& l : locks)
134 omp_destroy_lock(&l);
135
136 // prune empty inner maps created by pre-init
137 for (int is_b = 0; is_b < nspin; ++is_b)
138 for (auto it = mRs_a2D[is_b].begin(); it != mRs_a2D[is_b].end();)
139 {
140 if (it->second.empty())
141 it = mRs_a2D[is_b].erase(it);
142 else
143 ++it;
144 }
145 #endif
146
147 ModuleBase::timer::end("RI_2D_Comm", "split_m2D_ktoR_gamma");
148 return mRs_a2D;
149}
150
151template<typename Tdata, typename Tmatrix>
152auto RI_2D_Comm::split_m2D_ktoR_k(const UnitCell& ucell,
153 const K_Vectors& kv,
154 const std::vector<const Tmatrix*>& mks_2D,
155 const Parallel_Blkcyclic& pv,
156 const int nspin,
157 const bool spgsym)
158-> std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>>
159{
160 ModuleBase::TITLE("RI_2D_Comm","split_m2D_ktoR_k");
161 ModuleBase::timer::start("RI_2D_Comm", "split_m2D_ktoR_k");
162
163 const TC period = RI_Util::get_Born_vonKarmen_period(kv);
164 const std::map<int,int> nspin_k = {{1,1}, {2,2}, {4,1}};
165 const double SPIN_multiple = std::map<int, double>{ {1,0.5}, {2,1}, {4,1} }.at(nspin); // why?
166
167 std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>> mRs_a2D(nspin);
168 #ifdef _OPENMP
169 #pragma omp parallel
170 #endif
171 {
172 std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>> mRs_a2D_thread(nspin);
173 for (int is_k = 0; is_k < nspin_k.at(nspin); ++is_k)
174 {
175 const std::vector<int> ik_list = RI_2D_Comm::get_ik_list(kv, is_k);
176 const auto cells = RI_Util::get_Born_von_Karmen_cells(period);
177 #pragma omp for schedule(dynamic)
178 for (size_t icell = 0; icell < cells.size(); ++icell)
179 {
180 const TC& cell = cells[icell];
181 RI::Tensor<Tdata> mR_2D;
182 int ik_full = 0;
183 for (const int ik : ik_list)
184 {
185 auto set_mR_2D = [&mR_2D](auto&& mk_frac)
186 {
187 if (mR_2D.empty())
188 { mR_2D = RI::Global_Func::convert<Tdata>(mk_frac); }
189 else
190 { mR_2D = mR_2D + RI::Global_Func::convert<Tdata>(mk_frac); }
191 };
192 using Tdata_m = typename Tmatrix::value_type;
193 if (!spgsym)
194 {
195 RI::Tensor<Tdata_m> mk_2D = RI_Util::Vector_to_Tensor<Tdata_m>(*mks_2D[ik], pv.ncloc(), pv.nrloc());
196 const Tdata_m frac = SPIN_multiple
197 * RI::Global_Func::convert<Tdata_m>(std::exp(
198 -ModuleBase::TWO_PI * ModuleBase::IMAG_UNIT * (kv.kvec_c[ik] * (RI_Util::array3_to_Vector3(cell) * ucell.latvec))));
199 if (static_cast<int>(std::round(SPIN_multiple * kv.wk[ik] * kv.get_nkstot_nospin())) == 2)
200 { set_mR_2D(mk_2D * (frac * 0.5) + tensor_conj(mk_2D * (frac * 0.5))); }
201 else
202 { set_mR_2D(mk_2D * frac); }
203 }
204 else
205 { // traverse kstar, ik means ik_ibz
206 for (auto& isym_kvd : kv.kstars[ik % ik_list.size()])
207 {
209 *mks_2D[ik_full + is_k * kv.get_nkstot_nospin()], pv.ncloc(), pv.nrloc());
210 const Tdata_m frac = SPIN_multiple
211 * RI::Global_Func::convert<Tdata_m>(std::exp(
212 -ModuleBase::TWO_PI * ModuleBase::IMAG_UNIT * ((isym_kvd.second * ucell.G) * (RI_Util::array3_to_Vector3(cell) * ucell.latvec))));
213 set_mR_2D(mk_2D * frac);
214 ++ik_full;
215 }
216 }
217 } // end for ik
218 for(int iwt0_2D=0; iwt0_2D!=mR_2D.shape[0]; ++iwt0_2D)
219 {
220 const int iwt0 =ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)
221 ? pv.icglb(iwt0_2D)
222 : pv.irglb(iwt0_2D);
223 int iat0, iw0_b, is0_b;
224 std::tie(iat0,iw0_b,is0_b) = RI_2D_Comm::get_iat_iw_is_block(ucell,iwt0);
225 const int it0 = ucell.iat2it[iat0];
226 for(int iwt1_2D=0; iwt1_2D!=mR_2D.shape[1]; ++iwt1_2D)
227 {
228 const int iwt1 =ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)
229 ? pv.irglb(iwt1_2D)
230 : pv.icglb(iwt1_2D);
231 int iat1, iw1_b, is1_b;
232 std::tie(iat1,iw1_b,is1_b) = RI_2D_Comm::get_iat_iw_is_block(ucell,iwt1);
233 const int it1 = ucell.iat2it[iat1];
234
235 const int is_b = RI_2D_Comm::get_is_block(is_k, is0_b, is1_b);
236 RI::Tensor<Tdata>& mR_a2D = mRs_a2D_thread[is_b][iat0][{iat1, cell}];
237 if (mR_a2D.empty())
238 {
239 mR_a2D = RI::Tensor<Tdata>(
240 {static_cast<size_t>(ucell.atoms[it0].nw),
241 static_cast<size_t>(ucell.atoms[it1].nw)});
242 }
243 mR_a2D(iw0_b, iw1_b) = mR_2D(iwt0_2D, iwt1_2D);
244 } // for iwt1_2D
245 } // end for iwt0_2D
246 } // end for icell
247 } // end for is_k
248
249 #ifdef _OPENMP
250 #pragma omp critical
251 #endif
252 {
253 for(int is=0; is<nspin; ++is)
254 for(auto &mRs_A : mRs_a2D_thread[is])
255 for(auto &mRs_B : mRs_A.second)
256 {
257 assert(mRs_a2D[is][mRs_A.first][mRs_B.first].empty());
258 mRs_a2D[is][mRs_A.first][mRs_B.first] = std::move(mRs_B.second);
259 }
260 }
261 } // end #pragma omp parallel
262 ModuleBase::timer::end("RI_2D_Comm", "split_m2D_ktoR_k");
263 return mRs_a2D;
264}
265
266
267template<typename Tdata, typename TK>
269 const UnitCell &ucell,
270 const K_Vectors &kv,
271 const int ik,
272 const double alpha,
273 const std::vector<std::map<TA,std::map<TAC,RI::Tensor<Tdata>>>> &Hs,
274 const Parallel_Orbitals& pv,
275 TK* hk)
276{
277 ModuleBase::TITLE("RI_2D_Comm","add_Hexx");
278 ModuleBase::timer::start("RI_2D_Comm", "add_Hexx");
279
280 const std::map<int, std::vector<int>> is_list = {{1,{0}}, {2,{kv.isk[ik]}}, {4,{0,1,2,3}}};
281 for(const int is_b : is_list.at(PARAM.inp.nspin))
282 {
283 int is0_b, is1_b;
284 std::tie(is0_b,is1_b) = RI_2D_Comm::split_is_block(is_b);
285 for(const auto &Hs_tmpA : Hs[is_b])
286 {
287 const TA &iat0 = Hs_tmpA.first;
288 for(const auto &Hs_tmpB : Hs_tmpA.second)
289 {
290 const TA &iat1 = Hs_tmpB.first.first;
291 const TC &cell1 = Hs_tmpB.first.second;
292 const std::complex<double> frac = alpha
293 * std::exp( ModuleBase::TWO_PI*ModuleBase::IMAG_UNIT * (kv.kvec_c[ik] * (RI_Util::array3_to_Vector3(cell1)*ucell.latvec)) );
294 const RI::Tensor<Tdata> &H = Hs_tmpB.second;
295 for(size_t iw0_b=0; iw0_b<H.shape[0]; ++iw0_b)
296 {
297 const int iwt0 = RI_2D_Comm::get_iwt(ucell,iat0, iw0_b, is0_b);
298 if (pv.para_2d().irloc(iwt0) < 0) {
299 continue;
300 }
301 for(size_t iw1_b=0; iw1_b<H.shape[1]; ++iw1_b)
302 {
303 const int iwt1 = RI_2D_Comm::get_iwt(ucell,iat1, iw1_b, is1_b);
304 if (pv.para_2d().icloc(iwt1) < 0) {
305 continue;
306 }
307 LCAO_domain::set_mat2d(iwt0, iwt1, RI::Global_Func::convert<TK>(H(iw0_b, iw1_b)) * RI::Global_Func::convert<TK>(frac), pv, hk);
308 }
309 }
310 }
311 }
312 }
313 ModuleBase::timer::end("RI_2D_Comm", "add_Hexx");
314}
315
316
317template<typename Tdata, typename TR>
319 const int current_spin,
320 const double alpha,
321 const std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>>& Hs,
322 const Parallel_Orbitals& pv,
323 const int npol,
325 const RI::Cell_Nearest<int, int, 3, double, 3>* const cell_nearest)
326{
327 ModuleBase::TITLE("RI_2D_Comm", "add_HexxR");
328 ModuleBase::timer::start("RI_2D_Comm", "add_HexxR");
329 const std::map<int, std::vector<int>> is_list = { {1,{0}}, {2,{current_spin}}, {4,{0,1,2,3}} };
330 for (const int is_hs : is_list.at(PARAM.inp.nspin))
331 {
332 int is0_b = 0, is1_b = 0;
333 std::tie(is0_b, is1_b) = RI_2D_Comm::split_is_block(is_hs);
334 for (const auto& Hs_tmpA : Hs[is_hs])
335 {
336 const TA& iat0 = Hs_tmpA.first;
337 for (const auto& Hs_tmpB : Hs_tmpA.second)
338 {
339 const TA& iat1 = Hs_tmpB.first.first;
340 const TC& cell = Hs_tmpB.first.second;
342 (cell_nearest ?
343 cell_nearest->get_cell_nearest_discrete(iat0, iat1, cell)
344 : cell));
345 hamilt::BaseMatrix<TR>* HlocR = hR.find_matrix(iat0, iat1, R.x, R.y, R.z);
346 std::vector<int> row_indexes = pv.get_indexes_row(iat0);
347 std::vector<int> col_indexes = pv.get_indexes_col(iat1);
348 const RI::Tensor<Tdata>& HexxR = (Tdata)alpha * Hs_tmpB.second;
349 for (int lw0_b = 0;lw0_b < row_indexes.size();lw0_b += npol) // block
350 {
351 const int& gw0 = row_indexes[lw0_b] / npol;
352 const int& lw0 = (npol == 2) ? (lw0_b + is0_b) : lw0_b;
353 for (int lw1_b = 0;lw1_b < col_indexes.size();lw1_b += npol)
354 {
355 const int& gw1 = col_indexes[lw1_b] / npol;
356 const int& lw1 = (npol == 2) ? (lw1_b + is1_b) : lw1_b;
357 HlocR->add_element(lw0, lw1, RI::Global_Func::convert<TR>(HexxR(gw0, gw1)));
358 }
359 }
360 }
361 }
362 }
363
364 ModuleBase::timer::end("RI_2D_Comm", "add_HexxR");
365}
366
367// DM(R) format conversion: HContainer<TR> -> Ds (atom-pair / cell map).
368// This is the real-space (DM(R)) counterpart of split_m2D_ktoR (which converts DM(k)),
369// and the inverse of add_HexxR (which converts Ds -> HContainer); it therefore reuses the
370// exact same atom/orbital/spin-block index mapping as add_HexxR.
371// dm_container is the DM(R) vector returned by DensityMatrix::get_dmr_vec():
372// nspin==1 : size 1 (container 0 -> spin-block 0)
373// nspin==2 : size 2 (container is -> spin-block is)
374// nspin==4 : size 1 (container 0 holds the 2x2 npol blocks -> spin-blocks 0,1,2,3)
375template <typename TR, typename Tdata>
377 const std::vector<hamilt::HContainer<TR>*>& dm_container,
378 const UnitCell& ucell,
379 const Parallel_Orbitals& pv,
380 const int nspin)
381-> std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>>
382{
383 ModuleBase::TITLE("RI_2D_Comm", "dm_container_to_Ds");
384 ModuleBase::timer::start("RI_2D_Comm", "dm_container_to_Ds");
385
386 const int npol = (nspin == 4) ? 2 : 1;
387 // same spin prefactor as split_m2D_ktoR: DM(R) carries the full (spin-summed) occupation,
388 // while Ds is the single-spin-channel density matrix
389 const double SPIN_multiple = std::map<int, double>{ {1, 0.5}, {2, 1}, {4, 1} }.at(nspin);
390
391 std::vector<std::map<TA, std::map<TAC, RI::Tensor<Tdata>>>> Ds(nspin);
392
393 for (int ic = 0; ic < static_cast<int>(dm_container.size()); ++ic)
394 {
395 const hamilt::HContainer<TR>& dmR = *dm_container[ic];
396 // spin-blocks carried by this container (cf. add_HexxR's is_list)
397 const std::vector<int> is_list = (nspin == 4)
398 ? std::vector<int>{ 0, 1, 2, 3 }
399 : std::vector<int>{ ic };
400 for (int iap = 0; iap < static_cast<int>(dmR.size_atom_pairs()); ++iap)
401 {
402 const hamilt::AtomPair<TR>& ap = dmR.get_atom_pair(iap);
403 const int iat0 = ap.get_atom_i();
404 const int iat1 = ap.get_atom_j();
405 const int it0 = ucell.iat2it[iat0];
406 const int it1 = ucell.iat2it[iat1];
407 const std::vector<int> row_indexes = pv.get_indexes_row(iat0);
408 const std::vector<int> col_indexes = pv.get_indexes_col(iat1);
409 for (int iR = 0; iR < ap.get_R_size(); ++iR)
410 {
411 const ModuleBase::Vector3<int> R_index = ap.get_R_index(iR);
412 const TC cell = { R_index.x, R_index.y, R_index.z };
413 const hamilt::BaseMatrix<TR>* const dm_mat = ap.find_matrix(R_index);
414 if (dm_mat == nullptr) { continue; }
415 for (const int is_b : is_list)
416 {
417 int is0_b = 0, is1_b = 0;
418 std::tie(is0_b, is1_b) = RI_2D_Comm::split_is_block(is_b);
419 RI::Tensor<Tdata>& D = Ds[is_b][iat0][{iat1, cell}];
420 if (D.empty())
421 {
423 { static_cast<size_t>(ucell.atoms[it0].nw),
424 static_cast<size_t>(ucell.atoms[it1].nw) });
425 }
426 for (int lw0_b = 0; lw0_b < static_cast<int>(row_indexes.size()); lw0_b += npol)
427 {
428 const int gw0 = row_indexes[lw0_b] / npol;
429 const int lw0 = (npol == 2) ? (lw0_b + is0_b) : lw0_b;
430 for (int lw1_b = 0; lw1_b < static_cast<int>(col_indexes.size()); lw1_b += npol)
431 {
432 const int gw1 = col_indexes[lw1_b] / npol;
433 const int lw1 = (npol == 2) ? (lw1_b + is1_b) : lw1_b;
434 D(gw0, gw1) = RI::Global_Func::convert<Tdata>(
435 SPIN_multiple * dm_mat->get_value(lw0, lw1));
436 }
437 }
438 }
439 }
440 }
441 }
442
443 ModuleBase::timer::end("RI_2D_Comm", "dm_container_to_Ds");
444 return Ds;
445}
446
447template <typename TA, typename TAC, typename T>
448std::map<TA, std::map<TAC, T>> RI_2D_Comm::comm_map2_first(const MPI_Comm& mpi_comm,
449 const std::map<TA, std::map<TAC, T>>& Ds_in,
450 const std::set<TA>& s0,
451 const std::set<TA>& s1)
452{
453 RI::Communicate_Map_Period::Judge_Map2_First<TA> judge;
454 judge.s0 = s0;
455 judge.s1 = s1;
456 return comm_map2(mpi_comm, Ds_in, judge);
457}
458
459template <typename TA, typename TAC, typename T, typename Tjudge>
460std::map<TA, std::map<TAC, T>> RI_2D_Comm::comm_map2(const MPI_Comm& mpi_comm,
461 const std::map<TA, std::map<TAC, T>>& Ds_in,
462 const Tjudge& judge)
463{
464 Comm::Comm_Assemble<std::tuple<TA, TAC>, T, std::map<TA, std::map<TAC, T>>, Tjudge, std::map<TA, std::map<TAC, T>>>
465 com(mpi_comm);
466
467 com.traverse_keys_provide = Comm::Communicate_Map::traverse_keys<TA, TAC, T>;
468 com.get_value_provide = Comm::Communicate_Map::get_value<TA, TAC, T>;
469 com.set_value_require = set_value_add<TA, TAC, T>;
470 com.flag_lock_set_value = Comm::Comm_Tools::Lock_Type::Copy_merge;
471 com.init_datas_local = Comm::Communicate_Map::init_datas_local<TA, TAC, T>;
472 com.add_datas = add_datas<TA, TAC, T>;
473
474 std::map<TA, std::map<TAC, T>> Ds_out;
475 com.communicate(Ds_in, judge, Ds_out);
476 return Ds_out;
477}
478
479template <typename Tkey, typename Tvalue>
480void RI_2D_Comm::set_value_add(Tkey&& key, Tvalue&& value, std::map<Tkey, Tvalue>& data)
481{
482 using namespace RI::Array_Operator;
483 auto ptr = data.find(key);
484 if (ptr == data.end())
485 data[key] = std::move(value);
486 else
487 ptr->second = ptr->second + std::move(value);
488}
489
490template <typename Tkey0, typename Tkey1, typename Tvalue>
491void RI_2D_Comm::set_value_add(std::tuple<Tkey0, Tkey1>&& key,
492 Tvalue&& value,
493 std::map<Tkey0, std::map<Tkey1, Tvalue>>& data)
494{
495 set_value_add(std::move(std::get<1>(key)), std::move(value), data[std::get<0>(key)]);
496}
497
498template <typename Tkey, typename Tvalue>
499void RI_2D_Comm::add_datas(std::map<Tkey, Tvalue>&& data_local, std::map<Tkey, Tvalue>& data_recv)
500{
501 using namespace RI::Array_Operator;
502 auto ptr_local = data_local.begin();
503 auto ptr_recv = data_recv.begin();
504 for (; ptr_local != data_local.end() && ptr_recv != data_recv.end();)
505 {
506 const Tkey& key_local = ptr_local->first;
507 const Tkey& key_recv = ptr_recv->first;
508 if (key_local == key_recv)
509 {
510 ptr_recv->second = ptr_recv->second + std::move(ptr_local->second);
511 ++ptr_local;
512 ++ptr_recv;
513 }
514 else if (key_local < key_recv)
515 {
516 ptr_recv = data_recv.emplace_hint(ptr_recv, key_local, std::move(ptr_local->second));
517 ++ptr_local;
518 }
519 else
520 {
521 ++ptr_recv;
522 }
523 }
524 for (; ptr_local != data_local.end(); ++ptr_local)
525 {
526 ptr_recv = data_recv.emplace_hint(ptr_recv, ptr_local->first, std::move(ptr_local->second));
527 }
528}
529
530template <typename Tkey0, typename Tkey1, typename Tvalue>
531void RI_2D_Comm::add_datas(std::map<Tkey0, std::map<Tkey1, Tvalue>>&& data_local,
532 std::map<Tkey0, std::map<Tkey1, Tvalue>>& data_recv)
533{
534 auto ptr_local = data_local.begin();
535 auto ptr_recv = data_recv.begin();
536 for (; ptr_local != data_local.end() && ptr_recv != data_recv.end();)
537 {
538 const Tkey0& key_local = ptr_local->first;
539 const Tkey0& key_recv = ptr_recv->first;
540 if (key_local == key_recv)
541 {
542 add_datas(std::move(ptr_local->second), ptr_recv->second);
543 ++ptr_local;
544 ++ptr_recv;
545 }
546 else if (key_local < key_recv)
547 {
548 ptr_recv = data_recv.emplace_hint(ptr_recv, key_local, std::move(ptr_local->second));
549 ++ptr_local;
550 }
551 else
552 {
553 ++ptr_recv;
554 }
555 }
556 for (; ptr_local != data_local.end(); ++ptr_local)
557 {
558 ptr_recv = data_recv.emplace_hint(ptr_recv, ptr_local->first, std::move(ptr_local->second));
559 }
560}
561
562#endif
Definition abfs_vector3_order.h:16
Class for k-points management.
Definition klist.h:20
std::vector< int > isk
distinguish spin up and down k points
Definition klist.h:22
3 elements vector
Definition vector3.h:21
T x
Definition vector3.h:23
T y
Definition vector3.h:24
T z
Definition vector3.h:25
static void start(void)
Start total time calculation.
Definition timer.cpp:44
static void end(const std::string &class_name_in, const std::string &name_in)
Definition timer.cpp:109
std::vector< ModuleBase::Vector3< double > > kvec_c
Cartesian coordinates of the points, in unit of 2*pi/lat0.
Definition reciprocal_grid.h:55
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
int irloc(const int irglb) const
Definition parallel_blkcyc.h:78
int icloc(const int icglb) const
Definition parallel_blkcyc.h:83
Definition parallel_orbitals.h:16
std::vector< int > get_indexes_col() const
Definition parallel_orbitals.cpp:151
std::vector< int > get_indexes_row() const
gather global indexes of orbitals in this processor get_indexes_row() : global indexes (~NLOCAL) of r...
Definition parallel_orbitals.cpp:137
Parallel_Blkcyclic & para_2d()
Definition parallel_orbitals.h:24
Definition rpa_lri_coeff_v1.h:11
Provide the basic information about unitcell.
Definition unitcell.h:16
ModuleBase::Matrix3 & latvec
Definition unitcell.h:142
Definition atom_pair.h:45
const BaseMatrix< T > * find_matrix(const int &rx_in, const int &ry_in, const int &rz_in) const
Definition atom_pair.cpp:499
ModuleBase::Vector3< int > get_R_index(const int &index) const
Definition atom_pair.cpp:908
size_t get_R_size() const
Definition atom_pair.h:391
int get_atom_j() const
Definition atom_pair.cpp:370
int get_atom_i() const
get atom_i and atom_j for this AtomPair
Definition atom_pair.cpp:363
Definition base_matrix.h:20
void add_element(int mu, int nu, const T &value)
add a single element to the matrix
Definition base_matrix.h:57
T & get_value(const size_t &i_row, const size_t &j_col) const
get value from a whole matrix
Definition base_matrix.h:74
Definition hcontainer.h:147
size_t size_atom_pairs() const
calculate number of AtomPairs for current R index
Definition hcontainer.cpp:832
BaseMatrix< T > * find_matrix(int i, int j, int rx, int ry, int rz)
find BaseMatrix with atom index atom_i and atom_j and R index (rx, ry, rz) This interface can be used...
Definition hcontainer.cpp:426
AtomPair< T > & get_atom_pair(int i, int j) const
return a reference of AtomPair with index of atom I and J in atom_pairs
Definition hcontainer.cpp:528
#define T
Definition exp.cpp:237
void set_mat2d(const int &global_ir, const int &global_ic, const T &v, const Parallel_Orbitals &pv, T *mat)
Definition lcao_set_mat2d.cpp:13
const double TWO_PI
Definition constants.h:21
const std::complex< double > IMAG_UNIT(0.0, 1.0)
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18
std::vector< int > get_ik_list(const K_Vectors &kv, const int is_k)
Definition ri_2d_comm.cpp:65
std::array< Tcell, Ndim > TC
Definition ri_2d_comm.h:28
std::pair< TA, TC > TAC
Definition ri_2d_comm.h:29
int get_is_block(const int is_k, const int is_row_b, const int is_col_b)
Definition ri_2d_comm.cpp:91
std::map< TA, std::map< TAC, T > > comm_map2(const MPI_Comm &mpi_comm, const std::map< TA, std::map< TAC, T > > &Ds_in, const Tjudge &judge)
Definition ri_2d_comm.hpp:460
std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > split_m2D_ktoR_gamma(const UnitCell &ucell, const std::vector< const Tmatrix * > &mks_2D, const Parallel_Blkcyclic &pv, const int nspin)
std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > split_m2D_ktoR(const UnitCell &ucell, const K_Vectors &kv, const std::vector< const Tmatrix * > &mks_2D, const Parallel_Blkcyclic &pv, const int nspin, const bool spgsym=false)
int TA
Definition ri_2d_comm.h:25
std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > split_m2D_ktoR_k(const UnitCell &ucell, const K_Vectors &kv, const std::vector< const Tmatrix * > &mks_2D, const Parallel_Blkcyclic &pv, const int nspin, const bool spgsym=false)
void set_value_add(Tkey &&key, Tvalue &&value, std::map< Tkey, Tvalue > &data)
Definition ri_2d_comm.hpp:480
std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > dm_container_to_Ds(const std::vector< hamilt::HContainer< TR > * > &dm_container, const UnitCell &ucell, const Parallel_Orbitals &pv, const int nspin)
std::tuple< int, int > split_is_block(const int is_b)
Definition ri_2d_comm.cpp:103
void add_HexxR(const int current_spin, const double alpha, const std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > &Hs, const Parallel_Orbitals &pv, const int npol, hamilt::HContainer< TR > &HlocR, const RI::Cell_Nearest< int, int, 3, double, 3 > *const cell_nearest=nullptr)
Definition ri_2d_comm.hpp:318
std::tuple< int, int, int > get_iat_iw_is_block(const UnitCell &ucell, const int &iwt)
Definition ri_2d_comm.cpp:76
std::map< TA, std::map< TAC, T > > comm_map2_first(const MPI_Comm &mpi_comm, const std::map< TA, std::map< TAC, T > > &Ds_in, const std::set< TA > &s0, const std::set< TA > &s1)
Definition ri_2d_comm.hpp:448
void add_Hexx(const UnitCell &ucell, const K_Vectors &kv, const int ik, const double alpha, const std::vector< std::map< TA, std::map< TAC, RI::Tensor< Tdata > > > > &Hs, const Parallel_Orbitals &pv, TK *hk)
Definition ri_2d_comm.hpp:268
int get_iwt(const UnitCell &ucell, const int iat, const int iw_b, const int is_b)
Definition ri_2d_comm.cpp:118
void add_datas(std::map< Tkey, Tvalue > &&data_local, std::map< Tkey, Tvalue > &data_recv)
Definition ri_2d_comm.hpp:499
std::vector< std::array< Tcell, 1 > > get_Born_von_Karmen_cells(const std::array< Tcell, 1 > &Born_von_Karman_period)
Definition ri_util_bvk.h:36
RI::Tensor< Tdata > Vector_to_Tensor(const std::vector< Tdata > &m_old, const int nr, const int nc)
Definition ri_util.h:50
std::array< int, 3 > get_Born_vonKarmen_period(const K_Vectors &kv)
Definition ri_util_bvk.h:21
ModuleBase::Vector3< Tcell > array3_to_Vector3(const std::array< Tcell, 3 > &v)
Definition ri_util.h:32
Parameter PARAM
Definition parameter.cpp:3
RI::Tensor< double > tensor_conj(const RI::Tensor< double > &t)
Definition ri_2d_comm.hpp:29
std::array< int, 3 > TC
Definition test_write_ri_cv.cpp:9