Unverified Commit 76fb7972 authored by Richard Berger's avatar Richard Berger
Browse files

Simplify tests

parent 5533b923
Loading
Loading
Loading
Loading
+104 −83
Original line number Diff line number Diff line
@@ -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());

    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());
    }
    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());
    parse_input_script(input_file);

    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) {
        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";
        }
+141 −116
Original line number Diff line number Diff line
@@ -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;
    for (auto& pre_command : cfg.pre_commands) {
        command(pre_command);
    }

    std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file;
    parse_input_script(input_file);

    lmp->input->file(input_file.c_str());
    command("bond_style " + cfg.bond_style);

    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& 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");
    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->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) {
        command(post_command);
    }
    for (auto post_command : cfg.post_commands)
        lmp->input->one(post_command.c_str());
    lmp->input->one("run 0 post no");

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

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

    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 "
    command("clear");
    command("variable newton_bond delete");
    command("variable newton_bond index on");

    command("variable input_dir index " + INPUT_FOLDER);

    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}");

    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_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 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;
+162 −128

File changed.

Preview size limit exceeded, changes collapsed.