-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharchinfo.py
213 lines (150 loc) · 4.94 KB
/
archinfo.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
import numpy as np
import warnings
import scipy.optimize as op
import warnings
import LMC
from scipy import stats
pi = np.pi
MSME = 332948.6 # (M_sun/M_earth)
BIGG = 6.67e-11 # Newton's constant [SI units]
RSUN = 6.957e8 # solar radius [m]
MSUN = 1.988e30 # Solar mass [kg]
###
# Functions containing relevant physics
def P_to_a(P, Mstar):
"""
Convenience function to convert periods to semimajor axis from Kepler's Law
P: orbital periods [days]
Mstar: stellar mass [solar masses]
"""
Pearth = 365.24 # [days]
aearth = 215.05 # [solar radii]
return aearth * ((P/Pearth)**2 *(1/Mstar))**(1/3)
def calculate_duration(period, rho, rprs, cosi):
"""
Helper function to calculate transit duration predicted from a circular orbit
period: orbital period [days]
rho: stellar density [solar density]
rprs: planet-to-star radius ratio for system
cosi: cosine(inclination)
"""
G = BIGG / RSUN**3 * MSUN * (24*3600)**2 # Newton's constant [R_sun^3 * M_sun^-1 * days^-2]
term3 = ((3*period)/(G*rho*pi**2))**(1/3)
term2a = (1+rprs)**2
term2b = ((G*rho)/(3*pi))**(2/3)
term2c = period**(4/3)*cosi**2
term2 = (term2a - term2b*term2c)**(1/2)
return term3*term2
def residuals_for_duration_fit(x0, x1, data_dur, data_err):
"""
Helper function to return residuals for least squares fitting (op.leastsq)
x0: vector of parameters to vary in fit (cosi)
x1: vector of parameters to hold constant (periods, rhostar, rprs)
data_dur: measured transit durations [days]
data_err: corresponding errors [days]
"""
cosi = x0
period, rho, rprs = x1
model_dur = calculate_duration(period, rho, rprs, cosi)
return (data_dur - model_dur)/data_err
def calculate_flatness(data_dur, model_dur):
"""
Helper function to calculate flatness
data_dur: measured transit durations [days]
model_dur: model transit durations [days] from leastsq fit
"""
return np.std(data_dur-model_dur)/np.sqrt(np.mean(data_dur**2))
# Functions to compute system-level complexity measures
# Quantities definied in Gilbert & Fabrycky (2019)
def mu(mp, Mstar):
"""
Alias for dyanmical_mass
"""
return dynamical_mass(mp, Mstar)
def Q(masses):
"""
Alias for mass_partitioning
"""
return mass_partitioning(masses)
def M(periods, masses):
"""
Alias for monotonicity
"""
return monotonicity(periods, masses)
def S(periods, mp, Mstar):
"""
Alias for characteristic_spacing
"""
return characteristic_spacing(periods, mp, Mstar)
def C(periods, warn=True):
"""
Alias for gap_complexity
"""
return gap_complexity(periods, warn=warn)
def f(periods, rhostar, rprs, dur, dur_err):
"""
Alias for flatness
"""
return flatness(periods, rhostar, rprs, dur, dur_err)
###
def dynamical_mass(mp, Mstar):
"""
Dynamical mass
mp: array of planet masses [M_earth]
Mstar: stellar mass [M_sun]
"""
return np.sum(mp)/MSME/Mstar
def mass_partitioning(masses):
"""
Mass partitioning
masses: array of planet masses
"""
return LMC.D(masses/np.sum(masses))
def monotonicity(periods, masses):
"""
Monotonicity
periods: array of planet periods
masses: array of planet masses corresponding to each given period
"""
N = len(periods)
rho = stats.spearmanr(periods, masses)[0]
return rho*Q(masses)**(1/N)
def characteristic_spacing(periods, mp, Mstar):
"""
Characteristic spacing
periods: array of planet periods [days]
mp: array of planet masses [M_earth]
Mstar: Stellar mass [M_sun]
"""
a = P_to_a(periods, Mstar)
radius_H = ((mp[1:]+mp[:-1])/(3*Mstar*MSME))**(1/3) * (a[1:]+a[:-1])/2
delta_H = (a[1:]-a[:-1])/radius_H
return np.mean(delta_H)
def gap_complexity(periods, warn=True):
"""
Gap complexity
periods: array of planet periods
warn: boolean flag to control warnings (default=True)
"""
if len(periods) < 3:
if warn:
warnings.warn('Complexity is undefined for N < 3; returning NaN')
return np.nan
elif len(periods) >= 3:
order = np.argsort(periods)
P = np.array(periods)[order]
pp = np.log(P[1:]/P[:-1])/np.log(P.max()/P.min())
return LMC.C(pp)
def flatness(periods, rhostar, rprs, dur, dur_err):
"""
periods: orbital periods [days]
rhostar: stellar density [solar density]
rprs: planet-to-star radius ratio
dur: transit durations [hours]
dur_err: corresponding errors on transit durations
"""
cosi = np.array([0.0])
transit_params = [periods, rhostar, rprs]
cosi, success = op.leastsq(residuals_for_duration_fit, cosi, args=(transit_params, dur, dur_err))
model_dur = calculate_duration(periods, rhostar, rprs, cosi)
return calculate_flatness(dur, model_dur)