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

Raise RuntimeError when trying to apply a Wrapper to a non-Potential Force #633

Merged
merged 1 commit into from
Mar 4, 2024
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
14 changes: 14 additions & 0 deletions galpy/potential/WrapperPotential.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
planarPotential,
)
from .Potential import (
Force,
Potential,
_dim,
_evaluatephitorques,
Expand Down Expand Up @@ -90,6 +91,19 @@ def __init__(self, amp=1.0, pot=None, ro=None, vo=None, _init=None, **kwargs):
return None # Don't run __init__ at the end of setup
Potential.__init__(self, amp=amp, ro=ro, vo=vo)
self._pot = pot
# Check that we are not wrapping a non-potential Force object
if (
isinstance(self._pot, list)
and any(
[
isinstance(p, Force) and not isinstance(p, Potential)
for p in self._pot
]
)
) or (isinstance(self._pot, Force) and not isinstance(self._pot, Potential)):
raise RuntimeError(
"WrapperPotential cannot currently wrap non-Potential Force objects"
)
self.isNonAxi = _isNonAxi(self._pot)
# Check whether units are consistent between the wrapper and the
# wrapped potential
Expand Down
25 changes: 25 additions & 0 deletions tests/test_potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -5701,6 +5701,31 @@ def test_Wrapper_incompatibleunitserror():
return None


def test_Wrapper_Force_error():
# Test that applying a wrapper to a DissipativeForce does not currently work
def M(t):
return 1.0

# Initialize potentials and time-varying potentials
df = potential.ChandrasekharDynamicalFrictionForce(GMs=1.0)
with pytest.raises(RuntimeError) as excinfo:
df_wrap = potential.TimeDependentAmplitudeWrapperPotential(A=M, amp=1, pot=df)
assert (
"WrapperPotential cannot currently wrap non-Potential Force objects"
== excinfo.value.args[0]
)
# Also test for list
with pytest.raises(RuntimeError) as excinfo:
df_wrap = potential.TimeDependentAmplitudeWrapperPotential(
A=M, amp=1, pot=potential.MWPotential2014 + df
)
assert (
"WrapperPotential cannot currently wrap non-Potential Force objects"
== excinfo.value.args[0]
)
return None


def test_WrapperPotential_unittransfer_3d():
# Test that units are properly transferred between a potential and its
# wrapper
Expand Down
Loading