| 1 |
.. Copyright (c) 2016-2026 The Regents of the University of Michigan |
| 2 |
.. Part of GSD, released under the BSD 2-Clause License. |
| 3 |
|
| 4 |
.. _hoomd-examples: |
| 5 |
|
| 6 |
HOOMD examples |
| 7 |
-------------- |
| 8 |
|
| 9 |
`gsd.hoomd` provides high-level access to **HOOMD** schema **GSD** files. |
| 10 |
|
| 11 |
View the page source to find unformatted example code. |
| 12 |
|
| 13 |
Import the module |
| 14 |
^^^^^^^^^^^^^^^^^ |
| 15 |
|
| 16 |
.. ipython:: python |
| 17 |
|
| 18 |
import gsd.hoomd |
| 19 |
|
| 20 |
Define a frame |
| 21 |
^^^^^^^^^^^^^^^^^ |
| 22 |
|
| 23 |
.. ipython:: python |
| 24 |
|
| 25 |
frame = gsd.hoomd.Frame() |
| 26 |
frame.particles.N = 4 |
| 27 |
frame.particles.types = ['A', 'B'] |
| 28 |
frame.particles.typeid = [0,0,1,1] |
| 29 |
frame.particles.position = [[0,0,0],[1,1,1], [-1,-1,-1], [1,-1,-1]] |
| 30 |
frame.configuration.box = [3, 3, 3, 0, 0, 0] |
| 31 |
|
| 32 |
`gsd.hoomd.Frame` stores the state of a single system configuration, or frame, in the file. |
| 33 |
Instantiate this class to create a system configuration. All fields default to `None`. Each field is |
| 34 |
written to the file when not `None` **and** when the data does not match the data in the first frame |
| 35 |
or defaults specified in the schema. |
| 36 |
|
| 37 |
Create a hoomd gsd file |
| 38 |
^^^^^^^^^^^^^^^^^^^^^^^ |
| 39 |
|
| 40 |
.. ipython:: python |
| 41 |
|
| 42 |
f = gsd.hoomd.open(name='file.gsd', mode='w') |
| 43 |
@suppress |
| 44 |
f.close() |
| 45 |
|
| 46 |
Use `gsd.hoomd.open` to open a **GSD** file as a `gsd.hoomd.HOOMDTrajectory` instance. |
| 47 |
|
| 48 |
Write frames to a gsd file |
| 49 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 50 |
|
| 51 |
.. ipython:: python |
| 52 |
|
| 53 |
def create_frame(i): |
| 54 |
frame = gsd.hoomd.Frame() |
| 55 |
frame.configuration.step = i |
| 56 |
frame.particles.N = 4+i |
| 57 |
frame.particles.position = numpy.random.random(size=(4+i,3)) |
| 58 |
return frame |
| 59 |
|
| 60 |
f = gsd.hoomd.open(name='example.gsd', mode='w') |
| 61 |
f.extend( (create_frame(i) for i in range(10)) ) |
| 62 |
f.append( create_frame(10) ) |
| 63 |
len(f) |
| 64 |
@suppress |
| 65 |
f.close() |
| 66 |
|
| 67 |
`gsd.hoomd.HOOMDTrajectory` is similar to a sequence of `gsd.hoomd.Frame` objects. The |
| 68 |
`append <gsd.hoomd.HOOMDTrajectory.append>` and `extend <gsd.hoomd.HOOMDTrajectory.extend>` methods |
| 69 |
add frames to the trajectory. |
| 70 |
|
| 71 |
.. tip:: When using `extend <gsd.hoomd.HOOMDTrajectory.extend>`, pass in a |
| 72 |
generator or generator expression to avoid storing the entire |
| 73 |
trajectory in memory before writing it out. |
| 74 |
|
| 75 |
Randomly index frames |
| 76 |
^^^^^^^^^^^^^^^^^^^^^ |
| 77 |
|
| 78 |
.. ipython:: python |
| 79 |
|
| 80 |
f = gsd.hoomd.open(name='example.gsd', mode='r') |
| 81 |
frame = f[5] |
| 82 |
frame.configuration.step |
| 83 |
frame.particles.N |
| 84 |
frame.particles.position |
| 85 |
@suppress |
| 86 |
f.close() |
| 87 |
|
| 88 |
`gsd.hoomd.HOOMDTrajectory` supports random indexing of frames in the file. |
| 89 |
Indexing into a trajectory returns a `gsd.hoomd.Frame`. |
| 90 |
|
| 91 |
Slicing and selection |
| 92 |
^^^^^^^^^^^^^^^^^^^^^ |
| 93 |
|
| 94 |
Use the slicing operator to select individual frames or a subset of a |
| 95 |
trajectory. |
| 96 |
|
| 97 |
.. ipython:: python |
| 98 |
|
| 99 |
f = gsd.hoomd.open(name='example.gsd', mode='r') |
| 100 |
|
| 101 |
for frame in f[5:-2]: |
| 102 |
print(frame.configuration.step, end=' ') |
| 103 |
|
| 104 |
every_2nd_frame = f[::2] # create a view of a trajectory subset |
| 105 |
for frame in every_2nd_frame[:4]: |
| 106 |
print(frame.configuration.step, end=' ') |
| 107 |
@suppress |
| 108 |
f.close() |
| 109 |
|
| 110 |
Slicing a trajectory creates a trajectory view, which can then be queried for |
| 111 |
length or sliced again. |
| 112 |
|
| 113 |
Pure python reader |
| 114 |
^^^^^^^^^^^^^^^^^^ |
| 115 |
|
| 116 |
.. ipython:: python |
| 117 |
|
| 118 |
f = gsd.pygsd.GSDFile(open('example.gsd', 'rb')) |
| 119 |
trajectory = gsd.hoomd.HOOMDTrajectory(f); |
| 120 |
trajectory[3].particles.position |
| 121 |
@suppress |
| 122 |
f.close() |
| 123 |
|
| 124 |
You can use **GSD** without needing to compile C code to read **GSD** files |
| 125 |
using `gsd.pygsd.GSDFile` in combination with `gsd.hoomd.HOOMDTrajectory`. It |
| 126 |
only supports the ``rb`` mode and does not read files as fast as the C |
| 127 |
implementation. It takes in a python file-like object, so it can be used with |
| 128 |
in-memory IO classes, and grid file classes that access data over the internet. |
| 129 |
|
| 130 |
.. warning:: |
| 131 |
|
| 132 |
`gsd.pygsd` is **slow**. Use `gsd.hoomd.open` whenever possible. |
| 133 |
|
| 134 |
Access logged data |
| 135 |
^^^^^^^^^^^^^^^^^^ |
| 136 |
|
| 137 |
.. ipython:: python |
| 138 |
|
| 139 |
with gsd.hoomd.open(name='log-example.gsd', mode='w') as f: |
| 140 |
frame = gsd.hoomd.Frame() |
| 141 |
frame.particles.N = 4 |
| 142 |
for i in range(10): |
| 143 |
frame.configuration.step = i*100 |
| 144 |
frame.log['particles/net_force'] = numpy.array([[-1,2,-3+i], |
| 145 |
[0,2,-4], |
| 146 |
[-3,2,1], |
| 147 |
[1,2,3]], |
| 148 |
dtype=numpy.float32) |
| 149 |
frame.log['value/potential_energy'] = 1.5+i |
| 150 |
f.append(frame) |
| 151 |
|
| 152 |
Logged data is stored in the ``log`` dictionary as numpy arrays. Place data into |
| 153 |
this dictionary directly without the ``'log/'`` prefix and gsd will include it in |
| 154 |
the output. Store per-particle quantities with the prefix ``particles/``. Choose |
| 155 |
another prefix for other quantities. |
| 156 |
|
| 157 |
.. ipython:: python |
| 158 |
|
| 159 |
log = gsd.hoomd.read_log(name='log-example.gsd', scalar_only=True) |
| 160 |
list(log.keys()) |
| 161 |
log['log/value/potential_energy'] |
| 162 |
log['configuration/step'] |
| 163 |
|
| 164 |
Read logged data from the ``log`` dictionary. |
| 165 |
|
| 166 |
.. note:: |
| 167 |
|
| 168 |
Logged data must be a convertible to a numpy array of a supported type. |
| 169 |
|
| 170 |
.. ipython:: python |
| 171 |
:okexcept: |
| 172 |
|
| 173 |
with gsd.hoomd.open(name='example.gsd', mode='w') as f: |
| 174 |
frame = gsd.hoomd.Frame() |
| 175 |
frame.particles.N = 4 |
| 176 |
frame.log['invalid'] = dict(a=1, b=5) |
| 177 |
f.append(frame) |
| 178 |
|
| 179 |
Use multiprocessing |
| 180 |
^^^^^^^^^^^^^^^^^^^ |
| 181 |
|
| 182 |
.. code:: python |
| 183 |
|
| 184 |
import multiprocessing |
| 185 |
|
| 186 |
def count_particles(args): |
| 187 |
t, frame_idx = args |
| 188 |
return len(t[frame_idx].particles.position) |
| 189 |
|
| 190 |
with gsd.hoomd.open(name='example.gsd', mode='r') as t: |
| 191 |
with multiprocessing.Pool(processes=multiprocessing.cpu_count()) as pool: |
| 192 |
result = pool.map(count_particles, [(t, frame_idx) for frame_idx in range(len(t))]) |
| 193 |
|
| 194 |
result |
| 195 |
|
| 196 |
`gsd.hoomd.HOOMDTrajectory` can be pickled when in read mode to allow for multiprocessing through |
| 197 |
Python's :py:mod:`multiprocessing` library. Here ``count_particles`` finds the number of particles |
| 198 |
in each frame and appends it to a list. |
| 199 |
|
| 200 |
Using the command line |
| 201 |
^^^^^^^^^^^^^^^^^^^^^^ |
| 202 |
|
| 203 |
The GSD library provides a command line interface for reading files with |
| 204 |
first-class support for reading HOOMD GSD files. The CLI opens a Python |
| 205 |
interpreter with a file opened in a specified mode. |
| 206 |
|
| 207 |
.. code-block:: console |
| 208 |
|
| 209 |
$ gsd read -s hoomd 'example.gsd' |
| 210 |
... |
| 211 |
File: example.gsd |
| 212 |
Number of frames: 11 |
| 213 |
|
| 214 |
The GSD file handle is available via the "handle" variable. |
| 215 |
For supported schema, you may access the trajectory using the "traj" variable. |
| 216 |
Type "help(handle)" or "help(traj)" for more information. |
| 217 |
The gsd and gsd.fl packages are always loaded. |
| 218 |
Schema-specific modules (e.g. gsd.hoomd) are loaded if available. |
| 219 |
|
| 220 |
>>> len(traj) |
| 221 |
11 |
| 222 |
>>> traj[0].particles.position.shape == (4, 3) |
| 223 |
True |
| 224 |
>>> handle.read_chunk(0, 'particles/N') |
| 225 |
array([4], dtype=uint32) |