Skip to content

Commit 04f2abb

Browse files
authored
Merge pull request #341 from adrn/agama-interop
Add a way to convert Gala potential instances to Agama potential instances
2 parents 087235d + 02c2912 commit 04f2abb

File tree

6 files changed

+396
-140
lines changed

6 files changed

+396
-140
lines changed

CHANGES.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ New Features
77
- Added a ``.guiding_center()`` method to ``PhaseSpacePosition`` and ``Orbit`` to
88
compute the guiding center radius.
99

10+
- Added a way to convert Gala potential instances to Agama potential instances.
11+
1012
Bug fixes
1113
---------
1214

@@ -24,6 +26,10 @@ Bug fixes
2426
API changes
2527
-----------
2628

29+
- Changed the way potential interoperability is done with other Galactic dynamics
30+
packages (Agama, galpy, etc.). It is now handled by the ``Potential.as_interop()``
31+
method on all potential class instances.
32+
2733

2834
1.7.1 (2023-08-05)
2935
==================

gala/potential/potential/core.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
# Standard library
22
import abc
3-
from collections import OrderedDict
43
import copy as pycopy
5-
import warnings
64
import uuid
5+
import warnings
6+
from collections import OrderedDict
7+
8+
import astropy.units as u
79

810
# Third-party
911
import numpy as np
1012
from astropy.constants import G
11-
import astropy.units as u
1213
from astropy.utils import isiterable
14+
from astropy.utils.decorators import deprecated
1315

1416
try:
1517
from scipy.spatial.transform import Rotation
@@ -21,9 +23,10 @@
2123

2224
# Project
2325
from gala.util import GalaDeprecationWarning
24-
from ..common import CommonBase
25-
from ...util import ImmutableDict, atleast_2d
26+
2627
from ...units import DimensionlessUnitSystem
28+
from ...util import ImmutableDict, atleast_2d
29+
from ..common import CommonBase
2730

2831
__all__ = ["PotentialBase", "CompositePotential"]
2932

@@ -897,6 +900,11 @@ def value(self, *args, **kwargs):
897900
###########################################################################
898901
# Interoperability with other packages
899902
#
903+
@deprecated(
904+
since="v1.8",
905+
message="This has been replaced by a more general interoperability framework.",
906+
alternative="interop",
907+
)
900908
def to_galpy_potential(self, ro=None, vo=None):
901909
"""Convert a Gala potential to a Galpy potential instance
902910
@@ -905,9 +913,36 @@ def to_galpy_potential(self, ro=None, vo=None):
905913
ro : quantity-like (optional)
906914
vo : quantity-like (optional)
907915
"""
908-
from .interop import gala_to_galpy_potential
916+
return self.as_interop("galpy", ro=ro, vo=vo)
917+
918+
def as_interop(self, package, **kwargs):
919+
"""Interoperability with other Galactic dynamics packages
920+
921+
Parameters
922+
----------
923+
package : str
924+
The package to export the potential to. Currently supported packages are
925+
``"galpy"`` and ``"agama"``.
926+
kwargs
927+
Any additional keyword arguments are passed to the interop function.
928+
"""
929+
if package == "galpy":
930+
from .interop import gala_to_galpy_potential
931+
932+
kwargs.setdefault("ro", None)
933+
kwargs.setdefault("vo", None)
934+
return gala_to_galpy_potential(self, **kwargs)
935+
elif package == "agama":
936+
import agama
909937

910-
return gala_to_galpy_potential(self, ro=ro, vo=vo)
938+
from .interop import gala_to_agama_potential
939+
940+
agama_pot = gala_to_agama_potential(self, **kwargs)
941+
if not isinstance(agama_pot, agama.Potential):
942+
agama_pot = agama.Potential(*agama_pot)
943+
return agama_pot
944+
else:
945+
raise ValueError(f"Unsupported package: {package}")
911946

912947

913948
class CompositePotential(PotentialBase, OrderedDict):

0 commit comments

Comments
 (0)