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)
def Lpts(): np.random.seed(42) potential = LawMajewski2010() filename = os.path.join(plot_path, "Lpts_r.{}".format(ext)) filename2 = os.path.join(plot_path, "Lpts_v.{}".format(ext)) fig,axes = plt.subplots(2,4,figsize=grid_figsize, sharex=True, sharey=True) fig2,axes2 = plt.subplots(2,4,figsize=grid_figsize, sharex=True, sharey=True) bins = np.linspace(-3,3,50) 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() dt = -1. coord, r_tide, v_disp = particles_x1x2x3(p, s, sgr.potential, sgr.t1, sgr.t2, dt, at_tub=False) (x1,x2,x3,vx1,vx2,vx3) = coord ts = np.arange(sgr.t1,sgr.t2+dt,dt) t_idx = np.array([np.argmin(np.fabs(ts - t)) for t in p.tub]) _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 axes[0,k].set_rasterization_zorder(1) axes[0,k].plot(x1[jj-bnd:jj+bnd,ii]/r_tide[jj-bnd:jj+bnd,0], x2[jj-bnd:jj+bnd,ii]/r_tide[jj-bnd:jj+bnd,0], linestyle='-', alpha=0.1, marker=None, color='#555555', zorder=-1) axes[1,k].set_rasterization_zorder(1) axes[1,k].plot(x1[jj-bnd:jj+bnd,ii]/r_tide[jj-bnd:jj+bnd,0], x3[jj-bnd:jj+bnd,ii]/r_tide[jj-bnd:jj+bnd,0], linestyle='-', alpha=0.1, marker=None, color='#555555', zorder=-1) circ = Circle((0,0), radius=1., fill=False, alpha=0.75, edgecolor='k', linestyle='solid') axes[0,k].add_patch(circ) circ = Circle((0,0), radius=1., fill=False, alpha=0.75, edgecolor='k', linestyle='solid') axes[1,k].add_patch(circ) axes[0,k].axhline(0., color='k', alpha=0.75) axes[1,k].axhline(0., color='k', alpha=0.75) axes[0,k].set_xlim(-5,5) axes[0,k].set_ylim(axes[0,k].get_xlim()) axes[1,k].set_xlabel(r"$x_1/r_{\rm tide}$") if k == 0: axes[0,k].set_ylabel(r"$x_2/r_{\rm tide}$") axes[1,k].set_ylabel(r"$x_3/r_{\rm tide}$") _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 axes2[0,k].set_rasterization_zorder(1) axes2[0,k].plot(vx1[jj-bnd:jj+bnd,ii]/v_disp[jj-bnd:jj+bnd,0], vx2[jj-bnd:jj+bnd,ii]/v_disp[jj-bnd:jj+bnd,0], linestyle='-', alpha=0.1, marker=None, color='#555555', zorder=-1) axes2[1,k].set_rasterization_zorder(1) axes2[1,k].plot(vx1[jj-bnd:jj+bnd,ii]/v_disp[jj-bnd:jj+bnd,0], vx3[jj-bnd:jj+bnd,ii]/v_disp[jj-bnd:jj+bnd,0], linestyle='-', alpha=0.1, marker=None, color='#555555', zorder=-1) circ = Circle((0,0), radius=1., fill=False, alpha=0.75, edgecolor='k', linestyle='solid') axes2[0,k].add_patch(circ) circ = Circle((0,0), radius=1., fill=False, alpha=0.75, edgecolor='k', linestyle='solid') axes2[1,k].add_patch(circ) axes2[0,k].axhline(0., color='k', alpha=0.75) axes2[1,k].axhline(0., color='k', alpha=0.75) axes2[1,k].set_xlim(-5,5) axes2[1,k].set_ylim(axes2[1,k].get_xlim()) axes2[1,k].set_xlabel(r"$v_{x_1}/\sigma_v$") if k == 0: axes2[0,k].set_ylabel(r"$v_{x_2}/\sigma_v$") axes2[1,k].set_ylabel(r"$v_{x_3}/\sigma_v$") axes[0,k].text(0.5, 1.05, r"$2.5\times10^{}M_\odot$".format(_m), horizontalalignment='center', fontsize=24, transform=axes[0,k].transAxes) axes2[0,k].text(0.5, 1.05, r"$2.5\times10^{}M_\odot$".format(_m), horizontalalignment='center', fontsize=24, transform=axes2[0,k].transAxes) fig.tight_layout() fig.subplots_adjust(top=0.92, hspace=0.025, wspace=0.1) fig.savefig(filename) fig2.tight_layout() fig2.subplots_adjust(top=0.92, hspace=0.025, wspace=0.1) fig2.savefig(filename2)
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)
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)