Example #1
0
def main():

    potential = AxisymmetricLogPotential(units=usys,
                                         s=1/0.9,
                                         a=0.5*(u.kpc/u.Myr)**2,
                                         b=0.*u.kpc)

    x0 = np.array([10.0, 0.0, 2.]) # kpc
    v0 = (np.array([0., 220., 20.])*u.km/u.s)\
                    .to(u.kpc/u.Myr).value # kpc/Myr

    integrator = LeapfrogIntegrator(potential._acceleration_at,
                                    x0.T, v0.T)
    ts, xs, vs = integrator.run(dt=0.1, Nsteps=10000)

    fig,axes = plt.subplots(2,2,sharex=True,sharey=True,figsize=(10,10))
    axes[0,0].plot(xs[:,0,0], xs[:,0,1])
    axes[0,0].set_xlim(-12,12)
    axes[0,0].set_ylim(-12,12)

    axes[1,0].plot(xs[:,0,0], xs[:,0,2])

    axes[1,1].plot(xs[:,0,1], xs[:,0,2])
    axes[0,1].set_visible(False)

    fig.subplots_adjust(hspace=0.05, wspace=0.05)
    plt.show()
Example #2
0
                      .to_frame(heliocentric)

p_hel = particles._X.copy()
s_hel = satellite._X.copy()

p_gc = _hel_to_gc(p_hel)
s_gc = _hel_to_gc(s_hel)

gc = np.vstack((s_gc,p_gc)).copy()
acc = np.zeros_like(gc[:,:3])

times = []
for ii in range(10):
    a = time.time()
    integrator = LeapfrogIntegrator(potential._acceleration_at,
                                    np.array(gc[:,:3]), np.array(gc[:,3:]),
                                    args=(gc.shape[0], acc))

    t, rs, vs = integrator.run(t1=6200, t2=0, dt=-1)
    times.append(time.time()-a)

print(np.min(times), "seconds per integration")

times = []
for ii in range(10):
    a = time.time()
    back_integration_likelihood(6200, 0, -1, potential, p_gc, s_gc,
                                2.5e8, 0.01, particles.tub, 1.5,
                                np.array([-1]*nparticles))
    times.append(time.time()-a)
Example #3
0
def q_p(**kwargs):

    filename = os.path.join(plot_path, "q_p.pdf")
    fig,axes = plt.subplots(2,4,figsize=(14,7.5),
                            sharex=True, sharey=True)

    bins = np.linspace(0.,10,40)
    nparticles = 5000
    for kk,_m in enumerate(range(6,9+1)):
        mass = "2.5e{}".format(_m)
        m = float(mass)
        print(mass)

        sgr = SgrSimulation(sgr_path.format(_m), snapfile)
        p = sgr.particles(n=nparticles, expr="(tub!=0)")#" & (tub<400)")
        tub = p.tub
        s = sgr.satellite()

        potential = LawMajewski2010()

        X = np.vstack((s._X[...,:3], p._X[...,:3].copy()))
        V = np.vstack((s._X[...,3:], p._X[...,3:].copy()))
        integrator = LeapfrogIntegrator(potential._acceleration_at,
                                        np.array(X), np.array(V),
                                        args=(X.shape[0], np.zeros_like(X)))
        ts, rs, vs = integrator.run(t1=sgr.t1, t2=sgr.t2, dt=-1.)

        s_orbit = np.vstack((rs[:,0][:,np.newaxis].T, vs[:,0][:,np.newaxis].T)).T
        p_orbits = np.vstack((rs[:,1:].T, vs[:,1:].T)).T
        t_idx = np.array([np.argmin(np.fabs(ts - t)) for t in p.tub])

        p_x = np.array([p_orbits[jj,ii] for ii,jj in enumerate(t_idx)])
        s_x = np.array([s_orbit[jj,0] for jj in t_idx])

        #############################################
        # determine tail_bit
        diff = p_x-s_x
        norm_r = s_x[:,:3] / np.sqrt(np.sum(s_x[:,:3]**2, axis=-1))[:,np.newaxis]
        norm_diff_r = diff[:,:3] / np.sqrt(np.sum(diff[:,:3]**2, axis=-1))[:,np.newaxis]
        dot_prod_r = np.sum(norm_diff_r*norm_r, axis=-1)
        tail_bit = (dot_prod_r > 0.).astype(int)*2 - 1
        #############################################

        r_tide = potential._tidal_radius(m, s_orbit[...,:3])#*0.69336
        s_R_orbit = np.sqrt(np.sum(s_orbit[...,:3]**2, axis=-1))
        a_pm = (s_R_orbit + r_tide*tail_bit) / s_R_orbit
        q = np.sqrt(np.sum((p_x[:,:3] - s_x[:,:3])**2,axis=-1))

        f = r_tide / s_R_orbit
        s_V = np.sqrt(np.sum(s_orbit[...,3:]**2, axis=-1))
        vdisp = s_V * f / 1.4
        p = np.sqrt(np.sum((p_x[:,3:] - s_x[...,3:])**2,axis=-1))

        fig,axes = plt.subplots(2,1,figsize=(10,6),sharex=True)

        axes[0].plot(tub, q, marker='.', alpha=0.5, color='#666666')
        axes[0].plot(ts, r_tide*1.4, linewidth=2., alpha=0.8, color='k',
                     linestyle='-', marker=None)
        axes[0].set_ylim(0., max(r_tide)*4)

        axes[1].plot(tub, (p*u.kpc/u.Myr).to(u.km/u.s).value,
                     marker='.', alpha=0.5, color='#666666')
        axes[1].plot(ts, (vdisp*u.kpc/u.Myr).to(u.km/u.s).value, color='k',
                     linewidth=2., alpha=0.75, linestyle='-', marker=None)

        M_enc = potential._enclosed_mass(s_R_orbit)
        #delta_E = 4/3.*G.decompose(usys).value**2*m*(M_enc / s_V)**2*r_tide**2/s_R_orbit**4
        delta_v2 = 4/3.*G.decompose(usys).value**2*(M_enc / s_V)**2*\
                        np.mean(r_tide**2)/s_R_orbit**4
        delta_v = (np.sqrt(2*delta_v2)*u.kpc/u.Myr).to(u.km/u.s).value

        axes[1].plot(ts, delta_v, linewidth=2., color='#2166AC',
                     alpha=0.75, linestyle='--', marker=None)

        axes[1].set_ylim(0., max((vdisp*u.kpc/u.Myr).to(u.km/u.s).value)*4)

        axes[0].set_xlim(min(ts), max(ts))

        fig.savefig(os.path.join(plot_path, "q_p_{}.png".format(mass)),
                    transparent=True)
Example #4
0
                      .to_frame(heliocentric)

p_hel = particles._X.copy()
s_hel = satellite._X.copy()

p_gc = _hel_to_gc(p_hel)
s_gc = _hel_to_gc(s_hel)

gc = np.vstack((s_gc, p_gc)).copy()
acc = np.zeros_like(gc[:, :3])

times = []
for ii in range(10):
    a = time.time()
    integrator = LeapfrogIntegrator(potential._acceleration_at,
                                    np.array(gc[:, :3]),
                                    np.array(gc[:, 3:]),
                                    args=(gc.shape[0], acc))

    t, rs, vs = integrator.run(t1=6200, t2=0, dt=-1)
    times.append(time.time() - a)

print(np.min(times), "seconds per integration")

times = []
for ii in range(10):
    a = time.time()
    back_integration_likelihood(6200, 0, -1, potential, p_gc, s_gc, 2.5e8,
                                0.01, particles.tub, 1.5,
                                np.array([-1] * nparticles))
    times.append(time.time() - a)
Example #5
0
def total_rv():

    filenamer = os.path.join(plot_path, "rel_r.png")
    filenamev = os.path.join(plot_path, "rel_v.png")

    figr,axesr = plt.subplots(4,1,figsize=(10,14),
                              sharex=True)
    figv,axesv = plt.subplots(4,1,figsize=(10,14),
                              sharex=True)

    nparticles = 2000
    for k,_m in enumerate(range(6,9+1)):
        mass = "2.5e{}".format(_m)
        m = float(mass)
        print(mass)

        sgr = SgrSimulation(sgr_path.format(_m),snapfile)
        p = sgr.particles(n=nparticles, expr=expr)
        s = sgr.satellite()

        X = np.vstack((s._X[...,:3], p._X[...,:3].copy()))
        V = np.vstack((s._X[...,3:], p._X[...,3:].copy()))
        integrator = LeapfrogIntegrator(sgr.potential._acceleration_at,
                                        np.array(X), np.array(V),
                                        args=(X.shape[0], np.zeros_like(X)))
        ts, rs, vs = integrator.run(t1=sgr.t1, t2=sgr.t2, dt=-1.)

        s_orbit = np.vstack((rs[:,0][:,np.newaxis].T, vs[:,0][:,np.newaxis].T)).T
        p_orbits = np.vstack((rs[:,1:].T, vs[:,1:].T)).T
        t_idx = np.array([np.argmin(np.fabs(ts - t)) for t in p.tub])

        m_t = (-s.mdot*ts + s.m0)[:,np.newaxis]
        s_R = np.sqrt(np.sum(s_orbit[...,:3]**2, axis=-1))
        s_V = np.sqrt(np.sum(s_orbit[...,3:]**2, axis=-1))
        r_tide = sgr.potential._tidal_radius(m_t, s_orbit[...,:3])
        v_disp = s_V * r_tide / s_R

        # cartesian basis to project into
        x_hat = s_orbit[...,:3] / np.sqrt(np.sum(s_orbit[...,:3]**2, axis=-1))[...,np.newaxis]
        _y_hat = s_orbit[...,3:] / np.sqrt(np.sum(s_orbit[...,3:]**2, axis=-1))[...,np.newaxis]
        z_hat = np.cross(x_hat, _y_hat)
        y_hat = -np.cross(x_hat, z_hat)

        # translate to satellite position
        rel_orbits = p_orbits - s_orbit
        rel_pos = rel_orbits[...,:3]
        rel_vel = rel_orbits[...,3:]

        # project onto each
        X = np.sum(rel_pos * x_hat, axis=-1)
        Y = np.sum(rel_pos * y_hat, axis=-1)
        Z = np.sum(rel_pos * z_hat, axis=-1)
        RR = np.sqrt(X**2 + Y**2 + Z**2)

        VX = np.sum(rel_vel * x_hat, axis=-1)
        VY = np.sum(rel_vel * y_hat, axis=-1)
        VZ = np.sum(rel_vel * z_hat, axis=-1)
        VV = (np.sqrt(VX**2 + VY**2 + VZ**2)*u.kpc/u.Myr).to(u.km/u.s).value
        v_disp = (v_disp*u.kpc/u.Myr).to(u.km/u.s).value

        _tcross = r_tide / np.sqrt(G.decompose(usys).value*m/r_tide)
        for ii,jj in enumerate(t_idx):
            #tcross = r_tide[jj,0] / _v[jj,ii]
            tcross = _tcross[jj]
            bnd = int(tcross / 2)

            ix1,ix2 = jj-bnd, jj+bnd
            if ix1 < 0: ix1 = 0
            if ix2 > max(sgr.t1,sgr.t2): ix2 = -1
            axesr[k].plot(ts[ix1:ix2],
                          RR[ix1:ix2,ii],
                          linestyle='-', alpha=0.1, marker=None, color='#555555', zorder=-1)

            axesv[k].plot(ts[ix1:ix2],
                          VV[ix1:ix2,ii],
                          linestyle='-', alpha=0.1, marker=None, color='#555555', zorder=-1)

        axesr[k].plot(ts, r_tide*2., marker=None)

        axesr[k].set_xlim(ts.min(), ts.max())
        axesv[k].set_xlim(ts.min(), ts.max())

        axesr[k].set_ylim(0,max(r_tide)*7)
        axesv[k].set_ylim(0,max(v_disp)*7)

        # axes[1,k].set_xlabel(r"$x_1$")

        # if k == 0:
        #     axes[0,k].set_ylabel(r"$x_2$")
        #     axes[1,k].set_ylabel(r"$x_3$")

        axesr[k].text(3000, max(r_tide)*5, r"$2.5\times10^{}M_\odot$".format(_m))
        axesv[k].text(3000, max(v_disp)*5, r"$2.5\times10^{}M_\odot$".format(_m))

    axesr[-1].set_xlabel("time [Myr]")
    axesv[-1].set_xlabel("time [Myr]")

    figr.suptitle("Relative distance", fontsize=26)
    figr.tight_layout()
    figr.subplots_adjust(top=0.92, hspace=0.025, wspace=0.1)
    figr.savefig(filenamer)

    figv.suptitle("Relative velocity", fontsize=26)
    figv.tight_layout()
    figv.subplots_adjust(top=0.92, hspace=0.025, wspace=0.1)
    figv.savefig(filenamev)
Example #6
0
mass = float(_m)
# simulation = io.SgrSimulation(mass=_m)
# particles = simulation.particles(N=1000, expr="tub!=0")
# satellite = simulation.satellite()\
#                       .to_frame(heliocentric)

# s_hel = satellite._X.copy()
# s_gc = _hel_to_gc(s_hel)
s_gc = np.array([[8.363919011, 0.243352771, 16.864546659,
                  -0.04468993, -0.12392801, -0.01664498]]) # Pal5
s_hel = _gc_to_hel(s_gc)

# First integrate the orbit of the satellite back to get initial conditions
acc = np.zeros_like(s_gc[:,:3])
integrator = LeapfrogIntegrator(potential._acceleration_at,
                                np.array(s_gc[:,:3]), np.array(s_gc[:,3:]),
                                args=(s_gc.shape[0], acc))

t, rs, vs = integrator.run(t1=T, t2=0, dt=-dt)
init_r,init_v = rs[-1], vs[-1]

# integrate the orbit of the satellite
acc = np.zeros_like(s_gc[:,:3])
integrator = LeapfrogIntegrator(potential._acceleration_at,
                                init_r, init_v,
                                args=(1, acc))

t, rs, vs = integrator.run(t1=0, t2=T, dt=dt)

satellite_orbit = np.vstack((rs.T,vs.T)).T
Example #7
0
def fig3(**kwargs):
    """ Plot the PSD for 10 stars vs. back-integration time. """

    seed = int(kwargs.get("seed", 999))
    nPlot= 10
    nIntegrate = 1000
    dt = -1.

    # Read in the LM10 data
    np.random.seed(seed)
    lm10 = io.LM10Simulation()
    particles = lm10.particles(N=nIntegrate,
                               expr="(Pcol>-1) & (abs(Lmflag)==1) & (Pcol < 8)")
    satellite = lm10.satellite()

    # array of starting 6d positions
    gc = np.vstack((satellite._X,particles._X)).copy()
    acc = np.zeros_like(gc[:,:3])

    true_potential = LawMajewski2010()
    true_params = dict([(k,v.truth) for k,v in true_potential.parameters.items()])

    wrong_params = true_params.copy()
    wrong_params['v_halo'] = 1.25*wrong_params['v_halo']
    wrong_potential = LawMajewski2010(**wrong_params)

    sat_R = list()
    D_pses = list()
    ts = list()
    for potential in [true_potential, wrong_potential]:
        integrator = LeapfrogIntegrator(potential._acceleration_at,
                                        np.array(gc[:,:3]), np.array(gc[:,3:]),
                                        args=(gc.shape[0], acc))
        times, rs, vs = integrator.run(t1=lm10.t1, t2=lm10.t2, dt=dt)

        s_orbit = np.vstack((rs[:,0][:,np.newaxis].T, vs[:,0][:,np.newaxis].T)).T
        p_orbits = np.vstack((rs[:,1:].T, vs[:,1:].T)).T

        sat_var = np.zeros((len(times),6))
        sat_var[:,:3] = potential._tidal_radius(2.5e8, s_orbit[...,:3])*1.26
        sat_var[:,3:] += 0.02179966
        cov = (sat_var**2)[:,np.newaxis]

        D_ps = np.sqrt(np.sum((p_orbits - s_orbit)**2 / cov, axis=-1))
        D_pses.append(D_ps)

        sat_R.append(np.sqrt(np.sum(s_orbit[:,0,:3]**2, axis=-1)))
        ts.append(times)

    rcparams = {'xtick.major.size' : 16,
                'xtick.major.width' : 1.5}
    with rc_context(rc=rcparams):
        fig = plt.figure(figsize=(12.5,7))
        gs = GridSpec(2,4)

        axes = [plt.subplot(gs[0,:3]), plt.subplot(gs[1,:3]),
                plt.subplot(gs[0,3]), plt.subplot(gs[1,3])]
        axes[0].axhline(1.4, linestyle='--', color='#444444', linewidth=2.)
        axes[1].axhline(1.4, linestyle='--', color='#444444', linewidth=2.)

        for ii in range(nPlot):
            for jj in range(2):
                d = D_pses[jj][:,ii]
                sR = sat_R[jj]
                axes[jj].semilogy(ts[jj]/1000, d, alpha=0.4, color=sgr_color, linewidth=2.)
                axes[jj].semilogy(ts[jj][np.argmin(d)]/1000, np.min(d), marker='|',
                                  markeredgewidth=4, markeredgecolor='k', color='k',
                                  alpha=0.9, markersize=25)

        axes[0].set_ylim(0.6,20)
        axes[0].set_xlim(-6.1, 0.)
        axes[1].set_ylim(axes[0].get_ylim())
        axes[1].set_xlim(axes[0].get_xlim())
        axes[0].tick_params(axis='y', which='both', length=0., labelleft='off')
        axes[1].tick_params(axis='y', which='both', length=0., labelleft='off')

        # vertical histograms of D_ps values
        ylim = axes[0].get_ylim()
        bins = np.logspace(np.log10(ylim[0]), np.log10(ylim[1]), 50)
        n,xx,patches = axes[2].hist(np.min(D_pses[0], axis=0), bins=bins,
                                    orientation='horizontal', histtype='step',
                                    linewidth=2., fill=True, facecolor='w', edgecolor='k')
        n,xx,patches = axes[3].hist(np.min(D_pses[1], axis=0), bins=bins,
                                    orientation='horizontal', histtype='step',
                                    linewidth=2., fill=True, facecolor='w', edgecolor='k')
        axes[2].set_yscale('log')
        axes[3].set_yscale('log')
        axes[2].axis('off')
        axes[3].axis('off')

        axes[2].set_ylim(axes[0].get_ylim())
        axes[3].set_ylim(axes[1].get_ylim())
        axes[3].set_ylim(top=axes[1].get_ylim()[1]*1.02)
        axes[2].set_xlim(right=1.05*axes[2].get_xlim()[1])
        axes[3].set_xlim(right=1.05*axes[3].get_xlim()[1])

        axes[1].xaxis.tick_bottom()
        axes[1].set_xticklabels([])
        axes[0].xaxis.set_visible(False)

    fig.subplots_adjust(hspace=0.02, wspace=0., top=0.98, bottom=0.02, left=0.02, right=0.98)
    fig.savefig(os.path.join(plot_path, "fig3.pdf"))
Example #8
0
def q_p(**kwargs):

    filename = os.path.join(plot_path, "q_p.pdf")
    fig, axes = plt.subplots(2, 4, figsize=(14, 7.5), sharex=True, sharey=True)

    bins = np.linspace(0., 10, 40)
    nparticles = 5000
    for kk, _m in enumerate(range(6, 9 + 1)):
        mass = "2.5e{}".format(_m)
        m = float(mass)
        print(mass)

        sgr = SgrSimulation(sgr_path.format(_m), snapfile)
        p = sgr.particles(n=nparticles, expr="(tub!=0)")  #" & (tub<400)")
        tub = p.tub
        s = sgr.satellite()

        potential = LawMajewski2010()

        X = np.vstack((s._X[..., :3], p._X[..., :3].copy()))
        V = np.vstack((s._X[..., 3:], p._X[..., 3:].copy()))
        integrator = LeapfrogIntegrator(potential._acceleration_at,
                                        np.array(X),
                                        np.array(V),
                                        args=(X.shape[0], np.zeros_like(X)))
        ts, rs, vs = integrator.run(t1=sgr.t1, t2=sgr.t2, dt=-1.)

        s_orbit = np.vstack(
            (rs[:, 0][:, np.newaxis].T, vs[:, 0][:, np.newaxis].T)).T
        p_orbits = np.vstack((rs[:, 1:].T, vs[:, 1:].T)).T
        t_idx = np.array([np.argmin(np.fabs(ts - t)) for t in p.tub])

        p_x = np.array([p_orbits[jj, ii] for ii, jj in enumerate(t_idx)])
        s_x = np.array([s_orbit[jj, 0] for jj in t_idx])

        #############################################
        # determine tail_bit
        diff = p_x - s_x
        norm_r = s_x[:, :3] / np.sqrt(np.sum(s_x[:, :3]**2,
                                             axis=-1))[:, np.newaxis]
        norm_diff_r = diff[:, :3] / np.sqrt(np.sum(diff[:, :3]**2,
                                                   axis=-1))[:, np.newaxis]
        dot_prod_r = np.sum(norm_diff_r * norm_r, axis=-1)
        tail_bit = (dot_prod_r > 0.).astype(int) * 2 - 1
        #############################################

        r_tide = potential._tidal_radius(m, s_orbit[..., :3])  #*0.69336
        s_R_orbit = np.sqrt(np.sum(s_orbit[..., :3]**2, axis=-1))
        a_pm = (s_R_orbit + r_tide * tail_bit) / s_R_orbit
        q = np.sqrt(np.sum((p_x[:, :3] - s_x[:, :3])**2, axis=-1))

        f = r_tide / s_R_orbit
        s_V = np.sqrt(np.sum(s_orbit[..., 3:]**2, axis=-1))
        vdisp = s_V * f / 1.4
        p = np.sqrt(np.sum((p_x[:, 3:] - s_x[..., 3:])**2, axis=-1))

        fig, axes = plt.subplots(2, 1, figsize=(10, 6), sharex=True)

        axes[0].plot(tub, q, marker='.', alpha=0.5, color='#666666')
        axes[0].plot(ts,
                     r_tide * 1.4,
                     linewidth=2.,
                     alpha=0.8,
                     color='k',
                     linestyle='-',
                     marker=None)
        axes[0].set_ylim(0., max(r_tide) * 4)

        axes[1].plot(tub, (p * u.kpc / u.Myr).to(u.km / u.s).value,
                     marker='.',
                     alpha=0.5,
                     color='#666666')
        axes[1].plot(ts, (vdisp * u.kpc / u.Myr).to(u.km / u.s).value,
                     color='k',
                     linewidth=2.,
                     alpha=0.75,
                     linestyle='-',
                     marker=None)

        M_enc = potential._enclosed_mass(s_R_orbit)
        #delta_E = 4/3.*G.decompose(usys).value**2*m*(M_enc / s_V)**2*r_tide**2/s_R_orbit**4
        delta_v2 = 4/3.*G.decompose(usys).value**2*(M_enc / s_V)**2*\
                        np.mean(r_tide**2)/s_R_orbit**4
        delta_v = (np.sqrt(2 * delta_v2) * u.kpc / u.Myr).to(u.km / u.s).value

        axes[1].plot(ts,
                     delta_v,
                     linewidth=2.,
                     color='#2166AC',
                     alpha=0.75,
                     linestyle='--',
                     marker=None)

        axes[1].set_ylim(0.,
                         max((vdisp * u.kpc / u.Myr).to(u.km / u.s).value) * 4)

        axes[0].set_xlim(min(ts), max(ts))

        fig.savefig(os.path.join(plot_path, "q_p_{}.png".format(mass)),
                    transparent=True)
Example #9
0
# particles = simulation.particles(N=1000, expr="tub!=0")
# satellite = simulation.satellite()\
#                       .to_frame(heliocentric)

# s_hel = satellite._X.copy()
# s_gc = _hel_to_gc(s_hel)
s_gc = np.array([[
    8.363919011, 0.243352771, 16.864546659, -0.04468993, -0.12392801,
    -0.01664498
]])  # Pal5
s_hel = _gc_to_hel(s_gc)

# First integrate the orbit of the satellite back to get initial conditions
acc = np.zeros_like(s_gc[:, :3])
integrator = LeapfrogIntegrator(potential._acceleration_at,
                                np.array(s_gc[:, :3]),
                                np.array(s_gc[:, 3:]),
                                args=(s_gc.shape[0], acc))

t, rs, vs = integrator.run(t1=T, t2=0, dt=-dt)
init_r, init_v = rs[-1], vs[-1]

# integrate the orbit of the satellite
acc = np.zeros_like(s_gc[:, :3])
integrator = LeapfrogIntegrator(potential._acceleration_at,
                                init_r,
                                init_v,
                                args=(1, acc))

t, rs, vs = integrator.run(t1=0, t2=T, dt=dt)

satellite_orbit = np.vstack((rs.T, vs.T)).T