diff --git a/unittest/force-styles/test_angle_style.cpp b/unittest/force-styles/test_angle_style.cpp index 91f0f02dec..e24e5401df 100644 --- a/unittest/force-styles/test_angle_style.cpp +++ b/unittest/force-styles/test_angle_style.cpp @@ -62,7 +62,7 @@ void cleanup_lammps(LAMMPS *&lmp, const TestConfig &cfg) lmp = nullptr; } -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) { lmp->input->file(filename.c_str()); @@ -231,7 +217,12 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) // initialize system geometry LAMMPS::argv args = {"AngleStyle", "-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"; @@ -322,8 +313,14 @@ TEST(AngleStyle, plain) LAMMPS::argv args = {"AngleStyle", "-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; @@ -372,7 +369,12 @@ TEST(AngleStyle, 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 bond is forced to be on @@ -440,8 +442,14 @@ TEST(AngleStyle, omp) "-pk", "omp", "4", "-sf", "omp"}; ::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; @@ -494,7 +502,12 @@ TEST(AngleStyle, 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(); // skip over these tests if newton bond is forced to be on @@ -538,13 +551,24 @@ TEST(AngleStyle, kokkos_omp) if (!LAMMPS::is_installed_pkg("KOKKOS")) GTEST_SKIP(); if (test_config.skip_tests.count(test_info_->name())) GTEST_SKIP(); if (!Info::has_accelerator_feature("KOKKOS", "api", "openmp")) GTEST_SKIP(); + // 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() << "Cannot test KOKKOS/OpenMP with GPU support enabled"; LAMMPS::argv args = {"AngleStyle", "-log", "none", "-echo", "screen", "-nocite", "-k", "on", "t", "4", "-sf", "kk"}; ::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; @@ -594,7 +618,12 @@ TEST(AngleStyle, kokkos_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(); // skip over these tests if newton bond is forced to be on @@ -661,8 +690,14 @@ TEST(AngleStyle, numdiff) LAMMPS::argv args = {"AngleStyle", "-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; @@ -714,7 +749,13 @@ TEST(AngleStyle, 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) { @@ -862,7 +903,13 @@ TEST(AngleStyle, extract) LAMMPS::argv args = {"AngleStyle", "-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) { diff --git a/unittest/force-styles/test_bond_style.cpp b/unittest/force-styles/test_bond_style.cpp index 829c927569..660435cf49 100644 --- a/unittest/force-styles/test_bond_style.cpp +++ b/unittest/force-styles/test_bond_style.cpp @@ -62,7 +62,7 @@ void cleanup_lammps(LAMMPS *&lmp, const TestConfig &cfg) lmp = nullptr; } -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) { lmp->input->file(filename.c_str()); @@ -231,7 +217,13 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) // initialize system geometry LAMMPS::argv args = {"BondStyle", "-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"; @@ -322,8 +314,14 @@ TEST(BondStyle, plain) LAMMPS::argv args = {"BondStyle", "-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; @@ -372,7 +370,12 @@ TEST(BondStyle, 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 bond is forced to be on @@ -442,8 +445,14 @@ TEST(BondStyle, omp) "-pk", "omp", "4", "-sf", "omp"}; ::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; @@ -496,7 +505,12 @@ TEST(BondStyle, 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(); // skip over these tests if newton bond is forced to be on @@ -540,13 +554,24 @@ TEST(BondStyle, kokkos_omp) if (!LAMMPS::is_installed_pkg("KOKKOS")) GTEST_SKIP(); if (test_config.skip_tests.count(test_info_->name())) GTEST_SKIP(); if (!Info::has_accelerator_feature("KOKKOS", "api", "openmp")) GTEST_SKIP(); + // 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() << "Cannot test KOKKOS/OpenMP with GPU support enabled"; LAMMPS::argv args = {"BondStyle", "-log", "none", "-echo", "screen", "-nocite", "-k", "on", "t", "4", "-sf", "kk"}; ::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; @@ -600,7 +625,12 @@ TEST(BondStyle, kokkos_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(); // skip over these tests if newton bond is forced to be on @@ -649,8 +679,14 @@ TEST(BondStyle, numdiff) LAMMPS::argv args = {"BondStyle", "-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; @@ -702,7 +738,13 @@ TEST(BondStyle, 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) { @@ -956,7 +998,13 @@ TEST(BondStyle, extract) LAMMPS::argv args = {"BondStyle", "-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) { diff --git a/unittest/force-styles/test_dihedral_style.cpp b/unittest/force-styles/test_dihedral_style.cpp index 7bc608c29f..941eaad1c4 100644 --- a/unittest/force-styles/test_dihedral_style.cpp +++ b/unittest/force-styles/test_dihedral_style.cpp @@ -62,7 +62,7 @@ void cleanup_lammps(LAMMPS *&lmp, const TestConfig &cfg) lmp = nullptr; } -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) { auto *lmp = new LAMMPS(args, MPI_COMM_WORLD); @@ -238,7 +238,13 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) // initialize system geometry LAMMPS::argv args = {"DihedralStyle", "-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"; @@ -323,8 +329,14 @@ TEST(DihedralStyle, plain) LAMMPS::argv args = {"DihedralStyle", "-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; @@ -373,7 +385,12 @@ TEST(DihedralStyle, 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 bond is forced to be on @@ -443,8 +460,14 @@ TEST(DihedralStyle, omp) "-pk", "omp", "4", "-sf", "omp"}; ::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; @@ -498,7 +521,12 @@ TEST(DihedralStyle, 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(); // skip over these tests if newton bond is forced to be on @@ -542,13 +570,24 @@ TEST(DihedralStyle, kokkos_omp) if (!LAMMPS::is_installed_pkg("KOKKOS")) GTEST_SKIP(); if (test_config.skip_tests.count(test_info_->name())) GTEST_SKIP(); if (!Info::has_accelerator_feature("KOKKOS", "api", "openmp")) GTEST_SKIP(); + // 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() << "Cannot test KOKKOS/OpenMP with GPU support enabled"; LAMMPS::argv args = {"DihedralStyle", "-log", "none", "-echo", "screen", "-nocite", "-k", "on", "t", "4", "-sf", "kk"}; ::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; @@ -602,7 +641,12 @@ TEST(DihedralStyle, kokkos_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(); // skip over these tests if newton bond is forced to be on @@ -651,8 +695,14 @@ TEST(DihedralStyle, numdiff) LAMMPS::argv args = {"DihedralStyle", "-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; diff --git a/unittest/force-styles/test_fix_timestep.cpp b/unittest/force-styles/test_fix_timestep.cpp index ad668989fa..ec2dd4f144 100644 --- a/unittest/force-styles/test_fix_timestep.cpp +++ b/unittest/force-styles/test_fix_timestep.cpp @@ -63,7 +63,7 @@ void cleanup_lammps(LAMMPS *&lmp, const TestConfig &cfg) lmp = nullptr; } -LAMMPS *init_lammps(LAMMPS::argv &args, const TestConfig &cfg, const bool use_respa = false) +LAMMPS *init_lammps(LAMMPS::argv &args, const TestConfig &cfg, const bool use_respa) { LAMMPS *lmp; @@ -179,7 +179,12 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) { // initialize system geometry LAMMPS::argv args = {"FixIntegrate", "-log", "none", "-echo", "screen", "-nocite"}; - LAMMPS *lmp = init_lammps(args, config); + LAMMPS *lmp = nullptr; + try { + lmp = init_lammps(args, config, false); + } catch (std::exception &e) { + FAIL() << e.what(); + } if (!lmp) { std::cerr << "One or more prerequisite styles are not available " "in this LAMMPS configuration:\n"; @@ -259,7 +264,14 @@ TEST(FixTimestep, plain) LAMMPS::argv args = {"FixTimestep", "-log", "none", "-echo", "screen", "-nocite"}; ::testing::internal::CaptureStdout(); - LAMMPS *lmp = init_lammps(args, test_config); + 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; @@ -412,13 +424,20 @@ TEST(FixTimestep, plain) // fix nve/limit cannot work with r-RESPA ifix = lmp->modify->get_fix_by_id("test"); if (ifix && !utils::strmatch(ifix->style, "^rigid") && - !utils::strmatch(ifix->style, "^nve/limit")) { + !utils::strmatch(ifix->style, "^nve/limit") && !utils::strmatch(ifix->style, "^recenter")) { if (!verbose) ::testing::internal::CaptureStdout(); cleanup_lammps(lmp, test_config); if (!verbose) ::testing::internal::GetCapturedStdout(); ::testing::internal::CaptureStdout(); - lmp = init_lammps(args, test_config, true); + LAMMPS *lmp = nullptr; + try { + lmp = init_lammps(args, test_config, true); + } catch (std::exception &e) { + output = ::testing::internal::GetCapturedStdout(); + if (verbose) std::cout << output; + FAIL() << e.what(); + } output = ::testing::internal::GetCapturedStdout(); if (verbose) std::cout << output; @@ -553,15 +572,26 @@ TEST(FixTimestep, omp) "-pk", "omp", "4", "-sf", "omp"}; ::testing::internal::CaptureStdout(); - LAMMPS *lmp = init_lammps(args, test_config); + 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; if (!lmp) { - std::cerr << "One or more prerequisite styles are not available " + std::cerr << "One or more prerequisite styles with /omp suffix are not available " "in this LAMMPS configuration:\n"; for (auto &prerequisite : test_config.prerequisites) { - std::cerr << prerequisite.first << "_style " << prerequisite.second << "\n"; + if (prerequisite.first == "atom") { + std::cerr << prerequisite.first << "_style " << prerequisite.second << "\n"; + } else { + std::cerr << prerequisite.first << "_style " << prerequisite.second << "/omp\n"; + } } GTEST_SKIP(); } @@ -712,7 +742,13 @@ TEST(FixTimestep, omp) if (!verbose) ::testing::internal::GetCapturedStdout(); ::testing::internal::CaptureStdout(); - lmp = init_lammps(args, test_config, true); + try { + lmp = init_lammps(args, test_config, true); + } catch (std::exception &e) { + output = ::testing::internal::GetCapturedStdout(); + if (verbose) std::cout << output; + FAIL() << e.what(); + } output = ::testing::internal::GetCapturedStdout(); if (verbose) std::cout << output; diff --git a/unittest/force-styles/test_improper_style.cpp b/unittest/force-styles/test_improper_style.cpp index 76ede3a109..e272033e85 100644 --- a/unittest/force-styles/test_improper_style.cpp +++ b/unittest/force-styles/test_improper_style.cpp @@ -62,7 +62,7 @@ void cleanup_lammps(LAMMPS *&lmp, const TestConfig &cfg) lmp = nullptr; } -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) { lmp->input->file(filename.c_str()); @@ -231,7 +217,12 @@ void generate_yaml_file(const char *outfile, const TestConfig &config) // initialize system geometry LAMMPS::argv args = {"ImproperStyle", "-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"; @@ -316,8 +307,14 @@ TEST(ImproperStyle, plain) LAMMPS::argv args = {"ImproperStyle", "-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; @@ -366,7 +363,12 @@ TEST(ImproperStyle, 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 bond is forced to be on @@ -436,8 +438,14 @@ TEST(ImproperStyle, omp) "-pk", "omp", "4", "-sf", "omp"}; ::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; @@ -491,7 +499,12 @@ TEST(ImproperStyle, 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(); // skip over these tests if newton bond is forced to be on @@ -534,14 +547,25 @@ TEST(ImproperStyle, kokkos_omp) { if (!LAMMPS::is_installed_pkg("KOKKOS")) GTEST_SKIP(); if (test_config.skip_tests.count(test_info_->name())) GTEST_SKIP(); + // 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() << "Cannot test KOKKOS/OpenMP with GPU support enabled"; LAMMPS::argv args = {"ImproperStyle", "-log", "none", "-echo", "screen", "-nocite", "-k", "on", "t", "4", "-sf", "kk"}; ::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; @@ -594,7 +618,12 @@ TEST(ImproperStyle, kokkos_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(); // skip over these tests if newton bond is forced to be on @@ -641,8 +670,14 @@ TEST(ImproperStyle, numdiff) LAMMPS::argv args = {"ImproperStyle", "-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; diff --git a/unittest/force-styles/test_pair_style.cpp b/unittest/force-styles/test_pair_style.cpp index e7be72f1b4..6dabeab7a4 100644 --- a/unittest/force-styles/test_pair_style.cpp +++ b/unittest/force-styles/test_pair_style.cpp @@ -63,7 +63,7 @@ void cleanup_lammps(LAMMPS *&lmp, const TestConfig &cfg) lmp = nullptr; } -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; @@ -94,21 +94,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) { @@ -243,8 +229,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"; @@ -341,8 +331,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; @@ -400,7 +396,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 @@ -481,9 +482,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 @@ -520,8 +526,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; @@ -579,7 +591,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; @@ -634,6 +651,12 @@ TEST(PairStyle, kokkos_omp) if (!LAMMPS::is_installed_pkg("KOKKOS")) GTEST_SKIP(); if (test_config.skip_tests.count(test_info_->name())) GTEST_SKIP(); if (!Info::has_accelerator_feature("KOKKOS", "api", "openmp")) GTEST_SKIP(); + // 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() << "Cannot test KOKKOS/OpenMP with GPU support enabled"; + } LAMMPS::argv args = {"PairStyle", "-log", "none", "-echo", "screen", "-nocite", "-k", "on", "t", "4", "-sf", "kk"}; @@ -652,8 +675,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; @@ -710,7 +739,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; @@ -785,8 +819,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; @@ -865,8 +905,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; @@ -945,8 +991,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; @@ -1029,7 +1081,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) { @@ -1273,7 +1331,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) {