Skip to content
Merged
14 changes: 7 additions & 7 deletions src/lammpsio/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ def from_hoomd_gsd(cls, frame):
frame.validate()

# process HOOMD box to LAMMPS box
L = frame.configuration.box[:3]
tilt = frame.configuration.box[3:]
L = frame.configuration.box.copy()[:3]
tilt = frame.configuration.box.copy()[3:]
if frame.configuration.dimensions == 3:
tilt[0] *= L[1]
tilt[1:] *= L[2]
Expand Down Expand Up @@ -225,12 +225,12 @@ def to_hoomd_gsd(self, type_map=None):
if self.step is not None:
frame.configuration.step = int(self.step)

# we could shift the box later, but for now this is an error
if not numpy.allclose(-self.box.low, self.box.high):
raise ValueError("GSD boxes must be centered around 0")
L = self.box.high - self.box.low
center = 0.5 * (self.box.high + self.box.low)
if self.box.tilt is not None:
tilt = self.box.tilt
tilt = self.box.tilt.copy()
tilt[0] /= L[1]
tilt[1:] /= L[2]
else:
tilt = [0, 0, 0]
frame.configuration.box = numpy.concatenate((L, tilt))
Expand All @@ -247,7 +247,7 @@ def to_hoomd_gsd(self, type_map=None):

frame.particles.N = self.N
if self.has_position():
frame.particles.position = self.position.copy()
frame.particles.position = self.position.copy() - center
if self.has_velocity():
frame.particles.velocity = self.velocity.copy()
if self.has_image():
Expand Down
10 changes: 7 additions & 3 deletions tests/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,14 @@ def test_gsd_conversion():
snap2.to_hoomd_gsd()
assert numpy.all(snap2.id == [2, 1])

# check for error out on bad box
# check that box is set correctly when not originally centered
snap2.id = [1, 2]
snap2.position = [[0, 0, 0], [1, 1, 1]]
snap2.box.low = [-10, -10, -10]
with pytest.raises(ValueError):
snap2.to_hoomd_gsd()
center = (snap2.box.high + snap2.box.low) / 2
frame5 = snap2.to_hoomd_gsd()
assert numpy.allclose(frame5.configuration.box, [15, 15, 15, 0, 0, 0])
assert numpy.allclose(frame5.particles.position, snap2.position - center)


@pytest.mark.skipif(not has_gsd, reason="gsd not installed")
Expand Down