I’m trying to save the last frame of my HOOMD simulation as a gsd file in order to make a 3d histogram of the particle positions using matplotlib. I’ve tried following both the HOOMD documentation instructions on this and the gsd.hoomd instructions, but whenever I try to read the gsd file, it contains no frames.
Here’s what I tried (very basic code as an example). My matplotlib render simulation says the GSD file contains no frames.
import itertools
import math
import gsd.hoomd
import hoomd
import numpy
m = 4
N_particles = 4 * m**3
spacing = 2
K = math.ceil(N_particles ** (1 / 3))
L = K * spacing
x = numpy.linspace(- L / 2, L / 2, K, endpoint=False)
position = list(itertools.product(x, repeat=3))
frame = gsd.hoomd.Frame()
frame.particles.N = N_particles
frame.particles.position = position[0:N_particles]
frame.particles.typeid = [0] * N_particles
frame.configuration.box = [L, L, L, 0, 0, 0]
frame.particles.types = ['A']
with gsd.hoomd.open(name='lattice.gsd', mode='w') as f:
f.append(frame)
cpu = hoomd.device.CPU()
simulation = hoomd.Simulation(device=cpu, seed=1)
simulation.create_state_from_gsd(filename='lattice.gsd')
integrator = hoomd.md.Integrator(dt=0.01)
cell = hoomd.md.nlist.Cell(buffer=0.4)
lj = hoomd.md.pair.LJ(nlist=cell)
lj.params[('A', 'A')] = dict(epsilon=1, sigma=1)
lj.r_cut[('A', 'A')] = 2.5
integrator.forces.append(lj)
nvt = hoomd.md.methods.ConstantVolume(
filter=hoomd.filter.All(), thermostat=hoomd.md.methods.thermostats.Bussi(kT=0.8)
)
integrator.methods.append(nvt)
simulation.operations.integrator = integrator
simulation.state.thermalize_particle_momenta(filter=hoomd.filter.All(), kT=0.8)
simulation.run(100)
snap = gsd.hoomd.Frame()
with gsd.hoomd.open(name='example',
mode='w') as hf:
hf.append(snap)
print('done')