From 7d638e7b3ebd1253a4071232d26f0f41a580d31f Mon Sep 17 00:00:00 2001 From: sunliang98 <1700011430@pku.edu.cn> Date: Thu, 29 May 2025 19:29:38 +0800 Subject: [PATCH 1/2] Refactor: Standardize OFDFT screen output to match KSDFT format --- source/module_esolver/esolver_fp.h | 6 +++ source/module_esolver/esolver_ks.h | 7 --- source/module_esolver/esolver_of.cpp | 6 ++- source/module_esolver/esolver_of_tool.cpp | 57 ++++++++++++----------- 4 files changed, 40 insertions(+), 36 deletions(-) diff --git a/source/module_esolver/esolver_fp.h b/source/module_esolver/esolver_fp.h index 9cbdcc7362..c247c444b1 100644 --- a/source/module_esolver/esolver_fp.h +++ b/source/module_esolver/esolver_fp.h @@ -98,6 +98,12 @@ class ESolver_FP: public ESolver int pw_rho_flag = false; ///< flag for pw_rho, 0: not initialized, 1: initialized + //! the start time of scf iteration + #ifdef __MPI + double iter_time; + #else + std::chrono::system_clock::time_point iter_time; + #endif }; } // namespace ModuleESolver diff --git a/source/module_esolver/esolver_ks.h b/source/module_esolver/esolver_ks.h index c73412cbdf..b35429826c 100644 --- a/source/module_esolver/esolver_ks.h +++ b/source/module_esolver/esolver_ks.h @@ -79,13 +79,6 @@ class ESolver_KS : public ESolver_FP //! Electronic wavefunctions psi::Psi* psi = nullptr; - //! the start time of scf iteration -#ifdef __MPI - double iter_time; -#else - std::chrono::system_clock::time_point iter_time; -#endif - std::string basisname; //! esolver_ks_lcao.cpp double esolver_KS_ne = 0.0; //! number of electrons double diag_ethr; //! the threshold for diagonalization diff --git a/source/module_esolver/esolver_of.cpp b/source/module_esolver/esolver_of.cpp index c7e79d4729..db0afb1a9b 100644 --- a/source/module_esolver/esolver_of.cpp +++ b/source/module_esolver/esolver_of.cpp @@ -164,8 +164,12 @@ void ESolver_OF::runner(UnitCell& ucell, const int istep) if (PARAM.inp.of_ml_local_test) this->ml_->localTest(this->chr.rho, this->pw_rho); #endif - bool conv_esolver = false; // this conv_esolver is added by mohan 20250302 +#ifdef __MPI + this->iter_time = MPI_Wtime(); +#else + this->iter_time = std::chrono::system_clock::now(); +#endif while (true) { diff --git a/source/module_esolver/esolver_of_tool.cpp b/source/module_esolver/esolver_of_tool.cpp index dae34b995c..41720dc746 100644 --- a/source/module_esolver/esolver_of_tool.cpp +++ b/source/module_esolver/esolver_of_tool.cpp @@ -390,38 +390,32 @@ void ESolver_OF::print_info(const bool conv_esolver) { if (this->iter_ == 0) { - std::cout << "============================== Running OFDFT " + std::cout << " ============================= Running OFDFT " "==============================" << std::endl; - std::cout << "Iter Etot(Ha) Mu(Ha) Theta " - "PotNorm deltaE(Ha)" + std::cout << " ITER ETOT/eV EDIFF/eV EFERMI/eV POTNORM TIME/s" << std::endl; - // cout << "======================================== Running OFDFT - // ========================================" << endl; cout << "Iter - // Etot(Ha) Theta PotNorm min/max(den) - // min/max(dE/dPhi)" << endl; } - // ============ used to compare with PROFESS3.0 ================ - // double minDen = this->chr.rho[0][0]; - // double maxDen = this->chr.rho[0][0]; - // double minPot = this->pdEdphi_[0][0]; - // double maxPot = this->pdEdphi_[0][0]; - // for (int i = 0; i < this->pw_rho->nrxx; ++i) - // { - // if (this->chr.rho[0][i] < minDen) minDen = - // this->chr.rho[0][i]; if (this->chr.rho[0][i] > maxDen) - // maxDen = this->chr.rho[0][i]; if (this->pdEdphi_[0][i] < minPot) - // minPot = this->pdEdphi_[0][i]; if (this->pdEdphi_[0][i] > maxPot) - // maxPot = this->pdEdphi_[0][i]; - // } - std::cout << std::setw(6) << this->iter_ << std::setw(22) << std::scientific << std::setprecision(12) - << this->energy_current_ / 2. << std::setw(12) << std::setprecision(3) << this->pelec->eferm.get_efval(0) / 2. - << std::setw(12) << this->theta_[0] << std::setw(12) << this->normdLdphi_ << std::setw(12) - << (this->energy_current_ - this->energy_last_) / 2. << std::endl; - // ============ used to compare with PROFESS3.0 ================ - // << setw(10) << minDen << "/ " << setw(12) << maxDen - // << setw(10) << minPot << "/ " << setw(10) << maxPot << endl; - // ============================================================= + + std::map prefix_map = { + {"cg1", "CG"}, + {"cg2", "CG"}, + {"tn", "TN"} + }; + std::string iteration = prefix_map[PARAM.inp.of_method] + std::to_string(this->iter_); +#ifdef __MPI + double duration = (double)(MPI_Wtime() - this->iter_time); +#else + double duration + = (std::chrono::duration_cast(std::chrono::system_clock::now() - this->iter_time)).count() + / static_cast(1e6); +#endif + std::cout << " " << std::setw(8) << iteration << std::setw(18) << std::scientific << std::setprecision(8) + << this->energy_current_ * ModuleBase::Ry_to_eV + << std::setw(18) << (this->energy_current_ - this->energy_last_) * ModuleBase::Ry_to_eV + << std::setw(13) << std::setprecision(4) << this->pelec->eferm.get_efval(0) * ModuleBase::Ry_to_eV + << std::setw(13) << std::setprecision(4) << this->normdLdphi_ + << std::setw(6) << std::fixed << std::setprecision(2) << duration << std::endl; GlobalV::ofs_running << std::setprecision(12); GlobalV::ofs_running << std::setiosflags(std::ios::right); @@ -533,5 +527,12 @@ void ESolver_OF::print_info(const bool conv_esolver) /*formats=*/{"%20s", "%20.12f", "%20.12f"}, 0); table << titles << energies_Ry << energies_eV; GlobalV::ofs_running << table.str() << std::endl; + + // reset the iter_time for the next iteration +#ifdef __MPI + this->iter_time = MPI_Wtime(); +#else + this->iter_time = std::chrono::system_clock::now(); +#endif } } // namespace ModuleESolver From 9018250544efa8763c8af7677fcb6b2cf9f9c185 Mon Sep 17 00:00:00 2001 From: sunliang98 <1700011430@pku.edu.cn> Date: Thu, 29 May 2025 19:55:09 +0800 Subject: [PATCH 2/2] Fix: Fix the compile error --- source/module_esolver/esolver_fp.h | 4 ++++ source/module_esolver/esolver_ks.h | 6 ------ source/module_esolver/esolver_of_tool.cpp | 4 ++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/source/module_esolver/esolver_fp.h b/source/module_esolver/esolver_fp.h index c247c444b1..5d8d63e248 100644 --- a/source/module_esolver/esolver_fp.h +++ b/source/module_esolver/esolver_fp.h @@ -3,6 +3,10 @@ #include "esolver.h" +#ifndef __MPI +#include +#endif + //! plane wave basis #include "module_basis/module_pw/pw_basis.h" diff --git a/source/module_esolver/esolver_ks.h b/source/module_esolver/esolver_ks.h index b35429826c..4aba746a8f 100644 --- a/source/module_esolver/esolver_ks.h +++ b/source/module_esolver/esolver_ks.h @@ -1,12 +1,6 @@ #ifndef ESOLVER_KS_H #define ESOLVER_KS_H -#ifdef __MPI -#include -#else -#include -#endif - #include //#include diff --git a/source/module_esolver/esolver_of_tool.cpp b/source/module_esolver/esolver_of_tool.cpp index 41720dc746..39c4e7a691 100644 --- a/source/module_esolver/esolver_of_tool.cpp +++ b/source/module_esolver/esolver_of_tool.cpp @@ -410,8 +410,8 @@ void ESolver_OF::print_info(const bool conv_esolver) = (std::chrono::duration_cast(std::chrono::system_clock::now() - this->iter_time)).count() / static_cast(1e6); #endif - std::cout << " " << std::setw(8) << iteration << std::setw(18) << std::scientific << std::setprecision(8) - << this->energy_current_ * ModuleBase::Ry_to_eV + std::cout << " " << std::setw(8) << iteration + << std::setw(18) << std::scientific << std::setprecision(8) << this->energy_current_ * ModuleBase::Ry_to_eV << std::setw(18) << (this->energy_current_ - this->energy_last_) * ModuleBase::Ry_to_eV << std::setw(13) << std::setprecision(4) << this->pelec->eferm.get_efval(0) * ModuleBase::Ry_to_eV << std::setw(13) << std::setprecision(4) << this->normdLdphi_