From 96570fcdbef4a1a2bd75b07d2f21d3b4bad7d5f8 Mon Sep 17 00:00:00 2001 From: weiqingzhou Date: Tue, 12 Dec 2023 13:45:19 +0800 Subject: [PATCH 1/3] fix set_zero in elecstate_lcao --- source/module_elecstate/elecstate_lcao.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source/module_elecstate/elecstate_lcao.cpp b/source/module_elecstate/elecstate_lcao.cpp index d93fa3bee1..09927617bd 100644 --- a/source/module_elecstate/elecstate_lcao.cpp +++ b/source/module_elecstate/elecstate_lcao.cpp @@ -150,7 +150,10 @@ void ElecStateLCAO>::psiToRho(const psi::Psicharge->kin_r[0], this->charge->nrxx); + for (int is = 0; is < GlobalV::NSPIN; is++) + { + ModuleBase::GlobalFunc::ZEROS(this->charge->kin_r[is], this->charge->nrxx); + } Gint_inout inout1(this->loc->DM_R, this->charge->kin_r, Gint_Tools::job_type::tau); this->uhm->GK.cal_gint(&inout1); } @@ -222,7 +225,7 @@ void ElecStateLCAO::psiToRho(const psi::Psi& psi) { for (int is = 0; is < GlobalV::NSPIN; is++) { - ModuleBase::GlobalFunc::ZEROS(this->charge->kin_r[0], this->charge->nrxx); + ModuleBase::GlobalFunc::ZEROS(this->charge->kin_r[is], this->charge->nrxx); } Gint_inout inout1(this->loc->DM, this->charge->kin_r, Gint_Tools::job_type::tau); this->uhm->GG.cal_gint(&inout1); From d00dfb7a1531768eed90c5562563f781e2c7935e Mon Sep 17 00:00:00 2001 From: weiqingzhou Date: Tue, 12 Dec 2023 13:53:48 +0800 Subject: [PATCH 2/3] fix possible memory leak of kin in mix_rho() --- .../module_charge/charge_mixing.cpp | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/source/module_elecstate/module_charge/charge_mixing.cpp b/source/module_elecstate/module_charge/charge_mixing.cpp index 1d2029465d..15290c2ade 100755 --- a/source/module_elecstate/module_charge/charge_mixing.cpp +++ b/source/module_elecstate/module_charge/charge_mixing.cpp @@ -507,12 +507,16 @@ void Charge_Mixing::mix_rho(Charge* chr) if ((XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5) && mixing_tau) { kin_r123.resize(GlobalV::NSPIN * nrxx); + for (int is = 0; is < GlobalV::NSPIN; ++is) + { + double* kin_r123_is = kin_r123.data() + is * nrxx; #ifdef _OPENMP #pragma omp parallel for schedule(static, 512) #endif - for(int ir = 0 ; ir < GlobalV::NSPIN * nrxx ; ++ir) - { - kin_r123[ir] = chr->kin_r[0][ir]; + for(int ir = 0 ; ir < nrxx ; ++ir) + { + kin_r123_is[ir] = chr->kin_r[is][ir]; + } } } #ifdef USE_PAW @@ -569,12 +573,16 @@ void Charge_Mixing::mix_rho(Charge* chr) if ((XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5) && mixing_tau) { + for (int is = 0; is < GlobalV::NSPIN; ++is) + { + double* kin_r123_is = kin_r123.data() + is * nrxx; #ifdef _OPENMP #pragma omp parallel for schedule(static, 512) #endif - for(int ir = 0 ; ir < GlobalV::NSPIN * nrxx ; ++ir) - { - chr->kin_r_save[0][ir] = kin_r123[ir]; + for(int ir = 0 ; ir < nrxx ; ++ir) + { + chr->kin_r_save[is][ir] = kin_r123_is[ir]; + } } } From faafafa3f1119f0cae57daa74058a806c0133f36 Mon Sep 17 00:00:00 2001 From: weiqingzhou Date: Tue, 12 Dec 2023 14:17:30 +0800 Subject: [PATCH 3/3] refactor set_mixing & mix_reset --- source/module_elecstate/module_charge/charge_mixing.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/module_elecstate/module_charge/charge_mixing.cpp b/source/module_elecstate/module_charge/charge_mixing.cpp index 15290c2ade..251b81280a 100755 --- a/source/module_elecstate/module_charge/charge_mixing.cpp +++ b/source/module_elecstate/module_charge/charge_mixing.cpp @@ -89,6 +89,7 @@ void Charge_Mixing::set_mixing(const std::string& mixing_mode_in, #endif // Note: we can not init tau_mdata here temporarily, since set_xc_type() is after it. + // you can find initalize tau_mdata in mix_reset(); // this->mixing->init_mixing_data(this->tau_mdata, this->rhopw->nrxx * GlobalV::NSPIN, sizeof(double)); return; } @@ -463,6 +464,7 @@ void Charge_Mixing::mix_reset() { this->mixing->reset(); this->rho_mdata.reset(); + // initailize tau_mdata if ((XC_Functional::get_func_type() == 3 || XC_Functional::get_func_type() == 5) && mixing_tau) { if (GlobalV::SCF_THR_TYPE == 1) @@ -476,8 +478,9 @@ void Charge_Mixing::mix_reset() this->mixing->init_mixing_data(this->tau_mdata, this->rhopw->nrxx * GlobalV::NSPIN, sizeof(double)); } } + // reset for paw #ifdef USE_PAW - if(GlobalV::use_paw) this->mixing->init_mixing_data(this->nhat_mdata, this->rhopw->nrxx * GlobalV::NSPIN, sizeof(double)); + this->nhat_mdata.reset(); #endif }