Skip to content

Commit 83f966a

Browse files
Delete conv_esolver in esolver, and add 'conv_esolver' as a parameter in iter_finish, after_scf, and update_pot functions (deepmodeling#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 <[email protected]>
1 parent 1fa5e3a commit 83f966a

24 files changed

+681
-530
lines changed

source/Makefile.Objects

+1
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ OBJS_ESOLVER=esolver.o\
259259
OBJS_ESOLVER_LCAO=esolver_ks_lcao.o\
260260
esolver_ks_lcao_tddft.o\
261261
lcao_before_scf.o\
262+
lcao_after_scf.o\
262263
esolver_gets.o\
263264
lcao_others.o\
264265

source/module_esolver/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ if(ENABLE_LCAO)
1717
esolver_ks_lcao.cpp
1818
esolver_ks_lcao_tddft.cpp
1919
lcao_before_scf.cpp
20+
lcao_after_scf.cpp
2021
esolver_gets.cpp
2122
lcao_others.cpp
2223
)

source/module_esolver/esolver_fp.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,15 @@ void ESolver_FP::before_all_runners(UnitCell& ucell, const Input_para& inp)
127127
}
128128

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

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

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

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

289-
void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter)
289+
void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver)
290290
{
291291
//! output charge density
292292
if (PARAM.inp.out_chg[0] != -1)
293293
{
294-
if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || this->conv_esolver)
294+
if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || conv_esolver)
295295
{
296296
std::complex<double>** rhog_tot
297297
= (PARAM.inp.dm_to_rho) ? this->pelec->charge->rhog : this->pelec->charge->rhog_save;

source/module_esolver/esolver_fp.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ class ESolver_FP : public ESolver
3939
virtual void before_scf(UnitCell& ucell, const int istep);
4040

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

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

4747
//! ------------------------------------------------------------------------------
4848
//! These pointers will be deleted in the free_pointers() function every ion step.

source/module_esolver/esolver_ks.cpp

+18-16
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ void ESolver_KS<T, Device>::runner(UnitCell& ucell, const int istep)
437437
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT SCF");
438438

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

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

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

466466
// 9) after scf
467-
this->after_scf(ucell, istep);
467+
this->after_scf(ucell, istep, conv_esolver);
468468

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

526526
template <typename T, typename Device>
527-
void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& iter)
527+
void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& iter, bool &conv_esolver)
528528
{
529529
if (PARAM.inp.out_bandgap)
530530
{
@@ -578,30 +578,30 @@ void ESolver_KS<T, Device>::iter_finish(UnitCell& ucell, const int istep, int& i
578578
}
579579
#endif
580580

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

583583
// add energy threshold for SCF convergence
584584
if (this->scf_ene_thr > 0.0)
585585
{
586586
// calculate energy of output charge density
587-
this->update_pot(ucell, istep, iter);
587+
this->update_pot(ucell, istep, iter, conv_esolver);
588588
this->pelec->cal_energies(2); // 2 means Kohn-Sham functional
589589
// now, etot_old is the energy of input density, while etot is the energy of output density
590590
this->pelec->f_en.etot_delta = this->pelec->f_en.etot - this->pelec->f_en.etot_old;
591591
// output etot_delta
592592
GlobalV::ofs_running << " DeltaE_womix = " << this->pelec->f_en.etot_delta * ModuleBase::Ry_to_eV << " eV"
593593
<< std::endl;
594-
if (iter > 1 && this->conv_esolver == 1) // only check when density is converged
594+
if (iter > 1 && conv_esolver == 1) // only check when density is converged
595595
{
596596
// update the convergence flag
597-
this->conv_esolver
597+
conv_esolver
598598
= (std::abs(this->pelec->f_en.etot_delta * ModuleBase::Ry_to_eV) < this->scf_ene_thr);
599599
}
600600
}
601601

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

636636
#ifdef __MPI
637637
MPI_Bcast(&drho, 1, MPI_DOUBLE, 0, BP_WORLD);
638-
MPI_Bcast(&this->conv_esolver, 1, MPI_DOUBLE, 0, BP_WORLD);
638+
639+
// be careful! conv_esolver is bool, not double !! Maybe a bug 20250302 by mohan
640+
MPI_Bcast(&conv_esolver, 1, MPI_DOUBLE, 0, BP_WORLD);
639641
MPI_Bcast(pelec->charge->rho[0], this->pw_rhod->nrxx, MPI_DOUBLE, 0, BP_WORLD);
640642
#endif
641643

642644
// update potential
643645
// Hamilt should be used after it is constructed.
644646
// this->phamilt->update(conv_esolver);
645-
this->update_pot(ucell, istep, iter);
647+
this->update_pot(ucell, istep, iter, conv_esolver);
646648

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

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

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

694-
ESolver_FP::iter_finish(ucell, istep, iter);
696+
ESolver_FP::iter_finish(ucell, istep, iter, conv_esolver);
695697
}
696698

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

703705
// 1) call after_scf() of ESolver_FP
704-
ESolver_FP::after_scf(ucell, istep);
706+
ESolver_FP::after_scf(ucell, istep, conv_esolver);
705707

706708
// 2) write eigenvalues
707709
if (istep % PARAM.inp.out_interval == 0)

source/module_esolver/esolver_ks.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ESolver_KS : public ESolver_FP
4343
virtual void iter_init(UnitCell& ucell, const int istep, const int iter);
4444

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

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

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

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

6060
//! Hamiltonian
6161
hamilt::Hamilt<T, Device>* p_hamilt = nullptr;

0 commit comments

Comments
 (0)