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 DiffractionPattern k values/vectors #777

Merged
merged 37 commits into from
Jun 14, 2021
Merged
Changes from 3 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
a2de085
Rescale k values (validated on cubic box).
bdice May 26, 2021
e87fca1
Use full grid size.
bdice May 26, 2021
1ce917a
Save scale factor, also to k vectors.
bdice May 26, 2021
49a60ce
Update docs.
bdice Jun 2, 2021
2d77192
Update tests with comments and instructions.
bdice Jun 2, 2021
3fb56a7
Refactor diffraction plot function
tcmoore3 Jun 8, 2021
7a0c684
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 8, 2021
5082657
Merge https://github.com/glotzerlab/freud into fix/k-values
andkerr Jun 8, 2021
e9f2d56
Updated k-value and k-vector tests to check for correct values at the…
andkerr Jun 9, 2021
a2fa5a7
Merge branch 'fix/k-values' of https://github.com/glotzerlab/freud in…
andkerr Jun 9, 2021
d4a3ee5
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 9, 2021
25031b1
Streamlining a few pieces of the k-value and k-vector test per bdice'…
andkerr Jun 10, 2021
2e62ade
Cleaning up a few pieces of code to match the current branch.
andkerr Jun 10, 2021
753e809
Cleaning up code to match current branch
andkerr Jun 10, 2021
c8b839d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 10, 2021
adbfd3f
Added a very basic test to verify diffraction peak locations in a sim…
andkerr Jun 10, 2021
d92dc72
Merge branch 'fix/k-values' of https://github.com/glotzerlab/freud in…
andkerr Jun 10, 2021
1a1b5da
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 10, 2021
5716ecd
Update tests/test_diffraction_DiffractionPattern.py
andkerr Jun 11, 2021
d97da8a
Update tests/test_diffraction_DiffractionPattern.py
andkerr Jun 11, 2021
c9d9c97
Update tests/test_diffraction_DiffractionPattern.py
andkerr Jun 11, 2021
12a55cc
New test for correct diffraction peak locations in a simple cubic sys…
andkerr Jun 11, 2021
bcc1e79
Merge branch 'fix/k-values' of https://github.com/glotzerlab/freud in…
andkerr Jun 11, 2021
7a1e4f3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 11, 2021
33c8527
Updated k-vector description in diffraction.pyx docstring.
andkerr Jun 11, 2021
c558b86
Merge branch 'fix/k-values' of https://github.com/glotzerlab/freud in…
andkerr Jun 11, 2021
fd61d86
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 11, 2021
4632839
Streamlining and style fixes
andkerr Jun 14, 2021
f334c71
Merge branch 'fix/k-values' of https://github.com/glotzerlab/freud in…
andkerr Jun 14, 2021
2cd6cd3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Jun 14, 2021
64f3025
Cleaning up comments / unused variables
andkerr Jun 14, 2021
31832f3
Merge branch 'fix/k-values' of https://github.com/glotzerlab/freud in…
andkerr Jun 14, 2021
87b2947
Remove unused code.
bdice Jun 14, 2021
d5fc889
R vector.
bdice Jun 14, 2021
6871ce7
Remove noqa, it's not needed here because the line wrapped within the…
bdice Jun 14, 2021
7b92b12
Use more rigorous (imperfect) test case for peaks aligning with k*R.
bdice Jun 14, 2021
aa80c64
Make other DiffractionPattern test use approximate comparisons.
bdice Jun 14, 2021
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
16 changes: 7 additions & 9 deletions freud/diffraction.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ cdef class DiffractionPattern(_Compute):
cdef unsigned int _frame_counter
cdef double _box_matrix_scale_factor
cdef double[:] _view_orientation
cdef double _k_scale_factor
cdef cbool _k_values_cached
cdef cbool _k_vectors_cached

Expand Down Expand Up @@ -130,7 +131,7 @@ cdef class DiffractionPattern(_Compute):
"""Zoom, shear, and scale diffraction intensities.

Args:
img ((``grid_size//zoom, grid_size//zoom``) :class:`numpy.ndarray`):
img ((``grid_size, grid_size``) :class:`numpy.ndarray`):
Array of diffraction intensities.
box (:class:`~.box.Box`):
Simulation box.
Expand Down Expand Up @@ -218,8 +219,6 @@ cdef class DiffractionPattern(_Compute):
view_orientation = freud.util._convert_array(
view_orientation, (4,), np.double)

grid_size = int(self.grid_size / zoom)

# Compute the box projection matrix
inv_shear = self._calc_proj(view_orientation, system.box)

Expand All @@ -232,7 +231,7 @@ cdef class DiffractionPattern(_Compute):
xy += 0.5
xy %= 1
im, _, _ = np.histogram2d(
xy[:, 0], xy[:, 1], bins=np.linspace(0, 1, grid_size+1))
xy[:, 0], xy[:, 1], bins=np.linspace(0, 1, self.grid_size+1))

# Compute FFT and convolve with Gaussian
cdef double complex[:, :] diffraction_fft
Expand All @@ -259,8 +258,7 @@ cdef class DiffractionPattern(_Compute):
if not self._called_compute:
# Create a 1D axis of k-vector magnitudes
self._k_values_orig = np.fft.fftshift(np.fft.fftfreq(
n=self.output_size,
d=1/self.output_size))
n=self.output_size))

# Create a 3D meshgrid of k-vectors with shape
# (output_size, output_size, 3)
Expand All @@ -271,6 +269,7 @@ cdef class DiffractionPattern(_Compute):
# lazy evaluation of k-values and k-vectors
self._box_matrix_scale_factor = np.max(system.box.to_matrix())
self._view_orientation = view_orientation
self._k_scale_factor = 2 * np.pi * self.output_size / (self._box_matrix_scale_factor * zoom)
self._k_values_cached = False
self._k_vectors_cached = False

Expand Down Expand Up @@ -298,8 +297,7 @@ cdef class DiffractionPattern(_Compute):
def k_values(self):
"""(``output_size``, ) :class:`numpy.ndarray`: k-values."""
if not self._k_values_cached:
self._k_values = np.asarray(
self._k_values_orig) / self._box_matrix_scale_factor
self._k_values = np.asarray(self._k_values_orig) * self._k_scale_factor
self._k_values_cached = True
return np.asarray(self._k_values)

Expand All @@ -312,7 +310,7 @@ cdef class DiffractionPattern(_Compute):
if not self._k_vectors_cached:
self._k_vectors = rowan.rotate(
self._view_orientation,
self._k_vectors_orig) / self._box_matrix_scale_factor
self._k_vectors_orig) * self._k_scale_factor
self._k_vectors_cached = True
return np.asarray(self._k_vectors)

Expand Down