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

Refactoring LCAO codes. #3860

Merged
merged 39 commits into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
5d8b0c7
update timer, add std
mohanchen Mar 27, 2024
b5bcc7f
update the format of esolver_fp.cpp
mohanchen Mar 27, 2024
3c69ace
add description in esolver_fp.h
mohanchen Mar 27, 2024
e1551cf
update a few esolver files
mohanchen Mar 27, 2024
f0a5c3b
update esolver description for LCAO
mohanchen Mar 27, 2024
1c8ed86
update some formats of esolver codes
mohanchen Mar 27, 2024
ea0e2c4
update esolver_ks_pw.cpp formats
mohanchen Mar 27, 2024
ae36b11
update formats of esolver_ks_pw.cpp and esolver_ks_pw.h
mohanchen Mar 27, 2024
16b0f76
update esolver_ks_lcao_tddft.cpp formats
mohanchen Mar 27, 2024
1768c88
update format of esolver_sdft_pw.cpp
mohanchen Mar 27, 2024
6a606ed
update the format of esolver_sdft_pw_tool.cpp
mohanchen Mar 27, 2024
cac11ea
keep formating esolver_ks_pw.cpp
mohanchen Mar 27, 2024
e191f6c
formating esolver_of_interface.cpp
mohanchen Mar 27, 2024
8eddd5a
change GlobalC::ucell to ucell in esolver_ks.cpp
mohanchen Mar 27, 2024
bcf4d54
remove some GlobalC::ucell in esolver_sdft_pw.cpp
mohanchen Mar 27, 2024
1ca6574
refactor the code before getting rid of RA in esolver_lcao
mohanchen Mar 28, 2024
7987684
refactor before getting rid of LOWF
mohanchen Mar 28, 2024
186609f
refactor before getting rid of LCAO_hamilt.h and LCAO_matrix.h
mohanchen Mar 28, 2024
fc02fb8
refactor wavefunc_in_pw
mohanchen Mar 28, 2024
7a8064f
refactor density matrix
mohanchen Mar 28, 2024
f95129d
refactor the format cal_dm_psi.cpp
mohanchen Mar 28, 2024
98234ae
format forces.cpp
mohanchen Mar 30, 2024
583214c
refactor esolver_of_tool.cpp
mohanchen Mar 30, 2024
2638dd3
resolve conflict
mohanchen Mar 30, 2024
9505a9f
change member function beforescf in Esolver to before_scf
mohanchen Mar 31, 2024
313893e
change afterscf to after_scf
mohanchen Mar 31, 2024
7317a47
change updatepot to update_pot
mohanchen Mar 31, 2024
730b460
change eachiterinit to iter_init, change eachiterfinish to iter_finish
mohanchen Mar 31, 2024
31760e8
refactor esolvers, change member function names of most esolvers
mohanchen Mar 31, 2024
cccbb14
reformat esolver.h
mohanchen Mar 31, 2024
270a39d
update tests for esolvers
mohanchen Mar 31, 2024
bfc8ac7
add TITLE in esolver_ks_lcao
mohanchen Mar 31, 2024
3e15388
update esolver_ks_lcao
mohanchen Mar 31, 2024
413e73f
update esolver_lcao
mohanchen Mar 31, 2024
3f2006a
update timer::tick in esolver_lcao
mohanchen Mar 31, 2024
78c08cf
try to delete LCAO_Matrix in LCAO_Hamilt, and try to delete Parallel_…
mohanchen Mar 31, 2024
76b23de
fix the compiling issue with LCAO_hamilt.hpp
mohanchen Mar 31, 2024
4018fa2
try to divide the FORCE_k.cpp into several small files
mohanchen Mar 31, 2024
eee4f97
fix conflicts
mohanchen Mar 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions source/module_esolver/esolver_ks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ void ESolver_KS<T, Device>::run(const int istep, UnitCell& ucell)
ModuleBase::timer::tick(this->classname, "run");

this->before_scf(istep); //Something else to do before the iter loop

ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT SCF");

if(this->maxniter > 0)
{
this->print_head(); //print the headline on the screen.
Expand Down Expand Up @@ -499,9 +501,6 @@ void ESolver_KS<T, Device>::run(const int istep, UnitCell& ucell)
(std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now()
- iterstart)).count() / static_cast<double>(1e6);
#endif
/*
SCF print: G1 -3.435545e+03 0.000000e+00 3.607e-01 2.862e-01
*/

double dkin = 0.0; // for meta-GGA
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
Expand All @@ -525,7 +524,10 @@ void ESolver_KS<T, Device>::run(const int istep, UnitCell& ucell)
{
this->niter = iter;
bool stop = this->do_after_converge(iter);
if(stop) break;
if(stop)
{
break;
}
}

// notice for restart
Expand All @@ -544,7 +546,9 @@ void ESolver_KS<T, Device>::run(const int istep, UnitCell& ucell)
this->conv_elec
);
#endif //__RAPIDJSON
after_scf(istep);

this->after_scf(istep);

ModuleBase::timer::tick(this->classname, "run");
}

Expand Down
90 changes: 72 additions & 18 deletions source/module_esolver/esolver_ks_lcao.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "esolver_ks_lcao.h"
#include "module_base/tool_title.h"

#include "module_base/global_variable.h"
#include "module_io/dos_nao.h"
Expand All @@ -21,7 +22,6 @@
#include "module_hamilt_pw/hamilt_pwdft/global.h"
#include "module_io/print_info.h"
#ifdef __EXX
// #include "module_rpa/rpa.h"
#include "module_ri/RPA_LRI.h"
#endif

Expand Down Expand Up @@ -81,6 +81,8 @@ template <typename TK, typename TR>
void ESolver_KS_LCAO<TK, TR>::init(Input& inp, UnitCell& ucell)
{
ModuleBase::TITLE("ESolver_KS_LCAO", "init");
ModuleBase::timer::tick("ESolver_KS_LCAO", "init");

// if we are only calculating S, then there is no need
// to prepare for potentials and so on

Expand Down Expand Up @@ -123,7 +125,7 @@ void ESolver_KS_LCAO<TK, TR>::init(Input& inp, UnitCell& ucell)
// Init Basis should be put outside of Ensolver.
// * reading the localized orbitals/projectors
// * construct the interpolation tables.
this->Init_Basis_lcao(this->orb_con, inp, ucell);
this->init_basis_lcao(this->orb_con, inp, ucell);
//------------------init Basis_lcao----------------------

//! pass Hamilt-pointer to Operator
Expand All @@ -137,6 +139,7 @@ void ESolver_KS_LCAO<TK, TR>::init(Input& inp, UnitCell& ucell)

if (GlobalV::CALCULATION == "get_S")
{
ModuleBase::timer::tick("ESolver_KS_LCAO", "init");
return;
}

Expand Down Expand Up @@ -230,13 +233,19 @@ void ESolver_KS_LCAO<TK, TR>::init(Input& inp, UnitCell& ucell)
if (GlobalV::ocp)
{
this->pelec->fixed_weights(GlobalV::ocp_kb);
}
}

ModuleBase::timer::tick("ESolver_KS_LCAO", "init");
return;
}


template <typename TK, typename TR>
void ESolver_KS_LCAO<TK, TR>::init_after_vc(Input& inp, UnitCell& ucell)
{
ModuleBase::TITLE("ESolver_KS_LCAO", "init_after_vc");
ModuleBase::timer::tick("ESolver_KS_LCAO", "init_after_vc");

ESolver_KS<TK>::init_after_vc(inp, ucell);

if (GlobalV::md_prec_level == 2)
Expand Down Expand Up @@ -269,26 +278,38 @@ void ESolver_KS_LCAO<TK, TR>::init_after_vc(Input& inp, UnitCell& ucell)
&(this->pelec->f_en.etxc),
&(this->pelec->f_en.vtxc));
}
}
}

ModuleBase::timer::tick("ESolver_KS_LCAO", "init_after_vc");
return;
}


template <typename TK, typename TR>
double ESolver_KS_LCAO<TK, TR>::cal_energy()
{
ModuleBase::TITLE("ESolver_KS_LCAO", "cal_energy");

return this->pelec->f_en.etot;
}


template <typename TK, typename TR>
void ESolver_KS_LCAO<TK, TR>::cal_force(ModuleBase::matrix& force)
{
Force_Stress_LCAO<TK> FSL(this->RA, GlobalC::ucell.nat);
FSL.getForceStress(GlobalV::CAL_FORCE,
ModuleBase::TITLE("ESolver_KS_LCAO", "cal_force");
ModuleBase::timer::tick("ESolver_KS_LCAO", "cal_force");

Force_Stress_LCAO<TK> fsl(this->RA, GlobalC::ucell.nat);

fsl.getForceStress(
GlobalV::CAL_FORCE,
GlobalV::CAL_STRESS,
GlobalV::TEST_FORCE,
GlobalV::TEST_STRESS,
this->LOC,
this->orb_con.ParaV,
this->LM,
this->pelec,
this->psi,
this->UHM,
Expand All @@ -308,25 +329,35 @@ void ESolver_KS_LCAO<TK, TR>::cal_force(ModuleBase::matrix& force)
this->RA.delete_grid();

this->have_force = true;

ModuleBase::timer::tick("ESolver_KS_LCAO", "cal_force");
}


template <typename TK, typename TR>
void ESolver_KS_LCAO<TK, TR>::cal_stress(ModuleBase::matrix& stress)
{
ModuleBase::TITLE("ESolver_KS_LCAO", "cal_stress");
ModuleBase::timer::tick("ESolver_KS_LCAO", "cal_stress");

if (!this->have_force)
{
ModuleBase::matrix fcs;
this->cal_force(fcs);
}
stress = this->scs; // copy the stress
this->have_force = false;

ModuleBase::timer::tick("ESolver_KS_LCAO", "cal_stress");
}


template <typename TK, typename TR>
void ESolver_KS_LCAO<TK, TR>::post_process(void)
{
ModuleBase::TITLE("ESolver_KS_LCAO", "post_process");
ModuleBase::timer::tick("ESolver_KS_LCAO", "post_process");

GlobalV::ofs_running << "\n\n --------------------------------------------" << std::endl;
GlobalV::ofs_running << std::setprecision(16);
GlobalV::ofs_running << " !FINAL_ETOT_IS " << this->pelec->f_en.etot * ModuleBase::Ry_to_eV << " eV" << std::endl;
Expand Down Expand Up @@ -413,15 +444,18 @@ void ESolver_KS_LCAO<TK, TR>::post_process(void)
GlobalV::NBANDS,
this->p_hamilt);
}
ModuleBase::timer::tick("ESolver_KS_LCAO", "post_process");
}


template <typename TK, typename TR>
void ESolver_KS_LCAO<TK, TR>::Init_Basis_lcao(
void ESolver_KS_LCAO<TK, TR>::init_basis_lcao(
ORB_control& orb_con,
Input& inp,
UnitCell& ucell)
{
ModuleBase::TITLE("ESolver_KS_LCAO", "init_basis_lcao");

// autoset NB2D first
if (GlobalV::NB2D == 0)
{
Expand Down Expand Up @@ -500,12 +534,16 @@ void ESolver_KS_LCAO<TK, TR>::Init_Basis_lcao(
this->orb_con.setup_2d_division(GlobalV::ofs_running, GlobalV::ofs_warning);
this->orb_con.ParaV.set_atomic_trace(GlobalC::ucell.get_iat2iwt(), GlobalC::ucell.nat, GlobalV::NLOCAL);
}

return;
}


template <typename TK, typename TR>
void ESolver_KS_LCAO<TK, TR>::iter_init(const int istep, const int iter)
{
ModuleBase::TITLE("ESolver_KS_LCAO", "iter_init");

if (iter == 1)
{
this->p_chgmix->init_mixing(); // init mixing
Expand Down Expand Up @@ -645,6 +683,8 @@ void ESolver_KS_LCAO<TK, TR>::iter_init(const int istep, const int iter)
template <typename TK, typename TR>
void ESolver_KS_LCAO<TK, TR>::hamilt2density(int istep, int iter, double ethr)
{
ModuleBase::TITLE("ESolver_KS_LCAO", "hamilt2density");

// save input rho
this->pelec->charge->save_rho_before_sum_band();
// save density matrix for mixing
Expand Down Expand Up @@ -756,6 +796,8 @@ void ESolver_KS_LCAO<TK, TR>::hamilt2density(int istep, int iter, double ethr)
template <typename TK, typename TR>
void ESolver_KS_LCAO<TK, TR>::update_pot(const int istep, const int iter)
{
ModuleBase::TITLE("ESolver_KS_LCAO", "update_pot");

// print Hamiltonian and Overlap matrix
if (this->conv_elec)
{
Expand Down Expand Up @@ -852,6 +894,8 @@ void ESolver_KS_LCAO<TK, TR>::update_pot(const int istep, const int iter)
template <typename TK, typename TR>
void ESolver_KS_LCAO<TK, TR>::iter_finish(int iter)
{
ModuleBase::TITLE("ESolver_KS_LCAO", "iter_finish");

// mix density matrix
if (GlobalV::MIXING_RESTART > 0 && iter >= this->p_chgmix->mixing_restart && GlobalV::MIXING_DMR )
{
Expand Down Expand Up @@ -930,6 +974,8 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(int iter)
template <typename TK, typename TR>
void ESolver_KS_LCAO<TK, TR>::after_scf(const int istep)
{
ModuleBase::TITLE("ESolver_KS_LCAO", "after_scf");

// save charge difference into files for charge extrapolation
if (GlobalV::CALCULATION != "scf")
{
Expand Down Expand Up @@ -1015,6 +1061,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(const int istep)
this->LOWF.ParaV,
*(this->psi),
dynamic_cast<const elecstate::ElecStateLCAO<TK>*>(this->pelec)->get_DM());

ModuleBase::timer::tick("ESolver_KS_LCAO", "out_deepks_labels");

#endif
Expand Down Expand Up @@ -1042,6 +1089,7 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(const int istep)
} // qifeng add 2019/9/10, jiyy modify 2023/2/27, liuyu move here 2023-04-18
}

// spin constrain calculations, added by Tianqi Zhao.
if (GlobalV::sc_mag_switch)
{
SpinConstrain<TK, psi::DEVICE_CPU>& sc = SpinConstrain<TK, psi::DEVICE_CPU>::getScInstance();
Expand All @@ -1052,6 +1100,8 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(const int istep)
{
RA.delete_grid();
}

// quasi-orbitals, added by Yike Huang.
if(GlobalV::qo_switch)
{
toQO tqo(GlobalV::qo_basis, GlobalV::qo_strategy, GlobalV::qo_thr, GlobalV::qo_screening_coeff);
Expand All @@ -1069,6 +1119,8 @@ void ESolver_KS_LCAO<TK, TR>::after_scf(const int istep)
template <typename TK, typename TR>
bool ESolver_KS_LCAO<TK, TR>::do_after_converge(int& iter)
{
ModuleBase::TITLE("ESolver_KS_LCAO","do_after_converge");

#ifdef __EXX
if (GlobalC::exx_info.info_ri.real_number)
{
Expand All @@ -1088,11 +1140,13 @@ bool ESolver_KS_LCAO<TK, TR>::do_after_converge(int& iter)
iter);
}
#endif // __EXX

if(GlobalV::dft_plus_u)
{
// use the converged occupation matrix for next MD/Relax SCF calculation
GlobalC::dftu.initialed_locale = true;
}

return true;
}

Expand Down Expand Up @@ -1127,17 +1181,17 @@ ModuleIO::Output_DM1 ESolver_KS_LCAO<TK, TR>::create_Output_DM1(int istep)
template <typename TK, typename TR>
ModuleIO::Output_Mat_Sparse<TK> ESolver_KS_LCAO<TK, TR>::create_Output_Mat_Sparse(int istep)
{
return ModuleIO::Output_Mat_Sparse<TK>(hsolver::HSolverLCAO<TK>::out_mat_hsR,
hsolver::HSolverLCAO<TK>::out_mat_dh,
hsolver::HSolverLCAO<TK>::out_mat_t,
INPUT.out_mat_r,
istep,
this->pelec->pot->get_effective_v(),
*this->LOWF.ParaV,
this->UHM,
this->LM,
this->kv,
this->p_hamilt);
return ModuleIO::Output_Mat_Sparse<TK>(hsolver::HSolverLCAO<TK>::out_mat_hsR,
hsolver::HSolverLCAO<TK>::out_mat_dh,
hsolver::HSolverLCAO<TK>::out_mat_t,
INPUT.out_mat_r,
istep,
this->pelec->pot->get_effective_v(),
*this->LOWF.ParaV,
this->UHM,
this->LM,
this->kv,
this->p_hamilt);
}


Expand Down
6 changes: 3 additions & 3 deletions source/module_esolver/esolver_ks_lcao.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ namespace ModuleESolver

double cal_energy() override;

void cal_force(ModuleBase::matrix& force) override;
void cal_force(ModuleBase::matrix &force) override;

void cal_stress(ModuleBase::matrix& stress) override;
void cal_stress(ModuleBase::matrix &stress) override;

void post_process() override;

Expand Down Expand Up @@ -89,7 +89,7 @@ namespace ModuleESolver
ModuleBase::matrix scs;
bool have_force = false;

void Init_Basis_lcao(ORB_control& orb_con, Input& inp, UnitCell& ucell);
void init_basis_lcao(ORB_control& orb_con, Input& inp, UnitCell& ucell);

//--------------common for all calculation, not only scf-------------
// set matrix and grid integral
Expand Down
2 changes: 1 addition & 1 deletion source/module_esolver/esolver_ks_lcao_tddft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ void ESolver_KS_LCAO_TDDFT::init(Input& inp, UnitCell& ucell)
// Init Basis should be put outside of Ensolver.
// * reading the localized orbitals/projectors
// * construct the interpolation tables.
this->Init_Basis_lcao(this->orb_con, inp, ucell);
this->init_basis_lcao(this->orb_con, inp, ucell);
//------------------init Basis_lcao----------------------

//------------------init Hamilt_lcao----------------------
Expand Down
Loading