apply clang-format settings to the c++ sources in the unittest tree

This commit is contained in:
Axel Kohlmeyer
2020-06-13 01:54:58 -04:00
parent 4d62ea98cf
commit 3db944decc
18 changed files with 1400 additions and 1429 deletions

View File

@ -13,32 +13,32 @@
// unit tests for angle styles intended for molecular systems // unit tests for angle styles intended for molecular systems
#include "yaml_reader.h"
#include "yaml_writer.h"
#include "error_stats.h" #include "error_stats.h"
#include "test_config.h" #include "test_config.h"
#include "test_config_reader.h" #include "test_config_reader.h"
#include "test_main.h" #include "test_main.h"
#include "yaml_reader.h"
#include "yaml_writer.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h" #include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "lammps.h" #include "angle.h"
#include "atom.h" #include "atom.h"
#include "modify.h"
#include "compute.h" #include "compute.h"
#include "force.h" #include "force.h"
#include "angle.h"
#include "info.h" #include "info.h"
#include "input.h" #include "input.h"
#include "lammps.h"
#include "modify.h"
#include "universe.h" #include "universe.h"
#include <mpi.h>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cctype> #include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime> #include <ctime>
#include <mpi.h>
#include <iostream> #include <iostream>
#include <map> #include <map>
@ -46,12 +46,13 @@
#include <utility> #include <utility>
#include <vector> #include <vector>
using ::testing::StartsWith;
using ::testing::HasSubstr; using ::testing::HasSubstr;
using ::testing::StartsWith;
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
static void delete_file(const std::string & filename) { static void delete_file(const std::string &filename)
{
remove(filename.c_str()); remove(filename.c_str());
}; };
@ -63,9 +64,7 @@ void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg)
delete lmp; delete lmp;
} }
LAMMPS *init_lammps(int argc, char **argv, LAMMPS *init_lammps(int argc, char **argv, const TestConfig &cfg, const bool newton = true)
const TestConfig &cfg,
const bool newton=true)
{ {
LAMMPS *lmp; LAMMPS *lmp;
@ -74,7 +73,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
@ -86,19 +85,19 @@ LAMMPS *init_lammps(int argc, char **argv,
} }
} }
if (!info->has_style(prerequisite.first,style)) ++nfail; if (!info->has_style(prerequisite.first, style)) ++nfail;
} }
if (nfail > 0) { if (nfail > 0) {
delete info; delete info;
cleanup_lammps(lmp,cfg); cleanup_lammps(lmp, cfg);
return nullptr; return nullptr;
} }
// utility lambdas to improve readability // utility lambdas to improve readability
auto command = [&](const std::string & line) { auto command = [&](const std::string &line) {
lmp->input->one(line.c_str()); lmp->input->one(line.c_str());
}; };
auto parse_input_script = [&](const std::string & filename) { auto parse_input_script = [&](const std::string &filename) {
lmp->input->file(filename.c_str()); lmp->input->file(filename.c_str());
}; };
@ -110,7 +109,7 @@ LAMMPS *init_lammps(int argc, char **argv,
command("variable input_dir index " + INPUT_FOLDER); command("variable input_dir index " + INPUT_FOLDER);
for (auto& pre_command : cfg.pre_commands) { for (auto &pre_command : cfg.pre_commands) {
command(pre_command); command(pre_command);
} }
@ -119,11 +118,11 @@ LAMMPS *init_lammps(int argc, char **argv,
command("angle_style " + cfg.angle_style); command("angle_style " + cfg.angle_style);
for (auto& angle_coeff : cfg.angle_coeff) { for (auto &angle_coeff : cfg.angle_coeff) {
command("angle_coeff " + angle_coeff); command("angle_coeff " + angle_coeff);
} }
for (auto& post_command : cfg.post_commands) { for (auto &post_command : cfg.post_commands) {
command(post_command); command(post_command);
} }
@ -138,7 +137,7 @@ LAMMPS *init_lammps(int argc, char **argv,
void run_lammps(LAMMPS *lmp) void run_lammps(LAMMPS *lmp)
{ {
// utility lambda to improve readability // utility lambda to improve readability
auto command = [&](const std::string & line) { auto command = [&](const std::string &line) {
lmp->input->one(line.c_str()); lmp->input->one(line.c_str());
}; };
@ -153,7 +152,7 @@ void run_lammps(LAMMPS *lmp)
void restart_lammps(LAMMPS *lmp, const TestConfig &cfg) void restart_lammps(LAMMPS *lmp, const TestConfig &cfg)
{ {
// utility lambda to improve readability // utility lambda to improve readability
auto command = [&](const std::string & line) { auto command = [&](const std::string &line) {
lmp->input->one(line.c_str()); lmp->input->one(line.c_str());
}; };
@ -164,14 +163,13 @@ void restart_lammps(LAMMPS *lmp, const TestConfig &cfg)
command("angle_style " + cfg.angle_style); command("angle_style " + cfg.angle_style);
} }
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) {
command("angle_coeff " + angle_coeff); command("angle_coeff " + angle_coeff);
} }
} }
for (auto& post_command : cfg.post_commands) { for (auto &post_command : cfg.post_commands) {
command(post_command); command(post_command);
} }
@ -181,10 +179,10 @@ void restart_lammps(LAMMPS *lmp, const TestConfig &cfg)
void data_lammps(LAMMPS *lmp, const TestConfig &cfg) void data_lammps(LAMMPS *lmp, const TestConfig &cfg)
{ {
// utility lambdas to improve readability // utility lambdas to improve readability
auto command = [&](const std::string & line) { auto command = [&](const std::string &line) {
lmp->input->one(line.c_str()); lmp->input->one(line.c_str());
}; };
auto parse_input_script = [&](const std::string & filename) { auto parse_input_script = [&](const std::string &filename) {
lmp->input->file(filename.c_str()); lmp->input->file(filename.c_str());
}; };
@ -194,7 +192,7 @@ void data_lammps(LAMMPS *lmp, const TestConfig &cfg)
command("variable newton_bond delete"); command("variable newton_bond delete");
command("variable newton_bond index on"); command("variable newton_bond index on");
for (auto& pre_command : cfg.pre_commands) { for (auto &pre_command : cfg.pre_commands) {
command(pre_command); command(pre_command);
} }
@ -204,10 +202,10 @@ void data_lammps(LAMMPS *lmp, const TestConfig &cfg)
std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file; std::string input_file = INPUT_FOLDER + PATH_SEP + cfg.input_file;
parse_input_script(input_file); parse_input_script(input_file);
for (auto& angle_coeff : cfg.angle_coeff) { for (auto &angle_coeff : cfg.angle_coeff) {
command("angle_coeff " + angle_coeff); command("angle_coeff " + angle_coeff);
} }
for (auto& post_command : cfg.post_commands) { for (auto &post_command : cfg.post_commands) {
command(post_command); command(post_command);
} }
command("run 0 post no"); command("run 0 post no");
@ -218,16 +216,15 @@ void data_lammps(LAMMPS *lmp, const TestConfig &cfg)
void generate_yaml_file(const char *outfile, const TestConfig &config) void generate_yaml_file(const char *outfile, const TestConfig &config)
{ {
// initialize system geometry // initialize system geometry
const char *args[] = {"AngleStyle", "-log", "none", "-echo", "screen", "-nocite" }; const char *args[] = {"AngleStyle", "-log", "none", "-echo", "screen", "-nocite"};
char **argv = (char **)args; char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *); int argc = sizeof(args) / sizeof(char *);
LAMMPS *lmp = init_lammps(argc,argv,config); LAMMPS *lmp = init_lammps(argc, argv, 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";
} }
return; return;
} }
@ -245,7 +242,7 @@ void generate_yaml_file(const char *outfile, const TestConfig &config)
// date_generated // date_generated
std::time_t now = time(NULL); std::time_t now = time(NULL);
block = ctime(&now); block = ctime(&now);
block = block.substr(0,block.find("\n")-1); block = block.substr(0, block.find("\n") - 1);
writer.emit("date_generated", block); writer.emit("date_generated", block);
// epsilon // epsilon
@ -253,21 +250,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);
@ -280,7 +277,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);
@ -288,15 +285,15 @@ void generate_yaml_file(const char *outfile, const TestConfig &config)
// equilibrium angle // equilibrium angle
std::stringstream eqstr; std::stringstream eqstr;
eqstr << lmp->atom->nangletypes; eqstr << lmp->atom->nangletypes;
for (int i=0; i < lmp->atom->nangletypes; ++i) { for (int i = 0; i < lmp->atom->nangletypes; ++i) {
eqstr << " " << lmp->force->angle->equilibrium_angle(i+1); eqstr << " " << lmp->force->angle->equilibrium_angle(i + 1);
} }
writer.emit("equilibrium", eqstr.str()); writer.emit("equilibrium", eqstr.str());
// 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());
@ -309,17 +306,17 @@ void generate_yaml_file(const char *outfile, const TestConfig &config)
// init_stress // init_stress
double *stress = lmp->force->angle->virial; double *stress = lmp->force->angle->virial;
snprintf(buf,bufsize,"% 23.16e % 23.16e % 23.16e % 23.16e % 23.16e % 23.16e", snprintf(buf, bufsize, "% 23.16e % 23.16e % 23.16e % 23.16e % 23.16e % 23.16e", stress[0],
stress[0],stress[1],stress[2],stress[3],stress[4],stress[5]); stress[1], stress[2], stress[3], stress[4], stress[5]);
writer.emit_block("init_stress", buf); writer.emit_block("init_stress", buf);
// init_forces // init_forces
block.clear(); block.clear();
double **f = lmp->atom->f; double **f = lmp->atom->f;
tagint *tag = lmp->atom->tag; tagint *tag = lmp->atom->tag;
for (int i=0; i < natoms; ++i) { for (int i = 0; i < natoms; ++i) {
snprintf(buf,bufsize,"% 3d % 23.16e % 23.16e % 23.16e\n", snprintf(buf, bufsize, "% 3d % 23.16e % 23.16e % 23.16e\n", (int)tag[i], f[i][0], f[i][1],
(int)tag[i], f[i][0], f[i][1], f[i][2]); f[i][2]);
block += buf; block += buf;
} }
writer.emit_block("init_forces", block); writer.emit_block("init_forces", block);
@ -332,40 +329,40 @@ void generate_yaml_file(const char *outfile, const TestConfig &config)
// run_stress // run_stress
stress = lmp->force->angle->virial; stress = lmp->force->angle->virial;
snprintf(buf,bufsize,"% 23.16e % 23.16e % 23.16e % 23.16e % 23.16e % 23.16e", snprintf(buf, bufsize, "% 23.16e % 23.16e % 23.16e % 23.16e % 23.16e % 23.16e", stress[0],
stress[0],stress[1],stress[2],stress[3],stress[4],stress[5]); stress[1], stress[2], stress[3], stress[4], stress[5]);
writer.emit_block("run_stress", buf); writer.emit_block("run_stress", buf);
block.clear(); block.clear();
f = lmp->atom->f; f = lmp->atom->f;
tag = lmp->atom->tag; tag = lmp->atom->tag;
for (int i=0; i < natoms; ++i) { for (int i = 0; i < natoms; ++i) {
snprintf(buf,bufsize,"% 3d % 23.16e % 23.16e % 23.16e\n", snprintf(buf, bufsize, "% 3d % 23.16e % 23.16e % 23.16e\n", (int)tag[i], f[i][0], f[i][1],
(int)tag[i], f[i][0], f[i][1], f[i][2]); f[i][2]);
block += buf; block += buf;
} }
writer.emit_block("run_forces", block); writer.emit_block("run_forces", block);
cleanup_lammps(lmp,config); cleanup_lammps(lmp, config);
return; return;
} }
TEST(AngleStyle, plain) { TEST(AngleStyle, plain)
const char *args[] = {"AngleStyle", "-log", "none", "-echo", "screen", "-nocite" }; {
const char *args[] = {"AngleStyle", "-log", "none", "-echo", "screen", "-nocite"};
char **argv = (char **)args; char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *); int argc = sizeof(args) / sizeof(char *);
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
LAMMPS *lmp = init_lammps(argc,argv,test_config,true); LAMMPS *lmp = init_lammps(argc, argv, test_config, true);
std::string output = ::testing::internal::GetCapturedStdout(); std::string output = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << output; if (verbose) std::cout << output;
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";
} }
GTEST_SKIP(); GTEST_SKIP();
} }
@ -375,22 +372,21 @@ TEST(AngleStyle, plain) {
// abort if running in parallel and not all atoms are local // abort if running in parallel and not all atoms are local
const int nlocal = lmp->atom->nlocal; const int nlocal = lmp->atom->nlocal;
ASSERT_EQ(lmp->atom->natoms,nlocal); ASSERT_EQ(lmp->atom->natoms, nlocal);
double epsilon = test_config.epsilon; double epsilon = test_config.epsilon;
double **f=lmp->atom->f; double **f = lmp->atom->f;
tagint *tag=lmp->atom->tag; tagint *tag = lmp->atom->tag;
ErrorStats stats; ErrorStats stats;
stats.reset(); stats.reset();
const std::vector<coord_t> &f_ref = test_config.init_forces; const std::vector<coord_t> &f_ref = test_config.init_forces;
ASSERT_EQ(nlocal+1,f_ref.size()); ASSERT_EQ(nlocal + 1, f_ref.size());
for (int i=0; i < nlocal; ++i) { for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f[i][0], f_ref[tag[i]].x, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][0], f_ref[tag[i]].x, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon);
} }
if (print_stats) if (print_stats) std::cerr << "init_forces stats, newton on: " << stats << std::endl;
std::cerr << "init_forces stats, newton on: " << stats << std::endl;
Angle *angle = lmp->force->angle; Angle *angle = lmp->force->angle;
double *stress = angle->virial; double *stress = angle->virial;
@ -401,13 +397,11 @@ TEST(AngleStyle, plain) {
EXPECT_FP_LE_WITH_EPS(stress[3], test_config.init_stress.xy, epsilon); EXPECT_FP_LE_WITH_EPS(stress[3], test_config.init_stress.xy, epsilon);
EXPECT_FP_LE_WITH_EPS(stress[4], test_config.init_stress.xz, epsilon); EXPECT_FP_LE_WITH_EPS(stress[4], test_config.init_stress.xz, epsilon);
EXPECT_FP_LE_WITH_EPS(stress[5], test_config.init_stress.yz, epsilon); EXPECT_FP_LE_WITH_EPS(stress[5], test_config.init_stress.yz, epsilon);
if (print_stats) if (print_stats) std::cerr << "init_stress stats, newton on: " << stats << std::endl;
std::cerr << "init_stress stats, newton on: " << stats << std::endl;
stats.reset(); stats.reset();
EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.init_energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.init_energy, epsilon);
if (print_stats) if (print_stats) std::cerr << "init_energy stats, newton on: " << stats << std::endl;
std::cerr << "init_energy stats, newton on: " << stats << std::endl;
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
run_lammps(lmp); run_lammps(lmp);
@ -416,15 +410,14 @@ TEST(AngleStyle, plain) {
f = lmp->atom->f; f = lmp->atom->f;
stress = angle->virial; stress = angle->virial;
const std::vector<coord_t> &f_run = test_config.run_forces; const std::vector<coord_t> &f_run = test_config.run_forces;
ASSERT_EQ(nlocal+1,f_run.size()); ASSERT_EQ(nlocal + 1, f_run.size());
stats.reset(); stats.reset();
for (int i=0; i < nlocal; ++i) { for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f[i][0], f_run[tag[i]].x, 10*epsilon); EXPECT_FP_LE_WITH_EPS(f[i][0], f_run[tag[i]].x, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][1], f_run[tag[i]].y, 10*epsilon); EXPECT_FP_LE_WITH_EPS(f[i][1], f_run[tag[i]].y, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][2], f_run[tag[i]].z, 10*epsilon); EXPECT_FP_LE_WITH_EPS(f[i][2], f_run[tag[i]].z, 10 * epsilon);
} }
if (print_stats) if (print_stats) std::cerr << "run_forces stats, newton on: " << stats << std::endl;
std::cerr << "run_forces stats, newton on: " << stats << std::endl;
stress = angle->virial; stress = angle->virial;
stats.reset(); stats.reset();
@ -434,52 +427,47 @@ TEST(AngleStyle, plain) {
EXPECT_FP_LE_WITH_EPS(stress[3], test_config.run_stress.xy, epsilon); EXPECT_FP_LE_WITH_EPS(stress[3], test_config.run_stress.xy, epsilon);
EXPECT_FP_LE_WITH_EPS(stress[4], test_config.run_stress.xz, epsilon); EXPECT_FP_LE_WITH_EPS(stress[4], test_config.run_stress.xz, epsilon);
EXPECT_FP_LE_WITH_EPS(stress[5], test_config.run_stress.yz, epsilon); EXPECT_FP_LE_WITH_EPS(stress[5], test_config.run_stress.yz, epsilon);
if (print_stats) if (print_stats) std::cerr << "run_stress stats, newton on: " << stats << std::endl;
std::cerr << "run_stress stats, newton on: " << stats << std::endl;
stats.reset(); stats.reset();
int id = lmp->modify->find_compute("sum"); int id = lmp->modify->find_compute("sum");
double energy = lmp->modify->compute[id]->compute_scalar(); double energy = lmp->modify->compute[id]->compute_scalar();
EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.run_energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.run_energy, epsilon);
EXPECT_FP_LE_WITH_EPS(angle->energy, energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, energy, epsilon);
if (print_stats) if (print_stats) std::cerr << "run_energy stats, newton on: " << stats << std::endl;
std::cerr << "run_energy stats, newton on: " << stats << std::endl;
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
cleanup_lammps(lmp,test_config); cleanup_lammps(lmp, test_config);
lmp = init_lammps(argc,argv,test_config,false); lmp = init_lammps(argc, argv, test_config, false);
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
// skip over these tests if newton bond is forced to be on // skip over these tests if newton bond is forced to be on
if (lmp->force->newton_bond == 0) { if (lmp->force->newton_bond == 0) {
f=lmp->atom->f; f = lmp->atom->f;
tag=lmp->atom->tag; tag = lmp->atom->tag;
stats.reset(); stats.reset();
for (int i=0; i < nlocal; ++i) { for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f[i][0], f_ref[tag[i]].x, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][0], f_ref[tag[i]].x, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon);
} }
if (print_stats) if (print_stats) std::cerr << "init_forces stats, newton off:" << stats << std::endl;
std::cerr << "init_forces stats, newton off:" << stats << std::endl;
angle = lmp->force->angle; angle = lmp->force->angle;
stress = angle->virial; stress = angle->virial;
stats.reset(); stats.reset();
EXPECT_FP_LE_WITH_EPS(stress[0], test_config.init_stress.xx, 2*epsilon); EXPECT_FP_LE_WITH_EPS(stress[0], test_config.init_stress.xx, 2 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[1], test_config.init_stress.yy, 2*epsilon); EXPECT_FP_LE_WITH_EPS(stress[1], test_config.init_stress.yy, 2 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[2], test_config.init_stress.zz, 2*epsilon); EXPECT_FP_LE_WITH_EPS(stress[2], test_config.init_stress.zz, 2 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[3], test_config.init_stress.xy, 2*epsilon); EXPECT_FP_LE_WITH_EPS(stress[3], test_config.init_stress.xy, 2 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[4], test_config.init_stress.xz, 2*epsilon); EXPECT_FP_LE_WITH_EPS(stress[4], test_config.init_stress.xz, 2 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[5], test_config.init_stress.yz, 2*epsilon); EXPECT_FP_LE_WITH_EPS(stress[5], test_config.init_stress.yz, 2 * epsilon);
if (print_stats) if (print_stats) std::cerr << "init_stress stats, newton off:" << stats << std::endl;
std::cerr << "init_stress stats, newton off:" << stats << std::endl;
stats.reset(); stats.reset();
EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.init_energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.init_energy, epsilon);
if (print_stats) if (print_stats) std::cerr << "init_energy stats, newton off:" << stats << std::endl;
std::cerr << "init_energy stats, newton off:" << stats << std::endl;
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
run_lammps(lmp); run_lammps(lmp);
@ -488,13 +476,12 @@ TEST(AngleStyle, plain) {
f = lmp->atom->f; f = lmp->atom->f;
stress = angle->virial; stress = angle->virial;
stats.reset(); stats.reset();
for (int i=0; i < nlocal; ++i) { for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f[i][0], f_run[tag[i]].x, 10*epsilon); EXPECT_FP_LE_WITH_EPS(f[i][0], f_run[tag[i]].x, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][1], f_run[tag[i]].y, 10*epsilon); EXPECT_FP_LE_WITH_EPS(f[i][1], f_run[tag[i]].y, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][2], f_run[tag[i]].z, 10*epsilon); EXPECT_FP_LE_WITH_EPS(f[i][2], f_run[tag[i]].z, 10 * epsilon);
} }
if (print_stats) if (print_stats) std::cerr << "run_forces stats, newton off:" << stats << std::endl;
std::cerr << "run_forces stats, newton off:" << stats << std::endl;
stress = angle->virial; stress = angle->virial;
stats.reset(); stats.reset();
@ -504,33 +491,30 @@ TEST(AngleStyle, plain) {
EXPECT_FP_LE_WITH_EPS(stress[3], test_config.run_stress.xy, epsilon); EXPECT_FP_LE_WITH_EPS(stress[3], test_config.run_stress.xy, epsilon);
EXPECT_FP_LE_WITH_EPS(stress[4], test_config.run_stress.xz, epsilon); EXPECT_FP_LE_WITH_EPS(stress[4], test_config.run_stress.xz, epsilon);
EXPECT_FP_LE_WITH_EPS(stress[5], test_config.run_stress.yz, epsilon); EXPECT_FP_LE_WITH_EPS(stress[5], test_config.run_stress.yz, epsilon);
if (print_stats) if (print_stats) std::cerr << "run_stress stats, newton off:" << stats << std::endl;
std::cerr << "run_stress stats, newton off:" << stats << std::endl;
stats.reset(); stats.reset();
id = lmp->modify->find_compute("sum"); id = lmp->modify->find_compute("sum");
energy = lmp->modify->compute[id]->compute_scalar(); energy = lmp->modify->compute[id]->compute_scalar();
EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.run_energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.run_energy, epsilon);
EXPECT_FP_LE_WITH_EPS(angle->energy, energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, energy, epsilon);
if (print_stats) if (print_stats) std::cerr << "run_energy stats, newton off:" << stats << std::endl;
std::cerr << "run_energy stats, newton off:" << stats << std::endl;
} }
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
restart_lammps(lmp, test_config); restart_lammps(lmp, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
f=lmp->atom->f; f = lmp->atom->f;
tag=lmp->atom->tag; tag = lmp->atom->tag;
stats.reset(); stats.reset();
ASSERT_EQ(nlocal+1,f_ref.size()); ASSERT_EQ(nlocal + 1, f_ref.size());
for (int i=0; i < nlocal; ++i) { for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f[i][0], f_ref[tag[i]].x, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][0], f_ref[tag[i]].x, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon);
} }
if (print_stats) if (print_stats) std::cerr << "restart_forces stats:" << stats << std::endl;
std::cerr << "restart_forces stats:" << stats << std::endl;
angle = lmp->force->angle; angle = lmp->force->angle;
stress = angle->virial; stress = angle->virial;
@ -541,29 +525,26 @@ TEST(AngleStyle, plain) {
EXPECT_FP_LE_WITH_EPS(stress[3], test_config.init_stress.xy, epsilon); EXPECT_FP_LE_WITH_EPS(stress[3], test_config.init_stress.xy, epsilon);
EXPECT_FP_LE_WITH_EPS(stress[4], test_config.init_stress.xz, epsilon); EXPECT_FP_LE_WITH_EPS(stress[4], test_config.init_stress.xz, epsilon);
EXPECT_FP_LE_WITH_EPS(stress[5], test_config.init_stress.yz, epsilon); EXPECT_FP_LE_WITH_EPS(stress[5], test_config.init_stress.yz, epsilon);
if (print_stats) if (print_stats) std::cerr << "restart_stress stats:" << stats << std::endl;
std::cerr << "restart_stress stats:" << stats << std::endl;
stats.reset(); stats.reset();
EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.init_energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.init_energy, epsilon);
if (print_stats) if (print_stats) std::cerr << "restart_energy stats:" << stats << std::endl;
std::cerr << "restart_energy stats:" << stats << std::endl;
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
data_lammps(lmp, test_config); data_lammps(lmp, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
f=lmp->atom->f; f = lmp->atom->f;
tag=lmp->atom->tag; tag = lmp->atom->tag;
stats.reset(); stats.reset();
ASSERT_EQ(nlocal+1,f_ref.size()); ASSERT_EQ(nlocal + 1, f_ref.size());
for (int i=0; i < nlocal; ++i) { for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f[i][0], f_ref[tag[i]].x, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][0], f_ref[tag[i]].x, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon);
} }
if (print_stats) if (print_stats) std::cerr << "data_forces stats:" << stats << std::endl;
std::cerr << "data_forces stats:" << stats << std::endl;
angle = lmp->force->angle; angle = lmp->force->angle;
stress = angle->virial; stress = angle->virial;
@ -574,37 +555,35 @@ TEST(AngleStyle, plain) {
EXPECT_FP_LE_WITH_EPS(stress[3], test_config.init_stress.xy, epsilon); EXPECT_FP_LE_WITH_EPS(stress[3], test_config.init_stress.xy, epsilon);
EXPECT_FP_LE_WITH_EPS(stress[4], test_config.init_stress.xz, epsilon); EXPECT_FP_LE_WITH_EPS(stress[4], test_config.init_stress.xz, epsilon);
EXPECT_FP_LE_WITH_EPS(stress[5], test_config.init_stress.yz, epsilon); EXPECT_FP_LE_WITH_EPS(stress[5], test_config.init_stress.yz, epsilon);
if (print_stats) if (print_stats) std::cerr << "data_stress stats:" << stats << std::endl;
std::cerr << "data_stress stats:" << stats << std::endl;
stats.reset(); stats.reset();
EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.init_energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.init_energy, epsilon);
if (print_stats) if (print_stats) std::cerr << "data_energy stats:" << stats << std::endl;
std::cerr << "data_energy stats:" << stats << std::endl;
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
cleanup_lammps(lmp,test_config); cleanup_lammps(lmp, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
}; };
TEST(AngleStyle, omp) { TEST(AngleStyle, omp)
{
if (!LAMMPS::is_installed_pkg("USER-OMP")) GTEST_SKIP(); if (!LAMMPS::is_installed_pkg("USER-OMP")) GTEST_SKIP();
const char *args[] = {"AngleStyle", "-log", "none", "-echo", "screen", const char *args[] = {"AngleStyle", "-log", "none", "-echo", "screen", "-nocite",
"-nocite", "-pk", "omp", "4", "-sf", "omp"}; "-pk", "omp", "4", "-sf", "omp"};
char **argv = (char **)args; char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *); int argc = sizeof(args) / sizeof(char *);
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
LAMMPS *lmp = init_lammps(argc,argv,test_config,true); LAMMPS *lmp = init_lammps(argc, argv, test_config, true);
std::string output = ::testing::internal::GetCapturedStdout(); std::string output = ::testing::internal::GetCapturedStdout();
if (verbose) std::cout << output; if (verbose) std::cout << output;
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";
} }
GTEST_SKIP(); GTEST_SKIP();
} }
@ -614,40 +593,37 @@ TEST(AngleStyle, omp) {
// abort if running in parallel and not all atoms are local // abort if running in parallel and not all atoms are local
const int nlocal = lmp->atom->nlocal; const int nlocal = lmp->atom->nlocal;
ASSERT_EQ(lmp->atom->natoms,nlocal); ASSERT_EQ(lmp->atom->natoms, nlocal);
// relax error a bit for USER-OMP package // relax error a bit for USER-OMP package
double epsilon = 5.0*test_config.epsilon; double epsilon = 5.0 * test_config.epsilon;
double **f=lmp->atom->f; double **f = lmp->atom->f;
tagint *tag=lmp->atom->tag; tagint *tag = lmp->atom->tag;
const std::vector<coord_t> &f_ref = test_config.init_forces; const std::vector<coord_t> &f_ref = test_config.init_forces;
ErrorStats stats; ErrorStats stats;
stats.reset(); stats.reset();
for (int i=0; i < nlocal; ++i) { for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f[i][0], f_ref[tag[i]].x, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][0], f_ref[tag[i]].x, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon);
} }
if (print_stats) if (print_stats) std::cerr << "init_forces stats, newton on: " << stats << std::endl;
std::cerr << "init_forces stats, newton on: " << stats << std::endl;
Angle *angle = lmp->force->angle; Angle *angle = lmp->force->angle;
double *stress = angle->virial; double *stress = angle->virial;
stats.reset(); stats.reset();
EXPECT_FP_LE_WITH_EPS(stress[0], test_config.init_stress.xx, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[0], test_config.init_stress.xx, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[1], test_config.init_stress.yy, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[1], test_config.init_stress.yy, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[2], test_config.init_stress.zz, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[2], test_config.init_stress.zz, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[3], test_config.init_stress.xy, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[3], test_config.init_stress.xy, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[4], test_config.init_stress.xz, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[4], test_config.init_stress.xz, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[5], test_config.init_stress.yz, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[5], test_config.init_stress.yz, 10 * epsilon);
if (print_stats) if (print_stats) std::cerr << "init_stress stats, newton on: " << stats << std::endl;
std::cerr << "init_stress stats, newton on: " << stats << std::endl;
stats.reset(); stats.reset();
EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.init_energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.init_energy, epsilon);
if (print_stats) if (print_stats) std::cerr << "init_energy stats, newton on: " << stats << std::endl;
std::cerr << "init_energy stats, newton on: " << stats << std::endl;
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
run_lammps(lmp); run_lammps(lmp);
@ -656,26 +632,24 @@ TEST(AngleStyle, omp) {
f = lmp->atom->f; f = lmp->atom->f;
stress = angle->virial; stress = angle->virial;
const std::vector<coord_t> &f_run = test_config.run_forces; const std::vector<coord_t> &f_run = test_config.run_forces;
ASSERT_EQ(nlocal+1,f_run.size()); ASSERT_EQ(nlocal + 1, f_run.size());
stats.reset(); stats.reset();
for (int i=0; i < nlocal; ++i) { for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f[i][0], f_run[tag[i]].x, 10*epsilon); EXPECT_FP_LE_WITH_EPS(f[i][0], f_run[tag[i]].x, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][1], f_run[tag[i]].y, 10*epsilon); EXPECT_FP_LE_WITH_EPS(f[i][1], f_run[tag[i]].y, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][2], f_run[tag[i]].z, 10*epsilon); EXPECT_FP_LE_WITH_EPS(f[i][2], f_run[tag[i]].z, 10 * epsilon);
} }
if (print_stats) if (print_stats) std::cerr << "run_forces stats, newton on: " << stats << std::endl;
std::cerr << "run_forces stats, newton on: " << stats << std::endl;
stress = angle->virial; stress = angle->virial;
stats.reset(); stats.reset();
EXPECT_FP_LE_WITH_EPS(stress[0], test_config.run_stress.xx, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[0], test_config.run_stress.xx, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[1], test_config.run_stress.yy, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[1], test_config.run_stress.yy, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[2], test_config.run_stress.zz, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[2], test_config.run_stress.zz, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[3], test_config.run_stress.xy, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[3], test_config.run_stress.xy, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[4], test_config.run_stress.xz, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[4], test_config.run_stress.xz, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[5], test_config.run_stress.yz, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[5], test_config.run_stress.yz, 10 * epsilon);
if (print_stats) if (print_stats) std::cerr << "run_stress stats, newton on: " << stats << std::endl;
std::cerr << "run_stress stats, newton on: " << stats << std::endl;
stats.reset(); stats.reset();
int id = lmp->modify->find_compute("sum"); int id = lmp->modify->find_compute("sum");
@ -683,46 +657,42 @@ TEST(AngleStyle, omp) {
EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.run_energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.run_energy, epsilon);
// TODO: this is currently broken for USER-OMP with angle style hybrid // TODO: this is currently broken for USER-OMP with angle style hybrid
// needs to be fixed in the main code somewhere. Not sure where, though. // needs to be fixed in the main code somewhere. Not sure where, though.
if (test_config.angle_style.substr(0,6) != "hybrid") if (test_config.angle_style.substr(0, 6) != "hybrid")
EXPECT_FP_LE_WITH_EPS(angle->energy, energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, energy, epsilon);
if (print_stats) if (print_stats) std::cerr << "run_energy stats, newton on: " << stats << std::endl;
std::cerr << "run_energy stats, newton on: " << stats << std::endl;
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
cleanup_lammps(lmp,test_config); cleanup_lammps(lmp, test_config);
lmp = init_lammps(argc,argv,test_config,false); lmp = init_lammps(argc, argv, test_config, false);
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
// skip over these tests if newton bond is forced to be on // skip over these tests if newton bond is forced to be on
if (lmp->force->newton_bond == 0) { if (lmp->force->newton_bond == 0) {
f=lmp->atom->f; f = lmp->atom->f;
tag=lmp->atom->tag; tag = lmp->atom->tag;
stats.reset(); stats.reset();
for (int i=0; i < nlocal; ++i) { for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f[i][0], f_ref[tag[i]].x, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][0], f_ref[tag[i]].x, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][1], f_ref[tag[i]].y, epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon); EXPECT_FP_LE_WITH_EPS(f[i][2], f_ref[tag[i]].z, epsilon);
} }
if (print_stats) if (print_stats) std::cerr << "init_forces stats, newton off:" << stats << std::endl;
std::cerr << "init_forces stats, newton off:" << stats << std::endl;
angle = lmp->force->angle; angle = lmp->force->angle;
stress = angle->virial; stress = angle->virial;
stats.reset(); stats.reset();
EXPECT_FP_LE_WITH_EPS(stress[0], test_config.init_stress.xx, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[0], test_config.init_stress.xx, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[1], test_config.init_stress.yy, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[1], test_config.init_stress.yy, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[2], test_config.init_stress.zz, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[2], test_config.init_stress.zz, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[3], test_config.init_stress.xy, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[3], test_config.init_stress.xy, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[4], test_config.init_stress.xz, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[4], test_config.init_stress.xz, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[5], test_config.init_stress.yz, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[5], test_config.init_stress.yz, 10 * epsilon);
if (print_stats) if (print_stats) std::cerr << "init_stress stats, newton off:" << stats << std::endl;
std::cerr << "init_stress stats, newton off:" << stats << std::endl;
stats.reset(); stats.reset();
EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.init_energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.init_energy, epsilon);
if (print_stats) if (print_stats) std::cerr << "init_energy stats, newton off:" << stats << std::endl;
std::cerr << "init_energy stats, newton off:" << stats << std::endl;
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
run_lammps(lmp); run_lammps(lmp);
@ -730,24 +700,22 @@ TEST(AngleStyle, omp) {
f = lmp->atom->f; f = lmp->atom->f;
stats.reset(); stats.reset();
for (int i=0; i < nlocal; ++i) { for (int i = 0; i < nlocal; ++i) {
EXPECT_FP_LE_WITH_EPS(f[i][0], f_run[tag[i]].x, 10*epsilon); EXPECT_FP_LE_WITH_EPS(f[i][0], f_run[tag[i]].x, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][1], f_run[tag[i]].y, 10*epsilon); EXPECT_FP_LE_WITH_EPS(f[i][1], f_run[tag[i]].y, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(f[i][2], f_run[tag[i]].z, 10*epsilon); EXPECT_FP_LE_WITH_EPS(f[i][2], f_run[tag[i]].z, 10 * epsilon);
} }
if (print_stats) if (print_stats) std::cerr << "run_forces stats, newton off:" << stats << std::endl;
std::cerr << "run_forces stats, newton off:" << stats << std::endl;
stress = angle->virial; stress = angle->virial;
stats.reset(); stats.reset();
EXPECT_FP_LE_WITH_EPS(stress[0], test_config.run_stress.xx, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[0], test_config.run_stress.xx, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[1], test_config.run_stress.yy, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[1], test_config.run_stress.yy, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[2], test_config.run_stress.zz, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[2], test_config.run_stress.zz, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[3], test_config.run_stress.xy, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[3], test_config.run_stress.xy, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[4], test_config.run_stress.xz, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[4], test_config.run_stress.xz, 10 * epsilon);
EXPECT_FP_LE_WITH_EPS(stress[5], test_config.run_stress.yz, 10*epsilon); EXPECT_FP_LE_WITH_EPS(stress[5], test_config.run_stress.yz, 10 * epsilon);
if (print_stats) if (print_stats) std::cerr << "run_stress stats, newton off:" << stats << std::endl;
std::cerr << "run_stress stats, newton off:" << stats << std::endl;
stats.reset(); stats.reset();
id = lmp->modify->find_compute("sum"); id = lmp->modify->find_compute("sum");
@ -755,33 +723,32 @@ TEST(AngleStyle, omp) {
EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.run_energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, test_config.run_energy, epsilon);
// TODO: this is currently broken for USER-OMP with angle style hybrid // TODO: this is currently broken for USER-OMP with angle style hybrid
// needs to be fixed in the main code somewhere. Not sure where, though. // needs to be fixed in the main code somewhere. Not sure where, though.
if (test_config.angle_style.substr(0,6) != "hybrid") if (test_config.angle_style.substr(0, 6) != "hybrid")
EXPECT_FP_LE_WITH_EPS(angle->energy, energy, epsilon); EXPECT_FP_LE_WITH_EPS(angle->energy, energy, epsilon);
if (print_stats) if (print_stats) std::cerr << "run_energy stats, newton off:" << stats << std::endl;
std::cerr << "run_energy stats, newton off:" << stats << std::endl;
} }
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
cleanup_lammps(lmp,test_config); cleanup_lammps(lmp, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
}; };
TEST(AngleStyle, single) { TEST(AngleStyle, single)
const char *args[] = {"AngleStyle", "-log", "none", "-echo", "screen", "-nocite" }; {
const char *args[] = {"AngleStyle", "-log", "none", "-echo", "screen", "-nocite"};
char **argv = (char **)args; char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *); int argc = sizeof(args) / sizeof(char *);
// create a LAMMPS instance with standard settings to detect the number of atom types // create a LAMMPS instance with standard settings to detect the number of atom types
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
LAMMPS *lmp = init_lammps(argc,argv,test_config); LAMMPS *lmp = init_lammps(argc, argv, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
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";
} }
GTEST_SKIP(); GTEST_SKIP();
} }
@ -792,13 +759,13 @@ TEST(AngleStyle, single) {
if (molecular != 1) { if (molecular != 1) {
std::cerr << "Only simple molecular atom styles are supported\n"; std::cerr << "Only simple molecular atom styles are supported\n";
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
cleanup_lammps(lmp,test_config); cleanup_lammps(lmp, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
GTEST_SKIP(); GTEST_SKIP();
} }
// utility lambda to improve readability // utility lambda to improve readability
auto command = [&](const std::string & line) { auto command = [&](const std::string &line) {
lmp->input->one(line.c_str()); lmp->input->one(line.c_str());
}; };
@ -810,7 +777,7 @@ TEST(AngleStyle, single) {
command("variable input_dir index " + INPUT_FOLDER); command("variable input_dir index " + INPUT_FOLDER);
for (auto& pre_command : test_config.pre_commands) { for (auto &pre_command : test_config.pre_commands) {
command(pre_command); command(pre_command);
} }
@ -827,7 +794,7 @@ TEST(AngleStyle, single) {
char buf[10]; char buf[10];
std::string cmd("create_box 1 box"); std::string cmd("create_box 1 box");
cmd += " angle/types "; cmd += " angle/types ";
snprintf(buf,10,"%d",nangletypes); snprintf(buf, 10, "%d", nangletypes);
cmd += buf; cmd += buf;
cmd += " extra/angle/per/atom 2"; cmd += " extra/angle/per/atom 2";
cmd += " extra/special/per/atom 2"; cmd += " extra/special/per/atom 2";
@ -839,7 +806,7 @@ TEST(AngleStyle, single) {
command("angle_style " + test_config.angle_style); command("angle_style " + test_config.angle_style);
Angle *angle = lmp->force->angle; Angle *angle = lmp->force->angle;
for (auto& angle_coeff : test_config.angle_coeff) { for (auto &angle_coeff : test_config.angle_coeff) {
command("angle_coeff " + angle_coeff); command("angle_coeff " + angle_coeff);
} }
@ -850,7 +817,7 @@ TEST(AngleStyle, single) {
command("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");
command("create_bonds single/angle 1 1 2 3"); command("create_bonds single/angle 1 1 2 3");
for (auto& post_command : test_config.post_commands) { for (auto &post_command : test_config.post_commands) {
command(post_command); command(post_command);
} }
@ -904,14 +871,13 @@ TEST(AngleStyle, single) {
EXPECT_FP_LE_WITH_EPS(eangle[1], esingle[1], epsilon); EXPECT_FP_LE_WITH_EPS(eangle[1], esingle[1], epsilon);
EXPECT_FP_LE_WITH_EPS(eangle[2], esingle[2], epsilon); EXPECT_FP_LE_WITH_EPS(eangle[2], esingle[2], epsilon);
EXPECT_FP_LE_WITH_EPS(eangle[3], esingle[3], epsilon); EXPECT_FP_LE_WITH_EPS(eangle[3], esingle[3], epsilon);
if (print_stats) if (print_stats) std::cerr << "single_energy stats:" << stats << std::endl;
std::cerr << "single_energy stats:" << stats << std::endl;
int i = 0; int i = 0;
for (auto &dist : test_config.equilibrium) for (auto &dist : test_config.equilibrium)
EXPECT_NEAR(dist,angle->equilibrium_angle(++i),0.00001); EXPECT_NEAR(dist, angle->equilibrium_angle(++i), 0.00001);
if (!verbose) ::testing::internal::CaptureStdout(); if (!verbose) ::testing::internal::CaptureStdout();
cleanup_lammps(lmp,test_config); cleanup_lammps(lmp, test_config);
if (!verbose) ::testing::internal::GetCapturedStdout(); if (!verbose) ::testing::internal::GetCapturedStdout();
} }

File diff suppressed because it is too large Load Diff

View File

@ -12,32 +12,36 @@
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#include "error_stats.h" #include "error_stats.h"
#include <cmath>
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <cmath>
void ErrorStats::reset() { void ErrorStats::reset()
{
num = 0; num = 0;
maxidx = -1; maxidx = -1;
sum = sumsq = maxerr =0.0; sum = sumsq = maxerr = 0.0;
} }
void ErrorStats::add(const double &val) { void ErrorStats::add(const double &val)
{
++num; ++num;
if (val > maxerr) { if (val > maxerr) {
maxidx = num; maxidx = num;
maxerr = val; maxerr = val;
} }
sum += val; sum += val;
sumsq += val*val; sumsq += val * val;
} }
double ErrorStats::avg() const { double ErrorStats::avg() const
return (num > 0) ? sum/num : 0.0; {
return (num > 0) ? sum / num : 0.0;
} }
double ErrorStats::dev() const { double ErrorStats::dev() const
return (num > 0) ? sqrt(sumsq/num - sum/num*sum/num) : 0.0; {
return (num > 0) ? sqrt(sumsq / num - sum / num * sum / num) : 0.0;
} }
std::ostream &operator<<(std::ostream &out, const ErrorStats &stats) std::ostream &operator<<(std::ostream &out, const ErrorStats &stats)
@ -46,9 +50,7 @@ std::ostream &operator<<(std::ostream &out, const ErrorStats &stats)
const std::streamsize width = out.width(10); const std::streamsize width = out.width(10);
const std::streamsize prec = out.precision(3); const std::streamsize prec = out.precision(3);
out << std::scientific out << std::scientific << "Average: " << stats.avg() << " StdDev: " << stats.dev()
<< "Average: " << stats.avg()
<< " StdDev: " << stats.dev()
<< " MaxErr: " << stats.max(); << " MaxErr: " << stats.max();
out.precision(prec); out.precision(prec);
@ -57,4 +59,3 @@ std::ostream &operator<<(std::ostream &out, const ErrorStats &stats)
return out << " @ item: " << stats.idx(); return out << " @ item: " << stats.idx();
} }

View File

@ -20,9 +20,7 @@ class ErrorStats {
public: public:
friend std::ostream &operator<<(std::ostream &out, const ErrorStats &stats); friend std::ostream &operator<<(std::ostream &out, const ErrorStats &stats);
ErrorStats() { ErrorStats() { reset(); }
reset();
}
virtual ~ErrorStats() {} virtual ~ErrorStats() {}
void reset(); void reset();
@ -33,8 +31,8 @@ public:
double idx() const { return maxidx; } double idx() const { return maxidx; }
private: private:
double sum,sumsq,maxerr; double sum, sumsq, maxerr;
int num,maxidx; int num, maxidx;
}; };
extern std::ostream &operator<<(std::ostream &out, const ErrorStats &stats); extern std::ostream &operator<<(std::ostream &out, const ErrorStats &stats);

File diff suppressed because it is too large Load Diff

View File

@ -19,11 +19,11 @@
#include <vector> #include <vector>
struct coord_t { struct coord_t {
double x,y,z; double x, y, z;
}; };
struct stress_t { struct stress_t {
double xx,yy,zz,xy,xz,yz; double xx, yy, zz, xy, xz, yz;
}; };
class TestConfig { class TestConfig {
@ -32,7 +32,7 @@ public:
std::string date_generated; std::string date_generated;
std::string basename; std::string basename;
double epsilon; double epsilon;
std::vector<std::pair<std::string,std::string>> prerequisites; std::vector<std::pair<std::string, std::string>> prerequisites;
std::vector<std::string> pre_commands; std::vector<std::string> pre_commands;
std::vector<std::string> post_commands; std::vector<std::string> post_commands;
std::string input_file; std::string input_file;
@ -48,7 +48,7 @@ public:
std::vector<std::string> dihedral_coeff; std::vector<std::string> dihedral_coeff;
std::vector<std::string> improper_coeff; std::vector<std::string> improper_coeff;
std::vector<double> equilibrium; std::vector<double> equilibrium;
std::vector<std::pair<std::string,int>> extract; std::vector<std::pair<std::string, int>> extract;
int natoms; int natoms;
double init_energy; double init_energy;
double run_energy; double run_energy;
@ -61,26 +61,13 @@ public:
std::vector<coord_t> init_forces; std::vector<coord_t> init_forces;
std::vector<coord_t> run_forces; std::vector<coord_t> run_forces;
TestConfig() : lammps_version(""), TestConfig() :
date_generated(""), lammps_version(""), date_generated(""), basename(""), epsilon(1.0e-14), input_file(""),
basename(""), pair_style("zero"), bond_style("zero"), angle_style("zero"), dihedral_style("zero"),
epsilon(1.0e-14), improper_style("zero"), kspace_style("none"), natoms(0), init_energy(0), run_energy(0),
input_file(""), init_vdwl(0), run_vdwl(0), init_coul(0), run_coul(0), init_stress({0, 0, 0, 0, 0, 0}),
pair_style("zero"), run_stress({0, 0, 0, 0, 0, 0})
bond_style("zero"), {
angle_style("zero"),
dihedral_style("zero"),
improper_style("zero"),
kspace_style("none"),
natoms(0),
init_energy(0),
run_energy(0),
init_vdwl(0),
run_vdwl(0),
init_coul(0),
run_coul(0),
init_stress({0,0,0,0,0,0}),
run_stress({0,0,0,0,0,0}) {
prerequisites.clear(); prerequisites.clear();
pre_commands.clear(); pre_commands.clear();
post_commands.clear(); post_commands.clear();
@ -93,10 +80,10 @@ public:
init_forces.clear(); init_forces.clear();
run_forces.clear(); run_forces.clear();
} }
virtual ~TestConfig() {}; virtual ~TestConfig(){};
private: private:
TestConfig(const TestConfig &) {}; TestConfig(const TestConfig &){};
}; };
#endif #endif

View File

@ -11,22 +11,22 @@
See the README file in the top-level LAMMPS directory. See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#include "test_config.h"
#include "test_config_reader.h" #include "test_config_reader.h"
#include "yaml_reader.h" #include "test_config.h"
#include "yaml.h" #include "yaml.h"
#include "yaml_reader.h"
#include <cstring>
#include <cstdlib> #include <cstdlib>
#include <cstring>
#include <string>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string>
#include <utility> #include <utility>
#include <vector> #include <vector>
TestConfigReader::TestConfigReader(TestConfig & config) TestConfigReader::TestConfigReader(TestConfig &config) : YamlReader(), config(config)
: YamlReader(), config(config) { {
consumers["lammps_version"] = &TestConfigReader::lammps_version; consumers["lammps_version"] = &TestConfigReader::lammps_version;
consumers["date_generated"] = &TestConfigReader::date_generated; consumers["date_generated"] = &TestConfigReader::date_generated;
consumers["epsilon"] = &TestConfigReader::epsilon; consumers["epsilon"] = &TestConfigReader::epsilon;
@ -57,29 +57,32 @@ TestConfigReader::TestConfigReader(TestConfig & config)
consumers["equilibrium"] = &TestConfigReader::equilibrium; consumers["equilibrium"] = &TestConfigReader::equilibrium;
} }
void TestConfigReader::prerequisites(const yaml_event_t & event) { void TestConfigReader::prerequisites(const yaml_event_t &event)
{
config.prerequisites.clear(); config.prerequisites.clear();
std::stringstream data((char *)event.data.scalar.value); std::stringstream data((char *)event.data.scalar.value);
std::string key, value; std::string key, value;
while(1) { while (1) {
data >> key >> value; data >> key >> value;
if (data.eof()) break; if (data.eof()) break;
config.prerequisites.push_back(std::make_pair(key,value)); config.prerequisites.push_back(std::make_pair(key, value));
} }
} }
void TestConfigReader::pre_commands(const yaml_event_t & event) { void TestConfigReader::pre_commands(const yaml_event_t &event)
{
config.pre_commands.clear(); config.pre_commands.clear();
std::stringstream data((char *)event.data.scalar.value); std::stringstream data((char *)event.data.scalar.value);
std::string line; std::string line;
while(std::getline(data, line, '\n')) { while (std::getline(data, line, '\n')) {
config.pre_commands.push_back(line); config.pre_commands.push_back(line);
} }
} }
void TestConfigReader::post_commands(const yaml_event_t & event) { void TestConfigReader::post_commands(const yaml_event_t &event)
{
config.post_commands.clear(); config.post_commands.clear();
std::stringstream data((char *)event.data.scalar.value); std::stringstream data((char *)event.data.scalar.value);
std::string line; std::string line;
@ -89,63 +92,68 @@ void TestConfigReader::post_commands(const yaml_event_t & event) {
} }
} }
void TestConfigReader::lammps_version(const yaml_event_t & event) { void TestConfigReader::lammps_version(const yaml_event_t &event)
{
config.lammps_version = (char *)event.data.scalar.value; config.lammps_version = (char *)event.data.scalar.value;
} }
void TestConfigReader::date_generated(const yaml_event_t & event) { void TestConfigReader::date_generated(const yaml_event_t &event)
{
config.date_generated = (char *)event.data.scalar.value; config.date_generated = (char *)event.data.scalar.value;
} }
void TestConfigReader::epsilon(const yaml_event_t & event) { void TestConfigReader::epsilon(const yaml_event_t &event)
{
config.epsilon = atof((char *)event.data.scalar.value); config.epsilon = atof((char *)event.data.scalar.value);
} }
void TestConfigReader::input_file(const yaml_event_t & event) { void TestConfigReader::input_file(const yaml_event_t &event)
{
config.input_file = (char *)event.data.scalar.value; config.input_file = (char *)event.data.scalar.value;
} }
void TestConfigReader::extract(const yaml_event_t & event) { void TestConfigReader::extract(const yaml_event_t &event)
{
config.extract.clear(); config.extract.clear();
std::stringstream data((char *)event.data.scalar.value); std::stringstream data((char *)event.data.scalar.value);
std::string name; std::string name;
int value; int value;
while(1) { while (1) {
data >> name >> value; data >> name >> value;
if (data.eof()) break; if (data.eof()) break;
config.extract.push_back(make_pair(name, value)); config.extract.push_back(make_pair(name, value));
} }
} }
void TestConfigReader::natoms(const yaml_event_t & event) { void TestConfigReader::natoms(const yaml_event_t &event)
{
config.natoms = atoi((char *)event.data.scalar.value); config.natoms = atoi((char *)event.data.scalar.value);
} }
void TestConfigReader::init_stress(const yaml_event_t & event) { void TestConfigReader::init_stress(const yaml_event_t &event)
{
stress_t stress; stress_t stress;
sscanf((char *)event.data.scalar.value, sscanf((char *)event.data.scalar.value, "%lg %lg %lg %lg %lg %lg", &stress.xx, &stress.yy,
"%lg %lg %lg %lg %lg %lg", &stress.zz, &stress.xy, &stress.xz, &stress.yz);
&stress.xx, &stress.yy, &stress.zz,
&stress.xy, &stress.xz, &stress.yz);
config.init_stress = stress; config.init_stress = stress;
} }
void TestConfigReader::run_stress(const yaml_event_t & event) { void TestConfigReader::run_stress(const yaml_event_t &event)
{
stress_t stress; stress_t stress;
sscanf((char *)event.data.scalar.value, sscanf((char *)event.data.scalar.value, "%lg %lg %lg %lg %lg %lg", &stress.xx, &stress.yy,
"%lg %lg %lg %lg %lg %lg", &stress.zz, &stress.xy, &stress.xz, &stress.yz);
&stress.xx, &stress.yy, &stress.zz,
&stress.xy, &stress.xz, &stress.yz);
config.run_stress = stress; config.run_stress = stress;
} }
void TestConfigReader::init_forces(const yaml_event_t & event) { void TestConfigReader::init_forces(const yaml_event_t &event)
{
config.init_forces.clear(); config.init_forces.clear();
config.init_forces.resize(config.natoms+1); config.init_forces.resize(config.natoms + 1);
std::stringstream data((const char*)event.data.scalar.value); std::stringstream data((const char *)event.data.scalar.value);
std::string line; std::string line;
while(std::getline(data, line, '\n')) { while (std::getline(data, line, '\n')) {
int tag = 0; int tag = 0;
coord_t xyz; coord_t xyz;
sscanf(line.c_str(), "%d %lg %lg %lg", &tag, &xyz.x, &xyz.y, &xyz.z); sscanf(line.c_str(), "%d %lg %lg %lg", &tag, &xyz.x, &xyz.y, &xyz.z);
@ -153,13 +161,14 @@ void TestConfigReader::init_forces(const yaml_event_t & event) {
} }
} }
void TestConfigReader::run_forces(const yaml_event_t & event) { void TestConfigReader::run_forces(const yaml_event_t &event)
{
config.run_forces.clear(); config.run_forces.clear();
config.run_forces.resize(config.natoms+1); config.run_forces.resize(config.natoms + 1);
std::stringstream data((char *)event.data.scalar.value); std::stringstream data((char *)event.data.scalar.value);
std::string line; std::string line;
while(std::getline(data, line, '\n')) { while (std::getline(data, line, '\n')) {
int tag; int tag;
coord_t xyz; coord_t xyz;
sscanf(line.c_str(), "%d %lg %lg %lg", &tag, &xyz.x, &xyz.y, &xyz.z); sscanf(line.c_str(), "%d %lg %lg %lg", &tag, &xyz.x, &xyz.y, &xyz.z);
@ -167,11 +176,13 @@ void TestConfigReader::run_forces(const yaml_event_t & event) {
} }
} }
void TestConfigReader::pair_style(const yaml_event_t & event) { void TestConfigReader::pair_style(const yaml_event_t &event)
{
config.pair_style = (char *)event.data.scalar.value; config.pair_style = (char *)event.data.scalar.value;
} }
void TestConfigReader::pair_coeff(const yaml_event_t & event) { void TestConfigReader::pair_coeff(const yaml_event_t &event)
{
config.pair_coeff.clear(); config.pair_coeff.clear();
std::stringstream data((char *)event.data.scalar.value); std::stringstream data((char *)event.data.scalar.value);
std::string line; std::string line;
@ -181,11 +192,13 @@ void TestConfigReader::pair_coeff(const yaml_event_t & event) {
} }
} }
void TestConfigReader::bond_style(const yaml_event_t & event) { void TestConfigReader::bond_style(const yaml_event_t &event)
{
config.bond_style = (char *)event.data.scalar.value; config.bond_style = (char *)event.data.scalar.value;
} }
void TestConfigReader::bond_coeff(const yaml_event_t & event) { void TestConfigReader::bond_coeff(const yaml_event_t &event)
{
config.bond_coeff.clear(); config.bond_coeff.clear();
std::stringstream data((char *)event.data.scalar.value); std::stringstream data((char *)event.data.scalar.value);
std::string line; std::string line;
@ -195,11 +208,13 @@ void TestConfigReader::bond_coeff(const yaml_event_t & event) {
} }
} }
void TestConfigReader::angle_style(const yaml_event_t & event) { void TestConfigReader::angle_style(const yaml_event_t &event)
{
config.angle_style = (char *)event.data.scalar.value; config.angle_style = (char *)event.data.scalar.value;
} }
void TestConfigReader::angle_coeff(const yaml_event_t & event) { void TestConfigReader::angle_coeff(const yaml_event_t &event)
{
config.angle_coeff.clear(); config.angle_coeff.clear();
std::stringstream data((char *)event.data.scalar.value); std::stringstream data((char *)event.data.scalar.value);
std::string line; std::string line;
@ -209,40 +224,46 @@ void TestConfigReader::angle_coeff(const yaml_event_t & event) {
} }
} }
void TestConfigReader::equilibrium(const yaml_event_t & event) { void TestConfigReader::equilibrium(const yaml_event_t &event)
{
std::stringstream data((char *)event.data.scalar.value); std::stringstream data((char *)event.data.scalar.value);
config.equilibrium.clear(); config.equilibrium.clear();
double value; double value;
std::size_t num; std::size_t num;
data >> num; data >> num;
for (std::size_t i=0; i < num; ++i) { for (std::size_t i = 0; i < num; ++i) {
data >> value; data >> value;
if (data.eof()) break; if (data.eof()) break;
config.equilibrium.push_back(value); config.equilibrium.push_back(value);
} }
} }
void TestConfigReader::init_vdwl(const yaml_event_t & event) { void TestConfigReader::init_vdwl(const yaml_event_t &event)
{
config.init_vdwl = atof((char *)event.data.scalar.value); config.init_vdwl = atof((char *)event.data.scalar.value);
} }
void TestConfigReader::init_coul(const yaml_event_t & event) { void TestConfigReader::init_coul(const yaml_event_t &event)
{
config.init_coul = atof((char *)event.data.scalar.value); config.init_coul = atof((char *)event.data.scalar.value);
} }
void TestConfigReader::run_vdwl(const yaml_event_t & event) { void TestConfigReader::run_vdwl(const yaml_event_t &event)
{
config.run_vdwl = atof((char *)event.data.scalar.value); config.run_vdwl = atof((char *)event.data.scalar.value);
} }
void TestConfigReader::run_coul(const yaml_event_t & event) { void TestConfigReader::run_coul(const yaml_event_t &event)
{
config.run_coul = atof((char *)event.data.scalar.value); config.run_coul = atof((char *)event.data.scalar.value);
} }
void TestConfigReader::init_energy(const yaml_event_t & event) { void TestConfigReader::init_energy(const yaml_event_t &event)
{
config.init_energy = atof((char *)event.data.scalar.value); config.init_energy = atof((char *)event.data.scalar.value);
} }
void TestConfigReader::run_energy(const yaml_event_t & event) { void TestConfigReader::run_energy(const yaml_event_t &event)
{
config.run_energy = atof((char *)event.data.scalar.value); config.run_energy = atof((char *)event.data.scalar.value);
} }

View File

@ -17,37 +17,37 @@
#include "yaml_reader.h" #include "yaml_reader.h"
class TestConfigReader : public YamlReader<TestConfigReader> { class TestConfigReader : public YamlReader<TestConfigReader> {
TestConfig & config; TestConfig &config;
public: public:
TestConfigReader(TestConfig & config); TestConfigReader(TestConfig &config);
void prerequisites(const yaml_event_t & event); void prerequisites(const yaml_event_t &event);
void pre_commands(const yaml_event_t & event); void pre_commands(const yaml_event_t &event);
void post_commands(const yaml_event_t & event); void post_commands(const yaml_event_t &event);
void lammps_version(const yaml_event_t & event); void lammps_version(const yaml_event_t &event);
void date_generated(const yaml_event_t & event); void date_generated(const yaml_event_t &event);
void epsilon(const yaml_event_t & event); void epsilon(const yaml_event_t &event);
void input_file(const yaml_event_t & event); void input_file(const yaml_event_t &event);
void extract(const yaml_event_t & event); void extract(const yaml_event_t &event);
void natoms(const yaml_event_t & event); void natoms(const yaml_event_t &event);
void init_stress(const yaml_event_t & event); void init_stress(const yaml_event_t &event);
void run_stress(const yaml_event_t & event); void run_stress(const yaml_event_t &event);
void init_forces(const yaml_event_t & event); void init_forces(const yaml_event_t &event);
void run_forces(const yaml_event_t & event); void run_forces(const yaml_event_t &event);
void pair_style(const yaml_event_t & event); void pair_style(const yaml_event_t &event);
void pair_coeff(const yaml_event_t & event); void pair_coeff(const yaml_event_t &event);
void bond_style(const yaml_event_t & event); void bond_style(const yaml_event_t &event);
void bond_coeff(const yaml_event_t & event); void bond_coeff(const yaml_event_t &event);
void angle_style(const yaml_event_t & event); void angle_style(const yaml_event_t &event);
void angle_coeff(const yaml_event_t & event); void angle_coeff(const yaml_event_t &event);
void equilibrium(const yaml_event_t & event); void equilibrium(const yaml_event_t &event);
void init_vdwl(const yaml_event_t & event); void init_vdwl(const yaml_event_t &event);
void init_coul(const yaml_event_t & event); void init_coul(const yaml_event_t &event);
void run_vdwl(const yaml_event_t & event); void run_vdwl(const yaml_event_t &event);
void run_coul(const yaml_event_t & event); void run_coul(const yaml_event_t &event);
void init_energy(const yaml_event_t & event); void init_energy(const yaml_event_t &event);
void run_energy(const yaml_event_t & event); void run_energy(const yaml_event_t &event);
}; };
#endif #endif

View File

@ -16,16 +16,15 @@
#include "test_config_reader.h" #include "test_config_reader.h"
#include "gtest/gtest.h" #include "gtest/gtest.h"
#include <mpi.h>
#include <cstring> #include <cstring>
#include <iostream> #include <iostream>
#include <mpi.h>
// common read_yaml_file function // common read_yaml_file function
bool read_yaml_file(const char *infile, TestConfig &config) bool read_yaml_file(const char *infile, TestConfig &config)
{ {
auto reader = TestConfigReader(config); auto reader = TestConfigReader(config);
if (reader.parse_file(infile)) if (reader.parse_file(infile)) return false;
return false;
config.basename = reader.get_basename(); config.basename = reader.get_basename();
return true; return true;
@ -76,32 +75,32 @@ int main(int argc, char **argv)
return 2; return 2;
} }
int iarg=2; int iarg = 2;
while (iarg < argc) { while (iarg < argc) {
if (strcmp(argv[iarg],"-g") == 0) { if (strcmp(argv[iarg], "-g") == 0) {
if (iarg+1 < argc) { if (iarg + 1 < argc) {
generate_yaml_file(argv[iarg+1], test_config); generate_yaml_file(argv[iarg + 1], test_config);
return 0; return 0;
} else { } else {
usage(std::cerr,argv[0]); usage(std::cerr, argv[0]);
return 1; return 1;
} }
} else if (strcmp(argv[iarg],"-u") == 0) { } else if (strcmp(argv[iarg], "-u") == 0) {
generate_yaml_file(argv[1], test_config); generate_yaml_file(argv[1], test_config);
return 0; return 0;
} else if (strcmp(argv[iarg],"-d") == 0) { } else if (strcmp(argv[iarg], "-d") == 0) {
if (iarg+1 < argc) { if (iarg + 1 < argc) {
INPUT_FOLDER = argv[iarg+1]; INPUT_FOLDER = argv[iarg + 1];
iarg += 2; iarg += 2;
} else { } else {
usage(std::cerr,argv[0]); usage(std::cerr, argv[0]);
return 1; return 1;
} }
} else if (strcmp(argv[iarg],"-s") == 0) { } else if (strcmp(argv[iarg], "-s") == 0) {
print_stats = true; print_stats = true;
++iarg; ++iarg;
} else if (strcmp(argv[iarg],"-v") == 0) { } else if (strcmp(argv[iarg], "-v") == 0) {
verbose = true; verbose = true;
++iarg; ++iarg;
} else { } else {

View File

@ -22,11 +22,11 @@ extern bool print_stats;
extern bool verbose; extern bool verbose;
extern std::string INPUT_FOLDER; extern std::string INPUT_FOLDER;
#define EXPECT_FP_LE_WITH_EPS(val1,val2,eps) \ #define EXPECT_FP_LE_WITH_EPS(val1, val2, eps) \
do { \ do { \
const double diff = fabs(val1-val2); \ const double diff = fabs(val1 - val2); \
const double div = std::min(fabs(val1),fabs(val2)); \ const double div = std::min(fabs(val1), fabs(val2)); \
const double err = (div == 0.0) ? diff : diff/div; \ const double err = (div == 0.0) ? diff : diff / div; \
stats.add(err); \ stats.add(err); \
EXPECT_PRED_FORMAT2(::testing::DoubleLE, err, eps); \ EXPECT_PRED_FORMAT2(::testing::DoubleLE, err, eps); \
} while (0); } while (0);

View File

@ -17,11 +17,11 @@
#include "yaml.h" #include "yaml.h"
#include <cstdio> #include <cstdio>
#include <iostream>
#include <map> #include <map>
#include <string> #include <string>
#include <iostream>
template<typename ConsumerClass> class YamlReader { template <typename ConsumerClass> class YamlReader {
private: private:
enum StateValue { enum StateValue {
START, START,
@ -37,7 +37,7 @@ private:
std::string basename; std::string basename;
protected: protected:
typedef void (ConsumerClass::*EventConsumer)(const yaml_event_t & event); typedef void (ConsumerClass::*EventConsumer)(const yaml_event_t &event);
std::map<std::string, EventConsumer> consumers; std::map<std::string, EventConsumer> consumers;
public: public:
@ -46,20 +46,21 @@ public:
std::string get_basename() const { return basename; } std::string get_basename() const { return basename; }
int parse_file(const std::string & infile) { int parse_file(const std::string &infile)
{
basename = infile; basename = infile;
std::size_t found = basename.rfind(".yaml"); std::size_t found = basename.rfind(".yaml");
if (found > 0) basename = basename.substr(0,found); if (found > 0) basename = basename.substr(0, found);
found = basename.find_last_of("/\\"); found = basename.find_last_of("/\\");
if (found != std::string::npos) basename = basename.substr(found+1); if (found != std::string::npos) basename = basename.substr(found + 1);
FILE *fp = fopen(infile.c_str(),"r"); FILE *fp = fopen(infile.c_str(), "r");
yaml_parser_t parser; yaml_parser_t parser;
yaml_event_t event; yaml_event_t event;
if (!fp) { if (!fp) {
std::cerr << "Cannot open yaml file '" << infile std::cerr << "Cannot open yaml file '" << infile << "': " << strerror(errno)
<< "': " << strerror(errno) << std::endl; << std::endl;
return 1; return 1;
} }
yaml_parser_initialize(&parser); yaml_parser_initialize(&parser);
@ -75,9 +76,9 @@ public:
} }
if (accepted) { if (accepted) {
if(!consume_key_value(key, event)) { if (!consume_key_value(key, event)) {
std::cerr << "Ignoring unknown key/value pair: " << key std::cerr << "Ignoring unknown key/value pair: " << key << " = "
<< " = " << event.data.scalar.value << std::endl; << event.data.scalar.value << std::endl;
} }
} }
yaml_event_delete(&event); yaml_event_delete(&event);
@ -89,13 +90,14 @@ public:
} }
protected: protected:
bool consume_key_value(const std::string & key, const yaml_event_t & event) { bool consume_key_value(const std::string &key, const yaml_event_t &event)
{
auto it = consumers.find(key); auto it = consumers.find(key);
ConsumerClass *consumer = dynamic_cast<ConsumerClass*>(this); ConsumerClass *consumer = dynamic_cast<ConsumerClass *>(this);
if(consumer) { if (consumer) {
if(it != consumers.end()) { if (it != consumers.end()) {
//std::cerr << "Loading: " << key << std::endl; // std::cerr << "Loading: " << key << std::endl;
(consumer->*(it->second))(event); (consumer->*(it->second))(event);
return true; return true;
} }
@ -106,7 +108,8 @@ protected:
return false; return false;
} }
bool consume_event(yaml_event_t & event) { bool consume_event(yaml_event_t &event)
{
accepted = false; accepted = false;
switch (state) { switch (state) {
case START: case START:
@ -136,7 +139,7 @@ protected:
case ACCEPT_KEY: case ACCEPT_KEY:
switch (event.type) { switch (event.type) {
case YAML_SCALAR_EVENT: case YAML_SCALAR_EVENT:
key = (char *) event.data.scalar.value; key = (char *)event.data.scalar.value;
state = ACCEPT_VALUE; state = ACCEPT_VALUE;
break; break;
case YAML_MAPPING_END_EVENT: case YAML_MAPPING_END_EVENT:
@ -144,8 +147,7 @@ protected:
break; break;
default: default:
std::cerr << "UNHANDLED YAML EVENT (key): " << event.type std::cerr << "UNHANDLED YAML EVENT (key): " << event.type
<< "\nVALUE: " << event.data.scalar.value << "\nVALUE: " << event.data.scalar.value << std::endl;
<< std::endl;
state = ERROR; state = ERROR;
break; break;
} }
@ -159,8 +161,7 @@ protected:
break; break;
default: default:
std::cerr << "UNHANDLED YAML EVENT (value): " << event.type std::cerr << "UNHANDLED YAML EVENT (value): " << event.type
<< "\nVALUE: " << event.data.scalar.value << "\nVALUE: " << event.data.scalar.value << std::endl;
<< std::endl;
state = ERROR; state = ERROR;
break; break;
} }

View File

@ -14,10 +14,11 @@
#include "yaml_writer.h" #include "yaml_writer.h"
#include "yaml.h" #include "yaml.h"
#include <string>
#include <cstdio> #include <cstdio>
#include <string>
YamlWriter::YamlWriter(const char * outfile) { YamlWriter::YamlWriter(const char *outfile)
{
yaml_emitter_initialize(&emitter); yaml_emitter_initialize(&emitter);
fp = fopen(outfile, "w"); fp = fopen(outfile, "w");
if (!fp) { if (!fp) {
@ -31,13 +32,13 @@ YamlWriter::YamlWriter(const char * outfile) {
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0); yaml_document_start_event_initialize(&event, NULL, NULL, NULL, 0);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
yaml_mapping_start_event_initialize(&event, NULL, yaml_mapping_start_event_initialize(&event, NULL, (yaml_char_t *)YAML_MAP_TAG, 1,
(yaml_char_t *)YAML_MAP_TAG, YAML_ANY_MAPPING_STYLE);
1, YAML_ANY_MAPPING_STYLE);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
} }
YamlWriter::~YamlWriter() { YamlWriter::~YamlWriter()
{
yaml_mapping_end_event_initialize(&event); yaml_mapping_end_event_initialize(&event);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
yaml_document_end_event_initialize(&event, 0); yaml_document_end_event_initialize(&event, 0);
@ -48,84 +49,65 @@ YamlWriter::~YamlWriter() {
fclose(fp); fclose(fp);
} }
void YamlWriter::emit(const std::string &key, const double value) { void YamlWriter::emit(const std::string &key, const double value)
yaml_scalar_event_initialize(&event, NULL, {
(yaml_char_t *)YAML_STR_TAG, yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *) key.c_str(), (yaml_char_t *)key.c_str(), key.size(), 1, 0,
key.size(), 1, 0,
YAML_PLAIN_SCALAR_STYLE); YAML_PLAIN_SCALAR_STYLE);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
char buf[256]; char buf[256];
snprintf(buf,256,"%.15g",value); snprintf(buf, 256, "%.15g", value);
yaml_scalar_event_initialize(&event, NULL, yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG, (yaml_char_t *)buf,
(yaml_char_t *)YAML_STR_TAG, strlen(buf), 1, 0, YAML_PLAIN_SCALAR_STYLE);
(yaml_char_t *)buf,
strlen(buf), 1, 0,
YAML_PLAIN_SCALAR_STYLE);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
} }
void YamlWriter::emit(const std::string &key, const long value) { void YamlWriter::emit(const std::string &key, const long value)
yaml_scalar_event_initialize(&event, NULL, {
(yaml_char_t *)YAML_STR_TAG, yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *) key.c_str(), (yaml_char_t *)key.c_str(), key.size(), 1, 0,
key.size(), 1, 0,
YAML_PLAIN_SCALAR_STYLE); YAML_PLAIN_SCALAR_STYLE);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
char buf[256]; char buf[256];
snprintf(buf,256,"%ld",value); snprintf(buf, 256, "%ld", value);
yaml_scalar_event_initialize(&event, NULL, yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG, (yaml_char_t *)buf,
(yaml_char_t *)YAML_STR_TAG, strlen(buf), 1, 0, YAML_PLAIN_SCALAR_STYLE);
(yaml_char_t *)buf,
strlen(buf), 1, 0,
YAML_PLAIN_SCALAR_STYLE);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
} }
void YamlWriter::emit(const std::string &key, const int value) { void YamlWriter::emit(const std::string &key, const int value)
yaml_scalar_event_initialize(&event, NULL, {
(yaml_char_t *)YAML_STR_TAG, yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *) key.c_str(), (yaml_char_t *)key.c_str(), key.size(), 1, 0,
key.size(), 1, 0,
YAML_PLAIN_SCALAR_STYLE); YAML_PLAIN_SCALAR_STYLE);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
char buf[256]; char buf[256];
snprintf(buf,256,"%d",value); snprintf(buf, 256, "%d", value);
yaml_scalar_event_initialize(&event, NULL, yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG, (yaml_char_t *)buf,
(yaml_char_t *)YAML_STR_TAG, strlen(buf), 1, 0, YAML_PLAIN_SCALAR_STYLE);
(yaml_char_t *)buf,
strlen(buf), 1, 0,
YAML_PLAIN_SCALAR_STYLE);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
} }
void YamlWriter::emit(const std::string &key, const std::string &value) void YamlWriter::emit(const std::string &key, const std::string &value)
{ {
yaml_scalar_event_initialize(&event, NULL, yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *)YAML_STR_TAG, (yaml_char_t *)key.c_str(), key.size(), 1, 0,
(yaml_char_t *) key.c_str(),
key.size(), 1, 0,
YAML_PLAIN_SCALAR_STYLE); YAML_PLAIN_SCALAR_STYLE);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
yaml_scalar_event_initialize(&event, NULL, yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *)YAML_STR_TAG, (yaml_char_t *)value.c_str(), value.size(), 1, 0,
(yaml_char_t *)value.c_str(),
value.size(), 1, 0,
YAML_PLAIN_SCALAR_STYLE); YAML_PLAIN_SCALAR_STYLE);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
} }
void YamlWriter::emit_block(const std::string &key, const std::string &value) { void YamlWriter::emit_block(const std::string &key, const std::string &value)
yaml_scalar_event_initialize(&event, NULL, {
(yaml_char_t *)YAML_STR_TAG, yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *) key.c_str(), (yaml_char_t *)key.c_str(), key.size(), 1, 0,
key.size(), 1, 0,
YAML_PLAIN_SCALAR_STYLE); YAML_PLAIN_SCALAR_STYLE);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
yaml_scalar_event_initialize(&event, NULL, yaml_scalar_event_initialize(&event, NULL, (yaml_char_t *)YAML_STR_TAG,
(yaml_char_t *)YAML_STR_TAG, (yaml_char_t *)value.c_str(), value.size(), 1, 0,
(yaml_char_t *)value.c_str(),
value.size(), 1, 0,
YAML_LITERAL_SCALAR_STYLE); YAML_LITERAL_SCALAR_STYLE);
yaml_emitter_emit(&emitter, &event); yaml_emitter_emit(&emitter, &event);
} }

View File

@ -20,7 +20,7 @@
class YamlWriter { class YamlWriter {
public: public:
YamlWriter(const char * outfile); YamlWriter(const char *outfile);
virtual ~YamlWriter(); virtual ~YamlWriter();
// emitters // emitters
@ -36,8 +36,8 @@ private:
yaml_event_t event; yaml_event_t event;
private: private:
YamlWriter() {}; YamlWriter(){};
YamlWriter(const YamlWriter &) {}; YamlWriter(const YamlWriter &){};
}; };
#endif #endif

View File

@ -11,11 +11,11 @@
See the README file in the top-level LAMMPS directory. See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#include "gmock/gmock.h" #include "MANYBODY/pair_eim.h"
#include "gtest/gtest.h"
#include "lammps.h" #include "lammps.h"
#include "utils.h" #include "utils.h"
#include "MANYBODY/pair_eim.h" #include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <mpi.h> #include <mpi.h>
@ -23,19 +23,21 @@ using namespace LAMMPS_NS;
class EIMPotentialFileReaderTest : public ::testing::Test { class EIMPotentialFileReaderTest : public ::testing::Test {
protected: protected:
LAMMPS * lmp; LAMMPS *lmp;
PairEIM::Setfl setfl; PairEIM::Setfl setfl;
static const int nelements = 9; static const int nelements = 9;
void SetUp() override { void SetUp() override
const char *args[] = {"PotentialFileReaderTest", "-log", "none", "-echo", "screen", "-nocite" }; {
const char *args[] = {
"PotentialFileReaderTest", "-log", "none", "-echo", "screen", "-nocite"};
char **argv = (char **)args; char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *); int argc = sizeof(args) / sizeof(char *);
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD); lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
int npair = nelements*(nelements+1)/2; int npair = nelements * (nelements + 1) / 2;
setfl.ielement = new int[nelements]; setfl.ielement = new int[nelements];
setfl.mass = new double[nelements]; setfl.mass = new double[nelements];
setfl.negativity = new double[nelements]; setfl.negativity = new double[nelements];
@ -59,28 +61,29 @@ protected:
setfl.tp = new int[npair]; setfl.tp = new int[npair];
} }
void TearDown() override { void TearDown() override
delete [] setfl.ielement; {
delete [] setfl.mass; delete[] setfl.ielement;
delete [] setfl.negativity; delete[] setfl.mass;
delete [] setfl.ra; delete[] setfl.negativity;
delete [] setfl.ri; delete[] setfl.ra;
delete [] setfl.Ec; delete[] setfl.ri;
delete [] setfl.q0; delete[] setfl.Ec;
delete [] setfl.rcutphiA; delete[] setfl.q0;
delete [] setfl.rcutphiR; delete[] setfl.rcutphiA;
delete [] setfl.Eb; delete[] setfl.rcutphiR;
delete [] setfl.r0; delete[] setfl.Eb;
delete [] setfl.alpha; delete[] setfl.r0;
delete [] setfl.beta; delete[] setfl.alpha;
delete [] setfl.rcutq; delete[] setfl.beta;
delete [] setfl.Asigma; delete[] setfl.rcutq;
delete [] setfl.rq; delete[] setfl.Asigma;
delete [] setfl.rcutsigma; delete[] setfl.rq;
delete [] setfl.Ac; delete[] setfl.rcutsigma;
delete [] setfl.zeta; delete[] setfl.Ac;
delete [] setfl.rs; delete[] setfl.zeta;
delete [] setfl.tp; delete[] setfl.rs;
delete[] setfl.tp;
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
delete lmp; delete lmp;
@ -88,7 +91,8 @@ protected:
} }
}; };
TEST_F(EIMPotentialFileReaderTest, global_line) { TEST_F(EIMPotentialFileReaderTest, global_line)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
EIMPotentialFileReader reader(lmp, "ffield.eim"); EIMPotentialFileReader reader(lmp, "ffield.eim");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -99,7 +103,8 @@ TEST_F(EIMPotentialFileReaderTest, global_line) {
ASSERT_DOUBLE_EQ(setfl.rsmall, 1.645); ASSERT_DOUBLE_EQ(setfl.rsmall, 1.645);
} }
TEST_F(EIMPotentialFileReaderTest, element_line_sequential) { TEST_F(EIMPotentialFileReaderTest, element_line_sequential)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
EIMPotentialFileReader reader(lmp, "ffield.eim"); EIMPotentialFileReader reader(lmp, "ffield.eim");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -123,7 +128,8 @@ TEST_F(EIMPotentialFileReaderTest, element_line_sequential) {
ASSERT_DOUBLE_EQ(setfl.q0[1], 0.0000e+00); ASSERT_DOUBLE_EQ(setfl.q0[1], 0.0000e+00);
} }
TEST_F(EIMPotentialFileReaderTest, element_line_random) { TEST_F(EIMPotentialFileReaderTest, element_line_random)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
EIMPotentialFileReader reader(lmp, "ffield.eim"); EIMPotentialFileReader reader(lmp, "ffield.eim");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -138,7 +144,8 @@ TEST_F(EIMPotentialFileReaderTest, element_line_random) {
ASSERT_DOUBLE_EQ(setfl.q0[0], 0.0000e+00); ASSERT_DOUBLE_EQ(setfl.q0[0], 0.0000e+00);
} }
TEST_F(EIMPotentialFileReaderTest, pair_line) { TEST_F(EIMPotentialFileReaderTest, pair_line)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
EIMPotentialFileReader reader(lmp, "ffield.eim"); EIMPotentialFileReader reader(lmp, "ffield.eim");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -160,7 +167,8 @@ TEST_F(EIMPotentialFileReaderTest, pair_line) {
ASSERT_EQ(setfl.tp[0], 1); ASSERT_EQ(setfl.tp[0], 1);
} }
TEST_F(EIMPotentialFileReaderTest, pair_identical) { TEST_F(EIMPotentialFileReaderTest, pair_identical)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
EIMPotentialFileReader reader(lmp, "ffield.eim"); EIMPotentialFileReader reader(lmp, "ffield.eim");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();

View File

@ -11,23 +11,23 @@
See the README file in the top-level LAMMPS directory. See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "potential_file_reader.h"
#include "lammps.h"
#include "utils.h"
#include "MANYBODY/pair_sw.h"
#include "MANYBODY/pair_comb.h" #include "MANYBODY/pair_comb.h"
#include "MANYBODY/pair_comb3.h" #include "MANYBODY/pair_comb3.h"
#include "MANYBODY/pair_eim.h"
#include "MANYBODY/pair_gw.h"
#include "MANYBODY/pair_gw_zbl.h"
#include "MANYBODY/pair_nb3b_harmonic.h"
#include "MANYBODY/pair_sw.h"
#include "MANYBODY/pair_tersoff.h" #include "MANYBODY/pair_tersoff.h"
#include "MANYBODY/pair_tersoff_mod.h" #include "MANYBODY/pair_tersoff_mod.h"
#include "MANYBODY/pair_tersoff_mod_c.h" #include "MANYBODY/pair_tersoff_mod_c.h"
#include "MANYBODY/pair_tersoff_zbl.h" #include "MANYBODY/pair_tersoff_zbl.h"
#include "MANYBODY/pair_gw.h"
#include "MANYBODY/pair_gw_zbl.h"
#include "MANYBODY/pair_nb3b_harmonic.h"
#include "MANYBODY/pair_vashishta.h" #include "MANYBODY/pair_vashishta.h"
#include "MANYBODY/pair_eim.h" #include "lammps.h"
#include "potential_file_reader.h"
#include "utils.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <mpi.h> #include <mpi.h>
@ -47,25 +47,29 @@ const int LAMMPS_NS::PairVashishta::NPARAMS_PER_LINE;
class PotentialFileReaderTest : public ::testing::Test { class PotentialFileReaderTest : public ::testing::Test {
protected: protected:
LAMMPS * lmp; LAMMPS *lmp;
void SetUp() override { void SetUp() override
const char *args[] = {"PotentialFileReaderTest", "-log", "none", "-echo", "screen", "-nocite" }; {
const char *args[] = {
"PotentialFileReaderTest", "-log", "none", "-echo", "screen", "-nocite"};
char **argv = (char **)args; char **argv = (char **)args;
int argc = sizeof(args)/sizeof(char *); int argc = sizeof(args) / sizeof(char *);
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD); lmp = new LAMMPS(argc, argv, MPI_COMM_WORLD);
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
} }
void TearDown() override { void TearDown() override
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
delete lmp; delete lmp;
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
} }
}; };
TEST_F(PotentialFileReaderTest, Si) { TEST_F(PotentialFileReaderTest, Si)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
PotentialFileReader reader(lmp, "Si.sw", "Stillinger-Weber"); PotentialFileReader reader(lmp, "Si.sw", "Stillinger-Weber");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -74,7 +78,8 @@ TEST_F(PotentialFileReaderTest, Si) {
ASSERT_EQ(utils::count_words(line), PairSW::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairSW::NPARAMS_PER_LINE);
} }
TEST_F(PotentialFileReaderTest, Comb) { TEST_F(PotentialFileReaderTest, Comb)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
PotentialFileReader reader(lmp, "ffield.comb", "COMB"); PotentialFileReader reader(lmp, "ffield.comb", "COMB");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -83,7 +88,8 @@ TEST_F(PotentialFileReaderTest, Comb) {
ASSERT_EQ(utils::count_words(line), PairComb::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairComb::NPARAMS_PER_LINE);
} }
TEST_F(PotentialFileReaderTest, Comb3) { TEST_F(PotentialFileReaderTest, Comb3)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
PotentialFileReader reader(lmp, "ffield.comb3", "COMB3"); PotentialFileReader reader(lmp, "ffield.comb3", "COMB3");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -92,7 +98,8 @@ TEST_F(PotentialFileReaderTest, Comb3) {
ASSERT_EQ(utils::count_words(line), PairComb3::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairComb3::NPARAMS_PER_LINE);
} }
TEST_F(PotentialFileReaderTest, Tersoff) { TEST_F(PotentialFileReaderTest, Tersoff)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
PotentialFileReader reader(lmp, "Si.tersoff", "Tersoff"); PotentialFileReader reader(lmp, "Si.tersoff", "Tersoff");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -101,7 +108,8 @@ TEST_F(PotentialFileReaderTest, Tersoff) {
ASSERT_EQ(utils::count_words(line), PairTersoff::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairTersoff::NPARAMS_PER_LINE);
} }
TEST_F(PotentialFileReaderTest, TersoffMod) { TEST_F(PotentialFileReaderTest, TersoffMod)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
PotentialFileReader reader(lmp, "Si.tersoff.mod", "Tersoff/Mod"); PotentialFileReader reader(lmp, "Si.tersoff.mod", "Tersoff/Mod");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -110,7 +118,8 @@ TEST_F(PotentialFileReaderTest, TersoffMod) {
ASSERT_EQ(utils::count_words(line), PairTersoffMOD::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairTersoffMOD::NPARAMS_PER_LINE);
} }
TEST_F(PotentialFileReaderTest, TersoffModC) { TEST_F(PotentialFileReaderTest, TersoffModC)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
PotentialFileReader reader(lmp, "Si.tersoff.modc", "Tersoff/ModC"); PotentialFileReader reader(lmp, "Si.tersoff.modc", "Tersoff/ModC");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -119,7 +128,8 @@ TEST_F(PotentialFileReaderTest, TersoffModC) {
ASSERT_EQ(utils::count_words(line), PairTersoffMODC::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairTersoffMODC::NPARAMS_PER_LINE);
} }
TEST_F(PotentialFileReaderTest, TersoffZBL) { TEST_F(PotentialFileReaderTest, TersoffZBL)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
PotentialFileReader reader(lmp, "SiC.tersoff.zbl", "Tersoff/ZBL"); PotentialFileReader reader(lmp, "SiC.tersoff.zbl", "Tersoff/ZBL");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -128,7 +138,8 @@ TEST_F(PotentialFileReaderTest, TersoffZBL) {
ASSERT_EQ(utils::count_words(line), PairTersoffZBL::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairTersoffZBL::NPARAMS_PER_LINE);
} }
TEST_F(PotentialFileReaderTest, GW) { TEST_F(PotentialFileReaderTest, GW)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
PotentialFileReader reader(lmp, "SiC.gw", "GW"); PotentialFileReader reader(lmp, "SiC.gw", "GW");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -137,7 +148,8 @@ TEST_F(PotentialFileReaderTest, GW) {
ASSERT_EQ(utils::count_words(line), PairGW::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairGW::NPARAMS_PER_LINE);
} }
TEST_F(PotentialFileReaderTest, GWZBL) { TEST_F(PotentialFileReaderTest, GWZBL)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
PotentialFileReader reader(lmp, "SiC.gw.zbl", "GW/ZBL"); PotentialFileReader reader(lmp, "SiC.gw.zbl", "GW/ZBL");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -146,7 +158,8 @@ TEST_F(PotentialFileReaderTest, GWZBL) {
ASSERT_EQ(utils::count_words(line), PairGWZBL::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairGWZBL::NPARAMS_PER_LINE);
} }
TEST_F(PotentialFileReaderTest, Nb3bHarmonic) { TEST_F(PotentialFileReaderTest, Nb3bHarmonic)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
PotentialFileReader reader(lmp, "MOH.nb3b.harmonic", "NB3B Harmonic"); PotentialFileReader reader(lmp, "MOH.nb3b.harmonic", "NB3B Harmonic");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();
@ -155,7 +168,8 @@ TEST_F(PotentialFileReaderTest, Nb3bHarmonic) {
ASSERT_EQ(utils::count_words(line), PairNb3bHarmonic::NPARAMS_PER_LINE); ASSERT_EQ(utils::count_words(line), PairNb3bHarmonic::NPARAMS_PER_LINE);
} }
TEST_F(PotentialFileReaderTest, Vashishta) { TEST_F(PotentialFileReaderTest, Vashishta)
{
::testing::internal::CaptureStdout(); ::testing::internal::CaptureStdout();
PotentialFileReader reader(lmp, "SiC.vashishta", "Vashishta"); PotentialFileReader reader(lmp, "SiC.vashishta", "Vashishta");
::testing::internal::GetCapturedStdout(); ::testing::internal::GetCapturedStdout();

View File

@ -11,114 +11,127 @@
See the README file in the top-level LAMMPS directory. See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#include "lmptype.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "fmt/format.h" #include "fmt/format.h"
#include <string> #include "lmptype.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <exception> #include <exception>
#include <string>
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
using ::testing::Eq; using ::testing::Eq;
// this tests a subset of {fmt} that is most relevant to LAMMPS // this tests a subset of {fmt} that is most relevant to LAMMPS
TEST(FmtLib, insert_string) { TEST(FmtLib, insert_string)
{
const char val[] = "word"; const char val[] = "word";
auto text = fmt::format("word {}",val); auto text = fmt::format("word {}", val);
ASSERT_THAT(text, Eq("word word")); ASSERT_THAT(text, Eq("word word"));
} }
TEST(FmtLib, insert_int) { TEST(FmtLib, insert_int)
{
const int val = 333; const int val = 333;
auto text = fmt::format("word {}",val); auto text = fmt::format("word {}", val);
ASSERT_THAT(text, Eq("word 333")); ASSERT_THAT(text, Eq("word 333"));
} }
TEST(FmtLib, insert_neg_int) { TEST(FmtLib, insert_neg_int)
{
const int val = -333; const int val = -333;
auto text = fmt::format("word {}",val); auto text = fmt::format("word {}", val);
ASSERT_THAT(text, Eq("word -333")); ASSERT_THAT(text, Eq("word -333"));
} }
TEST(FmtLib, insert_bigint) { TEST(FmtLib, insert_bigint)
{
#if defined(LAMMPS_BIGBIG) || defined(LAMMPS_SMALLBIG) #if defined(LAMMPS_BIGBIG) || defined(LAMMPS_SMALLBIG)
const bigint val = 9945234592L; const bigint val = 9945234592L;
auto text = fmt::format("word {}",val); auto text = fmt::format("word {}", val);
ASSERT_THAT(text, Eq("word 9945234592")); ASSERT_THAT(text, Eq("word 9945234592"));
#else #else
GTEST_SKIP(); GTEST_SKIP();
#endif #endif
} }
TEST(FmtLib, insert_neg_bigint) { TEST(FmtLib, insert_neg_bigint)
{
#if defined(LAMMPS_BIGBIG) || defined(LAMMPS_SMALLBIG) #if defined(LAMMPS_BIGBIG) || defined(LAMMPS_SMALLBIG)
const bigint val = -9945234592L; const bigint val = -9945234592L;
auto text = fmt::format("word {}",val); auto text = fmt::format("word {}", val);
ASSERT_THAT(text, Eq("word -9945234592")); ASSERT_THAT(text, Eq("word -9945234592"));
#else #else
GTEST_SKIP(); GTEST_SKIP();
#endif #endif
} }
TEST(FmtLib, insert_tagint) { TEST(FmtLib, insert_tagint)
{
#if defined(LAMMPS_BIGBIG) #if defined(LAMMPS_BIGBIG)
const tagint val = 9945234592L; const tagint val = 9945234592L;
auto text = fmt::format("word {}",val); auto text = fmt::format("word {}", val);
ASSERT_THAT(text, Eq("word 9945234592")); ASSERT_THAT(text, Eq("word 9945234592"));
#else #else
GTEST_SKIP(); GTEST_SKIP();
#endif #endif
} }
TEST(FmtLib, insert_neg_tagint) { TEST(FmtLib, insert_neg_tagint)
{
#if defined(LAMMPS_BIGBIG) #if defined(LAMMPS_BIGBIG)
const tagint val = -9945234592L; const tagint val = -9945234592L;
auto text = fmt::format("word {}",val); auto text = fmt::format("word {}", val);
ASSERT_THAT(text, Eq("word -9945234592")); ASSERT_THAT(text, Eq("word -9945234592"));
#else #else
GTEST_SKIP(); GTEST_SKIP();
#endif #endif
} }
TEST(FmtLib, insert_imageint) { TEST(FmtLib, insert_imageint)
{
#if defined(LAMMPS_BIGBIG) #if defined(LAMMPS_BIGBIG)
const imageint val = 9945234592L; const imageint val = 9945234592L;
auto text = fmt::format("word {}",val); auto text = fmt::format("word {}", val);
ASSERT_THAT(text, Eq("word 9945234592")); ASSERT_THAT(text, Eq("word 9945234592"));
#else #else
GTEST_SKIP(); GTEST_SKIP();
#endif #endif
} }
TEST(FmtLib, insert_neg_imageint) { TEST(FmtLib, insert_neg_imageint)
{
#if defined(LAMMPS_BIGBIG) #if defined(LAMMPS_BIGBIG)
const imageint val = -9945234592L; const imageint val = -9945234592L;
auto text = fmt::format("word {}",val); auto text = fmt::format("word {}", val);
ASSERT_THAT(text, Eq("word -9945234592")); ASSERT_THAT(text, Eq("word -9945234592"));
#else #else
GTEST_SKIP(); GTEST_SKIP();
#endif #endif
} }
TEST(FmtLib, insert_double) { TEST(FmtLib, insert_double)
{
const double val = 1.5; const double val = 1.5;
auto text = fmt::format("word {}",val); auto text = fmt::format("word {}", val);
ASSERT_THAT(text, Eq("word 1.5")); ASSERT_THAT(text, Eq("word 1.5"));
} }
TEST(FmtLib, insert_neg_double) { TEST(FmtLib, insert_neg_double)
{
const double val = -1.5; const double val = -1.5;
auto text = fmt::format("word {}",val); auto text = fmt::format("word {}", val);
ASSERT_THAT(text, Eq("word -1.5")); ASSERT_THAT(text, Eq("word -1.5"));
} }
TEST(FmtLib, int_for_double) { TEST(FmtLib, int_for_double)
{
const double val = -1.5; const double val = -1.5;
ASSERT_THROW(fmt::format("word {:d}",val),std::exception); ASSERT_THROW(fmt::format("word {:d}", val), std::exception);
} }
TEST(FmtLib, double_for_int) { TEST(FmtLib, double_for_int)
{
const int val = 15; const int val = 15;
ASSERT_THROW(fmt::format("word {:g}",val),std::exception); ASSERT_THROW(fmt::format("word {:g}", val), std::exception);
} }

View File

@ -11,100 +11,117 @@
See the README file in the top-level LAMMPS directory. See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "tokenizer.h" #include "tokenizer.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
using ::testing::Eq; using ::testing::Eq;
TEST(Tokenizer, empty_string) { TEST(Tokenizer, empty_string)
{
Tokenizer t("", " "); Tokenizer t("", " ");
ASSERT_EQ(t.count(), 0); ASSERT_EQ(t.count(), 0);
} }
TEST(Tokenizer, whitespace_only) { TEST(Tokenizer, whitespace_only)
{
Tokenizer t(" ", " "); Tokenizer t(" ", " ");
ASSERT_EQ(t.count(), 0); ASSERT_EQ(t.count(), 0);
} }
TEST(Tokenizer, single_word) { TEST(Tokenizer, single_word)
{
Tokenizer t("test", " "); Tokenizer t("test", " ");
ASSERT_EQ(t.count(), 1); ASSERT_EQ(t.count(), 1);
} }
TEST(Tokenizer, two_words) { TEST(Tokenizer, two_words)
{
Tokenizer t("test word", " "); Tokenizer t("test word", " ");
ASSERT_EQ(t.count(), 2); ASSERT_EQ(t.count(), 2);
} }
TEST(Tokenizer, prefix_separators) { TEST(Tokenizer, prefix_separators)
{
Tokenizer t(" test word", " "); Tokenizer t(" test word", " ");
ASSERT_EQ(t.count(), 2); ASSERT_EQ(t.count(), 2);
} }
TEST(Tokenizer, postfix_separators) { TEST(Tokenizer, postfix_separators)
{
Tokenizer t("test word ", " "); Tokenizer t("test word ", " ");
ASSERT_EQ(t.count(), 2); ASSERT_EQ(t.count(), 2);
} }
TEST(Tokenizer, iterate_words) { TEST(Tokenizer, iterate_words)
{
Tokenizer t(" test word ", " "); Tokenizer t(" test word ", " ");
ASSERT_THAT(t.next(), Eq("test")); ASSERT_THAT(t.next(), Eq("test"));
ASSERT_THAT(t.next(), Eq("word")); ASSERT_THAT(t.next(), Eq("word"));
ASSERT_EQ(t.count(), 2); ASSERT_EQ(t.count(), 2);
} }
TEST(Tokenizer, default_separators) { TEST(Tokenizer, default_separators)
{
Tokenizer t(" \r\n test \t word \f"); Tokenizer t(" \r\n test \t word \f");
ASSERT_THAT(t.next(), Eq("test")); ASSERT_THAT(t.next(), Eq("test"));
ASSERT_THAT(t.next(), Eq("word")); ASSERT_THAT(t.next(), Eq("word"));
ASSERT_EQ(t.count(), 2); ASSERT_EQ(t.count(), 2);
} }
TEST(Tokenizer, as_vector) { TEST(Tokenizer, as_vector)
{
Tokenizer t(" \r\n test \t word \f"); Tokenizer t(" \r\n test \t word \f");
std::vector<std::string> list = t.as_vector(); std::vector<std::string> list = t.as_vector();
ASSERT_THAT(list[0], Eq("test")); ASSERT_THAT(list[0], Eq("test"));
ASSERT_THAT(list[1], Eq("word")); ASSERT_THAT(list[1], Eq("word"));
} }
TEST(ValueTokenizer, empty_string) { TEST(ValueTokenizer, empty_string)
{
ValueTokenizer values(""); ValueTokenizer values("");
ASSERT_FALSE(values.has_next()); ASSERT_FALSE(values.has_next());
} }
TEST(ValueTokenizer, bad_integer) { TEST(ValueTokenizer, bad_integer)
{
ValueTokenizer values("f10"); ValueTokenizer values("f10");
ASSERT_THROW(values.next_int(), InvalidIntegerException); ASSERT_THROW(values.next_int(), InvalidIntegerException);
} }
TEST(ValueTokenizer, bad_double) { TEST(ValueTokenizer, bad_double)
{
ValueTokenizer values("1a.0"); ValueTokenizer values("1a.0");
ASSERT_THROW(values.next_double(), InvalidFloatException); ASSERT_THROW(values.next_double(), InvalidFloatException);
} }
TEST(ValueTokenizer, valid_int) { TEST(ValueTokenizer, valid_int)
{
ValueTokenizer values("10"); ValueTokenizer values("10");
ASSERT_EQ(values.next_int(), 10); ASSERT_EQ(values.next_int(), 10);
} }
TEST(ValueTokenizer, valid_tagint) { TEST(ValueTokenizer, valid_tagint)
{
ValueTokenizer values("42"); ValueTokenizer values("42");
ASSERT_EQ(values.next_tagint(), 42); ASSERT_EQ(values.next_tagint(), 42);
} }
TEST(ValueTokenizer, valid_bigint) { TEST(ValueTokenizer, valid_bigint)
{
ValueTokenizer values("42"); ValueTokenizer values("42");
ASSERT_EQ(values.next_bigint(), 42); ASSERT_EQ(values.next_bigint(), 42);
} }
TEST(ValueTokenizer, valid_double) { TEST(ValueTokenizer, valid_double)
{
ValueTokenizer values("3.14"); ValueTokenizer values("3.14");
ASSERT_DOUBLE_EQ(values.next_double(), 3.14); ASSERT_DOUBLE_EQ(values.next_double(), 3.14);
} }
TEST(ValueTokenizer, valid_double_with_exponential) { TEST(ValueTokenizer, valid_double_with_exponential)
{
ValueTokenizer values("3.14e22"); ValueTokenizer values("3.14e22");
ASSERT_DOUBLE_EQ(values.next_double(), 3.14e22); ASSERT_DOUBLE_EQ(values.next_double(), 3.14e22);
} }

View File

@ -11,215 +11,265 @@
See the README file in the top-level LAMMPS directory. See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "utils.h" #include "utils.h"
#include <string> #include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <cerrno> #include <cerrno>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <string>
using namespace LAMMPS_NS; using namespace LAMMPS_NS;
using ::testing::Eq; using ::testing::Eq;
TEST(Utils, trim_comment) { TEST(Utils, trim_comment)
{
auto trimmed = utils::trim_comment("some text # comment"); auto trimmed = utils::trim_comment("some text # comment");
ASSERT_THAT(trimmed, Eq("some text ")); ASSERT_THAT(trimmed, Eq("some text "));
} }
TEST(Utils, count_words) { TEST(Utils, count_words)
{
ASSERT_EQ(utils::count_words("some text # comment"), 4); ASSERT_EQ(utils::count_words("some text # comment"), 4);
} }
TEST(Utils, count_words_non_default) { TEST(Utils, count_words_non_default)
{
ASSERT_EQ(utils::count_words("some text # comment", " #"), 3); ASSERT_EQ(utils::count_words("some text # comment", " #"), 3);
} }
TEST(Utils, trim_and_count_words) { TEST(Utils, trim_and_count_words)
{
ASSERT_EQ(utils::trim_and_count_words("some text # comment"), 2); ASSERT_EQ(utils::trim_and_count_words("some text # comment"), 2);
} }
TEST(Utils, count_words_with_extra_spaces) { TEST(Utils, count_words_with_extra_spaces)
{
ASSERT_EQ(utils::count_words(" some text # comment "), 4); ASSERT_EQ(utils::count_words(" some text # comment "), 4);
} }
TEST(Utils, valid_integer1) { TEST(Utils, valid_integer1)
{
ASSERT_TRUE(utils::is_integer("10")); ASSERT_TRUE(utils::is_integer("10"));
} }
TEST(Utils, valid_integer2) { TEST(Utils, valid_integer2)
{
ASSERT_TRUE(utils::is_integer("-10")); ASSERT_TRUE(utils::is_integer("-10"));
} }
TEST(Utils, valid_integer3) { TEST(Utils, valid_integer3)
{
ASSERT_TRUE(utils::is_integer("+10")); ASSERT_TRUE(utils::is_integer("+10"));
} }
TEST(Utils, valid_double1) { TEST(Utils, valid_double1)
{
ASSERT_TRUE(utils::is_double("10.0")); ASSERT_TRUE(utils::is_double("10.0"));
} }
TEST(Utils, valid_double2) { TEST(Utils, valid_double2)
{
ASSERT_TRUE(utils::is_double("1.")); ASSERT_TRUE(utils::is_double("1."));
} }
TEST(Utils, valid_double3) { TEST(Utils, valid_double3)
{
ASSERT_TRUE(utils::is_double(".0")); ASSERT_TRUE(utils::is_double(".0"));
} }
TEST(Utils, valid_double4) { TEST(Utils, valid_double4)
{
ASSERT_TRUE(utils::is_double("-10.0")); ASSERT_TRUE(utils::is_double("-10.0"));
} }
TEST(Utils, valid_double5) { TEST(Utils, valid_double5)
{
ASSERT_TRUE(utils::is_double("-1.")); ASSERT_TRUE(utils::is_double("-1."));
} }
TEST(Utils, valid_double6) { TEST(Utils, valid_double6)
{
ASSERT_TRUE(utils::is_double("-.0")); ASSERT_TRUE(utils::is_double("-.0"));
} }
TEST(Utils, valid_double7) { TEST(Utils, valid_double7)
{
ASSERT_TRUE(utils::is_double("+10.0")); ASSERT_TRUE(utils::is_double("+10.0"));
} }
TEST(Utils, valid_double8) { TEST(Utils, valid_double8)
{
ASSERT_TRUE(utils::is_double("+1.")); ASSERT_TRUE(utils::is_double("+1."));
} }
TEST(Utils, valid_double9) { TEST(Utils, valid_double9)
{
ASSERT_TRUE(utils::is_double("+.0")); ASSERT_TRUE(utils::is_double("+.0"));
} }
TEST(Utils, empty_not_an_integer) { TEST(Utils, empty_not_an_integer)
{
ASSERT_FALSE(utils::is_integer("")); ASSERT_FALSE(utils::is_integer(""));
} }
TEST(Utils, empty_not_a_double) { TEST(Utils, empty_not_a_double)
{
ASSERT_FALSE(utils::is_double("")); ASSERT_FALSE(utils::is_double(""));
} }
TEST(Utils, text_not_an_integer) { TEST(Utils, text_not_an_integer)
{
ASSERT_FALSE(utils::is_integer("one")); ASSERT_FALSE(utils::is_integer("one"));
} }
TEST(Utils, text_not_a_double) { TEST(Utils, text_not_a_double)
{
ASSERT_FALSE(utils::is_double("half")); ASSERT_FALSE(utils::is_double("half"));
} }
TEST(Utils, double_not_an_integer1) { TEST(Utils, double_not_an_integer1)
{
ASSERT_FALSE(utils::is_integer("10.0")); ASSERT_FALSE(utils::is_integer("10.0"));
} }
TEST(Utils, double_not_an_integer2) { TEST(Utils, double_not_an_integer2)
{
ASSERT_FALSE(utils::is_integer(".0")); ASSERT_FALSE(utils::is_integer(".0"));
} }
TEST(Utils, double_not_an_integer3) { TEST(Utils, double_not_an_integer3)
{
ASSERT_FALSE(utils::is_integer("1.")); ASSERT_FALSE(utils::is_integer("1."));
} }
TEST(Utils, integer_is_double1) { TEST(Utils, integer_is_double1)
{
ASSERT_TRUE(utils::is_double("10")); ASSERT_TRUE(utils::is_double("10"));
} }
TEST(Utils, integer_is_double2) { TEST(Utils, integer_is_double2)
{
ASSERT_TRUE(utils::is_double("-10")); ASSERT_TRUE(utils::is_double("-10"));
} }
TEST(Utils, is_double_with_exponential) { TEST(Utils, is_double_with_exponential)
{
ASSERT_TRUE(utils::is_double("+1e02")); ASSERT_TRUE(utils::is_double("+1e02"));
} }
TEST(Utils, is_double_with_neg_exponential) { TEST(Utils, is_double_with_neg_exponential)
{
ASSERT_TRUE(utils::is_double("1.0e-22")); ASSERT_TRUE(utils::is_double("1.0e-22"));
} }
TEST(Utils, is_double_with_pos_exponential) { TEST(Utils, is_double_with_pos_exponential)
{
ASSERT_TRUE(utils::is_double(".1e+22")); ASSERT_TRUE(utils::is_double(".1e+22"));
} }
TEST(Utils, signed_double_and_exponential) { TEST(Utils, signed_double_and_exponential)
{
ASSERT_TRUE(utils::is_double("-10E-22")); ASSERT_TRUE(utils::is_double("-10E-22"));
} }
TEST(Utils, is_double_with_d_exponential) { TEST(Utils, is_double_with_d_exponential)
{
ASSERT_FALSE(utils::is_double("10d22")); ASSERT_FALSE(utils::is_double("10d22"));
} }
TEST(Utils, is_double_with_neg_d_exponential) { TEST(Utils, is_double_with_neg_d_exponential)
{
ASSERT_FALSE(utils::is_double("10d-22")); ASSERT_FALSE(utils::is_double("10d-22"));
} }
TEST(Utils, signed_double_and_d_exponential) { TEST(Utils, signed_double_and_d_exponential)
{
ASSERT_FALSE(utils::is_double("-10D-22")); ASSERT_FALSE(utils::is_double("-10D-22"));
} }
TEST(Utils, strmatch_beg) { TEST(Utils, strmatch_beg)
ASSERT_TRUE(utils::strmatch("rigid/small/omp","^rigid")); {
ASSERT_TRUE(utils::strmatch("rigid/small/omp", "^rigid"));
} }
TEST(Utils, strmatch_mid1) { TEST(Utils, strmatch_mid1)
ASSERT_TRUE(utils::strmatch("rigid/small/omp","small")); {
ASSERT_TRUE(utils::strmatch("rigid/small/omp", "small"));
} }
TEST(Utils, strmatch_mid2) { TEST(Utils, strmatch_mid2)
ASSERT_TRUE(utils::strmatch("rigid/small/omp","omp")); {
ASSERT_TRUE(utils::strmatch("rigid/small/omp", "omp"));
} }
TEST(Utils, strmatch_end) { TEST(Utils, strmatch_end)
ASSERT_TRUE(utils::strmatch("rigid/small/omp","/omp$")); {
ASSERT_TRUE(utils::strmatch("rigid/small/omp", "/omp$"));
} }
TEST(Utils, no_strmatch_beg) { TEST(Utils, no_strmatch_beg)
ASSERT_FALSE(utils::strmatch("rigid/small/omp","^small")); {
ASSERT_FALSE(utils::strmatch("rigid/small/omp", "^small"));
} }
TEST(Utils, no_strmatch_mid) { TEST(Utils, no_strmatch_mid)
ASSERT_FALSE(utils::strmatch("rigid/small/omp","none")); {
ASSERT_FALSE(utils::strmatch("rigid/small/omp", "none"));
} }
TEST(Utils, no_strmatch_end) { TEST(Utils, no_strmatch_end)
ASSERT_FALSE(utils::strmatch("rigid/small/omp","/opt$")); {
ASSERT_FALSE(utils::strmatch("rigid/small/omp", "/opt$"));
} }
TEST(Utils, strmatch_whole_line) { TEST(Utils, strmatch_whole_line)
ASSERT_TRUE(utils::strmatch("ITEM: UNITS\n","^\\s*ITEM: UNITS\\s*$")); {
ASSERT_TRUE(utils::strmatch("ITEM: UNITS\n", "^\\s*ITEM: UNITS\\s*$"));
} }
TEST(Utils, no_strmatch_whole_line) { TEST(Utils, no_strmatch_whole_line)
ASSERT_FALSE(utils::strmatch("ITEM: UNITS\n","^\\s*ITEM: UNIT\\s*$")); {
ASSERT_FALSE(utils::strmatch("ITEM: UNITS\n", "^\\s*ITEM: UNIT\\s*$"));
} }
TEST(Utils, strmatch_integer_in_line) { TEST(Utils, strmatch_integer_in_line)
ASSERT_TRUE(utils::strmatch(" 5 angles\n","^\\s*\\d+\\s+angles\\s")); {
ASSERT_TRUE(utils::strmatch(" 5 angles\n", "^\\s*\\d+\\s+angles\\s"));
} }
TEST(Utils, strmatch_float_in_line) { TEST(Utils, strmatch_float_in_line)
ASSERT_TRUE(utils::strmatch(" 5.0 angles\n","^\\s*\\f+\\s+angles\\s")); {
ASSERT_TRUE(utils::strmatch(" 5.0 angles\n", "^\\s*\\f+\\s+angles\\s"));
} }
TEST(Utils, strmatch_int_as_float_in_line) { TEST(Utils, strmatch_int_as_float_in_line)
ASSERT_TRUE(utils::strmatch(" 5 angles\n","^\\s*\\f+\\s+angles\\s")); {
ASSERT_TRUE(utils::strmatch(" 5 angles\n", "^\\s*\\f+\\s+angles\\s"));
} }
TEST(Utils, strmatch_char_range) { TEST(Utils, strmatch_char_range)
ASSERT_TRUE(utils::strmatch("rigid","^[ip-s]+gid")); {
ASSERT_TRUE(utils::strmatch("rigid", "^[ip-s]+gid"));
} }
TEST(Utils, strmatch_opt_range) { TEST(Utils, strmatch_opt_range)
ASSERT_TRUE(utils::strmatch("rigid","^[0-9]*[p-s]igid")); {
ASSERT_TRUE(utils::strmatch("rigid", "^[0-9]*[p-s]igid"));
} }
TEST(Utils, path_join) { TEST(Utils, path_join)
{
#if defined(_WIN32) #if defined(_WIN32)
ASSERT_THAT(utils::path_join("c:\\parent\\folder", "filename"), Eq("c:\\parent\\folder\\filename")); ASSERT_THAT(utils::path_join("c:\\parent\\folder", "filename"),
Eq("c:\\parent\\folder\\filename"));
#else #else
ASSERT_THAT(utils::path_join("/parent/folder", "filename"), Eq("/parent/folder/filename")); ASSERT_THAT(utils::path_join("/parent/folder", "filename"), Eq("/parent/folder/filename"));
#endif #endif
} }
TEST(Utils, path_basename) { TEST(Utils, path_basename)
{
#if defined(_WIN32) #if defined(_WIN32)
ASSERT_THAT(utils::path_basename("c:\\parent\\folder\\filename"), Eq("filename")); ASSERT_THAT(utils::path_basename("c:\\parent\\folder\\filename"), Eq("filename"));
#else #else
@ -227,7 +277,8 @@ TEST(Utils, path_basename) {
#endif #endif
} }
TEST(Utils, getsyserror) { TEST(Utils, getsyserror)
{
#if defined(__linux__) #if defined(__linux__)
errno = ENOENT; errno = ENOENT;
std::string errmesg = utils::getsyserror(); std::string errmesg = utils::getsyserror();
@ -237,24 +288,25 @@ TEST(Utils, getsyserror) {
#endif #endif
} }
TEST(Utils, potential_file) { TEST(Utils, potential_file)
{
FILE *fp; FILE *fp;
fp = fopen("ctest.txt","w"); fp = fopen("ctest.txt", "w");
ASSERT_NE(fp,nullptr); ASSERT_NE(fp, nullptr);
fputs("# DATE: 2020-02-20 CONTRIBUTOR: Nessuno\n",fp); fputs("# DATE: 2020-02-20 CONTRIBUTOR: Nessuno\n", fp);
fclose(fp); fclose(fp);
EXPECT_TRUE(utils::file_is_readable("ctest.txt")); EXPECT_TRUE(utils::file_is_readable("ctest.txt"));
EXPECT_FALSE(utils::file_is_readable("no_such_file.txt")); EXPECT_FALSE(utils::file_is_readable("no_such_file.txt"));
EXPECT_THAT(utils::get_potential_file_path("ctest.txt"),Eq("ctest.txt")); EXPECT_THAT(utils::get_potential_file_path("ctest.txt"), Eq("ctest.txt"));
const char *folder = getenv("LAMMPS_POTENTIALS"); const char *folder = getenv("LAMMPS_POTENTIALS");
if (folder != nullptr) { if (folder != nullptr) {
std::string path=utils::path_join(folder,"Cu_u3.eam"); std::string path = utils::path_join(folder, "Cu_u3.eam");
EXPECT_THAT(utils::get_potential_file_path("Cu_u3.eam"),Eq(path)); EXPECT_THAT(utils::get_potential_file_path("Cu_u3.eam"), Eq(path));
} }
EXPECT_THAT(utils::get_potential_date("ctest.txt","Test"),Eq("2020-02-20")); EXPECT_THAT(utils::get_potential_date("ctest.txt", "Test"), Eq("2020-02-20"));
remove("ctest.txt"); remove("ctest.txt");
} }