Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions package/MDAnalysis/coordinates/timestep.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,11 @@ cdef class Timestep:
if new_dimensions is None:
self._unitcell[:] = 0
else:
new_dimensions = np.asarray(new_dimensions, dtype=np.float32)
if new_dimensions.ndim == 0 and new_dimensions < 0:
raise ValueError("Unit cell lengths must be non-negative")
if new_dimensions.size == 6 and (new_dimensions.reshape(-1)[:3] < 0).any():
raise ValueError("Unit cell lengths must be non-negative")
self._unitcell[:] = new_dimensions

@property
Expand Down
11 changes: 11 additions & 0 deletions testsuite/MDAnalysisTests/coordinates/test_timestep_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,17 @@ def test_set_zero_box(self, ts):
assert ts.dimensions is None
assert ts.triclinic_dimenions is None

@pytest.mark.parametrize("value", [-1.0, -np.inf])
def test_set_negative_length_rejected(self, ts, value):
with pytest.raises(ValueError, match="non-negative"):
ts.dimensions = [value, 2, 3, 90, 90, 90]

def test_scalar_dimensions_broadcast_is_preserved(self, ts):
ts.dimensions = 7
assert np.all(ts.dimensions == 7)
with pytest.raises(ValueError, match="non-negative"):
ts.dimensions = -1

def test_set_zero_box_triclinic(self, ts):
ts.triclinic_dimensions = np.zeros((3, 3))

Expand Down
Loading