From 261ba547619b7fab3d528f95f7f6c6eb315b7254 Mon Sep 17 00:00:00 2001 From: Steph McCallum Date: Fri, 28 Feb 2025 13:18:20 -0700 Subject: [PATCH 1/6] addressing issue #188 --- flowermd/library/systems.py | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/flowermd/library/systems.py b/flowermd/library/systems.py index 4668484e..4aaa402c 100644 --- a/flowermd/library/systems.py +++ b/flowermd/library/systems.py @@ -1,3 +1,36 @@ """Examples for the Systems class.""" # This is a placeholder for any class that inherits from base.system +import numpy +from scipy.spatial.distance import pdist + +class SingleChainSystem(System): + """Builds a box around a single chain. + + Calculates the maximum distance of the chain using scipy.spatial.distance.pdist(). + + Parameters + ---------- + See System class. + + """ + + def __init__(self, molecules, base_units=dict()): + super(SingleChainSystem, self).__init__( + molecules=molecules, + base_units=base_units + ) + + def _build_system(self): + chain = self.all_molecules[0] + children_pos_array = np.zeros((len(chain.children),3)) + for i in range(len(chain.children)): + children_pos_array[i] = chain.children[i].pos + eucl_dist = pdist(children_pos_array) + chain_length = np.max(eucl_dist) + box = mb.Box(lengths=np.array([chain_length] * 3) * 1.05) + comp = mb.Compound() + comp.add(chain) + comp.box = box + chain.translate_to((box.Lx / 2, box.Ly / 2, box.Lz / 2)) + return comp From 60aa623d04a6eb42f8bb5a0e4bff849bf3095a0a Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Feb 2025 20:22:02 +0000 Subject: [PATCH 2/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- flowermd/library/systems.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/flowermd/library/systems.py b/flowermd/library/systems.py index 4aaa402c..e7e5f0da 100644 --- a/flowermd/library/systems.py +++ b/flowermd/library/systems.py @@ -4,6 +4,7 @@ import numpy from scipy.spatial.distance import pdist + class SingleChainSystem(System): """Builds a box around a single chain. @@ -14,7 +15,7 @@ class SingleChainSystem(System): See System class. """ - + def __init__(self, molecules, base_units=dict()): super(SingleChainSystem, self).__init__( molecules=molecules, From 1352ea1d7fca8cebb43dab57f5f48a359e9f8459 Mon Sep 17 00:00:00 2001 From: Steph McCallum Date: Fri, 28 Feb 2025 13:58:18 -0700 Subject: [PATCH 3/6] incorporating some suggestions --- flowermd/library/systems.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/flowermd/library/systems.py b/flowermd/library/systems.py index 4aaa402c..c8df75ab 100644 --- a/flowermd/library/systems.py +++ b/flowermd/library/systems.py @@ -1,7 +1,7 @@ """Examples for the Systems class.""" -# This is a placeholder for any class that inherits from base.system -import numpy +from flowermd.base.system import System +import numpy as np from scipy.spatial.distance import pdist class SingleChainSystem(System): @@ -15,7 +15,8 @@ class SingleChainSystem(System): """ - def __init__(self, molecules, base_units=dict()): + def __init__(self, molecules, base_units=dict(),buffer=1.05): + self.buffer = buffer super(SingleChainSystem, self).__init__( molecules=molecules, base_units=base_units @@ -23,12 +24,9 @@ def __init__(self, molecules, base_units=dict()): def _build_system(self): chain = self.all_molecules[0] - children_pos_array = np.zeros((len(chain.children),3)) - for i in range(len(chain.children)): - children_pos_array[i] = chain.children[i].pos - eucl_dist = pdist(children_pos_array) + eucl_dist = pdist(self.all_molecules[0].xyz) chain_length = np.max(eucl_dist) - box = mb.Box(lengths=np.array([chain_length] * 3) * 1.05) + box = mb.Box(lengths=np.array([chain_length] * 3) * self.buffer) comp = mb.Compound() comp.add(chain) comp.box = box From e68e4a9a2b4dd56f5f3dc0705c29f96e5c96ca76 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 28 Feb 2025 21:04:40 +0000 Subject: [PATCH 4/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- flowermd/library/systems.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/flowermd/library/systems.py b/flowermd/library/systems.py index ebc1203d..81fb2f0d 100644 --- a/flowermd/library/systems.py +++ b/flowermd/library/systems.py @@ -1,9 +1,10 @@ """Examples for the Systems class.""" -from flowermd.base.system import System import numpy as np from scipy.spatial.distance import pdist +from flowermd.base.system import System + class SingleChainSystem(System): """Builds a box around a single chain. @@ -15,7 +16,7 @@ class SingleChainSystem(System): See System class. """ - + def __init__(self, molecules, base_units=dict(),buffer=1.05): self.buffer = buffer super(SingleChainSystem, self).__init__( From c7cd3283b2030621fb44a46dee11ad2dae1ffe83 Mon Sep 17 00:00:00 2001 From: Steph McCallum Date: Mon, 3 Mar 2025 16:10:17 -0700 Subject: [PATCH 5/6] starting unit tests --- flowermd/library/systems.py | 7 +-- flowermd/tests/library/test_systems.py | 70 ++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 flowermd/tests/library/test_systems.py diff --git a/flowermd/library/systems.py b/flowermd/library/systems.py index 81fb2f0d..354e0f9f 100644 --- a/flowermd/library/systems.py +++ b/flowermd/library/systems.py @@ -7,13 +7,14 @@ class SingleChainSystem(System): - """Builds a box around a single chain. + """Builds a vacuum box around a single chain. - Calculates the maximum distance of the chain using scipy.spatial.distance.pdist(). + The box lengths are chosen so they are at least as long as the largest particle distance. The maximum distance of the chain is calculated using scipy.spatial.distance.pdist(). This distance multiplied by a buffer defines the box dimensions. The chain is centered in the box. Parameters ---------- - See System class. + buffer : float, default 1.05 + A factor of the length of the chain to define the box dimensions. Needs to be greater than 1 so that the end particles are not on the boundaries. """ diff --git a/flowermd/tests/library/test_systems.py b/flowermd/tests/library/test_systems.py new file mode 100644 index 00000000..4fd36af9 --- /dev/null +++ b/flowermd/tests/library/test_systems.py @@ -0,0 +1,70 @@ +import os + +import gmso +import hoomd +import numpy as np +import pytest + +from flowermd import SingleChainSystem +from flowermd.internal.exceptions import ForceFieldError, ReferenceUnitError +from flowermd.library import ( + OPLS_AA, + LJChain, +) +from flowermd.tests import BaseTest + + +Class TestSystems(BaseTest): +''' +#test long chain,short chain,AA + def test_single_chain(self, polyethylene): + polyethylene_short = polyethylene(lengths=3, num_mols=1) + short_system = SingleChainSystem( + molecules=[polyethylene] + ) + short_system.apply_forcefield( + r_cut=2.5, force_field=[OPLS_AA()], auto_scale=True + ) + + + polyethylene_long = polyethylene(lengths=150, num_mols=1) + long_system = SingleChainSystem( + molecules=[polyethylene] + ) + long_system.apply_forcefield( + r_cut=2.5, force_field=[OPLS_AA()], auto_scale=True + ) + + #add assert for success in system being built + +#test CG chain + def test_single_chain_cg(self, polyethylene): + cg_chain = LJChain(lengths=15, num_mols=1) + system = SingleChainSystem( + molecules=[cg_chain] + ) + system.apply_forcefield( + r_cut=2.5, force_field=[OPLS_AA()], auto_scale=True + ) + + assert system.n_mol_types == 1 + assert len(system.all_molecules) == len(polyethylene.molecules) + assert len(system.hoomd_forcefield) > 0 + assert system.n_particles == system.hoomd_snapshot.particles.N + +#test buffer <= 1.0 breaks + def test_single_chain_buffer(self): + cg_chain = LJChain(lengths=15, num_mols=1) + system = SingleChainSystem( + molecules=[cg_chain], buffer=0.8 + ) + #add assert for break + +#test num_mols > 1 breaks + def test_single_chain_mols(self): + cg_chain = LJChain(lengths=15, num_mols=3) + system = SingleChainSystem( + molecules=[cg_chain] + ) + #add assert for break +''' From 1bce520ae0988d1e1d6fdc59396370c7f93c80f7 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 3 Mar 2025 23:11:35 +0000 Subject: [PATCH 6/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- flowermd/tests/library/test_systems.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/flowermd/tests/library/test_systems.py b/flowermd/tests/library/test_systems.py index 4fd36af9..cba0048b 100644 --- a/flowermd/tests/library/test_systems.py +++ b/flowermd/tests/library/test_systems.py @@ -13,7 +13,6 @@ ) from flowermd.tests import BaseTest - Class TestSystems(BaseTest): ''' #test long chain,short chain,AA @@ -26,17 +25,17 @@ def test_single_chain(self, polyethylene): r_cut=2.5, force_field=[OPLS_AA()], auto_scale=True ) - + polyethylene_long = polyethylene(lengths=150, num_mols=1) long_system = SingleChainSystem( molecules=[polyethylene] ) long_system.apply_forcefield( r_cut=2.5, force_field=[OPLS_AA()], auto_scale=True - ) - + ) + #add assert for success in system being built - + #test CG chain def test_single_chain_cg(self, polyethylene): cg_chain = LJChain(lengths=15, num_mols=1) @@ -45,13 +44,13 @@ def test_single_chain_cg(self, polyethylene): ) system.apply_forcefield( r_cut=2.5, force_field=[OPLS_AA()], auto_scale=True - ) + ) assert system.n_mol_types == 1 assert len(system.all_molecules) == len(polyethylene.molecules) assert len(system.hoomd_forcefield) > 0 assert system.n_particles == system.hoomd_snapshot.particles.N - + #test buffer <= 1.0 breaks def test_single_chain_buffer(self): cg_chain = LJChain(lengths=15, num_mols=1) @@ -59,7 +58,7 @@ def test_single_chain_buffer(self): molecules=[cg_chain], buffer=0.8 ) #add assert for break - + #test num_mols > 1 breaks def test_single_chain_mols(self): cg_chain = LJChain(lengths=15, num_mols=3)