Add MultipoleExpansionPotential for multipole expansion of arbitrary densities#829
Open
Add MultipoleExpansionPotential for multipole expansion of arbitrary densities#829
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #829 +/- ##
==========================================
- Coverage 99.91% 98.49% -1.42%
==========================================
Files 215 219 +4
Lines 30920 33509 +2589
Branches 618 662 +44
==========================================
+ Hits 30893 33005 +2112
- Misses 27 504 +477 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
efbc445 to
a345cf9
Compare
70a383d to
b9b9d48
Compare
42ce48b to
eb045a1
Compare
…densities Add MultipoleExpansionPotential, a new potential class that decomposes a given density function into real spherical harmonics on a radial grid and computes the gravitational potential via classical multipole integrals (galaxiesbook Ch. 12.3, eq. 12.76 & 12.79). Supports spherical, axisymmetric, and general (non-axisymmetric) density profiles via the symmetry parameter; accepts density as a callable (1/2/3 args) with or without astropy units, or a galpy Potential instance. Analytical forces are computed via a shared SphericalHarmonicPotentialMixin that provides the spherical-to-cylindrical force chain rule (_Rforce, _zforce, _phitorque). The radial force derivative includes spline derivative corrections for the inner/outer integrals, ensuring forces are the true negative gradient of the interpolated potential and giving excellent energy conservation in orbit integrations. Second derivatives are computed numerically via NumericalPotentialDerivativesMixin. Refactoring: - Extract shared spherical harmonics utilities (compute_legendre, sph_harm_normalization) from SCFPotential into galpy/util/special.py - Extract shared spherical-to-cylindrical force chain rule from SCFPotential into SphericalHarmonicPotentialMixin - SCFPotential now inherits from the mixin, removing duplicated code Other changes: - Add MultipoleExpansionPotential to estimateDeltaStaeckel special-case handling (like SCFPotential) to avoid negative delta2 from numerical second derivatives - Add adiabatic fallback for MultipoleExpansionPotential in orbit analytic parameter tests (ecc, rperi, rap, zmax) - Exclude SphericalHarmonicPotentialMixin from orbit/potential test lists - Add tolerances for MultipoleExpansionPotential in test_potential.py (Poisson, 2nd derivatives) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Implement analytical second derivatives via the spherical-to-cylindrical chain rule, replacing NumericalPotentialDerivativesMixin. This improves accuracy from ~1e-5 to machine precision for derivative self-consistency, allowing all test tolerances to use defaults. - Extend compute_legendre to support deriv=2 (d²P/dx²) - Add _compute_spher_2nd_derivs_at_point to MultipoleExpansionPotential - Add chain rule helpers (_evaluate_cyl_2nd_deriv) to SphericalHarmonicPotentialMixin - Add configurable spline degree k parameter (default k=3) - Remove NumericalPotentialDerivativesMixin dependency - Remove MultipoleExpansionPotential from estimateDeltaStaeckel special-casing - Remove custom tolerances for 2nd deriv and Poisson tests (now use defaults) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ines and auto-truncating non-axi terms Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…tegration
Implements potential evaluation, forces, and density in C using GSL splines
for radial functions and shared Legendre utilities from SCFPotential.c.
Enables ~255x speedup for orbit integration and ~19x for dynamical friction.
Key implementation details:
- Type 44 in the C potential system
- GSL cubic splines for radial interpolation (Radial_cos/sin, rho_cos/sin)
- Point-mass extrapolation for r > rmax: R_lm(r) = R_lm(rmax)*(rmax/r)^{l+1}
- Density clamps to rmin for r < rmin (prevents sigma_r divergence in CDF)
- Spline init follows ChandrasekharDynamicalFriction pattern (before nargs
loop, with saved flag before wrapper parsing) for correct pot_args ordering
- Force caching for repeated evaluations at same coordinates
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ion, to pick up coordinates given as keywords (dens(R,z=0,phi=0))
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…radial derivatives Replace the single R_lm(r) IUS spline with separate BPoly splines for the cumulative integrals I_inner(r) and I_outer(r), with pref_blm absorbed. Compute R, dR/dr, and d²R/dr² analytically via the product rule from the spline values and derivatives. This avoids the dynamic range issues that caused large interpolation errors for high-l terms. For r < rmin, assume constant density rho(rmin) and extend the cumulative integrals analytically. For r > rmax, use point-mass extrapolation (rho=0). Update both Python and C implementations. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
ac21794 to
7f7d327
Compare
…fficients directly for exact Python/C parity, combine radial evaluations, and replace MD5 caching with tuple keys Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
7f7d327 to
2a42e7c
Compare
0a880b3 to
2876d13
Compare
- Remove k parameter from public API (BPoly sets degree, old docstring misleading) - Skip sin integral computation for m=0 (never used, zero bytes reach C) - Enhance SCF test to verify only l=0,1 coefficients are non-zero - Add C coverage tests for below-grid log branch (line 255), d²R (lines 286-288), and non-axi potential evaluation Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…computation Split __init__ into spline-accepting __init__ and from_density classmethod, following the SCFPotential pattern. __init__ now takes precomputed rho_cos_splines/rho_sin_splines; from_density handles density decomposition. Make _parse_density and _compute_rho_lm static methods. Update class docstring to use real-valued spherical harmonic formulas with explicit projection integrals. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
b4370a1 to
805c485
Compare
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
MultipoleExpansionPotential, a new potential class that decomposes a given density function into real spherical harmonics on a radial grid and computes the gravitational potential via classical multipole integrals (galaxiesbook.org Ch. 12.3, eq. 12.76 & 12.79)symmetryparameter; accepts density as a callable (1/2/3 args) with or without astropy units, or a galpyPotentialinstanceSphericalHarmonicPotentialMixinproviding the spherical-to-cylindrical force chain rule (_Rforce,_zforce,_phitorque); radial force derivatives include spline derivative corrections for inner/outer integrals, ensuring forces are the true negative gradient of the interpolated potentialNumericalPotentialDerivativesMixinRefactoring
compute_legendre,sph_harm_normalization) fromSCFPotentialintogalpy/util/special.pySCFPotentialintoSphericalHarmonicPotentialMixin;SCFPotentialnow inherits from the mixin, removing duplicated codeOther changes
MultipoleExpansionPotentialtoestimateDeltaStaeckelspecial-case handling (likeSCFPotential) to avoid negative delta2 from numerical second derivativesMultipoleExpansionPotentialin orbit analytic parameter tests (ecc, rperi, rap, zmax)SphericalHarmonicPotentialMixinfrom orbit/potential test listsMultipoleExpansionPotentialintest_potential.py(Poisson, 2nd derivatives)Test plan
tests/test_MultipoleExpansionPotential.pycovering spherical (Hernquist), axisymmetric (MiyamotoNagai), SCF cross-validation, density reconstruction, normalization,isNonAxiflag, input variants (1/2/3-arg callable, Potential instance), analytical forces, and edge cases (r=0, infinity, phi=None)test_quantity.py::test_potential_paramunitstest_potential.pyandtest_orbit.pytests pass forMultipoleExpansionPotential