ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
libxc_abacus.h
Go to the documentation of this file.
1#ifndef LIBXC_ABACUS_H
2#define LIBXC_ABACUS_H
3
4#ifdef __LIBXC
5
8#include "xc_ncgga_radial.h"
9
10#include <xc.h>
11#include <xc_funcs.h>
12
13#include <tuple>
14#include <vector>
15
16#include <map>
17#include <utility>
18
19class Charge;
20
21namespace XC_Functional_Libxc
22{
23struct LibxcWeightedDerivatives
24{
25 double energy_sum;
26 std::vector<double> drho;
27 std::vector<double> dsigma;
28};
29
30// Complete forward data for the gga_grad=2 noncollinear Libxc graph:
31// rho_s = N_s(x),
32// g_s = G_h N_s(x).
33// Reverse the FFT gradient before applying the local map Jacobian;
34// this uses the same clipping branches as the forward energy.
35struct NclSfDiscreteData
36{
37 std::vector<ModuleXC::NcggaSpinMapPoint> spin_map;
38 std::vector<double> rho;
39 std::vector<std::vector<ModuleBase::Vector3<double>>> spin_gradient;
40};
41
42//-------------------
43// libxc_setup.cpp
44//-------------------
45
46// sets functional type, which allows combination of LIBXC keyword connected by "+"
47// for example: "XC_LDA_X+XC_LDA_C_PZ"
48extern std::pair<int, std::vector<int>> set_xc_type_libxc(const std::string &xc_func_in);
49
66extern std::vector<xc_func_type> init_func(
67 const std::vector<int> &func_id,
68 const int xc_polarized,
69 const double hybrid_alpha,
70 const double hse_omega);
71
72extern std::vector<xc_func_type> init_func(
73 const std::vector<int> &func_id,
74 const int xc_polarized,
75 const double hybrid_alpha,
76 const double hse_omega,
77 const bool use_gpu);
78
79// Host arrays in/out; the bounded CUDA workspace is released on return.
80void xc_cuda_exc_vxc(
81 xc_func_type &func,
82 const int nrxx,
83 const double *rho,
84 const double *sigma,
85 double *exc,
86 double *vrho,
87 double *vsigma);
88
101extern std::vector<std::vector<double>> ext_params_for(
102 const std::vector<int> &func_id, const double hybrid_alpha, const double hse_omega);
103
104extern void finish_func(std::vector<xc_func_type> &funcs);
105
106//-------------------
107// libxc_pot.cpp
108//-------------------
109
110extern std::tuple<double, double, ModuleBase::matrix> v_xc_libxc(
111 const std::vector<int> &func_id,
112 const int &nrxx, // number of real-space grid
113 const double &omega, // volume of cell
114 const double tpiba,
115 const Charge *const chr, // charge density
116 const int nspin,
117 const bool domag,
118 const bool domag_z,
119 const int gga_grad,
120 const std::map<int, double> *scaling_factor,
121 const double hybrid_alpha,
122 const double hse_omega);
123
124extern std::tuple<double, double, ModuleBase::matrix> v_xc_libxc(
125 const std::vector<int> &func_id,
126 const int &nrxx, // number of real-space grid
127 const double &omega, // volume of cell
128 const double tpiba,
129 const Charge *const chr, // charge density
130 const int nspin,
131 const bool domag,
132 const bool domag_z,
133 const int gga_grad,
134 const std::map<int, double> *scaling_factor,
135 const double hybrid_alpha,
136 const double hse_omega,
137 const bool use_gpu);
138
139// Reciprocal-metric derivative of the exact gga_grad=2 Libxc energy
140// graph. The returned lower-triangular tensor is the unnormalized local
141// grid sum; Stress_Func performs the pool reduction and divides by nxyz.
142extern void gradcorr_ncgga_sf_libxc(
143 const std::vector<int> &func_id,
144 const std::size_t nrxx,
145 const double tpiba,
146 const Charge *const chr,
147 const std::map<int, double> *scaling_factor,
148 const double hybrid_alpha,
149 const double hse_omega,
150 std::vector<double> &stress_gga);
151
152// for mGGA functional
153extern std::tuple<double, double, ModuleBase::matrix, ModuleBase::matrix> v_xc_meta(
154 const std::vector<int> &func_id,
155 const int &nrxx, // number of real-space grid
156 const double &omega, // volume of cell
157 const double tpiba,
158 const Charge *const chr,
159 const int nspin,
160 const double hybrid_alpha,
161 const double hse_omega);
162
163//-------------------
164// libxc_tools.cpp
165//-------------------
166
167// converting rho (abacus=>libxc)
168extern std::vector<double> convert_rho(const int nspin, const std::size_t nrxx, const Charge *const chr);
169
170// converting rho (abacus=>libxc)
171extern std::tuple<std::vector<double>, std::vector<double>> convert_rho_amag_nspin4(
172 const int nspin, const std::size_t nrxx, const Charge *const chr);
173
174// Build the exact gga_grad=2 local spin map and, when requested, its
175// mapped-density FFT-gradient graph. LDA-only callers set need_gradient=false.
176extern NclSfDiscreteData make_ncl_sf_discrete_data(
177 const std::size_t nrxx,
178 const double tpiba,
179 const Charge *const chr,
180 const bool need_gradient);
181
182// Reverse one aggregate of all scaled Libxc components. The returned
183// potential is already in (n,mx,my,mz) representation. An empty dsigma
184// selects the LDA-only local reverse and performs no FFT divergence.
185extern ModuleBase::matrix reverse_ncl_sf_discrete(
186 const std::size_t nrxx,
187 const NclSfDiscreteData &data,
188 const std::vector<double> &drho,
189 const std::vector<double> &dsigma,
190 const double tpiba,
191 const Charge *const chr);
192
193// calculating grho
194extern std::vector<std::vector<ModuleBase::Vector3<double>>> cal_gdr(
195 const int nspin,
196 const std::size_t nrxx,
197 const std::vector<double> &rho,
198 const double tpiba,
199 const Charge *const chr);
200
201extern void cal_gdr_and_lapl(
202 const int nspin,
203 const std::size_t nrxx,
204 const std::vector<double> &rho,
205 const double tpiba,
206 const Charge *const chr,
207 std::vector<std::vector<ModuleBase::Vector3<double>>> &gdr,
208 std::vector<double> &lapl,
209 const bool need_laplacian = true);
210
211// converting grho (abacus=>libxc)
212extern std::vector<double> convert_sigma(const std::vector<std::vector<ModuleBase::Vector3<double>>> &gdr);
213
214// sgn for threshold mask
215extern std::vector<double> cal_sgn(
216 const double rho_threshold,
217 const double grho_threshold,
218 const xc_func_type &func,
219 const int nspin,
220 const std::size_t nrxx,
221 const std::vector<double> &rho,
222 const std::vector<double> &sigma);
223
224// threshold masks for the xc potential (Quantum ESPRESSO convention):
225// the first mask applies to exc and vrho, the second one only to vsigma
226extern std::pair<std::vector<double>, std::vector<double>> cal_sgn_vxc(
227 const double rho_threshold_vrho,
228 const double rho_threshold_vsigma,
229 const double grho_threshold_vsigma,
230 const xc_func_type &func,
231 const int nspin,
232 const std::size_t nrxx,
233 const std::vector<double> &rho,
234 const std::vector<double> &sigma);
235
236// converting etxc from exc (libxc=>abacus)
237extern double convert_etxc(
238 const int nspin,
239 const std::size_t nrxx,
240 const std::vector<double> &sgn,
241 const std::vector<double> &rho,
242 std::vector<double> exc);
243
244// Reverse the density and sigma sanitizers for the weighted energy
245// accumulated by ABACUS. The result is in Hartree units and excludes the
246// real-space grid weight and ModuleBase::e2.
247extern LibxcWeightedDerivatives make_libxc_weighted_derivatives(
248 const xc_func_type &func,
249 const int nspin,
250 const std::size_t nrxx,
251 const std::vector<double> &sgn,
252 const std::vector<double> &rho,
253 const std::vector<double> &sigma,
254 const std::vector<double> &exc,
255 const std::vector<double> &vrho,
256 const std::vector<double> &vsigma);
257
258// Convert collinear LibXC derivatives to the potential.
259extern std::pair<double, ModuleBase::matrix> convert_vtxc_v(
260 const xc_func_type &func,
261 const int nspin,
262 const std::size_t nrxx,
263 const std::vector<double> &sgn_vrho,
264 const std::vector<double> &sgn_vsigma,
265 const std::vector<double> &rho,
266 const std::vector<std::vector<ModuleBase::Vector3<double>>> &gdr,
267 const std::vector<double> &vrho,
268 const std::vector<double> &vsigma,
269 const double tpiba,
270 const Charge *const chr);
271
272// dh for gga v
273extern std::vector<std::vector<double>> cal_dh(
274 const int nspin,
275 const std::size_t nrxx,
276 const std::vector<double> &sgn,
277 const std::vector<std::vector<ModuleBase::Vector3<double>>> &gdr,
278 const std::vector<double> &vsigma,
279 const double tpiba,
280 const Charge *const chr);
281
282// convert v for NSPIN=4
283// has_mag: whether the calculation has (noncollinear) magnetization,
284// i.e. domag || domag_z
285extern ModuleBase::matrix convert_v_nspin4(
286 const std::size_t nrxx,
287 const Charge *const chr,
288 const std::vector<double> &amag,
289 const ModuleBase::matrix &v,
290 const bool has_mag);
291
292//-------------------
293// libxc_lda_wrap.cpp
294//-------------------
295
296extern void xc_spin_libxc(
297 const std::vector<int> &func_id,
298 const double &rhoup,
299 const double &rhodw,
300 double &exc,
301 double &vxcup,
302 double &vxcdw,
303 const double hybrid_alpha,
304 const double hse_omega);
305
306//-------------------
307// libxc_gga_wrap.cpp
308//-------------------
309
310// the entire GGA functional, for nspin=1 case
311extern void gcxc_libxc(
312 const std::vector<int> &func_id,
313 const double &rho,
314 const double &grho,
315 double &sxc,
316 double &v1xc,
317 double &v2xc,
318 const double hybrid_alpha,
319 const double hse_omega);
320
321// Overload accepting an already-initialized functional vector. The caller
322// is responsible for init_func/finish_func; this overload does not touch
323// the lifetime of funcs. Useful for per-thread reuse inside OpenMP loops.
324extern void gcxc_libxc(
325 const std::vector<xc_func_type> &funcs,
326 const double &rho,
327 const double &grho,
328 double &sxc,
329 double &v1xc,
330 double &v2xc);
331
332// the entire GGA functional, for nspin=2 case
333extern void gcxc_spin_libxc(
334 const std::vector<int> &func_id,
335 const double rhoup,
336 const double rhodw,
337 const ModuleBase::Vector3<double> gdr1,
338 const ModuleBase::Vector3<double> gdr2,
339 double &sxc,
340 double &v1xcup,
341 double &v1xcdw,
342 double &v2xcup,
343 double &v2xcdw,
344 double &v2xcud,
345 const double hybrid_alpha,
346 const double hse_omega);
347
348// Overload accepting an already-initialized functional vector.
349extern void gcxc_spin_libxc(
350 const std::vector<xc_func_type> &funcs,
351 const double rhoup,
352 const double rhodw,
353 const ModuleBase::Vector3<double> gdr1,
354 const ModuleBase::Vector3<double> gdr2,
355 double &sxc,
356 double &v1xcup,
357 double &v1xcdw,
358 double &v2xcup,
359 double &v2xcdw,
360 double &v2xcud);
361
362//-------------------
363// libxc_mgga_wrap.cpp
364//-------------------
365
366// wrapper for the mGGA functionals
367extern void tau_xc(
368 const std::vector<int> &func_id,
369 const double &rho,
370 const double &grho,
371 const double &lapl_rho,
372 const double &atau,
373 double &sxc,
374 double &v1xc,
375 double &v2xc,
376 double &v3xc,
377 double &vlaplxc,
378 const double &hybrid_alpha,
379 const double &hse_omega);
380
381// Overload accepting an already-initialized functional vector.
382// hybrid_alpha scales the semilocal SCAN exchange for SCAN0.
383extern void tau_xc(
384 const std::vector<xc_func_type> &funcs,
385 const double &rho,
386 const double &grho,
387 const double &lapl_rho,
388 const double &atau,
389 double &sxc,
390 double &v1xc,
391 double &v2xc,
392 double &v3xc,
393 double &vlaplxc,
394 const double hybrid_alpha);
395
396extern void tau_xc_spin(
397 const std::vector<int> &func_id,
398 double rhoup,
399 double rhodw,
400 ModuleBase::Vector3<double> gdr1,
401 ModuleBase::Vector3<double> gdr2,
402 double laplup,
403 double lapldw,
404 double tauup,
405 double taudw,
406 double &sxc,
407 double &v1xcup,
408 double &v1xcdw,
409 double &v2xcup,
410 double &v2xcdw,
411 double &v2xcud,
412 double &v3xcup,
413 double &v3xcdw,
414 double &vlaplxcup,
415 double &vlaplxcdw,
416 const double &hybrid_alpha,
417 const double &hse_omega);
418
419// Overload accepting an already-initialized functional vector.
420// hybrid_alpha scales the semilocal SCAN exchange for SCAN0.
421extern void tau_xc_spin(
422 const std::vector<xc_func_type> &funcs,
423 double rhoup,
424 double rhodw,
425 ModuleBase::Vector3<double> gdr1,
426 ModuleBase::Vector3<double> gdr2,
427 double laplup,
428 double lapldw,
429 double tauup,
430 double taudw,
431 double &sxc,
432 double &v1xcup,
433 double &v1xcdw,
434 double &v2xcup,
435 double &v2xcdw,
436 double &v2xcud,
437 double &v3xcup,
438 double &v3xcdw,
439 double &vlaplxcup,
440 double &vlaplxcdw,
441 const double hybrid_alpha);
442
443} // namespace XC_Functional_Libxc
444
445#endif // __LIBXC
446
447#endif // LIBXC_ABACUS_H
void lapl_rho(const double &tpiba2, const std::complex< double > *rhog, double *lapn, const ModulePW::PW_Basis *rho_basis)
Definition cal_vcav.cpp:8
Definition charge.h:24
Definition libxc_host.h:11
std::vector< double > cal_sgn(const double, const double, const xc_func_type &, const int nspin, const std::size_t nrxx, const std::vector< double > &, const std::vector< double > &)
Definition test_pot_grad_xc.cpp:120
double func(const Vec3 &r, const std::vector< Vec3 > &R, const std::vector< double > &a, const std::vector< double > &n)
Definition test_partition.cpp:50