Skip to content

Commit

Permalink
Update to GSD 3 (#59)
Browse files Browse the repository at this point in the history
* Update to GSD 3

* fix write modes

* pin freud to support gsd3
  • Loading branch information
chrisjonesBSU committed Sep 21, 2023
1 parent 6b83bc5 commit c200db4
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 41 deletions.
2 changes: 1 addition & 1 deletion cmeutils/dynamics.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def msd_from_gsd(
Choose from "window" or "direct". See Freud for the differences
https://freud.readthedocs.io/en/latest/modules/msd.html#freud.msd.MSD
"""
with gsd.hoomd.open(gsdfile, "rb") as trajectory:
with gsd.hoomd.open(gsdfile, "r") as trajectory:
init_box = trajectory[start].configuration.box
final_box = trajectory[stop].configuration.box
assert all(
Expand Down
24 changes: 12 additions & 12 deletions cmeutils/gsd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ def get_type_position(
----------
typename : str or list of str
Name of particles of which to get the positions (found in
gsd.hoomd.Snapshot.particles.types)
gsd.hoomd.Frame.particles.types)
If you want the positions of multiple types, pass in a list
e.g., ['ca', 'c3']
gsd_file : str, default None
Filename of the gsd trajectory
snap : gsd.hoomd.Snapshot, default None
snap : gsd.hoomd.Frame, default None
Trajectory snapshot
gsd_frame : int, default -1
Frame number to get positions from. Supports negative indexing.
Expand Down Expand Up @@ -70,7 +70,7 @@ def get_all_types(gsd_file=None, snap=None, gsd_frame=-1):
----------
gsd_file : str, default None
Filename of the gsd trajectory
snap : gsd.hoomd.Snapshot, default None
snap : gsd.hoomd.Frame, default None
Trajectory snapshot
gsd_frame : int, default -1
Frame number to get positions from. Supports negative indexing.
Expand All @@ -94,7 +94,7 @@ def get_molecule_cluster(gsd_file=None, snap=None, gsd_frame=-1):
----------
gsd_file : str, default None
Filename of the gsd trajectory
snap : gsd.hoomd.Snapshot, default None
snap : gsd.hoomd.Frame, default None
Trajectory snapshot.
gsd_frame : int, default -1
Frame number of gsd_file to use to compute clusters.
Expand Down Expand Up @@ -125,13 +125,13 @@ def _validate_inputs(gsd_file, snap, gsd_frame):
if gsd_file:
assert isinstance(gsd_frame, int)
try:
with gsd.hoomd.open(name=gsd_file, mode="rb") as f:
with gsd.hoomd.open(name=gsd_file, mode="r") as f:
snap = f[gsd_frame]
except Exception as e:
print("Unable to open the gsd_file")
raise e
elif snap:
assert isinstance(snap, gsd.hoomd.Snapshot)
assert isinstance(snap, gsd.hoomd.Frame)
return snap


Expand All @@ -148,15 +148,15 @@ def snap_delete_types(snap, delete_types):
Parameters
----------
snap : gsd.hoomd.Snapshot
snap : gsd.hoomd.Frame
The snapshot to read in
Returns
-------
gsd.hoomd.Snapshot
gsd.hoomd.Frame
The new snapshot with particles deleted.
"""
new_snap = gsd.hoomd.Snapshot()
new_snap = gsd.hoomd.Frame()
delete_ids = [snap.particles.types.index(i) for i in delete_types]
selection = np.where(~np.isin(snap.particles.typeid, delete_ids))[0]
new_snap.particles.N = len(selection)
Expand Down Expand Up @@ -225,7 +225,7 @@ def update_rigid_snapshot(snapshot, mb_compound):
Parameters
----------
snapshot : gsd.hoomd.Snapshot
snapshot : gsd.hoomd.Frame
The snapshot returned from create_hoomd_forcefield
or create_hoomd_simulation in mBuild
mb_compound : mbuild.Compound, required
Expand Down Expand Up @@ -307,7 +307,7 @@ def ellipsoid_gsd(gsd_file, new_file, lpar, lperp):
Value of lperp of the ellipsoids
"""
with gsd.hoomd.open(new_file, "wb") as new_t:
with gsd.hoomd.open(new_file, "w") as new_t:
with gsd.hoomd.open(gsd_file) as old_t:
for snap in old_t:
snap.particles.type_shapes = [
Expand Down Expand Up @@ -352,7 +352,7 @@ def xml_to_gsd(xmlfile, gsdfile):
overwrite=True,
)
hoomd.util.unquiet_status()
with gsd.hoomd.open(f.name) as t, gsd.hoomd.open(gsdfile, "wb") as newt:
with gsd.hoomd.open(f.name) as t, gsd.hoomd.open(gsdfile, "w") as newt:
snap = t[0]
bonds = snap.bonds.group
bonds = bonds[np.lexsort((bonds[:, 1], bonds[:, 0]))]
Expand Down
24 changes: 12 additions & 12 deletions cmeutils/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def angle_distribution(
Filename of the GSD trajectory.
A_name, B_name, C_name : str
Name(s) of particles that form the angle triplet
(found in gsd.hoomd.Snapshot.particles.types)
(found in gsd.hoomd.Frame.particles.types)
They must be given in the same order as they form the angle
start : int
Starting frame index for accumulating bond lengths.
Expand Down Expand Up @@ -78,7 +78,7 @@ def angle_distribution(
elif degrees and theta_max is None:
theta_max = 180

trajectory = gsd.hoomd.open(gsd_file, mode="rb")
trajectory = gsd.hoomd.open(gsd_file, mode="r")
name = "-".join([A_name, B_name, C_name])
name_rev = "-".join([C_name, B_name, A_name])

Expand Down Expand Up @@ -149,7 +149,7 @@ def bond_distribution(
Filename of the GSD trajectory.
A_name, B_name : str
Name(s) of particles that form the bond pair
(found in gsd.hoomd.Snapshot.particles.types)
(found in gsd.hoomd.Frame.particles.types)
start : int
Starting frame index for accumulating bond lengths.
Negative numbers index from the end. (default 0)
Expand Down Expand Up @@ -179,7 +179,7 @@ def bond_distribution(
If histogram is True, returns a 2D array of bin centers and bin heights.
"""
trajectory = gsd.hoomd.open(gsd_file, mode="rb")
trajectory = gsd.hoomd.open(gsd_file, mode="r")
name = "-".join([A_name, B_name])
name_rev = "-".join([B_name, A_name])

Expand Down Expand Up @@ -243,7 +243,7 @@ def dihedral_distribution(
Filename of the GSD trajectory.
A_name, B_name, C_name, D_name: str
Name(s) of particles that form the dihedral quadruplett
(found in gsd.hoomd.Snapshot.particles.types)
(found in gsd.hoomd.Frame.particles.types)
They must be given in the same order as they form the dihedral
start : int
Starting frame index for accumulating bond lengths.
Expand Down Expand Up @@ -273,7 +273,7 @@ def dihedral_distribution(
If histogram is True, returns a 2D array of bin centers and bin heights.
"""
trajectory = gsd.hoomd.open(gsd_file, mode="rb")
trajectory = gsd.hoomd.open(gsd_file, mode="r")
name = "-".join([A_name, B_name, C_name, D_name])
name_rev = "-".join([D_name, C_name, B_name, A_name])

Expand Down Expand Up @@ -391,7 +391,7 @@ def gsd_rdf(
Filename of the GSD trajectory.
A_name, B_name : str
Name(s) of particles between which to calculate the RDF (found in
gsd.hoomd.Snapshot.particles.types)
gsd.hoomd.Frame.particles.types)
start : int
Starting frame index for accumulating the RDF. Negative numbers index
from the end. (default 0)
Expand All @@ -413,7 +413,7 @@ def gsd_rdf(
-------
(freud.density.RDF, float)
"""
with gsd.hoomd.open(gsdfile, mode="rb") as trajectory:
with gsd.hoomd.open(gsdfile, mode="r") as trajectory:
snap = trajectory[0]

if r_max is None:
Expand Down Expand Up @@ -482,13 +482,13 @@ def get_centers(gsdfile, new_gsdfile):
new_gsdfile : str
Filename of new GSD for centers.
"""
with gsd.hoomd.open(new_gsdfile, "wb") as new_traj, gsd.hoomd.open(
gsdfile, "rb"
with gsd.hoomd.open(new_gsdfile, "w") as new_traj, gsd.hoomd.open(
gsdfile, "r"
) as traj:
snap = traj[0]
cluster_idx = gsd_utils.get_molecule_cluster(snap=snap)
for snap in traj:
new_snap = gsd.hoomd.Snapshot()
new_snap = gsd.hoomd.Frame()
new_snap.configuration.box = snap.configuration.box
f_box = freud.box.Box.from_box(snap.configuration.box)
# Use the freud box to unwrap the particle positions
Expand Down Expand Up @@ -640,7 +640,7 @@ def all_atom_rdf(
-------
freud.density.RDF
"""
with gsd.hoomd.open(gsdfile, mode="rb") as trajectory:
with gsd.hoomd.open(gsdfile, mode="r") as trajectory:
snap = trajectory[start]
if r_max is None:
# Use a value just less than half the maximum box length.
Expand Down
8 changes: 4 additions & 4 deletions cmeutils/tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ def gsdfile_images(self, tmp_path):

@pytest.fixture
def snap(self, gsdfile):
with gsd.hoomd.open(name=gsdfile, mode="rb") as f:
with gsd.hoomd.open(name=gsdfile, mode="r") as f:
snap = f[-1]
return snap

@pytest.fixture
def snap_bond(self, gsdfile_bond):
with gsd.hoomd.open(name=gsdfile_bond, mode="rb") as f:
with gsd.hoomd.open(name=gsdfile_bond, mode="r") as f:
snap = f[-1]
return snap

Expand Down Expand Up @@ -72,7 +72,7 @@ def p3ht_fresnel(self):

def create_frame(i, add_bonds, images, seed=42):
np.random.seed(seed)
s = gsd.hoomd.Snapshot()
s = gsd.hoomd.Frame()
s.configuration.step = i
s.particles.N = 5
s.particles.types = ["A", "B"]
Expand All @@ -93,7 +93,7 @@ def create_frame(i, add_bonds, images, seed=42):


def create_gsd(filename, add_bonds=False, images=False):
with gsd.hoomd.open(name=filename, mode="wb") as f:
with gsd.hoomd.open(name=filename, mode="w") as f:
f.extend(
[
create_frame(i, add_bonds=add_bonds, images=images)
Expand Down
2 changes: 1 addition & 1 deletion cmeutils/tests/test_gsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
class TestGSD(BaseTest):
def test_ellipsoid_gsd(self, butane_gsd):
ellipsoid_gsd(butane_gsd, "ellipsoid.gsd", 0.5, 1.0)
with gsd.hoomd.open(name="ellipsoid.gsd", mode="rb") as f:
with gsd.hoomd.open(name="ellipsoid.gsd", mode="r") as f:
snap = f[-1]
assert snap.particles.type_shapes[0]["type"] == "Ellipsoid"

Expand Down
14 changes: 7 additions & 7 deletions cmeutils/visualize.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ def __init__(
gsd_file : str, required
Path to a GSD file to load
frame : int, optional, default 0
The frame of the GSD file to load the gsd.hoomd.Snapshot
The frame of the GSD file to load the gsd.hoomd.Frame
view_axis : np.ndarray (3,), optional, default (1, 0, 0)
Sets the fresnel.camera attributes of camrea position and direction
color_dict : dict, optional, default None
Set colors for particle types
diameter_scale : float, optional default 0.30
Scale the diameter values stored in gsd.hoomd.Snapshot
Scale the diameter values stored in gsd.hoomd.Frame
height : float, optional default 10
Sets the fresnel.camera height attriubute
Acts like a zoom where larger values zooms out
Expand All @@ -62,7 +62,7 @@ def __init__(
upwrap_positions: bool, optional, default False
If True, the particle positions are unwrapped in the image
This requires the GSD file snapshot contain accurate values for
gsd.hoomd.Snapshot.particles.image
gsd.hoomd.Frame.particles.image
device, fresnel.Device(), optional
Set the device to be used by the scene and in rendering.
show_box: bool, optional, default True
Expand Down Expand Up @@ -113,7 +113,7 @@ def frame(self, frame):

@property
def snapshot(self):
"""gsd.hoomd.Snapshot loaded from the GSD file.
"""gsd.hoomd.Frame loaded from the GSD file.
The snapshot loaded depends on FresnelGSD.frame
"""
return self._snapshot
Expand Down Expand Up @@ -154,7 +154,7 @@ def set_type_color(self, particle_type, color):
"""
if particle_type not in set(self.particle_types):
raise ValueError(
f"Particle type of {particle_type} is not in the Snapshot"
f"Particle type of {particle_type} is not in the Frame"
)
self._color_dict[particle_type] = color

Expand All @@ -163,7 +163,7 @@ def unwrap_positions(self):
"""If set to True, then positions of the particles are
unwrapped before creating the image.
This requires that the GSD file snapshots contain accurate
image values (gsd.hoomd.Snapshot.particles.image
image values (gsd.hoomd.Frame.particles.image
"""
return self._unwrap_positions
Expand Down Expand Up @@ -290,7 +290,7 @@ def positions(self):
@property
def radius(self):
"""Sets the size of the particles.
Determined by the gsd.hoomd.Snapshot.particles.diameter
Determined by the gsd.hoomd.Frame.particles.diameter
values and FresnelGSD.diameter_scale
"""
return self.snapshot.particles.diameter * self.diameter_scale
Expand Down
4 changes: 2 additions & 2 deletions environment-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ channels:
- conda-forge
dependencies:
- fresnel
- freud
- gsd=2.9
- freud>=2.13.1
- gsd>=3.0
- hoomd<4.0=*cpu*
- mbuild
- numpy
Expand Down
4 changes: 2 additions & 2 deletions environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ channels:
- conda-forge
dependencies:
- fresnel
- freud
- gsd=2.9
- freud>=2.13.1
- gsd>=3.0
- hoomd<4.0=*cpu*
- mbuild
- numpy
Expand Down

0 comments on commit c200db4

Please sign in to comment.