ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
diago_cg.h
Go to the documentation of this file.
1#ifndef MODULE_HSOLVER_DIAGO_CG_H_
2#define MODULE_HSOLVER_DIAGO_CG_H_
3
4#include <functional>
5#include <vector>
6
9
10#include <ATen/core/tensor.h>
12
13namespace hsolver {
14
15template <typename T, typename Device = base_device::DEVICE_CPU>
16class DiagoCG final
17{
18 // private: accessibility within class is private by default
19 // Note GetTypeReal<T>::type will
20 // return T if T is real type(float, double),
21 // otherwise return the real type of T(complex<float>, std::complex<double>)
22 using Real = typename GetTypeReal<T>::type;
24 public:
25 using Func = std::function<void(const ct::Tensor&, ct::Tensor&)>;
26 using SubspaceFunc = std::function<void(const ct::Tensor&, ct::Tensor&, const bool)>;
27 // Constructor need:
28 // 1. temporary mock of Hamiltonian "Hamilt_PW"
29 // 2. precondition pointer should point to place of precondition array.
30 DiagoCG(const std::string& basis_type, const std::string& calculation);
31 DiagoCG(
32 const std::string& basis_type,
33 const std::string& calculation,
34 const bool& need_subspace,
35 const SubspaceFunc& subspace_func,
36 const Real& pw_diag_thr,
37 const int& pw_diag_nmax,
38 const int& nproc_in_pool);
39
40 ~DiagoCG();
41
42 // virtual void init(){};
43 // refactor hpsi_info
44 // this is the diag() function for CG method
45 // returns avg_iter
46 double diag(const Func& hpsi_func,
47 const Func& spsi_func,
49 ct::Tensor& eigen,
50 const std::vector<double>& ethr_band,
51 const ct::Tensor& prec = {});
52
53 private:
54 Device * ctx_ = {};
57 int notconv_ = 0;
60 int n_band_ = 0;
62 int n_basis_ = 0;
64 double avg_iter_ = 0;
66 std::vector<int> iter_band;
74 std::string basis_type_ = {};
76 std::string calculation_ = {};
77
78 bool need_subspace_ = false;
80 Func hpsi_func_ = nullptr;
82 Func spsi_func_ = nullptr;
85
86 void calc_grad(
87 const ct::Tensor& prec,
88 ct::Tensor& grad,
89 ct::Tensor& hphi,
90 ct::Tensor& sphi,
91 ct::Tensor& pphi);
92
93 void orth_grad(
94 const ct::Tensor& psi,
95 const int& m,
96 ct::Tensor& grad,
97 ct::Tensor& scg,
98 ct::Tensor& lagrange);
99
100 void calc_gamma_cg(
101 const int& iter,
102 const Real& cg_norm,
103 const Real& theta,
104 const ct::Tensor& prec,
105 const ct::Tensor& scg,
106 const ct::Tensor& grad,
107 const ct::Tensor& phi_m,
108 Real& gg_last,
109 ct::Tensor& g0,
110 ct::Tensor& cg);
111
112 bool update_psi(
113 const ct::Tensor& pphi,
114 const ct::Tensor& cg,
115 const ct::Tensor& scg,
116 const double& ethreshold,
117 Real &cg_norm,
118 Real &theta,
119 Real &eigen,
120 ct::Tensor& phi_m,
121 ct::Tensor& sphi,
122 ct::Tensor& hphi);
123
124 void schmit_orth(const int& m, const ct::Tensor& psi, const ct::Tensor& sphi, ct::Tensor& phi_m);
125
126 // used in diag() for template replace Hamilt with Hamilt_PW
127 void diag_once(const ct::Tensor& prec,
129 ct::Tensor& eigen,
130 const std::vector<double>& ethr_band);
131
132 bool test_exit_cond(const int& ntry, const int& notconv) const;
133
135 const T * one_ = nullptr, * zero_ = nullptr, * neg_one_ = nullptr;
136};
137
138} // namespace hsolver
139
140#endif // MODULE_HSOLVER_DIAGO_CG_H_
A multi-dimensional array of elements of a single data type.
Definition tensor.h:32
Definition diago_cg.h:17
Func hpsi_func_
A function object that performs the hPsi calculation.
Definition diago_cg.h:80
bool need_subspace_
Definition diago_cg.h:78
Real pw_diag_thr_
threshold for cg diagonalization
Definition diago_cg.h:68
typename ct::PsiToContainer< Device >::type ct_Device
Definition diago_cg.h:23
void calc_grad(const ct::Tensor &prec, ct::Tensor &grad, ct::Tensor &hphi, ct::Tensor &sphi, ct::Tensor &pphi)
Definition diago_cg.cpp:216
void diag_once(const ct::Tensor &prec, ct::Tensor &psi, ct::Tensor &eigen, const std::vector< double > &ethr_band)
Definition diago_cg.cpp:57
void schmit_orth(const int &m, const ct::Tensor &psi, const ct::Tensor &sphi, ct::Tensor &phi_m)
Definition diago_cg.cpp:477
Func spsi_func_
A function object that performs the sPsi calculation.
Definition diago_cg.h:82
std::function< void(const ct::Tensor &, ct::Tensor &)> Func
Definition diago_cg.h:25
int pw_diag_nmax_
maximum iteration steps for cg diagonalization
Definition diago_cg.h:70
SubspaceFunc subspace_func_
A function object that performs the subspace calculation.
Definition diago_cg.h:84
typename GetTypeReal< T >::type Real
Definition diago_cg.h:22
const T * one_
Definition diago_cg.h:135
std::function< void(const ct::Tensor &, ct::Tensor &, const bool)> SubspaceFunc
Definition diago_cg.h:26
bool test_exit_cond(const int &ntry, const int &notconv) const
Definition diago_cg.cpp:565
const T * neg_one_
Definition diago_cg.h:135
int n_basis_
col size for input psi matrix
Definition diago_cg.h:62
~DiagoCG()
Definition diago_cg.cpp:49
double avg_iter_
average iteration steps for cg diagonalization
Definition diago_cg.h:64
int n_band_
Definition diago_cg.h:60
void calc_gamma_cg(const int &iter, const Real &cg_norm, const Real &theta, const ct::Tensor &prec, const ct::Tensor &scg, const ct::Tensor &grad, const ct::Tensor &phi_m, Real &gg_last, ct::Tensor &g0, ct::Tensor &cg)
Definition diago_cg.cpp:311
std::string calculation_
calculation type of ABACUS
Definition diago_cg.h:76
int notconv_
Definition diago_cg.h:57
std::string basis_type_
basis_type of psi
Definition diago_cg.h:74
Device * ctx_
Definition diago_cg.h:54
double diag(const Func &hpsi_func, const Func &spsi_func, ct::Tensor &psi, ct::Tensor &eigen, const std::vector< double > &ethr_band, const ct::Tensor &prec={})
Definition diago_cg.cpp:579
std::vector< int > iter_band
std::vector for iter count of each band
Definition diago_cg.h:66
void orth_grad(const ct::Tensor &psi, const int &m, ct::Tensor &grad, ct::Tensor &scg, ct::Tensor &lagrange)
Definition diago_cg.cpp:261
const T * zero_
Definition diago_cg.h:135
bool update_psi(const ct::Tensor &pphi, const ct::Tensor &cg, const ct::Tensor &scg, const double &ethreshold, Real &cg_norm, Real &theta, Real &eigen, ct::Tensor &phi_m, ct::Tensor &sphi, ct::Tensor &hphi)
Definition diago_cg.cpp:393
int nproc_in_pool_
number of processors in a node
Definition diago_cg.h:72
#define T
Definition exp.cpp:237
Definition diag_comm_info.h:9
Definition exx_lip.h:23
T type
Definition macros.h:8
Definition math_kernel_op.h:168
Definition tensor_types.h:113
This file contains the definition of the DataType enum class.
int nproc_in_pool
Definition pw_test.cpp:12