Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to write new snapshot that contains a subset of another snapshot's molecules. #85

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 125 additions & 0 deletions cmeutils/gsd_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,131 @@
print(f"XML data written to {gsdfile}")


def trim_snapshot_molecules(parent_snapshot, mol_indices):
"""Given a snapshot of a system, trim the snapshot to only include
a subset of the molecules.

Parameters
----------
parent_snapshot : gsd.hoomd.Frame
The snapshot to read in.
mol_indices : list of np.ndarray
List of arrays where each array contains the indices
of the particles in a molecule to include.

Returns
-------
gsd.hoomd.Frame
The new snapshot with only the specified molecules.

Notes
-----
See cmetuils.gsd_utils.get_molecule_cluster for a method to obtain
mol_indices.

"""
new_snap = gsd.hoomd.Frame()
new_snap.configuration.box = parent_snapshot.configuration.box
new_snap.particles.N = sum(len(i) for i in mol_indices)

Check warning on line 408 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L406-L408

Added lines #L406 - L408 were not covered by tests

# Write out particle info
for attr in [

Check warning on line 411 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L411

Added line #L411 was not covered by tests
"position",
"mass",
"velocity",
"orientation",
"image",
"diameter",
"angmom",
"typeid",
]:
setattr(

Check warning on line 421 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L421

Added line #L421 was not covered by tests
new_snap.particles,
attr,
np.concatenate(
list(
getattr(parent_snapshot.particles, attr)[i]
for i in mol_indices
)
),
)
new_snap.particles.types = parent_snapshot.particles.types

Check warning on line 431 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L431

Added line #L431 was not covered by tests

particle_index_map = dict()
count = 0
for indices in mol_indices:
for i in indices:
particle_index_map[i] = count
count += 1

Check warning on line 438 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L433-L438

Added lines #L433 - L438 were not covered by tests

# Write out bond info
mol_bond_groups = []
mol_bond_ids = []
for indices in mol_indices:
mask = np.any(

Check warning on line 444 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L441-L444

Added lines #L441 - L444 were not covered by tests
np.isin(parent_snapshot.bonds.group, indices.flatten()), axis=1
)
parent_mol_bonds = parent_snapshot.bonds.group[np.where(mask)[0]]
parent_mol_bond_typeids = parent_snapshot.bonds.typeid[

Check warning on line 448 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L447-L448

Added lines #L447 - L448 were not covered by tests
np.where(mask)[0]
]
new_mol_bonds = np.vectorize(particle_index_map.get)(parent_mol_bonds)
mol_bond_groups.append(new_mol_bonds)
mol_bond_ids.append(parent_mol_bond_typeids)

Check warning on line 453 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L451-L453

Added lines #L451 - L453 were not covered by tests

new_snap.bonds.types = parent_snapshot.bonds.types
new_snap.bonds.group = np.concatenate(mol_bond_groups)
new_snap.bonds.typeid = np.concatenate(mol_bond_ids)
new_snap.bonds.N = sum(len(i) for i in mol_bond_ids)

Check warning on line 458 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L455-L458

Added lines #L455 - L458 were not covered by tests

# Write out angle info
mol_angle_groups = []
mol_angle_ids = []
for indices in mol_indices:
mask = np.any(

Check warning on line 464 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L461-L464

Added lines #L461 - L464 were not covered by tests
np.isin(parent_snapshot.angles.group, indices.flatten()), axis=1
)
parent_mol_angles = parent_snapshot.angles.group[np.where(mask)[0]]
parent_mol_angle_typeids = parent_snapshot.angles.typeid[

Check warning on line 468 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L467-L468

Added lines #L467 - L468 were not covered by tests
np.where(mask)[0]
]
new_mol_angles = np.vectorize(particle_index_map.get)(parent_mol_angles)
mol_angle_groups.append(new_mol_angles)
mol_angle_ids.append(parent_mol_angle_typeids)

Check warning on line 473 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L471-L473

Added lines #L471 - L473 were not covered by tests

new_snap.angles.types = parent_snapshot.angles.types
new_snap.angles.group = np.concatenate(mol_angle_groups)
new_snap.angles.typeid = np.concatenate(mol_angle_ids)
new_snap.angles.N = sum(len(i) for i in mol_angle_ids)

Check warning on line 478 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L475-L478

Added lines #L475 - L478 were not covered by tests

# Write out dihedral info
mol_dihedral_groups = []
mol_dihedral_ids = []
for indices in mol_indices:
mask = np.any(

Check warning on line 484 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L481-L484

Added lines #L481 - L484 were not covered by tests
np.isin(parent_snapshot.dihedrals.group, indices.flatten()), axis=1
)
parent_mol_dihedrals = parent_snapshot.dihedrals.group[

Check warning on line 487 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L487

Added line #L487 was not covered by tests
np.where(mask)[0]
]
parent_mol_dihedral_typeids = parent_snapshot.dihedrals.typeid[

Check warning on line 490 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L490

Added line #L490 was not covered by tests
np.where(mask)[0]
]
new_mol_dihedrals = np.vectorize(particle_index_map.get)(

Check warning on line 493 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L493

Added line #L493 was not covered by tests
parent_mol_dihedrals
)
mol_dihedral_groups.append(new_mol_dihedrals)
mol_dihedral_ids.append(parent_mol_dihedral_typeids)

Check warning on line 497 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L496-L497

Added lines #L496 - L497 were not covered by tests

new_snap.dihedrals.types = parent_snapshot.dihedrals.types
new_snap.dihedrals.group = np.concatenate(mol_dihedral_groups)
new_snap.dihedrals.typeid = np.concatenate(mol_dihedral_ids)
new_snap.dihedrals.N = sum(len(i) for i in mol_dihedral_ids)

Check warning on line 502 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L499-L502

Added lines #L499 - L502 were not covered by tests

new_snap.validate()
return new_snap

Check warning on line 505 in cmeutils/gsd_utils.py

View check run for this annotation

Codecov / codecov/patch

cmeutils/gsd_utils.py#L504-L505

Added lines #L504 - L505 were not covered by tests


def identify_snapshot_connections(snapshot):
"""Identify angle and dihedral connections in a snapshot from bonds.

Expand Down
Loading