# hoomd.filter

Overview

 ParticleFilter Base class for all particle filters. All Select all particles in the system. CustomFilter Abstract base class for custom particle filters. Intersection Set intersection operation. Null Select no particles. SetDifference Set difference operation. Tags Select particles by tag. Type Select particles by type. Union Set union operation.

Details

Particle filters.

Particle filters describe criteria to select subsets of the particle in the system for use by various operations throughout HOOMD. To maintain high performance, filters are not re-evaluated on every use. Instead, each unique particular filter (defined by the class name and hash) is mapped to a group, an internally maintained list of the selected particles. Subsequent uses of the same particle filter specification (in the same hoomd.Simulation) will resolve to the same group and the originally selected particles, even if the state of the system has changed.

Groups are not completely static. HOOMD-blue re-evaluates the filter specifications and updates the group membership whenever the number of particles in the simulation changes. Use hoomd.update.FilterUpdater to trigger updates to groups.

For molecular dynamics simulations, each group maintains a count of the number of degrees of freedom given to the group by integration methods. This count is used by hoomd.md.compute.ThermodynamicQuantities and the integration methods themselves to compute the kinetic temperature. See hoomd.State.update_group_dof for details on when HOOMD-blue updates this count.

class hoomd.filter.ParticleFilter

Base class for all particle filters.

This class provides methods common to all particle filters.

Attention

Users should instantiate one of the subclasses. Calling ParticleFilter directly may result in an error.

__call__(state)

Evaluate the filter.

Returns

The particle tags selected by this filter.

Return type

list[int]

Note

This method may return tags that are only present on the local MPI rank. The full set of particles selected is the combination of these the lists across ranks with a set union operation.

__eq__(other)

Test for equality between two particle filters.

__hash__()

Return a hash of the filter parameters.

__str__()

Format a human readable string describing the filter.

class hoomd.filter.All

Select all particles in the system.

class hoomd.filter.CustomFilter

Abstract base class for custom particle filters.

The class allows the definition of particle filters in Python (see hoomd.filter.ParticleFilter).

Subclasses of this class must have __hash__, __eq__, and __call__ methods. The __hash__ and __eq__ methods will be used to cache the particle tags associated with a filter, thus __eq__ must correctly disambiguity any filters that would choose different particles. For more information on the Python data model see https://docs.python.org/3/reference/datamodel.html#object.__hash__ and https://docs.python.org/3/reference/datamodel.html#object.__eq__.

The example below creates a custom filter that filters out particles above and below a certain mass, and uses that filter in a hoomd.write.GSD object.

Example:

class MassFilter(hoomd.filter.CustomFilter):
def __init__(self, min_mass, max_mass):
self.min_mass = min_mass
self.max_mass = max_mass

def __hash__(self):
return hash((self.min_mass, self.max_mass))

def __eq__(self, other):
return (isinstance(other, DiameterFilter)
and self.min_mass == other.min_mass
and self.max_mass == other.max_mass)

def __call__(self, state):
with state.cpu_local_snapshot as snap:
masses = snap.particles.mass
indices = ((masses > self.min_mass)
& (masses < self.max_mass))
return np.copy(snap.particles.tag[indices])

# All particles with 1.0 < mass < 5.0
filter_ = DiameterFilter(1.0, 5.0)
gsd = hoomd.write.GSD('example.gsd', 100, filter=filter_)


Warning

Custom filters will not work with the set operation particle filters (i.e. hoomd.filter.Union, hoomd.filter.Intersection, or hoomd.filter.SetDifference). This restriction may be lifted in a future version.

abstract __call__(state)

Return the local particle tags that match the filter.

Returns the tags that are local to an MPI rank that match the particle filter. Tag numbers in a hoomd.Snapshot object are just their index.

Note

The exact requirements for the tags returned by custom filters on each MPI rank is that the set union of the returned arrays from each MPI rank be all particles that match the filter. For general use, however, it is recommended that each rank only return the tags for particles that are in the local MPI rank (excluding ghost particles). This is preferable for ease of use with local snapshots to avoid accidentally attempting to access invalid array indices from tags outside of the MPI rank.

Parameters

state (hoomd.State) – The simulation state to return the filtered tags from.

Returns

An array of MPI local tags that match the filter.

Return type
class hoomd.filter.Intersection(f, g)

Set intersection operation.

Parameters

Intersection is a composite filter. It selects particles in the set intersection $$f \cap g$$.

class hoomd.filter.Null

Select no particles.

class hoomd.filter.SetDifference(f, g)

Set difference operation.

Parameters

SetDifference is a composite filter. It selects particles in the set difference $$f \setminus g$$.

class hoomd.filter.Tags(tags)

Select particles by tag.

Parameters

tags (list[int]) – List of particle tags to select.

A particle tag is a unique identifier assigned to each particle in the simulation state. When the state is first initialized, it assigns tags 0 through N_particles to the particles in the order provided.

property tags

List of particle tags to select.

Type

list[int]

class hoomd.filter.Type(types)

Select particles by type.

Parameters

types (list[str]) – List of particle type names to select.

property types

List of particle type names to select.

Type

list[str]

class hoomd.filter.Union(f, g)

Set union operation.

Parameters

Union is a composite filter. It selects particles in the set union $$f \cup g$$.