def test_orbmethods(): from galpy.orbit import Orbit from galpy.potential import MWPotential2014 # 8/17/2019: added explicit z=0.025, because that was the default at the # time of the galpy paper, but the default has been changed o = Orbit([0.8, 0.3, 0.75, 0., 0.2, 0.], zo=0.025) # setup R,vR,vT,z,vz,phi times = numpy.linspace(0., 10., 1001) # Output times o.integrate(times, MWPotential2014) # Integrate o.E() # Energy assert numpy.fabs(o.E() + 1.2547650648697966 ) < 10.**-5., 'Orbit method does not work as expected' o.L() # Angular momentum assert numpy.all( numpy.fabs(o.L() - numpy.array([[0., -0.16, 0.6]])) < 10.**-5. ), 'Orbit method does not work as expected' o.Jacobi(OmegaP=0.65) #Jacobi integral E-OmegaP Lz assert numpy.fabs(o.Jacobi(OmegaP=0.65) - numpy.array([-1.64476506]) ) < 10.**-5., 'Orbit method does not work as expected' o.ER(times[-1]), o.Ez(times[-1]) # Rad. and vert. E at end assert numpy.fabs(o.ER(times[-1]) + 1.27601734263047 ) < 10.**-5., 'Orbit method does not work as expected' assert numpy.fabs(o.Ez(times[-1]) - 0.021252201847851909 ) < 10.**-5., 'Orbit method does not work as expected' o.rperi(), o.rap(), o.zmax() # Peri-/apocenter r, max. |z| assert numpy.fabs(o.rperi() - 0.44231993168097 ) < 10.**-5., 'Orbit method does not work as expected' assert numpy.fabs(o.rap() - 0.87769030382105 ) < 10.**-5., 'Orbit method does not work as expected' assert numpy.fabs(o.zmax() - 0.077452357289016 ) < 10.**-5., 'Orbit method does not work as expected' o.e() # eccentricity (rap-rperi)/(rap+rperi) assert numpy.fabs(o.e() - 0.32982348199330563 ) < 10.**-5., 'Orbit method does not work as expected' o.R(2., ro=8.) # Cylindrical radius at time 2. in kpc assert numpy.fabs(o.R(2., ro=8.) - 3.5470772876920007 ) < 10.**-3., 'Orbit method does not work as expected' o.vR(5., vo=220.) # Cyl. rad. velocity at time 5. in km/s assert numpy.fabs(o.vR(5., vo=220.) - 45.202530965094553 ) < 10.**-3., 'Orbit method does not work as expected' o.ra(1.), o.dec(1.) # RA and Dec at t=1. (default settings) # 5/12/2016: test weakened, because improved galcen<->heliocen # transformation has changed these, but still close assert numpy.fabs(o.ra(1.) - numpy.array([288.19277]) ) < 10.**-1., 'Orbit method does not work as expected' assert numpy.fabs(o.dec(1.) - numpy.array([18.98069155]) ) < 10.**-1., 'Orbit method does not work as expected' o.jr(type='adiabatic'), o.jz() # R/z actions (ad. approx.) assert numpy.fabs(o.jr(type='adiabatic') - 0.05285302231137586 ) < 10.**-3., 'Orbit method does not work as expected' assert numpy.fabs(o.jz() - 0.006637988850751242 ) < 10.**-3., 'Orbit method does not work as expected' # Rad. period w/ Staeckel approximation w/ focal length 0.5, o.Tr(type='staeckel', delta=0.5, ro=8., vo=220.) # in Gyr assert numpy.fabs( o.Tr(type='staeckel', delta=0.5, ro=8., vo=220.) - 0.1039467864018446 ) < 10.**-3., 'Orbit method does not work as expected' o.plot(d1='R', d2='z') # Plot the orbit in (R,z) o.plot3d() # Plot the orbit in 3D, w/ default [x,y,z] return None
def test_energy_momentum_of_sun(): o = Orbit() # create sun's orbit o.turn_physical_off() ts = np.linspace(0, 1000, 100000) o.integrate(ts, MWPotential2014) print('galpy energy = ', o.E()) coord = np.array([o.x(), o.y(), o.z(), o.vx(), o.vy(), o.vz()]) print('my energy = ', Energy(coord)) print(o.E() == Energy(coord)) print('galpy momentum = ', o.L()[0][2]) print('my momentum = ', L_z(coord)) print(o.L()[0][2] == L_z(coord))
def test_adinvariance(): from galpy.potential import IsochronePotential from galpy.orbit import Orbit from galpy.actionAngle import actionAngleIsochrone # Initialize two different IsochronePotentials ip1= IsochronePotential(normalize=1.,b=1.) ip2= IsochronePotential(normalize=0.5,b=1.) # Use TimeInterpPotential to interpolate smoothly tip= TimeInterpPotential(ip1,ip2,dt=100.,tform=50.) # Integrate: 1) Orbit in the first isochrone potential o1= Orbit([1.,0.1,1.1,0.0,0.1,0.]) ts= numpy.linspace(0.,50.,1001) o1.integrate(ts,tip) o1.plot(d1='x',d2='y',xrange=[-1.6,1.6],yrange=[-1.6,1.6], color='b') # 2) Orbit in the transition o2= o1(ts[-1]) # Last time step => initial time step ts2= numpy.linspace(50.,150.,1001) o2.integrate(ts2,tip) o2.plot(d1='x',d2='y',overplot=True,color='g') # 3) Orbit in the second isochrone potential o3= o2(ts2[-1]) ts3= numpy.linspace(150.,200.,1001) o3.integrate(ts3,tip) o3.plot(d1='x',d2='y',overplot=True,color='r') # Now we calculate energy, maximum height, and mean radius print(o1.E(pot=ip1), (o1.rperi()+o1.rap())/2, o1.zmax()) assert numpy.fabs(o1.E(pot=ip1)+2.79921356237) < 10.**-4., 'Energy in the adiabatic invariance test is different' assert numpy.fabs((o1.rperi()+o1.rap())/2-1.07854158141) < 10.**-4., 'mean radius in the adiabatic invariance test is different' assert numpy.fabs(o1.zmax()-0.106331362938) < 10.**-4., 'zmax in the adiabatic invariance test is different' print(o3.E(pot=ip2), (o3.rperi()+o3.rap())/2, o3.zmax()) assert numpy.fabs(o3.E(pot=ip2)+1.19677002624) < 10.**-4., 'Energy in the adiabatic invariance test is different' assert numpy.fabs((o3.rperi()+o3.rap())/2-1.39962036137) < 10.**-4., 'mean radius in the adiabatic invariance test is different' assert numpy.fabs(o3.zmax()-0.138364269321) < 10.**-4., 'zmax in the adiabatic invariance test is different' # The orbit has clearly moved to larger radii, # the actions are however conserved from beginning to end aAI1= actionAngleIsochrone(ip=ip1); print(aAI1(o1)) js= aAI1(o1) assert numpy.fabs(js[0]-numpy.array([ 0.00773779])) < 10.**-4., 'action in the adiabatic invariance test is different' assert numpy.fabs(js[1]-numpy.array([ 1.1])) < 10.**-4., 'action in the adiabatic invariance test is different' assert numpy.fabs(js[2]-numpy.array([ 0.0045361])) < 10.**-4., 'action in the adiabatic invariance test is different' aAI2= actionAngleIsochrone(ip=ip2); print(aAI2(o3)) js= aAI2(o3) assert numpy.fabs(js[0]-numpy.array([ 0.00773812])) < 10.**-4., 'action in the adiabatic invariance test is different' assert numpy.fabs(js[1]-numpy.array([ 1.1])) < 10.**-4., 'action in the adiabatic invariance test is different' assert numpy.fabs(js[2]-numpy.array([ 0.0045361])) < 10.**-4., 'action in the adiabatic invariance test is different' return None
def test_v_esc(self): R = z = phi = 1 ve = v_esc(MWPotential2014, [R, z, phi]) orb = Orbit(vxvv=[R/_pc, 0, ve/_kms, z/_pc, 0, phi]) E = orb.E(pot=MWPotential2014) E_inf = evaluatePotentials(MWPotential2014, 1e+12, 0) self.assertAlmostEqual(E, E_inf, 10, msg="v_esc returns incorrect " "escape velocity")
now_r_omega = o.R(ts)[-1] #kpc now_phi_omega = o.phi(ts)[-1] nowvr_om = o.vR(ts)[-1] nowvt_om = o.vT(ts)[-1] nowvz_om = o.vz(ts)[-1] #finding velocty components of omega cen now: now_arrayvr_omega = forward_omega_orbit.vR(-ts) now_arrayvt_omega = forward_omega_orbit.vT(-ts) now_arrayvz_omega = forward_omega_orbit.vz(-ts) now_vr_omega = forward_omega_orbit.vR(-ts)[-1] now_vt_omega = forward_omega_orbit.vT(-ts)[-1] now_vz_omega = forward_omega_orbit.vz(-ts)[-1] #finding energies of omega cen now: now_arraye_omega = forward_omega_orbit.E(-ts) now_arrayer_omega = forward_omega_orbit.ER(-ts) now_arrayez_omega = forward_omega_orbit.Ez(-ts) now_e_omega = now_arraye_omega[-1] now_er_omega = now_arrayer_omega[-1] now_ez_omega = now_arrayez_omega[-1] #print("current omega cen location according to code:", now_r_omega,now_vr_omega,now_vt_omega,now_z_omega,now_vz_omega,now_phi_omega) num_stars = 1000 vrom = o.vR() mu, sigma = (0, 100 / (np.sqrt(3))) sr = np.random.normal(mu, sigma, num_stars) vr_omega_array_vr = np.ones(num_stars) * (now_arrayvr_omega[0]) vt_omega_array_vt = np.ones(num_stars) * (now_arrayvt_omega[0])
pmra_mc[dist_mc < 0] = 0.0 vhel_mc[dist_mc < 0] = 0.0 pmdec_mc[dist_mc < 0] = 0.0 dist_mc[dist_mc < 0] = 0.0 for j in range(nmc): op= Orbit(vxvv=[ra[i],dec[i],dist_mc[j],pmra_mc[j],pmdec_mc[j],vhel_mc[j]],\ radec=True,uvw=False,ro=8.0, vo=220.0,zo=zsun, \ solarmotion=[-usun,vsun,wsun]) op.integrate(ts, MWPotential2014) rap_mc[j] = op.rap() rperi_mc[j] = op.rperi() rmean_mc[j] = 0.5 * (rap_mc[j] + rperi_mc[j]) zmax_mc[j] = op.zmax() ecc_mc[j] = op.e() ene_mc[j] = op.E() lz_mc[j] = op.L(0.0)[0][2] vphi_mc[j] = op.vphi(0.0) vrad_mc[j] = op.vR(0.0) # taking mean and std rap[i] = rap_mc.mean() rap_err[i] = rap_mc.std() rperi[i] = rperi_mc.mean() rperi_err[i] = rperi_mc.std() rmean[i] = rmean_mc.mean() rmean_err[i] = rmean_mc.std() zmax[i] = zmax_mc.mean() zmax_err[i] = zmax_mc.std() ecc[i] = ecc_mc.mean() ecc_err[i] = ecc_mc.std() ene[i] = ene_mc.mean()
#This is for observational purposes and to test the code and see stars against one another #star1 star1 = Orbit([(r_omega)*units.kpc, (vr_omega+10)*units.km/units.s, vt_omega*units.km/units.s, z_omega*units.kpc,vz_omega*units.km/units.s, phi_omega*units.deg], ro=8., vo=220., solarmotion='hogg') star1.integrate(-ts,MWPotential2014) #[R,vR,vT(,z,vz,phi)] #finding location of star 1 now: now_arrayz_star1 = star1.z(-ts) now_arrayR_star1 = star1.R(-ts) now_arrayphi_star1 = star1.phi(-ts) #finding velocty components of star 1 now: now_arrayvr_star1 = star1.vR(-ts) now_arrayvt_star1 = star1.vT(-ts) now_arrayvz_star1 = star1.vz(-ts) #finding energies of star 1 now: now_arraye_star1 = star1.E(-ts) now_arrayer_star1 = star1.ER(-ts) now_arrayez_star1 = star1.Ez(-ts) #compared with current orbit of omega cen arrayR_omega = forward_omega_orbit.R(-ts) arrayvr_omega = forward_omega_orbit.vR(-ts) arrayvt_omega = forward_omega_orbit.vT(-ts) arrayvz_omega = forward_omega_orbit.vz(-ts) arrayphi_omega = forward_omega_orbit.phi(-ts) arrayz_omega = forward_omega_orbit.z(-ts) rrr = arrayR_omega[-1] vrvr = arrayvr_omega[-1] vtvt = arrayvt_omega[-1] zzz = arrayz_omega[-1] vzvz = arrayvz_omega[-1]
def estimate_orbit_parameters(star_index, orbit_calculation_input): """ Estimate orbit parameters from the given MC sample of 6D information for the Nr of stars and save it into orbit_information """ # We are creating a dictionary for each star star_i = dict() ra = orbit_calculation_input['ra'] *u.deg dec = orbit_calculation_input['dec'] *u.deg dist = orbit_calculation_input['distance'] *u.pc pm_ra = orbit_calculation_input['pmra'] *u.mas/u.year pm_dec = orbit_calculation_input['pmdec'] *u.mas/u.year v_los = orbit_calculation_input['vrad'] *u.km/u.s # Create the Orbit instance o = Orbit( vxvv=[ra,dec,dist,pm_ra, pm_dec,v_los], ro=r_galactic_centre, vo=v_circular, zo=z_galactic_plane, solarmotion=[-11.1, 15.17, 7.25]*u.km/u.s, radec=True ) star_i = dict() #Galactocentric coordinates: star_i['X_XYZ'] = o.helioX()#*u.kpc star_i['Y_XYZ'] = o.helioY()#*u.kpc star_i['Z_XYZ'] = o.helioZ()#*u.kpc star_i['U_UVW'] = o.U()#*u.km/u.s star_i['V_UVW'] = o.V()#*u.km/u.s star_i['W_UVW'] = o.W()#*u.km/u.s star_i['R_Rzphi'] = o.R()#*u.kpc star_i['phi_Rzphi'] = o.phi()#*u.rad star_i['z_Rzphi'] = o.z()#*u.kpc star_i['vR_Rzphi'] = o.vR()#*u.km/u.s star_i['vT_Rzphi'] = o.vT()#*u.km/u.s star_i['vz_Rzphi'] = o.vz()#*u.km/u.s try: star_i['J_R'], star_i['L_Z'],star_i['J_Z'], star_i['omega_R'], star_i['omega_phi'], star_i['omega_z'], star_i['angle_R'], star_i['angle_phi'], star_i['angle_z'] = aAS.actionsFreqsAngles( #R,vR,vT,z,vz[,phi] star_i['R_Rzphi']*u.kpc, star_i['vR_Rzphi']*u.km/u.s, star_i['vT_Rzphi']*u.km/u.s, star_i['z_Rzphi']*u.kpc, star_i['vz_Rzphi']*u.km/u.s, star_i['phi_Rzphi']*u.rad, ro=r_galactic_centre,vo=v_circular ) except: star_i['omega_R'] = [np.nan] star_i['omega_phi'] = [np.nan] star_i['omega_z'] = [np.nan] star_i['angle_R'] = [np.nan] star_i['angle_phi'] = [np.nan] star_i['angle_z'] = [np.nan] try: star_i['J_R'], star_i['L_Z'],star_i['J_Z'] = aAS( #R,vR,vT,z,vz[,phi] star_i['R_Rzphi']*u.kpc, star_i['vR_Rzphi']*u.km/u.s, star_i['vT_Rzphi']*u.km/u.s, star_i['z_Rzphi']*u.kpc, star_i['vz_Rzphi']*u.km/u.s, star_i['phi_Rzphi']*u.rad, ro=r_galactic_centre,vo=v_circular ) except: star_i['J_R'] = [np.nan] star_i['L_Z'] = [np.nan] star_i['J_Z'] = [np.nan] try: star_i['ecc'], star_i['zmax'], star_i['R_peri'], star_i['R_ap'] = aAS.EccZmaxRperiRap( #R,vR,vT,z,vz[,phi] star_i['R_Rzphi']*u.kpc, star_i['vR_Rzphi']*u.km/u.s, star_i['vT_Rzphi']*u.km/u.s, star_i['z_Rzphi']*u.kpc, star_i['vz_Rzphi']*u.km/u.s, star_i['phi_Rzphi']*u.rad, ro=r_galactic_centre,vo=v_circular ) star_i['zmax'] star_i['R_peri'] star_i['R_peri'] except: star_i['ecc'] = [np.nan] star_i['zmax'] = [np.nan] star_i['R_peri'] = [np.nan] star_i['R_ap'] = [np.nan] star_i['Energy'] = o.E(pot=pot,ro=r_galactic_centre,vo=v_circular,zo=z_galactic_plane) return(star_i)
sun['z_Rzphi']*u.kpc, sun['vz_Rzphi']*u.km/u.s, sun['phi_Rzphi']*u.rad, ro=r_galactic_centre,vo=v_circular ) sun['zmax'] sun['R_peri'] sun['R_peri'] except: sun['ecc'] = [np.nan] sun['zmax'] = [np.nan] sun['R_peri'] = [np.nan] sun['R_ap'] = [np.nan] sun['Energy'] = o.E(pot=pot,ro=r_galactic_centre,vo=v_circular,zo=z_galactic_plane) print('Solar values:') print('X,Y,Z: '+"{:.2f}".format(sun['X_XYZ'])+' '+"{:.2f}".format(sun['Y_XYZ'])+' '+"{:.2f}".format(sun['Z_XYZ'])) print('U,V,W: '+"{:.2f}".format(sun['U_UVW'])+' '+"{:.2f}".format(sun['V_UVW'])+' '+"{:.2f}".format(sun['W_UVW'])) print('R,phi,z: '+"{:.2f}".format(sun['R_Rzphi'])+' '+"{:.2f}".format(sun['phi_Rzphi'])+' '+"{:.2f}".format(sun['z_Rzphi'])) print('vR,vT,vz: '+"{:.2f}".format(sun['vR_Rzphi'])+' '+"{:.2f}".format(sun['vT_Rzphi'])+' '+"{:.2f}".format(sun['vz_Rzphi'])) print('J_R,L_Z,J_Z: '+"{:.2f}".format(sun['J_R'][0])+' '+"{:.2f}".format(sun['L_Z'][0])+' '+"{:.2f}".format(sun['J_Z'][0])) print('Omega R/phi/z: '+"{:.2f}".format(sun['omega_R'][0])+' '+"{:.2f}".format(sun['omega_phi'][0])+' '+"{:.2f}".format(sun['omega_z'][0])) print('Angles R/phi/z: '+"{:.2f}".format(sun['angle_R'][0])+' '+"{:.2f}".format(sun['angle_phi'][0])+' '+"{:.2f}".format(sun['angle_z'][0])) print('ecc, zmax, R_peri, R_apo: '+"{:.2f}".format(sun['ecc'][0])+' '+"{:.2f}".format(sun['zmax'][0])+' '+"{:.2f}".format(sun['R_peri'][0])+' '+"{:.2f}".format(sun['R_ap'][0])) print('Energy: '+"{:.2f}".format(sun['Energy'])) # ### Input of 6D information in observable dimensions