ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
rdmft_tools.h
Go to the documentation of this file.
1//==========================================================
2// Author: Jingang Han
3// DATE : 2024-03-11
4//==========================================================
5#ifndef RDMFT_TOOLS_H
6#define RDMFT_TOOLS_H
7
8#include "source_cell/klist.h"
9#include "source_psi/psi.h"
10#include "source_base/matrix.h"
20
24
25
26#ifdef __EXX
28// there are some operator reload to print data in different formats
29#endif
30
31#include <iostream>
32#include <type_traits>
33#include <complex>
34#include <vector>
35#include <iomanip>
36
37
38
39namespace rdmft
40{
41
43// for the dft-xc-functional part of xc-functional, just use the default is right! Or don't use the function
44double occNum_func(double eta, int symbol = 0, const std::string XC_func_rdmft = "hf", const double alpha_power = 1.0);
45
46
47// wfc and H_wfc need to be k_firest and provide wfc(ik, 0, 0) and H_wfc(ik, 0, 0)
49template <typename TK>
50void HkPsi(const Parallel_Orbitals* ParaV, const TK& HK, const TK& wfc, TK& H_wfc)
51{
52
53 const int one_int = 1;
54 //const double one_double = 1.0, zero_double = 0.0;
55 const std::complex<double> one_complex = {1.0, 0.0};
56 const std::complex<double> zero_complex = {0.0, 0.0};
57 const char N_char = 'N';
58 const char C_char = 'C'; // Using 'C' is consistent with the formula
59
60#ifdef __MPI
61 const int nbasis = ParaV->para_2d().desc()[2];
62 const int nbands = ParaV->desc_wfc[3];
63
64 //because wfc(bands, basis'), H(basis, basis'), we do wfc*H^T(in the perspective of cpp, not in fortran). And get H_wfc(bands, basis) is correct.
65 ScalapackConnector::gemm( C_char, N_char, nbasis, nbands, nbasis, one_complex, &HK, one_int, one_int, ParaV->para_2d().desc(),
66 &wfc, one_int, one_int, ParaV->desc_wfc, zero_complex, &H_wfc, one_int, one_int, ParaV->desc_wfc );
67#endif
68}
69
70
71template <>
72void HkPsi<double>(const Parallel_Orbitals* ParaV, const double& HK, const double& wfc, double& H_wfc);
73
74
76template <typename TK>
77void cal_bra_op_ket(const Parallel_Orbitals* ParaV, const Parallel_Blkcyclic& para_Eij_in, const TK& wfc, const TK& H_wfc, std::vector<TK>& Dmn)
78{
79 const int one_int = 1;
80 const std::complex<double> one_complex = {1.0, 0.0};
81 const std::complex<double> zero_complex = {0.0, 0.0};
82 const char N_char = 'N';
83 const char C_char = 'C';
84
85 const int nrow_bands = para_Eij_in.nrloc();
86 const int ncol_bands = para_Eij_in.ncloc();
87
88#ifdef __MPI
89 const int nbasis = ParaV->para_2d().desc()[2];
90 const int nbands = ParaV->desc_wfc[3];
91
92 ScalapackConnector::gemm( C_char, N_char, nbands, nbands, nbasis, one_complex, &wfc, one_int, one_int, ParaV->desc_wfc,
93 &H_wfc, one_int, one_int, ParaV->desc_wfc, zero_complex, &Dmn[0], one_int, one_int, para_Eij_in.desc() );
94#endif
95}
96
97
98template <>
99void cal_bra_op_ket<double>(const Parallel_Orbitals* ParaV, const Parallel_Blkcyclic& para_Eij_in,
100 const double& wfc, const double& H_wfc, std::vector<double>& Dmn);
101
102
104template <typename TK>
105void _diagonal_in_serial(const Parallel_Blkcyclic& para_Eij_in, const std::vector<TK>& Dmn, double* wfcHwfc)
106{
107 const int nrow_bands = para_Eij_in.nrloc();
108 const int ncol_bands = para_Eij_in.ncloc();
109
110 for(int i=0; i<nrow_bands; ++i)
111 {
112 int i_global = para_Eij_in.irglb(i);
113 for(int j=0; j<ncol_bands; ++j)
114 {
115 int j_global = para_Eij_in.icglb(j);
116 if(i_global==j_global)
117 {
118 // because the Dmn obtained from pzgemm_() is stored column-major
119 wfcHwfc[j_global] = std::real( Dmn[i+j*nrow_bands] );
120 }
121 }
122 }
123}
124
125
127template <typename TK>
128void occNum_MulPsi(const Parallel_Orbitals* ParaV, const ModuleBase::matrix& occ_number, psi::Psi<TK>& wfc, int symbol = 0,
129 const std::string XC_func_rdmft = "hf", const double alpha = 1.0)
130{
131 const int nk_local = wfc.get_nk();
132 const int nbands_local = wfc.get_nbands();
133 const int nbasis_local = wfc.get_nbasis();
134
135 // const int nbasis = ParaV->desc[2]; // need to be deleted
136 // const int nbands = ParaV->desc_wfc[3];
137
138 for (int ik = 0; ik < nk_local; ++ik)
139 {
140 for (int ib_local = 0; ib_local < nbands_local; ++ib_local) // ib_local < nbands_local , some problem, ParaV->ncol_bands
141 {
142 const double occNum_local = occNum_func( occ_number(ik, ParaV->para_2d().icglb(ib_local)), symbol, XC_func_rdmft, alpha);
143 TK* wfc_pointer = &(wfc(ik, ib_local, 0));
144 BlasConnector::scal(nbasis_local, occNum_local, wfc_pointer, 1);
145 }
146 }
147}
148
149
151template <typename TK>
152void add_psi(const Parallel_Orbitals* ParaV,
153 const K_Vectors* kv,
154 const ModuleBase::matrix& occ_number,
155 psi::Psi<TK>& psi_TV,
156 psi::Psi<TK>& psi_hartree,
157 psi::Psi<TK>& psi_dft_XC,
158 psi::Psi<TK>& psi_exx_XC,
159 psi::Psi<TK>& occNum_Hpsi,
160 const std::string XC_func_rdmft = "hf",
161 const double alpha = 1.0)
162{
163 const int nk = psi_TV.get_nk();
164 const int nbn_local = psi_TV.get_nbands();
165 const int nbs_local = psi_TV.get_nbasis();
166 occNum_MulPsi(ParaV, occ_number, psi_TV);
167 occNum_MulPsi(ParaV, occ_number, psi_hartree);
168 occNum_MulPsi(ParaV, occ_number, psi_dft_XC);
169 occNum_MulPsi(ParaV, occ_number, psi_exx_XC, 2, XC_func_rdmft, alpha);
170
171 // const int nbasis = ParaV->desc[2];
172 // const int nbands = ParaV->desc_wfc[3];
173
174 for(int ik=0; ik<nk; ++ik)
175 {
176 for(int inbn=0; inbn<nbn_local; ++inbn)
177 {
178 TK* p_occNum_Hpsi = &( occNum_Hpsi(ik, inbn, 0) );
179 for(int inbs=0; inbs<nbs_local; ++inbs)
180 {
181 p_occNum_Hpsi[inbs] = psi_TV(ik, inbn, inbs) + psi_hartree(ik, inbn, inbs) + psi_dft_XC(ik, inbn, inbs) + psi_exx_XC(ik, inbn, inbs);
182 }
183
184 // test, consider the wk into psi or dE/d(wfc)
185 BlasConnector::scal(nbs_local, kv->wk[ik], p_occNum_Hpsi, 1);
186 }
187 }
188
189}
190
196void occNum_Mul_wfcHwfc(const ModuleBase::matrix& occ_number,
197 const ModuleBase::matrix& wfcHwfc,
198 ModuleBase::matrix& occNum_wfcHwfc,
199 int symbol = 0,
200 const std::string XC_func_rdmft = "hf",
201 const double alpha = 1.0);
202
203
208void add_occNum(const K_Vectors& kv,
209 const ModuleBase::matrix& occ_number,
210 const ModuleBase::matrix& wfcHwfc_TV_in,
211 const ModuleBase::matrix& wfcHwfc_hartree_in,
212 const ModuleBase::matrix& wfcHwfc_dft_XC_in,
213 const ModuleBase::matrix& wfcHwfc_exx_XC_in,
214 ModuleBase::matrix& occNum_wfcHwfc,
215 const std::string XC_func_rdmft = "hf",
216 const double alpha = 1.0);
217
218
220void add_wfcHwfc(const ModuleBase::matrix& wg,
221 const ModuleBase::matrix& wk_fun_occNum,
222 const ModuleBase::matrix& wfcHwfc_TV_in,
223 const ModuleBase::matrix& wfcHwfc_hartree_in,
224 const ModuleBase::matrix& wfcHwfc_XC_in,
225 ModuleBase::matrix& occNum_wfcHwfc,
226 const std::string XC_func_rdmft,
227 const double alpha);
228
229
231double getEnergy(const ModuleBase::matrix& occNum_wfcHwfc);
232
233
234
235
236
237
239template <typename TK, typename TR>
240class Veff_rdmft : public hamilt::OperatorLCAO<TK, TR>
241{
242 public:
247 const std::vector<ModuleBase::Vector3<double>>& kvec_d_in,
248 elecstate::Potential* pot_in,
250 const UnitCell* ucell_in,
251 const std::vector<double>& orb_cutoff,
252 const Grid_Driver* GridD_in,
253 const int& nspin,
254 const Charge* charge_in,
255 const ModulePW::PW_Basis* rho_basis_in,
256 const ModuleBase::matrix* vloc_in,
257 const ModuleBase::ComplexMatrix* sf_in,
258 const std::string potential_in,
259 double* etxc_in = nullptr,
260 double* vtxc_in = nullptr)
261 : orb_cutoff_(orb_cutoff), pot(pot_in), ucell(ucell_in),
262 gd(GridD_in), hamilt::OperatorLCAO<TK, TR>(hsk_in, kvec_d_in, hR_in), charge_(charge_in),
263 rho_basis_(rho_basis_in), vloc_(vloc_in), sf_(sf_in), potential_(potential_in), etxc(etxc_in), vtxc(vtxc_in)
264 {
266
267 this->initialize_HR(ucell_in, GridD_in);
268 }
269
271
278 virtual void contributeHR() override;
279
280 const UnitCell* ucell = nullptr;
281
282 const Grid_Driver* gd = nullptr;
283
284 private:
285
286 std::vector<double> orb_cutoff_;
287
288 // Charge calculating method in LCAO base and contained grid base calculation: DM_R, DM, pvpR_reduced
289
291
292 int nspin = 1;
294
300 void initialize_HR(const UnitCell* ucell_in, const Grid_Driver* GridD_in);
301
302 // added by jghan
303
304 const Charge* charge_ = nullptr;
305
306 std::string potential_;
307
309
311
313
314 double* etxc = nullptr;
315
316 double* vtxc = nullptr;
317
318};
319
320}
321
322#endif
static void scal(const int n, const float alpha, float *X, const int incX, base_device::AbacusDevice_t device_type=base_device::AbacusDevice_t::CpuDevice)
Definition blas_connector_vector.cpp:81
Definition charge.h:24
Grid_Driver class for neighbor search interface.
Definition sltk_grid_driver.h:63
Class for k-points management.
Definition klist.h:20
Definition complexmatrix.h:11
3 elements vector
Definition vector3.h:21
Definition matrix.h:15
std::vector< double > wk
Weight of each point.
Definition reciprocal_grid.h:61
A class which can convert a function of "r" to the corresponding linear superposition of plane waves ...
Definition pw_basis.h:60
Parallel block-cyclic matrix distribution.
Definition parallel_blkcyc.h:25
int irglb(const int irloc) const
Definition parallel_blkcyc.h:88
int ncloc() const
Definition parallel_blkcyc.h:40
const int * desc() const
Definition parallel_blkcyc.h:125
int nrloc() const
Definition parallel_blkcyc.h:35
int icglb(const int icloc) const
Definition parallel_blkcyc.h:93
Definition parallel_orbitals.h:16
int desc_wfc[9]
Definition parallel_orbitals.h:60
Parallel_Blkcyclic & para_2d()
Definition parallel_orbitals.h:24
static void gemm(const char transa, const char transb, const int M, const int N, const int K, const double alpha, const double *A, const int IA, const int JA, const int *DESCA, const double *B, const int IB, const int JB, const int *DESCB, const double beta, double *C, const int IC, const int JC, const int *DESCC)
Definition scalapack_connector.h:295
Provide the basic information about unitcell.
Definition unitcell.h:16
Definition potential_new.h:58
Definition hcontainer.h:147
Definition hs_matrix_k.h:12
Definition operator_lcao.h:13
OperatorLCAO(HS_Matrix_K< TK > *hsk_in, const std::vector< ModuleBase::Vector3< double > > &kvec_d_in, HContainer< TR > *hR_in)
H(R) matrix, R is the Bravis lattice vector.
Definition operator_lcao.h:16
enum calculation_type cal_type
Definition operator.h:129
Definition psi.h:37
const int & get_nbands() const
Definition psi.cpp:337
const int & get_nk() const
Definition psi.cpp:331
const int & get_nbasis() const
Definition psi.cpp:343
double * etxc
Definition rdmft_tools.h:314
double * vtxc
Definition rdmft_tools.h:316
std::string potential_
Definition rdmft_tools.h:306
const ModuleBase::ComplexMatrix * sf_
Definition rdmft_tools.h:312
const Grid_Driver * gd
Definition rdmft_tools.h:282
elecstate::Potential * pot
Definition rdmft_tools.h:290
Veff_rdmft(hamilt::HS_Matrix_K< TK > *hsk_in, const std::vector< ModuleBase::Vector3< double > > &kvec_d_in, elecstate::Potential *pot_in, hamilt::HContainer< TR > *hR_in, const UnitCell *ucell_in, const std::vector< double > &orb_cutoff, const Grid_Driver *GridD_in, const int &nspin, const Charge *charge_in, const ModulePW::PW_Basis *rho_basis_in, const ModuleBase::matrix *vloc_in, const ModuleBase::ComplexMatrix *sf_in, const std::string potential_in, double *etxc_in=nullptr, double *vtxc_in=nullptr)
Construct a new Veff object for multi-kpoint calculation.
Definition rdmft_tools.h:246
int current_spin
Definition rdmft_tools.h:293
~Veff_rdmft()
Definition rdmft_tools.h:270
virtual void contributeHR() override
contributeHR() is used to calculate the HR matrix <phi_{\mu, 0}|V_{eff}|phi_{\nu, R}...
const ModulePW::PW_Basis * rho_basis_
Definition rdmft_tools.h:308
const UnitCell * ucell
Definition rdmft_tools.h:280
const Charge * charge_
Definition rdmft_tools.h:304
int nspin
Definition rdmft_tools.h:292
void initialize_HR(const UnitCell *ucell_in, const Grid_Driver *GridD_in)
initialize HR, search the nearest neighbor atoms HContainer is used to store the electronic kinetic m...
Definition rdmft_tools.cpp:188
const ModuleBase::matrix * vloc_
Definition rdmft_tools.h:310
std::vector< double > orb_cutoff_
Definition rdmft_tools.h:286
Definition wf_op.cpp:6
@ lcao_gint
Definition operator.h:23
Reduced Density Matrix Functional Theory (RDMFT).
Definition rdmft.cpp:29
void HkPsi< double >(const Parallel_Orbitals *ParaV, const double &HK, const double &wfc, double &H_wfc)
Definition rdmft_tools.cpp:27
void cal_bra_op_ket< double >(const Parallel_Orbitals *ParaV, const Parallel_Blkcyclic &para_Eij_in, const double &wfc, const double &H_wfc, std::vector< double > &Dmn)
Definition rdmft_tools.cpp:51
void occNum_Mul_wfcHwfc(const ModuleBase::matrix &occ_number, const ModuleBase::matrix &wfcHwfc, ModuleBase::matrix &occNum_wfcHwfc, int symbol, const std::string XC_func_rdmft, const double alpha)
occNum_wfcHwfc = occNum*wfcHwfc + occNum_wfcHwfc
Definition rdmft_tools.cpp:78
void add_occNum(const K_Vectors &kv, const ModuleBase::matrix &occ_number, const ModuleBase::matrix &wfcHwfc_TV_in, const ModuleBase::matrix &wfcHwfc_hartree_in, const ModuleBase::matrix &wfcHwfc_dft_XC_in, const ModuleBase::matrix &wfcHwfc_exx_XC_in, ModuleBase::matrix &occNum_wfcHwfc, const std::string XC_func_rdmft, const double alpha)
Default symbol = 0 for the gradient of Etotal with respect to occupancy, symbol = 1 for the relevant ...
Definition rdmft_tools.cpp:96
void cal_bra_op_ket(const Parallel_Orbitals *ParaV, const Parallel_Blkcyclic &para_Eij_in, const TK &wfc, const TK &H_wfc, std::vector< TK > &Dmn)
implement matrix multiplication of sum_mu conj(wfc(ik, m ,mu)) * op_wfc(ik, n, mu)
Definition rdmft_tools.h:77
double getEnergy(const ModuleBase::matrix &occNum_wfcHwfc)
give certain occNum_wfcHwfc, get the corresponding energy
Definition rdmft_tools.cpp:142
void _diagonal_in_serial(const Parallel_Blkcyclic &para_Eij_in, const std::vector< TK > &Dmn, double *wfcHwfc)
for Dmn that conforms to the 2d-block rule, get its diagonal elements
Definition rdmft_tools.h:105
void HkPsi(const Parallel_Orbitals *ParaV, const TK &HK, const TK &wfc, TK &H_wfc)
implement matrix multiplication of Hk^dagger and psi
Definition rdmft_tools.h:50
void add_psi(const Parallel_Orbitals *ParaV, const K_Vectors *kv, const ModuleBase::matrix &occ_number, psi::Psi< TK > &psi_TV, psi::Psi< TK > &psi_hartree, psi::Psi< TK > &psi_dft_XC, psi::Psi< TK > &psi_exx_XC, psi::Psi< TK > &occNum_Hpsi, const std::string XC_func_rdmft="hf", const double alpha=1.0)
add psi with eta and g(eta)
Definition rdmft_tools.h:152
double occNum_func(const double eta, const int symbol, const std::string XC_func_rdmft, double alpha)
now support XC_func_rdmft = "hf", "muller", "power", "pbe", "pbe0". "wp22" and "cwp22" is realizing.
Definition rdmft_tools.cpp:159
void occNum_MulPsi(const Parallel_Orbitals *ParaV, const ModuleBase::matrix &occ_number, psi::Psi< TK > &wfc, int symbol=0, const std::string XC_func_rdmft="hf", const double alpha=1.0)
realize occNum_wfc = occNum * wfc. Calling this function and we can get wfc = occNum*wfc.
Definition rdmft_tools.h:128
void add_wfcHwfc(const ModuleBase::matrix &wg, const ModuleBase::matrix &wk_fun_occNum, const ModuleBase::matrix &wfcHwfc_TV_in, const ModuleBase::matrix &wfcHwfc_hartree_in, const ModuleBase::matrix &wfcHwfc_XC_in, ModuleBase::matrix &occNum_wfcHwfc, const std::string XC_func_rdmft, const double alpha)
do wk*g(occNum)*wfcHwfc and add for TV, hartree, XC. This function just use once, so it can be replac...
Definition rdmft_tools.cpp:125
Grid_Driver class for neighbor search interface.