gsd.hoomd module

hoomd schema reference implementation

The main package gsd.hoomd is a reference implementation of the GSD schema hoomd. It is a simple, but high performance and memory efficient, reader and writer for the schema. See HOOMD for full examples.

class gsd.hoomd.BondData(M)

Store bond data chunks.

Users should not need to instantiate this class. Use the bonds, angles, dihedrals, or impropers attribute of a Snapshot.

Instances resulting from file read operations will always store per bond quantities in numpy arrays of the defined types. User created snapshots can provide input data as python lists, tuples, numpy arrays of different types, etc… Such input elements will be converted to the appropriate array type by validate() which is called when writing a frame.

Note

M varies depending on the type of bond. The same python class represents all types of bonds.

Type M
Bond 2
Angle 3
Dihedral 4
Improper 4
N

Number of particles in the snapshot (bonds/N, angles/N, dihedrals/N, impropers/N, pairs/N).

Type:int
types

Names of the particle types (bonds/types, angles/types, dihedrals/types, impropers/types, pairs/types).

Type:list[str]
typeid
Type:numpy.ndarray or array_like [uint32, ndim=1, mode=’c’]
group
Type:numpy.ndarray or array_like [uint32, ndim=2, mode=’c’]
validate()

Validate all attributes.

First, convert every per bond attribute to a numpy array of the proper type. Then validate that all attributes have the correct dimensions.

Ignore any attributes that are None.

Warning

Per bond attributes that are not contiguous numpy arrays will be replaced with contiguous numpy arrays of the appropriate type.

class gsd.hoomd.ConfigurationData

Store configuration data.

Users should not need to instantiate this class. Use the configuration attribute of a Snapshot.

step

Time step of this frame (configuration/step).

Type:int
dimensions

Number of dimensions (configuration/dimensions).

Type:int
box

Box dimensions (configuration/box) - [lx, ly, lz, xy, xz, yz].

Type:numpy.ndarray or array_like [float, ndim=1, mode=’c’]
validate()

Validate all attributes.

First, convert every array attribute to a numpy array of the proper type. Then validate that all attributes have the correct dimensions.

Ignore any attributes that are None.

Warning

Array attributes that are not contiguous numpy arrays will be replaced with contiguous numpy arrays of the appropriate type.

class gsd.hoomd.ConstraintData

Store constraint data chunks.

Users should not need to instantiate this class. Use the constraints, attribute of a Snapshot.

Instances resulting from file read operations will always store per constraint quantities in numpy arrays of the defined types. User created snapshots can provide input data as python lists, tuples, numpy arrays of different types, etc… Such input elements will be converted to the appropriate array type by validate() which is called when writing a frame.

N

Number of particles in the snapshot (constraints/N).

Type:int
value

N length array defining constraint lengths (constraints/value).

Type:numpy.ndarray or array_like [float32, ndim=1, mode=’c’]
group

Nx2 array defining tags in the particle constraints (constraints/group).

Type:numpy.ndarray or array_like [int32, ndim=2, mode=’c’]
validate()

Validate all attributes.

First, convert every per constraint attribute to a numpy array of the proper type. Then validate that all attributes have the correct dimensions.

Ignore any attributes that are None.

Warning

Per bond attributes that are not contiguous numpy arrays will be replaced with contiguous numpy arrays of the appropriate type.

class gsd.hoomd.HOOMDTrajectory(file)

Read and write hoomd gsd files.

Parameters:file (gsd.fl.GSDFile) – File to access.

Create hoomd GSD files with create().

append(snapshot)

Append a snapshot to a hoomd gsd file.

Parameters:snapshot (Snapshot) – Snapshot to append.

Write the given snapshot to the file at the current frame and increase the frame counter. Do not attempt to write any fields that are None. For all non-None fields, scan them and see if they match the initial frame or the default value. If the given data differs, write it out to the frame. If it is the same, do not write it out as it can be instantiated either from the value at the initial frame or the default value.

extend(iterable)

Append each item of the iterable to the file.

Parameters:iterable – An iterable object the provides Snapshot instances. This could be another HOOMDTrajectory, a generator that modifies snapshots, or a simple list of snapshots.
read_frame(idx)

Read the frame at the given index from the file.

Parameters:idx (int) – Frame index to read.
Returns:Snapshot with the frame data

Replace any data chunks not present in the given frame with either data from frame 0, or initialize from default values if not in frame 0. Cache frame 0 data to avoid file read overhead. Return any default data as non-writable numpy arrays.

truncate()

Remove all frames from the file.

class gsd.hoomd.ParticleData

Store particle data chunks.

Users should not need to instantiate this class. Use the particles attribute of a Snapshot.

Instances resulting from file read operations will always store per particle quantities in numpy arrays of the defined types. User created snapshots can provide input data as python lists, tuples, numpy arrays of different types, etc… Such input elements will be converted to the appropriate array type by validate() which is called when writing a frame.

N

Number of particles in the snapshot (particles/N).

Type:int
types

Names of the particle types (particles/types).

Type:list[str]
position

Nx3 array defining particle position (particles/position).

Type:numpy.ndarray or array_like [float, ndim=2, mode=’c’]
orientation

Nx4 array defining particle position (particles/orientation).

Type:numpy.ndarray or array_like [float, ndim=2, mode=’c’]
typeid

N length array defining particle type ids (particles/typeid).

Type:numpy.ndarray or array_like [uint32, ndim=1, mode=’c’]
mass

N length array defining particle masses (particles/mass).

Type:numpy.ndarray or array_like [float, ndim=1, mode=’c’]
charge

N length array defining particle charges (particles/charge).

Type:numpy.ndarray or array_like [float, ndim=1, mode=’c’]
diameter

N length array defining particle diameters (particles/diameter).

Type:numpy.ndarray or array_like [float, ndim=1, mode=’c’]
body

N length array defining particle bodies (particles/body).

Type:numpy.ndarray or array_like [int32, ndim=1, mode=’c’]
moment_inertia

Nx3 array defining particle moments of inertia (particles/moment_inertia).

Type:numpy.ndarray or array_like [float, ndim=2, mode=’c’]
velocity

Nx3 array defining particle velocities (particles/velocity).

Type:numpy.ndarray or array_like [float, ndim=2, mode=’c’]
angmom

Nx4 array defining particle angular momenta (particles/angmom).

Type:numpy.ndarray or array_like [float, ndim=2, mode=’c’]
image

Nx3 array defining particle images (particles/image).

Type:numpy.ndarray or array_like [int32, ndim=2, mode=’c’]
validate()

Validate all attributes.

First, convert every per particle attribute to a numpy array of the proper type. Then validate that all attributes have the correct dimensions.

Ignore any attributes that are None.

Warning

Per particle attributes that are not contiguous numpy arrays will be replaced with contiguous numpy arrays of the appropriate type.

class gsd.hoomd.Snapshot

Top level snapshot container.

configuration

Configuration data.

Type:ConfigurationData
particles

Particle data snapshot.

Type:ParticleData
bonds

Bond data snapshot.

Type:BondData
angles

Angle data snapshot.

Type:BondData
dihedrals

Dihedral data snapshot.

Type:BondData
impropers

Improper data snapshot.

Type:BondData
pairs

Special pair interactions snapshot

Type:BondData
state

Dictionary containing state data

Type:dict

See the HOOMD schema specification for details on entries in the state dictionary. Entries in this dict are the chunk name without the state prefix. For example, state/hpmc/sphere/radius is stored in the dictionary entry state['hpmc/sphere/radius'].

validate()

Validate all contained snapshot data.

gsd.hoomd.create(name, snapshot=None)

Create a hoomd gsd file from the given snapshot.

Parameters:
  • name (str) – File name.
  • snapshot (Snapshot) – Snapshot to write to frame 0. No frame is written if snapshot is None.

Deprecated since version 1.2: As of version 1.2, you can create and open hoomd GSD files in the same call to open(). create() is kept for backwards compatibility.

Danger

The file is overwritten if it already exists.

gsd.hoomd.open(name, mode='rb')

Open a hoomd schema GSD file.

The return value of open() can be used as a context manager.

Parameters:
  • name (str) – File name to open.
  • mode (str) – File open mode.
Returns:

An HOOMDTrajectory instance that accesses the file name with the given mode.

Valid values for mode:

mode description
'rb' Open an existing file for reading.
'rb+' Open an existing file for reading and writing. Inefficient for large files.
'wb' Open a file for writing. Creates the file if needed, or overwrites an existing file.
'wb+' Open a file for reading and writing. Creates the file if needed, or overwrites an existing file. Inefficient for large files.
'xb' Create a gsd file exclusively and opens it for writing. Raise an FileExistsError exception if it already exists.
'xb+' Create a gsd file exclusively and opens it for reading and writing. Raise an FileExistsError exception if it already exists. Inefficient for large files.
'ab' Open an existing file for writing. Does not create or overwrite existing files.

New in version 1.2.