Fix quadrature noise in linking number diagnostic - #63
Open
rogeriojorge wants to merge 3 commits into
Open
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests.
|
Member
Author
|
Added SIMSOPT-compatible |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
compute_linking_number|r1-r2|^3denominator used by SIMSOPT, while safely masking padded block entriesMotivation
For disjoint closed curves, the Gauss linking number is integer-valued. The previous implementation returned the raw quadrature approximation, so unlinked circular coils could report a small nonzero loss. More seriously, differentiation of that residual could produce a nonzero gradient and perturb the coils even though linking number cannot vary continuously without an intersection.
SIMSOPT handles this by rounding each pair's absolute Gauss integral before summing and provides no derivative for this topological diagnostic. This PR adopts the same semantics in JAX using per-pair
roundandstop_gradient.The former
1e-12denominator offset was also removed because it changes the Gauss integral and prevented a point-for-point comparison with SIMSOPT. Invalid padded entries are now assigned a safe denominator and explicitly zeroed. A true zero separation remains singular, which is appropriate because linking number is undefined when curves intersect.Test coverage
The non-optional tests contain a NumPy translation of SIMSOPT's C++ loop, so CI verifies the same algorithm without adding SIMSOPT as a dependency. They check:
nfp=3,stellsym=Truesymmetry-expanded initial circular coilsblock_size=Noneand non-divisibleblock_size=16with 65/97 quadrature pointsThe optional integration tests use
pytest.importorskip("simsopt"). When SIMSOPT is available they additionally verify:gammaandgammadasharrays point-by-point for identical Fourier dofs, including every symmetry copyLinkingNumberfor every pairLocal results
Using the SIMSOPT checkout installed from
/Users/rogeriojorge/local/simsopt_test:python -m pytest tests/test_linking_number_reference.py tests/test_linking_number_simsopt.py -q— 8 passedpython -m pytest tests/test_objective_functions.py tests/test_linking_number_reference.py -q— 17 passedpython -m pytest -q— 90 passed, 1 xfailedNo production dependency was added. On CI without SIMSOPT, only the direct integration module is skipped; the independent NumPy reference coverage remains mandatory.