Skip to content

Commit 93454a3

Browse files
19helloFei Yang
andauthored
fix_setup_etot (#7489)
Co-authored-by: Fei Yang <2501213217@stu.pku.edu.cn>
1 parent b5e4d94 commit 93454a3

10 files changed

Lines changed: 16 additions & 132 deletions

source/source_relax/ions_move_basic.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ void Ions_Move_Basic::terminate(const bool converged, const int update_iter, con
209209
return;
210210
}
211211

212-
void Ions_Move_Basic::setup_etot(const double &energy_in, const bool judgement, const int istep, std::ofstream& ofs, std::vector<double>& etot_info)
212+
void Ions_Move_Basic::setup_etot(const double &energy_in, const int istep, std::vector<double>& etot_info)
213213
{
214214
// etot_info[0] = etot (current total energy)
215215
// 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,
224224
{
225225
etot_info[1] = etot_info[0];
226226
etot_info[0] = energy_in;
227-
if (etot_info[1] > etot_info[0])
228-
{
229-
etot_info[1] = etot_info[0];
230-
}
231227
}
232228
}
233229

source/source_relax/ions_move_basic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void terminate(const bool converged, const int update_iter, const UnitCell &ucel
7676
* @param istep Current ionic step index
7777
* @param etot_info Energy information array [etot, etot_p, ediff]
7878
*/
79-
void setup_etot(const double &energy_in, const bool judgement, const int istep, std::ofstream& ofs, std::vector<double>& etot_info);
79+
void setup_etot(const double &energy_in, const int istep, std::vector<double>& etot_info);
8080

8181
/**
8282
* @brief Compute dot product of two vectors.

source/source_relax/ions_move_bfgs.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bool Ions_Move_BFGS::start(UnitCell& ucell, const ModuleBase::matrix& force, con
5151
std::vector<double> pos_tmp(3 * ucell.nat);
5252
Ions_Move_Basic::setup_gradient(ucell, force, pos_tmp.data(), this->grad.data(), ofs);
5353
}
54-
Ions_Move_Basic::setup_etot(energy_in, false, istep, ofs, etot_info);
54+
Ions_Move_Basic::setup_etot(energy_in, istep, etot_info);
5555
bool converged = Ions_Move_Basic::check_converged(ucell, this->grad.data(), update_iter, ofs, etot_info);
5656

5757
if (converged)

source/source_relax/ions_move_cg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ bool Ions_Move_CG::start(UnitCell &ucell, const ModuleBase::matrix &force, const
7272
}
7373

7474
Ions_Move_Basic::setup_gradient(ucell, force, pos.data(), grad.data(), ofs);
75-
Ions_Move_Basic::setup_etot(etot_in, 0, istep, ofs, etot_info);
75+
Ions_Move_Basic::setup_etot(etot_in, istep, etot_info);
7676

7777
bool converged = false;
7878
if (flag == 0)

source/source_relax/ions_move_sd.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ bool Ions_Move_SD::start(UnitCell& ucell, const ModuleBase::matrix& force, const
3232
std::vector<double> grad(dim, 0.0);
3333
std::vector<double> move(dim, 0.0);
3434

35-
bool judgement = false;
36-
Ions_Move_Basic::setup_etot(etot_in, judgement, istep, ofs, etot_info);
35+
Ions_Move_Basic::setup_etot(etot_in, istep, etot_info);
3736
Ions_Move_Basic::setup_gradient(ucell, force, pos.data(), grad.data(), ofs);
3837

3938
if (istep == 1 || etot_in <= energy_saved)

source/source_relax/lattice_change_basic.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ void Lattice_Change_Basic::terminate(const bool converged, std::ofstream& ofs)
343343
return;
344344
}
345345

346-
void Lattice_Change_Basic::setup_etot(const double &energy_in, const bool judgement, std::vector<double>& etot_info)
346+
void Lattice_Change_Basic::setup_etot(const double &energy_in, std::vector<double>& etot_info)
347347
{
348348
// etot_info[0] = etot (current total energy)
349349
// 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
357357
}
358358
else
359359
{
360-
if (judgement)
361-
{
362-
etot_info[0] = energy_in;
363-
if (etot_info[1] > etot_info[0])
364-
{
365-
etot_info[1] = etot_info[0];
366-
}
367-
}
368-
else // for bfgs
369-
{
370-
etot_info[1] = etot_info[0];
371-
etot_info[0] = energy_in;
372-
}
360+
361+
etot_info[1] = etot_info[0];
362+
etot_info[0] = energy_in;
373363
}
374364

375365
return;

source/source_relax/lattice_change_basic.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,6 @@ void terminate(const bool converged, std::ofstream& ofs);
6868
* @param judgement Flag for SD method (true) or BFGS (false)
6969
* @param etot_info Vector containing [etot, etot_p]
7070
*/
71-
void setup_etot(const double &energy_in, const bool judgement, std::vector<double>& etot_info);
71+
void setup_etot(const double &energy_in, std::vector<double>& etot_info);
7272
} // namespace Lattice_Change_Basic
7373
#endif

source/source_relax/lattice_change_cg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ bool Lattice_Change_CG::start(UnitCell &ucell, const ModuleBase::matrix &stress_
7777

7878
ModuleBase::matrix stress(stress_in);
7979
Lattice_Change_Basic::setup_gradient(ucell, lat.data(), grad.data(), stress);
80-
Lattice_Change_Basic::setup_etot(etot_in, 0, etot_info);
80+
Lattice_Change_Basic::setup_etot(etot_in, etot_info);
8181

8282
bool converged = false;
8383
if (flag == 0)

source/source_relax/test/ions_move_basic_test.cpp

Lines changed: 3 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -265,78 +265,17 @@ TEST_F(IonsMoveBasicTest, TerminateNotConverged)
265265
EXPECT_THAT(ofs_output , ::testing::HasSubstr(expected_ofs));
266266
}
267267

268-
// Test the setup_etot() function case 1
269-
TEST_F(IonsMoveBasicTest, SetupEtotCase1)
270-
{
271-
// Initialize data
272-
const int istep = 1;
273-
std::vector<double> etot_info = {2.0, 1.0, 0.0};
274-
double energy_in = 3.0;
275-
bool judgement = true;
276-
277-
// Call the function being tested
278-
std::ofstream ofs("/dev/null");
279-
Ions_Move_Basic::setup_etot(energy_in, judgement, istep, ofs, etot_info);
280-
ofs.close();
281-
282-
// Check the results
283-
EXPECT_DOUBLE_EQ(etot_info[1], 3.0);
284-
EXPECT_DOUBLE_EQ(etot_info[0], 3.0);
285-
EXPECT_DOUBLE_EQ(etot_info[0] - etot_info[1], 0.0);
286-
}
287-
288-
// Test the setup_etot() function case 2
289-
TEST_F(IonsMoveBasicTest, SetupEtotCase2)
290-
{
291-
// Initialize data
292-
const int istep = 2;
293-
std::vector<double> etot_info = {2.0, 4.0};
294-
double energy_in = 3.0;
295-
bool judgement = true;
296-
297-
// Call the function being tested
298-
std::ofstream ofs("/dev/null");
299-
Ions_Move_Basic::setup_etot(energy_in, judgement, istep, ofs, etot_info);
300-
ofs.close();
301-
302-
// Check the results
303-
EXPECT_DOUBLE_EQ(etot_info[1], 2.0);
304-
EXPECT_DOUBLE_EQ(etot_info[0], 3.0);
305-
EXPECT_DOUBLE_EQ(etot_info[0] - etot_info[1], 1.0);
306-
}
307-
308-
// Test the setup_etot() function case 3
309-
TEST_F(IonsMoveBasicTest, SetupEtotCase3)
310-
{
311-
// Initialize data
312-
const int istep = 2;
313-
std::vector<double> etot_info = {2.0, 1.0};
314-
double energy_in = 3.0;
315-
bool judgement = true;
316-
317-
// Call the function being tested
318-
std::ofstream ofs("/dev/null");
319-
Ions_Move_Basic::setup_etot(energy_in, judgement, istep, ofs, etot_info);
320-
ofs.close();
321-
322-
// Check the results
323-
EXPECT_DOUBLE_EQ(etot_info[1], 2.0);
324-
EXPECT_DOUBLE_EQ(etot_info[0], 3.0);
325-
EXPECT_DOUBLE_EQ(etot_info[0] - etot_info[1], 1.0);
326-
}
327-
328-
// Test the setup_etot() function case 4
329-
TEST_F(IonsMoveBasicTest, SetupEtotCase4)
268+
// Test the setup_etot() function
269+
TEST_F(IonsMoveBasicTest, SetupEtot)
330270
{
331271
// Initialize data
332272
const int istep = 2;
333273
std::vector<double> etot_info = {2.0, 1.0};
334274
double energy_in = 3.0;
335-
bool judgement = false;
336275

337276
// Call the function being tested
338277
std::ofstream ofs("/dev/null");
339-
Ions_Move_Basic::setup_etot(energy_in, judgement, istep, ofs, etot_info);
278+
Ions_Move_Basic::setup_etot(energy_in, istep, etot_info);
340279
ofs.close();
341280

342281
// Check the results

source/source_relax/test/lattice_change_basic_test.cpp

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -475,54 +475,14 @@ TEST_F(LatticeChangeBasicTest, TerminateNotConverged)
475475
std::remove("test_terminate_not_converged.log");
476476
}
477477

478-
TEST_F(LatticeChangeBasicTest, SetupEtotStressStep1)
479-
{
480-
Lattice_Change_Basic::stress_step = 1;
481-
double energy_in = 100.0;
482-
std::vector<double> etot_info(2, 0.0);
483-
484-
Lattice_Change_Basic::setup_etot(energy_in, true, etot_info);
485-
486-
EXPECT_DOUBLE_EQ(energy_in, etot_info[1]);
487-
EXPECT_DOUBLE_EQ(energy_in, etot_info[0]);
488-
EXPECT_DOUBLE_EQ(0.0, etot_info[0] - etot_info[1]);
489-
}
490-
491-
TEST_F(LatticeChangeBasicTest, SetupEtotJudgementTrueHigherEnergy)
492-
{
493-
Lattice_Change_Basic::stress_step = 2;
494-
double energy_in = 90.0;
495-
std::vector<double> etot_info = {0.0, 100.0};
496-
497-
Lattice_Change_Basic::setup_etot(energy_in, true, etot_info);
498-
499-
// When judgement=true and etot_p > etot, etot_p is updated to etot
500-
EXPECT_DOUBLE_EQ(90.0, etot_info[0]);
501-
EXPECT_DOUBLE_EQ(90.0, etot_info[1]);
502-
EXPECT_DOUBLE_EQ(0.0, etot_info[0] - etot_info[1]);
503-
}
504-
505-
TEST_F(LatticeChangeBasicTest, SetupEtotJudgementTrueLowerEnergy)
506-
{
507-
Lattice_Change_Basic::stress_step = 2;
508-
double energy_in = 100.0;
509-
std::vector<double> etot_info = {0.0, 90.0};
510-
511-
Lattice_Change_Basic::setup_etot(energy_in, true, etot_info);
512-
513-
// When judgement=true and etot_p <= etot, etot_p is not updated
514-
EXPECT_DOUBLE_EQ(100.0, etot_info[0]);
515-
EXPECT_DOUBLE_EQ(90.0, etot_info[1]);
516-
EXPECT_DOUBLE_EQ(10.0, etot_info[0] - etot_info[1]);
517-
}
518478

519-
TEST_F(LatticeChangeBasicTest, SetupEtotJudgementFalse)
479+
TEST_F(LatticeChangeBasicTest, SetupEtot)
520480
{
521481
Lattice_Change_Basic::stress_step = 2;
522482
double energy_in = 80.0;
523483
std::vector<double> etot_info = {100.0, 90.0};
524484

525-
Lattice_Change_Basic::setup_etot(energy_in, false, etot_info);
485+
Lattice_Change_Basic::setup_etot(energy_in, etot_info);
526486

527487
EXPECT_DOUBLE_EQ(100.0, etot_info[1]);
528488
EXPECT_DOUBLE_EQ(80.0, etot_info[0]);

0 commit comments

Comments
 (0)