Skip to content

Commit

Permalink
Merge pull request #943 from glotzerlab/fix/noncubic-diffraction
Browse files Browse the repository at this point in the history
Fix/noncubic diffraction
  • Loading branch information
tommy-waltmann authored Apr 19, 2022
2 parents c09e072 + 025b314 commit b11763c
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
1 change: 1 addition & 0 deletions doc/source/reference/credits.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions freud/box.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,20 @@ cdef class Box:

return np.array(l_contains_mask).astype(bool)

@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,
)
and np.allclose(0, [self.xy, self.yz, self.xz], rtol=1e-5, atol=1e-5)
)

@property
def periodic(self):
r""":math:`\left(3, \right)` :class:`numpy.ndarray`: Get or set the
Expand Down
7 changes: 7 additions & 0 deletions freud/diffraction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,9 @@ cdef class DiffractionPattern(_Compute):
`GIXStapose application <https://github.com/cmelab/GIXStapose>`_ 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).
Expand Down Expand Up @@ -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(
Expand Down
15 changes: 15 additions & 0 deletions tests/test_box_Box.py
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +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),
],
)
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)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_diffraction_DiffractionPattern.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit b11763c

Please sign in to comment.