From 552fdc9563c63d9ca672ca984f2390da4f2f26dc Mon Sep 17 00:00:00 2001 From: chrisjonesbsu Date: Wed, 1 May 2024 09:31:12 -0600 Subject: [PATCH 1/3] add stride parameter when saving a CG trajectory --- grits/coarsegrain.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/grits/coarsegrain.py b/grits/coarsegrain.py index 8ff2847..d43718e 100644 --- a/grits/coarsegrain.py +++ b/grits/coarsegrain.py @@ -667,7 +667,7 @@ def save_mapping(self, filename): json.dump(self.mapping, f, cls=NumpyEncoder) print(f"Mapping saved to {filename}") - def save(self, cg_gsdfile, start=0, stop=None): + def save(self, cg_gsdfile, start=0, stop=None, stride=1): """Save the coarse-grain system to a gsd file. Does not calculate the image of the coarse-grain bead. @@ -688,6 +688,8 @@ def save(self, cg_gsdfile, start=0, stop=None): stop : int, default None Where to stop reading the gsd trajectory the system was created with. If None, will stop at the last frame. + stride : int, default 1 + The step size to use when iterating through start:stop """ typeid = [] types = [i.split("...")[0] for i in self.mapping] @@ -722,7 +724,7 @@ def save(self, cg_gsdfile, start=0, stop=None): ) as old: # stop being None is fine; slicing [0:None] gives whole array, # even in edge case where there's only one or two frames - for s in old[start:stop]: + for s in old[start:stop:stride]: new_snap = gsd.hoomd.Frame() position = [] mass = [] From c3ba3cc99b3de30b00bf700561815e0a7916f0f7 Mon Sep 17 00:00:00 2001 From: chrisjonesBSU Date: Wed, 1 May 2024 10:37:02 -0600 Subject: [PATCH 2/3] add test for stride --- grits/tests/test_coarsegrain.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/grits/tests/test_coarsegrain.py b/grits/tests/test_coarsegrain.py index cfd34b6..bfaad63 100644 --- a/grits/tests/test_coarsegrain.py +++ b/grits/tests/test_coarsegrain.py @@ -213,6 +213,22 @@ def test_benzene(self, tmp_path): cg_json = tmp_path / "cg-benzene.json" system.save_mapping(cg_json) + def test_stride(self, tmp_path): + gsdfile = path.join(asset_dir, "benzene-aa.gsd") + system = CG_System( + gsdfile, + beads={"_B": "c1ccccc1"}, + conversion_dict=amber_dict, + mass_scale=12.011, + ) + assert isinstance(system.mapping, dict) + assert len(system.mapping["_B...c1ccccc1"]) == 20 + + cg_gsd = tmp_path / "cg-benzene.gsd" + system.save(cg_gsdfile=cg_gsd, start=0, stop=-1, stride=2) + with gsd.hoomd.open(cg_gsd) as f: + assert len(f) == 3 + def test_anisobenzene(self, tmp_path): gsdfile = path.join(asset_dir, "benzene-aa.gsd") system = CG_System( From 95b1fc34209def527122b7208b94211ae495bf30 Mon Sep 17 00:00:00 2001 From: chrisjonesBSU Date: Wed, 1 May 2024 10:44:08 -0600 Subject: [PATCH 3/3] remove copied over stuff from another unit test --- grits/tests/test_coarsegrain.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/grits/tests/test_coarsegrain.py b/grits/tests/test_coarsegrain.py index bfaad63..534f471 100644 --- a/grits/tests/test_coarsegrain.py +++ b/grits/tests/test_coarsegrain.py @@ -221,9 +221,6 @@ def test_stride(self, tmp_path): conversion_dict=amber_dict, mass_scale=12.011, ) - assert isinstance(system.mapping, dict) - assert len(system.mapping["_B...c1ccccc1"]) == 20 - cg_gsd = tmp_path / "cg-benzene.gsd" system.save(cg_gsdfile=cg_gsd, start=0, stop=-1, stride=2) with gsd.hoomd.open(cg_gsd) as f: