From a1b2f821079a515ea8d35a4bc1193910abd136dc Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Mon, 14 Sep 2020 17:01:28 -0400 Subject: [PATCH] test for exceptions add example --- doc/src/pg_lib_config.rst | 38 ++++++++++++++++++++-- src/library.cpp | 6 ++-- unittest/c-library/CMakeLists.txt | 7 ++++ unittest/c-library/test_library_config.cpp | 5 +++ 4 files changed, 50 insertions(+), 6 deletions(-) diff --git a/doc/src/pg_lib_config.rst b/doc/src/pg_lib_config.rst index 9576e31dcd..f0ff7cbc43 100644 --- a/doc/src/pg_lib_config.rst +++ b/doc/src/pg_lib_config.rst @@ -1,9 +1,41 @@ Retrieving LAMMPS configuration information =========================================== -The following library functions can be used to query the -LAMMPS library about compile time settings and included -packages and styles. +The following library functions can be used to query the LAMMPS library +about compile time settings and included packages and styles. This +enables programs that use the library interface to run LAMMPS +simulations to determine, whether the linked LAMMPS library is compatible +with the requirements of the application without crashing during the +LAMMPS functions (e.g. due to missing pair styles from packages) or to +choose between different options (e.g. whether to use ``lj/cut``, +``lj/cut/opt``, ``lj/cut/omp`` or ``lj/cut/intel``). Most of the +functions can be called directly without first creating a LAMMPS +instance. While crashes within LAMMPS may be recovered from through +enabling :ref:`exceptions `, avoiding them proactively is +a safer approach. + +.. code-block:: C + :caption: Handle errors using a LAMMPS library with exceptions + + #include "library.h" + #include + + int main(int argc, char **argv) + { + void *handle; + + handle = lammps_open_no_mpi(0, NULL, NULL); + lammps_file(handle, "in.missing"); + if (lammps_has_error(handle)) { + char errmsg[256]; + int errtype; + errtype = lammps_get_last_error_message(handle, errmsg, 256); + fprintf(stderr, "LAMMPS failed with error: %s\n", errmsg); + return 1; + } + lammps_close(handle); + return 0; + } ----------------------- diff --git a/src/library.cpp b/src/library.cpp index 98bd99ee77..4a1c596fa4 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -4266,10 +4266,10 @@ the failing MPI ranks to send messages. * \param buf_size size of the provided string buffer * \return 1 when all ranks had the error, 1 on a single rank error. */ -int lammps_get_last_error_message(void *handle, char * buffer, int buf_size) { +int lammps_get_last_error_message(void *handle, char *buffer, int buf_size) { #ifdef LAMMPS_EXCEPTIONS - LAMMPS * lmp = (LAMMPS *) handle; - Error * error = lmp->error; + LAMMPS *lmp = (LAMMPS *) handle; + Error *error = lmp->error; if(!error->get_last_error().empty()) { int error_type = error->get_last_error_type(); diff --git a/unittest/c-library/CMakeLists.txt b/unittest/c-library/CMakeLists.txt index c0af60099e..ee4916427a 100644 --- a/unittest/c-library/CMakeLists.txt +++ b/unittest/c-library/CMakeLists.txt @@ -35,6 +35,13 @@ else() endif() list(APPEND TEST_CONFIG_DEFS -DLAMMPS_HAS_MPI=${HAS_MPI}) +if(LAMMPS_EXCEPTION) + set(HAS_EXCEPTIONS 1) +else() + set(HAS_EXCEPTIONS 0) +endif() +list(APPEND TEST_CONFIG_DEFS -DLAMMPS_HAS_EXCEPTIONS=${HAS_EXCEPTIONS}) + foreach(WITH "JPEG" "PNG" "GZIP" "FFMPEG") if(WITH_${WITH}) set(HAS_${WITH} 1) diff --git a/unittest/c-library/test_library_config.cpp b/unittest/c-library/test_library_config.cpp index 064ce83836..0e81683159 100644 --- a/unittest/c-library/test_library_config.cpp +++ b/unittest/c-library/test_library_config.cpp @@ -118,6 +118,11 @@ TEST_F(LibraryConfig, style_name) EXPECT_THAT(buf, StrEq("")); }; +TEST(LAMMPSConfig, exceptions) +{ + EXPECT_EQ(lammps_config_has_exceptions(), LAMMPS_HAS_EXCEPTIONS); +}; + TEST(LAMMPSConfig, mpi_support) { EXPECT_EQ(lammps_config_has_mpi_support(), LAMMPS_HAS_MPI);