Skip to content

Commit f5a2c17

Browse files
committed
Raise RuntimeError when trying to apply a Wrapper to a non-Potential Force (fixes #627 for now)
1 parent d266f57 commit f5a2c17

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

galpy/potential/WrapperPotential.py

+14
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
planarPotential,
1111
)
1212
from .Potential import (
13+
Force,
1314
Potential,
1415
_dim,
1516
_evaluatephitorques,
@@ -90,6 +91,19 @@ def __init__(self, amp=1.0, pot=None, ro=None, vo=None, _init=None, **kwargs):
9091
return None # Don't run __init__ at the end of setup
9192
Potential.__init__(self, amp=amp, ro=ro, vo=vo)
9293
self._pot = pot
94+
# Check that we are not wrapping a non-potential Force object
95+
if (
96+
isinstance(self._pot, list)
97+
and any(
98+
[
99+
isinstance(p, Force) and not isinstance(p, Potential)
100+
for p in self._pot
101+
]
102+
)
103+
) or (isinstance(self._pot, Force) and not isinstance(self._pot, Potential)):
104+
raise RuntimeError(
105+
"WrapperPotential cannot currently wrap non-Potential Force objects"
106+
)
93107
self.isNonAxi = _isNonAxi(self._pot)
94108
# Check whether units are consistent between the wrapper and the
95109
# wrapped potential

tests/test_potential.py

+25
Original file line numberDiff line numberDiff line change
@@ -5701,6 +5701,31 @@ def test_Wrapper_incompatibleunitserror():
57015701
return None
57025702

57035703

5704+
def test_Wrapper_Force_error():
5705+
# Test that applying a wrapper to a DissipativeForce does not currently work
5706+
def M(t):
5707+
return 1.0
5708+
5709+
# Initialize potentials and time-varying potentials
5710+
df = potential.ChandrasekharDynamicalFrictionForce(GMs=1.0)
5711+
with pytest.raises(RuntimeError) as excinfo:
5712+
df_wrap = potential.TimeDependentAmplitudeWrapperPotential(A=M, amp=1, pot=df)
5713+
assert (
5714+
"WrapperPotential cannot currently wrap non-Potential Force objects"
5715+
== excinfo.value.args[0]
5716+
)
5717+
# Also test for list
5718+
with pytest.raises(RuntimeError) as excinfo:
5719+
df_wrap = potential.TimeDependentAmplitudeWrapperPotential(
5720+
A=M, amp=1, pot=potential.MWPotential2014 + df
5721+
)
5722+
assert (
5723+
"WrapperPotential cannot currently wrap non-Potential Force objects"
5724+
== excinfo.value.args[0]
5725+
)
5726+
return None
5727+
5728+
57045729
def test_WrapperPotential_unittransfer_3d():
57055730
# Test that units are properly transferred between a potential and its
57065731
# wrapper

0 commit comments

Comments
 (0)