Skip to content

Commit

Permalink
add energy_stable count to energy threshold
Browse files Browse the repository at this point in the history
  • Loading branch information
hongriTianqi committed Aug 28, 2024
1 parent 93badfa commit 714478e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 7 deletions.
2 changes: 2 additions & 0 deletions source/module_esolver/esolver_fp.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ namespace ModuleESolver

bool conv_elec; // If electron density is converged in scf.

int energy_stable_count = 0; // Counter for stable energy convergence.

protected:
//! Something to do after SCF iterations when SCF is converged or comes to the max iter step.
virtual void after_scf(const int istep);
Expand Down
15 changes: 13 additions & 2 deletions source/module_esolver/esolver_ks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,7 @@ void ESolver_KS<T, Device>::runner(const int istep, UnitCell& ucell)
bool firstscf = true;
this->conv_elec = false;
this->niter = this->maxniter;
this->energy_stable_count = 0;

// 4) SCF iterations
double diag_ethr = GlobalV::PW_DIAG_THR;
Expand Down Expand Up @@ -644,8 +645,18 @@ void ESolver_KS<T, Device>::iter_finish(int& iter)
// add a energy threshold for SCF convergence
if (this->conv_elec == 0) // only check when density is not converged
{
this->conv_elec
= (iter != 1 && std::abs(this->pelec->f_en.etot_delta * ModuleBase::Ry_to_eV) < this->scf_ene_thr);
if (iter != 1 && std::abs(this->pelec->f_en.etot_delta * ModuleBase::Ry_to_eV) < this->scf_ene_thr)
{
this->energy_stable_count++;
}
else
{
this->energy_stable_count = 0;
}
if (this->energy_stable_count >= 2) // energy stable for 2 steps
{
this->conv_elec = true;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions source/module_esolver/esolver_ks_lcao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -998,7 +998,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(int& iter)
PARAM.inp.nspin,
iter,
this->pelec->f_en.etot,
this->scf_ene_thr);
this->energy_stable_count);
}
else
{
Expand All @@ -1009,7 +1009,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(int& iter)
PARAM.inp.nspin,
iter,
this->pelec->f_en.etot,
this->scf_ene_thr);
this->energy_stable_count);
}
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion source/module_ri/Exx_LRI_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class Exx_LRI_Interface
const int& nspin,
int& iter,
const double& etot,
const double& scf_ene_thr);
const int& energy_stable_count);
int two_level_step = 0;
double etot_last_outer_loop = 0.0;
private:
Expand Down
4 changes: 2 additions & 2 deletions source/module_ri/Exx_LRI_interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ bool Exx_LRI_Interface<T, Tdata>::exx_after_converge(
const int& nspin,
int& iter,
const double& etot,
const double& scf_ene_thr)
const int& energy_stable_count)
{ // only called if (GlobalC::exx_info.info_global.cal_exx)
auto restart_reset = [this]()
{ // avoid calling restart related procedure in the subsequent ion steps
Expand Down Expand Up @@ -169,7 +169,7 @@ bool Exx_LRI_Interface<T, Tdata>::exx_after_converge(
// exx converged or get max exx steps
if (this->two_level_step == GlobalC::exx_info.info_global.hybrid_step
|| (iter == 1 && this->two_level_step != 0) // density convergence of outer loop
|| (ediff < scf_ene_thr && this->two_level_step != 0)) //energy convergence of outer loop
|| (energy_stable_count >= 2 && this->two_level_step != 0)) //energy convergence of outer loop
{
restart_reset();
return true;
Expand Down

0 comments on commit 714478e

Please sign in to comment.