Skip to content

Sample all k vector directions in StaticStructureFactorDirect - #1506

Merged
DomFijan merged 5 commits into
glotzerlab:mainfrom
chrisjonesBSU:fix-sk-direct-octants
Nov 9, 2026
Merged

DomFijan merged 5 commits into
glotzerlab:mainfrom
chrisjonesBSU:fix-sk-direct-octants

Conversation

@chrisjonesBSU

@chrisjonesBSU chrisjonesBSU commented Oct 22, 2026 •

Copy link
Copy Markdown

Description

All changes are in reciprocal_isotropic and its helpers in StaticStructureFactorDirect.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_equation now 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 a NaN to unsigned int, which is undefined behavior. The k_max range bounds the innermost loop, and the k_min range skips points inside k_min.

  • The normalization in get_prune_distance3D and get_prune_distance2D is updated from an octant/quadrant to a half space/half plane (changed multiplicative prefactors) so num_sampled_k_points still gives about the requested number of k points.

The following changes fix issues with triclinic boxes.

  • Sign of the linear term, coef_b becomes 2 * 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_b when 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 of ceil(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_grid places 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:

  1. 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 dsf after their 2.0 release, and the test only runs if it can detect dsf . 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.

  2. There's a one-line change that seems to recover the ~3.5x slowdown from sampling the full grid. In compute_F_k, replacing F_ki += std::exp(std::complex<float>(0, alpha)); with F_ki += std::complex<float>(std::cos(alpha), std::sin(alpha)); made compute() 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:

chrisjonesBSU added 2 commits October 22, 2026 15:26
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.
@DomFijan

Copy link
Copy Markdown
Contributor

Thanks for the PR. I will take a look at this at some point this week hopefully, if not - first thing next week.

@DomFijan DomFijan left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I have a couple of suggestions for improvement. The Publish tests fail because we are on a private branch. In the future please create a branch on freud instead.

@joaander

joaander commented Nov 6, 2026

Copy link
Copy Markdown
Member

The Publish tests fail because we are on a private branch. In the future please create a branch on freud instead.

@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.

chrisjonesBSU added 2 commits November 7, 2026 22:51
@chrisjonesBSU

Copy link
Copy Markdown
Author

Thanks @DomFijan, all of your requested changes should be there.

Any thoughts about replacing F_ki += std::exp(std::complex<float>(0, alpha)); with std::complex<float>(std::cos(alpha), std::sin(alpha));? It does seem to make a noticeable improvement in the performance as it skips calling cexpf in glibc, which goes through a few extra steps.

@DomFijan

DomFijan commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Any thoughts about replacing F_ki += std::exp(std::complex<float>(0, alpha)); with std::complex<float>(std::cos(alpha), std::sin(alpha));? It does seem to make a noticeable improvement in the performance as it skips calling cexpf in glibc, which goes through a few extra steps.

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 DomFijan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think we are good to merge this now.

@chrisjonesBSU

Copy link
Copy Markdown
Author

Any thoughts about replacing F_ki += std::exp(std::complex<float>(0, alpha)); with std::complex<float>(std::cos(alpha), std::sin(alpha));? It does seem to make a noticeable improvement in the performance as it skips calling cexpf in glibc, which goes through a few extra steps.

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.

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.

@DomFijan
DomFijan merged commit be563f1 into glotzerlab:main Nov 9, 2026
24 of 25 checks passed
@chrisjonesBSU
chrisjonesBSU deleted the fix-sk-direct-octants branch November 9, 2026 14:46
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.

StaticStructureFactorDirect only samples k vectors in the positive octant

3 participants