refactor catching exceptions during LAMMPS initialization
this will avoid the nasty segfaults with "cannot have multiple stdout capturers" instead it will catch and display any exception thrown during init.
This commit is contained in:
@ -62,7 +62,7 @@ void cleanup_lammps(LAMMPS *lmp, const TestConfig &cfg)
|
||||
delete lmp;
|
||||
}
|
||||
|
||||
LAMMPS *init_lammps(LAMMPS::argv &args, const TestConfig &cfg, const bool newton = true)
|
||||
LAMMPS *init_lammps(LAMMPS::argv &args, const TestConfig &cfg, const bool newton)
|
||||
{
|
||||
LAMMPS *lmp;
|
||||
|
||||
@ -93,21 +93,7 @@ LAMMPS *init_lammps(LAMMPS::argv &args, const TestConfig &cfg, const bool newton
|
||||
|
||||
// utility lambdas to improve readability
|
||||
auto command = [&](const std::string &line) {
|
||||
try {
|
||||
lmp->input->one(line);
|
||||
} catch (LAMMPSAbortException &ae) {
|
||||
fprintf(stderr, "LAMMPS Error: %s\n", ae.what());
|
||||
exit(2);
|
||||
} catch (LAMMPSException &e) {
|
||||
fprintf(stderr, "LAMMPS Error: %s\n", e.what());
|
||||
exit(3);
|
||||
} catch (fmt::format_error &fe) {
|
||||
fprintf(stderr, "fmt::format_error: %s\n", fe.what());
|
||||
exit(4);
|
||||
} catch (std::exception &e) {
|
||||
fprintf(stderr, "General exception: %s\n", e.what());
|
||||
exit(5);
|
||||
}
|
||||
lmp->input->one(line);
|
||||
};
|
||||
|
||||
auto parse_input_script = [&](const std::string &filename) {
|
||||
@ -242,8 +228,12 @@ void generate_yaml_file(const char *outfile, const TestConfig &config)
|
||||
{
|
||||
// initialize system geometry
|
||||
LAMMPS::argv args = {"PairStyle", "-log", "none", "-echo", "screen", "-nocite"};
|
||||
|
||||
LAMMPS *lmp = init_lammps(args, config);
|
||||
LAMMPS *lmp = nullptr;
|
||||
try {
|
||||
lmp = init_lammps(args, config, true);
|
||||
} catch (std::exception &e) {
|
||||
FAIL() << e.what();
|
||||
}
|
||||
if (!lmp) {
|
||||
std::cerr << "One or more prerequisite styles are not available "
|
||||
"in this LAMMPS configuration:\n";
|
||||
@ -340,8 +330,14 @@ TEST(PairStyle, plain)
|
||||
LAMMPS::argv args = {"PairStyle", "-log", "none", "-echo", "screen", "-nocite"};
|
||||
|
||||
::testing::internal::CaptureStdout();
|
||||
LAMMPS *lmp = init_lammps(args, test_config, true);
|
||||
|
||||
LAMMPS *lmp = nullptr;
|
||||
try {
|
||||
lmp = init_lammps(args, test_config, true);
|
||||
} catch (std::exception &e) {
|
||||
std::string output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
FAIL() << e.what();
|
||||
}
|
||||
std::string output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
|
||||
@ -399,7 +395,12 @@ TEST(PairStyle, plain)
|
||||
|
||||
if (!verbose) ::testing::internal::CaptureStdout();
|
||||
cleanup_lammps(lmp, test_config);
|
||||
lmp = init_lammps(args, test_config, false);
|
||||
try {
|
||||
lmp = init_lammps(args, test_config, false);
|
||||
} catch (std::exception &e) {
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
FAIL() << e.what();
|
||||
}
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
|
||||
// skip over these tests if newton pair is forced to be on
|
||||
@ -480,9 +481,14 @@ TEST(PairStyle, plain)
|
||||
if (pair->respa_enable) {
|
||||
if (!verbose) ::testing::internal::CaptureStdout();
|
||||
cleanup_lammps(lmp, test_config);
|
||||
lmp = init_lammps(args, test_config, false);
|
||||
lmp->input->one("run_style respa 2 1 inner 1 4.8 5.5 outer 2");
|
||||
run_lammps(lmp);
|
||||
try {
|
||||
lmp = init_lammps(args, test_config, false);
|
||||
lmp->input->one("run_style respa 2 1 inner 1 4.8 5.5 outer 2");
|
||||
run_lammps(lmp);
|
||||
} catch (std::exception &e) {
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
FAIL() << e.what();
|
||||
}
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
|
||||
// need to relax error by a large amount with tabulation, since
|
||||
@ -519,8 +525,14 @@ TEST(PairStyle, omp)
|
||||
if (utils::strmatch(test_config.pair_style, "^dpd")) args[8] = "1";
|
||||
|
||||
::testing::internal::CaptureStdout();
|
||||
LAMMPS *lmp = init_lammps(args, test_config, true);
|
||||
|
||||
LAMMPS *lmp = nullptr;
|
||||
try {
|
||||
lmp = init_lammps(args, test_config, true);
|
||||
} catch (std::exception &e) {
|
||||
std::string output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
FAIL() << e.what();
|
||||
}
|
||||
std::string output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
|
||||
@ -578,7 +590,12 @@ TEST(PairStyle, omp)
|
||||
|
||||
if (!verbose) ::testing::internal::CaptureStdout();
|
||||
cleanup_lammps(lmp, test_config);
|
||||
lmp = init_lammps(args, test_config, false);
|
||||
try {
|
||||
lmp = init_lammps(args, test_config, false);
|
||||
} catch (std::exception &e) {
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
FAIL() << e.what();
|
||||
}
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
|
||||
pair = lmp->force->pair;
|
||||
@ -636,7 +653,9 @@ TEST(PairStyle, kokkos_omp)
|
||||
// if KOKKOS has GPU support enabled, it *must* be used. We cannot test OpenMP only.
|
||||
if (Info::has_accelerator_feature("KOKKOS", "api", "cuda") ||
|
||||
Info::has_accelerator_feature("KOKKOS", "api", "hip") ||
|
||||
Info::has_accelerator_feature("KOKKOS", "api", "sycl")) GTEST_SKIP();
|
||||
Info::has_accelerator_feature("KOKKOS", "api", "sycl")) {
|
||||
GTEST_SKIP() << "Cannot test KOKKOS/OpenMP with GPU support enabled";
|
||||
}
|
||||
|
||||
LAMMPS::argv args = {"PairStyle", "-log", "none", "-echo", "screen", "-nocite",
|
||||
"-k", "on", "t", "4", "-sf", "kk"};
|
||||
@ -655,8 +674,14 @@ TEST(PairStyle, kokkos_omp)
|
||||
args[9] = "1";
|
||||
|
||||
::testing::internal::CaptureStdout();
|
||||
LAMMPS *lmp = init_lammps(args, test_config, true);
|
||||
|
||||
LAMMPS *lmp = nullptr;
|
||||
try {
|
||||
lmp = init_lammps(args, test_config, true);
|
||||
} catch (std::exception &e) {
|
||||
std::string output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
FAIL() << e.what();
|
||||
}
|
||||
std::string output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
|
||||
@ -713,7 +738,12 @@ TEST(PairStyle, kokkos_omp)
|
||||
if (lmp->force->newton_pair == 0) {
|
||||
if (!verbose) ::testing::internal::CaptureStdout();
|
||||
cleanup_lammps(lmp, test_config);
|
||||
lmp = init_lammps(args, test_config, false);
|
||||
try {
|
||||
lmp = init_lammps(args, test_config, false);
|
||||
} catch (std::exception &e) {
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
FAIL() << e.what();
|
||||
}
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
|
||||
pair = lmp->force->pair;
|
||||
@ -788,8 +818,14 @@ TEST(PairStyle, gpu)
|
||||
}
|
||||
|
||||
::testing::internal::CaptureStdout();
|
||||
LAMMPS *lmp = init_lammps(args, test_config, false);
|
||||
|
||||
LAMMPS *lmp = nullptr;
|
||||
try {
|
||||
lmp = init_lammps(args, test_config, false);
|
||||
} catch (std::exception &e) {
|
||||
std::string output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
FAIL() << e.what();
|
||||
}
|
||||
std::string output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
|
||||
@ -868,8 +904,14 @@ TEST(PairStyle, intel)
|
||||
if (utils::strmatch(test_config.pair_style, "^dpd")) args[12] = "1";
|
||||
|
||||
::testing::internal::CaptureStdout();
|
||||
LAMMPS *lmp = init_lammps(args, test_config, true);
|
||||
|
||||
LAMMPS *lmp = nullptr;
|
||||
try {
|
||||
lmp = init_lammps(args, test_config, true);
|
||||
} catch (std::exception &e) {
|
||||
std::string output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
FAIL() << e.what();
|
||||
}
|
||||
std::string output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
|
||||
@ -948,8 +990,14 @@ TEST(PairStyle, opt)
|
||||
LAMMPS::argv args = {"PairStyle", "-log", "none", "-echo", "screen", "-nocite", "-sf", "opt"};
|
||||
|
||||
::testing::internal::CaptureStdout();
|
||||
LAMMPS *lmp = init_lammps(args, test_config);
|
||||
|
||||
LAMMPS *lmp = nullptr;
|
||||
try {
|
||||
lmp = init_lammps(args, test_config, true);
|
||||
} catch (std::exception &e) {
|
||||
std::string output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
FAIL() << e.what();
|
||||
}
|
||||
std::string output = ::testing::internal::GetCapturedStdout();
|
||||
if (verbose) std::cout << output;
|
||||
|
||||
@ -1032,7 +1080,13 @@ TEST(PairStyle, single)
|
||||
|
||||
// create a LAMMPS instance with standard settings to detect the number of atom types
|
||||
if (!verbose) ::testing::internal::CaptureStdout();
|
||||
LAMMPS *lmp = init_lammps(args, test_config);
|
||||
LAMMPS *lmp = nullptr;
|
||||
try {
|
||||
lmp = init_lammps(args, test_config, true);
|
||||
} catch (std::exception &e) {
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
FAIL() << e.what();
|
||||
}
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
|
||||
if (!lmp) {
|
||||
@ -1276,7 +1330,13 @@ TEST(PairStyle, extract)
|
||||
LAMMPS::argv args = {"PairStyle", "-log", "none", "-echo", "screen", "-nocite"};
|
||||
|
||||
if (!verbose) ::testing::internal::CaptureStdout();
|
||||
LAMMPS *lmp = init_lammps(args, test_config, true);
|
||||
LAMMPS *lmp = nullptr;
|
||||
try {
|
||||
lmp = init_lammps(args, test_config, true);
|
||||
} catch (std::exception &e) {
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
FAIL() << e.what();
|
||||
}
|
||||
if (!verbose) ::testing::internal::GetCapturedStdout();
|
||||
|
||||
if (!lmp) {
|
||||
|
||||
Reference in New Issue
Block a user