From 5f0c35bd18064c272783e4940911aae714a819fd Mon Sep 17 00:00:00 2001 From: lanshuyue <2000011337@stu.pku.edu.cn> Date: Wed, 9 Jul 2025 00:59:49 +0800 Subject: [PATCH 1/5] Improve md calculation stress output in running log --- source/source_io/output_log.cpp | 6 +++--- source/source_md/md_base.cpp | 14 ++++++++------ source/source_md/md_func.cpp | 3 ++- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/source/source_io/output_log.cpp b/source/source_io/output_log.cpp index f719c6485e..e95aa69053 100644 --- a/source/source_io/output_log.cpp +++ b/source/source_io/output_log.cpp @@ -285,8 +285,8 @@ void print_stress(const std::string& name, const ModuleBase::matrix& scs, } else { - title += " (KBAR)"; - unit = " KBAR"; + title += " (kbar)"; + unit = " kbar"; unit_transform = ModuleBase::RYDBERG_SI / pow(ModuleBase::BOHR_RADIUS_SI, 3) * 1.0e-8; } std::vector stress_x; @@ -317,7 +317,7 @@ void print_stress(const std::string& name, const ModuleBase::matrix& scs, fmt << stress_x << stress_y << stress_z; table = fmt.str(); ofs << table; - if (name == "TOTAL-STRESS") + if (name == "TOTAL-STRESS" && PARAM.inp.calculation != "md") { ofs << " #TOTAL-PRESSURE# (EXCLUDE KINETIC PART OF IONS): " << std::fixed << std::setprecision(6) << pressure << unit diff --git a/source/source_md/md_base.cpp b/source/source_md/md_base.cpp index cf15aedcaf..567365b942 100644 --- a/source/source_md/md_base.cpp +++ b/source/source_md/md_base.cpp @@ -184,6 +184,13 @@ void MD_base::print_md(std::ofstream& ofs, const bool& cal_stress) << std::endl; // running_log output + + if (cal_stress) + { + MD_func::print_stress(ofs, virial, stress); + ofs << std::endl; + } + ofs.unsetf(std::ios::fixed); ofs << std::setprecision(8); ofs << " ------------------------------------------------------------------------------------------------" @@ -209,12 +216,7 @@ void MD_base::print_md(std::ofstream& ofs, const bool& cal_stress) ofs << std::endl; ofs << " ------------------------------------------------------------------------------------------------" << std::endl; - - if (cal_stress) - { - MD_func::print_stress(ofs, virial, stress); - } - + ofs << std::endl; return; } diff --git a/source/source_md/md_func.cpp b/source/source_md/md_func.cpp index 245992401e..d6c09beafc 100644 --- a/source/source_md/md_func.cpp +++ b/source/source_md/md_func.cpp @@ -300,9 +300,10 @@ void print_stress(std::ofstream& ofs, const ModuleBase::matrix& virial, const Mo const double unit_transform = ModuleBase::HARTREE_SI / pow(ModuleBase::BOHR_RADIUS_SI, 3) * 1.0e-8; - ofs << " MD PRESSURE (ELECTRONS+IONS) : " << stress_scalar * unit_transform << " kbar" << std::endl; + ofs << " ELECTRONIC PART OF STRESS: " << virial_scalar * unit_transform << " kbar" << std::endl; ofs << " IONIC (KINETIC) PART OF STRESS: " << (stress_scalar - virial_scalar) * unit_transform << " kbar" << std::endl; + ofs << " MD PRESSURE (ELECTRONS+IONS) : " << stress_scalar * unit_transform << " kbar" << std::endl; // one should use 'print_stress' function in ../source/source_io/output_log.cpp /* From 324fd5989b38e271cc63a333c7dccb83f1991ae7 Mon Sep 17 00:00:00 2001 From: lanshuyue <2000011337@stu.pku.edu.cn> Date: Wed, 9 Jul 2025 01:45:15 +0800 Subject: [PATCH 2/5] Module_IO Unittest modify --- source/source_io/test/outputlog_test.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/source_io/test/outputlog_test.cpp b/source/source_io/test/outputlog_test.cpp index 92618bda66..4932596039 100644 --- a/source/source_io/test/outputlog_test.cpp +++ b/source/source_io/test/outputlog_test.cpp @@ -292,7 +292,7 @@ TEST(PrintStress, PrintStress) std::string output_str; getline(ifs, output_str); getline(ifs, output_str); // mohan add 2025-06-22 - EXPECT_THAT(output_str, testing::HasSubstr(" #TOTAL-STRESS (KBAR)#")); + EXPECT_THAT(output_str, testing::HasSubstr(" #TOTAL-STRESS (kbar)#")); getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr("----------------------------------------------------------------")); @@ -316,7 +316,7 @@ TEST(PrintStress, PrintStress) EXPECT_THAT(output_str, testing::HasSubstr("----------------------------------------------------------------")); getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr(" #TOTAL-PRESSURE# (EXCLUDE KINETIC PART OF IONS): 49035.075992 KBAR")); + EXPECT_THAT(output_str, testing::HasSubstr(" #TOTAL-PRESSURE# (EXCLUDE KINETIC PART OF IONS): 49035.075992 kbar")); ifs.close(); std::remove("running_stress.txt"); } From 155a128875bd71345903961872a3f575fd2b83e3 Mon Sep 17 00:00:00 2001 From: lanshuyue <2000011337@stu.pku.edu.cn> Date: Wed, 9 Jul 2025 15:38:08 +0800 Subject: [PATCH 3/5] ModuleMD Unittests modify --- source/source_md/test/fire_test.cpp | 23 +++++++++++++---------- source/source_md/test/langevin_test.cpp | 19 +++++++++++++++++++ source/source_md/test/md_func_test.cpp | 4 ++-- source/source_md/test/msst_test.cpp | 19 +++++++++++++++++++ source/source_md/test/nhchain_test.cpp | 16 ++++++++++++++++ source/source_md/test/verlet_test.cpp | 22 ++++++++++++++++++++++ 6 files changed, 91 insertions(+), 12 deletions(-) diff --git a/source/source_md/test/fire_test.cpp b/source/source_md/test/fire_test.cpp index 345f0619e4..94d3fe6623 100644 --- a/source/source_md/test/fire_test.cpp +++ b/source/source_md/test/fire_test.cpp @@ -184,6 +184,19 @@ TEST_F(FIREtest, PrintMD) std::ifstream ifs("running_fire.log"); std::string output_str; + // Line 6 + getline(ifs, output_str); + EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.2461 kbar")); + + // Line 7 + getline(ifs, output_str); + EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.838539 kbar")); + + // Line 5 + getline(ifs, output_str); + EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.08464 kbar")); + + getline(ifs, output_str); // Line 1 getline(ifs, output_str); EXPECT_THAT(output_str, @@ -209,17 +222,7 @@ TEST_F(FIREtest, PrintMD) testing::HasSubstr( " ------------------------------------------------------------------------------------------------")); - // Line 5 - getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391 kbar")); - - // Line 6 - getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar")); - - // Line 7 getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.83853919 kbar")); // Line 8 getline(ifs, output_str); diff --git a/source/source_md/test/langevin_test.cpp b/source/source_md/test/langevin_test.cpp index 9f8f4b78a4..a6b8b5b3cd 100644 --- a/source/source_md/test/langevin_test.cpp +++ b/source/source_md/test/langevin_test.cpp @@ -170,6 +170,25 @@ TEST_F(Langevin_test, print_md) std::ifstream ifs("running_langevin.log"); std::string output_str; getline(ifs, output_str); + EXPECT_THAT( + output_str, + testing::HasSubstr( + " ELECTRONIC PART OF STRESS: 0.2461 kbar" + )); + getline(ifs, output_str); + EXPECT_THAT( + output_str, + testing::HasSubstr( + " IONIC (KINETIC) PART OF STRESS: 0.838539 kbar" + )); + getline(ifs, output_str); + EXPECT_THAT( + output_str, + testing::HasSubstr( + " MD PRESSURE (ELECTRONS+IONS) : 1.08464 kbar" + )); + getline(ifs, output_str); + getline(ifs, output_str); EXPECT_THAT( output_str, testing::HasSubstr( diff --git a/source/source_md/test/md_func_test.cpp b/source/source_md/test/md_func_test.cpp index 4ec77c61f9..8455823885 100644 --- a/source/source_md/test/md_func_test.cpp +++ b/source/source_md/test/md_func_test.cpp @@ -385,11 +385,11 @@ TEST_F(MD_func_test, print_stress) std::ifstream ifs("running.log"); std::string output_str; getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr("MD PRESSURE (ELECTRONS+IONS) : 0 kbar")); - getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr("ELECTRONIC PART OF STRESS: 0 kbar")); getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr("IONIC (KINETIC) PART OF STRESS: 0 kbar")); + getline(ifs, output_str); + EXPECT_THAT(output_str, testing::HasSubstr("MD PRESSURE (ELECTRONS+IONS) : 0 kbar")); /* getline(ifs, output_str); getline(ifs, output_str); diff --git a/source/source_md/test/msst_test.cpp b/source/source_md/test/msst_test.cpp index ccb580538f..e8c54e805b 100644 --- a/source/source_md/test/msst_test.cpp +++ b/source/source_md/test/msst_test.cpp @@ -226,6 +226,25 @@ TEST_F(MSST_test, print_md) std::ifstream ifs("running_msst.log"); std::string output_str; getline(ifs, output_str); + EXPECT_THAT( + output_str, + testing::HasSubstr( + " ELECTRONIC PART OF STRESS: 0.2461 kbar" + )); + getline(ifs, output_str); + EXPECT_THAT( + output_str, + testing::HasSubstr( + " IONIC (KINETIC) PART OF STRESS: 0.830154 kbar" + )); + getline(ifs, output_str); + EXPECT_THAT( + output_str, + testing::HasSubstr( + " MD PRESSURE (ELECTRONS+IONS) : 1.07625 kbar" + )); + getline(ifs, output_str); + getline(ifs, output_str); EXPECT_THAT( output_str, testing::HasSubstr( diff --git a/source/source_md/test/nhchain_test.cpp b/source/source_md/test/nhchain_test.cpp index dac231ddae..f2ff553bba 100644 --- a/source/source_md/test/nhchain_test.cpp +++ b/source/source_md/test/nhchain_test.cpp @@ -216,6 +216,22 @@ TEST_F(NHC_test, print_md) std::ifstream ifs("running_nhchain.log"); std::string output_str; getline(ifs, output_str); + EXPECT_THAT( + output_str, + testing::HasSubstr( + " ELECTRONIC PART OF STRESS: 0.2461 kbar")); + getline(ifs, output_str); + EXPECT_THAT( + output_str, + testing::HasSubstr( + " IONIC (KINETIC) PART OF STRESS: 0.838539 kbar")); + getline(ifs, output_str); + EXPECT_THAT( + output_str, + testing::HasSubstr( + " MD PRESSURE (ELECTRONS+IONS) : 1.08464 kbar")); + getline(ifs, output_str); + getline(ifs, output_str); EXPECT_THAT( output_str, testing::HasSubstr( diff --git a/source/source_md/test/verlet_test.cpp b/source/source_md/test/verlet_test.cpp index 3f070569ce..de6cfbf462 100644 --- a/source/source_md/test/verlet_test.cpp +++ b/source/source_md/test/verlet_test.cpp @@ -311,6 +311,28 @@ TEST_F(Verlet_test, print_md) std::ifstream ifs("running_verlet.log"); std::string output_str; getline(ifs, output_str); + EXPECT_THAT( + output_str, + testing::HasSubstr( + " ELECTRONIC PART OF STRESS: 0.2461 kbar" + ) + ); + getline(ifs, output_str); + EXPECT_THAT( + output_str, + testing::HasSubstr( + " IONIC (KINETIC) PART OF STRESS: 0.838539 kbar" + ) + ); + getline(ifs, output_str); + EXPECT_THAT( + output_str, + testing::HasSubstr( + " MD PRESSURE (ELECTRONS+IONS) : 1.08464 kbar" + ) + ); + getline(ifs, output_str); + getline(ifs, output_str); EXPECT_THAT( output_str, testing::HasSubstr( From af0d19578c137e91fd656f0d6c7305535c2ad04b Mon Sep 17 00:00:00 2001 From: lanshuyue <2000011337@stu.pku.edu.cn> Date: Wed, 9 Jul 2025 15:43:54 +0800 Subject: [PATCH 4/5] modify code comment in fire_test.cpp --- source/source_md/test/fire_test.cpp | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/source/source_md/test/fire_test.cpp b/source/source_md/test/fire_test.cpp index 94d3fe6623..3fd7b16e5a 100644 --- a/source/source_md/test/fire_test.cpp +++ b/source/source_md/test/fire_test.cpp @@ -184,47 +184,31 @@ TEST_F(FIREtest, PrintMD) std::ifstream ifs("running_fire.log"); std::string output_str; - // Line 6 getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.2461 kbar")); - - // Line 7 getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.838539 kbar")); - - // Line 5 getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.08464 kbar")); - getline(ifs, output_str); - // Line 1 getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr( " ------------------------------------------------------------------------------------------------")); - - // Line 2 getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr( " Energy (Ry) Potential (Ry) Kinetic (Ry) Temperature (K) Pressure (kbar) ")); - - // Line 3 getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr( " -0.015365236 -0.023915637 0.0085504016 300 1.0846391 ")); - - // Line 4 getline(ifs, output_str); EXPECT_THAT( output_str, testing::HasSubstr( " ------------------------------------------------------------------------------------------------")); - getline(ifs, output_str); - - // Line 8 getline(ifs, output_str); EXPECT_THAT(output_str, testing::HasSubstr(" LARGEST FORCE (eV/A) : 0.049479926")); From 10f8f808ca6b45b4064e4589d19b8e9ee0da913c Mon Sep 17 00:00:00 2001 From: lanshuyue <2000011337@stu.pku.edu.cn> Date: Wed, 9 Jul 2025 16:34:57 +0800 Subject: [PATCH 5/5] maintain setprecision(8) for md stress output --- source/source_md/md_base.cpp | 8 ++++---- source/source_md/test/fire_test.cpp | 6 +++--- source/source_md/test/langevin_test.cpp | 20 ++++---------------- source/source_md/test/msst_test.cpp | 18 +++--------------- source/source_md/test/nhchain_test.cpp | 15 +++------------ source/source_md/test/verlet_test.cpp | 21 +++------------------ 6 files changed, 20 insertions(+), 68 deletions(-) diff --git a/source/source_md/md_base.cpp b/source/source_md/md_base.cpp index 567365b942..d1e4b67292 100644 --- a/source/source_md/md_base.cpp +++ b/source/source_md/md_base.cpp @@ -184,15 +184,15 @@ void MD_base::print_md(std::ofstream& ofs, const bool& cal_stress) << std::endl; // running_log output - + ofs.unsetf(std::ios::fixed); + ofs << std::setprecision(8); + if (cal_stress) { MD_func::print_stress(ofs, virial, stress); - ofs << std::endl; + ofs << std::endl; } - ofs.unsetf(std::ios::fixed); - ofs << std::setprecision(8); ofs << " ------------------------------------------------------------------------------------------------" << std::endl; ofs << " " << std::left << std::setw(20) << "Energy (Ry)" << std::left << std::setw(20) << "Potential (Ry)" diff --git a/source/source_md/test/fire_test.cpp b/source/source_md/test/fire_test.cpp index 3fd7b16e5a..61528b91c0 100644 --- a/source/source_md/test/fire_test.cpp +++ b/source/source_md/test/fire_test.cpp @@ -185,11 +185,11 @@ TEST_F(FIREtest, PrintMD) std::string output_str; getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.2461 kbar")); + EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar")); getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.838539 kbar")); + EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.83853919 kbar")); getline(ifs, output_str); - EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.08464 kbar")); + EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391 kbar")); getline(ifs, output_str); getline(ifs, output_str); EXPECT_THAT(output_str, diff --git a/source/source_md/test/langevin_test.cpp b/source/source_md/test/langevin_test.cpp index a6b8b5b3cd..7a3254454d 100644 --- a/source/source_md/test/langevin_test.cpp +++ b/source/source_md/test/langevin_test.cpp @@ -170,23 +170,11 @@ TEST_F(Langevin_test, print_md) std::ifstream ifs("running_langevin.log"); std::string output_str; getline(ifs, output_str); - EXPECT_THAT( - output_str, - testing::HasSubstr( - " ELECTRONIC PART OF STRESS: 0.2461 kbar" - )); + EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar")); getline(ifs, output_str); - EXPECT_THAT( - output_str, - testing::HasSubstr( - " IONIC (KINETIC) PART OF STRESS: 0.838539 kbar" - )); - getline(ifs, output_str); - EXPECT_THAT( - output_str, - testing::HasSubstr( - " MD PRESSURE (ELECTRONS+IONS) : 1.08464 kbar" - )); + EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.83853919 kbar")); + getline(ifs, output_str); + EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391 kbar")); getline(ifs, output_str); getline(ifs, output_str); EXPECT_THAT( diff --git a/source/source_md/test/msst_test.cpp b/source/source_md/test/msst_test.cpp index e8c54e805b..b59652a6fd 100644 --- a/source/source_md/test/msst_test.cpp +++ b/source/source_md/test/msst_test.cpp @@ -226,23 +226,11 @@ TEST_F(MSST_test, print_md) std::ifstream ifs("running_msst.log"); std::string output_str; getline(ifs, output_str); - EXPECT_THAT( - output_str, - testing::HasSubstr( - " ELECTRONIC PART OF STRESS: 0.2461 kbar" - )); + EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar")); getline(ifs, output_str); - EXPECT_THAT( - output_str, - testing::HasSubstr( - " IONIC (KINETIC) PART OF STRESS: 0.830154 kbar" - )); + EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.8301538 kbar")); // result different from other MD methods getline(ifs, output_str); - EXPECT_THAT( - output_str, - testing::HasSubstr( - " MD PRESSURE (ELECTRONS+IONS) : 1.07625 kbar" - )); + EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0762537 kbar")); // result different from other MD methods getline(ifs, output_str); getline(ifs, output_str); EXPECT_THAT( diff --git a/source/source_md/test/nhchain_test.cpp b/source/source_md/test/nhchain_test.cpp index f2ff553bba..60e9525d97 100644 --- a/source/source_md/test/nhchain_test.cpp +++ b/source/source_md/test/nhchain_test.cpp @@ -216,20 +216,11 @@ TEST_F(NHC_test, print_md) std::ifstream ifs("running_nhchain.log"); std::string output_str; getline(ifs, output_str); - EXPECT_THAT( - output_str, - testing::HasSubstr( - " ELECTRONIC PART OF STRESS: 0.2461 kbar")); + EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar")); getline(ifs, output_str); - EXPECT_THAT( - output_str, - testing::HasSubstr( - " IONIC (KINETIC) PART OF STRESS: 0.838539 kbar")); + EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.83853919 kbar")); getline(ifs, output_str); - EXPECT_THAT( - output_str, - testing::HasSubstr( - " MD PRESSURE (ELECTRONS+IONS) : 1.08464 kbar")); + EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391 kbar")); getline(ifs, output_str); getline(ifs, output_str); EXPECT_THAT( diff --git a/source/source_md/test/verlet_test.cpp b/source/source_md/test/verlet_test.cpp index de6cfbf462..00013abf15 100644 --- a/source/source_md/test/verlet_test.cpp +++ b/source/source_md/test/verlet_test.cpp @@ -311,26 +311,11 @@ TEST_F(Verlet_test, print_md) std::ifstream ifs("running_verlet.log"); std::string output_str; getline(ifs, output_str); - EXPECT_THAT( - output_str, - testing::HasSubstr( - " ELECTRONIC PART OF STRESS: 0.2461 kbar" - ) - ); + EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar")); getline(ifs, output_str); - EXPECT_THAT( - output_str, - testing::HasSubstr( - " IONIC (KINETIC) PART OF STRESS: 0.838539 kbar" - ) - ); + EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.83853919 kbar")); getline(ifs, output_str); - EXPECT_THAT( - output_str, - testing::HasSubstr( - " MD PRESSURE (ELECTRONS+IONS) : 1.08464 kbar" - ) - ); + EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391 kbar")); getline(ifs, output_str); getline(ifs, output_str); EXPECT_THAT(