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

Fix DiffractionPattern k values/vectors #777

merged 37 commits into from
Jun 14, 2021

Conversation

bdice
Copy link
Member

@bdice bdice commented May 26, 2021

Description

This PR rescales k values and k vectors so that they correspond to the usual conventions in condensed matter physics.

This corrects the calculation of k values/vectors such that the peaks in the image should now correspond to G vectors that satisfy the following relationship for any lattice vector R (from Wikipedia):

image

However, this has only been tested in a limited case (simple cubic system in a cubic box) and needs further testing. I am concerned that the current calculation using np.max(system.box.to_matrix()) is incorrect for boxes like {"Lx": 10, "Ly": 10, "Lz": 20} when viewing down the z-axis. It should probably be based on the plane used to project the points (the box face most aligned with the view axis).

I also changed the behavior of zoom, please see screenshots below for a description.

The notebooks used to test this behavior (and develop the correct scaling factors for NumPy's FFT frequencies) are attached here.
fft_tests.zip

To-do:

  • Add tests for different grid size, output size, zoom values with a cubic system.
  • Add documentation about the conventions of k vectors (formulas).
  • Investigate behavior for boxes that are short or long in z dimension.
  • Investigate behavior for boxes that aren't orthorhombic.
  • Investigate behavior for boxes that are 2D.

Once this is resolved, then we can investigate the problems in issue #750.

Motivation and Context

Resolves: #723

How Has This Been Tested?

Tests are needed. See to-dos above.

Screenshots

Previously, the constructor argument grid_size was divided by the compute argument zoom, and the resulting value was used as the "true" grid resolution of the histogram image and thus the FFT of that histogram. I experimented with changing this so that the zoom parameter does not affect the histogram resolution. Here is a screenshot of how that affects the behavior:

Truncated resolution, old behavior: grid_size/zoom

zoom = 4, grid_size = 512, output_size = 512
old_small

zoom = 2, grid_size = 512, output_size = 512
old_large

Full resolution, new behavior: grid_size

zoom = 4, grid_size = 512, output_size = 512
new_small

zoom = 2, grid_size = 512, output_size = 512
new_large

The difference is that the truncated resolution diffraction patterns appear to decay more quickly. I'm unsure about this, so I changed the code's behavior to use the full grid_size resolution.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds or improves functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation improvement (updates to user guides, docstrings, or developer docs)

Checklist:

  • I have read the CONTRIBUTING document.
  • My code follows the code style of this project.
  • I have updated the documentation (if relevant).
  • I have added tests that cover my changes (if relevant).
  • All new and existing tests passed.
  • I have updated the credits.
  • I have updated the Changelog.

@bdice bdice requested a review from a team as a code owner May 26, 2021 20:34
@bdice bdice requested review from alacour and removed request for a team May 26, 2021 20:34
@bdice bdice changed the title Fix DiffractionPattern k values Fix DiffractionPattern k values/vectors May 26, 2021
@codecov
Copy link

codecov bot commented May 26, 2021

Codecov Report

Merging #777 (aa80c64) into master (40f91bf) will decrease coverage by 0.09%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #777      +/-   ##
==========================================
- Coverage   55.66%   55.57%   -0.10%     
==========================================
  Files          16       16              
  Lines        2443     2438       -5     
  Branches       35       33       -2     
==========================================
- Hits         1360     1355       -5     
  Misses       1070     1070              
  Partials       13       13              
Impacted Files Coverage Δ
freud/diffraction.pyx 71.64% <100.00%> (-0.42%) ⬇️
freud/plot.py 83.25% <100.00%> (-0.23%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 40f91bf...aa80c64. Read the comment docs.

Copy link
Contributor

@alacour alacour left a comment

Choose a reason for hiding this comment

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

The changes to code look straightforward, although (visually) the intensities of the k-vectors only appear to be decaying slightly slower. Are you planning to examine noncubic boxes within this pull request?

@bdice
Copy link
Member Author

bdice commented Jun 2, 2021

The changes to code look straightforward, although (visually) the intensities of the k-vectors only appear to be decaying slightly slower. Are you planning to examine noncubic boxes within this pull request?

I haven't decided on a final scope for this pull request. I will discuss with @AKerr9509 and we will make a plan. Separating the to-do list at the top of this PR description into a series of smaller changes may be best. I propose the following:

  • In this first PR: Add very basic tests for the cubic system and then merge.
  • Second PR: Add tests for different grid size, output size, zoom values with a cubic system.
  • Third PR: Add documentation about the conventions of k vectors (formulas).
  • Fourth PR:
    • Investigate behavior for boxes that are short or long in z dimension.
    • Investigate behavior for boxes that aren't orthorhombic.
    • Investigate behavior for boxes that are 2D.

@bdice
Copy link
Member Author

bdice commented Jun 2, 2021

In a future PR, we're also going to change the normalization criteria such that S(k=0) = N. Currently it is S(k=0) = 1. The typical formulation in physics gives a result of N but we changed it to 1 so it's easier to plot. However, we can just adapt the plotting code to re-scale as needed, and keep the normal physics convention.

tcmoore3 and others added 6 commits June 8, 2021 13:52
Prior to this commit, the units of the diffraction plot were in
pixels...calling something like `ax.axhline(2*np.pi)` would put a
horizontal line on the 6th or 7th row of pixels from the top. This
commit changes that behavior so that the units in the image space
correlate to units in k-space...now calling `ax.axhline(2*np.pi)` will
put a horizontal line at y = 2*pi. This enables easy drawing on the
plots, which may be useful, e.g., to make sure peaks in s(k) correspond
to the actual lattice spacing in a crystalline system.

This change does not affect using the diffraction class to find where
the bright spots are, as that is more accurately done using the
`k_vectors` attribute of the class.
… ends of each array. Calculations used for comparison are based on np.fft.fftfreq. Any feedback on style and correctness is appreciated, in particular whether it is worth it to add tests for correct k-vectors at indices [0, -1] and [-1, 0] in addition to [0, 0] and [-1, -1].
tests/test_diffraction_DiffractionPattern.py Outdated Show resolved Hide resolved
tests/test_diffraction_DiffractionPattern.py Outdated Show resolved Hide resolved
tests/test_diffraction_DiffractionPattern.py Outdated Show resolved Hide resolved
tests/test_diffraction_DiffractionPattern.py Outdated Show resolved Hide resolved
andkerr and others added 7 commits June 9, 2021 22:22
…s suggestions. With quite a few tests that are identical except for slight changes to indices, I am wondering if there is someway to put this code in a loop.
…ple cubic system. It is sensitive to certain parameters right now; the test registers peaks best when sigma_noise = 0.1*lattice_length and vq.kmeans is only passed indices where structure factor is in the greatest 50 values.
tests/test_diffraction_DiffractionPattern.py Outdated Show resolved Hide resolved
tests/test_diffraction_DiffractionPattern.py Outdated Show resolved Hide resolved
tests/test_diffraction_DiffractionPattern.py Outdated Show resolved Hide resolved
andkerr and others added 9 commits June 10, 2021 23:24
Co-authored-by: Bradley Dice <bdice@bradleydice.com>
Co-authored-by: Bradley Dice <bdice@bradleydice.com>
Co-authored-by: Bradley Dice <bdice@bradleydice.com>
…tem with various grid size, output size, and zoom values. It isolates the brightest areas of the diffraction pattern (S(q) greater than a certain threshold), and verifies that the ideal peak locations given by k * R = 2 * pi * N are contained in these areas. I opted for this approach over locating peaks with scipy's kmeans algorithm because the random noise in the system and slight inexactness due to binning prevented kmeans from reliably locating the exact pixels which contained diffraction peaks. Parts of this test feel a bit 'hacky' right now (using a dictionary, iterating through it twice), and I am open to any/all style and efficiency suggestions.
Copy link
Member Author

@bdice bdice left a comment

Choose a reason for hiding this comment

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

A few comments for improvement. @AKerr9509 it seems like you're on the right track. Some tests are failing, take a look at the output from CI and you can see where your code isn't working (some variables aren't defined because you changed some of the names but not all). Ping me on Slack if get stuck or want help with anything.

freud/diffraction.pyx Outdated Show resolved Hide resolved
tests/test_diffraction_DiffractionPattern.py Outdated Show resolved Hide resolved
tests/test_diffraction_DiffractionPattern.py Outdated Show resolved Hide resolved
tests/test_diffraction_DiffractionPattern.py Outdated Show resolved Hide resolved
tests/test_diffraction_DiffractionPattern.py Outdated Show resolved Hide resolved
@bdice
Copy link
Member Author

bdice commented Jun 14, 2021

@AKerr9509 Good work on this! I'm going to merge since this fixes bugs and is at a good stopping point. We'll start a new pull request for follow-up work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Document/fix units for k vectors in diffraction
4 participants