def test_bad_input_flags(stellar_mass, planets, python_args, input_flag): Time, dt, Total = python_args with pytest.raises(ValueError) as exc_info: ttvfast.ttvfast(planets, stellar_mass, Time, dt, Total, input_flag=input_flag) assert 'invalid `input_flag`' in str(exc_info).lower()
def test_application(args): setup = args Time, dt, Total = setup[1:4] dt /= 5. assert Time == -1045 assert 0.1 < dt < 0.2 assert Total == 1700 params = setup[0] planet1 = ttvfast.models.Planet(*params[2:2 + 7]) planet2 = ttvfast.models.Planet(*params[2 + 7:]) gravity, stellar_mass = params[0:2] planets = [planet1, planet2] assert 0.9 < stellar_mass < 1.0 results = ttvfast.ttvfast(planets, stellar_mass, Time, dt, Total) python_rows = list(zip(*results['positions'])) expected = [ 1, 7, -8.828648752325788e+02, 6.363231859868642e-03, 4.321183741781629e-02 ] assert np.allclose(python_rows[22], expected)
def get_ttv(self, obs, transitcheck=True): params = self.stel_m_planets #planet1 = ttvfast.models.Planet(*params[1:1 + 7]) planet1 = ttvfast.models.Planet(*params[:7]) #planet2 = ttvfast.models.Planet(*params[1 + 7:]) planet2 = ttvfast.models.Planet(*params[7:]) #stellar_mass = params[0] stellar_mass = 0.95573417954 planets = [planet1, planet2] results = ttvfast.ttvfast(planets, stellar_mass, self.Time, self.dt, self.Total) integer_indices, epochs, times, rsky, vsky = results["positions"] t1_times = [] t2_times = [] count = 0 for i in range(len(times)): if (integer_indices[i] == 0): if (not np.isclose(times[i], -2.0)): t1_times.append(times[i]) count += 1 else: break else: t2_times.append(times[i]) if (transitcheck): if (count != len(obs.times)): print "Number of transits did not match [{a}=/={b}], logp=inf.".format( a=count, b=len(obs.times), time=datetime.utcnow()) return np.inf return t1_times
def get_chi2(self, obs): params = self.stel_m_planets planet1 = ttvfast.models.Planet(*params[:7]) planet2 = ttvfast.models.Planet(*params[7:]) stellar_mass = 0.95573417954 planets = [planet1, planet2] #get results... results = ttvfast.ttvfast(planets, stellar_mass, self.Time, self.dt, self.Total) integer_indices, epochs, times, rsky, vsky = results["positions"] t1_times = [] t2_times = [] count = 0 for i in range(len(times)): if (integer_indices[i] == 0): if (not np.isclose(times[i], -2.0)): t1_times.append(times[i]) count += 1 else: break else: t2_times.append(times[i]) if (count != len(obs.times)): #print "Number of transits did not match [{a}=/={b}], logp=inf.".format(a=count,b=len(obs.times),time=datetime.utcnow()) return np.inf #calc chi2 chi2 = 0. fac = len(t1_times) for i in range(len(t1_times)): chi2 += (t1_times[i] - obs.times[i])**2. * 1. / (obs.errors[i])**2. return chi2
def run_function(Time, dt, Total, params): # Taken from lweiss test planet1 = ttvfast.models.Planet(*params[2:2 + 7]) planet2 = ttvfast.models.Planet(*params[2 + 7:]) gravity, stellar_mass = params[0:2] planets = [planet1, planet2] results = ttvfast.ttvfast(planets, stellar_mass, Time, dt, Total)
def test_python_call(stellar_mass, planets, python_args): Time, dt, Total = python_args results = ttvfast.ttvfast(planets, stellar_mass, Time, dt, Total) python_rows = zip(*results['positions']) with open('testing/example_output.txt') as infile: for i, (python_row, c_row) in enumerate(zip(python_rows, infile)): c_row = c_row.strip().split() vals = (int(c_row[0]), int(c_row[1]), float(c_row[2]), float(c_row[3]), float(c_row[4])) assert np.allclose(vals, python_row) assert i == 374
def test_python_call(stellar_mass, planets, python_args): Time, dt, Total = python_args results = ttvfast.ttvfast(planets, stellar_mass, Time, dt, Total) python_rows = zip(*results) with open('testing/example_output.txt') as infile: for i, (python_row, c_row) in enumerate( zip(python_rows, infile)): c_row = c_row.strip().split() vals = (int(c_row[0]), int(c_row[1]), float(c_row[2]), float(c_row[3]), float(c_row[4])) assert np.allclose(vals, python_row) assert i == 374
def get_chi2(self, obs): #split and prep data for ttvfast params = self.stel_m_planets #planet1 = ttvfast.models.Planet(*params[1:1 + 7]) planet1 = ttvfast.models.Planet(*params[:7]) #planet2 = ttvfast.models.Planet(*params[1 + 7:]) planet2 = ttvfast.models.Planet(*params[7:]) #stellar_mass = params[0] stellar_mass = 0.95573417954 planets = [planet1, planet2] #get results... results = ttvfast.ttvfast(planets, stellar_mass, self.Time, self.dt, self.Total) #prep results integer_indices, epochs, times, rsky, vsky = results["positions"] t1_times = [] t2_times = [] count = 0 for i in range(len(times)): if (integer_indices[i] == 0): if (not np.isclose(times[i], -2.0)): t1_times.append(times[i]) count += 1 else: break else: t2_times.append(times[i]) if (count != len(obs.times)): print count print len(obs.times) print "Number of transits did not match, logp=inf." return np.inf #calc chi2 chi2 = 0. fac = len(t1_times) for i in range(len(t1_times)): chi2 += (t1_times[i] - obs.times[i])**2. * 1. / (obs.errors[i])**2. return chi2
def test_application(args): setup = args Time, dt, Total = setup[1:4] dt /= 5. assert Time == -1045 assert 0.1 < dt < 0.2 assert Total == 1700 params = setup[0] planet1 = ttvfast.models.Planet(*params[2:2+7]) planet2 = ttvfast.models.Planet(*params[2+7:]) gravity, stellar_mass = params[0:2] planets = [planet1, planet2] assert 0.9 < stellar_mass < 1.0 results = ttvfast.ttvfast(planets, stellar_mass, Time, dt, Total) python_rows = list(zip(*results)) expected = [1, 7, -8.828648752325788e+02, 6.363231859868642e-03, 4.321183741781629e-02] assert np.allclose(python_rows[22], expected)
def test_rv_given(stellar_mass, planets, python_args): rv_times = [ 475.40947, 481.50789, 505.56661, 508.60654, 514.56435, 533.36529, 537.45558, 551.44399, 582.35976, 597.30019, 611.24418, ] expected = [ -4.557399570827615e-07, -2.664545469316134e-05, -2.023759760196753e-05, -2.300427585052538e-06, 2.900816865436315e-05, 1.477347050308233e-05, 2.907887313600428e-05, -1.322952680464436e-05, 2.874508640612880e-05, -5.481921572024111e-06, -1.876024000347331e-05, ] Time, dt, Total = python_args results = ttvfast.ttvfast(planets, stellar_mass, Time, dt, Total, rv_times=rv_times) assert np.allclose(results['rv'], expected)
def forward_model(self, theta): start = self.start_time end = start + self.sim_length system = [] thetas = self.parse_with_fixed(theta) for i_pl, arr in enumerate(thetas): if self.planets[i_pl].transiting: mp_ms, P, hprime, kprime, i, Omega, t0 = arr e = hprime**2 + kprime**2 if e > e_cutoff: return None w = np.arctan2(kprime, hprime) * 180. / np.pi t0 += start M = self.t0_to_mean_anom(t0, e, w, P, start) else: mp_ms, P, hprime, kprime, i, Omega, M = arr e = hprime**2 + kprime**2 if e > e_cutoff: return None w = np.arctan2(kprime, hprime) * 180. / np.pi planet_params = [mp_ms * earth_per_sun, P, e, i, Omega, w, M] system.append(ttvfast.models.Planet(*planet_params)) results = ttvfast.ttvfast(system, stellarmass, start, self.dt, end) results = results['positions'] planet = np.array(results[0], dtype='int') time = np.array(results[2], dtype='float') timing_arrays = [] for i, transiting in enumerate(self.transiting): if transiting: timing_arr = time[planet == i] timing_arr = timing_arr[timing_arr != -2.0] timing_arr -= start timing_arrays.append(timing_arr) return np.array(timing_arrays)
def test_no_rv_given(stellar_mass, planets, python_args): Time, dt, Total = python_args results = ttvfast.ttvfast(planets, stellar_mass, Time, dt, Total) assert results['rv'] is None
def retr_modl_full(para, gdat, modltype): indxtranmodl = [[] for j in gdat.indxplan] timetranmodl = [[] for j in gdat.indxplan] indxtranmodlproj = [[] for j in gdat.indxplan] timetranmodlproj = [[] for j in gdat.indxplan] if gdat.modltype == 'line': for j in gdat.indxplan: indxtranmodl[j] = np.arange(500) #print('j') #print(j) #print('para[j]') #print(para[j]) #print timetranmodl[j] = para[j] + para[j + 3] * indxtranmodl[j] if gdat.modltype == 'nbod': mass = para[:gdat.numbplan] peri = para[1 * gdat.numbplan:2 * gdat.numbplan] ecce = para[2 * gdat.numbplan:3 * gdat.numbplan] incl = para[3 * gdat.numbplan:4 * gdat.numbplan] land = para[4 * gdat.numbplan:5 * gdat.numbplan] argu = para[5 * gdat.numbplan:6 * gdat.numbplan] anom = para[6 * gdat.numbplan:7 * gdat.numbplan] paraglob = [ 0.000295994511, # G 0.40, # Mstar #planet b mass[0], # mass of the planet peri[0], # period of the planet ecce[0], # eccentricity s of the planet incl[0], # inclination of the planet land[0], # longitude of the ascending node, Omega argu[0], # argument of periapsis, omega anom[0], # mean anomaly of the planet #planet c mass[1], # mass of the planet peri[1], # period of the planet ecce[1], # mass of the planet incl[1], # period of the planet land[1], # mass of the planet argu[1], # period of the planet anom[1], # mass of the planet #planet d mass[2], # mass of the planet peri[2], # period of the planet ecce[2], # mass of the planet incl[2], # period of the planet land[2], # mass of the planet argu[2], # period of the planet anom[2], # mass of the planet ] planet1 = ttvfast.models.Planet(*paraglob[2:9]) planet2 = ttvfast.models.Planet(*paraglob[9:16]) planet3 = ttvfast.models.Planet(*paraglob[16:]) gravity, gdat.massstar = paraglob[:2] listobjtplan = [planet1, planet2, planet3] results = ttvfast.ttvfast(listobjtplan, gdat.massstar, gdat.inittimefastttvr, gdat.delttimefastttvr, gdat.numbstepfastttvr) for j in gdat.indxplan: indx = np.where(np.array(results['positions'][0]) == j)[0] timetranmodl[j] = np.array(results['positions'][2])[indx] indxgood = np.where(timetranmodl[j] != -2.)[0] numbtran = indxgood.size timetranmodl[j] = timetranmodl[j][indxgood] indxtranmodl[j] = np.arange(numbtran, dtype=int) for j in gdat.indxplan: indxtranmodlproj[j] = np.intersect1d(indxtranmodl[j], gdat.indxtranobsd[j]) timetranmodlproj[j] = timetranmodl[j][indxtranmodlproj[j]] #print('j') #print(j) #print('indxtranmodl[j]') #summgene(indxtranmodl[j]) #print('timetranmodl[j]') #summgene(timetranmodl[j]) #print('gdat.indxtranobsd[j]') #summgene(gdat.indxtranobsd[j]) #print('gdat.timetranobsd[j]') #summgene(gdat.timetranobsd[j]) #print('indxtranmodlproj[j]') #summgene(indxtranmodlproj[j]) #print('timetranmodlproj[j]') #summgene(timetranmodlproj[j]) #print('') if gdat.diagmode: if timetranmodlproj[j].size != gdat.timetranobsd[j].size: print('j') print(j) print('timetranmodlproj[j]') print(timetranmodlproj[j]) print('gdat.timetranobsd[j]') print(gdat.timetranobsd[j]) raise Exception('') #print('') #print('') #print('') return indxtranmodl, timetranmodl, indxtranmodlproj, timetranmodlproj
def run_TTVFast(flat_params, mstar, init_time=0., final_time=None, dt=None): """A function to communicate with the wrapper around TTVFast Parameters ---------- flat_params : array or list A flat array containing the seven planet parameters of the first planet concatenated with the seven parameters of the next planet and so on. Thus, it should be of length 7 x number of planets. The order per planet must be: mass [Mearth], period [days], eccentricity, inclination [deg], argument of periastron [deg], mean anomaly [deg] and ascending node [deg]. mstar : float The stellar mass [Msun]. init_time : float Initial time of the simulations [days], by default 0. final_time : float Final time of the simulations [days], by default None. dt : float Timestep of the simulations [days], by default None. Returns ------- array An array with transit numbers and transit epochs for all planets labeled in the entrance order from flat_params. From TTVFast doc: [PLANET, EPOCH, TIME (DAYS), RSKY (AU), VSKY (AU/DAY)] SP[0] = planet index, SP[1] = transit number, SP[2] = central time, SP[3] = Rsky, SP[4] = Vsky """ if (len(flat_params) % 7) == 0: pass else: sys.exit('flat_params do not contains the necessary elements. '+ 'Seven parameters per planet must be given') # Split 'flat_params' using 7 parameters per planet iters = [iter(flat_params)] * 7 planets = list(zip(*iters)) # Iteratively adds planet's parameters to TTVFast planets_list = [] for planet in planets: planets_list.append( ttvfast.models.Planet( mass= planet[0]*Mearth_to_Msun, period = planet[1], eccentricity = planet[2], inclination = planet[3], argument = planet[4], mean_anomaly = planet[5], longnode = planet[6] ) ) signal = ttvfast.ttvfast( planets_list, stellar_mass=mstar, time=init_time, total=final_time, dt=dt, rv_times=None, input_flag=1) SP = signal['positions'] sig2 = SP[2] i = sig2.index(-2) SP = np.array([s[:i] for s in SP]) return SP