make consistent and avoid segfaults

This commit is contained in:
Axel Kohlmeyer
2023-08-14 21:48:10 -04:00
parent e51845776d
commit f0962f36f6

View File

@ -23,14 +23,14 @@ LammpsWrapper::LammpsWrapper() : lammps_handle(nullptr), plugin_handle(nullptr)
void LammpsWrapper::open(int narg, char **args) void LammpsWrapper::open(int narg, char **args)
{ {
if (!lammps_handle) { // since there may only be one LAMMPS instance in LAMMPS GUI we don't open a second
if (lammps_handle) return;
#if defined(LAMMPS_GUI_USE_PLUGIN) #if defined(LAMMPS_GUI_USE_PLUGIN)
lammps_handle = ((liblammpsplugin_t *)plugin_handle)->open_no_mpi(narg, args, nullptr); lammps_handle = ((liblammpsplugin_t *)plugin_handle)->open_no_mpi(narg, args, nullptr);
#else #else
lammps_handle = lammps_open_no_mpi(narg, args, nullptr); lammps_handle = lammps_open_no_mpi(narg, args, nullptr);
#endif #endif
} }
}
int LammpsWrapper::extract_setting(const char *keyword) int LammpsWrapper::extract_setting(const char *keyword)
{ {
@ -58,15 +58,14 @@ int LammpsWrapper::id_count(const char *keyword)
return val; return val;
} }
int LammpsWrapper::id_name(const char *keyword, int idx, char *buf, int buflen) int LammpsWrapper::id_name(const char *keyword, int idx, char *buf, int len)
{ {
int val = 0; int val = 0;
if (lammps_handle) { if (lammps_handle) {
#if defined(LAMMPS_GUI_USE_PLUGIN) #if defined(LAMMPS_GUI_USE_PLUGIN)
val = val = ((liblammpsplugin_t *)plugin_handle)->id_name(lammps_handle, keyword, idx, buf, len);
((liblammpsplugin_t *)plugin_handle)->id_name(lammps_handle, keyword, idx, buf, buflen);
#else #else
val = lammps_id_name(lammps_handle, keyword, idx, buf, buflen); val = lammps_id_name(lammps_handle, keyword, idx, buf, len);
#endif #endif
} }
return val; return val;
@ -143,6 +142,7 @@ bool LammpsWrapper::has_error() const
#endif #endif
} }
// may be called with null handle. returns global error then.
int LammpsWrapper::get_last_error_message(char *buf, int buflen) int LammpsWrapper::get_last_error_message(char *buf, int buflen)
{ {
#if defined(LAMMPS_GUI_USE_PLUGIN) #if defined(LAMMPS_GUI_USE_PLUGIN)
@ -155,9 +155,9 @@ int LammpsWrapper::get_last_error_message(char *buf, int buflen)
void LammpsWrapper::force_timeout() void LammpsWrapper::force_timeout()
{ {
#if defined(LAMMPS_GUI_USE_PLUGIN) #if defined(LAMMPS_GUI_USE_PLUGIN)
((liblammpsplugin_t *)plugin_handle)->force_timeout(lammps_handle); if (lammps_handle) ((liblammpsplugin_t *)plugin_handle)->force_timeout(lammps_handle);
#else #else
lammps_force_timeout(lammps_handle); if (lammps_handle) lammps_force_timeout(lammps_handle);
#endif #endif
} }