Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a way to convert Gala potential instances to Agama potential instances #341

Merged
merged 6 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ New Features
- Added a ``.guiding_center()`` method to ``PhaseSpacePosition`` and ``Orbit`` to
compute the guiding center radius.

- Added a way to convert Gala potential instances to Agama potential instances.

Bug fixes
---------

Expand All @@ -24,6 +26,10 @@ Bug fixes
API changes
-----------

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


1.7.1 (2023-08-05)
==================
Expand Down
49 changes: 42 additions & 7 deletions gala/potential/potential/core.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
# Standard library
import abc
from collections import OrderedDict
import copy as pycopy
import warnings
import uuid
import warnings
from collections import OrderedDict

import astropy.units as u

# Third-party
import numpy as np
from astropy.constants import G
import astropy.units as u
from astropy.utils import isiterable
from astropy.utils.decorators import deprecated

try:
from scipy.spatial.transform import Rotation
Expand All @@ -21,9 +23,10 @@

# Project
from gala.util import GalaDeprecationWarning
from ..common import CommonBase
from ...util import ImmutableDict, atleast_2d

from ...units import DimensionlessUnitSystem
from ...util import ImmutableDict, atleast_2d
from ..common import CommonBase

__all__ = ["PotentialBase", "CompositePotential"]

Expand Down Expand Up @@ -897,6 +900,11 @@ def value(self, *args, **kwargs):
###########################################################################
# Interoperability with other packages
#
@deprecated(
since="v1.8",
message="This has been replaced by a more general interoperability framework.",
alternative="interop",
)
def to_galpy_potential(self, ro=None, vo=None):
"""Convert a Gala potential to a Galpy potential instance

Expand All @@ -905,9 +913,36 @@ def to_galpy_potential(self, ro=None, vo=None):
ro : quantity-like (optional)
vo : quantity-like (optional)
"""
from .interop import gala_to_galpy_potential
return self.as_interop("galpy", ro=ro, vo=vo)

def as_interop(self, package, **kwargs):
"""Interoperability with other Galactic dynamics packages

Parameters
----------
package : str
The package to export the potential to. Currently supported packages are
``"galpy"`` and ``"agama"``.
kwargs
Any additional keyword arguments are passed to the interop function.
"""
if package == "galpy":
from .interop import gala_to_galpy_potential

kwargs.setdefault("ro", None)
kwargs.setdefault("vo", None)
return gala_to_galpy_potential(self, **kwargs)
elif package == "agama":
import agama

return gala_to_galpy_potential(self, ro=ro, vo=vo)
from .interop import gala_to_agama_potential

agama_pot = gala_to_agama_potential(self, **kwargs)
if not isinstance(agama_pot, agama.Potential):
agama_pot = agama.Potential(*agama_pot)
return agama_pot
else:
raise ValueError(f"Unsupported package: {package}")


class CompositePotential(PotentialBase, OrderedDict):
Expand Down
Loading