Skip to content

Commit 27075c1

Browse files
authored
wrap exx-routine in iter_finish (#5413)
1 parent efd8594 commit 27075c1

File tree

3 files changed

+66
-69
lines changed

3 files changed

+66
-69
lines changed

source/module_esolver/esolver_ks_lcao.cpp

Lines changed: 4 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -941,76 +941,11 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(int& iter)
941941

942942
#ifdef __EXX
943943
// 3) save exx matrix
944-
int two_level_step = GlobalC::exx_info.info_ri.real_number ? this->exd->two_level_step : this->exc->two_level_step;
945-
946-
if (GlobalC::restart.info_save.save_H && two_level_step > 0
947-
&& (!GlobalC::exx_info.info_global.separate_loop || iter == 1)) // to avoid saving the same value repeatedly
948-
{
949-
////////// for Add_Hexx_Type::k
950-
/*
951-
hamilt::HS_Matrix_K<TK> Hexxk_save(&this->pv, 1);
952-
for (int ik = 0; ik < this->kv.get_nks(); ++ik) {
953-
Hexxk_save.set_zero_hk();
954-
955-
hamilt::OperatorEXX<hamilt::OperatorLCAO<TK, TR>> opexx_save(&Hexxk_save,
956-
nullptr,
957-
this->kv);
958-
959-
opexx_save.contributeHk(ik);
960-
961-
GlobalC::restart.save_disk("Hexx",
962-
ik,
963-
this->pv.get_local_size(),
964-
Hexxk_save.get_hk());
965-
}*/
966-
////////// for Add_Hexx_Type:R
967-
const std::string& restart_HR_path = GlobalC::restart.folder + "HexxR" + std::to_string(GlobalV::MY_RANK);
968-
if (GlobalC::exx_info.info_ri.real_number)
969-
{
970-
ModuleIO::write_Hexxs_csr(restart_HR_path, GlobalC::ucell, this->exd->get_Hexxs());
971-
}
972-
else
973-
{
974-
ModuleIO::write_Hexxs_csr(restart_HR_path, GlobalC::ucell, this->exc->get_Hexxs());
975-
}
976-
if (GlobalV::MY_RANK == 0)
977-
{
978-
GlobalC::restart.save_disk("Eexx", 0, 1, &this->pelec->f_en.exx);
979-
}
980-
}
981-
982-
if (GlobalC::exx_info.info_global.cal_exx && this->conv_esolver)
944+
if (GlobalC::exx_info.info_global.cal_exx)
983945
{
984-
// Kerker mixing does not work for the density matrix.
985-
// In the separate loop case, it can still work in the subsequent inner loops where Hexx(DM) is fixed.
986-
// In the non-separate loop case where Hexx(DM) is updated in every iteration of the 2nd loop, it should be
987-
// closed.
988-
if (!GlobalC::exx_info.info_global.separate_loop)
989-
{
990-
this->p_chgmix->close_kerker_gg0();
991-
}
992-
if (GlobalC::exx_info.info_ri.real_number)
993-
{
994-
this->conv_esolver = this->exd->exx_after_converge(
995-
*this->p_hamilt,
996-
*dynamic_cast<const elecstate::ElecStateLCAO<TK>*>(this->pelec)->get_DM(),
997-
this->kv,
998-
PARAM.inp.nspin,
999-
iter,
1000-
this->pelec->f_en.etot,
1001-
this->scf_ene_thr);
1002-
}
1003-
else
1004-
{
1005-
this->conv_esolver = this->exc->exx_after_converge(
1006-
*this->p_hamilt,
1007-
*dynamic_cast<const elecstate::ElecStateLCAO<TK>*>(this->pelec)->get_DM(),
1008-
this->kv,
1009-
PARAM.inp.nspin,
1010-
iter,
1011-
this->pelec->f_en.etot,
1012-
this->scf_ene_thr);
1013-
}
946+
GlobalC::exx_info.info_ri.real_number ?
947+
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) :
948+
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);
1014949
}
1015950
#endif
1016951

source/module_ri/Exx_LRI_interface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ class Exx_LRI_Interface
6363
/// @brief in hamilt2density: calculate Hexx and Eexx
6464
void exx_hamilt2density(elecstate::ElecState& elec, const Parallel_Orbitals& pv, const int iter);
6565

66+
/// @brief in iter_finish: write Hexx, do something according to whether SCF is converged
67+
void exx_iter_finish(const K_Vectors& kv, const UnitCell& ucell,
68+
hamilt::Hamilt<T>& hamilt, elecstate::ElecState& elec, Charge_Mixing& chgmix,
69+
const double& scf_ene_thr, int& iter, bool& conv_esolver);
6670
/// @brief: in do_after_converge: add exx operators; do DM mixing if seperate loop
6771
bool exx_after_converge(
6872
hamilt::Hamilt<T>& hamilt,

source/module_ri/Exx_LRI_interface.hpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <sys/time.h>
1414
#include "module_io/csr_reader.h"
1515
#include "module_io/write_HS_sparse.h"
16+
#include "module_elecstate/elecstate_lcao.h"
1617

1718
template<typename T, typename Tdata>
1819
void Exx_LRI_Interface<T, Tdata>::write_Hexxs_cereal(const std::string& file_name) const
@@ -143,6 +144,63 @@ void Exx_LRI_Interface<T, Tdata>::exx_hamilt2density(elecstate::ElecState& elec,
143144
}
144145
}
145146

147+
template<typename T, typename Tdata>
148+
void Exx_LRI_Interface<T, Tdata>::exx_iter_finish(const K_Vectors& kv, const UnitCell& ucell,
149+
hamilt::Hamilt<T>& hamilt, elecstate::ElecState& elec, Charge_Mixing& chgmix,
150+
const double& scf_ene_thr, int& iter, bool& conv_esolver)
151+
{
152+
if (GlobalC::restart.info_save.save_H && this->two_level_step > 0
153+
&& (!GlobalC::exx_info.info_global.separate_loop || iter == 1)) // to avoid saving the same value repeatedly
154+
{
155+
////////// for Add_Hexx_Type::k
156+
/*
157+
hamilt::HS_Matrix_K<TK> Hexxk_save(&this->pv, 1);
158+
for (int ik = 0; ik < this->kv.get_nks(); ++ik) {
159+
Hexxk_save.set_zero_hk();
160+
161+
hamilt::OperatorEXX<hamilt::OperatorLCAO<TK, TR>> opexx_save(&Hexxk_save,
162+
nullptr,
163+
this->kv);
164+
165+
opexx_save.contributeHk(ik);
166+
167+
GlobalC::restart.save_disk("Hexx",
168+
ik,
169+
this->pv.get_local_size(),
170+
Hexxk_save.get_hk());
171+
}*/
172+
////////// for Add_Hexx_Type:R
173+
const std::string& restart_HR_path = GlobalC::restart.folder + "HexxR" + std::to_string(GlobalV::MY_RANK);
174+
ModuleIO::write_Hexxs_csr(restart_HR_path, GlobalC::ucell, this->get_Hexxs());
175+
176+
if (GlobalV::MY_RANK == 0)
177+
{
178+
GlobalC::restart.save_disk("Eexx", 0, 1, &elec.f_en.exx);
179+
}
180+
}
181+
182+
if (GlobalC::exx_info.info_global.cal_exx && conv_esolver)
183+
{
184+
// Kerker mixing does not work for the density matrix.
185+
// In the separate loop case, it can still work in the subsequent inner loops where Hexx(DM) is fixed.
186+
// In the non-separate loop case where Hexx(DM) is updated in every iteration of the 2nd loop, it should be
187+
// closed.
188+
if (!GlobalC::exx_info.info_global.separate_loop)
189+
{
190+
chgmix.close_kerker_gg0();
191+
}
192+
conv_esolver = this->exx_after_converge(
193+
hamilt,
194+
*dynamic_cast<const elecstate::ElecStateLCAO<T>*>(&elec)->get_DM(),
195+
kv,
196+
PARAM.inp.nspin,
197+
iter,
198+
elec.f_en.etot,
199+
scf_ene_thr);
200+
}
201+
//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
202+
}
203+
146204
template<typename T, typename Tdata>
147205
bool Exx_LRI_Interface<T, Tdata>::exx_after_converge(
148206
hamilt::Hamilt<T>& hamilt,

0 commit comments

Comments
 (0)