Repository navigation
Sample all k vector directions in StaticStructureFactorDirect - #1506
Conversation
reciprocal_isotropic used unsigned grid indices starting at 0, so only the positive octant (quadrant in 2D) of k space was sampled. Use signed indices over a half space instead, since S(k) = S(-k). Also fix the kz/ky range calculation for triclinic boxes and update the pruning constants so num_sampled_k_points keeps its meaning. Added a unit test that fails without these changes and passes with them.
|
Thanks for the PR. I will take a look at this at some point this week hopefully, if not - first thing next week. |
…ve diverged over time.
@chrisjonesBSU is not a Glotzer Lab member, so he does not have access to make a branch on the main repository. External developers should make pull requests from their public forks. |
… convention and improve performance.
… the final version of the changes
|
Thanks @DomFijan, all of your requested changes should be there. Any thoughts about replacing |
If the performance is noticeably better that would be great, especially since nothing changes mathematically (should be equivalent by Euler's formula). However, I think it would be better to have a separate PR, since this is not in any way related to the issues of sampling k vector directions. A benchmark with concrete numbers on single and many cores would be nice to get a feel for the impact on the performance. |
DomFijan
left a comment
There was a problem hiding this comment.
I think we are good to merge this now.
Sounds good. There is one other place in freud that would benefit from the same change as well. This is outside of the diffraction module, so even more of a reason to do a separate PR. I can open that soon. |
Description
All changes are in
reciprocal_isotropicand its helpers inStaticStructureFactorDirect.cc, in both the 3D and 2D implementations. The grid indices are now signed ints, and it now samples half of the total grid, rather than an octant, because S(k) = S(-k).Previously the indices were unsigned int starting at 0, so only the positive octant (quadrant in 2D) was sampled.
Other changes (some of these issues were hidden by having only positive vectors):
evaluate_quadratic_equationnow returns the integer range bracketing both + and - roots, or an empty range when the discriminant is negative. Previously it used only the + root, and when the discriminant was negative it cast aNaNto unsigned int, which is undefined behavior. Thek_maxrange bounds the innermost loop, and thek_minrange skips points insidek_min.The normalization in
get_prune_distance3Dandget_prune_distance2Dis updated from an octant/quadrant to a half space/half plane (changed multiplicative prefactors) sonum_sampled_k_pointsstill gives about the requested number of k points.The following changes fix issues with triclinic boxes.
Sign of the linear term,
coef_bbecomes2 * dot(...)instead of-2 * dot(...). This only affects triclinic boxes, where the dot product is nonzero. For triclinic boxes, the dot product can be positive or negative, we should leave the sign as is before passing into the quadratic solver, which uses-coef_bwhen calculating the center. This was shifting the center, causing the quadratic solver to drop some possible candiddate indices.Index bounds,
ceil(q_max * |a_i|), where|a_i|is the length of real-space box vector i, instead ofceil(q_max / |b_i|), which can stop short in triclinic boxes. The separate bound on the innermost index is removed because the quadratic gives that range.As mentioned in #1500, this PR was made with the assistance of Claude code.
Motivation and Context
Previously, only the all-positive octant was being sampled when generating k vectors because of the use of unsigned int types and counting up during vector generation. This gave incorrect S(k) results for anisotropic structures
Resolves: #1500
How Has This Been Tested?
A unit test was added on a simple system where S(k) can easily be computed via numpy.
test_oriented_system_against_full_gridplaces two particles in a box. For two particles, S(k) = 1 + cos(k·d), so the test compares freud's binned S(k) with a numpy average over every point of the reciprocal grid. It runs in a cubic box, a triclinic box and a 2D box. It fails on the current code in all three boxes and passes with this fix.A couple notes for maintainers:
I think the test against dynasor unit test will always be skipped now if the CI workflow is installing the latest release of dynasor. Its package level name is no longer
dsfafter their 2.0 release, and the test only runs if it can detectdsf. We could update the guard to test against latest versions of dynasor or replace it with the new test added by this PR. I haven't ran the test with the latest version of dynasor.There's a one-line change that seems to recover the ~3.5x slowdown from sampling the full grid. In
compute_F_k, replacingF_ki += std::exp(std::complex<float>(0, alpha));withF_ki += std::complex<float>(std::cos(alpha), std::sin(alpha));madecompute()about 3.6x faster in a quick local benchmark of 4,000 particles.This works because the exponent's real part is always 0, so$\exp(i\alpha)$ is just $\cos\alpha + i\sin\alpha$ .
The complete set of unit tests + the new one still pass with this change, and it doesn't need any changes to compiler flags. I'm happy to add it to this PR or open a separate one, whichever you prefer.
Also, I will make an effort to be active and respond, but also please feel free to make any edits you feel are necessary!
Checklist: