From e79c1d06d370d251df53e8dbc0b99d3d890cdad1 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 25 Mar 2025 22:01:35 +0800 Subject: [PATCH 01/35] update some timers --- .../module_basis/module_pw/pw_gatherscatter.h | 38 +++++++++----- source/module_esolver/esolver_fp.h | 2 +- source/module_esolver/esolver_ks.cpp | 18 +++---- source/module_esolver/esolver_ks.h | 8 +-- source/module_esolver/esolver_ks_lcao.cpp | 8 +-- source/module_esolver/esolver_ks_lcao.h | 2 +- .../module_esolver/esolver_ks_lcao_tddft.cpp | 2 +- source/module_esolver/esolver_ks_lcao_tddft.h | 2 +- source/module_esolver/esolver_ks_lcaopw.cpp | 8 +-- source/module_esolver/esolver_ks_lcaopw.h | 2 +- source/module_esolver/esolver_ks_pw.cpp | 6 +-- source/module_esolver/esolver_ks_pw.h | 2 +- source/module_esolver/esolver_sdft_pw.cpp | 8 +-- source/module_esolver/esolver_sdft_pw.h | 2 +- .../hamilt_pwdft/operator_pw/veff_pw.cpp | 52 +++++-------------- source/module_ri/Exx_LRI_interface.h | 4 +- source/module_ri/Exx_LRI_interface.hpp | 2 +- 17 files changed, 76 insertions(+), 90 deletions(-) diff --git a/source/module_basis/module_pw/pw_gatherscatter.h b/source/module_basis/module_pw/pw_gatherscatter.h index 3ce5c6d5a34..bcad2caa0a5 100644 --- a/source/module_basis/module_pw/pw_gatherscatter.h +++ b/source/module_basis/module_pw/pw_gatherscatter.h @@ -14,7 +14,7 @@ namespace ModulePW template void PW_Basis::gatherp_scatters(std::complex* in, std::complex* out) const { - ModuleBase::timer::tick(this->classname, "gatherp_scatters"); + //ModuleBase::timer::tick(this->classname, "gatherp_scatters"); if(this->poolnproc == 1) //In this case nst=nstot, nz = nplane, { @@ -32,7 +32,7 @@ void PW_Basis::gatherp_scatters(std::complex* in, std::complex* out) const outp[iz] = inp[iz]; } } - ModuleBase::timer::tick(this->classname, "gatherp_scatters"); + //ModuleBase::timer::tick(this->classname, "gatherp_scatters"); return; } #ifdef __MPI @@ -55,10 +55,15 @@ void PW_Basis::gatherp_scatters(std::complex* in, std::complex* out) const //exchange data //(nplane,nstot) to (numz[ip],ns, poolnproc) - if(typeid(T) == typeid(double)) - MPI_Alltoallv(out, numr, startr, MPI_DOUBLE_COMPLEX, in, numg, startg, MPI_DOUBLE_COMPLEX, this->pool_world); - else if(typeid(T) == typeid(float)) - MPI_Alltoallv(out, numr, startr, MPI_COMPLEX, in, numg, startg, MPI_COMPLEX, this->pool_world); + if(typeid(T) == typeid(double)) + { + MPI_Alltoallv(out, numr, startr, MPI_DOUBLE_COMPLEX, in, numg, startg, MPI_DOUBLE_COMPLEX, this->pool_world); + } + else if(typeid(T) == typeid(float)) + { + MPI_Alltoallv(out, numr, startr, MPI_COMPLEX, in, numg, startg, MPI_COMPLEX, this->pool_world); + } + // change (nz,ns) to (numz[ip],ns, poolnproc) #ifdef _OPENMP #pragma omp parallel for collapse(2) @@ -80,7 +85,7 @@ void PW_Basis::gatherp_scatters(std::complex* in, std::complex* out) const } #endif - ModuleBase::timer::tick(this->classname, "gatherp_scatters"); + //ModuleBase::timer::tick(this->classname, "gatherp_scatters"); return; } @@ -94,7 +99,7 @@ void PW_Basis::gatherp_scatters(std::complex* in, std::complex* out) const template void PW_Basis::gathers_scatterp(std::complex* in, std::complex* out) const { - ModuleBase::timer::tick(this->classname, "gathers_scatterp"); + //ModuleBase::timer::tick(this->classname, "gathers_scatterp"); if(this->poolnproc == 1) //In this case nrxx=fftnx*fftny*nz, nst = nstot, { @@ -147,10 +152,15 @@ void PW_Basis::gathers_scatterp(std::complex* in, std::complex* out) const //exchange data //(numz[ip],ns, poolnproc) to (nplane,nstot) - if(typeid(T) == typeid(double)) - MPI_Alltoallv(out, numg, startg, MPI_DOUBLE_COMPLEX, in, numr, startr, MPI_DOUBLE_COMPLEX, this->pool_world); - else if(typeid(T) == typeid(float)) - MPI_Alltoallv(out, numg, startg, MPI_COMPLEX, in, numr, startr, MPI_COMPLEX, this->pool_world); + if(typeid(T) == typeid(double)) + { + MPI_Alltoallv(out, numg, startg, MPI_DOUBLE_COMPLEX, in, numr, startr, MPI_DOUBLE_COMPLEX, this->pool_world); + } + else if(typeid(T) == typeid(float)) + { + MPI_Alltoallv(out, numg, startg, MPI_COMPLEX, in, numr, startr, MPI_COMPLEX, this->pool_world); + } + #ifdef _OPENMP #pragma omp parallel for schedule(static, 4096/sizeof(T)) #endif @@ -175,10 +185,10 @@ void PW_Basis::gathers_scatterp(std::complex* in, std::complex* out) const } #endif - ModuleBase::timer::tick(this->classname, "gathers_scatterp"); + //ModuleBase::timer::tick(this->classname, "gathers_scatterp"); return; } -} \ No newline at end of file +} diff --git a/source/module_esolver/esolver_fp.h b/source/module_esolver/esolver_fp.h index 1e7f23c02c7..a4507c7c553 100644 --- a/source/module_esolver/esolver_fp.h +++ b/source/module_esolver/esolver_fp.h @@ -56,7 +56,7 @@ class ESolver_FP: public ESolver //! Something to do after SCF iterations when SCF is converged or comes to the max iter step. virtual void after_scf(UnitCell& ucell, const int istep, const bool conv_esolver); - //! Something to do after hamilt2density function in each iter loop. + //! Something to do after hamilt2rho function in each iter loop. virtual void iter_finish(UnitCell& ucell, const int istep, int& iter, bool &conv_esolver); //! ------------------------------------------------------------------------------ diff --git a/source/module_esolver/esolver_ks.cpp b/source/module_esolver/esolver_ks.cpp index b4e3bcd349e..932da275293 100644 --- a/source/module_esolver/esolver_ks.cpp +++ b/source/module_esolver/esolver_ks.cpp @@ -343,25 +343,25 @@ void ESolver_KS::before_all_runners(UnitCell& ucell, const Input_para } //------------------------------------------------------------------------------ -//! the 5th function of ESolver_KS: hamilt2density_single +//! the 5th function of ESolver_KS: hamilt2rho_single //! mohan add 2024-05-11 //------------------------------------------------------------------------------ template -void ESolver_KS::hamilt2density_single(UnitCell& ucell, const int istep, const int iter, const double ethr) +void ESolver_KS::hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) { - ModuleBase::timer::tick(this->classname, "hamilt2density_single"); + ModuleBase::timer::tick(this->classname, "hamilt2rho_single"); // Temporarily, before HSolver is constructed, it should be overrided by // LCAO, PW, SDFT and TDDFT. // After HSolver is constructed, LCAO, PW, SDFT should delete their own - // hamilt2density_single() and use: - ModuleBase::timer::tick(this->classname, "hamilt2density_single"); + // hamilt2rho_single() and use: + ModuleBase::timer::tick(this->classname, "hamilt2rho_single"); } template -void ESolver_KS::hamilt2density(UnitCell& ucell, const int istep, const int iter, const double ethr) +void ESolver_KS::hamilt2rho(UnitCell& ucell, const int istep, const int iter, const double ethr) { // 7) use Hamiltonian to obtain charge density - this->hamilt2density_single(ucell, istep, iter, diag_ethr); + this->hamilt2rho_single(ucell, istep, iter, diag_ethr); // 8) for MPI: STOGROUP? need to rewrite // It may be changed when more clever parallel algorithm is @@ -398,7 +398,7 @@ void ESolver_KS::hamilt2density(UnitCell& ucell, const int istep, con diag_ethr, PARAM.inp.nelec); - this->hamilt2density_single(ucell, istep, iter, diag_ethr); + this->hamilt2rho_single(ucell, istep, iter, diag_ethr); drho = p_chgmix->get_drho(&this->chr, PARAM.inp.nelec); @@ -458,7 +458,7 @@ void ESolver_KS::runner(UnitCell& ucell, const int istep) this->iter_init(ucell, istep, iter); // 6) use Hamiltonian to obtain charge density - this->hamilt2density(ucell, istep, iter, diag_ethr); + this->hamilt2rho(ucell, istep, iter, diag_ethr); // 7) finish scf iterations this->iter_finish(ucell, istep, iter, conv_esolver); diff --git a/source/module_esolver/esolver_ks.h b/source/module_esolver/esolver_ks.h index 75e6ff91bcd..b0ef3eb279f 100644 --- a/source/module_esolver/esolver_ks.h +++ b/source/module_esolver/esolver_ks.h @@ -48,17 +48,17 @@ class ESolver_KS : public ESolver_FP //! Something to do before SCF iterations. virtual void before_scf(UnitCell& ucell, const int istep) override; - //! Something to do before hamilt2density function in each iter loop. + //! Something to do before hamilt2rho function in each iter loop. virtual void iter_init(UnitCell& ucell, const int istep, const int iter); - //! Something to do after hamilt2density function in each iter loop. + //! Something to do after hamilt2rho function in each iter loop. virtual void iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver) override; // calculate electron density from a specific Hamiltonian with ethr - virtual void hamilt2density_single(UnitCell& ucell, const int istep, const int iter, const double ethr); + virtual void hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr); // calculate electron density from a specific Hamiltonian - void hamilt2density(UnitCell& ucell, const int istep, const int iter, const double ethr); + void hamilt2rho(UnitCell& ucell, const int istep, const int iter, const double ethr); //! Something to do after SCF iterations when SCF is converged or comes to the max iter step. virtual void after_scf(UnitCell& ucell, const int istep, const bool conv_esolver) override; diff --git a/source/module_esolver/esolver_ks_lcao.cpp b/source/module_esolver/esolver_ks_lcao.cpp index 554be9a3c03..9549cc511e9 100644 --- a/source/module_esolver/esolver_ks_lcao.cpp +++ b/source/module_esolver/esolver_ks_lcao.cpp @@ -739,9 +739,9 @@ void ESolver_KS_LCAO::iter_init(UnitCell& ucell, const int istep, const template -void ESolver_KS_LCAO::hamilt2density_single(UnitCell& ucell, int istep, int iter, double ethr) +void ESolver_KS_LCAO::hamilt2rho_single(UnitCell& ucell, int istep, int iter, double ethr) { - ModuleBase::TITLE("ESolver_KS_LCAO", "hamilt2density_single"); + ModuleBase::TITLE("ESolver_KS_LCAO", "hamilt2rho_single"); // i1) reset energy this->pelec->f_en.eband = 0.0; @@ -781,11 +781,11 @@ void ESolver_KS_LCAO::hamilt2density_single(UnitCell& ucell, int istep, { if (GlobalC::exx_info.info_ri.real_number) { - this->exd->exx_hamilt2density(*this->pelec, this->pv, iter); + this->exd->exx_hamilt2rho(*this->pelec, this->pv, iter); } else { - this->exc->exx_hamilt2density(*this->pelec, this->pv, iter); + this->exc->exx_hamilt2rho(*this->pelec, this->pv, iter); } } #endif diff --git a/source/module_esolver/esolver_ks_lcao.h b/source/module_esolver/esolver_ks_lcao.h index 031400b2cc9..774a3bc16ba 100644 --- a/source/module_esolver/esolver_ks_lcao.h +++ b/source/module_esolver/esolver_ks_lcao.h @@ -67,7 +67,7 @@ class ESolver_KS_LCAO : public ESolver_KS virtual void iter_init(UnitCell& ucell, const int istep, const int iter) override; - virtual void hamilt2density_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; + virtual void hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; virtual void update_pot(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver) override; diff --git a/source/module_esolver/esolver_ks_lcao_tddft.cpp b/source/module_esolver/esolver_ks_lcao_tddft.cpp index 5628f5463df..4c38df56442 100644 --- a/source/module_esolver/esolver_ks_lcao_tddft.cpp +++ b/source/module_esolver/esolver_ks_lcao_tddft.cpp @@ -87,7 +87,7 @@ void ESolver_KS_LCAO_TDDFT::before_all_runners(UnitCell& ucell, const In } template -void ESolver_KS_LCAO_TDDFT::hamilt2density_single(UnitCell& ucell, +void ESolver_KS_LCAO_TDDFT::hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) diff --git a/source/module_esolver/esolver_ks_lcao_tddft.h b/source/module_esolver/esolver_ks_lcao_tddft.h index 9232590fbf0..862b16a6ca1 100644 --- a/source/module_esolver/esolver_ks_lcao_tddft.h +++ b/source/module_esolver/esolver_ks_lcao_tddft.h @@ -58,7 +58,7 @@ class ESolver_KS_LCAO_TDDFT : public ESolver_KS_LCAO, doubl void before_all_runners(UnitCell& ucell, const Input_para& inp) override; protected: - virtual void hamilt2density_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; + virtual void hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; virtual void update_pot(UnitCell& ucell, const int istep, const int iter, const bool conv_esolver) override; diff --git a/source/module_esolver/esolver_ks_lcaopw.cpp b/source/module_esolver/esolver_ks_lcaopw.cpp index 8439265bfd1..045ac5d715d 100644 --- a/source/module_esolver/esolver_ks_lcaopw.cpp +++ b/source/module_esolver/esolver_ks_lcaopw.cpp @@ -132,10 +132,10 @@ namespace ModuleESolver } template - void ESolver_KS_LIP::hamilt2density_single(UnitCell& ucell, const int istep, const int iter, const double ethr) + void ESolver_KS_LIP::hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) { - ModuleBase::TITLE("ESolver_KS_LIP", "hamilt2density_single"); - ModuleBase::timer::tick("ESolver_KS_LIP", "hamilt2density_single"); + ModuleBase::TITLE("ESolver_KS_LIP", "hamilt2rho_single"); + ModuleBase::timer::tick("ESolver_KS_LIP", "hamilt2rho_single"); // reset energy this->pelec->f_en.eband = 0.0; @@ -171,7 +171,7 @@ namespace ModuleESolver // need 'rho(out)' and 'vr (v_h(in) and v_xc(in))' this->pelec->f_en.deband = this->pelec->cal_delta_eband(ucell); - ModuleBase::timer::tick("ESolver_KS_LIP", "hamilt2density_single"); + ModuleBase::timer::tick("ESolver_KS_LIP", "hamilt2rho_single"); } template diff --git a/source/module_esolver/esolver_ks_lcaopw.h b/source/module_esolver/esolver_ks_lcaopw.h index c73b74a38cc..74c12db1f80 100644 --- a/source/module_esolver/esolver_ks_lcaopw.h +++ b/source/module_esolver/esolver_ks_lcaopw.h @@ -30,7 +30,7 @@ namespace ModuleESolver virtual void iter_finish(UnitCell& ucell, const int istep, int& iter, bool& conv_esolver) override; /// All the other interfaces except this one are the same as ESolver_KS_PW. - virtual void hamilt2density_single(UnitCell& ucell, + virtual void hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; diff --git a/source/module_esolver/esolver_ks_pw.cpp b/source/module_esolver/esolver_ks_pw.cpp index 4626286801e..8f3822cf529 100644 --- a/source/module_esolver/esolver_ks_pw.cpp +++ b/source/module_esolver/esolver_ks_pw.cpp @@ -450,12 +450,12 @@ void ESolver_KS_PW::iter_init(UnitCell& ucell, const int istep, const // Temporary, it should be replaced by hsolver later. template -void ESolver_KS_PW::hamilt2density_single(UnitCell& ucell, +void ESolver_KS_PW::hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) { - ModuleBase::timer::tick("ESolver_KS_PW", "hamilt2density_single"); + ModuleBase::timer::tick("ESolver_KS_PW", "hamilt2rho_single"); // reset energy this->pelec->f_en.eband = 0.0; @@ -524,7 +524,7 @@ void ESolver_KS_PW::hamilt2density_single(UnitCell& ucell, srho.begin(is, this->chr, this->pw_rhod, ucell.symm); } - ModuleBase::timer::tick("ESolver_KS_PW", "hamilt2density_single"); + ModuleBase::timer::tick("ESolver_KS_PW", "hamilt2rho_single"); } // Temporary, it should be rewritten with Hamilt class. diff --git a/source/module_esolver/esolver_ks_pw.h b/source/module_esolver/esolver_ks_pw.h index d046bd28da1..c1650861771 100644 --- a/source/module_esolver/esolver_ks_pw.h +++ b/source/module_esolver/esolver_ks_pw.h @@ -48,7 +48,7 @@ class ESolver_KS_PW : public ESolver_KS virtual void others(UnitCell& ucell, const int istep) override; - virtual void hamilt2density_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; + virtual void hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; virtual void allocate_hamilt(const UnitCell& ucell); virtual void deallocate_hamilt(); diff --git a/source/module_esolver/esolver_sdft_pw.cpp b/source/module_esolver/esolver_sdft_pw.cpp index d1c9da420ed..85cb3a909f8 100644 --- a/source/module_esolver/esolver_sdft_pw.cpp +++ b/source/module_esolver/esolver_sdft_pw.cpp @@ -132,10 +132,10 @@ void ESolver_SDFT_PW::after_scf(UnitCell& ucell, const int istep, con } template -void ESolver_SDFT_PW::hamilt2density_single(UnitCell& ucell, int istep, int iter, double ethr) +void ESolver_SDFT_PW::hamilt2rho_single(UnitCell& ucell, int istep, int iter, double ethr) { - ModuleBase::TITLE("ESolver_SDFT_PW", "hamilt2density"); - ModuleBase::timer::tick("ESolver_SDFT_PW", "hamilt2density"); + ModuleBase::TITLE("ESolver_SDFT_PW", "hamilt2rho"); + ModuleBase::timer::tick("ESolver_SDFT_PW", "hamilt2rho"); // reset energy this->pelec->f_en.eband = 0.0; @@ -207,7 +207,7 @@ void ESolver_SDFT_PW::hamilt2density_single(UnitCell& ucell, int iste #ifdef __MPI MPI_Bcast(&(this->pelec->f_en.deband), 1, MPI_DOUBLE, 0, BP_WORLD); #endif - ModuleBase::timer::tick("ESolver_SDFT_PW", "hamilt2density"); + ModuleBase::timer::tick("ESolver_SDFT_PW", "hamilt2rho"); } template diff --git a/source/module_esolver/esolver_sdft_pw.h b/source/module_esolver/esolver_sdft_pw.h index 4cb0fe0ddd1..dc284e7b21b 100644 --- a/source/module_esolver/esolver_sdft_pw.h +++ b/source/module_esolver/esolver_sdft_pw.h @@ -35,7 +35,7 @@ class ESolver_SDFT_PW : public ESolver_KS_PW protected: virtual void before_scf(UnitCell& ucell, const int istep) override; - virtual void hamilt2density_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; + virtual void hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; virtual void others(UnitCell& ucell, const int istep) override; diff --git a/source/module_hamilt_pw/hamilt_pwdft/operator_pw/veff_pw.cpp b/source/module_hamilt_pw/hamilt_pwdft/operator_pw/veff_pw.cpp index c931d61f2f8..d6c39132133 100644 --- a/source/module_hamilt_pw/hamilt_pwdft/operator_pw/veff_pw.cpp +++ b/source/module_hamilt_pw/hamilt_pwdft/operator_pw/veff_pw.cpp @@ -12,9 +12,11 @@ Veff>::Veff(const int* isk_in, const int veff_col, const ModulePW::PW_Basis_K* wfcpw_in) { - if (isk_in == nullptr || wfcpw_in == nullptr) { + if (isk_in == nullptr || wfcpw_in == nullptr) + { ModuleBase::WARNING_QUIT("VeffPW", "Constuctor of Operator::VeffPW is failed, please check your code!"); } + this->classname = "Veff"; this->cal_type = calculation_type::pw_veff; this->isk = isk_in; @@ -53,67 +55,41 @@ void Veff>::act( int max_npw = nbasis / npol; const int current_spin = this->isk[this->ik]; + #ifdef __DSP wfcpw->fft_bundle.resource_handler(1); #endif - // T *porter = new T[wfcpw->nmaxgr]; + for (int ib = 0; ib < nbands; ib += npol) { if (npol == 1) { - // wfcpw->recip2real(tmpsi_in, porter, this->ik); wfcpw->recip_to_real(tmpsi_in, this->porter, this->ik); - // NOTICE: when MPI threads are larger than number of Z grids + // NOTICE: when MPI threads are larger than the number of Z grids // veff would contain nothing, and nothing should be done in real space // but the 3DFFT can not be skipped, it will cause hanging if(this->veff_col != 0) { veff_op()(this->ctx, this->veff_col, this->porter, this->veff + current_spin * this->veff_col); - // const Real* current_veff = &(this->veff[0](current_spin, 0)); - // for (int ir = 0; ir < this->veff->nc; ++ir) - // { - // porter[ir] *= current_veff[ir]; - // } } - // wfcpw->real2recip(porter, tmhpsi, this->ik, true); wfcpw->real_to_recip(this->porter, tmhpsi, this->ik, true); - // wfcpw->convolution(this->ctx, - // this->ik, - // this->veff_col, - // tmpsi_in, - // this->veff+current_spin, - // tmhpsi, - // true); } else { - // T *porter1 = new T[wfcpw->nmaxgr]; - // fft to real space and doing things. + // FFT to real space and do things. wfcpw->recip_to_real(tmpsi_in, this->porter, this->ik); wfcpw->recip_to_real(tmpsi_in + max_npw, this->porter1, this->ik); if(this->veff_col != 0) { /// denghui added at 20221109 - const Real* current_veff[4]; - for(int is = 0; is < 4; is++) { - current_veff[is] = this->veff + is * this->veff_col ; // for CPU device - } + const Real* current_veff[4]; + for(int is = 0; is < 4; is++) + { + current_veff[is] = this->veff + is * this->veff_col ; // for CPU device + } veff_op()(this->ctx, this->veff_col, this->porter, this->porter1, current_veff); - // T sup, sdown; - // for (int ir = 0; ir < this->veff_col; ir++) { - // sup = this->porter[ir] * (current_veff[0][ir] + current_veff[3][ir]) - // + this->porter1[ir] - // * (current_veff[1][ir] - // - T(0.0, 1.0) * current_veff[2][ir]); - // sdown = this->porter1[ir] * (current_veff[0][ir] - current_veff[3][ir]) - // + this->porter[ir] - // * (current_veff[1][ir] - // + T(0.0, 1.0) * current_veff[2][ir]); - // this->porter[ir] = sup; - // this->porter1[ir] = sdown; - // } } - // (3) fft back to G space. + // FFT back to G space. wfcpw->real_to_recip(this->porter, tmhpsi, this->ik, true); wfcpw->real_to_recip(this->porter1, tmhpsi + max_npw, this->ik, true); } @@ -154,4 +130,4 @@ template class Veff, base_device::DEVICE_GPU>>; // template Veff, base_device::DEVICE_GPU>>::Veff(const // Veff, base_device::DEVICE_GPU>> *veff); #endif -} // namespace hamilt \ No newline at end of file +} // namespace hamilt diff --git a/source/module_ri/Exx_LRI_interface.h b/source/module_ri/Exx_LRI_interface.h index 9c18ebad84f..82ec91b1817 100644 --- a/source/module_ri/Exx_LRI_interface.h +++ b/source/module_ri/Exx_LRI_interface.h @@ -63,8 +63,8 @@ class Exx_LRI_Interface const K_Vectors& kv, const int& iter); - /// @brief in hamilt2density: calculate Hexx and Eexx - void exx_hamilt2density(elecstate::ElecState& elec, const Parallel_Orbitals& pv, const int iter); + /// @brief in hamilt2rho: calculate Hexx and Eexx + void exx_hamilt2rho(elecstate::ElecState& elec, const Parallel_Orbitals& pv, const int iter); /// @brief in iter_finish: write Hexx, do something according to whether SCF is converged void exx_iter_finish(const K_Vectors& kv, diff --git a/source/module_ri/Exx_LRI_interface.hpp b/source/module_ri/Exx_LRI_interface.hpp index 8eecf44cc99..7c52c8ad04b 100644 --- a/source/module_ri/Exx_LRI_interface.hpp +++ b/source/module_ri/Exx_LRI_interface.hpp @@ -157,7 +157,7 @@ void Exx_LRI_Interface::exx_eachiterinit(const int istep, } template -void Exx_LRI_Interface::exx_hamilt2density(elecstate::ElecState& elec, const Parallel_Orbitals& pv, const int iter) +void Exx_LRI_Interface::exx_hamilt2rho(elecstate::ElecState& elec, const Parallel_Orbitals& pv, const int iter) { // Peize Lin add 2020.04.04 if (XC_Functional::get_func_type() == 4 || XC_Functional::get_func_type() == 5) From 3a8b5206d5291e10677adf6e8b26aa925281fc6b Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 25 Mar 2025 22:18:09 +0800 Subject: [PATCH 02/35] update timer --- source/driver_run.cpp | 2 -- source/module_elecstate/potentials/pot_xc.cpp | 8 +++---- .../potentials/potential_new.cpp | 15 ++++++------- .../hamilt_pwdft/operator_pw/nonlocal_pw.cpp | 13 ++++++------ .../hamilt_pwdft/operator_pw/veff_pw.cpp | 4 ++-- source/module_hsolver/diago_iter_assist.cpp | 8 +++---- source/module_hsolver/hsolver_pw.cpp | 21 +++++-------------- 7 files changed, 30 insertions(+), 41 deletions(-) diff --git a/source/driver_run.cpp b/source/driver_run.cpp index 95066d0fa7c..ecf4f044055 100644 --- a/source/driver_run.cpp +++ b/source/driver_run.cpp @@ -26,7 +26,6 @@ void Driver::driver_run() { ModuleBase::TITLE("Driver", "driver_line"); - ModuleBase::timer::tick("Driver", "driver_line"); //! 1: setup cell and atom information // this warning should not be here, mohan 2024-05-22 @@ -93,6 +92,5 @@ void Driver::driver_run() //! 6: output the json file Json::create_Json(&ucell, PARAM); - ModuleBase::timer::tick("Driver", "driver_line"); return; } diff --git a/source/module_elecstate/potentials/pot_xc.cpp b/source/module_elecstate/potentials/pot_xc.cpp index f6b4607f2d2..907363fb17a 100644 --- a/source/module_elecstate/potentials/pot_xc.cpp +++ b/source/module_elecstate/potentials/pot_xc.cpp @@ -12,8 +12,8 @@ namespace elecstate void PotXC::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, ModuleBase::matrix& v_eff) { - ModuleBase::TITLE("PotXC", "cal_v_eff"); - ModuleBase::timer::tick("PotXC", "cal_v_eff"); + ModuleBase::TITLE("PotXC", "cal_veff"); + ModuleBase::timer::tick("PotXC", "cal_veff"); const int nrxx_current = chg->nrxx; //---------------------------------------------------------- @@ -41,7 +41,7 @@ void PotXC::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, Module *(this->vtxc_) = std::get<1>(etxc_vtxc_v); v_eff += std::get<2>(etxc_vtxc_v); } - ModuleBase::timer::tick("PotXC", "cal_v_eff"); + ModuleBase::timer::tick("PotXC", "cal_veff"); } -} // namespace elecstate \ No newline at end of file +} // namespace elecstate diff --git a/source/module_elecstate/potentials/potential_new.cpp b/source/module_elecstate/potentials/potential_new.cpp index 617e18a1788..4fb30c4b557 100644 --- a/source/module_elecstate/potentials/potential_new.cpp +++ b/source/module_elecstate/potentials/potential_new.cpp @@ -163,7 +163,8 @@ void Potential::allocate() void Potential::update_from_charge(const Charge*const chg, const UnitCell*const ucell) { ModuleBase::TITLE("Potential", "update_from_charge"); - ModuleBase::timer::tick("Potential", "update_from_charge"); + //ModuleBase::timer::tick("Potential", "update_from_charge"); + if (!this->fixed_done) { this->cal_fixed_v(this->v_effective_fixed.data()); @@ -216,13 +217,14 @@ void Potential::update_from_charge(const Charge*const chg, const UnitCell*const } #endif - ModuleBase::timer::tick("Potential", "update_from_charge"); + //ModuleBase::timer::tick("Potential", "update_from_charge"); } void Potential::cal_fixed_v(double* vl_pseudo) { ModuleBase::TITLE("Potential", "cal_fixed_v"); ModuleBase::timer::tick("Potential", "cal_fixed_v"); + this->v_effective_fixed.assign(this->v_effective_fixed.size(), 0.0); for (size_t i = 0; i < this->components.size(); i++) { @@ -237,10 +239,11 @@ void Potential::cal_fixed_v(double* vl_pseudo) void Potential::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, ModuleBase::matrix& v_eff) { - ModuleBase::TITLE("Potential", "cal_v_eff"); + ModuleBase::TITLE("Potential", "cal_veff"); + ModuleBase::timer::tick("Potential", "cal_veff"); + const int nspin_current = this->v_effective.nr; const int nrxx = this->v_effective.nc; - ModuleBase::timer::tick("Potential", "cal_v_eff"); // first of all, set v_effective to zero. this->v_effective.zero_out(); @@ -264,7 +267,7 @@ void Potential::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, Mo } } - ModuleBase::timer::tick("Potential", "cal_v_eff"); + ModuleBase::timer::tick("Potential", "cal_veff"); } void Potential::init_pot(int istep, const Charge*const chg) @@ -278,8 +281,6 @@ void Potential::init_pot(int istep, const Charge*const chg) this->update_from_charge(chg, this->ucell_); - // plots - // figure::picture(this->vr_eff1,GlobalC::rhopw->nx,GlobalC::rhopw->ny,GlobalC::rhopw->nz); ModuleBase::timer::tick("Potential", "init_pot"); return; } diff --git a/source/module_hamilt_pw/hamilt_pwdft/operator_pw/nonlocal_pw.cpp b/source/module_hamilt_pw/hamilt_pwdft/operator_pw/nonlocal_pw.cpp index 74823eccc1a..4386bb7ae36 100644 --- a/source/module_hamilt_pw/hamilt_pwdft/operator_pw/nonlocal_pw.cpp +++ b/source/module_hamilt_pw/hamilt_pwdft/operator_pw/nonlocal_pw.cpp @@ -216,7 +216,7 @@ void Nonlocal>::act( const int ngk_ik, const bool is_first_node)const { - ModuleBase::timer::tick("Operator", "NonlocalPW"); + ModuleBase::timer::tick("Operator", "nonlocal_pw"); if(is_first_node) { setmem_complex_op()(tmhpsi, 0, nbasis*nbands/npol); @@ -232,9 +232,10 @@ void Nonlocal>::act( //<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< // qianrui optimize 2021-3-31 int nkb = this->ppcell->nkb; - if (this->nkb_m < nbands * nkb) { - resmem_complex_op()(this->becp, nbands * nkb, "Nonlocal::becp"); - } + if (this->nkb_m < nbands * nkb) + { + resmem_complex_op()(this->becp, nbands * nkb, "Nonlocal::becp"); + } // ModuleBase::ComplexMatrix becp(nbands, nkb, false); char transa = 'C'; char transb = 'N'; @@ -302,7 +303,7 @@ void Nonlocal>::act( delete[] vnlpsi; #endif } - ModuleBase::timer::tick("Operator", "NonlocalPW"); + ModuleBase::timer::tick("Operator", "nonlocal_pw"); } template @@ -338,4 +339,4 @@ template class Nonlocal, base_device::DEVICE_GPU // Nonlocal, base_device::DEVICE_GPU>>::Nonlocal(const // Nonlocal, base_device::DEVICE_GPU>> *nonlocal); #endif -} // namespace hamilt \ No newline at end of file +} // namespace hamilt diff --git a/source/module_hamilt_pw/hamilt_pwdft/operator_pw/veff_pw.cpp b/source/module_hamilt_pw/hamilt_pwdft/operator_pw/veff_pw.cpp index d6c39132133..5d7f1dcf133 100644 --- a/source/module_hamilt_pw/hamilt_pwdft/operator_pw/veff_pw.cpp +++ b/source/module_hamilt_pw/hamilt_pwdft/operator_pw/veff_pw.cpp @@ -47,7 +47,7 @@ void Veff>::act( const int ngk_ik, const bool is_first_node)const { - ModuleBase::timer::tick("Operator", "VeffPW"); + ModuleBase::timer::tick("Operator", "veff_pw"); if(is_first_node) { setmem_complex_op()(tmhpsi, 0, nbasis*nbands/npol); @@ -99,7 +99,7 @@ void Veff>::act( #ifdef __DSP wfcpw->fft_bundle.resource_handler(0); #endif - ModuleBase::timer::tick("Operator", "VeffPW"); + ModuleBase::timer::tick("Operator", "veff_pw"); } template diff --git a/source/module_hsolver/diago_iter_assist.cpp b/source/module_hsolver/diago_iter_assist.cpp index e39b8bd44b2..afc865f2ef9 100644 --- a/source/module_hsolver/diago_iter_assist.cpp +++ b/source/module_hsolver/diago_iter_assist.cpp @@ -28,8 +28,8 @@ void DiagoIterAssist::diagH_subspace(const hamilt::Hamilt* // of evc, if set to 0, n_band = nstart, default 0 ) { - ModuleBase::TITLE("DiagoIterAssist", "diagH_subspace"); - ModuleBase::timer::tick("DiagoIterAssist", "diagH_subspace"); + ModuleBase::TITLE("DiagoAssist", "diag_subspace"); + ModuleBase::timer::tick("DiagoAssist", "diag_subspace"); // two case: // 1. pw base: nstart = n_band, psi(nbands * npwx) @@ -143,7 +143,7 @@ void DiagoIterAssist::diagH_subspace(const hamilt::Hamilt* delmem_complex_op()(scc); delmem_complex_op()(vcc); - ModuleBase::timer::tick("DiagoIterAssist", "diagH_subspace"); + ModuleBase::timer::tick("DiagoAssist", "diag_subspace"); } template @@ -608,4 +608,4 @@ template class DiagoIterAssist; template class DiagoIterAssist; #endif #endif -} // namespace hsolver \ No newline at end of file +} // namespace hsolver diff --git a/source/module_hsolver/hsolver_pw.cpp b/source/module_hsolver/hsolver_pw.cpp index 62bff8a9158..a8b9c02eecf 100644 --- a/source/module_hsolver/hsolver_pw.cpp +++ b/source/module_hsolver/hsolver_pw.cpp @@ -375,6 +375,7 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, Real* eigenvalue, const int& nk_nums) { + ModuleBase::timer::tick("HSolverPW", "solve_psik"); #ifdef __MPI const diag_comm_info comm_info = {POOL_WORLD, this->rank_in_pool, this->nproc_in_pool}; #else @@ -421,7 +422,6 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, // wrap the hpsi_func and spsi_func into a lambda function auto hpsi_func = [hm, cur_nbasis](const ct::Tensor& psi_in, ct::Tensor& hpsi_out) { - ModuleBase::timer::tick("DiagoCG_New", "hpsi_func"); // psi_in should be a 2D tensor: // psi_in.shape() = [nbands, nbasis] const auto ndim = psi_in.shape().ndim(); @@ -436,10 +436,8 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, using hpsi_info = typename hamilt::Operator::hpsi_info; hpsi_info info(&psi_wrapper, all_bands_range, hpsi_out.data()); hm->ops->hPsi(info); - ModuleBase::timer::tick("DiagoCG_New", "hpsi_func"); }; auto spsi_func = [this, hm](const ct::Tensor& psi_in, ct::Tensor& spsi_out) { - ModuleBase::timer::tick("DiagoCG_New", "spsi_func"); // psi_in should be a 2D tensor: // psi_in.shape() = [nbands, nbasis] const auto ndim = psi_in.shape().ndim(); @@ -462,17 +460,18 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, static_cast((ndim == 1 ? 1 : psi_in.shape().dim_size(0)) * (ndim == 1 ? psi_in.NumElements() : psi_in.shape().dim_size(1)))); } - - ModuleBase::timer::tick("DiagoCG_New", "spsi_func"); }; + auto psi_tensor = ct::TensorMap(psi.get_pointer(), ct::DataTypeToEnum::value, ct::DeviceTypeToEnum::value, ct::TensorShape({psi.get_nbands(), psi.get_nbasis()})); + auto eigen_tensor = ct::TensorMap(eigenvalue, ct::DataTypeToEnum::value, ct::DeviceTypeToEnum::value, ct::TensorShape({psi.get_nbands()})); + auto prec_tensor = ct::TensorMap(pre_condition.data(), ct::DataTypeToEnum::value, ct::DeviceTypeToEnum::value, @@ -491,7 +490,6 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, const int ndim = psi.get_current_ngk(); // hpsi_func (X, HX, ld, nvec) -> HX = H(X), X and HX blockvectors of size ld x nvec auto hpsi_func = [hm, cur_nbasis](T* psi_in, T* hpsi_out, const int ld_psi, const int nvec) { - ModuleBase::timer::tick("diago_bpcg", "hpsi_func"); // Convert "pointer data stucture" to a psi::Psi object auto psi_iter_wrapper = psi::Psi(psi_in, 1, nvec, ld_psi, cur_nbasis); @@ -501,8 +499,6 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, using hpsi_info = typename hamilt::Operator::hpsi_info; hpsi_info info(&psi_iter_wrapper, bands_range, hpsi_out); hm->ops->hPsi(info); - - ModuleBase::timer::tick("diago_bpcg", "hpsi_func"); }; DiagoBPCG bpcg(pre_condition.data()); bpcg.init_iter(PARAM.inp.nbands, nband_l, nbasis, ndim); @@ -512,7 +508,6 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, { // hpsi_func (X, HX, ld, nvec) -> HX = H(X), X and HX blockvectors of size ld x nvec auto hpsi_func = [hm, cur_nbasis](T* psi_in, T* hpsi_out, const int ld_psi, const int nvec) { - ModuleBase::timer::tick("DavSubspace", "hpsi_func"); // Convert "pointer data stucture" to a psi::Psi object auto psi_iter_wrapper = psi::Psi(psi_in, 1, nvec, ld_psi, cur_nbasis); @@ -522,8 +517,6 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, using hpsi_info = typename hamilt::Operator::hpsi_info; hpsi_info info(&psi_iter_wrapper, bands_range, hpsi_out); hm->ops->hPsi(info); - - ModuleBase::timer::tick("DavSubspace", "hpsi_func"); }; bool scf = this->calculation_type == "nscf" ? false : true; @@ -565,7 +558,6 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, /// wrap hpsi into lambda function, Matrix \times blockvector // hpsi_func (X, HX, ld, nvec) -> HX = H(X), X and HX blockvectors of size ld x nvec auto hpsi_func = [hm, cur_nbasis](T* psi_in, T* hpsi_out, const int ld_psi, const int nvec) { - ModuleBase::timer::tick("David", "hpsi_func"); // Convert pointer of psi_in to a psi::Psi object auto psi_iter_wrapper = psi::Psi(psi_in, 1, nvec, ld_psi, cur_nbasis); @@ -575,8 +567,6 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, using hpsi_info = typename hamilt::Operator::hpsi_info; hpsi_info info(&psi_iter_wrapper, bands_range, hpsi_out); hm->ops->hPsi(info); - - ModuleBase::timer::tick("David", "hpsi_func"); }; /// wrap spsi into lambda function, Matrix \times blockvector @@ -587,11 +577,9 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, const int ld_psi, // Leading dimension of psi and spsi. const int nvec // Number of vectors(bands) ) { - ModuleBase::timer::tick("David", "spsi_func"); // sPsi determines S=I or not by PARAM.globalv.use_uspp inside // sPsi(psi, spsi, nrow, npw, nbands) hm->sPsi(psi_in, spsi_out, ld_psi, ld_psi, nvec); - ModuleBase::timer::tick("David", "spsi_func"); }; DiagoDavid david(pre_condition.data(), nband, dim, PARAM.inp.pw_diag_ndim, this->use_paw, comm_info); @@ -606,6 +594,7 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, ntry_max, notconv_max)); } + ModuleBase::timer::tick("HSolverPW", "solve_psik"); return; } From aabb29af155f4dcb582c2bd5a5bf5ef6a579b274 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 25 Mar 2025 22:59:30 +0800 Subject: [PATCH 03/35] update output formats --- source/module_cell/klist.cpp | 4 ++-- source/module_cell/print_cell.cpp | 8 +++---- source/module_cell/unitcell.cpp | 6 ++--- .../module_charge/charge_mixing.cpp | 24 ++++++++++--------- source/module_elecstate/read_pseudo.cpp | 6 ++--- source/module_esolver/esolver.cpp | 2 +- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/source/module_cell/klist.cpp b/source/module_cell/klist.cpp index 3c771d3dc6d..405511e5f79 100644 --- a/source/module_cell/klist.cpp +++ b/source/module_cell/klist.cpp @@ -52,7 +52,7 @@ void K_Vectors::set(const UnitCell& ucell, { ModuleBase::TITLE("K_Vectors", "set"); - ofs << "\n\n\n\n"; + ofs << "\n\n"; ofs << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" << std::endl; ofs << " | |" << std::endl; ofs << " | Setup K-points |" << std::endl; @@ -61,7 +61,7 @@ void K_Vectors::set(const UnitCell& ucell, ofs << " | We treat the spin as another set of k-points. |" << std::endl; ofs << " | |" << std::endl; ofs << " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" << std::endl; - ofs << "\n\n\n\n"; + ofs << "\n\n"; ofs << "\n SETUP K-POINTS" << std::endl; diff --git a/source/module_cell/print_cell.cpp b/source/module_cell/print_cell.cpp index 7864f051041..1177bbdf41b 100644 --- a/source/module_cell/print_cell.cpp +++ b/source/module_cell/print_cell.cpp @@ -23,8 +23,7 @@ namespace unitcell bool direct = (Coordinate == "Direct"); std::string table; table += direct? "DIRECT COORDINATES\n": FmtCore::format("CARTESIAN COORDINATES ( UNIT = %20.12f Bohr ).\n", lat0); - const std::string redundant_header = direct? "taud_": "tauc_"; - table += FmtCore::format("%8s%20s%20s%20s%8s%20s%20s%20s\n", "atom", "x", "y", "z", "mag", "vx", "vy", "vz"); + table += FmtCore::format("%8s%10s%10s%10s%8s%8s%8s%8s\n", "atom", "x", "y", "z", "mag", "vx", "vy", "vz"); for(int it = 0; it < ntype; it++) { for (int ia = 0; ia < atoms[it].na; ia++) @@ -32,8 +31,7 @@ namespace unitcell const double& x = direct? atoms[it].taud[ia].x: atoms[it].tau[ia].x; const double& y = direct? atoms[it].taud[ia].y: atoms[it].tau[ia].y; const double& z = direct? atoms[it].taud[ia].z: atoms[it].tau[ia].z; - table += FmtCore::format("%5s%-s%-5d%20.10f%20.10f%20.10f%8.4f%20.10f%20.10f%20.10f\n", // I dont know why there must be a redundant "tau[c|d]_" in the output. So ugly, it should be removed! - redundant_header, + table += FmtCore::format(" %-s%-5d%10.3f%10.3f%10.3f%8.3f%8.3f%8.3f%8.3f\n", atoms[it].label, ia+1, x, @@ -140,4 +138,4 @@ namespace unitcell ofs.close(); return; } -} \ No newline at end of file +} diff --git a/source/module_cell/unitcell.cpp b/source/module_cell/unitcell.cpp index b5c8c6b8468..5f4dd37c866 100755 --- a/source/module_cell/unitcell.cpp +++ b/source/module_cell/unitcell.cpp @@ -214,7 +214,7 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) { if (ok) { - log << "\n\n\n\n"; + log << "\n\n"; log << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" ">>>>>>>>>>>>" << std::endl; @@ -254,7 +254,7 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) { log << " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" "<<<<<<<<<<<<" << std::endl; - log << "\n\n\n\n"; + log << "\n\n"; log << " READING UNITCELL INFORMATION" << std::endl; //======================== @@ -560,4 +560,4 @@ void UnitCell::compare_atom_labels(std::string label1, std::string label2) { } } } -} \ No newline at end of file +} diff --git a/source/module_elecstate/module_charge/charge_mixing.cpp b/source/module_elecstate/module_charge/charge_mixing.cpp index 0fcc13fc5e7..6f17b84d476 100644 --- a/source/module_elecstate/module_charge/charge_mixing.cpp +++ b/source/module_elecstate/module_charge/charge_mixing.cpp @@ -58,22 +58,24 @@ void Charge_Mixing::set_mixing(const std::string& mixing_mode_in, } // print into running.log - GlobalV::ofs_running<<"\n----------- Double Check Mixing Parameters Begin ------------"<mixing_mode <mixing_beta <mixing_gg0 <mixing_mode); + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "mixing_beta", this->mixing_beta); + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "mixing_gg0", this->mixing_gg0); + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "mixing_gg0_min", PARAM.inp.mixing_gg0_min); + if (PARAM.inp.nspin==2 || PARAM.inp.nspin==4) { - GlobalV::ofs_running<<"mixing_beta_mag: "<< this->mixing_beta_mag <mixing_beta_mag); + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "mixing_gg0_mag", PARAM.inp.mixing_gg0_mag); } if (PARAM.inp.mixing_angle > 0) { - GlobalV::ofs_running<<"mixing_angle: "<< PARAM.inp.mixing_angle <mixing_ndim <mixing_ndim); return; } @@ -240,4 +242,4 @@ bool Charge_Mixing::if_scf_oscillate(const int iteration, const double drho, con } return false; -} \ No newline at end of file +} diff --git a/source/module_elecstate/read_pseudo.cpp b/source/module_elecstate/read_pseudo.cpp index f91073e6c99..7921e43bbe6 100644 --- a/source/module_elecstate/read_pseudo.cpp +++ b/source/module_elecstate/read_pseudo.cpp @@ -11,7 +11,7 @@ namespace elecstate { void read_pseudo(std::ofstream& ofs, UnitCell& ucell) { // read in non-local pseudopotential and ouput the projectors. - ofs << "\n\n\n\n"; + ofs << "\n\n"; ofs << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" ">>>>" << std::endl; @@ -54,7 +54,7 @@ void read_pseudo(std::ofstream& ofs, UnitCell& ucell) { ofs << " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" "<<<<" << std::endl; - ofs << "\n\n\n\n"; + ofs << "\n\n"; read_cell_pseudopots(PARAM.inp.pseudo_dir, ofs, ucell); @@ -368,4 +368,4 @@ void print_unitcell_pseudo(const std::string& fn, UnitCell& ucell) return; } -} \ No newline at end of file +} diff --git a/source/module_esolver/esolver.cpp b/source/module_esolver/esolver.cpp index a727a7a095e..46f4e238401 100644 --- a/source/module_esolver/esolver.cpp +++ b/source/module_esolver/esolver.cpp @@ -95,7 +95,7 @@ std::string determine_type() ModuleBase::WARNING_QUIT("ESolver", "No such esolver_type combined with basis_type"); } - GlobalV::ofs_running << " The esolver type has been set to : " << esolver_type << std::endl; + GlobalV::ofs_running << "\n The esolver type: " << esolver_type << std::endl; auto device_info = PARAM.inp.device; From 931f4ac6dcecd66c848e36c43f634c052c5a99e1 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 26 Mar 2025 12:54:48 +0800 Subject: [PATCH 04/35] update print_cell --- source/module_cell/print_cell.cpp | 3 +-- source/module_cell/test/unitcell_test.cpp | 6 ++++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/source/module_cell/print_cell.cpp b/source/module_cell/print_cell.cpp index 1177bbdf41b..d05136753cc 100644 --- a/source/module_cell/print_cell.cpp +++ b/source/module_cell/print_cell.cpp @@ -31,9 +31,8 @@ namespace unitcell const double& x = direct? atoms[it].taud[ia].x: atoms[it].tau[ia].x; const double& y = direct? atoms[it].taud[ia].y: atoms[it].tau[ia].y; const double& z = direct? atoms[it].taud[ia].z: atoms[it].tau[ia].z; - table += FmtCore::format(" %-s%-5d%10.3f%10.3f%10.3f%8.3f%8.3f%8.3f%8.3f\n", + table += FmtCore::format("%8s%10.3f%10.3f%10.3f%8.3f%8.3f%8.3f%8.3f\n", atoms[it].label, - ia+1, x, y, z, diff --git a/source/module_cell/test/unitcell_test.cpp b/source/module_cell/test/unitcell_test.cpp index 08f764a22c7..7d037422bd5 100644 --- a/source/module_cell/test/unitcell_test.cpp +++ b/source/module_cell/test/unitcell_test.cpp @@ -1018,7 +1018,8 @@ TEST_F(UcellTest, PrintTauDirect) ifs.open("print_tau_direct"); std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); EXPECT_THAT(str, testing::HasSubstr("DIRECT COORDINATES")); - EXPECT_THAT(str, testing::HasSubstr("taud_C1 0.1000000000 0.1000000000 0.1000000000")); + EXPECT_THAT(str, testing::HasSubstr(" C 0.100 0.100 0.100 0.000 0.100 0.100 0.100")); + EXPECT_THAT(str, testing::HasSubstr(" H 0.150 0.150 0.150 0.000 0.100 0.100 0.100")); ifs.close(); remove("print_tau_direct"); } @@ -1036,7 +1037,8 @@ TEST_F(UcellTest, PrintTauCartesian) ifs.open("print_tau_Cartesian"); std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); EXPECT_THAT(str, testing::HasSubstr("CARTESIAN COORDINATES")); - EXPECT_THAT(str, testing::HasSubstr("tauc_C1 1.0000000000 1.0000000000 1.0000000000")); + EXPECT_THAT(str, testing::HasSubstr(" C 1.000 1.000 1.000 0.000 0.000 0.000 0.000")); + EXPECT_THAT(str, testing::HasSubstr(" H 1.500 1.500 1.500 0.000 0.000 0.000 0.000")); ifs.close(); remove("print_tau_Cartesian"); } From 71e555697282555df240101215c14ab596fceaeb Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 26 Mar 2025 13:18:11 +0800 Subject: [PATCH 05/35] add ofs in print_tau --- source/module_cell/print_cell.cpp | 5 +++-- source/module_cell/read_atoms.cpp | 2 +- source/module_cell/test/unitcell_test.cpp | 24 +++++++++++++++++------ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/source/module_cell/print_cell.cpp b/source/module_cell/print_cell.cpp index d05136753cc..221a6c259c3 100644 --- a/source/module_cell/print_cell.cpp +++ b/source/module_cell/print_cell.cpp @@ -11,7 +11,8 @@ namespace unitcell void print_tau(Atom* atoms, const std::string& Coordinate, const int ntype, - const double lat0) + const double lat0, + ofstream &ofs) { ModuleBase::TITLE("UnitCell", "print_tau"); // assert (direct || Coordinate == "Cartesian" || Coordinate == "Cartesian_angstrom"); // this line causes abort in unittest ReadAtomPositionsCACXY. @@ -43,7 +44,7 @@ namespace unitcell } } table += "\n"; - GlobalV::ofs_running << table << std::endl; + ofs << table << std::endl; return; } diff --git a/source/module_cell/read_atoms.cpp b/source/module_cell/read_atoms.cpp index 545ef21a5bc..c5b2d6ccabf 100644 --- a/source/module_cell/read_atoms.cpp +++ b/source/module_cell/read_atoms.cpp @@ -556,7 +556,7 @@ bool read_atom_positions(UnitCell& ucell, if (unitcell::check_tau(ucell.atoms, ucell.ntype, ucell.lat0)) { - unitcell::print_tau(ucell.atoms,ucell.Coordinate,ucell.ntype,ucell.lat0); + unitcell::print_tau(ucell.atoms,ucell.Coordinate,ucell.ntype,ucell.lat0,ofs_running); return true; } return false; diff --git a/source/module_cell/test/unitcell_test.cpp b/source/module_cell/test/unitcell_test.cpp index 7d037422bd5..70d058d090b 100644 --- a/source/module_cell/test/unitcell_test.cpp +++ b/source/module_cell/test/unitcell_test.cpp @@ -1010,10 +1010,14 @@ TEST_F(UcellTest, PrintTauDirect) UcellTestPrepare utp = UcellTestLib["C1H2-Index"]; PARAM.input.relax_new = utp.relax_new; ucell = utp.SetUcellInfo(); - GlobalV::ofs_running.open("print_tau_direct"); EXPECT_EQ(ucell->Coordinate, "Direct"); - unitcell::print_tau(ucell->atoms,ucell->Coordinate,ucell->ntype,ucell->lat0); - GlobalV::ofs_running.close(); + + // open a file + std::ofstream ofs("print_tau_direct"); + unitcell::print_tau(ucell->atoms,ucell->Coordinate,ucell->ntype,ucell->lat0,ofs); + ofs.close(); + + // readin the data std::ifstream ifs; ifs.open("print_tau_direct"); std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); @@ -1021,6 +1025,8 @@ TEST_F(UcellTest, PrintTauDirect) EXPECT_THAT(str, testing::HasSubstr(" C 0.100 0.100 0.100 0.000 0.100 0.100 0.100")); EXPECT_THAT(str, testing::HasSubstr(" H 0.150 0.150 0.150 0.000 0.100 0.100 0.100")); ifs.close(); + + // remove the file remove("print_tau_direct"); } @@ -1029,10 +1035,14 @@ TEST_F(UcellTest, PrintTauCartesian) UcellTestPrepare utp = UcellTestLib["C1H2-Cartesian"]; PARAM.input.relax_new = utp.relax_new; ucell = utp.SetUcellInfo(); - GlobalV::ofs_running.open("print_tau_Cartesian"); EXPECT_EQ(ucell->Coordinate, "Cartesian"); - unitcell::print_tau(ucell->atoms,ucell->Coordinate,ucell->ntype,ucell->lat0); - GlobalV::ofs_running.close(); + + // open a file + std::ofstream ofs("print_tau_Cartesian"); + unitcell::print_tau(ucell->atoms,ucell->Coordinate,ucell->ntype,ucell->lat0,ofs); + ofs.close(); + + // readin the data std::ifstream ifs; ifs.open("print_tau_Cartesian"); std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); @@ -1040,6 +1050,8 @@ TEST_F(UcellTest, PrintTauCartesian) EXPECT_THAT(str, testing::HasSubstr(" C 1.000 1.000 1.000 0.000 0.000 0.000 0.000")); EXPECT_THAT(str, testing::HasSubstr(" H 1.500 1.500 1.500 0.000 0.000 0.000 0.000")); ifs.close(); + + // remove the file remove("print_tau_Cartesian"); } From 4353a55af4a183bc154d6f8b6ac7e0c8ded3dee5 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 26 Mar 2025 13:23:41 +0800 Subject: [PATCH 06/35] update print_tau --- source/module_cell/print_cell.cpp | 2 +- source/module_cell/print_cell.h | 5 +++-- source/module_relax/relax_new/relax.cpp | 4 ++-- source/module_relax/relax_old/ions_move_basic.cpp | 4 ++-- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/source/module_cell/print_cell.cpp b/source/module_cell/print_cell.cpp index 221a6c259c3..86ae13a4c53 100644 --- a/source/module_cell/print_cell.cpp +++ b/source/module_cell/print_cell.cpp @@ -12,7 +12,7 @@ namespace unitcell const std::string& Coordinate, const int ntype, const double lat0, - ofstream &ofs) + std::ofstream &ofs) { ModuleBase::TITLE("UnitCell", "print_tau"); // assert (direct || Coordinate == "Cartesian" || Coordinate == "Cartesian_angstrom"); // this line causes abort in unittest ReadAtomPositionsCACXY. diff --git a/source/module_cell/print_cell.h b/source/module_cell/print_cell.h index d20313f2c92..84cffaf069b 100644 --- a/source/module_cell/print_cell.h +++ b/source/module_cell/print_cell.h @@ -8,7 +8,8 @@ namespace unitcell void print_tau(Atom* atoms, const std::string& Coordinate, const int ntype, - const double lat0); + const double lat0, + std::ofstream &ofs); /** * @brief UnitCell class is too heavy, this function would be moved @@ -40,4 +41,4 @@ namespace unitcell const int& iproc = 0); } -#endif \ No newline at end of file +#endif diff --git a/source/module_relax/relax_new/relax.cpp b/source/module_relax/relax_new/relax.cpp index 8dc081d5f92..5e563322d9f 100644 --- a/source/module_relax/relax_new/relax.cpp +++ b/source/module_relax/relax_new/relax.cpp @@ -637,7 +637,7 @@ void Relax::move_cell_ions(UnitCell& ucell, const bool is_new_dir) unitcell::update_pos_taud(ucell.lat,move_ion,ucell.ntype,ucell.nat,ucell.atoms); // Print the structure file. - unitcell::print_tau(ucell.atoms,ucell.Coordinate,ucell.ntype,ucell.lat0); + unitcell::print_tau(ucell.atoms,ucell.Coordinate,ucell.ntype,ucell.lat0,GlobalV::ofs_running); // ================================================================= // Step 4 : update G,GT and other stuff @@ -699,4 +699,4 @@ void Relax::move_cell_ions(UnitCell& ucell, const bool is_new_dir) unitcell::setup_cell_after_vc(ucell,GlobalV::ofs_running); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "SETUP UNITCELL"); } -} \ No newline at end of file +} diff --git a/source/module_relax/relax_old/ions_move_basic.cpp b/source/module_relax/relax_old/ions_move_basic.cpp index f65c925f2df..a8dabe57437 100644 --- a/source/module_relax/relax_old/ions_move_basic.cpp +++ b/source/module_relax/relax_old/ions_move_basic.cpp @@ -112,7 +112,7 @@ void Ions_Move_Basic::move_atoms(UnitCell &ucell, double *move, double *pos) //-------------------------------------------- // Print out the structure file. //-------------------------------------------- - unitcell::print_tau(ucell.atoms,ucell.Coordinate,ucell.ntype,ucell.lat0); + unitcell::print_tau(ucell.atoms,ucell.Coordinate,ucell.ntype,ucell.lat0,GlobalV::ofs_running); return; } @@ -217,7 +217,7 @@ void Ions_Move_Basic::terminate(const UnitCell &ucell) //----------------------------------------------------------- // Print the structure. //----------------------------------------------------------- - unitcell::print_tau(ucell.atoms,ucell.Coordinate,ucell.ntype,ucell.lat0); + unitcell::print_tau(ucell.atoms,ucell.Coordinate,ucell.ntype,ucell.lat0,GlobalV::ofs_running); return; } From 959fe8bca6778d6421a01f033db78fd0360b7a25 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 26 Mar 2025 14:13:37 +0800 Subject: [PATCH 07/35] update output formats --- .../module_neighbor/sltk_atom_arrange.cpp | 5 ++-- .../module_cell/module_neighbor/sltk_grid.cpp | 9 ++++--- source/module_cell/print_cell.cpp | 2 +- source/module_cell/read_atoms.cpp | 9 ++++--- source/module_elecstate/elecstate_print.cpp | 20 ++++++++++++--- .../module_gint/cal_psir_ylm.cpp | 6 ++--- source/module_io/print_info.cpp | 25 +++++++++++++------ 7 files changed, 52 insertions(+), 24 deletions(-) diff --git a/source/module_cell/module_neighbor/sltk_atom_arrange.cpp b/source/module_cell/module_neighbor/sltk_atom_arrange.cpp index 548389f55fd..b669fa3d4a5 100644 --- a/source/module_cell/module_neighbor/sltk_atom_arrange.cpp +++ b/source/module_cell/module_neighbor/sltk_atom_arrange.cpp @@ -74,8 +74,9 @@ void atom_arrange::search(const bool pbc_flag, ModuleBase::WARNING_QUIT("atom_arrange::search", " search_radius_bohr < 0,forbidden"); } - ModuleBase::GlobalFunc::OUT(ofs_in, "searching radius is (Bohr))", search_radius_bohr); - ModuleBase::GlobalFunc::OUT(ofs_in, "searching radius unit is (Bohr))", ucell.lat0); + ofs_in << " SEARCH ADJACENT ATOMS" << std::endl; + ModuleBase::GlobalFunc::OUT(ofs_in, "searching radius is (Bohr)", search_radius_bohr); + ModuleBase::GlobalFunc::OUT(ofs_in, "searching radius unit is (Bohr)", ucell.lat0); assert(ucell.nat > 0); diff --git a/source/module_cell/module_neighbor/sltk_grid.cpp b/source/module_cell/module_neighbor/sltk_grid.cpp index 4a409940fe1..73ccbcf9894 100644 --- a/source/module_cell/module_neighbor/sltk_grid.cpp +++ b/source/module_cell/module_neighbor/sltk_grid.cpp @@ -195,9 +195,10 @@ void Grid::setMemberVariables(std::ofstream& ofs_in, // output data to ofs } } } - ModuleBase::GlobalFunc::OUT(ofs_in, "Find the coordinate range of the input atom(unit:lat0)."); - ModuleBase::GlobalFunc::OUT(ofs_in, "min_tau", x_min, y_min, z_min); - ModuleBase::GlobalFunc::OUT(ofs_in, "max_tau", x_max, y_max, z_max); + + ofs_in << " RANGE OF ATOMIC COORDINATES (unit: lat0)" << std::endl; + ModuleBase::GlobalFunc::OUT(ofs_in, "smallest coordinates of atoms", x_min, y_min, z_min); + ModuleBase::GlobalFunc::OUT(ofs_in, "largest coordinates of atoms", x_max, y_max, z_max); this->box_edge_length = sradius + 0.1; // To avoid edge cases, the size of the box is slightly increased. @@ -220,7 +221,7 @@ void Grid::setMemberVariables(std::ofstream& ofs_in, // output data to ofs this->box_nx = glayerX + glayerX_minus; this->box_ny = glayerY + glayerY_minus; this->box_nz = glayerZ + glayerZ_minus; - ModuleBase::GlobalFunc::OUT(ofs_in, "BoxNumber", box_nx, box_ny, box_nz); + ModuleBase::GlobalFunc::OUT(ofs_in, "number of needed cells", box_nx, box_ny, box_nz); atoms_in_box.resize(this->box_nx); for (int i = 0; i < this->box_nx; i++) diff --git a/source/module_cell/print_cell.cpp b/source/module_cell/print_cell.cpp index 86ae13a4c53..ecce588e084 100644 --- a/source/module_cell/print_cell.cpp +++ b/source/module_cell/print_cell.cpp @@ -23,7 +23,7 @@ namespace unitcell assert(std::regex_search(Coordinate, pattern)); bool direct = (Coordinate == "Direct"); std::string table; - table += direct? "DIRECT COORDINATES\n": FmtCore::format("CARTESIAN COORDINATES ( UNIT = %20.12f Bohr ).\n", lat0); + table += direct? "DIRECT COORDINATES\n": FmtCore::format(" CARTESIAN COORDINATES ( UNIT = %15.8f Bohr ).\n", lat0); table += FmtCore::format("%8s%10s%10s%10s%8s%8s%8s%8s\n", "atom", "x", "y", "z", "mag", "vx", "vy", "vz"); for(int it = 0; it < ntype; it++) { diff --git a/source/module_cell/read_atoms.cpp b/source/module_cell/read_atoms.cpp index c5b2d6ccabf..b1feb05609f 100644 --- a/source/module_cell/read_atoms.cpp +++ b/source/module_cell/read_atoms.cpp @@ -514,7 +514,9 @@ bool read_atom_positions(UnitCell& ucell, ucell.atoms[it].m_loc_[ia].x = 1.0; ucell.atoms[it].m_loc_[ia].y = 1.0; ucell.atoms[it].m_loc_[ia].z = 1.0; - ucell.atoms[it].mag[ia] = sqrt(pow(ucell.atoms[it].m_loc_[ia].x,2)+pow(ucell.atoms[it].m_loc_[ia].y,2)+pow(ucell.atoms[it].m_loc_[ia].z,2)); + ucell.atoms[it].mag[ia] = sqrt(pow(ucell.atoms[it].m_loc_[ia].x,2) + +pow(ucell.atoms[it].m_loc_[ia].y,2) + +pow(ucell.atoms[it].m_loc_[ia].z,2)); ModuleBase::GlobalFunc::OUT(ofs_running,"Autoset magnetism for this atom", 1.0, 1.0, 1.0); } } @@ -535,15 +537,16 @@ bool read_atom_positions(UnitCell& ucell, // End Autoset magnetization } // end scan_begin -//check if any atom can move in MD + //check if any atom can move in MD if(!ucell.if_atoms_can_move() && PARAM.inp.calculation=="md" && PARAM.inp.esolver_type!="tddft") { ModuleBase::WARNING("read_atoms", "no atom can move in MD!"); return false; } + ofs_running << std::endl; - ModuleBase::GlobalFunc::OUT(ofs_running,"TOTAL ATOM NUMBER",ucell.nat); + ModuleBase::GlobalFunc::OUT(ofs_running,"total number of atoms",ucell.nat); if (ucell.nat == 0) { diff --git a/source/module_elecstate/elecstate_print.cpp b/source/module_elecstate/elecstate_print.cpp index 1e7972d8ecc..aabbdd9297c 100644 --- a/source/module_elecstate/elecstate_print.cpp +++ b/source/module_elecstate/elecstate_print.cpp @@ -273,7 +273,7 @@ void print_band(const ModuleBase::matrix& ekb, { GlobalV::ofs_running << std::setprecision(6); GlobalV::ofs_running << " Energy (eV) & Occupations for spin=" << klist->isk[ik] + 1 - << " K-point=" << ik + 1 << std::endl; + << " k-point=" << ik + 1 << std::endl; GlobalV::ofs_running << std::setiosflags(std::ios::showpoint); for (int ib = 0; ib < PARAM.globalv.nbands_l; ib++) { @@ -317,7 +317,7 @@ void print_etot(const Magnetism& magnet, GlobalV::ofs_running << std::setprecision(12); GlobalV::ofs_running << std::setiosflags(std::ios::right); - GlobalV::ofs_running << "\n Density error is " << scf_thr << std::endl; + GlobalV::ofs_running << " Electron density error is " << scf_thr << std::endl; if (PARAM.inp.basis_type == "pw") { @@ -327,6 +327,7 @@ void print_etot(const Magnetism& magnet, std::vector titles; std::vector energies_Ry; std::vector energies_eV; + if (printe > 0 && ((iter + 1) % printe == 0 || converged || iter == PARAM.inp.scf_nmax)) { int n_order = std::max(0, Occupy::gaussian_type); @@ -384,7 +385,7 @@ void print_etot(const Magnetism& magnet, } #ifdef __DEEPKS - if (PARAM.inp.deepks_scf) // caoyu add 2021-08-10 + if (PARAM.inp.deepks_scf) { titles.push_back("E_DeePKS"); energies_Ry.push_back(elec.f_en.edeepks_delta); @@ -399,6 +400,7 @@ void print_etot(const Magnetism& magnet, energies_Ry.push_back(elec.f_en.etot_harris); } + // print out the Fermi energy if needed if (PARAM.globalv.two_fermi) { titles.push_back("E_Fermi_up"); @@ -411,6 +413,8 @@ void print_etot(const Magnetism& magnet, titles.push_back("E_Fermi"); energies_Ry.push_back(elec.eferm.ef); } + + // print out the band gap if needed if (PARAM.inp.out_bandgap) { if (!PARAM.globalv.two_fermi) @@ -430,13 +434,21 @@ void print_etot(const Magnetism& magnet, std::transform(energies_Ry.begin(), energies_Ry.end(), energies_eV.begin(), [](double ener) { return ener * ModuleBase::Ry_to_eV; }); + + // for each SCF step, we print out energy FmtTable table({"Energy", "Rydberg", "eV"}, titles.size(), {"%-14s", "%20.10f", "%20.10f"}, {FmtTable::Align::LEFT, FmtTable::Align::CENTER}); + + // print out the titles table << titles << energies_Ry << energies_eV; + GlobalV::ofs_running << table.str() << std::endl; - if (PARAM.inp.out_level == "ie" || PARAM.inp.out_level == "m") // xiaohui add 'm' option, 2015-09-16 + + + + if (PARAM.inp.out_level == "ie" || PARAM.inp.out_level == "m") { std::vector mag; switch (PARAM.inp.nspin) diff --git a/source/module_hamilt_lcao/module_gint/cal_psir_ylm.cpp b/source/module_hamilt_lcao/module_gint/cal_psir_ylm.cpp index d1bda7fa088..0eb9b16e6ba 100644 --- a/source/module_hamilt_lcao/module_gint/cal_psir_ylm.cpp +++ b/source/module_hamilt_lcao/module_gint/cal_psir_ylm.cpp @@ -13,7 +13,7 @@ void cal_psir_ylm( const bool* const* const cal_flag, double* const* const psir_ylm) // cal_flag[bxyz][na_grid], whether the atom-grid distance is larger than cutoff { - ModuleBase::timer::tick("Gint_Tools", "cal_psir_ylm"); +// ModuleBase::timer::tick("Gint_Tools", "cal_psir_ylm"); std::vector ylma; const UnitCell& ucell = *gt.ucell; std::vector it_psi_uniform(gt.nwmax); @@ -107,7 +107,7 @@ void cal_psir_ylm( } // end distance<=(rcuts[it]-1.0e-15) } // end ib } // end id - ModuleBase::timer::tick("Gint_Tools", "cal_psir_ylm"); +// ModuleBase::timer::tick("Gint_Tools", "cal_psir_ylm"); return; } -} \ No newline at end of file +} diff --git a/source/module_io/print_info.cpp b/source/module_io/print_info.cpp index 61a5cc3b23a..6c60b7f7b5c 100644 --- a/source/module_io/print_info.cpp +++ b/source/module_io/print_info.cpp @@ -11,8 +11,13 @@ void setup_parameters(UnitCell& ucell, K_Vectors& kv) { ModuleBase::TITLE("ModuleIO", "setup_parameters"); - if(PARAM.inp.calculation=="scf" || PARAM.inp.calculation=="relax" || PARAM.inp.calculation=="cell-relax" || PARAM.inp.calculation=="nscf" - || PARAM.inp.calculation=="get_pchg" || PARAM.inp.calculation=="get_wf" || PARAM.inp.calculation=="md") + if(PARAM.inp.calculation=="scf" + || PARAM.inp.calculation=="relax" + || PARAM.inp.calculation=="cell-relax" + || PARAM.inp.calculation=="nscf" + || PARAM.inp.calculation=="get_pchg" + || PARAM.inp.calculation=="get_wf" + || PARAM.inp.calculation=="md") { std::cout << " ---------------------------------------------------------" << std::endl; if(PARAM.inp.calculation=="scf") @@ -203,7 +208,7 @@ void print_rhofft(ModulePW::PW_Basis* pw_rhod, << std::endl; } - ofs << "\n\n\n\n"; + ofs << "\n\n"; ofs << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" ">>>>" << std::endl; @@ -231,10 +236,13 @@ void print_rhofft(ModulePW::PW_Basis* pw_rhod, ofs << " | is 'npw' in each processor. " " |" << std::endl; + ofs << " | " + " |" + << std::endl; ofs << " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" "<<<<" << std::endl; - ofs << "\n\n\n\n"; + ofs << "\n\n"; ofs << "\n SETUP THE PLANE WAVE BASIS" << std::endl; double ecut = 4 * PARAM.inp.ecutwfc; @@ -317,7 +325,7 @@ void print_rhofft(ModulePW::PW_Basis* pw_rhod, void print_wfcfft(const Input_para& inp, ModulePW::PW_Basis_K& pw_wfc, std::ofstream& ofs) { - ofs << "\n\n\n\n"; + ofs << "\n\n"; ofs << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" ">>>>" << std::endl; @@ -345,10 +353,13 @@ void print_wfcfft(const Input_para& inp, ModulePW::PW_Basis_K& pw_wfc, std::ofst ofs << " | each k-point is 'npwk[ik]' in each processor " " |" << std::endl; + ofs << " | " + " |" + << std::endl; ofs << " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" "<<<<" << std::endl; - ofs << "\n\n\n\n"; + ofs << "\n\n"; ofs << "\n SETUP PLANE WAVES FOR WAVE FUNCTIONS" << std::endl; double ecut = inp.ecutwfc; @@ -424,4 +435,4 @@ void print_screen(const int& stress_step, const int& force_step, const int& iste GlobalV::ofs_running << " -------------------------------------------" << std::endl; } -} // namespace ModuleIO \ No newline at end of file +} // namespace ModuleIO From 9a8552d0f7fe3544f4591ea61d703233c2d61e8a Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 26 Mar 2025 15:29:24 +0800 Subject: [PATCH 08/35] update md outputs --- source/module_base/memory.cpp | 7 ++-- .../hamilt_lcaodft/FORCE_STRESS.cpp | 11 ++---- source/module_io/output_log.cpp | 38 ++++++++++++++----- source/module_md/md_func.cpp | 13 ++++--- 4 files changed, 45 insertions(+), 24 deletions(-) diff --git a/source/module_base/memory.cpp b/source/module_base/memory.cpp index 8d67f9ea4ca..3f8fe287afd 100644 --- a/source/module_base/memory.cpp +++ b/source/module_base/memory.cpp @@ -385,8 +385,9 @@ void Memory::print_all(std::ofstream &ofs) Parallel_Reduce::reduce_all(Memory::total_gpu); #endif #endif - ofs <<"\n NAME-------------------------|MEMORY(MB)--------" << std::endl; - ofs <0); @@ -440,7 +441,7 @@ void Memory::print_all(std::ofstream &ofs) return; } - ofs <<"\n NAME-------------------------|GPU MEMORY(MB)----" << std::endl; + ofs <<"\n NAME-------------------------|GPU MEMORY(MB)--------------" << std::endl; ofs <0); diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.cpp index fd4118f10c0..51eefdaea67 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.cpp @@ -733,19 +733,14 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, GlobalV::ofs_running << std::setiosflags(std::ios::showpos); GlobalV::ofs_running << std::setiosflags(std::ios::fixed) << std::setprecision(10) << std::endl; ModuleIO::print_stress("OVERLAP STRESS", soverlap, PARAM.inp.test_stress, ry); - // test ModuleIO::print_stress("T STRESS", stvnl_dphi, PARAM.inp.test_stress, ry); ModuleIO::print_stress("VNL STRESS", svnl_dbeta, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("T_VNL STRESS", stvnl, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("VL_dPHI STRESS", svl_dphi, PARAM.inp.test_stress, ry); ModuleIO::print_stress("VL_dVL STRESS", sigmadvl, PARAM.inp.test_stress, ry); ModuleIO::print_stress("HAR STRESS", sigmahar, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("EWALD STRESS", sigmaewa, PARAM.inp.test_stress, ry); ModuleIO::print_stress("cc STRESS", sigmacc, PARAM.inp.test_stress, ry); - // ModuleIO::print_stress("NLCC STRESS",sigmacc,PARAM.inp.test_stress,ry); ModuleIO::print_stress("XC STRESS", sigmaxc, PARAM.inp.test_stress, ry); if (vdw_solver != nullptr) { @@ -760,11 +755,13 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, ModuleIO::print_stress("DeltaSpin STRESS", stress_dspin, PARAM.inp.test_stress, ry); } ModuleIO::print_stress("TOTAL STRESS", scs, PARAM.inp.test_stress, ry); - } // end of test + GlobalV::ofs_running << std::setiosflags(std::ios::left); + // print total stress - ModuleIO::print_stress("TOTAL-STRESS", scs, true, ry); + bool screen = false; + ModuleIO::print_stress("TOTAL-STRESS", scs, screen, ry); double unit_transform = 0.0; unit_transform = ModuleBase::RYDBERG_SI / pow(ModuleBase::BOHR_RADIUS_SI, 3) * 1.0e-8; diff --git a/source/module_io/output_log.cpp b/source/module_io/output_log.cpp index 7bf3a08d993..dd7b81ab1c8 100644 --- a/source/module_io/output_log.cpp +++ b/source/module_io/output_log.cpp @@ -223,7 +223,10 @@ void print_force(std::ofstream& ofs_running, std::vector force_y; std::vector force_z; std::string table; - std::vector titles({name, "", "", ""}); + + ofs_running << " " << name << std::endl; + + std::vector titles({"Atoms", "Force_x", "Force_y", "Force_z"}); int iat = 0; for (int it = 0; it < cell.ntype; it++) { @@ -242,13 +245,24 @@ void print_force(std::ofstream& ofs_running, } } - FmtTable fmt(titles, atom_label.size(), {"%10s", "%20.10f", "%20.10f", "%20.10f"}); + // titles: titles for each column + // atom_label.size(): number of lines in this table + // format for each value in the column + FmtTable fmt(titles, + atom_label.size(), + {"%8s", "%20.10f", "%20.10f", "%20.10f"}, + {FmtTable::Align::RIGHT,FmtTable::Align::RIGHT}); + fmt << atom_label << force_x << force_y << force_z; table = fmt.str(); ofs_running << table << std::endl; - if (PARAM.inp.test_force) { std::cout << table << std::endl; -} + + if (PARAM.inp.test_force) + { + std::cout << table << std::endl; + } } + void print_stress(const std::string& name, const ModuleBase::matrix& scs, const bool screen, const bool ry) { const double output_acc = 1.0e-8; @@ -270,7 +284,10 @@ void print_stress(const std::string& name, const ModuleBase::matrix& scs, const std::vector stress_y; std::vector stress_z; std::string table; - std::vector titles({title, "", ""}); + + GlobalV::ofs_running << " " << title << std::endl; + + std::vector titles({"Stress_x", "Stress_y", "Stress_z"}); for (int i = 0; i < 3; i++) { double sx = scs(i, 0) * unit_transform; @@ -283,14 +300,16 @@ void print_stress(const std::string& name, const ModuleBase::matrix& scs, const double pressure = (scs(0, 0) + scs(1, 1) + scs(2, 2)) / 3.0 * unit_transform; - FmtTable fmt(titles, 3, {"%20.10f", "%20.10f", "%20.10f"}); + FmtTable fmt(titles, 3, {"%20.10f", "%20.10f", "%20.10f"}, + {FmtTable::Align::RIGHT,FmtTable::Align::RIGHT}); + fmt << stress_x << stress_y << stress_z; table = fmt.str(); GlobalV::ofs_running << table; if (name == "TOTAL-STRESS") { - GlobalV::ofs_running << " TOTAL-PRESSURE: " << std::fixed << std::setprecision(6) << pressure << unit - << std::endl + GlobalV::ofs_running << " TOTAL-PRESSURE (DO NOT INCLUDE KINETIC PART OF IONS): " << std::fixed + << std::setprecision(6) << pressure << unit << std::endl; } if (screen) @@ -298,7 +317,8 @@ void print_stress(const std::string& name, const ModuleBase::matrix& scs, const std::cout << table; if (name == "TOTAL-STRESS") { - std::cout << " TOTAL-PRESSURE: " << std::fixed << std::setprecision(6) << pressure << unit << std::endl + std::cout << " TOTAL-PRESSURE (DO NOT INCLUDE KINETIC PART OF IONS): " << std::fixed + << std::setprecision(6) << pressure << unit << std::endl; } } diff --git a/source/module_md/md_func.cpp b/source/module_md/md_func.cpp index 07bc1f03f21..3fa000f095f 100644 --- a/source/module_md/md_func.cpp +++ b/source/module_md/md_func.cpp @@ -293,16 +293,18 @@ void print_stress(std::ofstream& ofs, const ModuleBase::matrix& virial, const Mo for (int i = 0; i < 3; i++) { - stress_scalar += stress(i, i) / 3; - virial_scalar += virial(i, i) / 3; + stress_scalar += stress(i, i) / 3.0; + virial_scalar += virial(i, i) / 3.0; } const double unit_transform = ModuleBase::HARTREE_SI / pow(ModuleBase::BOHR_RADIUS_SI, 3) * 1.0e-8; - ofs << "Virtual Pressure is " << stress_scalar * unit_transform << " kbar " << std::endl; - ofs << "Virial Term is " << virial_scalar * unit_transform << " kbar " << std::endl; - ofs << "Kinetic Term is " << (stress_scalar - virial_scalar) * unit_transform << " kbar " << std::endl; + ofs << " MD PRESSURE (ELECTRONS+IONS) : " << stress_scalar * unit_transform << " KBAR" << std::endl; + ofs << " ELECTRONIC PART OF STRESS: " << virial_scalar * unit_transform << " KBAR" << std::endl; + ofs << " IONIC (KINETIC) PART OF STRESS: " << (stress_scalar - virial_scalar) * unit_transform << " KBAR" << std::endl; + // one should use 'print_stress' function in ../source/module_io/output_log.cpp +/* ofs.unsetf(std::ios::fixed); ofs << std::setprecision(8) << std::endl; ModuleBase::GlobalFunc::NEW_PART("MD STRESS (kbar)"); @@ -312,6 +314,7 @@ void print_stress(std::ofstream& ofs, const ModuleBase::matrix& virial, const Mo << std::setw(15) << stress(i, 2) * unit_transform << std::endl; } ofs << std::setiosflags(std::ios::left); +*/ return; } From eb0a3020ee84a6527b9c3ce4a80ec18914109d3c Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 27 Mar 2025 09:40:51 +0800 Subject: [PATCH 09/35] beging modifying the autotests --- tests/integrate/README.md | 7 ++-- tests/integrate/Single_job.sh | 9 +++-- tests/integrate/tools/run_check.sh | 59 +++++++++++++++++------------- 3 files changed, 42 insertions(+), 33 deletions(-) diff --git a/tests/integrate/README.md b/tests/integrate/README.md index 3923895a654..79ffff96d4f 100644 --- a/tests/integrate/README.md +++ b/tests/integrate/README.md @@ -1,9 +1,9 @@ -1, modify file general_info +1. Modify the file general_info -2, try this script for autotest: +2. try this script for autotest: ./Autotest.sh > check.txt -3, if want to focus on No. xxx example, such as 101_PW_OU_PL: +3. if you want to focus on No.xxx example, such as 101_PW_OU_PL: cd 101_PW_OU_PL ./../run_debug.sh $parameter @@ -20,7 +20,6 @@ NOTES: 3, if there is any bug occurs, rename the bug example path to mark it please. ####################### -################## "simple" scripts diff --git a/tests/integrate/Single_job.sh b/tests/integrate/Single_job.sh index 6b8f141d1d2..65815320e60 100755 --- a/tests/integrate/Single_job.sh +++ b/tests/integrate/Single_job.sh @@ -1,9 +1,10 @@ #!/bin/bash -#input parameter: - #none: run and check for this example; - #debug: copy the scripts to debug, then run and check this example; - #other: run this example and generate reference file. +# The note for using the script. +# input parameter: + #'none': run and check for this example; + #'debug': copy scripts to debug, then run and check this example; + #'other': run this example and generate reference file. if test -z $1 then diff --git a/tests/integrate/tools/run_check.sh b/tests/integrate/tools/run_check.sh index 98230887d38..48a5835dc30 100755 --- a/tests/integrate/tools/run_check.sh +++ b/tests/integrate/tools/run_check.sh @@ -21,34 +21,43 @@ check_out(){ done } -test -e ../general_info||echo "plese write the file general_info" -test -e ../general_info||exit 0 +test -e ../general_info|| echo "plese prepare the general_info file." + +test -e ../general_info|| exit 0 + exec_path=`grep EXEC ../general_info|awk '{printf $2}'` + test -e $exec_path || echo "Error! ABACUS path was wrong!!" + test -e $exec_path || exit 0 + CA=`grep CHECKACCURACY ../general_info | awk '{printf $2}'` + NP=`grep NUMBEROFPROCESS ../general_info | awk '{printf $2}'` - path_here=`pwd` - echo "Test in $path_here" - - echo "Begin test with $NP cores" - #parallel test - mpirun -np $NP $exec_path > log.txt - test -d OUT.autotest || echo "Some errors happened in ABACUS!" - test -d OUT.autotest || exit 0 - - #if any input parameters for this script, just generate reference file. - if test -z $1 - then - ../tools/catch_properties.sh result.out - check_out result.out - elif [ $1 == "debug" ] - then - ./catch_properties.sh result.out - check_out result.out - else - ../tools/catch_properties.sh result.ref - rm -r OUT.autotest - rm log.txt - fi +path_here=`pwd` +echo "Test in $path_here" + +echo "Begin testing the example with $NP cores" + +#parallel test +mpirun -np $NP $exec_path > log.txt + +test -d OUT.autotest || echo "Some errors happened in ABACUS!" + +test -d OUT.autotest || exit 0 + +#if any input parameters for this script, just generate reference file. +if test -z $1 +then +../tools/catch_properties.sh result.out +check_out result.out +elif [ $1 == "debug" ] +then +./catch_properties.sh result.out +check_out result.out +else +../tools/catch_properties.sh result.ref +rm -r OUT.autotest +rm log.txt +fi From 2fc86bb3a082aac67bc62a2a8caf508cb473e624 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 27 Mar 2025 10:39:42 +0800 Subject: [PATCH 10/35] update integrate tests --- source/module_cell/read_atoms.cpp | 2 +- tests/integrate/general_info | 3 +++ tests/integrate/tools/catch_properties.sh | 14 +++++++++----- tests/integrate/tools/run_check.sh | 5 +++-- 4 files changed, 16 insertions(+), 8 deletions(-) create mode 100644 tests/integrate/general_info diff --git a/source/module_cell/read_atoms.cpp b/source/module_cell/read_atoms.cpp index b1feb05609f..78f825281f2 100644 --- a/source/module_cell/read_atoms.cpp +++ b/source/module_cell/read_atoms.cpp @@ -546,7 +546,7 @@ bool read_atom_positions(UnitCell& ucell, ofs_running << std::endl; - ModuleBase::GlobalFunc::OUT(ofs_running,"total number of atoms",ucell.nat); + ModuleBase::GlobalFunc::OUT(ofs_running,"TOTAL ATOM NUMBER",ucell.nat); if (ucell.nat == 0) { diff --git a/tests/integrate/general_info b/tests/integrate/general_info new file mode 100644 index 00000000000..d548f755430 --- /dev/null +++ b/tests/integrate/general_info @@ -0,0 +1,3 @@ +EXEC /xxx/abacus +CHECKACCURACY 1.0e-2 +NUMBEROFPROCESS 1 diff --git a/tests/integrate/tools/catch_properties.sh b/tests/integrate/tools/catch_properties.sh index c72afea9b34..c7e16537802 100755 --- a/tests/integrate/tools/catch_properties.sh +++ b/tests/integrate/tools/catch_properties.sh @@ -77,27 +77,31 @@ word="driver_line" symmetry=$(get_input_key_value "symmetry" "INPUT") out_current=$(get_input_key_value "out_current" "INPUT") test -e $1 && rm $1 -#-------------------------------------------- + +#------------------------------------------------------------ # if NOT non-self-consistent calculations or linear response -#-------------------------------------------- +#------------------------------------------------------------ is_lr=0 if [ ! -z $esolver_type ] && ([ $esolver_type == "lr" ] || [ $esolver_type == "ks-lr" ]); then is_lr=1 fi +#---------------------------- +# total energy information +#---------------------------- if [ $calculation != "get_wf" ]\ && [ $calculation != "get_pchg" ] && [ $calculation != "get_S" ]\ && [ $is_lr == 0 ]; then - #etot=`grep ETOT_ $running_path | awk '{print $2}'` etot=$(grep "ETOT_" "$running_path" | tail -1 | awk '{print $2}') etotperatom=`awk 'BEGIN {x='$etot';y='$natom';printf "%.10f\n",x/y}'` echo "etotref $etot" >>$1 echo "etotperatomref $etotperatom" >>$1 fi - -#echo $etot +#---------------------------- +# force information #echo "hasforce:"$has_force +#---------------------------- if ! test -z "$has_force" && [ $has_force == 1 ]; then nn3=`echo "$natom + 1" |bc` #nn1=`echo "$natom + 1" |bc` diff --git a/tests/integrate/tools/run_check.sh b/tests/integrate/tools/run_check.sh index 48a5835dc30..61a4efb6180 100755 --- a/tests/integrate/tools/run_check.sh +++ b/tests/integrate/tools/run_check.sh @@ -1,6 +1,7 @@ #!/bin/bash - +# check_out: checking the output information +# input: result.out check_out(){ outfile=$1 worddir=`awk '{print $1}' $outfile` @@ -43,7 +44,7 @@ echo "Begin testing the example with $NP cores" #parallel test mpirun -np $NP $exec_path > log.txt -test -d OUT.autotest || echo "Some errors happened in ABACUS!" +test -d OUT.autotest || echo "Some errors occured in ABACUS!" test -d OUT.autotest || exit 0 From 5840f5053b9c1b85c8f79d487bcb7e35d150568f Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 27 Mar 2025 11:11:29 +0800 Subject: [PATCH 11/35] update some timers --- source/driver.cpp | 34 ++--- source/driver_run.cpp | 4 +- tests/integrate/tools/catch_properties.sh | 176 +++++++++++++++++----- 3 files changed, 161 insertions(+), 53 deletions(-) diff --git a/source/driver.cpp b/source/driver.cpp index efd76ca276b..ffa2d4c2f63 100644 --- a/source/driver.cpp +++ b/source/driver.cpp @@ -23,24 +23,27 @@ Driver::~Driver() void Driver::init() { - ModuleBase::TITLE("Driver", "init"); + // 1) Let's start by printing a title. + ModuleBase::TITLE("Driver", "ABACUS_begins"); + // 2) Print the current time, since it may run a long time. time_t time_start = std::time(nullptr); ModuleBase::timer::start(); - // (1) read the input parameters. - // INPUT should be initalized here and then pass to atomic world, mohan - // 2024-05-12 INPUT should not be GlobalC, mohan 2024-05-12 - Driver::reading(); - - // (2) welcome to the atomic world! + // 3) Welcome to the atomic world! Let's do some fancy stuff here. this->atomic_world(); - // (3) output information + // 4) All timers recorders are printed. + ModuleBase::timer::finish(GlobalV::ofs_running); + + // 5) All memory recorders are printed. + ModuleBase::Memory::print_all(GlobalV::ofs_running); + + // 6) Print the final time, hopefully it will not cost too long. time_t time_finish = std::time(nullptr); ModuleIO::print_time(time_start, time_finish); - // (4) close all of the running logs + // 7) Clean up: close all of the running logs ModuleBase::Global_File::close_all_log(GlobalV::MY_RANK, PARAM.inp.out_alllog,PARAM.inp.calculation); } @@ -169,16 +172,13 @@ void Driver::reading() void Driver::atomic_world() { ModuleBase::TITLE("Driver", "atomic_world"); - //-------------------------------------------------- - // choose basis sets: - // pw: plane wave basis set - // lcao_in_pw: LCAO expaned by plane wave basis set - // lcao: linear combination of atomic orbitals - //-------------------------------------------------- + ModuleBase::timer::tick("Driver", "atomic_world"); + + // reading information + this->reading(); // where the actual stuff is done this->driver_run(); - ModuleBase::timer::finish(GlobalV::ofs_running); - ModuleBase::Memory::print_all(GlobalV::ofs_running); + ModuleBase::timer::tick("Driver", "atomic_world"); } diff --git a/source/driver_run.cpp b/source/driver_run.cpp index ecf4f044055..2d24af0176d 100644 --- a/source/driver_run.cpp +++ b/source/driver_run.cpp @@ -25,12 +25,13 @@ */ void Driver::driver_run() { - ModuleBase::TITLE("Driver", "driver_line"); + ModuleBase::TITLE("Driver", "driver_run"); //! 1: setup cell and atom information // this warning should not be here, mohan 2024-05-22 #ifndef __LCAO if (PARAM.inp.basis_type == "lcao_in_pw" || PARAM.inp.basis_type == "lcao") { + ModuleBase::timer::tick("Driver","driver_run"); ModuleBase::WARNING_QUIT("driver", "to use LCAO basis, compile with __LCAO"); } @@ -92,5 +93,6 @@ void Driver::driver_run() //! 6: output the json file Json::create_Json(&ucell, PARAM); + return; } diff --git a/tests/integrate/tools/catch_properties.sh b/tests/integrate/tools/catch_properties.sh index c7e16537802..20f18456fbe 100755 --- a/tests/integrate/tools/catch_properties.sh +++ b/tests/integrate/tools/catch_properties.sh @@ -17,9 +17,7 @@ sum_file(){ done echo $sum } -#answer=`sum_file test.txt` -#echo $answer -#exit 0 + get_input_key_value(){ key=$1 @@ -28,8 +26,10 @@ get_input_key_value(){ echo $value } + file=$1 #echo $1 + calculation=`grep calculation INPUT | awk '{print $2}' | sed s/[[:space:]]//g` running_path=`echo "OUT.autotest/running_$calculation"".log"` natom=`grep -En '(^|[[:space:]])TOTAL ATOM NUMBER($|[[:space:]])' $running_path | tail -1 | awk '{print $6}'` @@ -73,7 +73,7 @@ esolver_type=$(get_input_key_value "esolver_type" "INPUT") rdmft=$(get_input_key_value "rdmft" "INPUT") #echo $running_path base=$(get_input_key_value "basis_type" "INPUT") -word="driver_line" +word_total_time="atomic_world" symmetry=$(get_input_key_value "symmetry" "INPUT") out_current=$(get_input_key_value "out_current" "INPUT") test -e $1 && rm $1 @@ -100,23 +100,22 @@ fi #---------------------------- # force information -#echo "hasforce:"$has_force +# echo "hasforce:"$has_force #---------------------------- if ! test -z "$has_force" && [ $has_force == 1 ]; then nn3=`echo "$natom + 1" |bc` - #nn1=`echo "$natom + 1" |bc` - #nn5=`echo "$natom + 6" |bc` - #grep -A$nn3 "TOTAL-FORCE" $running_path|sed '1,5d'|sed ''$nn1','$nn5'd'|awk '{printf $2"\t"$3"\t"$4"\n"}' > force.txt - grep -A$nn3 "TOTAL-FORCE" $running_path |awk 'NF==4{print $2,$3,$4}' | tail -$natom > force.txt #check the last step result + # check the last step result + grep -A$nn3 "TOTAL-FORCE" $running_path |awk 'NF==4{print $2,$3,$4}' | tail -$natom > force.txt total_force=`sum_file force.txt` rm force.txt echo "totalforceref $total_force" >>$1 fi -#echo $total_force -#echo "has_stress:"$has_stress +#------------------------------- +# stress information +# echo "has_stress:"$has_stress +#------------------------------- if ! test -z "$has_stress" && [ $has_stress == 1 ]; then - #grep -A6 "TOTAL-STRESS" $running_path|sed '1,4d'|sed '4,8d' >stress.txt grep -A4 "TOTAL-STRESS" $running_path| awk 'NF==3' | tail -3> stress.txt total_stress=`sum_file stress.txt` rm stress.txt @@ -124,22 +123,20 @@ if ! test -z "$has_stress" && [ $has_stress == 1 ]; then fi -#echo $total_stress -#if ! test -z "$has_charge" && [ $has_charge == 1 ]; then -# total_charge=`sum_file OUT.autotest/SPIN1_CHG` -# echo "totalchargeref $total_charge" >>$1 -#fi - - -#echo $total_charge +#------------------------------- +# DOS information +# echo $total_charge +#------------------------------- if ! test -z "$has_dos" && [ $has_dos == 1 ]; then total_dos=`cat OUT.autotest/DOS1_smearing.dat | awk 'END {print}' | awk '{print $3}'` echo "totaldosref $total_dos" >> $1 fi -# smearing_dos=`sum_file OUT.autotest/DOS1_smearing.dat` -# echo "totaldossmearing $smearing_dos" >> $1 +#smearing_dos=`sum_file OUT.autotest/DOS1_smearing.dat` +#echo "totaldossmearing $smearing_dos" >> $1 -#echo Onsager coefficiency +#------------------------------- +# Onsager coefficiency +#------------------------------- if ! test -z "$has_cond" && [ $has_cond == 1 ]; then onref=refOnsager.txt oncal=Onsager.txt @@ -148,7 +145,9 @@ if ! test -z "$has_cond" && [ $has_cond == 1 ]; then rm -f je-je.txt Chebycoef fi -#echo $out_dm1 +#------------------------------- +# echo $out_dm1 +#------------------------------- if ! test -z "$out_dm1" && [ $out_dm1 == 1 ]; then dm1ref=refdata-DMR-sparse_SPIN0.csr dm1cal=OUT.autotest/data-DMR-sparse_SPIN0.csr @@ -156,7 +155,9 @@ if ! test -z "$out_dm1" && [ $out_dm1 == 1 ]; then echo "CompareDM1_pass $?" >>$1 fi -#echo $out_pot1 +#------------------------------- +# echo $out_pot1 +#------------------------------- if ! test -z "$out_pot" && [ $out_pot == 1 ]; then pot1ref=refSPIN1_POT.cube pot1cal=OUT.autotest/SPIN1_POT.cube @@ -164,7 +165,9 @@ if ! test -z "$out_pot" && [ $out_pot == 1 ]; then echo "ComparePot1_pass $?" >>$1 fi +#------------------------------- #echo $out_pot2 +#------------------------------- if ! test -z "$out_pot" && [ $out_pot == 2 ]; then pot1ref=refElecStaticPot.cube pot1cal=OUT.autotest/ElecStaticPot.cube @@ -172,7 +175,10 @@ if ! test -z "$out_pot" && [ $out_pot == 2 ]; then echo "ComparePot_pass $?" >>$1 fi -#echo $out_elf +#------------------------------- +# Electron localized function +# echo $out_elf +#------------------------------- if ! test -z "$out_elf" && [ $out_elf == 1 ]; then elf1ref=refELF.cube elf1cal=OUT.autotest/ELF.cube @@ -180,7 +186,10 @@ if ! test -z "$out_elf" && [ $out_elf == 1 ]; then echo "ComparePot1_pass $?" >>$1 fi -#echo $get_s +#------------------------------- +# Overlap matrix +# echo $get_s +#------------------------------- if ! test -z "$get_s" && [ $get_s == "get_S" ]; then sref=refSR.csr scal=OUT.autotest/SR.csr @@ -188,7 +197,10 @@ if ! test -z "$get_s" && [ $get_s == "get_S" ]; then echo "CompareS_pass $?" >>$1 fi -#echo $out_pband +#------------------------------- +# Partial band structure +# echo $out_pband +#------------------------------- if ! test -z "$out_pband" && [ $out_pband == 1 ]; then #pbandref=refPBANDS_1 #pbandcal=OUT.autotest/PBANDS_1 @@ -200,7 +212,10 @@ if ! test -z "$out_pband" && [ $out_pband == 1 ]; then echo "CompareOrb_pass $?" >>$1 fi -#echo $toW90 +#------------------------------- +# Wannier90 information +# echo $toW90 +#------------------------------- if ! test -z "$toW90" && [ $toW90 == 1 ]; then amnref=diamond.amn amncal=OUT.autotest/diamond.amn @@ -218,15 +233,23 @@ if ! test -z "$toW90" && [ $toW90 == 1 ]; then echo "CompareEIG_pass $?" >>$1 fi -#echo total_dos -#echo $has_band +#------------------------------- +# Total DOS +# echo total_dos +# echo $has_band +#------------------------------- if ! test -z "$has_band" && [ $has_band == 1 ]; then bandref=refBANDS_1.dat bandcal=OUT.autotest/BANDS_1.dat python3 ../tools/CompareFile.py $bandref $bandcal 8 echo "CompareBand_pass $?" >>$1 fi -#echo $has_hs + + +#-------------------------------- +# Hamiltonian and overlap matrix +# echo $has_hs +#-------------------------------- if ! test -z "$has_hs" && [ $has_hs == 1 ]; then if ! test -z "$gamma_only" && [ $gamma_only == 1 ]; then href=data-0-H.ref @@ -246,6 +269,9 @@ if ! test -z "$has_hs" && [ $has_hs == 1 ]; then echo "CompareS_pass $?" >>$1 fi +#-------------------------------- +# exchange-correlation potential +#-------------------------------- if ! test -z "$has_xc" && [ $has_xc == 1 ]; then if ! test -z "$gamma_only" && [ $gamma_only == 1 ]; then xcref=k-0-Vxc.ref @@ -262,6 +288,9 @@ if ! test -z "$has_xc" && [ $has_xc == 1 ]; then echo "CompareOrbXC_pass $?" >>$1 fi +#-------------------------------- +# exchange-correlation potential +#-------------------------------- if ! test -z "$has_xc2" && [ $has_xc2 == 1 ]; then xc2ref=Vxc_R_spin0.ref xc2cal=OUT.autotest/Vxc_R_spin0.csr @@ -269,6 +298,9 @@ if ! test -z "$has_xc2" && [ $has_xc2 == 1 ]; then echo "CompareVXC_R_pass $?" >>$1 fi +#-------------------------------- +# separate terms in band enegy +#-------------------------------- if ! test -z "$has_eband_separate" && [ $has_eband_separate == 1 ]; then ekref=kinetic_out.ref ekcal=OUT.autotest/kinetic_out.dat @@ -288,6 +320,9 @@ if ! test -z "$has_eband_separate" && [ $has_eband_separate == 1 ]; then echo "CompareOrbVHartree_pass $?" >>$1 fi +#----------------------------------- +# Hamiltonian and overlap matrices +#----------------------------------- #echo $has_hs2 if ! test -z "$has_hs2" && [ $has_hs2 == 1 ]; then #python3 ../tools/CompareFile.py data-HR-sparse_SPIN0.csr.ref OUT.autotest/data-HR-sparse_SPIN0.csr 8 @@ -296,18 +331,27 @@ if ! test -z "$has_hs2" && [ $has_hs2 == 1 ]; then echo "CompareSR_pass $?" >>$1 fi +#----------------------------------- +# matrix +#----------------------------------- #echo $has_mat_r if ! test -z "$has_mat_r" && [ $has_mat_r == 1 ]; then python3 ../tools/CompareFile.py data-rR-sparse.csr.ref OUT.autotest/data-rR-sparse.csr 8 echo "ComparerR_pass $?" >>$1 fi +#----------------------------------- +# matrix +#----------------------------------- #echo $has_mat_t if ! test -z "$has_mat_t" && [ $has_mat_t == 1 ]; then python3 ../tools/CompareFile.py data-TR-sparse_SPIN0.csr.ref OUT.autotest/data-TR-sparse_SPIN0.csr 8 echo "ComparerTR_pass $?" >>$1 fi +#----------------------------------- +# matrix +#----------------------------------- #echo $has_mat_dh if ! test -z "$has_mat_dh" && [ $has_mat_dh == 1 ]; then python3 ../tools/CompareFile.py data-dHRx-sparse_SPIN0.csr.ref OUT.autotest/data-dHRx-sparse_SPIN0.csr 8 @@ -318,7 +362,10 @@ if ! test -z "$has_mat_dh" && [ $has_mat_dh == 1 ]; then echo "ComparerdHRz_pass $?" >>$1 fi +#--------------------------------------- +# SCAN exchange-correlation information #echo $has_scan +#--------------------------------------- if ! test -z "$has_scan" && [ $has_scan == "scan" ] && \ ! test -z "$out_chg" && [ $out_chg == 1 ]; then python3 ../tools/CompareFile.py SPIN1_CHG.cube.ref OUT.autotest/SPIN1_CHG.cube 8 @@ -327,14 +374,17 @@ if ! test -z "$has_scan" && [ $has_scan == "scan" ] && \ echo "SPIN1_TAU.cube_pass $?" >>$1 fi +#--------------------------------------- +# wave functions in real space # echo "$has_wfc_r" ## test out_wfc_r > 0 +#--------------------------------------- if ! test -z "$has_wfc_r" && [ $has_wfc_r == 1 ]; then if [[ ! -f OUT.autotest/running_scf.log ]];then echo "Can't find file OUT.autotest/running_scf.log" exit 1 fi nband=$(grep NBANDS OUT.autotest/running_scf.log|awk '{print $3}') -allgrid=$(grep "fft grid for wave functions" OUT.autotest/running_scf.log | awk -F "[=,\\\[\\\]]" '{print $3*$4*$5}') + allgrid=$(grep "fft grid for wave functions" OUT.autotest/running_scf.log | awk -F "[=,\\\[\\\]]" '{print $3*$4*$5}') for((band=0;band<$nband;band++));do if [[ -f "OUT.autotest/wfc_realspace/wfc_realspace_0_$band" ]];then variance_wfc_r=`sed -n "13,$"p OUT.autotest/wfc_realspace/wfc_realspace_0_$band | \ @@ -348,7 +398,10 @@ allgrid=$(grep "fft grid for wave functions" OUT.autotest/running_scf.log | awk done fi +#-------------------------------------------- +# wave functions in plane wave basis # echo "$has_wfc_pw" ## test out_wfc_pw > 0 +#-------------------------------------------- if ! test -z "$has_wfc_pw" && [ $has_wfc_pw == 1 ]; then if [[ ! -f OUT.autotest/WAVEFUNC1.txt ]];then echo "Can't find file OUT.autotest/WAVEFUNC1.txt" @@ -369,7 +422,11 @@ if ! test -z "$has_wfc_pw" && [ $has_wfc_pw == 1 ]; then }' OUT.autotest/WAVEFUNC1.txt >> $1 fi -# echo "$has_lowf" ## test out_wfc_lcao > 0 + +#-------------------------------------------- +# wave functions in LCAO basis +# echo "$has_lowf" # test out_wfc_lcao > 0 +#-------------------------------------------- if ! test -z "$has_lowf" && [ $has_lowf == 1 ]; then if ! test -z "$gamma_only" && [ $gamma_only == 1 ]; then wfc_cal=OUT.autotest/WFC_NAO_GAMMA1.txt @@ -400,6 +457,9 @@ if ! test -z "$has_lowf" && [ $has_lowf == 1 ]; then echo "Compare_wfc_lcao_pass $?" >>$1 fi +#-------------------------------------------- +# density matrix information +#-------------------------------------------- if ! test -z "$out_dm" && [ $out_dm == 1 ]; then dmfile=OUT.autotest/SPIN1_DM dmref=SPIN1_DM.ref @@ -412,11 +472,18 @@ if ! test -z "$out_dm" && [ $out_dm == 1 ]; then fi fi +#-------------------------------------------- +# mulliken charge +#-------------------------------------------- if ! test -z "$out_mul" && [ $out_mul == 1 ]; then python3 ../tools/CompareFile.py mulliken.txt.ref OUT.autotest/mulliken.txt 4 echo "Compare_mulliken_pass $?" >>$1 fi +#-------------------------------------------- +# obtain wave functions for each electronic +# state +#-------------------------------------------- if [ $calculation == "get_wf" ]; then nfile=0 # envfiles=`ls OUT.autotest/ | grep ENV$` @@ -444,6 +511,11 @@ if [ $calculation == "get_wf" ]; then fi fi + +#-------------------------------------------- +# obtian electron charge density for each +# electronic state +#-------------------------------------------- if [ $calculation == "get_pchg" ]; then nfile=0 # chgfiles=`ls OUT.autotest/ | grep -E '_CHG$'` @@ -471,6 +543,9 @@ if [ $calculation == "get_pchg" ]; then fi fi +#-------------------------------------------- +# implicit solvation model +#-------------------------------------------- if ! test -z "$imp_sol" && [ $imp_sol == 1 ]; then esol_el=`grep E_sol_el $running_path | awk '{print $3}'` esol_cav=`grep E_sol_cav $running_path | awk '{print $3}'` @@ -478,6 +553,9 @@ if ! test -z "$imp_sol" && [ $imp_sol == 1 ]; then echo "esolcavref $esol_cav" >>$1 fi +#-------------------------------------------- +# random phase approximation +#-------------------------------------------- if ! test -z "$run_rpa" && [ $run_rpa == 1 ]; then Etot_without_rpa=`grep Etot_without_rpa log.txt | awk 'BEGIN{FS=":"} {print $2}' ` echo "Etot_without_rpa $Etot_without_rpa" >> $1 @@ -486,6 +564,9 @@ if ! test -z "$run_rpa" && [ $run_rpa == 1 ]; then python3 ../tools/CompareFile.py $onref $oncal 8 fi +#-------------------------------------------- +# deepks +#-------------------------------------------- if ! test -z "$deepks_out_labels" && [ $deepks_out_labels == 1 ]; then sed '/n_des/d' OUT.autotest/deepks_desc.dat > des_tmp.txt total_des=`sum_file des_tmp.txt 5` @@ -505,6 +586,9 @@ if ! test -z "$deepks_out_labels" && [ $deepks_out_labels == 1 ]; then fi fi +#-------------------------------------------- +# band gap information +#-------------------------------------------- if ! test -z "$deepks_bandgap" && [ $deepks_bandgap == 1 ]; then odelta=`python3 get_odelta.py` echo "odelta $odelta" >>$1 @@ -512,6 +596,10 @@ if ! test -z "$deepks_bandgap" && [ $deepks_bandgap == 1 ]; then echo "oprec $oprec" >> $1 fi + +#-------------------------------------------- +# check vdelta in deepks +#-------------------------------------------- if ! test -z "$deepks_v_delta" && [ $deepks_v_delta == 1 ]; then totalh=`python3 get_sum_numpy.py OUT.autotest/deepks_htot.npy ` echo "totalh $totalh" >>$1 @@ -521,6 +609,9 @@ if ! test -z "$deepks_v_delta" && [ $deepks_v_delta == 1 ]; then echo "totalvdp $totalvdp" >> $1 fi +#-------------------------------------------- +# check vdelta in deepks +#-------------------------------------------- if ! test -z "$deepks_v_delta" && [ $deepks_v_delta == 2 ]; then totalh=`python3 get_sum_numpy.py OUT.autotest/deepks_htot.npy ` echo "totalh $totalh" >>$1 @@ -532,6 +623,9 @@ if ! test -z "$deepks_v_delta" && [ $deepks_v_delta == 2 ]; then echo "total_gevdm $total_gevdm" >> $1 fi +#-------------------------------------------- +# check symmetry +#-------------------------------------------- if ! test -z "$symmetry" && [ $symmetry == 1 ]; then pointgroup=`grep 'POINT GROUP' $running_path | tail -n 2 | head -n 1 | awk '{print $4}'` spacegroup=`grep 'SPACE GROUP' $running_path | tail -n 1 | awk '{print $7}'` @@ -541,6 +635,9 @@ if ! test -z "$symmetry" && [ $symmetry == 1 ]; then echo "nksibzref $nksibz" >>$1 fi +#-------------------------------------------- +# check currents in rt-TDDFT +#-------------------------------------------- if ! test -z "$out_current" && [ $out_current ]; then current1ref=refcurrent_total.dat current1cal=OUT.autotest/current_total.dat @@ -548,6 +645,9 @@ if ! test -z "$out_current" && [ $out_current ]; then echo "CompareCurrent_pass $?" >>$1 fi +#-------------------------------------------- +# Linear response function +#-------------------------------------------- if [ $is_lr == 1 ]; then lrns=$(get_input_key_value "lr_nstates" "INPUT") lrns1=`echo "$lrns + 1" |bc` @@ -556,6 +656,9 @@ if [ $is_lr == 1 ]; then echo "totexcitationenergyref $lreig_tot" >>$1 fi +#-------------------------------------------- +# Check RDMFT method +#-------------------------------------------- if ! test -z "$rdmft" && [[ $rdmft == 1 ]]; then echo "" >>$1 echo "The following energy units are in Rydberg:" >>$1 @@ -590,6 +693,9 @@ if ! test -z "$rdmft" && [[ $rdmft == 1 ]]; then echo "" >>$1 fi +#-------------------------------------------- +# Check time information +#-------------------------------------------- #echo $total_band -ttot=`grep $word $running_path | awk '{print $3}'` +ttot=`grep $word_total_time $running_path | awk '{print $3}'` echo "totaltimeref $ttot" >>$1 From 0189b6c1c142e90b8dc301d46ff38432d936df24 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 27 Mar 2025 11:20:11 +0800 Subject: [PATCH 12/35] fix total force and total stress --- tests/integrate/tools/catch_properties.sh | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/integrate/tools/catch_properties.sh b/tests/integrate/tools/catch_properties.sh index 20f18456fbe..b62a348d0de 100755 --- a/tests/integrate/tools/catch_properties.sh +++ b/tests/integrate/tools/catch_properties.sh @@ -103,11 +103,12 @@ fi # echo "hasforce:"$has_force #---------------------------- if ! test -z "$has_force" && [ $has_force == 1 ]; then - nn3=`echo "$natom + 1" |bc` + nn3=`echo "$natom + 3" |bc` + # echo "nn3=$nn3" # check the last step result grep -A$nn3 "TOTAL-FORCE" $running_path |awk 'NF==4{print $2,$3,$4}' | tail -$natom > force.txt total_force=`sum_file force.txt` - rm force.txt + rm force.txt echo "totalforceref $total_force" >>$1 fi @@ -116,7 +117,7 @@ fi # echo "has_stress:"$has_stress #------------------------------- if ! test -z "$has_stress" && [ $has_stress == 1 ]; then - grep -A4 "TOTAL-STRESS" $running_path| awk 'NF==3' | tail -3> stress.txt + grep -A6 "TOTAL-STRESS" $running_path| awk 'NF==3' | tail -3> stress.txt total_stress=`sum_file stress.txt` rm stress.txt echo "totalstressref $total_stress" >>$1 From 34bcd1a27eae20b77b51a29e409e88baa3d111c3 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 27 Mar 2025 11:28:49 +0800 Subject: [PATCH 13/35] update integrate tests --- tests/integrate/general_info | 4 ++-- tests/integrate/tools/run_check.sh | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/integrate/general_info b/tests/integrate/general_info index d548f755430..462616452fb 100644 --- a/tests/integrate/general_info +++ b/tests/integrate/general_info @@ -1,3 +1,3 @@ EXEC /xxx/abacus -CHECKACCURACY 1.0e-2 -NUMBEROFPROCESS 1 +CHECKACCURACY 2 +NUMBEROFPROCESS 4 diff --git a/tests/integrate/tools/run_check.sh b/tests/integrate/tools/run_check.sh index 61a4efb6180..d0066fd1349 100755 --- a/tests/integrate/tools/run_check.sh +++ b/tests/integrate/tools/run_check.sh @@ -6,13 +6,18 @@ check_out(){ outfile=$1 worddir=`awk '{print $1}' $outfile` for word in $worddir; do + # serach result.out and get information cal=`grep "$word" $outfile | awk '{printf "%.'$CA'f\n",$2}'` + # search result.ref and get information ref=`grep "$word" result.ref | awk '{printf "%.'$CA'f\n",$2}'` + # compute the error between 'cal' and 'ref' error=`awk 'BEGIN {x='$ref';y='$cal';printf "%.'$CA'f\n",x-y}'` + # compare the total time if [ $word == "totaltimeref" ]; then echo "$word this-test: $cal ref-1core-test: $ref" break fi + # except the total time, other comparison may lead to 'wrong' results if [ $(echo "$error == 0"|bc) = 0 ]; then echo "----------Wrong!----------" echo "word cal ref error" From 9deffae723cb340462a2d6f5841247b7bca8a14d Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 27 Mar 2025 14:25:35 +0800 Subject: [PATCH 14/35] update print_band --- source/module_elecstate/elecstate_print.cpp | 19 +++++++------ source/module_elecstate/elecstate_print.h | 9 ++++-- .../test/elecstate_print_test.cpp | 28 +++++++++++++------ source/module_esolver/esolver_ks.cpp | 3 +- 4 files changed, 38 insertions(+), 21 deletions(-) diff --git a/source/module_elecstate/elecstate_print.cpp b/source/module_elecstate/elecstate_print.cpp index 89e9ec38aba..da21f82a0d7 100644 --- a/source/module_elecstate/elecstate_print.cpp +++ b/source/module_elecstate/elecstate_print.cpp @@ -250,13 +250,16 @@ void print_band(const ModuleBase::matrix& ekb, const K_Vectors* klist, const int& ik, const int& printe, - const int& iter) + const int& iter, + std::ofstream &ofs) { + const double largest_eig = 1.0e10; + // check the band energy. bool wrong = false; for (int ib = 0; ib < PARAM.globalv.nbands_l; ++ib) { - if (std::abs(ekb(ik, ib)) > 1.0e10) + if (std::abs(ekb(ik, ib)) > largest_eig) { GlobalV::ofs_warning << " ik=" << ik + 1 << " ib=" << ib + 1 << " " << ekb(ik, ib) << " Ry" << std::endl; wrong = true; @@ -271,18 +274,18 @@ void print_band(const ModuleBase::matrix& ekb, { if (printe > 0 && ((iter + 1) % printe == 0)) { - GlobalV::ofs_running << std::setprecision(6); - GlobalV::ofs_running << " Energy (eV) & Occupations for spin=" << klist->isk[ik] + 1 + ofs << std::setprecision(6); + ofs << " Energy (eV) & Occupations for spin=" << klist->isk[ik] + 1 << " k-point=" << ik + 1 << std::endl; - GlobalV::ofs_running << std::setiosflags(std::ios::showpoint); + ofs << std::setiosflags(std::ios::showpoint); for (int ib = 0; ib < PARAM.globalv.nbands_l; ib++) { - GlobalV::ofs_running << " " << std::setw(6) << ib + 1 << std::setw(15) + ofs << " " << std::setw(6) << ib + 1 << std::setw(15) << ekb(ik, ib) * ModuleBase::Ry_to_eV; // for the first electron iteration, we don't have the energy // spectrum, so we can't get the occupations. - GlobalV::ofs_running << std::setw(15) << wg(ik, ib); - GlobalV::ofs_running << std::endl; + ofs << std::setw(15) << wg(ik, ib); + ofs << std::endl; } } } diff --git a/source/module_elecstate/elecstate_print.h b/source/module_elecstate/elecstate_print.h index a20c2278cc3..e1ad2fb284c 100644 --- a/source/module_elecstate/elecstate_print.h +++ b/source/module_elecstate/elecstate_print.h @@ -1,14 +1,17 @@ #ifndef ELECSATE_PRINT_H #define ELECSATE_PRINT_H + #include "module_elecstate/elecstate.h" + namespace elecstate { - void print_band(const ModuleBase::matrix& ekb, + void print_band(const ModuleBase::matrix& ekb, const ModuleBase::matrix& wg, const K_Vectors* klist, const int& ik, const int& printe, - const int& iter); + const int& iter, + std::ofstream &ofs); void print_format(const std::string& name, const double& value); @@ -30,4 +33,4 @@ namespace elecstate const double& avg_iter = 0, bool print = true); } -#endif \ No newline at end of file +#endif diff --git a/source/module_elecstate/test/elecstate_print_test.cpp b/source/module_elecstate/test/elecstate_print_test.cpp index 4904188cf0e..071bf994fa9 100644 --- a/source/module_elecstate/test/elecstate_print_test.cpp +++ b/source/module_elecstate/test/elecstate_print_test.cpp @@ -163,13 +163,16 @@ TEST_F(ElecStatePrintTest, PrintBand) PARAM.input.nbands = 2; PARAM.sys.nbands_l = 2; GlobalV::MY_RANK = 0; - GlobalV::ofs_running.open("test.dat", std::ios::out); - // print eigenvalue - elecstate::print_band(elecstate.ekb,elecstate.wg,elecstate.klist, 0, 1, 0); - GlobalV::ofs_running.close(); + + std::ofstream ofs; + ofs.open("test.dat", std::ios::out); + // print eigenvalues + elecstate::print_band(elecstate.ekb,elecstate.wg,elecstate.klist, 0, 1, 0, ofs); + ofs.close(); + ifs.open("test.dat", std::ios::in); std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); - EXPECT_THAT(str, testing::HasSubstr("Energy (eV) & Occupations for spin=1 K-point=1")); + EXPECT_THAT(str, testing::HasSubstr("Energy (eV) & Occupations for spin=1 k-point=1")); EXPECT_THAT(str, testing::HasSubstr("1 13.6057 0.100000")); EXPECT_THAT(str, testing::HasSubstr("2 27.2114 0.200000")); ifs.close(); @@ -193,12 +196,19 @@ TEST_F(ElecStatePrintTest, PrintBandWarning) { elecstate.ekb(0, 0) = 1.0e11; PARAM.input.nspin = 4; - GlobalV::ofs_running.open("test.dat", std::ios::out); + + std::ofstream ofs; + ofs.open("test.dat", std::ios::out); testing::internal::CaptureStdout(); - EXPECT_EXIT(elecstate::print_band(elecstate.ekb,elecstate.wg,elecstate.klist, 0, 1, 0), ::testing::ExitedWithCode(1), ""); + + EXPECT_EXIT(elecstate::print_band(elecstate.ekb,elecstate.wg,elecstate.klist, 0, 1, 0, ofs), + ::testing::ExitedWithCode(1), ""); + output = testing::internal::GetCapturedStdout(); EXPECT_THAT(output, testing::HasSubstr("Eigenvalues are too large!")); - GlobalV::ofs_running.close(); + + ofs.close(); + std::remove("test.dat"); } @@ -370,4 +380,4 @@ TEST_F(ElecStatePrintTest, PrintEtotColorS4) GlobalV::MY_RANK = 0; elecstate::print_etot(ucell.magnet,elecstate, converged, iter, scf_thr, scf_thr_kin, duration, printe, pw_diag_thr, avg_iter, print); delete elecstate.charge; -} \ No newline at end of file +} diff --git a/source/module_esolver/esolver_ks.cpp b/source/module_esolver/esolver_ks.cpp index 2a26b2c7de7..d47c864baee 100644 --- a/source/module_esolver/esolver_ks.cpp +++ b/source/module_esolver/esolver_ks.cpp @@ -550,7 +550,8 @@ void ESolver_KS::iter_finish(UnitCell& ucell, const int istep, int& i this->pelec->klist, ik, PARAM.inp.printe, - iter); + iter, + GlobalV::ofs_running); } // compute magnetization, only for LSDA(spin==2) From 871896029818e3a5b6319ca9b2011a2b949957da Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 27 Mar 2025 14:34:57 +0800 Subject: [PATCH 15/35] fix print_band --- source/module_elecstate/test/elecstate_print_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/module_elecstate/test/elecstate_print_test.cpp b/source/module_elecstate/test/elecstate_print_test.cpp index 071bf994fa9..4b457f2dc03 100644 --- a/source/module_elecstate/test/elecstate_print_test.cpp +++ b/source/module_elecstate/test/elecstate_print_test.cpp @@ -278,7 +278,7 @@ TEST_F(ElecStatePrintTest, PrintEtot) GlobalV::ofs_running.close(); ifs.open("test.dat", std::ios::in); std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); - EXPECT_THAT(str, testing::HasSubstr("Density error is 0.1")); + EXPECT_THAT(str, testing::HasSubstr("Electron density error is 0.1")); EXPECT_THAT(str, testing::HasSubstr("Error Threshold = 0.1")); EXPECT_THAT(str, testing::HasSubstr("E_KohnSham")); EXPECT_THAT(str, testing::HasSubstr("E_vdwD2")); @@ -320,7 +320,7 @@ TEST_F(ElecStatePrintTest, PrintEtot2) GlobalV::ofs_running.close(); ifs.open("test.dat", std::ios::in); std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); - EXPECT_THAT(str, testing::HasSubstr("Density error is 0.1")); + EXPECT_THAT(str, testing::HasSubstr("Electron density error is 0.1")); EXPECT_THAT(str, testing::HasSubstr("Error Threshold = 0.1")); EXPECT_THAT(str, testing::HasSubstr("E_KohnSham")); EXPECT_THAT(str, testing::HasSubstr("E_Harris")); From 05e9cc75e53fe1668bf77143e02880827b9b37f7 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 27 Mar 2025 17:38:48 +0800 Subject: [PATCH 16/35] update md print out information --- source/module_io/berryphase.cpp | 77 ++----------------------- source/module_io/write_wfc_pw.cpp | 4 +- source/module_md/fire.cpp | 7 ++- source/module_md/md_base.cpp | 10 ++-- source/module_md/md_func.cpp | 6 +- source/module_md/test/fire_test.cpp | 47 +++++++++------ source/module_md/test/langevin_test.cpp | 8 +-- source/module_md/test/md_func_test.cpp | 10 ++-- source/module_md/test/msst_test.cpp | 8 +-- source/module_md/test/nhchain_test.cpp | 11 ++-- source/module_md/test/verlet_test.cpp | 8 +-- 11 files changed, 67 insertions(+), 129 deletions(-) diff --git a/source/module_io/berryphase.cpp b/source/module_io/berryphase.cpp index c27c8b0f628..9efda5a09e9 100644 --- a/source/module_io/berryphase.cpp +++ b/source/module_io/berryphase.cpp @@ -36,7 +36,6 @@ void berryphase::get_occupation_bands() ModuleBase::WARNING_QUIT("berryphase::get_occupation_bands", "not enough bands for berryphase, increase band numbers."); } - // GlobalV::ofs_running << "the berryphase's occ_nbands is " << occ_nbands << std::endl; } #ifdef __LCAO @@ -214,25 +213,6 @@ void berryphase::set_kpoints(const K_Vectors& kv, const int direction) nppstr = mp_z + 1; } - - // test by jingan - /* - GlobalV::ofs_running << "direction is " << direction << std::endl; - GlobalV::ofs_running << "nppstr = " << nppstr << std::endl; - GlobalV::ofs_running << "total std::string is " << total_string << std::endl; - for(int istring = 0; istring < total_string; istring++) - { - GlobalV::ofs_running << " the std::string is " << istring << std::endl; - for(int count = 0; count < nppstr; count++) - { - GlobalV::ofs_running << "(" << kv.kvec_c[ k_index[istring][count] ].x << "," - << kv.kvec_c[ k_index[istring][count] ].y << "," - << kv.kvec_c[ k_index[istring][count] ].z << ")" << std::endl; - } - - } - */ - // test by jingan } #include "../module_base/complexmatrix.h" @@ -251,7 +231,6 @@ double berryphase::stringPhase(const UnitCell& ucell, int ik_2 = 0; ModuleBase::Vector3 G(0.0, 0.0, 0.0); ModuleBase::Vector3 dk = kv.kvec_c[k_index[index_str][1]] - kv.kvec_c[k_index[index_str][0]]; - // GlobalV::ofs_running << "the std::string index is " << index_str << std::endl; for (int k_start = 0; k_start < (nppstr - 1); k_start++) { @@ -351,45 +330,11 @@ double berryphase::stringPhase(const UnitCell& ucell, { if (PARAM.inp.nspin != 4) { - // std::complex my_det = lcao_method.det_berryphase(ik_1,ik_2,dk,nbands); zeta = zeta * lcao_method.det_berryphase(ucell,ik_1, ik_2, dk, nbands, *(this->paraV), psi_in, kv); - // test by jingan - // GlobalV::ofs_running << "methon 1: det = " << my_det << std::endl; - // test by jingan } else { } - - // test by jingan - /* - for (int mb = 0; mb < nbands; mb++) - { - - for (int nb = 0; nb < nbands; nb++) - { - - mat(nb, mb) = lcao_method.unkdotp_LCAO(ik_1,ik_2,nb,mb,dk,kv); - } - } - - std::complex det(1.0,0.0); - int info = 0; - int *ipiv = new int[nbands]; - LapackConnector::zgetrf(nbands, nbands, mat, nbands, ipiv, &info); - for (int ib = 0; ib < nbands; ib++) - { - if (ipiv[ib] != (ib+1)) det = -det * mat(ib,ib); - else det = det * mat(ib,ib); - } - - zeta = zeta*det; - - GlobalV::ofs_running << "methon 2: det = " << det << std::endl; - - delete[] ipiv; - */ - // test by jingan } #endif } @@ -441,9 +386,6 @@ void berryphase::Berry_Phase(const UnitCell& ucell, dtheta = atan2(cphik[istring].imag(), cphik[istring].real()); phik[istring] = (theta0 + dtheta) / (2 * ModuleBase::PI); phik_ave = phik_ave + wistring[istring] * phik[istring]; - // test by jingan - // GlobalV::ofs_running << "phik[" << istring << "] = " << phik[istring] << std::endl; - // test by jingan } if (PARAM.inp.nspin == 1) @@ -466,7 +408,6 @@ void berryphase::Berry_Phase(const UnitCell& ucell, mod_elec_tot = 1; } - // GlobalV::ofs_running << "Berry_Phase end " << std::endl; } void berryphase::Macroscopic_polarization(const UnitCell& ucell, @@ -574,20 +515,16 @@ void berryphase::Macroscopic_polarization(const UnitCell& ucell, polarization_ion[2] = polarization_ion[2] - 2.0 * round(polarization_ion[2] / 2.0); } - // delete[] mod_ion; - // delete[] pdl_ion_R1; - // delete[] pdl_ion_R2; - // delete[] pdl_ion_R3; - // ion polarization end // calculate Macroscopic polarization modulus because berry phase int modulus = 0; - if ((!lodd) && (PARAM.inp.nspin == 1)) { - modulus = 2; - } else { - modulus = 1; - } + if ((!lodd) && (PARAM.inp.nspin == 1)) + { + modulus = 2; + } else { + modulus = 1; + } // test by jingan // GlobalV::ofs_running << "ion polarization end" << std::endl; @@ -736,8 +673,6 @@ void berryphase::Macroscopic_polarization(const UnitCell& ucell, } } - // GlobalV::ofs_running << "the Macroscopic_polarization is over" << std::endl; - return; } diff --git a/source/module_io/write_wfc_pw.cpp b/source/module_io/write_wfc_pw.cpp index d00ca6a41fd..76c461de2b7 100644 --- a/source/module_io/write_wfc_pw.cpp +++ b/source/module_io/write_wfc_pw.cpp @@ -47,9 +47,7 @@ void ModuleIO::write_wfc_pw(const std::string& fn, } } } - // if(GlobalV::MY_RANK!=0) std::cout.clear(); - // std::cout<<"Hello"<* force) void MD_base::print_md(std::ofstream& ofs, const bool& cal_stress) { - if (my_rank) + if (my_rank!=0) { return; } @@ -158,6 +158,7 @@ void MD_base::print_md(std::ofstream& ofs, const bool& cal_stress) press += stress(i, i) / 3; } + // screen output std::cout << " ------------------------------------------------------------------------------------------------" << std::endl; std::cout << " " << std::left << std::setw(20) << "Energy (Ry)" << std::left << std::setw(20) << "Potential (Ry)" @@ -182,9 +183,9 @@ void MD_base::print_md(std::ofstream& ofs, const bool& cal_stress) std::cout << " ------------------------------------------------------------------------------------------------" << std::endl; + // running_log output ofs.unsetf(std::ios::fixed); - ofs << std::setprecision(8) << std::endl; - ofs << std::endl; + ofs << std::setprecision(8); ofs << " ------------------------------------------------------------------------------------------------" << std::endl; ofs << " " << std::left << std::setw(20) << "Energy (Ry)" << std::left << std::setw(20) << "Potential (Ry)" @@ -214,9 +215,6 @@ void MD_base::print_md(std::ofstream& ofs, const bool& cal_stress) MD_func::print_stress(ofs, virial, stress); } - ofs << std::endl; - ofs << std::endl; - return; } diff --git a/source/module_md/md_func.cpp b/source/module_md/md_func.cpp index 3fa000f095f..3c1d80fbe91 100644 --- a/source/module_md/md_func.cpp +++ b/source/module_md/md_func.cpp @@ -299,9 +299,9 @@ void print_stress(std::ofstream& ofs, const ModuleBase::matrix& virial, const Mo const double unit_transform = ModuleBase::HARTREE_SI / pow(ModuleBase::BOHR_RADIUS_SI, 3) * 1.0e-8; - ofs << " MD PRESSURE (ELECTRONS+IONS) : " << stress_scalar * unit_transform << " KBAR" << std::endl; - ofs << " ELECTRONIC PART OF STRESS: " << virial_scalar * unit_transform << " KBAR" << std::endl; - ofs << " IONIC (KINETIC) PART OF STRESS: " << (stress_scalar - virial_scalar) * unit_transform << " KBAR" << std::endl; + ofs << " MD PRESSURE (ELECTRONS+IONS) : " << stress_scalar * unit_transform << " kbar" << std::endl; + ofs << " ELECTRONIC PART OF STRESS: " << virial_scalar * unit_transform << " kbar" << std::endl; + ofs << " IONIC (KINETIC) PART OF STRESS: " << (stress_scalar - virial_scalar) * unit_transform << " kbar" << std::endl; // one should use 'print_stress' function in ../source/module_io/output_log.cpp /* diff --git a/source/module_md/test/fire_test.cpp b/source/module_md/test/fire_test.cpp index 81573498ae6..6365ef2d76a 100644 --- a/source/module_md/test/fire_test.cpp +++ b/source/module_md/test/fire_test.cpp @@ -177,41 +177,54 @@ TEST_F(FIREtest, Restart) TEST_F(FIREtest, PrintMD) { - std::ofstream ofs("running.log"); + std::ofstream ofs("running_fire.log"); mdrun->print_md(ofs, true); ofs.close(); - std::ifstream ifs("running.log"); + std::ifstream ifs("running_fire.log"); std::string output_str; + + // Line 1 getline(ifs, output_str); - getline(ifs, output_str); - getline(ifs, output_str); - EXPECT_THAT( - output_str, + EXPECT_THAT(output_str, testing::HasSubstr( " ------------------------------------------------------------------------------------------------")); + + // Line 2 getline(ifs, output_str); - EXPECT_THAT( - output_str, + EXPECT_THAT(output_str, testing::HasSubstr( " Energy (Ry) Potential (Ry) Kinetic (Ry) Temperature (K) Pressure (kbar) ")); + + // Line 3 getline(ifs, output_str); - EXPECT_THAT( - output_str, + EXPECT_THAT(output_str, testing::HasSubstr( " -0.015365236 -0.023915637 0.0085504016 300 1.0846391 ")); + + // Line 4 getline(ifs, output_str); EXPECT_THAT( output_str, testing::HasSubstr( " ------------------------------------------------------------------------------------------------")); - for (int i = 0; i < 11; ++i) - { - getline(ifs, output_str); - } - EXPECT_THAT(output_str, testing::HasSubstr(" Largest gradient in force is 0.049479926 eV/A")); + + // Line 5 + getline(ifs, output_str); + EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391 kbar")); + + // Line 6 + getline(ifs, output_str); + EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar")); + + // Line 7 getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr(" Threshold is -1 eV/A.")); + EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.83853919 kbar")); + + // Line 8 + getline(ifs, output_str); + EXPECT_THAT(output_str, testing::HasSubstr(" LARGEST FORCE (eV/A) : 0.049479926")); + ifs.close(); - remove("running.log"); + //remove("running_fire.log"); } diff --git a/source/module_md/test/langevin_test.cpp b/source/module_md/test/langevin_test.cpp index a06f279c4f3..baf59d3ba0c 100644 --- a/source/module_md/test/langevin_test.cpp +++ b/source/module_md/test/langevin_test.cpp @@ -163,15 +163,13 @@ TEST_F(Langevin_test, restart) TEST_F(Langevin_test, print_md) { - std::ofstream ofs("running.log"); + std::ofstream ofs("running_langevin.log"); mdrun->print_md(ofs, true); ofs.close(); - std::ifstream ifs("running.log"); + std::ifstream ifs("running_langevin.log"); std::string output_str; getline(ifs, output_str); - getline(ifs, output_str); - getline(ifs, output_str); EXPECT_THAT( output_str, testing::HasSubstr( @@ -192,5 +190,5 @@ TEST_F(Langevin_test, print_md) testing::HasSubstr( " ------------------------------------------------------------------------------------------------")); ifs.close(); - remove("running.log"); + remove("running_langevin.log"); } diff --git a/source/module_md/test/md_func_test.cpp b/source/module_md/test/md_func_test.cpp index 54d3ef3ea4d..ddebc68bbcc 100644 --- a/source/module_md/test/md_func_test.cpp +++ b/source/module_md/test/md_func_test.cpp @@ -385,11 +385,12 @@ TEST_F(MD_func_test, print_stress) std::ifstream ifs("running.log"); std::string output_str; getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr("Virtual Pressure is 0 kbar ")); + EXPECT_THAT(output_str, testing::HasSubstr("MD PRESSURE (ELECTRONS+IONS) : 0 kbar")); getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr("Virial Term is 0 kbar ")); + EXPECT_THAT(output_str, testing::HasSubstr("ELECTRONIC PART OF STRESS: 0 kbar")); getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr("Kinetic Term is 0 kbar ")); + EXPECT_THAT(output_str, testing::HasSubstr("IONIC (KINETIC) PART OF STRESS: 0 kbar")); +/* getline(ifs, output_str); getline(ifs, output_str); getline(ifs, output_str); @@ -409,6 +410,7 @@ TEST_F(MD_func_test, print_stress) EXPECT_THAT(output_str, testing::HasSubstr(" 0 0 0")); getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr(" 0 0 0")); +*/ ifs.close(); remove("running.log"); @@ -438,4 +440,4 @@ TEST_F(MD_func_test, current_step_warning) int istep = 0; double temperature = 0.0; EXPECT_EXIT(MD_func::current_md_info(0, file_dir, istep, temperature), ::testing::ExitedWithCode(1), ""); -} \ No newline at end of file +} diff --git a/source/module_md/test/msst_test.cpp b/source/module_md/test/msst_test.cpp index 27b94e5a2d9..f4cf7805d41 100644 --- a/source/module_md/test/msst_test.cpp +++ b/source/module_md/test/msst_test.cpp @@ -219,15 +219,13 @@ TEST_F(MSST_test, restart) TEST_F(MSST_test, print_md) { - std::ofstream ofs("running.log"); + std::ofstream ofs("running_msst.log"); mdrun->print_md(ofs, true); ofs.close(); - std::ifstream ifs("running.log"); + std::ifstream ifs("running_msst.log"); std::string output_str; getline(ifs, output_str); - getline(ifs, output_str); - getline(ifs, output_str); EXPECT_THAT( output_str, testing::HasSubstr( @@ -248,5 +246,5 @@ TEST_F(MSST_test, print_md) testing::HasSubstr( " ------------------------------------------------------------------------------------------------")); ifs.close(); - remove("running.log"); +// remove("running_msst.log"); } diff --git a/source/module_md/test/nhchain_test.cpp b/source/module_md/test/nhchain_test.cpp index 22f0bbf9a0b..6aaacc9869a 100644 --- a/source/module_md/test/nhchain_test.cpp +++ b/source/module_md/test/nhchain_test.cpp @@ -113,7 +113,6 @@ TEST_F(NHC_test, second_half) { mdrun->first_half(GlobalV::ofs_running); mdrun->second_half(); - ; EXPECT_NEAR(mdrun->pos[0].x, -0.00035596392702161582, doublethreshold); EXPECT_NEAR(mdrun->pos[0].y, 0.00026566987683715606, doublethreshold); @@ -146,7 +145,7 @@ TEST_F(NHC_test, write_restart) { mdrun->first_half(GlobalV::ofs_running); mdrun->second_half(); - ; + mdrun->step_ = 1; mdrun->step_rst_ = 2; mdrun->write_restart(PARAM.sys.global_out_dir); @@ -210,15 +209,13 @@ TEST_F(NHC_test, restart) TEST_F(NHC_test, print_md) { - std::ofstream ofs("running.log"); + std::ofstream ofs("running_nhchain.log"); mdrun->print_md(ofs, true); ofs.close(); - std::ifstream ifs("running.log"); + std::ifstream ifs("running_nhchain.log"); std::string output_str; getline(ifs, output_str); - getline(ifs, output_str); - getline(ifs, output_str); EXPECT_THAT( output_str, testing::HasSubstr( @@ -239,5 +236,5 @@ TEST_F(NHC_test, print_md) testing::HasSubstr( " ------------------------------------------------------------------------------------------------")); ifs.close(); - remove("running.log"); + //remove("running_nhchain.log"); } diff --git a/source/module_md/test/verlet_test.cpp b/source/module_md/test/verlet_test.cpp index 578761dcc5e..8ba61ed5e9e 100644 --- a/source/module_md/test/verlet_test.cpp +++ b/source/module_md/test/verlet_test.cpp @@ -304,15 +304,13 @@ TEST_F(Verlet_test, restart) TEST_F(Verlet_test, print_md) { - std::ofstream ofs("running.log"); + std::ofstream ofs("running_verlet.log"); mdrun->print_md(ofs, true); ofs.close(); - std::ifstream ifs("running.log"); + std::ifstream ifs("running_verlet.log"); std::string output_str; getline(ifs, output_str); - getline(ifs, output_str); - getline(ifs, output_str); EXPECT_THAT( output_str, testing::HasSubstr( @@ -333,5 +331,5 @@ TEST_F(Verlet_test, print_md) testing::HasSubstr( " ------------------------------------------------------------------------------------------------")); ifs.close(); - remove("running.log"); +// remove("running_verlet.log"); } From abc61a0ab2ffe2a8d89804cd3c9d94978b355eb6 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 27 Mar 2025 22:25:04 +0800 Subject: [PATCH 17/35] update print_stress --- source/module_esolver/esolver_dp.cpp | 2 +- source/module_esolver/esolver_lj.cpp | 2 +- .../hamilt_lcaodft/FORCE_STRESS.cpp | 32 ++++++++++--------- .../hamilt_ofdft/of_stress_pw.cpp | 23 ++++++------- .../hamilt_pwdft/stress_pw.cpp | 28 ++++++++-------- .../hamilt_stodft/sto_stress_pw.cpp | 23 ++++++------- source/module_io/output_log.cpp | 13 ++++---- source/module_io/output_log.h | 6 +++- source/module_io/test/outputlog_test.cpp | 30 +++++++++++------ 9 files changed, 89 insertions(+), 70 deletions(-) diff --git a/source/module_esolver/esolver_dp.cpp b/source/module_esolver/esolver_dp.cpp index e1c83b367d4..de642a6598f 100644 --- a/source/module_esolver/esolver_dp.cpp +++ b/source/module_esolver/esolver_dp.cpp @@ -136,7 +136,7 @@ void ESolver_DP::cal_stress(UnitCell& ucell, ModuleBase::matrix& stress) { stress = dp_virial; - ModuleIO::print_stress("TOTAL-STRESS", stress, true, false); + ModuleIO::print_stress("TOTAL-STRESS", stress, true, false, GlobalV::ofs_running); // external stress double unit_transform = ModuleBase::RYDBERG_SI / pow(ModuleBase::BOHR_RADIUS_SI, 3) * 1.0e-8; diff --git a/source/module_esolver/esolver_lj.cpp b/source/module_esolver/esolver_lj.cpp index f4ea687517c..28a4c031258 100644 --- a/source/module_esolver/esolver_lj.cpp +++ b/source/module_esolver/esolver_lj.cpp @@ -104,7 +104,7 @@ void ESolver_LJ::runner(UnitCell& ucell, const int istep) { stress = lj_virial; - ModuleIO::print_stress("TOTAL-STRESS", stress, true, false); + ModuleIO::print_stress("TOTAL-STRESS", stress, true, false, GlobalV::ofs_running); // external stress double unit_transform = ModuleBase::RYDBERG_SI / pow(ModuleBase::BOHR_RADIUS_SI, 3) * 1.0e-8; diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.cpp index 51eefdaea67..d8f5c273251 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/FORCE_STRESS.cpp @@ -729,39 +729,41 @@ void Force_Stress_LCAO::getForceStress(UnitCell& ucell, } } + const bool screen = PARAM.inp.test_stress; + GlobalV::ofs_running << "\n PARTS OF STRESS: " << std::endl; GlobalV::ofs_running << std::setiosflags(std::ios::showpos); GlobalV::ofs_running << std::setiosflags(std::ios::fixed) << std::setprecision(10) << std::endl; - ModuleIO::print_stress("OVERLAP STRESS", soverlap, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("T STRESS", stvnl_dphi, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("VNL STRESS", svnl_dbeta, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("T_VNL STRESS", stvnl, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("VL_dPHI STRESS", svl_dphi, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("VL_dVL STRESS", sigmadvl, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("HAR STRESS", sigmahar, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("EWALD STRESS", sigmaewa, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("cc STRESS", sigmacc, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("XC STRESS", sigmaxc, PARAM.inp.test_stress, ry); + ModuleIO::print_stress("OVERLAP STRESS", soverlap, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("T STRESS", stvnl_dphi, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("VNL STRESS", svnl_dbeta, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("T_VNL STRESS", stvnl, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("VL_dPHI STRESS", svl_dphi, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("VL_dVL STRESS", sigmadvl, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("HAR STRESS", sigmahar, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("EWALD STRESS", sigmaewa, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("cc STRESS", sigmacc, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("XC STRESS", sigmaxc, screen, ry, GlobalV::ofs_running); if (vdw_solver != nullptr) { - ModuleIO::print_stress("VDW STRESS", sigmaxc, PARAM.inp.test_stress, ry); + ModuleIO::print_stress("VDW STRESS", sigmaxc, screen, ry, GlobalV::ofs_running); } if (PARAM.inp.dft_plus_u) { - ModuleIO::print_stress("DFTU STRESS", stress_dftu, PARAM.inp.test_stress, ry); + ModuleIO::print_stress("DFTU STRESS", stress_dftu, screen, ry, GlobalV::ofs_running); } if (PARAM.inp.sc_mag_switch) { - ModuleIO::print_stress("DeltaSpin STRESS", stress_dspin, PARAM.inp.test_stress, ry); + ModuleIO::print_stress("DeltaSpin STRESS", stress_dspin, screen, ry, GlobalV::ofs_running); } - ModuleIO::print_stress("TOTAL STRESS", scs, PARAM.inp.test_stress, ry); + ModuleIO::print_stress("TOTAL STRESS", scs, screen, ry, GlobalV::ofs_running); } // end of test GlobalV::ofs_running << std::setiosflags(std::ios::left); // print total stress bool screen = false; - ModuleIO::print_stress("TOTAL-STRESS", scs, screen, ry); + ModuleIO::print_stress("TOTAL-STRESS", scs, screen, ry, GlobalV::ofs_running); double unit_transform = 0.0; unit_transform = ModuleBase::RYDBERG_SI / pow(ModuleBase::BOHR_RADIUS_SI, 3) * 1.0e-8; diff --git a/source/module_hamilt_pw/hamilt_ofdft/of_stress_pw.cpp b/source/module_hamilt_pw/hamilt_ofdft/of_stress_pw.cpp index c3964be8b36..5f42c126247 100644 --- a/source/module_hamilt_pw/hamilt_ofdft/of_stress_pw.cpp +++ b/source/module_hamilt_pw/hamilt_ofdft/of_stress_pw.cpp @@ -99,22 +99,23 @@ void OF_Stress_PW::cal_stress(ModuleBase::matrix& sigmatot, p_symm->symmetrize_mat3(sigmatot, ucell.lat); } - bool ry = false; - ModuleIO::print_stress("TOTAL-STRESS", sigmatot, true, ry); + const bool ry = false; + const bool screen = PARAM.inp.test_stress; + ModuleIO::print_stress("TOTAL-STRESS", sigmatot, true, ry, GlobalV::ofs_running); - if (PARAM.inp.test_stress) + if (screen) { GlobalV::ofs_running << "\n PARTS OF STRESS: " << std::endl; GlobalV::ofs_running << std::setiosflags(std::ios::showpos); GlobalV::ofs_running << std::setiosflags(std::ios::fixed) << std::setprecision(10) << std::endl; - ModuleIO::print_stress("KINETIC STRESS", sigmakin, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("LOCAL STRESS", sigmaloc, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("HARTREE STRESS", sigmahar, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("NON-LOCAL STRESS", sigmanl, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("XC STRESS", sigmaxc, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("EWALD STRESS", sigmaewa, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("NLCC STRESS", sigmaxcc, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("TOTAL STRESS", sigmatot, PARAM.inp.test_stress, ry); + ModuleIO::print_stress("KINETIC STRESS", sigmakin, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("LOCAL STRESS", sigmaloc, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("HARTREE STRESS", sigmahar, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("NON-LOCAL STRESS", sigmanl, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("XC STRESS", sigmaxc, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("EWALD STRESS", sigmaewa, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("NLCC STRESS", sigmaxcc, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("TOTAL STRESS", sigmatot, screen, ry, GlobalV::ofs_running); } ModuleBase::timer::tick("OF_Stress_PW", "cal_stress"); return; diff --git a/source/module_hamilt_pw/hamilt_pwdft/stress_pw.cpp b/source/module_hamilt_pw/hamilt_pwdft/stress_pw.cpp index 827eed53ae8..98cb66b2dcc 100644 --- a/source/module_hamilt_pw/hamilt_pwdft/stress_pw.cpp +++ b/source/module_hamilt_pw/hamilt_pwdft/stress_pw.cpp @@ -145,31 +145,31 @@ void Stress_PW::cal_stress(ModuleBase::matrix& sigmatot, p_symm->symmetrize_mat3(sigmatot, ucell.lat); } - bool ry = false; - ModuleIO::print_stress("TOTAL-STRESS", sigmatot, true, ry); + const bool ry = false; + const bool screen = PARAM.inp.test_stress; + ModuleIO::print_stress("TOTAL-STRESS", sigmatot, true, ry, GlobalV::ofs_running); - if (PARAM.inp.test_stress || true) + if (screen) { -// ry = true; GlobalV::ofs_running << "\n PARTS OF STRESS: " << std::endl; GlobalV::ofs_running << std::setiosflags(std::ios::showpos); GlobalV::ofs_running << std::setiosflags(std::ios::fixed) << std::setprecision(10) << std::endl; - ModuleIO::print_stress("KINETIC STRESS", sigmakin, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("LOCAL STRESS", sigmaloc, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("HARTREE STRESS", sigmahar, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("NON-LOCAL STRESS", sigmanl, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("XC STRESS", sigmaxc, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("EWALD STRESS", sigmaewa, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("NLCC STRESS", sigmaxcc, PARAM.inp.test_stress, ry); + ModuleIO::print_stress("KINETIC STRESS", sigmakin, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("LOCAL STRESS", sigmaloc, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("HARTREE STRESS", sigmahar, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("NON-LOCAL STRESS", sigmanl, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("XC STRESS", sigmaxc, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("EWALD STRESS", sigmaewa, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("NLCC STRESS", sigmaxcc, screen, ry, GlobalV::ofs_running); if (PARAM.inp.dft_plus_u || PARAM.inp.sc_mag_switch) { - ModuleIO::print_stress("ONSITE STRESS", sigmaonsite, PARAM.inp.test_stress, ry); + ModuleIO::print_stress("ONSITE STRESS", sigmaonsite, screen, ry, GlobalV::ofs_running); } if (GlobalC::exx_info.info_global.cal_exx) { - ModuleIO::print_stress("EXX STRESS", sigmaexx, PARAM.inp.test_stress, ry); + ModuleIO::print_stress("EXX STRESS", sigmaexx, screen, ry, GlobalV::ofs_running); } - ModuleIO::print_stress("TOTAL STRESS", sigmatot, PARAM.inp.test_stress, ry); + ModuleIO::print_stress("TOTAL STRESS", sigmatot, screen, ry, GlobalV::ofs_running); } ModuleBase::timer::tick("Stress_PW", "cal_stress"); return; diff --git a/source/module_hamilt_pw/hamilt_stodft/sto_stress_pw.cpp b/source/module_hamilt_pw/hamilt_stodft/sto_stress_pw.cpp index 4d77635139d..033a94159dc 100644 --- a/source/module_hamilt_pw/hamilt_stodft/sto_stress_pw.cpp +++ b/source/module_hamilt_pw/hamilt_stodft/sto_stress_pw.cpp @@ -77,22 +77,23 @@ void Sto_Stress_PW::cal_stress(ModuleBase::matrix& sigmatot, } bool ry = false; - ModuleIO::print_stress("TOTAL-STRESS", sigmatot, true, ry); + const bool screen = PARAM.inp.test_stress; + ModuleIO::print_stress("TOTAL-STRESS", sigmatot, true, ry, GlobalV::ofs_running); - if (PARAM.inp.test_stress) + if (screen) { ry = true; GlobalV::ofs_running << "\n PARTS OF STRESS: " << std::endl; GlobalV::ofs_running << std::setiosflags(std::ios::showpos); GlobalV::ofs_running << std::setiosflags(std::ios::fixed) << std::setprecision(10) << std::endl; - ModuleIO::print_stress("KINETIC STRESS", sigmakin, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("LOCAL STRESS", sigmaloc, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("HARTREE STRESS", sigmahar, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("NON-LOCAL STRESS", sigmanl, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("XC STRESS", sigmaxc, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("EWALD STRESS", sigmaewa, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("NLCC STRESS", sigmaxcc, PARAM.inp.test_stress, ry); - ModuleIO::print_stress("TOTAL STRESS", sigmatot, PARAM.inp.test_stress, ry); + ModuleIO::print_stress("KINETIC STRESS", sigmakin, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("LOCAL STRESS", sigmaloc, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("HARTREE STRESS", sigmahar, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("NON-LOCAL STRESS", sigmanl, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("XC STRESS", sigmaxc, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("EWALD STRESS", sigmaewa, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("NLCC STRESS", sigmaxcc, screen, ry, GlobalV::ofs_running); + ModuleIO::print_stress("TOTAL STRESS", sigmatot, screen, ry, GlobalV::ofs_running); } ModuleBase::timer::tick("Sto_Stress_PW", "cal_stress"); return; @@ -237,4 +238,4 @@ void Sto_Stress_PW::sto_stress_nl(ModuleBase::matrix& sigma, template class Sto_Stress_PW; #if ((defined __CUDA) || (defined __ROCM)) template class Sto_Stress_PW; -#endif \ No newline at end of file +#endif diff --git a/source/module_io/output_log.cpp b/source/module_io/output_log.cpp index 1bb46328b78..496e7f53013 100644 --- a/source/module_io/output_log.cpp +++ b/source/module_io/output_log.cpp @@ -262,7 +262,8 @@ void print_force(std::ofstream& ofs_running, } } -void print_stress(const std::string& name, const ModuleBase::matrix& scs, const bool screen, const bool ry) +void print_stress(const std::string& name, const ModuleBase::matrix& scs, + const bool screen, const bool ry, std::ofstream &ofs) { const double output_acc = 1.0e-8; double unit_transform = 1; @@ -284,7 +285,7 @@ void print_stress(const std::string& name, const ModuleBase::matrix& scs, const std::vector stress_z; std::string table; - GlobalV::ofs_running << " " << title << std::endl; + ofs << " " << title << std::endl; std::vector titles({"Stress_x", "Stress_y", "Stress_z"}); for (int i = 0; i < 3; i++) @@ -306,10 +307,10 @@ void print_stress(const std::string& name, const ModuleBase::matrix& scs, const fmt << stress_x << stress_y << stress_z; table = fmt.str(); - GlobalV::ofs_running << table; + ofs << table; if (name == "TOTAL-STRESS") { - GlobalV::ofs_running << " TOTAL-PRESSURE (DO NOT INCLUDE KINETIC PART OF IONS): " << std::fixed + ofs << " TOTAL-PRESSURE (DO NOT INCLUDE KINETIC PART OF IONS): " << std::fixed << std::setprecision(6) << pressure << unit << std::endl; } @@ -326,9 +327,9 @@ void print_stress(const std::string& name, const ModuleBase::matrix& scs, const return; } -void write_head(std::ofstream& ofs_running, const int& istep, const int& iter, const std::string& basisname) +void write_head(std::ofstream& ofs, const int& istep, const int& iter, const std::string& basisname) { - ofs_running << "\n " << basisname << " ALGORITHM --------------- ION=" << std::setw(4) << istep + 1 + ofs << "\n " << basisname << " ALGORITHM --------------- ION=" << std::setw(4) << istep + 1 << " ELEC=" << std::setw(4) << iter << "--------------------------------\n"; } diff --git a/source/module_io/output_log.h b/source/module_io/output_log.h index 2bbc90cd948..8f67e2f7686 100644 --- a/source/module_io/output_log.h +++ b/source/module_io/output_log.h @@ -64,7 +64,11 @@ void print_force(std::ofstream& ofs_running, /// @param name stress term name /// @param f stress components /// @param ry true if the unit of force is a.u. -void print_stress(const std::string& name, const ModuleBase::matrix& scs, const bool screen, const bool ry); +void print_stress(const std::string& name, + const ModuleBase::matrix& scs, + const bool screen, + const bool ry, + std::ofstream &ofs); /// @brief write head for scf iteration /// @param ofs_running output stream diff --git a/source/module_io/test/outputlog_test.cpp b/source/module_io/test/outputlog_test.cpp index 342ad14a8be..a1468e49f7a 100644 --- a/source/module_io/test/outputlog_test.cpp +++ b/source/module_io/test/outputlog_test.cpp @@ -252,31 +252,41 @@ TEST(PrintForce, PrintForce) force(1, 1) = 0.0; force(1, 2) = 0.0; - GlobalV::ofs_running.open("test.txt"); - ModuleIO::print_force(GlobalV::ofs_running, ucell, name, force, false); - GlobalV::ofs_running.close(); + std::ofstream ofs("running_force.txt"); + ModuleIO::print_force(ofs, ucell, name, force, false); + ofs.close(); - std::ifstream ifs("test.txt"); + std::ifstream ifs("running_force.txt"); std::string output_str; + + getline(ifs, output_str); + EXPECT_THAT(output_str, testing::HasSubstr("test")); + getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr("---------------------------------------------------------------------------")); + getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr("test")); + EXPECT_THAT(output_str," Atoms Force_x Force_y Force_z"); + getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr("---------------------------------------------------------------------------")); + getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr("Al1 25.7110532015 51.4221064030 77.1331596044")); + getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr("Al2 0.0000000000 0.0000000000 0.0000000000")); + getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr("---------------------------------------------------------------------------")); + ifs.close(); - std::remove("test.txt"); + //std::remove("running_force.txt"); } TEST(PrintStress, PrintStress) @@ -292,11 +302,11 @@ TEST(PrintStress, PrintStress) stress(2, 1) = 0.0; stress(2, 2) = 0.0; - GlobalV::ofs_running.open("test.txt"); + GlobalV::ofs_running.open("running_stress.txt"); ModuleIO::print_stress("TOTAL-STRESS", stress, true, false); GlobalV::ofs_running.close(); - std::ifstream ifs("test.txt"); + std::ifstream ifs("running_stress.txt"); std::string output_str; getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr("----------------------------------------------------------------")); @@ -315,7 +325,7 @@ TEST(PrintStress, PrintStress) getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr(" TOTAL-PRESSURE: 49035.075992 KBAR")); ifs.close(); - std::remove("test.txt"); + //std::remove("running_stress.txt"); } int main(int argc, char** argv) @@ -337,4 +347,4 @@ int main(int argc, char** argv) finishmpi(); #endif return result; -} \ No newline at end of file +} From bda0769a096169518f7c29b70d7000cff20b52fe Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 27 Mar 2025 23:02:30 +0800 Subject: [PATCH 18/35] update print_stress in dp, but still has problems in lj --- source/module_esolver/esolver_dp.cpp | 6 +----- source/module_esolver/esolver_lj.cpp | 5 ++++- source/module_io/output_log.h | 2 +- source/module_io/test/outputlog_test.cpp | 7 ++++--- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/source/module_esolver/esolver_dp.cpp b/source/module_esolver/esolver_dp.cpp index de642a6598f..1ca9939c359 100644 --- a/source/module_esolver/esolver_dp.cpp +++ b/source/module_esolver/esolver_dp.cpp @@ -28,8 +28,7 @@ #include #include -namespace ModuleESolver -{ +using namespace ModuleESolver; void ESolver_DP::before_all_runners(UnitCell& ucell, const Input_para& inp) { @@ -197,12 +196,9 @@ void ESolver_DP::type_map(const UnitCell& ucell) "The label " + ucell.atoms[it].label + " is not found in the type map."); } atype[iat] = label[ucell.atoms[it].label]; - // if (ia == 0) - // std::cout << "type: " << atype[iat] << std::endl; iat++; } } assert(ucell.nat == iat); } #endif -} // namespace ModuleESolver diff --git a/source/module_esolver/esolver_lj.cpp b/source/module_esolver/esolver_lj.cpp index 28a4c031258..d1459fe4906 100644 --- a/source/module_esolver/esolver_lj.cpp +++ b/source/module_esolver/esolver_lj.cpp @@ -5,6 +5,7 @@ #include "module_io/output_log.h" #include "module_io/cif_io.h" + namespace ModuleESolver { @@ -104,7 +105,9 @@ void ESolver_LJ::runner(UnitCell& ucell, const int istep) { stress = lj_virial; - ModuleIO::print_stress("TOTAL-STRESS", stress, true, false, GlobalV::ofs_running); + const bool screen = true; + const bool ry = false; + ModuleIO::print_stress("TOTAL-STRESS", stress, screen, ry, GlobalV::ofs_running); // external stress double unit_transform = ModuleBase::RYDBERG_SI / pow(ModuleBase::BOHR_RADIUS_SI, 3) * 1.0e-8; diff --git a/source/module_io/output_log.h b/source/module_io/output_log.h index 8f67e2f7686..56108648638 100644 --- a/source/module_io/output_log.h +++ b/source/module_io/output_log.h @@ -54,7 +54,7 @@ void output_vacuum_level(const UnitCell* ucell, /// @param name force term name /// @param force atomic forces /// @param ry true if the unit of force is a.u. -void print_force(std::ofstream& ofs_running, +void print_force(std::ofstream& ofs, const UnitCell& cell, const std::string& name, const ModuleBase::matrix& force, diff --git a/source/module_io/test/outputlog_test.cpp b/source/module_io/test/outputlog_test.cpp index a1468e49f7a..f12cbd8853c 100644 --- a/source/module_io/test/outputlog_test.cpp +++ b/source/module_io/test/outputlog_test.cpp @@ -302,9 +302,10 @@ TEST(PrintStress, PrintStress) stress(2, 1) = 0.0; stress(2, 2) = 0.0; - GlobalV::ofs_running.open("running_stress.txt"); - ModuleIO::print_stress("TOTAL-STRESS", stress, true, false); - GlobalV::ofs_running.close(); + + std::ofstream ofs("running_stress.txt"); + ModuleIO::print_stress("TOTAL-STRESS", stress, true, false, ofs); + ofs.close(); std::ifstream ifs("running_stress.txt"); std::string output_str; From c4b4b1965db3a360308952511ba2cd4b946213f9 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Thu, 27 Mar 2025 23:02:43 +0800 Subject: [PATCH 19/35] fix dp_test.cpp --- source/module_esolver/test/esolver_dp_test.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/source/module_esolver/test/esolver_dp_test.cpp b/source/module_esolver/test/esolver_dp_test.cpp index ab9ef17331f..7d2a0e3a2dc 100644 --- a/source/module_esolver/test/esolver_dp_test.cpp +++ b/source/module_esolver/test/esolver_dp_test.cpp @@ -20,6 +20,7 @@ * - ESolver_DP::post_process() * - ESolver_DP::type_map() */ + namespace ModuleIO { void print_force(std::ofstream& ofs_running, @@ -29,9 +30,15 @@ void print_force(std::ofstream& ofs_running, bool ry = true) { } -void print_stress(const std::string& name, const ModuleBase::matrix& scs, const bool screen, const bool ry) + +void print_stress(const std::string& name, + const ModuleBase::matrix& scs, + const bool screen, + const bool ry, + std::ofstream &ofs) { } + } // namespace ModuleIO class ESolverDPTest : public ::testing::Test From 1060d4a0c50b39882c61d05bbc62ca724de6d78f Mon Sep 17 00:00:00 2001 From: mohanchen Date: Fri, 28 Mar 2025 20:38:56 +0800 Subject: [PATCH 20/35] update esolver_lj --- source/module_md/test/setcell.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/module_md/test/setcell.h b/source/module_md/test/setcell.h index fedb2478e0e..0e321f7e8e4 100644 --- a/source/module_md/test/setcell.h +++ b/source/module_md/test/setcell.h @@ -29,7 +29,7 @@ void print_force(std::ofstream& ofs_running, bool ry = true) { } -void print_stress(const std::string& name, const ModuleBase::matrix& scs, const bool screen, const bool ry) +void print_stress(const std::string& name, const ModuleBase::matrix& scs, const bool screen, const bool ry, std::ofstream &ofs) { } } // namespace ModuleIO @@ -154,4 +154,4 @@ class Setcell }; }; -#endif \ No newline at end of file +#endif From b0d2d0ac962ddce88488653f4509d44c0ce64c7b Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sat, 29 Mar 2025 15:15:53 +0800 Subject: [PATCH 21/35] set mulliken charge accuracy from 4 to 3 --- tests/integrate/tools/catch_properties.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/integrate/tools/catch_properties.sh b/tests/integrate/tools/catch_properties.sh index b62a348d0de..f8d25085b53 100755 --- a/tests/integrate/tools/catch_properties.sh +++ b/tests/integrate/tools/catch_properties.sh @@ -477,7 +477,7 @@ fi # mulliken charge #-------------------------------------------- if ! test -z "$out_mul" && [ $out_mul == 1 ]; then - python3 ../tools/CompareFile.py mulliken.txt.ref OUT.autotest/mulliken.txt 4 + python3 ../tools/CompareFile.py mulliken.txt.ref OUT.autotest/mulliken.txt 3 echo "Compare_mulliken_pass $?" >>$1 fi From 3956c6f856292f0ac76c45be74dc0fd459eba12a Mon Sep 17 00:00:00 2001 From: mohanchen Date: Sun, 30 Mar 2025 22:29:49 +0800 Subject: [PATCH 22/35] update print force and print stress --- source/module_io/test/outputlog_test.cpp | 29 ++++++++++++++++-------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/source/module_io/test/outputlog_test.cpp b/source/module_io/test/outputlog_test.cpp index f12cbd8853c..786f6017dfb 100644 --- a/source/module_io/test/outputlog_test.cpp +++ b/source/module_io/test/outputlog_test.cpp @@ -260,18 +260,19 @@ TEST(PrintForce, PrintForce) std::string output_str; getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr("test")); + EXPECT_THAT(output_str, testing::HasSubstr(" test")); getline(ifs, output_str); EXPECT_THAT(output_str, - testing::HasSubstr("---------------------------------------------------------------------------")); + testing::HasSubstr("-------------------------------------------------------------------------")); getline(ifs, output_str); - EXPECT_THAT(output_str," Atoms Force_x Force_y Force_z"); + EXPECT_THAT(output_str, + testing::HasSubstr(" Atoms Force_x Force_y Force_z")); getline(ifs, output_str); EXPECT_THAT(output_str, - testing::HasSubstr("---------------------------------------------------------------------------")); + testing::HasSubstr("-------------------------------------------------------------------------")); getline(ifs, output_str); EXPECT_THAT(output_str, @@ -283,10 +284,10 @@ TEST(PrintForce, PrintForce) getline(ifs, output_str); EXPECT_THAT(output_str, - testing::HasSubstr("---------------------------------------------------------------------------")); + testing::HasSubstr("-------------------------------------------------------------------------")); ifs.close(); - //std::remove("running_force.txt"); + std::remove("running_force.txt"); } TEST(PrintStress, PrintStress) @@ -309,24 +310,34 @@ TEST(PrintStress, PrintStress) std::ifstream ifs("running_stress.txt"); std::string output_str; + getline(ifs, output_str); + EXPECT_THAT(output_str, testing::HasSubstr(" TOTAL-STRESS (KBAR)")); + getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr("----------------------------------------------------------------")); + getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr("TOTAL-STRESS (KBAR) ")); + EXPECT_THAT(output_str, testing::HasSubstr(" Stress_x Stress_y Stress_z")); + getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr("----------------------------------------------------------------")); + getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr(" 147105.2279754489 294210.4559508978 441315.6839263467")); + getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr(" 0.0000000000 0.0000000000 0.0000000000")); + getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr(" 0.0000000000 0.0000000000 0.0000000000")); + getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr("----------------------------------------------------------------")); + getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr(" TOTAL-PRESSURE: 49035.075992 KBAR")); + EXPECT_THAT(output_str, testing::HasSubstr(" TOTAL-PRESSURE (DO NOT INCLUDE KINETIC PART OF IONS): 49035.075992 KBAR")); ifs.close(); - //std::remove("running_stress.txt"); + std::remove("running_stress.txt"); } int main(int argc, char** argv) From ae6552c255d5f71823d0ffb125a6b5d681b28040 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 31 Mar 2025 16:03:32 +0800 Subject: [PATCH 23/35] delete kv variable in hamilt_lcao --- .../hamilt_lcaodft/hamilt_lcao.cpp | 57 +++++++++---------- .../hamilt_lcaodft/hamilt_lcao.h | 6 +- 2 files changed, 29 insertions(+), 34 deletions(-) diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp index 35184682e07..0a57e99c10f 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp @@ -53,15 +53,13 @@ HamiltLCAO::HamiltLCAO(const UnitCell& ucell, { this->classname = "HamiltLCAO"; - this->kv = &kv_in; - // Real space Hamiltonian is inited with template TR // this->hR = new HContainer(paraV); this->sR = new HContainer(paraV); // this->hsk = new HS_Matrix_K(paraV); this->getOperator() = new OverlapNew>(this->hsk, - this->kv->kvec_d, + kv_in.kvec_d, this->hR, this->sR, &ucell, @@ -94,7 +92,6 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, #endif ) { - this->kv = &kv_in; this->classname = "HamiltLCAO"; // Real space Hamiltonian is inited with template TR @@ -144,7 +141,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, // overlap term () is indispensable // in Gamma_only case, target SK is this->hsk->get_sk(), the target SR is this->sR this->getOperator() = new OverlapNew>(this->hsk, - this->kv->kvec_d, + kv_in.kvec_d, this->hR, this->sR, &ucell, @@ -156,7 +153,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.t_in_h) { Operator* ekinetic = new EkineticNew>(this->hsk, - this->kv->kvec_d, + kv_in.kvec_d, this->hR, &ucell, orb.cutoffs(), @@ -170,7 +167,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.vnl_in_h) { Operator* nonlocal = new NonlocalNew>(this->hsk, - this->kv->kvec_d, + kv_in.kvec_d, this->hR, &ucell, orb.cutoffs(), @@ -191,7 +188,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, // effective potential term Operator* veff = new Veff>(GG_in, this->hsk, - this->kv->kvec_d, + kv_in.kvec_d, pot_in, this->hR, // no explicit call yet &ucell, @@ -206,13 +203,13 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.deepks_scf) { Operator* deepks = new DeePKS>(this->hsk, - this->kv->kvec_d, + kv_in.kvec_d, this->hR, // no explicit call yet &ucell, &grid_d, two_center_bundle.overlap_orb_alpha.get(), &orb, - this->kv->get_nks(), + kv_in.get_nks(), DM_in, ld_in); this->getOperator()->add(deepks); @@ -227,14 +224,14 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.dft_plus_u == 2) { dftu = new OperatorDFTU>(this->hsk, - kv->kvec_d, + kv_in.kvec_d, this->hR, // no explicit call yet - this->kv->isk); + kv_in.isk); } else { dftu = new DFTU>(this->hsk, - this->kv->kvec_d, + kv_in.kvec_d, this->hR, ucell, &grid_d, @@ -261,7 +258,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, // Veff term this->getOperator() = new Veff>(GK_in, this->hsk, - kv->kvec_d, + kv_in.kvec_d, pot_in, this->hR, &ucell, @@ -274,7 +271,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, // initial operator for multi-k case // overlap term is indispensable Operator* overlap = new OverlapNew>(this->hsk, - this->kv->kvec_d, + kv_in.kvec_d, this->hR, this->sR, &ucell, @@ -295,7 +292,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.t_in_h) { Operator* ekinetic = new EkineticNew>(this->hsk, - this->kv->kvec_d, + kv_in.kvec_d, this->hR, &ucell, orb.cutoffs(), @@ -309,7 +306,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.vnl_in_h) { Operator* nonlocal = new NonlocalNew>(this->hsk, - this->kv->kvec_d, + kv_in.kvec_d, this->hR, &ucell, orb.cutoffs(), @@ -331,13 +328,13 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.deepks_scf) { Operator* deepks = new DeePKS>(this->hsk, - this->kv->kvec_d, + kv_in.kvec_d, hR, &ucell, &grid_d, two_center_bundle.overlap_orb_alpha.get(), &orb, - this->kv->get_nks(), + kv_in.get_nks(), DM_in, ld_in); this->getOperator()->add(deepks); @@ -361,7 +358,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, this->getOperator()->add(td_ekinetic); Operator* td_nonlocal - = new TDNonlocal>(this->hsk, this->kv->kvec_d, this->hR, &ucell, orb, &grid_d); + = new TDNonlocal>(this->hsk, kv_in.kvec_d, this->hR, &ucell, orb, &grid_d); this->getOperator()->add(td_nonlocal); } if (PARAM.inp.dft_plus_u) @@ -370,14 +367,14 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.dft_plus_u == 2) { dftu = new OperatorDFTU>(this->hsk, - kv->kvec_d, + kv_in.kvec_d, this->hR, // no explicit call yet - this->kv->isk); + kv_in.isk); } else { dftu = new DFTU>(this->hsk, - this->kv->kvec_d, + kv_in.kvec_d, this->hR, ucell, &grid_d, @@ -390,7 +387,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.sc_mag_switch) { Operator* sc_lambda = new DeltaSpin>(this->hsk, - kv->kvec_d, + kv_in.kvec_d, this->hR, ucell, &grid_d, @@ -411,7 +408,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, Operator* exx = new OperatorEXX>(this->hsk, this->hR, ucell, - *this->kv, + kv_in, Hexxd, Hexxc, Add_Hexx_Type::R, @@ -455,17 +452,17 @@ void HamiltLCAO::updateHk(const int ik) if (PARAM.inp.nspin == 2) { // if Veff is added and current_spin is changed, refresh HR - if (PARAM.inp.vl_in_h && this->kv->isk[ik] != this->current_spin) + if (PARAM.inp.vl_in_h && kv->isk[ik] != this->current_spin) { // change data pointer of HR - this->hR->allocate(this->hRS2.data() + this->hRS2.size() / 2 * this->kv->isk[ik], 0); + this->hR->allocate(this->hRS2.data() + this->hRS2.size() / 2 * kv->isk[ik], 0); if (this->refresh_times > 0) { this->refresh_times--; dynamic_cast*>(this->ops)->set_hr_done(false); } } - this->current_spin = this->kv->isk[ik]; + this->current_spin = kv->isk[ik]; } this->getOperator()->init(ik); ModuleBase::timer::tick("HamiltLCAO", "updateHk"); @@ -505,12 +502,12 @@ void HamiltLCAO::updateSk(const int ik, const int hk_type) if (hk_type == 1) // collumn-major matrix for SK { const int nrow = this->hsk->get_pv()->get_row_size(); - hamilt::folding_HR(*this->sR, this->getSk(), this->kv->kvec_d[ik], nrow, 1); + hamilt::folding_HR(*this->sR, this->getSk(), kv->kvec_d[ik], nrow, 1); } else if (hk_type == 0) // row-major matrix for SK { const int ncol = this->hsk->get_pv()->get_col_size(); - hamilt::folding_HR(*this->sR, this->getSk(), this->kv->kvec_d[ik], ncol, 0); + hamilt::folding_HR(*this->sR, this->getSk(), kv->kvec_d[ik], ncol, 0); } ModuleBase::timer::tick("HamiltLCAO", "updateSk"); } diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h index c7ead226253..2f8f95e6489 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h +++ b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h @@ -1,5 +1,5 @@ -#ifndef W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_HAMILT_LCAO_HAMILT_LCAODFT_HAMILT_LCAO_H -#define W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_HAMILT_LCAO_HAMILT_LCAODFT_HAMILT_LCAO_H +#ifndef HAMILT_LCAO_H +#define HAMILT_LCAO_H #include "module_basis/module_nao/two_center_bundle.h" #include "module_cell/klist.h" @@ -41,7 +41,6 @@ class HamiltLCAO : public Hamilt const Grid_Driver& grid_d, const Parallel_Orbitals* paraV, elecstate::Potential* pot_in, - const K_Vectors& kv_in, const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, elecstate::DensityMatrix* DM_in @@ -132,7 +131,6 @@ class HamiltLCAO : public Hamilt void matrix(MatrixBlock& hk_in, MatrixBlock& sk_in) override; private: - const K_Vectors* kv = nullptr; // Real space Hamiltonian HContainer* hR = nullptr; From 37c253ec4e943d9ed0f83282a865a072c7e30d22 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 31 Mar 2025 21:39:36 +0800 Subject: [PATCH 24/35] update some operators --- source/module_hamilt_general/operator.cpp | 38 +++--- source/module_hamilt_general/operator.h | 8 +- .../hamilt_lcaodft/hamilt_lcao.cpp | 81 +++++++------ .../hamilt_lcaodft/hamilt_lcao.h | 39 +++++-- .../hamilt_lcaodft/hs_matrix_k.hpp | 17 ++- .../operator_lcao/operator_lcao.h | 2 + .../operator_lcao/td_ekinetic_lcao.cpp | 4 + .../operator_lcao/td_ekinetic_lcao.h | 16 ++- source/module_hamilt_lcao/module_dftu/dftu.h | 4 +- .../module_dftu/dftu_folding.cpp | 14 ++- .../module_dftu/dftu_occup.cpp | 110 +++++++++++------- 11 files changed, 213 insertions(+), 120 deletions(-) diff --git a/source/module_hamilt_general/operator.cpp b/source/module_hamilt_general/operator.cpp index 08a5ba97ccd..6682777322a 100644 --- a/source/module_hamilt_general/operator.cpp +++ b/source/module_hamilt_general/operator.cpp @@ -16,8 +16,10 @@ Operator::~Operator() { delete this->hpsi; } + Operator* last = this->next_op; Operator* last_sub = this->next_sub_op; + while (last != nullptr || last_sub != nullptr) { if (last_sub != nullptr) @@ -58,7 +60,6 @@ typename Operator::hpsi_info Operator::hPsi(hpsi_info& inp T* hpsi_pointer = std::get<2>(input); if (this->in_place) { - // ModuleBase::GlobalFunc::COPYARRAY(this->hpsi->get_pointer(), hpsi_pointer, this->hpsi->size()); syncmem_op()(hpsi_pointer, this->hpsi->get_pointer(), this->hpsi->size()); delete this->hpsi; this->hpsi = new psi::Psi(hpsi_pointer, @@ -78,31 +79,35 @@ typename Operator::hpsi_info Operator::hPsi(hpsi_info& inp psi_input->get_nbasis(), true); - switch (op->get_act_type()) - { - case 2: - op->act(psi_wrapper, *this->hpsi, nbands); - break; - default: - op->act(nbands, - psi_input->get_nbasis(), - psi_input->get_npol(), - tmpsi_in, - this->hpsi->get_pointer(), - psi_input->get_current_nbas(), - is_first_node); - break; - } + switch (op->get_act_type()) + { + case 2: + op->act(psi_wrapper, *this->hpsi, nbands); + break; + default: + op->act(nbands, + psi_input->get_nbasis(), + psi_input->get_npol(), + tmpsi_in, + this->hpsi->get_pointer(), + psi_input->get_current_nbas(), + is_first_node); + break; + } }; ModuleBase::timer::tick("Operator", "hPsi"); + call_act(this, true); // first node + Operator* node((Operator*)this->next_op); + while (node != nullptr) { call_act(node, false); // other nodes node = (Operator*)(node->next_op); } + ModuleBase::timer::tick("Operator", "hPsi"); return hpsi_info(this->hpsi, psi::Range(1, 0, 0, nbands / psi_input->get_npol()), hpsi_pointer); @@ -157,6 +162,7 @@ void Operator::add(Operator* next) } } + template T* Operator::get_hpsi(const hpsi_info& info) const { diff --git a/source/module_hamilt_general/operator.h b/source/module_hamilt_general/operator.h index 941f3e29c1a..87567f9f362 100644 --- a/source/module_hamilt_general/operator.h +++ b/source/module_hamilt_general/operator.h @@ -73,8 +73,10 @@ class Operator /// developer-friendly interfaces for act() function /// interface type 2: input and change the Psi-type HPsi - // virtual void act(const psi::Psi& psi_in, psi::Psi& psi_out) const {}; - virtual void act(const psi::Psi& psi_in, psi::Psi& psi_out, const int nbands) const {}; + // virtual void act(const psi::Psi& psi_in, psi::Psi& psi_out) const {}; + virtual void act(const psi::Psi& psi_in, + psi::Psi& psi_out, + const int nbands) const {}; /// interface type 3: return a Psi-type HPsi // virtual psi::Psi act(const psi::Psi& psi_in) const { return psi_in; }; @@ -125,4 +127,4 @@ class Operator } // end namespace hamilt -#endif \ No newline at end of file +#endif diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp index 0a57e99c10f..99d41d43162 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp @@ -47,7 +47,7 @@ template HamiltLCAO::HamiltLCAO(const UnitCell& ucell, const Grid_Driver& grid_d, const Parallel_Orbitals* paraV, - const K_Vectors& kv_in, + const K_Vectors& kv, const TwoCenterIntegrator& intor_overlap_orb, const std::vector& orb_cutoff) { @@ -59,7 +59,7 @@ HamiltLCAO::HamiltLCAO(const UnitCell& ucell, // this->hsk = new HS_Matrix_K(paraV); this->getOperator() = new OverlapNew>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, this->sR, &ucell, @@ -75,7 +75,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, const Grid_Driver& grid_d, const Parallel_Orbitals* paraV, elecstate::Potential* pot_in, - const K_Vectors& kv_in, + const K_Vectors& kv, const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, elecstate::DensityMatrix* DM_in @@ -141,7 +141,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, // overlap term () is indispensable // in Gamma_only case, target SK is this->hsk->get_sk(), the target SR is this->sR this->getOperator() = new OverlapNew>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, this->sR, &ucell, @@ -153,7 +153,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.t_in_h) { Operator* ekinetic = new EkineticNew>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, &ucell, orb.cutoffs(), @@ -167,7 +167,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.vnl_in_h) { Operator* nonlocal = new NonlocalNew>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, &ucell, orb.cutoffs(), @@ -188,7 +188,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, // effective potential term Operator* veff = new Veff>(GG_in, this->hsk, - kv_in.kvec_d, + kv.kvec_d, pot_in, this->hR, // no explicit call yet &ucell, @@ -203,13 +203,13 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.deepks_scf) { Operator* deepks = new DeePKS>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, // no explicit call yet &ucell, &grid_d, two_center_bundle.overlap_orb_alpha.get(), &orb, - kv_in.get_nks(), + kv.get_nks(), DM_in, ld_in); this->getOperator()->add(deepks); @@ -224,14 +224,14 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.dft_plus_u == 2) { dftu = new OperatorDFTU>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, // no explicit call yet - kv_in.isk); + kv.isk); } else { dftu = new DFTU>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, ucell, &grid_d, @@ -258,7 +258,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, // Veff term this->getOperator() = new Veff>(GK_in, this->hsk, - kv_in.kvec_d, + kv.kvec_d, pot_in, this->hR, &ucell, @@ -271,7 +271,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, // initial operator for multi-k case // overlap term is indispensable Operator* overlap = new OverlapNew>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, this->sR, &ucell, @@ -292,7 +292,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.t_in_h) { Operator* ekinetic = new EkineticNew>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, &ucell, orb.cutoffs(), @@ -306,7 +306,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.vnl_in_h) { Operator* nonlocal = new NonlocalNew>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, &ucell, orb.cutoffs(), @@ -328,13 +328,13 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.deepks_scf) { Operator* deepks = new DeePKS>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, hR, &ucell, &grid_d, two_center_bundle.overlap_orb_alpha.get(), &orb, - kv_in.get_nks(), + kv.get_nks(), DM_in, ld_in); this->getOperator()->add(deepks); @@ -358,7 +358,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, this->getOperator()->add(td_ekinetic); Operator* td_nonlocal - = new TDNonlocal>(this->hsk, kv_in.kvec_d, this->hR, &ucell, orb, &grid_d); + = new TDNonlocal>(this->hsk, kv.kvec_d, this->hR, &ucell, orb, &grid_d); this->getOperator()->add(td_nonlocal); } if (PARAM.inp.dft_plus_u) @@ -367,14 +367,14 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.dft_plus_u == 2) { dftu = new OperatorDFTU>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, // no explicit call yet - kv_in.isk); + kv.isk); } else { dftu = new DFTU>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, ucell, &grid_d, @@ -387,7 +387,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.sc_mag_switch) { Operator* sc_lambda = new DeltaSpin>(this->hsk, - kv_in.kvec_d, + kv.kvec_d, this->hR, ucell, &grid_d, @@ -408,7 +408,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, Operator* exx = new OperatorEXX>(this->hsk, this->hR, ucell, - kv_in, + kv, Hexxd, Hexxc, Add_Hexx_Type::R, @@ -444,25 +444,28 @@ void HamiltLCAO::matrix(MatrixBlock& hk_in, MatrixBlock& sk_in) } template -void HamiltLCAO::updateHk(const int ik) +void HamiltLCAO::updateHk(const int ik, const int isk) { ModuleBase::TITLE("HamiltLCAO", "updateHk"); ModuleBase::timer::tick("HamiltLCAO", "updateHk"); + + // isk is kv.isk[ik], mohan note 2025-03-31 + // update global spin index if (PARAM.inp.nspin == 2) { // if Veff is added and current_spin is changed, refresh HR - if (PARAM.inp.vl_in_h && kv->isk[ik] != this->current_spin) + if (PARAM.inp.vl_in_h && isk != this->current_spin) { // change data pointer of HR - this->hR->allocate(this->hRS2.data() + this->hRS2.size() / 2 * kv->isk[ik], 0); + this->hR->allocate(this->hRS2.data() + this->hRS2.size() / 2 * isk, 0); if (this->refresh_times > 0) { this->refresh_times--; dynamic_cast*>(this->ops)->set_hr_done(false); } } - this->current_spin = kv->isk[ik]; + this->current_spin = isk; } this->getOperator()->init(ik); ModuleBase::timer::tick("HamiltLCAO", "updateHk"); @@ -494,21 +497,31 @@ Operator*& HamiltLCAO::getOperator() } template -void HamiltLCAO::updateSk(const int ik, const int hk_type) +void HamiltLCAO::updateSk( + const int ik, + std::vector>& kvec_d, + const int hk_type) { ModuleBase::TITLE("HamiltLCAO", "updateSk"); ModuleBase::timer::tick("HamiltLCAO", "updateSk"); + ModuleBase::GlobalFunc::ZEROS(this->getSk(), this->get_size_hsk()); + if (hk_type == 1) // collumn-major matrix for SK { const int nrow = this->hsk->get_pv()->get_row_size(); - hamilt::folding_HR(*this->sR, this->getSk(), kv->kvec_d[ik], nrow, 1); - } - else if (hk_type == 0) // row-major matrix for SK - { + hamilt::folding_HR(*this->sR, this->getSk(), kvec_d[ik], nrow, 1); + } + else if (hk_type == 0) // row-major matrix for SK + { const int ncol = this->hsk->get_pv()->get_col_size(); - hamilt::folding_HR(*this->sR, this->getSk(), kv->kvec_d[ik], ncol, 0); + hamilt::folding_HR(*this->sR, this->getSk(), kvec_d[ik], ncol, 0); } + else + { + ModuleBase::WARNING_QUIT("updateSk","the value of hk_type is incorrect."); + } + ModuleBase::timer::tick("HamiltLCAO", "updateSk"); } diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h index 2f8f95e6489..1995c2e4025 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h +++ b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h @@ -25,23 +25,30 @@ namespace hamilt { // template first for type of k space H matrix elements -// template second for type of temporary matrix, gamma_only fix-gamma-matrix + S-gamma, multi-k fix-Real + S-Real +// template second for type of temporary matrix, +// gamma_only fix-gamma-matrix + S-gamma, +// multi-k fix-Real + S-Real template class HamiltLCAO : public Hamilt { public: + + + using TAC = std::pair>; + + /** * @brief Constructor of Hamiltonian for LCAO base * HR and SR will be allocated with Operators */ - using TAC = std::pair>; HamiltLCAO(Gint_Gamma* GG_in, Gint_k* GK_in, const UnitCell& ucell, const Grid_Driver& grid_d, - const Parallel_Orbitals* paraV, - elecstate::Potential* pot_in, - const TwoCenterBundle& two_center_bundle, + const Parallel_Orbitals* paraV, + elecstate::Potential* pot_in, + const K_Vectors& kv, + const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, elecstate::DensityMatrix* DM_in #ifdef __DEEPKS @@ -56,13 +63,14 @@ class HamiltLCAO : public Hamilt std::vector>>>>* Hexxc = nullptr #endif ); + /** * @brief Constructor of vacuum Operators, only HR and SR will be initialed as empty HContainer */ HamiltLCAO(const UnitCell& ucell, const Grid_Driver& grid_d, const Parallel_Orbitals* paraV, - const K_Vectors& kv_in, + const K_Vectors& kv, const TwoCenterIntegrator& intor_overlap_orb, const std::vector& orb_cutoff); @@ -79,30 +87,36 @@ class HamiltLCAO : public Hamilt /// get pointer of Operator ops Operator*& getOperator(); - /// get hk-pointer + + /// get H(k) pointer TK* getHk() const { return this->hsk->get_hk(); } - /// get sk-pointer + + /// get S(k) pointer TK* getSk() const { return this->hsk->get_sk(); } + int get_size_hsk() const { return this->hsk->get_size(); } + /// get HR pointer of *this->hR, which is a HContainer and contains H(R) HContainer*& getHR() { return this->hR; } + /// get SR pointer of *this->sR, which is a HContainer and contains S(R) HContainer*& getSR() { return this->sR; } + #ifdef __DEEPKS /// get V_delta_R pointer of *this->V_delta_R, which is a HContainer and contains V_delta(R) HContainer*& get_V_delta_R() @@ -110,20 +124,24 @@ class HamiltLCAO : public Hamilt return this->V_delta_R; } #endif + /// refresh the status of HR void refresh() override; // for target K point, update consequence of hPsi() and matrix() - virtual void updateHk(const int ik) override; + void updateHk(const int ik, const int isk); /** * @brief special for LCAO, update SK only * * @param ik target K point + * @param kvec_d: direct coordinates of k-points * @param hk_type 0: SK is row-major, 1: SK is collumn-major * @return void */ - void updateSk(const int ik, const int hk_type = 0); + void updateSk(const int ik, + std::vector>& kvec_d, + const int hk_type = 0); // core function: return H(k) and S(k) matrixs for direct solving eigenvalues. // not used in PW base @@ -141,6 +159,7 @@ class HamiltLCAO : public Hamilt #endif HS_Matrix_K* hsk = nullptr; + // special case for NSPIN=2 , data of HR should be separated into two parts // save them in this->hRS2; std::vector hRS2; diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/hs_matrix_k.hpp b/source/module_hamilt_lcao/hamilt_lcaodft/hs_matrix_k.hpp index 495af32841e..429a4ccb9c3 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/hs_matrix_k.hpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/hs_matrix_k.hpp @@ -1,5 +1,5 @@ -#ifndef ABACUS_HS_MATRIX_K_HPP -#define ABACUS_HS_MATRIX_K_HPP +#ifndef HS_MATRIX_K_HPP +#define HS_MATRIX_K_HPP #include "module_basis/module_ao/parallel_orbitals.h" @@ -9,6 +9,7 @@ namespace hamilt template class HS_Matrix_K { + public: HS_Matrix_K(const Parallel_Orbitals* paraV, bool no_s=false){ hk.resize(paraV->nloc); @@ -18,17 +19,27 @@ namespace hamilt } this->pv = paraV; } + TK* get_hk() {return hk.data();} + TK* get_sk() {return sk.data();} + int get_size() {return hk.size();} + void set_zero_hk() {hk.assign(hk.size(), 0);} + void set_zero_sk() {sk.assign(sk.size(), 0);} + const Parallel_Orbitals* get_pv() const {return this->pv;} + private: + std::vector hk; + std::vector sk; + const Parallel_Orbitals* pv = nullptr; }; } -#endif \ No newline at end of file +#endif diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/operator_lcao.h b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/operator_lcao.h index c29ba87c0ed..f9ad63c9422 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/operator_lcao.h +++ b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/operator_lcao.h @@ -100,6 +100,8 @@ class OperatorLCAO : public Operator { // protected: // Hamiltonian matrices which are calculated in OperatorLCAO HS_Matrix_K* hsk = nullptr; + + // kvec_d const std::vector>& kvec_d; protected: diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_ekinetic_lcao.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_ekinetic_lcao.cpp index 382f53350e6..1642a5635d9 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_ekinetic_lcao.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_ekinetic_lcao.cpp @@ -31,6 +31,7 @@ TDEkinetic>::TDEkinetic(HS_Matrix_K* hsk_in, // initialize HR to get adjs info. this->initialize_HR(Grid); } + template TDEkinetic>::~TDEkinetic() { @@ -40,12 +41,14 @@ TDEkinetic>::~TDEkinetic() } TD_Velocity::td_vel_op = nullptr; } + // term A^2*S template void TDEkinetic>::td_ekinetic_scalar(std::complex* Hloc,const TR& overlap, int nnr) { return; } + // term A^2*S template <> void TDEkinetic, double>>::td_ekinetic_scalar(std::complex* Hloc, @@ -57,6 +60,7 @@ void TDEkinetic, double>>::td_ekinetic_scalar( Hloc[nnr] += tmp; return; } + // term A dot ∇ template void TDEkinetic>::td_ekinetic_grad(std::complex* Hloc, diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_ekinetic_lcao.h b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_ekinetic_lcao.h index c5ec078c14f..7f5881ac1fc 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_ekinetic_lcao.h +++ b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_ekinetic_lcao.h @@ -45,7 +45,9 @@ class TDEkinetic> : public OperatorLCAO ~TDEkinetic(); virtual void contributeHR() override; + virtual void contributeHk(int ik) override; + /// @brief init two center integrals and vector potential for td_ekintic term void init_td(); @@ -78,22 +80,30 @@ class TDEkinetic> : public OperatorLCAO * loop the atom-pairs in HR and calculate the ekinetic matrix */ void calculate_HR(); + virtual void set_HR_fixed(void*) override; TD_Velocity td_velocity; private: + const UnitCell* ucell = nullptr; + std::vector orb_cutoff_; HContainer* SR = nullptr; - /// @brief Store real space hamiltonian. TD term should include imaginary part, thus it has to be complex type. Only shared between TD operators. + + /// @brief Store real space hamiltonian. TD term should include imaginary part, + /// thus it has to be complex type. Only shared between TD operators. HContainer>* hR_tmp = nullptr; + const Grid_Driver* Grid = nullptr; const K_Vectors* kv; - /// @brief correction term iA⋅∇ + + /// @brief correction term i A nabla void td_ekinetic_scalar(std::complex* Hloc, const TR& Sloc, int nnr); + /// @brief correction term A^2*S void td_ekinetic_grad(std::complex* Hloc, int nnr, ModuleBase::Vector3 grad_overlap); @@ -106,7 +116,9 @@ class TDEkinetic> : public OperatorLCAO std::vector adjs_all; bool hR_tmp_done = false; + bool allocated = false; + bool output_hR_done = false; }; diff --git a/source/module_hamilt_lcao/module_dftu/dftu.h b/source/module_hamilt_lcao/module_dftu/dftu.h index a885340cf15..cd7afa001f5 100644 --- a/source/module_hamilt_lcao/module_dftu/dftu.h +++ b/source/module_hamilt_lcao/module_dftu/dftu.h @@ -194,7 +194,9 @@ class DFTU * only for Hamiltonian now, for force and stress will be developed later * use HContainer as input and output in mat_k */ - void folding_matrix_k_new(const int ik, hamilt::Hamilt>* p_ham); + void folding_matrix_k_new(const int ik, + std::vector>& kvec_d, + hamilt::Hamilt>* p_ham); //============================================================= // In dftu_force.cpp diff --git a/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp b/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp index 3ae80f8e141..b99fb0d6fd7 100644 --- a/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp +++ b/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp @@ -279,6 +279,7 @@ void DFTU::folding_matrix_k(const UnitCell& ucell, } void DFTU::folding_matrix_k_new(const int ik, + std::vector>& kvec_d, hamilt::Hamilt>* p_ham) { ModuleBase::TITLE("DFTU", "folding_matrix_k_new"); @@ -293,22 +294,23 @@ void DFTU::folding_matrix_k_new(const int ik, // get SR and fold to mat_k if(PARAM.globalv.gamma_only_local) { - dynamic_cast*>(p_ham)->updateSk(ik, hk_type); + dynamic_cast*>(p_ham) + ->updateSk(ik, kvec_d, hk_type); } else { if(PARAM.inp.nspin != 4) { - dynamic_cast, double>*>(p_ham)->updateSk(ik, hk_type); + dynamic_cast, double>*>(p_ham) + ->updateSk(ik, kvec_d, hk_type); } else { - dynamic_cast, std::complex>*>(p_ham)->updateSk(ik, hk_type); + dynamic_cast, std::complex>*>(p_ham) + ->updateSk(ik, hk_type); } } } - - } // namespace ModuleDFTU -#endif // __LCAO \ No newline at end of file +#endif // __LCAO diff --git a/source/module_hamilt_lcao/module_dftu/dftu_occup.cpp b/source/module_hamilt_lcao/module_dftu/dftu_occup.cpp index d4efeed4269..63b7af029e0 100644 --- a/source/module_hamilt_lcao/module_dftu/dftu_occup.cpp +++ b/source/module_hamilt_lcao/module_dftu/dftu_occup.cpp @@ -38,9 +38,10 @@ void DFTU::copy_locale(const UnitCell& ucell) for (int T = 0; T < ucell.ntype; T++) { - if (orbital_corr[T] == -1) { - continue; -} + if (orbital_corr[T] == -1) + { + continue; + } for (int I = 0; I < ucell.atoms[T].na; I++) { @@ -75,8 +76,10 @@ void DFTU::zero_locale(const UnitCell& ucell) for (int T = 0; T < ucell.ntype; T++) { - if (orbital_corr[T] == -1) { continue; -} + if (orbital_corr[T] == -1) + { + continue; + } for (int I = 0; I < ucell.atoms[T].na; I++) { @@ -114,9 +117,10 @@ void DFTU::mix_locale(const UnitCell& ucell, for (int T = 0; T < ucell.ntype; T++) { - if (orbital_corr[T] == -1) { - continue; -} + if (orbital_corr[T] == -1) + { + continue; + } for (int I = 0; I < ucell.atoms[T].na; I++) { @@ -161,7 +165,8 @@ void DFTU::cal_occup_m_k(const int iter, //=================Part 1====================== // call SCALAPACK routine to calculate the product of the S and density matrix - const char transN = 'N', transT = 'T'; + const char transN = 'N'; + const char transT = 'T'; const int one_int = 1; const std::complex beta(0.0,0.0), alpha(1.0,0.0); @@ -170,8 +175,10 @@ void DFTU::cal_occup_m_k(const int iter, for (int ik = 0; ik < kv.get_nks(); ik++) { // srho(mu,nu) = \sum_{iw} S(mu,iw)*dm_k(iw,nu) - this->folding_matrix_k_new(ik, p_ham); + this->folding_matrix_k_new(ik, kv.kvec_d, p_ham); + std::complex* s_k_pointer = nullptr; + if(PARAM.inp.nspin != 4) { s_k_pointer = dynamic_cast, double>*>(p_ham)->getSk(); @@ -210,9 +217,10 @@ void DFTU::cal_occup_m_k(const int iter, const int NL = ucell.atoms[it].nwl + 1; const int LC = orbital_corr[it]; - if (LC == -1) { - continue; -} + if (LC == -1) + { + continue; + } for (int ia = 0; ia < ucell.atoms[it].na; ia++) { @@ -220,18 +228,20 @@ void DFTU::cal_occup_m_k(const int iter, for (int l = 0; l < NL; l++) { - if (l != orbital_corr[it]) { - continue; -} + if (l != orbital_corr[it]) + { + continue; + } const int N = ucell.atoms[it].l_nchi[l]; for (int n = 0; n < N; n++) { // if(!Yukawa && n!=0) continue; - if (n != 0) { - continue; -} + if (n != 0) + { + continue; + } // Calculate the local occupation number matrix for (int m0 = 0; m0 < 2 * l + 1; m0++) @@ -256,12 +266,16 @@ void DFTU::cal_occup_m_k(const int iter, const int m0_all = m0 + ipol0 * (2 * l + 1); const int m1_all = m1 + ipol1 * (2 * l + 1); - if ((nu >= 0) && (mu >= 0)) - locale[iat][l][n][spin](m0_all, m1_all) += (srho[irc]).real() / 4.0; + if ((nu >= 0) && (mu >= 0)) + { + locale[iat][l][n][spin](m0_all, m1_all) += (srho[irc]).real() / 4.0; + } - if ((nu_prime >= 0) && (mu_prime >= 0)) - locale[iat][l][n][spin](m0_all, m1_all) - += (std::conj(srho[irc_prime])).real() / 4.0; + if ((nu_prime >= 0) && (mu_prime >= 0)) + { + locale[iat][l][n][spin](m0_all, m1_all) + += (std::conj(srho[irc_prime])).real() / 4.0; + } } // ipol1 } // m1 } // ipol0 @@ -277,9 +291,10 @@ void DFTU::cal_occup_m_k(const int iter, const int NL = ucell.atoms[it].nwl + 1; const int LC = orbital_corr[it]; - if (LC == -1) { - continue; -} + if (LC == -1) + { + continue; + } for (int ia = 0; ia < ucell.atoms[it].na; ia++) { @@ -287,19 +302,21 @@ void DFTU::cal_occup_m_k(const int iter, for (int l = 0; l < NL; l++) { - if (l != orbital_corr[it]) { - continue; -} + if (l != orbital_corr[it]) + { + continue; + } const int N = ucell.atoms[it].l_nchi[l]; for (int n = 0; n < N; n++) { // if(!Yukawa && n!=0) continue; - if (n != 0) { - continue; -} - // set the local occupation mumber matrix of spin up and down zeros + if (n != 0) + { + continue; + } + // set the local occupation mumber matrix of spin up and down zeros #ifdef __MPI if (PARAM.inp.nspin == 1 || PARAM.inp.nspin == 4) @@ -418,26 +435,29 @@ void DFTU::cal_occup_m_gamma(const int iter, for (int it = 0; it < ucell.ntype; it++) { const int NL = ucell.atoms[it].nwl + 1; - if (orbital_corr[it] == -1) { - continue; -} - for (int ia = 0; ia < ucell.atoms[it].na; ia++) + if (orbital_corr[it] == -1) + { + continue; + } + for (int ia = 0; ia < ucell.atoms[it].na; ia++) { const int iat = ucell.itia2iat(it, ia); for (int l = 0; l < NL; l++) { - if (l != orbital_corr[it]) { - continue; -} + if (l != orbital_corr[it]) + { + continue; + } const int N = ucell.atoms[it].l_nchi[l]; for (int n = 0; n < N; n++) { - if (n != 0) { - continue; -} + if (n != 0) + { + continue; + } // Calculate the local occupation number matrix for (int m0 = 0; m0 < 2 * l + 1; m0++) @@ -524,4 +544,4 @@ void DFTU::cal_occup_m_gamma(const int iter, return; } #endif -} // namespace ModuleDFTU \ No newline at end of file +} // namespace ModuleDFTU From 5fccb05d224a178a7c080b9cd8b258bf72261012 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Mon, 31 Mar 2025 22:51:04 +0800 Subject: [PATCH 25/35] update hamiltonian and operator --- .../hamilt_lcaodft/hamilt_lcao.cpp | 13 +- .../hamilt_lcaodft/hamilt_lcao.h | 17 +- .../operator_lcao/td_ekinetic_lcao.cpp | 6 +- source/module_hamilt_lcao/module_dftu/dftu.h | 12 +- .../module_dftu/dftu_folding.cpp | 8 +- .../module_dftu/dftu_force.cpp | 10 +- .../module_dftu/dftu_occup.cpp | 2 +- source/module_io/write_dos_lcao.cpp | 249 +++++++++--------- 8 files changed, 162 insertions(+), 155 deletions(-) diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp index 99d41d43162..240e3378d5b 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp @@ -53,10 +53,8 @@ HamiltLCAO::HamiltLCAO(const UnitCell& ucell, { this->classname = "HamiltLCAO"; - // Real space Hamiltonian is inited with template TR - // this->hR = new HContainer(paraV); + // initialize the overlap matrix this->sR = new HContainer(paraV); - // this->hsk = new HS_Matrix_K(paraV); this->getOperator() = new OverlapNew>(this->hsk, kv.kvec_d, @@ -350,7 +348,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, } Operator* td_ekinetic = new TDEkinetic>(this->hsk, this->hR, - kv, + &kv, &ucell, orb.cutoffs(), &grid_d, @@ -419,6 +417,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, this->getOperator()->add(exx); } #endif + // if NSPIN==2, HR should be separated into two parts, save HR into this->hRS2 int memory_fold = 1; if (PARAM.inp.nspin == 2) @@ -499,7 +498,7 @@ Operator*& HamiltLCAO::getOperator() template void HamiltLCAO::updateSk( const int ik, - std::vector>& kvec_d, + const ModuleBase::Vector3& kvec_d, const int hk_type) { ModuleBase::TITLE("HamiltLCAO", "updateSk"); @@ -510,12 +509,12 @@ void HamiltLCAO::updateSk( if (hk_type == 1) // collumn-major matrix for SK { const int nrow = this->hsk->get_pv()->get_row_size(); - hamilt::folding_HR(*this->sR, this->getSk(), kvec_d[ik], nrow, 1); + hamilt::folding_HR(*this->sR, this->getSk(), kvec_d, nrow, 1); } else if (hk_type == 0) // row-major matrix for SK { const int ncol = this->hsk->get_pv()->get_col_size(); - hamilt::folding_HR(*this->sR, this->getSk(), kvec_d[ik], ncol, 0); + hamilt::folding_HR(*this->sR, this->getSk(), kvec_d, ncol, 0); } else { diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h index 1995c2e4025..10dbd6463f1 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h +++ b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h @@ -140,39 +140,40 @@ class HamiltLCAO : public Hamilt * @return void */ void updateSk(const int ik, - std::vector>& kvec_d, + const ModuleBase::Vector3& kvec_d, const int hk_type = 0); // core function: return H(k) and S(k) matrixs for direct solving eigenvalues. // not used in PW base - // void matrix(MatrixBlock> &hk_in, MatrixBlock> &sk_in) override; void matrix(MatrixBlock& hk_in, MatrixBlock& sk_in) override; private: - // Real space Hamiltonian + //! Real space Hamiltonian H(R), where R is the Bravis lattice vector HContainer* hR = nullptr; + + //! Real space overlap matrix S(R), where R is the Bravis lattice vector HContainer* sR = nullptr; #ifdef __DEEPKS HContainer* V_delta_R = nullptr; #endif + //! Hamiltonian and overlap matrices for a specific k point HS_Matrix_K* hsk = nullptr; // special case for NSPIN=2 , data of HR should be separated into two parts // save them in this->hRS2; std::vector hRS2; + int refresh_times = 1; - /// current_spin for NSPIN=2, 0: hamiltonian for spin up, 1: hamiltonian for spin down + //! current_spin for NSPIN=2 case + //! 0: Hamiltonian for spin up, + //! 1: Hamiltonian for spin down int current_spin = 0; const int istep = 0; - - // sk and hk will be refactored to HamiltLCAO later - // std::vector sk; - // std::vector hk; }; } // namespace hamilt diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_ekinetic_lcao.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_ekinetic_lcao.cpp index 1642a5635d9..f48af2e65f0 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_ekinetic_lcao.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_ekinetic_lcao.cpp @@ -388,8 +388,11 @@ void TDEkinetic, double>>::contributeHk(int ik const Parallel_Orbitals* paraV = this->hR_tmp->get_atom_pair(0).get_paraV(); // save HR data for output int spin_tot = PARAM.inp.nspin; + if (spin_tot == 4) - ; + { + + } else if (!output_hR_done && TD_Velocity::out_mat_R) { for (int spin_now = 0; spin_now < spin_tot; spin_now++) @@ -402,6 +405,7 @@ void TDEkinetic, double>>::contributeHk(int ik } output_hR_done = true; } + // folding inside HR to HK if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { diff --git a/source/module_hamilt_lcao/module_dftu/dftu.h b/source/module_hamilt_lcao/module_dftu/dftu.h index cd7afa001f5..86856da5c4e 100644 --- a/source/module_hamilt_lcao/module_dftu/dftu.h +++ b/source/module_hamilt_lcao/module_dftu/dftu.h @@ -1,7 +1,3 @@ -//========================================================== -// Author: Xin Qu -// DATE : 2019-12-10 -//========================================================== #ifndef DFTU_H #define DFTU_H @@ -187,7 +183,7 @@ class DFTU const int dim1, const int dim2, std::complex* mat_k, - const std::vector>& kvec_d); + const ModuleBase::Vector3& kvec_d); /** * @brief new function of folding_S_matrix @@ -195,7 +191,7 @@ class DFTU * use HContainer as input and output in mat_k */ void folding_matrix_k_new(const int ik, - std::vector>& kvec_d, + const ModuleBase::Vector3& kvec_d, hamilt::Hamilt>* p_ham); //============================================================= @@ -220,7 +216,7 @@ class DFTU const int ik, const std::complex* rho_VU, ModuleBase::matrix& force_dftu, - const std::vector>& kvec_d); + const ModuleBase::Vector3& kvec_d); void cal_stress_k(const UnitCell& ucell, const Grid_Driver& gd, @@ -229,7 +225,7 @@ class DFTU const int ik, const std::complex* rho_VU, ModuleBase::matrix& stress_dftu, - const std::vector>& kvec_d); + const ModuleBase::Vector3& kvec_d); void cal_force_gamma(const UnitCell& ucell, const double* rho_VU, diff --git a/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp b/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp index b99fb0d6fd7..407a00eebd0 100644 --- a/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp +++ b/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp @@ -133,7 +133,7 @@ void DFTU::folding_matrix_k(const UnitCell& ucell, const int dim1, const int dim2, std::complex* mat_k, - const std::vector>& kvec_d) + const ModuleBase::Vector3& kvec_d) { ModuleBase::TITLE("DFTU", "folding_matrix_k"); ModuleBase::timer::tick("DFTU", "folding_matrix_k"); @@ -224,7 +224,7 @@ void DFTU::folding_matrix_k(const UnitCell& ucell, // dR is the index of box in Crystal coordinates //------------------------------------------------ ModuleBase::Vector3 dR(gd.getBox(ad).x, gd.getBox(ad).y, gd.getBox(ad).z); - const double arg = (kvec_d[ik] * dR) * ModuleBase::TWO_PI; + const double arg = (kvec_d * dR) * ModuleBase::TWO_PI; const std::complex kphase = std::complex(cos(arg), sin(arg)); //-------------------------------------------------- @@ -279,7 +279,7 @@ void DFTU::folding_matrix_k(const UnitCell& ucell, } void DFTU::folding_matrix_k_new(const int ik, - std::vector>& kvec_d, + const ModuleBase::Vector3& kvec_d, hamilt::Hamilt>* p_ham) { ModuleBase::TITLE("DFTU", "folding_matrix_k_new"); @@ -307,7 +307,7 @@ void DFTU::folding_matrix_k_new(const int ik, else { dynamic_cast, std::complex>*>(p_ham) - ->updateSk(ik, hk_type); + ->updateSk(ik, kvec_d, hk_type); } } } diff --git a/source/module_hamilt_lcao/module_dftu/dftu_force.cpp b/source/module_hamilt_lcao/module_dftu/dftu_force.cpp index 8d06d9a92f9..f4423f623d6 100644 --- a/source/module_hamilt_lcao/module_dftu/dftu_force.cpp +++ b/source/module_hamilt_lcao/module_dftu/dftu_force.cpp @@ -209,11 +209,11 @@ void DFTU::force_stress(const UnitCell& ucell, if (PARAM.inp.cal_force) { - cal_force_k(ucell, gd, fsr, pv, ik, &rho_VU[0], force_dftu, kv.kvec_d); + cal_force_k(ucell, gd, fsr, pv, ik, &rho_VU[0], force_dftu, kv.kvec_d[ik]); } if (PARAM.inp.cal_stress) { - cal_stress_k(ucell, gd, fsr, pv, ik, &rho_VU[0], stress_dftu, kv.kvec_d); + cal_stress_k(ucell, gd, fsr, pv, ik, &rho_VU[0], stress_dftu, kv.kvec_d[ik]); } } // ik } @@ -256,7 +256,7 @@ void DFTU::cal_force_k(const UnitCell& ucell, const int ik, const std::complex* rho_VU, ModuleBase::matrix& force_dftu, - const std::vector>& kvec_d) + const ModuleBase::Vector3& kvec_d) { ModuleBase::TITLE("DFTU", "cal_force_k"); ModuleBase::timer::tick("DFTU", "cal_force_k"); @@ -386,7 +386,7 @@ void DFTU::cal_stress_k(const UnitCell& ucell, const int ik, const std::complex* rho_VU, ModuleBase::matrix& stress_dftu, - const std::vector>& kvec_d) + const ModuleBase::Vector3& kvec_d) { ModuleBase::TITLE("DFTU", "cal_stress_k"); ModuleBase::timer::tick("DFTU", "cal_stress_k"); @@ -662,4 +662,4 @@ void DFTU::cal_stress_gamma(const UnitCell& ucell, return; } } // namespace ModuleDFTU -#endif \ No newline at end of file +#endif diff --git a/source/module_hamilt_lcao/module_dftu/dftu_occup.cpp b/source/module_hamilt_lcao/module_dftu/dftu_occup.cpp index 63b7af029e0..29e9547305e 100644 --- a/source/module_hamilt_lcao/module_dftu/dftu_occup.cpp +++ b/source/module_hamilt_lcao/module_dftu/dftu_occup.cpp @@ -175,7 +175,7 @@ void DFTU::cal_occup_m_k(const int iter, for (int ik = 0; ik < kv.get_nks(); ik++) { // srho(mu,nu) = \sum_{iw} S(mu,iw)*dm_k(iw,nu) - this->folding_matrix_k_new(ik, kv.kvec_d, p_ham); + this->folding_matrix_k_new(ik, kv.kvec_d[ik], p_ham); std::complex* s_k_pointer = nullptr; diff --git a/source/module_io/write_dos_lcao.cpp b/source/module_io/write_dos_lcao.cpp index 14beb2830c5..0c7a3090af0 100644 --- a/source/module_io/write_dos_lcao.cpp +++ b/source/module_io/write_dos_lcao.cpp @@ -448,15 +448,25 @@ void ModuleIO::write_dos_lcao(const UnitCell& ucell, { // calculate SK for current k point const std::complex* sk = nullptr; + + // collumn-major matrix + const int hk_type = 1; + if (PARAM.inp.nspin == 4) { - dynamic_cast, std::complex>*>(p_ham)->updateSk(ik, 1); - sk = dynamic_cast, std::complex>*>(p_ham)->getSk(); + dynamic_cast, std::complex>*>(p_ham) + ->updateSk(ik, kv.kvec_d[ik], hk_type); + + sk = dynamic_cast, std::complex>*>(p_ham) + ->getSk(); } else { - dynamic_cast, double>*>(p_ham)->updateSk(ik, 1); - sk = dynamic_cast, double>*>(p_ham)->getSk(); + dynamic_cast, double>*>(p_ham) + ->updateSk(ik, kv.kvec_d[ik], hk_type); + + sk = dynamic_cast, double>*>(p_ham) + ->getSk(); } psi->fix_k(ik); @@ -547,128 +557,125 @@ void ModuleIO::write_dos_lcao(const UnitCell& ucell, if (GlobalV::MY_RANK == 0) { - { - std::stringstream ps; - ps << PARAM.globalv.global_out_dir << "TDOS"; - std::ofstream out(ps.str().c_str()); - if (PARAM.inp.nspin == 1 || PARAM.inp.nspin == 4) - { - - for (int n = 0; n < npoints; ++n) - { - double y = 0.0; - double en = emin + n * de_ev; - for (int i = 0; i < PARAM.globalv.nlocal; i++) - { - y += pdos[0](i, n); - } - - out << std::setw(20) << en << std::setw(30) << y << std::endl; - } - } - else if (PARAM.inp.nspin == 2) - { - for (int n = 0; n < npoints; ++n) - { - double y = 0.0; - double z = 0.0; - double en = emin + n * de_ev; - for (int i = 0; i < PARAM.globalv.nlocal; i++) - { - y += pdos[0](i, n); - z += pdos[1](i, n); - } - - out << std::setw(20) << en << std::setw(30) << y << std::setw(30) << z << std::endl; - } - } - out.close(); - } + std::stringstream ps; + ps << PARAM.globalv.global_out_dir << "TDOS"; + std::ofstream ofs1(ps.str().c_str()); + if (PARAM.inp.nspin == 1 || PARAM.inp.nspin == 4) + { + + for (int n = 0; n < npoints; ++n) + { + double y = 0.0; + double en = emin + n * de_ev; + for (int i = 0; i < PARAM.globalv.nlocal; i++) + { + y += pdos[0](i, n); + } + + ofs1 << std::setw(20) << en << std::setw(30) << y << std::endl; + } + } + else if (PARAM.inp.nspin == 2) + { + for (int n = 0; n < npoints; ++n) + { + double y = 0.0; + double z = 0.0; + double en = emin + n * de_ev; + for (int i = 0; i < PARAM.globalv.nlocal; i++) + { + y += pdos[0](i, n); + z += pdos[1](i, n); + } + + ofs1 << std::setw(20) << en << std::setw(30) << y << std::setw(30) << z << std::endl; + } + } + ofs1.close(); /* decomposed Mulliken charge */ - { - std::stringstream as; - as << PARAM.globalv.global_out_dir << "PDOS"; - std::ofstream out(as.str().c_str()); - - out << "" << std::endl; - out << "" << PARAM.inp.nspin << "" << std::endl; - if (PARAM.inp.nspin == 4) - { - out << "" << std::setw(2) << PARAM.globalv.nlocal / 2 << "" << std::endl; - } - else - { - out << "" << std::setw(2) << PARAM.globalv.nlocal << "" << std::endl; - } - out << "" << std::endl; - - for (int n = 0; n < npoints; ++n) - { - double y = 0.0; - double en = emin + n * de_ev; - out << std::setw(20) << en << std::endl; - } - out << "" << std::endl; - for (int i = 0; i < ucell.nat; i++) - { - int a = ucell.iat2ia[i]; - int t = ucell.iat2it[i]; - Atom* atom1 = &ucell.atoms[t]; - const int s0 = ucell.itiaiw2iwt(t, a, 0); - for (int j = 0; j < atom1->nw; ++j) - { - const int L1 = atom1->iw2l[j]; - const int N1 = atom1->iw2n[j]; - const int m1 = atom1->iw2m[j]; - const int w = ucell.itiaiw2iwt(t, a, j); - - // out << "" <" << std::endl; - out << "" << std::endl; - if (PARAM.inp.nspin == 1) - { - for (int n = 0; n < npoints; ++n) - { - out << std::setw(13) << pdos[0](w, n) << std::endl; - } // n - } - else if (PARAM.inp.nspin == 2) - { - for (int n = 0; n < npoints; ++n) - { - out << std::setw(20) << pdos[0](w, n) << std::setw(30) << pdos[1](w, n) << std::endl; - } // n - } - else if (PARAM.inp.nspin == 4) - { - int w0 = w - s0; - for (int n = 0; n < npoints; ++n) - { - out << std::setw(20) << pdos[0](s0 + 2 * w0, n) + pdos[0](s0 + 2 * w0 + 1, n) << std::endl; - } // n - } - - out << "" << std::endl; - out << "" << std::endl; - } // j - } // i - - out << "" << std::endl; - out.close(); - } - ModuleIO::write_orb_info(&(ucell)); + std::stringstream as; + as << PARAM.globalv.global_out_dir << "PDOS"; + std::ofstream ofs2(as.str().c_str()); + + ofs2 << "" << std::endl; + ofs2 << "" << PARAM.inp.nspin << "" << std::endl; + if (PARAM.inp.nspin == 4) + { + ofs2 << "" << std::setw(2) << PARAM.globalv.nlocal / 2 << "" << std::endl; + } + else + { + ofs2 << "" << std::setw(2) << PARAM.globalv.nlocal << "" << std::endl; + } + ofs2 << "" << std::endl; + + for (int n = 0; n < npoints; ++n) + { + double y = 0.0; + double en = emin + n * de_ev; + ofs2 << std::setw(20) << en << std::endl; + } + ofs2 << "" << std::endl; + for (int i = 0; i < ucell.nat; i++) + { + int a = ucell.iat2ia[i]; + int t = ucell.iat2it[i]; + Atom* atom1 = &ucell.atoms[t]; + const int s0 = ucell.itiaiw2iwt(t, a, 0); + for (int j = 0; j < atom1->nw; ++j) + { + const int L1 = atom1->iw2l[j]; + const int N1 = atom1->iw2n[j]; + const int m1 = atom1->iw2m[j]; + const int w = ucell.itiaiw2iwt(t, a, j); + + // ofs2 << "" <" << std::endl; + ofs2 << "" << std::endl; + if (PARAM.inp.nspin == 1) + { + for (int n = 0; n < npoints; ++n) + { + ofs2 << std::setw(13) << pdos[0](w, n) << std::endl; + } // n + } + else if (PARAM.inp.nspin == 2) + { + for (int n = 0; n < npoints; ++n) + { + ofs2 << std::setw(20) << pdos[0](w, n) << std::setw(30) << pdos[1](w, n) << std::endl; + } // n + } + else if (PARAM.inp.nspin == 4) + { + int w0 = w - s0; + for (int n = 0; n < npoints; ++n) + { + ofs2 << std::setw(20) << pdos[0](s0 + 2 * w0, n) + pdos[0](s0 + 2 * w0 + 1, n) << std::endl; + } // n + } + + ofs2 << "" << std::endl; + ofs2 << "" << std::endl; + } // j + } // i + + ofs2 << "" << std::endl; + ofs2.close(); + ModuleIO::write_orb_info(&(ucell)); } delete[] pdos; - } + }// end PARAM.inp.out_dos == 2 + // output the DOS file. for (int is = 0; is < nspin0; ++is) { From 119872f3f8285f29ed1e2903847f043d97fe873c Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 1 Apr 2025 09:23:47 +0800 Subject: [PATCH 26/35] update updateHk funciton parameters --- source/module_esolver/lcao_after_scf.cpp | 1 + .../module_deepks/LCAO_deepks_interface.cpp | 6 +++++- .../module_deepks/LCAO_deepks_interface.h | 3 ++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/source/module_esolver/lcao_after_scf.cpp b/source/module_esolver/lcao_after_scf.cpp index 73fb309a059..d53743b1c77 100644 --- a/source/module_esolver/lcao_after_scf.cpp +++ b/source/module_esolver/lcao_after_scf.cpp @@ -221,6 +221,7 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const PARAM.globalv.nlocal, this->pelec->ekb, this->pelec->klist->kvec_d, + this->pelec->klist->isk, ucell, orb_, this->gd, diff --git a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp index 6a3626712ce..3fc4c886cf1 100644 --- a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp +++ b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp @@ -21,6 +21,7 @@ void LCAO_Deepks_Interface::out_deepks_labels(const double& etot, const int& nlocal, const ModuleBase::matrix& ekb, const std::vector>& kvec_d, + const std::vector& isk, // mohan add 2025-04-01 const UnitCell& ucell, const LCAO_Orbitals& orb, const Grid_Driver& GridD, @@ -337,8 +338,11 @@ void LCAO_Deepks_Interface::out_deepks_labels(const double& etot, for (int ik = 0; ik < nks; ik++) { h_tot[ik].create(nlocal, nlocal); - p_ham->updateHk(ik); + + p_ham->updateHk(ik, isk[ik]); + const TK* hk_ptr = p_ham->getHk(); + for (int i = 0; i < ParaV->nloc; i++) { h_mat[ik][i] = hk_ptr[i]; diff --git a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.h b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.h index 2f32c664782..e3e45366392 100644 --- a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.h +++ b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.h @@ -37,6 +37,7 @@ class LCAO_Deepks_Interface const int& nlocal, const ModuleBase::matrix& ekb, const std::vector>& kvec_d, + const std::vector& isk, // mohan add 2025-04-01 const UnitCell& ucell, const LCAO_Orbitals& orb, const Grid_Driver& GridD, @@ -51,4 +52,4 @@ class LCAO_Deepks_Interface }; #endif -#endif \ No newline at end of file +#endif From 975c2ccdfba54b8a2d5709b43d4922ae60d349ae Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 1 Apr 2025 18:14:05 +0800 Subject: [PATCH 27/35] fix bug --- .../hamilt_lcaodft/hamilt_lcao.cpp | 64 ++++++++++--------- .../hamilt_lcaodft/hamilt_lcao.h | 8 ++- 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp index 240e3378d5b..f830d56c646 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp @@ -47,17 +47,19 @@ template HamiltLCAO::HamiltLCAO(const UnitCell& ucell, const Grid_Driver& grid_d, const Parallel_Orbitals* paraV, - const K_Vectors& kv, + const K_Vectors& kv_in, const TwoCenterIntegrator& intor_overlap_orb, const std::vector& orb_cutoff) { this->classname = "HamiltLCAO"; + this->kv = &kv_in; + // initialize the overlap matrix this->sR = new HContainer(paraV); this->getOperator() = new OverlapNew>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, this->sR, &ucell, @@ -73,7 +75,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, const Grid_Driver& grid_d, const Parallel_Orbitals* paraV, elecstate::Potential* pot_in, - const K_Vectors& kv, + const K_Vectors& kv_in, const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, elecstate::DensityMatrix* DM_in @@ -92,6 +94,8 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, { this->classname = "HamiltLCAO"; + this->kv = &kv_in; + // Real space Hamiltonian is inited with template TR this->hR = new HContainer(paraV); this->sR = new HContainer(paraV); @@ -139,7 +143,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, // overlap term () is indispensable // in Gamma_only case, target SK is this->hsk->get_sk(), the target SR is this->sR this->getOperator() = new OverlapNew>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, this->sR, &ucell, @@ -151,7 +155,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.t_in_h) { Operator* ekinetic = new EkineticNew>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, &ucell, orb.cutoffs(), @@ -165,7 +169,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.vnl_in_h) { Operator* nonlocal = new NonlocalNew>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, &ucell, orb.cutoffs(), @@ -186,7 +190,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, // effective potential term Operator* veff = new Veff>(GG_in, this->hsk, - kv.kvec_d, + this->kv->kvec_d, pot_in, this->hR, // no explicit call yet &ucell, @@ -201,13 +205,13 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.deepks_scf) { Operator* deepks = new DeePKS>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, // no explicit call yet &ucell, &grid_d, two_center_bundle.overlap_orb_alpha.get(), &orb, - kv.get_nks(), + this->kv->get_nks(), DM_in, ld_in); this->getOperator()->add(deepks); @@ -222,14 +226,14 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.dft_plus_u == 2) { dftu = new OperatorDFTU>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, // no explicit call yet - kv.isk); + this->kv->isk); } else { dftu = new DFTU>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, ucell, &grid_d, @@ -256,7 +260,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, // Veff term this->getOperator() = new Veff>(GK_in, this->hsk, - kv.kvec_d, + this->kv->kvec_d, pot_in, this->hR, &ucell, @@ -269,7 +273,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, // initial operator for multi-k case // overlap term is indispensable Operator* overlap = new OverlapNew>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, this->sR, &ucell, @@ -290,7 +294,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.t_in_h) { Operator* ekinetic = new EkineticNew>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, &ucell, orb.cutoffs(), @@ -304,7 +308,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.vnl_in_h) { Operator* nonlocal = new NonlocalNew>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, &ucell, orb.cutoffs(), @@ -326,13 +330,13 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.deepks_scf) { Operator* deepks = new DeePKS>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, hR, &ucell, &grid_d, two_center_bundle.overlap_orb_alpha.get(), &orb, - kv.get_nks(), + this->kv->get_nks(), DM_in, ld_in); this->getOperator()->add(deepks); @@ -348,7 +352,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, } Operator* td_ekinetic = new TDEkinetic>(this->hsk, this->hR, - &kv, + this->kv, &ucell, orb.cutoffs(), &grid_d, @@ -356,7 +360,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, this->getOperator()->add(td_ekinetic); Operator* td_nonlocal - = new TDNonlocal>(this->hsk, kv.kvec_d, this->hR, &ucell, orb, &grid_d); + = new TDNonlocal>(this->hsk, this->kv->kvec_d, this->hR, &ucell, orb, &grid_d); this->getOperator()->add(td_nonlocal); } if (PARAM.inp.dft_plus_u) @@ -365,14 +369,14 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.dft_plus_u == 2) { dftu = new OperatorDFTU>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, // no explicit call yet - kv.isk); + this->kv->isk); } else { dftu = new DFTU>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, ucell, &grid_d, @@ -385,7 +389,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, if (PARAM.inp.sc_mag_switch) { Operator* sc_lambda = new DeltaSpin>(this->hsk, - kv.kvec_d, + this->kv->kvec_d, this->hR, ucell, &grid_d, @@ -406,7 +410,7 @@ HamiltLCAO::HamiltLCAO(Gint_Gamma* GG_in, Operator* exx = new OperatorEXX>(this->hsk, this->hR, ucell, - kv, + *kv, Hexxd, Hexxc, Add_Hexx_Type::R, @@ -443,28 +447,26 @@ void HamiltLCAO::matrix(MatrixBlock& hk_in, MatrixBlock& sk_in) } template -void HamiltLCAO::updateHk(const int ik, const int isk) +void HamiltLCAO::updateHk(const int ik) { ModuleBase::TITLE("HamiltLCAO", "updateHk"); ModuleBase::timer::tick("HamiltLCAO", "updateHk"); - // isk is kv.isk[ik], mohan note 2025-03-31 - // update global spin index if (PARAM.inp.nspin == 2) { // if Veff is added and current_spin is changed, refresh HR - if (PARAM.inp.vl_in_h && isk != this->current_spin) + if (PARAM.inp.vl_in_h && this->kv->isk[ik] != this->current_spin) { // change data pointer of HR - this->hR->allocate(this->hRS2.data() + this->hRS2.size() / 2 * isk, 0); + this->hR->allocate(this->hRS2.data() + this->hRS2.size() / 2 * this->kv->isk[ik], 0); if (this->refresh_times > 0) { this->refresh_times--; dynamic_cast*>(this->ops)->set_hr_done(false); } } - this->current_spin = isk; + this->current_spin = this->kv->isk[ik]; } this->getOperator()->init(ik); ModuleBase::timer::tick("HamiltLCAO", "updateHk"); diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h index 10dbd6463f1..f611148c494 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h +++ b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h @@ -47,7 +47,7 @@ class HamiltLCAO : public Hamilt const Grid_Driver& grid_d, const Parallel_Orbitals* paraV, elecstate::Potential* pot_in, - const K_Vectors& kv, + const K_Vectors& kv_in, const TwoCenterBundle& two_center_bundle, const LCAO_Orbitals& orb, elecstate::DensityMatrix* DM_in @@ -70,7 +70,7 @@ class HamiltLCAO : public Hamilt HamiltLCAO(const UnitCell& ucell, const Grid_Driver& grid_d, const Parallel_Orbitals* paraV, - const K_Vectors& kv, + const K_Vectors& kv_in, const TwoCenterIntegrator& intor_overlap_orb, const std::vector& orb_cutoff); @@ -129,7 +129,7 @@ class HamiltLCAO : public Hamilt void refresh() override; // for target K point, update consequence of hPsi() and matrix() - void updateHk(const int ik, const int isk); + virtual void updateHk(const int ik) override; /** * @brief special for LCAO, update SK only @@ -149,6 +149,8 @@ class HamiltLCAO : public Hamilt private: + const K_Vectors* kv = nullptr; + //! Real space Hamiltonian H(R), where R is the Bravis lattice vector HContainer* hR = nullptr; From 02eb1de8b9f9f2dae2a2309644437bc701595a04 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 1 Apr 2025 18:20:46 +0800 Subject: [PATCH 28/35] fix bug in updateHk --- source/module_esolver/lcao_after_scf.cpp | 1 - .../module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp | 3 +-- .../module_hamilt_lcao/module_deepks/LCAO_deepks_interface.h | 1 - 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/source/module_esolver/lcao_after_scf.cpp b/source/module_esolver/lcao_after_scf.cpp index d53743b1c77..73fb309a059 100644 --- a/source/module_esolver/lcao_after_scf.cpp +++ b/source/module_esolver/lcao_after_scf.cpp @@ -221,7 +221,6 @@ void ESolver_KS_LCAO::after_scf(UnitCell& ucell, const int istep, const PARAM.globalv.nlocal, this->pelec->ekb, this->pelec->klist->kvec_d, - this->pelec->klist->isk, ucell, orb_, this->gd, diff --git a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp index 3fc4c886cf1..48908e43c03 100644 --- a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp +++ b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.cpp @@ -21,7 +21,6 @@ void LCAO_Deepks_Interface::out_deepks_labels(const double& etot, const int& nlocal, const ModuleBase::matrix& ekb, const std::vector>& kvec_d, - const std::vector& isk, // mohan add 2025-04-01 const UnitCell& ucell, const LCAO_Orbitals& orb, const Grid_Driver& GridD, @@ -339,7 +338,7 @@ void LCAO_Deepks_Interface::out_deepks_labels(const double& etot, { h_tot[ik].create(nlocal, nlocal); - p_ham->updateHk(ik, isk[ik]); + p_ham->updateHk(ik); const TK* hk_ptr = p_ham->getHk(); diff --git a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.h b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.h index e3e45366392..075650b3f02 100644 --- a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.h +++ b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_interface.h @@ -37,7 +37,6 @@ class LCAO_Deepks_Interface const int& nlocal, const ModuleBase::matrix& ekb, const std::vector>& kvec_d, - const std::vector& isk, // mohan add 2025-04-01 const UnitCell& ucell, const LCAO_Orbitals& orb, const Grid_Driver& GridD, From 8c260955345c655dc4a5eb4ca5659b3002eb5f4e Mon Sep 17 00:00:00 2001 From: mohanchen Date: Tue, 1 Apr 2025 20:19:53 +0800 Subject: [PATCH 29/35] update updateSk --- .../hamilt_lcaodft/hamilt_lcao.cpp | 5 ++--- .../hamilt_lcaodft/hamilt_lcao.h | 4 +--- source/module_hamilt_lcao/module_dftu/dftu.h | 1 - .../module_dftu/dftu_folding.cpp | 19 +++++++++++-------- .../module_dftu/dftu_occup.cpp | 2 +- source/module_io/write_dos_lcao.cpp | 4 ++-- 6 files changed, 17 insertions(+), 18 deletions(-) diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp index f830d56c646..8831d8a2749 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.cpp @@ -500,7 +500,6 @@ Operator*& HamiltLCAO::getOperator() template void HamiltLCAO::updateSk( const int ik, - const ModuleBase::Vector3& kvec_d, const int hk_type) { ModuleBase::TITLE("HamiltLCAO", "updateSk"); @@ -511,12 +510,12 @@ void HamiltLCAO::updateSk( if (hk_type == 1) // collumn-major matrix for SK { const int nrow = this->hsk->get_pv()->get_row_size(); - hamilt::folding_HR(*this->sR, this->getSk(), kvec_d, nrow, 1); + hamilt::folding_HR(*this->sR, this->getSk(), this->kv->kvec_d[ik], nrow, 1); } else if (hk_type == 0) // row-major matrix for SK { const int ncol = this->hsk->get_pv()->get_col_size(); - hamilt::folding_HR(*this->sR, this->getSk(), kvec_d, ncol, 0); + hamilt::folding_HR(*this->sR, this->getSk(), this->kv->kvec_d[ik], ncol, 0); } else { diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h index f611148c494..554e23cf301 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h +++ b/source/module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h @@ -139,9 +139,7 @@ class HamiltLCAO : public Hamilt * @param hk_type 0: SK is row-major, 1: SK is collumn-major * @return void */ - void updateSk(const int ik, - const ModuleBase::Vector3& kvec_d, - const int hk_type = 0); + void updateSk(const int ik, const int hk_type = 0); // core function: return H(k) and S(k) matrixs for direct solving eigenvalues. // not used in PW base diff --git a/source/module_hamilt_lcao/module_dftu/dftu.h b/source/module_hamilt_lcao/module_dftu/dftu.h index 86856da5c4e..2ec3cf1ed62 100644 --- a/source/module_hamilt_lcao/module_dftu/dftu.h +++ b/source/module_hamilt_lcao/module_dftu/dftu.h @@ -191,7 +191,6 @@ class DFTU * use HContainer as input and output in mat_k */ void folding_matrix_k_new(const int ik, - const ModuleBase::Vector3& kvec_d, hamilt::Hamilt>* p_ham); //============================================================= diff --git a/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp b/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp index 407a00eebd0..ee3b6413848 100644 --- a/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp +++ b/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp @@ -236,15 +236,19 @@ void DFTU::folding_matrix_k(const UnitCell& ucell, // the index of orbitals in this processor const int iw1_all = start1 + ii; const int mu = pv.global2local_row(iw1_all); - if (mu < 0) { continue; -} + if (mu < 0) + { + continue; + } for (int jj = 0; jj < atom2->nw * PARAM.globalv.npol; jj++) { int iw2_all = start2 + jj; const int nu = pv.global2local_col(iw2_all); - if (nu < 0) { continue; -} + if (nu < 0) + { + continue; + } int iic; if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) @@ -279,7 +283,6 @@ void DFTU::folding_matrix_k(const UnitCell& ucell, } void DFTU::folding_matrix_k_new(const int ik, - const ModuleBase::Vector3& kvec_d, hamilt::Hamilt>* p_ham) { ModuleBase::TITLE("DFTU", "folding_matrix_k_new"); @@ -295,19 +298,19 @@ void DFTU::folding_matrix_k_new(const int ik, if(PARAM.globalv.gamma_only_local) { dynamic_cast*>(p_ham) - ->updateSk(ik, kvec_d, hk_type); + ->updateSk(ik, hk_type); } else { if(PARAM.inp.nspin != 4) { dynamic_cast, double>*>(p_ham) - ->updateSk(ik, kvec_d, hk_type); + ->updateSk(ik, hk_type); } else { dynamic_cast, std::complex>*>(p_ham) - ->updateSk(ik, kvec_d, hk_type); + ->updateSk(ik, hk_type); } } } diff --git a/source/module_hamilt_lcao/module_dftu/dftu_occup.cpp b/source/module_hamilt_lcao/module_dftu/dftu_occup.cpp index 29e9547305e..a745010aa7a 100644 --- a/source/module_hamilt_lcao/module_dftu/dftu_occup.cpp +++ b/source/module_hamilt_lcao/module_dftu/dftu_occup.cpp @@ -175,7 +175,7 @@ void DFTU::cal_occup_m_k(const int iter, for (int ik = 0; ik < kv.get_nks(); ik++) { // srho(mu,nu) = \sum_{iw} S(mu,iw)*dm_k(iw,nu) - this->folding_matrix_k_new(ik, kv.kvec_d[ik], p_ham); + this->folding_matrix_k_new(ik, p_ham); std::complex* s_k_pointer = nullptr; diff --git a/source/module_io/write_dos_lcao.cpp b/source/module_io/write_dos_lcao.cpp index 0c7a3090af0..1d5efa8341b 100644 --- a/source/module_io/write_dos_lcao.cpp +++ b/source/module_io/write_dos_lcao.cpp @@ -455,7 +455,7 @@ void ModuleIO::write_dos_lcao(const UnitCell& ucell, if (PARAM.inp.nspin == 4) { dynamic_cast, std::complex>*>(p_ham) - ->updateSk(ik, kv.kvec_d[ik], hk_type); + ->updateSk(ik, hk_type); sk = dynamic_cast, std::complex>*>(p_ham) ->getSk(); @@ -463,7 +463,7 @@ void ModuleIO::write_dos_lcao(const UnitCell& ucell, else { dynamic_cast, double>*>(p_ham) - ->updateSk(ik, kv.kvec_d[ik], hk_type); + ->updateSk(ik, hk_type); sk = dynamic_cast, double>*>(p_ham) ->getSk(); From b66725a9202a9b577382860e97cb596defa7e350 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 2 Apr 2025 09:09:14 +0800 Subject: [PATCH 30/35] update read atoms --- source/module_cell/read_atoms.cpp | 44 ++++---- source/module_cell/read_stru.cpp | 163 +++++++++++++++--------------- source/module_cell/unitcell.cpp | 103 +++++++++++-------- source/module_cell/unitcell.h | 2 +- 4 files changed, 165 insertions(+), 147 deletions(-) diff --git a/source/module_cell/read_atoms.cpp b/source/module_cell/read_atoms.cpp index 78f825281f2..7ca38fcd996 100644 --- a/source/module_cell/read_atoms.cpp +++ b/source/module_cell/read_atoms.cpp @@ -17,19 +17,21 @@ #ifdef __LCAO #include "module_basis/module_ao/ORB_read.h" // to use 'ORB' -- mohan 2021-01-30 #endif -namespace unitcell -{ -bool read_atom_positions(UnitCell& ucell, + +bool unitcell::read_atom_positions(UnitCell& ucell, std::ifstream &ifpos, std::ofstream &ofs_running, std::ofstream &ofs_warning) { ModuleBase::TITLE("UnitCell","read_atom_positions"); + std::string& Coordinate = ucell.Coordinate; const int ntype = ucell.ntype; + if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifpos, "ATOMIC_POSITIONS")) { ModuleBase::GlobalFunc::READ_VALUE(ifpos, Coordinate); + if(Coordinate != "Cartesian" && Coordinate != "Direct" && Coordinate != "Cartesian_angstrom" @@ -190,12 +192,14 @@ bool read_atom_positions(UnitCell& ucell, if( (int)tmpid[0] < 0 ) { - std::cout << "read_atom_positions, mismatch in atom number for atom type: " << ucell.atoms[it].label << std::endl; + std::cout << "read_atom_positions, mismatch in atom number for atom type: " + << ucell.atoms[it].label << std::endl; exit(1); } bool input_vec_mag=false; bool input_angle_mag=false; + // read if catch goodbit before "\n" and "#" while ( (tmpid != "\n") && (ifpos.good()) && (tmpid !="#") ) { @@ -237,7 +241,9 @@ bool read_atom_positions(UnitCell& ucell, ifpos.putback(tmp); ifpos >> ucell.atoms[it].m_loc_[ia].y>>ucell.atoms[it].m_loc_[ia].z; ucell.atoms[it].m_loc_[ia].x=tmpamg; - ucell.atoms[it].mag[ia]=sqrt(pow(ucell.atoms[it].m_loc_[ia].x,2)+pow(ucell.atoms[it].m_loc_[ia].y,2)+pow(ucell.atoms[it].m_loc_[ia].z,2)); + ucell.atoms[it].mag[ia]=sqrt(pow(ucell.atoms[it].m_loc_[ia].x,2) + +pow(ucell.atoms[it].m_loc_[ia].y,2) + +pow(ucell.atoms[it].m_loc_[ia].z,2)); input_vec_mag=true; } @@ -246,8 +252,6 @@ bool read_atom_positions(UnitCell& ucell, ifpos.putback(tmp); ucell.atoms[it].mag[ia]=tmpamg; } - - // ucell.atoms[it].mag[ia]; } else if ( tmpid == "angle1") { @@ -315,7 +319,6 @@ bool read_atom_positions(UnitCell& ucell, tmpid = ifpos.get(); } std::string mags; - //cout<<"mag"< diff = 0.0; - double norm = 0.0; - double tolerence_bohr = 1.0e-3; + ModuleBase::TITLE("UnitCell","check_tau"); + ModuleBase::timer::tick("UnitCell","check_tau"); + + ModuleBase::Vector3 diff = 0.0; + double norm = 0.0; + double tolerence_bohr = 1.0e-3; + + for(int T1=0; T1< ntype; T1++) + { + for(int I1=0; I1< atoms[T1].na; I1++) + { + double shortest_norm = 10000.0; // a large number + for(int T2=0; T2 norm ) + { + shortest_norm = norm; + } + if( norm < tolerence_bohr ) // unit is Bohr + { + GlobalV::ofs_warning << " two atoms are too close!" << std::endl; + GlobalV::ofs_warning << " type:" << atoms[T1].label << " atom " << I1 + 1 << std::endl; + GlobalV::ofs_warning << " type:" << atoms[T2].label << " atom " << I2 + 1 << std::endl; + GlobalV::ofs_warning << " distance = " << norm << " Bohr" << std::endl; + return false; + } + } + } + } + } + } + ModuleBase::timer::tick("UnitCell","check_tau"); + return true; +} - for(int T1=0; T1< ntype; T1++) - { - for(int I1=0; I1< atoms[T1].na; I1++) - { - double shortest_norm = 10000.0; // a large number - for(int T2=0; T2 norm ) - { - shortest_norm = norm; - } - if( norm < tolerence_bohr ) // unit is Bohr - { - GlobalV::ofs_warning << " two atoms are too close!" << std::endl; - GlobalV::ofs_warning << " type:" << atoms[T1].label << " atom " << I1 + 1 << std::endl; - GlobalV::ofs_warning << " type:" << atoms[T2].label << " atom " << I2 + 1 << std::endl; - GlobalV::ofs_warning << " distance = " << norm << " Bohr" << std::endl; - return false; - } - } - } - } - } - } - ModuleBase::timer::tick("UnitCell","check_tau"); - return true; - } - void check_dtau(Atom* atoms, - const int& ntype, - const double& lat0, - ModuleBase::Matrix3& latvec) - { - for(int it=0; ittaud[ia].x=fmod(atom1->taud[ia].x + 10000,1.0); - atom1->taud[ia].y=fmod(atom1->taud[ia].y + 10000,1.0); - atom1->taud[ia].z=fmod(atom1->taud[ia].z + 10000,1.0); +void unitcell::check_dtau(Atom* atoms, + const int& ntype, + const double& lat0, + ModuleBase::Matrix3& latvec) +{ + for(int it=0; ittaud[ia].x=fmod(atom1->taud[ia].x + 10000,1.0); + atom1->taud[ia].y=fmod(atom1->taud[ia].y + 10000,1.0); + atom1->taud[ia].z=fmod(atom1->taud[ia].z + 10000,1.0); + + double cx2=0.0; + double cy2=0.0; + double cz2=0.0; - double cx2=0.0; - double cy2=0.0; - double cz2=0.0; + ModuleBase::Mathzone::Direct_to_Cartesian( + atom1->taud[ia].x, atom1->taud[ia].y, atom1->taud[ia].z, + latvec.e11, latvec.e12, latvec.e13, + latvec.e21, latvec.e22, latvec.e23, + latvec.e31, latvec.e32, latvec.e33, + cx2, cy2, cz2); - ModuleBase::Mathzone::Direct_to_Cartesian( - atom1->taud[ia].x, atom1->taud[ia].y, atom1->taud[ia].z, - latvec.e11, latvec.e12, latvec.e13, - latvec.e21, latvec.e22, latvec.e23, - latvec.e31, latvec.e32, latvec.e33, - cx2, cy2, cz2); + atom1->tau[ia].x = cx2; + atom1->tau[ia].y = cy2; + atom1->tau[ia].z = cz2; - atom1->tau[ia].x = cx2; - atom1->tau[ia].y = cy2; - atom1->tau[ia].z = cz2; - - } - } - return; - } -} \ No newline at end of file + } + } + return; +} diff --git a/source/module_cell/unitcell.cpp b/source/module_cell/unitcell.cpp index 5f4dd37c866..3371a531d9f 100755 --- a/source/module_cell/unitcell.cpp +++ b/source/module_cell/unitcell.cpp @@ -340,9 +340,6 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) { log, "Reciprocal vectors: (Cartesian coordinate: in unit of 2 pi/a_0)", G); - // OUT(log,"lattice center x",latcenter.x); - // OUT(log,"lattice center y",latcenter.y); - // OUT(log,"lattice center z",latcenter.z); //=================================== // set index for iat2it, iat2ia @@ -350,18 +347,21 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) { this->set_iat2itia(); #ifdef USE_PAW - if (PARAM.inp.use_paw) { + if (PARAM.inp.use_paw) + { GlobalC::paw_cell.set_libpaw_cell(latvec, lat0); - int* typat; - double* xred; + int* typat = nullptr; + double* xred = nullptr; typat = new int[nat]; xred = new double[nat * 3]; int iat = 0; - for (int it = 0; it < ntype; it++) { - for (int ia = 0; ia < atoms[it].na; ia++) { + for (int it = 0; it < ntype; it++) + { + for (int ia = 0; ia < atoms[it].na; ia++) + { typat[iat] = it + 1; // Fortran index starts from 1 !!!! xred[iat * 3 + 0] = atoms[it].taud[ia].x; xred[iat * 3 + 1] = atoms[it].taud[ia].y; @@ -384,7 +384,8 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) { } -void UnitCell::set_iat2iwt(const int& npol_in) { +void UnitCell::set_iat2iwt(const int& npol_in) +{ #ifdef __DEBUG assert(npol_in == 1 || npol_in == 2); assert(this->nat > 0); @@ -394,8 +395,11 @@ void UnitCell::set_iat2iwt(const int& npol_in) { this->npol = npol_in; int iat = 0; int iwt = 0; - for (int it = 0; it < this->ntype; it++) { - for (int ia = 0; ia < atoms[it].na; ia++) { + + for (int it = 0; it < this->ntype; it++) + { + for (int ia = 0; ia < atoms[it].na; ia++) + { this->iat2iwt[iat] = iwt; iwt += atoms[it].nw * this->npol; ++iat; @@ -407,25 +411,31 @@ void UnitCell::set_iat2iwt(const int& npol_in) { // check if any atom can be moved -bool UnitCell::if_atoms_can_move() const { - for (int it = 0; it < this->ntype; it++) { +bool UnitCell::if_atoms_can_move() const +{ + for (int it = 0; it < this->ntype; it++) + { Atom* atom = &atoms[it]; - for (int ia = 0; ia < atom->na; ia++) { - if (atom->mbl[ia].x || atom->mbl[ia].y || atom->mbl[ia].z) { - return true; -} - } - } + for (int ia = 0; ia < atom->na; ia++) + { + if (atom->mbl[ia].x || atom->mbl[ia].y || atom->mbl[ia].z) + { + return true; + } + } + } return false; } // check if lattice vector can be changed -bool UnitCell::if_cell_can_change() const { - // need to be fixed next - if (this->lc[0] || this->lc[1] || this->lc[2]) { - return true; - } - return false; +bool UnitCell::if_cell_can_change() const +{ + // need to be fixed next + if (this->lc[0] || this->lc[1] || this->lc[2]) + { + return true; + } + return false; } void UnitCell::setup(const std::string& latname_in, @@ -498,7 +508,8 @@ void UnitCell::setup(const std::string& latname_in, } -void UnitCell::compare_atom_labels(std::string label1, std::string label2) { +void UnitCell::compare_atom_labels(const std::string &label1, const std::string &label2) +{ if (label1!= label2) //'!( "Ag" == "Ag" || "47" == "47" || "Silver" == Silver" )' { atom_in ai; @@ -515,22 +526,30 @@ void UnitCell::compare_atom_labels(std::string label1, std::string label2) { { std::string stru_label = ""; std::string psuedo_label = ""; - for (int ip = 0; ip < label1.length(); ip++) { - if (!(isdigit(label1[ip]) || label1[ip] == '_')) { - stru_label += label1[ip]; - } else { - break; - } - } - stru_label[0] = toupper(stru_label[0]); - - for (int ip = 0; ip < label2.length(); ip++) { - if (!(isdigit(label2[ip]) || label2[ip] == '_')) { - psuedo_label += label2[ip]; - } else { - break; - } - } + for (int ip = 0; ip < label1.length(); ip++) + { + if (!(isdigit(label1[ip]) || label1[ip] == '_')) + { + stru_label += label1[ip]; + } + else + { + break; + } + } + stru_label[0] = toupper(stru_label[0]); + + for (int ip = 0; ip < label2.length(); ip++) + { + if (!(isdigit(label2[ip]) || label2[ip] == '_')) + { + psuedo_label += label2[ip]; + } + else + { + break; + } + } psuedo_label[0] = toupper(psuedo_label[0]); if (!(stru_label == psuedo_label diff --git a/source/module_cell/unitcell.h b/source/module_cell/unitcell.h index ea9e4576f14..e7b3e3bdcfd 100644 --- a/source/module_cell/unitcell.h +++ b/source/module_cell/unitcell.h @@ -233,7 +233,7 @@ class UnitCell { /// @brief check consistency between two atom labels from STRU and pseudo or /// orb file - void compare_atom_labels(std::string label1, std::string label2); + void compare_atom_labels(const std::string &label1, const std::string &label2); /// @brief get atomCounts, which is a map from element type to atom number std::map get_atom_Counts() const; /// @brief get orbitalCounts, which is a map from element type to orbital From 65e4630385519be17d9d4136e6aa11fcaff7e670 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 2 Apr 2025 10:06:12 +0800 Subject: [PATCH 31/35] update SCAN_BEGIN, now the function ignore lines starting with # --- source/module_base/global_function.cpp | 36 ++++++++++++++++++------ source/module_base/global_function.h | 4 +-- source/module_cell/read_atom_species.cpp | 16 +++++++---- source/module_cell/read_atoms.cpp | 6 ++++ source/module_cell/unitcell.cpp | 26 ++++++++++++----- 5 files changed, 65 insertions(+), 23 deletions(-) diff --git a/source/module_base/global_function.cpp b/source/module_base/global_function.cpp index 30a7d24af5a..925745504e1 100644 --- a/source/module_base/global_function.cpp +++ b/source/module_base/global_function.cpp @@ -104,9 +104,11 @@ void DONE(std::ofstream &ofs, const std::string &description, const bool only_ra return; } -bool SCAN_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool restart, const bool ifwarn) +bool SCAN_BEGIN(std::ifstream &ifs, + const std::string &TargetName, + const bool restart, + const bool ifwarn) { - std::string SearchName; bool find = false; if (restart) { @@ -114,15 +116,31 @@ bool SCAN_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool re ifs.seekg(0); } ifs.rdstate(); - while (ifs.good()) + + std::string line; + while (std::getline(ifs,line)) { - ifs >> SearchName; - if (SearchName == TargetName) + //! obtain the first character, should not be # + size_t first_char_pos = line.find_first_not_of(" \t"); + if (first_char_pos != std::string::npos && line[first_char_pos] == '#') { - find = true; - break; - } - } + continue; + } + + //! search in each line + std::istringstream iss(line); + std::string SearchName; + while (iss >> SearchName) + { + if (SearchName == TargetName) + { + find = true; + std::cout << " search name = " << SearchName << std::endl; + return find; + } + } + } + if (!find && ifwarn) { GlobalV::ofs_warning << " In SCAN_BEGIN, can't find: " << TargetName << " block." << std::endl; diff --git a/source/module_base/global_function.h b/source/module_base/global_function.h index 115abde3b63..9f82bc5010a 100644 --- a/source/module_base/global_function.h +++ b/source/module_base/global_function.h @@ -1,5 +1,5 @@ -#ifndef W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_BASE_GLOBAL_FUNCTION_H -#define W_ABACUS_DEVELOP_ABACUS_DEVELOP_SOURCE_MODULE_BASE_GLOBAL_FUNCTION_H +#ifndef GLOBAL_FUNCTION_H +#define GLOBAL_FUNCTION_H #include "blas_connector.h" #include "global_function-func_each_2.h" // Peize Lin add 2016-09-07 diff --git a/source/module_cell/read_atom_species.cpp b/source/module_cell/read_atom_species.cpp index a50d4ee6d1e..c3735cd6fa5 100644 --- a/source/module_cell/read_atom_species.cpp +++ b/source/module_cell/read_atom_species.cpp @@ -13,6 +13,7 @@ bool read_atom_species(std::ifstream& ifa, UnitCell& ucell) { ModuleBase::TITLE("UnitCell","read_atom_species"); + const int ntype = ucell.ntype; std::string word; @@ -21,7 +22,6 @@ bool read_atom_species(std::ifstream& ifa, //========================================== if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "ATOMIC_SPECIES") ) { - ifa.ignore(300, '\n'); ModuleBase::GlobalFunc::OUT(ofs_running,"ntype",ntype); for (int i = 0;i < ntype;i++) { @@ -153,16 +153,21 @@ bool read_lattice_constant(std::ifstream& ifa, if(latName=="none") { + // check the existence of keyword "LATTICE_PARAMETERS" if (ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS", true, false)) { - ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant","do not use LATTICE_PARAMETERS without explicit specification of lattice type"); + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "do not use LATTICE_PARAMETERS without explicit specification of lattice type"); } + + // check the existence of keyword "LATTICE_VECTORS" if( !ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_VECTORS") ) { - ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant","Please set LATTICE_VECTORS in STRU file"); + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "Please set LATTICE_VECTORS in STRU file"); } else if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_VECTORS") ) { @@ -184,7 +189,8 @@ bool read_lattice_constant(std::ifstream& ifa, true, false)) { - ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant","do not use LATTICE_VECTORS along with explicit specification of lattice type"); + ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant", + "do not use LATTICE_VECTORS along with explicit specification of lattice type"); } if(latName=="sc") {//simple-cubic, ibrav = 1 @@ -383,4 +389,4 @@ bool read_lattice_constant(std::ifstream& ifa, return true; } -} \ No newline at end of file +} diff --git a/source/module_cell/read_atoms.cpp b/source/module_cell/read_atoms.cpp index 7ca38fcd996..81206124cd6 100644 --- a/source/module_cell/read_atoms.cpp +++ b/source/module_cell/read_atoms.cpp @@ -32,6 +32,8 @@ bool unitcell::read_atom_positions(UnitCell& ucell, { ModuleBase::GlobalFunc::READ_VALUE(ifpos, Coordinate); + std::cout << "Coordinate=" << Coordinate << std::endl; + if(Coordinate != "Cartesian" && Coordinate != "Direct" && Coordinate != "Cartesian_angstrom" @@ -72,6 +74,10 @@ bool unitcell::read_atom_positions(UnitCell& ucell, // start magnetization //======================================= ModuleBase::GlobalFunc::READ_VALUE(ifpos, ucell.atoms[it].label); + + std::cout << "label=" << ucell.atoms[it].label << std::endl; + + if(ucell.atoms[it].label != ucell.atom_label[it]) { ofs_warning << " Label orders in ATOMIC_POSITIONS and ATOMIC_SPECIES sections do not match!" << std::endl; diff --git a/source/module_cell/unitcell.cpp b/source/module_cell/unitcell.cpp index 3371a531d9f..088f89799c8 100755 --- a/source/module_cell/unitcell.cpp +++ b/source/module_cell/unitcell.cpp @@ -180,8 +180,10 @@ std::vector> UnitCell::get_constrain() const //============================================================== // Calculate various lattice related quantities for given latvec //============================================================== -void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) { +void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) +{ ModuleBase::TITLE("UnitCell", "setup_cell"); + // (1) init mag assert(ntype > 0); delete[] magnet.start_magnetization; @@ -203,7 +205,9 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) { this->pseudo_fn.resize(ntype); this->pseudo_type.resize(ntype); this->orbital_fn.resize(ntype); - if (GlobalV::MY_RANK == 0) { + + if (GlobalV::MY_RANK == 0) + { // open "atom_unitcell" file. std::ifstream ifa(fn.c_str(), std::ios::in); if (!ifa) @@ -268,7 +272,7 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) { //========================== // call read_atom_positions //========================== - ok2 = unitcell::read_atom_positions(*this,ifa, log, GlobalV::ofs_warning); + ok2 = unitcell::read_atom_positions(*this, ifa, log, GlobalV::ofs_warning); } } #ifdef __MPI @@ -295,15 +299,23 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) { // Firstly, latvec must be read in. //======================================================== assert(lat0 > 0.0); - this->omega = latvec.Det() * this->lat0 * lat0 * lat0; + this->omega = latvec.Det() * this->lat0 * this->lat0 * this->lat0; + + std::cout << "latvec.det=" << latvec.Det() << std::endl; + std::cout << "lat0=" << lat0 << std::endl; + + if (this->omega < 0) { std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; std::cout << " Warning: The lattice vector is left-handed; a right-handed vector is prefered." << std::endl; std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; - GlobalV::ofs_warning << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; - GlobalV::ofs_warning << " Warning: The lattice vector is left-handed; a right-handed vector is prefered." << std::endl; - GlobalV::ofs_warning << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; + GlobalV::ofs_warning << + "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; + GlobalV::ofs_warning << + " Warning: The lattice vector is left-handed; a right-handed vector is prefered." << std::endl; + GlobalV::ofs_warning << + "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; this->omega = std::abs(this->omega); } else if (this->omega == 0) From 73a9881363741415a66f4d68bb12e190676414db Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 2 Apr 2025 10:21:14 +0800 Subject: [PATCH 32/35] update --- source/module_base/global_function.cpp | 2 +- source/module_cell/read_atom_species.cpp | 1 - source/module_cell/read_atoms.cpp | 6 ------ 3 files changed, 1 insertion(+), 8 deletions(-) diff --git a/source/module_base/global_function.cpp b/source/module_base/global_function.cpp index 925745504e1..cb75ffd78c3 100644 --- a/source/module_base/global_function.cpp +++ b/source/module_base/global_function.cpp @@ -135,7 +135,7 @@ bool SCAN_BEGIN(std::ifstream &ifs, if (SearchName == TargetName) { find = true; - std::cout << " search name = " << SearchName << std::endl; + //std::cout << " search name = " << SearchName << std::endl; return find; } } diff --git a/source/module_cell/read_atom_species.cpp b/source/module_cell/read_atom_species.cpp index c3735cd6fa5..62244e016c9 100644 --- a/source/module_cell/read_atom_species.cpp +++ b/source/module_cell/read_atom_species.cpp @@ -369,7 +369,6 @@ bool read_lattice_constant(std::ifstream& ifa, } else { - std::cout << "latname is : " << latName << std::endl; ModuleBase::WARNING_QUIT("unitcell::read_lattice_constant","latname not supported!"); } } diff --git a/source/module_cell/read_atoms.cpp b/source/module_cell/read_atoms.cpp index 81206124cd6..febb065d4f0 100644 --- a/source/module_cell/read_atoms.cpp +++ b/source/module_cell/read_atoms.cpp @@ -32,8 +32,6 @@ bool unitcell::read_atom_positions(UnitCell& ucell, { ModuleBase::GlobalFunc::READ_VALUE(ifpos, Coordinate); - std::cout << "Coordinate=" << Coordinate << std::endl; - if(Coordinate != "Cartesian" && Coordinate != "Direct" && Coordinate != "Cartesian_angstrom" @@ -75,9 +73,6 @@ bool unitcell::read_atom_positions(UnitCell& ucell, //======================================= ModuleBase::GlobalFunc::READ_VALUE(ifpos, ucell.atoms[it].label); - std::cout << "label=" << ucell.atoms[it].label << std::endl; - - if(ucell.atoms[it].label != ucell.atom_label[it]) { ofs_warning << " Label orders in ATOMIC_POSITIONS and ATOMIC_SPECIES sections do not match!" << std::endl; @@ -113,7 +108,6 @@ bool unitcell::read_atom_positions(UnitCell& ucell, { ucell.atoms[it].nw = 0; ucell.atoms[it].nwl = 2; - //std::cout << ucell.lmaxmax << std::endl; if ( ucell.lmaxmax != 2 ) { ucell.atoms[it].nwl = ucell.lmaxmax; From d5b6acdb5289cb26fbdb8814bcd27e2043df0e93 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 2 Apr 2025 13:55:36 +0800 Subject: [PATCH 33/35] add SCAN_LINE_BEGIN --- source/module_base/global_function.cpp | 33 +++++- source/module_base/global_function.h | 23 +++- source/module_cell/read_pp_upf201.cpp | 110 ++++++++++++------ .../test/support/mock_unitcell.cpp | 10 +- 4 files changed, 132 insertions(+), 44 deletions(-) diff --git a/source/module_base/global_function.cpp b/source/module_base/global_function.cpp index cb75ffd78c3..452408f88b9 100644 --- a/source/module_base/global_function.cpp +++ b/source/module_base/global_function.cpp @@ -104,10 +104,41 @@ void DONE(std::ofstream &ofs, const std::string &description, const bool only_ra return; } + bool SCAN_BEGIN(std::ifstream &ifs, const std::string &TargetName, const bool restart, const bool ifwarn) +{ + std::string SearchName; + bool find = false; + if (restart) + { + ifs.clear(); + ifs.seekg(0); + } + ifs.rdstate(); + while (ifs.good()) + { + ifs >> SearchName; + if (SearchName == TargetName) + { + find = true; + break; + } + } + if (!find && ifwarn) + { + GlobalV::ofs_warning << " In SCAN_BEGIN, can't find: " << TargetName << " block." << std::endl; + } + return find; +} + + +bool SCAN_LINE_BEGIN(std::ifstream &ifs, + const std::string &TargetName, + const bool restart, + const bool ifwarn) { bool find = false; if (restart) @@ -143,7 +174,7 @@ bool SCAN_BEGIN(std::ifstream &ifs, if (!find && ifwarn) { - GlobalV::ofs_warning << " In SCAN_BEGIN, can't find: " << TargetName << " block." << std::endl; + GlobalV::ofs_warning << " In SCAN_LINE_BEGIN, can't find: " << TargetName << " block." << std::endl; } return find; } diff --git a/source/module_base/global_function.h b/source/module_base/global_function.h index 9f82bc5010a..b82dcea2067 100644 --- a/source/module_base/global_function.h +++ b/source/module_base/global_function.h @@ -145,13 +145,26 @@ static void READ_VALUE(std::ifstream& ifs, T& v) return; } -bool SCAN_BEGIN(std::ifstream& ifs, const std::string& TargetName, const bool restart = true, const bool ifwarn = true); -// ifwarn: whether to call GlobalV::ofs_warning when the TargetName is not found, used to avoid invalid warning. -// Mohan warning : the last term can't be written as const bool &restart, -// I don't know why. +//------------------------------------------------------------- +//! The `SCAN_BEGIN` function efficiently searches +//! text files for specified keywords +//------------------------------------------------------------- +bool SCAN_BEGIN(std::ifstream& ifs, + const std::string& TargetName, + const bool restart = true, + const bool ifwarn = true); + +//------------------------------------------------------------- +// The `SCAN_LINE_BEGIN` function efficiently searches +// text files for specified keywords while ignoring comment +// lines and whitespace. It skips any line starting with '#' +//------------------------------------------------------------- +bool SCAN_LINE_BEGIN(std::ifstream& ifs, + const std::string& TargetName, + const bool restart = true, + const bool ifwarn = true); void SCAN_END(std::ifstream& ifs, const std::string& TargetName, const bool ifwarn = true); -// ifwarn: whether to call GlobalV::ofs_warning when the TargetName is not found, used to avoid invalid warning. template static inline void DCOPY(const T& a, T& b, const int& dim) diff --git a/source/module_cell/read_pp_upf201.cpp b/source/module_cell/read_pp_upf201.cpp index e8e6a10b16b..094a972e269 100644 --- a/source/module_cell/read_pp_upf201.cpp +++ b/source/module_cell/read_pp_upf201.cpp @@ -109,7 +109,9 @@ int Pseudopot_upf::read_pseudo_upf201(std::ifstream &ifs, Atom_pseudo& pp) void Pseudopot_upf::getnameval(std::ifstream& ifs, int& n, std::string* name, std::string* val) { - std::string txt, word; + std::string txt; + std::string word; + // get long txt ifs >> txt; while (ifs >> word) @@ -128,26 +130,32 @@ void Pseudopot_upf::getnameval(std::ifstream& ifs, int& n, std::string* name, st while (1) { pos = txt.find("=", pos); - if (pos == std::string::npos) - break; - pos++; + if (pos == std::string::npos) + { + break; + } + pos++; n++; } + std::cout << "n=" << n << std::endl; + // get name & value pos = 0; - size_t pos2, ll; + size_t pos2=0; + size_t ll=0; for (int i = 0; i < n; ++i) { pos2 = txt.find("=", pos); for (; pos2 > pos; --pos2) // There may be a space before "="; { - if (txt.substr(pos2 - 1, 1) != " ") - break; + if (txt.substr(pos2 - 1, 1) != " ") + { + break; + } } ll = pos2 - pos; name[i] = txt.substr(pos, ll); - // std::cout<getnameval(ifs, nparameter, name, val); for (int ip = 0; ip < nparameter; ++ip) @@ -347,12 +366,17 @@ void Pseudopot_upf::read_pseudo_upf201_header(std::ifstream& ifs, Atom_pseudo& p void Pseudopot_upf::read_pseudo_upf201_mesh(std::ifstream& ifs, Atom_pseudo& pp) { - std::string name[50]; - std::string val[50]; - int nparameter; + const int max_n = 100; + std::string name[max_n]; + std::string val[max_n]; + int nparameter=0; + if (ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, "getnameval(ifs, nparameter, name, val); + +std::cout << "read mesh, nparameter=" << nparameter << std::endl; + for (int ip = 0; ip < nparameter; ++ip) { if (name[ip] == "dx") @@ -362,6 +386,9 @@ void Pseudopot_upf::read_pseudo_upf201_mesh(std::ifstream& ifs, Atom_pseudo& pp) else if (name[ip] == "mesh") { pp.mesh = atoi(val[ip].c_str()); + +std::cout << "mesh=" << pp.mesh << std::endl; + this->mesh_changed = false; if (pp.mesh % 2 == 0) { @@ -437,9 +464,12 @@ void Pseudopot_upf::read_pseudo_upf201_nonlocal(std::ifstream& ifs, Atom_pseudo& return; } std::string word; - std::string name[50]; - std::string val[50]; - int nparameter; + + const int max_n = 100; + std::string name[max_n]; + std::string val[max_n]; + int nparameter=0; + this->kbeta = std::vector(pp.nbeta); pp.lll = std::vector(pp.nbeta); this->els_beta = std::vector(pp.nbeta); @@ -680,9 +710,12 @@ void Pseudopot_upf::read_pseudo_upf201_nonlocal(std::ifstream& ifs, Atom_pseudo& void Pseudopot_upf::read_pseudo_upf201_pswfc(std::ifstream& ifs, Atom_pseudo& pp) { std::string word; - std::string name[50]; - std::string val[50]; - int nparameter; + + const int max_n = 100; + std::string name[max_n]; + std::string val[max_n]; + int nparameter=0; + ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, ""); pp.els = std::vector(pp.nchi, ""); pp.lchi = std::vector(pp.nchi, 0); @@ -815,9 +848,12 @@ void Pseudopot_upf::read_pseudo_upf201_fullwfc(std::ifstream& ifs) void Pseudopot_upf::read_pseudo_upf201_so(std::ifstream& ifs, Atom_pseudo& pp) { std::string word; - std::string name[50]; - std::string val[50]; - int nparameter; + + const int max_n = 100; + std::string name[max_n]; + std::string val[max_n]; + int nparameter=0; + ModuleBase::GlobalFunc::SCAN_BEGIN(ifs, ""); pp.jchi = std::vector(pp.nchi, 0.0); pp.jjj = std::vector(pp.nbeta, 0.0); @@ -893,4 +929,4 @@ void Pseudopot_upf::read_pseudo_upf201_so(std::ifstream& ifs, Atom_pseudo& pp) ModuleBase::GlobalFunc::SCAN_END(ifs, word); } ModuleBase::GlobalFunc::SCAN_END(ifs, ""); -} \ No newline at end of file +} diff --git a/source/module_cell/test/support/mock_unitcell.cpp b/source/module_cell/test/support/mock_unitcell.cpp index d2e7268f880..e62a266d6d1 100644 --- a/source/module_cell/test/support/mock_unitcell.cpp +++ b/source/module_cell/test/support/mock_unitcell.cpp @@ -17,15 +17,23 @@ UnitCell::~UnitCell() { delete[] atoms; } } + void UnitCell::print_cell(std::ofstream& ofs) const {} + void UnitCell::set_iat2itia() {} + void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) {} + bool UnitCell::if_atoms_can_move() const { return true; } + bool UnitCell::if_cell_can_change() const { return true; } + void UnitCell::setup(const std::string& latname_in, const int& ntype_in, const int& lmaxmax_in, const bool& init_vel_in, const std::string& fixed_axes_in) {} + void cal_nelec(const Atom* atoms, const int& ntype, double& nelec) {} -void UnitCell::compare_atom_labels(std::string label1, std::string label2) {} \ No newline at end of file + +void UnitCell::compare_atom_labels(const std::string &label1, const std::string &label2) {} From 4f499a6de6ec374d79deeb5ff0f385637ffe7369 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 2 Apr 2025 14:00:55 +0800 Subject: [PATCH 34/35] fix bug --- source/module_cell/read_pp_upf201.cpp | 6 ------ 1 file changed, 6 deletions(-) diff --git a/source/module_cell/read_pp_upf201.cpp b/source/module_cell/read_pp_upf201.cpp index 094a972e269..45351ca3cc0 100644 --- a/source/module_cell/read_pp_upf201.cpp +++ b/source/module_cell/read_pp_upf201.cpp @@ -138,8 +138,6 @@ void Pseudopot_upf::getnameval(std::ifstream& ifs, int& n, std::string* name, st n++; } - std::cout << "n=" << n << std::endl; - // get name & value pos = 0; size_t pos2=0; @@ -375,8 +373,6 @@ void Pseudopot_upf::read_pseudo_upf201_mesh(std::ifstream& ifs, Atom_pseudo& pp) { this->getnameval(ifs, nparameter, name, val); -std::cout << "read mesh, nparameter=" << nparameter << std::endl; - for (int ip = 0; ip < nparameter; ++ip) { if (name[ip] == "dx") @@ -387,8 +383,6 @@ std::cout << "read mesh, nparameter=" << nparameter << std::endl; { pp.mesh = atoi(val[ip].c_str()); -std::cout << "mesh=" << pp.mesh << std::endl; - this->mesh_changed = false; if (pp.mesh % 2 == 0) { From c8afa942db4ac2bbd3e9c78d29536e6d84966301 Mon Sep 17 00:00:00 2001 From: mohanchen Date: Wed, 2 Apr 2025 14:38:51 +0800 Subject: [PATCH 35/35] fix issue --- source/module_cell/read_atom_species.cpp | 49 ++++++++++++----------- source/module_cell/read_atoms.cpp | 2 +- source/module_cell/test/unitcell_test.cpp | 6 +-- 3 files changed, 30 insertions(+), 27 deletions(-) diff --git a/source/module_cell/read_atom_species.cpp b/source/module_cell/read_atom_species.cpp index 62244e016c9..bee3b0f2534 100644 --- a/source/module_cell/read_atom_species.cpp +++ b/source/module_cell/read_atom_species.cpp @@ -20,7 +20,7 @@ bool read_atom_species(std::ifstream& ifa, //========================================== // read in information of each type of atom //========================================== - if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "ATOMIC_SPECIES") ) + if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "ATOMIC_SPECIES") ) { ModuleBase::GlobalFunc::OUT(ofs_running,"ntype",ntype); for (int i = 0;i < ntype;i++) @@ -51,7 +51,8 @@ bool read_atom_species(std::ifstream& ifa, if (!end && ss >> one_string && one_string[0] != '#') { - if (one_string == "auto" || one_string == "upf" || one_string == "vwr" || one_string == "upf201" || one_string == "blps") + if (one_string == "auto" || one_string == "upf" + || one_string == "vwr" || one_string == "upf201" || one_string == "blps") { ucell.pseudo_type[i] = one_string; } @@ -61,7 +62,8 @@ bool read_atom_species(std::ifstream& ifa, } else { - GlobalV::ofs_warning << "unrecongnized pseudopotential type: " << one_string << ", check your STRU file." << std::endl; + GlobalV::ofs_warning << "unrecongnized pseudopotential type: " + << one_string << ", check your STRU file." << std::endl; ModuleBase::WARNING_QUIT("read_atom_species", "unrecongnized pseudo type."); } } @@ -69,7 +71,8 @@ bool read_atom_species(std::ifstream& ifa, // Peize Lin test for bsse 2021.04.07 const std::string bsse_label = "empty"; ucell.atoms[i].flag_empty_element = - (search( ucell.atom_label[i].begin(), ucell.atom_label[i].end(), bsse_label.begin(), bsse_label.end() ) != ucell.atom_label[i].end()) + (search( ucell.atom_label[i].begin(), ucell.atom_label[i].end(), + bsse_label.begin(), bsse_label.end() ) != ucell.atom_label[i].end()) ? true : false; } } @@ -80,7 +83,7 @@ bool read_atom_species(std::ifstream& ifa, ||((PARAM.inp.basis_type == "pw")&&(PARAM.inp.init_wfc.substr(0, 3) == "nao")) || PARAM.inp.onsite_radius > 0.0) { - if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "NUMERICAL_ORBITAL") ) + if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "NUMERICAL_ORBITAL") ) { for(int i=0; i> ucell.descriptor_file; } } @@ -105,7 +108,7 @@ bool read_atom_species(std::ifstream& ifa, #ifdef __EXX if( GlobalC::exx_info.info_global.cal_exx || PARAM.inp.rpa ) { - if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "ABFS_ORBITAL") ) + if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "ABFS_ORBITAL") ) { for(int i=0; i> latvec.e22; ModuleBase::GlobalFunc::READ_VALUE(ifa, latvec.e33); @@ -278,7 +281,7 @@ bool read_lattice_constant(std::ifstream& ifa, latvec.e11 = 0.5; latvec.e12 = 0.0; latvec.e13 = 0.0; latvec.e21 =-0.5; latvec.e22 = 0.0; latvec.e23 = 0.0; latvec.e31 = 0.0; latvec.e32 = 0.0; latvec.e33 = 0.0; - if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { ifa >> latvec.e12; latvec.e12 = latvec.e12 / 2.0; @@ -289,7 +292,7 @@ bool read_lattice_constant(std::ifstream& ifa, else if(latName=="fco") {//face-centered orthorhombic, ibrav = 10 double bba = 0.0; double cba = 0.0; - if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { ifa >> bba; ModuleBase::GlobalFunc::READ_VALUE(ifa, cba); @@ -302,7 +305,7 @@ bool read_lattice_constant(std::ifstream& ifa, else if(latName=="bco") {//body-centered orthorhombic, ibrav = 11 double bba = 0.0; double cba = 0.0; - if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { ifa >> bba; ModuleBase::GlobalFunc::READ_VALUE(ifa, cba); @@ -317,7 +320,7 @@ bool read_lattice_constant(std::ifstream& ifa, double bba = 0.0; double cba = 0.0; double cosab = 0.0; double e21 = 0.0; double e22 = 0.0; - if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { ifa >> bba >> cba; ModuleBase::GlobalFunc::READ_VALUE(ifa, cosab); @@ -333,7 +336,7 @@ bool read_lattice_constant(std::ifstream& ifa, double bba = 0.0; double cba = 0.0; double cosab = 0.0; double e21 = 0.0; double e22 = 0.0; - if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { ifa >> bba >> cba; ModuleBase::GlobalFunc::READ_VALUE(ifa, cosab); @@ -354,7 +357,7 @@ bool read_lattice_constant(std::ifstream& ifa, double cosbc = 0.0; double sinab = 0.0; double term = 0.0; - if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifa, "LATTICE_PARAMETERS") ) + if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifa, "LATTICE_PARAMETERS") ) { ifa >> bba >> cba >> cosab >> cosac; ModuleBase::GlobalFunc::READ_VALUE(ifa, cosbc); diff --git a/source/module_cell/read_atoms.cpp b/source/module_cell/read_atoms.cpp index febb065d4f0..b7b891dabc7 100644 --- a/source/module_cell/read_atoms.cpp +++ b/source/module_cell/read_atoms.cpp @@ -28,7 +28,7 @@ bool unitcell::read_atom_positions(UnitCell& ucell, std::string& Coordinate = ucell.Coordinate; const int ntype = ucell.ntype; - if( ModuleBase::GlobalFunc::SCAN_BEGIN(ifpos, "ATOMIC_POSITIONS")) + if( ModuleBase::GlobalFunc::SCAN_LINE_BEGIN(ifpos, "ATOMIC_POSITIONS")) { ModuleBase::GlobalFunc::READ_VALUE(ifpos, Coordinate); diff --git a/source/module_cell/test/unitcell_test.cpp b/source/module_cell/test/unitcell_test.cpp index 70d058d090b..397d11e5786 100644 --- a/source/module_cell/test/unitcell_test.cpp +++ b/source/module_cell/test/unitcell_test.cpp @@ -1171,7 +1171,7 @@ TEST_F(UcellTestReadStru, ReadAtomSpecies) PARAM.input.basis_type = "lcao"; PARAM.sys.deepks_setorb = true; EXPECT_NO_THROW(unitcell::read_atom_species(ifa, ofs_running,*ucell)); - EXPECT_NO_THROW(unitcell::read_lattice_constant(ifa, ofs_running,ucell->lat)); + EXPECT_NO_THROW(unitcell::read_lattice_constant(ifa, ofs_running, ucell->lat)); EXPECT_DOUBLE_EQ(ucell->latvec.e11, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e22, 4.27957); EXPECT_DOUBLE_EQ(ucell->latvec.e33, 4.27957); @@ -1185,7 +1185,7 @@ TEST_F(UcellTestReadStru, ReadAtomSpeciesWarning1) std::string fn = "./support/STRU_MgO_Warning1"; std::ifstream ifa(fn.c_str()); std::ofstream ofs_running; - ofs_running.open("read_atom_species.tmp"); + ofs_running.open("read_atom_species.txt"); ucell->ntype = 2; ucell->atoms = new Atom[ucell->ntype]; ucell->set_atom_flag = true; @@ -1195,7 +1195,7 @@ TEST_F(UcellTestReadStru, ReadAtomSpeciesWarning1) EXPECT_THAT(output, testing::HasSubstr("unrecongnized pseudo type.")); ofs_running.close(); ifa.close(); - remove("read_atom_species.tmp"); + //remove("read_atom_species.txt"); } TEST_F(UcellTestReadStru, ReadLatticeConstantWarning1)