From bc2b600f092735985317ec88ad43d0de4c17eaa4 Mon Sep 17 00:00:00 2001 From: Steve Plimpton Date: Wed, 13 Apr 2022 16:26:48 -0600 Subject: [PATCH 1/5] 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 *); }; From 5c70f45feb17b78341b158a02415f05c32ad5544 Mon Sep 17 00:00:00 2001 From: Steve Plimpton Date: Thu, 14 Apr 2022 10:49:12 -0600 Subject: [PATCH 2/5] updates to use new funcs from Python as well --- examples/mdi/sequence_driver.py | 34 ++++++++++++++++++++++++++------- src/MDI/fix_mdi_aimd.cpp | 13 ++++++++----- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/examples/mdi/sequence_driver.py b/examples/mdi/sequence_driver.py index ad1633ba2c..f658214759 100644 --- a/examples/mdi/sequence_driver.py +++ b/examples/mdi/sequence_driver.py @@ -290,22 +290,42 @@ if not mdiarg: error() # 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() +#if not plugin: +# mdi.MDI_Init(mdiarg) +# world = mdi.MDI_MPI_get_world_comm() # connect to engine - mdicomm = mdi.MDI_Accept_Communicator() +# mdicomm = mdi.MDI_Accept_Communicator() - perform_tasks(world,mdicomm,None) +# perform_tasks(world,mdicomm,None) # LAMMPS engine is a plugin library # launch plugin # MDI will call back to perform_tasks() -if plugin: - mdi.MDI_Init(mdiarg) +#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) + + +# new code to auto-detect whether engine is stand-alone code or plugin library + +mdi.MDI_Init(mdiarg) +mdicomm = mdi.MDI_Get_communicator(0) + +if mdicomm == mdi.MDI_COMM_NULL: + world = mdi.MDI_MPI_get_world_comm() + mdicomm = mdi.MDI_Accept_Communicator() + if mdicomm == mdi.MDI_COMM_NULL: + error("MDI unable to connect to stand-alone engine") + perform_tasks(world,mdicomm,None) +else: world = MPI.COMM_WORLD + method = mdi.MDI_Get_method(mdicomm) + if method != mdi.MDI_PLUGIN: + error("MDI internal error for plugin engine") plugin_args += " -mdi \"-role ENGINE -name lammps -method LINK\"" mdi.MDI_Launch_plugin(plugin,plugin_args,world,perform_tasks,None) diff --git a/src/MDI/fix_mdi_aimd.cpp b/src/MDI/fix_mdi_aimd.cpp index f43b051020..055d7984b7 100644 --- a/src/MDI/fix_mdi_aimd.cpp +++ b/src/MDI/fix_mdi_aimd.cpp @@ -110,21 +110,24 @@ 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 + + // 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 <= 0) error->all(FLERR,"Unable to connect to MDI engine"); + 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"); + if (method != MDI_PLUGIN) + error->all(FLERR,"MDI internal error for plugin engine"); } } From 20827b41056853aa41b8a86a8e35c0cf081be0c9 Mon Sep 17 00:00:00 2001 From: Steve Plimpton Date: Thu, 14 Apr 2022 15:00:09 -0600 Subject: [PATCH 3/5] sync with new version of MDI library --- examples/mdi/sequence_driver.py | 38 ++++++--------------------------- lib/mdi/Install.py | 2 +- 2 files changed, 8 insertions(+), 32 deletions(-) diff --git a/examples/mdi/sequence_driver.py b/examples/mdi/sequence_driver.py index f658214759..f15b2d4921 100644 --- a/examples/mdi/sequence_driver.py +++ b/examples/mdi/sequence_driver.py @@ -286,46 +286,22 @@ 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) +if not plugin: + world = mdi.MDI_MPI_get_world_comm() + mdicomm = mdi.MDI_Accept_Communicator() + perform_tasks(world,mdicomm,None) # LAMMPS engine is a plugin library # launch 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) - - -# new code to auto-detect whether engine is stand-alone code or plugin library - -mdi.MDI_Init(mdiarg) -mdicomm = mdi.MDI_Get_communicator(0) - -if mdicomm == mdi.MDI_COMM_NULL: - world = mdi.MDI_MPI_get_world_comm() - mdicomm = mdi.MDI_Accept_Communicator() - if mdicomm == mdi.MDI_COMM_NULL: - error("MDI unable to connect to stand-alone engine") - perform_tasks(world,mdicomm,None) -else: +if plugin: world = MPI.COMM_WORLD - method = mdi.MDI_Get_method(mdicomm) - if method != mdi.MDI_PLUGIN: - error("MDI internal error for plugin engine") 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 7c1e87fd89..b237127645 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.1" +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. From 64b1c45a49393ec476fdf08dda283e2a878a668c Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 14 Apr 2022 17:27:20 -0400 Subject: [PATCH 4/5] update MDI library and checksum --- cmake/Modules/Packages/MDI.cmake | 4 ++-- lib/mdi/Install.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) 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/lib/mdi/Install.py b/lib/mdi/Install.py index b237127645..a439da34d2 100644 --- a/lib/mdi/Install.py +++ b/lib/mdi/Install.py @@ -39,7 +39,7 @@ 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 From e66fb6a1c113f3cf7728d963eb02d517ea3d707b Mon Sep 17 00:00:00 2001 From: Axel Kohlmeyer Date: Thu, 14 Apr 2022 17:28:05 -0400 Subject: [PATCH 5/5] apply clang-format --- src/MDI/fix_mdi_aimd.cpp | 14 ++++++-------- src/MDI/mdi_engine.cpp | 9 ++++++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/MDI/fix_mdi_aimd.cpp b/src/MDI/fix_mdi_aimd.cpp index 055d7984b7..c878d183d3 100644 --- a/src/MDI/fix_mdi_aimd.cpp +++ b/src/MDI/fix_mdi_aimd.cpp @@ -110,24 +110,22 @@ 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); + MDI_Get_communicator(&mdicomm, 0); - if (mdicomm == MDI_COMM_NULL) { + 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"); + 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"); + MDI_Get_method(&method, mdicomm); + if (method != MDI_PLUGIN) error->all(FLERR, "MDI internal error for plugin engine"); } } 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);