Skip to content

Commit 8978f28

Browse files
mohanchenabacus_fixer
andauthored
reformat the MD output energy, potential, T, P, etc. (#7745)
* reformat the MD output energy, potential, T, P, etc. * fix failures in unittests * update json file style in source_io --------- Co-authored-by: abacus_fixer <mohanchen@pku.eud.cn>
1 parent 16afa8e commit 8978f28

12 files changed

Lines changed: 161 additions & 80 deletions

File tree

source/source_io/module_json/init_info.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include "init_info.h"
22

3-
#include "source_io/module_parameter/parameter.h"
3+
#include "source_io/module_parameter/input_parameter.h"
44
#include "para_json.h"
55
#include "abacusjson.h"
66

@@ -10,7 +10,7 @@ namespace Json
1010

1111
#ifdef __RAPIDJSON
1212

13-
void gen_init(UnitCell* ucell)
13+
void gen_init(UnitCell* ucell, const Input_para& inp)
1414
{
1515
std::string pgname = ucell->symm.pgname;
1616
std::string spgname = ucell->symm.spgname;
@@ -22,7 +22,7 @@ void gen_init(UnitCell* ucell)
2222

2323
int numAtoms = ucell->nat;
2424
AbacusJson::add_json({"init", "natom"}, numAtoms, false);
25-
AbacusJson::add_json({"init", "nband"}, PARAM.inp.nbands, false);
25+
AbacusJson::add_json({"init", "nband"}, inp.nbands, false);
2626

2727
// Json::AbacusJson::add_Json(numAtoms,false,"init", "natom");
2828
// Json::AbacusJson::add_Json(PARAM.inp.nbands,false,"init", "nband");
@@ -44,6 +44,28 @@ void gen_init(UnitCell* ucell)
4444
AbacusJson::add_json({"init", "nelectron"}, nelec_total, false);
4545

4646
// Json::AbacusJson::add_Json(nelec_total,false,"init", "nelectron");
47+
48+
// energy cutoff for wavefunctions (Ry)
49+
AbacusJson::add_json({"init", "ecutwfc"}, inp.ecutwfc, false);
50+
AbacusJson::add_json({"init", "ecutwfc_unit"}, "Ry", false);
51+
52+
// smearing method and sigma (Ry)
53+
AbacusJson::add_json({"init", "smearing_method"}, inp.smearing_method, false);
54+
AbacusJson::add_json({"init", "smearing_sigma"}, inp.smearing_sigma, false);
55+
AbacusJson::add_json({"init", "smearing_sigma_unit"}, "Ry", false);
56+
57+
// k-point mesh generation parameters
58+
AbacusJson::add_json({"init", "kmesh_type"}, inp.kmesh_type, false);
59+
Json::jsonValue kspacing_array(JarrayType);
60+
kspacing_array.JPushBack(inp.kspacing[0]);
61+
kspacing_array.JPushBack(inp.kspacing[1]);
62+
kspacing_array.JPushBack(inp.kspacing[2]);
63+
AbacusJson::add_json({"init", "kspacing"}, kspacing_array, false);
64+
Json::jsonValue koffset_array(JarrayType);
65+
koffset_array.JPushBack(inp.koffset[0]);
66+
koffset_array.JPushBack(inp.koffset[1]);
67+
koffset_array.JPushBack(inp.koffset[2]);
68+
AbacusJson::add_json({"init", "koffset"}, koffset_array, false);
4769
}
4870

4971
void add_nkstot(int nkstot)
@@ -54,7 +76,7 @@ void add_nkstot(int nkstot)
5476
// Json::AbacusJson::add_Json(nkstot_ibz,false,"init", "nkstot_ibz");
5577
}
5678

57-
void gen_stru(UnitCell* ucell)
79+
void gen_stru(UnitCell* ucell, const Input_para& inp)
5880
{
5981
AbacusJson::add_json({"comment"},
6082
"Unless otherwise specified, the unit of energy is eV and the unit of length is Angstrom",
@@ -77,7 +99,7 @@ void gen_stru(UnitCell* ucell)
7799

78100
Json::AbacusJson::add_json({"init", "element", atom_label}, atom_element, false);
79101

80-
std::string orbital_str = PARAM.inp.orbital_dir + orbital_fn[i];
102+
std::string orbital_str = inp.orbital_dir + orbital_fn[i];
81103
if (!orbital_str.compare(""))
82104
{
83105
Json::jsonValue nullValue;

source/source_io/module_json/init_info.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#include "source_cell/module_symmetry/symmetry.h"
55
#include "source_cell/unitcell.h"
66

7+
struct Input_para;
8+
79
/**
810
* @brief In this part of the code to complete the init part of the json tree.
911
*/
@@ -14,8 +16,9 @@ namespace Json
1416

1517
/**
1618
* @param ucell: ucell for reading json parameters.
19+
* @param inp: input parameters for reading json parameters.
1720
*/
18-
void gen_init(UnitCell* ucell);
21+
void gen_init(UnitCell* ucell, const Input_para& inp);
1922

2023
/**
2124
* @param nkstot,nkstot_ibz: two param in json tree
@@ -24,8 +27,9 @@ void add_nkstot(int nkstot);
2427

2528
/**
2629
* @param ucell: ucell for reading structure init in abacus.
30+
* @param inp: input parameters for reading orbital directory.
2731
*/
28-
void gen_stru(UnitCell* ucell);
32+
void gen_stru(UnitCell* ucell, const Input_para& inp);
2933
#endif
3034
} // namespace Json
3135
#endif

source/source_io/module_json/para_json.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,20 @@ void create_Json(UnitCell* ucell, const Parameter& param)
3737
{
3838
#ifdef __RAPIDJSON
3939
gen_general_info(param);
40-
gen_init(ucell);
41-
// gen_stru(ucell);
40+
gen_init(ucell, param.inp);
41+
// gen_stru(ucell, param.inp);
4242
#endif
4343
json_output();
4444
}
4545

46-
void gen_stru_wrapper(UnitCell* ucell)
46+
void gen_stru_wrapper(UnitCell* ucell, const Input_para& inp)
4747
{
4848
#ifdef __RAPIDJSON
4949
#ifdef __MPI
5050
if (GlobalV::MY_RANK == 0)
51-
gen_stru(ucell);
51+
gen_stru(ucell, inp);
5252
#else
53-
gen_stru(ucell);
53+
gen_stru(ucell, inp);
5454
#endif
5555
#endif
5656
}

source/source_io/module_json/para_json.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ void json_output();
1919
void convert_time(std::time_t time_now, std::string& time_str);
2020

2121
// generate struture wrapper function
22-
void gen_stru_wrapper(UnitCell *ucell);
22+
void gen_stru_wrapper(UnitCell *ucell, const Input_para& inp);
2323
} // namespace Json

source/source_io/module_json/test/para_json_test.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -210,16 +210,17 @@ TEST(AbacusJsonTest, GeneralInfo)
210210
std::time_t time_now = std::time(nullptr);
211211
std::string start_time_str;
212212
Json::convert_time(time_now, start_time_str);
213-
PARAM.sys.start_time = time_now;
214213

215-
PARAM.input.device = "cpu";
216-
PARAM.input.pseudo_dir = "./abacus/test/pseudo_dir";
217-
PARAM.input.orbital_dir = "./abacus/test/orbital_dir";
218-
PARAM.sys.global_in_stru = "./abacus/test/stru_file";
219-
PARAM.input.kpoint_file = "./abacus/test/kpoint_file";
214+
Parameter param;
215+
param.sys.start_time = time_now;
216+
param.input.device = "cpu";
217+
param.input.pseudo_dir = "./abacus/test/pseudo_dir";
218+
param.input.orbital_dir = "./abacus/test/orbital_dir";
219+
param.sys.global_in_stru = "./abacus/test/stru_file";
220+
param.input.kpoint_file = "./abacus/test/kpoint_file";
220221
// output the json file
221222
Json::AbacusJson::doc.Parse("{}");
222-
Json::gen_general_info(PARAM);
223+
Json::gen_general_info(param);
223224
Json::json_output();
224225

225226
std::string filename = "abacus.json";
@@ -257,7 +258,14 @@ TEST(AbacusJsonTest, InitInfo)
257258
ucell.symm.spgname = "O_h";
258259
ucell.atoms = atomlist;
259260
ucell.ntype = 3;
260-
PARAM.input.nbands = 10;
261+
Input_para inp;
262+
inp.nbands = 10;
263+
inp.ecutwfc = 50.0;
264+
inp.smearing_method = "gauss";
265+
inp.smearing_sigma = 0.015;
266+
inp.kspacing = {0.04, 0.04, 0.04};
267+
inp.koffset = {0.0, 0.0, 0.0};
268+
inp.kmesh_type = "gamma";
261269

262270
ucell.atoms[0].label = "Si";
263271
ucell.atoms[0].ncpp.zv = 3;
@@ -278,7 +286,7 @@ TEST(AbacusJsonTest, InitInfo)
278286
int Jnkstot = 1;
279287

280288
Json::add_nkstot(Jnkstot);
281-
Json::gen_init(&ucell);
289+
Json::gen_init(&ucell, inp);
282290

283291
ASSERT_TRUE(Json::AbacusJson::doc.HasMember("init"));
284292
ASSERT_EQ(Json::AbacusJson::doc["init"]["nkstot"].GetInt(), 1);
@@ -296,6 +304,19 @@ TEST(AbacusJsonTest, InitInfo)
296304
ASSERT_EQ(Json::AbacusJson::doc["init"]["natom_each_type"]["Si"].GetInt(), 1);
297305
ASSERT_EQ(Json::AbacusJson::doc["init"]["natom_each_type"]["C"].GetInt(), 2);
298306
ASSERT_EQ(Json::AbacusJson::doc["init"]["natom_each_type"]["O"].GetInt(), 3);
307+
308+
ASSERT_EQ(Json::AbacusJson::doc["init"]["ecutwfc"].GetDouble(), 50.0);
309+
ASSERT_STREQ(Json::AbacusJson::doc["init"]["ecutwfc_unit"].GetString(), "Ry");
310+
ASSERT_STREQ(Json::AbacusJson::doc["init"]["smearing_method"].GetString(), "gauss");
311+
ASSERT_EQ(Json::AbacusJson::doc["init"]["smearing_sigma"].GetDouble(), 0.015);
312+
ASSERT_STREQ(Json::AbacusJson::doc["init"]["smearing_sigma_unit"].GetString(), "Ry");
313+
ASSERT_STREQ(Json::AbacusJson::doc["init"]["kmesh_type"].GetString(), "gamma");
314+
ASSERT_EQ(Json::AbacusJson::doc["init"]["kspacing"][0].GetDouble(), 0.04);
315+
ASSERT_EQ(Json::AbacusJson::doc["init"]["kspacing"][1].GetDouble(), 0.04);
316+
ASSERT_EQ(Json::AbacusJson::doc["init"]["kspacing"][2].GetDouble(), 0.04);
317+
ASSERT_EQ(Json::AbacusJson::doc["init"]["koffset"][0].GetDouble(), 0.0);
318+
ASSERT_EQ(Json::AbacusJson::doc["init"]["koffset"][1].GetDouble(), 0.0);
319+
ASSERT_EQ(Json::AbacusJson::doc["init"]["koffset"][2].GetDouble(), 0.0);
299320
}
300321

301322
TEST(AbacusJsonTest, Init_stru_test)
@@ -347,7 +368,7 @@ TEST(AbacusJsonTest, Init_stru_test)
347368
ucell.atoms[i].tau[j] = 0.1 * j;
348369
}
349370
}
350-
Json::gen_stru(&ucell);
371+
Json::gen_stru(&ucell, Input_para{});
351372

352373
std::string filename = "readin.json";
353374
Json::AbacusJson::write_to_json(filename);

source/source_main/driver_run.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void Driver::driver_run()
7272

7373
// this Json part should be moved to before_all_runners, mohan 2024-05-12
7474
#ifdef __RAPIDJSON
75-
Json::gen_stru_wrapper(&ucell);
75+
Json::gen_stru_wrapper(&ucell, PARAM.inp);
7676
#endif
7777

7878
const std::string cal = PARAM.inp.calculation;

source/source_md/md_base.cpp

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -165,63 +165,67 @@ void MD_base::print_md(std::ofstream& ofs, const bool& cal_stress)
165165
}
166166

167167
// screen output
168-
std::cout << std::setprecision(8);
169-
std::cout << " ------------------------------------------------------------------------------------------------"
168+
std::cout << " -------------------------------------------------------------------------"
170169
<< std::endl;
171-
std::cout << " " << std::left << std::setw(20) << "Energy (Ry)" << std::left << std::setw(20) << "Potential (Ry)"
172-
<< std::left << std::setw(20) << "Kinetic (Ry)" << std::left << std::setw(20) << "Temperature (K)";
170+
std::cout << " " << std::left << std::setw(24) << "Energy (Ry)" << std::left << std::setw(24) << "Potential (Ry)"
171+
<< std::left << std::setw(24) << "Kinetic (Ry)" << std::endl;
172+
std::cout << std::setprecision(12);
173+
std::cout << " " << std::left << std::setw(24) << 2 * (potential + kinetic) << std::left << std::setw(24)
174+
<< 2 * potential << std::left << std::setw(24) << 2 * kinetic << std::endl;
175+
std::cout << " " << std::left << std::setw(24) << "Temperature (K)";
173176

174177
if (cal_stress)
175178
{
176-
std::cout << std::left << std::setw(20) << "Pressure (kbar)";
179+
std::cout << std::left << std::setw(24) << "Pressure (kbar)";
177180
}
178181

179182
std::cout << std::endl;
180-
std::cout << " " << std::left << std::setw(20) << 2 * (potential + kinetic) << std::left << std::setw(20)
181-
<< 2 * potential << std::left << std::setw(20) << 2 * kinetic << std::left << std::setw(20)
182-
<< t_current * ModuleBase::Hartree_to_K;
183+
std::cout << std::setprecision(6);
184+
std::cout << " " << std::left << std::setw(24) << t_current * ModuleBase::Hartree_to_K;
183185

184186
if (cal_stress)
185187
{
186-
std::cout << std::left << std::setw(20) << press * unit_transform;
188+
std::cout << std::left << std::setw(24) << press * unit_transform;
187189
}
188190

189191
std::cout << std::endl;
190-
std::cout << " ------------------------------------------------------------------------------------------------"
192+
std::cout << " -------------------------------------------------------------------------"
191193
<< std::endl;
192194

193195
// running_log output
194196
ofs.unsetf(std::ios::fixed);
195-
ofs << std::setprecision(8);
197+
ofs << std::setprecision(12);
196198

197199
if (cal_stress)
198200
{
199201
MD_func::print_stress(ofs, virial, stress);
200202
ofs << std::endl;
201203
}
202204

203-
ofs << " ------------------------------------------------------------------------------------------------"
205+
ofs << " -------------------------------------------------------------------------"
204206
<< std::endl;
205-
ofs << " " << std::left << std::setw(20) << "Energy (Ry)" << std::left << std::setw(20) << "Potential (Ry)"
206-
<< std::left << std::setw(20) << "Kinetic (Ry)" << std::left << std::setw(20) << "Temperature (K)";
207+
ofs << " " << std::left << std::setw(24) << "Energy (Ry)" << std::left << std::setw(24) << "Potential (Ry)"
208+
<< std::left << std::setw(24) << "Kinetic (Ry)" << std::endl;
209+
ofs << " " << std::left << std::setw(24) << 2 * (potential + kinetic) << std::left << std::setw(24) << 2 * potential
210+
<< std::left << std::setw(24) << 2 * kinetic << std::endl;
211+
ofs << " " << std::left << std::setw(24) << "Temperature (K)";
207212

208213
if (cal_stress)
209214
{
210-
ofs << std::left << std::setw(20) << "Pressure (kbar)";
215+
ofs << std::left << std::setw(24) << "Pressure (kbar)";
211216
}
212217

213218
ofs << std::endl;
214-
ofs << " " << std::left << std::setw(20) << 2 * (potential + kinetic) << std::left << std::setw(20) << 2 * potential
215-
<< std::left << std::setw(20) << 2 * kinetic << std::left << std::setw(20)
216-
<< t_current * ModuleBase::Hartree_to_K;
219+
ofs << std::setprecision(6);
220+
ofs << " " << std::left << std::setw(24) << t_current * ModuleBase::Hartree_to_K;
217221

218222
if (cal_stress)
219223
{
220-
ofs << std::left << std::setw(20) << press * unit_transform;
224+
ofs << std::left << std::setw(24) << press * unit_transform;
221225
}
222226

223227
ofs << std::endl;
224-
ofs << " ------------------------------------------------------------------------------------------------"
228+
ofs << " -------------------------------------------------------------------------"
225229
<< std::endl;
226230
ofs << std::endl;
227231
return;

source/source_md/test/fire_test.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,32 +185,38 @@ TEST_F(FIREtest, PrintMD)
185185
std::string output_str;
186186

187187
getline(ifs, output_str);
188-
EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992 kbar"));
188+
EXPECT_THAT(output_str, testing::HasSubstr(" ELECTRONIC PART OF STRESS: 0.24609992"));
189189
getline(ifs, output_str);
190-
EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.83853919 kbar"));
190+
EXPECT_THAT(output_str, testing::HasSubstr(" IONIC (KINETIC) PART OF STRESS: 0.838539188441"));
191191
getline(ifs, output_str);
192-
EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391 kbar"));
192+
EXPECT_THAT(output_str, testing::HasSubstr(" MD PRESSURE (ELECTRONS+IONS) : 1.0846391"));
193193
getline(ifs, output_str);
194194
getline(ifs, output_str);
195195
EXPECT_THAT(output_str,
196196
testing::HasSubstr(
197-
" ------------------------------------------------------------------------------------------------"));
197+
" ----------------------------------------"));
198198
getline(ifs, output_str);
199199
EXPECT_THAT(output_str,
200200
testing::HasSubstr(
201-
" Energy (Ry) Potential (Ry) Kinetic (Ry) Temperature (K) Pressure (kbar) "));
201+
" Energy (Ry) Potential (Ry) Kinetic (Ry) "));
202+
getline(ifs, output_str);
203+
EXPECT_THAT(output_str, testing::HasSubstr("-0.0153652356062"));
204+
EXPECT_THAT(output_str, testing::HasSubstr("-0.0239156372471"));
205+
EXPECT_THAT(output_str, testing::HasSubstr("0.00855040164087"));
202206
getline(ifs, output_str);
203207
EXPECT_THAT(output_str,
204208
testing::HasSubstr(
205-
" -0.015365236 -0.023915637 0.0085504016 300 1.0846391 "));
209+
" Temperature (K) Pressure (kbar) "));
210+
getline(ifs, output_str);
211+
EXPECT_THAT(output_str, testing::HasSubstr("1.08464"));
206212
getline(ifs, output_str);
207213
EXPECT_THAT(
208214
output_str,
209215
testing::HasSubstr(
210-
" ------------------------------------------------------------------------------------------------"));
216+
" ----------------------------------------"));
211217
getline(ifs, output_str);
212218
getline(ifs, output_str);
213-
EXPECT_THAT(output_str, testing::HasSubstr(" LARGEST FORCE (eV/A) : 0.049479926"));
219+
EXPECT_THAT(output_str, testing::HasSubstr(" LARGEST FORCE (eV/A) : 0.0494799"));
214220

215221
ifs.close();
216222
//remove("running_fire.log");

0 commit comments

Comments
 (0)