diff --git a/source/source_base/tool_quit.cpp b/source/source_base/tool_quit.cpp index d49c8e52250..65297226eea 100644 --- a/source/source_base/tool_quit.cpp +++ b/source/source_base/tool_quit.cpp @@ -133,7 +133,7 @@ void WARNING_QUIT(const std::string &file,const std::string &description,int ret void CHECK_WARNING_QUIT(const bool error_in, const std::string &file,const std::string &calculation,const std::string &description) { #ifdef __NORMAL -// only for UT, do nothing here + if(error_in) std::cout << description << std::endl; #else if(error_in) { diff --git a/source/source_basis/module_pw/pw_basis_k.cpp b/source/source_basis/module_pw/pw_basis_k.cpp index 0f997d3180a..727c0d03ba3 100644 --- a/source/source_basis/module_pw/pw_basis_k.cpp +++ b/source/source_basis/module_pw/pw_basis_k.cpp @@ -145,10 +145,19 @@ void PW_Basis_K::setupIndGk() } } this->npwk[ik] = ng; + int ng_global_k = ng; +#ifdef __MPI + MPI_Allreduce(MPI_IN_PLACE, &ng_global_k, 1, MPI_INT, MPI_SUM, this->pool_world); +#endif + const char* no_pw_message = "Current core has no plane waves! Please reduce the cores."; + if (ng_global_k == 0) + { + no_pw_message = "No plane waves are available for this k-point across the whole pool. Please increase ecutwfc or check KPT settings."; + } ModuleBase::CHECK_WARNING_QUIT((ng == 0), "pw_basis_k.cpp", PARAM.inp.calculation, - "Current core has no plane waves! Please reduce the cores."); + no_pw_message); if (this->npwk_max < ng) { this->npwk_max = ng; diff --git a/source/source_basis/module_pw/pw_distributeg.cpp b/source/source_basis/module_pw/pw_distributeg.cpp index 317d6ad863b..ea026e88d41 100644 --- a/source/source_basis/module_pw/pw_distributeg.cpp +++ b/source/source_basis/module_pw/pw_distributeg.cpp @@ -25,8 +25,9 @@ void PW_Basis::distribute_g() { ModuleBase::WARNING_QUIT("divide", "No such division type."); } + const char* no_pw_message = "Current core has no plane waves! Please reduce the cores."; ModuleBase::CHECK_WARNING_QUIT((this->npw == 0), "pw_distributeg.cpp", PARAM.inp.calculation, - "Current core has no plane waves! Please reduce the cores."); + no_pw_message); ModuleBase::timer::end(this->classname, "distributeg"); return; } diff --git a/source/source_basis/module_pw/test/test-other.cpp b/source/source_basis/module_pw/test/test-other.cpp index c367cc459c0..b81787f16d8 100644 --- a/source/source_basis/module_pw/test/test-other.cpp +++ b/source/source_basis/module_pw/test/test-other.cpp @@ -139,4 +139,66 @@ TEST_F(PWTEST,test_other) #ifdef __ENABLE_FLOAT_FFTW fftwf_cleanup(); #endif +} + +TEST_F(PWTEST, test_no_plane_wave_message_global_empty_k) +{ + ModulePW::PW_Basis_K pwktest(device_flag, precision_flag); + ModuleBase::Matrix3 latvec(0.2, 0, 0, 0, 1, 0, 0, 0, 1); +#ifdef __MPI + pwktest.initmpi(nproc_in_pool, rank_in_pool, POOL_WORLD); +#endif + const int nks = 1; + ModuleBase::Vector3 kvec_d[nks]; + kvec_d[0].set(0.5, 0.5, 0.5); + + pwktest.initgrids(2, latvec, 4, 4, 4); + pwktest.initparameters(true, 1e-4, nks, kvec_d); + testing::internal::CaptureStdout(); + pwktest.setuptransform(); + std::string output = testing::internal::GetCapturedStdout(); + + EXPECT_THAT(output, + testing::HasSubstr("No plane waves are available for this k-point across the whole pool. Please increase ecutwfc or check KPT settings.")); +} + +TEST_F(PWTEST, test_no_plane_wave_message_parallel_local_empty) +{ +#ifndef __MPI + GTEST_SKIP() << "Requires MPI ranks to simulate local-empty but global-nonempty case."; +#else + if (nproc_in_pool <= 1) + { + GTEST_SKIP() << "Requires more than one MPI rank."; + } + + ModulePW::PW_Basis_K pwktest(device_flag, precision_flag); + ModuleBase::Matrix3 latvec(0.2, 0, 0, 0, 1, 0, 0, 0, 1); + pwktest.initmpi(nproc_in_pool, rank_in_pool, POOL_WORLD); + + const int nks = 1; + ModuleBase::Vector3 kvec_d[nks]; + kvec_d[0].set(0.0, 0.0, 0.0); + + pwktest.initgrids(2, latvec, 4, 4, 4); + pwktest.initparameters(true, 8.0, nks, kvec_d); + testing::internal::CaptureStdout(); + pwktest.setuptransform(); + std::string output = testing::internal::GetCapturedStdout(); + + const int local_npwk = pwktest.npwk[0]; + int global_npwk = local_npwk; + MPI_Allreduce(MPI_IN_PLACE, &global_npwk, 1, MPI_INT, MPI_SUM, POOL_WORLD); + + const int local_target_rank = (local_npwk == 0 && global_npwk > 0) ? 1 : 0; + int any_target_rank = local_target_rank; + MPI_Allreduce(MPI_IN_PLACE, &any_target_rank, 1, MPI_INT, MPI_MAX, POOL_WORLD); + EXPECT_EQ(any_target_rank, 1); + + if (local_target_rank == 1) + { + EXPECT_THAT(output, + testing::HasSubstr("Current core has no plane waves! Please reduce the cores.")); + } +#endif } \ No newline at end of file