Example #1
0
def plot_dfcorrections(plotfilename):
    niters= [1,2,3,4,5,10,15,20,25]
    bovy_plot.bovy_print(fig_height=7.,fig_width=8.)
    ii= 0
    # Load DF
    pyplot.subplot(2,1,1)
    dfc= dehnendf(beta=0.,correct=True,niter=niters[ii])
    bovy_plot.bovy_plot(dfc._corr._rs,
                        numpy.log(dfc._corr._corrections[:,0]),
                        '-',gcf=True,color=cm.jet(1.),lw=2.,zorder=1,
                        xrange=[0.,5.],
                        yrange=[-0.25,0.25],
                        ylabel=r'$\ln \Sigma_{\mathrm{out}}(R)-\ln\Sigma_{\mathrm{DF}}(R)$')
    linthresh= 0.0001
    pyplot.yscale('symlog',linthreshy=linthresh)
    for ii,niter in enumerate(niters[1:]):
        dfcn= dehnendf(beta=0.,correct=True,niter=niter)
        dfcp= dehnendf(beta=0.,correct=True,niter=niter-1)
        bovy_plot.bovy_plot(dfc._corr._rs,
                            numpy.log(dfcn._corr._corrections[:,0])-numpy.log(dfcp._corr._corrections[:,0]),
                            '-',overplot=True,
                            color=cm.jet(1.-(ii+1)/float(len(niters))),lw=2.,
                            zorder=ii+2)
    pyplot.fill_between(numpy.linspace(0.,5.,2.),
                        -linthresh*numpy.ones(2),
                         linthresh*numpy.ones(2),color='0.9',
                         zorder=0)
    bovy_plot.bovy_text(4.,-0.00008,r'$\mathrm{linear\ scale}$',
                        backgroundcolor='w',size=16.)
    pyplot.subplot(2,1,2)
    bovy_plot.bovy_plot(dfc._corr._rs,
                        0.5*numpy.log(dfc._corr._corrections[:,1]),
                        '-',gcf=True,color=cm.jet(1.),lw=2.,zorder=1,
                        xrange=[0.,5.],
                        yrange=[-0.25,0.25],
                        xlabel=r'$R/R_0$',
                        ylabel=r'$\ln \sigma_{R,\mathrm{out}}(R)-\ln\sigma_{R,\mathrm{DF}}(R)$')
    pyplot.yscale('symlog',linthreshy=linthresh)
    for ii,niter in enumerate(niters[1:]):
        dfcn= dehnendf(beta=0.,correct=True,niter=niter)
        dfcp= dehnendf(beta=0.,correct=True,niter=niter-1)
        bovy_plot.bovy_plot(dfc._corr._rs,
                            numpy.log(dfcn._corr._corrections[:,1])-numpy.log(dfcp._corr._corrections[:,1]),
                            '-',overplot=True,
                            color=cm.jet(1.-(ii+1)/float(len(niters))),lw=2.,
                            zorder=ii+2)
    pyplot.fill_between(numpy.linspace(0.,5.,2.),
                        -linthresh*numpy.ones(2),
                         linthresh*numpy.ones(2),color='0.9',
                         zorder=0)
    bovy_plot.bovy_text(4.,-0.00008,r'$\mathrm{linear\ scale}$',
                        backgroundcolor='w',size=16.)
    pyplot.tight_layout()
    bovy_plot.bovy_end_print(plotfilename)
    return None
Example #2
0
def test_mildnonaxi_meanvr_grid():
    # Test that for a close to axisymmetric potential, the mean vr is close to zero
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    mvr, grid= edf.meanvR(0.9,phi=0.2,integrate_method='rk6_c',grid=True,
                          returnGrid=True,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(mvr) < 0.001, 'meanvR of evolveddiskdf for axisymmetric potential is not equal to zero'
    mvr= edf.meanvR(0.9,phi=0.2,integrate_method='rk6_c',grid=grid)
    assert numpy.fabs(mvr) < 0.001, 'meanvR of evolveddiskdf for axisymmetric potential is not equal to zero when calculated with pre-computed grid'
    #Pre-compute surfmass and use it, first test that grid is properly returned when given
    smass, ngrid= edf.vmomentsurfacemass(0.9,0,0,phi=0.2,
                                        integrate_method='rk6_c',
                                        grid=grid,gridpoints=_GRIDPOINTS,
                                        returnGrid=True)
    assert ngrid == grid, 'grid returned by vmomentsurfacemass w/ grid input is not the same as the input'
    #Pre-compute surfmass and use it
    nsmass= edf.vmomentsurfacemass(0.9,0,0,phi=0.2,
                                   integrate_method='rk6_c',
                                   grid=True,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(smass-nsmass) < 0.001, 'surfacemass computed w/ and w/o returnGrid are not the same'
    mvr= edf.meanvR(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                    surfacemass=smass)
    assert numpy.fabs(mvr) < 0.001, 'meanvR of evolveddiskdf for axisymmetric potential is not equal to zero when calculated with pre-computed grid and surfacemass'
    global _maxi_meanvr
    _maxi_meanvr= mvr
    global _maxi_surfacemass
    _maxi_surfacemass= smass
    return None
Example #3
0
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
Example #4
0
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
Example #5
0
def test_mildnonaxi_meanvt_hierarchgrid_tlist():
    # Test that for a close to axisymmetric potential, the mean vt is close to that of the initial DF
    idf = dehnendf(beta=0.)
    pot = [
        LogarithmicHaloPotential(normalize=1.),
        SteadyLogSpiralPotential(A=-0.005, omegas=0.2)
    ]  #very mild non-axi
    edf = evolveddiskdf(idf, pot=pot, to=-10.)
    mvt, grid = edf.meanvT(0.9,
                           t=[0., -2.5, -5., -7.5, -10.],
                           phi=0.2,
                           integrate_method='rk6_c',
                           grid=True,
                           hierarchgrid=True,
                           returnGrid=True,
                           gridpoints=_GRIDPOINTS)
    assert numpy.all(
        numpy.fabs(mvt - idf.meanvT(0.9)) < 0.005
    ), 'meanvT of evolveddiskdf for axisymmetric potential is not equal to that of the initial dehnendf when using hierarchgrid and tlist'
    mvt = edf.meanvT(0.9,
                     t=[0., -2.5, -5., -7.5, -10.],
                     phi=0.2,
                     integrate_method='rk6_c',
                     grid=grid,
                     gridpoints=_GRIDPOINTS)
    assert numpy.all(
        numpy.fabs(mvt - idf.meanvT(0.9)) < 0.005
    ), 'meanvT of evolveddiskdf for axisymmetric potential is not equal to that of the initial dehnendf when calculated with pre-computed grid when using hierarchgrid and tlist'
    return None
def test_mildnonaxi_meanvt_hierarchgrid():
    # Test that for a close to axisymmetric potential, the mean vt is close to that of the initial DF
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    mvt, grid= edf.meanvT(0.9,phi=0.2,integrate_method='rk6_c',
                          grid=True,hierarchgrid=True,
                          returnGrid=True,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(mvt-idf.meanvT(0.9)) < 0.005, 'meanvT of evolveddiskdf for axisymmetric potential is not equal to that of the initial dehnendf when using hierarchgrid'
    mvt= edf.meanvT(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                    gridpoints=_GRIDPOINTS)
    assert numpy.fabs(mvt-idf.meanvT(0.9)) < 0.005, 'meanvT of evolveddiskdf for axisymmetric potential is not equal to that of the initial dehnendf when calculated with pre-computed grid when using hierarchgrid'
    #Also test that the hierarchgrid is properly returned
    smass, ngrid= edf.vmomentsurfacemass(0.9,0,0,phi=0.2,
                                        integrate_method='rk6_c',
                                        grid=grid,gridpoints=_GRIDPOINTS,
                                         returnGrid=True)
    assert ngrid == grid, 'hierarchical grid returned by vmomentsurfacemass w/ grid input is not the same as the input'
    nsmass= edf.vmomentsurfacemass(0.9,0,0,phi=0.2,
                                   integrate_method='rk6_c',
                                   grid=True,hierarchgrid=True,
                                   gridpoints=_GRIDPOINTS)
    assert numpy.fabs(smass-nsmass) < 0.001, 'surfacemass computed w/ and w/o returnGrid are not the same'
    return None
Example #7
0
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
Example #8
0
def test_mildnonaxi_meanvr_grid_tlist():
    # Test that for a close to axisymmetric potential, the mean vr is close to zero
    idf = dehnendf(beta=0.)
    pot = [
        LogarithmicHaloPotential(normalize=1.),
        SteadyLogSpiralPotential(A=-0.005, omegas=0.2)
    ]  #very mild non-axi
    edf = evolveddiskdf(idf, pot=pot, to=-10.)
    mvr, grid = edf.meanvR(0.9,
                           t=[0., -2.5, -5., -7.5, -10.],
                           phi=0.2,
                           integrate_method='rk6_c',
                           grid=True,
                           returnGrid=True,
                           gridpoints=_GRIDPOINTS)
    assert numpy.all(
        numpy.fabs(mvr) < 0.003
    ), 'meanvR of evolveddiskdf for axisymmetric potential is not equal to zero for list of times'
    mvr = edf.meanvR(0.9,
                     t=[0., -2.5, -5., -7.5, -10.],
                     phi=0.2,
                     integrate_method='rk6_c',
                     grid=grid)
    assert numpy.all(
        numpy.fabs(mvr) < 0.003
    ), 'meanvR of evolveddiskdf for axisymmetric potential is not equal to zero when calculated with pre-computed grid for list of times'
    return None
Example #9
0
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
Example #10
0
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_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_mildnonaxi_meanvr_grid():
    # Test that for a close to axisymmetric potential, the mean vr is close to zero
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    mvr, grid= edf.meanvR(0.9,phi=0.2,integrate_method='rk6_c',grid=True,
                          returnGrid=True,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(mvr) < 0.001, 'meanvR of evolveddiskdf for axisymmetric potential is not equal to zero'
    mvr= edf.meanvR(0.9,phi=0.2,integrate_method='rk6_c',grid=grid)
    assert numpy.fabs(mvr) < 0.001, 'meanvR of evolveddiskdf for axisymmetric potential is not equal to zero when calculated with pre-computed grid'
    #Pre-compute surfmass and use it, first test that grid is properly returned when given
    smass, ngrid= edf.vmomentsurfacemass(0.9,0,0,phi=0.2,
                                        integrate_method='rk6_c',
                                        grid=grid,gridpoints=_GRIDPOINTS,
                                        returnGrid=True)
    assert ngrid == grid, 'grid returned by vmomentsurfacemass w/ grid input is not the same as the input'
    #Pre-compute surfmass and use it
    nsmass= edf.vmomentsurfacemass(0.9,0,0,phi=0.2,
                                   integrate_method='rk6_c',
                                   grid=True,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(smass-nsmass) < 0.001, 'surfacemass computed w/ and w/o returnGrid are not the same'
    mvr= edf.meanvR(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                    surfacemass=smass)
    assert numpy.fabs(mvr) < 0.001, 'meanvR of evolveddiskdf for axisymmetric potential is not equal to zero when calculated with pre-computed grid and surfacemass'
    global _maxi_meanvr
    _maxi_meanvr= mvr
    global _maxi_surfacemass
    _maxi_surfacemass= smass
    return None
Example #13
0
def test_plot_hierarchgrid():
    idf = dehnendf(beta=0.)
    pot = [
        LogarithmicHaloPotential(normalize=1.),
        SteadyLogSpiralPotential(A=-0.005, omegas=0.2)
    ]  #very mild non-axi
    edf = evolveddiskdf(idf, pot=pot, to=-10.)
    mvr, grid = edf.meanvR(0.9,
                           phi=0.2,
                           integrate_method='rk6_c',
                           grid=True,
                           hierarchgrid=True,
                           returnGrid=True,
                           gridpoints=_GRIDPOINTS)
    grid.plot()
    #w/ list of tiems
    mvr, grid = edf.meanvR(0.9,
                           t=[0., -2.5, -5., -7.5, -10.],
                           phi=0.2,
                           integrate_method='rk6_c',
                           grid=True,
                           hierarchgrid=True,
                           returnGrid=True,
                           gridpoints=_GRIDPOINTS)
    grid.plot(1)
    return None
Example #14
0
def create_fake_los(parser):
    """
    NAME:
       create_fake_los
    PURPOSE:
       create a fake APOGEE LOS
    INPUT:
       parser - from optparse
    OUTPUT:
       stuff as specified by the options
    HISTORY:
       2011-04-20 - Written - Bovy (NYU)
    """
    (options, args) = parser.parse_args()
    if len(args) == 0:
        parser.print_help()
        sys.exit(-1)
    nu.random.seed(seed=options.seed)
    #Set up DF
    print "Setting up DF ..."
    dfc = dehnendf(beta=options.beta,
                   profileParams=(options.rd, options.rs, options.so),
                   correct=True,
                   niter=20)
    #Sample until we have nobjects
    if options.local:
        sample_local(options, args, dfc)
    else:
        sample_los(options, args, dfc)
    return None
Example #15
0
def test_mildnonaxi_meanvt_grid_tlist_onet():
    # Test that for a close to axisymmetric potential, the mean vt is close to that of the initial DF, for a list consisting of a single time
    idf = dehnendf(beta=0.)
    pot = [
        LogarithmicHaloPotential(normalize=1.),
        SteadyLogSpiralPotential(A=-0.005, omegas=0.2)
    ]  #very mild non-axi
    edf = evolveddiskdf(idf, pot=pot, to=-10.)
    mvt, grid = edf.meanvT(0.9,
                           t=[0.],
                           phi=0.2,
                           integrate_method='rk6_c',
                           grid=True,
                           returnGrid=True,
                           gridpoints=_GRIDPOINTS)
    assert numpy.fabs(
        mvt - idf.meanvT(0.9)
    ) < 0.005, 'meanvT of evolveddiskdf for axisymmetric potential is not equal to that of the initial dehnendf'
    mvt = edf.meanvT(
        0.9,
        t=[0.],
        phi=0.2,
        integrate_method='rk6_c',
        grid=grid,
        gridpoints=_GRIDPOINTS,
    )
    assert numpy.fabs(
        mvt - idf.meanvT(0.9)
    ) < 0.005, 'meanvT of evolveddiskdf for axisymmetric potential is not equal to that of the initial dehnendf when calculated with pre-computed grid'
    global _maxi_meanvt
    _maxi_meanvt = mvt
    return None
def create_fake_los(parser):
    """
    NAME:
       create_fake_los
    PURPOSE:
       create a fake APOGEE LOS
    INPUT:
       parser - from optparse
    OUTPUT:
       stuff as specified by the options
    HISTORY:
       2011-04-20 - Written - Bovy (NYU)
    """
    (options,args)= parser.parse_args()
    if len(args) == 0:
        parser.print_help()
        sys.exit(-1)
    nu.random.seed(seed=options.seed)
    #Set up DF
    print "Setting up DF ..."
    dfc= dehnendf(beta=options.beta,profileParams=(options.rd,options.rs,options.so),correct=True,niter=20)
    #Sample until we have nobjects
    if options.local:
        sample_local(options,args,dfc)
    else:
        sample_los(options,args,dfc)
    return None
Example #17
0
def test_mildnonaxi_meanvt_hierarchgrid():
    # Test that for a close to axisymmetric potential, the mean vt is close to that of the initial DF
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    mvt, grid= edf.meanvT(0.9,phi=0.2,integrate_method='rk6_c',
                          grid=True,hierarchgrid=True,
                          returnGrid=True,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(mvt-idf.meanvT(0.9)) < 0.005, 'meanvT of evolveddiskdf for axisymmetric potential is not equal to that of the initial dehnendf when using hierarchgrid'
    mvt= edf.meanvT(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                    gridpoints=_GRIDPOINTS)
    assert numpy.fabs(mvt-idf.meanvT(0.9)) < 0.005, 'meanvT of evolveddiskdf for axisymmetric potential is not equal to that of the initial dehnendf when calculated with pre-computed grid when using hierarchgrid'
    #Also test that the hierarchgrid is properly returned
    smass, ngrid= edf.vmomentsurfacemass(0.9,0,0,phi=0.2,
                                        integrate_method='rk6_c',
                                        grid=grid,gridpoints=_GRIDPOINTS,
                                         returnGrid=True)
    assert ngrid == grid, 'hierarchical grid returned by vmomentsurfacemass w/ grid input is not the same as the input'
    nsmass= edf.vmomentsurfacemass(0.9,0,0,phi=0.2,
                                   integrate_method='rk6_c',
                                   grid=True,hierarchgrid=True,
                                   gridpoints=_GRIDPOINTS)
    assert numpy.fabs(smass-nsmass) < 0.001, 'surfacemass computed w/ and w/o returnGrid are not the same'
    return None
Example #18
0
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
Example #19
0
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_oort():
    from galpy.df import dehnendf
    df= dehnendf(beta=0.,correct=True,niter=20,
                 profileParams=(1./3.,1.,0.1))
    va= 1.-df.meanvT(1.) # asymmetric drift
    A= df.oortA(1.)
    B= df.oortB(1.)
    return None
Example #21
0
def test_oort():
    from galpy.df import dehnendf
    df= dehnendf(beta=0.,correct=True,niter=20,
                 profileParams=(1./3.,1.,0.1))
    va= 1.-df.meanvT(1.) # asymmetric drift
    A= df.oortA(1.)
    B= df.oortB(1.)
    return None
def _calc_pred(vs, location, l, dmax):
    """Calculate the predicted distribution"""
    thisvs = vs
    if _PREDICTCACHE:
        savefilename = os.path.join(
            os.getenv('DATADIR'), 'bovy', 'vclos',
            'predictedVclosDistribution_%i_%.0f_%i_%.0f_%.2f_%i_%.2f.sav' %
            (location, l, len(vs), _PREDICTVC, _PREDICTSIGMA, _PREDICTNDS,
             dmax))
    if _PREDICTCACHE and os.path.exists(savefilename):
        #Load prediction
        savefile = open(savefilename, 'rb')
        pred_dist = pickle.load(savefile)
        savefile.close()
    else:
        ds = numpy.linspace(0., dmax, _PREDICTNDS)
        dfc = dehnendf(beta=0.,
                       profileParams=(1. / 3., 1., _PREDICTSIGMA),
                       correct=True,
                       niter=20)
        thisl = l * _DEGTORAD
        thisvsolar = numpy.dot(
            _PREDICTVSOLAR, numpy.array([-numpy.cos(thisl),
                                         numpy.sin(thisl)]))
        thisvs += thisvsolar
        thisvs /= _PREDICTVC
        ii = 0
        pred_dist = numpy.zeros(len(vs))
        while ii < len(vs):
            out = 0.
            norm = 0.
            for jj in range(_PREDICTNDS):
                d = ds[jj]
                R = math.sqrt(1. + d**2. - 2. * d * math.cos(thisl))
                if R == 0.:
                    R += 0.0001
                    d += 0.0001
                if 1. / math.cos(thisl) < d and math.cos(thisl) > 0.:
                    theta = math.pi - math.asin(d / R * math.sin(thisl))
                else:
                    theta = math.asin(d / R * math.sin(thisl))
                vR = -thisvs[ii] * math.cos(
                    theta + thisl)  #vperp=0 for marginalization
                vT = thisvs[ii] * math.sin(theta + thisl)
                o = Orbit([R, vR, vT, theta])
                surfmass = dfc.surfacemassLOS(d, thisl, deg=False)
                out += surfmass * dfc(
                    o, marginalizeVperp=True) / dfc.targetSurfacemass(R)
                norm += surfmass
            pred_dist[ii] = out / norm
            ii += 1
        if _PREDICTCACHE:
            savefile = open(savefilename, 'wb')
            try:
                pickle.dump(pred_dist, savefile)
            finally:
                savefile.close()
    return pred_dist
def test_mildnonaxi_meanvt_direct():
    # Test that for a close to axisymmetric potential, the mean vt is close to that of the initial DF
    # We do this for an axisymmetric potential, bc otherwise it takes too long
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.)]
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    mvt= edf.meanvT(0.9,phi=0.2,integrate_method='rk6_c',grid=False)
    assert numpy.fabs(mvt-idf.meanvT(0.9)) < 0.001, 'meanvT of evolveddiskdf for axisymmetric potential is not equal to that of the initial dehnendf when using direct integration'
    return None
Example #24
0
def test_mildnonaxi_meanvt_direct():
    # Test that for a close to axisymmetric potential, the mean vt is close to that of the initial DF
    # We do this for an axisymmetric potential, bc otherwise it takes too long
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.)]
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    mvt= edf.meanvT(0.9,phi=0.2,integrate_method='rk6_c',grid=False)
    assert numpy.fabs(mvt-idf.meanvT(0.9)) < 0.001, 'meanvT of evolveddiskdf for axisymmetric potential is not equal to that of the initial dehnendf when using direct integration'
    return None
Example #25
0
def test_mildnonaxi_meanvr_direct():
    # Test that for an axisymmetric potential, the mean vr is close to zero
    # We do this for an axisymmetric potential, bc otherwise it takes too long
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.)]
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    mvr= edf.meanvR(0.9,phi=0.2,integrate_method='rk6_c',grid=False)
    assert numpy.fabs(mvr) < 0.001, 'meanvR of evolveddiskdf for axisymmetric potential is not equal to zero when calculated directly'
    return None
Example #26
0
def test_mildnonaxi_vertexdev_direct():
    # Test that for an axisymmetric potential, the vertex deviation is close zero
    # We do this for an axisymmetric potential, bc otherwise it takes too long
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.)]
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    vdev= edf.vertexdev(0.9,phi=0.2,integrate_method='rk6_c',grid=False)
    assert numpy.fabs(vdev) < 0.01/180.*numpy.pi, 'vertexdev of evolveddiskdf for axisymmetric potential is not equal to zero when calculated directly'
    return None
def test_mildnonaxi_meanvr_direct():
    # Test that for an axisymmetric potential, the mean vr is close to zero
    # We do this for an axisymmetric potential, bc otherwise it takes too long
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.)]
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    mvr= edf.meanvR(0.9,phi=0.2,integrate_method='rk6_c',grid=False)
    assert numpy.fabs(mvr) < 0.001, 'meanvR of evolveddiskdf for axisymmetric potential is not equal to zero when calculated directly'
    return None
def test_mildnonaxi_sigmat2_direct():
    # Test that for an axisymmetric potential, the sigmaT2  is close to the value of the initial DF   
    # We do this for an axisymmetric potential, bc otherwise it takes too long
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.)]
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    st2= edf.sigmaT2(0.9,phi=0.2,integrate_method='rk6_c',grid=False)
    ist2= idf.sigmaT2(0.9)
    assert numpy.fabs(numpy.log(st2)-numpy.log(ist2)) < 0.025, 'sigmat2 of evolveddiskdf for axisymmetric potential is not equal to that of initial DF when calculated directly'
    return None
Example #29
0
def test_mildnonaxi_sigmat2_direct():
    # Test that for an axisymmetric potential, the sigmaT2  is close to the value of the initial DF   
    # We do this for an axisymmetric potential, bc otherwise it takes too long
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.)]
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    st2= edf.sigmaT2(0.9,phi=0.2,integrate_method='rk6_c',grid=False)
    ist2= idf.sigmaT2(0.9)
    assert numpy.fabs(numpy.log(st2)-numpy.log(ist2)) < 0.025, 'sigmat2 of evolveddiskdf for axisymmetric potential is not equal to that of initial DF when calculated directly'
    return None
def test_mildnonaxi_meanvt_direct_tlist():
    # Shouldn't work
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.)]
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    try:
        edf.meanvT(0.9,t=[0.,-2.5,-5.,-7.5,-10.],
                   phi=0.2,integrate_method='rk6_c',grid=False)
    except IOError: pass
    else: raise AssertionError('direct evolveddiskdf calculation of meanvT w/ list of times did not raise IOError')
    return None
Example #31
0
def test_mildnonaxi_vertexdev_direct():
    # Test that for an axisymmetric potential, the vertex deviation is close zero
    # We do this for an axisymmetric potential, bc otherwise it takes too long
    idf = dehnendf(beta=0.)
    pot = [LogarithmicHaloPotential(normalize=1.)]
    edf = evolveddiskdf(idf, pot=pot, to=-10.)
    vdev = edf.vertexdev(0.9, phi=0.2, integrate_method='rk6_c', grid=False)
    assert numpy.fabs(
        vdev
    ) < 0.01 / 180. * numpy.pi, 'vertexdev of evolveddiskdf for axisymmetric potential is not equal to zero when calculated directly'
    return None
Example #32
0
def test_mildnonaxi_meanvt_direct_tlist():
    # Shouldn't work
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.)]
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    try:
        edf.meanvT(0.9,t=[0.,-2.5,-5.,-7.5,-10.],
                   phi=0.2,integrate_method='rk6_c',grid=False)
    except IOError: pass
    else: raise AssertionError('direct evolveddiskdf calculation of meanvT w/ list of times did not raise IOError')
    return None
Example #33
0
def test_meanlz():
    #This is a *very* rough test against a rough estimate of the mean
    qdf= quasiisothermaldf(1./4.,0.2,0.1,1.,1.,
                           pot=MWPotential,aA=aAS,cutcounter=True)
    from galpy.df import dehnendf #baseline
    dfc= dehnendf(profileParams=(1./4.,1.0, 0.2),
                  beta=0.,correct=False)
    assert numpy.fabs(numpy.log(qdf.meanlz(0.9,0.,mc=True))\
                          -numpy.log(0.9*dfc.meanvT(0.9))) < 0.1, 'meanlz is not what is expected'
    assert numpy.fabs(numpy.log(qdf.meanlz(0.5,0.,mc=True))\
                          -numpy.log(0.5*dfc.meanvT(0.5))) < 0.2, 'meanlz is not what is expected'
    return None
Example #34
0
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 test_plot_grid():
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    mvr, grid= edf.meanvR(0.9,phi=0.2,integrate_method='rk6_c',grid=True,
                          returnGrid=True,gridpoints=_GRIDPOINTS)
    grid.plot()
    #w/ list of tiems
    mvr, grid= edf.meanvR(0.9,t=[0.,-2.5,-5.,-7.5,-10.],
                          phi=0.2,integrate_method='rk6_c',grid=True,
                          returnGrid=True,gridpoints=_GRIDPOINTS)
    grid.plot(1)
    return None
def test_mildnonaxi_meanvr_grid_tlist():
    # Test that for a close to axisymmetric potential, the mean vr is close to zero
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    mvr, grid= edf.meanvR(0.9,t=[0.,-2.5,-5.,-7.5,-10.],
                          phi=0.2,integrate_method='rk6_c',
                          grid=True,returnGrid=True,gridpoints=_GRIDPOINTS)
    assert numpy.all(numpy.fabs(mvr) < 0.003), 'meanvR of evolveddiskdf for axisymmetric potential is not equal to zero for list of times'
    mvr= edf.meanvR(0.9,t=[0.,-2.5,-5.,-7.5,-10.],
                    phi=0.2,integrate_method='rk6_c',grid=grid)
    assert numpy.all(numpy.fabs(mvr) < 0.003), 'meanvR of evolveddiskdf for axisymmetric potential is not equal to zero when calculated with pre-computed grid for list of times'
    return None
Example #37
0
def test_meanvT_staeckel_gl():
    qdf= quasiisothermaldf(1./4.,0.2,0.1,1.,1.,
                           pot=MWPotential,aA=aAS,cutcounter=True)
    from galpy.df import dehnendf #baseline
    dfc= dehnendf(profileParams=(1./4.,1.0, 0.2),
                  beta=0.,correct=False)
    #In the mid-plane
    vtp9= qdf.meanvT(0.9,0.,gl=True)
    assert numpy.fabs(vtp9-dfc.meanvT(0.9)) < 0.05, "qdf's meanvT is not close to that of dehnendf"
    assert vtp9 <  vcirc(MWPotential,0.9), "qdf's meanvT is not less than the circular velocity (which we expect)"
    #higher up
    assert qdf.meanvR(0.9,0.2,gl=True) < vtp9, "qdf's meanvT above the plane is not less than in the plane (which we expect)"
    assert qdf.meanvR(0.9,-0.25,gl=True) < vtp9, "qdf's meanvT above the plane is not less than in the plane (which we expect)"
    return None
def test_mildnonaxi_meanvt_grid():
    # Test that for a close to axisymmetric potential, the mean vt is close to that of the initial DF
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    mvt, grid= edf.meanvT(0.9,phi=0.2,integrate_method='rk6_c',grid=True,
                          returnGrid=True,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(mvt-idf.meanvT(0.9)) < 0.005, 'meanvT of evolveddiskdf for axisymmetric potential is not equal to that of the initial dehnendf'
    mvt= edf.meanvT(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                    gridpoints=_GRIDPOINTS,)
    assert numpy.fabs(mvt-idf.meanvT(0.9)) < 0.005, 'meanvT of evolveddiskdf for axisymmetric potential is not equal to that of the initial dehnendf when calculated with pre-computed grid'
    global _maxi_meanvt
    _maxi_meanvt= mvt
    return None
def _calc_pred(vs,location,l,dmax):
    """Calculate the predicted distribution"""
    thisvs= vs
    if _PREDICTCACHE:
        savefilename= os.path.join(os.getenv('DATADIR'),'bovy','vclos',
                                   'predictedVclosDistribution_%i_%.0f_%i_%.0f_%.2f_%i_%.2f.sav' % (location,l,len(vs),_PREDICTVC,_PREDICTSIGMA,_PREDICTNDS,dmax))
    if _PREDICTCACHE and os.path.exists(savefilename):
        #Load prediction
        savefile= open(savefilename,'rb')
        pred_dist= pickle.load(savefile)
        savefile.close()
    else:
        ds= numpy.linspace(0.,dmax,_PREDICTNDS)
        dfc= dehnendf(beta=0.,profileParams=(1./3.,1.,_PREDICTSIGMA),correct=True,niter=20)
        thisl= l*_DEGTORAD
        thisvsolar= numpy.dot(_PREDICTVSOLAR,numpy.array([-numpy.cos(thisl),numpy.sin(thisl)]))
        thisvs+= thisvsolar
        thisvs/= _PREDICTVC
        ii= 0
        pred_dist= numpy.zeros(len(vs))
        while ii < len(vs):
            out= 0.
            norm= 0.
            for jj in range(_PREDICTNDS):
                d= ds[jj]
                R= math.sqrt(1.+d**2.-2.*d*math.cos(thisl))
                if R == 0.:
                    R+= 0.0001
                    d+= 0.0001
                if 1./math.cos(thisl) < d and math.cos(thisl) > 0.:
                    theta= math.pi-math.asin(d/R*math.sin(thisl))
                else:
                    theta= math.asin(d/R*math.sin(thisl))
                vR= -thisvs[ii]*math.cos(theta+thisl) #vperp=0 for marginalization
                vT= thisvs[ii]*math.sin(theta+thisl)
                o= Orbit([R,vR,vT,theta])
                surfmass= dfc.surfacemassLOS(d,thisl,deg=False)
                out+= surfmass*dfc(o,marginalizeVperp=True)/dfc.targetSurfacemass(R)
                norm+= surfmass
            pred_dist[ii]= out/norm
            ii+= 1
        if _PREDICTCACHE:
            savefile= open(savefilename,'wb')
            try:
                pickle.dump(pred_dist,savefile)
            finally:
                savefile.close()
    return pred_dist
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_mildnonaxi_vertexdev_grid():
    # Test that for a close to axisymmetric potential, the vertex deviation is close to zero
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    vdev, grid= edf.vertexdev(0.9,phi=0.2,integrate_method='rk6_c',grid=True,
                              returnGrid=True,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(vdev) < 2., 'vertexdev of evolveddiskdf for axisymmetric potential is not close to zero' #2 is pretty big, but the weak spiral creates that
    vdev= edf.vertexdev(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                        gridpoints=_GRIDPOINTS)
    assert numpy.fabs(vdev) < 2., 'vertexdev of evolveddiskdf for axisymmetric potential is not equal zero when calculated with pre-computed grid'
    vdev= edf.vertexdev(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                        sigmaR2=_maxi_sigmar2,sigmaT2=_maxi_sigmat2,
                        sigmaRT=_maxi_sigmart,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(vdev) < 2., 'sigmart of evolveddiskdf for axisymmetric potential is not equal to zero when calculated with pre-computed sigmaR2,sigmaT2,sigmaRT'
    return None
Example #42
0
def test_mildnonaxi_vertexdev_grid():
    # Test that for a close to axisymmetric potential, the vertex deviation is close to zero
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    vdev, grid= edf.vertexdev(0.9,phi=0.2,integrate_method='rk6_c',grid=True,
                              returnGrid=True,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(vdev) < 2., 'vertexdev of evolveddiskdf for axisymmetric potential is not close to zero' #2 is pretty big, but the weak spiral creates that
    vdev= edf.vertexdev(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                        gridpoints=_GRIDPOINTS)
    assert numpy.fabs(vdev) < 2., 'vertexdev of evolveddiskdf for axisymmetric potential is not equal zero when calculated with pre-computed grid'
    vdev= edf.vertexdev(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                        sigmaR2=_maxi_sigmar2,sigmaT2=_maxi_sigmat2,
                        sigmaRT=_maxi_sigmart,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(vdev) < 2., 'sigmart of evolveddiskdf for axisymmetric potential is not equal to zero when calculated with pre-computed sigmaR2,sigmaT2,sigmaRT'
    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/180.*numpy.pi+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., 'Cold elliptical disk does not agree with analytical calculation for vertexdev'
    return None
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 add_galaxy(self, mass, n_stars, axis, theta, tras):
     tras = np.asarray(tras)
     R = Universe.rotation_matrix(axis, theta)
     dfc = dehnendf(beta=0.)
     o = dfc.sample(n=n_stars, returnOrbit=True)
     stars = np.zeros(n_stars, dtype=self.__star_fields)
     mass = random.randint(1e2, 1e10)
     for idx in range(len(stars)):
         stars['pos'][idx, 0:2] = [o[idx].x(), o[idx].y()]
         stars['mass'][idx] = mass
         # Get velocity from angular velocity of sample
         tangential = np.linalg.norm(stars['pos'][idx, 0:2]) * o[idx].vT()
         unit_pos = stars['pos'][idx, 0:2] / np.linalg.norm(
             stars['pos'][idx, 0:2])
         unit_vel = np.array([-unit_pos[1], unit_pos[0]
                              ])  # 90 deg rotated counterclockwise
         stars['vel'][idx, 0:2] = unit_vel * tangential
         stars['pos'][idx] = np.dot(R, stars['pos'][idx]) + tras
     self.stars = np.append(self.stars, stars)
Example #46
0
def test_mildnonaxi_sigmat2_grid():
    # Test that for a close to axisymmetric potential, the sigmaR2 is close to the value of the initial DF
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    st2, grid= edf.sigmaT2(0.9,phi=0.2,integrate_method='rk6_c',grid=True,
                           returnGrid=True,gridpoints=_GRIDPOINTS)
    ist2= idf.sigmaT2(0.9)
    assert numpy.fabs(numpy.log(st2)-numpy.log(ist2)) < 0.025, 'sigmat2 of evolveddiskdf for axisymmetric potential is not equal to that of initial DF'
    st2= edf.sigmaT2(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                     gridpoints=_GRIDPOINTS)
    assert numpy.fabs(numpy.log(st2)-numpy.log(ist2)) < 0.025, 'sigmat2 of evolveddiskdf for axisymmetric potential is not equal to that of initial DF when calculated with pre-computed grid'
    st2= edf.sigmaT2(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                     meanvT=_maxi_meanvt,surfacemass=_maxi_surfacemass,
                     gridpoints=_GRIDPOINTS)
    assert numpy.fabs(numpy.log(st2)-numpy.log(ist2)) < 0.025, 'sigmat2 of evolveddiskdf for axisymmetric potential is not equal to that of initial DF when calculated with pre-computed grid and meanvR,surfacemass'
    global _maxi_sigmat2
    _maxi_sigmat2= st2
    return None
Example #47
0
def test_mildnonaxi_sigmart_grid():
    # Test that for a close to axisymmetric potential, the sigmaR2 is close to zero
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    srt, grid= edf.sigmaRT(0.9,phi=0.2,integrate_method='rk6_c',grid=True,
                           returnGrid=True,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(srt) < 0.01, 'sigmart of evolveddiskdf for axisymmetric potential is not equal to zero'
    srt= edf.sigmaRT(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                     gridpoints=_GRIDPOINTS)
    assert numpy.fabs(srt) < 0.01, 'sigmart of evolveddiskdf for axisymmetric potential is not equal zero when calculated with pre-computed grid'
    srt= edf.sigmaRT(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                     meanvR=_maxi_meanvr,
                     meanvT=_maxi_meanvt,surfacemass=_maxi_surfacemass,
                     gridpoints=_GRIDPOINTS)
    assert numpy.fabs(srt) < 0.01, 'sigmart of evolveddiskdf for axisymmetric potential is not equal to zero when calculated with pre-computed grid and meanvR,surfacemass'
    global _maxi_sigmart
    _maxi_sigmart= srt
    return None
def test_mildnonaxi_sigmart_grid():
    # Test that for a close to axisymmetric potential, the sigmaR2 is close to zero
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    srt, grid= edf.sigmaRT(0.9,phi=0.2,integrate_method='rk6_c',grid=True,
                           returnGrid=True,gridpoints=_GRIDPOINTS)
    assert numpy.fabs(srt) < 0.01, 'sigmart of evolveddiskdf for axisymmetric potential is not equal to zero'
    srt= edf.sigmaRT(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                     gridpoints=_GRIDPOINTS)
    assert numpy.fabs(srt) < 0.01, 'sigmart of evolveddiskdf for axisymmetric potential is not equal zero when calculated with pre-computed grid'
    srt= edf.sigmaRT(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                     meanvR=_maxi_meanvr,
                     meanvT=_maxi_meanvt,surfacemass=_maxi_surfacemass,
                     gridpoints=_GRIDPOINTS)
    assert numpy.fabs(srt) < 0.01, 'sigmart of evolveddiskdf for axisymmetric potential is not equal to zero when calculated with pre-computed grid and meanvR,surfacemass'
    global _maxi_sigmart
    _maxi_sigmart= srt
    return None
def test_mildnonaxi_sigmat2_grid():
    # Test that for a close to axisymmetric potential, the sigmaR2 is close to the value of the initial DF
    idf= dehnendf(beta=0.)
    pot= [LogarithmicHaloPotential(normalize=1.),
          SteadyLogSpiralPotential(A=-0.005,omegas=0.2)] #very mild non-axi
    edf= evolveddiskdf(idf,pot=pot,to=-10.)
    st2, grid= edf.sigmaT2(0.9,phi=0.2,integrate_method='rk6_c',grid=True,
                           returnGrid=True,gridpoints=_GRIDPOINTS)
    ist2= idf.sigmaT2(0.9)
    assert numpy.fabs(numpy.log(st2)-numpy.log(ist2)) < 0.025, 'sigmat2 of evolveddiskdf for axisymmetric potential is not equal to that of initial DF'
    st2= edf.sigmaT2(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                     gridpoints=_GRIDPOINTS)
    assert numpy.fabs(numpy.log(st2)-numpy.log(ist2)) < 0.025, 'sigmat2 of evolveddiskdf for axisymmetric potential is not equal to that of initial DF when calculated with pre-computed grid'
    st2= edf.sigmaT2(0.9,phi=0.2,integrate_method='rk6_c',grid=grid,
                     meanvT=_maxi_meanvt,surfacemass=_maxi_surfacemass,
                     gridpoints=_GRIDPOINTS)
    assert numpy.fabs(numpy.log(st2)-numpy.log(ist2)) < 0.025, 'sigmat2 of evolveddiskdf for axisymmetric potential is not equal to that of initial DF when calculated with pre-computed grid and meanvR,surfacemass'
    global _maxi_sigmat2
    _maxi_sigmat2= st2
    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 determine_vsolar_error(spiral=False):
    #Read the APOGEE-RC data and pixelate it
    #APOGEE-RC
    data= apread.rcsample()
    if _ADDLLOGGCUT:
        data= data[data['ADDL_LOGG_CUT'] == 1]
    #Cut
    indx= (numpy.fabs(data['RC_GALZ']) < 0.25)*(data['METALS'] > -1000.)
    data= data[indx]
    #Set up the Dehnen DF that we will sample from
    dfc= dehnendf(beta=0.,profileParams=(3./8.,33.3,31.4/220.))
    trueVsolar= 24./220.
    ntrials= 100
    mockvsolars= []
    for ii in range(ntrials):
        print "Working on %i / %i" % (ii+1,ntrials)
        mockvsolars.append(determine_vsolar_mock(data,dfc,trueVsolar,spiral))
    mockvsolars= numpy.array(mockvsolars)
    print mockvsolars
    print numpy.mean(mockvsolars-24.), numpy.std(mockvsolars)
    print numpy.sum(numpy.fabs(mockvsolars-24.) > 1.)/float(ntrials)
    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_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
Example #54
0
def test_diskdf():
    from galpy.df import dehnendf
    # Init. dehnendf w/ flat rot., hr=1/3, hs=1, and sr(1)=0.2
    df = dehnendf(beta=0., profileParams=(1. / 3., 1.0, 0.2))
    # Same, w/ correction factors to scale profiles
    dfc = dehnendf(beta=0.,
                   profileParams=(1. / 3., 1.0, 0.2),
                   correct=True,
                   niter=20)
    if True:
        # Log. diff. between scale and DF surf. dens.
        numpy.log(df.surfacemass(0.5) / df.targetSurfacemass(0.5))
        assert numpy.fabs(
            numpy.log(df.surfacemass(0.5) / df.targetSurfacemass(0.5)) +
            0.056954077791649592
        ) < 10.**-4., 'diskdf does not behave as expected'
        # Same for corrected DF
        numpy.log(dfc.surfacemass(0.5) / dfc.targetSurfacemass(0.5))
        assert numpy.fabs(
            numpy.log(dfc.surfacemass(0.5) / dfc.targetSurfacemass(0.5)) +
            4.1440377205802041e-06
        ) < 10.**-4., 'diskdf does not behave as expected'
        # Log. diff between scale and DF sr
        numpy.log(df.sigmaR2(0.5) / df.targetSigma2(0.5))
        assert numpy.fabs(
            numpy.log(df.sigmaR2(0.5) / df.targetSigma2(0.5)) +
            0.12786083001363127
        ) < 10.**-4., 'diskdf does not behave as expected'
        # Same for corrected DF
        numpy.log(dfc.sigmaR2(0.5) / dfc.targetSigma2(0.5))
        assert numpy.fabs(
            numpy.log(dfc.sigmaR2(0.5) / dfc.targetSigma2(0.5)) +
            6.8065001252214986e-06
        ) < 10.**-4., 'diskdf does not behave as expected'
        # Evaluate DF w/ R,vR,vT
        df(numpy.array([0.9, 0.1, 0.8]))
        assert numpy.fabs(
            df(numpy.array([0.9, 0.1, 0.8])) - numpy.array(0.1740247246180417)
        ) < 10.**-4., 'diskdf does not behave as expected'
        # Evaluate corrected DF w/ Orbit instance
        from galpy.orbit import Orbit
        dfc(Orbit([0.9, 0.1, 0.8]))
        assert numpy.fabs(
            dfc(Orbit([0.9, 0.1, 0.8])) - numpy.array(0.16834863725552207)
        ) < 10.**-4., 'diskdf does not behave as expected'
        # Calculate the mean velocities
        df.meanvR(0.9), df.meanvT(0.9)
        assert numpy.fabs(
            df.meanvR(0.9)) < 10.**-4., 'diskdf does not behave as expected'
        assert numpy.fabs(df.meanvT(0.9) - 0.91144428051168291
                          ) < 10.**-4., 'diskdf does not behave as expected'
        # Calculate the velocity dispersions
        numpy.sqrt(dfc.sigmaR2(0.9)), numpy.sqrt(dfc.sigmaT2(0.9))
        assert numpy.fabs(numpy.sqrt(dfc.sigmaR2(0.9)) - 0.22103383792719539
                          ) < 10.**-4., 'diskdf does not behave as expected'
        assert numpy.fabs(numpy.sqrt(dfc.sigmaT2(0.9)) - 0.17613725303902811
                          ) < 10.**-4., 'diskdf does not behave as expected'
        # Calculate the skew of the velocity distribution
        df.skewvR(0.9), df.skewvT(0.9)
        assert numpy.fabs(
            df.skewvR(0.9)) < 10.**-4., 'diskdf does not behave as expected'
        assert numpy.fabs(df.skewvT(0.9) + 0.47331638366025863
                          ) < 10.**-4., 'diskdf does not behave as expected'
        # Calculate the kurtosis of the velocity distribution
        df.kurtosisvR(0.9), df.kurtosisvT(0.9)
        assert numpy.fabs(df.kurtosisvR(0.9) + 0.13561300880237059
                          ) < 10.**-4., 'diskdf does not behave as expected'
        assert numpy.fabs(df.kurtosisvT(0.9) - 0.12612702099300721
                          ) < 10.**-4., 'diskdf does not behave as expected'
        # Calculate a higher-order moment of the velocity DF
        df.vmomentsurfacemass(1., 6., 2.) / df.surfacemass(1.)
        assert numpy.fabs(
            df.vmomentsurfacemass(1., 6., 2.) / df.surfacemass(1.) -
            0.00048953492205559054
        ) < 10.**-4., 'diskdf does not behave as expected'
        # Calculate the Oort functions
        dfc.oortA(1.), dfc.oortB(1.), dfc.oortC(1.), dfc.oortK(1.)
        assert numpy.fabs(dfc.oortA(1.) - 0.40958989067012197
                          ) < 10.**-4., 'diskdf does not behave as expected'
        assert numpy.fabs(dfc.oortB(1.) + 0.49396172114486514
                          ) < 10.**-4., 'diskdf does not behave as expected'
        assert numpy.fabs(
            dfc.oortC(1.)) < 10.**-4., 'diskdf does not behave as expected'
        assert numpy.fabs(
            dfc.oortK(1.)) < 10.**-4., 'diskdf does not behave as expected'
    # Sample Orbits from the DF, returns list of Orbits
    numpy.random.seed(1)
    os = dfc.sample(n=100, returnOrbit=True, nphi=1)
    # check that these have the right mean radius = 2hr=2/3
    rs = numpy.array([o.R() for o in os])
    assert numpy.fabs(numpy.mean(rs) - 2. / 3.) < 0.1
    # Sample vR and vT at given R, check their mean
    vrvt = dfc.sampleVRVT(0.7, n=500, target=True)
    vt = vrvt[:, 1]
    assert numpy.fabs(numpy.mean(vrvt[:, 0])) < 0.05
    assert numpy.fabs(numpy.mean(vt) - dfc.meanvT(0.7)) < 0.01
    # Sample Orbits along a given line-of-sight
    os = dfc.sampleLOS(45., n=1000)
    return None
Example #55
0
    elif dmaxs:
        pred_file.close()
        pred_file= os.path.join(os.getenv('DATADIR'),'bovy','vclos',
                                'l_vhelio_dmax_0.625000.sav')
        pred_file= open(pred_file,'rb')
        ls= pickle.load(pred_file)
        avg_pred2= pickle.load(pred_file)
        pred_file.close()
        pred_file= os.path.join(os.getenv('DATADIR'),'bovy','vclos',
                                'l_vhelio_dmax_1.875000.sav')
        pred_file= open(pred_file,'rb')
        ls= pickle.load(pred_file)
        avg_pred3= pickle.load(pred_file)
    pred_file.close()
else:
    dfc= dehnendf(beta=0.,correct=True,niter=20)
    if betas:
        dfc2= dehnendf(beta=0.1,correct=True,niter=20)
        dfc3= dehnendf(beta=-0.1,correct=True,niter=20)
    nds= 51
    nls= 101
    ds= numpy.linspace(0.,10./8.,nds)
    ls= numpy.linspace(0.,360.,nls)
    avg_pred= numpy.zeros(nls)
    if betas:
        avg_pred2= numpy.zeros(nls)
        avg_pred3= numpy.zeros(nls)
    for ii in range(nls):
        meanvlos= 0.
        norm= 0.
        if betas:
Example #56
0
from amuse.lab import *
from galpy.df import quasiisothermaldf, dehnendf
from galpy.potential import MWPotential2014, to_amuse
from galpy.util import bovy_conversion
from galpy.actionAngle import actionAngleStaeckel

import numpy as np

aAS = actionAngleStaeckel(pot=MWPotential2014, delta=0.45, c=True)
qdfS = quasiisothermaldf(1./3., 0.2, 0.1, 1., 1., pot=MWPotential2014, aA=aAS, cutcounter=True)
dfc = dehnendf(beta=0.)

'''
sample from Dehnen distribution function to get cylindrical coordinates
where |r| <= 1.0 kpc
'''

nsamples = 4000
o = dfc.sample(n=nsamples, returnOrbit=True, nphi=1, rrange=[0.0, 1.0])

rvals_spherical = [ e.R() for e in o ]
thetavals_spherical = [ np.arccos(1 - 2*np.random.random()) for i in range(nsamples) ]

rvals_cylindrical = [ rvals_spherical[i]*np.sin(thetavals_spherical[i]) for i in range(nsamples) ]
phivals_cylindrical = 2*np.pi * np.random.random(nsamples)
zvals_cylindrical = [ rvals_spherical[i]*np.cos(thetavals_spherical[i]) for i in range(nsamples) ]

    
#using Staeckel
aAS = actionAngleStaeckel(pot=MWPotential2014, delta=0.45, c=True)
qdfS = quasiisothermaldf(1./3., 0.2, 0.1, 1., 1., pot=MWPotential2014, aA=aAS, cutcounter=True)
Example #57
0
#Quick def
def safe_dl_to_rphi(d,l):
    R= math.sqrt(1.+d**2.-2.*d*math.cos(l))
    if R == 0.:
        R= 0.0001
        d+= 0.0001
    if 1./math.cos(l) < d and math.cos(l) > 0.:
        theta= math.pi-math.asin(d/R*math.sin(l))
    else:
        theta= math.asin(d/R*math.sin(l))
    return (R,theta,d,l)

if __name__ == '__main__':
    nds= 101
    ds= numpy.linspace(0.001,8./8.,nds)
    dfc= dehnendf(profileParams=(3./8.,2.,36./218.),beta=0.,correct=False)
    if False:
        #First calculate mean
        mean_savefilename= 'vhelio_l90.sav'
        if os.path.exists(mean_savefilename):
            savefile= open(mean_savefilename,'rb')
            plotds= pickle.load(savefile)
            plotvlos= pickle.load(savefile)
            savefile.close()
        else:
            vlos= numpy.zeros(nds)
            for jj in range(nds):
                print jj
                d,l= ds[jj], 90./180.*numpy.pi
                R,theta,d,l= safe_dl_to_rphi(d,l)
                vlos[jj]= dfc.meanvT(R)*math.sin(theta+l)