/
actionAngle.py
314 lines (274 loc) · 11.2 KB
/
actionAngle.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import copy
import types
import numpy
from ..util import config, conversion
from ..util.conversion import (
actionAngle_physical_input,
physical_compatible,
physical_conversion_actionAngle,
)
# Metaclass for copying docstrings from subclass methods, first func
# to copy func
def copyfunc(func):
return types.FunctionType(
func.__code__,
func.__globals__,
name=func.__name__,
argdefs=func.__defaults__,
closure=func.__closure__,
)
class MetaActionAngle(type):
"""Metaclass to assign subclass' docstrings for methods _evaluate, _actionsFreqs, _actionsFreqsAngles, and _EccZmaxRperiRap to their public cousins __call__, actionsFreqs, etc."""
def __new__(meta, name, bases, attrs):
for key in copy.copy(attrs): # copy bc size changes
if key[0] == "_":
skey = copy.copy(key[1:])
if skey == "evaluate":
skey = "__call__"
for base in bases:
original = getattr(base, skey, None)
if original is not None:
funccopy = copyfunc(original)
funccopy.__doc__ = attrs[key].__doc__
attrs[skey] = funccopy
break
return type.__new__(meta, name, bases, attrs)
# Python 2 & 3 compatible way to have a metaclass
class actionAngle(metaclass=MetaActionAngle):
"""Top-level class for actionAngle classes"""
def __init__(self, ro=None, vo=None):
"""
Initialize an actionAngle object.
Parameters
----------
ro : float or Quantity, optional
Distance scale for translation into internal units (default from configuration file).
vo : float or Quantity, optional
Velocity scale for translation into internal units (default from configuration file).
Notes
-----
- 2016-02-18 - Written - Bovy (UofT)
"""
# Parse ro and vo
if ro is None:
self._ro = config.__config__.getfloat("normalization", "ro")
self._roSet = False
else:
self._ro = conversion.parse_length_kpc(ro)
self._roSet = True
if vo is None:
self._vo = config.__config__.getfloat("normalization", "vo")
self._voSet = False
else:
self._vo = conversion.parse_velocity_kms(vo)
self._voSet = True
return None
def _check_consistent_units(self):
"""Internal function to check that the set of units for this object is consistent with that for the potential"""
assert physical_compatible(
self, self._pot
), "Physical conversion for the actionAngle object is not consistent with that of the Potential given to it"
def _check_consistent_units_orbitInput(self, orb):
"""Internal function to check that the set of units for this object is consistent with that for an input orbit"""
assert physical_compatible(
self, orb
), "Physical conversion for the actionAngle object is not consistent with that of the Orbit given to it"
def turn_physical_off(self):
"""
Turn off automatic returning of outputs in physical units.
Notes
------
- 2017-06-05 - Written - Bovy (UofT)
"""
self._roSet = False
self._voSet = False
return None
def turn_physical_on(self, ro=None, vo=None):
"""
Turn on automatic returning of outputs in physical units.
Parameters
----------
ro : float or Quantity, optional
Distance scale for translation into internal units (default from configuration file).
vo : float or Quantity, optional
Velocity scale for translation into internal units (default from configuration file).
Notes
-----
- 2016-06-05 - Written - Bovy (UofT)
- 2020-04-22 - Don't turn on a parameter when it is False - Bovy (UofT)
"""
if not ro is False:
self._roSet = True
ro = conversion.parse_length_kpc(ro)
if not ro is None:
self._ro = ro
if not vo is False:
self._voSet = True
vo = conversion.parse_velocity_kms(vo)
if not vo is None:
self._vo = vo
return None
def _parse_eval_args(self, *args, **kwargs):
if len(args) == 5: # R,vR.vT, z, vz
R, vR, vT, z, vz = args
self._eval_R = R
self._eval_vR = vR
self._eval_vT = vT
self._eval_z = z
self._eval_vz = vz
elif len(args) == 6: # R,vR.vT, z, vz, phi
R, vR, vT, z, vz, phi = args
self._eval_R = R
self._eval_vR = vR
self._eval_vT = vT
self._eval_z = z
self._eval_vz = vz
self._eval_phi = phi
else: # Orbit instance
if not kwargs.get("_noOrbUnitsCheck", False):
self._check_consistent_units_orbitInput(args[0])
if len(args) == 2:
orb = args[0](args[1])
else:
orb = args[0]
if len(orb.shape) > 1:
raise RuntimeError(
"Evaluating actionAngle methods with Orbit instances with multi-dimensional shapes is not supported"
)
self._eval_R = orb.R(use_physical=False)
self._eval_vR = orb.vR(use_physical=False)
self._eval_vT = orb.vT(use_physical=False)
if args[0].phasedim() > 4:
self._eval_z = orb.z(use_physical=False)
self._eval_vz = orb.vz(use_physical=False)
if args[0].phasedim() > 5:
self._eval_phi = orb.phi(use_physical=False)
else:
if args[0].phasedim() > 3:
self._eval_phi = orb.phi(use_physical=False)
self._eval_z = numpy.zeros_like(self._eval_R)
self._eval_vz = numpy.zeros_like(self._eval_R)
if hasattr(self, "_eval_z"): # calculate the polar angle
self._eval_theta = numpy.arctan2(self._eval_R, self._eval_z)
return None
@actionAngle_physical_input
@physical_conversion_actionAngle("__call__", pop=True)
def __call__(self, *args, **kwargs):
"""
Evaluate the actions (jr,lz,jz)
Parameters
----------
*args : tuple
Either:
a) R,vR,vT,z,vz[,phi]:
1) floats: phase-space value for single object (phi is optional) (each can be a Quantity)
2) numpy.ndarray: [N] phase-space values for N objects (each can be a Quantity)
b) Orbit instance: initial condition used if that's it, orbit(t) if there is a time given as well as the second argument
**kwargs : dict
Any other keyword arguments are passed to the _evaluate method.
Returns
-------
tuple
(jr,lz,jz)
Notes
-----
- 2014-01-03 - Written for top level - Bovy (IAS)
"""
try:
return self._evaluate(*args, **kwargs)
except AttributeError: # pragma: no cover
raise NotImplementedError(
"'__call__' method not implemented for this actionAngle module"
)
@actionAngle_physical_input
@physical_conversion_actionAngle("actionsFreqs", pop=True)
def actionsFreqs(self, *args, **kwargs):
"""
Evaluate the actions and frequencies (jr,lz,jz,Omegar,Omegaphi,Omegaz)
Parameters
----------
*args : tuple
Either:
a) R,vR,vT,z,vz[,phi]:
1) floats: phase-space value for single object (phi is optional) (each can be a Quantity)
2) numpy.ndarray: [N] phase-space values for N objects (each can be a Quantity)
b) Orbit instance: initial condition used if that's it, orbit(t) if there is a time given as well as the second argument
**kwargs : dict
Any other keyword arguments are passed to the _actionsFreqs method.
Returns
-------
tuple
(jr,lz,jz,Omegar,Omegaphi,Omegaz)
Notes
-----
- 2014-01-03 - Written for top level - Bovy (IAS)
"""
try:
return self._actionsFreqs(*args, **kwargs)
except AttributeError: # pragma: no cover
raise NotImplementedError(
"'actionsFreqs' method not implemented for this actionAngle module"
)
@actionAngle_physical_input
@physical_conversion_actionAngle("actionsFreqsAngles", pop=True)
def actionsFreqsAngles(self, *args, **kwargs):
"""
Evaluate the actions, frequencies, and angles (jr,lz,jz,Omegar,Omegaphi,Omegaz,angler,anglephi,anglez)
Parameters
----------
*args : tuple
Either:
a) R,vR,vT,z,vz,phi:
1) floats: phase-space value for single object (phi is optional) (each can be a Quantity)
2) numpy.ndarray: [N] phase-space values for N objects (each can be a Quantity)
b) Orbit instance: initial condition used if that's it, orbit(t) if there is a time given as well as the second argument
**kwargs : dict
Additional keyword arguments to be passed to _actionsFreqsAngles method.
Returns
-------
tuple
(jr,lz,jz,Omegar,Omegaphi,Omegaz,angler,anglephi,anglez)
Notes
-----
- 2014-01-03 - Written for top level - Bovy (IAS)
"""
try:
return self._actionsFreqsAngles(*args, **kwargs)
except AttributeError: # pragma: no cover
raise NotImplementedError(
"'actionsFreqsAngles' method not implemented for this actionAngle module"
)
@actionAngle_physical_input
@physical_conversion_actionAngle("EccZmaxRperiRap", pop=True)
def EccZmaxRperiRap(self, *args, **kwargs):
"""
Evaluate the eccentricity, maximum height above the plane, peri- and apocenter.
Parameters
----------
*args : tuple
Either:
a) R,vR,vT,z,vz,phi:
1) floats: phase-space value for single object (phi is optional) (each can be a Quantity)
2) numpy.ndarray: [N] phase-space values for N objects (each can be a Quantity)
b) Orbit instance: initial condition used if that's it, orbit(t) if there is a time given as well as the second argument
**kwargs : dict
A dictionary of keyword arguments.
Returns
-------
tuple
(eccentricity, maximum height above the plane, peri-, and apocenter)
Notes
-----
- 2017-12-12 - Written - Bovy (UofT)
"""
try:
return self._EccZmaxRperiRap(*args, **kwargs)
except AttributeError: # pragma: no cover
raise NotImplementedError(
"'EccZmaxRperiRap' method not implemented for this actionAngle module"
)
class UnboundError(Exception): # pragma: no cover
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)