Skip to content

Commit

Permalink
Merge branch 'main' of github.com:cmelab/flowerMD into log-particles
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjonesBSU committed Jul 12, 2024
2 parents 7f9152c + a8c9d8d commit ed63007
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 5 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/pytest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ jobs:

steps:
- name: Check out repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Build environment
uses: conda-incubator/setup-miniconda@v2
uses: conda-incubator/setup-miniconda@v3
with:
environment-file: environment-dev.yml
python-version: ${{ matrix.python-version }}
Expand All @@ -56,7 +56,7 @@ jobs:
run: python -m pytest -rs -v --cov=./ --cov-report=xml

- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
uses: codecov/codecov-action@v4
with:
token: ${{ secrets.CODECOV_TOKEN }}
file: ./coverage.xml
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ ci:
submodules: false
repos:
- repo: https://github.com/astral-sh/ruff-pre-commit
rev: v0.5.0 # Ruff version
rev: v0.5.1 # Ruff version
hooks:
- id: ruff
args: [--fix, --extend-ignore=E203]
Expand Down
22 changes: 21 additions & 1 deletion flowermd/base/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,11 @@ class Polymer(Molecule):
The bond length between connected atoms (units: nm)
bond_orientation: list, default None
The orientation of the bond between connected atoms.
periodic_bond_axis : str, default None
Axis which to orient the polymer backbone along.
Once the chain is aligned, a periodic bond between
head and tail atoms is formed.
Options are "x", "y", or "z"
"""

Expand All @@ -490,12 +495,14 @@ def __init__(
bond_indices=None,
bond_length=None,
bond_orientation=None,
periodic_bond_axis=None,
**kwargs,
):
self.lengths = check_return_iterable(lengths)
self.bond_indices = bond_indices
self.bond_length = bond_length
self.bond_orientation = bond_orientation
self.periodic_bond_axis = periodic_bond_axis
num_mols = check_return_iterable(num_mols)
if len(num_mols) != len(self.lengths):
raise ValueError("Number of molecules and lengths must be equal.")
Expand All @@ -513,14 +520,27 @@ def monomer(self):
return self._mb_molecule

def _build(self, length):
if self.periodic_bond_axis:
if not isinstance(
self.periodic_bond_axis, str
) or self.periodic_bond_axis.lower() not in ["x", "y", "z"]:
raise ValueError(
"Valid choices for a `periodic_bond_axis` are "
"'x', 'y', 'z'"
)
add_hydrogens = False
else:
add_hydrogens = True
chain = mbPolymer()
chain.add_monomer(
self.monomer,
indices=self.bond_indices,
separation=self.bond_length,
orientation=self.bond_orientation,
)
chain.build(n=length, sequence="A")
chain.build(n=length, sequence="A", add_hydrogens=add_hydrogens)
if self.periodic_bond_axis:
chain.create_periodic_bond(axis=self.periodic_bond_axis)
return chain

def _generate(self):
Expand Down
4 changes: 4 additions & 0 deletions flowermd/base/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,10 @@ class Lattice(System):
lattice is made by repeating and translating in the
x and y directions.
See the `periodic_bond_axis` paramter in `flowermd.base.Polymer`
if you wish to form head-tail bonds across the periodic boundary
in the lattice.
"""

def __init__(
Expand Down
19 changes: 19 additions & 0 deletions flowermd/tests/base/test_molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,22 @@ def test_align_z_axis(self, polyethylene):
for mol in pe.molecules:
backbone = get_backbone_vector(mol.xyz)
assert np.allclose(np.abs(backbone), np.array([0, 0, 1]), atol=1e-1)

@pytest.mark.parametrize("axis", ["x", "y", "z"])
def test_periodic_bond(self, polyethylene, axis):
pe_no_bond = polyethylene(num_mols=1, lengths=20)
n_bonds = pe_no_bond.molecules[0].n_bonds
n_particles = pe_no_bond.molecules[0].n_particles
pe_with_bond = polyethylene(
num_mols=1, lengths=20, periodic_bond_axis=axis
)
n_bonds_with = pe_with_bond.molecules[0].n_bonds
n_particles_with = pe_with_bond.molecules[0].n_particles
assert n_bonds - n_bonds_with == 1
assert n_particles - n_particles_with == 2

def test_periodic_bond_bad_axis(self, polyethylene):
with pytest.raises(ValueError):
polyethylene(num_mols=1, lengths=20, periodic_bond_axis=1)
with pytest.raises(ValueError):
polyethylene(num_mols=1, lengths=20, periodic_bond_axis="a")
27 changes: 27 additions & 0 deletions flowermd/tests/base/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,33 @@ def test_lattice_polymer(self, polyethylene):
np.abs(backbone), np.array([0, 0, 1]), atol=1e-1
)

@pytest.mark.parametrize("axis", ["x", "y", "z"])
def test_lattice_periodic_polymer(self, polyethylene, axis):
polyethylene = polyethylene(
lengths=10, num_mols=32, periodic_bond_axis=axis
)
system = Lattice(
molecules=[polyethylene],
x=1,
y=1,
n=4,
)
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
assert system.reference_values.keys() == {"energy", "length", "mass"}
for mol_class in system._molecules:
for mol in mol_class.molecules:
backbone = get_backbone_vector(mol.xyz)
assert np.allclose(
np.abs(backbone), np.array([0, 0, 1]), atol=1e-1
)

def test_lattice_molecule(self, benzene_molecule):
benzene_mol = benzene_molecule(n_mols=32)
system = Lattice(
Expand Down

0 comments on commit ed63007

Please sign in to comment.