Skip to content

Commit

Permalink
remove rigid body help funcs and tests, pin freud verisons
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisjonesBSU committed Mar 11, 2025
1 parent 3ca96e8 commit 1b01480
Show file tree
Hide file tree
Showing 4 changed files with 2 additions and 130 deletions.
103 changes: 0 additions & 103 deletions cmeutils/gsd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import numpy as np
from boltons.setutils import IndexedSet

from cmeutils.geometry import moit


def frame_to_freud_system(frame, ref_length=None):
"""Creates a freud system given a gsd.hoomd.Frame.
Expand Down Expand Up @@ -217,107 +215,6 @@ def snap_delete_types(snap, delete_types):
return new_snap


def create_rigid_snapshot(mb_compound):
"""Preps a hoomd snapshot to store rigid body information
This method relies on using built-in mBuild methods to
create the rigid body information.
Parameters
----------
mb_compound : mbuild.Compound, required
mBuild compound containing the rigid body information
of the complete system
Returns
-------
hoomd.Snapshot
A snapshot that contains the needed place-holder particles
to set up a rigid body simulation in HoomdBlue Version 3.
Pass this snapshot into the `init_snap` parameter
of mBuild's `create_hoomd_forcefield` function.
"""
import hoomd

rigid_ids = [p.rigid_id for p in mb_compound.particles()]
rigid_bodies = set(rigid_ids)
# Total number of rigid particles
N_mols = len(rigid_bodies)
init_snap = hoomd.Snapshot()
# Create place holder spots in the snapshot for rigid centers
init_snap.particles.types = ["R"]
init_snap.particles.N = N_mols
return init_snap


def update_rigid_snapshot(snapshot, mb_compound):
"""Update a snapshot prepared for rigid bodies with system informaiton
Parameters
----------
snapshot : gsd.hoomd.Frame
The snapshot returned from create_hoomd_forcefield
or create_hoomd_simulation in mBuild
mb_compound : mbuild.Compound, required
mBuild compound containing the rigid body information
of the complete system
"""
import hoomd

rigid_ids = [p.rigid_id for p in mb_compound.particles()]
rigid_bodies = set(rigid_ids)
# Total number of rigid body particles
N_mols = len(rigid_bodies)
N_p = [rigid_ids.count(i) for i in rigid_bodies]
# Right now, we're assuming each molecule has the same num of particles
assert len(set(N_p)) == 1
N_p = N_p[0] # Number of particles per molecule
mol_inds = [
np.arange(N_mols + i * N_p, N_mols + i * N_p + N_p)
for i in range(N_mols)
]

for i, inds in enumerate(mol_inds):
total_mass = np.sum(snapshot.particles.mass[inds])
com = (
np.sum(
snapshot.particles.position[inds]
* snapshot.particles.mass[inds, np.newaxis],
axis=0,
)
/ total_mass
)
snapshot.particles.position[i] = com
snapshot.particles.body[i] = i
snapshot.particles.body[inds] = i * np.ones_like(inds)
snapshot.particles.mass[i] = np.sum(snapshot.particles.mass[inds])
snapshot.particles.moment_inertia[i] = moit(
snapshot.particles.position[inds],
snapshot.particles.mass[inds],
center=com,
)

rigid = hoomd.md.constrain.Rigid()
inds = mol_inds[0]
r_pos = snapshot.particles.position[0]
c_pos = snapshot.particles.position[inds]
c_pos -= r_pos
c_pos = [tuple(i) for i in c_pos]
c_types = [
snapshot.particles.types[i] for i in snapshot.particles.typeid[inds]
]
c_orient = [tuple(i) for i in snapshot.particles.orientation[inds]]

rigid.body["R"] = {
"constituent_types": c_types,
"positions": c_pos,
"orientations": c_orient,
}
return snapshot, rigid


def ellipsoid_gsd(gsd_file, new_file, ellipsoid_types, lpar, lperp):
"""Add needed information to GSD file to visualize ellipsoids.
Expand Down
25 changes: 0 additions & 25 deletions cmeutils/tests/test_gsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,16 @@
import pytest
from base_test import BaseTest
from gmso.external import from_mbuild, to_gsd_snapshot
from mbuild.formats.hoomd_forcefield import to_hoomdsnapshot

from cmeutils.gsd_utils import (
_validate_inputs,
create_rigid_snapshot,
ellipsoid_gsd,
frame_to_freud_system,
get_all_types,
get_molecule_cluster,
get_type_position,
identify_snapshot_connections,
snap_delete_types,
update_rigid_snapshot,
xml_to_gsd,
)

Expand Down Expand Up @@ -47,28 +44,6 @@ def test_ellipsoid_gsd(self, butane_gsd):
snap = f[-1]
assert snap.particles.type_shapes[0]["type"] == "Ellipsoid"

def test_create_rigid_snapshot(self):
benzene = mb.load("c1ccccc1", smiles=True)
benzene.name = "Benzene"
box = mb.fill_box(benzene, 5, box=[3, 3, 3], seed=42)
box.label_rigid_bodies(discrete_bodies="Benzene")

rigid_init_snap = create_rigid_snapshot(box)
assert rigid_init_snap.particles.N == 5
assert rigid_init_snap.particles.types == ["R"]

def test_update_rigid_snapshot(self):
benzene = mb.load("c1ccccc1", smiles=True)
benzene.name = "Benzene"
box = mb.fill_box(benzene, 5, box=[3, 3, 3], seed=42)
box.label_rigid_bodies(discrete_bodies="Benzene")

rigid_init_snap = create_rigid_snapshot(box)
_snapshot, refs = to_hoomdsnapshot(box, hoomd_snapshot=rigid_init_snap)
snap, rigid_obj = update_rigid_snapshot(_snapshot, box)
assert snap.particles.N == 65
assert np.array_equal(snap.particles.typeid[0:5], np.array([0] * 5))

def test_get_type_position(self, gsdfile):
pos_array = get_type_position(gsd_file=gsdfile, typename="A")
assert type(pos_array) is type(np.array([]))
Expand Down
2 changes: 1 addition & 1 deletion environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: cmeutils-dev
channels:
- conda-forge
dependencies:
- freud >= 3.0
- freud >= 3.0,<3.2
- gmso >=0.11.2
- fresnel >=0.13.5
- gsd >=3.0
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: cmeutils
channels:
- conda-forge
dependencies:
- freud >= 3.0
- freud >= 3.0,<3.2
- gmso >=0.11.2
- fresnel >=0.13.5
- gsd >=3.0
Expand Down

0 comments on commit 1b01480

Please sign in to comment.