ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
cal_hs_grad.h
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_CAL_HS_GRAD_H
2#define ABACUS_SOURCE_LCAO_MODULE_LR_CAL_HS_GRAD_H
3#include <cassert>
9
10inline void filter_adjs_by_rcut(const UnitCell& ucell,
11 const int iat0,
12 AdjacentAtomInfo& adjs)
13{
14 std::vector<bool> is_adj(adjs.adj_num + 1, false);
15 for (int ad = 0;ad < adjs.adj_num + 1;++ad)
16 {
17 const int it0 = ucell.iat2it[iat0];
18 const int it1 = adjs.ntype[ad];
19 const int ia1 = adjs.natom[ad];
20 const int iat1 = ucell.itia2iat(it1,ia1);
21 const ModuleBase::Vector3<int>& R_index1 = adjs.box[ad];
22 if(ucell.cal_dtau(iat0, iat1, R_index1).norm() * ucell.lat0 < ucell.atoms[it0].Rcut + ucell.atoms[it1].Rcut - 1e-15)
23 {
24 is_adj[ad] = true;
25 }
26 }
27 filter_adjs(is_adj, adjs);
28}
29
32template<typename TR>
34{
35 hamilt::HContainer<TR> hcontainer(&pv);
36 for (int iat0 = 0;iat0 < ucell.nat; ++iat0)
37 {
38 const int it0 = ucell.iat2it[iat0];
39 const int ia0 = ucell.iat2ia[iat0];
41 gd.Find_atom(ucell, ucell.get_tau(iat0), it0, ia0, &adjs);
42 filter_adjs_by_rcut(ucell, iat0, adjs);
43
44 for (int ad = 0;ad < adjs.adj_num + 1;++ad)
45 {
46 const int it1 = adjs.ntype[ad];
47 const int ia1 = adjs.natom[ad];
48 const int iat1 = ucell.itia2iat(it1, ia1);
49 if (pv.get_nrow_atom(iat0) * pv.get_ncol_atom(iat1))
50 {
51 hamilt::AtomPair<TR> ap(iat0, iat1, adjs.box[ad], &pv);
52 hcontainer.insert_pair(ap);
53 }
54 }
55 }
56 hcontainer.allocate(nullptr, true);
57 return hcontainer;
58}
59
61inline std::vector<hamilt::HContainer<double>> cal_hs_grad(const char job,
62 const UnitCell& ucell,
63 const Parallel_Orbitals& pv,
64 const Grid_Driver& gd,
65 const TwoCenterBundle& two_center_bundle)
66{
67 if(job != 'S' && job != 'T')
68 {
69 throw std::invalid_argument("job must be 'S' or 'T'");
70 }
71 // allocate dHS (better to use HContainer<Vector3<double>> for access continuity)
72 // hamilt::HContainer<std::array<double, 3>> dHS(&pv);
73 std::vector<hamilt::HContainer<double>> dHS(3, build_hcontainer_local_op<double>(ucell, gd, pv));
74 // std::vector<hamilt::HContainer<double>> dHS(3, build_hcontainer_local_op<double>(ucell, gd, pv));
75 std::vector<double> tmp_deriv(3);
76 const int npol = ucell.get_npol();
77
78 for (int ixyz = 0;ixyz < 3;++ixyz)
79 {
80 // traverse ijR to calculate dHS
81 std::vector<int> ijr_info = dHS.at(ixyz).get_ijr_info();
82 // std::vector<int> ijr_info = dHS.at(0).get_ijr_info();
83 std::vector<int>::iterator it = ijr_info.begin();
84 const int npairs = *it++;
85 int npairs_count = 0;
86 while (it != ijr_info.end())
87 {
88 ++npairs_count;
89 const int iat0 = *it++;
90 const int it0 = ucell.iat2it[iat0];
91 const Atom& atom0 = ucell.atoms[it0];
92 const ModuleBase::Vector3<double> tau0 = ucell.get_tau(iat0);
93 std::vector<int> row_indexes = pv.get_indexes_row(iat0);
94
95 const int iat1 = *it++;
96 const int it1 = ucell.iat2it[iat1];
97 const Atom& atom1 = ucell.atoms[it1];
98 const ModuleBase::Vector3<double> tau1 = ucell.get_tau(iat1);
99 std::vector<int> col_indexes = pv.get_indexes_col(iat1);
100
101 const int nR = *it++;
102
103 for (int iR = 0;iR < nR;++iR)
104 {
105 // Read the three components in separate statements. The order in which function
106 // arguments are evaluated is UNSPECIFIED in C++, so `Vector3<double>(*it++, *it++, *it++)`
107 // may store the R triple permuted and silently corrupt periodic systems.
108 const int Rx = *it++;
109 const int Ry = *it++;
110 const int Rz = *it++;
111 const ModuleBase::Vector3<double> R(Rx, Ry, Rz); // int to double
112 ModuleBase::Vector3<double> relative_position = (tau1 - tau0 + R * ucell.latvec) * ucell.lat0;
113 hamilt::BaseMatrix<double>* dHS_block = dHS[ixyz].find_matrix(iat0, iat1, R.x, R.y, R.z);
114 // `ijr_info` came from this very container, so a miss means the indices are wrong.
115 assert(dHS_block != nullptr);
116
117 // OMP can be used here
118 for (int lw0 = 0;lw0 < row_indexes.size();lw0 += npol) // spin 1-3 of dHS is not needed at nspin=4
119 {
120 const int gw0 = row_indexes[lw0] / npol;
121 const int l0 = atom0.iw2l[gw0];
122 const int n0 = atom0.iw2n[gw0];
123 const int m0 = atom0.iw2m[gw0];
124 const int M0 = (m0 % 2 == 0) ? -m0 / 2 : (m0 + 1) / 2; // convert m (0,1,...2l) to M (-l, -l+1, ..., l-1, l)
125 for (int lw1 = 0;lw1 < col_indexes.size();lw1 += npol)
126 {
127 const int& gw1 = col_indexes[lw1] / npol;
128 const int l1 = atom1.iw2l[gw1];
129 const int n1 = atom1.iw2n[gw1];
130 const int m1 = atom1.iw2m[gw1];
131 const int M1 = (m1 % 2 == 0) ? -m1 / 2 : (m1 + 1) / 2;
132 switch (job)
133 {
134 case 'S':
135 two_center_bundle.overlap_orb->calculate(it0, l0, n0, M0, it1, l1, n1, M1,
136 relative_position, nullptr, tmp_deriv.data());
137 break;
138 case 'T':
139 two_center_bundle.kinetic_orb->calculate(it0, l0, n0, M0, it1, l1, n1, M1,
140 relative_position, nullptr, tmp_deriv.data());
141 break;
142 }
143 dHS_block->get_value(lw0, lw1) = tmp_deriv[ixyz];
144 }
145 }
146 }
147 }
148 assert(npairs == npairs_count);
149 }
150 return dHS;
151}
152#endif // ABACUS_SOURCE_LCAO_MODULE_LR_CAL_HS_GRAD_H
std::vector< hamilt::HContainer< double > > cal_hs_grad(const char job, const UnitCell &ucell, const Parallel_Orbitals &pv, const Grid_Driver &gd, const TwoCenterBundle &two_center_bundle)
Calculate <dPhi|phi> or <dPhi|T|phi> by 2-center integration.
Definition cal_hs_grad.h:61
void filter_adjs_by_rcut(const UnitCell &ucell, const int iat0, AdjacentAtomInfo &adjs)
Definition cal_hs_grad.h:10
hamilt::HContainer< TR > build_hcontainer_local_op(const UnitCell &ucell, const Grid_Driver &gd, const Parallel_Orbitals &pv)
Return a local Hamiltonian operator in type HContainer. It can replace OverlapNew::initialize_SR() an...
Definition cal_hs_grad.h:33
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
Atom class for storing atom information.
Definition atom_spec.h:14
std::vector< int > iw2m
use iw to find m
Definition atom_spec.h:31
double Rcut
cut-off radius (pengfei Li 16-2-29)
Definition atom_spec.h:47
std::vector< int > iw2l
use iw to find L
Definition atom_spec.h:33
std::vector< int > iw2n
use iw to find n
Definition atom_spec.h:32
Grid_Driver class for neighbor search interface.
Definition sltk_grid_driver.h:63
void Find_atom(const UnitCell &ucell, const int ntype, const int nnumber, AdjacentAtomInfo *adjs=nullptr) const
Find adjacent atoms for a given atom.
Definition sltk_grid_driver.cpp:24
3 elements vector
Definition vector3.h:21
T x
Definition vector3.h:23
T norm(void) const
Get the norm of a Vector3.
Definition vector3.h:213
T y
Definition vector3.h:24
T z
Definition vector3.h:25
Definition parallel_orbitals.h:16
std::vector< int > get_indexes_col() const
Definition parallel_orbitals.cpp:151
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
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 two_center_bundle.h:11
std::unique_ptr< TwoCenterIntegrator > kinetic_orb
Definition two_center_bundle.h:44
std::unique_ptr< TwoCenterIntegrator > overlap_orb
Definition two_center_bundle.h:45
Provide the basic information about unitcell.
Definition unitcell.h:16
Atom * atoms
Definition unitcell.h:165
const int & get_npol() const
Get npol.
Definition unitcell.h:66
const ModuleBase::Vector3< double > & get_tau(const int &iat) const
Get tau for atom iat.
Definition unitcell.h:104
double & lat0
Definition unitcell.h:135
ModuleBase::Matrix3 & latvec
Definition unitcell.h:142
std::vector< int > iat2ia
iat==>ia
Definition unitcell.h:157
ModuleBase::IntArray itia2iat
(it, ia)==>iat, the index in nat (add 2009-3-2 by mohan)
Definition unitcell.h:160
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 ModuleBase::Vector3< double > cal_dtau(const int &iat1, const int &iat2, const ModuleBase::Vector3< int > &R) const
Calculate vector between two atoms with R cell.
Definition unitcell.h:110
Definition atom_pair.h:45
Definition base_matrix.h:20
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
void insert_pair(const AtomPair< T > &atom_ij)
a AtomPair object can be inserted into HContainer, two steps: 1, find AtomPair with atom index atom_i...
Definition hcontainer.cpp:880
void allocate(T *data_array=nullptr, bool if_zero=false)
allocate memory for all <IJR> matrix if data_array is not nullptr, use memory after data_array for ea...
Definition hcontainer.cpp:324
void filter_adjs(const std::vector< bool > &is_adj, AdjacentAtomInfo &adjs)
Filter adjacent atoms based on boolean mask.
Definition sltk_grid_driver.cpp:66
Grid_Driver class for neighbor search interface.