Simplify tests

This commit is contained in:
Richard Berger
2020-05-20 04:12:48 -04:00
parent 5533b9233f
commit 76fb797264
3 changed files with 395 additions and 315 deletions

View File

@ -51,16 +51,15 @@ using ::testing::HasSubstr;
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
static void delete_file(const std::string & filename) {
remove(filename.c_str());
};
void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg) void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg)
{ {
std::string name; delete_file(cfg.basename + ".restart");
delete_file(cfg.basename + ".data");
name = cfg.basename + ".restart"; delete_file(cfg.basename + "-coeffs.in");
remove(name.c_str());
name = cfg.basename + ".data";
remove(name.c_str());
name = cfg.basename + "-coeffs.in";
remove(name.c_str());
delete lmp; delete lmp;
} }
@ -75,7 +74,7 @@ LAMMPS *init_lammps(int argc, char **argv,
// check if prerequisite styles are available // check if prerequisite styles are available
Info *info = new Info(lmp); Info *info = new Info(lmp);
int nfail = 0; int nfail = 0;
for (auto prerequisite : cfg.prerequisites) { for (auto& prerequisite : cfg.prerequisites) {
std::string style = prerequisite.second; std::string style = prerequisite.second;
// this is a test for angle styles, so if the suffixed // 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) { if (nfail > 0) {
delete info; delete info;
cleanup_lammps(lmp,cfg); 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) { if (newton) {
lmp->input->one("variable newton_bond index on"); command("variable newton_bond index on");
} else { } else {
lmp->input->one("variable newton_bond index off"); command("variable newton_bond index off");
} }
std::string set_input_dir = "variable input_dir index "; command("variable input_dir index " + INPUT_FOLDER);
set_input_dir += INPUT_FOLDER;
lmp->input->one(set_input_dir.c_str()); for (auto& pre_command : cfg.pre_commands) {
for (auto pre_command : cfg.pre_commands) command(pre_command);
lmp->input->one(pre_command.c_str()); }
std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file; 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 "); command("angle_style " + cfg.angle_style);
cmd += cfg.angle_style;
lmp->input->one(cmd.c_str()); for (auto& angle_coeff : cfg.angle_coeff) {
for (auto angle_coeff : cfg.angle_coeff) { command("angle_coeff " + angle_coeff);
cmd = "angle_coeff " + angle_coeff;
lmp->input->one(cmd.c_str());
} }
for (auto post_command : cfg.post_commands)
lmp->input->one(post_command.c_str()); for (auto& post_command : cfg.post_commands) {
lmp->input->one("run 0 post no"); command(post_command);
cmd = "write_restart " + cfg.basename + ".restart"; }
lmp->input->one(cmd.c_str());
cmd = "write_data " + cfg.basename + ".data"; command("run 0 post no");
lmp->input->one(cmd.c_str()); command("write_restart " + cfg.basename + ".restart");
cmd = "write_coeff " + cfg.basename + "-coeffs.in"; command("write_data " + cfg.basename + ".data");
lmp->input->one(cmd.c_str()); command("write_coeff " + cfg.basename + "-coeffs.in");
return lmp; return lmp;
} }
void run_lammps(LAMMPS *lmp) void run_lammps(LAMMPS *lmp)
{ {
lmp->input->one("fix 1 all nve"); // utility lambda to improve readability
lmp->input->one("compute pe all pe/atom"); auto command = [&](const std::string & line){
lmp->input->one("compute sum all reduce sum c_pe"); lmp->input->one(line.c_str());
lmp->input->one("thermo_style custom step temp pe press c_sum"); };
lmp->input->one("thermo 2");
lmp->input->one("run 4 post no"); 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) void restart_lammps(LAMMPS *lmp, const TestConfig &cfg)
{ {
lmp->input->one("clear"); // utility lambda to improve readability
std::string cmd("read_restart "); auto command = [&](const std::string & line){
cmd += cfg.basename + ".restart"; lmp->input->one(line.c_str());
lmp->input->one(cmd.c_str()); };
command("clear");
command("read_restart " + cfg.basename + ".restart");
if (!lmp->force->angle) { if (!lmp->force->angle) {
cmd = "angle_style " + cfg.angle_style; command("angle_style " + cfg.angle_style);
lmp->input->one(cmd.c_str());
} }
if ((cfg.angle_style.substr(0,6) == "hybrid") if ((cfg.angle_style.substr(0,6) == "hybrid")
|| !lmp->force->angle->writedata) { || !lmp->force->angle->writedata) {
for (auto angle_coeff : cfg.angle_coeff) { for (auto& angle_coeff : cfg.angle_coeff) {
cmd = "angle_coeff " + angle_coeff; command("angle_coeff " + angle_coeff);
lmp->input->one(cmd.c_str());
} }
} }
for (auto post_command : cfg.post_commands)
lmp->input->one(post_command.c_str()); for (auto& post_command : cfg.post_commands) {
lmp->input->one("run 0 post no"); command(post_command);
}
command("run 0 post no");
} }
void data_lammps(LAMMPS *lmp, const TestConfig &cfg) void data_lammps(LAMMPS *lmp, const TestConfig &cfg)
{ {
lmp->input->one("clear"); // utility lambdas to improve readability
lmp->input->one("variable angle_style delete"); auto command = [&](const std::string & line){
lmp->input->one("variable data_file delete"); lmp->input->one(line.c_str());
lmp->input->one("variable newton_bond delete"); };
lmp->input->one("variable newton_bond index on"); auto parse_input_script = [&](const std::string & filename){
lmp->input->file(filename.c_str());
};
for (auto pre_command : cfg.pre_commands) command("clear");
lmp->input->one(pre_command.c_str()); 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 '"); for (auto& pre_command : cfg.pre_commands) {
cmd += cfg.angle_style + "'"; command(pre_command);
lmp->input->one(cmd.c_str()); }
cmd = "variable data_file index "; command("variable angle_style index '" + cfg.angle_style + "'");
cmd += cfg.basename + ".data"; command("variable data_file index " + cfg.basename + ".data");
lmp->input->one(cmd.c_str());
std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file; 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) { for (auto& angle_coeff : cfg.angle_coeff) {
cmd = "angle_coeff " + angle_coeff; command("angle_coeff " + angle_coeff);
lmp->input->one(cmd.c_str());
} }
for (auto post_command : cfg.post_commands)
lmp->input->one(post_command.c_str()); for (auto& post_command : cfg.post_commands) {
lmp->input->one("run 0 post no"); command(post_command);
}
command("run 0 post no");
} }
@ -207,7 +228,7 @@ void generate_yaml_file(const char *outfile, const TestConfig &config)
if (!lmp) { if (!lmp) {
std::cerr << "One or more prerequisite styles are not available " std::cerr << "One or more prerequisite styles are not available "
"in this LAMMPS configuration:\n"; "in this LAMMPS configuration:\n";
for (auto prerequisite : config.prerequisites) { for (auto& prerequisite : config.prerequisites) {
std::cerr << prerequisite.first << "_style " std::cerr << prerequisite.first << "_style "
<< prerequisite.second << "\n"; << prerequisite.second << "\n";
} }
@ -235,21 +256,21 @@ void generate_yaml_file(const char *outfile, const TestConfig &config)
// prerequisites // prerequisites
block.clear(); block.clear();
for (auto prerequisite : config.prerequisites) { for (auto& prerequisite : config.prerequisites) {
block += prerequisite.first + " " + prerequisite.second + "\n"; block += prerequisite.first + " " + prerequisite.second + "\n";
} }
writer.emit_block("prerequisites", block); writer.emit_block("prerequisites", block);
// pre_commands // pre_commands
block.clear(); block.clear();
for (auto command : config.pre_commands) { for (auto& command : config.pre_commands) {
block += command + "\n"; block += command + "\n";
} }
writer.emit_block("pre_commands", block); writer.emit_block("pre_commands", block);
// post_commands // post_commands
block.clear(); block.clear();
for (auto command : config.post_commands) { for (auto& command : config.post_commands) {
block += command + "\n"; block += command + "\n";
} }
writer.emit_block("post_commands", block); writer.emit_block("post_commands", block);
@ -262,7 +283,7 @@ void generate_yaml_file(const char *outfile, const TestConfig &config)
// angle_coeff // angle_coeff
block.clear(); block.clear();
for (auto angle_coeff : config.angle_coeff) { for (auto& angle_coeff : config.angle_coeff) {
block += angle_coeff + "\n"; block += angle_coeff + "\n";
} }
writer.emit_block("angle_coeff", block); writer.emit_block("angle_coeff", block);
@ -270,7 +291,7 @@ void generate_yaml_file(const char *outfile, const TestConfig &config)
// extract // extract
block.clear(); block.clear();
std::stringstream outstr; std::stringstream outstr;
for (auto data : config.extract) { for (auto& data : config.extract) {
outstr << data.first << " " << data.second << std::endl; outstr << data.first << " " << data.second << std::endl;
} }
writer.emit_block("extract", outstr.str()); writer.emit_block("extract", outstr.str());
@ -336,7 +357,7 @@ TEST(AngleStyle, plain) {
if (!lmp) { if (!lmp) {
std::cerr << "One or more prerequisite styles are not available " std::cerr << "One or more prerequisite styles are not available "
"in this LAMMPS configuration:\n"; "in this LAMMPS configuration:\n";
for (auto prerequisite : test_config.prerequisites) { for (auto& prerequisite : test_config.prerequisites) {
std::cerr << prerequisite.first << "_style " std::cerr << prerequisite.first << "_style "
<< prerequisite.second << "\n"; << prerequisite.second << "\n";
} }
@ -570,7 +591,7 @@ TEST(AngleStyle, omp) {
if (!lmp) { if (!lmp) {
std::cerr << "One or more prerequisite styles with /omp suffix\n" std::cerr << "One or more prerequisite styles with /omp suffix\n"
"are not available in this LAMMPS configuration:\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 " std::cerr << prerequisite.first << "_style "
<< prerequisite.second << "\n"; << prerequisite.second << "\n";
} }

View File

@ -51,16 +51,15 @@ using ::testing::HasSubstr;
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
static void delete_file(const std::string & filename) {
remove(filename.c_str());
};
void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg) void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg)
{ {
std::string name; delete_file(cfg.basename + ".restart");
delete_file(cfg.basename + ".data");
name = cfg.basename + ".restart"; delete_file(cfg.basename + "-coeffs.in");
remove(name.c_str());
name = cfg.basename + ".data";
remove(name.c_str());
name = cfg.basename + "-coeffs.in";
remove(name.c_str());
delete lmp; delete lmp;
} }
@ -95,104 +94,125 @@ LAMMPS *init_lammps(int argc, char **argv,
return nullptr; 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) { if (newton) {
lmp->input->one("variable newton_bond index on"); command("variable newton_bond index on");
} else { } else {
lmp->input->one("variable newton_bond index off"); command("variable newton_bond index off");
} }
std::string set_input_dir = "variable input_dir index "; command("variable input_dir index " + INPUT_FOLDER);
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());
std::string input_file = INPUT_FOLDER + "/" + cfg.input_file; for (auto& pre_command : cfg.pre_commands) {
command(pre_command);
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 post_command : cfg.post_commands)
lmp->input->one(post_command.c_str()); std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file;
lmp->input->one("run 0 post no"); parse_input_script(input_file);
cmd = "write_restart " + cfg.basename + ".restart";
lmp->input->one(cmd.c_str()); command("bond_style " + cfg.bond_style);
cmd = "write_data " + cfg.basename + ".data";
lmp->input->one(cmd.c_str()); for (auto& bond_coeff : cfg.bond_coeff) {
cmd = "write_coeff " + cfg.basename + "-coeffs.in"; command("bond_coeff " + bond_coeff);
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; return lmp;
} }
void run_lammps(LAMMPS *lmp) void run_lammps(LAMMPS *lmp)
{ {
lmp->input->one("fix 1 all nve"); // utility lambda to improve readability
lmp->input->one("compute pe all pe/atom"); auto command = [&](const std::string & line){
lmp->input->one("compute sum all reduce sum c_pe"); lmp->input->one(line.c_str());
lmp->input->one("thermo_style custom step temp pe press c_sum"); };
lmp->input->one("thermo 2");
lmp->input->one("run 4 post no"); 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) void restart_lammps(LAMMPS *lmp, const TestConfig &cfg)
{ {
lmp->input->one("clear"); // utility lambda to improve readability
std::string cmd("read_restart "); auto command = [&](const std::string & line){
cmd += cfg.basename + ".restart"; lmp->input->one(line.c_str());
lmp->input->one(cmd.c_str()); };
command("clear");
command("read_restart " + cfg.basename + ".restart");
if (!lmp->force->bond) { if (!lmp->force->bond) {
cmd = "bond_style " + cfg.bond_style; command("bond_style " + cfg.bond_style);
lmp->input->one(cmd.c_str());
} }
if ((cfg.bond_style.substr(0,6) == "hybrid") if ((cfg.bond_style.substr(0,6) == "hybrid")
|| !lmp->force->bond->writedata) { || !lmp->force->bond->writedata) {
for (auto bond_coeff : cfg.bond_coeff) { for (auto& bond_coeff : cfg.bond_coeff) {
cmd = "bond_coeff " + bond_coeff; command("bond_coeff " + bond_coeff);
lmp->input->one(cmd.c_str());
} }
} }
for (auto post_command : cfg.post_commands)
lmp->input->one(post_command.c_str()); for (auto& post_command : cfg.post_commands) {
lmp->input->one("run 0 post no"); command(post_command);
}
command("run 0 post no");
} }
void data_lammps(LAMMPS *lmp, const TestConfig &cfg) void data_lammps(LAMMPS *lmp, const TestConfig &cfg)
{ {
lmp->input->one("clear"); // utility lambdas to improve readability
lmp->input->one("variable bond_style delete"); auto command = [&](const std::string & line){
lmp->input->one("variable data_file delete"); lmp->input->one(line.c_str());
lmp->input->one("variable newton_bond delete"); };
lmp->input->one("variable newton_bond index on"); auto parse_input_script = [&](const std::string & filename){
lmp->input->file(filename.c_str());
};
for (auto pre_command : cfg.pre_commands) command("clear");
lmp->input->one(pre_command.c_str()); 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 '"); for (auto& pre_command : cfg.pre_commands) {
cmd += cfg.bond_style + "'"; command(pre_command);
lmp->input->one(cmd.c_str()); }
cmd = "variable data_file index "; command("variable bond_style index '" + cfg.bond_style + "'");
cmd += cfg.basename + ".data"; command("variable data_file index " + cfg.basename + ".data");
lmp->input->one(cmd.c_str());
std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file; 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) { for (auto& bond_coeff : cfg.bond_coeff) {
cmd = "bond_coeff " + bond_coeff; command("bond_coeff " + bond_coeff);
lmp->input->one(cmd.c_str());
} }
for (auto post_command : cfg.post_commands)
lmp->input->one(post_command.c_str()); for (auto& post_command : cfg.post_commands) {
lmp->input->one("run 0 post no"); command(post_command);
}
command("run 0 post no");
} }
// re-generate yaml file with current settings. // re-generate yaml file with current settings.
@ -761,63 +781,68 @@ TEST(BondStyle, single) {
GTEST_SKIP(); GTEST_SKIP();
} }
// utility lambda to improve readability
auto command = [&](const std::string & line){
lmp->input->one(line.c_str());
};
Bond *bond = lmp->force->bond; Bond *bond = lmp->force->bond;
// now start over // now start over
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("clear"); command("clear");
lmp->input->one("variable newton_bond delete"); command("variable newton_bond delete");
lmp->input->one("variable newton_bond index on"); command("variable newton_bond index on");
std::string set_input_dir = "variable input_dir index "; command("variable input_dir index " + INPUT_FOLDER);
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());
lmp->input->one("atom_style molecular"); for (auto& pre_command : test_config.pre_commands) {
lmp->input->one("units ${units}"); command(pre_command);
lmp->input->one("boundary p p p"); }
lmp->input->one("newton ${newton_pair} ${newton_bond}");
lmp->input->one("special_bonds lj/coul " command("atom_style molecular");
"${bond_factor} ${angle_factor} ${dihedral_factor}"); 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]; char buf[10];
snprintf(buf,10,"%d",ntypes);
std::string cmd("create_box 1 box"); std::string cmd("create_box 1 box");
cmd += " bond/types "; cmd += " bond/types ";
snprintf(buf,10,"%d",nbondtypes); snprintf(buf,10,"%d",nbondtypes);
cmd += buf; cmd += buf;
cmd += " extra/bond/per/atom 2"; cmd += " extra/bond/per/atom 2";
cmd += " extra/special/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"); command("pair_style zero 8.0");
lmp->input->one("pair_coeff * *"); command("pair_coeff * *");
cmd = "bond_style "; command("bond_style " + test_config.bond_style);
cmd += test_config.bond_style;
lmp->input->one(cmd.c_str());
bond = lmp->force->bond; bond = lmp->force->bond;
for (auto bond_coeff : test_config.bond_coeff) { for (auto bond_coeff : test_config.bond_coeff) {
cmd = "bond_coeff " + bond_coeff; command("bond_coeff " + bond_coeff);
lmp->input->one(cmd.c_str());
} }
// create (only) four atoms and two bonds // create (only) four atoms and two bonds
lmp->input->one("mass * 1.0"); command("mass * 1.0");
lmp->input->one("create_atoms 1 single 5.0 -0.75 0.4 units box"); command("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"); command("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"); command("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"); command("create_atoms 1 single -5.5 -0.25 -0.1 units box");
lmp->input->one("create_bonds single/bond 1 1 2"); command("create_bonds single/bond 1 1 2");
lmp->input->one("create_bonds single/bond 2 3 4"); command("create_bonds single/bond 2 3 4");
for (auto post_command : test_config.post_commands)
lmp->input->one(post_command.c_str()); for (auto& post_command : test_config.post_commands) {
lmp->input->one("run 0 post no"); command(post_command);
}
command("run 0 post no");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
int idx1 = lmp->atom->map(1); int idx1 = lmp->atom->map(1);
@ -857,8 +882,8 @@ TEST(BondStyle, single) {
EXPECT_FP_LE_WITH_EPS(f[idx4][2], fsingle*delz2, epsilon); EXPECT_FP_LE_WITH_EPS(f[idx4][2], fsingle*delz2, epsilon);
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("displace_atoms all random 0.5 0.5 0.5 23456"); command("displace_atoms all random 0.5 0.5 0.5 23456");
lmp->input->one("run 0 post no"); command("run 0 post no");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
f = lmp->atom->f; f = lmp->atom->f;
@ -895,8 +920,8 @@ TEST(BondStyle, single) {
EXPECT_FP_LE_WITH_EPS(f[idx4][2], fsingle*delz2, epsilon); EXPECT_FP_LE_WITH_EPS(f[idx4][2], fsingle*delz2, epsilon);
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("displace_atoms all random 0.5 0.5 0.5 456963"); command("displace_atoms all random 0.5 0.5 0.5 456963");
lmp->input->one("run 0 post no"); command("run 0 post no");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
f = lmp->atom->f; f = lmp->atom->f;
@ -933,8 +958,8 @@ TEST(BondStyle, single) {
EXPECT_FP_LE_WITH_EPS(f[idx4][2], fsingle*delz2, epsilon); EXPECT_FP_LE_WITH_EPS(f[idx4][2], fsingle*delz2, epsilon);
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("displace_atoms all random 0.5 0.5 0.5 9726532"); command("displace_atoms all random 0.5 0.5 0.5 9726532");
lmp->input->one("run 0 post no"); command("run 0 post no");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
f = lmp->atom->f; f = lmp->atom->f;

View File

@ -51,16 +51,15 @@ using ::testing::HasSubstr;
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
static void delete_file(const std::string & filename) {
remove(filename.c_str());
};
void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg) void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg)
{ {
std::string name; delete_file(cfg.basename + ".restart");
delete_file(cfg.basename + ".data");
name = cfg.basename + ".restart"; delete_file(cfg.basename + "-coeffs.in");
remove(name.c_str());
name = cfg.basename + ".data";
remove(name.c_str());
name = cfg.basename + "-coeffs.in";
remove(name.c_str());
delete lmp; delete lmp;
} }
@ -75,7 +74,7 @@ LAMMPS *init_lammps(int argc, char **argv,
// check if prerequisite styles are available // check if prerequisite styles are available
Info *info = new Info(lmp); Info *info = new Info(lmp);
int nfail = 0; int nfail = 0;
for (auto prerequisite : cfg.prerequisites) { for (auto& prerequisite : cfg.prerequisites) {
std::string style = prerequisite.second; std::string style = prerequisite.second;
// this is a test for pair styles, so if the suffixed // this is a test for pair styles, so if the suffixed
@ -95,103 +94,122 @@ LAMMPS *init_lammps(int argc, char **argv,
return nullptr; 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) { if (newton) {
lmp->input->one("variable newton_pair index on"); command("variable newton_pair index on");
} else { } else {
lmp->input->one("variable newton_pair index off"); command("variable newton_pair index off");
} }
std::string set_input_dir = "variable input_dir index "; command("variable input_dir index " + INPUT_FOLDER);
set_input_dir += INPUT_FOLDER;
lmp->input->one(set_input_dir.c_str()); for (auto& pre_command : cfg.pre_commands) {
for (auto pre_command : cfg.pre_commands) command(pre_command);
lmp->input->one(pre_command.c_str()); }
std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file; 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 "); command("pair_style " + cfg.pair_style);
cmd += cfg.pair_style;
lmp->input->one(cmd.c_str()); for (auto& pair_coeff : cfg.pair_coeff) {
for (auto pair_coeff : cfg.pair_coeff) { command("pair_coeff " + pair_coeff);
cmd = "pair_coeff " + pair_coeff;
lmp->input->one(cmd.c_str());
} }
for (auto post_command : cfg.post_commands)
lmp->input->one(post_command.c_str()); for (auto& post_command : cfg.post_commands) {
lmp->input->one("run 0 post no"); command(post_command);
cmd = "write_restart " + cfg.basename + ".restart"; }
lmp->input->one(cmd.c_str());
cmd = "write_data " + cfg.basename + ".data"; command("run 0 post no");
lmp->input->one(cmd.c_str()); command("write_restart " + cfg.basename + ".restart");
cmd = "write_coeff " + cfg.basename + "-coeffs.in"; command("write_data " + cfg.basename + ".data");
lmp->input->one(cmd.c_str()); command("write_coeff " + cfg.basename + "-coeffs.in");
return lmp; return lmp;
} }
void run_lammps(LAMMPS *lmp) void run_lammps(LAMMPS *lmp)
{ {
lmp->input->one("fix 1 all nve"); // utility lambda to improve readability
lmp->input->one("compute pe all pe/atom"); auto command = [&](const std::string & line){
lmp->input->one("compute sum all reduce sum c_pe"); lmp->input->one(line.c_str());
lmp->input->one("thermo_style custom step temp pe press c_sum"); };
lmp->input->one("thermo 2");
lmp->input->one("run 4 post no"); 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) void restart_lammps(LAMMPS *lmp, const TestConfig &cfg)
{ {
lmp->input->one("clear"); // utility lambda to improve readability
std::string cmd("read_restart "); auto command = [&](const std::string & line){
cmd += cfg.basename + ".restart"; lmp->input->one(line.c_str());
lmp->input->one(cmd.c_str()); };
command("clear");
command("read_restart " + cfg.basename + ".restart");
if (!lmp->force->pair) { if (!lmp->force->pair) {
cmd = "pair_style " + cfg.pair_style; command("pair_style " + cfg.pair_style);
lmp->input->one(cmd.c_str());
} }
if (!lmp->force->pair->restartinfo if (!lmp->force->pair->restartinfo
|| !lmp->force->pair->writedata) { || !lmp->force->pair->writedata) {
for (auto pair_coeff : cfg.pair_coeff) { for (auto& pair_coeff : cfg.pair_coeff) {
cmd = "pair_coeff " + pair_coeff; command("pair_coeff " + pair_coeff);
lmp->input->one(cmd.c_str());
} }
} }
for (auto post_command : cfg.post_commands)
lmp->input->one(post_command.c_str()); for (auto& post_command : cfg.post_commands) {
lmp->input->one("run 0 post no"); command(post_command);
}
command("run 0 post no");
} }
void data_lammps(LAMMPS *lmp, const TestConfig &cfg) void data_lammps(LAMMPS *lmp, const TestConfig &cfg)
{ {
lmp->input->one("clear"); // utility lambdas to improve readability
lmp->input->one("variable pair_style delete"); auto command = [&](const std::string & line){
lmp->input->one("variable data_file delete"); lmp->input->one(line.c_str());
lmp->input->one("variable newton_pair delete"); };
lmp->input->one("variable newton_pair index on"); auto parse_input_script = [&](const std::string & filename){
lmp->input->file(filename.c_str());
};
for (auto pre_command : cfg.pre_commands) command("clear");
lmp->input->one(pre_command.c_str()); 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 '"); for (auto& pre_command : cfg.pre_commands) {
cmd += cfg.pair_style + "'"; command(pre_command);
lmp->input->one(cmd.c_str()); }
cmd = "variable data_file index "; command("variable pair_style index '" + cfg.pair_style + "'");
cmd += cfg.basename + ".data"; command("variable data_file index " + cfg.basename + ".data");
lmp->input->one(cmd.c_str());
std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file; 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) { for (auto& pair_coeff : cfg.pair_coeff) {
cmd = "pair_coeff " + pair_coeff; command("pair_coeff " + pair_coeff);
lmp->input->one(cmd.c_str());
} }
for (auto post_command : cfg.post_commands) for (auto& post_command : cfg.post_commands) {
lmp->input->one(post_command.c_str()); command(post_command);
lmp->input->one("run 0 post no"); }
command("run 0 post no");
} }
// re-generate yaml file with current settings. // re-generate yaml file with current settings.
@ -1011,62 +1029,73 @@ TEST(PairStyle, single) {
// now start over // now start over
if (!verbose) ::testing::internal::CaptureStdout(); 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 "; // utility lambda to improve readability
set_input_dir += INPUT_FOLDER; auto command = [&](const std::string & line){
lmp->input->one(set_input_dir.c_str()); lmp->input->one(line.c_str());
for (auto pre_command : test_config.pre_commands) };
lmp->input->one(pre_command.c_str());
lmp->input->one("atom_style full"); command("clear");
lmp->input->one("units ${units}"); command("variable newton_pair delete");
lmp->input->one("boundary p p p"); command("variable newton_pair index on");
lmp->input->one("newton ${newton_pair} ${newton_bond}");
if (molecular) { command("variable input_dir index " + INPUT_FOLDER);
lmp->input->one("special_bonds lj/coul "
"${bond_factor} ${angle_factor} ${dihedral_factor}"); 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]; char buf[10];
snprintf(buf,10,"%d",ntypes); snprintf(buf,10,"%d",ntypes);
std::string cmd("create_box "); std::string cmd("create_box ");
cmd += buf; cmd += buf;
cmd += " box"; cmd += " box";
if (molecular) { if (molecular) {
cmd += " bond/types 1"; cmd += " bond/types 1"
cmd += " extra/bond/per/atom 1"; " extra/bond/per/atom 1"
cmd += " extra/special/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; pair = lmp->force->pair;
for (auto pair_coeff : test_config.pair_coeff) { for (auto& pair_coeff : test_config.pair_coeff) {
cmd = "pair_coeff " + pair_coeff; command("pair_coeff " + pair_coeff);
lmp->input->one(cmd.c_str());
} }
// create (only) two atoms // create (only) two atoms
lmp->input->one("mass * 1.0"); command("mass * 1.0");
lmp->input->one("create_atoms 1 single 0.0 -0.75 0.4 units box"); command("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"); command("create_atoms 2 single 0.5 0.25 -0.1 units box");
lmp->input->one("set atom 1 charge -0.5"); command("set atom 1 charge -0.5");
lmp->input->one("set atom 2 charge 0.5"); command("set atom 2 charge 0.5");
if (molecular) { if (molecular) {
lmp->input->one("create_bonds single/bond 1 1 2"); command("create_bonds single/bond 1 1 2");
lmp->input->one("bond_style zero"); command("bond_style zero");
lmp->input->one("bond_coeff 1 2.0"); command("bond_coeff 1 2.0");
} }
for (auto post_command : test_config.post_commands)
lmp->input->one(post_command.c_str()); for (auto& post_command : test_config.post_commands) {
lmp->input->one("run 0 post no"); command(post_command);
}
command("run 0 post no");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
int idx1 = lmp->atom->map(1); int idx1 = lmp->atom->map(1);
@ -1094,8 +1123,8 @@ TEST(PairStyle, single) {
EXPECT_FP_LE_WITH_EPS(f[idx2][2], fsingle*delz, epsilon); EXPECT_FP_LE_WITH_EPS(f[idx2][2], fsingle*delz, epsilon);
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("displace_atoms all random 1.0 1.0 1.0 723456"); command("displace_atoms all random 1.0 1.0 1.0 723456");
lmp->input->one("run 0 post no"); command("run 0 post no");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
f = lmp->atom->f; f = lmp->atom->f;
@ -1118,8 +1147,8 @@ TEST(PairStyle, single) {
EXPECT_FP_LE_WITH_EPS(f[idx2][2], fsingle*delz, epsilon); EXPECT_FP_LE_WITH_EPS(f[idx2][2], fsingle*delz, epsilon);
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("displace_atoms all random 1.0 1.0 1.0 3456963"); command("displace_atoms all random 1.0 1.0 1.0 3456963");
lmp->input->one("run 0 post no"); command("run 0 post no");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
f = lmp->atom->f; f = lmp->atom->f;
@ -1142,8 +1171,8 @@ TEST(PairStyle, single) {
EXPECT_FP_LE_WITH_EPS(f[idx2][2], fsingle*delz, epsilon); EXPECT_FP_LE_WITH_EPS(f[idx2][2], fsingle*delz, epsilon);
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
lmp->input->one("displace_atoms all random 0.5 0.5 0.5 9726532"); command("displace_atoms all random 0.5 0.5 0.5 9726532");
lmp->input->one("run 0 post no"); command("run 0 post no");
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
f = lmp->atom->f; f = lmp->atom->f;
@ -1201,8 +1230,8 @@ TEST(PairStyle, extract) {
Pair *pair = lmp->force->pair; Pair *pair = lmp->force->pair;
void *ptr = nullptr; void *ptr = nullptr;
int dim = 0; int dim = 0;
for (auto extract : test_config.extract) { for (auto& extract : test_config.extract) {
ptr = pair->extract(extract.first.c_str(),dim); ptr = pair->extract(extract.first.c_str(), dim);
EXPECT_NE(ptr, nullptr); EXPECT_NE(ptr, nullptr);
EXPECT_EQ(dim, extract.second); EXPECT_EQ(dim, extract.second);
} }
@ -1218,16 +1247,21 @@ TEST(PairStyle, extract) {
pair->cutsq[i][j] = -1.0; 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) { // utility lambda to improve readability
cmd = "pair_coeff " + pair_coeff; auto command = [&](const std::string & line){
lmp->input->one(cmd.c_str()); 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(); pair->init();
for (int i=1; i <= ntypes; ++i) { for (int i=1; i <= ntypes; ++i) {
for (int j=1; j <= ntypes; ++j) { for (int j=1; j <= ntypes; ++j) {
EXPECT_GE(pair->cutsq[i][j],0.0); EXPECT_GE(pair->cutsq[i][j],0.0);