Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions source/source_relax/ions_move_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>& etot_info)
void Ions_Move_Basic::setup_etot(const double &energy_in, const int istep, std::vector<double>& etot_info)
{
// etot_info[0] = etot (current total energy)
// etot_info[1] = etot_p (previous total energy)
Expand All @@ -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];
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion source/source_relax/ions_move_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>& etot_info);
void setup_etot(const double &energy_in, const int istep, std::vector<double>& etot_info);

/**
* @brief Compute dot product of two vectors.
Expand Down
2 changes: 1 addition & 1 deletion source/source_relax/ions_move_bfgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ bool Ions_Move_BFGS::start(UnitCell& ucell, const ModuleBase::matrix& force, con
std::vector<double> 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)
Expand Down
2 changes: 1 addition & 1 deletion source/source_relax/ions_move_cg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 1 addition & 2 deletions source/source_relax/ions_move_sd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ bool Ions_Move_SD::start(UnitCell& ucell, const ModuleBase::matrix& force, const
std::vector<double> grad(dim, 0.0);
std::vector<double> 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)
Expand Down
18 changes: 4 additions & 14 deletions source/source_relax/lattice_change_basic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>& etot_info)
void Lattice_Change_Basic::setup_etot(const double &energy_in, std::vector<double>& etot_info)
{
// etot_info[0] = etot (current total energy)
// etot_info[1] = etot_p (previous total energy)
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion source/source_relax/lattice_change_basic.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<double>& etot_info);
void setup_etot(const double &energy_in, std::vector<double>& etot_info);
} // namespace Lattice_Change_Basic
#endif
2 changes: 1 addition & 1 deletion source/source_relax/lattice_change_cg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
67 changes: 3 additions & 64 deletions source/source_relax/test/ions_move_basic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> 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<double> 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<double> 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<double> 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
Expand Down
44 changes: 2 additions & 42 deletions source/source_relax/test/lattice_change_basic_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<double> 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<double> 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<double> 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<double> 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]);
Expand Down
Loading