From dbfa8ec607ba8379f7d937245943a380b55aebc1 Mon Sep 17 00:00:00 2001 From: Tommy Waltmann Date: Thu, 14 Apr 2022 13:16:54 -0400 Subject: [PATCH 1/8] added check in diffraction class --- freud/box.pyx | 6 ++++++ freud/diffraction.pyx | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/freud/box.pyx b/freud/box.pyx index 611d478a5..2d5cb138c 100644 --- a/freud/box.pyx +++ b/freud/box.pyx @@ -592,6 +592,12 @@ cdef class Box: return np.array(l_contains_mask).astype(bool) + @property + def cubic(self): + """bool: Whether the box is a cube.""" + return self.Lx == self.Ly and self.Ly == self.Lz and self.xy == self.yz\ + and self.yz == self.xz and self.xz == 0 + @property def periodic(self): r""":math:`\left(3, \right)` :class:`numpy.ndarray`: Get or set the diff --git a/freud/diffraction.pyx b/freud/diffraction.pyx index 2a2b48e3c..ed4bff17a 100644 --- a/freud/diffraction.pyx +++ b/freud/diffraction.pyx @@ -565,6 +565,9 @@ cdef class DiffractionPattern(_Compute): `GIXStapose application `_ and its predecessor, diffractometer :cite:`Jankowski2017`. + Note: + freud only supports diffraction patterns for cubic boxes. + Args: grid_size (unsigned int): Resolution of the diffraction grid (Default value = 512). @@ -731,6 +734,10 @@ cdef class DiffractionPattern(_Compute): system = freud.locality.NeighborQuery.from_system(system) + if not system.box.cubic: + raise ValueError("freud.diffraction.DiffractionPattern only " + "supports cubic boxes") + if view_orientation is None: view_orientation = np.array([1., 0., 0., 0.]) view_orientation = freud.util._convert_array( From f83e610431567f5fdb69863171390365015fe347 Mon Sep 17 00:00:00 2001 From: Tommy Waltmann Date: Thu, 14 Apr 2022 13:17:13 -0400 Subject: [PATCH 2/8] added test to diffraction class --- tests/test_diffraction_DiffractionPattern.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/test_diffraction_DiffractionPattern.py b/tests/test_diffraction_DiffractionPattern.py index 7c2cfe6e4..aa1c4c1b0 100644 --- a/tests/test_diffraction_DiffractionPattern.py +++ b/tests/test_diffraction_DiffractionPattern.py @@ -285,3 +285,13 @@ def test_cubic_system_parameterized(self): ideal_peaks[peak] = True assert all(ideal_peaks.values()) + + @pytest.mark.parametrize( + "noncubic_box_params", [dict(Lx=3, Ly=4, Lz=1), dict(Lx=3, Ly=3, Lz=3, xy=0.21)] + ) + def test_noncubic_system(self, noncubic_box_params): + box = freud.box.Box(**noncubic_box_params) + points = [[0, 0, 0]] + dp = freud.diffraction.DiffractionPattern() + with pytest.raises(ValueError): + dp.compute((box, points)) From f4eccbe6c90c6480d368439dc9d8c5d974b0600d Mon Sep 17 00:00:00 2001 From: Tommy Waltmann Date: Thu, 14 Apr 2022 13:23:41 -0400 Subject: [PATCH 3/8] add tests for cubic method of box class --- tests/test_box_Box.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/tests/test_box_Box.py b/tests/test_box_Box.py index beb34d667..490c21d61 100644 --- a/tests/test_box_Box.py +++ b/tests/test_box_Box.py @@ -374,6 +374,19 @@ def test_vectors(self): npt.assert_allclose(box.get_box_vector(2), [xz * Lz, yz * Lz, Lz]) npt.assert_allclose(box.v3, [xz * Lz, yz * Lz, Lz]) + @pytest.mark.parametrize("box_params, answer", + [(dict(Lx=1, Ly=1, Lz=1), True), + (dict(Lx=2, Ly=1, Lz=4), False), + (dict(Lx=1, Ly=1, Lz=1, xz=0.25), False), + (dict(Lx=3, Ly=3, Lz=3), True), + (dict(Lx=3, Ly=3, Lz=3, yz=0.01), False), + (dict(Lx=0.01, Ly=1, Lz=10000, xy=0.75), False), + ]) + def test_cubic(self, box_params, answer): + box = freud.box.Box(**box_params) + assert box.cubic is answer + + def test_periodic(self): box = freud.box.Box(1, 2, 3, 0, 0, 0) npt.assert_array_equal(box.periodic, True) From 1c2d7fec28c0e10f0bbb16eda97f02ce1bcf4bd6 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 14 Apr 2022 18:24:38 +0000 Subject: [PATCH 4/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- tests/test_box_Box.py | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/tests/test_box_Box.py b/tests/test_box_Box.py index 490c21d61..7967900de 100644 --- a/tests/test_box_Box.py +++ b/tests/test_box_Box.py @@ -374,19 +374,21 @@ def test_vectors(self): npt.assert_allclose(box.get_box_vector(2), [xz * Lz, yz * Lz, Lz]) npt.assert_allclose(box.v3, [xz * Lz, yz * Lz, Lz]) - @pytest.mark.parametrize("box_params, answer", - [(dict(Lx=1, Ly=1, Lz=1), True), - (dict(Lx=2, Ly=1, Lz=4), False), - (dict(Lx=1, Ly=1, Lz=1, xz=0.25), False), - (dict(Lx=3, Ly=3, Lz=3), True), - (dict(Lx=3, Ly=3, Lz=3, yz=0.01), False), - (dict(Lx=0.01, Ly=1, Lz=10000, xy=0.75), False), - ]) + @pytest.mark.parametrize( + "box_params, answer", + [ + (dict(Lx=1, Ly=1, Lz=1), True), + (dict(Lx=2, Ly=1, Lz=4), False), + (dict(Lx=1, Ly=1, Lz=1, xz=0.25), False), + (dict(Lx=3, Ly=3, Lz=3), True), + (dict(Lx=3, Ly=3, Lz=3, yz=0.01), False), + (dict(Lx=0.01, Ly=1, Lz=10000, xy=0.75), False), + ], + ) def test_cubic(self, box_params, answer): box = freud.box.Box(**box_params) assert box.cubic is answer - def test_periodic(self): box = freud.box.Box(1, 2, 3, 0, 0, 0) npt.assert_array_equal(box.periodic, True) From ba1ee6c1e6ca2d65ae07b6410667dab75251e8c9 Mon Sep 17 00:00:00 2001 From: Tommy Waltmann Date: Thu, 14 Apr 2022 14:33:02 -0400 Subject: [PATCH 5/8] update changelog and credits --- ChangeLog.md | 1 + doc/source/reference/credits.rst | 1 + 2 files changed, 2 insertions(+) diff --git a/ChangeLog.md b/ChangeLog.md index 94cfec5b2..42b226bd4 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -8,6 +8,7 @@ and this project adheres to ### Added * (breaking) Some `freud.diffraction.StaticStructureFactorDebye` property names changed to be more descriptive. +* `freud.diffraction.DiffractionPattern` now raises an exception when used with non-cubic boxes. ### Fixed * `freud.diffraction.StaticStructureFactorDebye` implementation now gives `S_k[0] = N`. diff --git a/doc/source/reference/credits.rst b/doc/source/reference/credits.rst index b7dee3c8d..0635983fe 100644 --- a/doc/source/reference/credits.rst +++ b/doc/source/reference/credits.rst @@ -329,6 +329,7 @@ Tommy Waltmann * Remove CI build configurations from CircleCI which were already covered by CIBuildWheel. * Change property names in ``StaticStructureFactorDebye`` class. * Reformat static structure factor tests. +* ``DiffractionPattern`` now raises an error when used with non-cubic boxes. Maya Martirossyan From 019f9cb4eba775dbab18fb436169901e15885ba6 Mon Sep 17 00:00:00 2001 From: tommy-waltmann <53307607+tommy-waltmann@users.noreply.github.com> Date: Fri, 15 Apr 2022 09:18:17 -0400 Subject: [PATCH 6/8] Update freud/box.pyx Co-authored-by: Vyas Ramasubramani --- freud/box.pyx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/freud/box.pyx b/freud/box.pyx index 2d5cb138c..93111c0da 100644 --- a/freud/box.pyx +++ b/freud/box.pyx @@ -595,8 +595,16 @@ cdef class Box: @property def cubic(self): """bool: Whether the box is a cube.""" - return self.Lx == self.Ly and self.Ly == self.Lz and self.xy == self.yz\ - and self.yz == self.xz and self.xz == 0 + return ( + not self.is2d + and np.allclose( + [self.Lx, self.Lx, self.Ly, self.Ly, self.Lz, self.Lz], + [self.Ly, self.Lz, self.Lx, self.Lz, self.Lx, self.Ly], + rtol=1e-5, + atol=1e-5, + ) + and np.allclose(0, [self.xy, self.yz, self.xz], rtol=1e-5, atol=1e-5) + ) @property def periodic(self): From 45f1c2e4acd5a49f0968db117c6b23b14581a092 Mon Sep 17 00:00:00 2001 From: Tommy Waltmann Date: Fri, 15 Apr 2022 09:28:02 -0400 Subject: [PATCH 7/8] fix indent --- freud/box.pyx | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/freud/box.pyx b/freud/box.pyx index 93111c0da..057844cdc 100644 --- a/freud/box.pyx +++ b/freud/box.pyx @@ -595,16 +595,16 @@ cdef class Box: @property def cubic(self): """bool: Whether the box is a cube.""" - return ( - not self.is2d - and np.allclose( - [self.Lx, self.Lx, self.Ly, self.Ly, self.Lz, self.Lz], - [self.Ly, self.Lz, self.Lx, self.Lz, self.Lx, self.Ly], - rtol=1e-5, - atol=1e-5, + return ( + not self.is2d + and np.allclose( + [self.Lx, self.Lx, self.Ly, self.Ly, self.Lz, self.Lz], + [self.Ly, self.Lz, self.Lx, self.Lz, self.Lx, self.Ly], + rtol=1e-5, + atol=1e-5, + ) + and np.allclose(0, [self.xy, self.yz, self.xz], rtol=1e-5, atol=1e-5) ) - and np.allclose(0, [self.xy, self.yz, self.xz], rtol=1e-5, atol=1e-5) - ) @property def periodic(self): From 025b314fb0ec8a9e5f397f281233512eabdabe23 Mon Sep 17 00:00:00 2001 From: Tommy Waltmann Date: Fri, 15 Apr 2022 09:39:57 -0400 Subject: [PATCH 8/8] typo --- freud/box.pyx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/freud/box.pyx b/freud/box.pyx index 057844cdc..b53400164 100644 --- a/freud/box.pyx +++ b/freud/box.pyx @@ -596,7 +596,7 @@ cdef class Box: def cubic(self): """bool: Whether the box is a cube.""" return ( - not self.is2d + not self.is2D and np.allclose( [self.Lx, self.Lx, self.Ly, self.Ly, self.Lz, self.Lz], [self.Ly, self.Lz, self.Lx, self.Lz, self.Lx, self.Ly],