Skip to content

Fix Fourier2 weights in UniformGrid - #350

Open
SajalDevX wants to merge 2 commits into
theochem:masterfrom
SajalDevX:fix-fourier2-weights
Open

SajalDevX wants to merge 2 commits into
theochem:masterfrom
SajalDevX:fix-fourier2-weights

Conversation

@SajalDevX

Copy link
Copy Markdown
Contributor

Fixes #349

UniformGrid with weight="Fourier2" gave weights that summed to about zero, so every integral came out near 0 (e.g. a Gaussian that should give pi**1.5 gave ~1e-16). In _fourier2 the sine argument used (2i - 1) p / M where the rectangle rule for sine series has (2i - 1) p / (2M), and the weights were scaled by the alternative volume (M - 1) h per axis instead of the grid volume M h.

Changes:

  • cubic.py: divide by 2 * shape[index] in the Fourier2 sine argument, scale by _calculate_volume instead of _calculate_alternative_volume, and fix the Fourier2 formula in the docstring to match.
  • test_cubic.py: test_fourier2_weights_are_correct had the same missing factor and volume, so I updated it to the corrected formula, and added test_fourier2_weights_integrate_gaussian, which integrates exp(-r^2) on a 21^3 grid over [-5, 5]^3 and compares with pi**1.5.

Testing:

  • Both Fourier2 tests fail on master (the Gaussian integral is 1.6e-16 instead of 5.568) and pass with the fix.
  • With the fix the Gaussian integral converges spectrally: error 1.4e-2, 1.1e-6, 9e-12 for 11, 21, 41 points per axis.
  • pytest src/grid/tests/test_cubic.py passes (33 passed).
  • Black (24.4.2) and ruff report nothing new in the touched files.

I used Claude to help find the bug and draft the test; I reviewed the change and ran the tests myself.

The sine argument used (2i-1)p/M instead of (2i-1)p/(2M), which made the
weights antisymmetric so they summed to about zero. The weights were also
scaled by the alternative volume instead of the grid volume.

This branch has not been deployed

No deployments
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.

UniformGrid weight="Fourier2" integrates everything to about zero

1 participant