-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathutils.py
447 lines (321 loc) · 10.5 KB
/
utils.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
"""A collection of miscellaneous utility functions"""
from scipy.special import jv
from astropy import constants as c
from astropy import units as u
import numpy as np
__all__ = ['chirp_mass', 'peters_g', 'peters_f', 'get_a_from_f_orb', 'get_f_orb_from_a', 'get_a_from_ecc',
'beta', 'c_0', 'fn_dot', 'ensure_array', 'D_plus_squared',
'D_cross_squared', 'D_plus_D_cross', 'F_plus_squared', 'F_cross_squared']
def chirp_mass(m_1, m_2):
"""Computes chirp mass of binaries
Parameters
----------
m_1 : `float/array`
Primary mass
m_2 : `float/array`
Secondary mass
Returns
-------
m_c : `float/array`
Chirp mass
"""
m_c = (m_1 * m_2)**(3/5) / (m_1 + m_2)**(1/5)
# simplify units if present
if isinstance(m_c, u.quantity.Quantity):
m_c = m_c.to(u.Msun)
return m_c
def peters_g(n, e):
"""Compute g(n, e) from Peters and Mathews (1963) Eq.20
This function gives the relative power of gravitational radiation at the nth harmonic
Parameters
----------
n : `int/array`
Harmonic(s) of interest
e : `float/array`
Eccentricity
Returns
-------
g : `array`
g(n, e) from Peters and Mathews (1963) Eq. 20
"""
bracket_1 = jv(n-2, n*e) - 2*e*jv(n-1, n*e) + 2/n*jv(n, n*e) + 2*e*jv(n+1, n*e) - jv(n+2, n*e)
bracket_2 = jv(n-2, n*e) - 2*jv(n, n*e) + jv(n+2, n*e)
bracket_3 = jv(n, n*e)
g = n**4/32 * (bracket_1**2 + (1 - e**2) * bracket_2**2 + 4 / (3 * n**3) * bracket_3**2)
return g
def peters_f(e):
"""f(e) from Peters and Mathews (1963) Eq.17
This function gives the integrated enhancement factor of gravitational radiation from an eccentric
source compared to an equivalent circular source.
Parameters
----------
e : `float/array`
Eccentricity
Returns
-------
f : `float/array`
Enhancement factor
Notes
-----
Note that this function represents an infinite sum of g(n, e) - :meth:`legwork.utils.peters_g`
"""
numerator = 1 + (73/24)*e**2 + (37/96)*e**4
denominator = (1 - e**2)**(7/2)
f = numerator / denominator
return f
def get_a_from_f_orb(f_orb, m_1, m_2):
"""Converts orbital frequency to semi-major axis
Using Kepler's third law, convert orbital frequency to semi-major axis.
Inverse of :func:`legwork.utils.get_f_orb_from_a`.
Parameters
----------
f_orb : `float/array`
Orbital frequency
m_1 : `float/array`
Primary mass
m_2 : `float/array`
Secondary mass
Returns
-------
a : `float/array`
Semi-major axis
"""
a = (c.G * (m_1 + m_2) / (2 * np.pi * f_orb)**2)**(1/3)
# simplify units if present
if isinstance(a, u.quantity.Quantity):
a = a.to(u.AU)
return a
def get_f_orb_from_a(a, m_1, m_2):
"""Converts semi-major axis to orbital frequency
Using Kepler's third law, convert semi-major axis to orbital frequency.
Inverse of :func:`legwork.utils.get_a_from_f_orb`.
Parameters
----------
a : `float/array`
Semi-major axis
m_1 : `float/array`
Primary mass
m_2 : `float/array`
Secondary mass
Returns
-------
f_orb : `float/array`
Orbital frequency
"""
f_orb = ((c.G * (m_1 + m_2) / a**3))**(0.5) / (2 * np.pi)
# simplify units if present
if isinstance(f_orb, u.quantity.Quantity):
f_orb = f_orb.to(u.Hz)
return f_orb
def beta(m_1, m_2):
"""Compute beta defined in Peters and Mathews (1964) Eq.5.9
Parameters
----------
m_1 : `float/array`
Primary mass
m_2 : `float/array`
Secondary mass
Returns
-------
beta : `float/array`
Constant defined in Peters and Mathews (1964) Eq.5.9.
"""
beta = 64 / 5 * c.G**3 / c.c**5 * m_1 * m_2 * (m_1 + m_2)
# simplify units if present
if isinstance(beta, u.quantity.Quantity):
beta = beta.to(u.m**4 / u.s)
return beta
def c_0(a_i, ecc_i):
"""Computes the c_0 factor in Peters and Mathews (1964) Eq.5.11
Parameters
----------
a_i : `float/array`
Initial semi-major axis
ecc_i : `float/array`
Initial eccentricity
Returns
-------
c_0 : `float`
Constant defined in Peters and Mathews (1964) Eq.5.11
"""
c_0 = a_i * (1 - ecc_i**2) * ecc_i**(-12/19) * (1 + (121/304)*ecc_i**2)**(-870/2299)
# simplify units if present
if isinstance(c_0, u.quantity.Quantity):
c_0 = c_0.to(u.AU)
return c_0
def get_a_from_ecc(ecc, c_0):
"""Convert eccentricity to semi-major axis
Use initial conditions and Peters (1964) Eq. 5.11 to convert ``ecc`` to ``a``.
Parameters
----------
ecc : `float/array`
Eccentricity
c_0 : `float`
Constant defined in Peters and Mathews (1964) Eq. 5.11. See :meth:`legwork.utils.c_0`
Returns
-------
a : `float/array`
Semi-major axis"""
a = c_0 * ecc**(12/19) / (1 - ecc**2) * (1 + (121/304) * ecc**2)**(870/2299)
# simplify units if present
if isinstance(a, u.quantity.Quantity):
a = a.to(u.AU)
return a
def fn_dot(m_c, f_orb, e, n):
"""Rate of change of nth frequency of a binary
Parameters
----------
m_c : `float/array`
Chirp mass
f_orb : `float/array`
Orbital frequency
e : `float/array`
Eccentricity
n : `int`
Harmonic of interest
Returns
-------
fn_dot : `float/array`
Rate of change of nth frequency
"""
if np.any(n < 1):
raise ValueError("All harmonics must be greater than or equal to 1")
fn_dot = (48 * n) / (5 * np.pi) * (c.G * m_c)**(5/3) / c.c**5 * (2 * np.pi * f_orb)**(11/3) * peters_f(e)
# simplify units if present
if isinstance(fn_dot, u.quantity.Quantity):
fn_dot = fn_dot.to(u.Hz / u.yr)
return fn_dot
def ensure_array(*args):
"""Convert arguments to numpy arrays
Convert arguments based on the following rules
- Ignore any None values
- Convert any lists to numpy arrays
- Wrap any other types in lists and convert to numpy arrays
Parameters
----------
args : `any`
Supply any number of arguments of any type
Returns
-------
array_args : `any`
Args converted to numpy arrays
any_not_arrays : `bool`
Whether any arg is not a list or None or a numpy array
"""
array_args = [None for i in range(len(args))]
any_not_arrays = False
for i in range(len(array_args)):
exists = args[i] is not None
has_units = isinstance(args[i], u.quantity.Quantity)
if exists and has_units:
if not isinstance(args[i].value, np.ndarray):
any_not_arrays = True
array_args[i] = np.asarray([args[i].value]) * args[i].unit
else:
array_args[i] = args[i]
elif exists and not has_units:
if not isinstance(args[i], np.ndarray):
if not isinstance(args[i], list):
any_not_arrays = True
array_args[i] = np.asarray([args[i]])
else:
array_args[i] = np.asarray(args[i])
else:
array_args[i] = args[i]
else:
array_args[i] = args[i]
return array_args, any_not_arrays
def D_plus_squared(theta, phi):
"""Required for the detector responses <F_+^2>, <F_x^2>, <F_+F_x>
Parameters
----------
theta : `float/array`
declination of the source
phi : `float/array`
right ascension of the source
Returns
-------
D_plus_2 : `float/array`
factor used for response; see eq. 44 of Cornish and Larson (2003)
"""
term_1 = 158 * np.cos(theta)**2
term_2 = 7 * np.cos(theta)**4
term_3 = -162 * np.sin(2 * phi)**2 * (1 + np.cos(theta)**2)**2
D_plus_2 = (3 / 2048) * (487 + term_1 + term_2 + term_3)
return D_plus_2
def D_cross_squared(theta, phi):
"""Required for the detector responses <F_+^2>, <F_x^2>, <F_+F_x>
Parameters
----------
theta : `float/array`
declination of the source
phi : `float/array`
right ascension of the source
Returns
-------
D_cross_2 : `float/array`
factor used for response; see eq. 44 of Cornish and Larson (2003)
"""
term_1 = 120 * np.sin(theta)**2
term_2 = np.cos(theta)**2
term_3 = 162 * np.sin(2 * phi)**2 * np.cos(theta)**2
D_cross_2 = (3 / 512) * (term_1 + term_2 + term_3)
return D_cross_2
def D_plus_D_cross(theta, phi):
"""Required for the detector responses <F_+^2>, <F_x^2>, <F_+F_x>
Parameters
----------
theta : `float/array`
declination of the source
phi : `float/array`
right ascension of the source
Returns
-------
D_plus_cross : `float/array`
factor used for response; see eq. 44 of Cornish and Larson (2003)
"""
term_1 = np.cos(theta) * np.sin(2 * phi)
term_2 = 2 * np.cos(phi)**2 - 1
term_3 = 1 + np.cos(theta)**2
D_plus_cross = (243 / 512) * term_1 * term_2 * term_3
return D_plus_cross
def F_plus_squared(theta, phi, psi):
"""Compute the auto-correlated detector response for the plus polarization
Parameters
----------
theta : `float/array`
declination of the source
phi : `float/array`
right ascension of the source
psi : `float/array`
polarization of the source
Returns
-------
F_plus_2 : `float/array`
Auto-correlated detector response for the plus polarization
"""
term_1 = np.cos(2 * psi)**2 * D_plus_squared(theta, phi)
term_2 = -np.sin(4 * psi) * D_plus_D_cross(theta, phi)
term_3 = np.sin(2 * psi)**2 * D_cross_squared(theta, phi)
F_plus_2 = (1 / 4) * (term_1 + term_2 + term_3)
return F_plus_2
def F_cross_squared(theta, phi, psi):
"""Compute the auto-correlated detector response for the cross polarization
Parameters
----------
theta : `float/array`
declination of the source
phi : `float/array`
right ascension of the source
psi : `float/array`
polarization of the source
Returns
-------
F_cross_2 : `float/array`
Auto-correlated detector response for the cross polarization
"""
term_1 = np.cos(2 * psi)**2 * D_cross_squared(theta, phi)
term_2 = np.sin(4 * psi) * D_plus_D_cross(theta, phi)
term_3 = np.sin(2 * psi)**2 * D_plus_squared(theta, phi)
F_cross_2 = (1 / 4) * (term_1 + term_2 + term_3)
return F_cross_2