move MDI C style library calls to separate files

This commit is contained in:
Axel Kohlmeyer
2021-05-13 11:37:05 -04:00
parent 7d7781373c
commit 8d9465ccdb
5 changed files with 136 additions and 106 deletions

View File

@ -17,6 +17,7 @@
------------------------------------------------------------------------- */ ------------------------------------------------------------------------- */
#include "fix_mdi_engine.h" #include "fix_mdi_engine.h"
#include "library_mdi.h"
#include "atom.h" #include "atom.h"
#include "comm.h" #include "comm.h"

View File

@ -0,0 +1,109 @@
/* ----------------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://lammps.sandia.gov/, Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
// ----------------------------------------------------------------------
// MolSSI Driver Interface functions
// ----------------------------------------------------------------------
#include "library_mdi.h"
// needed to enable MPI support
#define LAMMPS_LIB_MPI 1
#include "library.h"
#include "fix_mdi_engine.h"
#include <cstring>
using namespace LAMMPS_NS;
/** Initialize an instance of LAMMPS as an MDI plugin
*
\verbatim embed:rst
This function is called by the MolSSI Driver Interface library (MDI)
when LAMMPS is run as a plugin, and should not otherwise be used.
The function initializes MDI, then creates and initializes an instance
of LAMMPS. The command-line arguments ``argc`` and ``argv`` used to
initialize LAMMPS are recieved from MDI. The LAMMPS instance runs an
input file, which must include the ``mdi/engine`` command; when LAMMPS
executes this command, it will begin listening for commands from the
driver. The name of the input file is obtained from the ``-in``
command-line argument, which must be provided by the MDI driver.
\endverbatim
* \param command string buffer corresponding to the command to be executed
* \param comm MDI communicator that can be used to communicated with the driver.
* \param class_obj pointer to an instance of an mdi/engine fix cast to ``void *``.
* \return 0 on no error. */
int MDI_Plugin_init_lammps()
{
// initialize MDI
int mdi_argc;
char **mdi_argv;
if (MDI_Plugin_get_argc(&mdi_argc)) MPI_Abort(MPI_COMM_WORLD, 1);
if (MDI_Plugin_get_argv(&mdi_argv)) MPI_Abort(MPI_COMM_WORLD, 1);
if (MDI_Init(&mdi_argc, &mdi_argv)) MPI_Abort(MPI_COMM_WORLD, 1);
// get the MPI intra-communicator for this code
MPI_Comm mpi_world_comm = MPI_COMM_WORLD;
if (MDI_MPI_get_world_comm(&mpi_world_comm)) MPI_Abort(MPI_COMM_WORLD, 1);
// find the -in argument
int iarg = 0;
char *filename;
bool found_filename = false;
while (iarg < mdi_argc && !found_filename) {
if ((strcmp(mdi_argv[iarg], "-in") == 0) || (strcmp(mdi_argv[iarg], "-i") == 0)) {
if (iarg + 2 > mdi_argc) MPI_Abort(MPI_COMM_WORLD, 1);
filename = mdi_argv[iarg + 1];
found_filename = true;
// remove -in argument from the command list
mdi_argc -= 2;
for (int jarg = iarg; jarg < mdi_argc; jarg++) mdi_argv[jarg] = mdi_argv[jarg + 2];
}
iarg++;
}
if (!found_filename) MPI_Abort(MPI_COMM_WORLD, 1);
// create and run a LAMMPS instance
auto lmp = lammps_open(mdi_argc, mdi_argv, mpi_world_comm, NULL);
lammps_file(lmp, filename);
lammps_close(lmp);
return 0;
}
/** Execute an MDI command
*
\verbatim embed:rst
This function is called by the MolSSI Driver Interface Library (MDI)
when LAMMPS is run as a plugin, and should not otherwise be used.
The function executes a single command from an external MDI driver.
\endverbatim
* \param command string buffer corresponding to the command to be executed
* \param comm MDI communicator that can be used to communicated with the driver.
* \param class_obj pointer to an instance of an mdi/engine fix cast to ``void *``.
* \return 0 on no error, 1 on error. */
int lammps_execute_mdi_command(const char *command, MDI_Comm comm, void *class_obj)
{
FixMDIEngine *mdi_fix = (FixMDIEngine *) class_obj;
return mdi_fix->execute_command(command, comm);
}

View File

@ -0,0 +1,26 @@
/* -*- c -*- ------------------------------------------------------------
LAMMPS - Large-scale Atomic/Molecular Massively Parallel Simulator
https://lammps.sandia.gov/, Sandia National Laboratories
Steve Plimpton, sjplimp@sandia.gov
Copyright (2003) Sandia Corporation. Under the terms of Contract
DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
certain rights in this software. This software is distributed under
the GNU General Public License.
See the README file in the top-level LAMMPS directory.
------------------------------------------------------------------------- */
#ifndef LAMMPS_LIBRARY_MDI_H
#define LAMMPS_LIBRARY_MDI_H
/* C style library calls to LAMMPS when a LAMMPS shared library is
* used as a plugin through MolSSI Driver Interface (MDI). */
#include <mdi.h>
extern "C" {
int MDI_Plugin_init_lammps();
int lammps_execute_mdi_command(const char *, MDI_Comm, void *);
}
#endif

View File

@ -4978,103 +4978,6 @@ int lammps_get_last_error_message(void *handle, char *buffer, int buf_size) {
return 0; return 0;
} }
// ----------------------------------------------------------------------
// MolSSI Drive Interface functions
// ----------------------------------------------------------------------
/** Initialize an instance of LAMMPS as an MDI plugin
*
\verbatim embed:rst
This function is called by the MolSSI Driver Interface library (MDI)
when LAMMPS is run as a plugin, and should not otherwise be used.
The function initializes MDI, then creates and initializes an instance
of LAMMPS. The command-line arguments ``argc`` and ``argv`` used to
initialize LAMMPS are recieved from MDI. The LAMMPS instance runs an
input file, which must include the ``mdi/engine`` command; when LAMMPS
executes this command, it will begin listening for commands from the
driver. The name of the input file is obtained from the ``-in``
command-line argument, which must be provided by the MDI driver.
\endverbatim
* \param command string buffer corresponding to the command to be executed
* \param comm MDI communicator that can be used to communicated with the driver.
* \param class_obj pointer to an instance of an mdi/engine fix cast to ``void *``.
* \return 0 on no error. */
int MDI_Plugin_init_lammps()
{
// initialize MDI
int mdi_argc;
char **mdi_argv;
if (MDI_Plugin_get_argc(&mdi_argc))
MPI_Abort(MPI_COMM_WORLD, 1);
if (MDI_Plugin_get_argv(&mdi_argv))
MPI_Abort(MPI_COMM_WORLD, 1);
if (MDI_Init(&mdi_argc, &mdi_argv))
MPI_Abort(MPI_COMM_WORLD, 1);
// get the MPI intra-communicator for this code
MPI_Comm mpi_world_comm = MPI_COMM_WORLD;
if (MDI_MPI_get_world_comm(&mpi_world_comm))
MPI_Abort(MPI_COMM_WORLD, 1);
// find the -in argument
int iarg = 0;
char *filename;
bool found_filename = false;
while(iarg < mdi_argc && !found_filename) {
if ((strcmp(mdi_argv[iarg],"-in") == 0)
|| (strcmp(mdi_argv[iarg],"-i") == 0)) {
if (iarg+2 > mdi_argc)
MPI_Abort(MPI_COMM_WORLD, 1);
filename = mdi_argv[iarg+1];
found_filename = true;
// remove -in argument from the command list
mdi_argc -= 2;
for (int jarg=iarg; jarg < mdi_argc; jarg++)
mdi_argv[jarg] = mdi_argv[jarg+2];
}
iarg++;
}
if (!found_filename)
MPI_Abort(MPI_COMM_WORLD, 1);
// create and run a LAMMPS instance
LAMMPS *lmp = (LAMMPS*) lammps_open(mdi_argc, mdi_argv, mpi_world_comm, NULL);
lammps_file(lmp, filename);
lammps_close(lmp);
return 0;
}
/** Execute an MDI command
*
\verbatim embed:rst
This function is called by the MolSSI Driver Interface Library (MDI)
when LAMMPS is run as a plugin, and should not otherwise be used. The
function executes a single command from an external MDI driver. If the
LAMMPS library was compiled without ``-DLMP_USER_MDI``, the function
will fail and return a "1".
\endverbatim
* \param command string buffer corresponding to the command to be executed
* \param comm MDI communicator that can be used to communicated with the driver.
* \param class_obj pointer to an instance of an mdi/engine fix cast to ``void *``.
* \return 0 on no error, 1 on error. */
int lammps_execute_mdi_command(const char *command, MDI_Comm comm, void *class_obj)
{
#if defined(LMP_USER_MDI)
FixMDIEngine *mdi_fix = (FixMDIEngine *) class_obj;
return mdi_fix->execute_command(command, comm);
#else
return 1;
#endif
}
// Local Variables: // Local Variables:
// fill-column: 72 // fill-column: 72
// End: // End:

View File

@ -40,8 +40,6 @@
#include <inttypes.h> /* for int64_t */ #include <inttypes.h> /* for int64_t */
#endif #endif
#include "mdi_interface.h"
/** Data type constants for extracting data from atoms, computes and fixes /** Data type constants for extracting data from atoms, computes and fixes
* *
* Must be kept in sync with the equivalent constants in lammps/constants.py */ * Must be kept in sync with the equivalent constants in lammps/constants.py */
@ -245,13 +243,6 @@ void lammps_force_timeout(void *handle);
int lammps_has_error(void *handle); int lammps_has_error(void *handle);
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);
/* ----------------------------------------------------------------------
* MDI functions
* ---------------------------------------------------------------------- */
int MDI_Plugin_init_lammps();
int lammps_execute_mdi_command(const char* command, MDI_Comm comm, void* class_obj);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif