def rescale(sim, dscale, tscale, mscale): simr = rebound.Simulation() vscale = dscale / tscale simr.G *= mscale * tscale**2 / dscale**3 for p in sim.particles: simr.add(m=p.m / mscale, x=p.x / dscale, y=p.y / dscale, z=p.z / dscale, vx=p.vx / vscale, vy=p.vy / vscale, vz=p.vz / vscale, r=p.r / dscale) return simr
def test_sa_binary_version(self): sim = rebound.Simulation() sim.add(m=1) sim.add(m=1e-3,a=1,e=0.1,omega=0.1,M=0.1,inc=0.1,Omega=0.1) sim.add(m=1e-3,a=-2,e=1.1,omega=0.1,M=0.1,inc=0.1,Omega=0.1) sim.integrator = "ias15" sim.dt = 0.1313 sim.automateSimulationArchive("test.bin", walltime = 0.01,deletefile=True) sim.integrate(400.,exact_finish_time=0) with open("test.bin","r+b") as f: f.seek(30) f.write("1.0.0 ".encode('ascii')) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") sa = rebound.SimulationArchive("test.bin") self.assertEqual(1, len(w))
def test_af_ias15(self): sim = rebound.Simulation() sim.integrator = "ias15" sim.force_is_velocity_dependent = 1 sim.add(m=1) sim.add(m=1e-6,a=1) sim.add(m=1e-3,a=5) sim.move_to_com() def af(sim): fac = 0.01 sim.contents.particles[2].ax -= fac*sim.contents.particles[2].vx sim.contents.particles[2].ay -= fac*sim.contents.particles[2].vz sim.contents.particles[2].az -= fac*sim.contents.particles[2].vy sim.additional_forces = af sim.integrate(10.) self.assertAlmostEqual(sim.particles[2].a,4.86583,delta=1e-5)
def test_multiply_with_zero(self): sim = rebound.Simulation() sim.add(m=1) sim.add(m=1e-3, a=1, e=0.1, omega=0.1, M=0.1, inc=0.1, Omega=0.1) sim.add(m=1e-3, a=-2, e=1.1, omega=0.1, M=0.1, inc=0.1, Omega=0.1) sim.integrator = "whfast" sim.integrate(2.) sim = 0 * sim for i in range(sim.N): self.assertEqual(-sim.particles[i].x, 0.) self.assertEqual(-sim.particles[i].y, 0.) self.assertEqual(-sim.particles[i].z, 0.) self.assertEqual(-sim.particles[i].vx, 0.) self.assertEqual(-sim.particles[i].vy, 0.) self.assertEqual(-sim.particles[i].vz, 0.)
def test_janus_energy(self): for o, eps in [ (2,1e-4), (4,1e-8), (6,1e-9), (8,1e-11), (10,1e-13)]: sim = rebound.Simulation() sim.add(m=1.) sim.add(m=1e-3,a=1.12313) sim.add(m=1e-3,a=2.32323) sim.move_to_com() sim.dt = 0.25 sim.integrator = "janus" sim.ri_janus.order = o sim.ri_janus.scale_pos = 1e-16 sim.ri_janus.scale_vel = 1e-16 e0 = sim.calculate_energy() sim.integrate(1e2) e1 = sim.calculate_energy() self.assertLess(math.fabs((e0-e1)/e1),eps)
def test_time_scaling_from_integration(self): times = [] sims = [] mass = 1e-3 for P in [1, 10]: sim = rebound.Simulation() sim.add(m=1.) sim.add(m=mass, P=1*P) sim.add(m=mass, P=1.3*P) sim.add(m=mass, P=1.6*P) sims.append(sim) # Second time should have ~10x larger inst time. times = self.model.predict_instability_time(sims, seed=0, **SAMPLE_SETTINGS)[0] # Should be much larger time: self.assertGreater(times[1], 5*times[0])
def test_prediction(self): times = [] sims = [] for mass in [1e-4, 5e-5, 3e-5, 1e-5]: sim = rebound.Simulation() sim.add(m=1.) sim.add(m=mass, P=1) sim.add(m=mass, P=1.3) sim.add(m=mass, P=1.6) sims.append(sim) times = np.log10(self.model.predict_instability_time(sims, **SAMPLE_SETTINGS)[0]) # First one is unstable: self.assertTrue(times[0] < 4.0) # Should get more stable: self.assertTrue(np.all(times[1:] > times[:-1]))
def get_elements_fromdat(dat): ''' Given a dat file, gets relevant orbital elements Paraneters: dat: Data file Returns: relevant orbital elements calculated using Rebound''' a,e,i,Omega,omega,trueanomaly=get_orbital_elements(dat) sim=rebound.Simulation() #Add bh sim.add(m = 1) #Add 100 stars for j in range(len(dat)): sim.add(a=a[j],e=e[j],inc=i[j],Omega=Omega[j],omega=omega[j],f=trueanomaly[j]) orbits=sim.calculate_orbits(primary=sim.particles[0]) return get_orbital_elements_fromrebound(orbits)
def energy_notcom(self, s): integrator, maxerror = s sim = rebound.Simulation() rebound.data.add_outer_solar_system(sim) for p in sim.particles: p.vx += 1. com = sim.calculate_com() sim.integrator = integrator sim.dt = 0.0123235235*sim.particles[1].P e0 = sim.calculate_energy() sim.integrate(1000.*2.*3.1415) e1 = sim.calculate_energy() self.assertLess(math.fabs((e0-e1)/e1),maxerror) com1 = sim.calculate_com() self.assertLess(math.fabs((com.x+com.vx*sim.t-com1.x)/(com1.x+com1.y)),1e-12) self.assertLess(math.fabs((com.y+com.vy*sim.t-com1.y)/(com1.x+com1.y)),1e-12)
def test_custom_prior(self): mass = 1e-7 sim = rebound.Simulation() sim.add(m=1.) sim.add(m=mass, P=1) sim.add(m=mass, P=1.3) sim.add(m=mass, P=1.6) expected_center = 13.0 prior = lambda logT: np.exp(-(logT - expected_center)**2 / 2 / 0.1**2) times = np.log10( self.model.predict_instability_time(sim, prior_above_9=prior, **SAMPLE_SETTINGS)[0]) self.assertAlmostEqual(times, expected_center, delta=1e-2)
def reb_cb(cb,tmin=None,tmax=None): '''Return transit times for a circumbinary system. Parameters ---------- cb : CBSys object Class holding system configuration. tmin, tmax : float Start and end times of simulation, in days. ''' sim = rebound.Simulation() sim.t = 0.0 sim.add(m = cb.m1) comp = rebound.Particle(simulation=sim, m=cb.m2, a=cb.ab, e=cb.eb, inc=cb.ib, omega=cb.wb, f=cb.fb, Omega=cb.Wb) sim.add(comp) sim.move_to_com() planet = rebound.Particle(simulation=sim, m=cb.mp, a=cb.ap, e=cb.ep, inc=cb.ip, omega=cb.wp, f=cb.fp, Omega=cb.Wp) sim.add(planet) sim.move_to_com() # integrate to start time sim.integrate(tmax = (tmin - cb.t0)/365.25 * 2 * np.pi ) # globals for heartbeat p = sim.particles global transittimes, lastt, lastdx1, lastdx2, lastdx12, lastdv1, lastdv2 lastt = sim.t; lastdx1 = p[2].x - p[0].x lastdx2 = p[2].x - p[1].x lastdx12 = p[1].x - p[0].x lastdv1 = p[2].vx - p[0].vx lastdv2 = p[2].vx - p[1].vx transittimes = () # integrate to end sim.heartbeat = heartbeat sim.integrate(tmax = (tmax - cb.t0)/365.25 * 2 * np.pi ) # get transit times and convert back to days tts = np.array(transittimes) tts[:,1] /= 2.0 * np.pi / 365.25 tts[:,1] += cb.t0 return tts
def make_sim(d, system, inc, Omega, Nplanets=3): # set up simulation sim = rebound.Simulation() sim.integrator = 'whfast' sim.G = 1 sim.ri_whfast.safe_mode = 0 sim.collision = 'direct' sim.collision_resolve = collision # add sun Ms = d["Ms"] sim.add(m=Ms) earth2solar = 0.000003003 # earth to solar mass conversion # add planets nomass_sys, vaneye_sys, danjh_sys = sysp.get_system_lists() for i in range(1, Nplanets + 1): m, P = d["m%d"%i]*earth2solar, d["P%d"%i] a = ((P/365.)**2 * Ms)**(1./3.) if system in nomass_sys: e = d["e%d"%i] w = d["w%d"%i] M = d["MA%d"%i] elif system in vaneye_sys: e = d["e%d"%i] w = d["w%d"%i] min_MidTransitTime = np.min((data["T1"], data["T2"], data["T3"])) M = get_M(e, w, d["T%d"%i], P, min_MidTransitTime) elif system in danjh_sys: e = np.sqrt(d["h%d"%i]**2 + d["k%d"%i]**2) # sqrt(h^2 + k^2) w = np.arctan2(d["h%d"%i], d["k%d"%i]) # arctan2(h/k) epoch = 780 M = get_M(e, w, d["T%d"%i], P, epoch) # T = epoch = BJD-2,454,900 m *= Ms # dan jontoff-hutter planetary masses scaled to 1 solar mass. else: raise Exception('system is not recognized') sys.exit(1) hill = a*(m/(3*Ms))**(1./3.) #sim.add(m=m, a=a, e=e, omega=w, M=M, inc=inc[i-1], Omega=Omega[i-1], r=hill/2.) # G=1 units! sim.add(m=m, a=a, e=e, pomega=w, l=M+w, Omega=Omega[i-1], inc=inc[i-1], r=hill) # G=1 units! sim.move_to_com() #timestep dt = 2.*math.sqrt(3)/100. P1 = sim.particles[1].P sim.dt = dt*P1 # ~3% orbital period return sim
def backandforth(self, s): integrator, maxerror = s sim = rebound.Simulation() rebound.data.add_outer_solar_system(sim) for p in sim.particles: p.vx += 1. sim0=sim.copy() sim.integrator = integrator sim.dt = 0.0123235235*sim.particles[1].P steps = 10 for i in range(steps): sim.step() sim.dt *= -1 for i in range(steps): sim.step() for i in range(sim.N): self.assertLess(math.fabs(sim0.particles[i].x-sim.particles[i].x),maxerror)
def setup_sim(pos, vel, elements, denom=100): sim = rebound.Simulation() sim.G = 0.000295981944048623 # AU, Days, Msol sim.integrator = 'ias15' sim.dt = elements[1, 1] / denom # Set up initial conditions for i in range(pos.shape[0]): els = elements[i] x, y, z = pos[i] vx, vy, vz = vel[i] m = els[0] sim.add(m=m, x=x, y=y, z=z, vx=vx, vy=vy, vz=vz) sim.move_to_com() return sim
def setup_simv2(bjd0, Ms, Ps, ePs, T0s, eT0s, mps, emps, eccs, incs_deg, outname, interval_yrs, NRhill_to_stop=1, random=True, DeltaOmegamax=180): '''Create a simulation of a 2-planet system with custom input for the planetary parameters in the form of 1d arrays. example T0s = np.array([7264.49, 7264.39142]) ''' # Initialize sim = rebound.Simulation() sim.integrator = "whfast" sim.units = ('AU','Msun','yr') sim.automateSimulationArchive('%s.bin'%outname, interval=interval_yrs, deletefile=True) # Get keplerian parameters Psin, ePs = np.ascontiguousarray(Ps) + 0, np.ascontiguousarray(ePs) assert Ps[0] < Ps[1] Ps, Ks = np.zeros(2), np.zeros(2) while np.any(Ps<=0): Ps = Psin if random: Ps += np.array([np.random.randn()*ePs[0], np.random.randn()*ePs[1]]) if random: T0s += 2450000 + np.array([np.random.randn()*eT0s[0], np.random.randn()*eT0s[1]]) if random: mps += np.array([np.random.randn()*emps[0], np.random.randn()*emps[1]]) smas =rvs.semimajoraxis(Ps, Ms, mps) mps = rvs.kg2Msun(rvs.Mearth2kg(mps)) nplanets = Ps.size omegas = np.random.uniform(-np.pi, np.pi, nplanets) Omegas = np.random.uniform(-np.pi, np.pi, nplanets) thetas = 2*np.pi * foldAt(bjd0, Ps, T0s) - np.pi # Add star sim.add(m=Ms, hash='star') # Add planets for i in range(nplanets): sim.add(m=mps[i], a=smas[i], inc=np.deg2rad(incs_deg[i]-90), e=eccs[i], omega=omegas[i], Omega=Omegas[i], theta=thetas[i]) sim.move_to_com() RHill = (mps.sum()/(3.*Ms))**(1./3) * smas.mean() sim.exit_min_distance = float(RHill) * NRhill_to_stop return sim
def pltKep(rad_moon, sma, i, ecc): #rad_moon is the bodily radius of the moon #i is the inclination of the moon's orbital plane #sma is the semi-major axis of the moon's orbit #ecc is the eccentricity of the moon's orbit m_moon = 4. * math.pi * dens * (rad_moon**3) / 3. sim = rebound.Simulation() #start simulation sim.units = ('Hr', 'M', 'Kg') #use SI units sim.add(m=Mass_syn) #add central body sim.add(m=m_moon, a=sma, inc=i, e=ecc) fig, ax_main, ax_sub1, ax_sub2 = rebound.OrbitPlot( sim, slices=1, xlim=[-120000000, 60000000], ylim=[-60000000, 60000000], unitlabel='(m)', color=True)
def test_klo(self): sim = rebound.Simulation(binary) sim.integrator = "ias15" rebx = reboundx.Extras(sim) times = [0, 2000., 4000., 6000., 8000., 10000.] values = [1., 0.8, 0.6, 0.4, 0.3, 0.2] starmass = reboundx.Interpolator(rebx, times, values, "spline") m0 = sim.particles[0].m mint0 = starmass.interpolate(rebx, t=0) self.assertLess(abs((m0 - mint0) / m0), 1.e-6) mint1 = starmass.interpolate(rebx, t=5000) # advance klo mint1 = starmass.interpolate(rebx, t=0) # advance klo self.assertLess(abs((m0 - mint1) / m0), 1.e-6)
def test_subtract_same(self): sim = rebound.Simulation() sim.add(m=1) sim.add(m=1e-3, a=1, e=0.1, omega=0.1, M=0.1, inc=0.1, Omega=0.1) sim.add(m=1e-3, a=-2, e=1.1, omega=0.1, M=0.1, inc=0.1, Omega=0.1) sim.integrator = "whfast" sim.integrate(2.) sim_copy = sim.copy() sim = sim - sim_copy for i in range(sim.N): self.assertEqual(sim.particles[i].x, 0) self.assertEqual(sim.particles[i].y, 0) self.assertEqual(sim.particles[i].z, 0) self.assertEqual(sim.particles[i].vx, 0) self.assertEqual(sim.particles[i].vy, 0) self.assertEqual(sim.particles[i].vz, 0)
def test_div2(self): sim = rebound.Simulation() sim.add(m=1) sim.add(m=1e-3, a=1, e=0.1, omega=0.1, M=0.1, inc=0.1, Omega=0.1) sim.add(m=1e-3, a=-2, e=1.1, omega=0.1, M=0.1, inc=0.1, Omega=0.1) sim.integrator = "whfast" sim.integrate(2.) sim_copy = sim.copy() sim = sim / 2 for i in range(sim.N): self.assertEqual(2 * sim.particles[i].x, sim_copy.particles[i].x) self.assertEqual(2 * sim.particles[i].y, sim_copy.particles[i].y) self.assertEqual(2 * sim.particles[i].z, sim_copy.particles[i].z) self.assertEqual(2 * sim.particles[i].vx, sim_copy.particles[i].vx) self.assertEqual(2 * sim.particles[i].vy, sim_copy.particles[i].vy) self.assertEqual(2 * sim.particles[i].vz, sim_copy.particles[i].vz)
def doTest2(self): sim1 = runSimulation(self, 40., restart=False, interval=10., **params) if params['keep_unsynchronized'] == 1: sim2 = rebound.Simulation("test.bin") else: sa = rebound.SimulationArchive("test.bin") sim2 = sa.getSimulation(sa.tmax, keep_unsynchronized=0) compareSim(self, sim1, sim2) with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") sim1.integrate(sim1.t + 12.) sim2.integrate(sim2.t + 12.) if params["testparticle"] == 1: self.assertEqual(2, len(w)) else: self.assertEqual(0, len(w)) compareSim(self, sim1, sim2)
def setUp(self): self.sim = rebound.Simulation() self.sim.add(m=0.86, r = 0.78) self.sim.add(m=3.e-6, a=1., e=0.05) self.sim.move_to_com() ps = self.sim.particles self.rebx = reboundx.Extras(self.sim) self.tides = self.rebx.load_force("tides_constant_time_lag") self.rebx.add_force(self.tides) ps[0].params["tctl_k1"] = 0.023 # in AU ps[0].params["tctl_tau"] = 0.3 ps[0].params["Omega"] = 0. self.q = (ps[1].m/ps[0].m) self.T = ps[0].r**3/self.sim.G/ps[0].m/ps[0].params["tctl_tau"] self.taua = self.T/6/ps[0].params["tctl_k1"]/self.q/(1+self.q)*(ps[1].a/ps[0].r)**8
def test_rmultiply_with_minus_one(self): sim1 = rebound.Simulation() sim1.add(m=1) sim1.add(m=1e-3, a=1, e=0.1, omega=0.1, M=0.1, inc=0.1, Omega=0.1) sim1.add(m=1e-3, a=-2, e=1.1, omega=0.1, M=0.1, inc=0.1, Omega=0.1) sim1.integrator = "whfast" sim1.integrate(2.) sim2 = -1. * sim1 for i in range(sim1.N): self.assertEqual(-sim1.particles[i].x, sim2.particles[i].x) self.assertEqual(-sim1.particles[i].y, sim2.particles[i].y) self.assertEqual(-sim1.particles[i].z, sim2.particles[i].z) self.assertEqual(-sim1.particles[i].vx, sim2.particles[i].vx) self.assertEqual(-sim1.particles[i].vy, sim2.particles[i].vy) self.assertEqual(-sim1.particles[i].vz, sim2.particles[i].vz)
def test_sa_tmin(self): sim = rebound.Simulation() sim.add(m=1) sim.add(m=1e-3, a=1, e=0.1, omega=0.1, M=0.1, inc=0.1, Omega=0.1) sim.add(m=1e-3, a=-2, e=1.1, omega=0.1, M=0.1, inc=0.1, Omega=0.1) sim.integrator = "whfast" sim.dt = 0.1313 sim.integrate(400., exact_finish_time=0) sim.automateSimulationArchive("test.bin", interval=100., deletefile=True) tmin = sim.t sim.integrate(800., exact_finish_time=0) sa = rebound.SimulationArchive("test.bin") self.assertEqual(tmin, sa[0].t) self.assertEqual(tmin, sa.tmin) self.assertNotEqual(tmin, sa.tmax)
def test_sa_restart_generator(self): sim = rebound.Simulation() sim.add(m=1) sim.add(m=1e-3, a=1, e=0.1, omega=0.1, M=0.1, inc=0.1, Omega=0.1) sim.add(m=1e-3, a=-2, e=1.1, omega=0.1, M=0.1, inc=0.1, Omega=0.1) sim.integrator = "whfast" sim.dt = 0.1313 sim.ri_whfast.safe_mode = 0 sim.initSimulationArchive("test.bin", 10.) sim.integrate(40., exact_finish_time=0) sim = None sa = rebound.SimulationArchive("test.bin") times = [0., 11., 22.] for sim in sa.getSimulations(times, mode="close"): pass self.assertAlmostEqual(sim.t, 22.058400000000105, delta=sim.dt)
def simulation(integrator): print("Running "+integrator) with open(integrator+".txt","w") as f: sim = rebound.Simulation() sim.integrator = integrator sim.dt = 0.2 sim.add(m=1.) sim.add(m=0.01, a=1,e=0.1) sim.add(m=0.01, a=2.) sim.move_to_com() sim.init_megno() particles = sim.particles times = np.logspace(2,5,num=1000) for t in times: sim.integrate(t,0) print("%e %e %e %e %e %e %e %e\n" %(sim.t, sim.calculate_megno(), particles[0].x, particles[1].x, particles[2].x, particles[3].x, particles[4].x, particles[5].x),file=f)
def test_af_mercurius_closeencounter(self): sim = rebound.Simulation() sim.integrator = "mercurius" sim.ri_mercurius.hillfac = 100000 # make sure encounter happens sim.dt = 0.005 sim.force_is_velocity_dependent = 1 sim.add(m=1) sim.add(m=1e-6,a=1) sim.add(m=1e-3,a=5) sim.move_to_com() def af(sim): fac = 0.01 sim.contents.particles[2].ax -= fac*sim.contents.particles[2].vx sim.contents.particles[2].ay -= fac*sim.contents.particles[2].vz sim.contents.particles[2].az -= fac*sim.contents.particles[2].vy sim.additional_forces = af sim.integrate(10.) self.assertAlmostEqual(sim.particles[2].a,4.86583,delta=1e-4)
def simulation(par): saturn_a, saturn_e = par sim = rebound.Simulation() sim.integrator = "whfast" sim.min_dt = 5. sim.dt = 1. # These parameters are only approximately those of Jupiter and Saturn. sun = rebound.Particle(m=1.) sim.add(sun) jupiter = sim.add(primary=sun,m=0.000954, a=5.204, M=0.600, omega=0.257, e=0.048) saturn = sim.add(primary=sun,m=0.000285, a=saturn_a, M=0.871, omega=1.616, e=saturn_e) sim.move_to_com() sim.init_megno() sim.integrate(1e3*2.*np.pi) return [sim.calculate_megno(),1./(sim.calculate_lyapunov()*2.*np.pi)] # returns MEGNO and Lypunov timescale in years
def solarsystemsim(): sim = rebound.Simulation() sim.add(m=1.) sim.add(m=1.7e-7, a=0.39, e=0.21) sim.add(m=2.4e-6, a=0.72, e=0.007) sim.add(m=3.e-6, a=1, e=0.017) sim.add(m=3.2e-7, a=1.52, e=0.09) sim.add(m=1.e-3, a=5.2, e=0.049) sim.add(m=2.9e-4, a=9.54, e=0.055) sim.add(m=4.4e-5, a=19.2, e=0.047) sim.add(m=5.2e-5, a=30.1, e=0.009) for p in sim.particles[1:]: p.r = p.a * (p.m / 3)**(1 / 3) sim.move_to_com() sim.collision = 'line' sim.integrator = "whfast" sim.dt = 0.05 return sim
def test_H(self): j=3 k=1 a10 = 1.02 sim = rebound.Simulation() sim.G = 4*np.pi**2 sim.add(m=1.) sim.add(m=1.e-6, e=0.01, P=1., pomega=-np.pi/2, f=np.pi, jacobi_masses=True) sim.add(m=3.e-6, e=0.03, pomega=np.pi/2, P=float(j)/(j-k), jacobi_masses=True)#float(j)/(j-k), theta=3.14) sim.move_to_com() avars = Andoyer.from_Simulation(sim,j,k, a10=a10, average=True) p = avars.params pvars = Poincare.from_Simulation(sim, average=True) sGamma1 = pvars.particles[1].sGamma sGamma2 = pvars.particles[2].sGamma sLambda1 = pvars.particles[1].sLambda sLambda2 = pvars.particles[2].sLambda lambda1 = pvars.particles[1].l lambda2 = pvars.particles[2].l gamma1 = pvars.particles[1].gamma gamma2 = pvars.particles[2].gamma n10 = np.sqrt(p['G']*p['M1']/p['a10']**3) n20 = np.sqrt(p['G']*p['M2']/p['a10']**3*p['alpha']**3) z1 = np.sqrt(2*sGamma1/p['sLambda10']) z2 = np.sqrt(2*sGamma2/p['sLambda20']) Hkep = -0.5*(n10*p['m1']*p['sLambda10']**3/sLambda1**2 + n20*p['m2']*p['sLambda20']**3/sLambda2**2) sL10 = p['sLambda10'] sL20 = p['sLambda20'] Hkepexpanded = -n10*p['m1']*sL10/2*(1. - 2*avars.dL1hat + 3*avars.dL1hat**2)-n20*p['m2']*sL20/2*(1. - 2*avars.dL2hat + 3*avars.dL2hat**2) Hresprefac = -p['G']*p['m1']*p['m2']/p['a10']*p['alpha'] Hres = Hresprefac*(p['f']*z1*np.cos(j*lambda2 - (j-k)*lambda1 + gamma1)+p['g']*z2*np.cos(j*lambda2 - (j-k)*lambda1 + gamma2)) H0 = -p['n0']*p['K0']/2 H1 = p['eta']*p['n0']*avars.dK*(1-1.5*p['eta']/p['K0']*avars.dK) H2 = p['eta']*p['a']*avars.dP**2 Hkeptransformed = H0 + H1 + H2 Hrestransformed = p['eta']*p['c']*(2*avars.Psi1)**(k/2.)*np.cos(avars.theta+k*avars.psi1) self.assertAlmostEqual(Hkeptransformed, Hkepexpanded, delta=1.e-15) # should be exact self.assertAlmostEqual(Hrestransformed, Hres, delta=1.e-15) # should be exact for first order resonance (k=1) self.assertAlmostEqual(Hkepexpanded, Hkep, delta=(a10-1.)**2) # should match to O(da/a)^2, atrue=1, a10=a10 #Hfinal = -p['eta']*p['Phi0']/p['tau']*(4.*avars.Phi**2 - 3.*avars.Phiprime*avars.Phi + 9./16.*avars.Phiprime**2 + (2.*avars.Phi)**(k/2.)*np.cos(avars.phi) - p['n0']*p['tau']/p['Phi0']*avars.dK*(1.-1.5*p['eta']/p['K0']*avars.dK))+ H0 Hfinal = -p['eta']*p['Phi0']/p['tau']*(4.*(avars.Phi-avars.B)**2 + (2.*avars.Phi)**(k/2.)*np.cos(avars.phi) - p['n0']*p['tau']/p['Phi0']*avars.dK*(1.-1.5*p['eta']/p['K0']*avars.dK))+ H0 self.assertAlmostEqual(Hfinal, Hkepexpanded+Hres, delta=1.e-15) # should be exact
def make_sim_cart(m, q0, v0, sample_freq, integrator, debug_print): """ Make a Rebound simulation for the 3 body problem using Cartesian position and velocity. INPUTS: m: masses of of 3 bodies in solar units; array of size 3 q0: the starting positions of the 3 bodies in AU; array of size (3, 3,) = (num_body, space_dims) v0: the starting velocities of the 3 bodies in AU/yr; array of size (3, 3,) sample_freq: number of sample points per year, e.g. 365 integrator: the name of the rebound integrator. one of 'ias15' or 'whfast' RETURNS: sim: an instance of the simulator that is ready to integrate """ # Unpack the position components qx = q0[:, 0] qy = q0[:, 1] qz = q0[:, 2] # Unpack the velocity components vx = v0[:, 0] vy = v0[:, 1] vz = v0[:, 2] # Create a simulation sim = rebound.Simulation() # Set units sim.units = ('yr', 'AU', 'Msun') # Set integrator. if acceleration output required, must use ias15 sim.integrator = integrator # Set the simulation time step based on sample_freq sim.dt = 1.0 / sample_freq # Add the 3 bodies in the provided order for i in range(3): sim.add(m=m[i], x=qx[i], y=qy[i], z=qz[i], vx=vx[i], vy=vy[i], vz=vz[i]) if debug_print: print(f'particle {i}, m={m[i]}, x={qx[i]}, y={qy[i]}, z={qz[i]}, vx={vx[i]}, vy={vy[i]}, vz={vz[i]}') # Move to the center-of-momentum coordinate system sim.move_to_com() return sim