HOOMD¶
gsd.hoomd
provides high-level access to HOOMD schema GSD files.
View the page source to find unformatted example code that can be easily copied.
Define a snapshot¶
In [1]: s = gsd.hoomd.Snapshot()
In [2]: s.particles.N = 4
In [3]: s.particles.types = ['A', 'B']
In [4]: s.particles.typeid = [0,0,1,1]
In [5]: s.particles.position = [[0,0,0],[1,1,1], [-1,-1,-1], [1,-1,-1]]
In [6]: s.configuration.box = [3, 3, 3, 0, 0, 0]
gsd.hoomd
represents the state of a single frame with an instance of the
class gsd.hoomd.Snapshot
. Instantiate this class to create a system
configuration. All fields default to None
and are only written into the file
if not None
and do not match the data in the first frame, or defaults specified
in the schema.
Create a hoomd gsd file¶
In [7]: gsd.hoomd.open(name='test.gsd', mode='wb')
Out[7]: <gsd.hoomd.HOOMDTrajectory at 0x7f232ed7b860>
Append frames to a gsd file¶
In [8]: def create_frame(i):
...: s = gsd.hoomd.Snapshot()
...: s.configuration.step = i
...: s.particles.N = 4+i
...: s.particles.position = numpy.random.random(size=(4+i,3))
...: return s
...:
In [9]: t = gsd.hoomd.open(name='test.gsd', mode='wb')
In [10]: t.extend( (create_frame(i) for i in range(10)) )
In [11]: t.append( create_frame(11) )
# length is 12 because extend added 10, and append added 1
In [12]: len(t)
Out[12]: 11
Use gsd.hoomd.open()
to open a GSD file with the high level interface
gsd.hoomd.HOOMDTrajectory
. It behaves like a python list
, with
gsd.hoomd.HOOMDTrajectory.append()
and gsd.hoomd.HOOMDTrajectory.extend()
methods.
Note
gsd.hoomd.HOOMDTrajectory
currently doesn’t support files opened in
append mode.
Tip
When using gsd.hoomd.HOOMDTrajectory.extend()
, pass in a generator or
generator expression to avoid storing the entire trajectory in RAM before
writing it out.
Randomly index frames¶
In [13]: t = gsd.hoomd.open(name='test.gsd', mode='rb')
In [14]: snap = t[5]
In [15]: snap.configuration.step
Out[15]: 5
In [16]: snap.particles.N