From 7907696a4de4fc4dc38faac8e32d8c81a375588c Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Thu, 2 Apr 2026 15:50:14 +0800 Subject: [PATCH 01/20] refactor: remove template parameters from ESolver_KS_PW::prepare_init call --- source/source_esolver/esolver_ks_pw.cpp | 3 +-- source/source_psi/psi_prepare_base.h | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 4a17417b5b8..dc66bc6166e 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -147,8 +147,7 @@ void ESolver_KS_PW::before_scf(UnitCell& ucell, const int istep) if (ucell.cell_parameter_updated) { - auto* p_psi_init = static_cast*>(this->stp.p_psi_init); - p_psi_init->prepare_init(PARAM.inp.pw_seed); + this->stp.p_psi_init->prepare_init(PARAM.inp.pw_seed); } //! Init Hamiltonian (cell changed) diff --git a/source/source_psi/psi_prepare_base.h b/source/source_psi/psi_prepare_base.h index 13b8716d8e0..c7a10718bf9 100644 --- a/source/source_psi/psi_prepare_base.h +++ b/source/source_psi/psi_prepare_base.h @@ -16,6 +16,7 @@ class PSIPrepareBase public: PSIPrepareBase() = default; virtual ~PSIPrepareBase() = default; + virtual void prepare_init(const int& random_seed) = 0; }; } // namespace psi From ca2b43c710db11ba52d14bc444d37fc689d1188d Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Thu, 2 Apr 2026 16:12:32 +0800 Subject: [PATCH 02/20] refactor: remove deallocate_hamilt function --- source/source_esolver/esolver_ks_lcaopw.cpp | 16 +++++-------- source/source_esolver/esolver_ks_lcaopw.h | 1 - source/source_esolver/esolver_ks_pw.cpp | 26 +++++++++++---------- source/source_esolver/esolver_ks_pw.h | 1 - 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/source/source_esolver/esolver_ks_lcaopw.cpp b/source/source_esolver/esolver_ks_lcaopw.cpp index 7be0b0f5038..c6154ab5b8b 100644 --- a/source/source_esolver/esolver_ks_lcaopw.cpp +++ b/source/source_esolver/esolver_ks_lcaopw.cpp @@ -56,7 +56,11 @@ namespace ModuleESolver //**************************************************** delete this->psi_local; // delete Hamilt - this->deallocate_hamilt(); + if (this->p_hamilt != nullptr) + { + delete this->p_hamilt; + this->p_hamilt = nullptr; + } } template @@ -68,15 +72,7 @@ namespace ModuleESolver #endif ); } - template - void ESolver_KS_LIP::deallocate_hamilt() - { - if (this->p_hamilt != nullptr) - { - delete reinterpret_cast*>(this->p_hamilt); - this->p_hamilt = nullptr; - } - } + template void ESolver_KS_LIP::before_scf(UnitCell& ucell, const int istep) { diff --git a/source/source_esolver/esolver_ks_lcaopw.h b/source/source_esolver/esolver_ks_lcaopw.h index c5eac500e98..b8d6811f765 100644 --- a/source/source_esolver/esolver_ks_lcaopw.h +++ b/source/source_esolver/esolver_ks_lcaopw.h @@ -36,7 +36,6 @@ namespace ModuleESolver const double ethr) override; virtual void allocate_hamilt(const UnitCell& ucell) override; - virtual void deallocate_hamilt() override; psi::Psi* psi_local = nullptr; ///< psi for all local NAOs diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index dc66bc6166e..e05be08581d 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -52,7 +52,11 @@ ESolver_KS_PW::~ESolver_KS_PW() // do not add any codes in this deconstructor funcion //**************************************************** // delete Hamilt - this->deallocate_hamilt(); + if (this->p_hamilt != nullptr) + { + delete this->p_hamilt; + this->p_hamilt = nullptr; + } // delete exx_helper if (this->exx_helper != nullptr) @@ -77,15 +81,7 @@ void ESolver_KS_PW::allocate_hamilt(const UnitCell& ucell) &ucell); } -template -void ESolver_KS_PW::deallocate_hamilt() -{ - if (this->p_hamilt != nullptr) - { - delete static_cast*>(this->p_hamilt); - this->p_hamilt = nullptr; - } -} + template void ESolver_KS_PW::before_all_runners(UnitCell& ucell, const Input_para& inp) @@ -153,7 +149,11 @@ void ESolver_KS_PW::before_scf(UnitCell& ucell, const int istep) //! Init Hamiltonian (cell changed) //! Operators in HamiltPW should be reallocated once cell changed //! delete Hamilt if not first scf - this->deallocate_hamilt(); + if (this->p_hamilt != nullptr) + { + delete this->p_hamilt; + this->p_hamilt = nullptr; + } //! Allocate HamiltPW this->allocate_hamilt(ucell); @@ -163,7 +163,9 @@ void ESolver_KS_PW::before_scf(UnitCell& ucell, const int istep) // init DFT+U is done in "before_all_runners" in LCAO basis. This should be refactored, mohan note 2025-11-06 pw::setup_pot(istep, ucell, this->kv, this->sf, this->pelec, this->Pgrid, this->chr, this->locpp, this->ppcell, this->dftu, this->vsep_cell, - this->stp.template get_psi_t(), static_cast*>(this->p_hamilt), this->pw_wfc, this->pw_rhod, PARAM.inp); + this->stp.template get_psi_t(), + static_cast*>(this->p_hamilt), + this->pw_wfc, this->pw_rhod, PARAM.inp); // setup psi (electronic wave functions) this->stp.init(this->p_hamilt); diff --git a/source/source_esolver/esolver_ks_pw.h b/source/source_esolver/esolver_ks_pw.h index f527101d686..227aaeea254 100644 --- a/source/source_esolver/esolver_ks_pw.h +++ b/source/source_esolver/esolver_ks_pw.h @@ -49,7 +49,6 @@ class ESolver_KS_PW : public ESolver_KS virtual void hamilt2rho_single(UnitCell& ucell, const int istep, const int iter, const double ethr) override; virtual void allocate_hamilt(const UnitCell& ucell); - virtual void deallocate_hamilt(); // Electronic wave function psi Setup_Psi_pw stp; From 20b29e001d3547e23ad62a373c601cccd84938b6 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sat, 4 Apr 2026 16:50:34 +0800 Subject: [PATCH 03/20] =?UTF-8?q?refactor(setup=5Fpot):=20=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=20p=5Fhamilt=20=E7=9A=84=E6=A8=A1=E6=9D=BF=E5=8F=82?= =?UTF-8?q?=E6=95=B0=EF=BC=8C=E4=BD=BF=E7=94=A8=20HamiltBase=20=E5=9F=BA?= =?UTF-8?q?=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/source_esolver/esolver_ks_pw.cpp | 2 +- source/source_pw/module_pwdft/setup_pot.cpp | 10 +++++----- source/source_pw/module_pwdft/setup_pot.h | 2 +- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index e05be08581d..2efc2847a86 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -164,7 +164,7 @@ void ESolver_KS_PW::before_scf(UnitCell& ucell, const int istep) pw::setup_pot(istep, ucell, this->kv, this->sf, this->pelec, this->Pgrid, this->chr, this->locpp, this->ppcell, this->dftu, this->vsep_cell, this->stp.template get_psi_t(), - static_cast*>(this->p_hamilt), + this->p_hamilt, this->pw_wfc, this->pw_rhod, PARAM.inp); // setup psi (electronic wave functions) diff --git a/source/source_pw/module_pwdft/setup_pot.cpp b/source/source_pw/module_pwdft/setup_pot.cpp index d8a340535c8..76594cabfb3 100644 --- a/source/source_pw/module_pwdft/setup_pot.cpp +++ b/source/source_pw/module_pwdft/setup_pot.cpp @@ -19,7 +19,7 @@ void pw::setup_pot(const int istep, Plus_U &dftu, // mohan add 2025-11-06 VSep* vsep_cell, // U-1/2 method psi::Psi* kspw_psi, // electronic wave functions - hamilt::Hamilt* p_hamilt, // hamiltonian + hamilt::HamiltBase* p_hamilt, // hamiltonian ModulePW::PW_Basis_K *pw_wfc, // pw for wfc const ModulePW::PW_Basis *pw_rhod, // pw for rhod const Input_para& inp) // input parameters @@ -137,7 +137,7 @@ template void pw::setup_pot, base_device::DEVICE_CPU>( Plus_U &dftu, // mohan add 2025-11-06 VSep* vsep_cell, // U-1/2 method psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // electronic wave functions - hamilt::Hamilt, base_device::DEVICE_CPU>* p_hamilt, // hamiltonian + hamilt::HamiltBase* p_hamilt, // hamiltonian ModulePW::PW_Basis_K *pw_wfc, // pw for wfc const ModulePW::PW_Basis *pw_rhod, // pw for rhod const Input_para& inp); // input parameters @@ -156,7 +156,7 @@ template void pw::setup_pot, base_device::DEVICE_CPU>( Plus_U &dftu, // mohan add 2025-11-06 VSep* vsep_cell, // U-1/2 method psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // electronic wave functions - hamilt::Hamilt, base_device::DEVICE_CPU>* p_hamilt, // hamiltonian + hamilt::HamiltBase* p_hamilt, // hamiltonian ModulePW::PW_Basis_K *pw_wfc, // pw for wfc const ModulePW::PW_Basis *pw_rhod, // pw for rhod const Input_para& inp); // input parameters @@ -176,7 +176,7 @@ template void pw::setup_pot, base_device::DEVICE_GPU>( Plus_U &dftu, // mohan add 2025-11-06 VSep* vsep_cell, // U-1/2 method psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // electronic wave functions - hamilt::Hamilt, base_device::DEVICE_GPU>* p_hamilt, // hamiltonian + hamilt::HamiltBase* p_hamilt, // hamiltonian ModulePW::PW_Basis_K *pw_wfc, // pw for wfc const ModulePW::PW_Basis *pw_rhod, // pw for rhod const Input_para& inp); // input parameters @@ -194,7 +194,7 @@ template void pw::setup_pot, base_device::DEVICE_GPU>( Plus_U &dftu, // mohan add 2025-11-06 VSep* vsep_cell, // U-1/2 method psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // electronic wave functions - hamilt::Hamilt, base_device::DEVICE_GPU>* p_hamilt, // hamiltonian + hamilt::HamiltBase* p_hamilt, // hamiltonian ModulePW::PW_Basis_K *pw_wfc, // pw for wfc const ModulePW::PW_Basis *pw_rhod, // pw for rhod const Input_para& inp); // input parameters diff --git a/source/source_pw/module_pwdft/setup_pot.h b/source/source_pw/module_pwdft/setup_pot.h index 0f691bbfc75..d3f08a0f819 100644 --- a/source/source_pw/module_pwdft/setup_pot.h +++ b/source/source_pw/module_pwdft/setup_pot.h @@ -26,7 +26,7 @@ void setup_pot(const int istep, Plus_U &dftu, // mohan add 2025-11-06 VSep* vsep_cell, // U-1/2 method psi::Psi* kspw_psi, // electronic wave functions - hamilt::Hamilt* p_hamilt, // hamiltonian + hamilt::HamiltBase* p_hamilt, // hamiltonian ModulePW::PW_Basis_K *pw_wfc, // pw for wfc const ModulePW::PW_Basis *pw_rhod, // pw for rhod const Input_para& inp); // input parameters From b2d4745a65a93de6528f1bdaec5b916559d38eb8 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sat, 4 Apr 2026 16:55:45 +0800 Subject: [PATCH 04/20] =?UTF-8?q?style(setup=5Fpot):=20=E5=B0=86=20tab=20?= =?UTF-8?q?=E9=94=AE=E6=9B=BF=E6=8D=A2=E4=B8=BA=E7=A9=BA=E6=A0=BC=E9=94=AE?= =?UTF-8?q?=EF=BC=8C=E7=BB=9F=E4=B8=80=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/source_pw/module_pwdft/setup_pot.cpp | 204 ++++++++++---------- source/source_pw/module_pwdft/setup_pot.h | 24 +-- 2 files changed, 114 insertions(+), 114 deletions(-) diff --git a/source/source_pw/module_pwdft/setup_pot.cpp b/source/source_pw/module_pwdft/setup_pot.cpp index 76594cabfb3..dd333f6d2f7 100644 --- a/source/source_pw/module_pwdft/setup_pot.cpp +++ b/source/source_pw/module_pwdft/setup_pot.cpp @@ -8,21 +8,21 @@ template void pw::setup_pot(const int istep, - UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons - const Parallel_Grid ¶_grid, // parallel of FFT grids - const Charge &chr, // charge density - pseudopot_cell_vl &locpp, // local pseudopotentials - pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials Plus_U &dftu, // mohan add 2025-11-06 - VSep* vsep_cell, // U-1/2 method - psi::Psi* kspw_psi, // electronic wave functions + VSep* vsep_cell, // U-1/2 method + psi::Psi* kspw_psi, // electronic wave functions hamilt::HamiltBase* p_hamilt, // hamiltonian - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const Input_para& inp) // input parameters + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp) // input parameters { ModuleBase::TITLE("pw", "setup_pot"); @@ -41,48 +41,48 @@ void pw::setup_pot(const int istep, pelec->init_scf(ucell, para_grid, sf.strucFac, locpp.numeric, ucell.symm, (void*)pw_wfc); - //---------------------------------------------------------- - //! 2) Symmetrize the charge density (rho) - //---------------------------------------------------------- - - //! Symmetry_rho should behind init_scf, because charge should be - //! initialized first. liuyu comment: Symmetry_rho should be - //! located between init_rho and v_of_rho? - Symmetry_rho srho; - for (int is = 0; is < inp.nspin; is++) - { - srho.begin(is, chr, pw_rhod, ucell.symm); - } - - //---------------------------------------------------------- - //! 3) Calculate the effective potential with rho - //---------------------------------------------------------- - //! liuyu move here 2023-10-09 - //! D in uspp need vloc, thus behind init_scf() - //! calculate the effective coefficient matrix - //! for non-local pseudopotential projectors - ModuleBase::matrix veff = pelec->pot->get_eff_v(); - - ppcell.cal_effective_D(veff, pw_rhod, ucell); - - //---------------------------------------------------------- - //! 4) Onsite projectors - //---------------------------------------------------------- - if (PARAM.inp.onsite_radius > 0) - { - auto* onsite_p = projectors::OnsiteProjector::get_instance(); - onsite_p->init(PARAM.inp.orbital_dir, - &ucell, - *(kspw_psi), - kv, - *(pw_wfc), - sf, - PARAM.inp.onsite_radius, - PARAM.globalv.nqx, - PARAM.globalv.dq, - pelec->wg, - pelec->ekb); - } + //---------------------------------------------------------- + //! 2) Symmetrize the charge density (rho) + //---------------------------------------------------------- + + //! Symmetry_rho should behind init_scf, because charge should be + //! initialized first. liuyu comment: Symmetry_rho should be + //! located between init_rho and v_of_rho? + Symmetry_rho srho; + for (int is = 0; is < inp.nspin; is++) + { + srho.begin(is, chr, pw_rhod, ucell.symm); + } + + //---------------------------------------------------------- + //! 3) Calculate the effective potential with rho + //---------------------------------------------------------- + //! liuyu move here 2023-10-09 + //! D in uspp need vloc, thus behind init_scf() + //! calculate the effective coefficient matrix + //! for non-local pseudopotential projectors + ModuleBase::matrix veff = pelec->pot->get_eff_v(); + + ppcell.cal_effective_D(veff, pw_rhod, ucell); + + //---------------------------------------------------------- + //! 4) Onsite projectors + //---------------------------------------------------------- + if (PARAM.inp.onsite_radius > 0) + { + auto* onsite_p = projectors::OnsiteProjector::get_instance(); + onsite_p->init(PARAM.inp.orbital_dir, + &ucell, + *(kspw_psi), + kv, + *(pw_wfc), + sf, + PARAM.inp.onsite_radius, + PARAM.globalv.nqx, + PARAM.globalv.dq, + pelec->wg, + pelec->ekb); + } //---------------------------------------------------------- //! 5) Spin-constrained algorithms @@ -126,77 +126,77 @@ void pw::setup_pot(const int istep, template void pw::setup_pot, base_device::DEVICE_CPU>( const int istep, // ionic step - UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons - const Parallel_Grid ¶_grid, // parallel of FFT grids - const Charge &chr, // charge density - pseudopot_cell_vl &locpp, // local pseudopotentials - pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials Plus_U &dftu, // mohan add 2025-11-06 - VSep* vsep_cell, // U-1/2 method - psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // electronic wave functions + VSep* vsep_cell, // U-1/2 method + psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // electronic wave functions hamilt::HamiltBase* p_hamilt, // hamiltonian - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const Input_para& inp); // input parameters + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters template void pw::setup_pot, base_device::DEVICE_CPU>( const int istep, // ionic step - UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons - const Parallel_Grid ¶_grid, // parallel of FFT grids - const Charge &chr, // charge density - pseudopot_cell_vl &locpp, // local pseudopotentials - pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials Plus_U &dftu, // mohan add 2025-11-06 - VSep* vsep_cell, // U-1/2 method - psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // electronic wave functions + VSep* vsep_cell, // U-1/2 method + psi::Psi, base_device::DEVICE_CPU>* kspw_psi, // electronic wave functions hamilt::HamiltBase* p_hamilt, // hamiltonian - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const Input_para& inp); // input parameters + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters #if ((defined __CUDA) || (defined __ROCM)) template void pw::setup_pot, base_device::DEVICE_GPU>( const int istep, // ionic step - UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons - const Parallel_Grid ¶_grid, // parallel of FFT grids - const Charge &chr, // charge density - pseudopot_cell_vl &locpp, // local pseudopotentials - pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials Plus_U &dftu, // mohan add 2025-11-06 - VSep* vsep_cell, // U-1/2 method - psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // electronic wave functions + VSep* vsep_cell, // U-1/2 method + psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // electronic wave functions hamilt::HamiltBase* p_hamilt, // hamiltonian - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const Input_para& inp); // input parameters + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters template void pw::setup_pot, base_device::DEVICE_GPU>( const int istep, // ionic step - UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons - const Parallel_Grid ¶_grid, // parallel of FFT grids - const Charge &chr, // charge density - pseudopot_cell_vl &locpp, // local pseudopotentials - pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials Plus_U &dftu, // mohan add 2025-11-06 - VSep* vsep_cell, // U-1/2 method - psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // electronic wave functions + VSep* vsep_cell, // U-1/2 method + psi::Psi, base_device::DEVICE_GPU>* kspw_psi, // electronic wave functions hamilt::HamiltBase* p_hamilt, // hamiltonian - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const Input_para& inp); // input parameters + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters #endif diff --git a/source/source_pw/module_pwdft/setup_pot.h b/source/source_pw/module_pwdft/setup_pot.h index d3f08a0f819..de0b4e8f4c8 100644 --- a/source/source_pw/module_pwdft/setup_pot.h +++ b/source/source_pw/module_pwdft/setup_pot.h @@ -15,21 +15,21 @@ namespace pw template void setup_pot(const int istep, - UnitCell& ucell, // unitcell - const K_Vectors &kv, // kpoints + UnitCell& ucell, // unitcell + const K_Vectors &kv, // kpoints Structure_Factor &sf, // structure factors - elecstate::ElecState *pelec, // pointer of electrons - const Parallel_Grid ¶_grid, // parallel of FFT grids - const Charge &chr, // charge density - pseudopot_cell_vl &locpp, // local pseudopotentials - pseudopot_cell_vnl &ppcell, // non-local pseudopotentials + elecstate::ElecState *pelec, // pointer of electrons + const Parallel_Grid ¶_grid, // parallel of FFT grids + const Charge &chr, // charge density + pseudopot_cell_vl &locpp, // local pseudopotentials + pseudopot_cell_vnl &ppcell, // non-local pseudopotentials Plus_U &dftu, // mohan add 2025-11-06 - VSep* vsep_cell, // U-1/2 method - psi::Psi* kspw_psi, // electronic wave functions + VSep* vsep_cell, // U-1/2 method + psi::Psi* kspw_psi, // electronic wave functions hamilt::HamiltBase* p_hamilt, // hamiltonian - ModulePW::PW_Basis_K *pw_wfc, // pw for wfc - const ModulePW::PW_Basis *pw_rhod, // pw for rhod - const Input_para& inp); // input parameters + ModulePW::PW_Basis_K *pw_wfc, // pw for wfc + const ModulePW::PW_Basis *pw_rhod, // pw for rhod + const Input_para& inp); // input parameters } From 736a3ec348121b95499906822c366088e38673b1 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sun, 5 Apr 2026 10:49:56 +0800 Subject: [PATCH 05/20] =?UTF-8?q?refactor(onsite=5Fprojector):=20=E6=8B=86?= =?UTF-8?q?=E5=88=86=E6=89=93=E5=8D=B0=E5=87=BD=E6=95=B0=E5=88=B0=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E6=96=87=E4=BB=B6=EF=BC=8C=E4=BD=BF=E7=94=A8=E4=B8=8B?= =?UTF-8?q?=E5=88=92=E7=BA=BF=E5=91=BD=E5=90=8D=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/source_pw/module_pwdft/CMakeLists.txt | 1 + .../module_pwdft/onsite_proj_print.cpp | 105 ++++++++++++++++++ .../module_pwdft/onsite_proj_print.h | 52 +++++++++ .../module_pwdft/onsite_projector.cpp | 70 +----------- 4 files changed, 163 insertions(+), 65 deletions(-) create mode 100644 source/source_pw/module_pwdft/onsite_proj_print.cpp create mode 100644 source/source_pw/module_pwdft/onsite_proj_print.h diff --git a/source/source_pw/module_pwdft/CMakeLists.txt b/source/source_pw/module_pwdft/CMakeLists.txt index 9e34e9c7b4e..eb8f162437a 100644 --- a/source/source_pw/module_pwdft/CMakeLists.txt +++ b/source/source_pw/module_pwdft/CMakeLists.txt @@ -49,6 +49,7 @@ list(APPEND objects radial_proj.cpp onsite_projector.cpp onsite_proj_tools.cpp + onsite_proj_print.cpp vsep_pw.cpp ) diff --git a/source/source_pw/module_pwdft/onsite_proj_print.cpp b/source/source_pw/module_pwdft/onsite_proj_print.cpp new file mode 100644 index 00000000000..adb6e1246c0 --- /dev/null +++ b/source/source_pw/module_pwdft/onsite_proj_print.cpp @@ -0,0 +1,105 @@ +#include "source_pw/module_pwdft/onsite_proj_print.h" +#include "source_base/formatter.h" + +namespace projectors { +namespace print { + +void print_orb_chg( + const UnitCell* ucell, + const std::vector>& occs, + const std::vector& iat_nh, + const std::vector& atom_labels) +{ + // parameters for orbital charge output + FmtCore fmt_of_chg("%15.4f"); + FmtCore fmt_of_label("%-15s"); + GlobalV::ofs_running << std::endl; + GlobalV::ofs_running << "-------------------------------------------------------------------------------------------" << std::endl; + GlobalV::ofs_running << "Orbital Charge Analysis Charge Mag(x) Mag(y) Mag(z)" << std::endl; + GlobalV::ofs_running << "-------------------------------------------------------------------------------------------" << std::endl; + + // parameters for mag output + std::vector mag_x(ucell->nat, 0.0); + std::vector mag_y(ucell->nat, 0.0); + std::vector mag_z(ucell->nat, 0.0); + const std::vector title = {"Total Magnetism (uB)", "", "", ""}; + const std::vector fmts = {"%-26s", "%20.10f", "%20.10f", "%20.10f"}; + const std::vector orb_names = {"s", "p", "d", "f", "g"}; + FmtTable table(/*titles=*/title, + /*nrows=*/ucell->nat, + /*formats=*/fmts, + /*indent=*/0, + /*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::LEFT}); + // parameters for mag output + int occ_index = 0; + for(int iat=0; iatnat; iat++) + { + const int it = ucell->iat2it[iat]; + std::string atom_label = atom_labels[it]; + int ia = ucell->iat2ia[iat]; + GlobalV::ofs_running << FmtCore::format("%-20s", atom_label+std::to_string(ia+1)) << std::endl; + std::vector sum(4, 0.0); + int current_l = 1; + std::vector charge_mag(4, 0.0); + for(int ih=0; ih& atom_labels, + const std::vector& mag_x, + const std::vector& mag_y, + const std::vector& mag_z) +{ + const std::vector title = {"Total Magnetism (uB)", "", "", ""}; + const std::vector fmts = {"%-26s", "%20.10f", "%20.10f", "%20.10f"}; + FmtTable table(/*titles=*/title, + /*nrows=*/mag_x.size(), + /*formats=*/fmts, + /*indent=*/0, + /*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::LEFT}); + + table << atom_labels << mag_x << mag_y << mag_z; + GlobalV::ofs_running << table.str() << std::endl; +} + +void print_proj_status(int it, int nproj_it) +{ + if(nproj_it == 0) + { + std::cout << "BECP_PW >> No projectors defined for type " << it << std::endl; + } +} + +} // namespace print +} // namespace projectors diff --git a/source/source_pw/module_pwdft/onsite_proj_print.h b/source/source_pw/module_pwdft/onsite_proj_print.h new file mode 100644 index 00000000000..12e5a977432 --- /dev/null +++ b/source/source_pw/module_pwdft/onsite_proj_print.h @@ -0,0 +1,52 @@ +#ifndef MODULEHAMILTPW_ONSITE_PROJ_PRINT_H +#define MODULEHAMILTPW_ONSITE_PROJ_PRINT_H + +#include "source_cell/unitcell.h" +#include "source_base/global_variable.h" +#include +#include +#include + +namespace projectors { +namespace print { + +/** + * @brief Print orbital charge analysis + * + * @param ucell Unit cell pointer + * @param occs Occupation numbers + * @param iat_nh Number of projectors per atom + * @param atom_labels Atom labels + */ +void print_orb_chg( + const UnitCell* ucell, + const std::vector>& occs, + const std::vector& iat_nh, + const std::vector& atom_labels); + +/** + * @brief Print magnetism table + * + * @param atom_labels Atom labels + * @param mag_x Magnetic moment in x direction + * @param mag_y Magnetic moment in y direction + * @param mag_z Magnetic moment in z direction + */ +void print_mag_table( + const std::vector& atom_labels, + const std::vector& mag_x, + const std::vector& mag_y, + const std::vector& mag_z); + +/** + * @brief Print projector initialization status + * + * @param it Atom type index + * @param nproj_it Number of projectors for this type + */ +void print_proj_status(int it, int nproj_it); + +} // namespace print +} // namespace projectors + +#endif // MODULEHAMILTPW_ONSITE_PROJ_PRINT_H diff --git a/source/source_pw/module_pwdft/onsite_projector.cpp b/source/source_pw/module_pwdft/onsite_projector.cpp index 1cb7dc14406..d0574fff5aa 100644 --- a/source/source_pw/module_pwdft/onsite_projector.cpp +++ b/source/source_pw/module_pwdft/onsite_projector.cpp @@ -5,6 +5,7 @@ #include #include #include "source_pw/module_pwdft/onsite_projector.h" +#include "source_pw/module_pwdft/onsite_proj_print.h" #include "source_base/projgen.h" #include "source_base/kernels/math_kernel_op.h" @@ -226,9 +227,9 @@ void projectors::OnsiteProjector::init_proj(const std::string& orbita { const int nproj_it = nproj[it]; this->it2iproj[it].resize(nproj_it); + print::print_proj_status(it, nproj_it); if(nproj_it == 0) { - std::cout << "BECP_PW >> No projectors defined for type " << it << std::endl; continue; } std::ifstream ifs(orbital_dir + orb_files[it]); @@ -573,70 +574,9 @@ void projectors::OnsiteProjector::cal_occupations( const int npool = GlobalV::KPAR * PARAM.inp.bndpar; Parallel_Reduce::reduce_double_allpool(npool, GlobalV::NPROC_IN_POOL, (double*)(&(occs[0])), occs.size()*2); // occ has been reduced and calculate mag - // parameters for orbital charge output - FmtCore fmt_of_chg("%15.4f"); - FmtCore fmt_of_label("%-15s"); - GlobalV::ofs_running << std::endl; - GlobalV::ofs_running << "-------------------------------------------------------------------------------------------" << std::endl; - GlobalV::ofs_running << "Orbital Charge Analysis Charge Mag(x) Mag(y) Mag(z)" << std::endl; - GlobalV::ofs_running << "-------------------------------------------------------------------------------------------" << std::endl; - - // parameters for orbital charge output - // parameters for mag output - std::vector mag_x(this->ucell->nat, 0.0); - std::vector mag_y(this->ucell->nat, 0.0); - std::vector mag_z(this->ucell->nat,0.0); - auto atomLabels = this->ucell->get_atomLabels(); - const std::vector title = {"Total Magnetism (uB)", "", "", ""}; - const std::vector fmts = {"%-26s", "%20.10f", "%20.10f", "%20.10f"}; - const std::vector orb_names = {"s", "p", "d", "f", "g"}; - FmtTable table(/*titles=*/title, - /*nrows=*/this->ucell->nat, - /*formats=*/fmts, - /*indent=*/0, - /*align=*/{/*value*/FmtTable::Align::RIGHT, /*title*/FmtTable::Align::LEFT}); - // parameters for mag output - int occ_index = 0; - for(int iat=0;iatucell->nat;iat++) - { - const int it = this->ucell->iat2it[iat]; - std::string atom_label = atomLabels[it]; - int ia = this->ucell->iat2ia[iat]; - GlobalV::ofs_running << FmtCore::format("%-20s", atom_label+std::to_string(ia+1)) << std::endl; - std::vector sum(4, 0.0); - int current_l = 1; - std::vector charge_mag(4, 0.0); - for(int ih=0;ihiat_nh[iat];ih++) - { - charge_mag[3] += (occs[occ_index] - occs[occ_index + 3]).real(); - charge_mag[1] += (occs[occ_index + 1] + occs[occ_index + 2]).real(); - charge_mag[2] += (occs[occ_index + 1] - occs[occ_index + 2]).imag(); - charge_mag[0] += (occs[occ_index] + occs[occ_index + 3]).real(); - if(ih == current_l * current_l - 1) - { - sum[0] += charge_mag[0]; - sum[1] += charge_mag[1]; - sum[2] += charge_mag[2]; - sum[3] += charge_mag[3]; - GlobalV::ofs_running << FmtCore::format("%20s", orb_names[current_l-1]) - << fmt_of_chg.format(charge_mag[0]) << fmt_of_chg.format(charge_mag[1]) - << fmt_of_chg.format(charge_mag[2]) << fmt_of_chg.format(charge_mag[3]) << std::endl; - current_l++; - charge_mag.assign(4, 0.0); - } - occ_index += 4; - } - mag_x[iat] = sum[1]; - mag_y[iat] = sum[2]; - mag_z[iat] = sum[3]; - GlobalV::ofs_running << FmtCore::format("%20s", std::string("Sum")) << "" - << fmt_of_chg.format(sum[0]) << fmt_of_chg.format(sum[1]) - << fmt_of_chg.format(sum[2]) << fmt_of_chg.format(sum[3]) << std::endl; - } - GlobalV::ofs_running << "-------------------------------------------------------------------------------------------" << std::endl; - GlobalV::ofs_running << std::endl; - table << atomLabels << mag_x << mag_y << mag_z; - GlobalV::ofs_running << table.str() << std::endl; + // Print orbital charge analysis + auto atom_labels = this->ucell->get_atomLabels(); + print::print_orb_chg(this->ucell, occs, this->iat_nh, atom_labels); // print charge ModuleBase::timer::end("OnsiteProj", "cal_occupation"); From 9c99a37a4e1a262904e8ad7cd4d13c9ca3a8a767 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sun, 5 Apr 2026 11:01:15 +0800 Subject: [PATCH 06/20] =?UTF-8?q?refactor(onsite=5Fprojector):=20=E6=9B=B4?= =?UTF-8?q?=E6=96=B0=20Makefile.Objects=EF=BC=8C=E6=B7=BB=E5=8A=A0=20onsit?= =?UTF-8?q?e=5Fproj=5Fprint.o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/Makefile.Objects | 1 + 1 file changed, 1 insertion(+) diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 9f5785664f8..30b9206a720 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -760,6 +760,7 @@ OBJS_SRCPW=H_Ewald_pw.o\ sto_dos.o\ onsite_projector.o\ onsite_proj_tools.o\ + onsite_proj_print.o\ vsep_pw.o OBJS_VDW=vdw.o\ From 4f52c6b4803524a8a5006b4908d9dae92270cd58 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sun, 5 Apr 2026 14:40:44 +0800 Subject: [PATCH 07/20] change onsite_projector to onsite_proj --- source/Makefile.Objects | 2 +- source/source_esolver/esolver_ks_pw.cpp | 2 +- source/source_io/module_ctrl/ctrl_output_pw.cpp | 2 +- source/source_lcao/module_deltaspin/cal_mw.cpp | 2 +- source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp | 2 +- source/source_lcao/module_dftu/dftu_pw.cpp | 2 +- source/source_pw/module_pwdft/CMakeLists.txt | 2 +- source/source_pw/module_pwdft/forces_onsite.cpp | 2 +- .../module_pwdft/{onsite_projector.cpp => onsite_proj.cpp} | 2 +- .../module_pwdft/{onsite_projector.h => onsite_proj.h} | 0 source/source_pw/module_pwdft/op_pw_proj.cpp | 2 +- source/source_pw/module_pwdft/setup_pot.cpp | 2 +- source/source_pw/module_pwdft/stress_onsite.cpp | 2 +- 13 files changed, 12 insertions(+), 12 deletions(-) rename source/source_pw/module_pwdft/{onsite_projector.cpp => onsite_proj.cpp} (99%) rename source/source_pw/module_pwdft/{onsite_projector.h => onsite_proj.h} (100%) diff --git a/source/Makefile.Objects b/source/Makefile.Objects index 30b9206a720..9ad0ebe5094 100644 --- a/source/Makefile.Objects +++ b/source/Makefile.Objects @@ -758,7 +758,7 @@ OBJS_SRCPW=H_Ewald_pw.o\ sto_tool.o\ sto_elecond.o\ sto_dos.o\ - onsite_projector.o\ + onsite_proj.o\ onsite_proj_tools.o\ onsite_proj_print.o\ vsep_pw.o diff --git a/source/source_esolver/esolver_ks_pw.cpp b/source/source_esolver/esolver_ks_pw.cpp index 2efc2847a86..7eccfb5d61e 100644 --- a/source/source_esolver/esolver_ks_pw.cpp +++ b/source/source_esolver/esolver_ks_pw.cpp @@ -11,7 +11,7 @@ #include "source_hsolver/kernels/hegvd_op.h" #include "source_io/module_parameter/parameter.h" #include "source_lcao/module_deltaspin/spin_constrain.h" -#include "source_pw/module_pwdft/onsite_projector.h" +#include "source_pw/module_pwdft/onsite_proj.h" #include "source_lcao/module_dftu/dftu.h" #include "source_pw/module_pwdft/vsep_pw.h" #include "source_pw/module_pwdft/hamilt_pw.h" diff --git a/source/source_io/module_ctrl/ctrl_output_pw.cpp b/source/source_io/module_ctrl/ctrl_output_pw.cpp index 460afb9fa28..f084854238b 100644 --- a/source/source_io/module_ctrl/ctrl_output_pw.cpp +++ b/source/source_io/module_ctrl/ctrl_output_pw.cpp @@ -3,7 +3,7 @@ #include "../module_wf/write_wfc_pw.h" // use write_wfc_pw #include "../module_dos/write_dos_pw.h" // use write_dos_pw #include "../module_wannier/to_wannier90_pw.h" // wannier90 interface -#include "source_pw/module_pwdft/onsite_projector.h" // use projector +#include "source_pw/module_pwdft/onsite_proj.h" // use projector #include "../module_bessel/numerical_basis.h" #include "../module_bessel/numerical_descriptor.h" #include "../module_dos/cal_ldos.h" diff --git a/source/source_lcao/module_deltaspin/cal_mw.cpp b/source/source_lcao/module_deltaspin/cal_mw.cpp index 27d03dc12eb..0482f8f0709 100644 --- a/source/source_lcao/module_deltaspin/cal_mw.cpp +++ b/source/source_lcao/module_deltaspin/cal_mw.cpp @@ -4,7 +4,7 @@ #include "source_base/name_angular.h" #include "source_base/tool_title.h" #include "source_base/timer.h" -#include "source_pw/module_pwdft/onsite_projector.h" +#include "source_pw/module_pwdft/onsite_proj.h" #include "spin_constrain.h" #include "source_io/module_parameter/parameter.h" #ifdef __LCAO diff --git a/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp b/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp index 012e7d39e05..92794fbee27 100644 --- a/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp +++ b/source/source_lcao/module_deltaspin/cal_mw_from_lambda.cpp @@ -3,7 +3,7 @@ #include "source_hsolver/diago_iter_assist.h" #include "source_io/module_parameter/parameter.h" #include "spin_constrain.h" -#include "source_pw/module_pwdft/onsite_projector.h" +#include "source_pw/module_pwdft/onsite_proj.h" #include "source_base/parallel_reduce.h" #include "source_base/kernels/math_kernel_op.h" #include "source_hsolver/hsolver_lcao.h" diff --git a/source/source_lcao/module_dftu/dftu_pw.cpp b/source/source_lcao/module_dftu/dftu_pw.cpp index c66f67389bf..7a1a9bac3a6 100644 --- a/source/source_lcao/module_dftu/dftu_pw.cpp +++ b/source/source_lcao/module_dftu/dftu_pw.cpp @@ -1,5 +1,5 @@ #include "dftu.h" -#include "source_pw/module_pwdft/onsite_projector.h" +#include "source_pw/module_pwdft/onsite_proj.h" #include "source_base/parallel_reduce.h" #include "source_io/module_parameter/parameter.h" #include "source_base/timer.h" diff --git a/source/source_pw/module_pwdft/CMakeLists.txt b/source/source_pw/module_pwdft/CMakeLists.txt index eb8f162437a..dec6cd86d8e 100644 --- a/source/source_pw/module_pwdft/CMakeLists.txt +++ b/source/source_pw/module_pwdft/CMakeLists.txt @@ -47,7 +47,7 @@ list(APPEND objects fs_nonlocal_tools.cpp fs_kin_tools.cpp radial_proj.cpp - onsite_projector.cpp + onsite_proj.cpp onsite_proj_tools.cpp onsite_proj_print.cpp vsep_pw.cpp diff --git a/source/source_pw/module_pwdft/forces_onsite.cpp b/source/source_pw/module_pwdft/forces_onsite.cpp index c69ab81c6b8..afa5cfcfe94 100644 --- a/source/source_pw/module_pwdft/forces_onsite.cpp +++ b/source/source_pw/module_pwdft/forces_onsite.cpp @@ -1,7 +1,7 @@ #include "forces.h" #include "source_base/timer.h" #include "source_base/tool_title.h" -#include "source_pw/module_pwdft/onsite_projector.h" +#include "source_pw/module_pwdft/onsite_proj.h" #include "source_pw/module_pwdft/kernels/force_op.h" #include "source_io/module_parameter/parameter.h" #include "source_lcao/module_dftu/dftu.h" diff --git a/source/source_pw/module_pwdft/onsite_projector.cpp b/source/source_pw/module_pwdft/onsite_proj.cpp similarity index 99% rename from source/source_pw/module_pwdft/onsite_projector.cpp rename to source/source_pw/module_pwdft/onsite_proj.cpp index d0574fff5aa..82bd3516fd4 100644 --- a/source/source_pw/module_pwdft/onsite_projector.cpp +++ b/source/source_pw/module_pwdft/onsite_proj.cpp @@ -4,7 +4,7 @@ #include #include #include -#include "source_pw/module_pwdft/onsite_projector.h" +#include "source_pw/module_pwdft/onsite_proj.h" #include "source_pw/module_pwdft/onsite_proj_print.h" #include "source_base/projgen.h" diff --git a/source/source_pw/module_pwdft/onsite_projector.h b/source/source_pw/module_pwdft/onsite_proj.h similarity index 100% rename from source/source_pw/module_pwdft/onsite_projector.h rename to source/source_pw/module_pwdft/onsite_proj.h diff --git a/source/source_pw/module_pwdft/op_pw_proj.cpp b/source/source_pw/module_pwdft/op_pw_proj.cpp index 00f7d18e24d..8c7cddfc89c 100644 --- a/source/source_pw/module_pwdft/op_pw_proj.cpp +++ b/source/source_pw/module_pwdft/op_pw_proj.cpp @@ -5,7 +5,7 @@ #include "source_base/tool_quit.h" #include "source_lcao/module_deltaspin/spin_constrain.h" #include "source_lcao/module_dftu/dftu.h" -#include "source_pw/module_pwdft/onsite_projector.h" +#include "source_pw/module_pwdft/onsite_proj.h" #include "source_pw/module_pwdft/kernels/onsite_op.h" diff --git a/source/source_pw/module_pwdft/setup_pot.cpp b/source/source_pw/module_pwdft/setup_pot.cpp index dd333f6d2f7..4a194bc0483 100644 --- a/source/source_pw/module_pwdft/setup_pot.cpp +++ b/source/source_pw/module_pwdft/setup_pot.cpp @@ -2,7 +2,7 @@ #include "source_estate/module_charge/symmetry_rho.h" #include "source_lcao/module_deltaspin/spin_constrain.h" -#include "source_pw/module_pwdft/onsite_projector.h" +#include "source_pw/module_pwdft/onsite_proj.h" #include "source_lcao/module_dftu/dftu.h" #include "source_pw/module_pwdft/vsep_pw.h" diff --git a/source/source_pw/module_pwdft/stress_onsite.cpp b/source/source_pw/module_pwdft/stress_onsite.cpp index 5d0a078acb0..6c100ab09d0 100644 --- a/source/source_pw/module_pwdft/stress_onsite.cpp +++ b/source/source_pw/module_pwdft/stress_onsite.cpp @@ -1,6 +1,6 @@ #include "source_base/module_device/device.h" #include "source_base/timer.h" -#include "source_pw/module_pwdft/onsite_projector.h" +#include "source_pw/module_pwdft/onsite_proj.h" #include "source_io/module_parameter/parameter.h" #include "source_lcao/module_dftu/dftu.h" #include "source_lcao/module_deltaspin/spin_constrain.h" From 551091b1332003777a3c2a3479e25368c461fc72 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sun, 5 Apr 2026 14:44:29 +0800 Subject: [PATCH 08/20] refactor(pwdft): update onsite_proj_tools.h --- source/source_pw/module_pwdft/onsite_proj_tools.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/source/source_pw/module_pwdft/onsite_proj_tools.h b/source/source_pw/module_pwdft/onsite_proj_tools.h index 586a45fd948..31263849a29 100644 --- a/source/source_pw/module_pwdft/onsite_proj_tools.h +++ b/source/source_pw/module_pwdft/onsite_proj_tools.h @@ -70,8 +70,11 @@ class Onsite_Proj_tools void cal_dbecp_f(int ik, int npm, int ipol); void cal_force_dftu(int ik, int npm, FPTYPE* force, const int* orbital_corr, const std::complex* vu, const int size_vu, const FPTYPE* h_wg); + void cal_force_dspin(int ik, int npm, FPTYPE* force, const ModuleBase::Vector3* lambda, const FPTYPE* h_wg); + void cal_stress_dftu(int ik, int npm, FPTYPE* stress, const int* orbital_corr, const std::complex* vu, const int size_vu, const FPTYPE* h_wg); + void cal_stress_dspin(int ik, int npm, FPTYPE* stress, const ModuleBase::Vector3* lambda, const FPTYPE* h_wg); @@ -213,4 +216,4 @@ class Onsite_Proj_tools } // namespace hamilt -#endif \ No newline at end of file +#endif From c0fac9f80c5653db9a7f7ee03384dc051f14c6d5 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sun, 5 Apr 2026 14:45:53 +0800 Subject: [PATCH 09/20] refactor(pwdft): format function parameters in onsite_proj_tools.h --- .../module_pwdft/onsite_proj_tools.h | 96 +++++++++++++------ 1 file changed, 65 insertions(+), 31 deletions(-) diff --git a/source/source_pw/module_pwdft/onsite_proj_tools.h b/source/source_pw/module_pwdft/onsite_proj_tools.h index 31263849a29..9f75e0611eb 100644 --- a/source/source_pw/module_pwdft/onsite_proj_tools.h +++ b/source/source_pw/module_pwdft/onsite_proj_tools.h @@ -30,28 +30,32 @@ template class Onsite_Proj_tools { public: - Onsite_Proj_tools(const pseudopot_cell_vnl* nlpp_in, - const UnitCell* ucell_in, - const psi::Psi, Device>* psi_in, - const K_Vectors* kv_in, - const ModulePW::PW_Basis_K* wfc_basis_in, - const Structure_Factor* sf_in, - const ModuleBase::matrix& wg, - const ModuleBase::matrix& ekb); + Onsite_Proj_tools( + const pseudopot_cell_vnl* nlpp_in, + const UnitCell* ucell_in, + const psi::Psi, Device>* psi_in, + const K_Vectors* kv_in, + const ModulePW::PW_Basis_K* wfc_basis_in, + const Structure_Factor* sf_in, + const ModuleBase::matrix& wg, + const ModuleBase::matrix& ekb + ); // a more general constructor is in the following - Onsite_Proj_tools(const std::vector& nproj, // number of projectors for each atom type - const std::vector& lproj, - const ModuleBase::realArray& tab, // radials' spherical bessel transform - const ModuleBase::matrix& nhtol, - std::complex* vkb_buf, - const UnitCell* ucell_in, - const psi::Psi, Device>* psi_in, - const K_Vectors* kv_in, - const ModulePW::PW_Basis_K* wfc_basis_in, - const Structure_Factor* sf_in, - const ModuleBase::matrix& wg, - const ModuleBase::matrix& ekb); + Onsite_Proj_tools( + const std::vector& nproj, // number of projectors for each atom type + const std::vector& lproj, + const ModuleBase::realArray& tab, // radials' spherical bessel transform + const ModuleBase::matrix& nhtol, + std::complex* vkb_buf, + const UnitCell* ucell_in, + const psi::Psi, Device>* psi_in, + const K_Vectors* kv_in, + const ModulePW::PW_Basis_K* wfc_basis_in, + const Structure_Factor* sf_in, + const ModuleBase::matrix& wg, + const ModuleBase::matrix& ekb + ); ~Onsite_Proj_tools(); @@ -69,13 +73,41 @@ class Onsite_Proj_tools */ void cal_dbecp_f(int ik, int npm, int ipol); - void cal_force_dftu(int ik, int npm, FPTYPE* force, const int* orbital_corr, const std::complex* vu, const int size_vu, const FPTYPE* h_wg); - - void cal_force_dspin(int ik, int npm, FPTYPE* force, const ModuleBase::Vector3* lambda, const FPTYPE* h_wg); - - void cal_stress_dftu(int ik, int npm, FPTYPE* stress, const int* orbital_corr, const std::complex* vu, const int size_vu, const FPTYPE* h_wg); - - void cal_stress_dspin(int ik, int npm, FPTYPE* stress, const ModuleBase::Vector3* lambda, const FPTYPE* h_wg); + void cal_force_dftu( + int ik, + int npm, + FPTYPE* force, + const int* orbital_corr, + const std::complex* vu, + const int size_vu, + const FPTYPE* h_wg + ); + + void cal_force_dspin( + int ik, + int npm, + FPTYPE* force, + const ModuleBase::Vector3* lambda, + const FPTYPE* h_wg + ); + + void cal_stress_dftu( + int ik, + int npm, + FPTYPE* stress, + const int* orbital_corr, + const std::complex* vu, + const int size_vu, + const FPTYPE* h_wg + ); + + void cal_stress_dspin( + int ik, + int npm, + FPTYPE* stress, + const ModuleBase::Vector3* lambda, + const FPTYPE* h_wg + ); std::complex* get_becp() const { return becp; } @@ -85,10 +117,12 @@ class Onsite_Proj_tools /** * @brief allocate the memory for the variables */ - void allocate_memory(const ModuleBase::matrix& wg, - const ModuleBase::matrix& ekb, - const std::vector& nproj, - const std::vector& nch); + void allocate_memory( + const ModuleBase::matrix& wg, + const ModuleBase::matrix& ekb, + const std::vector& nproj, + const std::vector& nch + ); /** * @brief delete the memory for the variables */ From 81c016acfd8b91508db0e1576c85a16d23ceb182 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sun, 5 Apr 2026 15:46:16 +0800 Subject: [PATCH 10/20] update stress_onsite --- .../source_pw/module_pwdft/stress_onsite.cpp | 158 ++++++++++++------ 1 file changed, 111 insertions(+), 47 deletions(-) diff --git a/source/source_pw/module_pwdft/stress_onsite.cpp b/source/source_pw/module_pwdft/stress_onsite.cpp index 6c100ab09d0..e68383907c8 100644 --- a/source/source_pw/module_pwdft/stress_onsite.cpp +++ b/source/source_pw/module_pwdft/stress_onsite.cpp @@ -5,34 +5,66 @@ #include "source_lcao/module_dftu/dftu.h" #include "source_lcao/module_deltaspin/spin_constrain.h" #include "stress_func.h" -// calculate the nonlocal pseudopotential stress in PW + +/** + * @brief Calculate the nonlocal pseudopotential stress in PW basis + * + * This function computes the onsite contribution to the stress tensor + * including DFT+U and spin constraint effects. + * + * @param sigma Stress tensor to be updated + * @param wg Weight matrix for k-points + * @param wfc_basis Plane wave basis for wavefunctions + * @param ucell_in Unit cell information + * @param dftu DFT+U parameters + * @param psi_in Wavefunction object (only used for null check) + * @param p_symm Symmetry object for stress symmetrization + */ template -void Stress_Func::stress_onsite(ModuleBase::matrix& sigma, - const ModuleBase::matrix& wg, - const ModulePW::PW_Basis_K* wfc_basis, - const UnitCell& ucell_in, - const Plus_U &dftu, // mohan add 2025-11-06 - const psi::Psi , Device>* psi_in, - ModuleSymmetry::Symmetry* p_symm) +void Stress_Func::stress_onsite( + ModuleBase::matrix& sigma, + const ModuleBase::matrix& wg, + const ModulePW::PW_Basis_K* wfc_basis, + const UnitCell& ucell_in, + const Plus_U &dftu, // mohan add 2025-11-06 + const void* psi_in, + ModuleSymmetry::Symmetry* p_symm +) { ModuleBase::TITLE("Stress", "stress_onsite"); - if(psi_in == nullptr || wfc_basis == nullptr) + + // Early return if required pointers are null + if (psi_in == nullptr || wfc_basis == nullptr) { return; } + + // Check if cell volume is valid (non-zero) + if (ucell_in.omega <= 0.0) + { + ModuleBase::WARNING_QUIT("stress_onsite.cpp", "Cell volume is zero or negative, cannot calculate stress"); + } + ModuleBase::timer::start("Stress", "stress_onsite"); + // Allocate device memory for stress calculation FPTYPE* stress_device = nullptr; resmem_var_op()(stress_device, 9); setmem_var_op()(stress_device, 0, 9); - std::vector sigma_onsite(9, 0.0); + + // Host memory for stress storage + std::vector sigma_onsite(9, 0.0); - auto* onsite_p = projectors::OnsiteProjector::get_instance(); + // Get onsite projector instance + auto* onsite_projector = projectors::OnsiteProjector::get_instance(); + auto* fs_tools = onsite_projector->get_fs_tools(); const int nks = wfc_basis->nks; + + // Loop over all k-points for (int ik = 0; ik < nks; ik++) { - // skip zero weights to speed up + // Determine number of occupied bands (skip zero weights) int nbands_occ = wg.nc; while (wg(ik, nbands_occ - 1) == 0.0) { @@ -42,74 +74,106 @@ void Stress_Func::stress_onsite(ModuleBase::matrix& sigma, break; } } - const int npm = nbands_occ; + const int num_occupied_bands = nbands_occ; - // calculate becp = for all beta functions - onsite_p->get_fs_tools()->cal_becp(ik, npm); + // Calculate becp = for all beta functions + fs_tools->cal_becp(ik, num_occupied_bands); - // calculate dbecp = for all beta functions - // calculate stress = \sum * * D_{ij} + // Calculate stress contributions for each tensor component for (int ipol = 0; ipol < 3; ipol++) { for (int jpol = 0; jpol <= ipol; jpol++) { - FPTYPE* stress_device_tmp = stress_device + (ipol * 3 + jpol); - onsite_p->get_fs_tools()->cal_dbecp_s(ik, npm, ipol, jpol); - if(PARAM.inp.dft_plus_u) + // Get pointer to current stress component + FPTYPE* stress_component = stress_device + (ipol * 3 + jpol); + + // Calculate dbecp_s = + fs_tools->cal_dbecp_s(ik, num_occupied_bands, ipol, jpol); + + // Add DFT+U contribution if enabled + if (PARAM.inp.dft_plus_u) { - onsite_p->get_fs_tools()->cal_stress_dftu(ik, - npm, - stress_device_tmp, - dftu.orbital_corr.data(), - dftu.get_eff_pot_pw(0), - dftu.get_size_eff_pot_pw(), - wg.c); + fs_tools->cal_stress_dftu( + ik, + num_occupied_bands, + stress_component, + dftu.orbital_corr.data(), + dftu.get_eff_pot_pw(0), + dftu.get_size_eff_pot_pw(), + wg.c + ); } - if(PARAM.inp.sc_mag_switch) + + // Add spin constraint contribution if enabled + if (PARAM.inp.sc_mag_switch) { - spinconstrain::SpinConstrain>& sc = + // Get spin constraint instance + spinconstrain::SpinConstrain>& spin_constrain = spinconstrain::SpinConstrain>::getScInstance(); - const std::vector>& lambda = sc.get_sc_lambda(); - onsite_p->get_fs_tools()->cal_stress_dspin(ik, npm, stress_device_tmp, lambda.data(), wg.c); + + // Get lambda parameters + const std::vector>& lambda = spin_constrain.get_sc_lambda(); + + // Calculate spin constraint stress contribution + fs_tools->cal_stress_dspin( + ik, + num_occupied_bands, + stress_component, + lambda.data(), + wg.c + ); } } } } - // transfer stress from device to host + // Transfer stress from device to host syncmem_var_d2h_op()(sigma_onsite.data(), stress_device, 9); delmem_var_op()(stress_device); - // sum up forcenl from all processors - for (int l = 0; l < 3; l++) + // Step 1: Reduce stress contributions from all processors + for (int i = 0; i < 3; i++) { - for (int m = 0; m < 3; m++) + for (int j = 0; j < 3; j++) { - if (m > l) - { - sigma_onsite[l * 3 + m] = sigma_onsite[m * 3 + l]; - } - Parallel_Reduce::reduce_all(sigma_onsite[l * 3 + m]); // qianrui fix a bug for kpar > 1 + const int idx = i * 3 + j; + Parallel_Reduce::reduce_all(sigma_onsite[idx]); // qianrui fix a bug for kpar > 1 + } + } + + // Step 2: Rescale stress with 1/omega + const double inv_omega = 1.0 / ucell_in.omega; + for (int i = 0; i < 3; i++) + { + for (int j = 0; j < 3; j++) + { + const int idx = i * 3 + j; + sigma_onsite[idx] *= inv_omega; } } - // rescale the stress with 1/omega - for (int ipol = 0; ipol < 3; ipol++) + // Step 3: Handle symmetry for upper triangular elements + for (int i = 0; i < 3; i++) { - for (int jpol = 0; jpol < 3; jpol++) + for (int j = i + 1; j < 3; j++) { - sigma_onsite[ipol * 3 + jpol] *= 1.0 / ucell_in.omega; + const int idx = i * 3 + j; + const int transposed_idx = j * 3 + i; + sigma_onsite[idx] = sigma_onsite[transposed_idx]; } } - for (int ipol = 0; ipol < 3; ipol++) + // Step 4: Assign stress values to output matrix + for (int i = 0; i < 3; i++) { - for (int jpol = 0; jpol < 3; jpol++) + for (int j = 0; j < 3; j++) { - sigma(ipol, jpol) = sigma_onsite[ipol * 3 + jpol]; + const int idx = i * 3 + j; + sigma(i, j) = sigma_onsite[idx]; } } + // Symmetrize stress tensor if symmetry is enabled if (ModuleSymmetry::Symmetry::symm_flag == 1) { p_symm->symmetrize_mat3(sigma, ucell_in.lat); From 414fcf9daed1e7c92a521a0070eaa6f1b1018b6f Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sun, 5 Apr 2026 16:58:15 +0800 Subject: [PATCH 11/20] add example 99_SCF_DSPIN --- tests/03_NAO_multik/99_SCF_DPSIN/INPUT | 38 ++++++++++++++++++++++++ tests/03_NAO_multik/99_SCF_DPSIN/KPT | 4 +++ tests/03_NAO_multik/99_SCF_DPSIN/STRU | 21 +++++++++++++ tests/03_NAO_multik/99_SCF_DPSIN/run.sh | 19 ++++++++++++ tests/03_NAO_multik/99_SCF_DPSIN/sc.json | 23 ++++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 tests/03_NAO_multik/99_SCF_DPSIN/INPUT create mode 100644 tests/03_NAO_multik/99_SCF_DPSIN/KPT create mode 100644 tests/03_NAO_multik/99_SCF_DPSIN/STRU create mode 100644 tests/03_NAO_multik/99_SCF_DPSIN/run.sh create mode 100644 tests/03_NAO_multik/99_SCF_DPSIN/sc.json diff --git a/tests/03_NAO_multik/99_SCF_DPSIN/INPUT b/tests/03_NAO_multik/99_SCF_DPSIN/INPUT new file mode 100644 index 00000000000..499059e1ff3 --- /dev/null +++ b/tests/03_NAO_multik/99_SCF_DPSIN/INPUT @@ -0,0 +1,38 @@ +INPUT_PARAMETERS +pseudo_dir ../../../tests/PP_ORB +orbital_dir ../../../tests/PP_ORB +calculation scf +ecutwfc 20 +scf_thr 1.0e-5 +scf_nmax 20 + +smearing_method gaussian +smearing_sigma 0.07 + +mixing_type pulay +mixing_ndim 10 +mixing_beta 0.1 + +ks_solver genelpa +basis_type lcao +symmetry 0 + +noncolin true +nspin 4 +lspinorb true +out_mul true +cal_force 1 +cal_stress 1 + +#deltaspin +sc_mag_switch 1 # switch to control spin-constrained DFT +decay_grad_switch 1 # switch to control gradient break condition +sc_thr 1e-7 # Convergence criterion of spin-constrained iteration (RMS) in uB +nsc 150 # Maximal number of spin-constrained iteration +nsc_min 2 # Minimum number of spin-constrained iteration +sc_scf_nmin 2 # Minimum number of outer scf loop before initializing lambda loop +alpha_trial 0.01 # Initial trial step size for lambda in eV/uB^2 +sccut 3 # Maximal step size for lambda in eV/uB +sc_drop_thr 1.0e-2 # Convergence criterion ratio of lambda iteration in Spin-constrained DFT +sc_scf_thr 1.0e-4 # Density error threshold for inner loop of spin-constrained SCF +#sc_file sc.json # diff --git a/tests/03_NAO_multik/99_SCF_DPSIN/KPT b/tests/03_NAO_multik/99_SCF_DPSIN/KPT new file mode 100644 index 00000000000..4fd38968a05 --- /dev/null +++ b/tests/03_NAO_multik/99_SCF_DPSIN/KPT @@ -0,0 +1,4 @@ +K_POINTS +0 +Gamma +2 2 1 0 0 0 diff --git a/tests/03_NAO_multik/99_SCF_DPSIN/STRU b/tests/03_NAO_multik/99_SCF_DPSIN/STRU new file mode 100644 index 00000000000..ae2b187b97b --- /dev/null +++ b/tests/03_NAO_multik/99_SCF_DPSIN/STRU @@ -0,0 +1,21 @@ +ATOMIC_SPECIES +Fe 55.845 Fe.upf + +NUMERICAL_ORBITAL +Fe_gga_6au_100Ry_2s1p1d.orb + +LATTICE_CONSTANT +1.889725989 + +LATTICE_VECTORS + 2.8328152400000000 0.0000000000000000 0.0000000000000000 + 0.0000000000000000 2.8328152400000000 0.0000000000000000 + 0.0000000000000000 0.0000000000000000 2.8328152400000000 +ATOMIC_POSITIONS +Direct + +Fe +1.0 +2 +0.0 0.0 0.0 mag 5 angle1 90 angle2 0 +0.5 0.5 0.5 mag 5 angle1 90 angle2 0 diff --git a/tests/03_NAO_multik/99_SCF_DPSIN/run.sh b/tests/03_NAO_multik/99_SCF_DPSIN/run.sh new file mode 100644 index 00000000000..5a57ee041d3 --- /dev/null +++ b/tests/03_NAO_multik/99_SCF_DPSIN/run.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +ABACUS_PATH=$(awk -F "=" '$1=="ABACUS_PATH"{print $2}' ../../SETENV) +ABACUS_NPROCS=$(awk -F "=" '$1=="ABACUS_NPROCS"{print $2}' ../../SETENV) +ABACUS_THREADS=$(awk -F "=" '$1=="ABACUS_THREADS"{print $2}' ../../SETENV) + +OMP_NUM_THREADS=${ABACUS_THREADS} mpirun -np ${ABACUS_NPROCS} ${ABACUS_PATH} | tee scf.output + +if [[ ! -f scf.output ]] || + [[ ! -f OUT.ABACUS/running_scf.log ]] || + [[ ! -f OUT.ABACUS/mulliken.txt ]] || + [[ ! ( "$(tail -1 OUT.ABACUS/running_scf.log)" == " Total Time :"* ) ]] +then + echo "job is failed!" + exit 1 +else + echo "job is successed!" + exit 0 +fi diff --git a/tests/03_NAO_multik/99_SCF_DPSIN/sc.json b/tests/03_NAO_multik/99_SCF_DPSIN/sc.json new file mode 100644 index 00000000000..e1fa3e088bd --- /dev/null +++ b/tests/03_NAO_multik/99_SCF_DPSIN/sc.json @@ -0,0 +1,23 @@ +[ + { + "element": "Fe", + "itype": 0, + "ScDecayGrad": 0.9, + "ScAtomData": [ + { + "index": 0, + "lambda": [0, 0, 0], + "target_mag": [2.386, 0.0, 0.0], + "constrain": [1,1,1] + }, + { + "index": 1, + "lambda": [0, 0, 0], + "target_mag_val": 2.386, + "target_mag_angle1": 90.0, + "target_mag_angle2": 0.0, + "constrain": [1,1,1] + } + ] + } +] From ac1e817d842105436a5abf031eb52f00d5ebc1b5 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sun, 5 Apr 2026 16:58:46 +0800 Subject: [PATCH 12/20] remove run.sh --- tests/03_NAO_multik/99_SCF_DPSIN/run.sh | 19 ------------------- 1 file changed, 19 deletions(-) delete mode 100644 tests/03_NAO_multik/99_SCF_DPSIN/run.sh diff --git a/tests/03_NAO_multik/99_SCF_DPSIN/run.sh b/tests/03_NAO_multik/99_SCF_DPSIN/run.sh deleted file mode 100644 index 5a57ee041d3..00000000000 --- a/tests/03_NAO_multik/99_SCF_DPSIN/run.sh +++ /dev/null @@ -1,19 +0,0 @@ -#!/bin/bash - -ABACUS_PATH=$(awk -F "=" '$1=="ABACUS_PATH"{print $2}' ../../SETENV) -ABACUS_NPROCS=$(awk -F "=" '$1=="ABACUS_NPROCS"{print $2}' ../../SETENV) -ABACUS_THREADS=$(awk -F "=" '$1=="ABACUS_THREADS"{print $2}' ../../SETENV) - -OMP_NUM_THREADS=${ABACUS_THREADS} mpirun -np ${ABACUS_NPROCS} ${ABACUS_PATH} | tee scf.output - -if [[ ! -f scf.output ]] || - [[ ! -f OUT.ABACUS/running_scf.log ]] || - [[ ! -f OUT.ABACUS/mulliken.txt ]] || - [[ ! ( "$(tail -1 OUT.ABACUS/running_scf.log)" == " Total Time :"* ) ]] -then - echo "job is failed!" - exit 1 -else - echo "job is successed!" - exit 0 -fi From 5728fcd2e15277cc3969cecf53851f9fb0c62dd3 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sun, 5 Apr 2026 16:59:34 +0800 Subject: [PATCH 13/20] remove dspin constrain, remove the GPU/CPU codes for stress in onsite --- .../read_input_item_other.cpp | 8 +- .../module_pwdft/onsite_proj_tools.cpp | 155 ++++++++++++------ .../module_pwdft/onsite_proj_tools.h | 6 +- source/source_pw/module_pwdft/stress_func.h | 6 +- .../source_pw/module_pwdft/stress_onsite.cpp | 47 ++++-- 5 files changed, 141 insertions(+), 81 deletions(-) diff --git a/source/source_io/module_parameter/read_input_item_other.cpp b/source/source_io/module_parameter/read_input_item_other.cpp index 1ec1bf255a2..d929b0ee7f5 100644 --- a/source/source_io/module_parameter/read_input_item_other.cpp +++ b/source/source_io/module_parameter/read_input_item_other.cpp @@ -28,10 +28,10 @@ void ReadInput::item_others() item.check_value = [](const Input_Item& item, const Parameter& para) { if (para.input.sc_mag_switch) { - ModuleBase::WARNING_QUIT("ReadInput", - "This feature is not stable yet and might lead to " - "erroneous results.\n" - " Please wait for the official release version."); +// ModuleBase::WARNING_QUIT("ReadInput", +// "This feature is not stable yet and might lead to " +// "erroneous results.\n" +// " Please wait for the official release version."); // if (para.input.nspin != 4 && para.input.nspin != 2) // { // ModuleBase::WARNING_QUIT("ReadInput", "nspin must be 2 or diff --git a/source/source_pw/module_pwdft/onsite_proj_tools.cpp b/source/source_pw/module_pwdft/onsite_proj_tools.cpp index bae408a185b..51c07964003 100644 --- a/source/source_pw/module_pwdft/onsite_proj_tools.cpp +++ b/source/source_pw/module_pwdft/onsite_proj_tools.cpp @@ -911,14 +911,16 @@ void Onsite_Proj_tools::cal_force_dspin(int ik, } template -void Onsite_Proj_tools::cal_stress_dftu(int ik, - int npm, - FPTYPE* stress, - const int* orbital_corr, - const std::complex* vu, - const int size_vu, - const FPTYPE* h_wg) +std::vector Onsite_Proj_tools::cal_stress_dftu(int ik, + int npm, + const int* orbital_corr, + const std::complex* vu, + const int size_vu, + const FPTYPE* h_wg) { + // Create stress vector to store results + std::vector stress(9, 0.0); + int* orbital_corr_tmp = nullptr; std::complex* vu_tmp = nullptr; #if defined(__CUDA) || defined(__ROCM) @@ -929,6 +931,32 @@ void Onsite_Proj_tools::cal_stress_dftu(int ik, resmem_complex_op()(vu_tmp, size_vu); syncmem_complex_h2d_op()(vu_tmp, vu, size_vu); syncmem_var_h2d_op()(d_wg, h_wg, this->nbands * (ik+1)); + + // Allocate device memory for stress + FPTYPE* stress_device = nullptr; + resmem_var_op()(stress_device, 9); + setmem_var_op()(stress_device, 0, 9); + + cal_stress_nl_op()(this->ctx, + nkb, + npm, + this->ntype, + this->nbands, + ik, + atom_nh, + atom_na, + d_wg, + vu_tmp, + orbital_corr_tmp, + becp, + dbecp, + stress_device); + + // Transfer stress from device to host + syncmem_var_d2h_op()(stress.data(), stress_device, 9); + delmem_var_op()(stress_device); + delmem_complex_op()(vu_tmp); + delmem_int_op()(orbital_corr_tmp); } else #endif @@ -936,37 +964,35 @@ void Onsite_Proj_tools::cal_stress_dftu(int ik, orbital_corr_tmp = const_cast(orbital_corr); vu_tmp = const_cast*>(vu); d_wg = const_cast(h_wg); + + cal_stress_nl_op()(this->ctx, + nkb, + npm, + this->ntype, + this->nbands, + ik, + atom_nh, + atom_na, + d_wg, + vu_tmp, + orbital_corr_tmp, + becp, + dbecp, + stress.data()); } - cal_stress_nl_op()(this->ctx, - nkb, - npm, - this->ntype, - this->nbands, - ik, - atom_nh, - atom_na, - d_wg, - vu_tmp, - orbital_corr_tmp, - becp, - dbecp, - stress); -#if defined(__CUDA) || defined(__ROCM) - if (this->device == base_device::GpuDevice) - { - delmem_complex_op()(vu_tmp); - delmem_int_op()(orbital_corr_tmp); - } -#endif + + return stress; } template -void Onsite_Proj_tools::cal_stress_dspin(int ik, - int npm, - FPTYPE* stress, - const ModuleBase::Vector3* lambda, - const FPTYPE* h_wg) +std::vector Onsite_Proj_tools::cal_stress_dspin(int ik, + int npm, + const ModuleBase::Vector3* lambda, + const FPTYPE* h_wg) { + // Create stress vector to store results + std::vector stress(9, 0.0); + std::vector lambda_array(this->ucell_->nat * 3); for (int iat = 0; iat < this->ucell_->nat; iat++) { @@ -981,34 +1007,55 @@ void Onsite_Proj_tools::cal_stress_dspin(int ik, resmem_var_op()(lambda_tmp, this->ucell_->nat * 3); syncmem_var_h2d_op()(lambda_tmp, lambda_array.data(), this->ucell_->nat * 3); syncmem_var_h2d_op()(d_wg, h_wg, this->nbands * (ik+1)); + + // Allocate device memory for stress + FPTYPE* stress_device = nullptr; + resmem_var_op()(stress_device, 9); + setmem_var_op()(stress_device, 0, 9); + + const int force_nc = 3; + cal_stress_nl_op()(this->ctx, + nkb, + npm, + this->ntype, + this->nbands, + ik, + atom_nh, + atom_na, + d_wg, + lambda_tmp, + becp, + dbecp, + stress_device); + + // Transfer stress from device to host + syncmem_var_d2h_op()(stress.data(), stress_device, 9); + delmem_var_op()(stress_device); + delmem_var_op()(lambda_tmp); } else #endif { lambda_tmp = lambda_array.data(); d_wg = const_cast(h_wg); + + const int force_nc = 3; + cal_stress_nl_op()(this->ctx, + nkb, + npm, + this->ntype, + this->nbands, + ik, + atom_nh, + atom_na, + d_wg, + lambda_tmp, + becp, + dbecp, + stress.data()); } - const int force_nc = 3; - cal_stress_nl_op()(this->ctx, - nkb, - npm, - this->ntype, - this->nbands, - ik, - atom_nh, - atom_na, - d_wg, - lambda_tmp, - becp, - dbecp, - stress); - -#if defined(__CUDA) || defined(__ROCM) - if (this->device == base_device::GpuDevice) - { - delmem_var_op()(lambda_tmp); - } -#endif + + return stress; } // template instantiation diff --git a/source/source_pw/module_pwdft/onsite_proj_tools.h b/source/source_pw/module_pwdft/onsite_proj_tools.h index 9f75e0611eb..0e4ecd619ee 100644 --- a/source/source_pw/module_pwdft/onsite_proj_tools.h +++ b/source/source_pw/module_pwdft/onsite_proj_tools.h @@ -91,20 +91,18 @@ class Onsite_Proj_tools const FPTYPE* h_wg ); - void cal_stress_dftu( + std::vector cal_stress_dftu( int ik, int npm, - FPTYPE* stress, const int* orbital_corr, const std::complex* vu, const int size_vu, const FPTYPE* h_wg ); - void cal_stress_dspin( + std::vector cal_stress_dspin( int ik, int npm, - FPTYPE* stress, const ModuleBase::Vector3* lambda, const FPTYPE* h_wg ); diff --git a/source/source_pw/module_pwdft/stress_func.h b/source/source_pw/module_pwdft/stress_func.h index 815298aa9d4..0a144e3c81d 100644 --- a/source/source_pw/module_pwdft/stress_func.h +++ b/source/source_pw/module_pwdft/stress_func.h @@ -180,9 +180,9 @@ class Stress_Func void stress_onsite(ModuleBase::matrix& sigma, const ModuleBase::matrix& wg, const ModulePW::PW_Basis_K* wfc_basis, - const UnitCell& ucell_in, - const Plus_U &dftu, // mohan add 2025-11-06 - const psi::Psi , Device>* psi_in, + const UnitCell& ucell_in, + const Plus_U &dftu, // mohan add 2025-11-06 + const void* psi_in, ModuleSymmetry::Symmetry* p_symm); // nonlocal part in PW basis void get_dvnl1(ModuleBase::ComplexMatrix& vkb, diff --git a/source/source_pw/module_pwdft/stress_onsite.cpp b/source/source_pw/module_pwdft/stress_onsite.cpp index e68383907c8..770c69684ab 100644 --- a/source/source_pw/module_pwdft/stress_onsite.cpp +++ b/source/source_pw/module_pwdft/stress_onsite.cpp @@ -47,12 +47,7 @@ void Stress_Func::stress_onsite( ModuleBase::timer::start("Stress", "stress_onsite"); - // Allocate device memory for stress calculation - FPTYPE* stress_device = nullptr; - resmem_var_op()(stress_device, 9); - setmem_var_op()(stress_device, 0, 9); - - // Host memory for stress storage + // Host memory for stress storage (CPU only) std::vector sigma_onsite(9, 0.0); // Get onsite projector instance @@ -66,6 +61,20 @@ void Stress_Func::stress_onsite( { // Determine number of occupied bands (skip zero weights) int nbands_occ = wg.nc; + + // Check if nbands_occ is valid + if (nbands_occ < 0) + { + ModuleBase::WARNING_QUIT("stress_onsite.cpp", "Number of bands is negative, cannot calculate stress"); + } + + // Skip if no bands + if (nbands_occ == 0) + { + continue; + } + + // Find the highest occupied band with non-zero weight while (wg(ik, nbands_occ - 1) == 0.0) { nbands_occ--; @@ -84,24 +93,27 @@ void Stress_Func::stress_onsite( { for (int jpol = 0; jpol <= ipol; jpol++) { - // Get pointer to current stress component - FPTYPE* stress_component = stress_device + (ipol * 3 + jpol); - // Calculate dbecp_s = fs_tools->cal_dbecp_s(ik, num_occupied_bands, ipol, jpol); // Add DFT+U contribution if enabled if (PARAM.inp.dft_plus_u) { - fs_tools->cal_stress_dftu( + // Calculate DFT+U stress contribution + std::vector dftu_stress = fs_tools->cal_stress_dftu( ik, num_occupied_bands, - stress_component, dftu.orbital_corr.data(), dftu.get_eff_pot_pw(0), dftu.get_size_eff_pot_pw(), wg.c ); + + // Add to total stress + for (int idx = 0; idx < 9; idx++) + { + sigma_onsite[idx] += dftu_stress[idx]; + } } // Add spin constraint contribution if enabled @@ -115,21 +127,24 @@ void Stress_Func::stress_onsite( const std::vector>& lambda = spin_constrain.get_sc_lambda(); // Calculate spin constraint stress contribution - fs_tools->cal_stress_dspin( + std::vector dspin_stress = fs_tools->cal_stress_dspin( ik, num_occupied_bands, - stress_component, lambda.data(), wg.c ); + + // Add to total stress + for (int idx = 0; idx < 9; idx++) + { + sigma_onsite[idx] += dspin_stress[idx]; + } } } } } - // Transfer stress from device to host - syncmem_var_d2h_op()(sigma_onsite.data(), stress_device, 9); - delmem_var_op()(stress_device); + // No device memory to clean up (using CPU only) // Step 1: Reduce stress contributions from all processors for (int i = 0; i < 3; i++) From 97bc55f9e283c331b1a4d642580b8a65461ae6c6 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sun, 5 Apr 2026 17:07:02 +0800 Subject: [PATCH 14/20] add dspin example in 01_PW --- tests/01_PW/BUG_SCF_DSPIN/INPUT | 36 +++++++++++++++++++++++++++++++ tests/01_PW/BUG_SCF_DSPIN/KPT | 4 ++++ tests/01_PW/BUG_SCF_DSPIN/STRU | 21 ++++++++++++++++++ tests/01_PW/BUG_SCF_DSPIN/sc.json | 23 ++++++++++++++++++++ 4 files changed, 84 insertions(+) create mode 100644 tests/01_PW/BUG_SCF_DSPIN/INPUT create mode 100644 tests/01_PW/BUG_SCF_DSPIN/KPT create mode 100644 tests/01_PW/BUG_SCF_DSPIN/STRU create mode 100644 tests/01_PW/BUG_SCF_DSPIN/sc.json diff --git a/tests/01_PW/BUG_SCF_DSPIN/INPUT b/tests/01_PW/BUG_SCF_DSPIN/INPUT new file mode 100644 index 00000000000..94350f4eef1 --- /dev/null +++ b/tests/01_PW/BUG_SCF_DSPIN/INPUT @@ -0,0 +1,36 @@ +INPUT_PARAMETERS +pseudo_dir ../../../tests/PP_ORB +orbital_dir ../../../tests/PP_ORB +calculation scf +ecutwfc 20 +scf_thr 1.0e-5 +scf_nmax 20 + +smearing_method gaussian +smearing_sigma 0.07 + +mixing_type pulay +mixing_ndim 10 +mixing_beta 0.1 + +ks_solver cg +basis_type pw +symmetry 0 + +noncolin true +nspin 4 +lspinorb true +cal_force 1 +cal_stress 1 + +#deltaspin +sc_mag_switch 1 # switch to control spin-constrained DFT +decay_grad_switch 1 # switch to control gradient break condition +sc_thr 1e-7 # Convergence criterion of spin-constrained iteration (RMS) in uB +nsc 150 # Maximal number of spin-constrained iteration +nsc_min 2 # Minimum number of spin-constrained iteration +sc_scf_nmin 2 # Minimum number of outer scf loop before initializing lambda loop +alpha_trial 0.01 # Initial trial step size for lambda in eV/uB^2 +sccut 3 # Maximal step size for lambda in eV/uB +sc_drop_thr 1.0e-2 # Convergence criterion ratio of lambda iteration in Spin-constrained DFT +sc_scf_thr 1.0e-4 # Density error threshold for inner loop of spin-constrained SCF diff --git a/tests/01_PW/BUG_SCF_DSPIN/KPT b/tests/01_PW/BUG_SCF_DSPIN/KPT new file mode 100644 index 00000000000..4fd38968a05 --- /dev/null +++ b/tests/01_PW/BUG_SCF_DSPIN/KPT @@ -0,0 +1,4 @@ +K_POINTS +0 +Gamma +2 2 1 0 0 0 diff --git a/tests/01_PW/BUG_SCF_DSPIN/STRU b/tests/01_PW/BUG_SCF_DSPIN/STRU new file mode 100644 index 00000000000..ae2b187b97b --- /dev/null +++ b/tests/01_PW/BUG_SCF_DSPIN/STRU @@ -0,0 +1,21 @@ +ATOMIC_SPECIES +Fe 55.845 Fe.upf + +NUMERICAL_ORBITAL +Fe_gga_6au_100Ry_2s1p1d.orb + +LATTICE_CONSTANT +1.889725989 + +LATTICE_VECTORS + 2.8328152400000000 0.0000000000000000 0.0000000000000000 + 0.0000000000000000 2.8328152400000000 0.0000000000000000 + 0.0000000000000000 0.0000000000000000 2.8328152400000000 +ATOMIC_POSITIONS +Direct + +Fe +1.0 +2 +0.0 0.0 0.0 mag 5 angle1 90 angle2 0 +0.5 0.5 0.5 mag 5 angle1 90 angle2 0 diff --git a/tests/01_PW/BUG_SCF_DSPIN/sc.json b/tests/01_PW/BUG_SCF_DSPIN/sc.json new file mode 100644 index 00000000000..e1fa3e088bd --- /dev/null +++ b/tests/01_PW/BUG_SCF_DSPIN/sc.json @@ -0,0 +1,23 @@ +[ + { + "element": "Fe", + "itype": 0, + "ScDecayGrad": 0.9, + "ScAtomData": [ + { + "index": 0, + "lambda": [0, 0, 0], + "target_mag": [2.386, 0.0, 0.0], + "constrain": [1,1,1] + }, + { + "index": 1, + "lambda": [0, 0, 0], + "target_mag_val": 2.386, + "target_mag_angle1": 90.0, + "target_mag_angle2": 0.0, + "constrain": [1,1,1] + } + ] + } +] From e2e5a46f73a7b389aadb95a3dffa97eb82996bec Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Sun, 5 Apr 2026 17:27:11 +0800 Subject: [PATCH 15/20] Fix MODULE_IO_read_item_serial test by updating sc_mag_switch test case --- source/source_io/test_serial/read_input_item_test.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/source/source_io/test_serial/read_input_item_test.cpp b/source/source_io/test_serial/read_input_item_test.cpp index 398a0624a0e..d76eed56cb8 100644 --- a/source/source_io/test_serial/read_input_item_test.cpp +++ b/source/source_io/test_serial/read_input_item_test.cpp @@ -1632,10 +1632,8 @@ TEST_F(InputTest, Item_test2) { // sc_mag_switch auto it = find_label("sc_mag_switch", readinput.input_lists); param.input.sc_mag_switch = true; - testing::internal::CaptureStdout(); - EXPECT_EXIT(it->second.check_value(it->second, param), ::testing::ExitedWithCode(1), ""); - output = testing::internal::GetCapturedStdout(); - EXPECT_THAT(output, testing::HasSubstr("NOTICE")); + // Since sc_mag_switch check is disabled, just call the function without expecting exit + it->second.check_value(it->second, param); } { // sc_thr auto it = find_label("sc_thr", readinput.input_lists); From bd4493901bc4e834134513abe8f5521faefc2bcc Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Mon, 6 Apr 2026 14:24:19 +0800 Subject: [PATCH 16/20] fix bugs, the stress elements are calcualted one by one by GPU/CPU --- .../module_pwdft/onsite_proj_tools.cpp | 42 +++++++++---------- .../module_pwdft/onsite_proj_tools.h | 6 ++- .../source_pw/module_pwdft/stress_onsite.cpp | 40 +++++++++--------- 3 files changed, 43 insertions(+), 45 deletions(-) diff --git a/source/source_pw/module_pwdft/onsite_proj_tools.cpp b/source/source_pw/module_pwdft/onsite_proj_tools.cpp index 51c07964003..f8690e8af98 100644 --- a/source/source_pw/module_pwdft/onsite_proj_tools.cpp +++ b/source/source_pw/module_pwdft/onsite_proj_tools.cpp @@ -911,15 +911,14 @@ void Onsite_Proj_tools::cal_force_dspin(int ik, } template -std::vector Onsite_Proj_tools::cal_stress_dftu(int ik, - int npm, - const int* orbital_corr, - const std::complex* vu, - const int size_vu, - const FPTYPE* h_wg) +double Onsite_Proj_tools::cal_stress_dftu(int ik, + int npm, + const int* orbital_corr, + const std::complex* vu, + const int size_vu, + const FPTYPE* h_wg) { - // Create stress vector to store results - std::vector stress(9, 0.0); + double stress_out = 0.0; int* orbital_corr_tmp = nullptr; std::complex* vu_tmp = nullptr; @@ -934,8 +933,8 @@ std::vector Onsite_Proj_tools::cal_stress_dftu(int ik, // Allocate device memory for stress FPTYPE* stress_device = nullptr; - resmem_var_op()(stress_device, 9); - setmem_var_op()(stress_device, 0, 9); + resmem_var_op()(stress_device, 1); + setmem_var_op()(stress_device, 0, 1); cal_stress_nl_op()(this->ctx, nkb, @@ -953,7 +952,7 @@ std::vector Onsite_Proj_tools::cal_stress_dftu(int ik, stress_device); // Transfer stress from device to host - syncmem_var_d2h_op()(stress.data(), stress_device, 9); + syncmem_var_d2h_op()(&stress_out, stress_device, 1); delmem_var_op()(stress_device); delmem_complex_op()(vu_tmp); delmem_int_op()(orbital_corr_tmp); @@ -978,20 +977,19 @@ std::vector Onsite_Proj_tools::cal_stress_dftu(int ik, orbital_corr_tmp, becp, dbecp, - stress.data()); + &stress_out); } - return stress; + return stress_out; } template -std::vector Onsite_Proj_tools::cal_stress_dspin(int ik, - int npm, - const ModuleBase::Vector3* lambda, - const FPTYPE* h_wg) +double Onsite_Proj_tools::cal_stress_dspin(int ik, + int npm, + const ModuleBase::Vector3* lambda, + const FPTYPE* h_wg) { - // Create stress vector to store results - std::vector stress(9, 0.0); + double stress_out = 0.0; std::vector lambda_array(this->ucell_->nat * 3); for (int iat = 0; iat < this->ucell_->nat; iat++) @@ -1029,7 +1027,7 @@ std::vector Onsite_Proj_tools::cal_stress_dspin(int ik, stress_device); // Transfer stress from device to host - syncmem_var_d2h_op()(stress.data(), stress_device, 9); + syncmem_var_d2h_op()(&stress_out, stress_device, 9); delmem_var_op()(stress_device); delmem_var_op()(lambda_tmp); } @@ -1052,10 +1050,10 @@ std::vector Onsite_Proj_tools::cal_stress_dspin(int ik, lambda_tmp, becp, dbecp, - stress.data()); + &stress_out); } - return stress; + return stress_out; } // template instantiation diff --git a/source/source_pw/module_pwdft/onsite_proj_tools.h b/source/source_pw/module_pwdft/onsite_proj_tools.h index 0e4ecd619ee..e877a85070c 100644 --- a/source/source_pw/module_pwdft/onsite_proj_tools.h +++ b/source/source_pw/module_pwdft/onsite_proj_tools.h @@ -91,7 +91,8 @@ class Onsite_Proj_tools const FPTYPE* h_wg ); - std::vector cal_stress_dftu( + // return stress(i,j) value + double cal_stress_dftu( int ik, int npm, const int* orbital_corr, @@ -100,7 +101,8 @@ class Onsite_Proj_tools const FPTYPE* h_wg ); - std::vector cal_stress_dspin( + // return stress(i,j) value + double cal_stress_dspin( int ik, int npm, const ModuleBase::Vector3* lambda, diff --git a/source/source_pw/module_pwdft/stress_onsite.cpp b/source/source_pw/module_pwdft/stress_onsite.cpp index 770c69684ab..1b9a08bb882 100644 --- a/source/source_pw/module_pwdft/stress_onsite.cpp +++ b/source/source_pw/module_pwdft/stress_onsite.cpp @@ -93,6 +93,8 @@ void Stress_Func::stress_onsite( { for (int jpol = 0; jpol <= ipol; jpol++) { + const int idx = ipol * 3 + jpol; + // Calculate dbecp_s = fs_tools->cal_dbecp_s(ik, num_occupied_bands, ipol, jpol); @@ -100,7 +102,7 @@ void Stress_Func::stress_onsite( if (PARAM.inp.dft_plus_u) { // Calculate DFT+U stress contribution - std::vector dftu_stress = fs_tools->cal_stress_dftu( + double dftu_stress = fs_tools->cal_stress_dftu( ik, num_occupied_bands, dftu.orbital_corr.data(), @@ -109,11 +111,10 @@ void Stress_Func::stress_onsite( wg.c ); - // Add to total stress - for (int idx = 0; idx < 9; idx++) - { - sigma_onsite[idx] += dftu_stress[idx]; - } + sigma_onsite[idx] += dftu_stress; +#ifdef __DEBUG + std::cout << " idx=" << idx << " stress=" << sigma_onsite[idx] << std::endl; +#endif } // Add spin constraint contribution if enabled @@ -127,18 +128,14 @@ void Stress_Func::stress_onsite( const std::vector>& lambda = spin_constrain.get_sc_lambda(); // Calculate spin constraint stress contribution - std::vector dspin_stress = fs_tools->cal_stress_dspin( + double dspin_stress = fs_tools->cal_stress_dspin( ik, num_occupied_bands, lambda.data(), wg.c ); - // Add to total stress - for (int idx = 0; idx < 9; idx++) - { - sigma_onsite[idx] += dspin_stress[idx]; - } + sigma_onsite[idx] += dspin_stress; } } } @@ -152,6 +149,10 @@ void Stress_Func::stress_onsite( for (int j = 0; j < 3; j++) { const int idx = i * 3 + j; + if(j>i) + { + sigma_onsite[idx]=sigma_onsite[j*3+i]; + } Parallel_Reduce::reduce_all(sigma_onsite[idx]); // qianrui fix a bug for kpar > 1 } } @@ -167,18 +168,15 @@ void Stress_Func::stress_onsite( } } - // Step 3: Handle symmetry for upper triangular elements - for (int i = 0; i < 3; i++) +#ifdef __DEBUG + // Add to total stress + for (int idx = 0; idx < 9; idx++) { - for (int j = i + 1; j < 3; j++) - { - const int idx = i * 3 + j; - const int transposed_idx = j * 3 + i; - sigma_onsite[idx] = sigma_onsite[transposed_idx]; - } + std::cout << " idx=" << idx << " stress=" << sigma_onsite[idx] << std::endl; } +#endif - // Step 4: Assign stress values to output matrix + // Step 3: Assign stress values to output matrix for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) From 61b5f30d5550fa3a27f6fb68b6132af3dbc9c06c Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Tue, 7 Apr 2026 17:16:42 +0800 Subject: [PATCH 17/20] fix a potential bug in calculating stress of DFT+U or Deltaspin --- source/source_pw/module_pwdft/onsite_proj_tools.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/source_pw/module_pwdft/onsite_proj_tools.cpp b/source/source_pw/module_pwdft/onsite_proj_tools.cpp index f8690e8af98..3a483fedfea 100644 --- a/source/source_pw/module_pwdft/onsite_proj_tools.cpp +++ b/source/source_pw/module_pwdft/onsite_proj_tools.cpp @@ -1008,8 +1008,8 @@ double Onsite_Proj_tools::cal_stress_dspin(int ik, // Allocate device memory for stress FPTYPE* stress_device = nullptr; - resmem_var_op()(stress_device, 9); - setmem_var_op()(stress_device, 0, 9); + resmem_var_op()(stress_device, 1); + setmem_var_op()(stress_device, 0, 1); const int force_nc = 3; cal_stress_nl_op()(this->ctx, @@ -1027,7 +1027,7 @@ double Onsite_Proj_tools::cal_stress_dspin(int ik, stress_device); // Transfer stress from device to host - syncmem_var_d2h_op()(&stress_out, stress_device, 9); + syncmem_var_d2h_op()(&stress_out, stress_device, 1); delmem_var_op()(stress_device); delmem_var_op()(lambda_tmp); } From 35a5832752b45a894861c581dcc86fab2ddeb8c2 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Tue, 7 Apr 2026 23:05:58 +0800 Subject: [PATCH 18/20] rename tests in 11_PW_GPU --- tests/11_PW_GPU/001_PW_BPCG_GPU/README | 9 --------- tests/11_PW_GPU/CASES_GPU.txt | 14 +++++++------- .../11_PW_GPU/{001_PW_BPCG_GPU => scf_bpcg}/INPUT | 6 +++--- tests/11_PW_GPU/{001_PW_BPCG_GPU => scf_bpcg}/KPT | 0 tests/11_PW_GPU/scf_bpcg/README | 1 + tests/11_PW_GPU/{001_PW_BPCG_GPU => scf_bpcg}/STRU | 0 .../{001_PW_BPCG_GPU => scf_bpcg}/result.ref | 0 .../{001_PW_BPCG_GPU => scf_bpcg}/threshold | 0 tests/11_PW_GPU/{002_PW_CG_GPU => scf_cg}/INPUT | 0 tests/11_PW_GPU/{002_PW_CG_GPU => scf_cg}/KPT | 0 tests/11_PW_GPU/{002_PW_CG_GPU => scf_cg}/README | 0 tests/11_PW_GPU/{002_PW_CG_GPU => scf_cg}/STRU | 0 .../11_PW_GPU/{002_PW_CG_GPU => scf_cg}/result.ref | 0 .../11_PW_GPU/{002_PW_CG_GPU => scf_cg}/threshold | 0 .../{005_PW_CG_GPU_float => scf_cg_single}/INPUT | 0 .../11_PW_GPU/{003_PW_DA_GPU => scf_cg_single}/KPT | 0 .../{005_PW_CG_GPU_float => scf_cg_single}/README | 0 .../{003_PW_DA_GPU => scf_cg_single}/STRU | 0 .../result.ref | 0 .../threshold | 0 tests/11_PW_GPU/{003_PW_DA_GPU => scf_dav}/INPUT | 0 tests/11_PW_GPU/{004_PW_DS_GPU => scf_dav}/KPT | 0 tests/11_PW_GPU/{003_PW_DA_GPU => scf_dav}/README | 0 tests/11_PW_GPU/{004_PW_DS_GPU => scf_dav}/STRU | 0 .../{003_PW_DA_GPU => scf_dav}/result.ref | 0 .../11_PW_GPU/{003_PW_DA_GPU => scf_dav}/threshold | 0 .../11_PW_GPU/{004_PW_DS_GPU => scf_dav_sub}/INPUT | 0 .../{005_PW_CG_GPU_float => scf_dav_sub}/KPT | 0 .../{004_PW_DS_GPU => scf_dav_sub}/README | 0 .../{005_PW_CG_GPU_float => scf_dav_sub}/STRU | 0 .../{004_PW_DS_GPU => scf_dav_sub}/result.ref | 0 .../{004_PW_DS_GPU => scf_dav_sub}/threshold | 0 .../11_PW_GPU/{007_PW_OW_GPU => scf_out_wf}/INPUT | 0 .../{006_PW_get_wf_GPU => scf_out_wf}/KPT | 0 .../11_PW_GPU/{007_PW_OW_GPU => scf_out_wf}/README | 0 .../{006_PW_get_wf_GPU => scf_out_wf}/STRU | 0 .../{007_PW_OW_GPU => scf_out_wf}/result.ref | 0 .../{006_PW_get_wf_GPU => scf_out_wf_norm}/INPUT | 0 .../{007_PW_OW_GPU => scf_out_wf_norm}/KPT | 0 .../{006_PW_get_wf_GPU => scf_out_wf_norm}/README | 0 .../{007_PW_OW_GPU => scf_out_wf_norm}/STRU | 0 .../result.ref | 0 42 files changed, 11 insertions(+), 19 deletions(-) delete mode 100644 tests/11_PW_GPU/001_PW_BPCG_GPU/README rename tests/11_PW_GPU/{001_PW_BPCG_GPU => scf_bpcg}/INPUT (91%) rename tests/11_PW_GPU/{001_PW_BPCG_GPU => scf_bpcg}/KPT (100%) create mode 100644 tests/11_PW_GPU/scf_bpcg/README rename tests/11_PW_GPU/{001_PW_BPCG_GPU => scf_bpcg}/STRU (100%) rename tests/11_PW_GPU/{001_PW_BPCG_GPU => scf_bpcg}/result.ref (100%) rename tests/11_PW_GPU/{001_PW_BPCG_GPU => scf_bpcg}/threshold (100%) rename tests/11_PW_GPU/{002_PW_CG_GPU => scf_cg}/INPUT (100%) rename tests/11_PW_GPU/{002_PW_CG_GPU => scf_cg}/KPT (100%) rename tests/11_PW_GPU/{002_PW_CG_GPU => scf_cg}/README (100%) rename tests/11_PW_GPU/{002_PW_CG_GPU => scf_cg}/STRU (100%) rename tests/11_PW_GPU/{002_PW_CG_GPU => scf_cg}/result.ref (100%) rename tests/11_PW_GPU/{002_PW_CG_GPU => scf_cg}/threshold (100%) rename tests/11_PW_GPU/{005_PW_CG_GPU_float => scf_cg_single}/INPUT (100%) rename tests/11_PW_GPU/{003_PW_DA_GPU => scf_cg_single}/KPT (100%) rename tests/11_PW_GPU/{005_PW_CG_GPU_float => scf_cg_single}/README (100%) rename tests/11_PW_GPU/{003_PW_DA_GPU => scf_cg_single}/STRU (100%) rename tests/11_PW_GPU/{005_PW_CG_GPU_float => scf_cg_single}/result.ref (100%) rename tests/11_PW_GPU/{005_PW_CG_GPU_float => scf_cg_single}/threshold (100%) rename tests/11_PW_GPU/{003_PW_DA_GPU => scf_dav}/INPUT (100%) rename tests/11_PW_GPU/{004_PW_DS_GPU => scf_dav}/KPT (100%) rename tests/11_PW_GPU/{003_PW_DA_GPU => scf_dav}/README (100%) rename tests/11_PW_GPU/{004_PW_DS_GPU => scf_dav}/STRU (100%) rename tests/11_PW_GPU/{003_PW_DA_GPU => scf_dav}/result.ref (100%) rename tests/11_PW_GPU/{003_PW_DA_GPU => scf_dav}/threshold (100%) rename tests/11_PW_GPU/{004_PW_DS_GPU => scf_dav_sub}/INPUT (100%) rename tests/11_PW_GPU/{005_PW_CG_GPU_float => scf_dav_sub}/KPT (100%) rename tests/11_PW_GPU/{004_PW_DS_GPU => scf_dav_sub}/README (100%) rename tests/11_PW_GPU/{005_PW_CG_GPU_float => scf_dav_sub}/STRU (100%) rename tests/11_PW_GPU/{004_PW_DS_GPU => scf_dav_sub}/result.ref (100%) rename tests/11_PW_GPU/{004_PW_DS_GPU => scf_dav_sub}/threshold (100%) rename tests/11_PW_GPU/{007_PW_OW_GPU => scf_out_wf}/INPUT (100%) rename tests/11_PW_GPU/{006_PW_get_wf_GPU => scf_out_wf}/KPT (100%) rename tests/11_PW_GPU/{007_PW_OW_GPU => scf_out_wf}/README (100%) rename tests/11_PW_GPU/{006_PW_get_wf_GPU => scf_out_wf}/STRU (100%) rename tests/11_PW_GPU/{007_PW_OW_GPU => scf_out_wf}/result.ref (100%) rename tests/11_PW_GPU/{006_PW_get_wf_GPU => scf_out_wf_norm}/INPUT (100%) rename tests/11_PW_GPU/{007_PW_OW_GPU => scf_out_wf_norm}/KPT (100%) rename tests/11_PW_GPU/{006_PW_get_wf_GPU => scf_out_wf_norm}/README (100%) rename tests/11_PW_GPU/{007_PW_OW_GPU => scf_out_wf_norm}/STRU (100%) rename tests/11_PW_GPU/{006_PW_get_wf_GPU => scf_out_wf_norm}/result.ref (100%) diff --git a/tests/11_PW_GPU/001_PW_BPCG_GPU/README b/tests/11_PW_GPU/001_PW_BPCG_GPU/README deleted file mode 100644 index b8f97be67a1..00000000000 --- a/tests/11_PW_GPU/001_PW_BPCG_GPU/README +++ /dev/null @@ -1,9 +0,0 @@ -This test for: -*GaAs-deformation -*PW -*kpoints 2*2*2 -*sg15 pseudopotential -*smearing_method gauss -*ks_solver bpcg -*mixing_type broyden-kerker -*mixing_beta 0.4 diff --git a/tests/11_PW_GPU/CASES_GPU.txt b/tests/11_PW_GPU/CASES_GPU.txt index f550b7f0445..a10f2a682e2 100644 --- a/tests/11_PW_GPU/CASES_GPU.txt +++ b/tests/11_PW_GPU/CASES_GPU.txt @@ -1,7 +1,7 @@ -001_PW_BPCG_GPU -002_PW_CG_GPU -003_PW_DA_GPU -004_PW_DS_GPU -005_PW_CG_GPU_float -006_PW_get_wf_GPU -007_PW_OW_GPU +scf_bpcg +scf_cg +scf_cg_single +scf_dav +scf_dav_sub +scf_out_wf +scf_out_wf_norm diff --git a/tests/11_PW_GPU/001_PW_BPCG_GPU/INPUT b/tests/11_PW_GPU/scf_bpcg/INPUT similarity index 91% rename from tests/11_PW_GPU/001_PW_BPCG_GPU/INPUT rename to tests/11_PW_GPU/scf_bpcg/INPUT index 6893f164762..3631f7c2a2b 100644 --- a/tests/11_PW_GPU/001_PW_BPCG_GPU/INPUT +++ b/tests/11_PW_GPU/scf_bpcg/INPUT @@ -15,7 +15,7 @@ smearing_sigma 0.02 ecutwfc 40 scf_thr 1e-7 scf_nmax 100 -bndpar 2 +#bndpar 2 #Parameters (LCAO) basis_type pw @@ -33,6 +33,6 @@ mixing_type broyden mixing_beta 0.4 mixing_gg0 1.5 -pw_seed 1 +pw_seed 1 diago_smooth_ethr 1 -use_k_continuity 1 +use_k_continuity 1 diff --git a/tests/11_PW_GPU/001_PW_BPCG_GPU/KPT b/tests/11_PW_GPU/scf_bpcg/KPT similarity index 100% rename from tests/11_PW_GPU/001_PW_BPCG_GPU/KPT rename to tests/11_PW_GPU/scf_bpcg/KPT diff --git a/tests/11_PW_GPU/scf_bpcg/README b/tests/11_PW_GPU/scf_bpcg/README new file mode 100644 index 00000000000..d0477404fef --- /dev/null +++ b/tests/11_PW_GPU/scf_bpcg/README @@ -0,0 +1 @@ +This test for: BPCG method for GaAs diff --git a/tests/11_PW_GPU/001_PW_BPCG_GPU/STRU b/tests/11_PW_GPU/scf_bpcg/STRU similarity index 100% rename from tests/11_PW_GPU/001_PW_BPCG_GPU/STRU rename to tests/11_PW_GPU/scf_bpcg/STRU diff --git a/tests/11_PW_GPU/001_PW_BPCG_GPU/result.ref b/tests/11_PW_GPU/scf_bpcg/result.ref similarity index 100% rename from tests/11_PW_GPU/001_PW_BPCG_GPU/result.ref rename to tests/11_PW_GPU/scf_bpcg/result.ref diff --git a/tests/11_PW_GPU/001_PW_BPCG_GPU/threshold b/tests/11_PW_GPU/scf_bpcg/threshold similarity index 100% rename from tests/11_PW_GPU/001_PW_BPCG_GPU/threshold rename to tests/11_PW_GPU/scf_bpcg/threshold diff --git a/tests/11_PW_GPU/002_PW_CG_GPU/INPUT b/tests/11_PW_GPU/scf_cg/INPUT similarity index 100% rename from tests/11_PW_GPU/002_PW_CG_GPU/INPUT rename to tests/11_PW_GPU/scf_cg/INPUT diff --git a/tests/11_PW_GPU/002_PW_CG_GPU/KPT b/tests/11_PW_GPU/scf_cg/KPT similarity index 100% rename from tests/11_PW_GPU/002_PW_CG_GPU/KPT rename to tests/11_PW_GPU/scf_cg/KPT diff --git a/tests/11_PW_GPU/002_PW_CG_GPU/README b/tests/11_PW_GPU/scf_cg/README similarity index 100% rename from tests/11_PW_GPU/002_PW_CG_GPU/README rename to tests/11_PW_GPU/scf_cg/README diff --git a/tests/11_PW_GPU/002_PW_CG_GPU/STRU b/tests/11_PW_GPU/scf_cg/STRU similarity index 100% rename from tests/11_PW_GPU/002_PW_CG_GPU/STRU rename to tests/11_PW_GPU/scf_cg/STRU diff --git a/tests/11_PW_GPU/002_PW_CG_GPU/result.ref b/tests/11_PW_GPU/scf_cg/result.ref similarity index 100% rename from tests/11_PW_GPU/002_PW_CG_GPU/result.ref rename to tests/11_PW_GPU/scf_cg/result.ref diff --git a/tests/11_PW_GPU/002_PW_CG_GPU/threshold b/tests/11_PW_GPU/scf_cg/threshold similarity index 100% rename from tests/11_PW_GPU/002_PW_CG_GPU/threshold rename to tests/11_PW_GPU/scf_cg/threshold diff --git a/tests/11_PW_GPU/005_PW_CG_GPU_float/INPUT b/tests/11_PW_GPU/scf_cg_single/INPUT similarity index 100% rename from tests/11_PW_GPU/005_PW_CG_GPU_float/INPUT rename to tests/11_PW_GPU/scf_cg_single/INPUT diff --git a/tests/11_PW_GPU/003_PW_DA_GPU/KPT b/tests/11_PW_GPU/scf_cg_single/KPT similarity index 100% rename from tests/11_PW_GPU/003_PW_DA_GPU/KPT rename to tests/11_PW_GPU/scf_cg_single/KPT diff --git a/tests/11_PW_GPU/005_PW_CG_GPU_float/README b/tests/11_PW_GPU/scf_cg_single/README similarity index 100% rename from tests/11_PW_GPU/005_PW_CG_GPU_float/README rename to tests/11_PW_GPU/scf_cg_single/README diff --git a/tests/11_PW_GPU/003_PW_DA_GPU/STRU b/tests/11_PW_GPU/scf_cg_single/STRU similarity index 100% rename from tests/11_PW_GPU/003_PW_DA_GPU/STRU rename to tests/11_PW_GPU/scf_cg_single/STRU diff --git a/tests/11_PW_GPU/005_PW_CG_GPU_float/result.ref b/tests/11_PW_GPU/scf_cg_single/result.ref similarity index 100% rename from tests/11_PW_GPU/005_PW_CG_GPU_float/result.ref rename to tests/11_PW_GPU/scf_cg_single/result.ref diff --git a/tests/11_PW_GPU/005_PW_CG_GPU_float/threshold b/tests/11_PW_GPU/scf_cg_single/threshold similarity index 100% rename from tests/11_PW_GPU/005_PW_CG_GPU_float/threshold rename to tests/11_PW_GPU/scf_cg_single/threshold diff --git a/tests/11_PW_GPU/003_PW_DA_GPU/INPUT b/tests/11_PW_GPU/scf_dav/INPUT similarity index 100% rename from tests/11_PW_GPU/003_PW_DA_GPU/INPUT rename to tests/11_PW_GPU/scf_dav/INPUT diff --git a/tests/11_PW_GPU/004_PW_DS_GPU/KPT b/tests/11_PW_GPU/scf_dav/KPT similarity index 100% rename from tests/11_PW_GPU/004_PW_DS_GPU/KPT rename to tests/11_PW_GPU/scf_dav/KPT diff --git a/tests/11_PW_GPU/003_PW_DA_GPU/README b/tests/11_PW_GPU/scf_dav/README similarity index 100% rename from tests/11_PW_GPU/003_PW_DA_GPU/README rename to tests/11_PW_GPU/scf_dav/README diff --git a/tests/11_PW_GPU/004_PW_DS_GPU/STRU b/tests/11_PW_GPU/scf_dav/STRU similarity index 100% rename from tests/11_PW_GPU/004_PW_DS_GPU/STRU rename to tests/11_PW_GPU/scf_dav/STRU diff --git a/tests/11_PW_GPU/003_PW_DA_GPU/result.ref b/tests/11_PW_GPU/scf_dav/result.ref similarity index 100% rename from tests/11_PW_GPU/003_PW_DA_GPU/result.ref rename to tests/11_PW_GPU/scf_dav/result.ref diff --git a/tests/11_PW_GPU/003_PW_DA_GPU/threshold b/tests/11_PW_GPU/scf_dav/threshold similarity index 100% rename from tests/11_PW_GPU/003_PW_DA_GPU/threshold rename to tests/11_PW_GPU/scf_dav/threshold diff --git a/tests/11_PW_GPU/004_PW_DS_GPU/INPUT b/tests/11_PW_GPU/scf_dav_sub/INPUT similarity index 100% rename from tests/11_PW_GPU/004_PW_DS_GPU/INPUT rename to tests/11_PW_GPU/scf_dav_sub/INPUT diff --git a/tests/11_PW_GPU/005_PW_CG_GPU_float/KPT b/tests/11_PW_GPU/scf_dav_sub/KPT similarity index 100% rename from tests/11_PW_GPU/005_PW_CG_GPU_float/KPT rename to tests/11_PW_GPU/scf_dav_sub/KPT diff --git a/tests/11_PW_GPU/004_PW_DS_GPU/README b/tests/11_PW_GPU/scf_dav_sub/README similarity index 100% rename from tests/11_PW_GPU/004_PW_DS_GPU/README rename to tests/11_PW_GPU/scf_dav_sub/README diff --git a/tests/11_PW_GPU/005_PW_CG_GPU_float/STRU b/tests/11_PW_GPU/scf_dav_sub/STRU similarity index 100% rename from tests/11_PW_GPU/005_PW_CG_GPU_float/STRU rename to tests/11_PW_GPU/scf_dav_sub/STRU diff --git a/tests/11_PW_GPU/004_PW_DS_GPU/result.ref b/tests/11_PW_GPU/scf_dav_sub/result.ref similarity index 100% rename from tests/11_PW_GPU/004_PW_DS_GPU/result.ref rename to tests/11_PW_GPU/scf_dav_sub/result.ref diff --git a/tests/11_PW_GPU/004_PW_DS_GPU/threshold b/tests/11_PW_GPU/scf_dav_sub/threshold similarity index 100% rename from tests/11_PW_GPU/004_PW_DS_GPU/threshold rename to tests/11_PW_GPU/scf_dav_sub/threshold diff --git a/tests/11_PW_GPU/007_PW_OW_GPU/INPUT b/tests/11_PW_GPU/scf_out_wf/INPUT similarity index 100% rename from tests/11_PW_GPU/007_PW_OW_GPU/INPUT rename to tests/11_PW_GPU/scf_out_wf/INPUT diff --git a/tests/11_PW_GPU/006_PW_get_wf_GPU/KPT b/tests/11_PW_GPU/scf_out_wf/KPT similarity index 100% rename from tests/11_PW_GPU/006_PW_get_wf_GPU/KPT rename to tests/11_PW_GPU/scf_out_wf/KPT diff --git a/tests/11_PW_GPU/007_PW_OW_GPU/README b/tests/11_PW_GPU/scf_out_wf/README similarity index 100% rename from tests/11_PW_GPU/007_PW_OW_GPU/README rename to tests/11_PW_GPU/scf_out_wf/README diff --git a/tests/11_PW_GPU/006_PW_get_wf_GPU/STRU b/tests/11_PW_GPU/scf_out_wf/STRU similarity index 100% rename from tests/11_PW_GPU/006_PW_get_wf_GPU/STRU rename to tests/11_PW_GPU/scf_out_wf/STRU diff --git a/tests/11_PW_GPU/007_PW_OW_GPU/result.ref b/tests/11_PW_GPU/scf_out_wf/result.ref similarity index 100% rename from tests/11_PW_GPU/007_PW_OW_GPU/result.ref rename to tests/11_PW_GPU/scf_out_wf/result.ref diff --git a/tests/11_PW_GPU/006_PW_get_wf_GPU/INPUT b/tests/11_PW_GPU/scf_out_wf_norm/INPUT similarity index 100% rename from tests/11_PW_GPU/006_PW_get_wf_GPU/INPUT rename to tests/11_PW_GPU/scf_out_wf_norm/INPUT diff --git a/tests/11_PW_GPU/007_PW_OW_GPU/KPT b/tests/11_PW_GPU/scf_out_wf_norm/KPT similarity index 100% rename from tests/11_PW_GPU/007_PW_OW_GPU/KPT rename to tests/11_PW_GPU/scf_out_wf_norm/KPT diff --git a/tests/11_PW_GPU/006_PW_get_wf_GPU/README b/tests/11_PW_GPU/scf_out_wf_norm/README similarity index 100% rename from tests/11_PW_GPU/006_PW_get_wf_GPU/README rename to tests/11_PW_GPU/scf_out_wf_norm/README diff --git a/tests/11_PW_GPU/007_PW_OW_GPU/STRU b/tests/11_PW_GPU/scf_out_wf_norm/STRU similarity index 100% rename from tests/11_PW_GPU/007_PW_OW_GPU/STRU rename to tests/11_PW_GPU/scf_out_wf_norm/STRU diff --git a/tests/11_PW_GPU/006_PW_get_wf_GPU/result.ref b/tests/11_PW_GPU/scf_out_wf_norm/result.ref similarity index 100% rename from tests/11_PW_GPU/006_PW_get_wf_GPU/result.ref rename to tests/11_PW_GPU/scf_out_wf_norm/result.ref From 6346a3aa3481c163d16497151dfbc0c649311cc1 Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Wed, 8 Apr 2026 09:07:35 +0800 Subject: [PATCH 19/20] update codes, found a bug in DFT+U pw GPU version --- .../module_pwdft/kernels/stress_op.cpp | 1 + .../module_pwdft/onsite_proj_tools.cpp | 23 +++++---- tests/11_PW_GPU/BUG_nspin4_u/INPUT | 48 +++++++++++++++++++ tests/11_PW_GPU/BUG_nspin4_u/KPT | 4 ++ tests/11_PW_GPU/BUG_nspin4_u/README | 1 + tests/11_PW_GPU/BUG_nspin4_u/STRU | 22 +++++++++ 6 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 tests/11_PW_GPU/BUG_nspin4_u/INPUT create mode 100644 tests/11_PW_GPU/BUG_nspin4_u/KPT create mode 100644 tests/11_PW_GPU/BUG_nspin4_u/README create mode 100644 tests/11_PW_GPU/BUG_nspin4_u/STRU diff --git a/source/source_pw/module_pwdft/kernels/stress_op.cpp b/source/source_pw/module_pwdft/kernels/stress_op.cpp index 6034a527109..169b9c932c3 100644 --- a/source/source_pw/module_pwdft/kernels/stress_op.cpp +++ b/source/source_pw/module_pwdft/kernels/stress_op.cpp @@ -261,6 +261,7 @@ struct cal_stress_nl_op const std::complex* dbecp, FPTYPE* stress) { +// std::cout << " DFT+U kernel called " << std::endl; FPTYPE local_stress = 0; int iat = 0, sum = 0; for (int it = 0; it < ntype; it++) diff --git a/source/source_pw/module_pwdft/onsite_proj_tools.cpp b/source/source_pw/module_pwdft/onsite_proj_tools.cpp index 3a483fedfea..fec5f0a9fb2 100644 --- a/source/source_pw/module_pwdft/onsite_proj_tools.cpp +++ b/source/source_pw/module_pwdft/onsite_proj_tools.cpp @@ -913,22 +913,27 @@ void Onsite_Proj_tools::cal_force_dspin(int ik, template double Onsite_Proj_tools::cal_stress_dftu(int ik, int npm, - const int* orbital_corr, + const int* orb_corr, const std::complex* vu, const int size_vu, const FPTYPE* h_wg) { double stress_out = 0.0; - int* orbital_corr_tmp = nullptr; + int* orb_corr_tmp = nullptr; std::complex* vu_tmp = nullptr; #if defined(__CUDA) || defined(__ROCM) if (this->device == base_device::GpuDevice) { - resmem_int_op()(orbital_corr_tmp, this->ucell_->ntype); - syncmem_int_h2d_op()(orbital_corr_tmp, orbital_corr, this->ucell_->ntype); + // orb_corr_tmp + resmem_int_op()(orb_corr_tmp, this->ucell_->ntype); + syncmem_int_h2d_op()(orb_corr_tmp, orb_corr, this->ucell_->ntype); + + // vu_tmp resmem_complex_op()(vu_tmp, size_vu); syncmem_complex_h2d_op()(vu_tmp, vu, size_vu); + + // transfer data from from host to device syncmem_var_h2d_op()(d_wg, h_wg, this->nbands * (ik+1)); // Allocate device memory for stress @@ -946,7 +951,7 @@ double Onsite_Proj_tools::cal_stress_dftu(int ik, atom_na, d_wg, vu_tmp, - orbital_corr_tmp, + orb_corr_tmp, becp, dbecp, stress_device); @@ -955,12 +960,13 @@ double Onsite_Proj_tools::cal_stress_dftu(int ik, syncmem_var_d2h_op()(&stress_out, stress_device, 1); delmem_var_op()(stress_device); delmem_complex_op()(vu_tmp); - delmem_int_op()(orbital_corr_tmp); + delmem_int_op()(orb_corr_tmp); + std::cout << "BUG: DFT+U (GPU) stress_out = " << stress_out << std::endl; } else #endif { - orbital_corr_tmp = const_cast(orbital_corr); + orb_corr_tmp = const_cast(orb_corr); vu_tmp = const_cast*>(vu); d_wg = const_cast(h_wg); @@ -974,10 +980,11 @@ double Onsite_Proj_tools::cal_stress_dftu(int ik, atom_na, d_wg, vu_tmp, - orbital_corr_tmp, + orb_corr_tmp, becp, dbecp, &stress_out); +// std::cout << "DFT+U (CPU) stress_out = " << stress_out << std::endl; } return stress_out; diff --git a/tests/11_PW_GPU/BUG_nspin4_u/INPUT b/tests/11_PW_GPU/BUG_nspin4_u/INPUT new file mode 100644 index 00000000000..f23760c0d42 --- /dev/null +++ b/tests/11_PW_GPU/BUG_nspin4_u/INPUT @@ -0,0 +1,48 @@ +INPUT_PARAMETERS +suffix autotest +nbands 40 +device gpu + +calculation scf +ecutwfc 20 +scf_thr 1.0e-8 +scf_nmax 50 +out_chg 0 + +#init_chg file +#out_dos 1 +#dos_sigma 0.05 +#out_band 1 + +smearing_method gaussian +smearing_sigma 0.01 + +#force_thr_ev 0.01 +#relax_method cg +#relax_bfgs_init 0.5 + +mixing_type pulay +mixing_beta 0.3 +mixing_restart 1e-3 +mixing_dmr 1 +mixing_gg0 1.1 + +ks_solver dav_subspace +diago_smooth_ethr true +pw_diag_ndim 2 +basis_type pw +gamma_only 0 +noncolin 1 +lspinorb 1 +cal_force 1 +cal_stress 1 + +#Parameter DFT+U +dft_plus_u 1 +orbital_corr 2 +hubbard_u 5.0 +onsite_radius 3.0 +pseudo_dir ../../PP_ORB +orbital_dir ../../PP_ORB + +pw_seed 1 diff --git a/tests/11_PW_GPU/BUG_nspin4_u/KPT b/tests/11_PW_GPU/BUG_nspin4_u/KPT new file mode 100644 index 00000000000..e769af76382 --- /dev/null +++ b/tests/11_PW_GPU/BUG_nspin4_u/KPT @@ -0,0 +1,4 @@ +K_POINTS +0 +Gamma +2 1 1 0 0 0 diff --git a/tests/11_PW_GPU/BUG_nspin4_u/README b/tests/11_PW_GPU/BUG_nspin4_u/README new file mode 100644 index 00000000000..2f4f21efcc6 --- /dev/null +++ b/tests/11_PW_GPU/BUG_nspin4_u/README @@ -0,0 +1 @@ +GPU: DFTU + NSPIN4, Fe2, multi-k case diff --git a/tests/11_PW_GPU/BUG_nspin4_u/STRU b/tests/11_PW_GPU/BUG_nspin4_u/STRU new file mode 100644 index 00000000000..91021e0a697 --- /dev/null +++ b/tests/11_PW_GPU/BUG_nspin4_u/STRU @@ -0,0 +1,22 @@ +ATOMIC_SPECIES +Fe 1.000 Fe.upf + +NUMERICAL_ORBITAL +Fe_gga_6au_100Ry_4s2p2d1f.orb + +LATTICE_CONSTANT +8.190 + +LATTICE_VECTORS + 1.00 0.50 0.50 + 0.50 1.00 0.50 + 0.50 0.50 1.00 +ATOMIC_POSITIONS +Direct + +Fe +0.0 +2 +0.00 0.00 0.00 mag 1.0 1.0 1.0 +0.51 0.51 0.51 mag 1.0 1.0 1.0 + From f29de470a4ce852ca19b56670922e9bbb0dac86a Mon Sep 17 00:00:00 2001 From: abacus_fixer Date: Wed, 8 Apr 2026 12:54:00 +0800 Subject: [PATCH 20/20] add back bndpar parameter in the BPCG test --- tests/11_PW_GPU/scf_bpcg/INPUT | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/11_PW_GPU/scf_bpcg/INPUT b/tests/11_PW_GPU/scf_bpcg/INPUT index 3631f7c2a2b..53cfde6f436 100644 --- a/tests/11_PW_GPU/scf_bpcg/INPUT +++ b/tests/11_PW_GPU/scf_bpcg/INPUT @@ -15,7 +15,7 @@ smearing_sigma 0.02 ecutwfc 40 scf_thr 1e-7 scf_nmax 100 -#bndpar 2 +bndpar 2 #Parameters (LCAO) basis_type pw