From 39fdab34c419cded952647df93102afc6399ca21 Mon Sep 17 00:00:00 2001 From: kirk0830 Date: Tue, 6 Feb 2024 12:01:20 +0800 Subject: [PATCH 1/5] initialize this branch by make minor changes --- source/module_esolver/esolver_ks_pw.cpp | 3 ++- source/module_io/numerical_basis.cpp | 15 +++++++------ source/module_io/to_xml.h | 29 +++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 8 deletions(-) create mode 100644 source/module_io/to_xml.h diff --git a/source/module_esolver/esolver_ks_pw.cpp b/source/module_esolver/esolver_ks_pw.cpp index 89511f410b..341e51b099 100644 --- a/source/module_esolver/esolver_ks_pw.cpp +++ b/source/module_esolver/esolver_ks_pw.cpp @@ -1118,9 +1118,9 @@ void ESolver_KS_PW::postprocess() // output overlap if (winput::out_spillage <= 2) { - Numerical_Basis numerical_basis; if(INPUT.bessel_nao_rcuts.size() == 1) { + Numerical_Basis numerical_basis; numerical_basis.output_overlap(this->psi[0], this->sf, this->kv, this->pw_wfc); } else @@ -1129,6 +1129,7 @@ void ESolver_KS_PW::postprocess() { if(GlobalV::MY_RANK == 0) {std::cout << "update value: bessel_nao_rcut <- " << std::fixed << INPUT.bessel_nao_rcuts[i] << " a.u." << std::endl;} INPUT.bessel_nao_rcut = INPUT.bessel_nao_rcuts[i]; + Numerical_Basis numerical_basis; numerical_basis.output_overlap(this->psi[0], this->sf, this->kv, this->pw_wfc); std::string old_fname_header = winput::spillage_outdir + "/" + "orb_matrix."; std::string new_fname_header = winput::spillage_outdir + "/" + "orb_matrix_rcut" + std::to_string(int(INPUT.bessel_nao_rcut)) + "deriv"; diff --git a/source/module_io/numerical_basis.cpp b/source/module_io/numerical_basis.cpp index 2a21d6be46..3c06ff405d 100644 --- a/source/module_io/numerical_basis.cpp +++ b/source/module_io/numerical_basis.cpp @@ -139,12 +139,11 @@ void Numerical_Basis::output_overlap(const psi::Psi>& psi, } Parallel_Reduce::reduce_pool(overlap_V.c, overlap_V.nr * overlap_V.nc); // Peize Lin add 2020.04.23 #endif + if(ofs.good()) this->output_info(ofs, bessel_basis, kv); else assert(false); - this->output_info(ofs, bessel_basis, kv); + if(ofs.good()) this->output_k(ofs, kv); else assert(false); - this->output_k(ofs, kv); - - this->output_overlap_Q(ofs, overlap_Q, kv); + if(ofs.good()) this->output_overlap_Q(ofs, overlap_Q, kv); else assert(false); if (winput::out_spillage == 2) { @@ -649,6 +648,7 @@ void Numerical_Basis::output_overlap_Q(std::ofstream& ofs, if (GlobalV::MY_RANK==0) { ofs << "\n"; + ofs.flush(); } // (4) @@ -694,8 +694,9 @@ void Numerical_Basis::output_overlap_Q(std::ofstream& ofs, const int dim = Qtmp.getSize(); for (int i=0; i +#include +#include +/* this is the class to print information in xml format, NOT THE ONE CARRYING DATA */ +class to_xml { + public: + to_xml(); + ~to_xml(); + + private: + std::shared_ptr root_; +}; + +/* this is the class to carry data, relationship between sections are record in std::vector> container */ +class xml { + public: + xml(); + ~xml(); + + private: + std::string name_; + std::vector data_; + std::vector> attributes_; + + std::vector> children_; +}; \ No newline at end of file From 9412ce3751fc194f0446e486c8a96ce2f644ebeb Mon Sep 17 00:00:00 2001 From: kirk0830 Date: Sun, 18 Feb 2024 14:59:03 +0800 Subject: [PATCH 2/5] add warning on codes --- source/module_esolver/esolver_ks_pw.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/source/module_esolver/esolver_ks_pw.cpp b/source/module_esolver/esolver_ks_pw.cpp index 341e51b099..c5c4bc684b 100644 --- a/source/module_esolver/esolver_ks_pw.cpp +++ b/source/module_esolver/esolver_ks_pw.cpp @@ -1129,6 +1129,13 @@ void ESolver_KS_PW::postprocess() { if(GlobalV::MY_RANK == 0) {std::cout << "update value: bessel_nao_rcut <- " << std::fixed << INPUT.bessel_nao_rcuts[i] << " a.u." << std::endl;} INPUT.bessel_nao_rcut = INPUT.bessel_nao_rcuts[i]; + /* + SEVERE BUG + the memory management of numerical_basis class is NOT SAFE, data cleaning before overwriting is absent. + instance created from present implementation of numerical_basis SHOULD NOT BE USED FOR MORE THAN ONE TIME. + will cause data unexpected overwriting, file truncation and data loss. + Will be refactored in the future. + */ Numerical_Basis numerical_basis; numerical_basis.output_overlap(this->psi[0], this->sf, this->kv, this->pw_wfc); std::string old_fname_header = winput::spillage_outdir + "/" + "orb_matrix."; From de96d8ae437a8a011f38cf41a787780e12557bb1 Mon Sep 17 00:00:00 2001 From: kirk0830 Date: Tue, 20 Feb 2024 14:56:04 +0800 Subject: [PATCH 3/5] delete to_xml framework and move to other branch --- source/module_io/to_xml.h | 29 ----------------------------- 1 file changed, 29 deletions(-) delete mode 100644 source/module_io/to_xml.h diff --git a/source/module_io/to_xml.h b/source/module_io/to_xml.h deleted file mode 100644 index 32ddca647a..0000000000 --- a/source/module_io/to_xml.h +++ /dev/null @@ -1,29 +0,0 @@ -/* -To print any kind of information to an XML file -*/ -#include -#include -#include -/* this is the class to print information in xml format, NOT THE ONE CARRYING DATA */ -class to_xml { - public: - to_xml(); - ~to_xml(); - - private: - std::shared_ptr root_; -}; - -/* this is the class to carry data, relationship between sections are record in std::vector> container */ -class xml { - public: - xml(); - ~xml(); - - private: - std::string name_; - std::vector data_; - std::vector> attributes_; - - std::vector> children_; -}; \ No newline at end of file From 2bec121811f0656386f756747310cfdf3e7b1eea Mon Sep 17 00:00:00 2001 From: kirk0830 Date: Tue, 27 Feb 2024 14:41:09 +0800 Subject: [PATCH 4/5] modify code according to comment, also add annotation --- source/module_io/numerical_basis.cpp | 36 +++++++++++++++++----------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/source/module_io/numerical_basis.cpp b/source/module_io/numerical_basis.cpp index 3c06ff405d..cfb75b1fd0 100644 --- a/source/module_io/numerical_basis.cpp +++ b/source/module_io/numerical_basis.cpp @@ -91,9 +91,9 @@ void Numerical_Basis::output_overlap(const psi::Psi>& psi, ofs.open(ss.str().c_str()); } + // ALLOCATE MEMORY FOR THE OVERLAP MATRIX // OVERLAP : < J_mu | Psi > std::vector overlap_Q(kv.nks); - // OVERLAP : < J_mu | J_nu > std::vector overlap_Sq(kv.nks); @@ -101,7 +101,8 @@ void Numerical_Basis::output_overlap(const psi::Psi>& psi, ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"number of bands", GlobalV::NBANDS); ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"number of local orbitals", GlobalV::NLOCAL); ModuleBase::GlobalFunc::OUT(GlobalV::ofs_running,"number of eigenvalues of Jl(x)", this->bessel_basis.get_ecut_number()); - + + // CALCULATE THE OVERLAP MATRIX // nks now is the reduced k-points. for (int ik = 0; ik < kv.nks; ik++) { @@ -131,6 +132,7 @@ void Numerical_Basis::output_overlap(const psi::Psi>& psi, static_cast(derivative_order), kv); // Peize Lin add 2020.04.23 + // ALTHOUGH THIS FUNCTION NAMES output_overlap, IT ACTUALLY OUTPUTS THE OVERLAP MATRIX HERE #ifdef __MPI for (int ik = 0; ik < kv.nks; ik++) { @@ -139,19 +141,26 @@ void Numerical_Basis::output_overlap(const psi::Psi>& psi, } Parallel_Reduce::reduce_pool(overlap_V.c, overlap_V.nr * overlap_V.nc); // Peize Lin add 2020.04.23 #endif - if(ofs.good()) this->output_info(ofs, bessel_basis, kv); else assert(false); - - if(ofs.good()) this->output_k(ofs, kv); else assert(false); - - if(ofs.good()) this->output_overlap_Q(ofs, overlap_Q, kv); else assert(false); - - if (winput::out_spillage == 2) + // exception handling following, for FileNotOpenFailure + if(ofs.good()) this->output_info(ofs, bessel_basis, kv); // header of orb_matrix* file + else ModuleBase::WARNING_QUIT("Numerical_Basis","Failed to open file for writing the overlap matrix."); + // because one stage of file io complete, re-check the file status. + if(ofs.good()) this->output_k(ofs, kv); // ... + else ModuleBase::WARNING_QUIT("Numerical_Basis","Failed to write k-points to file."); + // because one stage of file io complete, re-check the file status. + if(ofs.good()) this->output_overlap_Q(ofs, overlap_Q, kv); // ... + else ModuleBase::WARNING_QUIT("Numerical_Basis","Failed to write overlap Q to file."); + // because one stage of file io complete, re-check the file status. + if(winput::out_spillage == 2) { - this->output_overlap_Sq(ss.str(), ofs, overlap_Sq, kv); + // caution: this is the largest matrix to be output, always flush + if(ofs.good()) this->output_overlap_Sq(ss.str(), ofs, overlap_Sq, kv); // ... + else ModuleBase::WARNING_QUIT("Numerical_Basis","Failed to write overlap S to file."); } - - this->output_overlap_V(ofs, overlap_V); // Peize Lin add 2020.04.23 - + // because one stage of file io complete, re-check the file status. + if(ofs.good()) this->output_overlap_V(ofs, overlap_V); // ... + // Peize Lin add 2020.04.23 + else ModuleBase::WARNING_QUIT("Numerical_Basis","Failed to write overlap V to file."); if (GlobalV::MY_RANK==0) ofs.close(); } return; @@ -696,7 +705,6 @@ void Numerical_Basis::output_overlap_Q(std::ofstream& ofs, { if ( count%4==0 ) ofs << std::endl; ofs << " " << Qtmp.ptr[i].real() << " " << Qtmp.ptr[i].imag(); - ofs.flush(); ++count; } // end data writing. From d31a09eac5a919077f4213c834e981955d9056eb Mon Sep 17 00:00:00 2001 From: kirk0830 Date: Tue, 27 Feb 2024 14:43:47 +0800 Subject: [PATCH 5/5] femove explicit flush in overlap_q --- source/module_io/numerical_basis.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/source/module_io/numerical_basis.cpp b/source/module_io/numerical_basis.cpp index cfb75b1fd0..f13d9ab6ee 100644 --- a/source/module_io/numerical_basis.cpp +++ b/source/module_io/numerical_basis.cpp @@ -657,7 +657,6 @@ void Numerical_Basis::output_overlap_Q(std::ofstream& ofs, if (GlobalV::MY_RANK==0) { ofs << "\n"; - ofs.flush(); } // (4)