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_psi/psi.h"
21
25
26
27#ifdef __EXX
30// there are some operator reload to print data in different formats
31#endif
32
33#include <iostream>
34#include <type_traits>
35#include <complex>
36#include <vector>
37#include <iomanip>
38
39
40
41namespace rdmft
42{
43
45// for the dft-xc-functional part of xc-functional, just use the default is right! Or don't use the function
46double occNum_func(double eta, int symbol = 0, const std::string XC_func_rdmft = "hf", const double alpha_power = 1.0);
47
48
49template <typename TK>
51{
52 TK* pwfc = &wfc(0, 0, 0);
53 for(int i=0; i<wfc.size(); ++i) { pwfc[i] = std::conj( pwfc[i] );
54}
55}
56
57
58template <>
60
61
62// wfc and H_wfc need to be k_firest and provide wfc(ik, 0, 0) and H_wfc(ik, 0, 0)
64template <typename TK>
65void HkPsi(const Parallel_Orbitals* ParaV, const TK& HK, const TK& wfc, TK& H_wfc)
66{
67
68 const int one_int = 1;
69 //const double one_double = 1.0, zero_double = 0.0;
70 const std::complex<double> one_complex = {1.0, 0.0};
71 const std::complex<double> zero_complex = {0.0, 0.0};
72 const char N_char = 'N';
73 const char C_char = 'C'; // Using 'C' is consistent with the formula
74
75#ifdef __MPI
76 const int nbasis = ParaV->desc[2];
77 const int nbands = ParaV->desc_wfc[3];
78
79 //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.
80 pzgemm_( &C_char, &N_char, &nbasis, &nbands, &nbasis, &one_complex, &HK, &one_int, &one_int, ParaV->desc,
81 &wfc, &one_int, &one_int, ParaV->desc_wfc, &zero_complex, &H_wfc, &one_int, &one_int, ParaV->desc_wfc );
82#endif
83}
84
85
86template <>
87void HkPsi<double>(const Parallel_Orbitals* ParaV, const double& HK, const double& wfc, double& H_wfc);
88
89
91template <typename TK>
92void cal_bra_op_ket(const Parallel_Orbitals* ParaV, const Parallel_2D& para_Eij_in, const TK& wfc, const TK& H_wfc, std::vector<TK>& Dmn)
93{
94 const int one_int = 1;
95 const std::complex<double> one_complex = {1.0, 0.0};
96 const std::complex<double> zero_complex = {0.0, 0.0};
97 const char N_char = 'N';
98 const char C_char = 'C';
99
100 const int nrow_bands = para_Eij_in.get_row_size();
101 const int ncol_bands = para_Eij_in.get_col_size();
102
103#ifdef __MPI
104 const int nbasis = ParaV->desc[2];
105 const int nbands = ParaV->desc_wfc[3];
106
107 pzgemm_( &C_char, &N_char, &nbands, &nbands, &nbasis, &one_complex, &wfc, &one_int, &one_int, ParaV->desc_wfc,
108 &H_wfc, &one_int, &one_int, ParaV->desc_wfc, &zero_complex, &Dmn[0], &one_int, &one_int, para_Eij_in.desc );
109#endif
110}
111
112
113template <>
114void cal_bra_op_ket<double>(const Parallel_Orbitals* ParaV, const Parallel_2D& para_Eij_in,
115 const double& wfc, const double& H_wfc, std::vector<double>& Dmn);
116
117
119template <typename TK>
120void _diagonal_in_serial(const Parallel_2D& para_Eij_in, const std::vector<TK>& Dmn, double* wfcHwfc)
121{
122 const int nrow_bands = para_Eij_in.get_row_size();
123 const int ncol_bands = para_Eij_in.get_col_size();
124
125 for(int i=0; i<nrow_bands; ++i)
126 {
127 int i_global = para_Eij_in.local2global_row(i);
128 for(int j=0; j<ncol_bands; ++j)
129 {
130 int j_global = para_Eij_in.local2global_col(j);
131 if(i_global==j_global)
132 {
133 // because the Dmn obtained from pzgemm_() is stored column-major
134 wfcHwfc[j_global] = std::real( Dmn[i+j*nrow_bands] );
135 }
136 }
137 }
138}
139
140
142template <typename TK>
143void occNum_MulPsi(const Parallel_Orbitals* ParaV, const ModuleBase::matrix& occ_number, psi::Psi<TK>& wfc, int symbol = 0,
144 const std::string XC_func_rdmft = "hf", const double alpha = 1.0)
145{
146 const int nk_local = wfc.get_nk();
147 const int nbands_local = wfc.get_nbands();
148 const int nbasis_local = wfc.get_nbasis();
149
150 // const int nbasis = ParaV->desc[2]; // need to be deleted
151 // const int nbands = ParaV->desc_wfc[3];
152
153 for (int ik = 0; ik < nk_local; ++ik)
154 {
155 for (int ib_local = 0; ib_local < nbands_local; ++ib_local) // ib_local < nbands_local , some problem, ParaV->ncol_bands
156 {
157 const double occNum_local = occNum_func( occ_number(ik, ParaV->local2global_col(ib_local)), symbol, XC_func_rdmft, alpha);
158 TK* wfc_pointer = &(wfc(ik, ib_local, 0));
159 BlasConnector::scal(nbasis_local, occNum_local, wfc_pointer, 1);
160 }
161 }
162}
163
164
166template <typename TK>
167void add_psi(const Parallel_Orbitals* ParaV,
168 const K_Vectors* kv,
169 const ModuleBase::matrix& occ_number,
170 psi::Psi<TK>& psi_TV,
171 psi::Psi<TK>& psi_hartree,
172 psi::Psi<TK>& psi_dft_XC,
173 psi::Psi<TK>& psi_exx_XC,
174 psi::Psi<TK>& occNum_Hpsi,
175 const std::string XC_func_rdmft = "hf",
176 const double alpha = 1.0)
177{
178 const int nk = psi_TV.get_nk();
179 const int nbn_local = psi_TV.get_nbands();
180 const int nbs_local = psi_TV.get_nbasis();
181 occNum_MulPsi(ParaV, occ_number, psi_TV);
182 occNum_MulPsi(ParaV, occ_number, psi_hartree);
183 occNum_MulPsi(ParaV, occ_number, psi_dft_XC);
184 occNum_MulPsi(ParaV, occ_number, psi_exx_XC, 2, XC_func_rdmft, alpha);
185
186 // const int nbasis = ParaV->desc[2];
187 // const int nbands = ParaV->desc_wfc[3];
188
189 for(int ik=0; ik<nk; ++ik)
190 {
191 for(int inbn=0; inbn<nbn_local; ++inbn)
192 {
193 TK* p_occNum_Hpsi = &( occNum_Hpsi(ik, inbn, 0) );
194 for(int inbs=0; inbs<nbs_local; ++inbs)
195 {
196 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);
197 }
198
199 // test, consider the wk into psi or dE/d(wfc)
200 BlasConnector::scal(nbs_local, kv->wk[ik], p_occNum_Hpsi, 1);
201 }
202 }
203
204}
205
211void occNum_Mul_wfcHwfc(const ModuleBase::matrix& occ_number,
212 const ModuleBase::matrix& wfcHwfc,
213 ModuleBase::matrix& occNum_wfcHwfc,
214 int symbol = 0,
215 const std::string XC_func_rdmft = "hf",
216 const double alpha = 1.0);
217
218
223void add_occNum(const K_Vectors& kv,
224 const ModuleBase::matrix& occ_number,
225 const ModuleBase::matrix& wfcHwfc_TV_in,
226 const ModuleBase::matrix& wfcHwfc_hartree_in,
227 const ModuleBase::matrix& wfcHwfc_dft_XC_in,
228 const ModuleBase::matrix& wfcHwfc_exx_XC_in,
229 ModuleBase::matrix& occNum_wfcHwfc,
230 const std::string XC_func_rdmft = "hf",
231 const double alpha = 1.0);
232
233
235void add_wfcHwfc(const ModuleBase::matrix& wg,
236 const ModuleBase::matrix& wk_fun_occNum,
237 const ModuleBase::matrix& wfcHwfc_TV_in,
238 const ModuleBase::matrix& wfcHwfc_hartree_in,
239 const ModuleBase::matrix& wfcHwfc_XC_in,
240 ModuleBase::matrix& occNum_wfcHwfc,
241 const std::string XC_func_rdmft,
242 const double alpha);
243
244
246double getEnergy(const ModuleBase::matrix& occNum_wfcHwfc);
247
248
249
250
251
252
254template <typename TK, typename TR>
255class Veff_rdmft : public hamilt::OperatorLCAO<TK, TR>
256{
257 public:
262 const std::vector<ModuleBase::Vector3<double>>& kvec_d_in,
263 elecstate::Potential* pot_in,
265 const UnitCell* ucell_in,
266 const std::vector<double>& orb_cutoff,
267 const Grid_Driver* GridD_in,
268 const int& nspin,
269 const Charge* charge_in,
270 const ModulePW::PW_Basis* rho_basis_in,
271 const ModuleBase::matrix* vloc_in,
272 const ModuleBase::ComplexMatrix* sf_in,
273 const std::string potential_in,
274 double* etxc_in = nullptr,
275 double* vtxc_in = nullptr)
276 : orb_cutoff_(orb_cutoff), pot(pot_in), ucell(ucell_in),
277 gd(GridD_in), hamilt::OperatorLCAO<TK, TR>(hsk_in, kvec_d_in, hR_in), charge_(charge_in),
278 rho_basis_(rho_basis_in), vloc_(vloc_in), sf_(sf_in), potential_(potential_in), etxc(etxc_in), vtxc(vtxc_in)
279 {
281
282 this->initialize_HR(ucell_in, GridD_in);
283 }
284
286
293 virtual void contributeHR() override;
294
296
298
299 private:
300
301 std::vector<double> orb_cutoff_;
302
303 // Charge calculating method in LCAO base and contained grid base calculation: DM_R, DM, pvpR_reduced
304
306
307 int nspin = 1;
309
315 void initialize_HR(const UnitCell* ucell_in, const Grid_Driver* GridD_in);
316
317 // added by jghan
318
320
321 std::string potential_;
322
324
326
328
329 double* etxc;
330
331 double* vtxc;
332
333};
334
335}
336
337#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:80
Definition charge.h:18
Definition sltk_grid_driver.h:43
Definition klist.h:13
std::vector< double > wk
Direct coordinates of k points.
Definition klist.h:18
Definition complexmatrix.h:14
3 elements vector
Definition vector3.h:22
Definition matrix.h:19
A class which can convert a function of "r" to the corresponding linear superposition of plane waves ...
Definition pw_basis.h:56
This class packs the basic information of 2D-block-cyclic parallel distribution of an arbitrary matri...
Definition parallel_2d.h:12
int local2global_col(const int ilc) const
get the global index of a local index (col)
Definition parallel_2d.h:63
int get_row_size() const
number of local rows
Definition parallel_2d.h:21
int local2global_row(const int ilr) const
get the global index of a local index (row)
Definition parallel_2d.h:57
int desc[9]
ScaLAPACK descriptor.
Definition parallel_2d.h:103
int get_col_size() const
number of local columns
Definition parallel_2d.h:27
Definition parallel_orbitals.h:9
int desc_wfc[9]
Definition parallel_orbitals.h:37
Definition unitcell.h:17
Definition potential_new.h:49
Definition hcontainer.h:144
Definition hs_matrix_k.hpp:11
Definition operator_lcao.h:12
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:15
enum calculation_type cal_type
Definition operator.h:107
Definition psi.h:37
const int & get_nbands() const
Definition psi.cpp:341
const int & get_nk() const
Definition psi.cpp:335
size_t size() const
Definition psi.cpp:353
const int & get_nbasis() const
Definition psi.cpp:347
this part of the code is copying from class Veff and do some modifications.
Definition rdmft_tools.h:256
double * etxc
Definition rdmft_tools.h:329
double * vtxc
Definition rdmft_tools.h:331
std::string potential_
Definition rdmft_tools.h:321
const ModuleBase::ComplexMatrix * sf_
Definition rdmft_tools.h:327
const Grid_Driver * gd
Definition rdmft_tools.h:297
elecstate::Potential * pot
Definition rdmft_tools.h:305
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:261
int current_spin
Definition rdmft_tools.h:308
virtual void contributeHR() override
contributeHR() is used to calculate the HR matrix <phi_{\mu, 0}|V_{eff}|phi_{\nu, R}> the contributio...
const ModulePW::PW_Basis * rho_basis_
Definition rdmft_tools.h:323
const UnitCell * ucell
Definition rdmft_tools.h:295
const Charge * charge_
Definition rdmft_tools.h:319
int nspin
Definition rdmft_tools.h:307
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:199
const ModuleBase::matrix * vloc_
Definition rdmft_tools.h:325
std::vector< double > orb_cutoff_
Definition rdmft_tools.h:301
Definition hamilt.h:12
Reduced Density Matrix Functional Theory (RDMFT)
Definition rdmft.cpp:24
void cal_bra_op_ket< double >(const Parallel_Orbitals *ParaV, const Parallel_2D &para_Eij_in, const double &wfc, const double &H_wfc, std::vector< double > &Dmn)
Definition rdmft_tools.cpp:55
void HkPsi< double >(const Parallel_Orbitals *ParaV, const double &HK, const double &wfc, double &H_wfc)
Definition rdmft_tools.cpp:31
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:82
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:100
void conj_psi< double >(psi::Psi< double > &wfc)
Definition rdmft_tools.cpp:28
void cal_bra_op_ket(const Parallel_Orbitals *ParaV, const Parallel_2D &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:92
double getEnergy(const ModuleBase::matrix &occNum_wfcHwfc)
give certain occNum_wfcHwfc, get the corresponding energy
Definition rdmft_tools.cpp:146
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:65
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:167
void _diagonal_in_serial(const Parallel_2D &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:120
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:163
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:143
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:129
void conj_psi(psi::Psi< TK > &wfc)
Definition rdmft_tools.h:50
void pzgemm_(const char *transa, const char *transb, const int *M, const int *N, const int *K, const std::complex< double > *alpha, const std::complex< double > *A, const int *IA, const int *JA, const int *DESCA, const std::complex< double > *B, const int *IB, const int *JB, const int *DESCB, const std::complex< double > *beta, std::complex< double > *C, const int *IC, const int *JC, const int *DESCC)