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

@ -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);