test for exceptions add example

This commit is contained in:
Axel Kohlmeyer
2020-09-14 17:01:28 -04:00
parent 4185608e92
commit a1b2f82107
4 changed files with 50 additions and 6 deletions

View File

@ -1,9 +1,41 @@
Retrieving LAMMPS configuration information Retrieving LAMMPS configuration information
=========================================== ===========================================
The following library functions can be used to query the The following library functions can be used to query the LAMMPS library
LAMMPS library about compile time settings and included about compile time settings and included packages and styles. This
packages and styles. 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 <exceptions>`, avoiding them proactively is
a safer approach.
.. code-block:: C
:caption: Handle errors using a LAMMPS library with exceptions
#include "library.h"
#include <stdio.h>
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;
}
----------------------- -----------------------

View File

@ -4266,10 +4266,10 @@ the failing MPI ranks to send messages.
* \param buf_size size of the provided string buffer * \param buf_size size of the provided string buffer
* \return 1 when all ranks had the error, 1 on a single rank error. * \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 #ifdef LAMMPS_EXCEPTIONS
LAMMPS * lmp = (LAMMPS *) handle; LAMMPS *lmp = (LAMMPS *) handle;
Error * error = lmp->error; Error *error = lmp->error;
if(!error->get_last_error().empty()) { if(!error->get_last_error().empty()) {
int error_type = error->get_last_error_type(); int error_type = error->get_last_error_type();

View File

@ -35,6 +35,13 @@ else()
endif() endif()
list(APPEND TEST_CONFIG_DEFS -DLAMMPS_HAS_MPI=${HAS_MPI}) 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") foreach(WITH "JPEG" "PNG" "GZIP" "FFMPEG")
if(WITH_${WITH}) if(WITH_${WITH})
set(HAS_${WITH} 1) set(HAS_${WITH} 1)

View File

@ -118,6 +118,11 @@ TEST_F(LibraryConfig, style_name)
EXPECT_THAT(buf, StrEq("")); EXPECT_THAT(buf, StrEq(""));
}; };
TEST(LAMMPSConfig, exceptions)
{
EXPECT_EQ(lammps_config_has_exceptions(), LAMMPS_HAS_EXCEPTIONS);
};
TEST(LAMMPSConfig, mpi_support) TEST(LAMMPSConfig, mpi_support)
{ {
EXPECT_EQ(lammps_config_has_mpi_support(), LAMMPS_HAS_MPI); EXPECT_EQ(lammps_config_has_mpi_support(), LAMMPS_HAS_MPI);