glotzerlab / gsd

Read and write GSD files for use with HOOMD-blue.
http://gsd.readthedocs.io
BSD 2-Clause "Simplified" License
25 stars 7 forks source link

gsd.hoomd calls nonexistent method to validate snapshot when writing #155

Closed cbkerr closed 1 year ago

cbkerr commented 2 years ago

Description

Script

Came across this while working but it also fails without anything in the snapshot.

import hoomd
import gsd.hoomd

device = hoomd.device.auto_select()
snap = hoomd.Snapshot(device.communicator)

with gsd.hoomd.open('test.gsd','wb') as f:
    f.append(snap)

# or just try snap.validate()

Output

  File "[truncated but not in hoomd source directory]/debug-hoomd.py", line 8, in <module>
    f.append(s)
  File "/Users/Corwin/miniconda3/envs/py310/lib/python3.10/site-packages/gsd/hoomd.py", line 724, in append
    snapshot.validate()
AttributeError: 'Snapshot' object has no attribute 'validate'

Expected output

This is how the tutorial writes a single frame to a gsd file, so it should work. https://hoomd-blue.readthedocs.io/en/v3.1.0/tutorial/01-Introducing-Molecular-Dynamics/02-Initializing-a-Random-System.html

Configuration

Platform:

Installation method:

Versions:

Developer

joaander commented 2 years ago

The linked example code uses gsd.hoomd.Snapshot. Replace hoomd.Snapshot with gsd.hoomd.Snapshot and your code should work.

HOOMD and GSD snapshot objects at mostly duck-type compatible, but not completely. By that, I mean you read quantities like snapshot.particles.types, snapshot.particles.position, interchangeably in analysis scripts. However, operations such as changing the size of a snapshot and using them with other HOOMD and GSD API calls is not possible without a Snapshot of the correct type.

HOOMD provides from_gsd_snapshot for those cases you need to convert from a gsd.hoomd.Snapshot to a hoomd.Snapshot.

Are you requesting a similar from_hoomd_snapshot in GSD?

cbkerr commented 2 years ago

Ah that is what I didn't notice!

In my hoomd scripts, I've been building up the snapshot then using create_state_from_snapshot without the need for gsd. When I wanted to dump the first frame for debugging it felt natural to use the same syntax.

Are you requesting a similar from_hoomd_snapshot in GSD?

I think that would resolve it. Would it cause any problems if gsd could accept either kind of snapshot and convert if needed?

joaander commented 2 years ago

Ah that is what I didn't notice!

In my hoomd scripts, I've been building up the snapshot then using create_state_from_snapshot without the need for gsd. When I wanted to dump the first frame for debugging it felt natural to use the same syntax.

The later tutorials use hoomd.write.GSD.write for this. It natively supports MPI simulations. If you use gsd itself to do the write, you need to add extra logic to handle MPI appropriately. Additionally, hoomd.write.GSD.write will be faster as it is all in C++ where as gsd is written in Python/Cython and accepts a much wider range of inputs.

The first tutorial uses gsd to write the file as it demonstrates best practices to separate initialization and simulation into separate workflow steps. The initialization step then has no dependence on HOOMD and can be run and examined locally before copying to a cluster, for example.

Are you requesting a similar from_hoomd_snapshot in GSD?

I think that would resolve it. Would it cause any problems if gsd could accept either kind of snapshot and convert if needed?

You can check the implementation, but I don't think it is technically feasible for gsd to directly accept hoomd.Snapshot objects. If you want to convert hoomd.Snapshot to gsd.hoomd.Snapshot, I recommend writing a class method gsd.hoomd.Snapshot.from_hoomd_snapshot so that users can perform the conversion explicitly when needed. It should make copies of the data buffers so that it does not depend on the lifetime of the C++ managed hoomd.Snapshot data structure.

github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

github-actions[bot] commented 1 year ago

This issue has been automatically closed because it has not had recent activity.