Skip to content

Commit 9114b4a

Browse files
committed
Merge branch 'develop' of https://github.com/deepmodeling/abacus-develop into refactor
2 parents 5b3f106 + 83f966a commit 9114b4a

26 files changed

+716
-556
lines changed

source/Makefile.Objects

Lines changed: 1 addition & 0 deletions
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_base/tool_title.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ namespace ModuleBase
1717

1818
void TITLE(const std::string &class_name,const std::string &function_name,const bool disable)
1919
{
20-
if (disable)
21-
{
22-
return; // no output
23-
}
20+
// if (disable)
21+
// {
22+
// return; // no output
23+
// }
2424
#ifdef __NORMAL
2525
std::cout<<" ==> "<<class_name<<"::"<<function_name<<"\t"
2626
<<ModuleBase::GlobalFunc::MemAvailable()/1024.0/1024<<" GB\t"
@@ -37,10 +37,10 @@ void TITLE(const std::string &class_name,const std::string &function_name,const
3737

3838
void TITLE(std::ofstream &ofs,const std::string &class_name,const std::string &function_name,const bool disable)
3939
{
40-
if (disable)
41-
{
42-
return; // no output
43-
}
40+
// if (disable)
41+
// {
42+
// return; // no output
43+
// }
4444
#ifdef __NORMAL
4545
std::cout<<"\n\n ==> "<<class_name<<"::"<<function_name<<"\t"
4646
<<ModuleBase::GlobalFunc::MemAvailable()/1024.0/1024<<" GB\t"

source/module_esolver/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
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

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,15 @@ void ESolver_FP::before_all_runners(UnitCell& ucell, const Input_para& inp)
130130
}
131131

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

137137
// 0) output convergence information
138-
ModuleIO::output_convergence_after_scf(this->conv_esolver, this->pelec->f_en.etot);
138+
ModuleIO::output_convergence_after_scf(conv_esolver, this->pelec->f_en.etot);
139139

140140
// 1) write fermi energy
141-
ModuleIO::output_efermi(this->conv_esolver, this->pelec->eferm.ef);
141+
ModuleIO::output_efermi(conv_esolver, this->pelec->eferm.ef);
142142

143143
// 2) update delta rho for charge extrapolation
144144
CE.update_delta_rho(ucell, &(this->chr), &(this->sf));
@@ -313,23 +313,12 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep)
313313
this->pelec->f_en.ewald_energy = H_Ewald_pw::compute_ewald(ucell, this->pw_rhod, this->sf.strucFac);
314314
}
315315

316-
//=========================================================
317-
// cal_ux should be called before init_scf because
318-
// the direction of ux is used in noncoline_rho
319-
//=========================================================
316+
//----------------------------------------------------------
317+
//! cal_ux should be called before init_scf because
318+
//! the direction of ux is used in noncoline_rho
319+
//----------------------------------------------------------
320320
elecstate::cal_ux(ucell);
321321

322-
this->pelec->init_scf(istep, ucell, this->Pgrid, this->sf.strucFac, this->locpp.numeric, ucell.symm);
323-
324-
//! Symmetry_rho should behind init_scf, because charge should be
325-
//! initialized first. liuyu comment: Symmetry_rho should be located between
326-
//! init_rho and v_of_rho?
327-
Symmetry_rho srho;
328-
for (int is = 0; is < PARAM.inp.nspin; is++)
329-
{
330-
srho.begin(is, *(this->pelec->charge), this->pw_rhod, ucell.symm);
331-
}
332-
333322
//! output the initial charge density
334323
if (PARAM.inp.out_chg[0] == 2)
335324
{
@@ -371,12 +360,12 @@ void ESolver_FP::before_scf(UnitCell& ucell, const int istep)
371360
return;
372361
}
373362

374-
void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter)
363+
void ESolver_FP::iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver)
375364
{
376365
//! output charge density
377366
if (PARAM.inp.out_chg[0] != -1)
378367
{
379-
if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || this->conv_esolver)
368+
if (iter % PARAM.inp.out_freq_elec == 0 || iter == PARAM.inp.scf_nmax || conv_esolver)
380369
{
381370
std::complex<double>** rhog_tot
382371
= (PARAM.inp.dm_to_rho) ? this->pelec->charge->rhog : this->pelec->charge->rhog_save;

source/module_esolver/esolver_fp.h

Lines changed: 2 additions & 2 deletions
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

Lines changed: 18 additions & 16 deletions
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

Lines changed: 3 additions & 3 deletions
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)