diff --git a/source/source_relax/ions_move_basic.cpp b/source/source_relax/ions_move_basic.cpp index 443570403c6..f2299cd77b3 100644 --- a/source/source_relax/ions_move_basic.cpp +++ b/source/source_relax/ions_move_basic.cpp @@ -209,7 +209,7 @@ void Ions_Move_Basic::terminate(const bool converged, const int update_iter, con return; } -void Ions_Move_Basic::setup_etot(const double &energy_in, const bool judgement, const int istep, std::ofstream& ofs, std::vector& etot_info) +void Ions_Move_Basic::setup_etot(const double &energy_in, const int istep, std::vector& etot_info) { // etot_info[0] = etot (current total energy) // etot_info[1] = etot_p (previous total energy) @@ -224,10 +224,6 @@ void Ions_Move_Basic::setup_etot(const double &energy_in, const bool judgement, { etot_info[1] = etot_info[0]; etot_info[0] = energy_in; - if (etot_info[1] > etot_info[0]) - { - etot_info[1] = etot_info[0]; - } } } diff --git a/source/source_relax/ions_move_basic.h b/source/source_relax/ions_move_basic.h index 482e3559e5e..c67dd8fa81c 100644 --- a/source/source_relax/ions_move_basic.h +++ b/source/source_relax/ions_move_basic.h @@ -76,7 +76,7 @@ void terminate(const bool converged, const int update_iter, const UnitCell &ucel * @param istep Current ionic step index * @param etot_info Energy information array [etot, etot_p, ediff] */ -void setup_etot(const double &energy_in, const bool judgement, const int istep, std::ofstream& ofs, std::vector& etot_info); +void setup_etot(const double &energy_in, const int istep, std::vector& etot_info); /** * @brief Compute dot product of two vectors. diff --git a/source/source_relax/ions_move_bfgs.cpp b/source/source_relax/ions_move_bfgs.cpp index 3763a43ac42..076e5e143dd 100644 --- a/source/source_relax/ions_move_bfgs.cpp +++ b/source/source_relax/ions_move_bfgs.cpp @@ -51,7 +51,7 @@ bool Ions_Move_BFGS::start(UnitCell& ucell, const ModuleBase::matrix& force, con std::vector pos_tmp(3 * ucell.nat); Ions_Move_Basic::setup_gradient(ucell, force, pos_tmp.data(), this->grad.data(), ofs); } - Ions_Move_Basic::setup_etot(energy_in, false, istep, ofs, etot_info); + Ions_Move_Basic::setup_etot(energy_in, istep, etot_info); bool converged = Ions_Move_Basic::check_converged(ucell, this->grad.data(), update_iter, ofs, etot_info); if (converged) diff --git a/source/source_relax/ions_move_cg.cpp b/source/source_relax/ions_move_cg.cpp index 8c7c23d10df..556d7c39202 100644 --- a/source/source_relax/ions_move_cg.cpp +++ b/source/source_relax/ions_move_cg.cpp @@ -72,7 +72,7 @@ bool Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const } Ions_Move_Basic::setup_gradient(ucell, force, pos.data(), grad.data(), ofs); - Ions_Move_Basic::setup_etot(etot_in, 0, istep, ofs, etot_info); + Ions_Move_Basic::setup_etot(etot_in, istep, etot_info); bool converged = false; if (flag == 0) diff --git a/source/source_relax/ions_move_sd.cpp b/source/source_relax/ions_move_sd.cpp index 276297ef8eb..252e2882a95 100644 --- a/source/source_relax/ions_move_sd.cpp +++ b/source/source_relax/ions_move_sd.cpp @@ -32,8 +32,7 @@ bool Ions_Move_SD::start(UnitCell& ucell, const ModuleBase::matrix& force, const std::vector grad(dim, 0.0); std::vector move(dim, 0.0); - bool judgement = false; - Ions_Move_Basic::setup_etot(etot_in, judgement, istep, ofs, etot_info); + Ions_Move_Basic::setup_etot(etot_in, istep, etot_info); Ions_Move_Basic::setup_gradient(ucell, force, pos.data(), grad.data(), ofs); if (istep == 1 || etot_in <= energy_saved) diff --git a/source/source_relax/lattice_change_basic.cpp b/source/source_relax/lattice_change_basic.cpp index 7f0b4a26124..fdfb88485e5 100644 --- a/source/source_relax/lattice_change_basic.cpp +++ b/source/source_relax/lattice_change_basic.cpp @@ -343,7 +343,7 @@ void Lattice_Change_Basic::terminate(const bool converged, std::ofstream& ofs) return; } -void Lattice_Change_Basic::setup_etot(const double &energy_in, const bool judgement, std::vector& etot_info) +void Lattice_Change_Basic::setup_etot(const double &energy_in, std::vector& etot_info) { // etot_info[0] = etot (current total energy) // etot_info[1] = etot_p (previous total energy) @@ -357,19 +357,9 @@ void Lattice_Change_Basic::setup_etot(const double &energy_in, const bool judgem } else { - if (judgement) - { - etot_info[0] = energy_in; - if (etot_info[1] > etot_info[0]) - { - etot_info[1] = etot_info[0]; - } - } - else // for bfgs - { - etot_info[1] = etot_info[0]; - etot_info[0] = energy_in; - } + + etot_info[1] = etot_info[0]; + etot_info[0] = energy_in; } return; diff --git a/source/source_relax/lattice_change_basic.h b/source/source_relax/lattice_change_basic.h index e3d92162627..2abdbc2806f 100644 --- a/source/source_relax/lattice_change_basic.h +++ b/source/source_relax/lattice_change_basic.h @@ -68,6 +68,6 @@ void terminate(const bool converged, std::ofstream& ofs); * @param judgement Flag for SD method (true) or BFGS (false) * @param etot_info Vector containing [etot, etot_p] */ -void setup_etot(const double &energy_in, const bool judgement, std::vector& etot_info); +void setup_etot(const double &energy_in, std::vector& etot_info); } // namespace Lattice_Change_Basic #endif diff --git a/source/source_relax/lattice_change_cg.cpp b/source/source_relax/lattice_change_cg.cpp index 171979cf7c0..ece7ba3ecf1 100644 --- a/source/source_relax/lattice_change_cg.cpp +++ b/source/source_relax/lattice_change_cg.cpp @@ -77,7 +77,7 @@ bool Lattice_Change_CG::start(UnitCell &ucell, const ModuleBase::matrix &stress_ ModuleBase::matrix stress(stress_in); Lattice_Change_Basic::setup_gradient(ucell, lat.data(), grad.data(), stress); - Lattice_Change_Basic::setup_etot(etot_in, 0, etot_info); + Lattice_Change_Basic::setup_etot(etot_in, etot_info); bool converged = false; if (flag == 0) diff --git a/source/source_relax/test/ions_move_basic_test.cpp b/source/source_relax/test/ions_move_basic_test.cpp index 7b6598cac51..9c042539ff8 100644 --- a/source/source_relax/test/ions_move_basic_test.cpp +++ b/source/source_relax/test/ions_move_basic_test.cpp @@ -265,78 +265,17 @@ TEST_F(IonsMoveBasicTest, TerminateNotConverged) EXPECT_THAT(ofs_output , ::testing::HasSubstr(expected_ofs)); } -// Test the setup_etot() function case 1 -TEST_F(IonsMoveBasicTest, SetupEtotCase1) -{ - // Initialize data - const int istep = 1; - std::vector etot_info = {2.0, 1.0, 0.0}; - double energy_in = 3.0; - bool judgement = true; - - // Call the function being tested - std::ofstream ofs("/dev/null"); - Ions_Move_Basic::setup_etot(energy_in, judgement, istep, ofs, etot_info); - ofs.close(); - - // Check the results - EXPECT_DOUBLE_EQ(etot_info[1], 3.0); - EXPECT_DOUBLE_EQ(etot_info[0], 3.0); - EXPECT_DOUBLE_EQ(etot_info[0] - etot_info[1], 0.0); -} - -// Test the setup_etot() function case 2 -TEST_F(IonsMoveBasicTest, SetupEtotCase2) -{ - // Initialize data - const int istep = 2; - std::vector etot_info = {2.0, 4.0}; - double energy_in = 3.0; - bool judgement = true; - - // Call the function being tested - std::ofstream ofs("/dev/null"); - Ions_Move_Basic::setup_etot(energy_in, judgement, istep, ofs, etot_info); - ofs.close(); - - // Check the results - EXPECT_DOUBLE_EQ(etot_info[1], 2.0); - EXPECT_DOUBLE_EQ(etot_info[0], 3.0); - EXPECT_DOUBLE_EQ(etot_info[0] - etot_info[1], 1.0); -} - -// Test the setup_etot() function case 3 -TEST_F(IonsMoveBasicTest, SetupEtotCase3) -{ - // Initialize data - const int istep = 2; - std::vector etot_info = {2.0, 1.0}; - double energy_in = 3.0; - bool judgement = true; - - // Call the function being tested - std::ofstream ofs("/dev/null"); - Ions_Move_Basic::setup_etot(energy_in, judgement, istep, ofs, etot_info); - ofs.close(); - - // Check the results - EXPECT_DOUBLE_EQ(etot_info[1], 2.0); - EXPECT_DOUBLE_EQ(etot_info[0], 3.0); - EXPECT_DOUBLE_EQ(etot_info[0] - etot_info[1], 1.0); -} - -// Test the setup_etot() function case 4 -TEST_F(IonsMoveBasicTest, SetupEtotCase4) +// Test the setup_etot() function +TEST_F(IonsMoveBasicTest, SetupEtot) { // Initialize data const int istep = 2; std::vector etot_info = {2.0, 1.0}; double energy_in = 3.0; - bool judgement = false; // Call the function being tested std::ofstream ofs("/dev/null"); - Ions_Move_Basic::setup_etot(energy_in, judgement, istep, ofs, etot_info); + Ions_Move_Basic::setup_etot(energy_in, istep, etot_info); ofs.close(); // Check the results diff --git a/source/source_relax/test/lattice_change_basic_test.cpp b/source/source_relax/test/lattice_change_basic_test.cpp index d9ddb0ffccf..a75a7662a7a 100644 --- a/source/source_relax/test/lattice_change_basic_test.cpp +++ b/source/source_relax/test/lattice_change_basic_test.cpp @@ -475,54 +475,14 @@ TEST_F(LatticeChangeBasicTest, TerminateNotConverged) std::remove("test_terminate_not_converged.log"); } -TEST_F(LatticeChangeBasicTest, SetupEtotStressStep1) -{ - Lattice_Change_Basic::stress_step = 1; - double energy_in = 100.0; - std::vector etot_info(2, 0.0); - - Lattice_Change_Basic::setup_etot(energy_in, true, etot_info); - - EXPECT_DOUBLE_EQ(energy_in, etot_info[1]); - EXPECT_DOUBLE_EQ(energy_in, etot_info[0]); - EXPECT_DOUBLE_EQ(0.0, etot_info[0] - etot_info[1]); -} - -TEST_F(LatticeChangeBasicTest, SetupEtotJudgementTrueHigherEnergy) -{ - Lattice_Change_Basic::stress_step = 2; - double energy_in = 90.0; - std::vector etot_info = {0.0, 100.0}; - - Lattice_Change_Basic::setup_etot(energy_in, true, etot_info); - - // When judgement=true and etot_p > etot, etot_p is updated to etot - EXPECT_DOUBLE_EQ(90.0, etot_info[0]); - EXPECT_DOUBLE_EQ(90.0, etot_info[1]); - EXPECT_DOUBLE_EQ(0.0, etot_info[0] - etot_info[1]); -} - -TEST_F(LatticeChangeBasicTest, SetupEtotJudgementTrueLowerEnergy) -{ - Lattice_Change_Basic::stress_step = 2; - double energy_in = 100.0; - std::vector etot_info = {0.0, 90.0}; - - Lattice_Change_Basic::setup_etot(energy_in, true, etot_info); - - // When judgement=true and etot_p <= etot, etot_p is not updated - EXPECT_DOUBLE_EQ(100.0, etot_info[0]); - EXPECT_DOUBLE_EQ(90.0, etot_info[1]); - EXPECT_DOUBLE_EQ(10.0, etot_info[0] - etot_info[1]); -} -TEST_F(LatticeChangeBasicTest, SetupEtotJudgementFalse) +TEST_F(LatticeChangeBasicTest, SetupEtot) { Lattice_Change_Basic::stress_step = 2; double energy_in = 80.0; std::vector etot_info = {100.0, 90.0}; - Lattice_Change_Basic::setup_etot(energy_in, false, etot_info); + Lattice_Change_Basic::setup_etot(energy_in, etot_info); EXPECT_DOUBLE_EQ(100.0, etot_info[1]); EXPECT_DOUBLE_EQ(80.0, etot_info[0]);