def test_elliptical_cold_vt(): # Test that the rotational velocity for the elliptical disk behaves as analytically expected idf = dehnendf(beta=0., profileParams=(1. / 3., 1., 0.0125)) cp = 0.05 pot = [ LogarithmicHaloPotential(normalize=1.), EllipticalDiskPotential(cp=cp, sp=0., p=0., tform=-150., tsteady=125.) ] edf = evolveddiskdf(idf, pot=pot, to=-150.) #Should be 1. mvt, grid = edf.meanvT(0.9, phi=-numpy.pi / 4., integrate_method='rk6_c', grid=True, returnGrid=True, gridpoints=_GRIDPOINTS) assert numpy.fabs( mvt - 1. ) < 10.**-3., 'Cold elliptical disk does not agree with analytical calculation for vt' #Should be 1.-cp mvt, grid = edf.meanvT(0.9, phi=0., integrate_method='rk6_c', grid=True, nsigma=7., returnGrid=True, gridpoints=_GRIDPOINTS) assert numpy.fabs( mvt - 1. + cp ) < 10.**-3., 'Cold elliptical disk does not agree with analytical calculation for vt' return None
def test_elliptical_cold_vertexdev(): # Test that the vertex deviations for the elliptical disk behaves as analytically expected idf = dehnendf(beta=0., profileParams=(1. / 3., 1., 0.0125)) cp = 0.05 pot = [ LogarithmicHaloPotential(normalize=1.), EllipticalDiskPotential(cp=cp, sp=0., p=0., tform=-150., tsteady=125.) ] edf = evolveddiskdf(idf, pot=pot, to=-150.) #Should be -2cp in radians vdev, grid = edf.vertexdev(0.9, phi=-numpy.pi / 4., integrate_method='rk6_c', grid=True, nsigma=7., returnGrid=True, gridpoints=_GRIDPOINTS) assert numpy.fabs( vdev + 2. * cp ) < 10.**-3., 'Cold elliptical disk does not agree with analytical calculation for vertexdev' #Should be 0 vdev, grid = edf.vertexdev(0.9, phi=0., integrate_method='rk6_c', grid=True, nsigma=7., returnGrid=True, gridpoints=_GRIDPOINTS) assert numpy.fabs( vdev ) < 10.**-2. / 180. * numpy.pi, 'Cold elliptical disk does not agree with analytical calculation for vertexdev' return None
def test_mildnonaxi_oortA_grid_tlist(): # Test that for a close to axisymmetric potential, the oortA is close to the value of the initial DF idf = dehnendf(beta=0.) pot = [ LogarithmicHaloPotential(normalize=1.), EllipticalDiskPotential(twophio=0.001) ] #very mild non-axi edf = evolveddiskdf(idf, pot=pot, to=-10.) oa, grid, dgridR, dgridphi=\ edf.oortA(0.9,t=[0.,-2.5,-5.,-7.5,-10.], phi=0.2,integrate_method='rk6_c', grid=True,derivRGrid=True,derivphiGrid=True, returnGrids=True, gridpoints=_GRIDPOINTS,derivGridpoints=_GRIDPOINTS) ioa = idf.oortA(0.9) assert numpy.all( numpy.fabs(oa - ioa) < 0.005 ), 'oortA of evolveddiskdf for axisymmetric potential is not equal to that of initial DF' oa = edf.oortA(0.9, t=[0., -2.5, -5., -7.5, -10.], phi=0.2, integrate_method='rk6_c', grid=grid, derivRGrid=dgridR, derivphiGrid=dgridphi, gridpoints=_GRIDPOINTS, derivGridpoints=_GRIDPOINTS) assert numpy.all( numpy.fabs(oa - ioa) < 0.005 ), 'oortA of evolveddiskdf for axisymmetric potential is not equal to that of initial DF when calculated with pre-computed grid' return None
def test_mildnonaxi_oortK_grid(): # Test that for a close to axisymmetric potential, the oortK is close to zero idf = dehnendf(beta=0.) pot = [ LogarithmicHaloPotential(normalize=1.), EllipticalDiskPotential(twophio=0.001) ] #very mild non-axi edf = evolveddiskdf(idf, pot=pot, to=-10.) ok, grid, dgridR, dgridphi=\ edf.oortK(0.9,phi=0.2,integrate_method='rk6_c', grid=True,derivRGrid=True,derivphiGrid=True, returnGrids=True, gridpoints=_GRIDPOINTS,derivGridpoints=_GRIDPOINTS) assert numpy.fabs( ok ) < 0.005, 'oortK of evolveddiskdf for axisymmetric potential is not equal to that of initial DF' ok = edf.oortK(0.9, phi=0.2, integrate_method='rk6_c', grid=grid, derivRGrid=dgridR, derivphiGrid=dgridphi, gridpoints=_GRIDPOINTS, derivGridpoints=_GRIDPOINTS) assert numpy.fabs( ok ) < 0.005, 'oortK of evolveddiskdf for axisymmetric potential is not equal to that of initial DF when calculated with pre-computed grid' return None
def test_call_marginalizevlos(): from galpy.orbit import Orbit idf = dehnendf(beta=0.) pot = [ LogarithmicHaloPotential(normalize=1.), EllipticalDiskPotential(twophio=0.001) ] #very mild non-axi edf = evolveddiskdf(idf, pot=pot[0], to=-10.) #one with just one potential #l=0 R, phi, vT = 0.8, 0., 0.7 vrs = numpy.linspace(-1., 1., 101) pvrs = numpy.array( [edf(Orbit([R, vr, vT, phi]), integrate_method='rk6_c') for vr in vrs]) assert numpy.fabs(numpy.log(numpy.sum(pvrs)*(vrs[1]-vrs[0]))\ -edf(Orbit([R,0.,vT,phi]),marginalizeVlos=True,integrate_method='rk6_c',log=True)) < 10.**-4., 'diskdf call w/ marginalizeVlos does not work' #l=270, this DF has some issues, but it suffices to test the mechanics of the code edf = evolveddiskdf(idf, pot=pot, to=-10.) R, phi, vR = numpy.sin(numpy.pi / 6.), -numpy.pi / 3., 0.4 #l=30 degree vts = numpy.linspace(0.3, 1.5, 101) pvts = numpy.array( [edf(Orbit([R, vR, vt, phi]), integrate_method='rk6_c') for vt in vts]) assert numpy.fabs(numpy.sum(pvts)*(vts[1]-vts[0])\ -edf(Orbit([R,vR,0.,phi]), marginalizeVlos=True, integrate_method='rk6_c', nsigma=4)) < 10.**-3.5, 'diskdf call w/ marginalizeVlos does not work' return None
def test_call_marginalizevperp(): from galpy.orbit import Orbit idf = dehnendf(beta=0.) pot = [ LogarithmicHaloPotential(normalize=1.), EllipticalDiskPotential(twophio=0.001) ] #very mild non-axi edf = evolveddiskdf(idf, pot=pot[0], to=-10.) #one with just one potential #l=0 R, phi, vR = 0.8, 0., 0.4 vts = numpy.linspace(0., 1.5, 51) pvts = numpy.array( [edf(Orbit([R, vR, vt, phi]), integrate_method='rk6_c') for vt in vts]) assert numpy.fabs(numpy.sum(pvts)*(vts[1]-vts[0])\ -edf(Orbit([R,vR,0.,phi]),marginalizeVperp=True,integrate_method='rk6_c')) < 10.**-3.5, 'evolveddiskdf call w/ marginalizeVperp does not work' #l=270 edf = evolveddiskdf(idf, pot=pot, to=-10.) R, phi, vT = numpy.sin(numpy.pi / 6.), -numpy.pi / 3., 0.7 #l=30 degree vrs = numpy.linspace(-1., 1., 101) pvrs = numpy.array( [edf(Orbit([R, vr, vT, phi]), integrate_method='rk6_c') for vr in vrs]) assert numpy.fabs(numpy.log(numpy.sum(pvrs)*(vrs[1]-vrs[0]))\ -edf(Orbit([R,0.,vT,phi]), marginalizeVperp=True, integrate_method='rk6_c',log=True, nsigma=4)) < 10.**-2.5, 'evolveddiskdf call w/ marginalizeVperp does not work' return None
def test_elliptical_cold_oortABCK_position2(): # Test that the Oort functions A, B, C, and K for the elliptical disk behaves as analytically expected idf= dehnendf(beta=0.,profileParams=(1./3.,1.,0.0125)) cp= 0.05 pot= [LogarithmicHaloPotential(normalize=1.), EllipticalDiskPotential(cp=cp,sp=0.,p=0.,tform=-150.,tsteady=125.)] edf= evolveddiskdf(idf,pot=pot,to=-150.) #Should be 0.5/0.9+cp/2 oorta, grid, gridr, gridp= edf.oortA(0.9,phi=0., integrate_method='rk6_c',grid=True, nsigma=7., derivRGrid=True,derivphiGrid=True, returnGrids=True, gridpoints=51, derivGridpoints=51) assert numpy.fabs(oorta-cp/2.-0.5/0.9) < 10.**-2.2, 'Cold elliptical disk does not agree with analytical calculation for oortA' #Should be -cp/2-0.5/0.9 oortb= edf.oortB(0.9,phi=0., integrate_method='rk6_c',grid=grid,nsigma=7., derivRGrid=gridr,derivphiGrid=gridp) assert numpy.fabs(oortb+cp/2.+0.5/0.9) < 10.**-2.2, 'Cold elliptical disk does not agree with analytical calculation for oortB' #Should be 0 oortc= edf.oortC(0.9,phi=0., integrate_method='rk6_c',grid=grid,nsigma=7., derivRGrid=gridr,derivphiGrid=gridp) assert numpy.fabs(oortc) < 10.**-3., 'Cold elliptical disk does not agree with analytical calculation for oortC' #Should be 0 oortk= edf.oortK(0.9,phi=0., integrate_method='rk6_c',grid=grid,nsigma=7., derivRGrid=gridr,derivphiGrid=gridp) assert numpy.fabs(oortk) < 10.**-3., 'Cold elliptical disk does not agree with analytical calculation for oortK' return None
def test_call_special(): from galpy.orbit import Orbit idf= dehnendf(beta=0.) pot= [LogarithmicHaloPotential(normalize=1.), EllipticalDiskPotential(twophio=0.001)] #very mild non-axi edf= evolveddiskdf(idf,pot=pot,to=-10.) o= Orbit([0.9,0.1,1.1,2.]) #call w/ and w/o explicit t assert numpy.fabs(numpy.log(edf(o,0.))-numpy.log(edf(o))) < 10.**-10., 'edf.__call__ w/ explicit t=0. and w/o t do not give the same answer' #call must get Orbit, otherwise error try: edf(0.9,0.1,1.1,2.) except IOError: pass else: raise AssertionError('edf.__call__ w/o Orbit input did not raise IOError') #Call w/ list, but just to assert numpy.fabs(numpy.log(edf(o,[-10.]))-numpy.log(idf(o))) < 10.**-10., 'edf.__call__ w/ tlist set to [to] did not return initial DF' #Call w/ just to assert numpy.fabs(numpy.log(edf(o,-10.))-numpy.log(idf(o))) < 10.**-10., 'edf.__call__ w/ tlist set to [to] did not return initial DF' #also w/ log assert numpy.fabs(edf(o,[-10.],log=True)-numpy.log(idf(o))) < 10.**-10., 'edf.__call__ w/ tlist set to [to] did not return initial DF (log)' assert numpy.fabs(edf(o,-10.,log=True)-numpy.log(idf(o))) < 10.**-10., 'edf.__call__ w/ tlist set to [to] did not return initial DF (log)' # Tests w/ odeint: tlist codeint= edf(o,[0.,-2.5,-5.,-7.5,-10.],integrate_method='odeint',log=True) crk6c= edf(o,[0.,-2.5,-5.,-7.5,-10.],integrate_method='rk6_c',log=True) assert numpy.all(numpy.fabs(codeint-crk6c) < 10.**-4.), 'edf.__call__ w/ odeint and tlist does not give the same result as w/ rk6_c' # Crazy orbit w/ tlist crk6c= edf(Orbit([3.,1.,-1.,2.]),[0.],integrate_method='odeint',log=True) assert crk6c < -20., 'crazy orbit does not have DF equal to zero' # deriv w/ odeint codeint= edf(o,[0.,-2.5,-5.,-7.5,-10.],integrate_method='odeint', deriv='R') crk6c= edf(o,[0.,-2.5,-5.,-7.5,-10.],integrate_method='rk6_c',deriv='R') assert numpy.all(numpy.fabs(codeint-crk6c) < 10.**-4.), 'edf.__call__ w/ odeint and tlist does not give the same result as w/ rk6_c (deriv=R)' # deriv w/ len(tlist)=1 crk6c= edf(o,[0.],integrate_method='rk6_c',deriv='R') crk6c2= edf(o,0.,integrate_method='rk6_c',deriv='R') assert numpy.all(numpy.fabs(crk6c-crk6c2) < 10.**-4.), 'edf.__call__ w/ tlist consisting of one time and just a scalar time do not agree' #Call w/ just to and deriv assert numpy.fabs(edf(o,-10.,deriv='R')-idf(o)*idf._dlnfdR(o._orb.vxvv[0],o._orb.vxvv[1],o._orb.vxvv[2])) < 10.**-10., 'edf.__call__ w/ to did not return initial DF (deriv=R)' assert numpy.fabs(edf(o,-10.,deriv='phi')) < 10.**-10., 'edf.__call__ w/ to did not return initial DF (deriv=phi)' # Call w/ just one t and odeint codeint= edf(o,0,integrate_method='odeint',log=True) crk6c= edf(o,0.,integrate_method='rk6_c',log=True) assert numpy.fabs(codeint-crk6c) < 10.**-4., 'edf.__call__ w/ odeint and tlist does not give the same result as w/ rk6_c' # Call w/ just one t and fallback to odeint # turn off C edf._pot[0].hasC= False edf._pot[0].hasC_dxdv= False codeint= edf(o,0,integrate_method='dopr54_c',log=True) assert numpy.fabs(codeint-crk6c) < 10.**-4., 'edf.__call__ w/ odeint and tlist does not give the same result as w/ rk6_c' # Call w/ just one t and fallback to leaprog cleapfrog= edf(o,0,integrate_method='leapfrog_c',log=True) assert numpy.fabs(cleapfrog-crk6c) < 10.**-4., 'edf.__call__ w/ odeint and tlist does not give the same result as w/ rk6_c'
def plot_vpecvo(filename, plotfilename): if not os.path.exists(filename): raise IOError("given filename does not exist") savefile = open(filename, 'rb') params = pickle.load(savefile) savefile.close() vos = numpy.array([s[0] for s in params]) * _REFV0 ros = numpy.array([s[1] for s in params]) * _REFR0 if _ROTCURVE == 'flat': vpec = numpy.array([s[7] for s in params ]) * _PMSGRA * ros - vos #7 w/ dwarf vpecR = numpy.array([s[6] for s in params]) * _VRSUN #6 w/ dwarf elif _ROTCURVE == 'powerlaw' or _ROTCURVE == 'linear': vpec = numpy.array([s[8] for s in params ]) * _PMSGRA * ros - vos #7 w/ dwarf vpecR = numpy.array([s[7] for s in params]) * _VRSUN #6 w/ dwarf bovy_plot.bovy_print() levels = list(special.erf(0.5 * numpy.arange(1, 4))) levels.append(1.01) #HACK to not plot outliers axScatter, axHistx, axHisty = bovy_plot.scatterplot( vpecR, #vos/ros+vpec/ros, vpec, 'k,', levels=levels, xlabel=r'$V_{R,\odot}\ [\mathrm{km\ s}^{-1}]$', ylabel=r'$V_{\phi,\odot}-V_c\ [\mathrm{km\ s}^{-1}]$', bins=31, xrange=[-15., 0.], yrange=[0., 35.], contours=True, cntrcolors='k', onedhists=True, cmap='gist_yarg', retAxes=True) #SBD10 value bovy_plot.bovy_plot([-20., 40.], [12.24, 12.24], '--', color='0.5', overplot=True) bovy_plot.bovy_text(-4., 12.7, r'$\mathrm{SBD10}$') axHisty.plot([0., 100.], [12.24, 12.24], '--', color='0.5') bovy_plot.bovy_plot([_VRSUN, _VRSUN], [-100., 100.], '--', color='0.5', overplot=True) bovy_plot.bovy_text(_VRSUN - .75, 7., r'$\mathrm{SBD10}$', rotation=90.) axHistx.plot([_VRSUN, _VRSUN], [0., 100.], '--', color='0.5') #Reid / Brunthaler #bovy_plot.bovy_plot([_PMSGRA,_PMSGRA],[-10.,100.], # '--',color='0.5',overplot=True) #axHistx.plot([_PMSGRA,_PMSGRA],[0.,100.],'--',color='0.5') #bovy_plot.bovy_text(29.4,5.,r'$\mathrm{RB04}$') #Inset, closed orbit at the Sun lp = LogarithmicHaloPotential(normalize=1.) ep = EllipticalDiskPotential(phib=numpy.pi / 2., p=0., tform=-100., tsteady=-100., twophio=14. / 220.) #0.072725) o = Orbit([1., 0., 1. + 14. / 220., 0.]) oc = Orbit([1., 0., 1., 0.]) ts = numpy.linspace(0., 4. * numpy.pi, 1001) o.integrate(ts, [lp, ep]) print o.e(analytic=True, pot=lp) oc.integrate(ts, lp) left, bottom, width, height = 0.45, 0.45, 0.25, 0.25 axInset = pyplot.axes([left, bottom, width, height]) pyplot.sca(axInset) pyplot.plot(o._orb.orbit[:, 0] * numpy.cos(o._orb.orbit[:, 3]), o._orb.orbit[:, 0] * numpy.sin(o._orb.orbit[:, 3]), 'k-') pyplot.plot(oc._orb.orbit[:, 0] * numpy.cos(oc._orb.orbit[:, 3]), oc._orb.orbit[:, 0] * numpy.sin(oc._orb.orbit[:, 3]), '--', color='0.6') pyplot.xlim(-1.5, 1.5) pyplot.ylim(-1.5, 1.5) #pyplot.xlabel(r'$x / R_0$') #pyplot.ylabel(r'$y / R_0$') nullfmt = NullFormatter() # no labels axInset.xaxis.set_major_formatter(nullfmt) axInset.yaxis.set_major_formatter(nullfmt) bovy_plot.bovy_end_print(plotfilename)