ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
phi_operator.h
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_HAMILT_MODULE_GINT_PHI_OPERATOR_H
2#define ABACUS_SOURCE_HAMILT_MODULE_GINT_PHI_OPERATOR_H
3
4#include "big_grid.h"
6#include <memory>
7#include <utility>
8#include <vector>
9
10namespace ModuleGint
11{
12
21{
22 public:
23 enum class TriPart
24 {
28 };
29
30 // constructor
31 PhiOperator() = default;
32
33 // set the big grid that the phiOperator is associated with
34 void set_bgrid(std::shared_ptr<const BigGrid> biggrid);
35
36 // getter
37 int get_rows() const { return rows_; }
38 int get_cols() const { return cols_; }
39
40 // get phi of the big grid
41 // the dimension of phi is num_mgrids * (\sum_{i=0}^{atoms_->size()} atoms_[i]->nw)
42 template <typename T>
43 void set_phi(T *phi) const;
44
45 // get phi and the gradient of phi of the big grid
46 // the dimension of phi and dphi is num_mgrids * (\sum_{i=0}^{atoms_->size()} atoms_[i]->nw)
47 // if you do not need phi, you can set phi to nullptr.
48 void set_phi_dphi(
49 double *phi,
50 double *dphi_x,
51 double *dphi_y,
52 double *dphi_z) const;
53
54 // get the hessian of the wave function values of the big grid
55 // the dimension of ddphi is num_mgrids * (\sum_{i=0}^{atoms_->size()} atoms_[i]->nw)
56 void set_ddphi(
57 double *ddphi_xx,
58 double *ddphi_xy,
59 double *ddphi_xz,
60 double *ddphi_yy,
61 double *ddphi_yz,
62 double *ddphi_zz) const;
63
64 // phi_dm(ir,iwt_2) = \sum_{iwt_1} phi(ir,iwt_1) * dm(iwt_1,iwt_2)
65 // The phi_dm output is always double regardless of Tin: this keeps the
66 // downstream phi_dot_phi reading a uniform fp64 right-hand side. When
67 // Tin=float the GEMM still runs in fp32 (K is small ~10, so per-call
68 // accumulation in fp32 is fine); only the final write into phi_dm casts
69 // to double.
70 template <typename Tin>
71 void phi_mul_dm(
72 const Tin *const phi, // phi(ir,iwt)
73 const HContainer<Tin> &dm, // dm(iwt_1,iwt_2)
74 const bool is_symm,
75 double *const phi_dm) const; // phi_dm(ir,iwt)
76
77 // result(ir,iwt) = phi(ir,iwt) * vl(ir)
78 template <typename T>
79 void phi_mul_vldr3(
80 const T *const vl, // vl(ir)
81 const T dr3,
82 const T *const phi, // phi(ir,iwt)
83 T *const result) const; // result(ir,iwt)
84
85 // hr(iwt_i,iwt_j) = \sum_{ir} phi_i(ir,iwt_i) * phi_i(ir,iwt_j)
86 // this is a thread-safe function.
87 // The accumulator hr is always double: when Tin=float we want fp32 multiplies
88 // but fp64 accumulation across many biggrids to avoid catastrophic precision loss.
89 template <typename Tin>
90 void phi_mul_phi(
91 const Tin *const phi_i, // phi_i(ir,iwt)
92 const Tin *const phi_j, // phi_j(ir,iwt)
93 HContainer<double> &hr, // hr(iwt_i,iwt_j)
94 const TriPart part) const;
95
96 // rho(ir) = \sum_{iwt} \phi_i(ir,iwt) * \phi_j(ir,iwt)
97 // phi_j is always double-typed (it is the output of phi_mul_dm, which now
98 // accumulates into a double buffer regardless of Tin). The inner dot
99 // product is accumulated in double too: fp32 phi_i + fp64 phi_j → fp64.
100 template <typename Tin>
101 void phi_dot_phi(
102 const Tin *const phi_i, // phi_i(ir,iwt)
103 const double *const phi_j, // phi_j(ir,iwt)
104 double *const rho) const; // rho(ir)
105
106 void phi_dot_dphi(
107 const double *phi,
108 const double *dphi_x,
109 const double *dphi_y,
110 const double *dphi_z,
111 ModuleBase::matrix *fvl) const;
112
113 void phi_dot_dphi_r(
114 const double *phi,
115 const double *dphi_x,
116 const double *dphi_y,
117 const double *dphi_z,
118 ModuleBase::matrix *svl) const;
119
120 void cal_env_gamma(
121 const double *phi,
122 const double *wfc,
123 const vector<int> &trace_lo,
124 double *rho) const;
125
126 void cal_env_k(
127 const double *phi,
128 const std::complex<double> *wfc,
129 const vector<int> &trace_lo,
130 const int ik,
131 const int npol,
132 const std::vector<Vec3d> &kvec_d,
133 const int grid_size,
134 std::complex<double> *wfc_r) const;
135
136 private:
137 void init_atom_pair_idx_();
138
139 // get the index of the first and the last meshgrid that both atom a and atom b affect
140 // Note that atom_pair_range_ only stores the cases where a <= b, so this function is needed to retrieve the value
141 const std::pair<int, int> &get_atom_pair_start_end_idx_(int a, int b) const
142 {
143 int x = std::min(a, b);
144 int y = std::abs(a - b);
145 return atom_pair_range_[(2 * biggrid_->get_atoms_num() - x + 1) * x / 2 + y];
146 }
147
148 bool is_atom_on_mgrid(int atom_idx, int mgrid_idx) const { return is_atom_on_mgrid_[atom_idx * rows_ + mgrid_idx]; }
149
150 // the row number of the phi matrix
151 // rows_ = biggrid_->get_mgrids_num()
152 int rows_;
153
154 // the column number of the phi matrix
155 // cols_ = biggrid_->get_phi_len()
156 int cols_;
157
158 // the local index of the meshgrids
159 std::vector<int> mgrid_lidx_;
160
161 // the big grid that the phi matrix is associated with
162 std::shared_ptr<const BigGrid> biggrid_;
163
164 // the relative coordinates of the atoms and the meshgrids
165 // atom_rcoords_[i][j] is the relative coordinate of the jth meshgrid and the ith atom
166 std::vector<std::vector<Vec3d>> atom_rcoords_;
167
168 // record whether the atom affects the meshgrid
169 // is_atom_on_mgrid_[i * rows_ + j] = true if the ith atom affects jhe ith meshgrid, otherwise false
170 std::vector<bool> is_atom_on_mgrid_;
171
172 // the start index of the phi of each atom
173 std::vector<int> atoms_startidx_;
174
175 // the length of phi of each atom
176 // atoms_phi_len_[i] = biggrid_->get_atom(i)->get_nw()
177 // TODO: remove it
178 std::vector<int> atoms_phi_len_;
179
180 // This data structure is used to store the index of the first and last meshgrid affected by each atom pair
181 std::vector<std::pair<int, int>> atom_pair_range_;
182};
183
184} // namespace ModuleGint
185
186#endif // ABACUS_SOURCE_HAMILT_MODULE_GINT_PHI_OPERATOR_H
Definition matrix.h:15
int get_rows() const
Definition phi_operator.h:37
bool is_atom_on_mgrid(int atom_idx, int mgrid_idx) const
Definition phi_operator.h:148
int get_cols() const
Definition phi_operator.h:38
void phi_mul_phi(const Tin *const phi_i, const Tin *const phi_j, HContainer< double > &hr, const TriPart part) const
Definition phi_operator.cpp:380
void cal_env_k(const double *phi, const std::complex< double > *wfc, const vector< int > &trace_lo, const int ik, const int npol, const std::vector< Vec3d > &kvec_d, const int grid_size, std::complex< double > *wfc_r) const
Definition phi_operator.cpp:171
void phi_dot_dphi_r(const double *phi, const double *dphi_x, const double *dphi_y, const double *dphi_z, ModuleBase::matrix *svl) const
Definition phi_operator.cpp:109
void cal_env_gamma(const double *phi, const double *wfc, const vector< int > &trace_lo, double *rho) const
Definition phi_operator.cpp:144
void phi_dot_dphi(const double *phi, const double *dphi_x, const double *dphi_y, const double *dphi_z, ModuleBase::matrix *fvl) const
Definition phi_operator.cpp:79
void phi_mul_vldr3(const T *const vl, const T dr3, const T *const phi, T *const result) const
Definition phi_operator.cpp:361
std::vector< bool > is_atom_on_mgrid_
Definition phi_operator.h:170
void set_phi_dphi(double *phi, double *dphi_x, double *dphi_y, double *dphi_z) const
Definition phi_operator.cpp:38
std::shared_ptr< const BigGrid > biggrid_
Definition phi_operator.h:162
void phi_dot_phi(const Tin *const phi_i, const double *const phi_j, double *const rho) const
Definition phi_operator.cpp:474
void init_atom_pair_idx_()
Definition phi_operator.cpp:216
void set_phi(T *phi) const
Definition phi_operator.cpp:290
std::vector< std::pair< int, int > > atom_pair_range_
Definition phi_operator.h:181
const std::pair< int, int > & get_atom_pair_start_end_idx_(int a, int b) const
Definition phi_operator.h:141
std::vector< std::vector< Vec3d > > atom_rcoords_
Definition phi_operator.h:166
TriPart
Definition phi_operator.h:24
@ Upper
Definition phi_operator.h:25
@ Lower
Definition phi_operator.h:26
@ Full
Definition phi_operator.h:27
std::vector< int > atoms_phi_len_
Definition phi_operator.h:178
std::vector< int > mgrid_lidx_
Definition phi_operator.h:159
void set_ddphi(double *ddphi_xx, double *ddphi_xy, double *ddphi_xz, double *ddphi_yy, double *ddphi_yz, double *ddphi_zz) const
Definition phi_operator.cpp:58
void phi_mul_dm(const Tin *const phi, const HContainer< Tin > &dm, const bool is_symm, double *const phi_dm) const
Definition phi_operator.cpp:301
void set_bgrid(std::shared_ptr< const BigGrid > biggrid)
Definition phi_operator.cpp:11
std::vector< int > atoms_startidx_
Definition phi_operator.h:173
int rows_
Definition phi_operator.h:152
int cols_
Definition phi_operator.h:156
#define T
Definition exp.cpp:237
Definition batch_biggrid.cpp:4
hamilt::HContainer< T > HContainer
Definition gint_type.h:15