From 9353004e3e7e34346db9c21d15b0d1478fce6c0b Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Sun, 4 Oct 2020 11:23:49 -0400 Subject: [PATCH] add APIs to determine of LAMMPS is running and to interrupt a run cleanly --- src/library.cpp | 28 ++++++++++++++++++++++++++++ src/library.h | 3 +++ 2 files changed, 31 insertions(+) diff --git a/src/library.cpp b/src/library.cpp index d8ec849f49..ebbebff2c1 100644 --- a/src/library.cpp +++ b/src/library.cpp @@ -38,6 +38,7 @@ #include "region.h" #include "output.h" #include "thermo.h" +#include "timer.h" #include "universe.h" #include "update.h" #include "variable.h" @@ -4619,6 +4620,33 @@ void lammps_decode_image_flags(imageint image, int *flags) flags[2] = (image >> IMG2BITS) - IMGMAX; } +/** Check if LAMMPS is currently inside a run or minimization + * + * This function can be used from signal handlers or multi-threaded + * applications to determine if the LAMMPS instance is currently active. + * + * \param handle pointer to a previously created LAMMPS instance cast to ``void *``. + * \return 0 if idle or >0 if active */ + +int lammps_is_running(void *handle) +{ + LAMMPS * lmp = (LAMMPS *) handle; + return lmp->update->whichflag; +} + +/** Force a timeout to cleanly stop an ongoing run + * + * This function can be used from signal handlers or multi-threaded + * applications to cleanly terminate an ongoing run. + * + * \param handle pointer to a previously created LAMMPS instance cast to ``void *`` */ + +void lammps_force_timeout(void *handle) +{ + LAMMPS * lmp = (LAMMPS *) handle; + return lmp->timer->force_timeout(); +} + // ---------------------------------------------------------------------- // Library functions for error handling with exceptions enabled // ---------------------------------------------------------------------- diff --git a/src/library.h b/src/library.h index 2ac0997487..c365bd9210 100644 --- a/src/library.h +++ b/src/library.h @@ -226,6 +226,9 @@ void lammps_set_fix_external_callback(void *, char *, FixExternalFnPtr, void*); void lammps_fix_external_set_energy_global(void *, char *, double); void lammps_fix_external_set_virial_global(void *, char *, double *); +int lammps_is_running(void *handle); +void lammps_force_timeout(void *handle); + int lammps_has_error(void *handle); int lammps_get_last_error_message(void *handle, char *buffer, int buf_size);