update prototypes and struct entries for liblammpsplugin. port to windows.

This commit is contained in:
Axel Kohlmeyer
2022-10-28 21:13:17 -04:00
parent e68b0b442e
commit 38335a4d47
2 changed files with 147 additions and 14 deletions

View File

@ -18,10 +18,29 @@
a LAMMPS plugin to some other software.
*/
#include "library.h"
#include "liblammpsplugin.h"
#include <stdlib.h>
#if defined(_WIN32)
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#if defined(_WIN32_WINNT)
#undef _WIN32_WINNT
#endif
// target Windows version is windows 7 and later
#define _WIN32_WINNT _WIN32_WINNT_WIN7
#define PSAPI_VERSION 2
#include <windows.h>
#else
#include <dlfcn.h>
#endif
#include <stdlib.h>
liblammpsplugin_t *liblammpsplugin_load(const char *lib)
{
@ -29,14 +48,26 @@ liblammpsplugin_t *liblammpsplugin_load(const char *lib)
void *handle;
if (lib == NULL) return NULL;
#ifdef _WIN32
handle = (void *) LoadLibrary(lib);
#else
handle = dlopen(lib,RTLD_NOW|RTLD_GLOBAL);
#endif
if (handle == NULL) return NULL;
lmp = (liblammpsplugin_t *) malloc(sizeof(liblammpsplugin_t));
lmp->handle = handle;
#ifdef _WIN32
#define ADDSYM(symbol) lmp->symbol = (void *) GetProcAddress((HINSTANCE) handle, "lammps_" #symbol)
#else
#define ADDSYM(symbol) lmp->symbol = dlsym(handle,"lammps_" #symbol)
#endif
#if defined(LAMMPS_LIB_MPI)
ADDSYM(open);
#endif
ADDSYM(open_no_mpi);
ADDSYM(open_fortran);
ADDSYM(close);
@ -46,6 +77,8 @@ liblammpsplugin_t *liblammpsplugin_load(const char *lib)
ADDSYM(kokkos_finalize);
ADDSYM(python_finalize);
ADDSYM(error);
ADDSYM(file);
ADDSYM(command);
ADDSYM(commands_list);
@ -70,6 +103,7 @@ liblammpsplugin_t *liblammpsplugin_load(const char *lib)
ADDSYM(extract_compute);
ADDSYM(extract_fix);
ADDSYM(extract_variable);
ADDSYM(extract_variable_datatype);
ADDSYM(set_variable);
ADDSYM(gather_atoms);
@ -77,8 +111,15 @@ liblammpsplugin_t *liblammpsplugin_load(const char *lib)
ADDSYM(gather_atoms_subset);
ADDSYM(scatter_atoms);
ADDSYM(scatter_atoms_subset);
ADDSYM(gather_bonds);
ADDSYM(gather);
ADDSYM(gather_concat);
ADDSYM(gather_subset);
ADDSYM(scatter);
ADDSYM(scatter_subset);
ADDSYM(create_atoms);
ADDSYM(find_pair_neighlist);
@ -116,6 +157,9 @@ liblammpsplugin_t *liblammpsplugin_load(const char *lib)
ADDSYM(plugin_count);
ADDSYM(plugin_name);
ADDSYM(encode_image_flags);
ADDSYM(decode_image_flags);
ADDSYM(set_fix_external_callback);
ADDSYM(fix_external_get_force);
ADDSYM(fix_external_set_energy_global);
@ -125,6 +169,8 @@ liblammpsplugin_t *liblammpsplugin_load(const char *lib)
ADDSYM(fix_external_set_vector_length);
ADDSYM(fix_external_set_vector);
ADDSYM(flush_buffers);
ADDSYM(free);
ADDSYM(is_running);
@ -139,6 +185,8 @@ liblammpsplugin_t *liblammpsplugin_load(const char *lib)
lmp->has_error = NULL;
lmp->get_last_error_message = NULL;
#endif
ADDSYM(python_api_version);
return lmp;
}
@ -147,7 +195,11 @@ int liblammpsplugin_release(liblammpsplugin_t *lmp)
if (lmp == NULL) return 1;
if (lmp->handle == NULL) return 2;
#ifdef _WIN32
FreeLibrary((HINSTANCE) handle);
#else
dlclose(lmp->handle);
#endif
free((void *)lmp);
return 0;
}