diff --git a/cmake/Modules/Packages/MDI.cmake b/cmake/Modules/Packages/MDI.cmake index c7ef6130b8..1eec53db37 100644 --- a/cmake/Modules/Packages/MDI.cmake +++ b/cmake/Modules/Packages/MDI.cmake @@ -8,8 +8,8 @@ option(DOWNLOAD_MDI "Download and compile the MDI library instead of using an al if(DOWNLOAD_MDI) message(STATUS "MDI download requested - we will build our own") - set(MDI_URL "https://github.com/MolSSI-MDI/MDI_Library/archive/v1.3.0.tar.gz" CACHE STRING "URL for MDI tarball") - set(MDI_MD5 "8a8da217148bd9b700083b67d795af5e" CACHE STRING "MD5 checksum for MDI tarball") + set(MDI_URL "https://github.com/MolSSI-MDI/MDI_Library/archive/v1.3.2.tar.gz" CACHE STRING "URL for MDI tarball") + set(MDI_MD5 "836f5da400d8cff0f0e4435640f9454f" CACHE STRING "MD5 checksum for MDI tarball") mark_as_advanced(MDI_URL) mark_as_advanced(MDI_MD5) enable_language(C) 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/examples/mdi/sequence_driver.py b/examples/mdi/sequence_driver.py index ad1633ba2c..f15b2d4921 100644 --- a/examples/mdi/sequence_driver.py +++ b/examples/mdi/sequence_driver.py @@ -286,18 +286,15 @@ while iarg < narg: if not mdiarg: error() +mdi.MDI_Init(mdiarg) + # LAMMPS engine is a stand-alone code # world = MPI communicator for just this driver # invoke perform_tasks() directly if not plugin: - mdi.MDI_Init(mdiarg) world = mdi.MDI_MPI_get_world_comm() - - # connect to engine - mdicomm = mdi.MDI_Accept_Communicator() - perform_tasks(world,mdicomm,None) # LAMMPS engine is a plugin library @@ -305,7 +302,6 @@ if not plugin: # MDI will call back to perform_tasks() if plugin: - mdi.MDI_Init(mdiarg) world = MPI.COMM_WORLD plugin_args += " -mdi \"-role ENGINE -name lammps -method LINK\"" mdi.MDI_Launch_plugin(plugin,plugin_args,world,perform_tasks,None) diff --git a/lib/mdi/Install.py b/lib/mdi/Install.py index 7f8cc91cd0..a439da34d2 100644 --- a/lib/mdi/Install.py +++ b/lib/mdi/Install.py @@ -34,12 +34,12 @@ make lib-meam args="-m ifort" # build MEAM lib with custom Makefile.ifort (usi # settings -version = "1.3.0" +version = "1.3.2" 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. checksums = { \ - '1.3.0' : '8a8da217148bd9b700083b67d795af5e', \ + '1.3.2' : '836f5da400d8cff0f0e4435640f9454f', \ } # print error message or help diff --git a/src/MDI/fix_mdi_aimd.cpp b/src/MDI/fix_mdi_aimd.cpp index 52df86e3e0..c878d183d3 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,30 @@ int FixMDIAimd::setmask() /* ---------------------------------------------------------------------- */ +void FixMDIAimd::init() +{ + if (mdicomm != MDI_COMM_NULL) return; + + // one-time auto-detect whether engine is stand-alone code or plugin library + // also initializes mdicomm + // plugin = 0/1 for engine = stand-alone code vs plugin library + + MDI_Get_communicator(&mdicomm, 0); + + if (mdicomm == MDI_COMM_NULL) { + plugin = 0; + MDI_Accept_communicator(&mdicomm); + if (mdicomm == MDI_COMM_NULL) error->all(FLERR, "MDI unable to connect to stand-alone engine"); + } else { + plugin = 1; + int method; + MDI_Get_method(&method, mdicomm); + if (method != MDI_PLUGIN) error->all(FLERR, "MDI internal error for plugin engine"); + } +} + +/* ---------------------------------------------------------------------- */ + 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_engine.cpp b/src/MDI/mdi_engine.cpp index 8408b2dd49..f2b96cb69a 100644 --- a/src/MDI/mdi_engine.cpp +++ b/src/MDI/mdi_engine.cpp @@ -601,7 +601,8 @@ void MDIEngine::mdi_md() // delete the instance before this method returns modify->add_fix("MDI_ENGINE_INTERNAL all MDI/ENGINE"); - FixMDIEngine *mdi_fix = dynamic_cast( modify->get_fix_by_id("MDI_ENGINE_INTERNAL")); + FixMDIEngine *mdi_fix = + dynamic_cast(modify->get_fix_by_id("MDI_ENGINE_INTERNAL")); mdi_fix->mdi_engine = this; // initialize LAMMPS and setup() the simulation @@ -723,7 +724,8 @@ void MDIEngine::mdi_optg() // delete the instance before this method returns modify->add_fix("MDI_ENGINE_INTERNAL all MDI/ENGINE"); - FixMDIEngine *mdi_fix = dynamic_cast( modify->get_fix_by_id("MDI_ENGINE_INTERNAL")); + FixMDIEngine *mdi_fix = + dynamic_cast(modify->get_fix_by_id("MDI_ENGINE_INTERNAL")); mdi_fix->mdi_engine = this; // initialize LAMMPS and setup() the simulation @@ -956,7 +958,8 @@ void MDIEngine::create_system() // optionally set charges if specified by ">CHARGES" if (flag_velocities) - lammps_create_atoms(lmp, sys_natoms, nullptr, sys_types, sys_coords, sys_velocities, nullptr, 1); + lammps_create_atoms(lmp, sys_natoms, nullptr, sys_types, sys_coords, sys_velocities, nullptr, + 1); else lammps_create_atoms(lmp, sys_natoms, nullptr, sys_types, sys_coords, nullptr, nullptr, 1); 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 *); };