From 320b07fb2cc323070433ec1b6f0a102c16a1a6d5 Mon Sep 17 00:00:00 2001 From: liiutao <74701833+A-006@users.noreply.github.com> Date: Fri, 20 Sep 2024 18:04:56 +0800 Subject: [PATCH 1/8] Refactor:Use PARAM instead of ks_solver,nb2d,search_radius (#5138) * delete nb2d * delete ks_solver in cpp file * delete ks_solver in test file * change function in ks_solver * Modify search_radius * fix compile bug * [pre-commit.ci lite] apply automatic fixes * modify parameter search_radius * modify the init number --- source/module_base/global_function.h | 6 ++-- .../module_base/test/global_function_test.cpp | 15 +++++---- source/module_elecstate/elecstate_getters.cpp | 2 +- source/module_elecstate/elecstate_lcao.cpp | 10 +++--- .../module_elecstate/elecstate_lcao_tddft.cpp | 4 +-- .../module_elecstate/module_dm/cal_dm_psi.cpp | 3 +- source/module_esolver/esolver_ks_lcao.cpp | 4 +-- .../module_esolver/esolver_ks_lcao_tddft.cpp | 4 +-- source/module_esolver/esolver_ks_pw.cpp | 2 +- source/module_esolver/esolver_lj.cpp | 6 ++-- source/module_esolver/esolver_lj.h | 1 + source/module_esolver/esolver_sdft_pw.cpp | 2 +- source/module_esolver/lcao_before_scf.cpp | 14 ++++---- source/module_esolver/lcao_gets.cpp | 10 +++--- source/module_esolver/lcao_nscf.cpp | 4 +-- source/module_esolver/lcao_others.cpp | 9 ++---- source/module_esolver/set_matrix_grid.cpp | 5 +-- .../hamilt_lcaodft/LCAO_init_basis.cpp | 12 +++---- .../hamilt_lcaodft/LCAO_set_mat2d.cpp | 3 +- .../hamilt_lcaodft/fedm_gamma.cpp | 2 +- .../operator_lcao/deepks_lcao.cpp | 2 +- .../operator_lcao/operator_lcao.cpp | 2 +- .../operator_lcao/overlap_new.cpp | 3 +- .../operator_lcao/sc_lambda_lcao.cpp | 5 +-- .../operator_lcao/td_ekinetic_lcao.cpp | 2 +- .../operator_lcao/td_nonlocal_lcao.cpp | 3 +- .../test/test_sc_lambda_lcao.cpp | 19 ++++++----- .../hamilt_lcaodft/spar_u.cpp | 9 +++--- .../module_deepks/LCAO_deepks_odelta.cpp | 2 +- .../module_deepks/LCAO_deepks_vdelta.cpp | 2 +- .../module_deepks/cal_gdmx.cpp | 2 +- .../module_deepks/cal_gdmx_k.cpp | 2 +- .../module_deepks/deepks_fgamma.cpp | 2 +- .../module_deepks/deepks_fk.cpp | 8 +++-- .../module_deepks/deepks_hmat.cpp | 3 +- .../module_deepks/orbital_precalc.cpp | 2 +- .../module_deepks/orbital_precalc_k.cpp | 2 +- .../module_deepks/test/nnr.cpp | 2 +- .../module_dftu/dftu_folding.cpp | 4 +-- .../hamilt_pwdft/wavefunc.cpp | 32 +++++++++---------- source/module_io/input_conv.cpp | 3 -- source/module_io/write_HS.hpp | 4 +-- source/module_lr/esolver_lrtd_lcao.cpp | 6 ++-- .../operator_casida/operator_lr_hxc.cpp | 2 +- source/module_md/test/lj_pot_test.cpp | 5 +-- source/module_md/test/setcell.h | 3 +- .../test/psi_initializer_unit_test.cpp | 2 +- source/module_ri/RI_2D_Comm.hpp | 9 ++---- 48 files changed, 134 insertions(+), 126 deletions(-) diff --git a/source/module_base/global_function.h b/source/module_base/global_function.h index 385bf05c14..115abde3b6 100644 --- a/source/module_base/global_function.h +++ b/source/module_base/global_function.h @@ -355,10 +355,10 @@ T ddot_real(const int& dim, const std::complex* psi_L, const std::complex* // NAME : IS_COLUMN_MAJOR_KS_SOLVER // check ks_solver requires column major or not //========================================================== -static inline bool IS_COLUMN_MAJOR_KS_SOLVER() +static inline bool IS_COLUMN_MAJOR_KS_SOLVER(std::string ks_solver) { - return GlobalV::KS_SOLVER == "genelpa" || GlobalV::KS_SOLVER == "elpa" || GlobalV::KS_SOLVER == "scalapack_gvx" || GlobalV::KS_SOLVER == "cusolver" - || GlobalV::KS_SOLVER == "cusolvermp" || GlobalV::KS_SOLVER == "cg_in_lcao" || GlobalV::KS_SOLVER == "pexsi" || GlobalV::KS_SOLVER == "lapack"; + return ks_solver == "genelpa" || ks_solver == "elpa" || ks_solver == "scalapack_gvx" || ks_solver == "cusolver" + || ks_solver == "cusolvermp" || ks_solver == "cg_in_lcao" || ks_solver == "pexsi" || ks_solver == "lapack"; } } // namespace GlobalFunc diff --git a/source/module_base/test/global_function_test.cpp b/source/module_base/test/global_function_test.cpp index 85bfd0a1da..992fa2a3be 100644 --- a/source/module_base/test/global_function_test.cpp +++ b/source/module_base/test/global_function_test.cpp @@ -13,7 +13,7 @@ #include "gmock/gmock.h" #include "gtest/gtest.h" -#include +#include /************************************************ * unit test of functions in global_function @@ -57,7 +57,7 @@ * - print out warning info in running.log file * - COPYARRAY * - copy complex or double arrays - * - IS_COLUMN_MAJOR_KS_SOLVER() + * - IS_COLUMN_MAJOR_KS_SOLVER(PARAM.input.ks_solver) * - judge whether the KS_SOLVER is column major * - VECTOR_TO_PTR * - get a copy of the ptr of a vector @@ -300,7 +300,8 @@ TEST_F(GlobalFunctionTest, OutV2) { tmp_para += "a"; length = tmp_para.size()+1; - if (length == 32) EXPECT_THAT(str, testing::HasSubstr(tmp_para)); + if (length == 32) { EXPECT_THAT(str, testing::HasSubstr(tmp_para)); +} } ifs.close(); } @@ -430,8 +431,8 @@ TEST_F(GlobalFunctionTest, MakeDir) TEST_F(GlobalFunctionTest, OutTime) { std::string name = "scf"; - start = time(NULL); - end = time(NULL) + 200; + start = time(nullptr); + end = time(nullptr) + 200; ModuleBase::GlobalFunc::OUT_TIME(name, start, end); GlobalV::ofs_warning.close(); ifs.open("warning.log"); @@ -654,8 +655,8 @@ TEST_F(GlobalFunctionTest, COPYARRAY) TEST_F(GlobalFunctionTest,IsColumnMajor) { - GlobalV::KS_SOLVER = "genelpa"; - EXPECT_TRUE(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()); + PARAM.input.ks_solver = "genelpa"; + EXPECT_TRUE(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.input.ks_solver)); } TEST_F(GlobalFunctionTest,Vector2Ptr) diff --git a/source/module_elecstate/elecstate_getters.cpp b/source/module_elecstate/elecstate_getters.cpp index 90b4c3ffdd..30bf81f94c 100644 --- a/source/module_elecstate/elecstate_getters.cpp +++ b/source/module_elecstate/elecstate_getters.cpp @@ -55,7 +55,7 @@ double get_ucell_tot_magnetization_nc_z() std::string get_ks_solver_type() { - return GlobalV::KS_SOLVER; + return PARAM.inp.ks_solver; } } // namespace elecstate diff --git a/source/module_elecstate/elecstate_lcao.cpp b/source/module_elecstate/elecstate_lcao.cpp index b50c101abc..0650af2ecf 100644 --- a/source/module_elecstate/elecstate_lcao.cpp +++ b/source/module_elecstate/elecstate_lcao.cpp @@ -38,9 +38,9 @@ void ElecStateLCAO>::psiToRho(const psi::Psi> dm_k_2d(); - if (GlobalV::KS_SOLVER == "genelpa" || GlobalV::KS_SOLVER == "elpa" || GlobalV::KS_SOLVER == "scalapack_gvx" || GlobalV::KS_SOLVER == "lapack" - || GlobalV::KS_SOLVER == "cusolver" || GlobalV::KS_SOLVER == "cusolvermp" - || GlobalV::KS_SOLVER == "cg_in_lcao") // Peize Lin test 2019-05-15 + if (PARAM.inp.ks_solver == "genelpa" || PARAM.inp.ks_solver == "elpa" || PARAM.inp.ks_solver == "scalapack_gvx" || PARAM.inp.ks_solver == "lapack" + || PARAM.inp.ks_solver == "cusolver" || PARAM.inp.ks_solver == "cusolvermp" + || PARAM.inp.ks_solver == "cg_in_lcao") // Peize Lin test 2019-05-15 { elecstate::cal_dm_psi(this->DM->get_paraV_pointer(), this->wg, @@ -91,8 +91,8 @@ void ElecStateLCAO::psiToRho(const psi::Psi& psi) this->calculate_weights(); this->calEBand(); - if (GlobalV::KS_SOLVER == "genelpa" || GlobalV::KS_SOLVER == "elpa" || GlobalV::KS_SOLVER == "scalapack_gvx" || GlobalV::KS_SOLVER == "lapack" - || GlobalV::KS_SOLVER == "cusolver" || GlobalV::KS_SOLVER == "cusolvermp" || GlobalV::KS_SOLVER == "cg_in_lcao") + if (PARAM.inp.ks_solver == "genelpa" || PARAM.inp.ks_solver == "elpa" || PARAM.inp.ks_solver == "scalapack_gvx" || PARAM.inp.ks_solver == "lapack" + || PARAM.inp.ks_solver == "cusolver" || PARAM.inp.ks_solver == "cusolvermp" || PARAM.inp.ks_solver == "cg_in_lcao") { ModuleBase::timer::tick("ElecStateLCAO", "cal_dm_2d"); diff --git a/source/module_elecstate/elecstate_lcao_tddft.cpp b/source/module_elecstate/elecstate_lcao_tddft.cpp index ec145ca326..48ce026e1f 100644 --- a/source/module_elecstate/elecstate_lcao_tddft.cpp +++ b/source/module_elecstate/elecstate_lcao_tddft.cpp @@ -23,8 +23,8 @@ void ElecStateLCAO_TDDFT::psiToRho_td(const psi::Psi>& psi) // this part for calculating DMK in 2d-block format, not used for charge now // psi::Psi> dm_k_2d(); - if (GlobalV::KS_SOLVER == "genelpa" || GlobalV::KS_SOLVER == "scalapack_gvx" - || GlobalV::KS_SOLVER == "lapack") // Peize Lin test 2019-05-15 + if (PARAM.inp.ks_solver == "genelpa" || PARAM.inp.ks_solver == "scalapack_gvx" + || PARAM.inp.ks_solver == "lapack") // Peize Lin test 2019-05-15 { elecstate::cal_dm_psi(this->DM->get_paraV_pointer(), this->wg, psi, *(this->DM)); this->DM->cal_DMR(); diff --git a/source/module_elecstate/module_dm/cal_dm_psi.cpp b/source/module_elecstate/module_dm/cal_dm_psi.cpp index 863bf8e035..47fbfbf8c3 100644 --- a/source/module_elecstate/module_dm/cal_dm_psi.cpp +++ b/source/module_elecstate/module_dm/cal_dm_psi.cpp @@ -1,5 +1,6 @@ #include "cal_dm_psi.h" +#include "module_parameter/parameter.h" #include "module_base/blas_connector.h" #include "module_base/scalapack_connector.h" #include "module_base/timer.h" @@ -123,7 +124,7 @@ void cal_dm_psi(const Parallel_Orbitals* ParaV, // C++: dm(iw1,iw2) = wfc(ib,iw1).T * wg_wfc(ib,iw2) #ifdef __MPI - if (GlobalV::KS_SOLVER == "cg_in_lcao") + if (PARAM.inp.ks_solver == "cg_in_lcao") { psiMulPsi(wg_wfc, wfc, dmk_pointer); } else diff --git a/source/module_esolver/esolver_ks_lcao.cpp b/source/module_esolver/esolver_ks_lcao.cpp index 5bca940644..735ef0dda1 100644 --- a/source/module_esolver/esolver_ks_lcao.cpp +++ b/source/module_esolver/esolver_ks_lcao.cpp @@ -705,8 +705,8 @@ void ESolver_KS_LCAO::hamilt2density(int istep, int iter, double ethr) this->pelec->f_en.eband = 0.0; this->pelec->f_en.demet = 0.0; - hsolver::HSolverLCAO hsolver_lcao_obj(&(this->pv), GlobalV::KS_SOLVER); - hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, GlobalV::KS_SOLVER, false); + hsolver::HSolverLCAO hsolver_lcao_obj(&(this->pv), PARAM.inp.ks_solver); + hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, PARAM.inp.ks_solver, false); if (PARAM.inp.out_bandgap) diff --git a/source/module_esolver/esolver_ks_lcao_tddft.cpp b/source/module_esolver/esolver_ks_lcao_tddft.cpp index 2baa7d713d..17d36bc46d 100644 --- a/source/module_esolver/esolver_ks_lcao_tddft.cpp +++ b/source/module_esolver/esolver_ks_lcao_tddft.cpp @@ -168,8 +168,8 @@ void ESolver_KS_LCAO_TDDFT::hamilt2density(const int istep, const int iter, cons this->pelec->f_en.demet = 0.0; if (this->psi != nullptr) { - hsolver::HSolverLCAO> hsolver_lcao_obj(&this->pv, GlobalV::KS_SOLVER); - hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec_td, GlobalV::KS_SOLVER, false); + hsolver::HSolverLCAO> hsolver_lcao_obj(&this->pv, PARAM.inp.ks_solver); + hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec_td, PARAM.inp.ks_solver, false); } } // else diff --git a/source/module_esolver/esolver_ks_pw.cpp b/source/module_esolver/esolver_ks_pw.cpp index f8d4e512e8..22ec454c2a 100644 --- a/source/module_esolver/esolver_ks_pw.cpp +++ b/source/module_esolver/esolver_ks_pw.cpp @@ -141,7 +141,7 @@ void ESolver_KS_PW::before_all_runners(const Input_para& inp, UnitCel //! 7) prepare some parameters for electronic wave functions initilization this->p_wf_init = new psi::WFInit(PARAM.inp.init_wfc, - GlobalV::KS_SOLVER, + PARAM.inp.ks_solver, PARAM.inp.basis_type, PARAM.inp.psi_initializer, &this->wf, diff --git a/source/module_esolver/esolver_lj.cpp b/source/module_esolver/esolver_lj.cpp index c33b8ab331..6bfb943fa4 100644 --- a/source/module_esolver/esolver_lj.cpp +++ b/source/module_esolver/esolver_lj.cpp @@ -38,7 +38,7 @@ namespace ModuleESolver GlobalV::ofs_running, grid_neigh, ucell, - GlobalV::SEARCH_RADIUS, + search_radius, PARAM.inp.test_atom_input); double distance=0.0; @@ -95,7 +95,7 @@ namespace ModuleESolver PARAM.inp.search_pbc, grid_neigh, ucell, - GlobalV::SEARCH_RADIUS, + search_radius, PARAM.inp.test_atom_input); #endif } @@ -200,7 +200,7 @@ namespace ModuleESolver } // set the search radius - GlobalV::SEARCH_RADIUS = rcut_max + 0.01; + search_radius = rcut_max + 0.01; } void ESolver_LJ::set_c6_c12(const int rule, const std::vector epsilon, const std::vector sigma) diff --git a/source/module_esolver/esolver_lj.h b/source/module_esolver/esolver_lj.h index e7d573d825..668e39c287 100644 --- a/source/module_esolver/esolver_lj.h +++ b/source/module_esolver/esolver_lj.h @@ -42,6 +42,7 @@ namespace ModuleESolver void cal_en_shift(const bool is_shift); //--------------temporary---------------------------- + double search_radius=-1.0; ModuleBase::matrix lj_rcut; ModuleBase::matrix lj_c12; ModuleBase::matrix lj_c6; diff --git a/source/module_esolver/esolver_sdft_pw.cpp b/source/module_esolver/esolver_sdft_pw.cpp index 81f487a912..19ce25f03b 100644 --- a/source/module_esolver/esolver_sdft_pw.cpp +++ b/source/module_esolver/esolver_sdft_pw.cpp @@ -78,7 +78,7 @@ void ESolver_SDFT_PW::before_all_runners(const Input_para& inp, UnitCell& ucell) // 6) prepare some parameters for electronic wave functions initilization this->p_wf_init = new psi::WFInit>(PARAM.inp.init_wfc, - GlobalV::KS_SOLVER, + PARAM.inp.ks_solver, PARAM.inp.basis_type, PARAM.inp.psi_initializer, &this->wf, diff --git a/source/module_esolver/lcao_before_scf.cpp b/source/module_esolver/lcao_before_scf.cpp index 4c2ffb4e2f..64bc023bfd 100644 --- a/source/module_esolver/lcao_before_scf.cpp +++ b/source/module_esolver/lcao_before_scf.cpp @@ -62,12 +62,12 @@ void ESolver_KS_LCAO::beforesolver(const int istep) { nsk = PARAM.inp.nspin; ncol = this->pv.ncol_bands; - if (GlobalV::KS_SOLVER == "genelpa" - || GlobalV::KS_SOLVER == "elpa" - || GlobalV::KS_SOLVER == "lapack" - || GlobalV::KS_SOLVER == "pexsi" - || GlobalV::KS_SOLVER == "cusolver" - || GlobalV::KS_SOLVER == "cusolvermp") { + if (PARAM.inp.ks_solver == "genelpa" + || PARAM.inp.ks_solver == "elpa" + || PARAM.inp.ks_solver == "lapack" + || PARAM.inp.ks_solver == "pexsi" + || PARAM.inp.ks_solver == "cusolver" + || PARAM.inp.ks_solver == "cusolvermp") { ncol = this->pv.ncol; } } @@ -156,7 +156,7 @@ void ESolver_KS_LCAO::beforesolver(const int istep) &(this->pv), PARAM.inp.nspin, this->kv, - GlobalV::KS_SOLVER, + PARAM.inp.ks_solver, this->p_hamilt, this->psi, this->pelec); diff --git a/source/module_esolver/lcao_gets.cpp b/source/module_esolver/lcao_gets.cpp index ae3413d36d..cb1c1ab696 100644 --- a/source/module_esolver/lcao_gets.cpp +++ b/source/module_esolver/lcao_gets.cpp @@ -44,7 +44,8 @@ void ESolver_KS_LCAO, double>::get_S(void) { ModuleBase::TITLE("ESolver_KS_LCAO", "get_S"); // (1) Find adjacent atoms for each atom. - GlobalV::SEARCH_RADIUS = atom_arrange::set_sr_NL(GlobalV::ofs_running, + double search_radius = -1.0; + search_radius = atom_arrange::set_sr_NL(GlobalV::ofs_running, PARAM.inp.out_level, orb_.get_rcutmax_Phi(), GlobalC::ucell.infoNL.get_rcutmax_Beta(), @@ -54,7 +55,7 @@ void ESolver_KS_LCAO, double>::get_S(void) GlobalV::ofs_running, GlobalC::GridD, GlobalC::ucell, - GlobalV::SEARCH_RADIUS, + search_radius, PARAM.inp.test_atom_input); this->RA.for_2d(this->pv, PARAM.globalv.gamma_only_local, orb_.cutoffs()); @@ -85,7 +86,8 @@ void ESolver_KS_LCAO, std::complex>::get_S(void) { ModuleBase::TITLE("ESolver_KS_LCAO", "get_S"); // (1) Find adjacent atoms for each atom. - GlobalV::SEARCH_RADIUS = atom_arrange::set_sr_NL(GlobalV::ofs_running, + double search_radius = -1.0; + search_radius = atom_arrange::set_sr_NL(GlobalV::ofs_running, PARAM.inp.out_level, orb_.get_rcutmax_Phi(), GlobalC::ucell.infoNL.get_rcutmax_Beta(), @@ -95,7 +97,7 @@ void ESolver_KS_LCAO, std::complex>::get_S(void) GlobalV::ofs_running, GlobalC::GridD, GlobalC::ucell, - GlobalV::SEARCH_RADIUS, + search_radius, PARAM.inp.test_atom_input); this->RA.for_2d(this->pv, PARAM.globalv.gamma_only_local, orb_.cutoffs()); diff --git a/source/module_esolver/lcao_nscf.cpp b/source/module_esolver/lcao_nscf.cpp index bca272e1e4..7904c0d6ea 100644 --- a/source/module_esolver/lcao_nscf.cpp +++ b/source/module_esolver/lcao_nscf.cpp @@ -52,8 +52,8 @@ void ESolver_KS_LCAO::nscf() { // then when the istep is a variable of scf or nscf, // istep becomes istep-1, this should be fixed in future int istep = 0; - hsolver::HSolverLCAO hsolver_lcao_obj(&(this->pv), GlobalV::KS_SOLVER); - hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, GlobalV::KS_SOLVER, true); + hsolver::HSolverLCAO hsolver_lcao_obj(&(this->pv), PARAM.inp.ks_solver); + hsolver_lcao_obj.solve(this->p_hamilt, this->psi[0], this->pelec, PARAM.inp.ks_solver, true); time_t time_finish = std::time(nullptr); ModuleBase::GlobalFunc::OUT_TIME("cal_bands", time_start, time_finish); diff --git a/source/module_esolver/lcao_others.cpp b/source/module_esolver/lcao_others.cpp index 994e7d7b58..667eac6c20 100644 --- a/source/module_esolver/lcao_others.cpp +++ b/source/module_esolver/lcao_others.cpp @@ -70,17 +70,12 @@ void ESolver_KS_LCAO::others(const int istep) { // test_search_neighbor(); std::cout << FmtCore::format("\n * * * * * *\n << Start %s.\n", "testing neighbour"); - if (GlobalV::SEARCH_RADIUS < 0) - { - std::cout << " SEARCH_RADIUS : " << GlobalV::SEARCH_RADIUS << std::endl; - std::cout << " please make sure search_radius > 0" << std::endl; - } - + double search_radius = PARAM.inp.search_radius; atom_arrange::search(PARAM.inp.search_pbc, GlobalV::ofs_running, GlobalC::GridD, GlobalC::ucell, - GlobalV::SEARCH_RADIUS, + search_radius, PARAM.inp.test_atom_input, true); std::cout << FmtCore::format(" >> Finish %s.\n * * * * * *\n", "testing neighbour"); diff --git a/source/module_esolver/set_matrix_grid.cpp b/source/module_esolver/set_matrix_grid.cpp index e320678ebe..508ecc452d 100644 --- a/source/module_esolver/set_matrix_grid.cpp +++ b/source/module_esolver/set_matrix_grid.cpp @@ -27,7 +27,8 @@ void ESolver_KS_LCAO::set_matrix_grid(Record_adj& ra) ModuleBase::timer::tick("ESolver_KS_LCAO", "set_matrix_grid"); // (1) Find adjacent atoms for each atom. - GlobalV::SEARCH_RADIUS = atom_arrange::set_sr_NL(GlobalV::ofs_running, + double search_radius = -1.0; + search_radius = atom_arrange::set_sr_NL(GlobalV::ofs_running, PARAM.inp.out_level, orb_.get_rcutmax_Phi(), GlobalC::ucell.infoNL.get_rcutmax_Beta(), @@ -37,7 +38,7 @@ void ESolver_KS_LCAO::set_matrix_grid(Record_adj& ra) GlobalV::ofs_running, GlobalC::GridD, GlobalC::ucell, - GlobalV::SEARCH_RADIUS, + search_radius, PARAM.inp.test_atom_input); // ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running,"SEARCH ADJACENT diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/LCAO_init_basis.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/LCAO_init_basis.cpp index 1ac92ff578..92384e4c5c 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/LCAO_init_basis.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/LCAO_init_basis.cpp @@ -24,21 +24,21 @@ void init_basis_lcao(Parallel_Orbitals& pv, ModuleBase::TITLE("ESolver_KS_LCAO", "init_basis_lcao"); const int nlocal = GlobalV::NLOCAL; - + int nb2d = PARAM.inp.nb2d; // autoset NB2D first - if (GlobalV::NB2D == 0) + if (nb2d == 0) { if (nlocal > 0) { - GlobalV::NB2D = (PARAM.inp.nspin == 4) ? 2 : 1; + nb2d = (PARAM.inp.nspin == 4) ? 2 : 1; } if (nlocal > 500) { - GlobalV::NB2D = 32; + nb2d = 32; } if (nlocal > 1000) { - GlobalV::NB2D = 64; + nb2d = 64; } } @@ -75,7 +75,7 @@ void init_basis_lcao(Parallel_Orbitals& pv, // storage form of H and S matrices on each processor // is determined in 'divide_HS_2d' subroutine - int try_nb = pv.init(nlocal, nlocal, GlobalV::NB2D, DIAG_WORLD); + int try_nb = pv.init(nlocal, nlocal, nb2d, DIAG_WORLD); try_nb += pv.set_nloc_wfc_Eij(GlobalV::NBANDS, GlobalV::ofs_running, GlobalV::ofs_warning); if (try_nb != 0) { diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/LCAO_set_mat2d.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/LCAO_set_mat2d.cpp index 0e1f98ed5d..5506ca3904 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/LCAO_set_mat2d.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/LCAO_set_mat2d.cpp @@ -1,5 +1,6 @@ #include "module_hamilt_lcao/hamilt_lcaodft/LCAO_domain.h" +#include "module_parameter/parameter.h" namespace LCAO_domain { @@ -22,7 +23,7 @@ void set_mat2d( const int ic = pv.global2local_col(global_ic); const long index = - ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER() + ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver) ? ic * static_cast(pv.nrow) + ir : ir * static_cast(pv.ncol) + ic; diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/fedm_gamma.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/fedm_gamma.cpp index 6a466c3c59..728525a656 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/fedm_gamma.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/fedm_gamma.cpp @@ -47,7 +47,7 @@ void Force_LCAO::cal_fedm( elecstate::DensityMatrix edm(&pv, nspin); #ifdef __PEXSI - if (GlobalV::KS_SOLVER == "pexsi") + if (PARAM.inp.ks_solver == "pexsi") { auto pes = dynamic_cast*>(pelec); for (int ik = 0; ik < nspin; ik++) diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/deepks_lcao.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/deepks_lcao.cpp index d3b4a96dd7..95df523db2 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/deepks_lcao.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/deepks_lcao.cpp @@ -518,7 +518,7 @@ void hamilt::DeePKS>::contributeHk(int ik) // set SK to zero and then calculate SK for each k vector ModuleBase::GlobalFunc::ZEROS(h_delta_k, this->hsk->get_size()); - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { const int nrow = this->hsk->get_pv()->get_row_size(); hamilt::folding_HR(*this->H_V_delta, h_delta_k, this->kvec_d[ik], nrow, 1); diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/operator_lcao.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/operator_lcao.cpp index ebc60ac7f4..7b55390e65 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/operator_lcao.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/operator_lcao.cpp @@ -229,7 +229,7 @@ template void OperatorLCAO::contributeHk(int ik) { ModuleBase::TITLE("OperatorLCAO", "contributeHk"); ModuleBase::timer::tick("OperatorLCAO", "contributeHk"); - if(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { const int nrow = this->hsk->get_pv()->get_row_size(); hamilt::folding_HR(*this->hR, this->hsk->get_hk(), this->kvec_d[ik], nrow, 1); diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/overlap_new.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/overlap_new.cpp index c29edfc0e5..608f6c5180 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/overlap_new.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/overlap_new.cpp @@ -1,5 +1,6 @@ #include "overlap_new.h" +#include "module_parameter/parameter.h" #include "module_base/timer.h" #include "module_base/tool_title.h" #include "module_cell/module_neighbor/sltk_grid_driver.h" @@ -196,7 +197,7 @@ void hamilt::OverlapNew>::contributeHk(int ik) ModuleBase::timer::tick("OverlapNew", "contributeHk"); // set SK to zero and then calculate SK for each k vector this->hsk->set_zero_sk(); - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { const int nrow = this->SR->get_atom_pair(0).get_paraV()->get_row_size(); hamilt::folding_HR(*this->SR, this->hsk->get_sk(), this->kvec_d[ik], nrow, 1); diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/sc_lambda_lcao.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/sc_lambda_lcao.cpp index 2aa96175dd..7f7302443c 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/sc_lambda_lcao.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/sc_lambda_lcao.cpp @@ -1,5 +1,6 @@ #include "sc_lambda_lcao.h" #include "module_hamilt_lcao/module_deltaspin/spin_constrain.h" +#include "module_parameter/parameter.h" #include namespace hamilt @@ -22,7 +23,7 @@ void OperatorScLambda, std::complex>>: = SpinConstrain, base_device::DEVICE_CPU>::getScInstance(); std::vector> h_lambda(this->hsk->get_pv()->nloc); std::fill(h_lambda.begin(), h_lambda.end(), std::complex(0, 0)); - sc.cal_h_lambda(&h_lambda[0], this->hsk->get_sk(), ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(), this->isk[ik]); + sc.cal_h_lambda(&h_lambda[0], this->hsk->get_sk(), ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver), this->isk[ik]); std::complex* hk = this->hsk->get_hk(); for (int irc = 0; irc < this->hsk->get_pv()->nloc; irc++) { @@ -42,7 +43,7 @@ void OperatorScLambda, double>>::contributeHk( = SpinConstrain, base_device::DEVICE_CPU>::getScInstance(); std::vector> h_lambda(this->hsk->get_pv()->nloc); std::fill(h_lambda.begin(), h_lambda.end(), std::complex(0, 0)); - sc.cal_h_lambda(&h_lambda[0], this->hsk->get_sk(), ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(), this->isk[ik]); + sc.cal_h_lambda(&h_lambda[0], this->hsk->get_sk(), ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver), this->isk[ik]); std::complex* hk = this->hsk->get_hk(); for (int irc = 0; irc < this->hsk->get_pv()->nloc; irc++) { 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 a41b82c901..a69454e418 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 @@ -396,7 +396,7 @@ void TDEkinetic, double>>::contributeHk(int ik output_hR_done = true; } // folding inside HR to HK - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { const int nrow = paraV->get_row_size(); hamilt::folding_HR(*this->hR_tmp, this->hsk->get_hk(), this->kvec_d[ik], nrow, 1); diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_nonlocal_lcao.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_nonlocal_lcao.cpp index 545a8fd95d..328b55dbec 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_nonlocal_lcao.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/td_nonlocal_lcao.cpp @@ -1,5 +1,6 @@ #include "td_nonlocal_lcao.h" +#include "module_parameter/parameter.h" #include "module_base/timer.h" #include "module_base/tool_title.h" #include "module_cell/module_neighbor/sltk_grid_driver.h" @@ -444,7 +445,7 @@ void hamilt::TDNonlocal, double>>::con ModuleBase::TITLE("TDNonlocal", "contributeHk"); ModuleBase::timer::tick("TDNonlocal", "contributeHk"); // folding inside HR to HK - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { const int nrow = this->hsk->get_pv()->get_row_size(); folding_HR(*this->hR_tmp, this->hsk->get_hk(), this->kvec_d[ik], nrow, 1); diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/test/test_sc_lambda_lcao.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/test/test_sc_lambda_lcao.cpp index 6bde15f26a..17aa7b76d7 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/test/test_sc_lambda_lcao.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/operator_lcao/test/test_sc_lambda_lcao.cpp @@ -1,9 +1,12 @@ #include #include - -#include "../sc_lambda_lcao.h" #include "gmock/gmock.h" #include "gtest/gtest.h" +#define private public +#include "module_parameter/parameter.h" +#undef private + +#include "../sc_lambda_lcao.h" #include "module_cell/klist.h" #include "module_hamilt_lcao/module_deltaspin/spin_constrain.h" @@ -103,9 +106,9 @@ TEST_F(ScLambdaLCAOTest, ContributeHk) sc_lambda[0][1] = 1.0; sc_lambda[0][2] = 1.0; sc.set_sc_lambda(sc_lambda, 1); - // set KS_SOLVER, which determines IS_COLUMN_MAJOR_KS_SOLVER() - GlobalV::KS_SOLVER = "genelpa"; - EXPECT_TRUE(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()); + // set KS_SOLVER, which determines IS_COLUMN_MAJOR_KS_SOLVER(PARAM.input.ks_solver) + PARAM.input.ks_solver = "genelpa"; + EXPECT_TRUE(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.input.ks_solver)); // set sc_lambda_op auto sc_lambda_op = hamilt::OperatorScLambda, std::complex>>(&hsk, @@ -162,9 +165,9 @@ TEST_F(ScLambdaLCAOTest, ContributeHkS2) sc_lambda[0][1] = 0.0; sc_lambda[0][2] = 1.0; sc.set_sc_lambda(sc_lambda, 1); - // set KS_SOLVER, which determines IS_COLUMN_MAJOR_KS_SOLVER() - GlobalV::KS_SOLVER = "genelpa"; - EXPECT_TRUE(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()); + // set KS_SOLVER, which determines IS_COLUMN_MAJOR_KS_SOLVER(PARAM.input.ks_solver) + PARAM.input.ks_solver = "genelpa"; + EXPECT_TRUE(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.input.ks_solver)); // set sc_lambda_op auto sc_lambda_op = hamilt::OperatorScLambda, double>>(&hsk, diff --git a/source/module_hamilt_lcao/hamilt_lcaodft/spar_u.cpp b/source/module_hamilt_lcao/hamilt_lcaodft/spar_u.cpp index fe0c903e1f..f8f886688a 100644 --- a/source/module_hamilt_lcao/hamilt_lcaodft/spar_u.cpp +++ b/source/module_hamilt_lcao/hamilt_lcaodft/spar_u.cpp @@ -1,5 +1,6 @@ #include "spar_u.h" #include "module_base/parallel_reduce.h" +#include "module_parameter/parameter.h" #include "module_hamilt_pw/hamilt_pwdft/global.h" #include "module_base/timer.h" #include "module_hamilt_lcao/module_dftu/dftu.h" @@ -60,7 +61,7 @@ void sparse_format::cal_HR_dftu( for (auto &col_loop : row_loop.second) { ic = pv.global2local_col(col_loop.first); - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { iic = ir + ic * pv.nrow; } @@ -85,7 +86,7 @@ void sparse_format::cal_HR_dftu( ic = pv.global2local_col(j); if (ic >= 0) { - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { iic = ir + ic * pv.nrow; } @@ -180,7 +181,7 @@ void sparse_format::cal_HR_dftu_soc( for (auto &col_loop : row_loop.second) { ic = pv.global2local_col(col_loop.first); - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { iic = ir + ic * pv.nrow; } @@ -205,7 +206,7 @@ void sparse_format::cal_HR_dftu_soc( ic = pv.global2local_col(j); if (ic >= 0) { - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { iic = ir + ic * pv.nrow; } diff --git a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_odelta.cpp b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_odelta.cpp index 4417265c51..e51f8a66d3 100644 --- a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_odelta.cpp +++ b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_odelta.cpp @@ -64,7 +64,7 @@ void LCAO_Deepks::cal_o_delta_k(const std::vector= 0 && nu >= 0) { int iic; - if(GlobalV::KS_SOLVER=="genelpa" || GlobalV::KS_SOLVER=="scalapack_gvx" || GlobalV::KS_SOLVER=="pexsi") // save the matrix as column major format + if(PARAM.inp.ks_solver=="genelpa" || PARAM.inp.ks_solver=="scalapack_gvx" || PARAM.inp.ks_solver=="pexsi") // save the matrix as column major format { iic = mu + nu * pv->nrow; } diff --git a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_vdelta.cpp b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_vdelta.cpp index 092ceb50cc..90b4687778 100644 --- a/source/module_hamilt_lcao/module_deepks/LCAO_deepks_vdelta.cpp +++ b/source/module_hamilt_lcao/module_deepks/LCAO_deepks_vdelta.cpp @@ -64,7 +64,7 @@ void LCAO_Deepks::cal_e_delta_band_k(const std::vector= 0 && nu >= 0) { int iic; - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { iic = mu + nu * pv->nrow; } diff --git a/source/module_hamilt_lcao/module_deepks/cal_gdmx.cpp b/source/module_hamilt_lcao/module_deepks/cal_gdmx.cpp index 3ecefaeada..6950e030ea 100644 --- a/source/module_hamilt_lcao/module_deepks/cal_gdmx.cpp +++ b/source/module_hamilt_lcao/module_deepks/cal_gdmx.cpp @@ -107,7 +107,7 @@ void LCAO_Deepks::cal_gdmx(const std::vector& dm, hamilt::AtomPair dm_pair(ibt1, ibt2, 0, 0, 0, pv); dm_pair.allocate(nullptr, 1); - if(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { dm_pair.add_from_matrix(dm.data(), pv->get_row_size(), 1.0, 1); } diff --git a/source/module_hamilt_lcao/module_deepks/cal_gdmx_k.cpp b/source/module_hamilt_lcao/module_deepks/cal_gdmx_k.cpp index eed8597534..62a3a25c26 100644 --- a/source/module_hamilt_lcao/module_deepks/cal_gdmx_k.cpp +++ b/source/module_hamilt_lcao/module_deepks/cal_gdmx_k.cpp @@ -112,7 +112,7 @@ void LCAO_Deepks::cal_gdmx_k(const std::vector> double sinp, cosp; ModuleBase::libm::sincos(arg, &sinp, &cosp); const std::complex kphase = std::complex(cosp, sinp); - if(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { dm_pair.add_from_matrix(dm[ik].data(), pv->get_row_size(), kphase, 1); } diff --git a/source/module_hamilt_lcao/module_deepks/deepks_fgamma.cpp b/source/module_hamilt_lcao/module_deepks/deepks_fgamma.cpp index cebd0f7c21..57615ce4a9 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_fgamma.cpp +++ b/source/module_hamilt_lcao/module_deepks/deepks_fgamma.cpp @@ -104,7 +104,7 @@ void DeePKS_domain::cal_f_delta_gamma( for(int is=0;is dm_pair(ibt1, ibt2, (dR2-dR1).x, (dR2-dR1).y, (dR2-dR1).z, &pv); dm_pair.allocate(nullptr, 1); @@ -106,7 +107,7 @@ void DeePKS_domain::cal_f_delta_k( double sinp, cosp; ModuleBase::libm::sincos(arg, &sinp, &cosp); const std::complex kphase = std::complex(cosp, sinp); - if(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if(ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { dm_pair.add_from_matrix(dm[ik].data(), pv.get_row_size(), kphase, 1); } @@ -266,7 +267,8 @@ void DeePKS_domain::cal_f_delta_k( { for(int j=0;j<3;++j) { - if(j>i) svnl_dalpha(j,i) = svnl_dalpha(i,j); + if(j>i) { svnl_dalpha(j,i) = svnl_dalpha(i,j); +} svnl_dalpha(i,j) *= weight ; } } diff --git a/source/module_hamilt_lcao/module_deepks/deepks_hmat.cpp b/source/module_hamilt_lcao/module_deepks/deepks_hmat.cpp index 08a56ca57e..85d3b443da 100644 --- a/source/module_hamilt_lcao/module_deepks/deepks_hmat.cpp +++ b/source/module_hamilt_lcao/module_deepks/deepks_hmat.cpp @@ -1,5 +1,6 @@ #ifdef __DEEPKS +#include "module_parameter/parameter.h" #include "LCAO_deepks.h" #include "module_base/parallel_reduce.h" @@ -48,7 +49,7 @@ void DeePKS_domain::collect_h_mat( if (ic>=0) { int iic=0; - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { iic=ir+ic*pv.nrow; } diff --git a/source/module_hamilt_lcao/module_deepks/orbital_precalc.cpp b/source/module_hamilt_lcao/module_deepks/orbital_precalc.cpp index 39fa86150c..0e01573331 100644 --- a/source/module_hamilt_lcao/module_deepks/orbital_precalc.cpp +++ b/source/module_hamilt_lcao/module_deepks/orbital_precalc.cpp @@ -157,7 +157,7 @@ void LCAO_Deepks::cal_orbital_precalc( dm_pair.allocate(dm_array.data(), 0); - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { dm_pair.add_from_matrix(dm_hl[0][is].c, pv->get_row_size(), diff --git a/source/module_hamilt_lcao/module_deepks/orbital_precalc_k.cpp b/source/module_hamilt_lcao/module_deepks/orbital_precalc_k.cpp index 956ed15ba4..fd8b24e0c7 100644 --- a/source/module_hamilt_lcao/module_deepks/orbital_precalc_k.cpp +++ b/source/module_hamilt_lcao/module_deepks/orbital_precalc_k.cpp @@ -193,7 +193,7 @@ void LCAO_Deepks::cal_orbital_precalc_k( const std::complex kphase = std::complex(cosp, sinp); - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { dm_pair.add_from_matrix(dm_hl_k[0][ik].c, pv->get_row_size(), diff --git a/source/module_hamilt_lcao/module_deepks/test/nnr.cpp b/source/module_hamilt_lcao/module_deepks/test/nnr.cpp index b1d882da72..a7616f7a59 100644 --- a/source/module_hamilt_lcao/module_deepks/test/nnr.cpp +++ b/source/module_hamilt_lcao/module_deepks/test/nnr.cpp @@ -214,7 +214,7 @@ void test_deepks::folding_nnr(const Test_Deepks::K_Vectors &kv) if(nu<0)continue; int iic; - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER() ) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.input.ks_solver) ) { iic=mu+nu*ParaO.nrow; } diff --git a/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp b/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp index c973b121d7..7beb16f42e 100644 --- a/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp +++ b/source/module_hamilt_lcao/module_dftu/dftu_folding.cpp @@ -240,7 +240,7 @@ void DFTU::folding_matrix_k( } int iic; - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { iic = mu + nu * pv.nrow; } @@ -278,7 +278,7 @@ void DFTU::folding_matrix_k_new(const int ik, ModuleBase::timer::tick("DFTU", "folding_matrix_k_new"); int hk_type = 0; - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { hk_type = 1; } diff --git a/source/module_hamilt_pw/hamilt_pwdft/wavefunc.cpp b/source/module_hamilt_pw/hamilt_pwdft/wavefunc.cpp index 7353ceabde..00c0bded36 100644 --- a/source/module_hamilt_pw/hamilt_pwdft/wavefunc.cpp +++ b/source/module_hamilt_pw/hamilt_pwdft/wavefunc.cpp @@ -197,7 +197,7 @@ void diago_PAO_in_pw_k2(const int& ik, std::vector> s_wfcatom(nbands * nbasis); castmem_z2c_h2h_op()(cpu_ctx, cpu_ctx, s_wfcatom.data(), wfcatom.c, nbands * nbasis); - if (GlobalV::KS_SOLVER == "cg") + if (PARAM.inp.ks_solver == "cg") { std::vector etfile(nbands, 0.0); if (phm_in != nullptr) @@ -256,7 +256,7 @@ void diago_PAO_in_pw_k2(const int& ik, { p_wf->random(wvf.get_pointer(), 0, nbands, ik, wfc_basis); - if (GlobalV::KS_SOLVER == "cg") // xiaohui add 2013-09-02 + if (PARAM.inp.ks_solver == "cg") // xiaohui add 2013-09-02 { if (phm_in != nullptr) { @@ -301,7 +301,7 @@ void diago_PAO_in_pw_k2(const int& ik, castmem_z2c_h2h_op()(cpu_ctx, cpu_ctx, s_wfcatom.data(), wfcatom.c, starting_nw * nbasis); // if(GlobalV::DIAGO_TYPE == "cg") xiaohui modify 2013-09-02 - if (GlobalV::KS_SOLVER == "cg") // xiaohui add 2013-09-02 + if (PARAM.inp.ks_solver == "cg") // xiaohui add 2013-09-02 { if (phm_in != nullptr) { @@ -353,7 +353,7 @@ void diago_PAO_in_pw_k2(const int& ik, ModuleIO::read_wfc_pw(filename.str(), wfc_basis, ik, p_wf->nkstot, wfcatom); - if (GlobalV::KS_SOLVER == "cg") + if (PARAM.inp.ks_solver == "cg") { std::vector etfile(nbands, 0.0); if (phm_in != nullptr) @@ -412,7 +412,7 @@ void diago_PAO_in_pw_k2(const int& ik, if (p_wf->init_wfc == "random" || (p_wf->init_wfc.substr(0, 6) == "atomic" && GlobalC::ucell.natomwfc == 0)) { p_wf->random(wvf.get_pointer(), 0, nbands, ik, wfc_basis); - if (GlobalV::KS_SOLVER == "cg") // xiaohui add 2013-09-02 + if (PARAM.inp.ks_solver == "cg") // xiaohui add 2013-09-02 { if (phm_in != nullptr) { @@ -455,7 +455,7 @@ void diago_PAO_in_pw_k2(const int& ik, // (7) Diago with cg method. // if(GlobalV::DIAGO_TYPE == "cg") xiaohui modify 2013-09-02 - if (GlobalV::KS_SOLVER == "cg") // xiaohui add 2013-09-02 + if (PARAM.inp.ks_solver == "cg") // xiaohui add 2013-09-02 { if (phm_in != nullptr) { @@ -566,13 +566,13 @@ void diago_PAO_in_pw_k2(const base_device::DEVICE_GPU* ctx, } std::complex* c_wfcatom = nullptr; - if (GlobalV::KS_SOLVER != "bpcg") + if (PARAM.inp.ks_solver != "bpcg") { // store wfcatom on the GPU resmem_cd_op()(gpu_ctx, c_wfcatom, wfcatom.nr * wfcatom.nc); castmem_z2c_h2d_op()(gpu_ctx, cpu_ctx, c_wfcatom, wfcatom.c, wfcatom.nr * wfcatom.nc); } - if (GlobalV::KS_SOLVER == "cg") // xiaohui add 2013-09-02 + if (PARAM.inp.ks_solver == "cg") // xiaohui add 2013-09-02 { // (7) Diago with cg method. if (phm_in != nullptr) @@ -591,7 +591,7 @@ void diago_PAO_in_pw_k2(const base_device::DEVICE_GPU* ctx, // GlobalC::hm.diagH_subspace(ik ,starting_nw, nbands, wfcatom, wfcatom, etatom.data()); } } - else if (GlobalV::KS_SOLVER == "dav" || GlobalV::KS_SOLVER == "dav_subspace") + else if (PARAM.inp.ks_solver == "dav" || PARAM.inp.ks_solver == "dav_subspace") { assert(nbands <= wfcatom.nr); // replace by haozhihan 2022-11-23 @@ -602,11 +602,11 @@ void diago_PAO_in_pw_k2(const base_device::DEVICE_GPU* ctx, &wvf(0, 0), nbasis); } - else if (GlobalV::KS_SOLVER == "bpcg") + else if (PARAM.inp.ks_solver == "bpcg") { castmem_z2c_h2d_op()(gpu_ctx, cpu_ctx, &wvf(0, 0), wfcatom.c, wfcatom.nr * wfcatom.nc); } - if (GlobalV::KS_SOLVER != "bpcg") + if (PARAM.inp.ks_solver != "bpcg") { delmem_cd_op()(gpu_ctx, c_wfcatom); } @@ -669,13 +669,13 @@ void diago_PAO_in_pw_k2(const base_device::DEVICE_GPU* ctx, } std::complex* z_wfcatom = nullptr; - if (GlobalV::KS_SOLVER != "bpcg") + if (PARAM.inp.ks_solver != "bpcg") { // store wfcatom on the GPU resmem_zd_op()(gpu_ctx, z_wfcatom, wfcatom.nr * wfcatom.nc); syncmem_z2z_h2d_op()(gpu_ctx, cpu_ctx, z_wfcatom, wfcatom.c, wfcatom.nr * wfcatom.nc); } - if (GlobalV::KS_SOLVER == "cg") // xiaohui add 2013-09-02 + if (PARAM.inp.ks_solver == "cg") // xiaohui add 2013-09-02 { // (7) Diago with cg method. if (phm_in != nullptr) @@ -694,7 +694,7 @@ void diago_PAO_in_pw_k2(const base_device::DEVICE_GPU* ctx, // GlobalC::hm.diagH_subspace(ik ,starting_nw, nbands, wfcatom, wfcatom, etatom.data()); } } - else if (GlobalV::KS_SOLVER == "dav" || GlobalV::KS_SOLVER == "dav_subspace") + else if (PARAM.inp.ks_solver == "dav" || PARAM.inp.ks_solver == "dav_subspace") { assert(nbands <= wfcatom.nr); // replace by haozhihan 2022-11-23 @@ -705,12 +705,12 @@ void diago_PAO_in_pw_k2(const base_device::DEVICE_GPU* ctx, &wvf(0, 0), nbasis); } - else if (GlobalV::KS_SOLVER == "bpcg") + else if (PARAM.inp.ks_solver == "bpcg") { syncmem_z2z_h2d_op()(gpu_ctx, cpu_ctx, &wvf(0, 0), wfcatom.c, wfcatom.nr * wfcatom.nc); } - if (GlobalV::KS_SOLVER != "bpcg") + if (PARAM.inp.ks_solver != "bpcg") { delmem_zd_op()(gpu_ctx, z_wfcatom); } diff --git a/source/module_io/input_conv.cpp b/source/module_io/input_conv.cpp index d4c1398575..41a9f8542a 100644 --- a/source/module_io/input_conv.cpp +++ b/source/module_io/input_conv.cpp @@ -248,8 +248,6 @@ void Input_Conv::Convert() ModuleSymmetry::Symmetry::symm_flag = std::stoi(PARAM.inp.symmetry); ModuleSymmetry::Symmetry::symm_autoclose = PARAM.inp.symmetry_autoclose; - GlobalV::KS_SOLVER = PARAM.inp.ks_solver; - GlobalV::SEARCH_RADIUS = PARAM.inp.search_radius; //---------------------------------------------------------- // planewave (8/8) @@ -259,7 +257,6 @@ void Input_Conv::Convert() // diagonalization (5/5) //---------------------------------------------------------- - GlobalV::NB2D = PARAM.inp.nb2d; //---------------------------------------------------------- // iteration (1/3) diff --git a/source/module_io/write_HS.hpp b/source/module_io/write_HS.hpp index 7095717ff4..ae4d2d880f 100644 --- a/source/module_io/write_HS.hpp +++ b/source/module_io/write_HS.hpp @@ -138,7 +138,7 @@ void ModuleIO::save_mat(const int istep, if (ic >= 0) { int iic; - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { iic = ir + ic * pv.nrow; } @@ -214,7 +214,7 @@ void ModuleIO::save_mat(const int istep, if (ic >= 0) { int iic; - if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER()) + if (ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver)) { iic = ir + ic * pv.nrow; } diff --git a/source/module_lr/esolver_lrtd_lcao.cpp b/source/module_lr/esolver_lrtd_lcao.cpp index b54a35504c..f5cb2d9b01 100644 --- a/source/module_lr/esolver_lrtd_lcao.cpp +++ b/source/module_lr/esolver_lrtd_lcao.cpp @@ -298,7 +298,8 @@ LR::ESolver_LR::ESolver_LR(const Input_para& inp, UnitCell& ucell) : inpu // search adjacent atoms and init Gint std::cout << "ucell.infoNL.get_rcutmax_Beta(): " << GlobalC::ucell.infoNL.get_rcutmax_Beta() << std::endl; - GlobalV::SEARCH_RADIUS = atom_arrange::set_sr_NL(GlobalV::ofs_running, + double search_radius = -1.0; + search_radius = atom_arrange::set_sr_NL(GlobalV::ofs_running, PARAM.inp.out_level, orb.get_rcutmax_Phi(), GlobalC::ucell.infoNL.get_rcutmax_Beta(), @@ -307,7 +308,7 @@ LR::ESolver_LR::ESolver_LR(const Input_para& inp, UnitCell& ucell) : inpu GlobalV::ofs_running, GlobalC::GridD, this->ucell, - GlobalV::SEARCH_RADIUS, + search_radius, PARAM.inp.test_atom_input); this->set_gint(); this->gint_->gridt = &this->gt_; @@ -557,7 +558,6 @@ template void LR::ESolver_LR::read_ks_wfc() { assert(this->psi_ks != nullptr); - GlobalV::NB2D = 1; this->pelec->ekb.create(this->kv.get_nks(), this->nbands); this->pelec->wg.create(this->kv.get_nks(), this->nbands); diff --git a/source/module_lr/operator_casida/operator_lr_hxc.cpp b/source/module_lr/operator_casida/operator_lr_hxc.cpp index edce5a3d51..f9bc15f2f1 100644 --- a/source/module_lr/operator_casida/operator_lr_hxc.cpp +++ b/source/module_lr/operator_casida/operator_lr_hxc.cpp @@ -71,7 +71,7 @@ namespace LR ct::Tensor(ct::DataTypeToEnum::value, ct::DeviceTypeToEnum::value, { pmat->get_col_size(), pmat->get_row_size() })); for (auto& v : v_hxc_2d) v.zero(); - int nrow = ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER() ? this->pmat->get_row_size() : this->pmat->get_col_size(); + int nrow = ModuleBase::GlobalFunc::IS_COLUMN_MAJOR_KS_SOLVER(PARAM.inp.ks_solver) ? this->pmat->get_row_size() : this->pmat->get_col_size(); for (int ik = 0;ik < nk;++ik) { folding_HR(*this->hR, v_hxc_2d[ik].data(), this->kv.kvec_d[ik], nrow, 1); } // V(R) -> V(k) // LR_Util::print_HR(*this->hR, this->ucell.nat, "4.VR"); // if (this->first_print) diff --git a/source/module_md/test/lj_pot_test.cpp b/source/module_md/test/lj_pot_test.cpp index 52ff825369..8bc03b6762 100644 --- a/source/module_md/test/lj_pot_test.cpp +++ b/source/module_md/test/lj_pot_test.cpp @@ -1,5 +1,6 @@ #include "gtest/gtest.h" #define private public +#include "module_parameter/parameter.h" #include "module_esolver/esolver_lj.h" #include "module_md/md_func.h" #include "setcell.h" @@ -101,7 +102,7 @@ TEST_F(LJ_pot_test, RcutSearchRadius) EXPECT_NEAR(p_esolver->lj_rcut(i, j), 3.0 * ModuleBase::ANGSTROM_AU, doublethreshold); } } - EXPECT_NEAR(GlobalV::SEARCH_RADIUS, 3.0 * ModuleBase::ANGSTROM_AU + 0.01, doublethreshold); + EXPECT_NEAR(p_esolver->search_radius, 3.0 * ModuleBase::ANGSTROM_AU + 0.01, doublethreshold); rcut = {3.0, 4.0, 5.0}; p_esolver->rcut_search_radius(rcut); @@ -109,7 +110,7 @@ TEST_F(LJ_pot_test, RcutSearchRadius) EXPECT_NEAR(p_esolver->lj_rcut(0, 1), 4.0 * ModuleBase::ANGSTROM_AU, doublethreshold); EXPECT_NEAR(p_esolver->lj_rcut(1, 0), 4.0 * ModuleBase::ANGSTROM_AU, doublethreshold); EXPECT_NEAR(p_esolver->lj_rcut(1, 1), 5.0 * ModuleBase::ANGSTROM_AU, doublethreshold); - EXPECT_NEAR(GlobalV::SEARCH_RADIUS, 5.0 * ModuleBase::ANGSTROM_AU + 0.01, doublethreshold); + EXPECT_NEAR(p_esolver->search_radius, 5.0 * ModuleBase::ANGSTROM_AU + 0.01, doublethreshold); } TEST_F(LJ_pot_test, SetC6C12) diff --git a/source/module_md/test/setcell.h b/source/module_md/test/setcell.h index e8ccc7e65b..915382c89e 100644 --- a/source/module_md/test/setcell.h +++ b/source/module_md/test/setcell.h @@ -128,9 +128,10 @@ class Setcell { PARAM.sys.global_out_dir = "./"; PARAM.sys.global_readin_dir = "./"; - GlobalV::SEARCH_RADIUS = 8.5 * ModuleBase::ANGSTROM_AU; + PARAM.input.search_radius = 8.5 * ModuleBase::ANGSTROM_AU; PARAM.input.cal_stress = true; + input.mdp.dump_virial = true; input.mdp.dump_force = true; input.mdp.dump_vel = true; diff --git a/source/module_psi/test/psi_initializer_unit_test.cpp b/source/module_psi/test/psi_initializer_unit_test.cpp index 3d10cb06d6..989d1f045a 100644 --- a/source/module_psi/test/psi_initializer_unit_test.cpp +++ b/source/module_psi/test/psi_initializer_unit_test.cpp @@ -122,7 +122,7 @@ class PsiIntializerUnitTest : public ::testing::Test { PARAM.sys.npol = 1; PARAM.input.calculation = "scf"; PARAM.input.init_wfc = "random"; - GlobalV::KS_SOLVER = "cg"; + PARAM.input.ks_solver = "cg"; PARAM.sys.domag = false; PARAM.sys.domag_z = false; // lattice diff --git a/source/module_ri/RI_2D_Comm.hpp b/source/module_ri/RI_2D_Comm.hpp index a02b50f52c..a168764340 100644 --- a/source/module_ri/RI_2D_Comm.hpp +++ b/source/module_ri/RI_2D_Comm.hpp @@ -1,6 +1,5 @@ //======================= // AUTHOR : Peize Lin -#include "module_parameter/parameter.h" // DATE : 2022-08-17 //======================= @@ -13,7 +12,7 @@ #include "module_base/tool_title.h" #include "module_base/timer.h" #include "module_hamilt_lcao/hamilt_lcaodft/LCAO_domain.h" - +#include "module_parameter/parameter.h" #include #include @@ -84,8 +83,7 @@ auto RI_2D_Comm::split_m2D_ktoR(const K_Vectors & kv, const std::vector Date: Fri, 20 Sep 2024 22:03:18 +0800 Subject: [PATCH 2/8] Refactor: consistent order of hpsi (#5134) --- source/module_hsolver/diago_dav_subspace.cpp | 6 +++--- source/module_hsolver/diago_david.cpp | 4 ++-- source/module_hsolver/diago_david.h | 4 ++-- source/module_hsolver/hsolver_pw.cpp | 10 +++++----- source/module_hsolver/test/diago_david_float_test.cpp | 2 +- source/module_hsolver/test/diago_david_real_test.cpp | 2 +- source/module_hsolver/test/diago_david_test.cpp | 2 +- source/module_lr/hsolver_lrtd.cpp | 4 ++-- 8 files changed, 17 insertions(+), 17 deletions(-) diff --git a/source/module_hsolver/diago_dav_subspace.cpp b/source/module_hsolver/diago_dav_subspace.cpp index 99e0065137..bcb9780271 100644 --- a/source/module_hsolver/diago_dav_subspace.cpp +++ b/source/module_hsolver/diago_dav_subspace.cpp @@ -124,7 +124,7 @@ int Diago_DavSubspace::diag_once(const HPsiFunc& hpsi_func, // compute h*psi_in_iter // NOTE: bands after the first n_band should yield zero - hpsi_func(this->hphi, this->psi_in_iter, this->nbase_x, this->dim, 0, this->nbase_x - 1); + hpsi_func(this->psi_in_iter, this->hphi, this->nbase_x, this->dim, 0, this->nbase_x - 1); // at this stage, notconv = n_band and nbase = 0 // note that nbase of cal_elem is an inout parameter: nbase := nbase + notconv @@ -421,7 +421,7 @@ void Diago_DavSubspace::cal_grad(const HPsiFunc& hpsi_func, } // update hpsi[:, nbase:nbase+notconv] - hpsi_func(&hphi[nbase * this->dim], psi_iter, this->nbase_x, this->dim, nbase, nbase + notconv - 1); + hpsi_func(psi_iter, &hphi[nbase * this->dim], this->nbase_x, this->dim, nbase, nbase + notconv - 1); ModuleBase::timer::tick("Diago_DavSubspace", "cal_grad"); return; @@ -886,7 +886,7 @@ void Diago_DavSubspace::diagH_subspace(T* psi_pointer, // [in] & [out { // do hPsi for all bands - hpsi_func(hphi, psi_pointer, n_band, dmax, 0, nstart - 1); + hpsi_func(psi_pointer, hphi, n_band, dmax, 0, nstart - 1); gemm_op()(ctx, 'C', diff --git a/source/module_hsolver/diago_david.cpp b/source/module_hsolver/diago_david.cpp index 0c76dacb6b..a66b106c26 100644 --- a/source/module_hsolver/diago_david.cpp +++ b/source/module_hsolver/diago_david.cpp @@ -230,7 +230,7 @@ int DiagoDavid::diag_once(const HPsiFunc& hpsi_func, // end of SchmidtOrth and calculate H|psi> // hpsi_info dav_hpsi_in(&basis, psi::Range(true, 0, 0, nband - 1), this->hpsi); // phm_in->ops->hPsi(dav_hpsi_in); - hpsi_func(this->hpsi, basis, nbase_x, dim, 0, nband - 1); + hpsi_func(basis, hpsi, nbase_x, dim, 0, nband - 1); this->cal_elem(dim, nbase, nbase_x, this->notconv, this->hpsi, this->spsi, this->hcc, this->scc); @@ -601,7 +601,7 @@ void DiagoDavid::cal_grad(const HPsiFunc& hpsi_func, // psi::Range(true, 0, nbase, nbase + notconv - 1), // &hpsi[nbase * dim]); // &hp(nbase, 0) // phm_in->ops->hPsi(dav_hpsi_in); - hpsi_func(&hpsi[nbase * dim], basis, nbase_x, dim, nbase, nbase + notconv - 1); + hpsi_func(basis, &hpsi[nbase * dim], nbase_x, dim, nbase, nbase + notconv - 1); delmem_complex_op()(this->ctx, lagrange); delmem_complex_op()(this->ctx, vc_ev_vector); diff --git a/source/module_hsolver/diago_david.h b/source/module_hsolver/diago_david.h index c95d20490c..16bc471e2c 100644 --- a/source/module_hsolver/diago_david.h +++ b/source/module_hsolver/diago_david.h @@ -39,8 +39,8 @@ class DiagoDavid : public DiagH * For eigenvalue problem HX = λX or generalized eigenvalue problem HX = λSX, * this function computes the product of the Hamiltonian matrix H and a blockvector X. * - * @param[out] HX Pointer to output blockvector of type `T*`. - * @param[in] X Pointer to input blockvector of type `T*`. + * @param[out] X Pointer to input blockvector of type `T*`. + * @param[in] HX Pointer to output blockvector of type `T*`. * @param[in] neig Number of eigebpairs required. * @param[in] dim Dimension of matrix. * @param[in] id_start Start index of blockvector. diff --git a/source/module_hsolver/hsolver_pw.cpp b/source/module_hsolver/hsolver_pw.cpp index 09c8814e9f..d20718b7f3 100644 --- a/source/module_hsolver/hsolver_pw.cpp +++ b/source/module_hsolver/hsolver_pw.cpp @@ -434,8 +434,8 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, else if (this->method == "dav_subspace") { auto ngk_pointer = psi.get_ngk_pointer(); - auto hpsi_func = [hm, ngk_pointer](T* hpsi_out, - T* psi_in, + auto hpsi_func = [hm, ngk_pointer](T* psi_in, + T* hpsi_out, const int nband_in, const int nbasis_in, const int band_index1, @@ -492,9 +492,9 @@ void HSolverPW::hamiltSolvePsiK(hamilt::Hamilt* hm, auto ngk_pointer = psi.get_ngk_pointer(); /// wrap hpsi into lambda function, Matrix \times blockvector - /// hpsi(HX, X, nband, dim, band_index1, band_index2) - auto hpsi_func = [hm, ngk_pointer](T* hpsi_out, - T* psi_in, + /// hpsi(X, HX, nband, dim, band_index1, band_index2) + auto hpsi_func = [hm, ngk_pointer](T* psi_in, + T* hpsi_out, const int nband_in, const int nbasis_in, const int band_index1, diff --git a/source/module_hsolver/test/diago_david_float_test.cpp b/source/module_hsolver/test/diago_david_float_test.cpp index 89ea88be0f..20d50d4a60 100644 --- a/source/module_hsolver/test/diago_david_float_test.cpp +++ b/source/module_hsolver/test/diago_david_float_test.cpp @@ -107,7 +107,7 @@ class DiagoDavPrepare #endif - auto hpsi_func = [phm](std::complex* hpsi_out,std::complex* psi_in, + auto hpsi_func = [phm](std::complex* psi_in,std::complex* hpsi_out, const int nband_in, const int nbasis_in, const int band_index1, const int band_index2) { diff --git a/source/module_hsolver/test/diago_david_real_test.cpp b/source/module_hsolver/test/diago_david_real_test.cpp index e2510101e6..a5be15ba70 100644 --- a/source/module_hsolver/test/diago_david_real_test.cpp +++ b/source/module_hsolver/test/diago_david_real_test.cpp @@ -106,7 +106,7 @@ class DiagoDavPrepare #endif - auto hpsi_func = [phm](double* hpsi_out,double* psi_in, + auto hpsi_func = [phm](double* psi_in,double* hpsi_out, const int nband_in, const int nbasis_in, const int band_index1, const int band_index2) { diff --git a/source/module_hsolver/test/diago_david_test.cpp b/source/module_hsolver/test/diago_david_test.cpp index c9dfcf2cca..79b099c6f4 100644 --- a/source/module_hsolver/test/diago_david_test.cpp +++ b/source/module_hsolver/test/diago_david_test.cpp @@ -109,7 +109,7 @@ class DiagoDavPrepare #endif - auto hpsi_func = [phm](std::complex* hpsi_out,std::complex* psi_in, + auto hpsi_func = [phm](std::complex* psi_in,std::complex* hpsi_out, const int nband_in, const int nbasis_in, const int band_index1, const int band_index2) { diff --git a/source/module_lr/hsolver_lrtd.cpp b/source/module_lr/hsolver_lrtd.cpp index f3ea744327..638ae6e807 100644 --- a/source/module_lr/hsolver_lrtd.cpp +++ b/source/module_lr/hsolver_lrtd.cpp @@ -82,8 +82,8 @@ namespace LR // do diag and add davidson iteration counts up to avg_iter auto hpsi_func = [pHamilt]( - T* hpsi_out, T* psi_in, + T* hpsi_out, const int nband_in, const int nbasis_in, const int band_index1, @@ -119,8 +119,8 @@ namespace LR comm_info); std::function hpsi_func = [pHamilt]( - T* hpsi_out, T* psi_in, + T* hpsi_out, const int nband_in, const int nbasis_in, const int band_index1, From 75f1e6dd4a8087dd32aa27b803a1b6caebda133f Mon Sep 17 00:00:00 2001 From: LUNASEA <33978601+maki49@users.noreply.github.com> Date: Fri, 20 Sep 2024 22:03:52 +0800 Subject: [PATCH 3/8] Fix read/write HexxR at nspin=4 (#5142) --- source/module_io/restart_exx_csr.hpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/source/module_io/restart_exx_csr.hpp b/source/module_io/restart_exx_csr.hpp index 441ed24002..e18a9d5a8d 100644 --- a/source/module_io/restart_exx_csr.hpp +++ b/source/module_io/restart_exx_csr.hpp @@ -40,9 +40,10 @@ namespace ModuleIO const SparseMatrix& matrix = csr.getMatrix(iR); for (auto& ijv : matrix.getElements()) { - const int& i = ijv.first.first; - const int& j = ijv.first.second; - Hexxs.at(is).at(ucell.iwt2iat[i]).at({ ucell.iwt2iat[j], { R[0], R[1], R[2] } })(ucell.iwt2iw[i], ucell.iwt2iw[j]) = ijv.second; + const int& npol = ucell.get_npol(); + const int& i = ijv.first.first * npol; + const int& j = ijv.first.second * npol; + Hexxs.at(is).at(ucell.iwt2iat[i]).at({ ucell.iwt2iat[j], { R[0], R[1], R[2] } })(ucell.iwt2iw[i] / npol, ucell.iwt2iw[j] / npol) = ijv.second; } } } @@ -64,8 +65,8 @@ namespace ModuleIO int iat2 = a2R_data.first.first; int nw1 = ucell.atoms[ucell.iat2it[iat1]].nw; int nw2 = ucell.atoms[ucell.iat2it[iat2]].nw; - int start1 = ucell.atoms[ucell.iat2it[iat1]].stapos_wf + ucell.iat2ia[iat1] * nw1; - int start2 = ucell.atoms[ucell.iat2it[iat2]].stapos_wf + ucell.iat2ia[iat2] * nw2; + int start1 = ucell.atoms[ucell.iat2it[iat1]].stapos_wf / ucell.get_npol() + ucell.iat2ia[iat1] * nw1; + int start2 = ucell.atoms[ucell.iat2it[iat2]].stapos_wf / ucell.get_npol() + ucell.iat2ia[iat2] * nw2; const TC& R = a2R_data.first.second; auto& matrix = a2R_data.second; @@ -73,8 +74,8 @@ namespace ModuleIO for (int i = 0;i < nw1;++i) { for (int j = 0;j < nw2;++j) { target[dR][start1 + i][start2 + j] = ((std::abs(matrix(i, j)) > sparse_threshold) ? matrix(i, j) : static_cast(0)); -} -} + } + } } } return target; From 80b2c751d274142bb608de4ea01c0d049fb9804c Mon Sep 17 00:00:00 2001 From: kirk0830 <67682086+kirk0830@users.noreply.github.com> Date: Fri, 20 Sep 2024 22:05:18 +0800 Subject: [PATCH 4/8] add ipool != 0 return (#5145) Co-authored-by: Qianrui Liu <76200646+Qianruipku@users.noreply.github.com> --- source/module_esolver/esolver_fp.cpp | 1 + source/module_io/rhog_io.cpp | 4 ++++ source/module_io/rhog_io.h | 1 + 3 files changed, 6 insertions(+) diff --git a/source/module_esolver/esolver_fp.cpp b/source/module_esolver/esolver_fp.cpp index 9eea82454c..a12d050040 100644 --- a/source/module_esolver/esolver_fp.cpp +++ b/source/module_esolver/esolver_fp.cpp @@ -204,6 +204,7 @@ void ESolver_FP::after_scf(const int istep) PARAM.inp.nspin, GlobalC::ucell.GT, rhog_tot, + GlobalV::MY_POOL, GlobalV::RANK_IN_POOL, GlobalV::NPROC_IN_POOL); } diff --git a/source/module_io/rhog_io.cpp b/source/module_io/rhog_io.cpp index fba566cc02..f01b78fdcb 100644 --- a/source/module_io/rhog_io.cpp +++ b/source/module_io/rhog_io.cpp @@ -204,11 +204,15 @@ bool ModuleIO::write_rhog(const std::string& fchg, const int nspin, // GlobalV const ModuleBase::Matrix3& GT, // from UnitCell, useful for calculating the miller std::complex** rhog, + const int ipool, const int irank, const int nrank) { ModuleBase::TITLE("ModuleIO", "write_rhog"); ModuleBase::timer::tick("ModuleIO", "write_rhog"); + if (ipool != 0) { return true; } + // only one pool writes the rhog, because rhog in all pools are identical. + // for large-scale data, it is not wise to collect all distributed components to the // master process and then write the data to the file. Instead, we can write the data // processer by processer. diff --git a/source/module_io/rhog_io.h b/source/module_io/rhog_io.h index 25c1b71202..f6f5b11899 100644 --- a/source/module_io/rhog_io.h +++ b/source/module_io/rhog_io.h @@ -51,6 +51,7 @@ bool write_rhog(const std::string& fchg, const int nspin, // GlobalV const ModuleBase::Matrix3& GT, // from UnitCell, useful for calculating the miller std::complex** rhog, + const int ipool, const int irank, const int nrank); From 90e49213be3c08e092f02efb5fb172d11d0492ce Mon Sep 17 00:00:00 2001 From: Qianrui Liu <76200646+Qianruipku@users.noreply.github.com> Date: Sat, 21 Sep 2024 15:42:20 +0800 Subject: [PATCH 5/8] Refactor: add CalAtomsInfo to modify parameter (#5132) * Refactor: add CalAtomsInfo to modify parameter prepare to remove globalv * fix compile * fix UTs --- source/module_cell/cal_atoms_info.h | 75 +++++++ .../module_neighbor/test/prepare_unitcell.h | 4 +- source/module_cell/test/prepare_unitcell.h | 4 +- .../test/support/mock_unitcell.cpp | 2 +- .../module_cell/test/unitcell_test_readpp.cpp | 170 +++++++++++++- source/module_cell/unitcell.cpp | 211 +++++++++++++----- source/module_cell/unitcell.h | 23 +- source/module_elecstate/elecstate.cpp | 105 --------- source/module_elecstate/elecstate.h | 3 - .../module_dm/test/prepare_unitcell.h | 4 +- .../test/elecstate_base_test.cpp | 141 ------------ source/module_esolver/esolver_ks.cpp | 6 +- source/module_esolver/esolver_of.cpp | 1 - .../module_deltaspin/test/prepare_unitcell.h | 4 +- .../module_hcontainer/test/prepare_unitcell.h | 4 +- .../test/hsolver_supplementary_mock.h | 5 - source/module_io/test/prepare_unitcell.h | 4 +- source/module_io/test/write_orb_info_test.cpp | 1 + .../module_io/test_serial/prepare_unitcell.h | 4 +- source/module_lr/esolver_lrtd_lcao.cpp | 1 - source/module_parameter/parameter.h | 9 +- 21 files changed, 435 insertions(+), 346 deletions(-) create mode 100644 source/module_cell/cal_atoms_info.h mode change 100644 => 100755 source/module_cell/unitcell.cpp diff --git a/source/module_cell/cal_atoms_info.h b/source/module_cell/cal_atoms_info.h new file mode 100644 index 0000000000..92789eb1ee --- /dev/null +++ b/source/module_cell/cal_atoms_info.h @@ -0,0 +1,75 @@ +#ifndef CAL_ATOMS_INFO_H +#define CAL_ATOMS_INFO_H +#include "module_parameter/parameter.h" +#include "unitcell.h" +class CalAtomsInfo +{ + public: + CalAtomsInfo(){}; + ~CalAtomsInfo(){}; + + /** + * @brief Calculate the atom information from pseudopotential to set Parameter + * + * @param atoms [in] Atom pointer + * @param ntype [in] number of atom types + * @param para [out] Parameter object + */ + void cal_atoms_info(const Atom* atoms, const int& ntype, Parameter& para) + { + // calculate initial total magnetization when NSPIN=2 + if (para.inp.nspin == 2 && !para.globalv.two_fermi) + { + for (int it = 0; it < ntype; ++it) + { + for (int ia = 0; ia < atoms[it].na; ++ia) + { + GlobalV::nupdown += atoms[it].mag[ia]; + } + } + GlobalV::ofs_running << " The readin total magnetization is " << GlobalV::nupdown << std::endl; + } + + if (!para.inp.use_paw) + { + // decide whether to be USPP + for (int it = 0; it < ntype; ++it) + { + if (atoms[it].ncpp.tvanp) + { + GlobalV::use_uspp = true; + } + } + + // calculate the total number of local basis + GlobalV::NLOCAL = 0; + for (int it = 0; it < ntype; ++it) + { + const int nlocal_it = atoms[it].nw * atoms[it].na; + if (para.inp.nspin != 4) + { + GlobalV::NLOCAL += nlocal_it; + } + else + { + GlobalV::NLOCAL += nlocal_it * 2; // zhengdy-soc + } + } + } + + // calculate the total number of electrons + cal_nelec(atoms, ntype, GlobalV::nelec); + + // autoset and check GlobalV::NBANDS + std::vector nelec_spin(2, 0.0); + if (para.inp.nspin == 2) + { + nelec_spin[0] = (GlobalV::nelec + GlobalV::nupdown) / 2.0; + nelec_spin[1] = (GlobalV::nelec - GlobalV::nupdown) / 2.0; + } + cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS); + + return; + } +}; +#endif \ No newline at end of file diff --git a/source/module_cell/module_neighbor/test/prepare_unitcell.h b/source/module_cell/module_neighbor/test/prepare_unitcell.h index 5f217aadf2..a92f15842f 100644 --- a/source/module_cell/module_neighbor/test/prepare_unitcell.h +++ b/source/module_cell/module_neighbor/test/prepare_unitcell.h @@ -252,8 +252,8 @@ UcellTestPrepare::UcellTestPrepare(std::string latname_in, coor_type(coor_type_in), coordinates(coordinates_in) { - mbl = {0}; - velocity = {0}; + mbl = std::valarray(0.0, coordinates_in.size()); + velocity = std::valarray(0.0, coordinates_in.size()); } UcellTestPrepare::UcellTestPrepare(std::string latname_in, diff --git a/source/module_cell/test/prepare_unitcell.h b/source/module_cell/test/prepare_unitcell.h index a02eafdbb5..f6692ae8f7 100644 --- a/source/module_cell/test/prepare_unitcell.h +++ b/source/module_cell/test/prepare_unitcell.h @@ -251,8 +251,8 @@ UcellTestPrepare::UcellTestPrepare(std::string latname_in, coor_type(coor_type_in), coordinates(coordinates_in) { - mbl = {0}; - velocity = {0}; + mbl = std::valarray(0.0, coordinates_in.size()); + velocity = std::valarray(0.0, coordinates_in.size()); } UcellTestPrepare::UcellTestPrepare(std::string latname_in, diff --git a/source/module_cell/test/support/mock_unitcell.cpp b/source/module_cell/test/support/mock_unitcell.cpp index 23b4df3909..b91430987a 100644 --- a/source/module_cell/test/support/mock_unitcell.cpp +++ b/source/module_cell/test/support/mock_unitcell.cpp @@ -124,5 +124,5 @@ void UnitCell::setup(const std::string& latname_in, const int& lmaxmax_in, const bool& init_vel_in, const std::string& fixed_axes_in) {} -void UnitCell::cal_nelec(double& nelec) {} +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 diff --git a/source/module_cell/test/unitcell_test_readpp.cpp b/source/module_cell/test/unitcell_test_readpp.cpp index b2b01c0559..f643db848b 100644 --- a/source/module_cell/test/unitcell_test_readpp.cpp +++ b/source/module_cell/test/unitcell_test_readpp.cpp @@ -89,6 +89,8 @@ Magnetism::~Magnetism() { delete[] this->start_magnetization; } * possible of an element * - CalNelec: UnitCell::cal_nelec * - calculate the total number of valence electrons from psp files + * - CalNbands: elecstate::ElecState::cal_nbands() + * - calculate the number of bands */ // mock function @@ -114,9 +116,16 @@ class UcellTest : public ::testing::Test { pp_dir = "./support/"; PARAM.input.pseudo_rcut = 15.0; PARAM.input.dft_functional = "default"; + PARAM.input.esolver_type = "ksdft"; PARAM.input.test_pseudo_cell = true; PARAM.input.nspin = 1; PARAM.input.basis_type = "pw"; + GlobalV::nelec = 10.0; + GlobalV::nupdown = 0.0; + PARAM.sys.two_fermi = false; + GlobalV::NBANDS = 6; + GlobalV::NLOCAL = 6; + PARAM.input.lspinorb = false; } void TearDown() { ofs.close(); } }; @@ -256,6 +265,7 @@ TEST_F(UcellTest, CalNwfc1) { ucell->read_cell_pseudopots(pp_dir, ofs); EXPECT_FALSE(ucell->atoms[0].ncpp.has_so); EXPECT_FALSE(ucell->atoms[1].ncpp.has_so); + GlobalV::NLOCAL = 3 * 9; ucell->cal_nwfc(ofs); EXPECT_EQ(ucell->atoms[0].iw2l[8], 2); EXPECT_EQ(ucell->atoms[0].iw2n[8], 0); @@ -282,7 +292,6 @@ TEST_F(UcellTest, CalNwfc1) { EXPECT_EQ(ucell->atoms[0].nw, 9); EXPECT_EQ(ucell->atoms[1].nw, 9); EXPECT_EQ(ucell->nwmax, 9); - EXPECT_EQ(GlobalV::NLOCAL, 3 * 9); // check itia2iat EXPECT_EQ(ucell->itia2iat.getSize(), 4); EXPECT_EQ(ucell->itia2iat(0, 0), 0); @@ -322,8 +331,8 @@ TEST_F(UcellTest, CalNwfc2) { ucell->read_cell_pseudopots(pp_dir, ofs); EXPECT_FALSE(ucell->atoms[0].ncpp.has_so); EXPECT_FALSE(ucell->atoms[1].ncpp.has_so); - ucell->cal_nwfc(ofs); - EXPECT_EQ(GlobalV::NLOCAL, 3 * 9 * 2); + GlobalV::NLOCAL = 3 * 9 * 2; + EXPECT_NO_THROW(ucell->cal_nwfc(ofs)); } TEST_F(UcellDeathTest, CheckStructure) { @@ -396,10 +405,163 @@ TEST_F(UcellTest, CalNelec) { EXPECT_EQ(1, ucell->atoms[0].na); EXPECT_EQ(2, ucell->atoms[1].na); double nelec = 0; - ucell->cal_nelec(nelec); + cal_nelec(ucell->atoms, ucell->ntype, nelec); EXPECT_DOUBLE_EQ(6, nelec); } +TEST_F(UcellTest, CalNbands) +{ + std::vector nelec_spin(2, 5.0); + cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS); + EXPECT_EQ(GlobalV::NBANDS, 6); +} + +TEST_F(UcellTest, CalNbandsFractionElec) +{ + GlobalV::nelec = 9.5; + std::vector nelec_spin(2, 5.0); + cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS); + EXPECT_EQ(GlobalV::NBANDS, 6); +} + +TEST_F(UcellTest, CalNbandsSOC) +{ + PARAM.input.lspinorb = true; + GlobalV::NBANDS = 0; + std::vector nelec_spin(2, 5.0); + cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS); + EXPECT_EQ(GlobalV::NBANDS, 20); +} + +TEST_F(UcellTest, CalNbandsSDFT) +{ + PARAM.input.esolver_type = "sdft"; + std::vector nelec_spin(2, 5.0); + EXPECT_NO_THROW(cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS)); +} + +TEST_F(UcellTest, CalNbandsLCAO) +{ + PARAM.input.basis_type = "lcao"; + std::vector nelec_spin(2, 5.0); + EXPECT_NO_THROW(cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS)); +} + +TEST_F(UcellTest, CalNbandsLCAOINPW) +{ + PARAM.input.basis_type = "lcao_in_pw"; + GlobalV::NLOCAL = GlobalV::NBANDS - 1; + std::vector nelec_spin(2, 5.0); + testing::internal::CaptureStdout(); + EXPECT_EXIT(cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS), ::testing::ExitedWithCode(0), ""); + output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("NLOCAL < NBANDS")); +} + +TEST_F(UcellTest, CalNbandsWarning1) +{ + GlobalV::NBANDS = GlobalV::nelec / 2 - 1; + std::vector nelec_spin(2, 5.0); + testing::internal::CaptureStdout(); + EXPECT_EXIT(cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS), ::testing::ExitedWithCode(0), ""); + output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("Too few bands!")); +} + +TEST_F(UcellTest, CalNbandsWarning2) +{ + PARAM.input.nspin = 2; + GlobalV::nupdown = 4.0; + std::vector nelec_spin(2); + nelec_spin[0] = (GlobalV::nelec + GlobalV::nupdown) / 2.0; + nelec_spin[1] = (GlobalV::nelec - GlobalV::nupdown) / 2.0; + testing::internal::CaptureStdout(); + EXPECT_EXIT(cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS), ::testing::ExitedWithCode(0), ""); + output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("Too few spin up bands!")); +} + +TEST_F(UcellTest, CalNbandsWarning3) +{ + PARAM.input.nspin = 2; + GlobalV::nupdown = -4.0; + std::vector nelec_spin(2); + nelec_spin[0] = (GlobalV::nelec + GlobalV::nupdown) / 2.0; + nelec_spin[1] = (GlobalV::nelec - GlobalV::nupdown) / 2.0; + testing::internal::CaptureStdout(); + EXPECT_EXIT(cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS), ::testing::ExitedWithCode(0), ""); + output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("Too few spin down bands!")); +} + +TEST_F(UcellTest, CalNbandsSpin1) +{ + PARAM.input.nspin = 1; + GlobalV::NBANDS = 0; + std::vector nelec_spin(2, 5.0); + cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS); + EXPECT_EQ(GlobalV::NBANDS, 15); +} + +TEST_F(UcellTest, CalNbandsSpin1LCAO) +{ + PARAM.input.nspin = 1; + GlobalV::NBANDS = 0; + PARAM.input.basis_type = "lcao"; + std::vector nelec_spin(2, 5.0); + cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS); + EXPECT_EQ(GlobalV::NBANDS, 6); +} + +TEST_F(UcellTest, CalNbandsSpin4) +{ + PARAM.input.nspin = 4; + GlobalV::NBANDS = 0; + std::vector nelec_spin(2, 5.0); + cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS); + EXPECT_EQ(GlobalV::NBANDS, 30); +} + +TEST_F(UcellTest, CalNbandsSpin4LCAO) +{ + PARAM.input.nspin = 4; + GlobalV::NBANDS = 0; + PARAM.input.basis_type = "lcao"; + std::vector nelec_spin(2, 5.0); + cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS); + EXPECT_EQ(GlobalV::NBANDS, 6); +} + +TEST_F(UcellTest, CalNbandsSpin2) +{ + PARAM.input.nspin = 2; + GlobalV::NBANDS = 0; + std::vector nelec_spin(2, 5.0); + cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS); + EXPECT_EQ(GlobalV::NBANDS, 16); +} + +TEST_F(UcellTest, CalNbandsSpin2LCAO) +{ + PARAM.input.nspin = 2; + GlobalV::NBANDS = 0; + PARAM.input.basis_type = "lcao"; + std::vector nelec_spin(2, 5.0); + cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS); + EXPECT_EQ(GlobalV::NBANDS, 6); +} + +TEST_F(UcellTest, CalNbandsGaussWarning) +{ + GlobalV::NBANDS = 5; + std::vector nelec_spin(2, 5.0); + PARAM.input.smearing_method = "gaussian"; + testing::internal::CaptureStdout(); + EXPECT_EXIT(cal_nbands(GlobalV::nelec, GlobalV::NLOCAL, nelec_spin, GlobalV::NBANDS), ::testing::ExitedWithCode(0), ""); + output = testing::internal::GetCapturedStdout(); + EXPECT_THAT(output, testing::HasSubstr("for smearing, num. of bands > num. of occupied bands")); +} + #ifdef __MPI #include "mpi.h" int main(int argc, char** argv) { diff --git a/source/module_cell/unitcell.cpp b/source/module_cell/unitcell.cpp old mode 100644 new mode 100755 index 08c68aa259..4b90c06d9c --- a/source/module_cell/unitcell.cpp +++ b/source/module_cell/unitcell.cpp @@ -8,6 +8,7 @@ #include "module_base/global_variable.h" #include "unitcell.h" #include "module_parameter/parameter.h" +#include "cal_atoms_info.h" #ifdef __LCAO #include "../module_basis/module_ao/ORB_read.h" // to use 'ORB' -- mohan 2021-01-30 @@ -594,17 +595,6 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) { this->bcast_unitcell(); #endif - // after read STRU, calculate initial total magnetization when NSPIN=2 - if (PARAM.inp.nspin == 2 && !PARAM.globalv.two_fermi) { - for (int it = 0; it < this->ntype; it++) { - for (int ia = 0; ia < this->atoms[it].na; ia++) { - GlobalV::nupdown += this->atoms[it].mag[ia]; - } - } - GlobalV::ofs_running << " The readin total magnetization is " - << GlobalV::nupdown << std::endl; - } - //======================================================== // Calculate unit cell volume // the reason to calculate volume here is @@ -831,14 +821,15 @@ void UnitCell::read_pseudo(std::ofstream& ofs) { ModuleBase::WARNING_QUIT("setup_cell", "All DFT functional must consistent."); } - if (atoms[it].ncpp.tvanp) { - GlobalV::use_uspp = true; - } } // setup the total number of PAOs cal_natomwfc(ofs); + // Calculate the information of atoms from the pseudopotential to set PARAM + CalAtomsInfo ca; + ca.cal_atoms_info(this->atoms, this->ntype, PARAM); + // setup GlobalV::NLOCAL cal_nwfc(ofs); @@ -950,14 +941,14 @@ void UnitCell::cal_nwfc(std::ofstream& log) { //=========================== // (3) set nwfc and stapos_wf //=========================== - GlobalV::NLOCAL = 0; + int nlocal_tmp = 0; for (int it = 0; it < ntype; it++) { - atoms[it].stapos_wf = GlobalV::NLOCAL; + atoms[it].stapos_wf = nlocal_tmp; const int nlocal_it = atoms[it].nw * atoms[it].na; if (PARAM.inp.nspin != 4) { - GlobalV::NLOCAL += nlocal_it; + nlocal_tmp += nlocal_it; } else { - GlobalV::NLOCAL += nlocal_it * 2; // zhengdy-soc + nlocal_tmp += nlocal_it * 2; // zhengdy-soc } // for tests @@ -968,17 +959,18 @@ void UnitCell::cal_nwfc(std::ofstream& log) { // OUT(GlobalV::ofs_running,"NLOCAL",GlobalV::NLOCAL); log << " " << std::setw(40) << "NLOCAL" - << " = " << GlobalV::NLOCAL << std::endl; + << " = " << nlocal_tmp << std::endl; //======================================================== // (4) set index for itia2iat, itiaiw2iwt //======================================================== // mohan add 2010-09-26 - assert(GlobalV::NLOCAL > 0); + assert(nlocal_tmp > 0); + assert(nlocal_tmp == GlobalV::NLOCAL); delete[] iwt2iat; delete[] iwt2iw; - this->iwt2iat = new int[GlobalV::NLOCAL]; - this->iwt2iw = new int[GlobalV::NLOCAL]; + this->iwt2iat = new int[nlocal_tmp]; + this->iwt2iw = new int[nlocal_tmp]; this->itia2iat.create(ntype, namax); // this->itiaiw2iwt.create(ntype, namax, nwmax*PARAM.globalv.npol); @@ -1582,65 +1574,160 @@ void UnitCell::remake_cell() { } } -void UnitCell::cal_nelec(double& nelec) { +void cal_nelec(const Atom* atoms, const int& ntype, double& nelec) +{ ModuleBase::TITLE("UnitCell", "cal_nelec"); GlobalV::ofs_running << "\n SETUP THE ELECTRONS NUMBER" << std::endl; - if (nelec == 0) { - if (PARAM.inp.use_paw) { + if (nelec == 0) + { + if (PARAM.inp.use_paw) + { #ifdef USE_PAW - for (int it = 0; it < this->ntype; it++) { + for (int it = 0; it < ntype; it++) + { std::stringstream ss1, ss2; - ss1 << " electron number of element " - << GlobalC::paw_cell.get_zat(it) << std::endl; - const int nelec_it - = GlobalC::paw_cell.get_val(it) * this->atoms[it].na; + ss1 << " electron number of element " << GlobalC::paw_cell.get_zat(it) << std::endl; + const int nelec_it = GlobalC::paw_cell.get_val(it) * atoms[it].na; nelec += nelec_it; - ss2 << "total electron number of element " - << GlobalC::paw_cell.get_zat(it); - - ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, - ss1.str(), - GlobalC::paw_cell.get_val(it)); - ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, - ss2.str(), - nelec_it); + ss2 << "total electron number of element " << GlobalC::paw_cell.get_zat(it); + + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, ss1.str(), GlobalC::paw_cell.get_val(it)); + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, ss2.str(), nelec_it); } #endif - } else { - for (int it = 0; it < this->ntype; it++) { + } + else + { + for (int it = 0; it < ntype; it++) + { std::stringstream ss1, ss2; - ss1 << "electron number of element " << this->atoms[it].label; - const double nelec_it - = this->atoms[it].ncpp.zv * this->atoms[it].na; + ss1 << "electron number of element " << atoms[it].label; + const double nelec_it = atoms[it].ncpp.zv * atoms[it].na; nelec += nelec_it; - ss2 << "total electron number of element " - << this->atoms[it].label; - - ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, - ss1.str(), - this->atoms[it].ncpp.zv); - ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, - ss2.str(), - nelec_it); + ss2 << "total electron number of element " << atoms[it].label; + + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, ss1.str(), atoms[it].ncpp.zv); + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, ss2.str(), nelec_it); } - ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, - "AUTOSET number of electrons: ", - nelec); + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "AUTOSET number of electrons: ", nelec); } } - if (PARAM.inp.nelec_delta != 0) { - ModuleBase::GlobalFunc::OUT( - GlobalV::ofs_running, - "nelec_delta is NOT zero, please make sure you know what you are " - "doing! nelec_delta: ", - PARAM.inp.nelec_delta); + if (PARAM.inp.nelec_delta != 0) + { + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, + "nelec_delta is NOT zero, please make sure you know what you are " + "doing! nelec_delta: ", + PARAM.inp.nelec_delta); nelec += PARAM.inp.nelec_delta; ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nelec now: ", nelec); } return; } +void cal_nbands(const int& nelec, const int& nlocal, const std::vector& nelec_spin, int& nbands) +{ + if (PARAM.inp.esolver_type == "sdft") // qianrui 2021-2-20 + { + return; + } + //======================================= + // calculate number of bands (setup.f90) + //======================================= + double occupied_bands = static_cast(nelec / ModuleBase::DEGSPIN); + if (PARAM.inp.lspinorb == 1) { + occupied_bands = static_cast(nelec); + } + + if ((occupied_bands - std::floor(occupied_bands)) > 0.0) + { + occupied_bands = std::floor(occupied_bands) + 1.0; // mohan fix 2012-04-16 + } + + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "occupied bands", occupied_bands); + + if (nbands == 0) + { + if (PARAM.inp.nspin == 1) + { + const int nbands1 = static_cast(occupied_bands) + 10; + const int nbands2 = static_cast(1.2 * occupied_bands) + 1; + nbands = std::max(nbands1, nbands2); + if (PARAM.inp.basis_type != "pw") { + nbands = std::min(nbands, nlocal); + } + } + else if (PARAM.inp.nspin == 4) + { + const int nbands3 = nelec + 20; + const int nbands4 = static_cast(1.2 * nelec) + 1; + nbands = std::max(nbands3, nbands4); + if (PARAM.inp.basis_type != "pw") { + nbands = std::min(nbands, nlocal); + } + } + else if (PARAM.inp.nspin == 2) + { + const double max_occ = std::max(nelec_spin[0], nelec_spin[1]); + const int nbands3 = static_cast(max_occ) + 11; + const int nbands4 = static_cast(1.2 * max_occ) + 1; + nbands = std::max(nbands3, nbands4); + if (PARAM.inp.basis_type != "pw") { + nbands = std::min(nbands, nlocal); + } + } + ModuleBase::GlobalFunc::AUTO_SET("NBANDS", nbands); + } + // else if ( PARAM.inp.calculation=="scf" || PARAM.inp.calculation=="md" || PARAM.inp.calculation=="relax") //pengfei + // 2014-10-13 + else + { + if (nbands < occupied_bands) { + ModuleBase::WARNING_QUIT("unitcell", "Too few bands!"); + } + if (PARAM.inp.nspin == 2) + { + if (nbands < nelec_spin[0]) + { + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nelec_up", nelec_spin[0]); + ModuleBase::WARNING_QUIT("ElecState::cal_nbands", "Too few spin up bands!"); + } + if (nbands < nelec_spin[1]) + { + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nelec_down", nelec_spin[1]); + ModuleBase::WARNING_QUIT("ElecState::cal_nbands", "Too few spin down bands!"); + } + } + } + + // mohan add 2010-09-04 + // std::cout << "nbands(this-> = " < num. of occupied bands"); + } + } + + // mohan update 2021-02-19 + // mohan add 2011-01-5 + if (PARAM.inp.basis_type == "lcao" || PARAM.inp.basis_type == "lcao_in_pw") + { + if (nbands > nlocal) + { + ModuleBase::WARNING_QUIT("ElecState::cal_nbandsc", "NLOCAL < NBANDS"); + } + else + { + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "NLOCAL", nlocal); + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "NBANDS", nbands); + } + } + + ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "NBANDS", nbands); +} + void UnitCell::compare_atom_labels(std::string label1, std::string label2) { if (label1 != label2) //'!( "Ag" == "Ag" || "47" == "47" || "Silver" == Silver" )' diff --git a/source/module_cell/unitcell.h b/source/module_cell/unitcell.h index 9a8e54ce3d..4657d97b62 100644 --- a/source/module_cell/unitcell.h +++ b/source/module_cell/unitcell.h @@ -307,10 +307,6 @@ class UnitCell { const bool& init_vel_in, const std::string& fixed_axes_in); - /// @brief calculate the total number of electrons in system - /// (GlobalV::nelec) - void cal_nelec(double& nelec); - /// @brief check consistency between two atom labels from STRU and pseudo or /// orb file void compare_atom_labels(std::string label1, std::string label2); @@ -334,4 +330,23 @@ class UnitCell { std::vector> get_lnchiCounts() const; }; +/** + * @brief calculate the total number of electrons in system + * + * @param atoms [in] atom pointer + * @param ntype [in] number of atom types + * @param nelec [out] total number of electrons + */ +void cal_nelec(const Atom* atoms, const int& ntype, double& nelec); + +/** + * @brief Calculate the number of bands. + * + * @param nelec [in] total number of electrons + * @param nlocal [in] total number of local basis + * @param nelec_spin [in] number of electrons for each spin + * @param nbands [out] number of bands + */ +void cal_nbands(const int& nelec, const int& nlocal, const std::vector& nelec_spin, int& nbands); + #endif // unitcell class diff --git a/source/module_elecstate/elecstate.cpp b/source/module_elecstate/elecstate.cpp index 720d920ed5..6d77e815a6 100644 --- a/source/module_elecstate/elecstate.cpp +++ b/source/module_elecstate/elecstate.cpp @@ -248,116 +248,11 @@ void ElecState::init_ks(Charge* chg_in, // pointer for class Charge this->bigpw = bigpw_in; // init nelec_spin with nelec and nupdown this->init_nelec_spin(); - // autoset and check GlobalV::NBANDS, nelec_spin is used when NSPIN==2 - this->cal_nbands(); // initialize ekb and wg this->ekb.create(nk_in, GlobalV::NBANDS); this->wg.create(nk_in, GlobalV::NBANDS); } -void ElecState::cal_nbands() -{ - if (PARAM.inp.esolver_type == "sdft") // qianrui 2021-2-20 - { - return; - } - //======================================= - // calculate number of bands (setup.f90) - //======================================= - double occupied_bands = static_cast(GlobalV::nelec / ModuleBase::DEGSPIN); - if (PARAM.inp.lspinorb == 1) { - occupied_bands = static_cast(GlobalV::nelec); - } - - if ((occupied_bands - std::floor(occupied_bands)) > 0.0) - { - occupied_bands = std::floor(occupied_bands) + 1.0; // mohan fix 2012-04-16 - } - - ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "occupied bands", occupied_bands); - - if (GlobalV::NBANDS == 0) - { - if (PARAM.inp.nspin == 1) - { - const int nbands1 = static_cast(occupied_bands) + 10; - const int nbands2 = static_cast(1.2 * occupied_bands) + 1; - GlobalV::NBANDS = std::max(nbands1, nbands2); - if (PARAM.inp.basis_type != "pw") { - GlobalV::NBANDS = std::min(GlobalV::NBANDS, GlobalV::NLOCAL); - } - } - else if (PARAM.inp.nspin == 4) - { - const int nbands3 = GlobalV::nelec + 20; - const int nbands4 = static_cast(1.2 * GlobalV::nelec) + 1; - GlobalV::NBANDS = std::max(nbands3, nbands4); - if (PARAM.inp.basis_type != "pw") { - GlobalV::NBANDS = std::min(GlobalV::NBANDS, GlobalV::NLOCAL); - } - } - else if (PARAM.inp.nspin == 2) - { - const double max_occ = std::max(this->nelec_spin[0], this->nelec_spin[1]); - const int nbands3 = static_cast(max_occ) + 11; - const int nbands4 = static_cast(1.2 * max_occ) + 1; - GlobalV::NBANDS = std::max(nbands3, nbands4); - if (PARAM.inp.basis_type != "pw") { - GlobalV::NBANDS = std::min(GlobalV::NBANDS, GlobalV::NLOCAL); - } - } - ModuleBase::GlobalFunc::AUTO_SET("NBANDS", GlobalV::NBANDS); - } - // else if ( PARAM.inp.calculation=="scf" || PARAM.inp.calculation=="md" || PARAM.inp.calculation=="relax") //pengfei - // 2014-10-13 - else - { - if (GlobalV::NBANDS < occupied_bands) { - ModuleBase::WARNING_QUIT("unitcell", "Too few bands!"); - } - if (PARAM.inp.nspin == 2) - { - if (GlobalV::NBANDS < this->nelec_spin[0]) - { - ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nelec_up", this->nelec_spin[0]); - ModuleBase::WARNING_QUIT("ElecState::cal_nbands", "Too few spin up bands!"); - } - if (GlobalV::NBANDS < this->nelec_spin[1]) - { - ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "nelec_down", this->nelec_spin[1]); - ModuleBase::WARNING_QUIT("ElecState::cal_nbands", "Too few spin down bands!"); - } - } - } - - // mohan add 2010-09-04 - // std::cout << "nbands(this-> = " < num. of occupied bands"); - } - } - - // mohan update 2021-02-19 - // mohan add 2011-01-5 - if (PARAM.inp.basis_type == "lcao" || PARAM.inp.basis_type == "lcao_in_pw") - { - if (GlobalV::NBANDS > GlobalV::NLOCAL) - { - ModuleBase::WARNING_QUIT("ElecState::cal_nbandsc", "NLOCAL < NBANDS"); - } - else - { - ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "NLOCAL", GlobalV::NLOCAL); - ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "NBANDS", GlobalV::NBANDS); - } - } - - ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running, "NBANDS", GlobalV::NBANDS); -} - void set_is_occupied(std::vector& is_occupied, elecstate::ElecState* pes, const int i_scf, diff --git a/source/module_elecstate/elecstate.h b/source/module_elecstate/elecstate.h index 023b72b03a..a9fc79ff7c 100644 --- a/source/module_elecstate/elecstate.h +++ b/source/module_elecstate/elecstate.h @@ -83,9 +83,6 @@ class ElecState //for NSPIN=4, it will record total number, magnetization for x, y, z direction std::vector nelec_spin; - //calculate nbands and - void cal_nbands(); - virtual void print_psi(const psi::Psi& psi_in, const int istep = -1) { return; diff --git a/source/module_elecstate/module_dm/test/prepare_unitcell.h b/source/module_elecstate/module_dm/test/prepare_unitcell.h index 6b295fc0f2..01f37ddd9c 100644 --- a/source/module_elecstate/module_dm/test/prepare_unitcell.h +++ b/source/module_elecstate/module_dm/test/prepare_unitcell.h @@ -308,8 +308,8 @@ UcellTestPrepare::UcellTestPrepare(std::string latname_in, coor_type(coor_type_in), coordinates(coordinates_in) { - mbl = {0}; - velocity = {0}; + mbl = std::valarray(0.0, coordinates_in.size()); + velocity = std::valarray(0.0, coordinates_in.size()); } UcellTestPrepare::UcellTestPrepare(std::string latname_in, diff --git a/source/module_elecstate/test/elecstate_base_test.cpp b/source/module_elecstate/test/elecstate_base_test.cpp index de015315ba..54a5839f4c 100644 --- a/source/module_elecstate/test/elecstate_base_test.cpp +++ b/source/module_elecstate/test/elecstate_base_test.cpp @@ -98,8 +98,6 @@ void Charge::check_rho() * - determine the number of electrons for spin up and down * - Constructor: elecstate::ElecState(charge, rhopw, bigpw) * - constructor ElecState using existing charge, rhopw, bigpw - * - CalNbands: elecstate::ElecState::cal_nbands() - * - calculate the number of bands * - InitKS: elecstate::ElecState::init_ks() * - initialize the elecstate for KS-DFT * - GetRho: elecstate::ElecState::getRho() @@ -183,145 +181,6 @@ TEST_F(ElecStateTest, Constructor) delete charge; } -TEST_F(ElecStateTest, CalNbands) -{ - elecstate->cal_nbands(); - EXPECT_EQ(GlobalV::NBANDS, 6); -} - -TEST_F(ElecStateTest, CalNbandsFractionElec) -{ - GlobalV::nelec = 9.5; - elecstate->cal_nbands(); - EXPECT_EQ(GlobalV::NBANDS, 6); -} - -TEST_F(ElecStateTest, CalNbandsSOC) -{ - PARAM.input.lspinorb = true; - GlobalV::NBANDS = 0; - elecstate->cal_nbands(); - EXPECT_EQ(GlobalV::NBANDS, 20); -} - -TEST_F(ElecStateTest, CalNbandsSDFT) -{ - PARAM.input.esolver_type = "sdft"; - EXPECT_NO_THROW(elecstate->cal_nbands()); -} - -TEST_F(ElecStateTest, CalNbandsLCAO) -{ - PARAM.input.basis_type = "lcao"; - EXPECT_NO_THROW(elecstate->cal_nbands()); -} - -TEST_F(ElecStateDeathTest, CalNbandsLCAOINPW) -{ - PARAM.input.basis_type = "lcao_in_pw"; - GlobalV::NLOCAL = GlobalV::NBANDS - 1; - testing::internal::CaptureStdout(); - EXPECT_EXIT(elecstate->cal_nbands(), ::testing::ExitedWithCode(0), ""); - output = testing::internal::GetCapturedStdout(); - EXPECT_THAT(output, testing::HasSubstr("NLOCAL < NBANDS")); -} - -TEST_F(ElecStateDeathTest, CalNbandsWarning1) -{ - GlobalV::NBANDS = GlobalV::nelec / 2 - 1; - testing::internal::CaptureStdout(); - EXPECT_EXIT(elecstate->cal_nbands(), ::testing::ExitedWithCode(0), ""); - output = testing::internal::GetCapturedStdout(); - EXPECT_THAT(output, testing::HasSubstr("Too few bands!")); -} - -TEST_F(ElecStateDeathTest, CalNbandsWarning2) -{ - PARAM.input.nspin = 2; - GlobalV::nupdown = 4.0; - elecstate->init_nelec_spin(); - testing::internal::CaptureStdout(); - EXPECT_EXIT(elecstate->cal_nbands(), ::testing::ExitedWithCode(0), ""); - output = testing::internal::GetCapturedStdout(); - EXPECT_THAT(output, testing::HasSubstr("Too few spin up bands!")); -} - -TEST_F(ElecStateDeathTest, CalNbandsWarning3) -{ - PARAM.input.nspin = 2; - GlobalV::nupdown = -4.0; - elecstate->init_nelec_spin(); - testing::internal::CaptureStdout(); - EXPECT_EXIT(elecstate->cal_nbands(), ::testing::ExitedWithCode(0), ""); - output = testing::internal::GetCapturedStdout(); - EXPECT_THAT(output, testing::HasSubstr("Too few spin down bands!")); -} - -TEST_F(ElecStateTest, CalNbandsSpin1) -{ - PARAM.input.nspin = 1; - GlobalV::NBANDS = 0; - elecstate->cal_nbands(); - EXPECT_EQ(GlobalV::NBANDS, 15); -} - -TEST_F(ElecStateTest, CalNbandsSpin1LCAO) -{ - PARAM.input.nspin = 1; - GlobalV::NBANDS = 0; - PARAM.input.basis_type = "lcao"; - elecstate->cal_nbands(); - EXPECT_EQ(GlobalV::NBANDS, 6); -} - -TEST_F(ElecStateTest, CalNbandsSpin4) -{ - PARAM.input.nspin = 4; - GlobalV::NBANDS = 0; - elecstate->cal_nbands(); - EXPECT_EQ(GlobalV::NBANDS, 30); -} - -TEST_F(ElecStateTest, CalNbandsSpin4LCAO) -{ - PARAM.input.nspin = 4; - GlobalV::NBANDS = 0; - PARAM.input.basis_type = "lcao"; - elecstate->cal_nbands(); - EXPECT_EQ(GlobalV::NBANDS, 6); -} - -TEST_F(ElecStateTest, CalNbandsSpin2) -{ - PARAM.input.nspin = 2; - GlobalV::NBANDS = 0; - elecstate->init_nelec_spin(); - elecstate->cal_nbands(); - EXPECT_EQ(GlobalV::NBANDS, 16); -} - -TEST_F(ElecStateTest, CalNbandsSpin2LCAO) -{ - PARAM.input.nspin = 2; - GlobalV::NBANDS = 0; - PARAM.input.basis_type = "lcao"; - elecstate->init_nelec_spin(); - elecstate->cal_nbands(); - EXPECT_EQ(GlobalV::NBANDS, 6); -} - -TEST_F(ElecStateDeathTest, CalNbandsGaussWarning) -{ - Occupy::use_gaussian_broadening = true; - EXPECT_TRUE(Occupy::gauss()); - GlobalV::NBANDS = 5; - testing::internal::CaptureStdout(); - EXPECT_EXIT(elecstate->cal_nbands(), ::testing::ExitedWithCode(0), ""); - output = testing::internal::GetCapturedStdout(); - EXPECT_THAT(output, testing::HasSubstr("for smearing, num. of bands > num. of occupied bands")); - Occupy::use_gaussian_broadening = false; -} - TEST_F(ElecStateTest, InitKS) { Charge* charge = new Charge; diff --git a/source/module_esolver/esolver_ks.cpp b/source/module_esolver/esolver_ks.cpp index 0bc5c6ad0d..240d34dc4e 100644 --- a/source/module_esolver/esolver_ks.cpp +++ b/source/module_esolver/esolver_ks.cpp @@ -11,6 +11,7 @@ #include "module_io/print_info.h" #include "module_io/write_istate_info.h" #include "module_parameter/parameter.h" +#include "module_cell/cal_atoms_info.h" #include //--------------Temporary---------------- @@ -187,13 +188,12 @@ void ESolver_KS::before_all_runners(const Input_para& inp, UnitCell& } delete[] atom_coord; delete[] atom_type; + CalAtomsInfo ca; + ca.cal_atoms_info(ucell.atoms, ucell.ntype, PARAM); } #endif /// End PAW - //! 3) calculate the electron number - ucell.cal_nelec(GlobalV::nelec); - //! 4) it has been established that // xc_func is same for all elements, therefore // only the first one if used diff --git a/source/module_esolver/esolver_of.cpp b/source/module_esolver/esolver_of.cpp index edd50a6962..49edcb90b2 100644 --- a/source/module_esolver/esolver_of.cpp +++ b/source/module_esolver/esolver_of.cpp @@ -70,7 +70,6 @@ void ESolver_OF::before_all_runners(const Input_para& inp, UnitCell& ucell) this->max_iter_ = inp.scf_nmax; this->dV_ = ucell.omega / this->pw_rho->nxyz; - ucell.cal_nelec(GlobalV::nelec); ModuleBase::GlobalFunc::DONE(GlobalV::ofs_running, "SETUP UNITCELL"); XC_Functional::set_xc_type(ucell.atoms[0].ncpp.xc_func); diff --git a/source/module_hamilt_lcao/module_deltaspin/test/prepare_unitcell.h b/source/module_hamilt_lcao/module_deltaspin/test/prepare_unitcell.h index aa8221a8fe..ec95f74087 100644 --- a/source/module_hamilt_lcao/module_deltaspin/test/prepare_unitcell.h +++ b/source/module_hamilt_lcao/module_deltaspin/test/prepare_unitcell.h @@ -232,8 +232,8 @@ UcellTestPrepare::UcellTestPrepare(std::string latname_in, coor_type(coor_type_in), coordinates(coordinates_in) { - mbl = {0}; - velocity = {0}; + mbl = std::valarray(0.0, coordinates_in.size()); + velocity = std::valarray(0.0, coordinates_in.size()); } UcellTestPrepare::UcellTestPrepare(const UcellTestPrepare &utp): diff --git a/source/module_hamilt_lcao/module_hcontainer/test/prepare_unitcell.h b/source/module_hamilt_lcao/module_hcontainer/test/prepare_unitcell.h index 0fdce8f66b..70d9eebaea 100644 --- a/source/module_hamilt_lcao/module_hcontainer/test/prepare_unitcell.h +++ b/source/module_hamilt_lcao/module_hcontainer/test/prepare_unitcell.h @@ -308,8 +308,8 @@ UcellTestPrepare::UcellTestPrepare(std::string latname_in, coor_type(coor_type_in), coordinates(coordinates_in) { - mbl = {0}; - velocity = {0}; + mbl = std::valarray(0.0, coordinates_in.size()); + velocity = std::valarray(0.0, coordinates_in.size()); } UcellTestPrepare::UcellTestPrepare(std::string latname_in, diff --git a/source/module_hsolver/test/hsolver_supplementary_mock.h b/source/module_hsolver/test/hsolver_supplementary_mock.h index 8cee0698af..95c69e555e 100644 --- a/source/module_hsolver/test/hsolver_supplementary_mock.h +++ b/source/module_hsolver/test/hsolver_supplementary_mock.h @@ -57,11 +57,6 @@ void ElecState::init_ks(Charge* chg_in, // pointer for class Charge return; } -void ElecState::cal_nbands() -{ - return; -} - } // namespace elecstate diff --git a/source/module_io/test/prepare_unitcell.h b/source/module_io/test/prepare_unitcell.h index 5a831eea06..d9deaf0ff3 100644 --- a/source/module_io/test/prepare_unitcell.h +++ b/source/module_io/test/prepare_unitcell.h @@ -252,8 +252,8 @@ UcellTestPrepare::UcellTestPrepare(std::string latname_in, coor_type(coor_type_in), coordinates(coordinates_in) { - mbl = {0}; - velocity = {0}; + mbl = std::valarray(0.0, coordinates_in.size()); + velocity = std::valarray(0.0, coordinates_in.size()); } UcellTestPrepare::UcellTestPrepare(std::string latname_in, diff --git a/source/module_io/test/write_orb_info_test.cpp b/source/module_io/test/write_orb_info_test.cpp index 7da84e439f..b9c6f66483 100644 --- a/source/module_io/test/write_orb_info_test.cpp +++ b/source/module_io/test/write_orb_info_test.cpp @@ -49,6 +49,7 @@ TEST(OrbInfo,WriteOrbInfo) PARAM.input.nspin = 1; PARAM.input.basis_type = "pw"; PARAM.input.dft_functional = "default"; + GlobalV::NLOCAL = 18; ucell->read_cell_pseudopots(pp_dir,ofs); ucell->cal_nwfc(ofs); ModuleIO::write_orb_info(ucell); diff --git a/source/module_io/test_serial/prepare_unitcell.h b/source/module_io/test_serial/prepare_unitcell.h index 5f217aadf2..a92f15842f 100644 --- a/source/module_io/test_serial/prepare_unitcell.h +++ b/source/module_io/test_serial/prepare_unitcell.h @@ -252,8 +252,8 @@ UcellTestPrepare::UcellTestPrepare(std::string latname_in, coor_type(coor_type_in), coordinates(coordinates_in) { - mbl = {0}; - velocity = {0}; + mbl = std::valarray(0.0, coordinates_in.size()); + velocity = std::valarray(0.0, coordinates_in.size()); } UcellTestPrepare::UcellTestPrepare(std::string latname_in, diff --git a/source/module_lr/esolver_lrtd_lcao.cpp b/source/module_lr/esolver_lrtd_lcao.cpp index f5cb2d9b01..f8ae2037e0 100644 --- a/source/module_lr/esolver_lrtd_lcao.cpp +++ b/source/module_lr/esolver_lrtd_lcao.cpp @@ -236,7 +236,6 @@ LR::ESolver_LR::ESolver_LR(const Input_para& inp, UnitCell& ucell) : inpu this->pelec = new elecstate::ElecStateLCAO(); // necessary steps in ESolver_KS::before_all_runners : symmetry and k-points - ucell.cal_nelec(GlobalV::nelec); if (ModuleSymmetry::Symmetry::symm_flag == 1) { GlobalC::ucell.symm.analy_sys(ucell.lat, ucell.st, ucell.atoms, GlobalV::ofs_running); diff --git a/source/module_parameter/parameter.h b/source/module_parameter/parameter.h index 38b544f2ed..87f1efe267 100644 --- a/source/module_parameter/parameter.h +++ b/source/module_parameter/parameter.h @@ -6,6 +6,7 @@ namespace ModuleIO { class ReadInput; } +class CalAtomInfo; class Parameter { public: @@ -32,8 +33,12 @@ class Parameter void set_start_time(const std::time_t& start_time); private: - // Only ReadInput can modify the value of Parameter. - friend class ModuleIO::ReadInput; + // Only ReadInput and CalAtomInfo can modify the value of Parameter. + // Do not add extra friend class here!!! + friend class ModuleIO::ReadInput; // ReadInput read INPUT file and give the value to Parameter + friend class CalAtomsInfo; // CalAtomInfo calculate the atom information from pseudopotential and give the value to + // Parameter + // INPUT parameters Input_para input; // System parameters From 7c8a4da18fa005bc8a15afadba09f6a5691c9568 Mon Sep 17 00:00:00 2001 From: Yu Liu <77716030+YuLiu98@users.noreply.github.com> Date: Sat, 21 Sep 2024 15:44:50 +0800 Subject: [PATCH 6/8] Refactor: remove useless warning for dp (#5153) * Refactor: move output in relax to module_io * Refactor: update get_conv_elec() * Refactor: remove delete_tmp_file() * Tests: add unittests --- source/module_base/global_file.cpp | 26 ------------- source/module_base/global_file.h | 8 +--- source/module_base/test/global_file_test.cpp | 22 ----------- source/module_esolver/esolver.h | 2 +- source/module_esolver/esolver_fp.cpp | 9 +++++ source/module_esolver/esolver_fp.h | 3 ++ source/module_esolver/esolver_ks.cpp | 10 ----- source/module_esolver/esolver_ks.h | 7 +--- source/module_io/output_log.cpp | 22 +++++++++++ source/module_io/output_log.h | 6 +++ source/module_io/test/outputlog_test.cpp | 27 +++++++++++++ source/module_md/run_md.cpp | 2 - source/module_relax/relax_driver.cpp | 40 ++------------------ 13 files changed, 74 insertions(+), 110 deletions(-) diff --git a/source/module_base/global_file.cpp b/source/module_base/global_file.cpp index c2cb6ad0f1..b912996f4e 100644 --- a/source/module_base/global_file.cpp +++ b/source/module_base/global_file.cpp @@ -274,30 +274,4 @@ void ModuleBase::Global_File::close_all_log(const int rank, const bool out_alllo #endif return; } - -void ModuleBase::Global_File::delete_tmp_files() -{ - if (GlobalV::MY_RANK == 0) - { - for (int is = 0; is < PARAM.inp.nspin; ++is) - { - std::string tmp_chg_1 = PARAM.globalv.global_out_dir + "NOW_SPIN" + std::to_string(is + 1) + "_CHG.cube"; - std::string tmp_chg_2 = PARAM.globalv.global_out_dir + "OLD1_SPIN" + std::to_string(is + 1) + "_CHG.cube"; - std::string tmp_chg_3 = PARAM.globalv.global_out_dir + "OLD2_SPIN" + std::to_string(is + 1) + "_CHG.cube"; - - if (access(tmp_chg_1.c_str(), 0) == 0) - { - std::remove(tmp_chg_1.c_str()); - } - if (access(tmp_chg_2.c_str(), 0) == 0) - { - std::remove(tmp_chg_2.c_str()); - } - if (access(tmp_chg_3.c_str(), 0) == 0) - { - std::remove(tmp_chg_3.c_str()); - } - } - } -} } diff --git a/source/module_base/global_file.h b/source/module_base/global_file.h index 1db9a0a4b6..9b6895388c 100644 --- a/source/module_base/global_file.h +++ b/source/module_base/global_file.h @@ -30,13 +30,7 @@ namespace Global_File void make_dir_atom(const std::string &label); void open_log ( std::ofstream &ofs, const std::string &fn, const std::string &calculation, const bool &restart); void close_log( std::ofstream &ofs, const std::string &fn); - void close_all_log(const int rank, const bool out_alllog = false,const std::string &calculation = "md"); - - /** - * @brief delete tmperary files - * - */ - void delete_tmp_files(); + void close_all_log(const int rank, const bool out_alllog = false, const std::string& calculation = "md"); } } #endif diff --git a/source/module_base/test/global_file_test.cpp b/source/module_base/test/global_file_test.cpp index 9d06349bbc..adc7f30fc7 100644 --- a/source/module_base/test/global_file_test.cpp +++ b/source/module_base/test/global_file_test.cpp @@ -157,26 +157,4 @@ TEST_F(GlobalFile,closealllog) } remove("running.log"); remove("warning.log"); -} - -TEST_F(GlobalFile, DeleteTmpFiles) -{ - - std::string tmp_chg_1 = PARAM.sys.global_out_dir + "NOW_SPIN1_CHG.cube"; - std::string tmp_chg_2 = PARAM.sys.global_out_dir + "OLD1_SPIN1_CHG.cube"; - std::string tmp_chg_3 = PARAM.sys.global_out_dir + "OLD2_SPIN1_CHG.cube"; - std::ofstream ofs1(tmp_chg_1.c_str()); - std::ofstream ofs2(tmp_chg_2.c_str()); - std::ofstream ofs3(tmp_chg_3.c_str()); - ofs1.close(); - ofs2.close(); - ofs3.close(); - EXPECT_TRUE(access(tmp_chg_1.c_str(), 0) == 0); - EXPECT_TRUE(access(tmp_chg_2.c_str(), 0) == 0); - EXPECT_TRUE(access(tmp_chg_3.c_str(), 0) == 0); - - ModuleBase::Global_File::delete_tmp_files(); - EXPECT_TRUE(access(tmp_chg_1.c_str(), 0) == -1); - EXPECT_TRUE(access(tmp_chg_2.c_str(), 0) == -1); - EXPECT_TRUE(access(tmp_chg_3.c_str(), 0) == -1); } \ No newline at end of file diff --git a/source/module_esolver/esolver.h b/source/module_esolver/esolver.h index 1092be261e..2ffeb1372f 100644 --- a/source/module_esolver/esolver.h +++ b/source/module_esolver/esolver.h @@ -61,7 +61,7 @@ class ESolver // get conv_elec used in current scf virtual bool get_conv_elec() { - return false; + return true; } std::string classname; }; diff --git a/source/module_esolver/esolver_fp.cpp b/source/module_esolver/esolver_fp.cpp index a12d050040..ae66c34f9e 100644 --- a/source/module_esolver/esolver_fp.cpp +++ b/source/module_esolver/esolver_fp.cpp @@ -117,6 +117,15 @@ void ESolver_FP::before_all_runners(const Input_para& inp, UnitCell& cell) return; } +//------------------------------------------------------------------------------ +//! the 12th function of ESolver_KS: get_conv_elec +//! tqzhao add 2024-05-15 +//------------------------------------------------------------------------------ +bool ESolver_FP::get_conv_elec() +{ + return this->conv_elec; +} + //! Something to do after SCF iterations when SCF is converged or comes to the max iter step. void ESolver_FP::after_scf(const int istep) { diff --git a/source/module_esolver/esolver_fp.h b/source/module_esolver/esolver_fp.h index 4d5fcf5c41..4e60fa0aa8 100644 --- a/source/module_esolver/esolver_fp.h +++ b/source/module_esolver/esolver_fp.h @@ -44,6 +44,9 @@ namespace ModuleESolver //! Initialize of the first-principels energy solver virtual void before_all_runners(const Input_para& inp, UnitCell& cell) override; + // get conv_elec used in current scf + virtual bool get_conv_elec() override; + virtual void init_after_vc(const Input_para& inp, UnitCell& cell); // liuyu add 2023-03-09 //! Electronic states diff --git a/source/module_esolver/esolver_ks.cpp b/source/module_esolver/esolver_ks.cpp index 240d34dc4e..634c5c51ea 100644 --- a/source/module_esolver/esolver_ks.cpp +++ b/source/module_esolver/esolver_ks.cpp @@ -759,16 +759,6 @@ int ESolver_KS::get_maxniter() return this->maxniter; } -//------------------------------------------------------------------------------ -//! the 12th function of ESolver_KS: get_conv_elec -//! tqzhao add 2024-05-15 -//------------------------------------------------------------------------------ -template -bool ESolver_KS::get_conv_elec() -{ - return this->conv_elec; -} - //------------------------------------------------------------------------------ //! the 16th-20th functions of ESolver_KS //! mohan add 2024-05-12 diff --git a/source/module_esolver/esolver_ks.h b/source/module_esolver/esolver_ks.h index 583be4910d..c541b5e074 100644 --- a/source/module_esolver/esolver_ks.h +++ b/source/module_esolver/esolver_ks.h @@ -56,11 +56,8 @@ class ESolver_KS : public ESolver_FP // get maxniter used in current scf virtual int get_maxniter() override; - // get conv_elec used in current scf - virtual bool get_conv_elec() override; - - protected: - //! Something to do before SCF iterations. + protected: + //! Something to do before SCF iterations. virtual void before_scf(const int istep) {}; //! Something to do before hamilt2density function in each iter loop. diff --git a/source/module_io/output_log.cpp b/source/module_io/output_log.cpp index a940422ea4..9913f6ec89 100644 --- a/source/module_io/output_log.cpp +++ b/source/module_io/output_log.cpp @@ -21,6 +21,28 @@ void output_convergence_after_scf(bool& convergence, double& energy, std::ofstre } } +void output_after_relax(bool conv_ion, bool conv_elec, std::ofstream& ofs_running) +{ + if (conv_ion && !conv_elec) + { + std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; + std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; + std::cout << " Relaxation is converged, but the SCF is unconverged! The results are unreliable. " << std::endl; + std::cout << " It is suggested to increase the maximum SCF step and/or perform the relaxation again." + << std::endl; + std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; + std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; + ofs_running << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; + ofs_running << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; + ofs_running << "\n Relaxation is converged, but the SCF is unconverged! The results are unreliable.. " + << std::endl; + ofs_running << "\n It is suggested to increase the maximum SCF step and/or perform the relaxation again. " + << std::endl; + ofs_running << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; + ofs_running << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; + } +} + void output_efermi(bool& convergence, double& efermi, std::ofstream& ofs_running) { if (convergence && PARAM.inp.out_level != "m") diff --git a/source/module_io/output_log.h b/source/module_io/output_log.h index 76bb6d293d..5fb0c2227d 100644 --- a/source/module_io/output_log.h +++ b/source/module_io/output_log.h @@ -16,6 +16,12 @@ namespace ModuleIO /// @param ofs_running the output stream void output_convergence_after_scf(bool& convergence, double& energy, std::ofstream& ofs_running = GlobalV::ofs_running); +/// @brief output after relaxation +/// @param conv_ion if is convergence for ions +/// @param conv_elec if is convergence for electrons +/// @param ofs_running the output stream +void output_after_relax(bool conv_ion, bool conv_elec, std::ofstream& ofs_running = GlobalV::ofs_running); + /// @brief output the fermi energy /// @param convergence if is convergence /// @param efermi diff --git a/source/module_io/test/outputlog_test.cpp b/source/module_io/test/outputlog_test.cpp index 1fd15a30ea..2e7a085f81 100644 --- a/source/module_io/test/outputlog_test.cpp +++ b/source/module_io/test/outputlog_test.cpp @@ -86,6 +86,33 @@ TEST(OutputEfermiTest, TestConvergence) { std::remove("test_output_efermi.txt"); } +// Test the output_efermi function +TEST(OutputAfterRelaxTest, TestConvergence) +{ + bool conv_ion = true; + bool conv_elec = false; + std::ofstream ofs_running("test_output_after_relax.txt"); + ModuleIO::output_after_relax(conv_ion, conv_elec, ofs_running); + ofs_running.close(); + + std::ifstream ifs_running("test_output_after_relax.txt"); + std::stringstream ss; + ss << ifs_running.rdbuf(); + std::string file_content = ss.str(); + ifs_running.close(); + + std::string expected_content + = "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n Relaxation is converged, but the SCF is unconverged! The " + "results are unreliable.. \n\n It is suggested to increase the maximum SCF step and/or perform the " + "relaxation again. " + "\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n\n%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n"; + + EXPECT_EQ(file_content, expected_content); + std::remove("test_output_after_relax.txt"); +} + TEST(OutputEfermiTest, TestNotConvergence) { bool convergence = false; double efermi = 1.0; diff --git a/source/module_md/run_md.cpp b/source/module_md/run_md.cpp index 35928a9fc3..34f1f4152a 100644 --- a/source/module_md/run_md.cpp +++ b/source/module_md/run_md.cpp @@ -122,8 +122,6 @@ void md_line(UnitCell& unit_in, ModuleESolver::ESolver* p_esolver, const Paramet mdrun->step_++; } - ModuleBase::Global_File::delete_tmp_files(); - delete mdrun; ModuleBase::timer::tick("Run_MD", "md_line"); return; diff --git a/source/module_relax/relax_driver.cpp b/source/module_relax/relax_driver.cpp index c549812f63..43bc2f51b7 100644 --- a/source/module_relax/relax_driver.cpp +++ b/source/module_relax/relax_driver.cpp @@ -2,12 +2,13 @@ #include "module_base/global_file.h" #include "module_hamilt_pw/hamilt_pwdft/global.h" // use chr. +#include "module_io/cif_io.h" #include "module_io/json_output/output_info.h" +#include "module_io/output_log.h" #include "module_io/print_info.h" #include "module_io/read_exit_file.h" #include "module_io/write_wfc_r.h" #include "module_parameter/parameter.h" -#include "module_io/cif_io.h" void Relax_Driver::relax_driver(ModuleESolver::ESolver* p_esolver) { ModuleBase::TITLE("Ions", "opt_ions"); @@ -128,37 +129,7 @@ void Relax_Driver::relax_driver(ModuleESolver::ESolver* p_esolver) "data_?"); } - if (p_esolver && stop && p_esolver->get_maxniter() == p_esolver->get_niter() - && !(p_esolver->get_conv_elec())) - { - std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - << std::endl; - std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - << std::endl; - std::cout << " Relaxation is converged, but the SCF is unconverged! The results are unreliable. " - << std::endl; - std::cout - << " It is suggested to increase the maximum SCF step and/or perform the relaxation again." - << std::endl; - std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - << std::endl; - std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - << std::endl; - GlobalV::ofs_running - << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; - GlobalV::ofs_running - << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; - GlobalV::ofs_running - << "\n Relaxation is converged, but the SCF is unconverged! The results are unreliable.. " - << std::endl; - GlobalV::ofs_running - << "\n It is suggested to increase the maximum SCF step and/or perform the relaxation again. " - << std::endl; - GlobalV::ofs_running - << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; - GlobalV::ofs_running - << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << std::endl; - } + ModuleIO::output_after_relax(stop, p_esolver->get_conv_elec(), GlobalV::ofs_running); } #ifdef __RAPIDJSON @@ -188,11 +159,6 @@ void Relax_Driver::relax_driver(ModuleESolver::ESolver* p_esolver) std::cout << " ION DYNAMICS FINISHED :)" << std::endl; } - if (PARAM.inp.calculation == "relax" || PARAM.inp.calculation == "cell-relax") - { - ModuleBase::Global_File::delete_tmp_files(); - } - ModuleBase::timer::tick("Ions", "opt_ions"); return; } From 81f25416e19d392b1586f31b4d8b0072c5a16450 Mon Sep 17 00:00:00 2001 From: LUNASEA <33978601+maki49@users.noreply.github.com> Date: Mon, 23 Sep 2024 20:43:32 +0800 Subject: [PATCH 7/8] Fix the undefined behavior in sph_bessel_recursive (#5158) * fix the undefined behavior in sph_bessel_recursive * [pre-commit.ci lite] apply automatic fixes --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> --- source/module_base/sph_bessel_recursive.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/module_base/sph_bessel_recursive.h b/source/module_base/sph_bessel_recursive.h index a74b0987e3..e11af5127d 100644 --- a/source/module_base/sph_bessel_recursive.h +++ b/source/module_base/sph_bessel_recursive.h @@ -7,7 +7,7 @@ #define SPH_BESSEL_RECURSIVE_H #include -#include"stddef.h" +#include namespace ModuleBase { @@ -32,7 +32,7 @@ class Sph_Bessel_Recursive::D1 private: std::vector> jlx; // jlx[l][x] - double dx; + double dx = 0.0; bool finish_set_dx = false; void cal_jlx_0( const int l_size ); @@ -56,7 +56,7 @@ class Sph_Bessel_Recursive::D2 private: std::vector>> jlx; // jlx[l][x1][x2] - double dx; + double dx = 0.0; bool finish_set_dx = false; void cal_jlx_0( const int l_size, const size_t ix1_size, const size_t ix2_size ); From acb792df75aec68957611591a72c88d8126b48f9 Mon Sep 17 00:00:00 2001 From: James Misaka Date: Mon, 23 Sep 2024 20:46:43 +0800 Subject: [PATCH 8/8] Toolchain 202403 (#5144) * code quality * downgrade 5.0.5 to 4.1.6 in default * update version tag * update openblas version * rapidjson setting update * Update get_openblas_arch.sh * update cmake scripts code * update elpa version * Update README.md --- toolchain/README.md | 10 +++++----- toolchain/build_abacus_gnu.sh | 4 +++- toolchain/build_abacus_intel-mpich.sh | 4 +++- toolchain/build_abacus_intel.sh | 4 +++- toolchain/scripts/VERSION | 2 +- toolchain/scripts/get_openblas_arch.sh | 4 ++-- toolchain/scripts/stage0/install_cmake.sh | 9 +++++---- toolchain/scripts/stage1/install_openmpi.sh | 9 +++++---- toolchain/scripts/stage2/install_openblas.sh | 4 ++-- toolchain/scripts/stage3/install_elpa.sh | 14 ++++++++------ toolchain/scripts/stage4/install_rapidjson.sh | 2 ++ 11 files changed, 39 insertions(+), 27 deletions(-) diff --git a/toolchain/README.md b/toolchain/README.md index 9f13f6614e..386efc8313 100644 --- a/toolchain/README.md +++ b/toolchain/README.md @@ -1,6 +1,6 @@ # The ABACUS Toolchain -Version 2024.2 +Version 2024.3 ## Author @@ -108,13 +108,13 @@ The needed dependencies version default: - `cmake` 3.30.0 - `gcc` 13.2.0 (which will always NOT be installed, But use system) -- `OpenMPI` 5.0.5 +- `OpenMPI` 4.1.6 (5.0.5 can be used but have some problem in OpenMP parallel computation in ELPA) - `MPICH` 4.2.2 -- `OpenBLAS` 0.3.27 (Intel toolchain need `get_vars.sh` tool from it) -- `ScaLAPACK` 2.2.1 +- `OpenBLAS` 0.3.28 (Intel toolchain need `get_vars.sh` tool from it) +- `ScaLAPACK` 2.2.1 (a developing version) - `FFTW` 3.3.10 - `LibXC` 6.2.2 -- `ELPA` 2024.03.001 +- `ELPA` 2024.05.001 - `CEREAL` 1.3.2 - `RapidJSON` 1.1.0 And Intel-oneAPI need user or server manager to manually install from Intel. diff --git a/toolchain/build_abacus_gnu.sh b/toolchain/build_abacus_gnu.sh index a5b88316fe..c797392585 100755 --- a/toolchain/build_abacus_gnu.sh +++ b/toolchain/build_abacus_gnu.sh @@ -28,6 +28,7 @@ ELPA=$INSTALL_DIR/elpa-2024.03.001/cpu FFTW3=$INSTALL_DIR/fftw-3.3.10 CEREAL=$INSTALL_DIR/cereal-1.3.2/include/cereal LIBXC=$INSTALL_DIR/libxc-6.2.2 +RAPIDJSON=$INSTALL_DIR/rapidjson-1.1.0/ # LIBRI=$INSTALL_DIR/LibRI-0.1.1 # LIBCOMM=$INSTALL_DIR/LibComm-0.1.0 # LIBTORCH=$INSTALL_DIR/libtorch-2.1.2/share/cmake/Torch @@ -47,7 +48,8 @@ cmake -B $BUILD_DIR -DCMAKE_INSTALL_PREFIX=$PREFIX \ -DENABLE_LIBXC=ON \ -DUSE_OPENMP=ON \ -DUSE_ELPA=ON \ - -DENABLE_RAPIDJSON=OFF \ + -DENABLE_RAPIDJSON=ON \ + -DRapdidJSON_DIR=$RAPIDJSON \ # -DENABLE_DEEPKS=1 \ # -DTorch_DIR=$LIBTORCH \ # -Dlibnpy_INCLUDE_DIR=$LIBNPY \ diff --git a/toolchain/build_abacus_intel-mpich.sh b/toolchain/build_abacus_intel-mpich.sh index e199f049cf..dc7fad7d96 100755 --- a/toolchain/build_abacus_intel-mpich.sh +++ b/toolchain/build_abacus_intel-mpich.sh @@ -26,6 +26,7 @@ PREFIX=$ABACUS_DIR ELPA=$INSTALL_DIR/elpa-2024.03.001/cpu CEREAL=$INSTALL_DIR/cereal-1.3.2/include/cereal LIBXC=$INSTALL_DIR/libxc-6.2.2 +RAPIDJSON=$INSTALL_DIR/rapidjson-1.1.0/ # LIBTORCH=$INSTALL_DIR/libtorch-2.1.2/share/cmake/Torch # LIBNPY=$INSTALL_DIR/libnpy-1.0.1/include # LIBRI=$INSTALL_DIR/LibRI-0.1.1 @@ -43,7 +44,8 @@ cmake -B $BUILD_DIR -DCMAKE_INSTALL_PREFIX=$PREFIX \ -DENABLE_LIBXC=ON \ -DUSE_OPENMP=ON \ -DUSE_ELPA=ON \ - -DENABLE_RAPIDJSON=OFF \ + -DENABLE_RAPIDJSON=ON \ + -DRapdidJSON_DIR=$RAPIDJSON \ # -DENABLE_DEEPKS=1 \ # -DTorch_DIR=$LIBTORCH \ # -Dlibnpy_INCLUDE_DIR=$LIBNPY \ diff --git a/toolchain/build_abacus_intel.sh b/toolchain/build_abacus_intel.sh index 777c812655..0b4563c688 100755 --- a/toolchain/build_abacus_intel.sh +++ b/toolchain/build_abacus_intel.sh @@ -26,6 +26,7 @@ PREFIX=$ABACUS_DIR ELPA=$INSTALL_DIR/elpa-2024.03.001/cpu CEREAL=$INSTALL_DIR/cereal-1.3.2/include/cereal LIBXC=$INSTALL_DIR/libxc-6.2.2 +RAPIDJSON=$INSTALL_DIR/rapidjson-1.1.0/ # LIBTORCH=$INSTALL_DIR/libtorch-2.1.2/share/cmake/Torch # LIBNPY=$INSTALL_DIR/libnpy-1.0.1/include # LIBRI=$INSTALL_DIR/LibRI-0.1.1 @@ -44,7 +45,8 @@ cmake -B $BUILD_DIR -DCMAKE_INSTALL_PREFIX=$PREFIX \ -DENABLE_LIBXC=ON \ -DUSE_OPENMP=ON \ -DUSE_ELPA=ON \ - -DENABLE_RAPIDJSON=OFF \ + -DENABLE_RAPIDJSON=ON \ + -DRapdidJSON_DIR=$RAPIDJSON \ # -DENABLE_DEEPKS=1 \ # -DTorch_DIR=$LIBTORCH \ # -Dlibnpy_INCLUDE_DIR=$LIBNPY \ diff --git a/toolchain/scripts/VERSION b/toolchain/scripts/VERSION index a6faad3be8..bce1d6fc01 100644 --- a/toolchain/scripts/VERSION +++ b/toolchain/scripts/VERSION @@ -1,2 +1,2 @@ # version file to force a rebuild of the entire toolchain -VERSION="2024.2" +VERSION="2024.3" diff --git a/toolchain/scripts/get_openblas_arch.sh b/toolchain/scripts/get_openblas_arch.sh index 70b492d576..4104ff821c 100755 --- a/toolchain/scripts/get_openblas_arch.sh +++ b/toolchain/scripts/get_openblas_arch.sh @@ -8,8 +8,8 @@ [ "${BASH_SOURCE[0]}" ] && SCRIPT_NAME="${BASH_SOURCE[0]}" || SCRIPT_NAME=$0 SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_NAME")" && pwd -P)" -openblas_ver="0.3.27" # Keep in sync with install_openblas.sh -openblas_sha256="aa2d68b1564fe2b13bc292672608e9cdeeeb6dc34995512e65c3b10f4599e897" +openblas_ver="0.3.28" # Keep in sync with install_openblas.sh +openblas_sha256="f1003466ad074e9b0c8d421a204121100b0751c96fc6fcf3d1456bd12f8a00a1" openblas_pkg="OpenBLAS-${openblas_ver}.tar.gz" source "${SCRIPT_DIR}"/common_vars.sh diff --git a/toolchain/scripts/stage0/install_cmake.sh b/toolchain/scripts/stage0/install_cmake.sh index a76472901b..b16c9719b2 100755 --- a/toolchain/scripts/stage0/install_cmake.sh +++ b/toolchain/scripts/stage0/install_cmake.sh @@ -36,17 +36,18 @@ case "${with_cmake}" in pkg_install_dir="${INSTALLDIR}/cmake-${cmake_ver}" #pkg_install_dir="${HOME}/apps/cmake/${cmake_ver}" install_lock_file="$pkg_install_dir/install_successful" + cmake_pkg="cmake-${cmake_ver}-${cmake_arch}.sh" if verify_checksums "${install_lock_file}"; then echo "cmake-${cmake_ver} is already installed, skipping it." else - if [ -f cmake-${cmake_ver}-${cmake_arch}.sh ]; then - echo "cmake-${cmake_ver}-${cmake_arch}.sh is found" + if [ -f $cmake_pkg ]; then + echo "$cmake_pkg is found" else - download_pkg_from_ABACUS_org "${cmake_sha256}" "cmake-${cmake_ver}-${cmake_arch}.sh" + download_pkg_from_ABACUS_org "${cmake_sha256}" "$cmake_pkg" fi echo "Installing from scratch into ${pkg_install_dir}" mkdir -p ${pkg_install_dir} - /bin/sh cmake-${cmake_ver}-${cmake_arch}.sh --prefix=${pkg_install_dir} --skip-license > install.log 2>&1 || tail -n ${LOG_LINES} install.log + /bin/sh $cmake_pkg --prefix=${pkg_install_dir} --skip-license > install.log 2>&1 || tail -n ${LOG_LINES} install.log write_checksums "${install_lock_file}" "${SCRIPT_DIR}/stage0/$(basename ${SCRIPT_NAME})" fi ;; diff --git a/toolchain/scripts/stage1/install_openmpi.sh b/toolchain/scripts/stage1/install_openmpi.sh index c5baa47843..99adfc5bd9 100755 --- a/toolchain/scripts/stage1/install_openmpi.sh +++ b/toolchain/scripts/stage1/install_openmpi.sh @@ -8,10 +8,10 @@ [ "${BASH_SOURCE[0]}" ] && SCRIPT_NAME="${BASH_SOURCE[0]}" || SCRIPT_NAME=$0 SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_NAME")/.." && pwd -P)" -openmpi_ver="5.0.5" -openmpi_sha256="6588d57c0a4bd299a24103f4e196051b29e8b55fbda49e11d5b3d32030a32776" -# openmpi_ver="4.1.6" -# openmpi_sha256="f740994485516deb63b5311af122c265179f5328a0d857a567b85db00b11e415" +#openmpi_ver="5.0.5" +#openmpi_sha256="6588d57c0a4bd299a24103f4e196051b29e8b55fbda49e11d5b3d32030a32776" +openmpi_ver="4.1.6" +openmpi_sha256="f740994485516deb63b5311af122c265179f5328a0d857a567b85db00b11e415" openmpi_pkg="openmpi-${openmpi_ver}.tar.bz2" source "${SCRIPT_DIR}"/common_vars.sh @@ -64,6 +64,7 @@ case "${with_openmpi}" in # OpenMPI 5.0 only supports PMIx # PMI support is required for Slurm, but not for other schedulers # default not use + # for OpenMPI 4.1 with pmi slurm, we can open this setting manually # if [ $(command -v srun) ]; then # echo "Slurm installation found. OpenMPI will be configured with --with-pmi." # EXTRA_CONFIGURE_FLAGS="--with-pmi" diff --git a/toolchain/scripts/stage2/install_openblas.sh b/toolchain/scripts/stage2/install_openblas.sh index 18039b2023..6c411edaad 100755 --- a/toolchain/scripts/stage2/install_openblas.sh +++ b/toolchain/scripts/stage2/install_openblas.sh @@ -8,8 +8,8 @@ [ "${BASH_SOURCE[0]}" ] && SCRIPT_NAME="${BASH_SOURCE[0]}" || SCRIPT_NAME=$0 SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_NAME")/.." && pwd -P)" -openblas_ver="0.3.27" # Keep in sync with get_openblas_arch.sh -openblas_sha256="aa2d68b1564fe2b13bc292672608e9cdeeeb6dc34995512e65c3b10f4599e897" +openblas_ver="0.3.28" # Keep in sync with get_openblas_arch.sh +openblas_sha256="f1003466ad074e9b0c8d421a204121100b0751c96fc6fcf3d1456bd12f8a00a1" openblas_pkg="OpenBLAS-${openblas_ver}.tar.gz" source "${SCRIPT_DIR}"/common_vars.sh diff --git a/toolchain/scripts/stage3/install_elpa.sh b/toolchain/scripts/stage3/install_elpa.sh index 24756c43b7..eff2bc7fe8 100755 --- a/toolchain/scripts/stage3/install_elpa.sh +++ b/toolchain/scripts/stage3/install_elpa.sh @@ -9,8 +9,8 @@ SCRIPT_DIR="$(cd "$(dirname "$SCRIPT_NAME")/.." && pwd -P)" # From https://elpa.mpcdf.mpg.de/software/tarball-archive/ELPA_TARBALL_ARCHIVE.html -elpa_ver="2024.03.001" -elpa_sha256="41c6cbf56d2dac26443faaba8a77307d261bf511682a64b96e24def77c813622" +elpa_ver="2024.05.001" +elpa_sha256="9caf41a3e600e2f6f4ce1931bd54185179dade9c171556d0c9b41bbc6940f2f6" source "${SCRIPT_DIR}"/common_vars.sh @@ -59,13 +59,15 @@ case "$with_elpa" in echo "elpa-${elpa_ver} is already installed, skipping it." else require_env MATH_LIBS - if [ -f elpa-${elpa_ver}.tar.gz ]; then - echo "elpa-${elpa_ver}.tar.gz is found" + elpa_pkg="elpa-${elpa_ver}.tar.gz" + url=f"https://elpa.mpcdf.mpg.de/software/tarball-archive/Releases/${elpa_ver}/${elpa_pkg}" + if [ -f ${elpa_pkg} ]; then + echo "${elpa_pkg} is found" else - download_pkg_from_ABACUS_org "${elpa_sha256}" "elpa-${elpa_ver}.tar.gz" + download_pkg_from_url "${elpa_sha256}" "${elpa_pkg}" "${url}" fi [ -d elpa-${elpa_ver} ] && rm -rf elpa-${elpa_ver} - tar -xzf elpa-${elpa_ver}.tar.gz + tar -xzf ${elpa_pkg} # elpa expect FC to be an mpi fortran compiler that is happy # with long lines, and that a bunch of libs can be found diff --git a/toolchain/scripts/stage4/install_rapidjson.sh b/toolchain/scripts/stage4/install_rapidjson.sh index 7883c96943..bce4d84d6f 100755 --- a/toolchain/scripts/stage4/install_rapidjson.sh +++ b/toolchain/scripts/stage4/install_rapidjson.sh @@ -55,6 +55,8 @@ case "$with_rapidjson" in tar -xzf $filename mkdir -p "${pkg_install_dir}" cp -r $dirname/* "${pkg_install_dir}/" + # for CMake to find rapidjson + cp ${pkg_install_dir}/RapidJSONConfig.cmake.in ${pkg_install_dir}/RapidJSONConfig.cmake write_checksums "${install_lock_file}" "${SCRIPT_DIR}/stage4/$(basename ${SCRIPT_NAME})" fi ;;