diff --git a/unittest/force-styles/angle_style.cpp b/unittest/force-styles/angle_style.cpp index d5e2059b2b..f5299d5b0e 100644 --- a/unittest/force-styles/angle_style.cpp +++ b/unittest/force-styles/angle_style.cpp @@ -51,16 +51,15 @@ using ::testing::HasSubstr; using namespace LAMMPS_NS; +static void delete_file(const std::string & filename) { + remove(filename.c_str()); +}; + void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg) { - std::string name; - - name = cfg.basename + ".restart"; - remove(name.c_str()); - name = cfg.basename + ".data"; - remove(name.c_str()); - name = cfg.basename + "-coeffs.in"; - remove(name.c_str()); + delete_file(cfg.basename + ".restart"); + delete_file(cfg.basename + ".data"); + delete_file(cfg.basename + "-coeffs.in"); delete lmp; } @@ -75,7 +74,7 @@ LAMMPS *init_lammps(int argc, char **argv, // check if prerequisite styles are available Info *info = new Info(lmp); int nfail = 0; - for (auto prerequisite : cfg.prerequisites) { + for (auto& prerequisite : cfg.prerequisites) { std::string style = prerequisite.second; // this is a test for angle styles, so if the suffixed @@ -92,106 +91,128 @@ LAMMPS *init_lammps(int argc, char **argv, if (nfail > 0) { delete info; cleanup_lammps(lmp,cfg); - return NULL; + return nullptr; } + // utility lambdas to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + auto parse_input_script = [&](const std::string & filename){ + lmp->input->file(filename.c_str()); + }; + if (newton) { - lmp->input->one("variable newton_bond index on"); + command("variable newton_bond index on"); } else { - lmp->input->one("variable newton_bond index off"); + command("variable newton_bond index off"); } - std::string set_input_dir = "variable input_dir index "; - set_input_dir += INPUT_FOLDER; - lmp->input->one(set_input_dir.c_str()); - for (auto pre_command : cfg.pre_commands) - lmp->input->one(pre_command.c_str()); + command("variable input_dir index " + INPUT_FOLDER); + + for (auto& pre_command : cfg.pre_commands) { + command(pre_command); + } std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file; - lmp->input->file(input_file.c_str()); + parse_input_script(input_file); - std::string cmd("angle_style "); - cmd += cfg.angle_style; - lmp->input->one(cmd.c_str()); - for (auto angle_coeff : cfg.angle_coeff) { - cmd = "angle_coeff " + angle_coeff; - lmp->input->one(cmd.c_str()); + command("angle_style " + cfg.angle_style); + + for (auto& angle_coeff : cfg.angle_coeff) { + command("angle_coeff " + angle_coeff); } - for (auto post_command : cfg.post_commands) - lmp->input->one(post_command.c_str()); - lmp->input->one("run 0 post no"); - cmd = "write_restart " + cfg.basename + ".restart"; - lmp->input->one(cmd.c_str()); - cmd = "write_data " + cfg.basename + ".data"; - lmp->input->one(cmd.c_str()); - cmd = "write_coeff " + cfg.basename + "-coeffs.in"; - lmp->input->one(cmd.c_str()); + + for (auto& post_command : cfg.post_commands) { + command(post_command); + } + + command("run 0 post no"); + command("write_restart " + cfg.basename + ".restart"); + command("write_data " + cfg.basename + ".data"); + command("write_coeff " + cfg.basename + "-coeffs.in"); return lmp; } void run_lammps(LAMMPS *lmp) { - lmp->input->one("fix 1 all nve"); - lmp->input->one("compute pe all pe/atom"); - lmp->input->one("compute sum all reduce sum c_pe"); - lmp->input->one("thermo_style custom step temp pe press c_sum"); - lmp->input->one("thermo 2"); - lmp->input->one("run 4 post no"); + // utility lambda to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + + command("fix 1 all nve"); + command("compute pe all pe/atom"); + command("compute sum all reduce sum c_pe"); + command("thermo_style custom step temp pe press c_sum"); + command("thermo 2"); + command("run 4 post no"); } void restart_lammps(LAMMPS *lmp, const TestConfig &cfg) { - lmp->input->one("clear"); - std::string cmd("read_restart "); - cmd += cfg.basename + ".restart"; - lmp->input->one(cmd.c_str()); + // utility lambda to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + + command("clear"); + command("read_restart " + cfg.basename + ".restart"); if (!lmp->force->angle) { - cmd = "angle_style " + cfg.angle_style; - lmp->input->one(cmd.c_str()); + command("angle_style " + cfg.angle_style); } + if ((cfg.angle_style.substr(0,6) == "hybrid") || !lmp->force->angle->writedata) { - for (auto angle_coeff : cfg.angle_coeff) { - cmd = "angle_coeff " + angle_coeff; - lmp->input->one(cmd.c_str()); + for (auto& angle_coeff : cfg.angle_coeff) { + command("angle_coeff " + angle_coeff); } } - for (auto post_command : cfg.post_commands) - lmp->input->one(post_command.c_str()); - lmp->input->one("run 0 post no"); + + for (auto& post_command : cfg.post_commands) { + command(post_command); + } + + command("run 0 post no"); } void data_lammps(LAMMPS *lmp, const TestConfig &cfg) { - lmp->input->one("clear"); - lmp->input->one("variable angle_style delete"); - lmp->input->one("variable data_file delete"); - lmp->input->one("variable newton_bond delete"); - lmp->input->one("variable newton_bond index on"); + // utility lambdas to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + auto parse_input_script = [&](const std::string & filename){ + lmp->input->file(filename.c_str()); + }; - for (auto pre_command : cfg.pre_commands) - lmp->input->one(pre_command.c_str()); + command("clear"); + command("variable angle_style delete"); + command("variable data_file delete"); + command("variable newton_bond delete"); + command("variable newton_bond index on"); - std::string cmd("variable angle_style index '"); - cmd += cfg.angle_style + "'"; - lmp->input->one(cmd.c_str()); + for (auto& pre_command : cfg.pre_commands) { + command(pre_command); + } - cmd = "variable data_file index "; - cmd += cfg.basename + ".data"; - lmp->input->one(cmd.c_str()); + command("variable angle_style index '" + cfg.angle_style + "'"); + command("variable data_file index " + cfg.basename + ".data"); std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file; - lmp->input->file(input_file.c_str()); + parse_input_script(input_file); - for (auto angle_coeff : cfg.angle_coeff) { - cmd = "angle_coeff " + angle_coeff; - lmp->input->one(cmd.c_str()); + for (auto& angle_coeff : cfg.angle_coeff) { + command("angle_coeff " + angle_coeff); } - for (auto post_command : cfg.post_commands) - lmp->input->one(post_command.c_str()); - lmp->input->one("run 0 post no"); + + for (auto& post_command : cfg.post_commands) { + command(post_command); + } + + command("run 0 post no"); } @@ -207,7 +228,7 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) if (!lmp) { std::cerr << "One or more prerequisite styles are not available " "in this LAMMPS configuration:\n"; - for (auto prerequisite : config.prerequisites) { + for (auto& prerequisite : config.prerequisites) { std::cerr << prerequisite.first << "_style " << prerequisite.second << "\n"; } @@ -235,21 +256,21 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) // prerequisites block.clear(); - for (auto prerequisite : config.prerequisites) { + for (auto& prerequisite : config.prerequisites) { block += prerequisite.first + " " + prerequisite.second + "\n"; } writer.emit_block("prerequisites", block); // pre_commands block.clear(); - for (auto command : config.pre_commands) { + for (auto& command : config.pre_commands) { block += command + "\n"; } writer.emit_block("pre_commands", block); // post_commands block.clear(); - for (auto command : config.post_commands) { + for (auto& command : config.post_commands) { block += command + "\n"; } writer.emit_block("post_commands", block); @@ -262,7 +283,7 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) // angle_coeff block.clear(); - for (auto angle_coeff : config.angle_coeff) { + for (auto& angle_coeff : config.angle_coeff) { block += angle_coeff + "\n"; } writer.emit_block("angle_coeff", block); @@ -270,7 +291,7 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) // extract block.clear(); std::stringstream outstr; - for (auto data : config.extract) { + for (auto& data : config.extract) { outstr << data.first << " " << data.second << std::endl; } writer.emit_block("extract", outstr.str()); @@ -336,7 +357,7 @@ TEST(AngleStyle, plain) { if (!lmp) { std::cerr << "One or more prerequisite styles are not available " "in this LAMMPS configuration:\n"; - for (auto prerequisite : test_config.prerequisites) { + for (auto& prerequisite : test_config.prerequisites) { std::cerr << prerequisite.first << "_style " << prerequisite.second << "\n"; } @@ -570,7 +591,7 @@ TEST(AngleStyle, omp) { if (!lmp) { std::cerr << "One or more prerequisite styles with /omp suffix\n" "are not available in this LAMMPS configuration:\n"; - for (auto prerequisite : test_config.prerequisites) { + for (auto& prerequisite : test_config.prerequisites) { std::cerr << prerequisite.first << "_style " << prerequisite.second << "\n"; } diff --git a/unittest/force-styles/bond_style.cpp b/unittest/force-styles/bond_style.cpp index 07e3e28e1b..d44eec995d 100644 --- a/unittest/force-styles/bond_style.cpp +++ b/unittest/force-styles/bond_style.cpp @@ -51,16 +51,15 @@ using ::testing::HasSubstr; using namespace LAMMPS_NS; +static void delete_file(const std::string & filename) { + remove(filename.c_str()); +}; + void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg) { - std::string name; - - name = cfg.basename + ".restart"; - remove(name.c_str()); - name = cfg.basename + ".data"; - remove(name.c_str()); - name = cfg.basename + "-coeffs.in"; - remove(name.c_str()); + delete_file(cfg.basename + ".restart"); + delete_file(cfg.basename + ".data"); + delete_file(cfg.basename + "-coeffs.in"); delete lmp; } @@ -95,104 +94,125 @@ LAMMPS *init_lammps(int argc, char **argv, return nullptr; } + // utility lambdas to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + auto parse_input_script = [&](const std::string & filename){ + lmp->input->file(filename.c_str()); + }; + if (newton) { - lmp->input->one("variable newton_bond index on"); + command("variable newton_bond index on"); } else { - lmp->input->one("variable newton_bond index off"); + command("variable newton_bond index off"); } - std::string set_input_dir = "variable input_dir index "; - set_input_dir += INPUT_FOLDER; - lmp->input->one(set_input_dir.c_str()); - for (auto pre_command : cfg.pre_commands) - lmp->input->one(pre_command.c_str()); + command("variable input_dir index " + INPUT_FOLDER); - std::string input_file = INPUT_FOLDER + "/" + cfg.input_file; - - lmp->input->file(input_file.c_str()); - - std::string cmd("bond_style "); - cmd += cfg.bond_style; - lmp->input->one(cmd.c_str()); - for (auto bond_coeff : cfg.bond_coeff) { - cmd = "bond_coeff " + bond_coeff; - lmp->input->one(cmd.c_str()); + for (auto& pre_command : cfg.pre_commands) { + command(pre_command); } - for (auto post_command : cfg.post_commands) - lmp->input->one(post_command.c_str()); - lmp->input->one("run 0 post no"); - cmd = "write_restart " + cfg.basename + ".restart"; - lmp->input->one(cmd.c_str()); - cmd = "write_data " + cfg.basename + ".data"; - lmp->input->one(cmd.c_str()); - cmd = "write_coeff " + cfg.basename + "-coeffs.in"; - lmp->input->one(cmd.c_str()); + + std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file; + parse_input_script(input_file); + + command("bond_style " + cfg.bond_style); + + for (auto& bond_coeff : cfg.bond_coeff) { + command("bond_coeff " + bond_coeff); + } + + for (auto& post_command : cfg.post_commands) { + command(post_command); + } + + command("run 0 post no"); + command("write_restart " + cfg.basename + ".restart"); + command("write_data " + cfg.basename + ".data"); + command("write_coeff " + cfg.basename + "-coeffs.in"); return lmp; } void run_lammps(LAMMPS *lmp) { - lmp->input->one("fix 1 all nve"); - lmp->input->one("compute pe all pe/atom"); - lmp->input->one("compute sum all reduce sum c_pe"); - lmp->input->one("thermo_style custom step temp pe press c_sum"); - lmp->input->one("thermo 2"); - lmp->input->one("run 4 post no"); + // utility lambda to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + + command("fix 1 all nve"); + command("compute pe all pe/atom"); + command("compute sum all reduce sum c_pe"); + command("thermo_style custom step temp pe press c_sum"); + command("thermo 2"); + command("run 4 post no"); } void restart_lammps(LAMMPS *lmp, const TestConfig &cfg) { - lmp->input->one("clear"); - std::string cmd("read_restart "); - cmd += cfg.basename + ".restart"; - lmp->input->one(cmd.c_str()); + // utility lambda to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + + command("clear"); + command("read_restart " + cfg.basename + ".restart"); if (!lmp->force->bond) { - cmd = "bond_style " + cfg.bond_style; - lmp->input->one(cmd.c_str()); + command("bond_style " + cfg.bond_style); } + if ((cfg.bond_style.substr(0,6) == "hybrid") || !lmp->force->bond->writedata) { - for (auto bond_coeff : cfg.bond_coeff) { - cmd = "bond_coeff " + bond_coeff; - lmp->input->one(cmd.c_str()); + for (auto& bond_coeff : cfg.bond_coeff) { + command("bond_coeff " + bond_coeff); } } - for (auto post_command : cfg.post_commands) - lmp->input->one(post_command.c_str()); - lmp->input->one("run 0 post no"); + + for (auto& post_command : cfg.post_commands) { + command(post_command); + } + + command("run 0 post no"); } void data_lammps(LAMMPS *lmp, const TestConfig &cfg) { - lmp->input->one("clear"); - lmp->input->one("variable bond_style delete"); - lmp->input->one("variable data_file delete"); - lmp->input->one("variable newton_bond delete"); - lmp->input->one("variable newton_bond index on"); + // utility lambdas to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + auto parse_input_script = [&](const std::string & filename){ + lmp->input->file(filename.c_str()); + }; - for (auto pre_command : cfg.pre_commands) - lmp->input->one(pre_command.c_str()); + command("clear"); + command("variable bond_style delete"); + command("variable data_file delete"); + command("variable newton_bond delete"); + command("variable newton_bond index on"); - std::string cmd("variable bond_style index '"); - cmd += cfg.bond_style + "'"; - lmp->input->one(cmd.c_str()); + for (auto& pre_command : cfg.pre_commands) { + command(pre_command); + } - cmd = "variable data_file index "; - cmd += cfg.basename + ".data"; - lmp->input->one(cmd.c_str()); + command("variable bond_style index '" + cfg.bond_style + "'"); + command("variable data_file index " + cfg.basename + ".data"); std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file; - lmp->input->file(input_file.c_str()); + parse_input_script(input_file); - for (auto bond_coeff : cfg.bond_coeff) { - cmd = "bond_coeff " + bond_coeff; - lmp->input->one(cmd.c_str()); + for (auto& bond_coeff : cfg.bond_coeff) { + command("bond_coeff " + bond_coeff); } - for (auto post_command : cfg.post_commands) - lmp->input->one(post_command.c_str()); - lmp->input->one("run 0 post no"); + + for (auto& post_command : cfg.post_commands) { + command(post_command); + } + + command("run 0 post no"); } // re-generate yaml file with current settings. @@ -761,63 +781,68 @@ TEST(BondStyle, single) { GTEST_SKIP(); } + // utility lambda to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + Bond *bond = lmp->force->bond; // now start over if (!verbose) ::testing::internal::CaptureStdout(); - lmp->input->one("clear"); - lmp->input->one("variable newton_bond delete"); - lmp->input->one("variable newton_bond index on"); + command("clear"); + command("variable newton_bond delete"); + command("variable newton_bond index on"); - std::string set_input_dir = "variable input_dir index "; - set_input_dir += INPUT_FOLDER; - lmp->input->one(set_input_dir.c_str()); - for (auto pre_command : test_config.pre_commands) - lmp->input->one(pre_command.c_str()); + command("variable input_dir index " + INPUT_FOLDER); - lmp->input->one("atom_style molecular"); - lmp->input->one("units ${units}"); - lmp->input->one("boundary p p p"); - lmp->input->one("newton ${newton_pair} ${newton_bond}"); - lmp->input->one("special_bonds lj/coul " - "${bond_factor} ${angle_factor} ${dihedral_factor}"); + for (auto& pre_command : test_config.pre_commands) { + command(pre_command); + } + + command("atom_style molecular"); + command("units ${units}"); + command("boundary p p p"); + command("newton ${newton_pair} ${newton_bond}"); + command("special_bonds lj/coul " + "${bond_factor} ${angle_factor} ${dihedral_factor}"); + + command("atom_modify map array"); + command("region box block -10.0 10.0 -10.0 10.0 -10.0 10.0 units box"); - lmp->input->one("atom_modify map array"); - lmp->input->one("region box block -10.0 10.0 -10.0 10.0 -10.0 10.0 units box"); char buf[10]; - snprintf(buf,10,"%d",ntypes); std::string cmd("create_box 1 box"); cmd += " bond/types "; snprintf(buf,10,"%d",nbondtypes); cmd += buf; cmd += " extra/bond/per/atom 2"; cmd += " extra/special/per/atom 2"; - lmp->input->one(cmd.c_str()); + command(cmd); - lmp->input->one("pair_style zero 8.0"); - lmp->input->one("pair_coeff * *"); + command("pair_style zero 8.0"); + command("pair_coeff * *"); - cmd = "bond_style "; - cmd += test_config.bond_style; - lmp->input->one(cmd.c_str()); + command("bond_style " + test_config.bond_style); bond = lmp->force->bond; for (auto bond_coeff : test_config.bond_coeff) { - cmd = "bond_coeff " + bond_coeff; - lmp->input->one(cmd.c_str()); + command("bond_coeff " + bond_coeff); } // create (only) four atoms and two bonds - lmp->input->one("mass * 1.0"); - lmp->input->one("create_atoms 1 single 5.0 -0.75 0.4 units box"); - lmp->input->one("create_atoms 1 single 5.5 0.25 -0.1 units box"); - lmp->input->one("create_atoms 1 single -5.0 0.75 0.4 units box"); - lmp->input->one("create_atoms 1 single -5.5 -0.25 -0.1 units box"); - lmp->input->one("create_bonds single/bond 1 1 2"); - lmp->input->one("create_bonds single/bond 2 3 4"); - for (auto post_command : test_config.post_commands) - lmp->input->one(post_command.c_str()); - lmp->input->one("run 0 post no"); + command("mass * 1.0"); + command("create_atoms 1 single 5.0 -0.75 0.4 units box"); + command("create_atoms 1 single 5.5 0.25 -0.1 units box"); + command("create_atoms 1 single -5.0 0.75 0.4 units box"); + command("create_atoms 1 single -5.5 -0.25 -0.1 units box"); + command("create_bonds single/bond 1 1 2"); + command("create_bonds single/bond 2 3 4"); + + for (auto& post_command : test_config.post_commands) { + command(post_command); + } + + command("run 0 post no"); if (!verbose) ::testing::internal::GetCapturedStdout(); int idx1 = lmp->atom->map(1); @@ -857,8 +882,8 @@ TEST(BondStyle, single) { EXPECT_FP_LE_WITH_EPS(f[idx4][2], fsingle*delz2, epsilon); if (!verbose) ::testing::internal::CaptureStdout(); - lmp->input->one("displace_atoms all random 0.5 0.5 0.5 23456"); - lmp->input->one("run 0 post no"); + command("displace_atoms all random 0.5 0.5 0.5 23456"); + command("run 0 post no"); if (!verbose) ::testing::internal::GetCapturedStdout(); f = lmp->atom->f; @@ -895,8 +920,8 @@ TEST(BondStyle, single) { EXPECT_FP_LE_WITH_EPS(f[idx4][2], fsingle*delz2, epsilon); if (!verbose) ::testing::internal::CaptureStdout(); - lmp->input->one("displace_atoms all random 0.5 0.5 0.5 456963"); - lmp->input->one("run 0 post no"); + command("displace_atoms all random 0.5 0.5 0.5 456963"); + command("run 0 post no"); if (!verbose) ::testing::internal::GetCapturedStdout(); f = lmp->atom->f; @@ -933,8 +958,8 @@ TEST(BondStyle, single) { EXPECT_FP_LE_WITH_EPS(f[idx4][2], fsingle*delz2, epsilon); if (!verbose) ::testing::internal::CaptureStdout(); - lmp->input->one("displace_atoms all random 0.5 0.5 0.5 9726532"); - lmp->input->one("run 0 post no"); + command("displace_atoms all random 0.5 0.5 0.5 9726532"); + command("run 0 post no"); if (!verbose) ::testing::internal::GetCapturedStdout(); f = lmp->atom->f; diff --git a/unittest/force-styles/pair_style.cpp b/unittest/force-styles/pair_style.cpp index e1aa42879b..50e1c010c8 100644 --- a/unittest/force-styles/pair_style.cpp +++ b/unittest/force-styles/pair_style.cpp @@ -51,16 +51,15 @@ using ::testing::HasSubstr; using namespace LAMMPS_NS; +static void delete_file(const std::string & filename) { + remove(filename.c_str()); +}; + void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg) { - std::string name; - - name = cfg.basename + ".restart"; - remove(name.c_str()); - name = cfg.basename + ".data"; - remove(name.c_str()); - name = cfg.basename + "-coeffs.in"; - remove(name.c_str()); + delete_file(cfg.basename + ".restart"); + delete_file(cfg.basename + ".data"); + delete_file(cfg.basename + "-coeffs.in"); delete lmp; } @@ -75,7 +74,7 @@ LAMMPS *init_lammps(int argc, char **argv, // check if prerequisite styles are available Info *info = new Info(lmp); int nfail = 0; - for (auto prerequisite : cfg.prerequisites) { + for (auto& prerequisite : cfg.prerequisites) { std::string style = prerequisite.second; // this is a test for pair styles, so if the suffixed @@ -95,103 +94,122 @@ LAMMPS *init_lammps(int argc, char **argv, return nullptr; } + // utility lambdas to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + auto parse_input_script = [&](const std::string & filename){ + lmp->input->file(filename.c_str()); + }; + if (newton) { - lmp->input->one("variable newton_pair index on"); + command("variable newton_pair index on"); } else { - lmp->input->one("variable newton_pair index off"); + command("variable newton_pair index off"); } - std::string set_input_dir = "variable input_dir index "; - set_input_dir += INPUT_FOLDER; - lmp->input->one(set_input_dir.c_str()); - for (auto pre_command : cfg.pre_commands) - lmp->input->one(pre_command.c_str()); + command("variable input_dir index " + INPUT_FOLDER); + + for (auto& pre_command : cfg.pre_commands) { + command(pre_command); + } std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file; - lmp->input->file(input_file.c_str()); + parse_input_script(input_file); - std::string cmd("pair_style "); - cmd += cfg.pair_style; - lmp->input->one(cmd.c_str()); - for (auto pair_coeff : cfg.pair_coeff) { - cmd = "pair_coeff " + pair_coeff; - lmp->input->one(cmd.c_str()); + command("pair_style " + cfg.pair_style); + + for (auto& pair_coeff : cfg.pair_coeff) { + command("pair_coeff " + pair_coeff); } - for (auto post_command : cfg.post_commands) - lmp->input->one(post_command.c_str()); - lmp->input->one("run 0 post no"); - cmd = "write_restart " + cfg.basename + ".restart"; - lmp->input->one(cmd.c_str()); - cmd = "write_data " + cfg.basename + ".data"; - lmp->input->one(cmd.c_str()); - cmd = "write_coeff " + cfg.basename + "-coeffs.in"; - lmp->input->one(cmd.c_str()); + + for (auto& post_command : cfg.post_commands) { + command(post_command); + } + + command("run 0 post no"); + command("write_restart " + cfg.basename + ".restart"); + command("write_data " + cfg.basename + ".data"); + command("write_coeff " + cfg.basename + "-coeffs.in"); return lmp; } void run_lammps(LAMMPS *lmp) { - lmp->input->one("fix 1 all nve"); - lmp->input->one("compute pe all pe/atom"); - lmp->input->one("compute sum all reduce sum c_pe"); - lmp->input->one("thermo_style custom step temp pe press c_sum"); - lmp->input->one("thermo 2"); - lmp->input->one("run 4 post no"); + // utility lambda to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + + command("fix 1 all nve"); + command("compute pe all pe/atom"); + command("compute sum all reduce sum c_pe"); + command("thermo_style custom step temp pe press c_sum"); + command("thermo 2"); + command("run 4 post no"); } void restart_lammps(LAMMPS *lmp, const TestConfig &cfg) { - lmp->input->one("clear"); - std::string cmd("read_restart "); - cmd += cfg.basename + ".restart"; - lmp->input->one(cmd.c_str()); + // utility lambda to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + + command("clear"); + command("read_restart " + cfg.basename + ".restart"); if (!lmp->force->pair) { - cmd = "pair_style " + cfg.pair_style; - lmp->input->one(cmd.c_str()); + command("pair_style " + cfg.pair_style); } if (!lmp->force->pair->restartinfo || !lmp->force->pair->writedata) { - for (auto pair_coeff : cfg.pair_coeff) { - cmd = "pair_coeff " + pair_coeff; - lmp->input->one(cmd.c_str()); + for (auto& pair_coeff : cfg.pair_coeff) { + command("pair_coeff " + pair_coeff); } } - for (auto post_command : cfg.post_commands) - lmp->input->one(post_command.c_str()); - lmp->input->one("run 0 post no"); + + for (auto& post_command : cfg.post_commands) { + command(post_command); + } + + command("run 0 post no"); } void data_lammps(LAMMPS *lmp, const TestConfig &cfg) { - lmp->input->one("clear"); - lmp->input->one("variable pair_style delete"); - lmp->input->one("variable data_file delete"); - lmp->input->one("variable newton_pair delete"); - lmp->input->one("variable newton_pair index on"); + // utility lambdas to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + auto parse_input_script = [&](const std::string & filename){ + lmp->input->file(filename.c_str()); + }; - for (auto pre_command : cfg.pre_commands) - lmp->input->one(pre_command.c_str()); + command("clear"); + command("variable pair_style delete"); + command("variable data_file delete"); + command("variable newton_pair delete"); + command("variable newton_pair index on"); - std::string cmd("variable pair_style index '"); - cmd += cfg.pair_style + "'"; - lmp->input->one(cmd.c_str()); + for (auto& pre_command : cfg.pre_commands) { + command(pre_command); + } - cmd = "variable data_file index "; - cmd += cfg.basename + ".data"; - lmp->input->one(cmd.c_str()); + command("variable pair_style index '" + cfg.pair_style + "'"); + command("variable data_file index " + cfg.basename + ".data"); std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file; - lmp->input->file(input_file.c_str()); + parse_input_script(input_file); - for (auto pair_coeff : cfg.pair_coeff) { - cmd = "pair_coeff " + pair_coeff; - lmp->input->one(cmd.c_str()); + for (auto& pair_coeff : cfg.pair_coeff) { + command("pair_coeff " + pair_coeff); } - for (auto post_command : cfg.post_commands) - lmp->input->one(post_command.c_str()); - lmp->input->one("run 0 post no"); + for (auto& post_command : cfg.post_commands) { + command(post_command); + } + command("run 0 post no"); } // re-generate yaml file with current settings. @@ -1011,62 +1029,73 @@ TEST(PairStyle, single) { // now start over if (!verbose) ::testing::internal::CaptureStdout(); - lmp->input->one("clear"); - lmp->input->one("variable newton_pair delete"); - lmp->input->one("variable newton_pair index on"); - std::string set_input_dir = "variable input_dir index "; - set_input_dir += INPUT_FOLDER; - lmp->input->one(set_input_dir.c_str()); - for (auto pre_command : test_config.pre_commands) - lmp->input->one(pre_command.c_str()); + // utility lambda to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; - lmp->input->one("atom_style full"); - lmp->input->one("units ${units}"); - lmp->input->one("boundary p p p"); - lmp->input->one("newton ${newton_pair} ${newton_bond}"); - if (molecular) { - lmp->input->one("special_bonds lj/coul " - "${bond_factor} ${angle_factor} ${dihedral_factor}"); + command("clear"); + command("variable newton_pair delete"); + command("variable newton_pair index on"); + + command("variable input_dir index " + INPUT_FOLDER); + + for (auto& pre_command : test_config.pre_commands) { + command(pre_command); } - lmp->input->one("atom_modify map array"); - lmp->input->one("region box block -10.0 10.0 -10.0 10.0 -10.0 10.0 units box"); + + command("atom_style full"); + command("units ${units}"); + command("boundary p p p"); + command("newton ${newton_pair} ${newton_bond}"); + + if (molecular) { + command("special_bonds lj/coul " + "${bond_factor} ${angle_factor} ${dihedral_factor}"); + } + + command("atom_modify map array"); + command("region box block -10.0 10.0 -10.0 10.0 -10.0 10.0 units box"); + char buf[10]; snprintf(buf,10,"%d",ntypes); std::string cmd("create_box "); cmd += buf; cmd += " box"; if (molecular) { - cmd += " bond/types 1"; - cmd += " extra/bond/per/atom 1"; - cmd += " extra/special/per/atom 1"; + cmd += " bond/types 1" + " extra/bond/per/atom 1" + " extra/special/per/atom 1"; } - lmp->input->one(cmd.c_str()); + command(cmd); + + command("pair_style " + test_config.pair_style); - cmd = "pair_style "; - cmd += test_config.pair_style; - lmp->input->one(cmd.c_str()); pair = lmp->force->pair; - for (auto pair_coeff : test_config.pair_coeff) { - cmd = "pair_coeff " + pair_coeff; - lmp->input->one(cmd.c_str()); + for (auto& pair_coeff : test_config.pair_coeff) { + command("pair_coeff " + pair_coeff); } // create (only) two atoms - lmp->input->one("mass * 1.0"); - lmp->input->one("create_atoms 1 single 0.0 -0.75 0.4 units box"); - lmp->input->one("create_atoms 2 single 0.5 0.25 -0.1 units box"); - lmp->input->one("set atom 1 charge -0.5"); - lmp->input->one("set atom 2 charge 0.5"); + command("mass * 1.0"); + command("create_atoms 1 single 0.0 -0.75 0.4 units box"); + command("create_atoms 2 single 0.5 0.25 -0.1 units box"); + command("set atom 1 charge -0.5"); + command("set atom 2 charge 0.5"); + if (molecular) { - lmp->input->one("create_bonds single/bond 1 1 2"); - lmp->input->one("bond_style zero"); - lmp->input->one("bond_coeff 1 2.0"); + command("create_bonds single/bond 1 1 2"); + command("bond_style zero"); + command("bond_coeff 1 2.0"); } - for (auto post_command : test_config.post_commands) - lmp->input->one(post_command.c_str()); - lmp->input->one("run 0 post no"); + + for (auto& post_command : test_config.post_commands) { + command(post_command); + } + + command("run 0 post no"); if (!verbose) ::testing::internal::GetCapturedStdout(); int idx1 = lmp->atom->map(1); @@ -1094,8 +1123,8 @@ TEST(PairStyle, single) { EXPECT_FP_LE_WITH_EPS(f[idx2][2], fsingle*delz, epsilon); if (!verbose) ::testing::internal::CaptureStdout(); - lmp->input->one("displace_atoms all random 1.0 1.0 1.0 723456"); - lmp->input->one("run 0 post no"); + command("displace_atoms all random 1.0 1.0 1.0 723456"); + command("run 0 post no"); if (!verbose) ::testing::internal::GetCapturedStdout(); f = lmp->atom->f; @@ -1118,8 +1147,8 @@ TEST(PairStyle, single) { EXPECT_FP_LE_WITH_EPS(f[idx2][2], fsingle*delz, epsilon); if (!verbose) ::testing::internal::CaptureStdout(); - lmp->input->one("displace_atoms all random 1.0 1.0 1.0 3456963"); - lmp->input->one("run 0 post no"); + command("displace_atoms all random 1.0 1.0 1.0 3456963"); + command("run 0 post no"); if (!verbose) ::testing::internal::GetCapturedStdout(); f = lmp->atom->f; @@ -1142,8 +1171,8 @@ TEST(PairStyle, single) { EXPECT_FP_LE_WITH_EPS(f[idx2][2], fsingle*delz, epsilon); if (!verbose) ::testing::internal::CaptureStdout(); - lmp->input->one("displace_atoms all random 0.5 0.5 0.5 9726532"); - lmp->input->one("run 0 post no"); + command("displace_atoms all random 0.5 0.5 0.5 9726532"); + command("run 0 post no"); if (!verbose) ::testing::internal::GetCapturedStdout(); f = lmp->atom->f; @@ -1201,8 +1230,8 @@ TEST(PairStyle, extract) { Pair *pair = lmp->force->pair; void *ptr = nullptr; int dim = 0; - for (auto extract : test_config.extract) { - ptr = pair->extract(extract.first.c_str(),dim); + for (auto& extract : test_config.extract) { + ptr = pair->extract(extract.first.c_str(), dim); EXPECT_NE(ptr, nullptr); EXPECT_EQ(dim, extract.second); } @@ -1218,16 +1247,21 @@ TEST(PairStyle, extract) { pair->cutsq[i][j] = -1.0; } } - std::string cmd = "pair_style "; - cmd += test_config.pair_style; - lmp->input->one(cmd.c_str()); - EXPECT_EQ(pair,lmp->force->pair); - for (auto pair_coeff : test_config.pair_coeff) { - cmd = "pair_coeff " + pair_coeff; - lmp->input->one(cmd.c_str()); + // utility lambda to improve readability + auto command = [&](const std::string & line){ + lmp->input->one(line.c_str()); + }; + + command("pair_style " + test_config.pair_style); + EXPECT_EQ(pair, lmp->force->pair); + + for (auto& pair_coeff : test_config.pair_coeff) { + command("pair_coeff " + pair_coeff); } + pair->init(); + for (int i=1; i <= ntypes; ++i) { for (int j=1; j <= ntypes; ++j) { EXPECT_GE(pair->cutsq[i][j],0.0);