Skip to content

Commit

Permalink
Make sure that KingPotential = -GM/r outside of the tidal radius by i…
Browse files Browse the repository at this point in the history
…ncluding offset and solving the relevant ODE more accurately (also making sure that the King mass is consistent)
  • Loading branch information
jobovy committed Oct 23, 2024
1 parent 42e43e7 commit ec7e6c3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 15 deletions.
17 changes: 3 additions & 14 deletions galpy/df/kingdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def solve(self, npt=1001):
],
[0.0, rbreak],
[self.W0, 0.0],
method="LSODA",
method="DOP853",
t_eval=r[: npt // 2],
)
W[: npt // 2] = sol.y[0]
Expand All @@ -155,7 +155,7 @@ def solve(self, npt=1001):
],
[sol.y[0, -1], 0.0],
[rbreak, sol.y[1, -1]],
method="LSODA",
method="DOP853",
t_eval=W[npt // 2 - 1 :],
)
r[npt // 2 - 1 :] = sol.y[0]
Expand All @@ -171,18 +171,7 @@ def solve(self, npt=1001):
# Interpolate solution
self._W_from_r = interpolate.InterpolatedUnivariateSpline(self._r, self._W, k=3)
# Compute the cumulative mass and store the total mass
mass_shells = numpy.array(
[
integrate.quad(lambda r: _FOURPI * r**2 * self.dens(r), rlo, rhi)[0]
for rlo, rhi in zip(r[:-1], r[1:])
]
)
self._cumul_mass = numpy.hstack(
(
integrate.quad(lambda r: _FOURPI * r**2 * self.dens(r), 0.0, r[0])[0],
numpy.cumsum(mass_shells),
)
)
self._cumul_mass = -self._dWdr * self._r**2.0
self.mass = self._cumul_mass[-1]
return None

Expand Down
2 changes: 1 addition & 1 deletion galpy/potential/KingPotential.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self, W0=2.0, M=3.0, rt=1.5, npt=1001, _sfkdf=None, ro=None, vo=Non
/ radius_scale**2.0
* numpy.interp(r / radius_scale, sfkdf._r, sfkdf._dWdr),
rgrid=sfkdf._r * radius_scale,
Phi0=-W0 * mass_scale / radius_scale,
Phi0=-W0 * mass_scale / radius_scale - M / rt,
ro=ro,
vo=vo,
)
13 changes: 13 additions & 0 deletions tests/test_potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -7734,6 +7734,19 @@ def test_InterpSnapshotRZPotential_pickling():
return None


# Test that the King potential goes as -GM/r at r > rt
def test_king_potential_beyond_tidal():
mass = 3.0
kp = potential.KingPotential(W0=6.0, rt=2.0, M=mass)
r = numpy.linspace(2.1, 10.0, 1001)
# Accuracy is limited because of the numerical solution of the King ODE and
# the fact that interpSphericalPotential doesn't directly use the solved W potential
assert numpy.all(
numpy.fabs(kp(r, 0.0) / mass * r + 1.0) < 1e-3
), "King potential does not go as GM/r at r > rt"
return None


# Test that trying to plot a potential with xy=True and effective=True raises a RuntimeError
def test_plotting_xy_effective_error():
# First a single potential
Expand Down

0 comments on commit ec7e6c3

Please sign in to comment.