From bcc6d1b2d7f48bc73ab94f396b4aec5248573f88 Mon Sep 17 00:00:00 2001 From: Qianruipku Date: Tue, 26 Nov 2024 20:20:51 +0800 Subject: [PATCH] fix wrong nscf energy of sDFT add nscf energy tests --- source/module_elecstate/elecstate_pw.cpp | 5 ----- source/module_esolver/esolver_ks.cpp | 9 +++++---- .../hamilt_stodft/sto_iter.cpp | 8 ++++---- .../module_hamilt_pw/hamilt_stodft/sto_iter.h | 2 +- source/module_hsolver/hsolver_pw_sdft.cpp | 20 ++++++++++++++----- .../module_hsolver/test/test_hsolver_sdft.cpp | 2 +- .../integrate/107_PW_OBOD_MemSaver/result.ref | 4 +++- .../107_PW_OB_outputbands/result.ref | 4 +++- .../integrate/107_PW_OD_outputdos/result.ref | 4 +++- tests/integrate/107_PW_W90/result.ref | 4 +++- .../integrate/117_PW_out_pot_nscf/result.ref | 4 +++- tests/integrate/186_PW_NSCF_KG_100/result.ref | 4 +++- tests/integrate/186_PW_SDOS_MALL/result.ref | 6 +++--- tests/integrate/186_PW_SKG_10D10S/result.ref | 5 +++-- tests/integrate/207_NO_KP_OB/result.ref | 4 +++- tests/integrate/207_NO_KP_OD/result.ref | 4 +++- tests/integrate/207_NO_KP_OH_nscf/result.ref | 4 +++- .../integrate/214_NO_mulliken_nscf/result.ref | 4 +++- .../integrate/217_NO_out_pot_nscf/result.ref | 4 +++- tests/integrate/tools/catch_properties.sh | 2 +- 20 files changed, 66 insertions(+), 37 deletions(-) diff --git a/source/module_elecstate/elecstate_pw.cpp b/source/module_elecstate/elecstate_pw.cpp index 0070f1a193..48d25ed298 100644 --- a/source/module_elecstate/elecstate_pw.cpp +++ b/source/module_elecstate/elecstate_pw.cpp @@ -135,11 +135,6 @@ void ElecStatePW::parallelK() { #ifdef __MPI this->charge->rho_mpi(); - if(PARAM.inp.esolver_type == "sdft") //qinarui add it 2021-7-21 - { - this->f_en.eband /= GlobalV::NPROC_IN_POOL; - MPI_Allreduce(MPI_IN_PLACE, &this->f_en.eband, 1, MPI_DOUBLE, MPI_SUM, STO_WORLD); - } #endif } diff --git a/source/module_esolver/esolver_ks.cpp b/source/module_esolver/esolver_ks.cpp index 65d2796d48..53136de819 100644 --- a/source/module_esolver/esolver_ks.cpp +++ b/source/module_esolver/esolver_ks.cpp @@ -442,15 +442,16 @@ void ESolver_KS::runner(UnitCell& ucell, const int istep) this->diag_ethr = PARAM.inp.pw_diag_thr; for (int iter = 1; iter <= this->maxniter; ++iter) { - // 6) initialization of SCF iterations + // 5) initialization of SCF iterations this->iter_init(ucell, istep, iter); + // 6) use Hamiltonian to obtain charge density this->hamilt2density(ucell, istep, iter, diag_ethr); - // 10) finish scf iterations + // 7) finish scf iterations this->iter_finish(ucell, istep, iter); - // 13) check convergence + // 8) check convergence if (this->conv_esolver || this->oscillate_esolver) { this->niter = iter; @@ -462,7 +463,7 @@ void ESolver_KS::runner(UnitCell& ucell, const int istep) } } // end scf iterations - // 15) after scf + // 9) after scf ModuleBase::timer::tick(this->classname, "after_scf"); this->after_scf(ucell, istep); ModuleBase::timer::tick(this->classname, "after_scf"); diff --git a/source/module_hamilt_pw/hamilt_stodft/sto_iter.cpp b/source/module_hamilt_pw/hamilt_stodft/sto_iter.cpp index b9c0fe95f6..08f2b894e1 100644 --- a/source/module_hamilt_pw/hamilt_stodft/sto_iter.cpp +++ b/source/module_hamilt_pw/hamilt_stodft/sto_iter.cpp @@ -472,13 +472,13 @@ void Stochastic_Iter::calHsqrtchi(Stochastic_WF& stowf) } template -void Stochastic_Iter::sum_stoband(Stochastic_WF& stowf, +void Stochastic_Iter::sum_stoeband(Stochastic_WF& stowf, elecstate::ElecStatePW* pes, hamilt::Hamilt* pHamilt, ModulePW::PW_Basis_K* wfc_basis) { - ModuleBase::TITLE("Stochastic_Iter", "sum_stoband"); - ModuleBase::timer::tick("Stochastic_Iter", "sum_stoband"); + ModuleBase::TITLE("Stochastic_Iter", "sum_stoeband"); + ModuleBase::timer::tick("Stochastic_Iter", "sum_stoeband"); const int npwx = wfc_basis->npwk_max; const int norder = p_che->norder; @@ -556,7 +556,7 @@ void Stochastic_Iter::sum_stoband(Stochastic_WF& stowf, MPI_Allreduce(MPI_IN_PLACE, &sto_eband, 1, MPI_DOUBLE, MPI_SUM, MPI_COMM_WORLD); #endif pes->f_en.eband += sto_eband; - ModuleBase::timer::tick("Stochastic_Iter", "sum_stoband"); + ModuleBase::timer::tick("Stochastic_Iter", "sum_stoeband"); } template diff --git a/source/module_hamilt_pw/hamilt_stodft/sto_iter.h b/source/module_hamilt_pw/hamilt_stodft/sto_iter.h index 75bcda3fe6..f990b0078b 100644 --- a/source/module_hamilt_pw/hamilt_stodft/sto_iter.h +++ b/source/module_hamilt_pw/hamilt_stodft/sto_iter.h @@ -52,7 +52,7 @@ class Stochastic_Iter * @param pHamilt hamiltonian * @param wfc_basis wfc pw basis */ - void sum_stoband(Stochastic_WF& stowf, + void sum_stoeband(Stochastic_WF& stowf, elecstate::ElecStatePW* pes, hamilt::Hamilt* pHamilt, ModulePW::PW_Basis_K* wfc_basis); diff --git a/source/module_hsolver/hsolver_pw_sdft.cpp b/source/module_hsolver/hsolver_pw_sdft.cpp index e66db52392..2eec4fa6ca 100644 --- a/source/module_hsolver/hsolver_pw_sdft.cpp +++ b/source/module_hsolver/hsolver_pw_sdft.cpp @@ -108,29 +108,39 @@ void HSolverPW_SDFT::solve(hamilt::Hamilt* pHamilt, // prepare sqrt{f(\hat{H})}|\chi> to calculate density, force and stress stoiter.calHsqrtchi(stowf); + // calculate eband = \sum_{ik,ib} w(ik)f(ik,ib)e_{ikib}, demet = -TS elecstate::ElecStatePW* pes_pw = static_cast*>(pes); if (GlobalV::MY_STOGROUP == 0) { pes_pw->calEBand(); } + if (nbands > 0) + { +#ifdef __MPI + pes->f_en.eband /= GlobalV::NPROC_IN_POOL; + MPI_Allreduce(MPI_IN_PLACE, &pes->f_en.eband, 1, MPI_DOUBLE, MPI_SUM, STO_WORLD); + MPI_Bcast(&pes->f_en.eband, 1, MPI_DOUBLE, 0, PARAPW_WORLD); +#endif + } + stoiter.sum_stoeband(stowf, pes_pw, pHamilt, wfc_basis); + + + + // for nscf, skip charge if (skip_charge) { ModuleBase::timer::tick("HSolverPW_SDFT", "solve"); return; } + //(5) calculate new charge density // calculate KS rho. pes_pw->init_rho_data(); if (nbands > 0) { pes_pw->psiToRho(psi); -#ifdef __MPI - MPI_Bcast(&pes->f_en.eband, 1, MPI_DOUBLE, 0, PARAPW_WORLD); -#endif } - // calculate stochastic rho - stoiter.sum_stoband(stowf, pes_pw, pHamilt, wfc_basis); stoiter.cal_storho(stowf, pes_pw, wfc_basis); // will do rho symmetry and energy calculation in esolver diff --git a/source/module_hsolver/test/test_hsolver_sdft.cpp b/source/module_hsolver/test/test_hsolver_sdft.cpp index 05bda81301..81e09579cb 100644 --- a/source/module_hsolver/test/test_hsolver_sdft.cpp +++ b/source/module_hsolver/test/test_hsolver_sdft.cpp @@ -141,7 +141,7 @@ void Stochastic_Iter::calHsqrtchi(Stochastic_WF& stowf) } template -void Stochastic_Iter::sum_stoband(Stochastic_WF& stowf, +void Stochastic_Iter::sum_stoeband(Stochastic_WF& stowf, elecstate::ElecStatePW* pes, hamilt::Hamilt* pHamilt, ModulePW::PW_Basis_K* wfc_basis) diff --git a/tests/integrate/107_PW_OBOD_MemSaver/result.ref b/tests/integrate/107_PW_OBOD_MemSaver/result.ref index 4e7e82ffae..5ae47e41d3 100644 --- a/tests/integrate/107_PW_OBOD_MemSaver/result.ref +++ b/tests/integrate/107_PW_OBOD_MemSaver/result.ref @@ -1,3 +1,5 @@ +etotref -206.9728084455294 +etotperatomref -103.4864042228 totaldosref 1190.44 CompareBand_pass 0 -totaltimeref 0.33861 +totaltimeref 0.38 diff --git a/tests/integrate/107_PW_OB_outputbands/result.ref b/tests/integrate/107_PW_OB_outputbands/result.ref index 434ccd54fb..6cf8149fd0 100644 --- a/tests/integrate/107_PW_OB_outputbands/result.ref +++ b/tests/integrate/107_PW_OB_outputbands/result.ref @@ -1,2 +1,4 @@ +etotref -206.9727001566756 +etotperatomref -103.4863500783 CompareBand_pass 0 -totaltimeref 0.28457 +totaltimeref 0.23 diff --git a/tests/integrate/107_PW_OD_outputdos/result.ref b/tests/integrate/107_PW_OD_outputdos/result.ref index 168065ab4d..55da3bcd9f 100644 --- a/tests/integrate/107_PW_OD_outputdos/result.ref +++ b/tests/integrate/107_PW_OD_outputdos/result.ref @@ -1,2 +1,4 @@ +etotref -206.9727320695148 +etotperatomref -103.4863660348 totaldosref 1190.44 -totaltimeref +totaltimeref 0.26 diff --git a/tests/integrate/107_PW_W90/result.ref b/tests/integrate/107_PW_W90/result.ref index b207246f7f..39d46783e0 100644 --- a/tests/integrate/107_PW_W90/result.ref +++ b/tests/integrate/107_PW_W90/result.ref @@ -1,4 +1,6 @@ +etotref -308.0061503011041 +etotperatomref -154.0030751506 CompareAMN_pass 0 CompareMMN_pass 0 CompareEIG_pass 0 -totaltimeref 9.3341 +totaltimeref 2.15 diff --git a/tests/integrate/117_PW_out_pot_nscf/result.ref b/tests/integrate/117_PW_out_pot_nscf/result.ref index 2bc525963e..316d9d5cdd 100644 --- a/tests/integrate/117_PW_out_pot_nscf/result.ref +++ b/tests/integrate/117_PW_out_pot_nscf/result.ref @@ -1,5 +1,7 @@ +etotref -3526.200848639589 +etotperatomref -3526.2008486396 ComparePot1_pass 0 pointgroupref O_h spacegroupref O_h nksibzref 3 -totaltimeref 0.35 +totaltimeref 0.33 diff --git a/tests/integrate/186_PW_NSCF_KG_100/result.ref b/tests/integrate/186_PW_NSCF_KG_100/result.ref index 81ba641b34..b4cba98151 100644 --- a/tests/integrate/186_PW_NSCF_KG_100/result.ref +++ b/tests/integrate/186_PW_NSCF_KG_100/result.ref @@ -1,5 +1,7 @@ +etotref -150.4802162021569 +etotperatomref -150.4802162022 CompareH_Failed 0 pointgroupref O_h spacegroupref O_h nksibzref 3 -totaltimeref 0.67841 +totaltimeref 1.46 diff --git a/tests/integrate/186_PW_SDOS_MALL/result.ref b/tests/integrate/186_PW_SDOS_MALL/result.ref index 46e9954ab6..4d3f5a2f9d 100644 --- a/tests/integrate/186_PW_SDOS_MALL/result.ref +++ b/tests/integrate/186_PW_SDOS_MALL/result.ref @@ -1,7 +1,7 @@ -etotref 0 -etotperatomref 0.0000000000 +etotref -150.4802139726653 +etotperatomref -150.4802139727 totaldosref 96.4227 pointgroupref O_h spacegroupref O_h nksibzref 3 -totaltimeref 5.1234 +totaltimeref 11.83 diff --git a/tests/integrate/186_PW_SKG_10D10S/result.ref b/tests/integrate/186_PW_SKG_10D10S/result.ref index 06f597369b..eba9f432e9 100644 --- a/tests/integrate/186_PW_SKG_10D10S/result.ref +++ b/tests/integrate/186_PW_SKG_10D10S/result.ref @@ -1,6 +1,7 @@ +etotref -150.5555663868826 +etotperatomref -150.5555663869 CompareH_Failed 0 pointgroupref O_h spacegroupref O_h nksibzref 3 -totaltimeref 58.0934 -4.88 +totaltimeref 3.67 diff --git a/tests/integrate/207_NO_KP_OB/result.ref b/tests/integrate/207_NO_KP_OB/result.ref index bbea51b545..76fc204d2a 100644 --- a/tests/integrate/207_NO_KP_OB/result.ref +++ b/tests/integrate/207_NO_KP_OB/result.ref @@ -1,3 +1,5 @@ +etotref -205.4532897097782 +etotperatomref -102.7266448549 CompareOrb_pass 0 CompareBand_pass 0 -totaltimeref 0.27705 +totaltimeref 0.31 diff --git a/tests/integrate/207_NO_KP_OD/result.ref b/tests/integrate/207_NO_KP_OD/result.ref index 82771bf625..e25a30d841 100644 --- a/tests/integrate/207_NO_KP_OD/result.ref +++ b/tests/integrate/207_NO_KP_OD/result.ref @@ -1,2 +1,4 @@ +etotref -205.4532897097782 +etotperatomref -102.7266448549 totaldosref 1189.14 -totaltimeref 0.31661 +totaltimeref 0.31 diff --git a/tests/integrate/207_NO_KP_OH_nscf/result.ref b/tests/integrate/207_NO_KP_OH_nscf/result.ref index 42ab488c7b..00b9cf0799 100644 --- a/tests/integrate/207_NO_KP_OH_nscf/result.ref +++ b/tests/integrate/207_NO_KP_OH_nscf/result.ref @@ -1,4 +1,6 @@ +etotref -205.4532897097782 +etotperatomref -102.7266448549 CompareSR_pass 0 ComparerR_pass 0 ComparerTR_pass 0 -totaltimeref 1.88 +totaltimeref 2.21 diff --git a/tests/integrate/214_NO_mulliken_nscf/result.ref b/tests/integrate/214_NO_mulliken_nscf/result.ref index 43334c821d..5e27829ff3 100644 --- a/tests/integrate/214_NO_mulliken_nscf/result.ref +++ b/tests/integrate/214_NO_mulliken_nscf/result.ref @@ -1,2 +1,4 @@ +etotref -50.22627762925340 +etotperatomref -25.1131388146 Compare_mulliken_pass 0 -totaltimeref 0.20 +totaltimeref 0.76 diff --git a/tests/integrate/217_NO_out_pot_nscf/result.ref b/tests/integrate/217_NO_out_pot_nscf/result.ref index 9bdd0ce2c4..32b3897852 100644 --- a/tests/integrate/217_NO_out_pot_nscf/result.ref +++ b/tests/integrate/217_NO_out_pot_nscf/result.ref @@ -1,5 +1,7 @@ +etotref -196.6254071148604 +etotperatomref -98.3127035574 ComparePot1_pass 0 pointgroupref T_d spacegroupref O_h nksibzref 1 -totaltimeref 0.49 +totaltimeref 0.35 diff --git a/tests/integrate/tools/catch_properties.sh b/tests/integrate/tools/catch_properties.sh index fe3b1f703a..6819e80a0a 100755 --- a/tests/integrate/tools/catch_properties.sh +++ b/tests/integrate/tools/catch_properties.sh @@ -83,7 +83,7 @@ if [ ! -z $esolver_type ] && ([ $esolver_type == "lr" ] || [ $esolver_type == "k is_lr=1 fi -if [ $calculation != "nscf" ] && [ $calculation != "get_wf" ]\ +if [ $calculation != "get_wf" ]\ && [ $calculation != "get_pchg" ] && [ $calculation != "get_S" ]\ && [ $is_lr == 0 ]; then #etot=`grep ETOT_ $running_path | awk '{print $2}'`