Skip to content

Commit 9fe5351

Browse files
Fix Lattice system class for coarse-grained molecules (#129)
* add test that makes lattice with bead spring polymer * add warning in align z axis method * add notes to Lattice class * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix precommit issue * fix typo --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 6698740 commit 9fe5351

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

flowermd/base/molecule.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import itertools
44
import os.path
55
import random
6+
import warnings
67
from typing import List
78

89
import mbuild as mb
@@ -211,13 +212,20 @@ def _align_backbones_z_axis(self, heavy_atoms_only=False):
211212
backbone_direction = np.array([0, 0, 1])
212213
for mol in self.molecules:
213214
if heavy_atoms_only:
214-
positions = np.array(
215-
[
216-
p.xyz[0]
217-
for p in mol.particles()
218-
if p.element.symbol != "H"
219-
]
220-
)
215+
try:
216+
positions = np.array(
217+
[
218+
p.xyz[0]
219+
for p in mol.particles()
220+
if p.element.symbol != "H"
221+
]
222+
)
223+
except AttributeError:
224+
positions = mol.xyz
225+
warnings.warn(
226+
"No element information found."
227+
"Using all particle positions to fit backbone axis."
228+
)
221229
else:
222230
positions = mol.xyz
223231
backbone = get_backbone_vector(positions)

flowermd/base/system.py

+7
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,13 @@ class Lattice(System):
712712
basis_vector : array-like, default [0.5, 0.5, 0]
713713
The vector between points in the unit cell.
714714
715+
Notes
716+
-----
717+
The system is built in a way that the long axis of the
718+
molecules is aligned with the z direction, and the
719+
lattice is made by repeating and translating in the
720+
x and y directions.
721+
715722
"""
716723

717724
def __init__(

flowermd/tests/base/test_system.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010

1111
from flowermd import Lattice, Pack
1212
from flowermd.internal.exceptions import ForceFieldError, ReferenceUnitError
13-
from flowermd.library import OPLS_AA, OPLS_AA_DIMETHYLETHER, OPLS_AA_PPS
13+
from flowermd.library import (
14+
OPLS_AA,
15+
OPLS_AA_DIMETHYLETHER,
16+
OPLS_AA_PPS,
17+
LJChain,
18+
)
1419
from flowermd.tests import BaseTest
1520

1621

@@ -811,6 +816,10 @@ def test_lattice_molecule(self, benzene_molecule):
811816
assert system.n_particles == system.hoomd_snapshot.particles.N
812817
assert system.reference_values.keys() == {"energy", "length", "mass"}
813818

819+
def test_lattice_bead_spring(self):
820+
chains = LJChain(lengths=10, num_mols=32)
821+
Lattice(molecules=chains, x=1, y=1, n=4)
822+
814823
def test_scale_charges(self, pps):
815824
pps_mol = pps(num_mols=5, lengths=5)
816825
no_scale = Pack(

flowermd/tests/library/test_polymers.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,14 @@ def test_lj_chain(self):
4747
cg_chain = LJChain(
4848
lengths=3,
4949
num_mols=1,
50-
bead_sequence=["A"],
51-
bead_mass={"A": 100},
52-
bond_lengths={"A-A": 1.5},
50+
bead_sequence=["_A"],
51+
bead_mass={"_A": 100},
52+
bond_lengths={"_A-_A": 1.5},
5353
)
5454
assert cg_chain.n_particles == 3
5555
assert cg_chain.molecules[0].mass == 300
56+
with pytest.warns():
57+
cg_chain._align_backbones_z_axis(heavy_atoms_only=True)
5658

5759
def test_lj_chain_sequence(self):
5860
cg_chain = LJChain(

0 commit comments

Comments
 (0)