ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
xc_kernel.h
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_POTENTIALS_XC_KERNEL_H
2#define ABACUS_SOURCE_LCAO_MODULE_LR_POTENTIALS_XC_KERNEL_H
3
8#include <stdexcept>
9#include <memory>
10#define CREF(x) const std::vector<double>& x = x##_
11namespace LR
12{
17 namespace libxc_idx
18 {
20 constexpr int p2[3][3] = { {0,1,2},{1,3,4},{2,4,5} };
23 constexpr int p3[3][3][3] = {
24 { {0,1,2},{1,3,4},{2,4,5} },
25 { {1,3,4},{3,6,7},{4,7,8} },
26 { {2,4,5},{4,7,8},{5,8,9} } };
27
28 inline constexpr int r3(const int s0, const int s1, const int s2)
29 {
30 return s0 + s1 + s2;
31 }
32
33 inline constexpr int r2(const int s0, const int s1)
34 {
35 return s0 + s1;
36 }
37
38 inline constexpr int rs(const int s, const int a)
39 {
40 return 3 * s + a;
41 }
42
43 inline constexpr int r2s(const int s0, const int s1, const int a)
44 {
45 return 3 * (s0 + s1) + a;
46 }
47
48 inline constexpr int rs2(const int s, const int a, const int b)
49 {
50 return 6 * s + p2[a][b];
51 }
52
55 constexpr double theta[2][3] = { {2., 1., 0.}, {0., 1., 2.} };
57 constexpr int chan[2][3] = { {0, 1, -1}, {-1, 0, 1} };
58 }
59
66 inline double sigma_potential_derivative(const double* rho_sigma, const double* sigma_sigma,
67 const int channel, const double* density_response, const double* sigma_response,
68 const int first_spin, const int end_spin)
69 {
70 double response = 0.;
71 for (int spin = first_spin; spin < end_spin; ++spin)
72 {
73 response += rho_sigma[libxc_idx::rs(spin, channel)] * density_response[spin];
74 }
75 for (int a = first_spin; a < end_spin + 1; ++a)
76 {
77 response += sigma_sigma[libxc_idx::p2[channel][a]] * sigma_response[a];
78 }
79 return response;
80 }
81
84 {
85 using Tvec = std::vector<double>;
86 public:
88 enum GxcSpin { NoGxc = 0, Singlet = 1, Triplet = 2, BothSpins = 3 };
91 KernelXC(const ModulePW::PW_Basis& rho_basis,
92 const UnitCell& ucell,
93 const Charge& chg_gs,
94 const Parallel_Grid& pgrid,
95 const int& nspin,
96 const std::string& kernel_name,
97 const std::vector<std::string>& lr_init_xc_kernel,
98 const bool openshell = false,
99 const int gxc_spin = GxcSpin::NoGxc);
102 KernelXC(Purpose purpose, const ModulePW::PW_Basis& rho_basis,
103 const UnitCell& ucell, const Charge& chg_gs, const Parallel_Grid& pgrid,
104 const int& nspin, const std::string& kernel_name,
105 const std::vector<std::string>& lr_init_xc_kernel, bool openshell, int gxc_spin);
107
109 int nspin() const
110 {
111 return nspin_;
112 }
113 int functional_type() const
114 {
115 return functional_type_;
116 }
117 const std::string& name() const
118 {
119 return kernel_name_;
120 }
121
125
126 // const references
127 CREF(vrho);CREF(vsigma); CREF(v2rho2); CREF(v2rhosigma); CREF(v2sigma2);
128 CREF(v3rho3);
129 CREF(v3rho2sigma);
130 CREF(v3rhosigma2);
131 CREF(v3sigma3);
140 const ClosedShellGga& closed_shell_gga(const bool triplet) const
141 {
142 if (openshell_)
143 {
144 throw std::logic_error("Closed-shell GGA coefficients requested for an open-shell kernel");
145 }
146 return (nspin_ == 1 || !triplet) ? closed_shell_gga_s_ : closed_shell_gga_t_;
147 }
148
167 struct GxcCoef
168 {
170 bool built = false;
171 // Local part A.
172 std::vector<double> a_s2;
173 std::vector<double> a_st;
174 std::vector<double> a_t2;
175 std::vector<double> a_q;
176 // Divergence coefficients, all multiplying the ground-state density gradient.
177 std::vector<double> e_s2;
178 std::vector<double> e_st;
179 std::vector<double> e_t2;
180 std::vector<double> e_q;
181 };
182
185 const GxcCoef& gxc(const bool triplet) const
186 {
187 const GxcCoef& ret = (nspin_ == 1 || !triplet) ? gxc_s_ : gxc_t_;
188 if (!ret.built)
189 {
190 throw std::runtime_error(
191 "KernelXC: the " + std::string(triplet ? "triplet" : "singlet")
192 + " g^xc coefficients were not built; pass the matching `GxcSpin` flag to the constructor.");
193 }
194 return ret;
195 }
196
200 const Tvec& sigma_jacobian() const { return sigma_jacobian_; }
201
202 const bool& openshell = openshell_;
203 const std::vector<std::vector<ModuleBase::Vector3<double>>>& drho_gs = drho_gs_;
210 bool is_gga() const
211 {
212 return !this->drho_gs_.empty();
213 }
214
215 private:
216#ifdef __LIBXC
218 void f_xc_libxc(const int& nspin, const double& omega, const double& tpiba, const double* const* const rho_gs, const double* const rho_core = nullptr);
220 void get_rho_drho_sigma(const int& nspin,
221 const double& tpiba,
222 const double* const* const rho_gs,
223 const double* const rho_core,
224 const bool& is_gga,
225 std::vector<double>& rho,
226 std::vector<std::vector<ModuleBase::Vector3<double>>>& gradrho,
227 std::vector<double>& sigma);
228#endif
229 // See https://libxc.gitlab.io/manual/libxc-5.1.x/ for the naming convention of the following members.
230 // std::map<std::string, std::vector<double>> kernel_set_; // [kernel_type][nrxx][nspin]
231
232 // ================================== XC kernels ============================================
233 std::vector<double> vrho_;
234 std::vector<double> vsigma_;
235 std::vector<double> v2rho2_;
236 std::vector<double> v2rhosigma_;
237 std::vector<double> v2sigma2_;
238 // std::map<std::string, std::vector<ModuleBase::Vector3<double>>> grad_kernel_set_;// [kernel_type][nrxx][nspin], intermediate terms for GGA
239 // for nspin=1, gga
240 std::vector<std::vector<ModuleBase::Vector3<double>>> drho_gs_;
243
244 // ================================== XC kernels ============================================
245 // ================================== XC kernel Gradiants ====================================
250
251 // The two spin combinations of $v^{(2)}$'s coefficients (see `GxcCoef` above).
252 // `gxc_t_` stays empty for nspin=1, where there is no triplet.
257 void build_gxc_coef(GxcCoef& dst, const bool triplet, const int& nspin, const bool& is_gga);
258 const int nspin_;
259 const std::string kernel_name_;
265 // ================================== XC kernel Gradiants ====================================
267 const bool openshell_ = false;
268 };
269
272 std::shared_ptr<const KernelXC> build_xc_kernel(const std::string& kernel_name,
273 const ModulePW::PW_Basis& rho_basis, const UnitCell& ucell, const Charge& chg_gs,
274 const Parallel_Grid& pgrid, const int nspin, const bool openshell, const int gxc_spin,
275 const std::vector<std::string>& lr_init_xc_kernel);
278 std::shared_ptr<const KernelXC> build_gs_xc_kernel(const std::string& kernel_name,
279 const ModulePW::PW_Basis& rho_basis, const UnitCell& ucell, const Charge& chg_gs,
280 const Parallel_Grid& pgrid, int nspin, bool openshell,
281 const std::vector<std::string>& lr_init_xc_kernel);
282
283}
284
285#endif // ABACUS_SOURCE_LCAO_MODULE_LR_POTENTIALS_XC_KERNEL_H
Definition charge.h:24
std::vector< double > Tvec
Definition xc_kernel.h:85
ClosedShellGga closed_shell_gga_t_
Definition xc_kernel.h:242
Tvec v3rho2sigma_
Definition xc_kernel.h:247
GxcCoef gxc_s_
Definition xc_kernel.h:253
~KernelXC()
Definition xc_kernel.h:106
const std::string kernel_name_
Definition xc_kernel.h:259
const GxcCoef & gxc(const bool triplet) const
Definition xc_kernel.h:185
int functional_type_
Definition xc_kernel.h:260
const ClosedShellGga & closed_shell_gga(const bool triplet) const
Definition xc_kernel.h:140
bool gs_sigma_fallback_used_
Definition xc_kernel.h:264
bool is_gga() const
Definition xc_kernel.h:210
Tvec v3rho3_
Definition xc_kernel.h:246
const std::string & name() const
Definition xc_kernel.h:117
int nspin() const
Metadata belongs to this coefficient set, independent of later global XC changes.
Definition xc_kernel.h:109
std::vector< double > v2rho2_
Definition xc_kernel.h:235
const Purpose purpose_
Definition xc_kernel.h:262
Tvec v3rhosigma2_
Definition xc_kernel.h:248
void build_gxc_coef(GxcCoef &dst, const bool triplet, const int &nspin, const bool &is_gga)
Fill dst for one spin combination. All the spin algebralives here, driven by the weight vectors that ...
GxcCoef gxc_t_
Definition xc_kernel.h:254
CREF(v3sigma3)
KernelXC(const ModulePW::PW_Basis &rho_basis, const UnitCell &ucell, const Charge &chg_gs, const Parallel_Grid &pgrid, const int &nspin, const std::string &kernel_name, const std::vector< std::string > &lr_init_xc_kernel, const bool openshell=false, const int gxc_spin=GxcSpin::NoGxc)
Definition xc_kernel.cpp:65
bool gs_sigma_fallback_used() const
Definition xc_kernel.h:124
std::vector< double > v2rhosigma_
Definition xc_kernel.h:236
CREF(v3rhosigma2)
CREF(v3rho2sigma)
const std::vector< std::vector< ModuleBase::Vector3< double > > > & drho_gs
Definition xc_kernel.h:203
const Tvec & sigma_jacobian() const
Definition xc_kernel.h:200
const int nspin_
Definition xc_kernel.h:258
const int gxc_spin_
which GxcCoef sets to build, see GxcSpin
Definition xc_kernel.h:261
std::vector< double > vrho_
Definition xc_kernel.h:233
CREF(v3rho3)
const bool & openshell
Definition xc_kernel.h:202
std::vector< std::vector< ModuleBase::Vector3< double > > > drho_gs_
$\nabla\rho$ of the ground state, size: nspin*nrxx
Definition xc_kernel.h:240
GxcSpin
Which spin combinations of the $g^{xc}$ coefficient set (GxcCoef) to build.
Definition xc_kernel.h:88
@ BothSpins
Definition xc_kernel.h:88
@ Singlet
Definition xc_kernel.h:88
@ NoGxc
Definition xc_kernel.h:88
@ Triplet
Definition xc_kernel.h:88
ClosedShellGga closed_shell_gga_s_
Definition xc_kernel.h:241
std::vector< double > v2sigma2_
Definition xc_kernel.h:237
std::vector< double > vsigma_
Definition xc_kernel.h:234
int functional_type() const
Definition xc_kernel.h:113
const ModulePW::PW_Basis & rho_basis_
Definition xc_kernel.h:266
const bool openshell_
Definition xc_kernel.h:267
Tvec v3sigma3_
Definition xc_kernel.h:249
Tvec sigma_jacobian_
Definition xc_kernel.h:263
Purpose
GS response starts from raw sigma; non-finite HSE coefficients trigger a reported fallback.
Definition xc_kernel.h:90
@ GroundStateResponse
Definition xc_kernel.h:90
@ LinearResponse
Definition xc_kernel.h:90
3 elements vector
Definition vector3.h:21
A class which can convert a function of "r" to the corresponding linear superposition of plane waves ...
Definition pw_basis.h:60
Definition parallel_grid.h:8
Provide the basic information about unitcell.
Definition unitcell.h:16
Definition xc_kernel.h:18
constexpr double theta[2][3]
Definition xc_kernel.h:55
constexpr int chan[2][3]
which density-gradient channel goes with (tau, a); -1 where theta vanishes
Definition xc_kernel.h:57
constexpr int rs(const int s, const int a)
v2rhosigma (6): [rho u,d] x [sigma uu,ud,dd]
Definition xc_kernel.h:38
constexpr int p2[3][3]
unordered sigma PAIR -> v2sigma2 (6) and the per-rho block of v3rhosigma2
Definition xc_kernel.h:20
constexpr int r2(const int s0, const int s1)
v2rho2 (3): (uu,ud,dd)
Definition xc_kernel.h:33
constexpr int rs2(const int s, const int a, const int b)
v3rhosigma2 (12): [rho u,d] x [unordered sigma pair]
Definition xc_kernel.h:48
constexpr int r2s(const int s0, const int s1, const int a)
v3rho2sigma (9): [rho-pair uu,ud,dd] x [sigma uu,ud,dd]
Definition xc_kernel.h:43
constexpr int p3[3][3][3]
Definition xc_kernel.h:23
constexpr int r3(const int s0, const int s1, const int s2)
v3rho3 (4): (uuu,uud,udd,ddd) – indexed by the number of d's
Definition xc_kernel.h:28
Definition esolver_lr_lcao_tddft.h:28
double sigma_potential_derivative(const double *rho_sigma, const double *sigma_sigma, const int channel, const double *density_response, const double *sigma_response, const int first_spin, const int end_spin)
Definition xc_kernel.h:66
std::shared_ptr< const KernelXC > build_gs_xc_kernel(const std::string &kernel_name, const ModulePW::PW_Basis &rho_basis, const UnitCell &ucell, const Charge &chg_gs, const Parallel_Grid &pgrid, int nspin, bool openshell, const std::vector< std::string > &lr_init_xc_kernel)
Definition xc_kernel.cpp:79
std::shared_ptr< const KernelXC > build_xc_kernel(const std::string &kernel_name, const ModulePW::PW_Basis &rho_basis, const UnitCell &ucell, const Charge &chg_gs, const Parallel_Grid &pgrid, const int nspin, const bool openshell, const int gxc_spin, const std::vector< std::string > &lr_init_xc_kernel)
Definition xc_kernel.cpp:56
Definition xc_kernel.h:136
Tvec rho_sigma
Definition xc_kernel.h:137
Tvec sigma_sigma
Definition xc_kernel.h:138
The coefficients of $v^{(2)}(r)=\iint dr'dr''\,g^{xc}(r,r',r'')\rho^1(r')\rho^1(r'')$ for ONE spin co...
Definition xc_kernel.h:168
std::vector< double > a_st
Definition xc_kernel.h:173
bool built
Availability is independent of the number of points assigned to this rank.
Definition xc_kernel.h:170
std::vector< double > e_t2
Definition xc_kernel.h:179
std::vector< double > e_st
Definition xc_kernel.h:178
std::vector< double > e_q
Definition xc_kernel.h:180
std::vector< double > e_s2
Definition xc_kernel.h:177
std::vector< double > a_s2
Definition xc_kernel.h:172
std::vector< double > a_q
Definition xc_kernel.h:175
std::vector< double > a_t2
Definition xc_kernel.h:174