ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
hamilt_casida.h
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_LCAO_MODULE_LR_HAMILT_CASIDA_H
2#define ABACUS_SOURCE_LCAO_MODULE_LR_HAMILT_CASIDA_H
3
4#include <typeinfo>
11#ifdef __EXX
16#endif
17namespace LR
18{
19 template<typename T>
21 {
22 public:
23 HamiltLR(const std::string& xc_kernel,
24 const int& nspin,
25 const int& naos,
26 const std::vector<int>& nocc,
27 const std::vector<int>& nvirt,
28 const UnitCell& ucell_in,
29 const std::vector<double>& orb_cutoff,
30 const Grid_Driver& gd_in,
31 const psi::Psi<T>& psi_ks_in,
32 const ModuleBase::matrix& eig_ks,
33#ifdef __EXX
34 std::weak_ptr<Exx_LRI<T>> exx_lri_in,
35 const double& exx_alpha,
36#endif
37 std::weak_ptr<PotLRBase> pot_in,
38 const K_Vectors& kv_in,
39 const std::vector<Parallel_Blkcyclic>& pX_in,
40 const Parallel_Blkcyclic& pc_in,
41 const Parallel_Orbitals& pmat_in,
42 const std::string& spin_type,
43 const std::string& in_dir,
44 const std::string& out_dir,
45 const std::string& ri_hartree_benchmark = "none",
46 const std::vector<int>& aims_nbasis = {})
47 : HamiltLR(nspin, nocc, nvirt, pX_in, kv_in, pmat_in)
48 {
49 ModuleBase::TITLE("HamiltLR", "HamiltLR");
50 if (ri_hartree_benchmark != "aims" && ri_hartree_benchmark !="aims-librpa") { assert(aims_nbasis.empty()); }
51 if (ri_hartree_benchmark == "none") { LR_Util::initialize_DMR(*this->DM_trans, pmat_in, ucell_in, gd_in, orb_cutoff); }
52 // this->DM_trans->init_dmr(&gd_in, &ucell_in); // too large due to not restricted by orb_cutoff
53
54 // 1.add the diag operator (the first one)
55 this->ops = new OperatorLRDiag<T>(eig_ks.c, pX[0], nk, nocc[0], nvirt[0], false);
56 // 2.add Hxc operator
57#ifdef __EXX
58 using TAC = std::pair<int, std::array<int, 3>>;
59 using TLRI = std::map<int, std::map<TAC, RI::Tensor<T>>>;
60 TLRI Cs_read;
61 TLRI Vs_read;
62#ifdef __DEBUG
63 // TLRI Vs_compare = LRI_CV_Tools::read_Vs_abf<T>(in_dir + "Vs");
64 // LRI_CV_Tools::write_Vs_abf(Vs_read, "Vs_read_from_coulomb");
65 // LRI_CV_Tools::write_Cs_ao(Cs_read, "Cs_ao_read"); // ensure Cs_ao is read correctly
66 // assert(RI_Benchmark::compare_Vs(Vs_read, Vs_compare));
67#endif
68 if (ri_hartree_benchmark != "none")
69 {
70#ifdef __EXX
71 if (spin_type == "singlet")
72 {
73 int use_fine_kgrid = 0;
74 if (ri_hartree_benchmark == "aims" || ri_hartree_benchmark == "aims-librpa")
75 {
76 LR_IO::RI_kRlist kRlist (ucell_in,
77 const_cast<K_Vectors*>(&kv_in),
78 nspin, in_dir, out_dir, use_fine_kgrid);
79 // though C and V are real, here still use <T> to multiply with psi
80 Cs_read = LRI_CV_Tools::read_Cs_ao_all<T>(in_dir);
81 Vs_read = LR_IO::read_coulomb_mat_general_k<T,T>(in_dir, Cs_read, kRlist);
82 }
83 else if (ri_hartree_benchmark == "abacus")
84 {
85 Cs_read = LRI_CV_Tools::read_Cs_ao<T>(in_dir + "Cs");
86 Vs_read = LRI_CV_Tools::read_Vs_abf<T>(in_dir + "Vs");
87 }
88 else if (ri_hartree_benchmark == "abacus-librpa")
89 {
90 LR_IO::RI_kRlist kRlist (ucell_in,
91 const_cast<K_Vectors*>(&kv_in),
92 nspin, in_dir, out_dir, use_fine_kgrid);
93 Cs_read = LRI_CV_Tools::read_Cs_ao_all<T>(in_dir);
94 Vs_read = LR_IO::read_coulomb_mat_k<T,T>(in_dir, Cs_read, kRlist);
95 }
96 if (!std::set<std::string>({ "rpa", "hf"}).count(xc_kernel)) {
97 throw std::runtime_error("ri_hartree_benchmark is only supported for xc_kernel = rpa, hf");
98 }
99 RI_Benchmark::OperatorRIHartree<T>* ri_hartree_op
100 = new RI_Benchmark::OperatorRIHartree<T>(ucell_in, naos, nocc[0], nvirt[0], psi_ks_in,
101 Cs_read, Vs_read);
102 this->ops->add(ri_hartree_op);
103 }
104 else if (spin_type == "triplet") { std::cout << "Hatree term is not needed for S2:triplet." << std::endl; }
105#else
106 ModuleBase::WARNING_QUIT("ESolver_LR", "RI benchmark is only supported when compile with LibRI.");
107#endif
108 }
109 else
110#endif
111 {
112 hamilt::Operator<T>* lr_hxc = new OperatorLRHxc<T>(nspin, naos, nocc, nvirt, psi_ks_in,
113 *this->DM_trans, pot_in, ucell_in, orb_cutoff, gd_in, kv_in, pX_in, pc_in, pmat_in, {0}, T(1.0), OperatorLRHxc<T>::MO_TO_AO_TYPE::CC_vo, hamilt::calculation_type::lr_dmtrans_hxc);
114 this->ops->add(lr_hxc);
115 }
116#ifdef __EXX
117 if (exx_kernel_list().count(xc_kernel) )
118 { //add Exx operator
119 if (ri_hartree_benchmark != "none" && spin_type == "singlet")
120 {
121 exx_lri_in.lock()->reset_Cs(Cs_read);
122 exx_lri_in.lock()->reset_Vs(Vs_read);
123 }
124 // std::cout << "exx_alpha=" << exx_alpha << std::endl; // the default value of exx_alpha is 0.25 when dft_functional is pbe or hse
125 // this ground-state Casida operator is always MO_TO_AO_TYPE::CC_vo, which never
126 // reads the force-only coxt_full/cvx_full buffers, so cal_force is always false.
127 hamilt::Operator<T>* lr_exx = new OperatorLREXX<T>(nspin, naos, nocc[0], nvirt[0], ucell_in, psi_ks_in,
128 *this->DM_trans, exx_lri_in, kv_in, pX_in[0], pc_in, pmat_in,
129 /*cal_force=*/false,
130 (xc_kernel == "hf" ? 1.0 : exx_alpha), //alpha
131 OperatorLREXX<T>::MO_TO_AO_TYPE::CC_vo,
133 this->ops->add(lr_exx);
134 }
135#endif
136
137 this->cal_dm_trans = [&, this](const int& is, const T* const X)->void
138 {
139 const psi::Psi<T> psi_ks_is = LR_Util::get_psi_spin(psi_ks_in, is, nk);
140#ifdef __MPI
141 std::vector<ct::Tensor> dm_trans_2d = cal_dm_trans_pblas(X, pX[is], psi_ks_is, pc_in, naos, nocc[is], nvirt[is], pmat_in.para_2d(), (T)1.0 / (T)nk);
142 if (this->tdm_sym) for (container::Tensor& t : dm_trans_2d) LR_Util::matsym(t.data<T>(), naos, pmat_in.para_2d());
143#else
144 std::vector<ct::Tensor> dm_trans_2d = cal_dm_trans_blas(X, psi_ks_is, nocc[is], nvirt[is], (T)1.0 / (T)nk);
145 if (this->tdm_sym) for (auto& t : dm_trans_2d) LR_Util::matsym(t.data<T>(), naos);
146#endif
147 // LR_Util::print_tensor<T>(dm_trans_2d[0], "dm_trans_2d[0]", &pmat_in);
148 // tensor to vector, then set DMK
149 for (int ik = 0;ik < nk;++ik) { this->DM_trans->set_dmk_ptr(ik, dm_trans_2d[ik].data<T>()); }
150 };
151 }
152 ~HamiltLR() { delete this->ops; }
153
154 std::vector<T> matrix()const;
155
156 virtual void hPsi(const T* const psi_in, T* const hpsi, const int ld_psi, const int nband) const
157 {
158 assert(ld_psi == nk * pX[0].size());
159 for (int ib = 0;ib < nband;++ib)
160 {
161 const int offset = ib * ld_psi;
162 this->cal_dm_trans(0, psi_in + offset); // calculate transition density matrix here
163 hamilt::Operator<T>* node(this->ops);
164 while (node != nullptr)
165 {
166 node->act(/*nband=*/1, ld_psi, /*npol=*/1, psi_in + offset, hpsi + offset);
167 node = (hamilt::Operator<T>*)(node->next_op);
168 }
169 }
170 }
171
172 // void global2local(T* lvec, const T* gvec, const int& nband) const
173 // {
174 // const int npairs = nocc[0] * nvirt[0];
175 // for (int ib = 0;ib < nband;++ib)
176 // {
177 // const int loffset_b = ib * nk * pX[0].size();
178 // const int goffset_b = ib * nk * npairs;
179 // for (int ik = 0;ik < nk;++ik)
180 // {
181 // const int loffset = loffset_b + ik * pX[0].size();
182 // const int goffset = goffset_b + ik * npairs;
183 // for (int lo = 0;lo < pX[0].get_col_size();++lo)
184 // {
185 // const int go = pX[0].icglb(lo);
186 // for (int lv = 0;lv < pX[0].get_row_size();++lv)
187 // {
188 // const int gv = pX[0].irglb(lv);
189 // lvec[loffset + lo * pX[0].get_row_size() + lv] = gvec[goffset + go * nvirt[0] + gv];
190 // }
191 // }
192 // }
193 // }
194 // }
195
196 // const references
197 const std::vector<int>& nocc;
198 const std::vector<int>& nvirt;
199 const int nspin = 1;
200 const int nk = 1;
201 const std::vector<Parallel_Blkcyclic>& pX;
202 protected:
206 HamiltLR(const int& nspin_in,
207 const std::vector<int>& nocc_in,
208 const std::vector<int>& nvirt_in,
209 const std::vector<Parallel_Blkcyclic>& pX_in,
210 const K_Vectors& kv_in,
211 const Parallel_Orbitals& pmat_in)
212 : nocc(nocc_in), nvirt(nvirt_in), nspin(nspin_in),
213 nk(kv_in.get_nks() / nspin_in), pX(pX_in)
214 {
215 // Both Casida and Z operators use one transition-density spin channel.
217 &pmat_in, 1, kv_in.kvec_d, this->nk);
218 }
219
220 const bool tdm_sym = false;
221 T one()const;
224 std::unique_ptr<module_dm::DensityMatrix<T, T>> DM_trans;
225
228
229 std::function<void(const int&, const T* const)> cal_dm_trans;
230 };
231}
232
233#endif // ABACUS_SOURCE_LCAO_MODULE_LR_HAMILT_CASIDA_H
Definition exx_lri.h:50
Grid_Driver class for neighbor search interface.
Definition sltk_grid_driver.h:63
Class for k-points management.
Definition klist.h:20
Definition hamilt_casida.h:21
const int nk
Definition hamilt_casida.h:200
const std::vector< Parallel_Blkcyclic > & pX
Definition hamilt_casida.h:201
~HamiltLR()
Definition hamilt_casida.h:152
virtual void hPsi(const T *const psi_in, T *const hpsi, const int ld_psi, const int nband) const
Definition hamilt_casida.h:156
std::function< void(const int &, const T *const)> cal_dm_trans
Definition hamilt_casida.h:229
const int nspin
Definition hamilt_casida.h:199
HamiltLR(const std::string &xc_kernel, const int &nspin, const int &naos, const std::vector< int > &nocc, const std::vector< int > &nvirt, const UnitCell &ucell_in, const std::vector< double > &orb_cutoff, const Grid_Driver &gd_in, const psi::Psi< T > &psi_ks_in, const ModuleBase::matrix &eig_ks, std::weak_ptr< PotLRBase > pot_in, const K_Vectors &kv_in, const std::vector< Parallel_Blkcyclic > &pX_in, const Parallel_Blkcyclic &pc_in, const Parallel_Orbitals &pmat_in, const std::string &spin_type, const std::string &in_dir, const std::string &out_dir, const std::string &ri_hartree_benchmark="none", const std::vector< int > &aims_nbasis={})
Definition hamilt_casida.h:23
const std::vector< int > & nocc
Definition hamilt_casida.h:197
std::unique_ptr< module_dm::DensityMatrix< T, T > > DM_trans
Definition hamilt_casida.h:224
const bool tdm_sym
whether to symmetrize the transition density matrix
Definition hamilt_casida.h:220
std::vector< T > matrix() const
Definition hamilt_casida.cpp:8
HamiltLR(const int &nspin_in, const std::vector< int > &nocc_in, const std::vector< int > &nvirt_in, const std::vector< Parallel_Blkcyclic > &pX_in, const K_Vectors &kv_in, const Parallel_Orbitals &pmat_in)
Definition hamilt_casida.h:206
hamilt::Operator< T, base_device::DEVICE_CPU > * ops
first node operator, add operations from each operators
Definition hamilt_casida.h:227
const std::vector< int > & nvirt
Definition hamilt_casida.h:198
T one() const
@ CC_vo
Definition operator_lr_hxc.h:24
Definition matrix.h:15
double * c
Definition matrix.h:21
std::vector< ModuleBase::Vector3< double > > kvec_d
Direct coordinates of the points.
Definition reciprocal_grid.h:57
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
Definition parallel_orbitals.h:16
Parallel_Blkcyclic & para_2d()
Definition parallel_orbitals.h:24
Provide the basic information about unitcell.
Definition unitcell.h:16
Definition operator.h:42
Operator * next_op
interface type 3: return a Psi-type HPsi
Definition operator.h:112
virtual void act(const int nbands, const int nbasis, const int npol, const T *tmpsi_in, T *tmhpsi, const int ngk_ik=0, const bool is_first_node=false) const
Definition operator.h:95
Definition psi.h:37
#define T
Definition exp.cpp:237
TLRI< T > read_Cs_ao(const std::string &file_path, const double &threshold=1e-10)
Definition write_ri_cv.hpp:30
TLRI< T > read_Vs_abf(const std::string &file_path, const double &threshold=1e-10)
Definition write_ri_cv.hpp:136
TLRI< T > read_Cs_ao_all(const std::string &path, const double &threshold=1e-10)
Definition write_ri_cv.hpp:95
psi::Psi< T > get_psi_spin(const psi::Psi< T > &psi_in, const int &is, const int &nk)
get the Psi wrapper of the selected spin from the Psi object
Definition lr_util.hpp:235
std::unique_ptr< T > make_unique(Args &&... args)
Definition lr_util.hpp:33
void matsym(const T *in, const int n, T *out)
Definition lr_util.hpp:79
void initialize_DMR(module_dm::DensityMatrix< T, TR > &dm, const Parallel_Orbitals &pmat, const UnitCell &ucell, const Grid_Driver &gd, const std::vector< double > &orb_cutoff)
Definition lr_util_hcontainer.h:179
Definition esolver_lr_lcao_tddft.h:28
std::vector< container::Tensor > cal_dm_trans_blas(const T *const X_istate, const psi::Psi< T > &c, const int &nocc, const int &nvirt, const T factor=(T) 1.0, const LR_Util::MO_TYPE type=LR_Util::MO_TYPE::VO)
calculate the 2d-block transition density matrix in AO basis using ?gemm
std::vector< container::Tensor > cal_dm_trans_pblas(const T *const X_istate, const Parallel_Blkcyclic &px, const psi::Psi< T > &c, const Parallel_Blkcyclic &pc, const int naos, const int nocc, const int nvirt, const Parallel_Blkcyclic &pmat, const T factor=(T) 1.0, const LR_Util::MO_TYPE type=LR_Util::MO_TYPE::VO)
calculate the 2d-block transition density matrix in AO basis using p?gemm
void WARNING_QUIT(const std::string &, const std::string &)
Combine the functions of WARNING and QUIT.
Definition test_delley.cpp:15
void TITLE(const std::string &class_name, const std::string &function_name, const bool disable)
Definition tool_title.cpp:18
@ lr_dmtrans_exx
Definition operator.h:32
@ lr_dmtrans_hxc
Definition operator.h:29
std::pair< int, TC > TAC
Definition test_write_ri_cv.cpp:10
std::map< int, std::map< TAC, RI::Tensor< T > > > TLRI
Definition test_write_ri_cv.cpp:12