From 4f35e360c77fdad597b903320b98ade0b447e3af Mon Sep 17 00:00:00 2001 From: linpz Date: Tue, 8 Oct 2024 13:31:03 +0800 Subject: [PATCH 1/2] Fix memory bug in XC_Functional_Libxc::cal_gdr() --- source/module_hamilt_general/module_xc/xc_functional_libxc.h | 1 + .../module_xc/xc_functional_libxc_tools.cpp | 2 +- .../module_xc/xc_functional_libxc_vxc.cpp | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/source/module_hamilt_general/module_xc/xc_functional_libxc.h b/source/module_hamilt_general/module_xc/xc_functional_libxc.h index 73bf03e51c..1d04f5ee20 100644 --- a/source/module_hamilt_general/module_xc/xc_functional_libxc.h +++ b/source/module_hamilt_general/module_xc/xc_functional_libxc.h @@ -68,6 +68,7 @@ namespace XC_Functional_Libxc // calculating grho extern std::vector>> cal_gdr( const int nspin, + const std::size_t nrxx, const std::vector &rho, const double tpiba, const Charge* const chr); diff --git a/source/module_hamilt_general/module_xc/xc_functional_libxc_tools.cpp b/source/module_hamilt_general/module_xc/xc_functional_libxc_tools.cpp index c8513001a3..701502918d 100644 --- a/source/module_hamilt_general/module_xc/xc_functional_libxc_tools.cpp +++ b/source/module_hamilt_general/module_xc/xc_functional_libxc_tools.cpp @@ -51,12 +51,12 @@ XC_Functional_Libxc::convert_rho_amag_nspin4( std::vector>> XC_Functional_Libxc::cal_gdr( const int nspin, + const std::size_t nrxx, const std::vector &rho, const double tpiba, const Charge* const chr) { std::vector>> gdr(nspin); - const std::size_t nrxx = rho.size(); for( int is=0; is!=nspin; ++is ) { std::vector rhor(nrxx); diff --git a/source/module_hamilt_general/module_xc/xc_functional_libxc_vxc.cpp b/source/module_hamilt_general/module_xc/xc_functional_libxc_vxc.cpp index a215c02fe9..ca08be0697 100644 --- a/source/module_hamilt_general/module_xc/xc_functional_libxc_vxc.cpp +++ b/source/module_hamilt_general/module_xc/xc_functional_libxc_vxc.cpp @@ -68,7 +68,7 @@ std::tuple XC_Functional_Libxc::v_xc_libxc( / std::vector sigma; if(is_gga) { - gdr = XC_Functional_Libxc::cal_gdr(nspin, rho, tpiba, chr); + gdr = XC_Functional_Libxc::cal_gdr(nspin, nrxx, rho, tpiba, chr); sigma = XC_Functional_Libxc::convert_sigma(gdr); } @@ -183,7 +183,7 @@ std::tuple XC_Functional_Li const std::vector rho = XC_Functional_Libxc::convert_rho(nspin, nrxx, chr); const std::vector>> gdr - = XC_Functional_Libxc::cal_gdr(nspin, rho, tpiba, chr); + = XC_Functional_Libxc::cal_gdr(nspin, nrxx, rho, tpiba, chr); const std::vector sigma = XC_Functional_Libxc::convert_sigma(gdr); //converting kin_r From 1e79ca3cad4539c72e90f04983e2a66731ae4ea2 Mon Sep 17 00:00:00 2001 From: linpz Date: Thu, 10 Oct 2024 13:27:08 +0800 Subject: [PATCH 2/2] Refactor XC_Functional_Libxc::convert_vtxc_v() --- .../module_xc/xc_functional_libxc.h | 5 ++--- .../module_xc/xc_functional_libxc_tools.cpp | 13 +++++-------- .../module_xc/xc_functional_libxc_vxc.cpp | 7 ++++--- 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/source/module_hamilt_general/module_xc/xc_functional_libxc.h b/source/module_hamilt_general/module_xc/xc_functional_libxc.h index 1d04f5ee20..75b2693d0e 100644 --- a/source/module_hamilt_general/module_xc/xc_functional_libxc.h +++ b/source/module_hamilt_general/module_xc/xc_functional_libxc.h @@ -96,7 +96,7 @@ namespace XC_Functional_Libxc std::vector exc); // converting vtxc and v from vrho and vsigma (libxc=>abacus) - extern double convert_vtxc_v( + extern std::pair convert_vtxc_v( const xc_func_type &func, const int nspin, const std::size_t nrxx, @@ -106,8 +106,7 @@ namespace XC_Functional_Libxc const std::vector &vrho, const std::vector &vsigma, const double tpiba, - const Charge* const chr, - ModuleBase::matrix &v); + const Charge* const chr); // dh for gga v extern std::vector> cal_dh( diff --git a/source/module_hamilt_general/module_xc/xc_functional_libxc_tools.cpp b/source/module_hamilt_general/module_xc/xc_functional_libxc_tools.cpp index 701502918d..6975d066b7 100644 --- a/source/module_hamilt_general/module_xc/xc_functional_libxc_tools.cpp +++ b/source/module_hamilt_general/module_xc/xc_functional_libxc_tools.cpp @@ -163,8 +163,8 @@ double XC_Functional_Libxc::convert_etxc( return etxc; } -// converting etxc from exc (libxc=>abacus) -double XC_Functional_Libxc::convert_vtxc_v( +// converting vtxc and v from vrho and vsigma (libxc=>abacus) +std::pair XC_Functional_Libxc::convert_vtxc_v( const xc_func_type &func, const int nspin, const std::size_t nrxx, @@ -174,13 +174,10 @@ double XC_Functional_Libxc::convert_vtxc_v( const std::vector &vrho, const std::vector &vsigma, const double tpiba, - const Charge* const chr, - ModuleBase::matrix &v) + const Charge* const chr) { - assert(v.nr==nspin); - assert(v.nc==nrxx); - double vtxc = 0.0; + ModuleBase::matrix v(nspin, nrxx); #ifdef _OPENMP #pragma omp parallel for collapse(2) reduction(+:vtxc) schedule(static, 256) @@ -215,7 +212,7 @@ double XC_Functional_Libxc::convert_vtxc_v( vtxc -= rvtxc; } // end if(func.info->family == XC_FAMILY_GGA || func.info->family == XC_FAMILY_HYB_GGA)) - return vtxc; + return std::make_pair(vtxc, std::move(v)); } diff --git a/source/module_hamilt_general/module_xc/xc_functional_libxc_vxc.cpp b/source/module_hamilt_general/module_xc/xc_functional_libxc_vxc.cpp index ca08be0697..8b270cd233 100644 --- a/source/module_hamilt_general/module_xc/xc_functional_libxc_vxc.cpp +++ b/source/module_hamilt_general/module_xc/xc_functional_libxc_vxc.cpp @@ -110,12 +110,13 @@ std::tuple XC_Functional_Libxc::v_xc_libxc( / } etxc += XC_Functional_Libxc::convert_etxc(nspin, nrxx, sgn, rho, exc); - vtxc += XC_Functional_Libxc::convert_vtxc_v( + const std::pair vtxc_v = XC_Functional_Libxc::convert_vtxc_v( func, nspin, nrxx, sgn, rho, gdr, vrho, vsigma, - tpiba, chr, - v); + tpiba, chr); + vtxc += std::get<0>(vtxc_v); + v += std::get<1>(vtxc_v); } // end for( xc_func_type &func : funcs ) if(4==PARAM.inp.nspin)