From d16bc40254fed59681346d9e1317ed26956b43a1 Mon Sep 17 00:00:00 2001 From: Niklas Leimeroth Date: Thu, 6 Oct 2022 09:21:53 +0200 Subject: [PATCH 1/3] draft --- pylammpsmpi/lammps_wrapper.py | 4 ++-- pylammpsmpi/mpi/lmpmpi.py | 5 ++++- pylammpsmpi/utils/lammps.py | 26 ++++++++++++++++++-------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/pylammpsmpi/lammps_wrapper.py b/pylammpsmpi/lammps_wrapper.py index 57d48d70..9ab1c412 100644 --- a/pylammpsmpi/lammps_wrapper.py +++ b/pylammpsmpi/lammps_wrapper.py @@ -22,7 +22,7 @@ class LammpsLibrary: Top level class which manages the lammps library provided by LammpsBase """ - def __init__(self, cores=1, working_directory=".", client=None, mode="local"): + def __init__(self, cores=1, working_directory=".", client=None, mode="local", cmdargs=None): self.cores = cores self.working_directory = working_directory self.client = client @@ -42,7 +42,7 @@ def __init__(self, cores=1, working_directory=".", client=None, mode="local"): elif self.mode == "local": self.lmp = LammpsBase( - cores=self.cores, working_directory=self.working_directory + cores=self.cores, working_directory=self.working_directory, cmdargs=cmdargs ) self.lmp.start_process() diff --git a/pylammpsmpi/mpi/lmpmpi.py b/pylammpsmpi/mpi/lmpmpi.py index 4db14678..e2141eb6 100644 --- a/pylammpsmpi/mpi/lmpmpi.py +++ b/pylammpsmpi/mpi/lmpmpi.py @@ -42,7 +42,10 @@ } # Lammps executable -job = lammps(cmdargs=["-screen", "none"]) +args = ["-screen", "none"] +if len(sys.argv) > 1: + args.extend(sys.argv[0:]) +job = lammps(cmdargs=args) def extract_compute(funct_args): diff --git a/pylammpsmpi/utils/lammps.py b/pylammpsmpi/utils/lammps.py index 285965ef..79c606ca 100644 --- a/pylammpsmpi/utils/lammps.py +++ b/pylammpsmpi/utils/lammps.py @@ -20,22 +20,32 @@ class LammpsBase: - def __init__(self, cores=8, working_directory="."): + def __init__(self, cores=8, working_directory=".", cmdargs=None): self.cores = cores self.working_directory = working_directory self._process = None + self._cmdargs = cmdargs def start_process(self): executable = os.path.join( os.path.dirname(os.path.abspath(__file__)), "../mpi", "lmpmpi.py" ) - self._process = subprocess.Popen( - ["mpiexec", "--oversubscribe", "-n", str(self.cores), "python", executable], - stdout=subprocess.PIPE, - stderr=None, - stdin=subprocess.PIPE, - cwd=self.working_directory, - ) + if self._cmdargs is None: + self._process = subprocess.Popen( + ["mpiexec", "--oversubscribe", "-n", str(self.cores), "python", executable], + stdout=subprocess.PIPE, + stderr=None, + stdin=subprocess.PIPE, + cwd=self.working_directory, + ) + else: + self._process = subprocess.Popen( + ["mpiexec", "--oversubscribe", "-n", str(self.cores), "python", executable] + self._cmdargs, + stdout=subprocess.PIPE, + stderr=None, + stdin=subprocess.PIPE, + cwd=self.working_directory, + ) def _send(self, command, data=None): """ From 9d45ad0a4c22a71ce51c2bc7adb0f8f9fac07c6d Mon Sep 17 00:00:00 2001 From: Niklas Leimeroth Date: Thu, 6 Oct 2022 09:54:15 +0200 Subject: [PATCH 2/3] clean up and test --- pylammpsmpi/mpi/lmpmpi.py | 2 +- pylammpsmpi/utils/lammps.py | 26 ++++++++++---------------- tests/test_pylammpsmpi_local.py | 5 +++++ 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/pylammpsmpi/mpi/lmpmpi.py b/pylammpsmpi/mpi/lmpmpi.py index e2141eb6..b2ed04aa 100644 --- a/pylammpsmpi/mpi/lmpmpi.py +++ b/pylammpsmpi/mpi/lmpmpi.py @@ -44,7 +44,7 @@ # Lammps executable args = ["-screen", "none"] if len(sys.argv) > 1: - args.extend(sys.argv[0:]) + args.extend(sys.argv[1:]) job = lammps(cmdargs=args) diff --git a/pylammpsmpi/utils/lammps.py b/pylammpsmpi/utils/lammps.py index 79c606ca..c5c9a882 100644 --- a/pylammpsmpi/utils/lammps.py +++ b/pylammpsmpi/utils/lammps.py @@ -30,22 +30,16 @@ def start_process(self): executable = os.path.join( os.path.dirname(os.path.abspath(__file__)), "../mpi", "lmpmpi.py" ) - if self._cmdargs is None: - self._process = subprocess.Popen( - ["mpiexec", "--oversubscribe", "-n", str(self.cores), "python", executable], - stdout=subprocess.PIPE, - stderr=None, - stdin=subprocess.PIPE, - cwd=self.working_directory, - ) - else: - self._process = subprocess.Popen( - ["mpiexec", "--oversubscribe", "-n", str(self.cores), "python", executable] + self._cmdargs, - stdout=subprocess.PIPE, - stderr=None, - stdin=subprocess.PIPE, - cwd=self.working_directory, - ) + cmds = ["mpiexec", "--oversubscribe", "-n", str(self.cores), "python", executable] + if self._cmdargs is not None: + cmds.extend(self._cmdargs) + self._process = subprocess.Popen( + cmds, + stdout=subprocess.PIPE, + stderr=None, + stdin=subprocess.PIPE, + cwd=self.working_directory, + ) def _send(self, command, data=None): """ diff --git a/tests/test_pylammpsmpi_local.py b/tests/test_pylammpsmpi_local.py index 1fc98914..c894bc20 100644 --- a/tests/test_pylammpsmpi_local.py +++ b/tests/test_pylammpsmpi_local.py @@ -81,5 +81,10 @@ def test_extract_box(self): self.lmp.file(os.path.join(self.execution_path, "in.simple")) + def test_cmdarg_options(self): + self.lmp2 = LammpsLibrary(cores=2, mode='local', cmdargs=["-cite", "citations.txt"]) + self.lmp2.file(os.path.join(self.execution_path, "in.simple")) + assert os.path.isfile(os.path.join(self.execution_path, "citations.txt")) + if __name__ == "__main__": unittest.main() From a51eeb04473ca339a7e069e15f44ac0438b6c4d6 Mon Sep 17 00:00:00 2001 From: pyiron-runner Date: Thu, 6 Oct 2022 07:58:58 +0000 Subject: [PATCH 3/3] Format black --- pylammpsmpi/lammps_wrapper.py | 8 ++++++-- pylammpsmpi/utils/lammps.py | 9 ++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/pylammpsmpi/lammps_wrapper.py b/pylammpsmpi/lammps_wrapper.py index 9ab1c412..64515457 100644 --- a/pylammpsmpi/lammps_wrapper.py +++ b/pylammpsmpi/lammps_wrapper.py @@ -22,7 +22,9 @@ class LammpsLibrary: Top level class which manages the lammps library provided by LammpsBase """ - def __init__(self, cores=1, working_directory=".", client=None, mode="local", cmdargs=None): + def __init__( + self, cores=1, working_directory=".", client=None, mode="local", cmdargs=None + ): self.cores = cores self.working_directory = working_directory self.client = client @@ -42,7 +44,9 @@ def __init__(self, cores=1, working_directory=".", client=None, mode="local", cm elif self.mode == "local": self.lmp = LammpsBase( - cores=self.cores, working_directory=self.working_directory, cmdargs=cmdargs + cores=self.cores, + working_directory=self.working_directory, + cmdargs=cmdargs, ) self.lmp.start_process() diff --git a/pylammpsmpi/utils/lammps.py b/pylammpsmpi/utils/lammps.py index c5c9a882..1b61bba3 100644 --- a/pylammpsmpi/utils/lammps.py +++ b/pylammpsmpi/utils/lammps.py @@ -30,7 +30,14 @@ def start_process(self): executable = os.path.join( os.path.dirname(os.path.abspath(__file__)), "../mpi", "lmpmpi.py" ) - cmds = ["mpiexec", "--oversubscribe", "-n", str(self.cores), "python", executable] + cmds = [ + "mpiexec", + "--oversubscribe", + "-n", + str(self.cores), + "python", + executable, + ] if self._cmdargs is not None: cmds.extend(self._cmdargs) self._process = subprocess.Popen(