Skip to content

Commit

Permalink
Add seed parameter and kwargs to Pack (#184)
Browse files Browse the repository at this point in the history
* adding small projects

* add seed parameter

* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci

* removing files

* moving *kwargs argument

* [pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci

* remove unused files

* fix kwargs passing, update test

* test for fill box kwarg

* fix label name in test

* updating docstrings to add kwargs

---------

Co-authored-by: Stephanie McCallum <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: chrisjonesBSU <[email protected]>
Co-authored-by: Chris Jones <[email protected]>
  • Loading branch information
5 people authored Jan 30, 2025
1 parent 9519806 commit d3497a2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
20 changes: 17 additions & 3 deletions flowermd/base/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ class System(ABC):
Dictionary of base units to use for scaling.
Dictionary keys are "length", "mass", and "energy". Values should be an
unyt array of the desired base unit.
kwargs
See classes that inherit from System for kwargs
Warnings
--------
Expand All @@ -68,6 +70,7 @@ def __init__(
self,
molecules,
base_units=dict(),
**kwargs,
):
self._molecules = check_return_iterable(molecules)
self.all_molecules = []
Expand Down Expand Up @@ -123,7 +126,7 @@ def __init__(
self.n_mol_types += 1

# Create mBuild system
self.system = self._build_system()
self.system = self._build_system(**kwargs)
# Create GMSO topology
self.gmso_system = self._convert_to_gmso()

Expand Down Expand Up @@ -626,6 +629,10 @@ class Pack(System):
The space (nm) between the edge of the box and the molecules.
overlap : float, default 0.2
Minimum separation (nm) between particles of different molecules.
seed : int, default 12345
Change seed to be passed to PACKMOL for different starting positions
kwargs
Arguments to be passed into mbuild.packing.fill_box
.. warning::
Expand Down Expand Up @@ -655,7 +662,9 @@ def __init__(
packing_expand_factor=5,
edge=0.2,
overlap=0.2,
seed=12345,
fix_orientation=False,
**kwargs,
):
if not isinstance(density, u.array.unyt_quantity):
self.density = density * u.Unit("g") / u.Unit("cm**3")
Expand All @@ -667,10 +676,13 @@ def __init__(
self.packing_expand_factor = packing_expand_factor
self.edge = edge
self.overlap = overlap
self.seed = seed
self.fix_orientation = fix_orientation
super(Pack, self).__init__(molecules=molecules, base_units=base_units)
super(Pack, self).__init__(
molecules=molecules, base_units=base_units, **kwargs
)

def _build_system(self):
def _build_system(self, **kwargs):
mass_density = u.Unit("kg") / u.Unit("m**3")
number_density = u.Unit("m**-3")
if self.density.units.dimensions == mass_density.dimensions:
Expand All @@ -694,8 +706,10 @@ def _build_system(self):
n_compounds=[1 for i in self.all_molecules],
box=list(target_box * self.packing_expand_factor),
overlap=self.overlap,
seed=self.seed,
edge=self.edge,
fix_orientation=self.fix_orientation,
**kwargs,
)
return system

Expand Down
10 changes: 5 additions & 5 deletions flowermd/tests/base/test_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,8 @@ def test_polymer_different_chain_lengths(self, dimethylether_smiles):
bond_orientation=[None, None],
)
assert polymer.n_particles == 53
assert len(polymer.molecules[0].children) == 3
assert len(polymer.molecules[1].children) == 4
assert len(polymer.molecules[0].labels["all-monomers"]) == 3
assert len(polymer.molecules[1].labels["all-monomers"]) == 4

def test_polymer_different_num_mol(self, dimethylether_smiles):
polymer = Polymer(
Expand All @@ -220,9 +220,9 @@ def test_polymer_different_num_mol(self, dimethylether_smiles):
bond_orientation=[None, None],
)
assert polymer.n_particles == 55
assert len(polymer.molecules[0].children) == 3
assert len(polymer.molecules[1].children) == 2
assert len(polymer.molecules[2].children) == 2
assert len(polymer.molecules[0].labels["all-monomers"]) == 3
assert len(polymer.molecules[1].labels["all-monomers"]) == 2
assert len(polymer.molecules[2].labels["all-monomers"]) == 2

def test_polymer_unequal_num_mol_length(self, dimethylether_smiles):
with pytest.raises(ValueError):
Expand Down
27 changes: 27 additions & 0 deletions flowermd/tests/base/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,33 @@ def test_pack_box(self, benzene_molecule):
high_density_system.box.lengths
)

def test_pack_seed(self, benzene_molecule):
benzene_mol = benzene_molecule(n_mols=3)
default_seed = Pack(molecules=[benzene_mol], density=0.1)
change_seed = Pack(molecules=[benzene_mol], density=0.1, seed=12340)
assert not np.array_equal(
default_seed.system.xyz, change_seed.system.xyz
)

# adding test for kwargs argument in system.py Pack class
def test_pack_kwargs_attr(self, polyethylene):
polyethylene = polyethylene(lengths=5, num_mols=1)
system1 = Pack(
molecules=[polyethylene],
density=1.0,
overlap=0.2,
seed=12345,
fix_orientation=True,
)
system2 = Pack(
molecules=[polyethylene],
density=1.0,
overlap=0.2,
seed=12345,
fix_orientation=False,
)
assert not np.array_equal(system1.system.xyz, system2.system.xyz)

def test_mass(self, pps_molecule):
pps_mol = pps_molecule(n_mols=20)
system = Pack(molecules=[pps_mol], density=1.0)
Expand Down

0 comments on commit d3497a2

Please sign in to comment.