Skip to content

Commit

Permalink
Improve the C++ code quality (#3945)
Browse files Browse the repository at this point in the history
* update timer, add std

* update the format of esolver_fp.cpp

* add description in esolver_fp.h

* update a few esolver files

* update esolver description for LCAO

* update some formats of esolver codes

* update esolver_ks_pw.cpp formats

* update formats of esolver_ks_pw.cpp and esolver_ks_pw.h

* update esolver_ks_lcao_tddft.cpp formats

* update format of esolver_sdft_pw.cpp

* update the format of esolver_sdft_pw_tool.cpp

* keep formating esolver_ks_pw.cpp

* formating esolver_of_interface.cpp

* change GlobalC::ucell to ucell in esolver_ks.cpp

* remove some GlobalC::ucell in esolver_sdft_pw.cpp

* refactor the code before getting rid of RA in esolver_lcao

* refactor before getting rid of LOWF

* refactor before getting rid of LCAO_hamilt.h and LCAO_matrix.h

* refactor wavefunc_in_pw

* refactor density matrix

* refactor the format cal_dm_psi.cpp

* format forces.cpp

* refactor esolver_of_tool.cpp

* change member function beforescf in Esolver to before_scf

* change afterscf to after_scf

* change updatepot to update_pot

* change eachiterinit to iter_init, change eachiterfinish to iter_finish

* refactor esolvers, change member function names of most esolvers

* reformat esolver.h

* update tests for esolvers

* add TITLE in esolver_ks_lcao

* update esolver_ks_lcao

* update esolver_lcao

* update timer::tick in esolver_lcao

* try to delete LCAO_Matrix in LCAO_Hamilt, and try to delete Parallel_Orbitals in Force_k

* fix the compiling issue with LCAO_hamilt.hpp

* try to divide the FORCE_k.cpp into several small files

* divide FORCE_k into foverlap_k.cpp ftvnl_dphi_k.cpp fvl_dphi_k.cpp fvnl_dbeta_k.cpp four files

* get rid of UHM in FORCE_k.cpp

* cannot compile, but I have modified some files in order to get rid of Gint_k and Gint_Gamma in UHM

* keep updating, cannot run

* update write_Vxc, cannot run yet

* keep updating gint_gamma and gint_k

* update LCAO_matrix.cpp

* divide force files, and update Makefile.Objects

* update LCAO_hamilt.cpp

* delete genH pointer in UHM

* divide LCAO_hamilt.cpp into small codes, grid_init.cpp is the first one

* update grid_init in esolver_ks_lcao

* add a new namespace named sparse_format, most of the functions that originally belong to LCAO_hamilt should be moved to sparse_format

* cannot find the mismatch of DFTU

* fix the DFTU error

* update

* enable the test_memory function again by setting calcalculation parameter in INPUT file

* update memory record functions

* add sparse_format_u

* keep refactoring LCAO_hamilt

* rename sparse format files, which are originally defined in LCAO_hamilt.h

* add spar_u.h and spar_u.cpp

* update sparse matrix

* add spar_exx

* update LCAO_hamilt.cpp

* tear down LCAO_hamilt.h and .cpp, DONE.

* keep updating spar

* fix the bugs after deleting LCAO_hamilt

* continue

* fix some errors when compiling

* fix compiling errors in DOS

* fix errors without uhm

* remove directly use of LM in spar_hsr.cpp

* update spa_ files

* keep cleaning the mass left by UHM

* finnaly, all files can be compiled without uhm

* previous commit has problems, now it has been fixed.

* fix makefile bugs

* fix exx compiling errors

* fix exx problems

* update exx

* fix exx

* in some sense, the exx code is a disaster

* let's fix exx again

* fix undefination

* fix a bug in LCAO Refactor Step 4, I accidently forgot to add & for Grid_Driver& _grid in output_mat_sparse.h

* refactor a little bit about MD files

* update some formats in md module

* refactor gint.cpp

* refactor titles of gint.cpp

* refactor gint.cpp

* fix some defects in gint

* update gint

* refactor gint

* refactor gint

* small updates of gint

* small updates of psi

---------

Co-authored-by: maki49 <1579492865@qq.com>
  • Loading branch information
mohanchen and maki49 committed Apr 13, 2024
1 parent 00fcb53 commit 93b3276
Show file tree
Hide file tree
Showing 29 changed files with 780 additions and 315 deletions.
221 changes: 145 additions & 76 deletions source/module_hamilt_lcao/module_gint/gint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,65 @@

Gint::~Gint()
{
delete this->hRGint;
delete this->hRGintCd;
// mohan add 2024-04-09
if(this->hRGint != nullptr)
{
delete this->hRGint;
}

// mohan add 2024-04-09
if(this->hRGint != nullptr)
{
delete this->hRGintCd;
}

for(int is=0;is<this->DMRGint.size();is++)
{
delete this->DMRGint[is];
}
#ifdef __MPI
if(this->DMRGint_full != nullptr) delete this->DMRGint_full;
if(this->DMRGint_full != nullptr)
{
delete this->DMRGint_full;
}
#endif

}

void Gint::cal_gint(Gint_inout *inout)
{

ModuleBase::timer::tick("Gint_interface", "cal_gint");

if(inout->job==Gint_Tools::job_type::vlocal) ModuleBase::TITLE("Gint_interface","cal_gint_vlocal");
if(inout->job==Gint_Tools::job_type::vlocal_meta) ModuleBase::TITLE("Gint_interface","cal_gint_vlocal_meta");
if(inout->job==Gint_Tools::job_type::rho) ModuleBase::TITLE("Gint_interface","cal_gint_rho");
if(inout->job==Gint_Tools::job_type::tau) ModuleBase::TITLE("Gint_interface","cal_gint_tau");
if(inout->job==Gint_Tools::job_type::force) ModuleBase::TITLE("Gint_interface","cal_gint_force");
if(inout->job==Gint_Tools::job_type::force_meta) ModuleBase::TITLE("Gint_interface","cal_gint_force_meta");

if(inout->job==Gint_Tools::job_type::vlocal) ModuleBase::timer::tick("Gint_interface", "cal_gint_vlocal");
if(inout->job==Gint_Tools::job_type::vlocal_meta) ModuleBase::timer::tick("Gint_interface","cal_gint_vlocal_meta");
if(inout->job==Gint_Tools::job_type::rho) ModuleBase::timer::tick("Gint_interface","cal_gint_rho");
if(inout->job==Gint_Tools::job_type::tau) ModuleBase::timer::tick("Gint_interface","cal_gint_tau");
if(inout->job==Gint_Tools::job_type::force) ModuleBase::timer::tick("Gint_interface","cal_gint_force");
if(inout->job==Gint_Tools::job_type::force_meta) ModuleBase::timer::tick("Gint_interface","cal_gint_force_meta");
if(inout->job==Gint_Tools::job_type::vlocal)
{
ModuleBase::TITLE("Gint_interface","cal_gint_vlocal");
ModuleBase::timer::tick("Gint_interface", "cal_gint_vlocal");
}
else if(inout->job==Gint_Tools::job_type::vlocal_meta)
{
ModuleBase::TITLE("Gint_interface","cal_gint_vlocal_meta");
ModuleBase::timer::tick("Gint_interface","cal_gint_vlocal_meta");
}
else if(inout->job==Gint_Tools::job_type::rho)
{
ModuleBase::TITLE("Gint_interface","cal_gint_rho");
ModuleBase::timer::tick("Gint_interface","cal_gint_rho");
}
else if(inout->job==Gint_Tools::job_type::tau)
{
ModuleBase::TITLE("Gint_interface","cal_gint_tau");
ModuleBase::timer::tick("Gint_interface","cal_gint_tau");
}
else if(inout->job==Gint_Tools::job_type::force)
{
ModuleBase::TITLE("Gint_interface","cal_gint_force");
ModuleBase::timer::tick("Gint_interface","cal_gint_force");
}
if(inout->job==Gint_Tools::job_type::force_meta)
{
ModuleBase::TITLE("Gint_interface","cal_gint_force_meta");
ModuleBase::timer::tick("Gint_interface","cal_gint_force_meta");
}

const int max_size = this->gridt->max_atom;
const int LD_pool = max_size*GlobalC::ucell.nwmax;
Expand All @@ -70,7 +99,9 @@ void Gint::cal_gint(Gint_inout *inout)
// it's a uniform grid to save orbital values, so the delta_r is a constant.
const double delta_r = GlobalC::ORB.dr_uniform;

if((inout->job==Gint_Tools::job_type::vlocal || inout->job==Gint_Tools::job_type::vlocal_meta) && !GlobalV::GAMMA_ONLY_LOCAL)
if((inout->job==Gint_Tools::job_type::vlocal
|| inout->job==Gint_Tools::job_type::vlocal_meta)
&& !GlobalV::GAMMA_ONLY_LOCAL)
{
if(!pvpR_alloc_flag)
{
Expand All @@ -95,13 +126,15 @@ void Gint::cal_gint(Gint_inout *inout)

//perpare auxiliary arrays to store thread-specific values
#ifdef _OPENMP
double* pvpR_thread;
double* pvpR_thread = nullptr;
hamilt::HContainer<double>* hRGint_thread = nullptr;// auxiliary pointer for multi-threading
if(inout->job==Gint_Tools::job_type::vlocal || inout->job==Gint_Tools::job_type::vlocal_meta)

if(inout->job==Gint_Tools::job_type::vlocal
|| inout->job==Gint_Tools::job_type::vlocal_meta)
{
if(!GlobalV::GAMMA_ONLY_LOCAL)
{
pvpR_thread = new double[nnrg];
pvpR_thread = new double[nnrg]();
ModuleBase::GlobalFunc::ZEROS(pvpR_thread, nnrg);
}
if(GlobalV::GAMMA_ONLY_LOCAL && lgd>0)
Expand All @@ -110,7 +143,9 @@ void Gint::cal_gint(Gint_inout *inout)
}
}

double *pvdpRx_thread, *pvdpRy_thread, *pvdpRz_thread;
double *pvdpRx_thread = nullptr;
double *pvdpRy_thread = nullptr;
double *pvdpRz_thread = nullptr;
if(inout->job==Gint_Tools::job_type::dvlocal)
{
pvdpRx_thread = new double[nnrg];
Expand All @@ -123,7 +158,8 @@ void Gint::cal_gint(Gint_inout *inout)

ModuleBase::matrix fvl_dphi_thread;
ModuleBase::matrix svl_dphi_thread;
if(inout->job==Gint_Tools::job_type::force || inout->job==Gint_Tools::job_type::force_meta)
if(inout->job==Gint_Tools::job_type::force
|| inout->job==Gint_Tools::job_type::force_meta)
{
if(inout->isforce)
{
Expand All @@ -145,7 +181,10 @@ void Gint::cal_gint(Gint_inout *inout)
// get the value: how many atoms has orbital value on this grid.
const int na_grid = this->gridt->how_many_atoms[ grid_index ];

if(na_grid==0) continue;
if(na_grid==0)
{
continue;
}

if(inout->job == Gint_Tools::job_type::rho)
{
Expand All @@ -166,100 +205,120 @@ void Gint::cal_gint(Gint_inout *inout)
{
double* vldr3 = Gint_Tools::get_vldr3(inout->vl, this->bxyz, this->bx, this->by, this->bz,
this->nplane, this->gridt->start_ind[grid_index], ncyz, dv);

double** DM_in;
if(GlobalV::GAMMA_ONLY_LOCAL) DM_in = inout->DM[GlobalV::CURRENT_SPIN];
if(!GlobalV::GAMMA_ONLY_LOCAL) DM_in = inout->DM_R;
#ifdef _OPENMP

if(GlobalV::GAMMA_ONLY_LOCAL)
{
DM_in = inout->DM[GlobalV::CURRENT_SPIN];
}
else if(!GlobalV::GAMMA_ONLY_LOCAL)
{
DM_in = inout->DM_R;
}

#ifdef _OPENMP
this->gint_kernel_force(na_grid, grid_index, delta_r, vldr3, LD_pool,
DM_in, inout->ispin, inout->isforce, inout->isstress,
&fvl_dphi_thread, &svl_dphi_thread);
#else
#else
this->gint_kernel_force(na_grid, grid_index, delta_r, vldr3, LD_pool,
DM_in, inout->ispin, inout->isforce, inout->isstress,
inout->fvl_dphi, inout->svl_dphi);
#endif
#endif
delete[] vldr3;
}
else if(inout->job==Gint_Tools::job_type::vlocal)
{
double* vldr3 = Gint_Tools::get_vldr3(inout->vl, this->bxyz, this->bx, this->by, this->bz,
this->nplane, this->gridt->start_ind[grid_index], ncyz, dv);
#ifdef _OPENMP
if((GlobalV::GAMMA_ONLY_LOCAL && lgd>0) || !GlobalV::GAMMA_ONLY_LOCAL)
{
this->gint_kernel_vlocal(na_grid, grid_index, delta_r, vldr3, LD_pool,
if((GlobalV::GAMMA_ONLY_LOCAL && lgd>0) || !GlobalV::GAMMA_ONLY_LOCAL)
{
this->gint_kernel_vlocal(na_grid, grid_index, delta_r, vldr3, LD_pool,
pvpR_thread, hRGint_thread);
}
#else
if(GlobalV::GAMMA_ONLY_LOCAL && lgd>0)
{
this->gint_kernel_vlocal(na_grid, grid_index, delta_r, vldr3, LD_pool, nullptr);
}
if(!GlobalV::GAMMA_ONLY_LOCAL)
{
this->gint_kernel_vlocal(na_grid, grid_index, delta_r, vldr3, LD_pool,
}
#else
if(GlobalV::GAMMA_ONLY_LOCAL && lgd>0)
{
this->gint_kernel_vlocal(na_grid, grid_index, delta_r, vldr3, LD_pool, nullptr);
}
if(!GlobalV::GAMMA_ONLY_LOCAL)
{
this->gint_kernel_vlocal(na_grid, grid_index, delta_r, vldr3, LD_pool,
this->pvpR_reduced[inout->ispin]);
}
#endif
}
#endif
delete[] vldr3;
}
else if(inout->job==Gint_Tools::job_type::dvlocal)
{
double* vldr3 = Gint_Tools::get_vldr3(inout->vl, this->bxyz, this->bx, this->by, this->bz,
this->nplane, this->gridt->start_ind[grid_index], ncyz, dv);
this->nplane, this->gridt->start_ind[grid_index], ncyz, dv);
#ifdef _OPENMP
this->gint_kernel_dvlocal(na_grid, grid_index, delta_r, vldr3, LD_pool,
this->gint_kernel_dvlocal(na_grid, grid_index, delta_r, vldr3, LD_pool,
pvdpRx_thread, pvdpRy_thread, pvdpRz_thread);
#else
this->gint_kernel_dvlocal(na_grid, grid_index, delta_r, vldr3, LD_pool,
this->pvdpRx_reduced[inout->ispin], this->pvdpRy_reduced[inout->ispin], this->pvdpRz_reduced[inout->ispin]);
#endif
#else
this->gint_kernel_dvlocal(na_grid, grid_index, delta_r, vldr3, LD_pool,
this->pvdpRx_reduced[inout->ispin],
this->pvdpRy_reduced[inout->ispin],
this->pvdpRz_reduced[inout->ispin]);
#endif
delete[] vldr3;
}
else if(inout->job==Gint_Tools::job_type::vlocal_meta)
{
double* vldr3 = Gint_Tools::get_vldr3(inout->vl, this->bxyz, this->bx, this->by, this->bz,
this->nplane, this->gridt->start_ind[grid_index], ncyz, dv);
double* vkdr3 = Gint_Tools::get_vldr3(inout->vofk,this->bxyz, this->bx, this->by, this->bz,
this->nplane, this->gridt->start_ind[grid_index], ncyz, dv);
this->nplane, this->gridt->start_ind[grid_index], ncyz, dv);
#ifdef _OPENMP
if((GlobalV::GAMMA_ONLY_LOCAL && lgd>0) || !GlobalV::GAMMA_ONLY_LOCAL)
{
this->gint_kernel_vlocal_meta(na_grid, grid_index, delta_r, vldr3, vkdr3, LD_pool,
if((GlobalV::GAMMA_ONLY_LOCAL && lgd>0) || !GlobalV::GAMMA_ONLY_LOCAL)
{
this->gint_kernel_vlocal_meta(na_grid, grid_index, delta_r, vldr3, vkdr3, LD_pool,
pvpR_thread, hRGint_thread);
}
#else
if(GlobalV::GAMMA_ONLY_LOCAL && lgd>0)
{
this->gint_kernel_vlocal_meta(na_grid, grid_index, delta_r, vldr3, vkdr3, LD_pool, nullptr);
}
if(!GlobalV::GAMMA_ONLY_LOCAL)
{
this->gint_kernel_vlocal_meta(na_grid, grid_index, delta_r, vldr3, vkdr3, LD_pool,
}
#else
if(GlobalV::GAMMA_ONLY_LOCAL && lgd>0)
{
this->gint_kernel_vlocal_meta(na_grid, grid_index, delta_r, vldr3, vkdr3, LD_pool, nullptr);
}
if(!GlobalV::GAMMA_ONLY_LOCAL)
{
this->gint_kernel_vlocal_meta(na_grid, grid_index, delta_r, vldr3, vkdr3, LD_pool,
this->pvpR_reduced[inout->ispin]);
}
#endif
}
#endif
delete[] vldr3;
delete[] vkdr3;
}
else if(inout->job == Gint_Tools::job_type::force_meta)
{
double* vldr3 = Gint_Tools::get_vldr3(inout->vl, this->bxyz, this->bx, this->by, this->bz,
this->nplane, this->gridt->start_ind[grid_index], ncyz, dv);

double* vkdr3 = Gint_Tools::get_vldr3(inout->vofk, this->bxyz, this->bx, this->by, this->bz,
this->nplane, this->gridt->start_ind[grid_index], ncyz, dv);
double** DM_in;
if(GlobalV::GAMMA_ONLY_LOCAL) DM_in = inout->DM[GlobalV::CURRENT_SPIN];
if(!GlobalV::GAMMA_ONLY_LOCAL) DM_in = inout->DM_R;
#ifdef _OPENMP
this->gint_kernel_force_meta(na_grid, grid_index, delta_r, vldr3, vkdr3, LD_pool,

double** DM_in;

if(GlobalV::GAMMA_ONLY_LOCAL)
{
DM_in = inout->DM[GlobalV::CURRENT_SPIN];
}
else if(!GlobalV::GAMMA_ONLY_LOCAL)
{
DM_in = inout->DM_R;
}
#ifdef _OPENMP
this->gint_kernel_force_meta(na_grid, grid_index, delta_r, vldr3, vkdr3, LD_pool,
DM_in, inout->ispin, inout->isforce, inout->isstress,
&fvl_dphi_thread, &svl_dphi_thread);
#else
this->gint_kernel_force_meta(na_grid, grid_index, delta_r, vldr3, vkdr3, LD_pool,
#else
this->gint_kernel_force_meta(na_grid, grid_index, delta_r, vldr3, vkdr3, LD_pool,
DM_in, inout->ispin, inout->isforce, inout->isstress,
inout->fvl_dphi, inout->svl_dphi);
#endif
#endif
delete[] vldr3;
delete[] vkdr3;
}
Expand Down Expand Up @@ -314,6 +373,7 @@ void Gint::cal_gint(Gint_inout *inout)
if(inout->job==Gint_Tools::job_type::tau) ModuleBase::timer::tick("Gint_interface","cal_gint_tau");
if(inout->job==Gint_Tools::job_type::force) ModuleBase::timer::tick("Gint_interface","cal_gint_force");
if(inout->job==Gint_Tools::job_type::force_meta) ModuleBase::timer::tick("Gint_interface","cal_gint_force_meta");

return;
}

Expand Down Expand Up @@ -381,13 +441,19 @@ void Gint::initialize_pvpR(
}
if(GlobalV::NSPIN!=4)
{
if(this->hRGint != nullptr) delete this->hRGint;
if(this->hRGint != nullptr)
{
delete this->hRGint;
}
this->hRGint = new hamilt::HContainer<double>(ucell_in.nat);
}
else
{
npol = 2;
if(this->hRGintCd != nullptr) delete this->hRGintCd;
if(this->hRGintCd != nullptr)
{
delete this->hRGintCd;
}
this->hRGintCd = new hamilt::HContainer<std::complex<double>>(ucell_in.nat);
for (int is = 0; is < GlobalV::NSPIN; is++)
{
Expand All @@ -398,7 +464,10 @@ void Gint::initialize_pvpR(
this->DMRGint[is] = new hamilt::HContainer<double>(ucell_in.nat);
}
#ifdef __MPI
if(this->DMRGint_full != nullptr) delete this->DMRGint_full;
if(this->DMRGint_full != nullptr)
{
delete this->DMRGint_full;
}
this->DMRGint_full = new hamilt::HContainer<double>(ucell_in.nat);
#endif
}
Expand Down Expand Up @@ -588,4 +657,4 @@ void Gint::transfer_DM2DtoGrid(std::vector<hamilt::HContainer<double>*> DM2D)
}
}
ModuleBase::timer::tick("Gint","transfer_DMR");
}
}
4 changes: 3 additions & 1 deletion source/module_hamilt_lcao/module_gint/gint.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ class Gint
void transfer_DM2DtoGrid(std::vector<hamilt::HContainer<double>*> DM2D);

const Grid_Technique* gridt = nullptr;

protected:

// variables related to FFT grid
int nbx;
int nby;
Expand Down Expand Up @@ -232,4 +234,4 @@ class Gint
double** pvdpRz_reduced = nullptr;
};

#endif
#endif

0 comments on commit 93b3276

Please sign in to comment.