Skip to content

Commit

Permalink
Refactor: removal of GlobalC::UOT (#4322)
Browse files Browse the repository at this point in the history
* initial commit

* passing uot down to various operators

* eliminate get_const_instance

* remove UOT in output module

* remove UOT in force calculations

* finalize the removal of GlobalC::UOT
  • Loading branch information
jinzx10 authored Jun 7, 2024
1 parent 69db5c6 commit 51bdd59
Show file tree
Hide file tree
Showing 46 changed files with 219 additions and 132 deletions.
10 changes: 0 additions & 10 deletions source/module_basis/module_ao/ORB_gen_tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,10 @@
#include "module_base/math_integral.h"
#include "module_base/constants.h"

namespace GlobalC
{
///here is a member of ORB_gen_tables class
ORB_gen_tables UOT;
}

ORB_gen_tables::ORB_gen_tables() {}
ORB_gen_tables::~ORB_gen_tables() {}

const ORB_gen_tables& ORB_gen_tables::get_const_instance()
{
return GlobalC::UOT;
}

/// call in hamilt_linear::init_before_ions.
void ORB_gen_tables::gen_tables(
std::ofstream &ofs_in,
Expand Down
10 changes: 0 additions & 10 deletions source/module_basis/module_ao/ORB_gen_tables.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class ORB_gen_tables
ORB_gen_tables();
~ORB_gen_tables();

// static function to get global instance
static const ORB_gen_tables& get_const_instance();

void gen_tables(
std::ofstream &ofs_in, // mohan add 2021-05-07
LCAO_Orbitals &orb,
Expand Down Expand Up @@ -122,11 +119,4 @@ class ORB_gen_tables

};

/// PLEASE try to get rid of GlobalC::UOT, which is a global variable
/// mohan add 2021-03-30
namespace GlobalC
{
extern ORB_gen_tables UOT;
}

#endif
22 changes: 14 additions & 8 deletions source/module_esolver/esolver_ks_lcao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "module_hamilt_lcao/module_dftu/dftu.h"
#include "module_hamilt_pw/hamilt_pwdft/global.h"
#include "module_io/print_info.h"
#include <memory>
#ifdef __EXX
#include "module_ri/RPA_LRI.h"
#endif
Expand Down Expand Up @@ -81,8 +82,9 @@ template <typename TK, typename TR>
ESolver_KS_LCAO<TK, TR>::~ESolver_KS_LCAO()
{
#ifndef USE_NEW_TWO_CENTER
this->orb_con.clear_after_ions(GlobalC::UOT, GlobalC::ORB, GlobalV::deepks_setorb, GlobalC::ucell.infoNL.nproj);
this->orb_con.clear_after_ions(*uot_, GlobalC::ORB, GlobalV::deepks_setorb, GlobalC::ucell.infoNL.nproj);
#endif
delete uot_;
}


Expand Down Expand Up @@ -386,6 +388,7 @@ void ESolver_KS_LCAO<TK, TR>::cal_force(ModuleBase::matrix& force)
this->gen_h, // mohan add 2024-04-02
this->GG, // mohan add 2024-04-01
this->GK, // mohan add 2024-04-01
uot_,
force,
this->scs,
this->sf,
Expand Down Expand Up @@ -564,6 +567,14 @@ void ESolver_KS_LCAO<TK, TR>::init_basis_lcao(
// * reading the localized orbitals/projectors
// * construct the interpolation tables.


// NOTE: This following raw pointer serves as a temporary step in
// LCAO refactoring. Eventually, it will be replaced by a shared_ptr,
// which is the only owner of the ORB_gen_tables object. All other
// usages will take a weak_ptr.
uot_ = new ORB_gen_tables;
auto& two_center_bundle = uot_->two_center_bundle;

two_center_bundle.reset(new TwoCenterBundle);
two_center_bundle->build_orb(ucell.ntype, ucell.orbital_fn);
two_center_bundle->build_alpha(GlobalV::deepks_setorb, &ucell.descriptor_file);
Expand All @@ -586,7 +597,7 @@ void ESolver_KS_LCAO<TK, TR>::init_basis_lcao(

#ifndef USE_NEW_TWO_CENTER
this->orb_con.set_orb_tables(GlobalV::ofs_running,
GlobalC::UOT,
*uot_,
GlobalC::ORB,
ucell.lat0,
GlobalV::deepks_setorb,
Expand All @@ -596,12 +607,6 @@ void ESolver_KS_LCAO<TK, TR>::init_basis_lcao(
ucell.infoNL.Beta);
#else
two_center_bundle->tabulate();

// transfer the ownership to UOT
// this is a temporary solution during refactoring
// the final version will get rid of UOT
// and transfer individual ownership of TwoCenterIntegrator to corresponding operator
GlobalC::UOT.two_center_bundle = std::move(two_center_bundle);
#endif

if (this->orb_con.setup_2d)
Expand Down Expand Up @@ -1442,6 +1447,7 @@ ModuleIO::Output_Mat_Sparse<TK> ESolver_KS_LCAO<TK, TR>::create_Output_Mat_Spars
this->orb_con.ParaV,
this->gen_h, // mohan add 2024-04-06
this->GK, // mohan add 2024-04-01
uot_,
this->LM,
GlobalC::GridD, // mohan add 2024-04-06
this->kv,
Expand Down
10 changes: 9 additions & 1 deletion source/module_esolver/esolver_ks_lcao.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ namespace ModuleESolver

Grid_Technique GridT;

std::unique_ptr<TwoCenterBundle> two_center_bundle;
// The following variable is introduced in the stage-1 of LCAO
// refactoring. It is supposed to replace the previous GlobalC::UOT.
//
// This is the only place supposed to have the ownership; all other
// places should be considered as "borrowing" the object. Unfortunately,
// imposing shared_ptr/weak_ptr is only possible once GlobalC::UOT is
// completely removed from the code; we have to rely on raw pointers
// during the transition period.
ORB_gen_tables* uot_;

// Temporarily store the stress to unify the interface with PW,
// because it's hard to seperate force and stress calculation in LCAO.
Expand Down
9 changes: 5 additions & 4 deletions source/module_esolver/esolver_ks_lcao_elec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ void ESolver_KS_LCAO<TK, TR>::beforesolver(const int istep)
&(this->LOC),
this->pelec->pot,
this->kv,
uot_,
#ifdef __EXX
DM,
GlobalC::exx_info.info_ri.real_number ? &this->exd->two_level_step : &this->exc->two_level_step);
Expand All @@ -167,11 +168,11 @@ void ESolver_KS_LCAO<TK, TR>::beforesolver(const int istep)
{
const Parallel_Orbitals* pv = this->LM.ParaV;
// build and save <psi(0)|alpha(R)> at beginning
GlobalC::ld.build_psialpha(GlobalV::CAL_FORCE, GlobalC::ucell, GlobalC::ORB, GlobalC::GridD, GlobalC::UOT);
GlobalC::ld.build_psialpha(GlobalV::CAL_FORCE, GlobalC::ucell, GlobalC::ORB, GlobalC::GridD, *uot_);

if (GlobalV::deepks_out_unittest)
{
GlobalC::ld.check_psialpha(GlobalV::CAL_FORCE, GlobalC::ucell, GlobalC::ORB, GlobalC::GridD, GlobalC::UOT);
GlobalC::ld.check_psialpha(GlobalV::CAL_FORCE, GlobalC::ucell, GlobalC::ORB, GlobalC::GridD, *uot_);
}
}
#endif
Expand Down Expand Up @@ -486,7 +487,7 @@ void ESolver_KS_LCAO<std::complex<double>, double>::get_S(void)

if (this->p_hamilt == nullptr)
{
this->p_hamilt = new hamilt::HamiltLCAO<std::complex<double>, double>(&this->LM, this->kv);
this->p_hamilt = new hamilt::HamiltLCAO<std::complex<double>, double>(&this->LM, this->kv, uot_);
dynamic_cast<hamilt::OperatorLCAO<std::complex<double>, double>*>(this->p_hamilt->ops)->contributeHR();
}

Expand Down Expand Up @@ -517,7 +518,7 @@ void ESolver_KS_LCAO<std::complex<double>, std::complex<double>>::get_S(void)
this->LM.ParaV = &this->orb_con.ParaV;
if (this->p_hamilt == nullptr)
{
this->p_hamilt = new hamilt::HamiltLCAO<std::complex<double>, std::complex<double>>(&this->LM, this->kv);
this->p_hamilt = new hamilt::HamiltLCAO<std::complex<double>, std::complex<double>>(&this->LM, this->kv, uot_);
dynamic_cast<hamilt::OperatorLCAO<std::complex<double>, std::complex<double>>*>(this->p_hamilt->ops)
->contributeHR();
}
Expand Down
1 change: 1 addition & 0 deletions source/module_esolver/esolver_ks_lcao_tddft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ void ESolver_KS_LCAO_TDDFT::after_scf(const int istep)
this->psi,
pelec,
kv,
uot_,
tmp_DM->get_paraV_pointer(),
this->RA,
this->LM, // mohan add 2024-04-02
Expand Down
2 changes: 2 additions & 0 deletions source/module_hamilt_lcao/hamilt_lcaodft/FORCE.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class Force_LCAO
#endif
LCAO_gen_fixedH& gen_h, // mohan add 2024-04-02
typename TGint<T>::type& gint,
const ORB_gen_tables* uot,
const Parallel_Orbitals& pv,
LCAO_Matrix& lm,
const K_Vectors* kv = nullptr,
Expand All @@ -71,6 +72,7 @@ class Force_LCAO
void allocate(const Parallel_Orbitals& pv,
LCAO_Matrix& lm,
LCAO_gen_fixedH& gen_h,
const ORB_gen_tables* uot,
const int& nks = 0,
const std::vector<ModuleBase::Vector3<double>>& kvec_d = {});

Expand Down
7 changes: 7 additions & 0 deletions source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void Force_Stress_LCAO<T>::getForceStress(const bool isforce,
LCAO_gen_fixedH &gen_h, // mohan add 2024-04-02
Gint_Gamma &gint_gamma, // mohan add 2024-04-01
Gint_k &gint_k, // mohan add 2024-04-01
const ORB_gen_tables* uot,
ModuleBase::matrix& fcs,
ModuleBase::matrix& scs,
const Structure_Factor& sf,
Expand Down Expand Up @@ -160,6 +161,7 @@ void Force_Stress_LCAO<T>::getForceStress(const bool isforce,
gen_h, // mohan add 2024-04-02
gint_gamma,
gint_k,
uot,
pv,
lm,
kv);
Expand Down Expand Up @@ -245,6 +247,7 @@ void Force_Stress_LCAO<T>::getForceStress(const bool isforce,
nullptr,
GlobalC::ucell,
&GlobalC::GridD,
uot,
&GlobalC::dftu,
*(lm.ParaV));
tmp_dftu.cal_force_stress(isforce, isstress, force_dftu, stress_dftu);
Expand Down Expand Up @@ -747,6 +750,7 @@ void Force_Stress_LCAO<double>::integral_part(
LCAO_gen_fixedH &gen_h, // mohan add 2024-04-02
Gint_Gamma &gint_gamma, // mohan add 2024-04-01
Gint_k &gint_k, // mohan add 2024-04-01
const ORB_gen_tables* uot,
const Parallel_Orbitals& pv,
LCAO_Matrix &lm,
const K_Vectors& kv)
Expand All @@ -769,6 +773,7 @@ void Force_Stress_LCAO<double>::integral_part(
#endif
gen_h,
gint_gamma,
uot,
pv,
lm);
return;
Expand Down Expand Up @@ -797,6 +802,7 @@ void Force_Stress_LCAO<std::complex<double>>::integral_part(
LCAO_gen_fixedH &gen_h, // mohan add 2024-04-02
Gint_Gamma &gint_gamma,
Gint_k &gint_k,
const ORB_gen_tables* uot,
const Parallel_Orbitals& pv,
LCAO_Matrix &lm,
const K_Vectors& kv)
Expand All @@ -818,6 +824,7 @@ void Force_Stress_LCAO<std::complex<double>>::integral_part(
#endif
gen_h,
gint_k,
uot,
pv,
lm,
& kv,
Expand Down
2 changes: 2 additions & 0 deletions source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Force_Stress_LCAO
LCAO_gen_fixedH &gen_h, // mohan add 2024-04-02
Gint_Gamma &gint_gamma, // mohan add 2024-04-01
Gint_k &gint_k, // mohan add 2024-04-01
const ORB_gen_tables* uot,
ModuleBase::matrix& fcs,
ModuleBase::matrix& scs,
const Structure_Factor& sf,
Expand Down Expand Up @@ -92,6 +93,7 @@ class Force_Stress_LCAO
LCAO_gen_fixedH &gen_h, // mohan add 2024-04-02
Gint_Gamma &gint_gamma,
Gint_k &gint_k,
const ORB_gen_tables* uot,
const Parallel_Orbitals &pv,
LCAO_Matrix &lm,
const K_Vectors& kv);
Expand Down
14 changes: 8 additions & 6 deletions source/module_hamilt_lcao/hamilt_lcaodft/FORCE_gamma.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ template<>
void Force_LCAO<double>::allocate(const Parallel_Orbitals& pv,
LCAO_Matrix& lm,
LCAO_gen_fixedH& gen_h,
const ORB_gen_tables* uot,
const int& nks,
const std::vector<ModuleBase::Vector3<double>>& kvec_d)
{
Expand Down Expand Up @@ -67,7 +68,7 @@ void Force_LCAO<double>::allocate(const Parallel_Orbitals& pv,
}
// calculate dS in LCAO basis
// ModuleBase::timer::tick("Force_LCAO_gamma","build_S_new");
gen_h.build_ST_new('S', cal_deri, GlobalC::ucell, GlobalC::ORB, GlobalC::UOT, &(GlobalC::GridD), lm.Sloc.data());
gen_h.build_ST_new('S', cal_deri, GlobalC::ucell, GlobalC::ORB, *uot, &(GlobalC::GridD), lm.Sloc.data());
// ModuleBase::timer::tick("Force_LCAO_gamma","build_S_new");

// calculate dT in LCAP
Expand All @@ -84,12 +85,12 @@ void Force_LCAO<double>::allocate(const Parallel_Orbitals& pv,
// calculate dT
// calculate T + VNL(P1) in LCAO basis
// ModuleBase::timer::tick("Force_LCAO_gamma","build_T_new");
gen_h.build_ST_new('T', cal_deri, GlobalC::ucell, GlobalC::ORB, GlobalC::UOT, &(GlobalC::GridD), lm.Hloc_fixed.data());
gen_h.build_ST_new('T', cal_deri, GlobalC::ucell, GlobalC::ORB, *uot, &(GlobalC::GridD), lm.Hloc_fixed.data());
// ModuleBase::timer::tick("Force_LCAO_gamma","build_T_new");
// test_gamma(lm.DHloc_fixed_x, "dHloc_fixed_x T part");

// ModuleBase::timer::tick("Force_LCAO_gamma","build_Nonlocal_mu");
gen_h.build_Nonlocal_mu_new(lm.Hloc_fixed.data(), cal_deri, GlobalC::ucell, GlobalC::ORB, GlobalC::UOT, &(GlobalC::GridD));
gen_h.build_Nonlocal_mu_new(lm.Hloc_fixed.data(), cal_deri, GlobalC::ucell, GlobalC::ORB, *uot, &(GlobalC::GridD));
// ModuleBase::timer::tick("Force_LCAO_gamma","build_Nonlocal_mu");
// test_gamma(lm.DHloc_fixed_x, "dHloc_fixed_x Vnl part");

Expand All @@ -100,7 +101,7 @@ void Force_LCAO<double>::allocate(const Parallel_Orbitals& pv,

lm.zeros_HSgamma('S');

gen_h.build_ST_new('S', cal_deri, GlobalC::ucell, GlobalC::ORB, GlobalC::UOT, &(GlobalC::GridD), lm.Sloc.data(), INPUT.cal_syns, INPUT.dmax);
gen_h.build_ST_new('S', cal_deri, GlobalC::ucell, GlobalC::ORB, *uot, &(GlobalC::GridD), lm.Sloc.data(), INPUT.cal_syns, INPUT.dmax);

bool bit = false; // LiuXh, 2017-03-21

Expand Down Expand Up @@ -202,6 +203,7 @@ void Force_LCAO<double>::ftable(const bool isforce,
#endif
LCAO_gen_fixedH& gen_h, // mohan add 2024-04-02
TGint<double>::type& gint,
const ORB_gen_tables* uot,
const Parallel_Orbitals& pv,
LCAO_Matrix& lm,
const K_Vectors* kv,
Expand All @@ -219,7 +221,7 @@ void Force_LCAO<double>::ftable(const bool isforce,

// allocate DSloc_x, DSloc_y, DSloc_z
// allocate DHloc_fixed_x, DHloc_fixed_y, DHloc_fixed_z
this->allocate(pv, lm, gen_h);
this->allocate(pv, lm, gen_h, uot);

// calculate the 'energy density matrix' here.
this->cal_foverlap(isforce, isstress, psi, pv, pelec, lm, foverlap, soverlap);
Expand All @@ -229,7 +231,7 @@ void Force_LCAO<double>::ftable(const bool isforce,

this->cal_ftvnl_dphi(DM, pv, GlobalC::ucell, lm, isforce, isstress, ftvnl_dphi, stvnl_dphi);

this->cal_fvnl_dbeta(DM, pv, GlobalC::ucell, GlobalC::ORB, GlobalC::UOT, GlobalC::GridD, isforce, isstress, fvnl_dbeta, svnl_dbeta);
this->cal_fvnl_dbeta(DM, pv, GlobalC::ucell, GlobalC::ORB, *uot, GlobalC::GridD, isforce, isstress, fvnl_dbeta, svnl_dbeta);

this->cal_fvl_dphi(isforce, isstress, pelec->pot, gint, fvl_dphi, svl_dphi);

Expand Down
13 changes: 8 additions & 5 deletions source/module_hamilt_lcao/hamilt_lcaodft/FORCE_k.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ template<>
void Force_LCAO<std::complex<double>>::allocate(const Parallel_Orbitals& pv,
LCAO_Matrix& lm,
LCAO_gen_fixedH& gen_h,
const ORB_gen_tables* uot,
const int& nks,
const std::vector<ModuleBase::Vector3<double>>& kvec_d)
{
Expand Down Expand Up @@ -88,7 +89,7 @@ void Force_LCAO<std::complex<double>>::allocate(const Parallel_Orbitals& pv,
// calculate dS = <phi | dphi>
//-----------------------------
bool cal_deri = true;
gen_h.build_ST_new('S', cal_deri, GlobalC::ucell, GlobalC::ORB, GlobalC::UOT, &(GlobalC::GridD), gen_h.LM->SlocR.data());
gen_h.build_ST_new('S', cal_deri, GlobalC::ucell, GlobalC::ORB, *uot, &(GlobalC::GridD), gen_h.LM->SlocR.data());

//-----------------------------------------
// (2) allocate for <phi | T + Vnl | dphi>
Expand All @@ -111,10 +112,10 @@ void Force_LCAO<std::complex<double>>::allocate(const Parallel_Orbitals& pv,

// calculate dT=<phi|kin|dphi> in LCAO
// calculate T + VNL(P1) in LCAO basis
gen_h.build_ST_new('T', cal_deri, GlobalC::ucell, GlobalC::ORB, GlobalC::UOT, &(GlobalC::GridD), gen_h.LM->Hloc_fixedR.data());
gen_h.build_ST_new('T', cal_deri, GlobalC::ucell, GlobalC::ORB, *uot, &(GlobalC::GridD), gen_h.LM->Hloc_fixedR.data());

// calculate dVnl=<phi|dVnl|dphi> in LCAO
gen_h.build_Nonlocal_mu_new(gen_h.LM->Hloc_fixed.data(), cal_deri, GlobalC::ucell, GlobalC::ORB, GlobalC::UOT, &(GlobalC::GridD));
gen_h.build_Nonlocal_mu_new(gen_h.LM->Hloc_fixed.data(), cal_deri, GlobalC::ucell, GlobalC::ORB, *uot, &(GlobalC::GridD));

// calculate asynchronous S matrix to output for Hefei-NAMD
if (INPUT.cal_syns)
Expand All @@ -126,7 +127,7 @@ void Force_LCAO<std::complex<double>>::allocate(const Parallel_Orbitals& pv,
cal_deri,
GlobalC::ucell,
GlobalC::ORB,
GlobalC::UOT,
*uot,
&(GlobalC::GridD),
lm.SlocR.data(),
INPUT.cal_syns,
Expand Down Expand Up @@ -289,6 +290,7 @@ void Force_LCAO<std::complex<double>>::test(
#endif
LCAO_gen_fixedH& gen_h,
TGint<std::complex<double>>::type& gint,
const ORB_gen_tables* uot,
const Parallel_Orbitals& pv,
LCAO_Matrix& lm,
const K_Vectors* kv,
Expand All @@ -304,6 +306,7 @@ void Force_LCAO<std::complex<double>>::test(
pv,
lm,
gen_h,
uot,
kv->get_nks(),
kv->kvec_d);

Expand Down Expand Up @@ -347,7 +350,7 @@ void Force_LCAO<std::complex<double>>::test(
pv,
GlobalC::ucell,
GlobalC::ORB,
GlobalC::UOT,
*uot,
GlobalC::GridD,
isforce,
isstress,
Expand Down
Loading

0 comments on commit 51bdd59

Please sign in to comment.