From bc2b600f092735985317ec88ad43d0de4c17eaa4 Mon Sep 17 00:00:00 2001 From: Steve Plimpton Date: Wed, 13 Apr 2022 16:26:48 -0600 Subject: [PATCH] changes to use new MDI library functions --- examples/mdi/in.aimd.driver.plugin | 2 +- lib/mdi/Install.py | 2 +- src/MDI/fix_mdi_aimd.cpp | 50 ++++++++++++++++++------------ src/MDI/fix_mdi_aimd.h | 7 ++--- src/MDI/mdi_plugin.cpp | 13 +------- src/MDI/mdi_plugin.h | 1 - 6 files changed, 37 insertions(+), 38 deletions(-) diff --git a/examples/mdi/in.aimd.driver.plugin b/examples/mdi/in.aimd.driver.plugin index 858e42e2bd..d8c7fdde62 100644 --- a/examples/mdi/in.aimd.driver.plugin +++ b/examples/mdi/in.aimd.driver.plugin @@ -23,7 +23,7 @@ fix 1 all nve # NPT #fix 1 all npt temp 1.0 1.0 0.1 iso 1.0 1.0 1.0 -fix 2 all mdi/aimd plugin +fix 2 all mdi/aimd fix_modify 2 energy yes virial yes thermo_style custom step temp pe etotal press vol diff --git a/lib/mdi/Install.py b/lib/mdi/Install.py index 7f8cc91cd0..7c1e87fd89 100644 --- a/lib/mdi/Install.py +++ b/lib/mdi/Install.py @@ -34,7 +34,7 @@ make lib-meam args="-m ifort" # build MEAM lib with custom Makefile.ifort (usi # settings -version = "1.3.0" +version = "1.3.1" url = "https://github.com/MolSSI-MDI/MDI_Library/archive/v%s.tar.gz" % version # known checksums for different MDI versions. used to validate the download. diff --git a/src/MDI/fix_mdi_aimd.cpp b/src/MDI/fix_mdi_aimd.cpp index 52df86e3e0..f43b051020 100644 --- a/src/MDI/fix_mdi_aimd.cpp +++ b/src/MDI/fix_mdi_aimd.cpp @@ -29,7 +29,7 @@ enum { NATIVE, REAL, METAL }; // LAMMPS units which MDI supports FixMDIAimd::FixMDIAimd(LAMMPS *lmp, int narg, char **arg) : Fix(lmp, narg, arg) { - if (narg > 4) error->all(FLERR, "Illegal fix mdi/aimd command"); + if (narg != 3) error->all(FLERR, "Illegal fix mdi/aimd command"); scalar_flag = 1; global_freq = 1; @@ -38,16 +38,6 @@ FixMDIAimd::FixMDIAimd(LAMMPS *lmp, int narg, char **arg) : Fix(lmp, narg, arg) virial_global_flag = 1; thermo_energy = thermo_virial = 1; - // check for plugin arg - - plugin = 0; - if (narg == 4) { - if (strcmp(arg[3], "plugin") == 0) - plugin = 1; - else - error->all(FLERR, "Illegal fix mdi/aimd command"); - } - // check requirements for LAMMPS to work with MDI as an engine if (atom->tag_enable == 0) error->all(FLERR, "Cannot use MDI engine without atom IDs"); @@ -62,6 +52,11 @@ FixMDIAimd::FixMDIAimd(LAMMPS *lmp, int narg, char **arg) : Fix(lmp, narg, arg) if (role != MDI_DRIVER) error->all(FLERR, "Must invoke LAMMPS as an MDI driver to use fix mdi/aimd"); + // mdicomm will be one-time initialized in init() + // cannot be done here for a plugin library, b/c mdi plugin command is later + + mdicomm = MDI_COMM_NULL; + // storage for all atoms buf3 = buf3all = nullptr; @@ -78,13 +73,6 @@ FixMDIAimd::FixMDIAimd(LAMMPS *lmp, int narg, char **arg) : Fix(lmp, narg, arg) unit_conversions(); - // connect to MDI engine, only if engine is stand-alone code - - if (!plugin) { - MDI_Accept_communicator(&mdicomm); - if (mdicomm <= 0) error->all(FLERR, "Unable to connect to MDI engine"); - } - nprocs = comm->nprocs; } @@ -92,7 +80,8 @@ FixMDIAimd::FixMDIAimd(LAMMPS *lmp, int narg, char **arg) : Fix(lmp, narg, arg) FixMDIAimd::~FixMDIAimd() { - // send exit command to engine, only if engine is stand-alone code + // send exit command to engine if it is a stand-alone code + // for plugin, this happens in MDIPlugin::plugin_wrapper() if (!plugin) { int ierr = MDI_Send_command("EXIT", mdicomm); @@ -118,6 +107,29 @@ int FixMDIAimd::setmask() /* ---------------------------------------------------------------------- */ +void FixMDIAimd::init() +{ + if (mdicomm != MDI_COMM_NULL) return; + + // one-time initialization of mdicomm + // plugin = 0/1 if MDI engine is a stand-alone code vs plugin library + + MDI_Get_communicator(&mdicomm,0); + + if (mdicomm == MDI_COMM_NULL) { + plugin = 0; + MDI_Accept_communicator(&mdicomm); + if (mdicomm <= 0) error->all(FLERR,"Unable to connect to MDI engine"); + } else { + plugin = 1; + int method; + MDI_Get_method(&method,mdicomm); + if (method != MDI_PLUGIN) error->all(FLERR,"MDI internal error"); + } +} + +/* ---------------------------------------------------------------------- */ + void FixMDIAimd::setup(int vflag) { post_force(vflag); diff --git a/src/MDI/fix_mdi_aimd.h b/src/MDI/fix_mdi_aimd.h index 6ca886794f..7d408b414f 100644 --- a/src/MDI/fix_mdi_aimd.h +++ b/src/MDI/fix_mdi_aimd.h @@ -27,14 +27,11 @@ namespace LAMMPS_NS { class FixMDIAimd : public Fix { public: - // MDI communicator, public so that LAMMPS can work with a plugin - - MDI_Comm mdicomm; - FixMDIAimd(class LAMMPS *, int, char **); ~FixMDIAimd(); int setmask(); + void init(); void setup(int); void setup_pre_reverse(int, int); void pre_reverse(int, int); @@ -46,6 +43,8 @@ class FixMDIAimd : public Fix { int nprocs; int plugin; + MDI_Comm mdicomm; + int eflag_caller; double engine_energy; int lmpunits; diff --git a/src/MDI/mdi_plugin.cpp b/src/MDI/mdi_plugin.cpp index 99549a621f..3c8e38481d 100644 --- a/src/MDI/mdi_plugin.cpp +++ b/src/MDI/mdi_plugin.cpp @@ -92,11 +92,6 @@ MDIPlugin::MDIPlugin(LAMMPS *_lmp, int narg, char **arg) : Pointers(_lmp) strcat(plugin_args, extra_arg); } - // find FixMDIAimd instance so can reset its mdicomm - // NOTE: this is a kludge - need better way to handle this - - fixptr = modify->get_fix_by_style("mdi/aimd")[0]; - // launch the MDI plugin library // path for lib was specified in -mdi command-line arg when LAMMPS started // this calls back to plugin_wrapper, which must issue MDI EXIT at end @@ -108,8 +103,7 @@ MDIPlugin::MDIPlugin(LAMMPS *_lmp, int narg, char **arg) : Pointers(_lmp) /* ---------------------------------------------------------------------- callback function from MDI_Launch_plugin() - this function must wrap entire interaction of LAMMPS as a driver - with the plugin + this function wraps entire interaction of LAMMPS driver with the plugin ---------------------------------------------------------------------- */ int MDIPlugin::plugin_wrapper(void * /*pmpicomm*/, MDI_Comm mdicomm, void *vptr) @@ -118,11 +112,6 @@ int MDIPlugin::plugin_wrapper(void * /*pmpicomm*/, MDI_Comm mdicomm, void *vptr) LAMMPS *lammps = ptr->lmp; char *lammps_command = ptr->lammps_command; - // set FixMDIAimd mdicomm to driver's mdicomm passed to this callback - - auto aimdptr = dynamic_cast (ptr->fixptr); - aimdptr->mdicomm = mdicomm; - // invoke the specified LAMMPS command // that operation will issue MDI commands to the plugin engine diff --git a/src/MDI/mdi_plugin.h b/src/MDI/mdi_plugin.h index c70a09975e..3299658dc8 100644 --- a/src/MDI/mdi_plugin.h +++ b/src/MDI/mdi_plugin.h @@ -25,7 +25,6 @@ class MDIPlugin : protected Pointers { private: char *lammps_command; - class Fix *fixptr; static int plugin_wrapper(void *, MDI_Comm, void *); };