ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
symm_rotation_r_hcontainer.hpp
Go to the documentation of this file.
1#include "symm_rotation.h"
4namespace ModuleSymmetry
5{
6 template<typename TR>
7 inline void print_global(const TR* tlocal, const int nr, const int nc, const std::string name)
8 {
9 GlobalV::ofs_running << name << std::endl;
10 for (int i = 0;i < nr;++i)
11 {
12 for (int j = 0;j < nc;++j) GlobalV::ofs_running << tlocal[j + i * nc] << " ";
14 }
15 }
16 template<typename TR>
17 inline void print_local(const Parallel_Orbitals& pv, const TR* tlocal, const std::string name)
18 {
19 GlobalV::ofs_running << name << std::endl;
20 for (int i = 0; i < pv.para_2d().nrloc(); ++i)
21 {
22 for (int j = 0; j < pv.para_2d().ncloc(); ++j)
23 GlobalV::ofs_running << tlocal[j + i * pv.para_2d().ncloc()] << " ";
25 }
26 }
27 template<typename TR>
28 inline void print_atompair_local(const Parallel_Orbitals& pv, const int iat1, const int iat2, const TR* tlocal, const std::string name)
29 {
30 GlobalV::ofs_running << name << std::endl;
31 for (int i = 0;i < pv.get_nrow_atom(iat1);++i)
32 {
33 for (int j = 0;j < pv.get_ncol_atom(iat2);++j) GlobalV::ofs_running << tlocal[j + i * pv.get_ncol_atom(iat2)] << " ";
35 }
36 }
37
38 template<typename TR> // HContainer type
40 const Symmetry& symm, const Atom* atoms,
41 const std::vector<int>& iat2it, const std::vector<int>& iat2ia, const char mode,
42 const hamilt::HContainer<TR>& HR_irreduceble,
43 hamilt::HContainer<TR>& HR_rotated)const
44 {
45 ModuleBase::TITLE("Symmetry_rotation", "restore_HR");
46 ModuleBase::timer::start("Symmetry_rotation", "restore_HR");
47 for (auto& apR_isym_irapR : this->irs_.full_map_to_irreducible_sector_)
48 {
49 const Tap& ap = apR_isym_irapR.first.first;
50 const TC& R = apR_isym_irapR.first.second;
51 const int& isym = apR_isym_irapR.second.first;
52 const Tap& irap = apR_isym_irapR.second.second.first;
53 const TC& irR = apR_isym_irapR.second.second.second;
54 assert(irR == this->irs_.rotate_R(symm, isym, ap.first, ap.second, R));
55 // get in and out pointer from HContainer
56 const hamilt::AtomPair<TR>& irap_hc = HR_irreduceble.get_atom_pair(irap.first, irap.second);
57 const int irR_hc = irap_hc.find_R(irR[0], irR[1], irR[2]);
58 if (irR_hc < 0) continue;
59 const TR* irijR_ptr = irap_hc.get_pointer(irR_hc);
60 const hamilt::AtomPair<TR>& ap_hc = HR_rotated.get_atom_pair(ap.first, ap.second);
61 const int R_hc = ap_hc.find_R(R[0], R[1], R[2]);
62 if (R_hc < 0) continue;
63 TR* ijR_ptr = ap_hc.get_pointer(R_hc);
64 rotate_atompair_parallel(irijR_ptr, isym, atoms, iat2it, iat2ia, irap, ap, mode, *irap_hc.get_paraV(), ijR_ptr);
65 }
66 ModuleBase::timer::end("Symmetry_rotation", "restore_HR");
67 }
68
69 inline void set_block(const int starti, const int startj, const int nr, const ModuleBase::ComplexMatrix& block, double* obj)
70 { // row-major (col-contiguous)
71 for (int i = 0;i < block.nr;++i)
72 for (int j = 0;j < block.nc;++j)
73 obj[starti + i + (startj + j) * nr] = block(j, i).real();
74 };
75 inline void set_block(const int starti, const int startj, const int nr, const ModuleBase::ComplexMatrix& block, std::complex<double>* obj)
76 { // row-major (col-contiguous)
77 for (int i = 0;i < block.nr;++i)
78 for (int j = 0;j < block.nc;++j)
79 obj[starti + i + (startj + j) * nr] = block(j, i);
80 };
81 template<typename TR>
82 void Symmetry_rotation::rotate_atompair_parallel(const TR* Alocal_in, const int isym, const Atom* atoms,
83 const std::vector<int>& iat2it, const std::vector<int>& iat2ia,
84 const Tap& ap_in, const Tap& ap_out, const char mode, const Parallel_Orbitals& pv, TR* Alocal_out, const bool output)const
85 {
86 // all the matrices are row-major (col-contiguous)
87 int iat1 = ap_in.first, iat2 = ap_in.second;
88 int it1 = iat2it[iat1], it2 = iat2it[iat2];
89 int ia1 = iat2ia[iat1], ia2 = iat2ia[iat2];
90 // contruct T matrix
91 std::vector<TR> T1, T2;
92 auto set_rotation_matrix = [&](const int& it, const int& ia, std::vector<TR>& T)->int
93 {
94 T.resize(atoms[it].nw * atoms[it].nw, 0.0);
95 const int iwstart = atoms[it].stapos_wf + ia * atoms[it].nw;
96 int iw = 0;
97 while (iw < atoms[it].nw)
98 {
99 int l = atoms[it].iw2l[iw];
100 int nm = 2 * l + 1;
101 // this->set_block_to_mat2d(iwstart, iwstart, this->rotmat_Slm_[isym][l], T_full_2d, pv);
102 set_block(iw, iw, atoms[it].nw, this->rotmat_Slm_[isym][l], T.data());
103 iw += nm;
104 }
105 return iwstart;
106 };
107 int iw1start = set_rotation_matrix(it1, ia1, T1);
108 int iw2start = set_rotation_matrix(it2, ia2, T2);
109
110 // copy Aocal_in to a global atom-pair matrix
111 std::vector<TR> A(atoms[it1].nw * atoms[it2].nw, 0.0);
112
113 int abr1 = pv.atom_begin_row[iat1], abc2 = pv.atom_begin_col[iat2];
114 if (abr1 >= 0 && abc2 >= 0)
115 { // "pv.para_2d().irglb(i) - iw1start": global index in current atom pair
116 for (int j = 0;j < pv.get_ncol_atom(iat2);++j)
117 for (int i = 0;i < pv.get_nrow_atom(iat1);++i)
118 A[(pv.para_2d().irglb(i + abr1) - iw1start) * atoms[it2].nw
119 + (pv.para_2d().icglb(j + abc2) - iw2start)]
120 = Alocal_in[j + i * pv.get_ncol_atom(iat2)];
121 }
122 if (output) print_global(A.data(), atoms[it1].nw, atoms[it2].nw, "A before allreduce");
123 Parallel_Reduce::reduce_all(A.data(), A.size());
124
125 // rotate
126 const char notrans = 'N', transpose = 'T', dagger = 'C';
127 // const std::complex<double> alpha(1.0, 0.0), beta(0.0, 0.0);
128 std::vector<TR> AT2(atoms[it1].nw * atoms[it2].nw, 0.0);
129 std::vector<TR> TAT(atoms[it1].nw * atoms[it2].nw, 0.0);
130 if (mode == 'H')
131 { // H'=T1^\dagger * H * T2
132 BlasConnector::gemm(notrans, notrans, atoms[it1].nw, atoms[it2].nw, atoms[it2].nw,
133 1.0, A.data(), atoms[it2].nw, T2.data(), atoms[it2].nw, 0.0, AT2.data(), atoms[it2].nw);
134 BlasConnector::gemm(dagger, notrans, atoms[it1].nw, atoms[it2].nw, atoms[it1].nw,
135 1.0, T1.data(), atoms[it1].nw, AT2.data(), atoms[it2].nw, 0.0, TAT.data(), atoms[it2].nw);
136 }
137 else if (mode == 'D')
138 { // D' = T1^T * D * T2^* = T1^T * [T2^\dagger * D^T]^T
139 BlasConnector::gemm(dagger, transpose, atoms[it2].nw, atoms[it1].nw, atoms[it2].nw,
140 1.0, T2.data(), atoms[it2].nw, A.data(), atoms[it2].nw, 0.0, AT2.data(), atoms[it1].nw);
141 BlasConnector::gemm(transpose, transpose, atoms[it1].nw, atoms[it2].nw, atoms[it1].nw,
142 1.0, T1.data(), atoms[it1].nw, AT2.data(), atoms[it1].nw, 0.0, TAT.data(), atoms[it2].nw);
143 }
144 else throw std::invalid_argument("Symmetry_rotation::rotate_atompair_tensor: invalid mode.");
145
146 if (output)
147 {
148 print_global(A.data(), atoms[it1].nw, atoms[it2].nw, "A");
149 print_global(T1.data(), atoms[it1].nw, atoms[it1].nw, "T1");
150 print_global(TAT.data(), atoms[it1].nw, atoms[it2].nw, "TAT");
151 print_atompair_local(pv, iat1, iat2, Alocal_out, "Alocal_out");
152 }
153
154 // copy back to Alocal_out
155 iat1 = ap_out.first, iat2 = ap_out.second;
156 it1 = iat2it[iat1], it2 = iat2it[iat2];
157 ia1 = iat2ia[iat1], ia2 = iat2ia[iat2];
158 abr1 = pv.atom_begin_row[iat1], abc2 = pv.atom_begin_col[iat2];
159 iw1start = atoms[it1].stapos_wf + ia1 * atoms[it1].nw;
160 iw2start = atoms[it2].stapos_wf + ia2 * atoms[it2].nw;
161 if (abr1 >= 0 && abc2 >= 0)
162 {// ap_in index for TAT but ap_out index for Alocal_out
163 for (int j = 0;j < pv.get_ncol_atom(iat2);++j)
164 for (int i = 0;i < pv.get_nrow_atom(iat1);++i)
165 Alocal_out[j + i * pv.get_ncol_atom(iat2)]
166 = TAT[(pv.para_2d().irglb(i + abr1) - iw1start) * atoms[it2].nw
167 + (pv.para_2d().icglb(j + abc2) - iw2start)];
168 }
169 }
170}
Atom class for storing atom information.
Definition atom_spec.h:14
int nw
number of local orbitals (l,n,m) of this type
Definition atom_spec.h:36
int stapos_wf
start position of wave functions
Definition atom_spec.h:49
std::vector< int > iw2l
use iw to find L
Definition atom_spec.h:33
static void gemm(const char transa, const char transb, const int m, const int n, const int k, const float alpha, const float *a, const int lda, const float *b, const int ldb, const float beta, float *c, const int ldc, base_device::AbacusDevice_t device_type=base_device::AbacusDevice_t::CpuDevice)
Definition blas_connector_matrix.cpp:27
Definition complexmatrix.h:11
int nr
Definition complexmatrix.h:15
matrix real() const
Definition complexmatrix.cpp:265
int nc
Definition complexmatrix.h:16
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
Irred_Sector irs_
irreducible sector
Definition symm_rotation_k.h:190
std::vector< std::vector< ModuleBase::ComplexMatrix > > rotmat_Slm_
the rotation matrix under the basis of S_l^m. size: [nsym][lmax][nm*nm]
Definition symm_rotation_k.h:167
void rotate_atompair_parallel(const TR *Alocal_in, const int isym, const Atom *atoms, const std::vector< int > &iat2it, const std::vector< int > &iat2ia, const Tap &ap_in, const Tap &ap_out, const char mode, const Parallel_Orbitals &pv, TR *Alocal_out, const bool output=false) const
Definition symm_rotation_r_hcontainer.hpp:82
std::map< int, std::map< std::pair< int, TC >, RI::Tensor< Tdata > > > restore_HR(const Symmetry &symm, const Atom *atoms, const std::vector< int > &iat2it, const char mode, const std::map< int, std::map< std::pair< int, TC >, RI::Tensor< Tdata > > > &HR_irreduceble) const
Definition symm_rotation_r.hpp:79
RI::Tensor< Tdata > set_rotation_matrix(const Atom &a, const int &isym) const
Definition symm_rotation_r.hpp:295
Symmetry analysis class.
Definition symmetry.h:24
int irglb(const int irloc) const
Definition parallel_blkcyc.h:88
int ncloc() const
Definition parallel_blkcyc.h:40
Definition parallel_orbitals.h:16
int get_nrow_atom(int iat) const
Definition parallel_orbitals.cpp:110
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
Parallel_Blkcyclic & para_2d()
Definition parallel_orbitals.h:24
std::vector< int > atom_begin_col
Definition parallel_orbitals.h:117
std::vector< int > atom_begin_row
Definition parallel_orbitals.h:116
Definition atom_pair.h:45
const Parallel_Orbitals * get_paraV() const
get Parallel_Orbitals pointer of this AtomPair for checking 2d-block parallel
Definition atom_pair.cpp:389
T * get_pointer(int ir) const
get pointer of value from a submatrix
Definition atom_pair.cpp:950
int find_R(const int &rx_in, const int &ry_in, const int &rz_in) const
Definition atom_pair.cpp:424
Definition hcontainer.h:147
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
Definition output.h:13
#define T
Definition exp.cpp:237
std::ofstream ofs_running
Definition global_variable.cpp:38
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18
Definition klist_io.h:13
std::pair< int, int > Tap
Definition irred_sec.h:14
std::array< int, 3 > TC
Definition irred_sec.h:15
void print_local(const Parallel_Orbitals &pv, const TR *tlocal, const std::string name)
Definition symm_rotation_r_hcontainer.hpp:17
void set_block(const int starti, const int startj, const RI::Tensor< std::complex< double > > &block, RI::Tensor< Tdata > &obj_tensor)
Definition symm_rotation_r.hpp:284
void print_global(const TR *tlocal, const int nr, const int nc, const std::string name)
Definition symm_rotation_r_hcontainer.hpp:7
void print_atompair_local(const Parallel_Orbitals &pv, const int iat1, const int iat2, const TR *tlocal, const std::string name)
Definition symm_rotation_r_hcontainer.hpp:28
void reduce_all(T &object)
reduce in all process
Definition depend_mock.cpp:14