ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
pot_hxc_lrtd.h
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_POTENTIALS_POT_HXC_LRTD_H
2#define ABACUS_SOURCE_LCAO_MODULE_LR_POTENTIALS_POT_HXC_LRTD_H
3
4#include "xc_kernel.h"
5#include "pot_lr_base.h"
6#include <unordered_map>
7#include <memory>
8
9namespace LR
10{
11 class PotHxcLR : public PotLRBase
12 {
13 public:
29 enum SpinType { S1 = 0, S2_singlet = 1, S2_triplet = 2, S2_updown = 3, S2_gs = 4, S1_gs = 5 };
31 enum XCType { None = 0, LDA = 1, GGA = 2, HYB_GGA = 4 };
35 PotHxcLR(std::shared_ptr<const KernelXC> kernel,
36 const ModulePW::PW_Basis& rho_basis, const UnitCell& ucell, const int nrxx,
37 const int nspin, const SpinType& st);
39 virtual void cal_v_eff(double** rho, const UnitCell& ucell, ModuleBase::matrix& v_eff,
40 const std::vector<int>& ispin_op = { 0,0 }) const override;
41
43 {
45 }
46
47 private:
53 const std::shared_ptr<const KernelXC> xc_kernel_components_;
56
57 // enum class as key for unordered_map is not supported in C++11 sometimes
58 // https://github.com/llvm/llvm-project/issues/49601
59 // struct SpinHash { std::size_t operator()(const SpinType& s) const { return std::hash<int>()(static_cast<int>(s)); } };
60 using Tfunc = std::function<void(const double* const ,
62 const std::vector<int>& ispin_op) >;
63 // std::unordered_map<SpinType, Tfunc, SpinHash> kernel_to_potential_;
64 std::map<SpinType, Tfunc> kernel_to_potential_;
65
66 void set_integral_func(const SpinType& s, const XCType& xc);
67
68 // ---- scratch buffers ------------------------------------------------------------
69 // `cal_v_eff` used to allocate (and value-initialize) every temporary on every call:
70 // at a 200^3 grid that is ~450 MB of memset per call, all of it overwritten before it
71 // is read, plus the page faults of freshly mapped pages. They now come from a pool
72 // that grows on demand and is SHARED by every `PotHxcLR` (a calculation holds three of
73 // them -- singlet, triplet and the ground-state kernel -- and giving each its own copy
74 // cost ~0.9 GB on the bigger grids). Sharing is safe because `cal_v_eff` is only ever
75 // entered from a single thread: all the OpenMP lives inside the loops it calls.
76 struct Scratch
77 {
78 std::vector<ModuleBase::Vector3<double>> drho;
79 std::vector<ModuleBase::Vector3<double>> gdot;
80 std::vector<double> vxc;
81 std::vector<std::complex<double>> rhog;
82 std::vector<std::complex<double>> vg;
83 void alloc(const int nrxx, const int npw, const int nmaxgr, const bool gga);
84 };
85 static Scratch& scratch();
94 void add_v_hartree(const UnitCell& ucell, ModuleBase::matrix& v_eff, const double factor) const;
95 // ---- pre-contracted spin combinations ------------------------------------------
96 // At nspin=2 the singlet/triplet integrands read `v2rho2[3ir] +- v2rho2[3ir+1]` and
97 // `2*vsigma[3ir] +- vsigma[3ir+1]` at every grid point of every call. Both combinations
98 // depend only on the ground state, so they are formed once here. This also turns two
99 // strided reads into one contiguous one, which is where most of the gain is.
100 // Only the combination this potential's own `spin_type_` needs is built (one scalar
101 // array each, so 8 B/point, and nothing at all for nspin=1 or the open-shell branch),
102 // which is why they live here rather than in the shared `KernelXC`.
103 std::vector<double> v2rho2_comb_;
104 std::vector<double> vsigma_comb_;
105 void build_spin_combos(const bool gga);
106 };
107
108} // namespace LR
109
110#endif // ABACUS_SOURCE_LCAO_MODULE_LR_POTENTIALS_POT_HXC_LRTD_H
Calculate the exchange-correlation (XC) kernel ($f_{xc}=\delta^2E_xc/\delta\rho^2$) and store its com...
Definition xc_kernel.h:84
PotHxcLR(std::shared_ptr< const KernelXC > kernel, const ModulePW::PW_Basis &rho_basis, const UnitCell &ucell, const int nrxx, const int nspin, const SpinType &st)
Definition pot_hxc_lrtd.cpp:13
void set_integral_func(const SpinType &s, const XCType &xc)
Definition pot_hxc_lrtd.cpp:197
virtual void cal_v_eff(double **rho, const UnitCell &ucell, ModuleBase::matrix &v_eff, const std::vector< int > &ispin_op={ 0, 0 }) const override
Definition pot_hxc_lrtd.cpp:143
~PotHxcLR()
Definition pot_hxc_lrtd.h:38
std::vector< double > v2rho2_comb_
Definition pot_hxc_lrtd.h:103
SpinType
Definition pot_hxc_lrtd.h:29
@ S1
Definition pot_hxc_lrtd.h:29
@ S2_gs
Definition pot_hxc_lrtd.h:29
@ S1_gs
Definition pot_hxc_lrtd.h:29
@ S2_singlet
Definition pot_hxc_lrtd.h:29
@ S2_triplet
Definition pot_hxc_lrtd.h:29
@ S2_updown
Definition pot_hxc_lrtd.h:29
const SpinType spin_type_
Definition pot_hxc_lrtd.h:54
static Scratch & scratch()
Definition pot_hxc_lrtd.cpp:33
const std::shared_ptr< const KernelXC > xc_kernel_components_
Definition pot_hxc_lrtd.h:53
const KernelXC & xc_kernel_components() const
Definition pot_hxc_lrtd.h:42
void add_v_hartree(const UnitCell &ucell, ModuleBase::matrix &v_eff, const double factor) const
Definition pot_hxc_lrtd.cpp:117
XCType
XCType here is to determin the method of integration from kernel to potential, not the way calculatin...
Definition pot_hxc_lrtd.h:31
@ HYB_GGA
Definition pot_hxc_lrtd.h:31
@ None
Definition pot_hxc_lrtd.h:31
@ LDA
Definition pot_hxc_lrtd.h:31
@ GGA
Definition pot_hxc_lrtd.h:31
XCType xc_type_
Definition pot_hxc_lrtd.h:55
void build_spin_combos(const bool gga)
Definition pot_hxc_lrtd.cpp:70
std::map< SpinType, Tfunc > kernel_to_potential_
Definition pot_hxc_lrtd.h:64
std::function< void(const double *const, ModuleBase::matrix &, const std::vector< int > &ispin_op) > Tfunc
Definition pot_hxc_lrtd.h:60
std::vector< double > vsigma_comb_
GGA only.
Definition pot_hxc_lrtd.h:104
PotLRBase(const ModulePW::PW_Basis &rho_basis, const int &nspin, const int &nrxx, const double &tpiba)
Definition pot_lr_base.h:20
const int & nrxx
Definition pot_lr_base.h:27
const int & nspin
Definition pot_lr_base.h:28
Definition matrix.h:15
A class which can convert a function of "r" to the corresponding linear superposition of plane waves ...
Definition pw_basis.h:60
Provide the basic information about unitcell.
Definition unitcell.h:16
Definition esolver_lr_lcao_tddft.h:28
Definition pot_hxc_lrtd.h:77
std::vector< ModuleBase::Vector3< double > > gdot
integrand of the divergence
Definition pot_hxc_lrtd.h:79
std::vector< double > vxc
accumulated $v^{xc}$
Definition pot_hxc_lrtd.h:80
void alloc(const int nrxx, const int npw, const int nmaxgr, const bool gga)
Definition pot_hxc_lrtd.cpp:39
std::vector< std::complex< double > > rhog
$\rho^X(G)$, npw
Definition pot_hxc_lrtd.h:81
std::vector< std::complex< double > > vg
G-space scratch, nmaxgr.
Definition pot_hxc_lrtd.h:82
std::vector< ModuleBase::Vector3< double > > drho
$\nabla\rho^X$
Definition pot_hxc_lrtd.h:78