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

WIP: Fix diffraction scaling. #761

Closed
wants to merge 5 commits into from
Closed
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
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON)
# https://stackoverflow.com/questions/50600708/combining-cmake-object-libraries-with-shared-libraries
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_subdirectory(CMake)
include(FindPackageMessage)
find_package_config_first(TBB)
if(TBB_FOUND)
get_target_property(_tbb_library TBB::tbb IMPORTED_LOCATION_RELEASE)
get_target_property(_tbb_include_dir TBB::tbb INTERFACE_INCLUDE_DIRECTORIES)
find_package_message(
tbb "Found TBB: ${TBB_DIR} ${_tbb_library} ${_tbb_include_dir}"
"[${_tbb_library}][${_tbb_include_dir}]")
endif()

# The TBB config doesn't define these variable, which we use later.
if(NOT DEFINED TBB_INCLUDE_DIR OR NOT DEFINED TBB_LIBRARY)
Expand Down
22 changes: 13 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 _zoom
cdef cbool _k_values_cached
cdef cbool _k_vectors_cached

Expand All @@ -88,7 +89,7 @@ cdef class DiffractionPattern(_Compute):

def _calc_proj(self, view_orientation, box):
"""Calculate the inverse shear matrix from finding the projected box
vectors whose area of parallogram is the largest.
vectors whose parallelogram area is the largest.

Args:
view_orientation ((:math:`4`) :class:`numpy.ndarray`):
Expand Down Expand Up @@ -116,6 +117,7 @@ cdef class DiffractionPattern(_Compute):
# Determine the largest projection area along the view axis and use
# that face for the projection into 2D.
best_projection_axis = np.argmax(projections)
best_projection = np.max(projections)
secondary_axes = np.array([
best_projection_axis + 1, best_projection_axis + 2]) % 3

Expand All @@ -124,9 +126,9 @@ cdef class DiffractionPattern(_Compute):

# Return the inverse shear matrix
inv_shear = np.linalg.inv(shear)
return inv_shear
return inv_shear, best_projection

def _transform(self, img, box, inv_shear, zoom):
def _transform(self, img, box, inv_shear, zoom, best_projection):
"""Zoom, shear, and scale diffraction intensities.

Args:
Expand Down Expand Up @@ -158,8 +160,9 @@ cdef class DiffractionPattern(_Compute):
roll_shift -= 0.5 / zoom

box_matrix = box.to_matrix()
ss = np.max(box_matrix) * inv_shear

# TODO: Should this use the max of the secondary indices? (i.e.
# should a big Lz affect this?)
ss = np.sqrt(best_projection) * inv_shear
shift_matrix = np.array(
[[1, 0, -roll],
[0, 1, -roll],
Expand All @@ -168,8 +171,8 @@ cdef class DiffractionPattern(_Compute):
# Translation for [roll_shift, roll_shift]
# Then shift using ss
shear_matrix = np.array(
[[ss[1, 0], ss[0, 0], roll_shift],
[ss[1, 1], ss[0, 1], roll_shift],
[[0, ss[1, 1], roll_shift],
[ss[0, 0], 0, roll_shift],
[0, 0, 1]])

zoom_matrix = np.diag((zoom, zoom, 1))
Expand Down Expand Up @@ -221,7 +224,7 @@ cdef class DiffractionPattern(_Compute):
grid_size = int(self.grid_size / zoom)

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

# Rotate points by the view quaternion and shear by the box projection
xy = rowan.rotate(view_orientation, system.points)[:, 0:2]
Expand Down Expand Up @@ -249,7 +252,7 @@ cdef class DiffractionPattern(_Compute):
# Transform the image (scale, shear, zoom) and normalize S(k) by N^2
N = len(system.points)
diffraction_frame = self._transform(
diffraction_frame, system.box, inv_shear, zoom) / (N*N)
diffraction_frame, system.box, inv_shear, zoom, best_projection) / (N*N)

# Add to the diffraction pattern and increment the frame counter
self._diffraction += np.asarray(diffraction_frame)
Expand All @@ -271,6 +274,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._zoom = zoom
self._k_values_cached = False
self._k_vectors_cached = False

Expand Down