From 7bb11650181e0fcbabaaf48d490232d5c3187aa3 Mon Sep 17 00:00:00 2001 From: swf1996120 <1247155045@qq.com> Date: Fri, 30 Oct 2026 05:38:06 +0800 Subject: [PATCH 1/3] Reject invalid unit cell angles --- package/MDAnalysis/coordinates/timestep.pyx | 9 +++++++++ .../MDAnalysisTests/coordinates/test_timestep_api.py | 5 +++++ 2 files changed, 14 insertions(+) diff --git a/package/MDAnalysis/coordinates/timestep.pyx b/package/MDAnalysis/coordinates/timestep.pyx index 09e44c0f551..1f71ce09f62 100644 --- a/package/MDAnalysis/coordinates/timestep.pyx +++ b/package/MDAnalysis/coordinates/timestep.pyx @@ -468,6 +468,15 @@ 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: + values = new_dimensions.reshape(-1) + if (values[:3] < 0).any(): + raise ValueError("Unit cell lengths must be non-negative") + if not np.isfinite(values[3:]).all() or not ((values[3:] > 0) & (values[3:] < 180)).all(): + raise ValueError("Unit cell angles must be finite and strictly between 0 and 180 degrees") self._unitcell[:] = new_dimensions @property diff --git a/testsuite/MDAnalysisTests/coordinates/test_timestep_api.py b/testsuite/MDAnalysisTests/coordinates/test_timestep_api.py index 1b2bc468b3e..5cdbb81cbd4 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_timestep_api.py +++ b/testsuite/MDAnalysisTests/coordinates/test_timestep_api.py @@ -170,6 +170,11 @@ def test_dimensions_set_box(self, ts, dtype): assert ts.dimensions.dtype == np.float32 assert_allclose(ts.dimensions, self.newbox) + @pytest.mark.parametrize("angles", ([0, 90, 90], [180, 90, 90], [-1, 90, 90], [np.nan, 90, 90], [np.inf, 90, 90])) + def test_dimensions_reject_invalid_angles(self, ts, angles): + with pytest.raises(ValueError, match="angles"): + ts.dimensions = [10, 10, 10, *angles] + def test_volume(self, ts): ts.dimensions = self.newbox assert_equal(ts.volume, self.ref_volume) From 1387f05af7db7206e91921f63d9eb006bc652410 Mon Sep 17 00:00:00 2001 From: swf1996120 <1247155045@qq.com> Date: Fri, 30 Oct 2026 05:38:43 +0800 Subject: [PATCH 2/3] Reject scalar unit cell dimensions --- package/MDAnalysis/coordinates/timestep.pyx | 7 +++++-- testsuite/MDAnalysisTests/coordinates/test_timestep_api.py | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/package/MDAnalysis/coordinates/timestep.pyx b/package/MDAnalysis/coordinates/timestep.pyx index 1f71ce09f62..55c0d1e1745 100644 --- a/package/MDAnalysis/coordinates/timestep.pyx +++ b/package/MDAnalysis/coordinates/timestep.pyx @@ -469,8 +469,11 @@ cdef class Timestep: 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.ndim == 0: + if new_dimensions != 0: + raise ValueError("Unit cell dimensions must contain six values") + self._unitcell[:] = 0 + return if new_dimensions.size == 6: values = new_dimensions.reshape(-1) if (values[:3] < 0).any(): diff --git a/testsuite/MDAnalysisTests/coordinates/test_timestep_api.py b/testsuite/MDAnalysisTests/coordinates/test_timestep_api.py index 5cdbb81cbd4..5afef13e557 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_timestep_api.py +++ b/testsuite/MDAnalysisTests/coordinates/test_timestep_api.py @@ -170,6 +170,12 @@ def test_dimensions_set_box(self, ts, dtype): assert ts.dimensions.dtype == np.float32 assert_allclose(ts.dimensions, self.newbox) + def test_dimensions_reject_scalar_broadcast(self, ts): + with pytest.raises(ValueError, match="six values"): + ts.dimensions = 5 + ts.dimensions = 0 + assert ts.dimensions is None + @pytest.mark.parametrize("angles", ([0, 90, 90], [180, 90, 90], [-1, 90, 90], [np.nan, 90, 90], [np.inf, 90, 90])) def test_dimensions_reject_invalid_angles(self, ts, angles): with pytest.raises(ValueError, match="angles"): From 66488f8399cd644079dc28faea259f96568437f0 Mon Sep 17 00:00:00 2001 From: swf1996120 <1247155045@qq.com> Date: Fri, 30 Oct 2026 05:41:45 +0800 Subject: [PATCH 3/3] Format unit-cell angle tests --- testsuite/MDAnalysisTests/coordinates/test_timestep_api.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/testsuite/MDAnalysisTests/coordinates/test_timestep_api.py b/testsuite/MDAnalysisTests/coordinates/test_timestep_api.py index 5afef13e557..780f7451cd9 100644 --- a/testsuite/MDAnalysisTests/coordinates/test_timestep_api.py +++ b/testsuite/MDAnalysisTests/coordinates/test_timestep_api.py @@ -176,7 +176,10 @@ def test_dimensions_reject_scalar_broadcast(self, ts): ts.dimensions = 0 assert ts.dimensions is None - @pytest.mark.parametrize("angles", ([0, 90, 90], [180, 90, 90], [-1, 90, 90], [np.nan, 90, 90], [np.inf, 90, 90])) + @pytest.mark.parametrize( + "angles", + ([0, 90, 90], [180, 90, 90], [-1, 90, 90], [np.nan, 90, 90], [np.inf, 90, 90]), + ) def test_dimensions_reject_invalid_angles(self, ts, angles): with pytest.raises(ValueError, match="angles"): ts.dimensions = [10, 10, 10, *angles]