Calling External Program which uses MPI from python with MPI4PY

Viewed 213

In a project I am developing a python code which runs LAMMPS as a library via its python interface and MPI4PY parallelism module. My code act as a puppet master which drives LAMMPS:

from mpi4py import MPI
from lammps import lammps  # ', PyLammps
import numpy as np
def init_opls(datafile, lmp_opls='', cnt_log_opls=0,communicator=''):
    if not lmp_opls:
       cnt_log_opls += 1
       # lammps can create an instance with a custom comminicator
       lmp_opls = lammps(comm=communicator) 
     return lmp_opls, cnt_log_opls
def run_sim(lmp, simtype, tempi=100, tempf=100, steps=100, current_step=0):
    """ lmp : lammps instance, simtype : nvt or nve , temp
    """
    lmp.command("reset_timestep %d" % current_step)
    lmp.command("dump opls all custom 100 dump.lammpstrj id mass q x y z")
    lmp.command("dump_modify opls sort id append yes")
    lmp.command('dump_modify opls format line "%5d %12.3f %12.8f %12.4f %12.4f %12.4f"')
    lmp.command(
         'fix run_sim all %s temp %f %f 100.0 ' % (simtype, tempi, tempf))
    lmp.command('run %d ' % steps)
    lmp.command('unfix run_sim')
    lmp.command("undump opls")
    current_step = lmp.get_thermo("step")
    return lmp, current_step
def main():
    import os
    import sys
    me = MPI.COMM_WORLD.Get_rank()
    datafileopls = 'MEDIUM2_opls.lmp'
    comm = MPI.COMM_WORLD
    current_step = 0
    lmp_opls,cnt = init_opls(datafileopls,communicator=comm)
    lmp_opls, current_step = run_sim(lmp_opls,"nvt",tempi=100., tempf=900.0,
                    steps=500, current_step=current_step)
if me == 0:
    comm2 =  MPI.COMM_SELF.Spawn('reaxff',args=[],maxprocs=1) 
    comm2.Disconnect()

In the last part after finishing with LAMMPS I would like to run another md code called reaxff. 'reaxff' is an executable file which I do not have any control but it runs parallel with MPI. With this setup it gives this error:

MPID_Init(1976).............: spawn process group was unable to obtain parent 
port name from the channelenter code here

I tried calling reaxff from rank=0 as a system call but this time it only use 1 cpu which it should use all available cpus but creates 16 threads on same cpu. What I want to do is to call reaxff and it should use all available processors. Honestly I do not have much experience this and any solutions should be welcome.

0 Answers
Related