From a6a54c08e103f90ad179922c736afa49bc50f483 Mon Sep 17 00:00:00 2001 From: sunliang98 <1700011430@pku.edu.cn> Date: Tue, 3 Dec 2024 16:30:40 +0800 Subject: [PATCH 1/4] Refactor: Remove read_pseudo and read_cell_pseudopots from Unitcell. --- source/module_cell/read_cell_pseudopots.cpp | 119 ------- source/module_cell/unitcell.cpp | 227 ------------- source/module_cell/unitcell.h | 4 - source/module_elecstate/CMakeLists.txt | 1 + source/module_elecstate/read_pseudo.cpp | 355 ++++++++++++++++++++ source/module_elecstate/read_pseudo.h | 15 + source/module_esolver/esolver_fp.cpp | 3 +- source/module_esolver/esolver_gets.cpp | 3 +- 8 files changed, 375 insertions(+), 352 deletions(-) create mode 100644 source/module_elecstate/read_pseudo.cpp create mode 100644 source/module_elecstate/read_pseudo.h diff --git a/source/module_cell/read_cell_pseudopots.cpp b/source/module_cell/read_cell_pseudopots.cpp index 541128e100..5ad091b31f 100644 --- a/source/module_cell/read_cell_pseudopots.cpp +++ b/source/module_cell/read_cell_pseudopots.cpp @@ -1,124 +1,5 @@ -#include "module_base/parallel_common.h" #include "module_parameter/parameter.h" #include "unitcell.h" -#include "read_pp.h" - -#include // Peize Lin fix bug about strcmp 2016-08-02 - -//========================================================== -// Read pseudopotential according to the dir -//========================================================== -void UnitCell::read_cell_pseudopots(const std::string& pp_dir, std::ofstream& log) -{ - ModuleBase::TITLE("UnitCell", "read_cell_pseudopots"); - // setup reading log for pseudopot_upf - std::stringstream ss; - ss << PARAM.globalv.global_out_dir << "atom_pseudo.log"; - - // Read in the atomic pseudo potentials - std::string pp_address; - for (int i = 0; i < ntype; i++) - { - Pseudopot_upf upf; - upf.coulomb_potential = this->atoms[i].coulomb_potential; - - // mohan update 2010-09-12 - int error = 0; - int error_ap = 0; - - if (GlobalV::MY_RANK == 0) - { - pp_address = pp_dir + this->pseudo_fn[i]; - error = upf.init_pseudo_reader(pp_address, this->pseudo_type[i], this->atoms[i].ncpp); // xiaohui add 2013-06-23 - - if (error == 0) // mohan add 2021-04-16 - { - if (this->atoms[i].flag_empty_element) // Peize Lin add for bsse 2021.04.07 - { - upf.set_empty_element(this->atoms[i].ncpp); - } - upf.set_upf_q(this->atoms[i].ncpp); // liuyu add 2023-09-21 - // average pseudopotential if needed - error_ap = upf.average_p(PARAM.inp.soc_lambda, this->atoms[i].ncpp); // added by zhengdy 2020-10-20 - } - this->atoms[i].coulomb_potential = upf.coulomb_potential; - } - -#ifdef __MPI - Parallel_Common::bcast_int(error); - Parallel_Common::bcast_int(error_ap); - Parallel_Common::bcast_bool(atoms[i].coulomb_potential); -#endif - - if (error_ap) - { - ModuleBase::WARNING_QUIT("UnitCell::read_cell_pseudopots", "error when average the pseudopotential."); - } - - if (error == 1) - { - std::cout << " Pseudopotential directory now is : " << pp_address << std::endl; - GlobalV::ofs_warning << " Pseudopotential directory now is : " << pp_address << std::endl; - ModuleBase::WARNING_QUIT("UnitCell::read_cell_pseudopots", "Couldn't find pseudopotential file."); - } - else if (error == 2) - { - ModuleBase::WARNING_QUIT("UnitCell::read_cell_pseudopots", "Pseudopotential data do not match."); - } - else if (error == 3) - { - ModuleBase::WARNING_QUIT( - "UnitCell::read_cell_pseudopots", - "Check the reference states in pseudopotential .vwr file.\n Also the norm of the read in pseudo wave " - "functions\n explicitly please check S, P and D channels.\n If the norm of the wave function is \n " - "unreasonable large (should be near 1.0), ABACUS would quit. \n The solution is to turn off the wave " - "functions \n and the corresponding non-local projectors together\n in .vwr pseudopotential file."); - } - else if (error == 4) - { - ModuleBase::WARNING_QUIT("UnitCell::read_cell_pseudopots", "Unknown pseudopotential type."); - } - - if (GlobalV::MY_RANK == 0) - { - upf.complete_default(this->atoms[i].ncpp); - log << "\n Read in pseudopotential file is " << pseudo_fn[i] << std::endl; - ModuleBase::GlobalFunc::OUT(log, "pseudopotential type", atoms[i].ncpp.pp_type); - ModuleBase::GlobalFunc::OUT(log, "exchange-correlation functional", atoms[i].ncpp.xc_func); - ModuleBase::GlobalFunc::OUT(log, "nonlocal core correction", atoms[i].ncpp.nlcc); - // ModuleBase::GlobalFunc::OUT(log, "spin orbital", atoms[i].has_so); - ModuleBase::GlobalFunc::OUT(log, "valence electrons", atoms[i].ncpp.zv); - ModuleBase::GlobalFunc::OUT(log, "lmax", atoms[i].ncpp.lmax); - ModuleBase::GlobalFunc::OUT(log, "number of zeta", atoms[i].ncpp.nchi); - ModuleBase::GlobalFunc::OUT(log, "number of projectors", atoms[i].ncpp.nbeta); - for (int ib = 0; ib < atoms[i].ncpp.nbeta; ib++) - { - ModuleBase::GlobalFunc::OUT(log, "L of projector", atoms[i].ncpp.lll[ib]); - } - // ModuleBase::GlobalFunc::OUT(log,"Grid Mesh Number", atoms[i].mesh); - if (PARAM.inp.dft_functional != "default") - { - std::string xc_func1 = PARAM.inp.dft_functional; - transform(xc_func1.begin(), xc_func1.end(), xc_func1.begin(), (::toupper)); - if (xc_func1 != atoms[i].ncpp.xc_func) - { - std::cout << " dft_functional readin is: " << PARAM.inp.dft_functional << std::endl; - std::cout << " dft_functional in pseudopot file is: " << atoms[i].ncpp.xc_func << std::endl; - std::cout << " Please make sure this is what you need" << std::endl; - GlobalV::ofs_warning << " dft_functional readin is: " << PARAM.inp.dft_functional << std::endl; - GlobalV::ofs_warning << " dft_functional in pseudopot file is: " << atoms[i].ncpp.xc_func - << std::endl; - GlobalV::ofs_warning << " Please make sure this is what you need" << std::endl; - - atoms[i].ncpp.xc_func = xc_func1; - log << " XC functional updated to : " << PARAM.inp.dft_functional << std::endl; - ModuleBase::GlobalFunc::OUT(log, "exchange-correlation functional", atoms[i].ncpp.xc_func); - } - } - } - } - return; -} void UnitCell::print_unitcell_pseudo(const std::string& fn) { diff --git a/source/module_cell/unitcell.cpp b/source/module_cell/unitcell.cpp index 71f460b39a..f0db84ed91 100755 --- a/source/module_cell/unitcell.cpp +++ b/source/module_cell/unitcell.cpp @@ -8,7 +8,6 @@ #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 @@ -661,232 +660,6 @@ void UnitCell::setup_cell(const std::string& fn, std::ofstream& log) { return; } -void UnitCell::read_pseudo(std::ofstream& ofs) { - // read in non-local pseudopotential and ouput the projectors. - ofs << "\n\n\n\n"; - ofs << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" - ">>>>" - << std::endl; - ofs << " | " - " |" - << std::endl; - ofs << " | Reading pseudopotentials files: " - " |" - << std::endl; - ofs << " | The pseudopotential file is in UPF format. The 'NC' indicates " - "that |" - << std::endl; - ofs << " | the type of pseudopotential is 'norm conserving'. Functional of " - " |" - << std::endl; - ofs << " | exchange and correlation is decided by 4 given parameters in " - "UPF |" - << std::endl; - ofs << " | file. We also read in the 'core correction' if there exists. " - " |" - << std::endl; - ofs << " | Also we can read the valence electrons number and the maximal " - " |" - << std::endl; - ofs << " | angular momentum used in this pseudopotential. We also read in " - "the |" - << std::endl; - ofs << " | trail wave function, trail atomic density and " - "local-pseudopotential|" - << std::endl; - ofs << " | on logrithmic grid. The non-local pseudopotential projector is " - "also|" - << std::endl; - ofs << " | read in if there is any. " - " |" - << std::endl; - ofs << " | " - " |" - << std::endl; - ofs << " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" - "<<<<" - << std::endl; - ofs << "\n\n\n\n"; - - read_cell_pseudopots(PARAM.inp.pseudo_dir, ofs); - - if (GlobalV::MY_RANK == 0) { - for (int it = 0; it < this->ntype; it++) { - Atom* atom = &atoms[it]; - if (!(atom->label_orb.empty())) { - compare_atom_labels(atom->label_orb, atom->ncpp.psd); - } - } - - if (PARAM.inp.out_element_info) { - for (int i = 0; i < this->ntype; i++) { - ModuleBase::Global_File::make_dir_atom(this->atoms[i].label); - } - for (int it = 0; it < ntype; it++) { - Atom* atom = &atoms[it]; - std::stringstream ss; - ss << PARAM.globalv.global_out_dir << atom->label << "/" - << atom->label << ".NONLOCAL"; - std::ofstream ofs(ss.str().c_str()); - - ofs << "
" << std::endl; - ofs << std::setw(10) << atom->label << "\t" - << "label" << std::endl; - ofs << std::setw(10) << atom->ncpp.pp_type << "\t" - << "pseudopotential type" << std::endl; - ofs << std::setw(10) << atom->ncpp.lmax << "\t" - << "lmax" << std::endl; - ofs << "
" << std::endl; - - ofs << "\n" << std::endl; - ofs << std::setw(10) << atom->ncpp.nbeta << "\t" - << "nummber of projectors." << std::endl; - for (int ib = 0; ib < atom->ncpp.nbeta; ib++) { - for (int ib2 = 0; ib2 < atom->ncpp.nbeta; ib2++) { - ofs << std::setw(10) << atom->ncpp.lll[ib] << " " - << atom->ncpp.lll[ib2] << " " - << atom->ncpp.dion(ib, ib2) << std::endl; - } - } - ofs << "" << std::endl; - - for (int i = 0; i < atom->ncpp.nbeta; i++) { - ofs << "" << std::endl; - ofs << std::setw(10) << i << "\t" - << "the index of projectors." << std::endl; - ofs << std::setw(10) << atom->ncpp.lll[i] << "\t" - << "the angular momentum." << std::endl; - - // mohan add - // only keep the nonzero part. - int cut_mesh = atom->ncpp.mesh; - for (int j = atom->ncpp.mesh - 1; j >= 0; --j) { - if (std::abs(atom->ncpp.betar(i, j)) > 1.0e-10) { - cut_mesh = j; - break; - } - } - if (cut_mesh % 2 == 0) { - ++cut_mesh; -} - - ofs << std::setw(10) << cut_mesh << "\t" - << "the number of mesh points." << std::endl; - - for (int j = 0; j < cut_mesh; ++j) { - ofs << std::setw(15) << atom->ncpp.r[j] << std::setw(15) - << atom->ncpp.betar(i, j) << std::setw(15) - << atom->ncpp.rab[j] << std::endl; - } - ofs << "" << std::endl; - } - - ofs.close(); - } - } - } - -#ifdef __MPI - bcast_unitcell2(); -#endif - - for (int it = 0; it < ntype; it++) { - if (atoms[0].ncpp.xc_func != atoms[it].ncpp.xc_func) { - GlobalV::ofs_warning << "\n type " << atoms[0].label - << " functional is " << atoms[0].ncpp.xc_func; - - GlobalV::ofs_warning << "\n type " << atoms[it].label - << " functional is " << atoms[it].ncpp.xc_func - << std::endl; - - ModuleBase::WARNING_QUIT("setup_cell", - "All DFT functional must consistent."); - } - } - - // 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 PARAM.globalv.nlocal - cal_nwfc(ofs); - - // Check whether the number of valence is minimum - if (GlobalV::MY_RANK == 0) { - int abtype = 0; - for (int it = 0; it < ntype; it++) { - if (ModuleBase::MinZval.find(atoms[it].ncpp.psd) - != ModuleBase::MinZval.end()) { - if (atoms[it].ncpp.zv - > ModuleBase::MinZval.at(atoms[it].ncpp.psd)) { - abtype += 1; - if (abtype == 1) { - std::cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - "%%%%%%%%%%%%%%%%%%%%%%%%%%" - << std::endl; - ofs << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - "%%%%%%%%%%%%%%%%%%%%%" - << std::endl; - } - std::cout << " Warning: the number of valence electrons in " - "pseudopotential > " - << ModuleBase::MinZval.at(atoms[it].ncpp.psd); - std::cout << " for " << atoms[it].ncpp.psd << ": " - << ModuleBase::EleConfig.at(atoms[it].ncpp.psd) - << std::endl; - ofs << " Warning: the number of valence electrons in " - "pseudopotential > " - << ModuleBase::MinZval.at(atoms[it].ncpp.psd); - ofs << " for " << atoms[it].ncpp.psd << ": " - << ModuleBase::EleConfig.at(atoms[it].ncpp.psd) - << std::endl; - } - } - } - if (abtype > 0) { - std::cout << " Pseudopotentials with additional electrons can " - "yield (more) accurate outcomes, but may be " - "less efficient." - << std::endl; - std::cout - << " If you're confident that your chosen pseudopotential is " - "appropriate, you can safely ignore " - "this warning." - << std::endl; - std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - "%%%%%%%%%%%%\n" - << std::endl; - ofs << " Pseudopotentials with additional electrons can yield " - "(more) accurate outcomes, but may be less " - "efficient." - << std::endl; - ofs << " If you're confident that your chosen pseudopotential is " - "appropriate, you can safely ignore this " - "warning." - << std::endl; - ofs << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" - "%%%%%%%"; - ModuleBase::GlobalFunc::OUT(ofs, ""); - } - } - - cal_meshx(); - -#ifdef __MPI - Parallel_Common::bcast_int(meshx); - Parallel_Common::bcast_int(natomwfc); - Parallel_Common::bcast_int(lmax); - Parallel_Common::bcast_int(lmax_ppwf); -#endif -} - //=========================================== // calculate the total number of local basis // Target : nwfc, lmax, diff --git a/source/module_cell/unitcell.h b/source/module_cell/unitcell.h index 6fcf87a259..77c0e61420 100644 --- a/source/module_cell/unitcell.h +++ b/source/module_cell/unitcell.h @@ -252,7 +252,6 @@ class UnitCell { std::ofstream& ofs_running, std::ofstream& ofs_warning); // read in atomic positions - void read_pseudo(std::ofstream& ofs); int find_type(const std::string& label); void print_tau() const; /** @@ -284,9 +283,6 @@ class UnitCell { // is fixed, adjust the lattice vectors void remake_cell(); - // read in pseudopotential from files for each type of atom - void read_cell_pseudopots(const std::string& fn, std::ofstream& log); - //================================================================ // cal_natomwfc : calculate total number of atomic wavefunctions // cal_nwfc : calculate total number of local basis and lmax diff --git a/source/module_elecstate/CMakeLists.txt b/source/module_elecstate/CMakeLists.txt index 8d6f2e5f69..31fd543709 100644 --- a/source/module_elecstate/CMakeLists.txt +++ b/source/module_elecstate/CMakeLists.txt @@ -32,6 +32,7 @@ list(APPEND objects magnetism.cpp occupy.cpp cal_ux.cpp + read_pseudo.cpp ) if(ENABLE_LCAO) diff --git a/source/module_elecstate/read_pseudo.cpp b/source/module_elecstate/read_pseudo.cpp new file mode 100644 index 0000000000..9110b79551 --- /dev/null +++ b/source/module_elecstate/read_pseudo.cpp @@ -0,0 +1,355 @@ +#include "read_pseudo.h" +#include "module_parameter/parameter.h" +#include "module_base/global_file.h" +#include "module_cell/read_pp.h" +#include "module_cell/cal_atoms_info.h" +#include "module_base/element_elec_config.h" +#include "module_base/parallel_common.h" + +#include // Peize Lin fix bug about strcmp 2016-08-02 + +namespace elecstate { + +void read_pseudo(std::ofstream& ofs, UnitCell& ucell) { + // read in non-local pseudopotential and ouput the projectors. + ofs << "\n\n\n\n"; + ofs << " >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>" + ">>>>" + << std::endl; + ofs << " | " + " |" + << std::endl; + ofs << " | Reading pseudopotentials files: " + " |" + << std::endl; + ofs << " | The pseudopotential file is in UPF format. The 'NC' indicates " + "that |" + << std::endl; + ofs << " | the type of pseudopotential is 'norm conserving'. Functional of " + " |" + << std::endl; + ofs << " | exchange and correlation is decided by 4 given parameters in " + "UPF |" + << std::endl; + ofs << " | file. We also read in the 'core correction' if there exists. " + " |" + << std::endl; + ofs << " | Also we can read the valence electrons number and the maximal " + " |" + << std::endl; + ofs << " | angular momentum used in this pseudopotential. We also read in " + "the |" + << std::endl; + ofs << " | trail wave function, trail atomic density and " + "local-pseudopotential|" + << std::endl; + ofs << " | on logrithmic grid. The non-local pseudopotential projector is " + "also|" + << std::endl; + ofs << " | read in if there is any. " + " |" + << std::endl; + ofs << " | " + " |" + << std::endl; + ofs << " <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<" + "<<<<" + << std::endl; + ofs << "\n\n\n\n"; + + read_cell_pseudopots(PARAM.inp.pseudo_dir, ofs, ucell); + + if (GlobalV::MY_RANK == 0) { + for (int it = 0; it < ucell.ntype; it++) { + Atom* atom = &ucell.atoms[it]; + if (!(atom->label_orb.empty())) { + ucell.compare_atom_labels(atom->label_orb, atom->ncpp.psd); + } + } + + if (PARAM.inp.out_element_info) { + for (int i = 0; i < ucell.ntype; i++) { + ModuleBase::Global_File::make_dir_atom(ucell.atoms[i].label); + } + for (int it = 0; it < ucell.ntype; it++) { + Atom* atom = &ucell.atoms[it]; + std::stringstream ss; + ss << PARAM.globalv.global_out_dir << atom->label << "/" + << atom->label << ".NONLOCAL"; + std::ofstream ofs(ss.str().c_str()); + + ofs << "
" << std::endl; + ofs << std::setw(10) << atom->label << "\t" + << "label" << std::endl; + ofs << std::setw(10) << atom->ncpp.pp_type << "\t" + << "pseudopotential type" << std::endl; + ofs << std::setw(10) << atom->ncpp.lmax << "\t" + << "lmax" << std::endl; + ofs << "
" << std::endl; + + ofs << "\n" << std::endl; + ofs << std::setw(10) << atom->ncpp.nbeta << "\t" + << "nummber of projectors." << std::endl; + for (int ib = 0; ib < atom->ncpp.nbeta; ib++) { + for (int ib2 = 0; ib2 < atom->ncpp.nbeta; ib2++) { + ofs << std::setw(10) << atom->ncpp.lll[ib] << " " + << atom->ncpp.lll[ib2] << " " + << atom->ncpp.dion(ib, ib2) << std::endl; + } + } + ofs << "" << std::endl; + + for (int i = 0; i < atom->ncpp.nbeta; i++) { + ofs << "" << std::endl; + ofs << std::setw(10) << i << "\t" + << "the index of projectors." << std::endl; + ofs << std::setw(10) << atom->ncpp.lll[i] << "\t" + << "the angular momentum." << std::endl; + + // mohan add + // only keep the nonzero part. + int cut_mesh = atom->ncpp.mesh; + for (int j = atom->ncpp.mesh - 1; j >= 0; --j) { + if (std::abs(atom->ncpp.betar(i, j)) > 1.0e-10) { + cut_mesh = j; + break; + } + } + if (cut_mesh % 2 == 0) { + ++cut_mesh; + } + + ofs << std::setw(10) << cut_mesh << "\t" + << "the number of mesh points." << std::endl; + + for (int j = 0; j < cut_mesh; ++j) { + ofs << std::setw(15) << atom->ncpp.r[j] << std::setw(15) + << atom->ncpp.betar(i, j) << std::setw(15) + << atom->ncpp.rab[j] << std::endl; + } + ofs << "" << std::endl; + } + + ofs.close(); + } + } + } + +#ifdef __MPI + ucell.bcast_unitcell2(); +#endif + + for (int it = 0; it < ucell.ntype; it++) { + if (ucell.atoms[0].ncpp.xc_func != ucell.atoms[it].ncpp.xc_func) { + GlobalV::ofs_warning << "\n type " << ucell.atoms[0].label + << " functional is " << ucell.atoms[0].ncpp.xc_func; + + GlobalV::ofs_warning << "\n type " << ucell.atoms[it].label + << " functional is " << ucell.atoms[it].ncpp.xc_func + << std::endl; + + ModuleBase::WARNING_QUIT("setup_cell", + "All DFT functional must consistent."); + } + } + + // setup the total number of PAOs + ucell.cal_natomwfc(ofs); + + // Calculate the information of atoms from the pseudopotential to set PARAM + CalAtomsInfo ca; + ca.cal_atoms_info(ucell.atoms, ucell.ntype, PARAM); + + // setup PARAM.globalv.nlocal + ucell.cal_nwfc(ofs); + + // Check whether the number of valence is minimum + if (GlobalV::MY_RANK == 0) { + int abtype = 0; + for (int it = 0; it < ucell.ntype; it++) { + if (ModuleBase::MinZval.find(ucell.atoms[it].ncpp.psd) + != ModuleBase::MinZval.end()) { + if (ucell.atoms[it].ncpp.zv + > ModuleBase::MinZval.at(ucell.atoms[it].ncpp.psd)) { + abtype += 1; + if (abtype == 1) { + std::cout << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%%%%%%%%%%%%%%" + << std::endl; + ofs << "\n%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%%%%%%%%%" + << std::endl; + } + std::cout << " Warning: the number of valence electrons in " + "pseudopotential > " + << ModuleBase::MinZval.at(ucell.atoms[it].ncpp.psd); + std::cout << " for " << ucell.atoms[it].ncpp.psd << ": " + << ModuleBase::EleConfig.at(ucell.atoms[it].ncpp.psd) + << std::endl; + ofs << " Warning: the number of valence electrons in " + "pseudopotential > " + << ModuleBase::MinZval.at(ucell.atoms[it].ncpp.psd); + ofs << " for " << ucell.atoms[it].ncpp.psd << ": " + << ModuleBase::EleConfig.at(ucell.atoms[it].ncpp.psd) + << std::endl; + } + } + } + if (abtype > 0) { + std::cout << " Pseudopotentials with additional electrons can " + "yield (more) accurate outcomes, but may be " + "less efficient." + << std::endl; + std::cout + << " If you're confident that your chosen pseudopotential is " + "appropriate, you can safely ignore " + "this warning." + << std::endl; + std::cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%\n" + << std::endl; + ofs << " Pseudopotentials with additional electrons can yield " + "(more) accurate outcomes, but may be less " + "efficient." + << std::endl; + ofs << " If you're confident that your chosen pseudopotential is " + "appropriate, you can safely ignore this " + "warning." + << std::endl; + ofs << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" + "%%%%%%%"; + ModuleBase::GlobalFunc::OUT(ofs, ""); + } + } + + ucell.cal_meshx(); + +#ifdef __MPI + Parallel_Common::bcast_int(ucell.meshx); + Parallel_Common::bcast_int(ucell.natomwfc); + Parallel_Common::bcast_int(ucell.lmax); + Parallel_Common::bcast_int(ucell.lmax_ppwf); +#endif +} + +//========================================================== +// Read pseudopotential according to the dir +//========================================================== +void read_cell_pseudopots(const std::string& pp_dir, std::ofstream& log, UnitCell& ucell) +{ + ModuleBase::TITLE("Elecstate", "read_cell_pseudopots"); + // setup reading log for pseudopot_upf + std::stringstream ss; + ss << PARAM.globalv.global_out_dir << "atom_pseudo.log"; + + // Read in the atomic pseudo potentials + std::string pp_address; + for (int i = 0; i < ucell.ntype; i++) + { + Pseudopot_upf upf; + upf.coulomb_potential = ucell.atoms[i].coulomb_potential; + + // mohan update 2010-09-12 + int error = 0; + int error_ap = 0; + + if (GlobalV::MY_RANK == 0) + { + pp_address = pp_dir + ucell.pseudo_fn[i]; + error = upf.init_pseudo_reader(pp_address, ucell.pseudo_type[i], ucell.atoms[i].ncpp); // xiaohui add 2013-06-23 + + if (error == 0) // mohan add 2021-04-16 + { + if (ucell.atoms[i].flag_empty_element) // Peize Lin add for bsse 2021.04.07 + { + upf.set_empty_element(ucell.atoms[i].ncpp); + } + upf.set_upf_q(ucell.atoms[i].ncpp); // liuyu add 2023-09-21 + // average pseudopotential if needed + error_ap = upf.average_p(PARAM.inp.soc_lambda, ucell.atoms[i].ncpp); // added by zhengdy 2020-10-20 + } + ucell.atoms[i].coulomb_potential = upf.coulomb_potential; + } + +#ifdef __MPI + Parallel_Common::bcast_int(error); + Parallel_Common::bcast_int(error_ap); + Parallel_Common::bcast_bool(ucell.atoms[i].coulomb_potential); +#endif + + if (error_ap) + { + ModuleBase::WARNING_QUIT("UnitCell::read_cell_pseudopots", "error when average the pseudopotential."); + } + + if (error == 1) + { + std::cout << " Pseudopotential directory now is : " << pp_address << std::endl; + GlobalV::ofs_warning << " Pseudopotential directory now is : " << pp_address << std::endl; + ModuleBase::WARNING_QUIT("UnitCell::read_cell_pseudopots", "Couldn't find pseudopotential file."); + } + else if (error == 2) + { + ModuleBase::WARNING_QUIT("UnitCell::read_cell_pseudopots", "Pseudopotential data do not match."); + } + else if (error == 3) + { + ModuleBase::WARNING_QUIT( + "UnitCell::read_cell_pseudopots", + "Check the reference states in pseudopotential .vwr file.\n Also the norm of the read in pseudo wave " + "functions\n explicitly please check S, P and D channels.\n If the norm of the wave function is \n " + "unreasonable large (should be near 1.0), ABACUS would quit. \n The solution is to turn off the wave " + "functions \n and the corresponding non-local projectors together\n in .vwr pseudopotential file."); + } + else if (error == 4) + { + ModuleBase::WARNING_QUIT("UnitCell::read_cell_pseudopots", "Unknown pseudopotential type."); + } + + if (GlobalV::MY_RANK == 0) + { + upf.complete_default(ucell.atoms[i].ncpp); + log << "\n Read in pseudopotential file is " << ucell.pseudo_fn[i] << std::endl; + ModuleBase::GlobalFunc::OUT(log, "pseudopotential type", ucell.atoms[i].ncpp.pp_type); + ModuleBase::GlobalFunc::OUT(log, "exchange-correlation functional", ucell.atoms[i].ncpp.xc_func); + ModuleBase::GlobalFunc::OUT(log, "nonlocal core correction", ucell.atoms[i].ncpp.nlcc); + // ModuleBase::GlobalFunc::OUT(log, "spin orbital", ucell.atoms[i].has_so); + ModuleBase::GlobalFunc::OUT(log, "valence electrons", ucell.atoms[i].ncpp.zv); + ModuleBase::GlobalFunc::OUT(log, "lmax", ucell.atoms[i].ncpp.lmax); + ModuleBase::GlobalFunc::OUT(log, "number of zeta", ucell.atoms[i].ncpp.nchi); + ModuleBase::GlobalFunc::OUT(log, "number of projectors", ucell.atoms[i].ncpp.nbeta); + for (int ib = 0; ib < ucell.atoms[i].ncpp.nbeta; ib++) + { + ModuleBase::GlobalFunc::OUT(log, "L of projector", ucell.atoms[i].ncpp.lll[ib]); + } + // ModuleBase::GlobalFunc::OUT(log,"Grid Mesh Number", atoms[i].mesh); + if (PARAM.inp.dft_functional != "default") + { + std::string xc_func1 = PARAM.inp.dft_functional; + transform(xc_func1.begin(), xc_func1.end(), xc_func1.begin(), (::toupper)); + if (xc_func1 != ucell.atoms[i].ncpp.xc_func) + { + std::cout << " dft_functional readin is: " << PARAM.inp.dft_functional << std::endl; + std::cout << " dft_functional in pseudopot file is: " << ucell.atoms[i].ncpp.xc_func << std::endl; + std::cout << " Please make sure this is what you need" << std::endl; + GlobalV::ofs_warning << " dft_functional readin is: " << PARAM.inp.dft_functional << std::endl; + GlobalV::ofs_warning << " dft_functional in pseudopot file is: " << ucell.atoms[i].ncpp.xc_func + << std::endl; + GlobalV::ofs_warning << " Please make sure this is what you need" << std::endl; + + ucell.atoms[i].ncpp.xc_func = xc_func1; + log << " XC functional updated to : " << PARAM.inp.dft_functional << std::endl; + ModuleBase::GlobalFunc::OUT(log, "exchange-correlation functional", ucell.atoms[i].ncpp.xc_func); + } + } + } + } + return; +} + + +} \ No newline at end of file diff --git a/source/module_elecstate/read_pseudo.h b/source/module_elecstate/read_pseudo.h new file mode 100644 index 0000000000..9f98033f16 --- /dev/null +++ b/source/module_elecstate/read_pseudo.h @@ -0,0 +1,15 @@ +#ifndef READ_PSEUDO_H +#define READ_PSEUDO_H + +#include "module_cell/unitcell.h" + +namespace elecstate { + + void read_pseudo(std::ofstream& ofs, UnitCell& ucell); + + // read in pseudopotential from files for each type of atom + void read_cell_pseudopots(const std::string& fn, std::ofstream& log, UnitCell& ucell); + +} + +#endif \ No newline at end of file diff --git a/source/module_esolver/esolver_fp.cpp b/source/module_esolver/esolver_fp.cpp index 776e837d9a..44b069c34d 100644 --- a/source/module_esolver/esolver_fp.cpp +++ b/source/module_esolver/esolver_fp.cpp @@ -2,6 +2,7 @@ #include "module_base/global_variable.h" #include "module_elecstate/module_charge/symmetry_rho.h" +#include "module_elecstate/read_pseudo.h" #include "module_hamilt_pw/hamilt_pwdft/global.h" #include "module_io/cif_io.h" #include "module_io/cube_io.h" @@ -59,7 +60,7 @@ void ESolver_FP::before_all_runners(UnitCell& ucell, const Input_para& inp) //! 1) read pseudopotentials if (!PARAM.inp.use_paw) { - ucell.read_pseudo(GlobalV::ofs_running); + elecstate::read_pseudo(GlobalV::ofs_running, ucell); } //! 2) initialie the plane wave basis for rho diff --git a/source/module_esolver/esolver_gets.cpp b/source/module_esolver/esolver_gets.cpp index 5a39566c49..4090e82016 100644 --- a/source/module_esolver/esolver_gets.cpp +++ b/source/module_esolver/esolver_gets.cpp @@ -3,6 +3,7 @@ #include "module_base/timer.h" #include "module_cell/module_neighbor/sltk_atom_arrange.h" #include "module_elecstate/elecstate_lcao.h" +#include "module_elecstate/read_pseudo.h" #include "module_hamilt_lcao/hamilt_lcaodft/LCAO_domain.h" #include "module_hamilt_lcao/hamilt_lcaodft/hamilt_lcao.h" #include "module_hamilt_lcao/hamilt_lcaodft/operator_lcao/operator_lcao.h" @@ -29,7 +30,7 @@ void ESolver_GetS::before_all_runners(UnitCell& ucell, const Input_para& inp) ModuleBase::timer::tick("ESolver_GetS", "before_all_runners"); // 1.1) read pseudopotentials - ucell.read_pseudo(GlobalV::ofs_running); + elecstate::read_pseudo(GlobalV::ofs_running, ucell); // 1.2) symmetrize things if (ModuleSymmetry::Symmetry::symm_flag == 1) From a0a7f56ae290961f08231dc2fd86444e329ba3f4 Mon Sep 17 00:00:00 2001 From: sunliang98 <1700011430@pku.edu.cn> Date: Tue, 3 Dec 2024 17:27:09 +0800 Subject: [PATCH 2/4] Test: Update Unit test. --- source/module_cell/test/CMakeLists.txt | 1 + .../test/support/mock_unitcell.cpp | 3 -- .../module_cell/test/unitcell_test_para.cpp | 5 +-- .../module_cell/test/unitcell_test_readpp.cpp | 33 ++++++++++--------- source/module_cell/test_pw/CMakeLists.txt | 2 +- .../test/LCAO_deepks_test_prep.cpp | 3 +- source/module_io/test/write_orb_info_test.cpp | 3 +- source/module_md/test/CMakeLists.txt | 1 + 8 files changed, 27 insertions(+), 24 deletions(-) diff --git a/source/module_cell/test/CMakeLists.txt b/source/module_cell/test/CMakeLists.txt index 6558197058..1c5294ae03 100644 --- a/source/module_cell/test/CMakeLists.txt +++ b/source/module_cell/test/CMakeLists.txt @@ -26,6 +26,7 @@ list(APPEND cell_simple_srcs ../read_pp_vwr.cpp ../read_pp_blps.cpp ../check_atomic_stru.cpp + ../../module_elecstate/read_pseudo.cpp ) add_library(cell_info OBJECT ${cell_simple_srcs}) diff --git a/source/module_cell/test/support/mock_unitcell.cpp b/source/module_cell/test/support/mock_unitcell.cpp index 973e347f31..88b36235f0 100644 --- a/source/module_cell/test/support/mock_unitcell.cpp +++ b/source/module_cell/test/support/mock_unitcell.cpp @@ -68,8 +68,6 @@ int UnitCell::read_atom_species(std::ifstream& ifa, std::ofstream& ofs_running) { return 0; } -void UnitCell::read_cell_pseudopots(const std::string& pp_dir, - std::ofstream& log) {} bool UnitCell::read_atom_positions(std::ifstream& ifpos, std::ofstream& ofs_running, std::ofstream& ofs_warning) { @@ -94,7 +92,6 @@ void UnitCell::read_orb_file(int it, std::string& orb_file, std::ofstream& ofs_running, Atom* atom) {} -void UnitCell::read_pseudo(std::ofstream& ofs) {} int UnitCell::find_type(const std::string& label) { return 0; } void UnitCell::print_tau() const {} void UnitCell::print_stru_file(const std::string& fn, diff --git a/source/module_cell/test/unitcell_test_para.cpp b/source/module_cell/test/unitcell_test_para.cpp index a6a097d9e8..dbe302a486 100644 --- a/source/module_cell/test/unitcell_test_para.cpp +++ b/source/module_cell/test/unitcell_test_para.cpp @@ -7,6 +7,7 @@ #include "module_base/global_variable.h" #include "module_base/mathzone.h" #include "module_cell/unitcell.h" +#include "module_elecstate/read_pseudo.h" #include #include #ifdef __MPI @@ -93,7 +94,7 @@ class UcellTest : public ::testing::Test #ifdef __MPI TEST_F(UcellTest, BcastUnitcell2) { - ucell->read_cell_pseudopots(pp_dir, ofs); + elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell); ucell->bcast_unitcell2(); if (GlobalV::MY_RANK != 0) { @@ -153,7 +154,7 @@ TEST_F(UcellTest, ReadPseudo) { PARAM.input.pseudo_dir = pp_dir; PARAM.input.out_element_info = true; - ucell->read_pseudo(ofs); + elecstate::read_pseudo(ofs, *ucell); // check_structure will print some warning info // output nonlocal file if (GlobalV::MY_RANK == 0) diff --git a/source/module_cell/test/unitcell_test_readpp.cpp b/source/module_cell/test/unitcell_test_readpp.cpp index 509f4f179e..8a693c4c54 100644 --- a/source/module_cell/test/unitcell_test_readpp.cpp +++ b/source/module_cell/test/unitcell_test_readpp.cpp @@ -8,6 +8,7 @@ #include "module_base/mathzone.h" #include "module_cell/check_atomic_stru.h" #include "module_cell/unitcell.h" +#include "module_elecstate/read_pseudo.h" #include #include #ifdef __MPI @@ -136,7 +137,7 @@ TEST_F(UcellDeathTest, ReadCellPPWarning1) { PARAM.input.lspinorb = true; ucell->pseudo_fn[1] = "H_sr.upf"; testing::internal::CaptureStdout(); - EXPECT_EXIT(ucell->read_cell_pseudopots(pp_dir, ofs), + EXPECT_EXIT(elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell), ::testing::ExitedWithCode(1), ""); output = testing::internal::GetCapturedStdout(); @@ -147,7 +148,7 @@ TEST_F(UcellDeathTest, ReadCellPPWarning1) { TEST_F(UcellDeathTest, ReadCellPPWarning2) { pp_dir = "./arbitrary/"; testing::internal::CaptureStdout(); - EXPECT_EXIT(ucell->read_cell_pseudopots(pp_dir, ofs), + EXPECT_EXIT(elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell), ::testing::ExitedWithCode(1), ""); output = testing::internal::GetCapturedStdout(); @@ -158,7 +159,7 @@ TEST_F(UcellDeathTest, ReadCellPPWarning2) { TEST_F(UcellDeathTest, ReadCellPPWarning3) { ucell->pseudo_type[0] = "upf"; testing::internal::CaptureStdout(); - EXPECT_EXIT(ucell->read_cell_pseudopots(pp_dir, ofs), + EXPECT_EXIT(elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell), ::testing::ExitedWithCode(1), ""); output = testing::internal::GetCapturedStdout(); @@ -169,7 +170,7 @@ TEST_F(UcellDeathTest, ReadCellPPWarning3) { TEST_F(UcellDeathTest, ReadCellPPWarning4) { PARAM.input.dft_functional = "LDA"; testing::internal::CaptureStdout(); - EXPECT_NO_THROW(ucell->read_cell_pseudopots(pp_dir, ofs)); + EXPECT_NO_THROW(elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell)); output = testing::internal::GetCapturedStdout(); EXPECT_THAT(output, testing::HasSubstr("dft_functional readin is: LDA")); EXPECT_THAT(output, @@ -181,7 +182,7 @@ TEST_F(UcellDeathTest, ReadCellPPWarning4) { TEST_F(UcellDeathTest, ReadCellPPWarning5) { ucell->pseudo_type[0] = "upf0000"; testing::internal::CaptureStdout(); - EXPECT_EXIT(ucell->read_cell_pseudopots(pp_dir, ofs), + EXPECT_EXIT(elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell), ::testing::ExitedWithCode(1), ""); output = testing::internal::GetCapturedStdout(); @@ -190,7 +191,7 @@ TEST_F(UcellDeathTest, ReadCellPPWarning5) { TEST_F(UcellTest, ReadCellPP) { ucell->atoms[1].flag_empty_element = true; - ucell->read_cell_pseudopots(pp_dir, ofs); + elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell); EXPECT_EQ(ucell->atoms[0].ncpp.pp_type, "NC"); EXPECT_FALSE(ucell->atoms[0].ncpp.has_so); // becomes false in average_p EXPECT_FALSE(ucell->atoms[1].ncpp.has_so); @@ -213,7 +214,7 @@ TEST_F(UcellTest, ReadCellPP) { } TEST_F(UcellTest, CalMeshx) { - ucell->read_cell_pseudopots(pp_dir, ofs); + elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell); ucell->cal_meshx(); EXPECT_EQ(ucell->atoms[0].ncpp.msh, 1247); EXPECT_EQ(ucell->atoms[1].ncpp.msh, 1165); @@ -221,7 +222,7 @@ TEST_F(UcellTest, CalMeshx) { } TEST_F(UcellTest, CalNatomwfc1) { - ucell->read_cell_pseudopots(pp_dir, ofs); + elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell); EXPECT_FALSE(ucell->atoms[0].ncpp.has_so); EXPECT_FALSE(ucell->atoms[1].ncpp.has_so); ucell->cal_natomwfc(ofs); @@ -235,7 +236,7 @@ TEST_F(UcellTest, CalNatomwfc1) { TEST_F(UcellTest, CalNatomwfc2) { PARAM.input.lspinorb = false; PARAM.input.nspin = 4; - ucell->read_cell_pseudopots(pp_dir, ofs); + elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell); EXPECT_FALSE(ucell->atoms[0].ncpp.has_so); EXPECT_FALSE(ucell->atoms[1].ncpp.has_so); ucell->cal_natomwfc(ofs); @@ -249,7 +250,7 @@ TEST_F(UcellTest, CalNatomwfc2) { TEST_F(UcellTest, CalNatomwfc3) { PARAM.input.lspinorb = true; PARAM.input.nspin = 4; - ucell->read_cell_pseudopots(pp_dir, ofs); + elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell); EXPECT_TRUE(ucell->atoms[0].ncpp.has_so); EXPECT_TRUE(ucell->atoms[1].ncpp.has_so); ucell->cal_natomwfc(ofs); @@ -262,7 +263,7 @@ TEST_F(UcellTest, CalNatomwfc3) { } TEST_F(UcellTest, CalNwfc1) { - ucell->read_cell_pseudopots(pp_dir, ofs); + elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell); EXPECT_FALSE(ucell->atoms[0].ncpp.has_so); EXPECT_FALSE(ucell->atoms[1].ncpp.has_so); PARAM.sys.nlocal = 3 * 9; @@ -328,7 +329,7 @@ TEST_F(UcellTest, CalNwfc1) { TEST_F(UcellTest, CalNwfc2) { PARAM.input.nspin = 4; PARAM.input.basis_type = "lcao"; - ucell->read_cell_pseudopots(pp_dir, ofs); + elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell); EXPECT_FALSE(ucell->atoms[0].ncpp.has_so); EXPECT_FALSE(ucell->atoms[1].ncpp.has_so); PARAM.sys.nlocal = 3 * 9 * 2; @@ -336,7 +337,7 @@ TEST_F(UcellTest, CalNwfc2) { } TEST_F(UcellDeathTest, CheckStructure) { - ucell->read_cell_pseudopots(pp_dir, ofs); + elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell); EXPECT_FALSE(ucell->atoms[0].ncpp.has_so); EXPECT_FALSE(ucell->atoms[1].ncpp.has_so); // trial 1 @@ -379,7 +380,7 @@ TEST_F(UcellDeathTest, ReadPseudoWarning1) { PARAM.input.out_element_info = true; ucell->pseudo_fn[1] = "H_sr_lda.upf"; testing::internal::CaptureStdout(); - EXPECT_EXIT(ucell->read_pseudo(ofs), ::testing::ExitedWithCode(1), ""); + EXPECT_EXIT(elecstate::read_pseudo(ofs, *ucell), ::testing::ExitedWithCode(1), ""); output = testing::internal::GetCapturedStdout(); EXPECT_THAT(output, testing::HasSubstr("All DFT functional must consistent.")); @@ -390,7 +391,7 @@ TEST_F(UcellDeathTest, ReadPseudoWarning2) { PARAM.input.out_element_info = true; ucell->pseudo_fn[0] = "Al_ONCV_PBE-1.0.upf"; testing::internal::CaptureStdout(); - EXPECT_NO_THROW(ucell->read_pseudo(ofs)); + EXPECT_NO_THROW(elecstate::read_pseudo(ofs, *ucell)); output = testing::internal::GetCapturedStdout(); EXPECT_THAT( output, @@ -399,7 +400,7 @@ TEST_F(UcellDeathTest, ReadPseudoWarning2) { } TEST_F(UcellTest, CalNelec) { - ucell->read_cell_pseudopots(pp_dir, ofs); + elecstate::read_cell_pseudopots(pp_dir, ofs, *ucell); EXPECT_EQ(4, ucell->atoms[0].ncpp.zv); EXPECT_EQ(1, ucell->atoms[1].ncpp.zv); EXPECT_EQ(1, ucell->atoms[0].na); diff --git a/source/module_cell/test_pw/CMakeLists.txt b/source/module_cell/test_pw/CMakeLists.txt index 3e1bc0b07e..52ac0d4617 100644 --- a/source/module_cell/test_pw/CMakeLists.txt +++ b/source/module_cell/test_pw/CMakeLists.txt @@ -13,7 +13,7 @@ AddTest( LIBS parameter ${math_libs} base device SOURCES unitcell_test_pw.cpp ../unitcell.cpp ../read_atoms.cpp ../read_cell_pseudopots.cpp ../atom_spec.cpp ../atom_pseudo.cpp ../pseudo.cpp ../read_pp.cpp ../read_pp_complete.cpp ../read_pp_upf201.cpp ../read_pp_upf100.cpp - ../read_pp_vwr.cpp ../read_pp_blps.cpp ../../module_io/output.cpp + ../read_pp_vwr.cpp ../read_pp_blps.cpp ../../module_io/output.cpp ../../module_elecstate/read_pseudo.cpp ) find_program(BASH bash) diff --git a/source/module_hamilt_lcao/module_deepks/test/LCAO_deepks_test_prep.cpp b/source/module_hamilt_lcao/module_deepks/test/LCAO_deepks_test_prep.cpp index f97e26a0ea..15b1d11ab0 100644 --- a/source/module_hamilt_lcao/module_deepks/test/LCAO_deepks_test_prep.cpp +++ b/source/module_hamilt_lcao/module_deepks/test/LCAO_deepks_test_prep.cpp @@ -1,5 +1,6 @@ #include "LCAO_deepks_test.h" #include "module_base/global_variable.h" +#include "module_elecstate/read_pseudo.h" #define private public #include "module_parameter/parameter.h" #undef private @@ -126,7 +127,7 @@ void test_deepks::set_ekcut() void test_deepks::setup_cell() { ucell.setup_cell("STRU", GlobalV::ofs_running); - ucell.read_pseudo(GlobalV::ofs_running); + elecstate::read_pseudo(GlobalV::ofs_running, ucell); return; } diff --git a/source/module_io/test/write_orb_info_test.cpp b/source/module_io/test/write_orb_info_test.cpp index 9c4fae341b..272c15cd8d 100644 --- a/source/module_io/test/write_orb_info_test.cpp +++ b/source/module_io/test/write_orb_info_test.cpp @@ -6,6 +6,7 @@ #include "module_io/write_orb_info.h" #include "module_cell/unitcell.h" #include "prepare_unitcell.h" +#include "module_elecstate/read_pseudo.h" #ifdef __LCAO InfoNonlocal::InfoNonlocal(){} @@ -50,7 +51,7 @@ TEST(OrbInfo,WriteOrbInfo) PARAM.input.basis_type = "pw"; PARAM.input.dft_functional = "default"; PARAM.sys.nlocal = 18; - ucell->read_cell_pseudopots(pp_dir,ofs); + elecstate::read_cell_pseudopots(pp_dir,ofs,*ucell); ucell->cal_nwfc(ofs); ModuleIO::write_orb_info(ucell); ofs.close(); diff --git a/source/module_md/test/CMakeLists.txt b/source/module_md/test/CMakeLists.txt index f2f4e713b0..58b5824d2e 100644 --- a/source/module_md/test/CMakeLists.txt +++ b/source/module_md/test/CMakeLists.txt @@ -47,6 +47,7 @@ list(APPEND depend_files ../../module_base/parallel_reduce.cpp ../../module_base/parallel_global.cpp ../../module_base/parallel_comm.cpp + ../../module_elecstate/read_pseudo.cpp ) AddTest( From 9c7cff80c3bb61f0ac58ed00358afacfbaa328be Mon Sep 17 00:00:00 2001 From: sunliang98 <1700011430@pku.edu.cn> Date: Tue, 3 Dec 2024 17:39:54 +0800 Subject: [PATCH 3/4] Refactor: Remove print_unitcell_pseudo from UnitCell. Delete module_cell/read_cell_pseudopots.cpp --- source/module_cell/CMakeLists.txt | 1 - source/module_cell/read_cell_pseudopots.cpp | 19 ------------------- source/module_cell/test/CMakeLists.txt | 1 - .../test/support/mock_unitcell.cpp | 1 - source/module_cell/test/unitcell_test.cpp | 3 ++- source/module_cell/test_pw/CMakeLists.txt | 2 +- source/module_cell/unitcell.h | 1 - source/module_elecstate/read_pseudo.cpp | 17 +++++++++++++++++ source/module_elecstate/read_pseudo.h | 2 ++ source/module_md/test/CMakeLists.txt | 1 - 10 files changed, 22 insertions(+), 26 deletions(-) delete mode 100644 source/module_cell/read_cell_pseudopots.cpp diff --git a/source/module_cell/CMakeLists.txt b/source/module_cell/CMakeLists.txt index eabad91be6..c658450e3f 100644 --- a/source/module_cell/CMakeLists.txt +++ b/source/module_cell/CMakeLists.txt @@ -18,7 +18,6 @@ add_library( read_pp_vwr.cpp unitcell.cpp read_atoms.cpp - read_cell_pseudopots.cpp setup_nonlocal.cpp klist.cpp parallel_kpoints.cpp diff --git a/source/module_cell/read_cell_pseudopots.cpp b/source/module_cell/read_cell_pseudopots.cpp deleted file mode 100644 index 5ad091b31f..0000000000 --- a/source/module_cell/read_cell_pseudopots.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include "module_parameter/parameter.h" -#include "unitcell.h" - -void UnitCell::print_unitcell_pseudo(const std::string& fn) -{ - if (PARAM.inp.test_pseudo_cell) { - ModuleBase::TITLE("UnitCell", "print_unitcell_pseudo"); -} - std::ofstream ofs(fn.c_str()); - - this->print_cell(ofs); - for (int i = 0; i < ntype; i++) - { - atoms[i].print_Atom(ofs); - } - - ofs.close(); - return; -} diff --git a/source/module_cell/test/CMakeLists.txt b/source/module_cell/test/CMakeLists.txt index 1c5294ae03..acb79c77db 100644 --- a/source/module_cell/test/CMakeLists.txt +++ b/source/module_cell/test/CMakeLists.txt @@ -15,7 +15,6 @@ install(FILES unitcell_test_parallel.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) list(APPEND cell_simple_srcs ../unitcell.cpp ../read_atoms.cpp - ../read_cell_pseudopots.cpp ../atom_spec.cpp ../atom_pseudo.cpp ../pseudo.cpp diff --git a/source/module_cell/test/support/mock_unitcell.cpp b/source/module_cell/test/support/mock_unitcell.cpp index 88b36235f0..b0e4e204ac 100644 --- a/source/module_cell/test/support/mock_unitcell.cpp +++ b/source/module_cell/test/support/mock_unitcell.cpp @@ -108,7 +108,6 @@ void UnitCell::remake_cell() {} void UnitCell::cal_nwfc(std::ofstream& log) {} void UnitCell::cal_meshx() {} void UnitCell::cal_natomwfc(std::ofstream& log) {} -void UnitCell::print_unitcell_pseudo(const std::string& fn) {} bool UnitCell::check_tau() const { return true; } bool UnitCell::if_atoms_can_move() const { return true; } bool UnitCell::if_cell_can_change() const { return true; } diff --git a/source/module_cell/test/unitcell_test.cpp b/source/module_cell/test/unitcell_test.cpp index ea46081986..88052a433b 100644 --- a/source/module_cell/test/unitcell_test.cpp +++ b/source/module_cell/test/unitcell_test.cpp @@ -5,6 +5,7 @@ #undef private #include "module_cell/unitcell.h" #include "module_elecstate/cal_ux.h" +#include "module_elecstate/read_pseudo.h" #include "memory" #include "module_base/global_variable.h" @@ -822,7 +823,7 @@ TEST_F(UcellTest, PrintUnitcellPseudo) ucell = utp.SetUcellInfo(); PARAM.input.test_pseudo_cell = 1; std::string fn = "printcell.log"; - ucell->print_unitcell_pseudo(fn); + elecstate::print_unitcell_pseudo(fn, *ucell); std::ifstream ifs; ifs.open("printcell.log"); std::string str((std::istreambuf_iterator(ifs)), std::istreambuf_iterator()); diff --git a/source/module_cell/test_pw/CMakeLists.txt b/source/module_cell/test_pw/CMakeLists.txt index 52ac0d4617..f4bcc63c3f 100644 --- a/source/module_cell/test_pw/CMakeLists.txt +++ b/source/module_cell/test_pw/CMakeLists.txt @@ -11,7 +11,7 @@ install(FILES unitcell_test_pw_para.sh DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) AddTest( TARGET cell_unitcell_test_pw LIBS parameter ${math_libs} base device - SOURCES unitcell_test_pw.cpp ../unitcell.cpp ../read_atoms.cpp ../read_cell_pseudopots.cpp ../atom_spec.cpp + SOURCES unitcell_test_pw.cpp ../unitcell.cpp ../read_atoms.cpp ../atom_spec.cpp ../atom_pseudo.cpp ../pseudo.cpp ../read_pp.cpp ../read_pp_complete.cpp ../read_pp_upf201.cpp ../read_pp_upf100.cpp ../read_pp_vwr.cpp ../read_pp_blps.cpp ../../module_io/output.cpp ../../module_elecstate/read_pseudo.cpp ) diff --git a/source/module_cell/unitcell.h b/source/module_cell/unitcell.h index 77c0e61420..4b8a04be02 100644 --- a/source/module_cell/unitcell.h +++ b/source/module_cell/unitcell.h @@ -291,7 +291,6 @@ class UnitCell { void cal_nwfc(std::ofstream& log); void cal_meshx(); void cal_natomwfc(std::ofstream& log); - void print_unitcell_pseudo(const std::string& fn); bool check_tau() const; // mohan add 2011-03-03 bool if_atoms_can_move() const; bool if_cell_can_change() const; diff --git a/source/module_elecstate/read_pseudo.cpp b/source/module_elecstate/read_pseudo.cpp index 9110b79551..0274fb9ab7 100644 --- a/source/module_elecstate/read_pseudo.cpp +++ b/source/module_elecstate/read_pseudo.cpp @@ -351,5 +351,22 @@ void read_cell_pseudopots(const std::string& pp_dir, std::ofstream& log, UnitCel return; } +void print_unitcell_pseudo(const std::string& fn, UnitCell& ucell) +{ + if (PARAM.inp.test_pseudo_cell) + { + ModuleBase::TITLE("UnitCell", "print_unitcell_pseudo"); + } + std::ofstream ofs(fn.c_str()); + + ucell.print_cell(ofs); + for (int i = 0; i < ucell.ntype; i++) + { + ucell.atoms[i].print_Atom(ofs); + } + + ofs.close(); + return; +} } \ No newline at end of file diff --git a/source/module_elecstate/read_pseudo.h b/source/module_elecstate/read_pseudo.h index 9f98033f16..066abbd239 100644 --- a/source/module_elecstate/read_pseudo.h +++ b/source/module_elecstate/read_pseudo.h @@ -10,6 +10,8 @@ namespace elecstate { // read in pseudopotential from files for each type of atom void read_cell_pseudopots(const std::string& fn, std::ofstream& log, UnitCell& ucell); + void print_unitcell_pseudo(const std::string& fn, UnitCell& ucell); + } #endif \ No newline at end of file diff --git a/source/module_md/test/CMakeLists.txt b/source/module_md/test/CMakeLists.txt index 58b5824d2e..16051470fd 100644 --- a/source/module_md/test/CMakeLists.txt +++ b/source/module_md/test/CMakeLists.txt @@ -8,7 +8,6 @@ list(APPEND depend_files ../../module_cell/atom_spec.cpp ../../module_cell/atom_pseudo.cpp ../../module_cell/read_atoms.cpp - ../../module_cell/read_cell_pseudopots.cpp ../../module_cell/pseudo.cpp ../../module_cell/read_pp.cpp ../../module_cell/read_pp_complete.cpp From d81b399821f12c623e08f7a1b7bf303a0f62794c Mon Sep 17 00:00:00 2001 From: sunliang98 <1700011430@pku.edu.cn> Date: Tue, 3 Dec 2024 20:45:29 +0800 Subject: [PATCH 4/4] Test: Update Makefile --- source/Makefile.Objects | 2 +- source/module_hamilt_lcao/module_deepks/test/Makefile | 2 ++ source/module_hamilt_lcao/module_deepks/test/Makefile.Objects | 2 ++ 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 16380bbe98..7dd7762326 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -182,7 +182,6 @@ OBJS_CELL=atom_pseudo.o\ read_pp_vwr.o\ unitcell.o\ read_atoms.o\ - read_cell_pseudopots.o\ setup_nonlocal.o\ klist.o\ cell_index.o\ @@ -229,6 +228,7 @@ OBJS_ELECSTAT=elecstate.o\ H_TDDFT_pw.o\ pot_xc.o\ cal_ux.o\ + read_pseudo.o\ OBJS_ELECSTAT_LCAO=elecstate_lcao.o\ elecstate_lcao_cal_tau.o\ diff --git a/source/module_hamilt_lcao/module_deepks/test/Makefile b/source/module_hamilt_lcao/module_deepks/test/Makefile index e1d6aaa920..1b08db03be 100644 --- a/source/module_hamilt_lcao/module_deepks/test/Makefile +++ b/source/module_hamilt_lcao/module_deepks/test/Makefile @@ -51,6 +51,7 @@ VPATH=../../module_base\ :../../module_basis/module_ao\ :../../module_neighbor\ :../../module_cell\ +:../../module_elecstate\ :../../\ :../\ :./\ @@ -70,6 +71,7 @@ $(OBJS_CELL)\ $(OBJS_ORB)\ $(OBJS_NEIGHBOR)\ $(OBJS_PW)\ +$(OBJS_ELECSTATE)\ FP_OBJS=$(patsubst %.o, ${OBJ_DIR}/%.o, ${FP_OBJS_0}) diff --git a/source/module_hamilt_lcao/module_deepks/test/Makefile.Objects b/source/module_hamilt_lcao/module_deepks/test/Makefile.Objects index 271c1ac775..9e62c5551a 100644 --- a/source/module_hamilt_lcao/module_deepks/test/Makefile.Objects +++ b/source/module_hamilt_lcao/module_deepks/test/Makefile.Objects @@ -73,3 +73,5 @@ sltk_atom.o\ sltk_atom_input.o\ sltk_grid.o\ sltk_grid_driver.o + +OBJS_ELECSTATE=read_pseudo.o\ \ No newline at end of file