From 3853d025fe526730f68196e318c6a2157ed0d5f4 Mon Sep 17 00:00:00 2001 From: linpz Date: Sat, 20 Jun 2026 15:47:25 +0800 Subject: [PATCH 1/2] add move constructor for several classes --- source/source_estate/module_charge/charge.cpp | 68 +++++++++++++++++++ source/source_estate/module_charge/charge.h | 1 + .../module_dm/density_matrix.cpp | 25 +++++++ .../source_estate/module_dm/density_matrix.h | 3 +- .../module_dm/test/test_dm_constructor.cpp | 30 ++++++++ .../source_estate/module_pot/H_Hartree_pw.h | 1 + source/source_estate/module_pot/pot_base.h | 13 +++- .../source_estate/module_pot/pot_cosikr.cpp | 19 ++++-- source/source_estate/module_pot/pot_cosikr.h | 25 +++---- .../source_estate/module_pot/pot_xc_fdm.cpp | 28 +++++--- source/source_estate/module_pot/pot_xc_fdm.h | 15 ++-- .../module_pot/potential_new.cpp | 41 +++++++++++ .../source_estate/module_pot/potential_new.h | 1 + source/source_estate/test/CMakeLists.txt | 2 +- source/source_estate/test/charge_test.cpp | 33 ++++++++- .../source_estate/test/potential_new_test.cpp | 42 ++++++++++++ source/source_lcao/module_gint/gint_rho.h | 12 +++- source/source_lcao/module_gint/gint_vl.h | 13 +++- .../module_gint/test/CMakeLists.txt | 1 + .../module_gint/test/test_gint_common.cpp | 45 ++++++++++++ .../module_hcontainer/base_matrix.cpp | 2 +- .../module_hcontainer/base_matrix.h | 2 +- .../test/test_hcontainer.cpp | 14 ++++ 23 files changed, 395 insertions(+), 41 deletions(-) diff --git a/source/source_estate/module_charge/charge.cpp b/source/source_estate/module_charge/charge.cpp index 32d0b4f8356..72aa1c8fa02 100644 --- a/source/source_estate/module_charge/charge.cpp +++ b/source/source_estate/module_charge/charge.cpp @@ -31,6 +31,7 @@ #include "source_hamilt/module_xc/xc_functional.h" #include "source_io/module_parameter/parameter.h" +#include #include Charge::Charge() @@ -39,6 +40,73 @@ Charge::Charge() allocate_rho_final_scf = false; // LiuXh add 20180619 } +Charge::Charge(Charge&& other) noexcept + : rho(other.rho), + rho_save(other.rho_save), + rhog(other.rhog), + rhog_save(other.rhog_save), + kin_r(other.kin_r), + kin_r_save(other.kin_r_save), + pgrid(other.pgrid), + _space_rho(other._space_rho), + _space_rho_save(other._space_rho_save), + _space_rhog(other._space_rhog), + _space_rhog_save(other._space_rhog_save), + _space_kin_r(other._space_kin_r), + _space_kin_r_save(other._space_kin_r_save), + nhat(other.nhat), + nhat_save(other.nhat_save), + rho_core(other.rho_core), + rhog_core(other.rhog_core), + prenspin(other.prenspin), + nrxx(other.nrxx), + nxyz(other.nxyz), + ngmc(other.ngmc), + nspin(other.nspin), + rhopw(other.rhopw), + cal_elf(other.cal_elf), + omega_(other.omega_), + allocate_rho(other.allocate_rho), + allocate_rho_final_scf(other.allocate_rho_final_scf) +#ifdef __MPI + , + rec(other.rec), + dis(other.dis) +#endif +{ + other.rho = nullptr; + other.rho_save = nullptr; + other.rhog = nullptr; + other.rhog_save = nullptr; + other.kin_r = nullptr; + other.kin_r_save = nullptr; + other.pgrid = nullptr; + other._space_rho = nullptr; + other._space_rho_save = nullptr; + other._space_rhog = nullptr; + other._space_rhog_save = nullptr; + other._space_kin_r = nullptr; + other._space_kin_r_save = nullptr; + other.nhat = nullptr; + other.nhat_save = nullptr; + other.rho_core = nullptr; + other.rhog_core = nullptr; + other.prenspin = 1; + other.nrxx = 0; + other.nxyz = 0; + other.ngmc = 0; + other.nspin = 0; + other.rhopw = nullptr; + other.cal_elf = false; + other.omega_ = nullptr; + other.allocate_rho = false; + other.allocate_rho_final_scf = false; +#ifdef __MPI + other.rec = nullptr; + other.dis = nullptr; +#endif +} + Charge::~Charge() { this->destroy(); diff --git a/source/source_estate/module_charge/charge.h b/source/source_estate/module_charge/charge.h index 9064dfc5fb8..3f6fbbfe959 100644 --- a/source/source_estate/module_charge/charge.h +++ b/source/source_estate/module_charge/charge.h @@ -19,6 +19,7 @@ class Charge public: Charge(); + Charge(Charge&& other) noexcept; ~Charge(); //========================================================== diff --git a/source/source_estate/module_dm/density_matrix.cpp b/source/source_estate/module_dm/density_matrix.cpp index 7414e00fd3b..9414b27b31a 100644 --- a/source/source_estate/module_dm/density_matrix.cpp +++ b/source/source_estate/module_dm/density_matrix.cpp @@ -9,6 +9,8 @@ #include "source_base/constants.h" #include "source_cell/klist.h" +#include + namespace elecstate { @@ -53,6 +55,29 @@ DensityMatrix::DensityMatrix(const Parallel_Orbitals* paraV_in, const in ModuleBase::Memory::record("DensityMatrix::DMK", this->_DMK.size() * this->_DMK[0].size() * sizeof(TK)); } +template +DensityMatrix::DensityMatrix(DensityMatrix&& other) noexcept + : EDMK(std::move(other.EDMK)), +#ifdef __PEXSI + pexsi_EDM(std::move(other.pexsi_EDM)), +#endif + _DMR(std::move(other._DMR)), + _DMR_save(std::move(other._DMR_save)), + _DMR_grid(std::move(other._DMR_grid)), + _DMK(std::move(other._DMK)), + _kvec_d(std::move(other._kvec_d)), + _paraV(other._paraV), + _nspin(other._nspin), + _nk(other._nk), + dmr_origin_(std::move(other.dmr_origin_)), + dmr_tmp_(other.dmr_tmp_) +{ + other._paraV = nullptr; + other._nspin = 1; + other._nk = 0; + other.dmr_tmp_ = nullptr; +} + // calculate DMR from DMK using blas for multi-k calculation diff --git a/source/source_estate/module_dm/density_matrix.h b/source/source_estate/module_dm/density_matrix.h index 90c51e9c061..b3b748f9b7c 100644 --- a/source/source_estate/module_dm/density_matrix.h +++ b/source/source_estate/module_dm/density_matrix.h @@ -89,6 +89,7 @@ class DensityMatrix const int nspin, const std::vector>& kvec_d, const int nk); + DensityMatrix(DensityMatrix&& other) noexcept; /** * @brief Constructor of class DensityMatrix for gamma-only calculation, where kvector is not required @@ -301,7 +302,7 @@ class DensityMatrix /** * @brief K_Vectors object, which is used to get k-point information */ - const std::vector> _kvec_d; + std::vector> _kvec_d; /** * @brief Parallel_Orbitals object, which contain all information of 2D block cyclic distribution diff --git a/source/source_estate/module_dm/test/test_dm_constructor.cpp b/source/source_estate/module_dm/test/test_dm_constructor.cpp index f82abf8676b..6192976fdbc 100644 --- a/source/source_estate/module_dm/test/test_dm_constructor.cpp +++ b/source/source_estate/module_dm/test/test_dm_constructor.cpp @@ -1,4 +1,5 @@ #include +#include #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -89,6 +90,22 @@ class DMTest : public testing::Test #endif }; +TEST(DensityMatrixMoveTest, MoveConstructorIsNoexcept) +{ + static_assert(std::is_move_constructible>::value, + "DensityMatrix should be move constructible"); + static_assert(std::is_nothrow_move_constructible>::value, + "DensityMatrix move constructor should be noexcept"); + static_assert(std::is_move_constructible, double>>::value, + "DensityMatrix, double> should be move constructible"); + static_assert(std::is_nothrow_move_constructible, double>>::value, + "DensityMatrix, double> move constructor should be noexcept"); + static_assert(std::is_move_constructible, std::complex>>::value, + "DensityMatrix, std::complex> should be move constructible"); + static_assert(std::is_nothrow_move_constructible, std::complex>>::value, + "DensityMatrix, std::complex> move constructor should be noexcept"); +} + TEST_F(DMTest, DMConstructor_GammaOnly) { // construct DM @@ -102,6 +119,19 @@ TEST_F(DMTest, DMConstructor_GammaOnly) EXPECT_EQ(DM.get_DMK_ncol(), paraV->ncol); } +TEST_F(DMTest, MoveConstructorTransfersDMKStorage) +{ + int nspin = 2; + elecstate::DensityMatrix DM(paraV, nspin); + double* dmk_data = DM.get_DMK_vector()[0].data(); + + elecstate::DensityMatrix moved(std::move(DM)); + + EXPECT_EQ(moved.get_DMK_vector()[0].data(), dmk_data); + EXPECT_TRUE(DM.get_DMK_vector().empty()); + EXPECT_TRUE(DM.get_DMR_vector().empty()); +} + TEST_F(DMTest, DMConstructor_nspin1) { // initalize a kvectors diff --git a/source/source_estate/module_pot/H_Hartree_pw.h b/source/source_estate/module_pot/H_Hartree_pw.h index c5ca6bdd0b5..cbba051fa03 100644 --- a/source/source_estate/module_pot/H_Hartree_pw.h +++ b/source/source_estate/module_pot/H_Hartree_pw.h @@ -57,6 +57,7 @@ class PotHartree : public PotBase { public: PotHartree(const ModulePW::PW_Basis* rho_basis_in); + PotHartree(PotHartree&& other) noexcept = default; void cal_v_eff(const Charge*const chg, const UnitCell*const ucell, ModuleBase::matrix& v_eff); }; diff --git a/source/source_estate/module_pot/pot_base.h b/source/source_estate/module_pot/pot_base.h index aaa578bc5bf..cb8dae8e637 100644 --- a/source/source_estate/module_pot/pot_base.h +++ b/source/source_estate/module_pot/pot_base.h @@ -22,6 +22,17 @@ class PotBase { public: PotBase(){} + PotBase(PotBase&& other) noexcept + : fixed_mode(other.fixed_mode), + dynamic_mode(other.dynamic_mode), + rho_basis_(other.rho_basis_), + rho_basis_smooth_(other.rho_basis_smooth_) + { + other.fixed_mode = false; + other.dynamic_mode = false; + other.rho_basis_ = nullptr; + other.rho_basis_smooth_ = nullptr; + } virtual ~PotBase(){} virtual void cal_v_eff(const Charge*const chg, const UnitCell*const ucell, ModuleBase::matrix& v_eff){} @@ -40,4 +51,4 @@ class PotBase } // namespace elecstate -#endif \ No newline at end of file +#endif diff --git a/source/source_estate/module_pot/pot_cosikr.cpp b/source/source_estate/module_pot/pot_cosikr.cpp index 66a5ef7849c..6084a4f51da 100644 --- a/source/source_estate/module_pot/pot_cosikr.cpp +++ b/source/source_estate/module_pot/pot_cosikr.cpp @@ -6,15 +6,16 @@ #include "pot_cosikr.h" #include +#include namespace elecstate { Pot_Cosikr::Pot_Cosikr( - const ModulePW::PW_Basis* rho_basis_in, - const ModuleBase::Vector3 &kvec_d_in, - const std::vector &phase_in, - const std::vector &litude_in) + const ModulePW::PW_Basis* rho_basis_in, + const ModuleBase::Vector3 &kvec_d_in, + const std::vector &phase_in, + const std::vector &litude_in) :kvec_d(kvec_d_in), phase(phase_in), amplitude(amplitude_in) @@ -24,6 +25,14 @@ Pot_Cosikr::Pot_Cosikr( this->fixed_mode = false; } +Pot_Cosikr::Pot_Cosikr(Pot_Cosikr&& other) noexcept + : PotBase(std::move(other)), + kvec_d(other.kvec_d), + phase(std::move(other.phase)), + amplitude(std::move(other.amplitude)) +{ +} + void Pot_Cosikr::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, ModuleBase::matrix &v_eff) { @@ -50,4 +59,4 @@ void Pot_Cosikr::cal_v_eff(const Charge*const chg, const UnitCell*const ucell, M ModuleBase::timer::end("Pot_Cosikr", "cal_veff"); } -} \ No newline at end of file +} diff --git a/source/source_estate/module_pot/pot_cosikr.h b/source/source_estate/module_pot/pot_cosikr.h index d6953a5dfe6..73cd8b06b95 100644 --- a/source/source_estate/module_pot/pot_cosikr.h +++ b/source/source_estate/module_pot/pot_cosikr.h @@ -16,21 +16,22 @@ namespace elecstate // ampitude * cos( 2pi*( k * r + phase ) ) class Pot_Cosikr : public PotBase { - public: - Pot_Cosikr( - const ModulePW::PW_Basis* rho_basis_in, - const ModuleBase::Vector3 &kvec_d_in, - const std::vector &phase_in, - const std::vector &litude_in); + public: + Pot_Cosikr( + const ModulePW::PW_Basis* rho_basis_in, + const ModuleBase::Vector3 &kvec_d_in, + const std::vector &phase_in, + const std::vector &litude_in); + Pot_Cosikr(Pot_Cosikr&& other) noexcept; void cal_v_eff(const Charge*const chg, const UnitCell*const ucell, ModuleBase::matrix &v_eff) override; - private: - const ModuleBase::Vector3 kvec_d; - const std::vector phase; - const std::vector amplitude; -}; + private: + ModuleBase::Vector3 kvec_d; + std::vector phase; + std::vector amplitude; +}; } -#endif \ No newline at end of file +#endif diff --git a/source/source_estate/module_pot/pot_xc_fdm.cpp b/source/source_estate/module_pot/pot_xc_fdm.cpp index d0a49cc1012..905cac9fc12 100644 --- a/source/source_estate/module_pot/pot_xc_fdm.cpp +++ b/source/source_estate/module_pot/pot_xc_fdm.cpp @@ -3,11 +3,13 @@ // DATE : 2025-10-01 //======================= -#include "pot_xc_fdm.h" -#include "source_hamilt/module_xc/xc_functional.h" - -namespace elecstate -{ +#include "pot_xc_fdm.h" +#include "source_hamilt/module_xc/xc_functional.h" + +#include + +namespace elecstate +{ PotXC_FDM::PotXC_FDM( const ModulePW::PW_Basis* rho_basis_in, @@ -21,10 +23,18 @@ PotXC_FDM::PotXC_FDM( const std::tuple etxc_vtxc_v_0 = XC_Functional::v_xc(this->chg_0->nrxx, this->chg_0, ucell); - this->v_xc_0 = std::get<2>(etxc_vtxc_v_0); -} - -void PotXC_FDM::cal_v_eff( + this->v_xc_0 = std::get<2>(etxc_vtxc_v_0); +} + +PotXC_FDM::PotXC_FDM(PotXC_FDM&& other) noexcept + : PotBase(std::move(other)), + chg_0(other.chg_0), + v_xc_0(std::move(other.v_xc_0)) +{ + other.chg_0 = nullptr; +} + +void PotXC_FDM::cal_v_eff( const Charge*const chg_1, const UnitCell*const ucell, ModuleBase::matrix& v_eff) diff --git a/source/source_estate/module_pot/pot_xc_fdm.h b/source/source_estate/module_pot/pot_xc_fdm.h index eb1cd880576..6637fed28aa 100644 --- a/source/source_estate/module_pot/pot_xc_fdm.h +++ b/source/source_estate/module_pot/pot_xc_fdm.h @@ -15,20 +15,21 @@ class PotXC_FDM : public PotBase { public: - PotXC_FDM( - const ModulePW::PW_Basis* rho_basis_in, - const Charge*const chg_0_in, - const UnitCell*const ucell); + PotXC_FDM( + const ModulePW::PW_Basis* rho_basis_in, + const Charge*const chg_0_in, + const UnitCell*const ucell); + PotXC_FDM(PotXC_FDM&& other) noexcept; void cal_v_eff( const Charge*const chg_1, const UnitCell*const ucell, ModuleBase::matrix& v_eff) override; - const Charge*const chg_0 = nullptr; - ModuleBase::matrix v_xc_0; + const Charge* chg_0 = nullptr; + ModuleBase::matrix v_xc_0; }; } // namespace elecstate -#endif \ No newline at end of file +#endif diff --git a/source/source_estate/module_pot/potential_new.cpp b/source/source_estate/module_pot/potential_new.cpp index 70b35cdf000..5f1a72c18ac 100644 --- a/source/source_estate/module_pot/potential_new.cpp +++ b/source/source_estate/module_pot/potential_new.cpp @@ -11,6 +11,7 @@ #include "pot_ml_exx.h" #include +#include namespace elecstate { @@ -38,6 +39,46 @@ Potential::Potential(const ModulePW::PW_Basis* rho_basis_in, this->allocate(); } +Potential::Potential(Potential&& other) noexcept + : PotBase(std::move(other)), + v_eff_fixed(std::move(other.v_eff_fixed)), + v_eff(std::move(other.v_eff)), + veff_smooth(std::move(other.veff_smooth)), + vofk_smooth(std::move(other.vofk_smooth)), + v_xc(std::move(other.v_xc)), + s_veff_smooth(other.s_veff_smooth), + s_vofk_smooth(other.s_vofk_smooth), + d_veff_smooth(other.d_veff_smooth), + d_vofk_smooth(other.d_vofk_smooth), + vofk_eff(std::move(other.vofk_eff)), + fixed_done(other.fixed_done), + etxc_(other.etxc_), + vtxc_(other.vtxc_), + vl_of_0(other.vl_of_0), + components(std::move(other.components)), + ucell_(other.ucell_), + vloc_(other.vloc_), + structure_factors_(other.structure_factors_), + solvent_(other.solvent_), + vsep_cell(other.vsep_cell), + use_gpu_(other.use_gpu_) +{ + other.s_veff_smooth = nullptr; + other.s_vofk_smooth = nullptr; + other.d_veff_smooth = nullptr; + other.d_vofk_smooth = nullptr; + other.fixed_done = false; + other.etxc_ = nullptr; + other.vtxc_ = nullptr; + other.use_gpu_ = false; + other.vl_of_0 = 0.0; + other.ucell_ = nullptr; + other.vloc_ = nullptr; + other.structure_factors_ = nullptr; + other.solvent_ = nullptr; + other.vsep_cell = nullptr; +} + Potential::~Potential() { if (this->components.size() > 0) diff --git a/source/source_estate/module_pot/potential_new.h b/source/source_estate/module_pot/potential_new.h index ca79ca02f63..a6f5714ff5a 100644 --- a/source/source_estate/module_pot/potential_new.h +++ b/source/source_estate/module_pot/potential_new.h @@ -63,6 +63,7 @@ class Potential : public PotBase double* etxc_in, double* vtxc_in, VSep* vsep_cell_in = nullptr); + Potential(Potential&& other) noexcept; ~Potential(); // initialize potential when SCF begin diff --git a/source/source_estate/test/CMakeLists.txt b/source/source_estate/test/CMakeLists.txt index 9bd76ae2486..768d735afec 100644 --- a/source/source_estate/test/CMakeLists.txt +++ b/source/source_estate/test/CMakeLists.txt @@ -75,7 +75,7 @@ AddTest( AddTest( TARGET MODULE_ESTATE_potentials_new LIBS parameter ${math_libs} base device planewave_serial - SOURCES potential_new_test.cpp ../module_pot/potential_new.cpp + SOURCES potential_new_test.cpp ../module_pot/potential_new.cpp ../module_pot/pot_cosikr.cpp ) AddTest( diff --git a/source/source_estate/test/charge_test.cpp b/source/source_estate/test/charge_test.cpp index 261bcc5e3e7..a7c4988b2aa 100644 --- a/source/source_estate/test/charge_test.cpp +++ b/source/source_estate/test/charge_test.cpp @@ -1,6 +1,8 @@ #include "gtest/gtest.h" #include "gmock/gmock.h" +#include + #define private public #define protected public #include "source_cell/unitcell.h" @@ -98,6 +100,36 @@ TEST_F(ChargeTest, Constructor) EXPECT_FALSE(charge->allocate_rho_final_scf); } +TEST(ChargeMoveTest, MoveConstructorIsNoexcept) +{ + static_assert(std::is_move_constructible::value, "Charge should be move constructible"); + static_assert(std::is_nothrow_move_constructible::value, "Charge move constructor should be noexcept"); +} + +TEST_F(ChargeTest, MoveConstructorTransfersAllocatedPointers) +{ + charge->set_rhopw(rhopw); + const bool kin_den = charge->kin_density(); + charge->allocate(PARAM.input.nspin, kin_den); + double** rho = charge->rho; + std::complex** rhog = charge->rhog; + double* space_rho = charge->_space_rho; + double* rho_core = charge->rho_core; + + Charge moved(std::move(*charge)); + + EXPECT_EQ(moved.rho, rho); + EXPECT_EQ(moved.rhog, rhog); + EXPECT_EQ(moved._space_rho, space_rho); + EXPECT_EQ(moved.rho_core, rho_core); + EXPECT_EQ(charge->rho, nullptr); + EXPECT_EQ(charge->rhog, nullptr); + EXPECT_EQ(charge->_space_rho, nullptr); + EXPECT_EQ(charge->rho_core, nullptr); + EXPECT_FALSE(charge->allocate_rho); + EXPECT_FALSE(charge->allocate_rho_final_scf); +} + TEST_F(ChargeTest, Allocate) { // ucell info @@ -220,4 +252,3 @@ TEST_F(ChargeTest, InitFinalScf) charge->init_final_scf(); EXPECT_TRUE(charge->allocate_rho_final_scf); } - diff --git a/source/source_estate/test/potential_new_test.cpp b/source/source_estate/test/potential_new_test.cpp index f91f473efaa..2f1b6f128a8 100644 --- a/source/source_estate/test/potential_new_test.cpp +++ b/source/source_estate/test/potential_new_test.cpp @@ -1,7 +1,12 @@ #include "gtest/gtest.h" +#include #include #define private public +#define protected public +#include "source_estate/module_pot/H_Hartree_pw.h" +#include "source_estate/module_pot/pot_cosikr.h" +#include "source_estate/module_pot/pot_xc_fdm.h" #include "source_estate/module_pot/potential_new.h" #include "source_hamilt/module_xc/xc_functional.h" #include "source_io/module_parameter/parameter.h" @@ -164,6 +169,43 @@ class PotentialNewTest : public ::testing::Test } }; +TEST(PotentialMoveTest, MoveConstructorsAreNoexcept) +{ + static_assert(std::is_move_constructible::value, + "Potential should be move constructible"); + static_assert(std::is_nothrow_move_constructible::value, + "Potential move constructor should be noexcept"); + static_assert(std::is_move_constructible::value, + "PotHartree should be move constructible"); + static_assert(std::is_nothrow_move_constructible::value, + "PotHartree move constructor should be noexcept"); + static_assert(std::is_move_constructible::value, + "PotXC_FDM should be move constructible"); + static_assert(std::is_nothrow_move_constructible::value, + "PotXC_FDM move constructor should be noexcept"); + static_assert(std::is_move_constructible::value, + "Pot_Cosikr should be move constructible"); + static_assert(std::is_nothrow_move_constructible::value, + "Pot_Cosikr move constructor should be noexcept"); +} + +TEST(PotentialMoveTest, PotCosikrMoveStealsVectorStorage) +{ + ModuleBase::Vector3 kvec(1.0, 2.0, 3.0); + std::vector phase{0.25, 0.5}; + std::vector amplitude{1.5, 2.5}; + elecstate::Pot_Cosikr pot(nullptr, kvec, phase, amplitude); + const double* phase_data = pot.phase.data(); + const double* amplitude_data = pot.amplitude.data(); + + elecstate::Pot_Cosikr moved(std::move(pot)); + + EXPECT_EQ(moved.phase.data(), phase_data); + EXPECT_EQ(moved.amplitude.data(), amplitude_data); + EXPECT_TRUE(pot.phase.empty()); + EXPECT_TRUE(pot.amplitude.empty()); +} + TEST_F(PotentialNewTest, ConstructorCPUDouble) { rhopw->nrxx = 100; diff --git a/source/source_lcao/module_gint/gint_rho.h b/source/source_lcao/module_gint/gint_rho.h index 5bcede5f19f..0febcae958a 100644 --- a/source/source_lcao/module_gint/gint_rho.h +++ b/source/source_lcao/module_gint/gint_rho.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "source_lcao/module_hcontainer/hcontainer.h" #include "gint.h" @@ -18,6 +19,15 @@ class Gint_rho : public Gint double **rho, bool is_dm_symm = true) : dm_vec_(dm_vec), nspin_(nspin), is_dm_symm_(is_dm_symm), rho_(rho) {} + Gint_rho(Gint_rho&& other) noexcept + : Gint(std::move(other)), + dm_vec_(std::move(other.dm_vec_)), + nspin_(other.nspin_), + is_dm_symm_(other.is_dm_symm_), + rho_(other.rho_) + { + other.rho_ = nullptr; + } void cal_gint(); @@ -36,7 +46,7 @@ class Gint_rho : public Gint const std::vector& rho_data) const; // input - const std::vector*> dm_vec_; + std::vector*> dm_vec_; const int nspin_; // if true, it means the DMR matrix is symmetric, diff --git a/source/source_lcao/module_gint/gint_vl.h b/source/source_lcao/module_gint/gint_vl.h index cd21f178c01..ca6eb877d16 100644 --- a/source/source_lcao/module_gint/gint_vl.h +++ b/source/source_lcao/module_gint/gint_vl.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include "source_lcao/module_hcontainer/hcontainer.h" #include "gint.h" @@ -15,7 +16,17 @@ class Gint_vl : public Gint Gint_vl( const double* vr_eff, HContainer* hR) - : vr_eff_(vr_eff), hR_(hR), dr3_(gint_info_->get_mgrid_volume()) {} + : vr_eff_(vr_eff), hR_(hR), dr3_(gint_info_ == nullptr ? 0.0 : gint_info_->get_mgrid_volume()) {} + Gint_vl(Gint_vl&& other) noexcept + : Gint(std::move(other)), + vr_eff_(other.vr_eff_), + hR_(other.hR_), + dr3_(other.dr3_) + { + other.vr_eff_ = nullptr; + other.hR_ = nullptr; + other.dr3_ = 0.0; + } void cal_gint(); diff --git a/source/source_lcao/module_gint/test/CMakeLists.txt b/source/source_lcao/module_gint/test/CMakeLists.txt index a6c0267ec32..b1c3236232a 100644 --- a/source/source_lcao/module_gint/test/CMakeLists.txt +++ b/source/source_lcao/module_gint/test/CMakeLists.txt @@ -11,6 +11,7 @@ AddTest( LIBS parameter ${math_libs} psi base device SOURCES test_gint_common.cpp tmp_mocks.cpp + ../gint.cpp ../gint_common.cpp ../../module_hcontainer/base_matrix.cpp ../../module_hcontainer/hcontainer.cpp diff --git a/source/source_lcao/module_gint/test/test_gint_common.cpp b/source/source_lcao/module_gint/test/test_gint_common.cpp index 4d6a21607ca..9581214735e 100644 --- a/source/source_lcao/module_gint/test/test_gint_common.cpp +++ b/source/source_lcao/module_gint/test/test_gint_common.cpp @@ -2,7 +2,12 @@ #include "../gint_common.h" #include "../gint_info.h" +#define private public +#include "../gint_rho.h" +#include "../gint_vl.h" +#undef private +#include #include class GintCommonTest : public ::testing::Test @@ -54,6 +59,46 @@ class GintCommonTest : public ::testing::Test ModuleGint::GintInfo* gint_info_ = nullptr; }; +TEST(GintMoveTest, MoveConstructorsAreNoexcept) +{ + static_assert(std::is_move_constructible::value, + "Gint_rho should be move constructible"); + static_assert(std::is_nothrow_move_constructible::value, + "Gint_rho move constructor should be noexcept"); + static_assert(std::is_move_constructible::value, + "Gint_vl should be move constructible"); + static_assert(std::is_nothrow_move_constructible::value, + "Gint_vl move constructor should be noexcept"); +} + +TEST_F(GintCommonTest, MoveConstructorsTransferPointerMembers) +{ + double rho_storage[2]{}; + double* rho_rows[1]{rho_storage}; + hamilt::HContainer dm; + std::vector*> dm_vec{&dm}; + ModuleGint::Gint_rho rho(dm_vec, 1, rho_rows); + const auto dm_vec_data = rho.dm_vec_.data(); + + ModuleGint::Gint_rho moved_rho(std::move(rho)); + + EXPECT_EQ(moved_rho.dm_vec_.data(), dm_vec_data); + EXPECT_TRUE(rho.dm_vec_.empty()); + EXPECT_EQ(moved_rho.rho_, rho_rows); + EXPECT_EQ(rho.rho_, nullptr); + + double vr_eff[1]{}; + hamilt::HContainer hR; + ModuleGint::Gint_vl vl(vr_eff, &hR); + + ModuleGint::Gint_vl moved_vl(std::move(vl)); + + EXPECT_EQ(moved_vl.vr_eff_, vr_eff); + EXPECT_EQ(moved_vl.hR_, &hR); + EXPECT_EQ(vl.vr_eff_, nullptr); + EXPECT_EQ(vl.hR_, nullptr); +} + TEST_F(GintCommonTest, GetHrFloatBuildsExpectedShape) { auto hr = gint_info_->get_hr(); diff --git a/source/source_lcao/module_hcontainer/base_matrix.cpp b/source/source_lcao/module_hcontainer/base_matrix.cpp index 5e6c7b00a28..23b77b0c0f4 100644 --- a/source/source_lcao/module_hcontainer/base_matrix.cpp +++ b/source/source_lcao/module_hcontainer/base_matrix.cpp @@ -18,7 +18,7 @@ BaseMatrix::BaseMatrix(const int& nrow_, const int& ncol_, T* data_existed) // move constructor template -BaseMatrix::BaseMatrix(BaseMatrix&& matrix) +BaseMatrix::BaseMatrix(BaseMatrix&& matrix) noexcept { this->nrow_local = matrix.nrow_local; this->ncol_local = matrix.ncol_local; diff --git a/source/source_lcao/module_hcontainer/base_matrix.h b/source/source_lcao/module_hcontainer/base_matrix.h index 36044ee187f..990c58b3929 100644 --- a/source/source_lcao/module_hcontainer/base_matrix.h +++ b/source/source_lcao/module_hcontainer/base_matrix.h @@ -24,7 +24,7 @@ class BaseMatrix // copy constructor BaseMatrix(const BaseMatrix& matrix); // move constructor - BaseMatrix(BaseMatrix&& matrix); + BaseMatrix(BaseMatrix&& matrix) noexcept; // Destructor of class BaseMatrix ~BaseMatrix(); diff --git a/source/source_lcao/module_hcontainer/test/test_hcontainer.cpp b/source/source_lcao/module_hcontainer/test/test_hcontainer.cpp index ef948f87d8d..01431e1a1f3 100644 --- a/source/source_lcao/module_hcontainer/test/test_hcontainer.cpp +++ b/source/source_lcao/module_hcontainer/test/test_hcontainer.cpp @@ -1,6 +1,8 @@ #include "gtest/gtest.h" #include "source_lcao/module_hcontainer/hcontainer.h" +#include + /** * Unit test of HContainer * HContainer is a container of hamilt::AtomPair, in this test, we test the following functions: @@ -46,6 +48,18 @@ class HContainerTest : public ::testing::Test hamilt::HContainer* HR; }; +TEST(HContainerMoveTest, MoveConstructorIsNoexcept) +{ + static_assert(std::is_nothrow_move_constructible>::value, + "BaseMatrix move constructor should be noexcept"); + static_assert(std::is_nothrow_move_constructible>::value, + "AtomPair move constructor should be noexcept"); + static_assert(std::is_nothrow_move_constructible>::value, + "HContainer move constructor should be noexcept"); + static_assert(std::is_nothrow_move_constructible>>::value, + "HContainer> move constructor should be noexcept"); +} + // using TEST_F to test HContainer::insert_pair TEST_F(HContainerTest, insert_pair) { From df55ca6e770babec8d15e6dcb36305a76c5031e4 Mon Sep 17 00:00:00 2001 From: linpz Date: Sun, 21 Jun 2026 02:18:26 +0800 Subject: [PATCH 2/2] remove test --- .../module_dm/test/test_dm_constructor.cpp | 30 ------------- source/source_estate/test/CMakeLists.txt | 2 +- source/source_estate/test/charge_test.cpp | 33 +------------- .../source_estate/test/potential_new_test.cpp | 42 ----------------- .../module_gint/test/CMakeLists.txt | 1 - .../module_gint/test/test_gint_common.cpp | 45 ------------------- .../test/test_hcontainer.cpp | 14 ------ 7 files changed, 2 insertions(+), 165 deletions(-) diff --git a/source/source_estate/module_dm/test/test_dm_constructor.cpp b/source/source_estate/module_dm/test/test_dm_constructor.cpp index 6192976fdbc..f82abf8676b 100644 --- a/source/source_estate/module_dm/test/test_dm_constructor.cpp +++ b/source/source_estate/module_dm/test/test_dm_constructor.cpp @@ -1,5 +1,4 @@ #include -#include #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -90,22 +89,6 @@ class DMTest : public testing::Test #endif }; -TEST(DensityMatrixMoveTest, MoveConstructorIsNoexcept) -{ - static_assert(std::is_move_constructible>::value, - "DensityMatrix should be move constructible"); - static_assert(std::is_nothrow_move_constructible>::value, - "DensityMatrix move constructor should be noexcept"); - static_assert(std::is_move_constructible, double>>::value, - "DensityMatrix, double> should be move constructible"); - static_assert(std::is_nothrow_move_constructible, double>>::value, - "DensityMatrix, double> move constructor should be noexcept"); - static_assert(std::is_move_constructible, std::complex>>::value, - "DensityMatrix, std::complex> should be move constructible"); - static_assert(std::is_nothrow_move_constructible, std::complex>>::value, - "DensityMatrix, std::complex> move constructor should be noexcept"); -} - TEST_F(DMTest, DMConstructor_GammaOnly) { // construct DM @@ -119,19 +102,6 @@ TEST_F(DMTest, DMConstructor_GammaOnly) EXPECT_EQ(DM.get_DMK_ncol(), paraV->ncol); } -TEST_F(DMTest, MoveConstructorTransfersDMKStorage) -{ - int nspin = 2; - elecstate::DensityMatrix DM(paraV, nspin); - double* dmk_data = DM.get_DMK_vector()[0].data(); - - elecstate::DensityMatrix moved(std::move(DM)); - - EXPECT_EQ(moved.get_DMK_vector()[0].data(), dmk_data); - EXPECT_TRUE(DM.get_DMK_vector().empty()); - EXPECT_TRUE(DM.get_DMR_vector().empty()); -} - TEST_F(DMTest, DMConstructor_nspin1) { // initalize a kvectors diff --git a/source/source_estate/test/CMakeLists.txt b/source/source_estate/test/CMakeLists.txt index 768d735afec..9bd76ae2486 100644 --- a/source/source_estate/test/CMakeLists.txt +++ b/source/source_estate/test/CMakeLists.txt @@ -75,7 +75,7 @@ AddTest( AddTest( TARGET MODULE_ESTATE_potentials_new LIBS parameter ${math_libs} base device planewave_serial - SOURCES potential_new_test.cpp ../module_pot/potential_new.cpp ../module_pot/pot_cosikr.cpp + SOURCES potential_new_test.cpp ../module_pot/potential_new.cpp ) AddTest( diff --git a/source/source_estate/test/charge_test.cpp b/source/source_estate/test/charge_test.cpp index a7c4988b2aa..261bcc5e3e7 100644 --- a/source/source_estate/test/charge_test.cpp +++ b/source/source_estate/test/charge_test.cpp @@ -1,8 +1,6 @@ #include "gtest/gtest.h" #include "gmock/gmock.h" -#include - #define private public #define protected public #include "source_cell/unitcell.h" @@ -100,36 +98,6 @@ TEST_F(ChargeTest, Constructor) EXPECT_FALSE(charge->allocate_rho_final_scf); } -TEST(ChargeMoveTest, MoveConstructorIsNoexcept) -{ - static_assert(std::is_move_constructible::value, "Charge should be move constructible"); - static_assert(std::is_nothrow_move_constructible::value, "Charge move constructor should be noexcept"); -} - -TEST_F(ChargeTest, MoveConstructorTransfersAllocatedPointers) -{ - charge->set_rhopw(rhopw); - const bool kin_den = charge->kin_density(); - charge->allocate(PARAM.input.nspin, kin_den); - double** rho = charge->rho; - std::complex** rhog = charge->rhog; - double* space_rho = charge->_space_rho; - double* rho_core = charge->rho_core; - - Charge moved(std::move(*charge)); - - EXPECT_EQ(moved.rho, rho); - EXPECT_EQ(moved.rhog, rhog); - EXPECT_EQ(moved._space_rho, space_rho); - EXPECT_EQ(moved.rho_core, rho_core); - EXPECT_EQ(charge->rho, nullptr); - EXPECT_EQ(charge->rhog, nullptr); - EXPECT_EQ(charge->_space_rho, nullptr); - EXPECT_EQ(charge->rho_core, nullptr); - EXPECT_FALSE(charge->allocate_rho); - EXPECT_FALSE(charge->allocate_rho_final_scf); -} - TEST_F(ChargeTest, Allocate) { // ucell info @@ -252,3 +220,4 @@ TEST_F(ChargeTest, InitFinalScf) charge->init_final_scf(); EXPECT_TRUE(charge->allocate_rho_final_scf); } + diff --git a/source/source_estate/test/potential_new_test.cpp b/source/source_estate/test/potential_new_test.cpp index 2f1b6f128a8..f91f473efaa 100644 --- a/source/source_estate/test/potential_new_test.cpp +++ b/source/source_estate/test/potential_new_test.cpp @@ -1,12 +1,7 @@ #include "gtest/gtest.h" -#include #include #define private public -#define protected public -#include "source_estate/module_pot/H_Hartree_pw.h" -#include "source_estate/module_pot/pot_cosikr.h" -#include "source_estate/module_pot/pot_xc_fdm.h" #include "source_estate/module_pot/potential_new.h" #include "source_hamilt/module_xc/xc_functional.h" #include "source_io/module_parameter/parameter.h" @@ -169,43 +164,6 @@ class PotentialNewTest : public ::testing::Test } }; -TEST(PotentialMoveTest, MoveConstructorsAreNoexcept) -{ - static_assert(std::is_move_constructible::value, - "Potential should be move constructible"); - static_assert(std::is_nothrow_move_constructible::value, - "Potential move constructor should be noexcept"); - static_assert(std::is_move_constructible::value, - "PotHartree should be move constructible"); - static_assert(std::is_nothrow_move_constructible::value, - "PotHartree move constructor should be noexcept"); - static_assert(std::is_move_constructible::value, - "PotXC_FDM should be move constructible"); - static_assert(std::is_nothrow_move_constructible::value, - "PotXC_FDM move constructor should be noexcept"); - static_assert(std::is_move_constructible::value, - "Pot_Cosikr should be move constructible"); - static_assert(std::is_nothrow_move_constructible::value, - "Pot_Cosikr move constructor should be noexcept"); -} - -TEST(PotentialMoveTest, PotCosikrMoveStealsVectorStorage) -{ - ModuleBase::Vector3 kvec(1.0, 2.0, 3.0); - std::vector phase{0.25, 0.5}; - std::vector amplitude{1.5, 2.5}; - elecstate::Pot_Cosikr pot(nullptr, kvec, phase, amplitude); - const double* phase_data = pot.phase.data(); - const double* amplitude_data = pot.amplitude.data(); - - elecstate::Pot_Cosikr moved(std::move(pot)); - - EXPECT_EQ(moved.phase.data(), phase_data); - EXPECT_EQ(moved.amplitude.data(), amplitude_data); - EXPECT_TRUE(pot.phase.empty()); - EXPECT_TRUE(pot.amplitude.empty()); -} - TEST_F(PotentialNewTest, ConstructorCPUDouble) { rhopw->nrxx = 100; diff --git a/source/source_lcao/module_gint/test/CMakeLists.txt b/source/source_lcao/module_gint/test/CMakeLists.txt index b1c3236232a..a6c0267ec32 100644 --- a/source/source_lcao/module_gint/test/CMakeLists.txt +++ b/source/source_lcao/module_gint/test/CMakeLists.txt @@ -11,7 +11,6 @@ AddTest( LIBS parameter ${math_libs} psi base device SOURCES test_gint_common.cpp tmp_mocks.cpp - ../gint.cpp ../gint_common.cpp ../../module_hcontainer/base_matrix.cpp ../../module_hcontainer/hcontainer.cpp diff --git a/source/source_lcao/module_gint/test/test_gint_common.cpp b/source/source_lcao/module_gint/test/test_gint_common.cpp index 9581214735e..4d6a21607ca 100644 --- a/source/source_lcao/module_gint/test/test_gint_common.cpp +++ b/source/source_lcao/module_gint/test/test_gint_common.cpp @@ -2,12 +2,7 @@ #include "../gint_common.h" #include "../gint_info.h" -#define private public -#include "../gint_rho.h" -#include "../gint_vl.h" -#undef private -#include #include class GintCommonTest : public ::testing::Test @@ -59,46 +54,6 @@ class GintCommonTest : public ::testing::Test ModuleGint::GintInfo* gint_info_ = nullptr; }; -TEST(GintMoveTest, MoveConstructorsAreNoexcept) -{ - static_assert(std::is_move_constructible::value, - "Gint_rho should be move constructible"); - static_assert(std::is_nothrow_move_constructible::value, - "Gint_rho move constructor should be noexcept"); - static_assert(std::is_move_constructible::value, - "Gint_vl should be move constructible"); - static_assert(std::is_nothrow_move_constructible::value, - "Gint_vl move constructor should be noexcept"); -} - -TEST_F(GintCommonTest, MoveConstructorsTransferPointerMembers) -{ - double rho_storage[2]{}; - double* rho_rows[1]{rho_storage}; - hamilt::HContainer dm; - std::vector*> dm_vec{&dm}; - ModuleGint::Gint_rho rho(dm_vec, 1, rho_rows); - const auto dm_vec_data = rho.dm_vec_.data(); - - ModuleGint::Gint_rho moved_rho(std::move(rho)); - - EXPECT_EQ(moved_rho.dm_vec_.data(), dm_vec_data); - EXPECT_TRUE(rho.dm_vec_.empty()); - EXPECT_EQ(moved_rho.rho_, rho_rows); - EXPECT_EQ(rho.rho_, nullptr); - - double vr_eff[1]{}; - hamilt::HContainer hR; - ModuleGint::Gint_vl vl(vr_eff, &hR); - - ModuleGint::Gint_vl moved_vl(std::move(vl)); - - EXPECT_EQ(moved_vl.vr_eff_, vr_eff); - EXPECT_EQ(moved_vl.hR_, &hR); - EXPECT_EQ(vl.vr_eff_, nullptr); - EXPECT_EQ(vl.hR_, nullptr); -} - TEST_F(GintCommonTest, GetHrFloatBuildsExpectedShape) { auto hr = gint_info_->get_hr(); diff --git a/source/source_lcao/module_hcontainer/test/test_hcontainer.cpp b/source/source_lcao/module_hcontainer/test/test_hcontainer.cpp index 01431e1a1f3..ef948f87d8d 100644 --- a/source/source_lcao/module_hcontainer/test/test_hcontainer.cpp +++ b/source/source_lcao/module_hcontainer/test/test_hcontainer.cpp @@ -1,8 +1,6 @@ #include "gtest/gtest.h" #include "source_lcao/module_hcontainer/hcontainer.h" -#include - /** * Unit test of HContainer * HContainer is a container of hamilt::AtomPair, in this test, we test the following functions: @@ -48,18 +46,6 @@ class HContainerTest : public ::testing::Test hamilt::HContainer* HR; }; -TEST(HContainerMoveTest, MoveConstructorIsNoexcept) -{ - static_assert(std::is_nothrow_move_constructible>::value, - "BaseMatrix move constructor should be noexcept"); - static_assert(std::is_nothrow_move_constructible>::value, - "AtomPair move constructor should be noexcept"); - static_assert(std::is_nothrow_move_constructible>::value, - "HContainer move constructor should be noexcept"); - static_assert(std::is_nothrow_move_constructible>>::value, - "HContainer> move constructor should be noexcept"); -} - // using TEST_F to test HContainer::insert_pair TEST_F(HContainerTest, insert_pair) {