Skip to content

Commit

Permalink
Delete conv_esolver in esolver, and add 'conv_esolver' as a parameter…
Browse files Browse the repository at this point in the history
… in iter_finish, after_scf, and update_pot functions (#5941)

* update the format in esolver_ks_pw.cpp

* create a new file lcao_after_scf.cpp and move some codes from esolver_ks_lcao to here

* add conv_esolver as parameter of iter_finish, after_scf, update_pot

* add missing file in Makefile.Objects

* deal with the multiple definitions that appear in hpp files

* update codes about hpp files

* add new kinetic operator header file in order to print out T (kinetic) matrix

* update lcao_after_scf, support RPA and LRI

* update lcao_after_scf

* update

---------

Co-authored-by: Hongxu Ren <60290838+Flying-dragon-boxing@users.noreply.github.com>
  • Loading branch information
mohanchen and Flying-dragon-boxing authored Mar 3, 2025
1 parent 1fa5e3a commit 83f966a
Show file tree
Hide file tree
Showing 24 changed files with 681 additions and 530 deletions.
1 change: 1 addition & 0 deletions source/Makefile.Objects
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ OBJS_ESOLVER=esolver.o\
OBJS_ESOLVER_LCAO=esolver_ks_lcao.o\
esolver_ks_lcao_tddft.o\
lcao_before_scf.o\
lcao_after_scf.o\
esolver_gets.o\
lcao_others.o\

Expand Down
1 change: 1 addition & 0 deletions source/module_esolver/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if(ENABLE_LCAO)
esolver_ks_lcao.cpp
esolver_ks_lcao_tddft.cpp
lcao_before_scf.cpp
lcao_after_scf.cpp
esolver_gets.cpp
lcao_others.cpp
)
Expand Down
10 changes: 5 additions & 5 deletions source/module_esolver/esolver_fp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ void ESolver_FP::before_all_runners(UnitCell& ucell, const Input_para& inp)
}

//! Something to do after SCF iterations when SCF is converged or comes to the max iter step.
void ESolver_FP::after_scf(UnitCell& ucell, const int istep)
void ESolver_FP::after_scf(UnitCell& ucell, const int istep, const bool conv_esolver)
{
ModuleBase::TITLE("ESolver_FP", "after_scf");

// 0) output convergence information
ModuleIO::output_convergence_after_scf(this->conv_esolver, this->pelec->f_en.etot);
ModuleIO::output_convergence_after_scf(conv_esolver, this->pelec->f_en.etot);

// 1) write fermi energy
ModuleIO::output_efermi(this->conv_esolver, this->pelec->eferm.ef);
ModuleIO::output_efermi(conv_esolver, this->pelec->eferm.ef);

// 2) update delta rho for charge extrapolation
CE.update_delta_rho(ucell, &(this->chr), &(this->sf));
Expand Down Expand Up @@ -286,12 +286,12 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep)
return;
}

void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter)
void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver)
{
//! output charge density
if (PARAM.inp.out_chg[0] != -1)
{
if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || this->conv_esolver)
if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || conv_esolver)
{
std::complex<double>** rhog_tot
= (PARAM.inp.dm_to_rho) ? this->pelec->charge->rhog : this->pelec->charge->rhog_save;
Expand Down
4 changes: 2 additions & 2 deletions source/module_esolver/esolver_fp.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ class ESolver_FP : public ESolver
virtual void before_scf(UnitCell& ucell, const int istep);

//! Something to do after SCF iterations when SCF is converged or comes to the max iter step.
virtual void after_scf(UnitCell& ucell, const int istep);
virtual void after_scf(UnitCell& ucell, const int istep, const bool conv_esolver);

//! Something to do after hamilt2density function in each iter loop.
virtual void iter_finish(UnitCell& ucell, const int istep, int& iter);
virtual void iter_finish(UnitCell& ucell, const int istep, int& iter, bool &conv_esolver);

//! ------------------------------------------------------------------------------
//! These pointers will be deleted in the free_pointers() function every ion step.
Expand Down
34 changes: 18 additions & 16 deletions source/module_esolver/esolver_ks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ void ESolver_KS<T, Device>::runner(UnitCell& ucell, const int istep)
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT SCF");

// 4) SCF iterations
this->conv_esolver = false;
bool conv_esolver = false;
this->niter = this->maxniter;
this->diag_ethr = PARAM.inp.pw_diag_thr;
for (int iter = 1; iter <= this->maxniter; ++iter)
Expand All @@ -449,10 +449,10 @@ void ESolver_KS<T, Device>::runner(UnitCell& ucell, const int istep)
this->hamilt2density(ucell, istep, iter, diag_ethr);

// 7) finish scf iterations
this->iter_finish(ucell, istep, iter);
this->iter_finish(ucell, istep, iter, conv_esolver);

// 8) check convergence
if (this->conv_esolver || this->oscillate_esolver)
if (conv_esolver || this->oscillate_esolver)
{
this->niter = iter;
if (this->oscillate_esolver)
Expand All @@ -464,7 +464,7 @@ void ESolver_KS<T, Device>::runner(UnitCell& ucell, const int istep)
} // end scf iterations

// 9) after scf
this->after_scf(ucell, istep);
this->after_scf(ucell, istep, conv_esolver);

ModuleBase::timer::tick(this->classname, "runner");
return;
Expand Down Expand Up @@ -524,7 +524,7 @@ void ESolver_KS<T, Device>::iter_init(UnitCell& ucell, const int istep, const in
}

template <typename T, typename Device>
void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& iter)
void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& iter, bool &conv_esolver)
{
if (PARAM.inp.out_bandgap)
{
Expand Down Expand Up @@ -578,30 +578,30 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i
}
#endif

this->conv_esolver = (drho < this->scf_thr && not_restart_step && is_U_converged);
conv_esolver = (drho < this->scf_thr && not_restart_step && is_U_converged);

// add energy threshold for SCF convergence
if (this->scf_ene_thr > 0.0)
{
// calculate energy of output charge density
this->update_pot(ucell, istep, iter);
this->update_pot(ucell, istep, iter, conv_esolver);
this->pelec->cal_energies(2); // 2 means Kohn-Sham functional
// now, etot_old is the energy of input density, while etot is the energy of output density
this->pelec->f_en.etot_delta = this->pelec->f_en.etot - this->pelec->f_en.etot_old;
// output etot_delta
GlobalV::ofs_running << " DeltaE_womix = " << this->pelec->f_en.etot_delta * ModuleBase::Ry_to_eV << " eV"
<< std::endl;
if (iter > 1 && this->conv_esolver == 1) // only check when density is converged
if (iter > 1 && conv_esolver == 1) // only check when density is converged
{
// update the convergence flag
this->conv_esolver
conv_esolver
= (std::abs(this->pelec->f_en.etot_delta * ModuleBase::Ry_to_eV) < this->scf_ene_thr);
}
}

// If drho < hsolver_error in the first iter or drho < scf_thr, we
// do not change rho.
if (drho < hsolver_error || this->conv_esolver || PARAM.inp.calculation == "nscf")
if (drho < hsolver_error || conv_esolver || PARAM.inp.calculation == "nscf")
{
if (drho < hsolver_error)
{
Expand Down Expand Up @@ -635,14 +635,16 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i

#ifdef __MPI
MPI_Bcast(&drho, 1, MPI_DOUBLE, 0, BP_WORLD);
MPI_Bcast(&this->conv_esolver, 1, MPI_DOUBLE, 0, BP_WORLD);

// be careful! conv_esolver is bool, not double !! Maybe a bug 20250302 by mohan
MPI_Bcast(&conv_esolver, 1, MPI_DOUBLE, 0, BP_WORLD);
MPI_Bcast(pelec->charge->rho[0], this->pw_rhod->nrxx, MPI_DOUBLE, 0, BP_WORLD);
#endif

// update potential
// Hamilt should be used after it is constructed.
// this->phamilt->update(conv_esolver);
this->update_pot(ucell, istep, iter);
this->update_pot(ucell, istep, iter, conv_esolver);

// 1 means Harris-Foulkes functional
// 2 means Kohn-Sham functional
Expand Down Expand Up @@ -671,7 +673,7 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i
dkin = p_chgmix->get_dkin(pelec->charge, PARAM.inp.nelec);
}

this->pelec->print_etot(ucell.magnet,this->conv_esolver, iter, drho, dkin, duration, PARAM.inp.printe, diag_ethr);
this->pelec->print_etot(ucell.magnet,conv_esolver, iter, drho, dkin, duration, PARAM.inp.printe, diag_ethr);

// Json, need to be moved to somewhere else
#ifdef __RAPIDJSON
Expand All @@ -691,17 +693,17 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i
std::cout << " SCF restart after this step!" << std::endl;
}

ESolver_FP::iter_finish(ucell, istep, iter);
ESolver_FP::iter_finish(ucell, istep, iter, conv_esolver);
}

//! Something to do after SCF iterations when SCF is converged or comes to the max iter step.
template <typename T, typename Device>
void ESolver_KS<T, Device>::after_scf(UnitCell& ucell, const int istep)
void ESolver_KS<T, Device>::after_scf(UnitCell& ucell, const int istep, const bool conv_esolver)
{
ModuleBase::TITLE("ESolver_KS", "after_scf");

// 1) call after_scf() of ESolver_FP
ESolver_FP::after_scf(ucell, istep);
ESolver_FP::after_scf(ucell, istep, conv_esolver);

// 2) write eigenvalues
if (istep % PARAM.inp.out_interval == 0)
Expand Down
6 changes: 3 additions & 3 deletions source/module_esolver/esolver_ks.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ESolver_KS : public ESolver_FP
virtual void iter_init(UnitCell& ucell, const int istep, const int iter);

//! Something to do after hamilt2density function in each iter loop.
virtual void iter_finish(UnitCell& ucell, const int istep, int& iter) override;
virtual void iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver) override;

// calculate electron density from a specific Hamiltonian with ethr
virtual void hamilt2density_single(UnitCell& ucell, const int istep, const int iter, const double ethr);
Expand All @@ -52,10 +52,10 @@ class ESolver_KS : public ESolver_FP
void hamilt2density(UnitCell& ucell, const int istep, const int iter, const double ethr);

//! Something to do after SCF iterations when SCF is converged or comes to the max iter step.
virtual void after_scf(UnitCell& ucell, const int istep) override;
virtual void after_scf(UnitCell& ucell, const int istep, const bool conv_esolver) override;

//! <Temporary> It should be replaced by a function in Hamilt Class
virtual void update_pot(UnitCell& ucell, const int istep, const int iter){};
virtual void update_pot(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver){};

//! Hamiltonian
hamilt::Hamilt<T, Device>* p_hamilt = nullptr;
Expand Down
Loading

0 comments on commit 83f966a

Please sign in to comment.