Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Change some functions in module_deepks into template. #5731

Merged
merged 10 commits into from
Dec 16, 2024
15 changes: 9 additions & 6 deletions source/module_hamilt_lcao/hamilt_lcaodft/FORCE_gamma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,21 @@ void Force_LCAO<double>::ftable(const bool isforce,

GlobalC::ld.cal_gedm(ucell.nat);

DeePKS_domain::cal_f_delta_gamma(dm_gamma,
ucell,
orb,
gd,
const int nks=1;
DeePKS_domain::cal_f_delta_gamma(dm_gamma,
ucell,
orb,
gd,
*this->ParaV,
GlobalC::ld.lmaxd,
nks,
kv->kvec_d,
GlobalC::ld.nlm_save,
GlobalC::ld.gedm,
GlobalC::ld.inl_index,
GlobalC::ld.F_delta,
isstress,
svnl_dalpha);
isstress,
svnl_dalpha);

#ifdef __MPI
Parallel_Reduce::reduce_all(GlobalC::ld.F_delta.c, GlobalC::ld.F_delta.nr * GlobalC::ld.F_delta.nc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ void DeePKS<OperatorLCAO<double, double>>::contributeHR()
{
ModuleBase::timer::tick("DeePKS", "contributeHR");
const Parallel_Orbitals* pv = this->hsk->get_pv();
GlobalC::ld.cal_projected_DM(this->DM, *this->ucell, *ptr_orb_, *(this->gd));
GlobalC::ld.cal_projected_DM<double>(this->DM, *this->ucell, *ptr_orb_, *(this->gd));
GlobalC::ld.cal_descriptor(this->ucell->nat);
GlobalC::ld.cal_gedm(this->ucell->nat);
// recalculate the H_V_delta
Expand Down Expand Up @@ -186,7 +186,7 @@ void DeePKS<OperatorLCAO<std::complex<double>, double>>::contributeHR()
{
ModuleBase::timer::tick("DeePKS", "contributeHR");

GlobalC::ld.cal_projected_DM(this->DM, *this->ucell, *ptr_orb_, *this->gd);
GlobalC::ld.cal_projected_DM<std::complex<double>>(this->DM, *this->ucell, *ptr_orb_, *this->gd);
GlobalC::ld.cal_descriptor(this->ucell->nat);
// calculate dE/dD
GlobalC::ld.cal_gedm(this->ucell->nat);
Expand Down Expand Up @@ -219,7 +219,7 @@ void DeePKS<OperatorLCAO<std::complex<double>, std::complex<double>>>::contribut
{
ModuleBase::timer::tick("DeePKS", "contributeHR");

GlobalC::ld.cal_projected_DM(this->DM, *this->ucell, *ptr_orb_, *this->gd);
GlobalC::ld.cal_projected_DM<std::complex<double>>(this->DM, *this->ucell, *ptr_orb_, *this->gd);
GlobalC::ld.cal_descriptor(this->ucell->nat);
// calculate dE/dD
GlobalC::ld.cal_gedm(this->ucell->nat);
Expand Down
9 changes: 4 additions & 5 deletions source/module_hamilt_lcao/module_deepks/LCAO_deepks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,14 +512,13 @@ void LCAO_Deepks::del_v_delta_pdm_shell(const int nks,const int nlocal)
return;
}

void LCAO_Deepks::dpks_cal_e_delta_band(const std::vector<std::vector<double>>& dm, const int nks)
template <typename TK>
void LCAO_Deepks::dpks_cal_e_delta_band(const std::vector<std::vector<TK>>& dm, const int nks)
{
this->cal_e_delta_band(dm, nks);
}

void LCAO_Deepks::dpks_cal_e_delta_band(const std::vector<std::vector<std::complex<double>>>& dm, const int nks)
{
this->cal_e_delta_band(dm, nks);
}
template void LCAO_Deepks::dpks_cal_e_delta_band<double>(const std::vector<std::vector<double>>& dm, const int nks);
template void LCAO_Deepks::dpks_cal_e_delta_band<std::complex<double>>(const std::vector<std::vector<std::complex<double>>>& dm, const int nks);

#endif
50 changes: 13 additions & 37 deletions source/module_hamilt_lcao/module_deepks/LCAO_deepks.h
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,10 @@ class LCAO_Deepks

// There are 6 subroutines in this file:
// 1. cal_projected_DM, which is used for calculating pdm for gamma point calculation
// 2. cal_projected_DM_k, counterpart of 1, for multi-k
// 3. check_projected_dm, which prints pdm to descriptor.dat
// 2. check_projected_dm, which prints pdm to descriptor.dat

// 4. cal_gdmx, calculating gdmx (and optionally gdm_epsl for stress) for gamma point
// 5. check_gdmx, which prints gdmx to a series of .dat files
// 3. cal_gdmx, calculating gdmx (and optionally gdm_epsl for stress) for gamma point
// 4. check_gdmx, which prints gdmx to a series of .dat files

public:
/**
Expand All @@ -299,28 +298,14 @@ class LCAO_Deepks
* 2. SCF calculation of DeePKS with init_chg = file and pdm has been read for restarting SCF
* 3. Relax/Cell-Relax/MD calculation, non-first step will use the convergence pdm from the last step as initial pdm
*/
void cal_projected_DM(const elecstate::DensityMatrix<double, double>* dm,
const UnitCell& ucell,
const LCAO_Orbitals& orb,
const Grid_Driver& GridD);

void cal_projected_DM(const elecstate::DensityMatrix<std::complex<double>, double>* dm,
template <typename TK>
void cal_projected_DM(const elecstate::DensityMatrix<TK, double>* dm,
const UnitCell& ucell,
const LCAO_Orbitals& orb,
const Grid_Driver& GridD);

void check_projected_dm();

void cal_projected_DM_equiv(const elecstate::DensityMatrix<double, double>* dm,
const UnitCell& ucell,
const LCAO_Orbitals& orb,
const Grid_Driver& GridD);

void cal_projected_DM_k_equiv(const elecstate::DensityMatrix<std::complex<double>, double>* dm,
const UnitCell& ucell,
const LCAO_Orbitals& orb,
const Grid_Driver& GridD);

// calculate the gradient of pdm with regard to atomic positions
// d/dX D_{Inl,mm'}
template <typename TK>
Expand Down Expand Up @@ -358,21 +343,18 @@ class LCAO_Deepks
// tr (rho * V_delta)

// Four subroutines are contained in the file:
// 5. cal_e_delta_band : calculates e_delta_bands for gamma only
// 6. cal_e_delta_band_k : counterpart of 4, for multi-k
// 5. cal_e_delta_band : calculates e_delta_bands

public:
/// calculate tr(\rho V_delta)
// void cal_e_delta_band(const std::vector<ModuleBase::matrix>& dm/**<[in] density matrix*/);
void cal_e_delta_band(const std::vector<std::vector<double>>& dm /**<[in] density matrix*/, const int /*nks*/);
// void cal_e_delta_band_k(const std::vector<ModuleBase::ComplexMatrix>& dm/**<[in] density matrix*/,
// const int nks);
void cal_e_delta_band(const std::vector<std::vector<std::complex<double>>>& dm /**<[in] density matrix*/,
const int nks);
template <typename TK>
void cal_e_delta_band(const std::vector<std::vector<TK>>& dm /**<[in] density matrix*/, const int nks);

//! a temporary interface for cal_e_delta_band and cal_e_delta_band_k
void dpks_cal_e_delta_band(const std::vector<std::vector<double>>& dm, const int nks);
void dpks_cal_e_delta_band(const std::vector<std::vector<std::complex<double>>>& dm, const int nks);
template <typename TK>
void dpks_cal_e_delta_band(const std::vector<std::vector<TK>>& dm, const int nks);


//-------------------
// LCAO_deepks_odelta.cpp
Expand All @@ -381,17 +363,11 @@ class LCAO_Deepks
// This file contains subroutines for calculating O_delta,
// which corresponds to the correction of the band gap.

// There are two subroutines in this file:
// 1. cal_o_delta, which is used for gamma point calculation
// 2. cal_o_delta_k, which is used for multi-k calculation

public:
void cal_o_delta(const std::vector<std::vector<ModuleBase::matrix>>&
template <typename TK, typename TH>
void cal_o_delta(const std::vector<std::vector<TH>>&
dm_hl /**<[in] modified density matrix that contains HOMO and LUMO only*/,
const int nks);
void cal_o_delta(const std::vector<std::vector<ModuleBase::ComplexMatrix>>&
dm_hl /**<[in] modified density matrix that contains HOMO and LUMO only*/,
const int nks);

//-------------------
// LCAO_deepks_torch.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void LCAO_Deepks_Interface<TK, TR>::out_deepks_labels(const double& etot,
}

ld->cal_orbital_precalc<TK,TH>(dm_bandgap, nat, nks, kvec_d, ucell, orb, GridD);
ld->cal_o_delta(dm_bandgap, nks);
ld->cal_o_delta<TK,TH>(dm_bandgap, nks);

// save obase and orbital_precalc
LCAO_deepks_io::save_npy_orbital_precalc(nat,
Expand Down Expand Up @@ -249,7 +249,7 @@ void LCAO_Deepks_Interface<TK, TR>::out_deepks_labels(const double& etot,
// when deepks_scf is on, the init pdm should be same as the out pdm, so we should not recalculate the pdm
if(!PARAM.inp.deepks_scf)
{
ld->cal_projected_DM(dm, ucell, orb, GridD);
ld->cal_projected_DM<TK>(dm, ucell, orb, GridD);
}

ld->check_projected_dm(); // print out the projected dm for NSCF calculaiton
Expand Down
88 changes: 39 additions & 49 deletions source/module_hamilt_lcao/module_deepks/LCAO_deepks_odelta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,29 @@
//which is defind as sum_mu,nu rho^{hl}_mu,nu <chi_mu|alpha>V(D)<alpha|chi_nu>
//where rho^{hl}_mu,nu = C_{L\mu}C_{L\nu} - C_{H\mu}C_{H\nu}, L for LUMO, H for HOMO

//There are two subroutines in this file:
//1. cal_o_delta, which is used for gamma point calculation
//2. cal_o_delta_k, which is used for multi-k calculation

#ifdef __DEEPKS

#include "LCAO_deepks.h"
#include "module_base/parallel_reduce.h"

void LCAO_Deepks::cal_o_delta(const std::vector<std::vector<ModuleBase::matrix>>& dm_hl, const int nks)
template <typename TK, typename TH>
void LCAO_Deepks::cal_o_delta(const std::vector<std::vector<TH>>& dm_hl, const int nks)
{
ModuleBase::TITLE("LCAO_Deepks", "cal_o_delta");
this->o_delta.zero_out();
for (int hl = 0; hl < 1; ++hl)
{
for (int i = 0; i < PARAM.globalv.nlocal; ++i)
{
for (int j = 0; j < PARAM.globalv.nlocal; ++j)
{
const int mu = pv->global2local_row(j);
const int nu = pv->global2local_col(i);

if (mu >= 0 && nu >= 0)
{
const int index = nu * pv->nrow + mu;
for (int is = 0; is < PARAM.inp.nspin; ++is)
{
this->o_delta(0,hl) += dm_hl[hl][is](nu, mu) * this->H_V_delta[0][index];
}
}
}
}
Parallel_Reduce::reduce_all(this->o_delta(0, hl));
}
return;
}


//calculating the correction of (LUMO-HOMO) energies, i.e., band gap corrections
//for multi_k calculations
void LCAO_Deepks::cal_o_delta(const std::vector<std::vector<ModuleBase::ComplexMatrix>>& dm_hl,
const int nks)
{
ModuleBase::TITLE("LCAO_Deepks", "cal_o_delta_k");

for(int ik=0; ik<nks; ik++)
this->o_delta.zero_out();
for (int ik = 0; ik < nks; ik++)
{
for (int hl=0; hl<1; hl++)
for (int hl = 0; hl < 1; ++hl)
{
std::complex<double> o_delta_k=std::complex<double>(0.0,0.0);
TK o_delta_tmp = TK(0.0);
for (int i = 0; i < PARAM.globalv.nlocal; ++i)
{
for (int j = 0; j < PARAM.globalv.nlocal; ++j)
{
const int mu = pv->global2local_row(j);
const int nu = pv->global2local_col(i);

if (mu >= 0 && nu >= 0)
{
int iic;
Expand All @@ -72,16 +38,40 @@ void LCAO_Deepks::cal_o_delta(const std::vector<std::vector<ModuleBase::ComplexM
{
iic = mu * pv->ncol + nu;
}
o_delta_k += dm_hl[hl][ik](nu, mu) * this->H_V_delta_k[ik][iic];
if constexpr (std::is_same<TK, double>::value)
{
for (int is = 0; is < PARAM.inp.nspin; ++is)
{
o_delta_tmp += dm_hl[hl][is](nu, mu) * this->H_V_delta[0][iic];
}
}
else
{
o_delta_tmp += dm_hl[hl][ik](nu, mu) * this->H_V_delta_k[ik][iic];
}
}
} //end j
} //end i
Parallel_Reduce::reduce_all(o_delta_k);
this->o_delta(ik,hl) = o_delta_k.real();
}// end hl
}// end nks

}
}
Parallel_Reduce::reduce_all(o_delta_tmp);
if constexpr (std::is_same<TK, double>::value)
{
this->o_delta(ik,hl) = o_delta_tmp;
}
else
{
this->o_delta(ik,hl) = o_delta_tmp.real();
}
}
}
return;
}

template void LCAO_Deepks::cal_o_delta<double, ModuleBase::matrix>(
const std::vector<std::vector<ModuleBase::matrix>>& dm_hl,
const int nks);

template void LCAO_Deepks::cal_o_delta<std::complex<double>, ModuleBase::ComplexMatrix>(
const std::vector<std::vector<ModuleBase::ComplexMatrix>>& dm_hl,
const int nks);

#endif
Loading
Loading