ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
dftu_nao_ijr.h
Go to the documentation of this file.
1#ifndef DFTU_LCAO_IJR_H
2#define DFTU_LCAO_IJR_H
3
6#include "source_cell/module_neighbor/sltk_grid_driver.h" // AdjacentAtomInfo (complete type needed)
10
11#include <cassert>
12#include <cmath>
13#include <complex>
14#include <unordered_map>
15#include <vector>
16
17namespace DFTU_LCAO
18{
19
22using NlmTot = std::vector<std::vector<std::unordered_map<int, std::vector<double>>>>;
23
36 const int iat0,
37 const int target_L,
38 const int nspin,
39 const int current_spin,
40 std::vector<double>& occ)
41{
42 if (nspin == 4)
43 {
44 dftu.occmat().get_flat(iat0, target_L, occ);
45 }
46 else
47 {
48 for (int i = 0; i < static_cast<int>(occ.size()); i++)
49 {
50 occ[i] = dftu.occmat().get(iat0, target_L, current_spin,
51 i / (2 * target_L + 1), i % (2 * target_L + 1));
52 }
53 }
54}
55
72template <typename TR>
73void cal_hr_ijr(const int iat1,
74 const int iat2,
75 const int npol,
76 const Parallel_Orbitals& pv,
77 const std::unordered_map<int, std::vector<double>>& nlm1_all,
78 const std::unordered_map<int, std::vector<double>>& nlm2_all,
79 const std::vector<TR>& pot_onsite,
80 TR* data_pointer)
81{
82 assert(iat1 >= 0);
83 assert(iat2 >= 0);
84 assert(npol > 0);
85 assert(data_pointer != nullptr);
86 std::vector<int> row_indexes = pv.get_indexes_row(iat1);
87 std::vector<int> col_indexes = pv.get_indexes_col(iat2);
88 const int m_size = int(sqrt(pot_onsite.size()) / npol);
89 // step_trace = 0 for NSPIN=1,2; ={0, 1, local_col, local_col+1} for NSPIN=4
90 std::vector<int> step_trace(npol * npol, 0);
91 for (int is = 0; is < npol; is++)
92 {
93 for (int is2 = 0; is2 < npol; is2++)
94 {
95 step_trace[is * npol + is2] = pv.get_ncol_atom(iat2) * is + is2;
96 }
97 }
98 // calculate the local matrix
99 for (int iw1l = 0; iw1l < int(row_indexes.size()); iw1l += npol)
100 {
101 const std::vector<double>& nlm1 = nlm1_all.find(row_indexes[iw1l])->second;
102 for (int iw2l = 0; iw2l < int(col_indexes.size()); iw2l += npol)
103 {
104 const std::vector<double>& nlm2 = nlm2_all.find(col_indexes[iw2l])->second;
105#ifdef __DEBUG
106 assert(nlm1.size() == nlm2.size());
107#endif
108 for (int is = 0; is < npol * npol; ++is)
109 {
110 int start = is * m_size * m_size;
111 TR nlm_tmp = TR(0);
112 for (int m1 = 0; m1 < m_size; m1++)
113 {
114 for (int m2 = 0; m2 < m_size; m2++)
115 {
116 nlm_tmp += nlm1[m1] * nlm2[m2] * pot_onsite[m1 * m_size + m2 + start];
117 }
118 }
119 data_pointer[step_trace[is]] += nlm_tmp;
120 }
121 data_pointer += npol;
122 }
123 data_pointer += (npol - 1) * col_indexes.size();
124 }
125}
126
142inline void cal_occ_ijr(const int iat1,
143 const int iat2,
144 const int npol,
145 const Parallel_Orbitals& pv,
146 const std::unordered_map<int, std::vector<double>>& nlm1_all,
147 const std::unordered_map<int, std::vector<double>>& nlm2_all,
148 const double* dm_pointer,
149 std::vector<double>& occ)
150{
151 assert(iat1 >= 0);
152 assert(iat2 >= 0);
153 assert(npol > 0);
154 assert(dm_pointer != nullptr);
155 std::vector<int> row_indexes = pv.get_indexes_row(iat1);
156 std::vector<int> col_indexes = pv.get_indexes_col(iat2);
157 const int m_size = int(sqrt(occ.size()) / npol);
158 const int m_size2 = m_size * m_size;
159#ifdef __DEBUG
160 assert(m_size2 * npol * npol == int(occ.size()));
161#endif
162 // step_trace = 0 for NSPIN=1,2; ={0, 1, local_col, local_col+1} for NSPIN=4
163 std::vector<int> step_trace(npol * npol, 0);
164 for (int is = 0; is < npol; is++)
165 {
166 for (int is2 = 0; is2 < npol; is2++)
167 {
168 step_trace[is * npol + is2] = pv.get_ncol_atom(iat2) * is + is2;
169 }
170 }
171 for (int iw1l = 0; iw1l < int(row_indexes.size()); iw1l += npol)
172 {
173 const std::vector<double>& nlm1 = nlm1_all.find(row_indexes[iw1l])->second;
174 for (int iw2l = 0; iw2l < int(col_indexes.size()); iw2l += npol)
175 {
176 const std::vector<double>& nlm2 = nlm2_all.find(col_indexes[iw2l])->second;
177#ifdef __DEBUG
178 assert(nlm1.size() == nlm2.size());
179#endif
180 for (int is1 = 0; is1 < npol; ++is1)
181 {
182 for (int is2 = 0; is2 < npol; ++is2)
183 {
184 for (int m1 = 0; m1 < m_size; ++m1)
185 {
186 for (int m2 = 0; m2 < m_size; ++m2)
187 {
188 occ[m1 * m_size + m2 + (is1 * npol + is2) * m_size2]
189 += nlm1[m1] * nlm2[m2] * dm_pointer[step_trace[is1 * npol + is2]];
190 }
191 }
192 }
193 }
194 dm_pointer += npol;
195 }
196 dm_pointer += (npol - 1) * col_indexes.size();
197 }
198}
199
220template <typename Body>
221inline void for_adj_pair(const UnitCell& ucell,
222 const int iat0,
223 const AdjacentAtomInfo& adjs,
224 const NlmTot& nlm_tot,
225 Body&& body)
226{
227 for (int ad1 = 0; ad1 < adjs.adj_num + 1; ++ad1)
228 {
229 const int T1 = adjs.ntype[ad1];
230 const int I1 = adjs.natom[ad1];
231 const int iat1 = ucell.itia2iat(T1, I1);
232 const ModuleBase::Vector3<int>& R_index1 = adjs.box[ad1];
233 const std::unordered_map<int, std::vector<double>>& nlm1 = nlm_tot[iat0][ad1];
234 for (int ad2 = 0; ad2 < adjs.adj_num + 1; ++ad2)
235 {
236 const int T2 = adjs.ntype[ad2];
237 const int I2 = adjs.natom[ad2];
238 const int iat2 = ucell.itia2iat(T2, I2);
239 const std::unordered_map<int, std::vector<double>>& nlm2 = nlm_tot[iat0][ad2];
240 const ModuleBase::Vector3<int>& R_index2 = adjs.box[ad2];
241 ModuleBase::Vector3<int> R_vector(R_index2[0] - R_index1[0],
242 R_index2[1] - R_index1[1],
243 R_index2[2] - R_index1[2]);
244 body(iat1, iat2, R_vector, nlm1, nlm2);
245 }
246 }
247}
248
265template <typename TR>
268 const NlmTot& nlm_tot,
269 const int iat0,
270 const AdjacentAtomInfo& adjs,
271 const Parallel_Orbitals& pv,
272 const std::vector<TR>& pot_onsite)
273{
274 for_adj_pair(ucell, iat0, adjs, nlm_tot,
275 [&](int iat1, int iat2,
276 const ModuleBase::Vector3<int>& R_vector,
277 const std::unordered_map<int, std::vector<double>>& nlm1,
278 const std::unordered_map<int, std::vector<double>>& nlm2)
279 {
280 hamilt::BaseMatrix<TR>* tmp = hR->find_matrix(iat1, iat2, R_vector[0], R_vector[1], R_vector[2]);
281 if (tmp != nullptr)
282 {
283#ifdef _OPENMP
284#pragma omp critical(dftu_hr_update)
285#endif
286 {
287 cal_hr_ijr<TR>(iat1, iat2, ucell.get_npol(), pv,
288 nlm1, nlm2, pot_onsite, tmp->get_pointer());
289 }
290 }
291 });
292}
293
312inline void compute_occ_from_dmr(const UnitCell& ucell,
314 const int iat0,
315 const int target_L,
316 const int current_spin,
317 const int nspin,
318 const AdjacentAtomInfo& adjs,
319 const Parallel_Orbitals& pv,
320 const NlmTot& nlm_tot,
321 const hamilt::HContainer<double>& dmR_current,
322 std::vector<double>& occ)
323{
324 for_adj_pair(ucell, iat0, adjs, nlm_tot,
325 [&](int iat1, int iat2,
326 const ModuleBase::Vector3<int>& R_vector,
327 const std::unordered_map<int, std::vector<double>>& nlm1,
328 const std::unordered_map<int, std::vector<double>>& nlm2)
329 {
331 = dmR_current.find_matrix(iat1, iat2, R_vector[0], R_vector[1], R_vector[2]);
332 if (tmp != nullptr)
333 {
334 cal_occ_ijr(iat1, iat2, ucell.get_npol(), pv,
335 nlm1, nlm2, tmp->get_pointer(), occ);
336 }
337 });
338 Parallel_Reduce::reduce_all(occ.data(), occ.size());
339 if (nspin == 1)
340 {
341 for (double& v : occ) { v *= 0.5; }
342 }
343 dftu.occmat().set_flat(iat0, target_L, current_spin, occ);
344}
345
346} // namespace DFTU_LCAO
347
348#endif
Struct of array for packing the Adjacent atom information.
Definition sltk_grid_driver.h:21
int adj_num
number of adjacent atoms
Definition sltk_grid_driver.h:30
std::vector< ModuleBase::Vector3< int > > box
box indices of adjacent atoms
Definition sltk_grid_driver.h:34
std::vector< int > ntype
types of adjacent atoms
Definition sltk_grid_driver.h:31
std::vector< int > natom
indices of adjacent atoms
Definition sltk_grid_driver.h:32
3 elements vector
Definition vector3.h:21
Definition parallel_orbitals.h:16
std::vector< int > get_indexes_col() const
Definition parallel_orbitals.cpp:151
int get_ncol_atom(int iat) const
dimension getters for 2D-block-cyclic division of Hamiltonian matrix ncloc() : total number of column...
Definition parallel_orbitals.cpp:87
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
Definition dftu_base.h:21
Provide the basic information about unitcell.
Definition unitcell.h:16
const int & get_npol() const
Get npol.
Definition unitcell.h:66
ModuleBase::IntArray itia2iat
(it, ia)==>iat, the index in nat (add 2009-3-2 by mohan)
Definition unitcell.h:160
Definition base_matrix.h:20
T * get_pointer() const
get pointer of value from a submatrix
Definition base_matrix.h:86
Definition hcontainer.h:147
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
Definition dftu_nao_adj.cpp:14
void accumulate_hr_for_iat0(const UnitCell &ucell, hamilt::HContainer< TR > *hR, const NlmTot &nlm_tot, const int iat0, const AdjacentAtomInfo &adjs, const Parallel_Orbitals &pv, const std::vector< TR > &pot_onsite)
accumulate the real-space HR contributions of one Hubbard atom (iat0) from the precomputed pot_onsite...
Definition dftu_nao_ijr.h:266
void cal_occ_ijr(const int iat1, const int iat2, const int npol, const Parallel_Orbitals &pv, const std::unordered_map< int, std::vector< double > > &nlm1_all, const std::unordered_map< int, std::vector< double > > &nlm2_all, const double *dm_pointer, std::vector< double > &occ)
accumulate one atom-pair contribution to the DFT+U occupation matrix: occ_mm' += sum_R DMR(I,...
Definition dftu_nao_ijr.h:142
std::vector< std::vector< std::unordered_map< int, std::vector< double > > > > NlmTot
Definition dftu_nao_ijr.h:22
void load_occ_from_file(const Plus_U_Base &dftu, const int iat0, const int target_L, const int nspin, const int current_spin, std::vector< double > &occ)
load the flattened occupation matrix of one Hubbard atom from pre-read occ_mat data.
Definition dftu_nao_ijr.h:35
void compute_occ_from_dmr(const UnitCell &ucell, Plus_U_Base &dftu, const int iat0, const int target_L, const int current_spin, const int nspin, const AdjacentAtomInfo &adjs, const Parallel_Orbitals &pv, const NlmTot &nlm_tot, const hamilt::HContainer< double > &dmR_current, std::vector< double > &occ)
compute the occupation matrix of one Hubbard atom (iat0) from the real-space density matrix: occ(m,...
Definition dftu_nao_ijr.h:312
void cal_hr_ijr(const int iat1, const int iat2, const int npol, const Parallel_Orbitals &pv, const std::unordered_map< int, std::vector< double > > &nlm1_all, const std::unordered_map< int, std::vector< double > > &nlm2_all, const std::vector< TR > &pot_onsite, TR *data_pointer)
accumulate one real-space HR atom-pair block for DFT+U: HR += <psi_I|beta_m> * pot_onsite(m,...
Definition dftu_nao_ijr.h:73
void for_adj_pair(const UnitCell &ucell, const int iat0, const AdjacentAtomInfo &adjs, const NlmTot &nlm_tot, Body &&body)
walk all (ad1, ad2) adjacent-atom pairs of one Hubbard atom iat0 and invoke body for each pair that h...
Definition dftu_nao_ijr.h:221
void reduce_all(T &object)
reduce in all process
Definition depend_mock.cpp:14
Grid_Driver class for neighbor search interface.
Plus_U_Base dftu
Definition test_dftu_nao_op.cpp:11
iclock::time_point start
Definition test_partition.cpp:22