Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/noncubic diffraction #943

Merged
merged 8 commits into from
Apr 19, 2022
Merged
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
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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's sort of odd that the names here are not fully qualified like the ones in the Changelog, but I guess that's following the existing pattern so no worries.


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))