From 714478e868a36444df368355b19c07c011c6758f Mon Sep 17 00:00:00 2001 From: hongriTianqi Date: Wed, 28 Aug 2024 17:24:59 +0800 Subject: [PATCH] add energy_stable count to energy threshold --- source/module_esolver/esolver_fp.h | 2 ++ source/module_esolver/esolver_ks.cpp | 15 +++++++++++++-- source/module_esolver/esolver_ks_lcao.cpp | 4 ++-- source/module_ri/Exx_LRI_interface.h | 2 +- source/module_ri/Exx_LRI_interface.hpp | 4 ++-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/source/module_esolver/esolver_fp.h b/source/module_esolver/esolver_fp.h index 4d5fcf5c41..a90f63a170 100644 --- a/source/module_esolver/esolver_fp.h +++ b/source/module_esolver/esolver_fp.h @@ -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); diff --git a/source/module_esolver/esolver_ks.cpp b/source/module_esolver/esolver_ks.cpp index 8226184e39..c8ca1bb4e9 100644 --- a/source/module_esolver/esolver_ks.cpp +++ b/source/module_esolver/esolver_ks.cpp @@ -425,6 +425,7 @@ void ESolver_KS::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; @@ -644,8 +645,18 @@ void ESolver_KS::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; + } } } diff --git a/source/module_esolver/esolver_ks_lcao.cpp b/source/module_esolver/esolver_ks_lcao.cpp index 177b791b98..39062d69ba 100644 --- a/source/module_esolver/esolver_ks_lcao.cpp +++ b/source/module_esolver/esolver_ks_lcao.cpp @@ -998,7 +998,7 @@ void ESolver_KS_LCAO::iter_finish(int& iter) PARAM.inp.nspin, iter, this->pelec->f_en.etot, - this->scf_ene_thr); + this->energy_stable_count); } else { @@ -1009,7 +1009,7 @@ void ESolver_KS_LCAO::iter_finish(int& iter) PARAM.inp.nspin, iter, this->pelec->f_en.etot, - this->scf_ene_thr); + this->energy_stable_count); } } #endif diff --git a/source/module_ri/Exx_LRI_interface.h b/source/module_ri/Exx_LRI_interface.h index 9f16e95acc..2377857470 100644 --- a/source/module_ri/Exx_LRI_interface.h +++ b/source/module_ri/Exx_LRI_interface.h @@ -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: diff --git a/source/module_ri/Exx_LRI_interface.hpp b/source/module_ri/Exx_LRI_interface.hpp index aaa338845d..228256f0ed 100644 --- a/source/module_ri/Exx_LRI_interface.hpp +++ b/source/module_ri/Exx_LRI_interface.hpp @@ -130,7 +130,7 @@ bool Exx_LRI_Interface::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 @@ -169,7 +169,7 @@ bool Exx_LRI_Interface::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;