From 1727ee5f9a8d750003d6be84ae59e1c342edaba9 Mon Sep 17 00:00:00 2001 From: maki49 <1579492865@qq.com> Date: Tue, 5 Nov 2024 23:21:59 +0800 Subject: [PATCH] wrap exx-routine in iter_finish --- source/module_esolver/esolver_ks_lcao.cpp | 73 ++--------------------- source/module_ri/Exx_LRI_interface.h | 4 ++ source/module_ri/Exx_LRI_interface.hpp | 58 ++++++++++++++++++ 3 files changed, 66 insertions(+), 69 deletions(-) diff --git a/source/module_esolver/esolver_ks_lcao.cpp b/source/module_esolver/esolver_ks_lcao.cpp index cb455ace00..f028f9f601 100644 --- a/source/module_esolver/esolver_ks_lcao.cpp +++ b/source/module_esolver/esolver_ks_lcao.cpp @@ -941,76 +941,11 @@ void ESolver_KS_LCAO::iter_finish(int& iter) #ifdef __EXX // 3) save exx matrix - int two_level_step = GlobalC::exx_info.info_ri.real_number ? this->exd->two_level_step : this->exc->two_level_step; - - if (GlobalC::restart.info_save.save_H && two_level_step > 0 - && (!GlobalC::exx_info.info_global.separate_loop || iter == 1)) // to avoid saving the same value repeatedly - { - ////////// for Add_Hexx_Type::k - /* - hamilt::HS_Matrix_K Hexxk_save(&this->pv, 1); - for (int ik = 0; ik < this->kv.get_nks(); ++ik) { - Hexxk_save.set_zero_hk(); - - hamilt::OperatorEXX> opexx_save(&Hexxk_save, - nullptr, - this->kv); - - opexx_save.contributeHk(ik); - - GlobalC::restart.save_disk("Hexx", - ik, - this->pv.get_local_size(), - Hexxk_save.get_hk()); - }*/ - ////////// for Add_Hexx_Type:R - const std::string& restart_HR_path = GlobalC::restart.folder + "HexxR" + std::to_string(GlobalV::MY_RANK); - if (GlobalC::exx_info.info_ri.real_number) - { - ModuleIO::write_Hexxs_csr(restart_HR_path, GlobalC::ucell, this->exd->get_Hexxs()); - } - else - { - ModuleIO::write_Hexxs_csr(restart_HR_path, GlobalC::ucell, this->exc->get_Hexxs()); - } - if (GlobalV::MY_RANK == 0) - { - GlobalC::restart.save_disk("Eexx", 0, 1, &this->pelec->f_en.exx); - } - } - - if (GlobalC::exx_info.info_global.cal_exx && this->conv_esolver) + if (GlobalC::exx_info.info_global.cal_exx) { - // Kerker mixing does not work for the density matrix. - // In the separate loop case, it can still work in the subsequent inner loops where Hexx(DM) is fixed. - // In the non-separate loop case where Hexx(DM) is updated in every iteration of the 2nd loop, it should be - // closed. - if (!GlobalC::exx_info.info_global.separate_loop) - { - this->p_chgmix->close_kerker_gg0(); - } - if (GlobalC::exx_info.info_ri.real_number) - { - this->conv_esolver = this->exd->exx_after_converge( - *this->p_hamilt, - *dynamic_cast*>(this->pelec)->get_DM(), - this->kv, - PARAM.inp.nspin, - iter, - this->pelec->f_en.etot, - this->scf_ene_thr); - } - else - { - this->conv_esolver = this->exc->exx_after_converge( - *this->p_hamilt, - *dynamic_cast*>(this->pelec)->get_DM(), - this->kv, - PARAM.inp.nspin, - iter, - this->pelec->f_en.etot, - this->scf_ene_thr); - } + GlobalC::exx_info.info_ri.real_number ? + this->exd->exx_iter_finish(this->kv, GlobalC::ucell, *this->p_hamilt, *this->pelec, *this->p_chgmix, this->scf_ene_thr, iter, this->conv_esolver) : + this->exc->exx_iter_finish(this->kv, GlobalC::ucell, *this->p_hamilt, *this->pelec, *this->p_chgmix, this->scf_ene_thr, iter, this->conv_esolver); } #endif diff --git a/source/module_ri/Exx_LRI_interface.h b/source/module_ri/Exx_LRI_interface.h index 3eb3db37d2..902c105b04 100644 --- a/source/module_ri/Exx_LRI_interface.h +++ b/source/module_ri/Exx_LRI_interface.h @@ -63,6 +63,10 @@ class Exx_LRI_Interface /// @brief in hamilt2density: calculate Hexx and Eexx void exx_hamilt2density(elecstate::ElecState& elec, const Parallel_Orbitals& pv, const int iter); + /// @brief in iter_finish: write Hexx, do something according to whether SCF is converged + void exx_iter_finish(const K_Vectors& kv, const UnitCell& ucell, + hamilt::Hamilt& hamilt, elecstate::ElecState& elec, Charge_Mixing& chgmix, + const double& scf_ene_thr, int& iter, bool& conv_esolver); /// @brief: in do_after_converge: add exx operators; do DM mixing if seperate loop bool exx_after_converge( hamilt::Hamilt& hamilt, diff --git a/source/module_ri/Exx_LRI_interface.hpp b/source/module_ri/Exx_LRI_interface.hpp index ae0c3869f8..ec0cf1fa04 100644 --- a/source/module_ri/Exx_LRI_interface.hpp +++ b/source/module_ri/Exx_LRI_interface.hpp @@ -13,6 +13,7 @@ #include #include "module_io/csr_reader.h" #include "module_io/write_HS_sparse.h" +#include "module_elecstate/elecstate_lcao.h" template void Exx_LRI_Interface::write_Hexxs_cereal(const std::string& file_name) const @@ -143,6 +144,63 @@ void Exx_LRI_Interface::exx_hamilt2density(elecstate::ElecState& elec, } } +template +void Exx_LRI_Interface::exx_iter_finish(const K_Vectors& kv, const UnitCell& ucell, + hamilt::Hamilt& hamilt, elecstate::ElecState& elec, Charge_Mixing& chgmix, + const double& scf_ene_thr, int& iter, bool& conv_esolver) +{ + if (GlobalC::restart.info_save.save_H && this->two_level_step > 0 + && (!GlobalC::exx_info.info_global.separate_loop || iter == 1)) // to avoid saving the same value repeatedly + { + ////////// for Add_Hexx_Type::k + /* + hamilt::HS_Matrix_K Hexxk_save(&this->pv, 1); + for (int ik = 0; ik < this->kv.get_nks(); ++ik) { + Hexxk_save.set_zero_hk(); + + hamilt::OperatorEXX> opexx_save(&Hexxk_save, + nullptr, + this->kv); + + opexx_save.contributeHk(ik); + + GlobalC::restart.save_disk("Hexx", + ik, + this->pv.get_local_size(), + Hexxk_save.get_hk()); + }*/ + ////////// for Add_Hexx_Type:R + const std::string& restart_HR_path = GlobalC::restart.folder + "HexxR" + std::to_string(GlobalV::MY_RANK); + ModuleIO::write_Hexxs_csr(restart_HR_path, GlobalC::ucell, this->get_Hexxs()); + + if (GlobalV::MY_RANK == 0) + { + GlobalC::restart.save_disk("Eexx", 0, 1, &elec.f_en.exx); + } + } + + if (GlobalC::exx_info.info_global.cal_exx && conv_esolver) + { + // Kerker mixing does not work for the density matrix. + // In the separate loop case, it can still work in the subsequent inner loops where Hexx(DM) is fixed. + // In the non-separate loop case where Hexx(DM) is updated in every iteration of the 2nd loop, it should be + // closed. + if (!GlobalC::exx_info.info_global.separate_loop) + { + chgmix.close_kerker_gg0(); + } + conv_esolver = this->exx_after_converge( + hamilt, + *dynamic_cast*>(&elec)->get_DM(), + kv, + PARAM.inp.nspin, + iter, + elec.f_en.etot, + scf_ene_thr); + } + //else if ( PARAM.inp.rdmft && two_level_step ) { conv_esolver = true; } // for RDMFT in the future to quit after the first iter of the exx-loop +} + template bool Exx_LRI_Interface::exx_after_converge( hamilt::Hamilt& hamilt,