Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MDI new functions #3218

Merged
merged 5 commits into from Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions cmake/Modules/Packages/MDI.cmake
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion examples/mdi/in.aimd.driver.plugin
Expand Up @@ -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
Expand Down
8 changes: 2 additions & 6 deletions examples/mdi/sequence_driver.py
Expand Up @@ -286,26 +286,22 @@ def perform_tasks(world,mdicomm,dummy):

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
# 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)
4 changes: 2 additions & 2 deletions lib/mdi/Install.py
Expand Up @@ -34,12 +34,12 @@

# 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
Expand Down
51 changes: 32 additions & 19 deletions src/MDI/fix_mdi_aimd.cpp
Expand Up @@ -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;
Expand All @@ -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");
Expand All @@ -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;
Expand All @@ -78,21 +73,15 @@ 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;
}

/* ---------------------------------------------------------------------- */

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);
Expand All @@ -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);
Expand Down
7 changes: 3 additions & 4 deletions src/MDI/fix_mdi_aimd.h
Expand Up @@ -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);
Expand All @@ -46,6 +43,8 @@ class FixMDIAimd : public Fix {
int nprocs;
int plugin;

MDI_Comm mdicomm;

int eflag_caller;
double engine_energy;
int lmpunits;
Expand Down
9 changes: 6 additions & 3 deletions src/MDI/mdi_engine.cpp
Expand Up @@ -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<FixMDIEngine *>( modify->get_fix_by_id("MDI_ENGINE_INTERNAL"));
FixMDIEngine *mdi_fix =
dynamic_cast<FixMDIEngine *>(modify->get_fix_by_id("MDI_ENGINE_INTERNAL"));
mdi_fix->mdi_engine = this;

// initialize LAMMPS and setup() the simulation
Expand Down Expand Up @@ -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<FixMDIEngine *>( modify->get_fix_by_id("MDI_ENGINE_INTERNAL"));
FixMDIEngine *mdi_fix =
dynamic_cast<FixMDIEngine *>(modify->get_fix_by_id("MDI_ENGINE_INTERNAL"));
mdi_fix->mdi_engine = this;

// initialize LAMMPS and setup() the simulation
Expand Down Expand Up @@ -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);

Expand Down
13 changes: 1 addition & 12 deletions src/MDI/mdi_plugin.cpp
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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<FixMDIAimd *> (ptr->fixptr);
aimdptr->mdicomm = mdicomm;

// invoke the specified LAMMPS command
// that operation will issue MDI commands to the plugin engine

Expand Down
1 change: 0 additions & 1 deletion src/MDI/mdi_plugin.h
Expand Up @@ -25,7 +25,6 @@ class MDIPlugin : protected Pointers {

private:
char *lammps_command;
class Fix *fixptr;

static int plugin_wrapper(void *, MDI_Comm, void *);
};
Expand Down