Skip to content

Comments

Add MultipoleExpansionPotential for multipole expansion of arbitrary densities#829

Open
jobovy wants to merge 6 commits intomainfrom
multipole-expansion-potential
Open

Add MultipoleExpansionPotential for multipole expansion of arbitrary densities#829
jobovy wants to merge 6 commits intomainfrom
multipole-expansion-potential

Conversation

@jobovy
Copy link
Owner

@jobovy jobovy commented Feb 13, 2026

Summary

  • Adds 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)
  • 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 via SphericalHarmonicPotentialMixin providing 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 potential
  • Second derivatives 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)

Test plan

  • 34 tests in tests/test_MultipoleExpansionPotential.py covering spherical (Hernquist), axisymmetric (MiyamotoNagai), SCF cross-validation, density reconstruction, normalization, isNonAxi flag, input variants (1/2/3-arg callable, Potential instance), analytical forces, and edge cases (r=0, infinity, phi=None)
  • Astropy unit density tests (1/2/3-arg) in test_quantity.py::test_potential_paramunits
  • All 50 existing SCF tests pass after mixin refactoring
  • All test_potential.py and test_orbit.py tests pass for MultipoleExpansionPotential

@codecov
Copy link

codecov bot commented Feb 13, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.91%. Comparing base (5b3ae02) to head (b9b9d48).

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jobovy jobovy force-pushed the multipole-expansion-potential branch 2 times, most recently from efbc445 to a345cf9 Compare February 15, 2026 20:14
@jobovy jobovy changed the title Add MultiPoleExpansionPotential for multipole expansion of arbitrary densities Add MultipoleExpansionPotential for multipole expansion of arbitrary densities Feb 15, 2026
@jobovy jobovy force-pushed the multipole-expansion-potential branch from a345cf9 to 8ec9690 Compare February 16, 2026 22:51
…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>
@jobovy jobovy force-pushed the multipole-expansion-potential branch 4 times, most recently from d1a8ddc to 0757b9e Compare February 19, 2026 02:34
jobovy and others added 4 commits February 19, 2026 09:23
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))
@jobovy jobovy force-pushed the multipole-expansion-potential branch 2 times, most recently from cb73b5e to 70a383d Compare February 19, 2026 19:59
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@jobovy jobovy force-pushed the multipole-expansion-potential branch from 70a383d to b9b9d48 Compare February 19, 2026 21:04
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.

1 participant