Skip to content

Commit db41600

Browse files
authored
Improve md calculation stress output in running log (#6366)
* Improve md calculation stress output in running log * Module_IO Unittest modify * ModuleMD Unittests modify * modify code comment in fire_test.cpp * maintain setprecision(8) for md stress output
1 parent 8e8c30f commit db41600

File tree

10 files changed

+52
-34
lines changed

10 files changed

+52
-34
lines changed

source/source_io/output_log.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,8 @@ void print_stress(const std::string& name, const ModuleBase::matrix& scs,
285285
}
286286
else
287287
{
288-
title += " (KBAR)";
289-
unit = " KBAR";
288+
title += " (kbar)";
289+
unit = " kbar";
290290
unit_transform = ModuleBase::RYDBERG_SI / pow(ModuleBase::BOHR_RADIUS_SI, 3) * 1.0e-8;
291291
}
292292
std::vector<double> stress_x;
@@ -317,7 +317,7 @@ void print_stress(const std::string& name, const ModuleBase::matrix& scs,
317317
fmt << stress_x << stress_y << stress_z;
318318
table = fmt.str();
319319
ofs << table;
320-
if (name == "TOTAL-STRESS")
320+
if (name == "TOTAL-STRESS" && PARAM.inp.calculation != "md")
321321
{
322322
ofs << " #TOTAL-PRESSURE# (EXCLUDE KINETIC PART OF IONS): " << std::fixed
323323
<< std::setprecision(6) << pressure << unit

source/source_io/test/outputlog_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ TEST(PrintStress, PrintStress)
292292
std::string output_str;
293293
getline(ifs, output_str);
294294
getline(ifs, output_str); // mohan add 2025-06-22
295-
EXPECT_THAT(output_str, testing::HasSubstr(" #TOTAL-STRESS (KBAR)#"));
295+
EXPECT_THAT(output_str, testing::HasSubstr(" #TOTAL-STRESS (kbar)#"));
296296

297297
getline(ifs, output_str);
298298
EXPECT_THAT(output_str, testing::HasSubstr("----------------------------------------------------------------"));
@@ -316,7 +316,7 @@ TEST(PrintStress, PrintStress)
316316
EXPECT_THAT(output_str, testing::HasSubstr("----------------------------------------------------------------"));
317317

318318
getline(ifs, output_str);
319-
EXPECT_THAT(output_str, testing::HasSubstr(" #TOTAL-PRESSURE# (EXCLUDE KINETIC PART OF IONS): 49035.075992 KBAR"));
319+
EXPECT_THAT(output_str, testing::HasSubstr(" #TOTAL-PRESSURE# (EXCLUDE KINETIC PART OF IONS): 49035.075992 kbar"));
320320
ifs.close();
321321
std::remove("running_stress.txt");
322322
}

source/source_md/md_base.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ void MD_base::print_md(std::ofstream& ofs, const bool& cal_stress)
186186
// running_log output
187187
ofs.unsetf(std::ios::fixed);
188188
ofs << std::setprecision(8);
189+
190+
if (cal_stress)
191+
{
192+
MD_func::print_stress(ofs, virial, stress);
193+
ofs << std::endl;
194+
}
195+
189196
ofs << " ------------------------------------------------------------------------------------------------"
190197
<< std::endl;
191198
ofs << " " << std::left << std::setw(20) << "Energy (Ry)" << std::left << std::setw(20) << "Potential (Ry)"
@@ -209,12 +216,7 @@ void MD_base::print_md(std::ofstream& ofs, const bool& cal_stress)
209216
ofs << std::endl;
210217
ofs << " ------------------------------------------------------------------------------------------------"
211218
<< std::endl;
212-
213-
if (cal_stress)
214-
{
215-
MD_func::print_stress(ofs, virial, stress);
216-
}
217-
219+
ofs << std::endl;
218220
return;
219221
}
220222

source/source_md/md_func.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,10 @@ void print_stress(std::ofstream& ofs, const ModuleBase::matrix& virial, const Mo
300300

301301
const double unit_transform = ModuleBase::HARTREE_SI / pow(ModuleBase::BOHR_RADIUS_SI, 3) * 1.0e-8;
302302

303-
ofs << " MD PRESSURE (ELECTRONS+IONS) : " << stress_scalar * unit_transform << " kbar" << std::endl;
303+
304304
ofs << " ELECTRONIC PART OF STRESS: " << virial_scalar * unit_transform << " kbar" << std::endl;
305305
ofs << " IONIC (KINETIC) PART OF STRESS: " << (stress_scalar - virial_scalar) * unit_transform << " kbar" << std::endl;
306+
ofs << " MD PRESSURE (ELECTRONS+IONS) : " << stress_scalar * unit_transform << " kbar" << std::endl;
306307

307308
// one should use 'print_stress' function in ../source/source_io/output_log.cpp
308309
/*

source/source_md/test/fire_test.cpp

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -184,44 +184,31 @@ TEST_F(FIREtest, PrintMD)
184184
std::ifstream ifs("running_fire.log");
185185
std::string output_str;
186186

187-
// Line 1
187+
getline(ifs, output_str);
188+
EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar"));
189+
getline(ifs, output_str);
190+
EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.83853919 kbar"));
191+
getline(ifs, output_str);
192+
EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391 kbar"));
193+
getline(ifs, output_str);
188194
getline(ifs, output_str);
189195
EXPECT_THAT(output_str,
190196
testing::HasSubstr(
191197
" ------------------------------------------------------------------------------------------------"));
192-
193-
// Line 2
194198
getline(ifs, output_str);
195199
EXPECT_THAT(output_str,
196200
testing::HasSubstr(
197201
" Energy (Ry) Potential (Ry) Kinetic (Ry) Temperature (K) Pressure (kbar) "));
198-
199-
// Line 3
200202
getline(ifs, output_str);
201203
EXPECT_THAT(output_str,
202204
testing::HasSubstr(
203205
" -0.015365236 -0.023915637 0.0085504016 300 1.0846391 "));
204-
205-
// Line 4
206206
getline(ifs, output_str);
207207
EXPECT_THAT(
208208
output_str,
209209
testing::HasSubstr(
210210
" ------------------------------------------------------------------------------------------------"));
211-
212-
// Line 5
213211
getline(ifs, output_str);
214-
EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391 kbar"));
215-
216-
// Line 6
217-
getline(ifs, output_str);
218-
EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar"));
219-
220-
// Line 7
221-
getline(ifs, output_str);
222-
EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.83853919 kbar"));
223-
224-
// Line 8
225212
getline(ifs, output_str);
226213
EXPECT_THAT(output_str, testing::HasSubstr(" LARGEST FORCE (eV/A) : 0.049479926"));
227214

source/source_md/test/langevin_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,13 @@ TEST_F(Langevin_test, print_md)
169169

170170
std::ifstream ifs("running_langevin.log");
171171
std::string output_str;
172+
getline(ifs, output_str);
173+
EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar"));
174+
getline(ifs, output_str);
175+
EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.83853919 kbar"));
176+
getline(ifs, output_str);
177+
EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391 kbar"));
178+
getline(ifs, output_str);
172179
getline(ifs, output_str);
173180
EXPECT_THAT(
174181
output_str,

source/source_md/test/md_func_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,11 @@ TEST_F(MD_func_test, print_stress)
385385
std::ifstream ifs("running.log");
386386
std::string output_str;
387387
getline(ifs, output_str);
388-
EXPECT_THAT(output_str, testing::HasSubstr("MD PRESSURE (ELECTRONS+IONS) : 0 kbar"));
389-
getline(ifs, output_str);
390388
EXPECT_THAT(output_str, testing::HasSubstr("ELECTRONIC PART OF STRESS: 0 kbar"));
391389
getline(ifs, output_str);
392390
EXPECT_THAT(output_str, testing::HasSubstr("IONIC (KINETIC) PART OF STRESS: 0 kbar"));
391+
getline(ifs, output_str);
392+
EXPECT_THAT(output_str, testing::HasSubstr("MD PRESSURE (ELECTRONS+IONS) : 0 kbar"));
393393
/*
394394
getline(ifs, output_str);
395395
getline(ifs, output_str);

source/source_md/test/msst_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,13 @@ TEST_F(MSST_test, print_md)
225225

226226
std::ifstream ifs("running_msst.log");
227227
std::string output_str;
228+
getline(ifs, output_str);
229+
EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar"));
230+
getline(ifs, output_str);
231+
EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.8301538 kbar")); // result different from other MD methods
232+
getline(ifs, output_str);
233+
EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0762537 kbar")); // result different from other MD methods
234+
getline(ifs, output_str);
228235
getline(ifs, output_str);
229236
EXPECT_THAT(
230237
output_str,

source/source_md/test/nhchain_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ TEST_F(NHC_test, print_md)
215215

216216
std::ifstream ifs("running_nhchain.log");
217217
std::string output_str;
218+
getline(ifs, output_str);
219+
EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar"));
220+
getline(ifs, output_str);
221+
EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.83853919 kbar"));
222+
getline(ifs, output_str);
223+
EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391 kbar"));
224+
getline(ifs, output_str);
218225
getline(ifs, output_str);
219226
EXPECT_THAT(
220227
output_str,

source/source_md/test/verlet_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ TEST_F(Verlet_test, print_md)
310310

311311
std::ifstream ifs("running_verlet.log");
312312
std::string output_str;
313+
getline(ifs, output_str);
314+
EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar"));
315+
getline(ifs, output_str);
316+
EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.83853919 kbar"));
317+
getline(ifs, output_str);
318+
EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391 kbar"));
319+
getline(ifs, output_str);
313320
getline(ifs, output_str);
314321
EXPECT_THAT(
315322
output_str,

0 commit comments

Comments
 (0)