From d16bc40254fed59681346d9e1317ed26956b43a1 Mon Sep 17 00:00:00 2001 From: Niklas Leimeroth Date: Thu, 6 Oct 2022 09:21:53 +0200 Subject: [PATCH 1/9] 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/9] 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/9] 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( From e04ee48d5089ec94f35e39d80e5e8bddc4941e56 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Tue, 11 Oct 2022 10:20:31 -0600 Subject: [PATCH 4/9] Update test --- tests/test_pylammpsmpi_local.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/test_pylammpsmpi_local.py b/tests/test_pylammpsmpi_local.py index c894bc20..7f12077c 100644 --- a/tests/test_pylammpsmpi_local.py +++ b/tests/test_pylammpsmpi_local.py @@ -12,7 +12,7 @@ class TestLocalLammpsLibrary(unittest.TestCase): @classmethod def setUpClass(cls): cls.execution_path = os.path.dirname(os.path.abspath(__file__)) - cls.lmp = LammpsLibrary(cores=2, mode='local') + cls.lmp = LammpsLibrary(cores=2, mode='local', cmdargs=["-cite", "citations.txt"]) cls.lmp.file(os.path.join(cls.execution_path, "in.simple")) @classmethod @@ -80,11 +80,10 @@ def test_extract_box(self): self.lmp.clear() 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")) + self.lmp.clear() + self.lmp.file(os.path.join(self.execution_path, "in.simple")) + self.assertTrue(os.path.isfile(os.path.join(self.execution_path, "citations.txt"))) if __name__ == "__main__": unittest.main() From ae50ba985b2e4f05f471b79ab30f327ec5742251 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Tue, 11 Oct 2022 10:23:07 -0600 Subject: [PATCH 5/9] Add space --- tests/test_pylammpsmpi_local.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/test_pylammpsmpi_local.py b/tests/test_pylammpsmpi_local.py index 7f12077c..befbc007 100644 --- a/tests/test_pylammpsmpi_local.py +++ b/tests/test_pylammpsmpi_local.py @@ -85,5 +85,6 @@ def test_cmdarg_options(self): self.lmp.file(os.path.join(self.execution_path, "in.simple")) self.assertTrue(os.path.isfile(os.path.join(self.execution_path, "citations.txt"))) + if __name__ == "__main__": unittest.main() From 44a7b627a35eb9e27a4840432f05e78082376d73 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Tue, 11 Oct 2022 10:31:03 -0600 Subject: [PATCH 6/9] use absolute path for citations --- tests/test_pylammpsmpi_local.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_pylammpsmpi_local.py b/tests/test_pylammpsmpi_local.py index befbc007..6605be4a 100644 --- a/tests/test_pylammpsmpi_local.py +++ b/tests/test_pylammpsmpi_local.py @@ -12,7 +12,8 @@ class TestLocalLammpsLibrary(unittest.TestCase): @classmethod def setUpClass(cls): cls.execution_path = os.path.dirname(os.path.abspath(__file__)) - cls.lmp = LammpsLibrary(cores=2, mode='local', cmdargs=["-cite", "citations.txt"]) + citation_file = os.path.join(cls.execution_path, "citations.txt") + cls.lmp = LammpsLibrary(cores=2, mode='local', cmdargs=["-cite", citation_file]) cls.lmp.file(os.path.join(cls.execution_path, "in.simple")) @classmethod From 36f3800d2dd289ebd960358b0e122300b2b2a674 Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Tue, 11 Oct 2022 10:36:51 -0600 Subject: [PATCH 7/9] Clean up variable usage --- tests/test_pylammpsmpi_local.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/test_pylammpsmpi_local.py b/tests/test_pylammpsmpi_local.py index 6605be4a..ac870e95 100644 --- a/tests/test_pylammpsmpi_local.py +++ b/tests/test_pylammpsmpi_local.py @@ -12,9 +12,10 @@ class TestLocalLammpsLibrary(unittest.TestCase): @classmethod def setUpClass(cls): cls.execution_path = os.path.dirname(os.path.abspath(__file__)) - citation_file = os.path.join(cls.execution_path, "citations.txt") - cls.lmp = LammpsLibrary(cores=2, mode='local', cmdargs=["-cite", citation_file]) - cls.lmp.file(os.path.join(cls.execution_path, "in.simple")) + cls.citation_file = os.path.join(cls.execution_path, "citations.txt") + cls.lammps_file = os.path.join(cls.execution_path, "in.simple") + cls.lmp = LammpsLibrary(cores=2, mode='local', cmdargs=["-cite", cls.citation_file]) + cls.lmp.file(cls.lammps_file) @classmethod def tearDownClass(cls): @@ -79,12 +80,12 @@ def test_extract_box(self): self.assertEqual(box[0][0], 0.0) self.assertEqual(np.round(box[1][0], 2), 8.0) self.lmp.clear() - self.lmp.file(os.path.join(self.execution_path, "in.simple")) + self.lmp.file(self.lammps_file) def test_cmdarg_options(self): self.lmp.clear() - self.lmp.file(os.path.join(self.execution_path, "in.simple")) - self.assertTrue(os.path.isfile(os.path.join(self.execution_path, "citations.txt"))) + self.lmp.file(self.lammps_file) + self.assertTrue(os.path.isfile(self.citation_file)) if __name__ == "__main__": From f13978be4187ba213e7dd2fa230646efe9f44b6b Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Tue, 11 Oct 2022 10:37:46 -0600 Subject: [PATCH 8/9] Remove unnecessary reset --- tests/test_pylammpsmpi_local.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/test_pylammpsmpi_local.py b/tests/test_pylammpsmpi_local.py index ac870e95..f4ef06cc 100644 --- a/tests/test_pylammpsmpi_local.py +++ b/tests/test_pylammpsmpi_local.py @@ -83,8 +83,6 @@ def test_extract_box(self): self.lmp.file(self.lammps_file) def test_cmdarg_options(self): - self.lmp.clear() - self.lmp.file(self.lammps_file) self.assertTrue(os.path.isfile(self.citation_file)) From f4bec9aa2943a1186200c1e408d98786356cb57b Mon Sep 17 00:00:00 2001 From: Jan Janssen Date: Tue, 11 Oct 2022 10:45:25 -0600 Subject: [PATCH 9/9] Fix dask implementation to support command line arguments --- pylammpsmpi/lammps_wrapper.py | 1 + tests/test_pylammpsmpi_cluster.py | 29 +++++++++++++++++++++-------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/pylammpsmpi/lammps_wrapper.py b/pylammpsmpi/lammps_wrapper.py index 64515457..771c1189 100644 --- a/pylammpsmpi/lammps_wrapper.py +++ b/pylammpsmpi/lammps_wrapper.py @@ -35,6 +35,7 @@ def __init__( LammpsBase, cores=self.cores, working_directory=self.working_directory, + cmdargs=cmdargs, actor=True, ) self.lmp = fut.result() diff --git a/tests/test_pylammpsmpi_cluster.py b/tests/test_pylammpsmpi_cluster.py index d4a80eda..d1a7c015 100644 --- a/tests/test_pylammpsmpi_cluster.py +++ b/tests/test_pylammpsmpi_cluster.py @@ -13,10 +13,20 @@ class TestLocalLammpsLibrary(unittest.TestCase): @classmethod def setUpClass(cls): cls.execution_path = os.path.dirname(os.path.abspath(__file__)) - cluster = LocalCluster(n_workers=1, threads_per_worker=2) + cls.citation_file = os.path.join(cls.execution_path, "citations.txt") + cls.lammps_file = os.path.join(cls.execution_path, "in.simple") + cluster = LocalCluster( + n_workers=1, + threads_per_worker=2 + ) client = Client(cluster) - cls.lmp = LammpsLibrary(cores=2, mode='dask', client=client) - cls.lmp.file(os.path.join(cls.execution_path, "in.simple")) + cls.lmp = LammpsLibrary( + cores=2, + mode='dask', + cmdargs=["-cite", cls.citation_file], + client=client + ) + cls.lmp.file(cls.lammps_file) @classmethod def tearDownClass(cls): @@ -60,11 +70,11 @@ def test_scatter_atoms(self): f1 = self.lmp.gather_atoms("f") self.assertEqual(f1[1][0], val) - f = self.lmp.gather_atoms("f", ids=[1,2]) + f = self.lmp.gather_atoms("f", ids=[1, 2]) val = np.random.randint(0, 100) f[1][1] = val - self.lmp.scatter_atoms("f", f, ids=[1,2]) - f1 = self.lmp.gather_atoms("f", ids=[1,2]) + self.lmp.scatter_atoms("f", f, ids=[1, 2]) + f1 = self.lmp.gather_atoms("f", ids=[1, 2]) self.assertEqual(f1[1][1], val) def test_extract_box(self): @@ -75,12 +85,15 @@ def test_extract_box(self): self.assertEqual(np.round(box[1][0], 2), 6.72) self.lmp.delete_atoms("group", "all") - self.lmp.reset_box([0.0,0.0,0.0], [8.0,8.0,8.0], 0.0,0.0,0.0) + self.lmp.reset_box([0.0, 0.0, 0.0], [8.0, 8.0, 8.0], 0.0, 0.0, 0.0) box = self.lmp.extract_box() self.assertEqual(box[0][0], 0.0) self.assertEqual(np.round(box[1][0], 2), 8.0) self.lmp.clear() - self.lmp.file(os.path.join(self.execution_path, "in.simple")) + self.lmp.file(self.lammps_file) + + def test_cmdarg_options(self): + self.assertTrue(os.path.isfile(self.citation_file)) if __name__ == "__main__":