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;
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;