Skip to content

Commit 49d6afa

Browse files
Fix: md restart can not read STRU (#5215)
* Refactor: rename conv_elec to conv_esolver * Refactor: move some print func to Module_IO * Fix: md restart can not read STRU * Fix: read STRU in md restart * [pre-commit.ci lite] apply automatic fixes * Tests: update tests --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 5329628 commit 49d6afa

34 files changed

+243
-307
lines changed

source/driver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ void Driver::init()
4141

4242
// (3) output information
4343
time_t time_finish = std::time(nullptr);
44-
Print_Info::print_time(time_start, time_finish);
44+
ModuleIO::print_time(time_start, time_finish);
4545

4646
// (4) close all of the running logs
4747
ModuleBase::Global_File::close_all_log(GlobalV::MY_RANK, PARAM.inp.out_alllog,PARAM.inp.calculation);

source/module_esolver/esolver.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,8 @@ class ESolver
5858
return 0;
5959
}
6060

61-
// get conv_elec used in current scf
62-
virtual bool get_conv_elec()
63-
{
64-
return true;
65-
}
61+
bool conv_esolver = true; // whether esolver is converged
62+
6663
std::string classname;
6764
};
6865

source/module_esolver/esolver_fp.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -119,23 +119,14 @@ void ESolver_FP::before_all_runners(const Input_para& inp, UnitCell& cell)
119119
return;
120120
}
121121

122-
//------------------------------------------------------------------------------
123-
//! the 12th function of ESolver_KS: get_conv_elec
124-
//! tqzhao add 2024-05-15
125-
//------------------------------------------------------------------------------
126-
bool ESolver_FP::get_conv_elec()
127-
{
128-
return this->conv_elec;
129-
}
130-
131122
//! Something to do after SCF iterations when SCF is converged or comes to the max iter step.
132123
void ESolver_FP::after_scf(const int istep)
133124
{
134125
// 0) output convergence information
135-
ModuleIO::output_convergence_after_scf(this->conv_elec, this->pelec->f_en.etot);
126+
ModuleIO::output_convergence_after_scf(this->conv_esolver, this->pelec->f_en.etot);
136127

137128
// 1) write fermi energy
138-
ModuleIO::output_efermi(this->conv_elec, this->pelec->eferm.ef);
129+
ModuleIO::output_efermi(this->conv_esolver, this->pelec->eferm.ef);
139130

140131
// 2) update delta rho for charge extrapolation
141132
CE.update_delta_rho(GlobalC::ucell, &(this->chr), &(this->sf));

source/module_esolver/esolver_fp.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,6 @@ namespace ModuleESolver
4444
//! Initialize of the first-principels energy solver
4545
virtual void before_all_runners(const Input_para& inp, UnitCell& cell) override;
4646

47-
// get conv_elec used in current scf
48-
virtual bool get_conv_elec() override;
49-
5047
virtual void init_after_vc(const Input_para& inp, UnitCell& cell); // liuyu add 2023-03-09
5148

5249
//! Electronic states
@@ -64,8 +61,6 @@ namespace ModuleESolver
6461
//! K points in Brillouin zone
6562
K_Vectors kv;
6663

67-
bool conv_elec; // If electron density is converged in scf.
68-
6964
protected:
7065
//! Something to do after SCF iterations when SCF is converged or comes to the max iter step.
7166
virtual void after_scf(const int istep);

source/module_esolver/esolver_ks.cpp

Lines changed: 16 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
#include <chrono>
88
#endif
99
#include "module_base/timer.h"
10+
#include "module_cell/cal_atoms_info.h"
1011
#include "module_io/json_output/init_info.h"
12+
#include "module_io/output_log.h"
1113
#include "module_io/print_info.h"
1214
#include "module_io/write_istate_info.h"
1315
#include "module_parameter/parameter.h"
14-
#include "module_cell/cal_atoms_info.h"
1516

1617
#include <iostream>
1718
//--------------Temporary----------------
@@ -221,7 +222,7 @@ void ESolver_KS<T, Device>::before_all_runners(const Input_para& inp, UnitCell&
221222
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS");
222223

223224
//! 7) print information
224-
Print_Info::setup_parameters(ucell, this->kv);
225+
ModuleIO::setup_parameters(ucell, this->kv);
225226

226227
//! 8) new plane wave basis, fft grids, etc.
227228
#ifdef __MPI
@@ -419,7 +420,7 @@ void ESolver_KS<T, Device>::runner(const int istep, UnitCell& ucell)
419420
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT SCF");
420421

421422
bool firstscf = true;
422-
this->conv_elec = false;
423+
this->conv_esolver = false;
423424
this->niter = this->maxniter;
424425

425426
// 4) SCF iterations
@@ -429,7 +430,7 @@ void ESolver_KS<T, Device>::runner(const int istep, UnitCell& ucell)
429430
for (int iter = 1; iter <= this->maxniter; ++iter)
430431
{
431432
// 5) write head
432-
this->write_head(GlobalV::ofs_running, istep, iter);
433+
ModuleIO::write_head(GlobalV::ofs_running, istep, iter, this->basisname);
433434

434435
#ifdef __MPI
435436
auto iterstart = MPI_Wtime();
@@ -496,9 +497,9 @@ void ESolver_KS<T, Device>::runner(const int istep, UnitCell& ucell)
496497
PARAM.inp.esolver_type,
497498
diag_ethr,
498499
PARAM.inp.nelec);
499-
500+
500501
// The error of HSolver is larger than drho,
501-
// so a more precise HSolver should be excuconv_elected.
502+
// so a more precise HSolver should be executed.
502503
if (hsolver_error > drho)
503504
{
504505
diag_ethr = hsolver::reset_diag_ethr(GlobalV::ofs_running,
@@ -540,11 +541,11 @@ void ESolver_KS<T, Device>::runner(const int istep, UnitCell& ucell)
540541
}
541542
#endif
542543

543-
this->conv_elec = (drho < this->scf_thr && not_restart_step && is_U_converged);
544+
this->conv_esolver = (drho < this->scf_thr && not_restart_step && is_U_converged);
544545

545546
// If drho < hsolver_error in the first iter or drho < scf_thr, we
546547
// do not change rho.
547-
if (drho < hsolver_error || this->conv_elec)
548+
if (drho < hsolver_error || this->conv_esolver)
548549
{
549550
if (drho < hsolver_error)
550551
{
@@ -577,13 +578,13 @@ void ESolver_KS<T, Device>::runner(const int istep, UnitCell& ucell)
577578
}
578579
#ifdef __MPI
579580
MPI_Bcast(&drho, 1, MPI_DOUBLE, 0, PARAPW_WORLD);
580-
MPI_Bcast(&this->conv_elec, 1, MPI_DOUBLE, 0, PARAPW_WORLD);
581+
MPI_Bcast(&this->conv_esolver, 1, MPI_DOUBLE, 0, PARAPW_WORLD);
581582
MPI_Bcast(pelec->charge->rho[0], this->pw_rhod->nrxx, MPI_DOUBLE, 0, PARAPW_WORLD);
582583
#endif
583584

584585
// 9) update potential
585586
// Hamilt should be used after it is constructed.
586-
// this->phamilt->update(conv_elec);
587+
// this->phamilt->update(conv_esolver);
587588
this->update_pot(istep, iter);
588589

589590
// 10) finish scf iterations
@@ -617,7 +618,7 @@ void ESolver_KS<T, Device>::runner(const int istep, UnitCell& ucell)
617618
#endif //__RAPIDJSON
618619

619620
// 13) check convergence
620-
if (this->conv_elec)
621+
if (this->conv_esolver)
621622
{
622623
this->niter = iter;
623624
break;
@@ -632,7 +633,7 @@ void ESolver_KS<T, Device>::runner(const int istep, UnitCell& ucell)
632633
std::cout << " >> Leave SCF iteration.\n * * * * * *" << std::endl;
633634
#ifdef __RAPIDJSON
634635
// 14) add Json of efermi energy converge
635-
Json::add_output_efermi_converge(this->pelec->eferm.ef * ModuleBase::Ry_to_eV, this->conv_elec);
636+
Json::add_output_efermi_converge(this->pelec->eferm.ef * ModuleBase::Ry_to_eV, this->conv_esolver);
636637
#endif //__RAPIDJSON
637638

638639
// 15) after scf
@@ -666,9 +667,9 @@ void ESolver_KS<T, Device>::iter_finish(int& iter)
666667
this->pelec->f_en.etot_old = this->pelec->f_en.etot;
667668

668669
// add a energy threshold for SCF convergence
669-
if (this->conv_elec == 0) // only check when density is not converged
670+
if (this->conv_esolver == 0) // only check when density is not converged
670671
{
671-
this->conv_elec
672+
this->conv_esolver
672673
= (iter != 1 && std::abs(this->pelec->f_en.etot_delta * ModuleBase::Ry_to_eV) < this->scf_ene_thr);
673674
}
674675
}
@@ -687,33 +688,6 @@ void ESolver_KS<T, Device>::after_scf(const int istep)
687688
}
688689
}
689690

690-
//------------------------------------------------------------------------------
691-
//! the 8th function of ESolver_KS: print_head
692-
//! mohan add 2024-05-12
693-
//------------------------------------------------------------------------------
694-
template <typename T, typename Device>
695-
void ESolver_KS<T, Device>::print_head()
696-
{
697-
std::cout << " " << std::setw(7) << "ITER";
698-
699-
if (PARAM.inp.nspin == 2)
700-
{
701-
std::cout << std::setw(10) << "TMAG";
702-
std::cout << std::setw(10) << "AMAG";
703-
}
704-
705-
std::cout << std::setw(15) << "ETOT(eV)";
706-
std::cout << std::setw(15) << "EDIFF(eV)";
707-
std::cout << std::setw(11) << "DRHO";
708-
709-
if (XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5)
710-
{
711-
std::cout << std::setw(11) << "DKIN";
712-
}
713-
714-
std::cout << std::setw(11) << "TIME(s)" << std::endl;
715-
}
716-
717691
//------------------------------------------------------------------------------
718692
//! the 8th function of ESolver_KS: print_iter
719693
//! mohan add 2024-05-12
@@ -725,18 +699,7 @@ void ESolver_KS<T, Device>::print_iter(const int iter,
725699
const double duration,
726700
const double ethr)
727701
{
728-
this->pelec->print_etot(this->conv_elec, iter, drho, dkin, duration, PARAM.inp.printe, ethr);
729-
}
730-
731-
//------------------------------------------------------------------------------
732-
//! the 9th function of ESolver_KS: write_head
733-
//! mohan add 2024-05-12
734-
//------------------------------------------------------------------------------
735-
template <typename T, typename Device>
736-
void ESolver_KS<T, Device>::write_head(std::ofstream& ofs_running, const int istep, const int iter)
737-
{
738-
ofs_running << "\n " << this->basisname << " ALGORITHM --------------- ION=" << std::setw(4) << istep + 1
739-
<< " ELEC=" << std::setw(4) << iter << "--------------------------------\n";
702+
this->pelec->print_etot(this->conv_esolver, iter, drho, dkin, duration, PARAM.inp.printe, ethr);
740703
}
741704

742705
//------------------------------------------------------------------------------

source/module_esolver/esolver_ks.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ class ESolver_KS : public ESolver_FP
7373
virtual void update_pot(const int istep, const int iter) {};
7474

7575
protected:
76-
77-
// Print the headline on the screen:
78-
// ITER ETOT(eV) EDIFF(eV) DRHO TIME(s)
79-
void print_head();
80-
81-
// Print inforamtion in each iter
76+
// Print inforamtion in each iter
8277
// G1 -3.435545e+03 0.000000e+00 3.607e-01 2.862e-01
8378
// for metaGGA
8479
// ITER ETOT(eV) EDIFF(eV) DRHO DKIN TIME(s)
@@ -90,13 +85,6 @@ class ESolver_KS : public ESolver_FP
9085
const double duration,
9186
const double ethr);
9287

93-
// Write the headline in the running_log file
94-
// "PW/LCAO" ALGORITHM --------------- ION= 1 ELEC= 1--------------------------------
95-
void write_head(
96-
std::ofstream& ofs_running,
97-
const int istep,
98-
const int iter);
99-
10088
//! Hamiltonian
10189
hamilt::Hamilt<T, Device>* p_hamilt = nullptr;
10290

source/module_esolver/esolver_ks_lcao.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ void ESolver_KS_LCAO<TK, TR>::before_all_runners(const Input_para& inp, UnitCell
136136
this->kv.set(ucell.symm, PARAM.inp.kpoint_file, PARAM.inp.nspin, ucell.G, ucell.latvec, GlobalV::ofs_running);
137137
ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "INIT K-POINTS");
138138

139-
Print_Info::setup_parameters(ucell, this->kv);
139+
ModuleIO::setup_parameters(ucell, this->kv);
140140
}
141141
else
142142
{
@@ -817,7 +817,7 @@ void ESolver_KS_LCAO<TK, TR>::update_pot(const int istep, const int iter)
817817
ModuleBase::TITLE("ESolver_KS_LCAO", "update_pot");
818818

819819
// 1) print Hamiltonian and Overlap matrix
820-
if (this->conv_elec || iter == PARAM.inp.scf_nmax)
820+
if (this->conv_esolver || iter == PARAM.inp.scf_nmax)
821821
{
822822
if (!PARAM.globalv.gamma_only_local && (PARAM.inp.out_mat_hs[0] || PARAM.inp.deepks_v_delta))
823823
{
@@ -875,7 +875,7 @@ void ESolver_KS_LCAO<TK, TR>::update_pot(const int istep, const int iter)
875875
}
876876

877877
// 2) print wavefunctions
878-
if (elecstate::ElecStateLCAO<TK>::out_wfc_lcao && (this->conv_elec || iter == PARAM.inp.scf_nmax)
878+
if (elecstate::ElecStateLCAO<TK>::out_wfc_lcao && (this->conv_esolver || iter == PARAM.inp.scf_nmax)
879879
&& (istep % PARAM.inp.out_interval == 0))
880880
{
881881
ModuleIO::write_wfc_nao(elecstate::ElecStateLCAO<TK>::out_wfc_lcao,
@@ -887,7 +887,7 @@ void ESolver_KS_LCAO<TK, TR>::update_pot(const int istep, const int iter)
887887
istep);
888888
}
889889

890-
if (!this->conv_elec)
890+
if (!this->conv_esolver)
891891
{
892892
if (PARAM.inp.nspin == 4)
893893
{
@@ -977,7 +977,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(int& iter)
977977
}
978978
}
979979

980-
if (GlobalC::exx_info.info_global.cal_exx && this->conv_elec)
980+
if (GlobalC::exx_info.info_global.cal_exx && this->conv_esolver)
981981
{
982982
// Kerker mixing does not work for the density matrix.
983983
// In the separate loop case, it can still work in the subsequent inner loops where Hexx(DM) is fixed.
@@ -989,7 +989,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(int& iter)
989989
}
990990
if (GlobalC::exx_info.info_ri.real_number)
991991
{
992-
this->conv_elec = this->exd->exx_after_converge(
992+
this->conv_esolver = this->exd->exx_after_converge(
993993
*this->p_hamilt,
994994
*dynamic_cast<const elecstate::ElecStateLCAO<TK>*>(this->pelec)->get_DM(),
995995
this->kv,
@@ -1000,7 +1000,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(int& iter)
10001000
}
10011001
else
10021002
{
1003-
this->conv_elec = this->exc->exx_after_converge(
1003+
this->conv_esolver = this->exc->exx_after_converge(
10041004
*this->p_hamilt,
10051005
*dynamic_cast<const elecstate::ElecStateLCAO<TK>*>(this->pelec)->get_DM(),
10061006
this->kv,
@@ -1080,7 +1080,7 @@ void ESolver_KS_LCAO<TK, TR>::iter_finish(int& iter)
10801080
}
10811081

10821082
// 6) use the converged occupation matrix for next MD/Relax SCF calculation
1083-
if (PARAM.inp.dft_plus_u && this->conv_elec)
1083+
if (PARAM.inp.dft_plus_u && this->conv_esolver)
10841084
{
10851085
GlobalC::dftu.initialed_locale = true;
10861086
}

source/module_esolver/esolver_ks_lcao_tddft.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ void ESolver_KS_LCAO_TDDFT::hamilt2density(const int istep, const int iter, cons
233233
void ESolver_KS_LCAO_TDDFT::update_pot(const int istep, const int iter)
234234
{
235235
// print Hamiltonian and Overlap matrix
236-
if (this->conv_elec)
236+
if (this->conv_esolver)
237237
{
238238
if (!PARAM.globalv.gamma_only_local)
239239
{
@@ -282,8 +282,8 @@ void ESolver_KS_LCAO_TDDFT::update_pot(const int istep, const int iter)
282282
}
283283
}
284284

285-
if (elecstate::ElecStateLCAO<std::complex<double>>::out_wfc_lcao && (this->conv_elec || iter == PARAM.inp.scf_nmax)
286-
&& (istep % PARAM.inp.out_interval == 0))
285+
if (elecstate::ElecStateLCAO<std::complex<double>>::out_wfc_lcao
286+
&& (this->conv_esolver || iter == PARAM.inp.scf_nmax) && (istep % PARAM.inp.out_interval == 0))
287287
{
288288
ModuleIO::write_wfc_nao(elecstate::ElecStateLCAO<std::complex<double>>::out_wfc_lcao,
289289
this->psi[0],
@@ -295,7 +295,7 @@ void ESolver_KS_LCAO_TDDFT::update_pot(const int istep, const int iter)
295295
}
296296

297297
// Calculate new potential according to new Charge Density
298-
if (!this->conv_elec)
298+
if (!this->conv_esolver)
299299
{
300300
if (PARAM.inp.nspin == 4)
301301
{
@@ -316,7 +316,7 @@ void ESolver_KS_LCAO_TDDFT::update_pot(const int istep, const int iter)
316316
const int nlocal = PARAM.globalv.nlocal;
317317

318318
// store wfc and Hk laststep
319-
if (istep >= (wf.init_wfc == "file" ? 0 : 1) && this->conv_elec)
319+
if (istep >= (wf.init_wfc == "file" ? 0 : 1) && this->conv_esolver)
320320
{
321321
if (this->psi_laststep == nullptr)
322322
{
@@ -378,7 +378,7 @@ void ESolver_KS_LCAO_TDDFT::update_pot(const int istep, const int iter)
378378
}
379379

380380
// print "eigen value" for tddft
381-
if (this->conv_elec)
381+
if (this->conv_esolver)
382382
{
383383
GlobalV::ofs_running << "---------------------------------------------------------------"
384384
"---------------------------------"

0 commit comments

Comments
 (0)