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% 99.91%
========================================
Files 215 219 +4
Lines 30920 31634 +714
Branches 613 644 +31
========================================
+ Hits 30893 31607 +714
Misses 27 27 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
efbc445 to
a345cf9
Compare
a345cf9 to
8ec9690
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>
d1a8ddc to
0757b9e
Compare
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))
cb73b5e to
70a383d
Compare
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
70a383d to
b9b9d48
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