Skip to content

Commit 3984296

Browse files
Unify the output of BFGS, CG, and FIRE relaxation method. (#5374)
* Refactor: Unify the output of BFGS, CG, and FIRE relaxation method. * Refactor: Remove unnecessary line breaks. * Test: Update corresponding Unit tests. * [pre-commit.ci lite] apply automatic fixes * Test: Update Unit tests. --------- Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
1 parent 4d2366c commit 3984296

File tree

10 files changed

+67
-40
lines changed

10 files changed

+67
-40
lines changed

source/module_md/fire.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ void FIRE::print_md(std::ofstream& ofs, const bool& cal_stress)
7474
{
7575
MD_base::print_md(ofs, cal_stress);
7676

77-
ofs << " LARGEST GRAD (eV/A) : " << max * ModuleBase::Hartree_to_eV * ModuleBase::ANGSTROM_AU << std::endl;
77+
ofs << "\n Largest gradient in force is " << max * ModuleBase::Hartree_to_eV * ModuleBase::ANGSTROM_AU << " eV/A." << std::endl;
78+
ofs << " Threshold is " << PARAM.inp.force_thr_ev << " eV/A." << std::endl;
7879
std::cout << " LARGEST GRAD (eV/A) : " << max * ModuleBase::Hartree_to_eV * ModuleBase::ANGSTROM_AU << std::endl;
7980

8081
return;

source/module_md/test/fire_test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,11 +205,13 @@ TEST_F(FIREtest, PrintMD)
205205
output_str,
206206
testing::HasSubstr(
207207
" ------------------------------------------------------------------------------------------------"));
208-
for (int i = 0; i < 10; ++i)
208+
for (int i = 0; i < 11; ++i)
209209
{
210210
getline(ifs, output_str);
211211
}
212-
EXPECT_THAT(output_str, testing::HasSubstr(" LARGEST GRAD (eV/A) : 0.049479926"));
212+
EXPECT_THAT(output_str, testing::HasSubstr(" Largest gradient in force is 0.049479926 eV/A"));
213+
getline(ifs, output_str);
214+
EXPECT_THAT(output_str, testing::HasSubstr(" Threshold is -1 eV/A."));
213215
ifs.close();
214216
remove("running.log");
215217
}

source/module_relax/relax_new/relax.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ bool Relax::setup_gradient(const ModuleBase::matrix& force, const ModuleBase::ma
140140
std::cout << " LARGEST GRAD (eV/A) : " << max_grad << std::endl;
141141
etot_p = etot;
142142
}
143+
144+
GlobalV::ofs_running << "\n Largest gradient in force is " << max_grad << " eV/A." << std::endl;
145+
GlobalV::ofs_running << " Threshold is " << PARAM.inp.force_thr_ev << " eV/A." << std::endl;
143146
//=========================================
144147
//set gradient for cell degrees of freedom
145148
//=========================================
@@ -223,8 +226,8 @@ bool Relax::setup_gradient(const ModuleBase::matrix& force, const ModuleBase::ma
223226
force_converged = false;
224227
}
225228

226-
GlobalV::ofs_running << "\n Largest gradient in stress is " << largest_grad << std::endl;
227-
GlobalV::ofs_running << "\n Threshold is = " << PARAM.inp.stress_thr << std::endl;
229+
GlobalV::ofs_running << "\n Largest gradient in stress is " << largest_grad << " kbar." << std::endl;
230+
GlobalV::ofs_running << " Threshold is " << PARAM.inp.stress_thr << " kbar." << std::endl;
228231
}
229232

230233
if(force_converged)

source/module_relax/relax_old/ions_move_basic.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ void Ions_Move_Basic::check_converged(const UnitCell &ucell, const double *grad)
147147
std::cout << " ETOT DIFF (eV) : " << Ions_Move_Basic::ediff * ModuleBase::Ry_to_eV << std::endl;
148148
std::cout << " LARGEST GRAD (eV/A) : " << Ions_Move_Basic::largest_grad * ModuleBase::Ry_to_eV / 0.529177
149149
<< std::endl;
150+
151+
GlobalV::ofs_running << "\n Largest gradient in force is " << largest_grad * ModuleBase::Ry_to_eV / 0.529177
152+
<< " eV/A." << std::endl;
153+
GlobalV::ofs_running << " Threshold is " << PARAM.inp.force_thr_ev << " eV/A." << std::endl;
150154
}
151155

152156
const double etot_diff = std::abs(Ions_Move_Basic::ediff);
@@ -165,8 +169,6 @@ void Ions_Move_Basic::check_converged(const UnitCell &ucell, const double *grad)
165169
{
166170
GlobalV::ofs_running << "\n Ion relaxation is converged!" << std::endl;
167171
GlobalV::ofs_running << "\n Energy difference (Ry) = " << etot_diff << std::endl;
168-
GlobalV::ofs_running << "\n Largest gradient is (eV/A) = " << largest_grad * ModuleBase::Ry_to_eV / 0.529177
169-
<< std::endl;
170172

171173
Ions_Move_Basic::converged = true;
172174
++Ions_Move_Basic::update_iter;

source/module_relax/relax_old/lattice_change_basic.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ void Lattice_Change_Basic::change_lattice(UnitCell &ucell, double *move, double
7272
{
7373
ModuleBase::TITLE("Lattice_Change_Basic", "change_lattice");
7474

75-
assert(move != NULL);
76-
assert(lat != NULL);
75+
assert(move != nullptr);
76+
assert(lat != nullptr);
7777

7878
/*
7979
std::cout<<" LATTICE CONSTANT OLD:"<<std::endl;
@@ -86,11 +86,15 @@ void Lattice_Change_Basic::change_lattice(UnitCell &ucell, double *move, double
8686
if (ModuleSymmetry::Symmetry::symm_flag && ucell.symm.nrotk > 0)
8787
{
8888
ModuleBase::matrix move_mat_t(3, 3);
89-
for (int i = 0;i < 3;++i)for (int j = 0;j < 3;++j)move_mat_t(j, i) = move[i * 3 + j] / ucell.lat0; //transpose
89+
for (int i = 0;i < 3;++i) {for (int j = 0;j < 3;++j) {move_mat_t(j, i) = move[i * 3 + j] / ucell.lat0; //transpose
90+
}
91+
}
9092
ModuleBase::matrix symm_move_mat_t = (move_mat_t * ucell.G.to_matrix());//symmetrize (latvec^{-1} * move_mat)^T
9193
ucell.symm.symmetrize_mat3(symm_move_mat_t, ucell.lat);
9294
move_mat_t = symm_move_mat_t * ucell.latvec.Transpose().to_matrix();//G^{-1}=latvec^T
93-
for (int i = 0;i < 3;++i)for (int j = 0;j < 3;++j)move[i * 3 + j] = move_mat_t(j, i) * ucell.lat0;//transpose back
95+
for (int i = 0;i < 3;++i) {for (int j = 0;j < 3;++j) {move[i * 3 + j] = move_mat_t(j, i) * ucell.lat0;//transpose back
96+
}
97+
}
9498
}
9599

96100
if (ucell.lc[0] != 0)
@@ -167,8 +171,9 @@ void Lattice_Change_Basic::check_converged(const UnitCell &ucell, ModuleBase::ma
167171
{
168172
for (int i = 0; i < 3; i++)
169173
{
170-
if (stress_ii_max < std::abs(stress(i, i)))
174+
if (stress_ii_max < std::abs(stress(i, i))) {
171175
stress_ii_max = std::abs(stress(i, i));
176+
}
172177
for (int j = 0; j < 3; j++)
173178
{
174179
if (Lattice_Change_Basic::largest_grad < std::abs(stress(i, j)))
@@ -207,14 +212,15 @@ void Lattice_Change_Basic::check_converged(const UnitCell &ucell, ModuleBase::ma
207212
if (Lattice_Change_Basic::largest_grad < PARAM.inp.stress_thr && stress_ii_max < PARAM.inp.stress_thr)
208213
{
209214
GlobalV::ofs_running << "\n Lattice relaxation is converged!" << std::endl;
210-
GlobalV::ofs_running << "\n Largest gradient is = " << largest_grad << std::endl;
215+
GlobalV::ofs_running << "\n Largest gradient in stress is " << largest_grad << " kbar." << std::endl;
216+
GlobalV::ofs_running << " Threshold is " << PARAM.inp.stress_thr << " kbar." << std::endl;
211217
Lattice_Change_Basic::converged = true;
212218
++Lattice_Change_Basic::update_iter;
213219
}
214220
else
215221
{
216222
GlobalV::ofs_running << "\n Lattice relaxation is not converged yet (threshold is " << PARAM.inp.stress_thr
217-
<< ")" << std::endl;
223+
<< " kbar)" << std::endl;
218224
Lattice_Change_Basic::converged = false;
219225
}
220226
}
@@ -227,22 +233,23 @@ void Lattice_Change_Basic::check_converged(const UnitCell &ucell, ModuleBase::ma
227233
if (Lattice_Change_Basic::largest_grad < 10 * PARAM.inp.stress_thr)
228234
{
229235
GlobalV::ofs_running << "\n Lattice relaxation is converged!" << std::endl;
230-
GlobalV::ofs_running << "\n Largest gradient is = " << largest_grad << std::endl;
236+
GlobalV::ofs_running << "\n Largest gradient in stress is " << largest_grad << " kbar." << std::endl;
237+
GlobalV::ofs_running << " Threshold is " << PARAM.inp.stress_thr << " kbar." << std::endl;
231238
Lattice_Change_Basic::converged = true;
232239
++Lattice_Change_Basic::update_iter;
233240
}
234241
else
235242
{
236243
GlobalV::ofs_running << "\n Lattice relaxation is not converged yet (threshold is " << PARAM.inp.stress_thr
237-
<< ")" << std::endl;
244+
<< " kbar)" << std::endl;
238245
Lattice_Change_Basic::converged = false;
239246
}
240247
}
241248

242249
return;
243250
}
244251

245-
void Lattice_Change_Basic::terminate(void)
252+
void Lattice_Change_Basic::terminate()
246253
{
247254
ModuleBase::TITLE("Lattice_Change_Basic", "terminate");
248255
if (Lattice_Change_Basic::converged)

source/module_relax/relax_old/test/ions_move_basic_test.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,8 @@ TEST_F(IonsMoveBasicTest, CheckConvergedCase1)
132132

133133
std::string expected_ofs
134134
= " old total energy (ry) = 0\n new total energy (ry) = 0\n "
135-
" energy difference (ry) = 0\n largest gradient (ry/bohr) = 0\n largest force is 0, no "
135+
" energy difference (ry) = 0\n largest gradient (ry/bohr) = 0\n\n"
136+
" Largest gradient in force is 0 eV/A.\n Threshold is -1 eV/A.\n largest force is 0, no "
136137
"movement is possible.\n it may converged, otherwise no movement of atom is allowed.\n";
137138
std::string expected_std = " ETOT DIFF (eV) : 0\n LARGEST GRAD (eV/A) : 0\n";
138139

@@ -170,8 +171,9 @@ TEST_F(IonsMoveBasicTest, CheckConvergedCase2)
170171

171172
std::string expected_ofs
172173
= " old total energy (ry) = 0\n new total energy (ry) = 0\n "
173-
" energy difference (ry) = 0\n largest gradient (ry/bohr) = 0.1\n\n Ion relaxation is "
174-
"converged!\n\n Energy difference (Ry) = 0\n\n Largest gradient is (eV/A) = 2.57111\n";
174+
" energy difference (ry) = 0\n largest gradient (ry/bohr) = 0.1\n\n"
175+
" Largest gradient in force is 2.57111 eV/A.\n Threshold is -1 eV/A.\n\n Ion relaxation is "
176+
"converged!\n\n Energy difference (Ry) = 0\n";
175177
std::string expected_std = " ETOT DIFF (eV) : 0\n LARGEST GRAD (eV/A) : 2.57111\n";
176178

177179
EXPECT_EQ(expected_ofs, ofs_output);
@@ -208,7 +210,8 @@ TEST_F(IonsMoveBasicTest, CheckConvergedCase3)
208210

209211
std::string expected_ofs
210212
= " old total energy (ry) = 0\n new total energy (ry) = 0\n "
211-
" energy difference (ry) = 1\n largest gradient (ry/bohr) = 0.1\n\n Ion relaxation is not "
213+
" energy difference (ry) = 1\n largest gradient (ry/bohr) = 0.1\n\n"
214+
" Largest gradient in force is 2.57111 eV/A.\n Threshold is -1 eV/A.\n\n Ion relaxation is not "
212215
"converged yet (threshold is 25.7111)\n";
213216
std::string expected_std = " ETOT DIFF (eV) : 13.6057\n LARGEST GRAD (eV/A) : 2.57111\n";
214217

source/module_relax/relax_old/test/ions_move_cg_test.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ TEST_F(IonsMoveCGTest, TestStartConverged)
8989
GlobalV::ofs_running.close();
9090

9191
// Check output
92-
std::string expected_output = " largest force is 0, no movement is possible.\n it may converged, otherwise no "
92+
std::string expected_output = "\n Largest gradient in force is 0 eV/A.\n Threshold is -1 eV/A.\n"
93+
" largest force is 0, no movement is possible.\n it may converged, otherwise no "
9394
"movement of atom is allowed.\n end of geometry optimization\n "
9495
" istep = 1\n update iteration = 5\n";
9596
std::ifstream ifs("log");
@@ -122,7 +123,8 @@ TEST_F(IonsMoveCGTest, TestStartSd)
122123
GlobalV::ofs_running.close();
123124

124125
// Check output
125-
std::string expected_output = "\n Ion relaxation is not converged yet (threshold is 0.0257111)\n";
126+
std::string expected_output = "\n Largest gradient in force is 0.257111 eV/A.\n Threshold is -1 eV/A.\n\n"
127+
" Ion relaxation is not converged yet (threshold is 0.0257111)\n";
126128
std::ifstream ifs("log");
127129
std::string output((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
128130
ifs.close();
@@ -159,7 +161,8 @@ TEST_F(IonsMoveCGTest, TestStartTrialGoto)
159161
GlobalV::ofs_running.close();
160162

161163
// Check output
162-
std::string expected_output = "\n Ion relaxation is not converged yet (threshold is 0.0257111)\n";
164+
std::string expected_output = "\n Largest gradient in force is 0.0257111 eV/A.\n Threshold is -1 eV/A.\n\n"
165+
" Ion relaxation is not converged yet (threshold is 0.0257111)\n";
163166
std::ifstream ifs("log");
164167
std::string output((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
165168
ifs.close();
@@ -195,7 +198,8 @@ TEST_F(IonsMoveCGTest, TestStartTrial)
195198
GlobalV::ofs_running.close();
196199

197200
// Check output
198-
std::string expected_output = "\n Ion relaxation is not converged yet (threshold is 0.0257111)\n";
201+
std::string expected_output = "\n Largest gradient in force is 0.257111 eV/A.\n Threshold is -1 eV/A.\n\n"
202+
" Ion relaxation is not converged yet (threshold is 0.0257111)\n";
199203
std::ifstream ifs("log");
200204
std::string output((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
201205
ifs.close();
@@ -233,7 +237,8 @@ TEST_F(IonsMoveCGTest, TestStartNoTrialGotoCase1)
233237
GlobalV::ofs_running.close();
234238

235239
// Check output
236-
std::string expected_output = "\n Ion relaxation is not converged yet (threshold is 0.0257111)\n";
240+
std::string expected_output = "\n Largest gradient in force is 0.0257111 eV/A.\n Threshold is -1 eV/A.\n\n"
241+
" Ion relaxation is not converged yet (threshold is 0.0257111)\n";
237242
std::ifstream ifs("log");
238243
std::string output((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
239244
ifs.close();
@@ -270,7 +275,8 @@ TEST_F(IonsMoveCGTest, TestStartNoTrialGotoCase2)
270275
GlobalV::ofs_running.close();
271276

272277
// Check output
273-
std::string expected_output = "\n Ion relaxation is not converged yet (threshold is 0.0257111)\n";
278+
std::string expected_output = "\n Largest gradient in force is 0.257111 eV/A.\n Threshold is -1 eV/A.\n\n"
279+
" Ion relaxation is not converged yet (threshold is 0.0257111)\n";
274280
std::ifstream ifs("log");
275281
std::string output((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
276282
ifs.close();
@@ -308,7 +314,8 @@ TEST_F(IonsMoveCGTest, TestStartNoTrial)
308314
GlobalV::ofs_running.close();
309315

310316
// Check output
311-
std::string expected_output = "\n Ion relaxation is not converged yet (threshold is 0.0257111)\n";
317+
std::string expected_output = "\n Largest gradient in force is 0.0257111 eV/A.\n Threshold is -1 eV/A.\n\n"
318+
" Ion relaxation is not converged yet (threshold is 0.0257111)\n";
312319
std::ifstream ifs("log");
313320
std::string output((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
314321
ifs.close();

source/module_relax/relax_old/test/ions_move_sd_test.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ TEST_F(IonsMoveSDTest, TestStartConverged)
8484
GlobalV::ofs_running.close();
8585

8686
// Check output
87-
std::string expected_output = " largest force is 0, no movement is possible.\n it may converged, otherwise no "
87+
std::string expected_output = "\n Largest gradient in force is 0 eV/A.\n Threshold is -1 eV/A.\n"
88+
" largest force is 0, no movement is possible.\n it may converged, otherwise no "
8889
"movement of atom is allowed.\n end of geometry optimization\n "
8990
" istep = 1\n update iteration = 5\n";
9091
std::ifstream ifs("log");
@@ -122,7 +123,8 @@ TEST_F(IonsMoveSDTest, TestStartNotConverged)
122123
GlobalV::ofs_running.close();
123124

124125
// Check output
125-
std::string expected_output = "\n Ion relaxation is not converged yet (threshold is 0.0257111)\n";
126+
std::string expected_output = "\n Largest gradient in force is 25.7111 eV/A.\n Threshold is -1 eV/A.\n\n"
127+
" Ion relaxation is not converged yet (threshold is 0.0257111)\n";
126128
std::ifstream ifs("log");
127129
std::string output((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
128130
ifs.close();

source/module_relax/relax_old/test/lattice_change_basic_test.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ TEST_F(LatticeChangeBasicTest, CheckConvergedCase1)
241241

242242
// Check the results
243243
std::ifstream ifs("log");
244-
std::string expected_output = "\n Lattice relaxation is not converged yet (threshold is 10)\n";
244+
std::string expected_output = "\n Lattice relaxation is not converged yet (threshold is 10 kbar)\n";
245245
std::string output((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
246246
EXPECT_EQ(output, expected_output);
247247
EXPECT_EQ(Lattice_Change_Basic::update_iter, 0);
@@ -316,7 +316,7 @@ TEST_F(LatticeChangeBasicTest, CheckConvergedCase3)
316316

317317
// Check the results
318318
std::ifstream ifs("log");
319-
std::string expected_output = "\n Lattice relaxation is converged!\n\n Largest gradient is = 0.147105\n";
319+
std::string expected_output = "\n Lattice relaxation is converged!\n\n Largest gradient in stress is 0.147105 kbar.\n Threshold is 10 kbar.\n";
320320
std::string output((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
321321
EXPECT_EQ(output, expected_output);
322322
EXPECT_EQ(Lattice_Change_Basic::update_iter, 1);
@@ -353,7 +353,7 @@ TEST_F(LatticeChangeBasicTest, CheckConvergedCase4)
353353

354354
// Check the results
355355
std::ifstream ifs("log");
356-
std::string expected_output = "\n Lattice relaxation is not converged yet (threshold is 10)\n";
356+
std::string expected_output = "\n Lattice relaxation is not converged yet (threshold is 10 kbar)\n";
357357
std::string output((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
358358
EXPECT_EQ(output, expected_output);
359359
EXPECT_EQ(Lattice_Change_Basic::update_iter, 0);
@@ -428,7 +428,7 @@ TEST_F(LatticeChangeBasicTest, CheckConvergedCase6)
428428

429429
// Check the results
430430
std::ifstream ifs("log");
431-
std::string expected_output = "\n Lattice relaxation is converged!\n\n Largest gradient is = 0.147105\n";
431+
std::string expected_output = "\n Lattice relaxation is converged!\n\n Largest gradient in stress is 0.147105 kbar.\n Threshold is 10 kbar.\n";
432432
std::string output((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
433433
EXPECT_EQ(output, expected_output);
434434
EXPECT_EQ(Lattice_Change_Basic::update_iter, 1);

0 commit comments

Comments
 (0)