Skip to content

Commit

Permalink
handle mix of rigid and non rigid
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjonesBSU committed Oct 30, 2024
1 parent 937f913 commit ffe639c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
8 changes: 6 additions & 2 deletions gmso/external/convert_hoomd.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,10 @@ def _parse_particle_information(
# Check for rigid molecules
rigid_mols = any([site.molecule.isrigid for site in top.sites])
if rigid_mols:
rigid_ids = [site.molecule.number for site in top.sites]
rigid_ids_set = set(rigid_ids)
rigid_ids = [
site.molecule.number if site.molecule.isrigid else -1 for site in top.sites
]
rigid_ids_set = set([i for i in rigid_ids if i != -1])
n_rigid = len(rigid_ids_set)
rigid_charges = np.zeros(n_rigid) * charges.units
rigid_masses = np.zeros(n_rigid) * masses.units
Expand All @@ -314,6 +316,8 @@ def _parse_particle_information(
typeids = np.concatenate((np.array([0] * n_rigid), typeids + 1))
# Update mass list and position list of Frame
for idx, _id in enumerate(rigid_ids_set):
if _id == 1:
continue
group_indices = np.where(np.array(rigid_ids) == _id)[0]
group_positions = xyz[group_indices]
group_masses = masses[group_indices]
Expand Down
23 changes: 23 additions & 0 deletions gmso/tests/test_hoomd.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,29 @@ def test_rigid_bodies(self):
):
assert np.array_equal(np.array(group1), np.array(group2) + 2)

def test_rigid_bodies_mix(self):
ethane = mb.lib.molecules.Ethane()
ethane.name = "ethane"
benzene = mb.load("c1ccccc1", smiles=True)
benzene.name = "benzene"
box = mb.fill_box([benzene, ethane], n_compounds=[1, 1], box=[2, 2, 2])
top = from_mbuild(box)
for site in top.sites:
if site.molecule.name == "benzene":
site.molecule.isrigid = True

snapshot, refs = to_gsd_snapshot(top)
assert snapshot.particles.typeid[0] == 0
assert snapshot.particles.N == top.n_sites + 1
assert np.array_equal(
snapshot.particles.body[-ethane.n_particles :],
np.array([-1] * ethane.n_particles),
)
assert np.array_equal(
snapshot.particles.body[1 : benzene.n_particles + 1],
np.array([0] * benzene.n_particles),
)

@pytest.mark.skipif(
int(hoomd_version[0]) < 4, reason="Unsupported features in HOOMD 3"
)
Expand Down

0 comments on commit ffe639c

Please sign in to comment.