예제 #1
0
    def test_rotational_invariance(self):
        j=4
        k=1
        avars = Andoyer.from_Simulation(self.sim, j, k, i1=1, i2=2)

        rot = np.pi/3.
        ps = self.sim.particles
        simrot = rebound.Simulation()
        simrot.G = self.sim.G
        simrot.add(m=ps[0].m)
        for p in ps[1:]:
            simrot.add(m=p.m, a=p.a, e=p.e, inc=p.inc, Omega=p.Omega+rot, pomega=p.pomega+rot, l=p.l+rot)
        simrot.move_to_com()

        avarsrot = Andoyer.from_Simulation(simrot, j, k, i1=1, i2=2)
        self.assertAlmostEqual(avars.X, avarsrot.X, delta=self.delta) 
        self.assertAlmostEqual(avars.Y, avarsrot.Y, delta=self.delta) 
        self.assertAlmostEqual(avars.Zcom, avarsrot.Zcom, delta=self.delta) 
        self.assertAlmostEqual(np.cos(avars.phiZcom+rot), np.cos(avarsrot.phiZcom), delta=self.delta) 
        self.assertAlmostEqual(avars.B, avarsrot.B, delta=self.delta) 
        self.assertAlmostEqual(avars.dKprime, avarsrot.dKprime, delta=self.delta) 
        self.assertAlmostEqual(np.cos(avars.theta+k*rot), np.cos(avarsrot.theta), delta=self.delta) 
        p = avars.params
        fac = (p['m1']*p['sLambda10'] + p['m2']*p['sLambda20'])/p['K0']
        self.assertAlmostEqual(np.cos(avars.theta1+fac*rot), np.cos(avarsrot.theta1), delta=self.delta) 
예제 #2
0
def get_k(row):
    sim = rebound.SimulationArchive(sim_names + "_sa_%d.bin" % (row[0]))[0]
    #     print(sim)
    p2 = sim.particles[2]
    row['h'] = p2.e * np.sin(p2.pomega)
    row['k'] = p2.e * np.cos(p2.pomega)
    avars = Andoyer.from_Simulation(sim,
                                    a10=sim.particles[1].a,
                                    j=5,
                                    k=1,
                                    i1=1,
                                    i2=2,
                                    average=False)
    row['Z12'] = avars.Z
    row['Zcom12'] = avars.Zcom
    avars = Andoyer.from_Simulation(sim,
                                    a10=sim.particles[1].a,
                                    j=4,
                                    k=1,
                                    i1=2,
                                    i2=3,
                                    average=False)
    row['Z23'] = avars.Z
    row['Zcom23'] = avars.Zcom
    row['e1'] = sim.particles[1].e
    row['e2'] = sim.particles[2].e
    row['e3'] = sim.particles[3].e
    row['m1'] = sim.particles[1].m
    row['m2'] = sim.particles[2].m
    row['m3'] = sim.particles[3].m
    return row
예제 #3
0
    def test_from_elements(self):
        j = 5
        k = 2
        Zstar = 0.1
        libfac = 0.5
        a10 = 3.
        a1 = 3.1
        G = 2.
        m1 = 1.e-7
        m2 = 1.e-4
        ecom = 0.05
        phiecom = 0.7

        avars = Andoyer.from_elements(j,
                                      k,
                                      Zstar,
                                      libfac,
                                      a10,
                                      a1,
                                      G,
                                      m1=m1,
                                      m2=m2,
                                      Zcom=ecom,
                                      phiZcom=phiecom)
        self.assertAlmostEqual(avars.Zstar, Zstar, delta=1.e-12)
        self.assertAlmostEqual(avars.Zcom, ecom, delta=1.e-15)
        self.assertAlmostEqual(avars.phiZcom, phiecom, delta=1.e-15)
        sim = avars.to_Simulation()
        self.assertAlmostEqual(
            sim.particles[1].a, a1, delta=3 * ((a1 - a10) / a10)**
            2)  # should match to O(da/a)^2, atrue=1, a10=a10
예제 #4
0
    def test_ecom(self):
        j = 57
        k = 2
        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 / 6,
                f=np.pi,
                jacobi_masses=True)
        sim.add(m=3.e-6,
                e=0.03,
                pomega=np.pi / 3,
                P=float(j) / (j - k),
                jacobi_masses=True)  #float(j)/(j-k), theta=3.14)
        sim.move_to_com()
        ps = sim.particles
        e1x = ps[1].e * np.cos(ps[1].pomega)
        e1y = ps[1].e * np.sin(ps[1].pomega)
        e2x = ps[2].e * np.cos(ps[2].pomega)
        e2y = ps[2].e * np.sin(ps[2].pomega)

        avars = Andoyer.from_Simulation(sim, j, k, a10=1.02, average=True)
        m1 = avars.params['m1']
        m2 = avars.params['m2']
        ecomx = (m1 * e1x + m2 * e2x) / (m1 + m2)
        ecomy = (m1 * e1y + m2 * e2y) / (m1 + m2)
        ecomsim = np.sqrt(ecomx**2 + ecomy**2)

        self.assertAlmostEqual(avars.Zcom, ecomsim, delta=1.e-3)
        phiecomsim = np.arctan2(ecomy, ecomx)
        self.assertAlmostEqual(avars.phiZcom, phiecomsim, delta=1.e-3)
예제 #5
0
    def test_ZsGammaConversion(self):
        avars = Andoyer.from_Simulation(self.sim, 4, 1)
        Z = avars.Z
        phiZ = avars.psi1
        Zcom = avars.Zcom
        phiZcom = avars.phiZcom
        sGamma1, gamma1, sGamma2, gamma2 = avars.Zs_to_sGammas(Z, phiZ, Zcom, phiZcom)
        nZ, nphiZ, nZcom, nphiZcom = avars.sGammas_to_Zs(sGamma1, gamma1, sGamma2, gamma2)
        self.assertAlmostEqual(nZ,Z, delta=1.e-15)
        self.assertAlmostEqual(np.mod(nphiZ, 2*np.pi), np.mod(phiZ, 2*np.pi), delta=1.e-15)
        self.assertAlmostEqual(nZcom, Zcom, delta=1.e-15)
        self.assertAlmostEqual(np.mod(nphiZcom, 2*np.pi), np.mod(phiZcom, 2*np.pi), delta=1.e-15)

        # should also be equivalent for 2 massive particles to rotation
        pvars = Poincare.from_Simulation(self.sim)
        ps = pvars.particles
        p = avars.params
        f, g = p['f'], p['g']
        ff = f*np.sqrt(p['eta']/p['m1']/p['sLambda10'])
        gg = g*np.sqrt(p['eta']/p['m2']/p['sLambda20'])
        norm = np.sqrt(ff*ff + gg*gg)
        psirotmatrix = np.array([[ff,gg],[-gg,ff]]) / norm
        invpsirotmatrix = np.array([[ff,-gg],[gg,ff]]) / norm
        Psi1,psi1,Psi2,psi2 = self.rotate_actions(ps[1].Gamma/p['eta'],ps[1].gamma,ps[2].Gamma/p['eta'],ps[2].gamma, psirotmatrix)
        self.assertAlmostEqual(Psi1, avars.Psi1, delta=1.e-15)
        self.assertAlmostEqual(np.mod(psi1, 2*np.pi), np.mod(avars.psi1, 2*np.pi), delta=1.e-15)
        self.assertAlmostEqual(Psi2, avars.Psi2, delta=1.e-15)
        self.assertAlmostEqual(np.mod(psi2, 2*np.pi), np.mod(avars.psi2, 2*np.pi), delta=1.e-15)
예제 #6
0
 def test_dP(self):
     j=5
     k=2
     sim = rebound.Simulation()
     sim.add(m=1.)
     sim.add(m=1.e-6, P=1.)
     sim.add(m=3.e-6, P=1.68)
     sim.move_to_com()
     avars = Andoyer.from_Simulation(sim,j,k, a10=0.32, average=False) # real a0 ~0.29, 10% err
     self.assertAlmostEqual(1.68-float(j)/(j-k), avars.dP, delta=0.01) # err is da^2 or smaller, so 1%
예제 #7
0
 def test_masses(self):
     # turn off averaging for all transformation tests since averaging
     # is not symmetric back and forth (there's diff of O(s^2))
     masses = [1., 1.e-5, 1.e-7, 1.e-3]
     pairs = [[1,2], [2,3], [1,3]]
     for i1, i2 in pairs:
         m = [masses[0], masses[i1], masses[i2]]
         avars = Andoyer.from_Simulation(self.sim, 4, 1, average=False, i1=i1, i2=i2)
         sim = avars.to_Simulation(masses=m, average=False)
         self.compare_particles(self.sim, sim, i1, 1, self.delta)
         self.compare_particles(self.sim, sim, i2, 2, self.delta)
예제 #8
0
    def test_scale_invariance(self):
        avars = Andoyer.from_Simulation(self.sim, 4, 1, i1=1, i2=2)
        
        mfac = 3.
        dfac = 0.05
        vfac = np.sqrt(mfac/dfac)
        for p in self.sim.particles:
            p.m *= mfac 
            p.vx *= vfac
            p.vy *= vfac
            p.vz *= vfac
            p.x *= dfac
            p.y *= dfac
            p.z *= dfac

        avarsscaled = Andoyer.from_Simulation(self.sim, 4, 1, i1=1, i2=2)
        self.assertAlmostEqual(avars.X, avarsscaled.X, delta=self.delta) 
        self.assertAlmostEqual(avars.Y, avarsscaled.Y, delta=self.delta) 
        self.assertAlmostEqual(avars.Zcom, avarsscaled.Zcom, delta=self.delta) 
        self.assertAlmostEqual(np.cos(avars.phiZcom), np.cos(avarsscaled.phiZcom), delta=self.delta) 
        self.assertAlmostEqual(avars.B, avarsscaled.B, delta=self.delta) 
        self.assertAlmostEqual(avars.dKprime, avarsscaled.dKprime, delta=self.delta) 
        self.assertAlmostEqual(np.cos(avars.theta), np.cos(avarsscaled.theta), delta=self.delta) 
        self.assertAlmostEqual(np.cos(avars.theta1), np.cos(avarsscaled.theta1), delta=self.delta) 
예제 #9
0
    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
예제 #10
0
def populate_trio(sim, trio, pairs, jk, a10, tseries, i):
    Ns = 3
    ps = sim.particles
    for q, [label, i1, i2] in enumerate(pairs):
        e1x, e1y = ps[i1].e * np.cos(ps[i1].pomega), -ps[i1].e * np.sin(
            ps[i1].pomega)
        e2x, e2y = ps[i2].e * np.cos(ps[i2].pomega), -ps[i2].e * np.sin(
            ps[i2].pomega)
        try:
            # it's unlikely but possible for bodies to land nearly on top of  each other midtimestep and get a big kick that doesn't get caught by collisions  post timestep. All these cases are unstable, so flag them as above
            # average only affects a (Lambda) Z and I think  Zcom  don't depend on a. Zsep and Zstar slightly, but several sig figs in even when close at conjunction
            j, k = jk[label]
            avars = Andoyer.from_Simulation(sim,
                                            a10=a10[label],
                                            j=j,
                                            k=k,
                                            i1=i1,
                                            i2=i2,
                                            average=False)
            tseries[i, Ns * q + 1] = avars.Z * np.sqrt(2)  # EM = Z*sqrt(2)
            tseries[i, Ns * q + 2] = avars.Zcom  # no sqrt(2) factor
        except:  # no nearby resonance, use EM and ecom
            tseries[i, Ns * q + 1] = np.sqrt((e2x - e1x)**2 + (e2y - e1y)**2)
            tseries[i, Ns * q + 2] = np.sqrt(
                (ps[i1].m * e1x + ps[i2].m * e2x)**2 +
                (ps[i1].m * e1y + ps[i2].m * e2y)**2) / (ps[i1].m + ps[i2].m)
        try:
            j, k, tseries[i, Ns * q + 3] = find_strongest_MMR(
                sim, i1, i2
            )  # will fail if any osculating orbit is hyperbolic, flag as unstable
        except:
            return False

    tseries[i, 7] = sim.calculate_megno()  # megno

    return True
예제 #11
0
def get_resonant(seed, Nplanets=3):
    r = random.Random()
    r.seed(seed)

    Phiprimecrits = [0, 1., -2. / 3.]
    pairs = ['inner', 'outer', 'split']

    k = r.randint(1, 2)
    pairindex = r.randint(0, 2)
    pair = pairs[pairindex]
    m1 = logunif(r, 1.e-7, 1.e-4)
    m2 = logunif(r, 1.e-7, 1.e-4)
    eH = ((m1 + m2) / 3.)**(1. / 3.)
    ehillstable = 3.5 * eH
    jmax = k / (1 - 1 / (1 + 3.5 * eH)**1.5)
    if pair == 'split':
        if Nplanets == 2:
            return  # don't want 2planet systems 60 hill radii apart
        maxHillradii = 60.  # 3rd planet will go in middle so draw up to 60
    else:
        maxHillradii = 30.
    jmin = max(k + 1, k / (1 - 1 / (1 + maxHillradii * eH)**1.5))
    jmin = int(np.ceil(jmin))
    jmax = int(np.floor(jmax))
    if k == 2:  # if k = 2, want odd j so we don't get e.g. 8:6 = 4:3
        if jmin % 2 == 0:  # even
            jmin += 1
        if jmax % 2 == 0:
            jmax -= 1
    j = r.randrange(
        jmin, jmax + 1,
        k)  # choose randomly between limits in steps of k e.g. (3,5,7,9)
    a1 = 1.
    a2 = (float(j) / (j - k))**(2. / 3.)
    ecross1 = (a2 - a1) / a1
    ecross2 = (a2 - a1) / a2
    emin1 = m2 / ecross1**2
    emin2 = m1 / ecross2**2
    emin = max(
        emin1, emin2
    )  # take as min Z the larger of the kicks a planet gets at conjunction
    emin = max(
        emin, (m1 + m2)**(1. / k)
    )  # below mtot^1/k, the resonant term is smaller than the second order mass terms we ignore
    emax = min(ecross1, ecross2)

    avars = Andoyer(j=j, k=k, X=0, Y=0, m1=m1, m2=m2)
    Phiprimecrit = Phiprimecrits[k]
    Xcrit = get_Xstarres(k, Phiprimecrit)
    Phicrit = 0.5 * Xcrit**2
    emin = max(
        avars.Phi_to_Z(Phicrit), emin
    )  # first quantity is value of Z at bifurcation when res first appears

    Zstar = logunif(r, emin, emax)
    libfac = logunif(r, 3.e-3, 3)
    negative = r.randint(0, 1)
    if negative:
        libfac *= -1

    Zcom = logunif(r, emin, emax)
    avars = Andoyer.from_elements(j=j,
                                  k=k,
                                  Zstar=Zstar,
                                  libfac=libfac,
                                  m1=m1,
                                  m2=m2,
                                  Zcom=Zcom,
                                  phiZcom=r.uniform(0, 2 * np.pi),
                                  theta=r.uniform(0, 2 * np.pi),
                                  theta1=r.uniform(0, 2 * np.pi))
    tmax = r.uniform(0, 10 * avars.tlib)
    H = AndoyerHamiltonian(avars)
    H.integrate(tmax)
    pvars = avars.to_Poincare()
    ps = pvars.particles
    if Nplanets == 3:
        m3 = logunif(r, 1.e-7, 1.e-4)
        pvarssorted = Poincare(G=pvars.G)

        if pair == "inner":
            eH = ((m2 + m3) / 3.)**(1. / 3.)
            beta = r.uniform(3.5, 30)
            a3 = a2 * (1 + beta * eH)
            ecross3 = (a3 - a2) / a3
            emin3 = m2 / ecross3**2
            e3 = logunif(r, emin3, ecross3)
            pvarssorted.add(m=ps[1].m,
                            M=ps[1].M,
                            a=ps[1].a,
                            e=ps[1].e,
                            gamma=ps[1].gamma,
                            l=ps[1].l)
            pvarssorted.add(m=ps[2].m,
                            M=ps[2].M,
                            a=ps[2].a,
                            e=ps[2].e,
                            gamma=ps[2].gamma,
                            l=ps[2].l)
            pvarssorted.add(m=m3,
                            M=1,
                            a=a3,
                            e=e3,
                            gamma=r.uniform(0, 2 * np.pi),
                            l=r.uniform(0, 2 * np.pi))

        elif pair == "outer":
            eH = ((m1 + m3) / 3.)**(1. / 3.)
            beta = r.uniform(3.5, 30)
            a3 = a1 / (1 + beta * eH)
            ecross3 = (a1 - a3) / a3
            emin3 = m1 / ecross3**2
            e3 = logunif(r, emin3, ecross3)
            pvarssorted.add(m=m3,
                            M=1,
                            a=a3,
                            e=e3,
                            gamma=r.uniform(0, 2 * np.pi),
                            l=r.uniform(0, 2 * np.pi))
            pvarssorted.add(m=ps[1].m,
                            M=ps[1].M,
                            a=ps[1].a,
                            e=ps[1].e,
                            gamma=ps[1].gamma,
                            l=ps[1].l)
            pvarssorted.add(m=ps[2].m,
                            M=ps[2].M,
                            a=ps[2].a,
                            e=ps[2].e,
                            gamma=ps[2].gamma,
                            l=ps[2].l)

        elif pair == "split":
            eH1 = ((m1 + m3) / 3.)**(1. / 3.)
            eH2 = ((m2 + m3) / 3.)**(1. / 3.)
            amin = a1 * (1 + 3.5 * eH1)
            amax = min(a1 * (1 + 30 * eH1), a2 / (1 + 3.5 * eH2))
            if amin > amax:
                seed += 900000
                return get_resonant(
                    seed=seed)  # draw new sample (may not be 'split')
            a3 = r.uniform(amin, amax)
            ecross3 = min((a3 - a1) / a1, (a2 - a3) / a3)
            emin3 = max(m1 / ((a3 - a1) / a3)**2, m2 / ((a2 - a3) / a3)**2)
            e3 = logunif(r, emin3, ecross3)
            pvarssorted.add(m=ps[1].m,
                            M=ps[1].M,
                            a=ps[1].a,
                            e=ps[1].e,
                            gamma=ps[1].gamma,
                            l=ps[1].l)
            pvarssorted.add(m=m3,
                            M=1,
                            a=a3,
                            e=e3,
                            gamma=r.uniform(0, 2 * np.pi),
                            l=r.uniform(0, 2 * np.pi))
            pvarssorted.add(m=ps[2].m,
                            M=ps[2].M,
                            a=ps[2].a,
                            e=ps[2].e,
                            gamma=ps[2].gamma,
                            l=ps[2].l)

        sim = pvarssorted.to_Simulation()
    else:
        sim = avars.to_Simulation()
    # add inclinations and scale s.t. a1 = 1, Mprimary=1. and G = 4*pi**2
    ps = sim.particles

    siminc = rebound.Simulation()
    siminc.G = 4 * np.pi**2

    dscale = ps[1].a
    tscale = ps[1].P
    mscale = siminc.G * dscale**3 / tscale**2  # ps[0].m + ps[1].m

    siminc.add(m=ps[0].m / mscale,
               x=ps[0].x / dscale,
               y=ps[0].y / dscale,
               vx=ps[0].vx / dscale * tscale,
               vy=ps[0].vy / dscale * tscale)
    for p in ps[1:]:
        siminc.add(m=p.m / mscale,
                   a=p.a / dscale,
                   e=p.e,
                   inc=logunif(r, 1.e-3, 1.e-1),
                   Omega=r.uniform(0, 2 * np.pi),
                   pomega=p.pomega,
                   l=p.l)
        rH = siminc.particles[-1].a * (siminc.particles[-1].m / 3. /
                                       siminc.particles[0].m)**(1. / 3.)
        siminc.particles[-1].r = rH
    siminc.move_to_com()
    siminc.integrator = "whfast"
    siminc.dt = 2. * np.sqrt(3) / 100. * siminc.particles[1].P
    siminc.ri_whfast.safe_mode = 0
    siminc.collision = "line"
    return siminc, j, k, pairindex, Zstar, libfac, Zcom