def test_actionAngleTorus_basic():
    from galpy.actionAngle import actionAngleTorus
    from galpy.potential import MWPotential, rl, vcirc, \
        FlattenedPowerPotential, PlummerPotential
    tol= -4.
    jr= 10.**-10.
    jz= 10.**-10.
    aAT= actionAngleTorus(pot=MWPotential)
    # at R=1, Lz=1
    jphi= 1.
    angler= numpy.linspace(0.,2.*numpy.pi,101)
    anglephi= numpy.linspace(0.,2.*numpy.pi,101)+1.
    anglez= numpy.linspace(0.,2.*numpy.pi,101)+2.
    RvR= aAT(jr,jphi,jz,angler,anglephi,anglez).T
    assert numpy.all(numpy.fabs(RvR[0]-rl(MWPotential,jphi)) < 10.**tol), \
        'circular orbit does not have constant radius for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[1]) < 10.**tol), \
        'circular orbit does not have zero radial velocity for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[2]-vcirc(MWPotential,rl(MWPotential,jphi))) < 10.**tol), \
        'circular orbit does not have constant vT=vc for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[3]) < 10.**tol), \
        'circular orbit does not have zero vertical height for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[4]) < 10.**tol), \
        'circular orbit does not have zero vertical velocity for actionAngleTorus'
    # at Lz=1.5, using Plummer
    tol= -3.25
    pp= PlummerPotential(normalize=1.)
    aAT= actionAngleTorus(pot=pp)
    jphi= 1.5
    RvR= aAT(jr,jphi,jz,angler,anglephi,anglez).T
    assert numpy.all(numpy.fabs(RvR[0]-rl(pp,jphi)) < 10.**tol), \
        'circular orbit does not have constant radius for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[1]) < 10.**tol), \
        'circular orbit does not have zero radial velocity for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[2]-vcirc(pp,rl(pp,jphi))) < 10.**tol), \
        'circular orbit does not have constant vT=vc for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[3]) < 10.**tol), \
        'circular orbit does not have zero vertical height for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[4]) < 10.**tol), \
        'circular orbit does not have zero vertical velocity for actionAngleTorus'
    # at Lz=0.5, using FlattenedPowerPotential
    tol= -4.
    fp= FlattenedPowerPotential(normalize=1.)
    aAT= actionAngleTorus(pot=fp)
    jphi= 0.5
    RvR= aAT(jr,jphi,jz,angler,anglephi,anglez).T
    assert numpy.all(numpy.fabs(RvR[0]-rl(fp,jphi)) < 10.**tol), \
        'circular orbit does not have constant radius for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[1]) < 10.**tol), \
        'circular orbit does not have zero radial velocity for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[2]-vcirc(fp,rl(fp,jphi))) < 10.**tol), \
        'circular orbit does not have constant vT=vc for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[3]) < 10.**tol), \
        'circular orbit does not have zero vertical height for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[4]) < 10.**tol), \
        'circular orbit does not have zero vertical velocity for actionAngleTorus'
    return None
def test_actionAngleTorus_basic():
    from galpy.actionAngle import actionAngleTorus
    from galpy.potential import MWPotential, rl, vcirc, \
        FlattenedPowerPotential, PlummerPotential
    tol= -4.
    jr= 10.**-10.
    jz= 10.**-10.
    aAT= actionAngleTorus(pot=MWPotential)
    # at R=1, Lz=1
    jphi= 1.
    angler= numpy.linspace(0.,2.*numpy.pi,101)
    anglephi= numpy.linspace(0.,2.*numpy.pi,101)+1.
    anglez= numpy.linspace(0.,2.*numpy.pi,101)+2.
    RvR= aAT(jr,jphi,jz,angler,anglephi,anglez).T
    assert numpy.all(numpy.fabs(RvR[0]-rl(MWPotential,jphi)) < 10.**tol), \
        'circular orbit does not have constant radius for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[1]) < 10.**tol), \
        'circular orbit does not have zero radial velocity for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[2]-vcirc(MWPotential,rl(MWPotential,jphi))) < 10.**tol), \
        'circular orbit does not have constant vT=vc for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[3]) < 10.**tol), \
        'circular orbit does not have zero vertical height for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[4]) < 10.**tol), \
        'circular orbit does not have zero vertical velocity for actionAngleTorus'
    # at Lz=1.5, using Plummer
    tol= -3.25
    pp= PlummerPotential(normalize=1.)
    aAT= actionAngleTorus(pot=pp)
    jphi= 1.5
    RvR= aAT(jr,jphi,jz,angler,anglephi,anglez).T
    assert numpy.all(numpy.fabs(RvR[0]-rl(pp,jphi)) < 10.**tol), \
        'circular orbit does not have constant radius for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[1]) < 10.**tol), \
        'circular orbit does not have zero radial velocity for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[2]-vcirc(pp,rl(pp,jphi))) < 10.**tol), \
        'circular orbit does not have constant vT=vc for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[3]) < 10.**tol), \
        'circular orbit does not have zero vertical height for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[4]) < 10.**tol), \
        'circular orbit does not have zero vertical velocity for actionAngleTorus'
    # at Lz=0.5, using FlattenedPowerPotential
    tol= -4.
    fp= FlattenedPowerPotential(normalize=1.)
    aAT= actionAngleTorus(pot=fp)
    jphi= 0.5
    RvR= aAT(jr,jphi,jz,angler,anglephi,anglez).T
    assert numpy.all(numpy.fabs(RvR[0]-rl(fp,jphi)) < 10.**tol), \
        'circular orbit does not have constant radius for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[1]) < 10.**tol), \
        'circular orbit does not have zero radial velocity for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[2]-vcirc(fp,rl(fp,jphi))) < 10.**tol), \
        'circular orbit does not have constant vT=vc for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[3]) < 10.**tol), \
        'circular orbit does not have zero vertical height for actionAngleTorus'
    assert numpy.all(numpy.fabs(RvR[4]) < 10.**tol), \
        'circular orbit does not have zero vertical velocity for actionAngleTorus'
    return None
 def vcirc(self,R):
     from galpy.potential import vcirc
     if self._interpvcirc:
         indx= (R >= self._rgrid[0])*(R <= self._rgrid[-1])
         out= numpy.empty_like(R)
         if numpy.sum(indx) > 0:
             if self._logR:
                 out[indx]= self._vcircInterp(numpy.log(R[indx]))
             else:
                 out[indx]= self._vcircInterp(R[indx])
         if numpy.sum(True^indx) > 0:
             out[True^indx]= vcirc(self._origPot,R[True^indx])
         return out
     else:
         return vcirc(self._origPot,R)
Exemple #4
0
 def circular_velocity(self, r):
     vcirc = potential.vcirc(self.pot,
                             r.value_in(units.kpc) / self.ro,
                             phi=0,
                             ro=self.ro,
                             vo=self.vo) | units.kms
     return vcirc
Exemple #5
0
    def galactic_velocity(self, gal, interpolants, fixed_potential=None):
        """
        Calculates the pre-SN galactic velocity for the tracer particles at their initial radius R. 
        """

        print('Calculating the pre-SN galactic velocity...\n')

        # --- Using galpy's vcirc method, we can easily calculate the rotation velocity at any R

        R_vals = self.R

        Vcircs = []
        for idx, (t0, R) in enumerate(zip(self.t0, R_vals)):
            # if fixed_potential, we take the potential at the specified timestep only
            if fixed_potential:
                t0_pot = fixed_potential
            else:
                t0_pot = t0

            # if interpolants are provided, use these for the calculation
            if interpolants:
                potential = interpolants[t0_pot]
            else:
                potential = gal.full_potentials[t0_pot]

            Vcircs.append(vcirc(potential, R).value)

        self.Vcirc = np.asarray(Vcircs) * u.km / u.s
def estimateBIsochrone(R,z,pot=None):
    """
    NAME:
       estimateBIsochrone
    PURPOSE:
       Estimate a good value for the scale of the isochrone potential by matching the slope of the rotation curve
    INPUT:
       R,z = coordinates (if these are arrays, the median estimated delta is returned, i.e., if this is an orbit)
       pot= Potential instance or list thereof
    OUTPUT:
       b if 1 R,Z given
       bmin,bmedian,bmax if multiple R given       
    HISTORY:
       2013-09-12 - Written - Bovy (IAS)
    """
    if pot is None: #pragma: no cover
        raise IOError("pot= needs to be set to a Potential instance or list thereof")
    if isinstance(R,nu.ndarray):
        bs= nu.array([estimateBIsochrone(R[ii],z[ii],pot=pot) for ii in range(len(R))])
        return (nu.amin(bs[True-nu.isnan(bs)]),
                nu.median(bs[True-nu.isnan(bs)]),
                nu.amax(bs[True-nu.isnan(bs)]))
    else:
        r2= R**2.+z**2
        r= math.sqrt(r2)
        dlvcdlr= dvcircdR(pot,r)/vcirc(pot,r)*r
        try:
            b= optimize.brentq(lambda x: dlvcdlr-(x/math.sqrt(r2+x**2.)-0.5*r2/(r2+x**2.)),
                               0.01,100.)
        except: #pragma: no cover
            b= nu.nan
        return b
Exemple #7
0
def sample_galpy_potential(pot,n,rmin,rmax,ro=8.,vo=220.,coordinates='cartesian'):
    ran=np.random.rand(n)
    rad=np.linspace(rmin,rmax,n)
    
    try:
        menc=pot.mass(rad/ro,z=0,t=0,forceint=False)
    except:
        vc= potential.vcirc(pot,rad/ro,phi=0,t=0.,ro=ro,vo=vo,use_physical=False)
        menc=vc**2.*(rad/ro)

    menc*=bovy_conversion.mass_in_msol(ro=ro,vo=vo)       
    
    r=np.interp(ran, menc/menc[-1], rad)
    phi=2.0*np.pi*np.random.rand(n)
    theta=np.arccos(1.0-2.0*np.random.rand(n))
    
    x=r*np.sin(theta)*np.cos(phi)
    y=r*np.sin(theta)*np.sin(phi)
    z=r*np.cos(theta)
    
    sigma_v_1d=vo*potential.vcirc(pot,rad/ro,phi=0,t=0.,ro=ro,vo=vo,use_physical=False)/np.sqrt(3.)

    vx=np.random.normal(0.,sigma_v_1d,n)        
    vy=np.random.normal(0.,sigma_v_1d,n)        
    vz=np.random.normal(0.,sigma_v_1d,n) 
    
    if coordinates=='spherical':
        vr = (vx * np.sin(theta) * np.cos(phi)
            + vy * np.sin(theta) * np.sin(phi)
            + vz * np.cos(theta)
        )
        vtheta = (
            vx * np.cos(theta) * np.cos(phi)
            + vy * np.cos(theta) * np.sin(phi)
            - vz * np.sin(theta)
        )
        vphi = vx * -np.sin(phi) + vy * np.cos(phi)
        
        x,y,z=r,phi,theta
        vx,vy,vz=vr,vphi,vtheta
        
    elif coordinates=='cylindrical':
        x,y,z=bovy_coords.rect_to_cyl(x,y,z)
        vx,vy,vz=bovy_coords.rect_to_cyl_vec(vx,vy,vz,x,y,z,True)
    
    return x,y,z,vx,vy,vz
Exemple #8
0
 def vcirc(self,R):
     if self._interpvcirc:
         if self._logR:
             return self._vcircInterp(numpy.log(R))
         else:
             return self._vcircInterp(R)
     else:
         from galpy.potential import vcirc
         return vcirc(self._origPot,R)
Exemple #9
0
 def enclosed_mass(self, r):
     grav = 4.302e-6  #kpc (km/s)^2/Msun
     vc2 = potential.vcirc(self.pot,
                           r.value_in(units.kpc) / self.ro,
                           phi=0,
                           t=self.tgalpy,
                           ro=self.ro,
                           vo=self.vo)**2.
     menc = vc2 * r.value_in(units.kpc) / grav | units.MSun
     return menc
Exemple #10
0
def estimateBIsochrone(pot,R,z,phi=None):
    """
    NAME:

       estimateBIsochrone

    PURPOSE:

       Estimate a good value for the scale of the isochrone potential by matching the slope of the rotation curve

    INPUT:

       pot- Potential instance or list thereof

       R,z - coordinates (if these are arrays, the median estimated delta is returned, i.e., if this is an orbit)

       phi= (None) azimuth to use for non-axisymmetric potentials (array if R and z are arrays)

    OUTPUT:

       b if 1 R,Z given

       bmin,bmedian,bmax if multiple R given       

    HISTORY:

       2013-09-12 - Written - Bovy (IAS)

       2016-02-20 - Changed input order to allow physical conversions - Bovy (UofT)

       2016-06-28 - Added phi= keyword for non-axisymmetric potential - Bovy (UofT)

    """
    if pot is None: #pragma: no cover
        raise IOError("pot= needs to be set to a Potential instance or list thereof")
    if isinstance(R,nu.ndarray):
        if phi is None: phi= [None for r in R]
        bs= nu.array([estimateBIsochrone(pot,R[ii],z[ii],phi=phi[ii],
                                         use_physical=False)
                      for ii in range(len(R))])
        return nu.array([nu.amin(bs[True^nu.isnan(bs)]),
                         nu.median(bs[True^nu.isnan(bs)]),
                         nu.amax(bs[True^nu.isnan(bs)])])
    else:
        r2= R**2.+z**2
        r= math.sqrt(r2)
        dlvcdlr= dvcircdR(pot,r,phi=phi,use_physical=False)/vcirc(pot,r,phi=phi,use_physical=False)*r
        try:
            b= optimize.brentq(lambda x: dlvcdlr-(x/math.sqrt(r2+x**2.)-0.5*r2/(r2+x**2.)),
                               0.01,100.)
        except: #pragma: no cover
            b= nu.nan
        return b
def estimateBIsochrone(pot,R,z,phi=None):
    """
    NAME:

       estimateBIsochrone

    PURPOSE:

       Estimate a good value for the scale of the isochrone potential by matching the slope of the rotation curve

    INPUT:

       pot- Potential instance or list thereof

       R,z - coordinates (if these are arrays, the median estimated delta is returned, i.e., if this is an orbit)

       phi= (None) azimuth to use for non-axisymmetric potentials (array if R and z are arrays)

    OUTPUT:

       b if 1 R,Z given

       bmin,bmedian,bmax if multiple R given       

    HISTORY:

       2013-09-12 - Written - Bovy (IAS)

       2016-02-20 - Changed input order to allow physical conversions - Bovy (UofT)

       2016-06-28 - Added phi= keyword for non-axisymmetric potential - Bovy (UofT)

    """
    if pot is None: #pragma: no cover
        raise IOError("pot= needs to be set to a Potential instance or list thereof")
    if isinstance(R,nu.ndarray):
        if phi is None: phi= [None for r in R]
        bs= nu.array([estimateBIsochrone(pot,R[ii],z[ii],phi=phi[ii],
                                         use_physical=False)
                      for ii in range(len(R))])
        return nu.array([nu.amin(bs[True-nu.isnan(bs)]),
                         nu.median(bs[True-nu.isnan(bs)]),
                         nu.amax(bs[True-nu.isnan(bs)])])
    else:
        r2= R**2.+z**2
        r= math.sqrt(r2)
        dlvcdlr= dvcircdR(pot,r,phi=phi,use_physical=False)/vcirc(pot,r,phi=phi,use_physical=False)*r
        try:
            b= optimize.brentq(lambda x: dlvcdlr-(x/math.sqrt(r2+x**2.)-0.5*r2/(r2+x**2.)),
                               0.01,100.)
        except: #pragma: no cover
            b= nu.nan
        return b
Exemple #12
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
Exemple #13
0
def VpratioMWBH(Rpkpc):
    #MWPotential2014.append(KeplerPotential(amp=4*10**6./bovy_conversion.mass_in_msol(par.Vs,par.Rskpc)))
    #a = vcirc(MWPotential2014,Rpkpc/par.Rskpc)
    '''
    bp= PowerSphericalPotentialwCutoff(alpha=1.8,rc=1.9/8.,normalize=0.05)
    mp= MiyamotoNagaiPotential(a=3./8.,b=0.28/8.,normalize=.6)
    np= NFWPotential(a=16./8.,normalize=.35)
    kp = KeplerPotential(amp=4*10**6./bovy_conversion.mass_in_msol(par.Vs,par.Rskpc))
    MWBH = [bp,mp,np,kp]
    a = vcirc(MWBH,Rpkpc/par.Rskpc)
    '''
    MWPotential2014wBH = [
        MWPotential2014,
        KeplerPotential(amp=4 * 10**6. /
                        bovy_conversion.mass_in_msol(par.Vs, par.Rskpc))
    ]
    a = vcirc(MWPotential2014wBH, Rpkpc / par.Rskpc)
    return a
Exemple #14
0
def kuepper_flattening_post(params: Sequence[float], qmean: float,
                            qerr: float) -> float:
    """A Kuepper et al. (2015) - flattened force posterior.

    Consists solely of the priors and a constraint on q_Phi.

    Parameters
    ----------
    params : list
        (3,) list. The Mhalo, a, qNFW
    qmean : float
    qerr : float

    Returns
    -------
    float

    """
    Mhalo, a, qNFW = params

    if Mhalo < 0.001 or Mhalo > 10.0:
        return -1e18
    elif a < 0.1 or a > 100.0:
        return -1e18
    elif qNFW < 0.2 or qNFW > 1.8:
        return -1e18

    pot: list = setup_potential_kuepper(Mhalo, a)
    vcpred = potential.vcirc(pot, 8.0 * u.kpc)

    if vcpred < 200.0 or vcpred > 280.0:
        return -1e18

    FR, FZ = force_pal5_kuepper(pot, qNFW)
    qpred = np.sqrt(2.0 * FR / FZ)

    post: float = -0.5 * np.square((qpred - qmean) / qerr)

    return post
Exemple #15
0
def calcDFResults(options,args,boot=True,nomedian=False):
    if len(args) == 2 and options.sample == 'gk':
        toptions= copy.copy(options)
        toptions.sample= 'g'
        toptions.select= 'all'
        outg= calcDFResults(toptions,[args[0]],boot=boot,nomedian=True)
        toptions.sample= 'k'
        toptions.select= 'program'
        outk= calcDFResults(toptions,[args[1]],boot=boot,nomedian=True)
        #Combine
        out= {}
        for k in outg.keys():
            valg= outg[k]
            valk= outk[k]
            val= numpy.zeros(len(valg)+len(valk))
            val[0:len(valg)]= valg
            val[len(valg):len(valg)+len(valk)]= valk
            out[k]= val
        if nomedian: return out
        else: return add_median(out,boot=boot)
    raw= read_rawdata(options)
    #Bin the data
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    tightbinned= binned
    #Map the bins with ndata > minndata in 1D
    fehs, afes= [], []
    counter= 0
    abindx= numpy.zeros((len(binned.fehedges)-1,len(binned.afeedges)-1),
                        dtype='int')
    for ii in range(len(binned.fehedges)-1):
        for jj in range(len(binned.afeedges)-1):
            data= binned(binned.feh(ii),binned.afe(jj))
            if len(data) < options.minndata:
                continue
            #print binned.feh(ii), binned.afe(jj), len(data)
            fehs.append(binned.feh(ii))
            afes.append(binned.afe(jj))
            abindx[ii,jj]= counter
            counter+= 1
    nabundancebins= len(fehs)
    fehs= numpy.array(fehs)
    afes= numpy.array(afes)
    #Load each of the solutions
    sols= []
    chi2s= []
    savename= args[0]
    initname= options.init
    for ii in range(nabundancebins):
        spl= savename.split('.')
        newname= ''
        for jj in range(len(spl)-1):
            newname+= spl[jj]
            if not jj == len(spl)-2: newname+= '.'
        newname+= '_%i.' % ii
        newname+= spl[-1]
        savefilename= newname
        #Read savefile
        try:
            savefile= open(savefilename,'rb')
        except IOError:
            print "WARNING: MISSING ABUNDANCE BIN"
            sols.append(None)
            chi2s.append(None)
        else:
            sols.append(pickle.load(savefile))
            chi2s.append(pickle.load(savefile))
            savefile.close()
        #Load samples as well
        if options.mcsample:
            #Do the same for init
            spl= initname.split('.')
            newname= ''
            for jj in range(len(spl)-1):
                newname+= spl[jj]
                if not jj == len(spl)-2: newname+= '.'
            newname+= '_%i.' % ii
            newname+= spl[-1]
            options.init= newname
    mapfehs= monoAbundanceMW.fehs()
    mapafes= monoAbundanceMW.afes()
    #Now plot
    #Run through the pixels and gather
    fehs= []
    afes= []
    ndatas= []
    zmedians= []
    #Basic parameters
    hrs= []
    srs= []
    szs= []
    hsrs= []
    hszs= []
    outfracs= []
    rds= []
    rdexps= []
    vcs= []
    zhs= []
    zhexps= []
    dlnvcdlnrs= []
    plhalos= []
    rorss= []
    dvts= []
    #derived parameters
    surfzs= []
    surfz800s= []
    surfzdisks= []
    massdisks= []
    rhoos= []
    rhooalts= []
    rhodms= []
    vcdvcros= []
    vcdvcs= []
    vescs= []
    mloglikemins= []
    for ii in range(tightbinned.npixfeh()):
        for jj in range(tightbinned.npixafe()):
            data= binned(tightbinned.feh(ii),tightbinned.afe(jj))
            if len(data) < options.minndata:
                continue
            #Find abundance indx
            fehindx= binned.fehindx(tightbinned.feh(ii))#Map onto regular binning
            afeindx= binned.afeindx(tightbinned.afe(jj))
            solindx= abindx[fehindx,afeindx]
            monoabindx= numpy.argmin((tightbinned.feh(ii)-mapfehs)**2./0.01 \
                                         +(tightbinned.afe(jj)-mapafes)**2./0.0025)
            if sols[solindx] is None:
                continue
            try:
                pot= setup_potential(sols[solindx],options,1,interpDens=True,
                                     interpdvcircdr=True,returnrawpot=True)
            except RuntimeError:
                print "A bin has an unphysical potential ..."
                continue
#            if 'dpdisk' in options.potential.lower():
#                try:
#                    rawpot= setup_potential(sols[solindx],options,1,
#                                            returnrawpot=True)
#                except RuntimeError:
#                    print "A bin has an unphysical potential ..."
#                    continue
            fehs.append(tightbinned.feh(ii))
            afes.append(tightbinned.afe(jj))
            zmedians.append(numpy.median(numpy.fabs(data.zc+_ZSUN)))
            #vc
            s= get_potparams(sols[solindx],options,1)
            ro= get_ro(sols[solindx],options)
            if options.fixvo:
                vcs.append(options.fixvo*_REFV0)
            else:
                vcs.append(s[1]*_REFV0)
            #rd
            rds.append(numpy.exp(s[0]))
            #zh
            zhs.append(numpy.exp(s[2-(1-(options.fixvo is None))]))
            #rdexp & zhexp
            if options.sample == 'g': tz= 1.1/_REFR0/ro
            elif options.sample == 'k': tz= 0.84/_REFR0/ro
            if 'mpdisk' in options.potential.lower() or 'mwpotential' in options.potential.lower():
                mp= potential.MiyamotoNagaiPotential(a=rds[-1],b=zhs[-1])
                #rdexp
                f= mp.dens(1.,0.125)
                dr= 10.**-3.
                df= (mp.dens(1.+dr/2.,0.125)-mp.dens(1.-dr/2.,0.125))/dr
                rdexps.append(-f/df)
                #zhexp
                f= mp.dens(1.,tz)
                dz= 10.**-3.
                df= (mp.dens(1.,tz+dz/2.)-mp.dens(1.,tz-dz/2.))/dz
                zhexps.append(-f/df)
            elif 'dpdisk' in options.potential.lower():
                rdexps.append(numpy.exp(s[0]))
                zhexps.append(numpy.exp(s[2-(1-(options.fixvo is None))]))
            #ndata
            ndatas.append(len(data))
            #hr
            dfparams= get_dfparams(sols[solindx],0,options)
            if options.relative:
                thishr= monoAbundanceMW.hr(mapfehs[monoabindx],mapafes[monoabindx])
                hrs.append(dfparams[0]*_REFR0/thishr)
            else:
                hrs.append(dfparams[0]*_REFR0)
            #sz
            if options.relative:
                thissz= monoAbundanceMW.sigmaz(mapfehs[monoabindx],mapafes[monoabindx])
                szs.append(dfparams[2]*_REFV0/thissz)
            else:
                szs.append(dfparams[2]*_REFV0)
            #sr
            if options.relative:
                thissr= monoAbundanceMW.sigmaz(mapfehs[monoabindx],mapafes[monoabindx])*2.#BOVY: UPDATE
                srs.append(dfparams[1]*_REFV0/thissr)
            else:
                srs.append(dfparams[1]*_REFV0)
            #hsr
            hsrs.append(dfparams[3]*_REFR0)
            #hsz
            hszs.append(dfparams[4]*_REFR0)
            #outfrac
            outfracs.append(dfparams[5])
            #rhodm
            #Setup potential
            vo= get_vo(sols[solindx],options,1)
            if 'mwpotential' in options.potential.lower():
                rhodms.append(pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.)
            elif options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                rhodms.append(pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.)
            elif options.potential.lower() == 'mpdiskflplhalofixplfixbulgeflat':
                rhodms.append(pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.)
            elif options.potential.lower() == 'dpdiskplhalofixbulgeflat' \
                    or options.potential.lower() == 'dpdiskplhalofixbulgeflatwgas' \
                    or options.potential.lower() == 'dpdiskplhalofixbulgeflatwgasalt' \
                    or options.potential.lower() == 'dpdiskflplhalofixbulgeflatwgas':
                rhodms.append(pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.)
            #rhoo
            rhoos.append(potential.evaluateDensities(1.,0.,pot)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.)
            #surfz
            surfzs.append(2.*integrate.quad((lambda zz: potential.evaluateDensities(1.,zz,pot)),0.,options.height/_REFR0/ro)[0]*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*_REFR0*ro)
            #surfz800
            surfz800s.append(2.*integrate.quad((lambda zz: potential.evaluateDensities(1.,zz,pot)),0.,0.8/_REFR0/ro)[0]*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*_REFR0*ro)
            #surzdisk
            if 'mpdisk' in options.potential.lower() or 'mwpotential' in options.potential.lower():
                surfzdisks.append(2.*integrate.quad((lambda zz: potential.evaluateDensities(1.,zz,pot[0])),0.,options.height/_REFR0/ro)[0]*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*_REFR0*ro)
                surfzdiskzm= 2.*integrate.quad((lambda zz: potential.evaluateDensities(1.,zz,pot[0])),0.,tz)[0]*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*_REFR0*ro
            elif 'dpdisk' in options.potential.lower():
                surfzdisks.append(2.*pot[0].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.*zhexps[-1]*ro*_REFR0*1000.)
            #rhooalt
            if options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                rhooalts.append(rhoos[-1]-pot[0].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.+surfzdiskzm/2./zhexps[-1]/ro/_REFR0/1000./(1.-numpy.exp(-tz/zhexps[-1])))
            elif options.potential.lower() == 'dpdiskplhalofixbulgeflat' \
                    or options.potential.lower() == 'dpdiskplhalofixbulgeflatwgas' \
                    or options.potential.lower() == 'dpdiskplhalofixbulgeflatwgasalt' \
                    or options.potential.lower() == 'dpdiskflplhalofixbulgeflatwgas':
                rhooalts.append(rhoos[-1])
            #massdisk
            if options.potential.lower() == 'dpdiskplhalofixbulgeflat' \
                    or options.potential.lower() == 'dpdiskplhalofixbulgeflatwgas' \
                    or options.potential.lower() == 'dpdiskplhalofixbulgeflatwgasalt' \
                    or options.potential.lower() == 'dpdiskflplhalofixbulgeflatwgas':
                rhod= pot[0].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
            else:
                rhod= surfzdiskzm/2./zhexps[-1]/ro/_REFR0/1000./(1.-numpy.exp(-tz/zhexps[-1]))
            massdisks.append(rhod*2.*zhexps[-1]*numpy.exp(1./rdexps[-1])*rdexps[-1]**2.*2.*numpy.pi*(ro*_REFR0)**3./10.)
            #plhalo
            if options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                plhalos.append(pot[1].alpha)
                plhalos.append((1.-pot[1].alpha)/(pot[1].alpha-3.))
            elif options.potential.lower() == 'dpdiskplhalofixbulgeflat' \
                    or options.potential.lower() == 'dpdiskplhalofixbulgeflatwgas' \
                    or options.potential.lower() == 'dpdiskplhalofixbulgeflatwgasalt' \
                    or options.potential.lower() == 'dpdiskflplhalofixbulgeflatwgas':
                plhalos.append(pot[1].alpha)
                rorss.append((1.-pot[1].alpha)/(pot[1].alpha-3.))
            #dlnvcdlnr
            if options.potential.lower() == 'dpdiskplhalofixbulgeflat' \
                    or options.potential.lower() == 'dpdiskplhalofixbulgeflatwgas' \
                    or options.potential.lower() == 'dpdiskplhalofixbulgeflatwgasalt' \
                    or options.potential.lower() == 'dpdiskflplhalofixbulgeflatwgas':
                dlnvcdlnrs.append(potential.dvcircdR(pot,1.))
            else:
                dlnvcdlnrs.append(potential.dvcircdR(pot,1.))
            #vcdvc
            if options.potential.lower() == 'dpdiskplhalofixbulgeflat' \
                    or options.potential.lower() == 'dpdiskplhalofixbulgeflatwgas' \
                    or options.potential.lower() == 'dpdiskplhalofixbulgeflatwgasalt' \
                    or options.potential.lower() == 'dpdiskflplhalofixbulgeflatwgas':
                vcdvcros.append(pot[0].vcirc(1.)/potential.vcirc(pot,1.))
                vcdvcs.append(pot[0].vcirc(2.2*rdexps[-1])/potential.vcirc(pot,2.2*rdexps[-1]))
            else:
                vcdvcros.append(pot[0].vcirc(1.)/potential.vcirc(pot,1.))
                vcdvcs.append(pot[0].vcirc(2.2*rdexps[-1])/potential.vcirc(pot,2.2*rdexps[-1]))
            #mloglike
            mloglikemins.append(chi2s[solindx])
            #escape velocity
            vescs.append(potential.vesc(pot,1.)*_REFV0)
            if options.fitdvt:
                dvts.append(sols[solindx][0])
    #Gather
    fehs= numpy.array(fehs)
    afes= numpy.array(afes)
    zmedians= numpy.array(zmedians)
    ndatas= numpy.array(ndatas)
    #Basic parameters
    hrs= numpy.array(hrs)
    srs= numpy.array(srs)
    szs= numpy.array(szs)
    hsrs= numpy.array(hsrs)
    hszs= numpy.array(hszs)
    outfracs= numpy.array(outfracs)
    vcs= numpy.array(vcs)
    rds= numpy.array(rds)
    zhs= numpy.array(zhs)
    rdexps= numpy.array(rdexps)
    zhexps= numpy.array(zhexps)
    dlnvcdlnrs= numpy.array(dlnvcdlnrs)
    plhalos= numpy.array(plhalos)
    rorss= numpy.array(rorss)
    if options.fitdvt:
        dvts= numpy.array(dvts)
    #derived parameters
    surfzs= numpy.array(surfzs)
    surfz800s= numpy.array(surfz800s)
    surfzdisks= numpy.array(surfzdisks)
    massdisks= numpy.array(massdisks)
    rhoos= numpy.array(rhoos)
    rhooalts= numpy.array(rhooalts)
    rhodms= numpy.array(rhodms)
    vcdvcros= numpy.array(vcdvcros)
    vcdvcs= numpy.array(vcdvcs)
    rexps= numpy.sqrt(2.)*(rds+zhs)/2.2
    mloglikemins= numpy.array(mloglikemins)
    vescs= numpy.array(vescs)
    #Load into dictionary
    out= {}
    out['feh']= fehs
    out['afe']= afes
    out['zmedian']= zmedians
    out['ndata']= ndatas
    out['hr']= hrs
    out['sr']= srs
    out['sz']= szs
    out['hsr']= hsrs
    out['hsz']= hszs
    out['outfrac']= outfracs
    out['vc']= vcs
    out['rd']= rds
    out['zh']= zhs
    out['rdexp']= rdexps
    out['zhexp']= zhexps
    out['dlnvcdlnr']= dlnvcdlnrs
    out['plhalo']= plhalos
    out['rors']= rorss
    out['surfz']= surfzs
    out['surfz800']= surfz800s
    out['surfzdisk']= surfzdisks
    out['massdisk']= massdisks
    out['rhoo']= rhoos
    out['rhooalt']= rhooalts
    out['rhodm']= rhodms
    out['vcdvc']= vcdvcs
    out['vcdvcro']= vcdvcros
    out['rexp']= rexps
    out['mloglikemin']= mloglikemins
    out['vesc']= vescs
    if options.fitdvt:
        out['dvt']= dvts
    if nomedian: return out
    else: return add_median(out,boot=boot)
Exemple #16
0
 def jmomentsurfacemass(self,
                        R,
                        z,
                        n,
                        m,
                        o,
                        nsigma=None,
                        mc=True,
                        nmc=10000,
                        _returnmc=False,
                        _vrs=None,
                        _vts=None,
                        _vzs=None,
                        **kwargs):
     """
     NAME:
        jmomentsurfacemass
     PURPOSE:
        calculate the an arbitrary moment of an action
        of the velocity distribution 
        at R times the surfacmass
     INPUT:
        R - radius at which to calculate the moment(/ro)
        n - jr^n
        m - lz^m
        o - jz^o
     OPTIONAL INPUT:
        nsigma - number of sigma to integrate the velocities over (when doing explicit numerical integral)
        mc= if True, calculate using Monte Carlo integration
        nmc= if mc, use nmc samples
     OUTPUT:
        <jr^n lz^m jz^o  x surface-mass> at R
     HISTORY:
        2012-08-09 - Written - Bovy (IAS@MPIA)
     """
     if nsigma == None:
         nsigma = _NSIGMA
     logSigmaR = (self._ro - R) / self._hr
     sigmaR1 = self._sr * numpy.exp((self._ro - R) / self._hsr)
     sigmaz1 = self._sz * numpy.exp((self._ro - R) / self._hsz)
     thisvc = potential.vcirc(self._pot, R)
     #Use the asymmetric drift equation to estimate va
     gamma = numpy.sqrt(0.5)
     va= sigmaR1**2./2./thisvc\
         *(gamma**2.-1. #Assume close to flat rotation curve, sigphi2/sigR2 =~ 0.5
            +R*(1./self._hr+2./self._hsr))
     if math.fabs(va) > sigmaR1:
         va = 0.  #To avoid craziness near the center
     if mc:
         mvT = (thisvc - va) / gamma / sigmaR1
         if _vrs is None:
             vrs = numpy.random.normal(size=nmc)
         else:
             vrs = _vrs
         if _vts is None:
             vts = numpy.random.normal(size=nmc) + mvT
         else:
             vts = _vts
         if _vzs is None:
             vzs = numpy.random.normal(size=nmc)
         else:
             vzs = _vzs
         Is = _jmomentsurfaceMCIntegrand(vzs, vrs, vts,
                                         nump.ones(nmc) * R,
                                         numpy.ones(nmc) * z, self, sigmaR1,
                                         gamma, sigmaz1, mvT, n, m, o)
         if _returnmc:
             return (numpy.mean(Is) * sigmaR1**2. * gamma * sigmaz1, vrs,
                     vts, vzs)
         else:
             return numpy.mean(Is) * sigmaR1**2. * gamma * sigmaz1
     else:
         return integrate.tplquad(
             _jmomentsurfaceIntegrand, 1. / gamma *
             (thisvc - va) / sigmaR1 - nsigma, 1. / gamma *
             (thisvc - va) / sigmaR1 + nsigma, lambda x: 0.,
             lambda x: nsigma, lambda x, y: 0., lambda x, y: nsigma,
             (R, z, self, sigmaR1, gamma, sigmaz1, n, m, o), **
             kwargs)[0] * sigmaR1**2. * gamma * sigmaz1
Exemple #17
0
 def __init__(self,
              hr,
              sr,
              sz,
              hsr,
              hsz,
              pot=None,
              aA=None,
              cutcounter=False,
              _precomputerg=True,
              _precomputergrmax=None,
              _precomputergnLz=51,
              ro=1.,
              lo=10. / 220. * 8.):
     """
     NAME:
        __init__
     PURPOSE:
        Initialize a quasi-isothermal DF
     INPUT:
        hr - radial scale length
        sr - radial velocity dispersion at the solar radius
        sz - vertical velocity dispersion at the solar radius
        hsr - radial-velocity-dispersion scale length
        hsz - vertial-velocity-dispersion scale length
        pot= Potential instance or list thereof
        aA= actionAngle instance used to convert (x,v) to actions
        cutcounter= if True, set counter-rotating stars' DF to zero
        ro= reference radius for surface mass and sigmas
        lo= reference angular momentum below where there are significant numbers of retrograde stars
     OTHER INPUTS:
        _precomputerg= if True (default), pre-compute the rL(L)
        _precomputergrmax= if set, this is the maximum R for which to pre-compute rg (default: 5*hr)
        _precomputergnLz if set, number of Lz to pre-compute rg for (default: 51)
     OUTPUT:
        object
     HISTORY:
        2012-07-25 - Started - Bovy (IAS@MPIA)
     """
     self._hr = hr
     self._sr = sr
     self._sz = sz
     self._hsr = hsr
     self._hsz = hsz
     self._ro = ro
     self._lo = lo
     self._lnsr = math.log(self._sr)
     self._lnsz = math.log(self._sz)
     if pot is None:
         raise IOError("pot= must be set")
     self._pot = pot
     if aA is None:
         raise IOError("aA= must be set")
     self._aA = aA
     self._cutcounter = cutcounter
     if _precomputerg:
         if _precomputergrmax is None:
             _precomputergrmax = 5 * self._hr
         self._precomputergrmax = _precomputergrmax
         self._precomputergnLz = _precomputergnLz
         self._precomputergLzmin = 0.01
         self._precomputergLzmax= self._precomputergrmax\
             *potential.vcirc(self._pot,self._precomputergrmax)
         self._precomputergLzgrid = numpy.linspace(self._precomputergLzmin,
                                                   self._precomputergLzmax,
                                                   self._precomputergnLz)
         self._rls = numpy.array(
             [potential.rl(self._pot, l) for l in self._precomputergLzgrid])
         #Spline interpolate
         self._rgInterp = interpolate.InterpolatedUnivariateSpline(
             self._precomputergLzgrid, self._rls, k=3)
     else:
         self._precomputergrmax = 0.
         self._rgInterp = None
         self._rls = None
         self._precomputergnr = None
         self._precomputergLzgrid = None
     self._precomputerg = _precomputerg
     return None
Exemple #18
0
def fct_vlsr(r, r0, v0, l, b):
    return (r0 * vcirc(MWPotential2014, r / r0, ro=r0, vo=v0) / r -
            vcirc(MWPotential2014, 1, ro=r0, vo=v0)) * np.sin(l) * np.cos(b)
cdfrho=[cdf_bulge(rr[ii]) for ii in range(len(rr))]
icdf= interpolate.InterpolatedUnivariateSpline(cdfrho,rr,k=1)

rand_r=np.empty(nr)

for jj in range(nr):
    rand_r[jj] = icdf(np.random.uniform())
    
rand_theta=np.random.uniform(0.,2.*np.pi,nr)
rand_phi = np.arccos(1.- 2.*np.random.uniform(0.,1.,nr))

R,z,theta=spherical_to_cylindrical(rand_r,rand_theta,rand_phi)

vR=np.zeros(nr)
vz=np.zeros(nr)
vT=vcirc(nobarpot,R/8.)

t_age=np.linspace(tage,0.,1001)/bovy_conversion.time_in_Gyr(vo,ro)  
coord=[]
orbits=[]
    
for ii in range(nr):
        coord.append([R[ii]/8.,vR[ii],vT[ii],z[ii]/8.,vz[ii],theta[ii]])
        orbits.append(Orbit(coord[ii])) 
        orbits[ii].turn_physical_off()
        orbits[ii].integrate(t_age,barpot)
        
tout = np.linspace(tage,0.,10)/bovy_conversion.time_in_Gyr(vo,ro) 

for kk in tout : 
        tt = round(kk*bovy_conversion.time_in_Gyr(vo,ro),1)        
Exemple #20
0
    def calcRapRperi(self,**kwargs):
        """
        NAME:
           calcRapRperi
        PURPOSE:
           calculate the apocenter and pericenter radii
        INPUT:
        OUTPUT:
           (rperi,rap)
        HISTORY:
           2010-12-01 - Written - Bovy (NYU)
        """
        if hasattr(self,'_rperirap'): #pragma: no cover
            return self._rperirap
        EL= self.calcEL(**kwargs)
        E, L= EL
        if self._vR == 0. and m.fabs(self._vT - vcirc(self._pot,self._R,use_physical=False)) < _EPS: #We are on a circular orbit
            rperi= self._R
            rap = self._R
        elif self._vR == 0. and self._vT > vcirc(self._pot,self._R,use_physical=False): #We are exactly at pericenter
            rperi= self._R
            if self._gamma != 0.:
                startsign= _rapRperiAxiEq(self._R+10.**-8.,E,L,self._pot)
                startsign/= m.fabs(startsign)
            else: startsign= 1.
            rend= _rapRperiAxiFindStart(self._R,E,L,self._pot,rap=True,
                                        startsign=startsign)
            rap= optimize.brentq(_rapRperiAxiEq,rperi+0.00001,rend,
                                 args=(E,L,self._pot))
#                                   fprime=_rapRperiAxiDeriv)
        elif self._vR == 0. and self._vT < vcirc(self._pot,self._R,use_physical=False): #We are exactly at apocenter
            rap= self._R
            if self._gamma != 0.:
                startsign= _rapRperiAxiEq(self._R-10.**-8.,E,L,self._pot)
                startsign/= m.fabs(startsign)
            else: startsign= 1.
            rstart= _rapRperiAxiFindStart(self._R,E,L,self._pot,
                                          startsign=startsign)
            if rstart == 0.: rperi= 0.
            else:
                rperi= optimize.brentq(_rapRperiAxiEq,rstart,rap-0.000001,
                                       args=(E,L,self._pot))
#                                   fprime=_rapRperiAxiDeriv)
        else:
            if self._gamma != 0.:
                startsign= _rapRperiAxiEq(self._R,E,L,self._pot)
                startsign/= m.fabs(startsign)
            else:
                startsign= 1.
            rstart= _rapRperiAxiFindStart(self._R,E,L,self._pot,
                                          startsign=startsign)
            if rstart == 0.: rperi= 0.
            else: 
                try:
                    rperi= optimize.brentq(_rapRperiAxiEq,rstart,self._R,
                                           (E,L,self._pot),
                                           maxiter=200)
                except RuntimeError: #pragma: no cover
                    raise UnboundError("Orbit seems to be unbound")
            rend= _rapRperiAxiFindStart(self._R,E,L,self._pot,rap=True,
                                        startsign=startsign)
            rap= optimize.brentq(_rapRperiAxiEq,self._R,rend,
                                 (E,L,self._pot))
        self._rperirap= (rperi,rap)
        return self._rperirap
Exemple #21
0
def plot_DFsingles(options,args):
    raw= read_rawdata(options)
    #Bin the data
    binned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe)
    if options.tighten:
        tightbinned= pixelAfeFeh(raw,dfeh=options.dfeh,dafe=options.dafe,
                                 fehmin=-1.6,fehmax=0.5,afemin=-0.05,
                                 afemax=0.55)
    else:
        tightbinned= binned
    #Map the bins with ndata > minndata in 1D
    fehs, afes= [], []
    counter= 0
    abindx= numpy.zeros((len(binned.fehedges)-1,len(binned.afeedges)-1),
                        dtype='int')
    for ii in range(len(binned.fehedges)-1):
        for jj in range(len(binned.afeedges)-1):
            data= binned(binned.feh(ii),binned.afe(jj))
            if len(data) < options.minndata:
                continue
            #print binned.feh(ii), binned.afe(jj), len(data)
            fehs.append(binned.feh(ii))
            afes.append(binned.afe(jj))
            abindx[ii,jj]= counter
            counter+= 1
    nabundancebins= len(fehs)
    fehs= numpy.array(fehs)
    afes= numpy.array(afes)
    #Load each solutions
    sols= []
    savename= args[0]
    initname= options.init
    for ii in range(nabundancebins):
        spl= savename.split('.')
        newname= ''
        for jj in range(len(spl)-1):
            newname+= spl[jj]
            if not jj == len(spl)-2: newname+= '.'
        newname+= '_%i.' % ii
        newname+= spl[-1]
        savefilename= newname
        #Read savefile
        try:
            savefile= open(savefilename,'rb')
        except IOError:
            print "WARNING: MISSING ABUNDANCE BIN"
            sols.append(None)
        else:
            sols.append(pickle.load(savefile))
            savefile.close()
        #Load samples as well
        if options.mcsample:
            #Do the same for init
            spl= initname.split('.')
            newname= ''
            for jj in range(len(spl)-1):
                newname+= spl[jj]
                if not jj == len(spl)-2: newname+= '.'
            newname+= '_%i.' % ii
            newname+= spl[-1]
            options.init= newname
    mapfehs= monoAbundanceMW.fehs()
    mapafes= monoAbundanceMW.afes()
    #Now plot
    #Run through the pixels and gather
    if options.type.lower() == 'afe' or options.type.lower() == 'feh' \
            or options.type.lower() == 'fehafe' \
            or options.type.lower() == 'afefeh':
        plotthis= []
        errors= []
    else:
        plotthis= numpy.zeros((tightbinned.npixfeh(),tightbinned.npixafe()))
    for ii in range(tightbinned.npixfeh()):
        for jj in range(tightbinned.npixafe()):
            data= binned(tightbinned.feh(ii),tightbinned.afe(jj))
            if len(data) < options.minndata:
                if options.type.lower() == 'afe' or options.type.lower() == 'feh' or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'afefeh':
                    continue
                else:
                    plotthis[ii,jj]= numpy.nan
                    continue
            #Find abundance indx
            fehindx= binned.fehindx(tightbinned.feh(ii))#Map onto regular binning
            afeindx= binned.afeindx(tightbinned.afe(jj))
            solindx= abindx[fehindx,afeindx]
            monoabindx= numpy.argmin((tightbinned.feh(ii)-mapfehs)**2./0.01 \
                                         +(tightbinned.afe(jj)-mapafes)**2./0.0025)
            if sols[solindx] is None:
                if options.type.lower() == 'afe' or options.type.lower() == 'feh' or options.type.lower() == 'fehafe' \
                        or options.type.lower() == 'afefeh':
                    continue
                else:
                    plotthis[ii,jj]= numpy.nan
                    continue
            if options.type.lower() == 'q':
                s= get_potparams(sols[solindx],options,1)
                plotthis[ii,jj]= s[0]
                if not options.flatten is None:
                    plotthis[ii,jj]/= options.flatten
            elif options.type.lower() == 'vc':
                if options.fixvo:
                    plotthis[ii,jj]= 1.
                else:
                    s= get_potparams(sols[solindx],options,1)
                    plotthis[ii,jj]= s[1]
            elif options.type.lower() == 'rd':
                s= get_potparams(sols[solindx],options,1)
                plotthis[ii,jj]= numpy.exp(s[0])
            elif options.type.lower() == 'zh':
                s= get_potparams(sols[solindx],options,1)
                plotthis[ii,jj]= numpy.exp(s[2-(1-(options.fixvo is None))])
            elif options.type.lower() == 'ndata':
                plotthis[ii,jj]= len(data)
            elif options.type.lower() == 'hr':
                s= get_dfparams(sols[solindx],0,options)
                plotthis[ii,jj]= s[0]*_REFR0
                if options.relative:
                    thishr= monoAbundanceMW.hr(mapfehs[monoabindx],mapafes[monoabindx])
                    plotthis[ii,jj]/= thishr
            elif options.type.lower() == 'sz':
                s= get_dfparams(sols[solindx],0,options)
                plotthis[ii,jj]= s[2]*_REFV0
                if options.relative:
                    thissz= monoAbundanceMW.sigmaz(mapfehs[monoabindx],mapafes[monoabindx])
                    plotthis[ii,jj]/= thissz
            elif options.type.lower() == 'sr':
                s= get_dfparams(sols[solindx],0,options)
                plotthis[ii,jj]= s[1]*_REFV0
                if options.relative:
                    thissr= monoAbundanceMW.sigmaz(mapfehs[monoabindx],mapafes[monoabindx])*2.
                    plotthis[ii,jj]/= thissr
            elif options.type.lower() == 'srsz':
                #Setup everything
                pot= setup_potential(sols[solindx],options,1)
                vo= get_vo(sols[solindx],options,1)
                ro= get_ro(sols[solindx],options)
                aA= setup_aA(pot,options)               
                dfparams= get_dfparams(sols[solindx],0,options,log=False)
                if options.dfmodel.lower() == 'qdf':
                    #Normalize
                    hr= dfparams[0]/ro
                    sr= dfparams[1]/vo
                    sz= dfparams[2]/vo
                    hsr= dfparams[3]/ro
                    hsz= dfparams[4]/ro
                    #Setup
                    qdf= quasiisothermaldf(hr,sr,sz,hsr,hsz,pot=pot,
                                           aA=aA,cutcounter=True)              
                plotthis[ii,jj]= numpy.sqrt(qdf.sigmaR2(1.,1./_REFR0/ro,
                                                        ngl=options.ngl,gl=True)\
                                                /qdf.sigmaz2(1.,1./_REFR0/ro,
                                                            ngl=options.ngl,gl=True))
            elif options.type.lower() == 'outfrac':
                s= get_dfparams(sols[solindx],0,options)
                plotthis[ii,jj]= s[5]
            elif options.type.lower() == 'rhodm':
                #Setup potential
                pot= setup_potential(sols[solindx],options,1)
                vo= get_vo(sols[solindx],options,1)
                ro= get_ro(sols[solindx],options)
                if 'mwpotential' in options.potential.lower():
                    plotthis[ii,jj]= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                elif options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                    plotthis[ii,jj]= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                elif options.potential.lower() == 'mpdiskflplhalofixplfixbulgeflat':
                    plotthis[ii,jj]= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
            elif options.type.lower() == 'rhoo':
                #Setup potential
                pot= setup_potential(sols[solindx],options,1)
                vo= get_vo(sols[solindx],options,1)
                ro= get_ro(sols[solindx],options)
                plotthis[ii,jj]= potential.evaluateDensities(1.,0.,pot)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
            elif options.type.lower() == 'surfz':
                #Setup potential
                pot= setup_potential(sols[solindx],options,1)
                vo= get_vo(sols[solindx],options,1)
                ro= get_ro(sols[solindx],options)
                plotthis[ii,jj]= 2.*integrate.quad((lambda zz: potential.evaluateDensities(1.,zz,pot)),0.,options.height/_REFR0/ro)[0]*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*_REFR0*ro
            elif options.type.lower() == 'surfzdisk':
                #Setup potential
                pot= setup_potential(sols[solindx],options,1)
                vo= get_vo(sols[solindx],options,1)
                ro= get_ro(sols[solindx],options)
                if 'mpdisk' in options.potential.lower() or 'mwpotential' in options.potential.lower():
                    plotthis[ii,jj]= 2.*integrate.quad((lambda zz: potential.evaluateDensities(1.,zz,pot[0])),0.,options.height/_REFR0/ro)[0]*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*_REFR0*ro
            elif options.type.lower() == 'kz':
                #Setup potential
                pot= setup_potential(sols[solindx],options,1)
                vo= get_vo(sols[solindx],options,1)
                ro= get_ro(sols[solindx],options)
                plotthis[ii,jj]= numpy.fabs(potential.evaluatezforces(1.,options.height/ro/_REFR0,pot)/2./numpy.pi/4.302*_REFV0**2.*vo**2./_REFR0/ro)
            elif options.type.lower() == 'plhalo':
                #Setup potential
                pot= setup_potential(sols[solindx],options,1)
                vo= get_vo(sols[solindx],options,1)
                ro= get_ro(sols[solindx],options)
                if options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                    plotthis[ii,jj]= pot[1].alpha
            elif options.type.lower() == 'qhalo':
                #Setup potential
                s= get_potparams(sols[solindx],options,1)
                if options.potential.lower() == 'mpdiskflplhalofixplfixbulgeflat':
                    plotthis[ii,jj]= s[4]
            elif options.type.lower() == 'dlnvcdlnr':
                #Setup potential
                pot= setup_potential(sols[solindx],options,1)
                vo= get_vo(sols[solindx],options,1)
                ro= get_ro(sols[solindx],options)
                plotthis[ii,jj]= potential.dvcircdR(pot,1.)
            elif options.type.lower() == 'fd':
                #Setup potential
                pot= setup_potential(sols[solindx],options,1)
                vo= get_vo(sols[solindx],options,1)
                ro= get_ro(sols[solindx],options)
                if 'mwpotential' in options.potential.lower():
                    plotthis[ii,jj]= (pot[0].vcirc(1.))**2.
            elif options.type.lower() == 'fh':
                #Setup potential
                pot= setup_potential(sols[solindx],options,1)
                vo= get_vo(sols[solindx],options,1)
                ro= get_ro(sols[solindx],options)
                if 'mwpotential' in options.potential.lower():
                    plotthis[ii,jj]= (pot[1].vcirc(1.))**2.
            elif options.type.lower() == 'fb':
                #Setup potential
                pot= setup_potential(sols[solindx],options,1)
                vo= get_vo(sols[solindx],options,1)
                ro= get_ro(sols[solindx],options)
                if 'mwpotential' in options.potential.lower():
                    plotthis[ii,jj]= (pot[2].vcirc(1.))**2.
            elif options.type.lower() == 'afe' or options.type.lower() == 'feh' or options.type.lower() == 'fehafe' \
                    or options.type.lower() == 'afefeh':
                thisplot=[tightbinned.feh(ii),
                          tightbinned.afe(jj),
                          len(data)]
                if options.subtype.lower() == 'qvc':
                    s= get_potparams(sols[solindx],options,1)
                    thisq= s[0]
                    if not options.flatten is None:
                        thisq/= options.flatten
                    thisvc= s[1]
                    thisplot.extend([thisq,thisvc])
                elif options.subtype.lower() == 'rdzh':
                    s= get_potparams(sols[solindx],options,1)
                    thisrd= numpy.exp(s[0])
                    thiszh= numpy.exp(s[2-(1-(options.fixvo is None))])
                    thisplot.extend([thisrd,thiszh])
                elif options.subtype.lower() == 'zhvc':
                    s= get_potparams(sols[solindx],options,1)
                    thiszh= numpy.exp(s[2-(1-(options.fixvo is None))])
                    thisvc= s[1]*_REFV0
                    thisplot.extend([thiszh,thisvc])
                elif options.subtype.lower() == 'dlnvcdlnrvc':
                    s= get_potparams(sols[solindx],options,1)
                    thisslope= s[3-(1-(options.fixvo is None))]/30.
                    thisvc= s[1]*_REFV0
                    thisplot.extend([thisslope,thisvc])
                elif options.subtype.lower() == 'rdvc':
                    s= get_potparams(sols[solindx],options,1)
                    thisrd= numpy.exp(s[0])
                    thisvc= s[1]*_REFV0
                    thisplot.extend([thisrd,thisvc])
                elif options.subtype.lower() == 'rdplhalo':
                    s= get_potparams(sols[solindx],options,1)
                    thisrd= numpy.exp(s[0])
                    #Setup potential
                    pot= setup_potential(sols[solindx],options,1)
                    vo= get_vo(sols[solindx],options,1)
                    ro= get_ro(sols[solindx],options)
                    if options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                        thisplhalo= pot[1].alpha
                    thisplot.extend([thisrd,thisplhalo])
                elif options.subtype.lower() == 'dlnvcdlnrplhalo':
                    s= get_potparams(sols[solindx],options,1)
                    thisslope= s[3-(1-(options.fixvo is None))]/30.
                    #Setup potential
                    pot= setup_potential(sols[solindx],options,1)
                    vo= get_vo(sols[solindx],options,1)
                    ro= get_ro(sols[solindx],options)
                    if options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                        thisplhalo= pot[1].alpha
                    thisplot.extend([thisslope,thisplhalo])
                elif options.subtype.lower() == 'dlnvcdlnrzh':
                    s= get_potparams(sols[solindx],options,1)
                    thisslope= s[3-(1-(options.fixvo is None))]/30.
                    thiszh= numpy.exp(s[2-(1-(options.fixvo is None))])
                    thisplot.extend([thisslope,thiszh])
                elif options.subtype.lower() == 'vc14plhalo':
                    s= get_potparams(sols[solindx],options,1)
                    #Setup potential
                    pot= setup_potential(sols[solindx],options,1)
                    vo= get_vo(sols[solindx],options,1)
                    ro= get_ro(sols[solindx],options)
                    if options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                        thisplhalo= pot[1].alpha
                    thisvc14= potential.vcirc(pot,14./_REFR0/ro)*_REFV0*vo
                    thisplot.extend([thisplhalo,thisvc14])
                elif options.subtype.lower() == 'plhalovc':
                    s= get_potparams(sols[solindx],options,1)
                    thisvc= s[1]*_REFV0
                    #Setup potential
                    pot= setup_potential(sols[solindx],options,1)
                    vo= get_vo(sols[solindx],options,1)
                    ro= get_ro(sols[solindx],options)
                    if options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                        thisplhalo= pot[1].alpha
                    thisplot.extend([thisplhalo,thisvc])
                elif options.subtype.lower() == 'zhplhalo':
                    s= get_potparams(sols[solindx],options,1)
                    thiszh= numpy.exp(s[2-(1-(options.fixvo is None))])
                    #Setup potential
                    pot= setup_potential(sols[solindx],options,1)
                    vo= get_vo(sols[solindx],options,1)
                    ro= get_ro(sols[solindx],options)
                    if options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                        thisplhalo= pot[1].alpha
                    thisplot.extend([thiszh,thisplhalo])
                elif options.subtype.lower() == 'rhodmzh':
                    s= get_potparams(sols[solindx],options,1)
                    thisrd= numpy.exp(s[0])
                    #Setup potential
                    pot= setup_potential(sols[solindx],options,1)
                    vo= get_vo(sols[solindx],options,1)
                    ro= get_ro(sols[solindx],options)
                    thiszh= numpy.exp(s[2-(1-(options.fixvo is None))])
                    if 'mwpotential' in options.potential.lower():
                        thisrhodm= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                    elif options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                        thisrhodm= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                    thisplot.extend([thisrhodm,thiszh])
                elif options.subtype.lower() == 'rhodmsurfz':
                    s= get_potparams(sols[solindx],options,1)
                    thisrd= numpy.exp(s[0])
                    #Setup potential
                    pot= setup_potential(sols[solindx],options,1)
                    vo= get_vo(sols[solindx],options,1)
                    ro= get_ro(sols[solindx],options)
                    thissurfz= 2.*integrate.quad((lambda zz: potential.evaluateDensities(1.,zz,pot)),0.,options.height/_REFR0/ro)[0]*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*_REFR0*ro
                    if 'mwpotential' in options.potential.lower():
                        thisrhodm= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                    elif options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                        thisrhodm= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                    thisplot.extend([thisrhodm,thissurfz])
                elif options.subtype.lower() == 'surfzzh':
                    s= get_potparams(sols[solindx],options,1)
                    thisrd= numpy.exp(s[0])
                    #Setup potential
                    pot= setup_potential(sols[solindx],options,1)
                    vo= get_vo(sols[solindx],options,1)
                    ro= get_ro(sols[solindx],options)
                    thiszh= numpy.exp(s[2-(1-(options.fixvo is None))])
                    thissurfz= 2.*integrate.quad((lambda zz: potential.evaluateDensities(1.,zz,pot)),0.,options.height/_REFR0/ro)[0]*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*_REFR0*ro
                    thisplot.extend([thissurfz,thiszh])
                elif options.subtype.lower() == 'rhoozh':
                    s= get_potparams(sols[solindx],options,1)
                    thisrd= numpy.exp(s[0])
                    #Setup potential
                    pot= setup_potential(sols[solindx],options,1)
                    vo= get_vo(sols[solindx],options,1)
                    ro= get_ro(sols[solindx],options)
                    thiszh= numpy.exp(s[2-(1-(options.fixvo is None))])
                    thisrhoo= potential.evaluateDensities(1.,0.,pot)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                    thisplot.extend([thisrhoo,thiszh])
                elif options.subtype.lower() == 'rhodmvc':
                    s= get_potparams(sols[solindx],options,1)
                    thisvc= s[1]*_REFV0
                    #Setup potential
                    pot= setup_potential(sols[solindx],options,1)
                    vo= get_vo(sols[solindx],options,1)
                    ro= get_ro(sols[solindx],options)
                    if 'mwpotential' in options.potential.lower():
                        thisrhodm= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                    elif options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                        thisrhodm= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                    thisplot.extend([thisrhodm,thisvc])
                elif options.subtype.lower() == 'rhodmrd':
                    s= get_potparams(sols[solindx],options,1)
                    thisrd= numpy.exp(s[0])
                    #Setup potential
                    pot= setup_potential(sols[solindx],options,1)
                    vo= get_vo(sols[solindx],options,1)
                    ro= get_ro(sols[solindx],options)
                    thisrdh= numpy.exp(s[0])
                    if 'mwpotential' in options.potential.lower():
                        thisrhodm= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                    elif options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                        thisrhodm= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                    thisplot.extend([thisrhodm,thisrd])
                elif options.subtype.lower() == 'rhodmplhalo':
                    s= get_potparams(sols[solindx],options,1)
                    thisrd= numpy.exp(s[0])
                    #Setup potential
                    pot= setup_potential(sols[solindx],options,1)
                    vo= get_vo(sols[solindx],options,1)
                    ro= get_ro(sols[solindx],options)
                    if options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                        thisplhalo= pot[1].alpha
                    if 'mwpotential' in options.potential.lower():
                        thisrhodm= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                    elif options.potential.lower() == 'mpdiskplhalofixbulgeflat':
                        thisrhodm= pot[1].dens(1.,0.)*_REFV0**2.*vo**2./_REFR0**2./ro**2./4.302*10.**-3.
                    thisplot.extend([thisrhodm,thisplhalo])
                plotthis.append(thisplot)
    #Set up plot
    if options.type.lower() == 'q':
        if not options.flatten is None:
            vmin, vmax= 0.9, 1.1
            zlabel=r'$\mathrm{flattening}\ q / %.1f$' % options.flatten
        elif 'real' in options.outfilename.lower():
            vmin, vmax= 0.9, 1.1
            medianq= numpy.median(plotthis[numpy.isfinite(plotthis)])
            plotthis/= medianq
            zlabel=r'$\mathrm{flattening}\ q / %.2f$' % medianq
        else:
            vmin, vmax= 0.5, 1.2
            zlabel=r'$\mathrm{flattening}\ q$'
    elif options.type.lower() == 'vc':
        vmin, vmax= 0.95, 1.05
        zlabel=r'$V_c / %i\ \mathrm{km\,s}^{-1}$' % int(_REFV0)
        if 'real' in options.outfilename.lower():
           medianvc= numpy.median(plotthis[numpy.isfinite(plotthis)])
           plotthis/= medianvc
           zlabel=r'$V_c / %i\ \mathrm{km\,s}^{-1}$' % int(_REFV0*medianvc)
    elif options.type.lower() == 'rhodm':
        vmin, vmax= 0.00, 0.02
        zlabel=r'$\rho_{\mathrm{DM}}(R_0,0)\ [M_\odot\,\mathrm{pc}^{-3}]$'
    elif options.type.lower() == 'rhoo':
        vmin, vmax= 0.00, 0.2
        zlabel=r'$\rho(R_0,0)\ [M_\odot\,\mathrm{pc}^{-3}]$'
    elif options.type.lower() == 'surfz':
        vmin, vmax= 50.,120.
        zlabel=r'$\Sigma(%.1f\,\mathrm{kpc};R_0)\ [M_\odot\,\mathrm{pc}^{-2}]$' % options.height
    elif options.type.lower() == 'surfzdisk':
        vmin, vmax= 20.,90.
        zlabel=r'$\Sigma_{\mathrm{disk}}(%.1f\,\mathrm{kpc};R_0)\ [M_\odot\,\mathrm{pc}^{-2}]$' % options.height
    elif options.type.lower() == 'kz':
        vmin, vmax= 50.,120.
        zlabel=r'$K_Z(%.1f\,\mathrm{kpc};R_0)\ [M_\odot\,\mathrm{pc}^{-2}]$' % options.height
    elif options.type.lower() == 'dlnvcdlnr':
        vmin, vmax= -0.3,0.2
        zlabel=r'$\frac{\mathrm{d} \ln V_c}{\mathrm{d} \ln R}$'
    elif options.type.lower() == 'fd':
        vmin, vmax= 0.00, 1.
        zlabel=r'$V_{c,\mathrm{disk}} / V_c\,(R_0)$'
    elif options.type.lower() == 'fh':
        vmin, vmax= 0.00, 1.
        zlabel=r'$V_{c,\mathrm{halo}} / V_c\,(R_0)$'
    elif options.type.lower() == 'fb':
        vmin, vmax= 0.00, .1
        zlabel=r'$V_{c,\mathrm{halo}} / V_c\,(R_0)$'
    elif options.type.lower() == 'rd':
        vmin, vmax= 0.2, 0.6
        zlabel=r'$R_d / R_0$'
    elif options.type.lower() == 'zh':
        vmin, vmax= 0.0125, 0.075
        zlabel=r'$z_h / R_0$'
    elif options.type.lower() == 'plhalo':
        vmin, vmax= 0.0, 2.
        zlabel=r'$\alpha\ \mathrm{in}\ \rho_{\mathrm{halo}} \propto 1/r^\alpha$'
    elif options.type.lower() == 'qhalo':
        vmin, vmax= 0.4, 1.15
        zlabel=r'$q_\Phi^{\mathrm{halo}}$'
    elif options.type.lower() == 'ndata':
        vmin, vmax= numpy.nanmin(plotthis), numpy.nanmax(plotthis)
        zlabel=r'$N_\mathrm{data}$'
    elif options.type == 'hr':
        if options.relative:
            vmin, vmax= 0.8, 1.2
            zlabel=r'$\mathrm{input / output\ radial\ scale\ length}$'
        else:
            vmin, vmax= 1.35,4.5
            zlabel=r'$\mathrm{model\ radial\ scale\ length\ [kpc]}$'
    elif options.type == 'sz':
        if options.relative:
            vmin, vmax= 0.8, 1.2
            zlabel= r'$\mathrm{input / output\ model}\ \sigma_z$'
        else:
            vmin, vmax= 10.,60.
            zlabel= r'$\mathrm{model}\ \sigma_z\ [\mathrm{km\ s}^{-1}]$'
    elif options.type == 'sr':
        if options.relative:
            vmin, vmax= 0.8, 1.2
            zlabel= r'$\mathrm{input/output\ model}\ \sigma_R$'
        else:
            vmin, vmax= 10.,60.
            zlabel= r'$\mathrm{model}\ \sigma_R\ [\mathrm{km\ s}^{-1}]$'
    elif options.type == 'srsz':
        vmin, vmax= 0.5,2.
        zlabel= r'$\sigma_R/\sigma_z\ (R_0,1\,\mathrm{kpc})$'
    elif options.type == 'outfrac':
        vmin, vmax= 0., 1.
        zlabel= r'$\mathrm{relative\ number\ of\ outliers}$'
    elif options.type == 'afe':
        vmin, vmax= 0.0,.5
        zlabel=r'$[\alpha/\mathrm{Fe}]$'
    elif options.type == 'feh':
        vmin, vmax= -1.6,0.4
        zlabel=r'$[\mathrm{Fe/H}]$'
    elif options.type == 'fehafe':
        vmin, vmax= -.7,.7
        zlabel=r'$[\mathrm{Fe/H}]-[\mathrm{Fe/H}]_{1/2}([\alpha/\mathrm{Fe}])$'
    elif options.type == 'afefeh':
        vmin, vmax= -.15,.15
        zlabel=r'$[\alpha/\mathrm{Fe}]-[\alpha/\mathrm{Fe}]_{1/2}([\mathrm{Fe/H}])$'
    if options.tighten:
        xrange=[-1.6,0.5]
        yrange=[-0.05,0.55]
    else:
        xrange=[-2.,0.5]
        yrange=[-0.2,0.6]
    #Now plot
    if options.type.lower() == 'afe' or options.type.lower() == 'feh' \
            or options.type.lower() == 'fehafe':
        #Gather everything
        afe, feh, ndata, x, y= [], [], [], [], []
        for ii in range(len(plotthis)):
            afe.append(plotthis[ii][1])
            feh.append(plotthis[ii][0])
            ndata.append(plotthis[ii][2])
            x.append(plotthis[ii][3])
            y.append(plotthis[ii][4])
        afe= numpy.array(afe)
        feh= numpy.array(feh)
        ndata= numpy.array(ndata)
        x= numpy.array(x)
        y= numpy.array(y)
        #Process ndata
        ndata= ndata**.5
        ndata= ndata/numpy.median(ndata)*35.
        if options.type.lower() == 'afe':
            plotc= afe
        elif options.type.lower() == 'feh':
            plotc= feh
        elif options.type.lower() == 'afefeh':
            #Go through the bins to determine whether feh is high or low for this alpha
            plotc= numpy.zeros(len(afe))
            for ii in range(tightbinned.npixfeh()):
                fehbin= ii
                data= tightbinned.data[(tightbinned.data.feh > tightbinned.fehedges[fehbin])\
                                           *(tightbinned.data.feh <= tightbinned.fehedges[fehbin+1])]
                medianafe= numpy.median(data.afe)
                for jj in range(len(afe)):
                    if feh[jj] == tightbinned.feh(ii):
                        plotc[jj]= afe[jj]-medianafe
        else:
            #Go through the bins to determine whether feh is high or low for this alpha
            plotc= numpy.zeros(len(feh))
            for ii in range(tightbinned.npixafe()):
                afebin= ii
                data= tightbinned.data[(tightbinned.data.afe > tightbinned.afeedges[afebin])\
                                           *(tightbinned.data.afe <= tightbinned.afeedges[afebin+1])]
                medianfeh= numpy.median(data.feh)
                for jj in range(len(feh)):
                    if afe[jj] == tightbinned.afe(ii):
                        plotc[jj]= feh[jj]-medianfeh
        onedhists=False
        if options.subtype.lower() == 'qvc':
            if not options.flatten is None:
                xrange= [0.9,1.1]
                xlabel=r'$\mathrm{flattening}\ q / %.1f$' % options.flatten
            elif 'real' in options.outfilename.lower():
                xrange= [0.9,1.1]
                medianq= numpy.median(x[numpy.isfinite(x)])
                x/= medianq
                xlabel=r'$\mathrm{flattening}\ q / %.2f$' % medianq
            else:
                xrange= [0.5, 1.2]
                xlabel=r'$\mathrm{flattening}\ q$'
            yrange= [0.95, 1.05]
            ylabel=r'$V_c / %i\ \mathrm{km\,s}^{-1}$' % int(_REFV0)
            if 'real' in options.outfilename.lower():
                medianvc= numpy.median(y[numpy.isfinite(y)])
                y/= medianvc
                ylabel=r'$V_c / %i\ \mathrm{km\,s}^{-1}$' % int(_REFV0*medianvc)
        elif options.subtype.lower() == 'rdzh':
            yrange= [0.0125,0.1]
            xrange= [0.2,0.8]
            xlabel=r'$R_d / R_0$'
            ylabel=r'$z_h / R_0$'
        elif options.subtype.lower() == 'rdplhalo':
            yrange= [0.,2.]
            xrange= [0.2,0.8]
            xlabel=r'$R_d / R_0$'
            ylabel=r'$\alpha\ \mathrm{in}\ \rho_{\mathrm{halo}} \propto 1/r^\alpha$'
        elif options.subtype.lower() == 'vc14plhalo':
            yrange= [210.,280.]
            xrange= [0.,2.]
            xlabel=r'$\alpha\ \mathrm{in}\ \rho_{\mathrm{halo}} \propto 1/r^\alpha$'
            ylabel=r'$V_c (R=14\,\mathrm{kpc})\ [\mathrm{km\,s}^{-1}$]'
        elif options.subtype.lower() == 'zhplhalo':
            yrange= [0.,2.]
            xrange= [0.0125,0.1]

            ylabel=r'$\alpha\ \mathrm{in}\ \rho_{\mathrm{halo}} \propto 1/r^\alpha$'
            xlabel=r'$z_h / R_0$'
        elif options.subtype.lower() == 'rhodmplhalo':
            xrange= [0.,0.02]
            yrange= [0.,2.]
            ylabel=r'$\alpha\ \mathrm{in}\ \rho_{\mathrm{halo}} \propto 1/r^\alpha$'
            xlabel=r'$\rho_{\mathrm{DM}}(R_0,0)\ [M_\odot\,\mathrm{pc}^{-3}]$'
        elif options.subtype.lower() == 'rhodmzh':
            yrange= [0.0125,0.1]
            xrange= [0.,0.02]
            ylabel=r'$z_h / R_0$'
            xlabel=r'$\rho_{\mathrm{DM}}(R_0,0)\ [M_\odot\,\mathrm{pc}^{-3}]$'
        elif options.subtype.lower() == 'rhoozh':
            yrange= [0.0125,0.1]
            xrange= [0.,0.2]
            ylabel=r'$z_h / R_0$'
            xlabel=r'$\rho(R_0,0)\ [M_\odot\,\mathrm{pc}^{-3}]$'
        elif options.subtype.lower() == 'surfzzh':
            yrange= [0.0125,0.1]
            xrange= [50.+20.*(options.height-1.1),120.+20.*(options.height-1.1)]
            ylabel=r'$z_h / R_0$'
            xlabel=r'$\Sigma(%.1f\,\mathrm{kpc};R_0)\ [M_\odot\,\mathrm{pc}^{-2}]$' % options.height
        elif options.subtype.lower() == 'rhodmsurfz':
            yrange= [50.+20.*(options.height-1.1),120.+20.*(options.height-1.1)]
            xrange= [0.,0.02]
            ylabel=r'$\Sigma(%.1f\,\mathrm{kpc};R_0)\ [M_\odot\,\mathrm{pc}^{-2}]$' % options.height
            xlabel=r'$\rho_{\mathrm{DM}}(R_0,0)\ [M_\odot\,\mathrm{pc}^{-3}]$'
        elif options.subtype.lower() == 'rhodmrd':
            yrange= [0.2,0.8]
            xrange= [0.,0.02]
            ylabel=r'$R_d / R_0$'
            xlabel=r'$\rho_{\mathrm{DM}}(R_0,0)\ [M_\odot\,\mathrm{pc}^{-3}]$'
        elif options.subtype.lower() == 'rdvc':
            yrange= [210.,250.]
            xrange= [0.2,0.8]
            xlabel=r'$R_d / R_0$'
            ylabel=r'$V_c\ [\mathrm{km\,s}^{-1}]$'
        elif options.subtype.lower() == 'zhvc':
            yrange= [210.,250.]
            xrange= [0.0125,0.1]
            xlabel=r'$z_h / R_0$'
            ylabel=r'$V_c\ [\mathrm{km\,s}^{-1}]$'
        elif options.subtype.lower() == 'dlnvcdlnrvc':
            yrange= [210.,250.]
            xrange= [-0.2,0.07]
            xlabel=r'$\mathrm{d}\ln V_c / \mathrm{d}\ln R\, (R_0)$'
            ylabel=r'$V_c\ [\mathrm{km\,s}^{-1}]$'
            onedhists=True
        elif options.subtype.lower() == 'dlnvcdlnrplhalo':
            yrange= [0.,2.]
            ylabel=r'$\alpha\ \mathrm{in}\ \rho_{\mathrm{halo}} \propto 1/r^\alpha$'
            xrange= [-0.2,0.07]
            xlabel=r'$\mathrm{d}\ln V_c / \mathrm{d}\ln R\, (R_0)$'
        elif options.subtype.lower() == 'dlnvcdlnrzh':
            yrange= [0.0125,0.1]
            ylabel=r'$z_h / R_0$'
            xrange= [-0.2,0.07]
            xlabel=r'$\mathrm{d}\ln V_c / \mathrm{d}\ln R\, (R_0)$'
        elif options.subtype.lower() == 'rhodmvc':
            yrange= [210.,250.]
            xrange= [0.,0.02]
            ylabel=r'$V_c\ [\mathrm{km\,s}^{-1}]$'
            xlabel=r'$\rho_{\mathrm{DM}}(R_0,0)\ [M_\odot\,\mathrm{pc}^{-3}]$'
        elif options.subtype.lower() == 'plhalovc':
            yrange= [210.,250.]
            xrange= [0.,2.]
            xlabel=r'$\alpha\ \mathrm{in}\ \rho_{\mathrm{halo}} \propto 1/r^\alpha$'
            ylabel=r'$V_c\ [\mathrm{km\,s}^{-1}$]'
        bovy_plot.bovy_print(fig_height=3.87,fig_width=5.)
        ax= bovy_plot.bovy_plot(x,y,
                            s=ndata,c=plotc,
                            cmap='jet',
                            xlabel=xlabel,ylabel=ylabel,
                            clabel=zlabel,
                            xrange=xrange,yrange=yrange,
                            vmin=vmin,vmax=vmax,
                            scatter=True,edgecolors='none',
                            colorbar=True-onedhists,
                            onedhists=onedhists,
                            onedhistxnormed=onedhists,
                            onedhistynormed=onedhists,
                            bins=15)
        if onedhists:
            axS, axx, axy= ax
        if options.subtype.lower() == 'dlnvcdlnrvc':
            #Plot prior on one-d axes
            sb= numpy.linspace(-0.2,0.0399,1001)
            fsb= numpy.exp(numpy.log((0.04-sb)/0.04)-(0.04-sb)/0.04)
            fsb/= numpy.sum(fsb)*(sb[1]-sb[0])
            axx.plot(sb,fsb,'-',color='0.65')
            tvc= numpy.linspace(150.,350.,1001)
            fvc= numpy.exp(-(tvc-225.)**2./2./15.**2.)
            fvc/= numpy.sum(fvc)*(tvc[1]-tvc[0])
            axy.plot(fvc,tvc,'-',color='0.65')
    else:
        bovy_plot.bovy_print()
        bovy_plot.bovy_dens2d(plotthis.T,origin='lower',cmap='jet',
                              interpolation='nearest',
                              xlabel=r'$[\mathrm{Fe/H}]$',
                              ylabel=r'$[\alpha/\mathrm{Fe}]$',
                              zlabel=zlabel,
                              xrange=xrange,yrange=yrange,
                              vmin=vmin,vmax=vmax,
                              contours=False,
                              colorbar=True,shrink=0.78)
        if options.type.lower() == 'q' or options.type.lower() == 'vc' \
                or options.relative or options.type.lower() == 'rd' \
                or options.type.lower() == 'fd' \
                or options.type.lower() == 'fh' \
                or options.type.lower() == 'fb' \
                or options.type.lower() == 'plhalo' \
                or options.type.lower() == 'surfz' \
                or options.type.lower() == 'surfzdisk' \
                or options.type.lower() == 'rhoo' \
                or options.type.lower() == 'qhalo' \
                or options.type.lower() == 'kz':
            bovy_plot.bovy_text(r'$\mathrm{median} = %.2f \pm %.2f$' % (numpy.median(plotthis[numpy.isfinite(plotthis)]),
                                                                        1.4826*numpy.median(numpy.fabs(plotthis[numpy.isfinite(plotthis)]-numpy.median(plotthis[numpy.isfinite(plotthis)])))),
                                bottom_left=True,size=14.)
        if options.type.lower() == 'zh' or options.type.lower() == 'rhodm':
            bovy_plot.bovy_text(r'$\mathrm{median} = %.4f \pm %.4f$' % (numpy.median(plotthis[numpy.isfinite(plotthis)]),
                                                                        1.4826*numpy.median(numpy.fabs(plotthis[numpy.isfinite(plotthis)]-numpy.median(plotthis[numpy.isfinite(plotthis)])))),
                                bottom_left=True,size=14.)
    bovy_plot.bovy_end_print(options.outfilename)
    return None
    def __init__(self,
                 RZPot=None,rgrid=(numpy.log(0.01),numpy.log(20.),101),
                 zgrid=(0.,1.,101),logR=True,
                 interpPot=False,interpRforce=False,interpzforce=False,
                 interpDens=False,
                 interpvcirc=False,
                 interpdvcircdr=False,
                 interpepifreq=False,interpverticalfreq=False,
                 ro=None,vo=None,
                 use_c=False,enable_c=False,zsym=True,
                 numcores=None):
        """
        NAME:

           __init__

        PURPOSE:

           Initialize an interpRZPotential instance

        INPUT:

           RZPot - RZPotential to be interpolated

           rgrid - R grid to be given to linspace as in rs= linspace(*rgrid)

           zgrid - z grid to be given to linspace as in zs= linspace(*zgrid)

           logR - if True, rgrid is in the log of R so logrs= linspace(*rgrid)

           interpPot, interpRforce, interpzforce, interpDens,interpvcirc, interpepifreq, interpverticalfreq, interpdvcircdr= if True, interpolate these functions

           use_c= use C to speed up the calculation of the grid

           enable_c= enable use of C for interpolations

           zsym= if True (default), the potential is assumed to be symmetric around z=0 (so you can use, e.g.,  zgrid=(0.,1.,101)).

           numcores= if set to an integer, use this many cores (only used for vcirc, dvcircdR, epifreq, and verticalfreq; NOT NECESSARILY FASTER, TIME TO MAKE SURE)

           ro=, vo= distance and velocity scales for translation into internal units (default from configuration file)

        OUTPUT:

           instance

        HISTORY:

           2010-07-21 - Written - Bovy (NYU)

           2013-01-24 - Started with new implementation - Bovy (IAS)

        """
        if isinstance(RZPot,interpRZPotential):
            from galpy.potential import PotentialError
            raise PotentialError('Cannot setup interpRZPotential with another interpRZPotential')
        # Propagate ro and vo
        roSet= True
        voSet= True
        if ro is None:
            if isinstance(RZPot,list):
                ro= RZPot[0]._ro
                roSet= RZPot[0]._roSet
            else:
                ro= RZPot._ro
                roSet= RZPot._roSet
        if vo is None:
            if isinstance(RZPot,list):
                vo= RZPot[0]._vo
                voSet= RZPot[0]._voSet
            else:
                vo= RZPot._vo
                voSet= RZPot._voSet
        Potential.__init__(self,amp=1.,ro=ro,vo=vo)
        # Turn off physical if it hadn't been on
        if not roSet: self._roSet= False
        if not voSet: self._voSet= False
        self._origPot= RZPot
        self._rgrid= numpy.linspace(*rgrid)
        self._logR= logR
        if self._logR:
            self._rgrid= numpy.exp(self._rgrid)
            self._logrgrid= numpy.log(self._rgrid)
        self._zgrid= numpy.linspace(*zgrid)
        self._interpPot= interpPot
        self._interpRforce= interpRforce
        self._interpzforce= interpzforce
        self._interpDens= interpDens
        self._interpvcirc= interpvcirc
        self._interpdvcircdr= interpdvcircdr
        self._interpepifreq= interpepifreq
        self._interpverticalfreq= interpverticalfreq
        self._enable_c= enable_c*ext_loaded
        self.hasC= self._enable_c
        self._zsym= zsym
        if interpPot:
            if use_c*ext_loaded:
                self._potGrid, err= calc_potential_c(self._origPot,self._rgrid,self._zgrid)
            else:
                from galpy.potential import evaluatePotentials
                potGrid= numpy.zeros((len(self._rgrid),len(self._zgrid)))
                for ii in range(len(self._rgrid)):
                    for jj in range(len(self._zgrid)):
                        potGrid[ii,jj]= evaluatePotentials(self._origPot,self._rgrid[ii],self._zgrid[jj])
                self._potGrid= potGrid
            if self._logR:
                self._potInterp= interpolate.RectBivariateSpline(self._logrgrid,
                                                                 self._zgrid,
                                                                 self._potGrid,
                                                                 kx=3,ky=3,s=0.)
            else:
                self._potInterp= interpolate.RectBivariateSpline(self._rgrid,
                                                                 self._zgrid,
                                                                 self._potGrid,
                                                                 kx=3,ky=3,s=0.)
            if enable_c*ext_loaded:
                self._potGrid_splinecoeffs= calc_2dsplinecoeffs_c(self._potGrid)
        if interpRforce:
            if use_c*ext_loaded:
                self._rforceGrid, err= calc_potential_c(self._origPot,self._rgrid,self._zgrid,rforce=True)
            else:
                from galpy.potential import evaluateRforces
                rforceGrid= numpy.zeros((len(self._rgrid),len(self._zgrid)))
                for ii in range(len(self._rgrid)):
                    for jj in range(len(self._zgrid)):
                        rforceGrid[ii,jj]= evaluateRforces(self._origPot,self._rgrid[ii],self._zgrid[jj])
                self._rforceGrid= rforceGrid
            if self._logR:
                self._rforceInterp= interpolate.RectBivariateSpline(self._logrgrid,
                                                                    self._zgrid,
                                                                    self._rforceGrid,
                                                                    kx=3,ky=3,s=0.)
            else:
                self._rforceInterp= interpolate.RectBivariateSpline(self._rgrid,
                                                                    self._zgrid,
                                                                    self._rforceGrid,
                                                                    kx=3,ky=3,s=0.)
            if enable_c*ext_loaded:
                self._rforceGrid_splinecoeffs= calc_2dsplinecoeffs_c(self._rforceGrid)
        if interpzforce:
            if use_c*ext_loaded:
                self._zforceGrid, err= calc_potential_c(self._origPot,self._rgrid,self._zgrid,zforce=True)
            else:
                from galpy.potential import evaluatezforces
                zforceGrid= numpy.zeros((len(self._rgrid),len(self._zgrid)))
                for ii in range(len(self._rgrid)):
                    for jj in range(len(self._zgrid)):
                        zforceGrid[ii,jj]= evaluatezforces(self._origPot,self._rgrid[ii],self._zgrid[jj])
                self._zforceGrid= zforceGrid
            if self._logR:
                self._zforceInterp= interpolate.RectBivariateSpline(self._logrgrid,
                                                                    self._zgrid,
                                                                    self._zforceGrid,
                                                                    kx=3,ky=3,s=0.)
            else:
                self._zforceInterp= interpolate.RectBivariateSpline(self._rgrid,
                                                                    self._zgrid,
                                                                    self._zforceGrid,
                                                                    kx=3,ky=3,s=0.)
            if enable_c*ext_loaded:
                self._zforceGrid_splinecoeffs= calc_2dsplinecoeffs_c(self._zforceGrid)
        if interpDens:
            from galpy.potential import evaluateDensities
            densGrid= numpy.zeros((len(self._rgrid),len(self._zgrid)))
            for ii in range(len(self._rgrid)):
                for jj in range(len(self._zgrid)):
                    densGrid[ii,jj]= evaluateDensities(self._origPot,self._rgrid[ii],self._zgrid[jj])
            self._densGrid= densGrid
            if self._logR:
                self._densInterp= interpolate.RectBivariateSpline(self._logrgrid,
                                                                  self._zgrid,
                                                                  numpy.log(self._densGrid+10.**-10.),
                                                                  kx=3,ky=3,s=0.)
            else:
                self._densInterp= interpolate.RectBivariateSpline(self._rgrid,
                                                                  self._zgrid,
                                                                  numpy.log(self._densGrid+10.**-10.),
                                                                  kx=3,ky=3,s=0.)
        if interpvcirc:
            from galpy.potential import vcirc
            if not numcores is None:
                self._vcircGrid= multi.parallel_map((lambda x: vcirc(self._origPot,self._rgrid[x])),
                                                    list(range(len(self._rgrid))),numcores=numcores)
            else:
                self._vcircGrid= numpy.array([vcirc(self._origPot,r) for r in self._rgrid])
            if self._logR:
                self._vcircInterp= interpolate.InterpolatedUnivariateSpline(self._logrgrid,self._vcircGrid,k=3)
            else:
                self._vcircInterp= interpolate.InterpolatedUnivariateSpline(self._rgrid,self._vcircGrid,k=3)
        if interpdvcircdr:
            from galpy.potential import dvcircdR
            if not numcores is None:
                self._dvcircdrGrid= multi.parallel_map((lambda x: dvcircdR(self._origPot,self._rgrid[x])),
                                                       list(range(len(self._rgrid))),numcores=numcores)
            else:
                self._dvcircdrGrid= numpy.array([dvcircdR(self._origPot,r) for r in self._rgrid])
            if self._logR:
                self._dvcircdrInterp= interpolate.InterpolatedUnivariateSpline(self._logrgrid,self._dvcircdrGrid,k=3)
            else:
                self._dvcircdrInterp= interpolate.InterpolatedUnivariateSpline(self._rgrid,self._dvcircdrGrid,k=3)
        if interpepifreq:
            from galpy.potential import epifreq
            if not numcores is None:
                self._epifreqGrid= numpy.array(multi.parallel_map((lambda x: epifreq(self._origPot,self._rgrid[x])),
                                                      list(range(len(self._rgrid))),numcores=numcores))
            else:
                self._epifreqGrid= numpy.array([epifreq(self._origPot,r) for r in self._rgrid])
            indx= True^numpy.isnan(self._epifreqGrid)
            if numpy.sum(indx) < 4:
                if self._logR:
                    self._epifreqInterp= interpolate.InterpolatedUnivariateSpline(self._logrgrid[indx],self._epifreqGrid[indx],k=1)
                else:
                    self._epifreqInterp= interpolate.InterpolatedUnivariateSpline(self._rgrid[indx],self._epifreqGrid[indx],k=1)
            else:
                if self._logR:
                    self._epifreqInterp= interpolate.InterpolatedUnivariateSpline(self._logrgrid[indx],self._epifreqGrid[indx],k=3)
                else:
                    self._epifreqInterp= interpolate.InterpolatedUnivariateSpline(self._rgrid[indx],self._epifreqGrid[indx],k=3)
        if interpverticalfreq:
            from galpy.potential import verticalfreq
            if not numcores is None:
                self._verticalfreqGrid= multi.parallel_map((lambda x: verticalfreq(self._origPot,self._rgrid[x])),
                                                       list(range(len(self._rgrid))),numcores=numcores)
            else:
                self._verticalfreqGrid= numpy.array([verticalfreq(self._origPot,r) for r in self._rgrid])
            if self._logR:
                self._verticalfreqInterp= interpolate.InterpolatedUnivariateSpline(self._logrgrid,self._verticalfreqGrid,k=3)
            else:
                self._verticalfreqInterp= interpolate.InterpolatedUnivariateSpline(self._rgrid,self._verticalfreqGrid,k=3)
        return None
Exemple #23
0
def _sample_galpy_potential(pot,
                            n,
                            rmin,
                            rmax,
                            ro=8.,
                            vo=220.,
                            coordinates='cartesian'):
    """Generate positions and velocities from galpy potentail

    Parameters
    ----------
    pot : class
        galpy potential
    N : int
        number of stars in the cluster (default: 1000)
    rmin : float
        minimum stellar radius (default: 0.01)
    rmax : float
        maximnum stellar radius (default: 100.)
    ro : float
        galpy distance scaling parameter
    vo : float
        galpy velocity scaling parameter
    coordinates : str
        coordinate system to return (default: cartesian)

    Returns
    -------
    x,y,z,vx,vy,vz : float
        positions and velocities of generated points

    History
    -------
    2020 - Written - Webb (UofT)
    """
    ran = np.random.rand(n)
    rad = np.linspace(rmin, rmax, n)

    try:
        menc = pot.mass(rad / ro, z=0, t=0, forceint=False)
    except:
        vc = potential.vcirc(pot,
                             rad / ro,
                             phi=0,
                             t=0.,
                             ro=ro,
                             vo=vo,
                             use_physical=False)
        menc = vc**2. * (rad / ro)

    menc *= bovy_conversion.mass_in_msol(ro=ro, vo=vo)

    r = np.interp(ran, menc / menc[-1], rad)
    phi = 2.0 * np.pi * np.random.rand(n)
    theta = np.arccos(1.0 - 2.0 * np.random.rand(n))

    x = r * np.sin(theta) * np.cos(phi)
    y = r * np.sin(theta) * np.sin(phi)
    z = r * np.cos(theta)

    sigma_v_1d = vo * potential.vcirc(
        pot, rad / ro, phi=0, t=0., ro=ro, vo=vo,
        use_physical=False) / np.sqrt(3.)

    vx = np.random.normal(0., sigma_v_1d, n)
    vy = np.random.normal(0., sigma_v_1d, n)
    vz = np.random.normal(0., sigma_v_1d, n)

    if coordinates == 'spherical':
        vr = (vx * np.sin(theta) * np.cos(phi) +
              vy * np.sin(theta) * np.sin(phi) + vz * np.cos(theta))
        vtheta = (vx * np.cos(theta) * np.cos(phi) +
                  vy * np.cos(theta) * np.sin(phi) - vz * np.sin(theta))
        vphi = vx * -np.sin(phi) + vy * np.cos(phi)

        x, y, z = r, phi, theta
        vx, vy, vz = vr, vphi, vtheta

    elif coordinates == 'cylindrical':
        x, y, z = bovy_coords.rect_to_cyl(x, y, z)
        vx, vy, vz = bovy_coords.rect_to_cyl_vec(vx, vy, vz, x, y, z, True)

    return x, y, z, vx, vy, vz
Exemple #24
0
def f_vlsr(r, R0, v0, l, b):
    return (R0 * vcirc(MWPotential2014,r/R0,ro=R0,vo=v0) / 
            r - vcirc(MWPotential2014,1,ro=R0,vo=v0)) * np.sin(l) * np.cos(b)
Exemple #25
0
 def __init__(self,
              RZPot=None,rgrid=(0.01,2.,101),zgrid=(0.,0.2,101),logR=False,
              interpPot=False,interpRforce=False,interpzforce=False,
              interpDens=False,
              interpvcirc=False,
              interpdvcircdr=False,
              interpepifreq=False,interpverticalfreq=False,
              use_c=False,enable_c=False,zsym=True,
              numcores=None):
     """
     NAME:
        __init__
     PURPOSE:
        Initialize an interpRZPotential instance
     INPUT:
        RZPot - RZPotential to be interpolated
        rgrid - R grid to be given to linspace
        zgrid - z grid to be given to linspace
        logR - if True, rgrid is in the log of R
        interpPot, interpRfoce, interpzforce, interpDens,interpvcirc, interpeopifreq, interpverticalfreq, interpdvcircdr= if True, interpolate these functions
        use_c= use C to speed up the calculation
        enable_c= enable use of C for interpolations
        zsym= if True (default), the potential is assumed to be symmetric around z=0 (so you can use, e.g.,  zgrid=(0.,1.,101)).
        numcores= if set to an integer, use this many cores (only used for vcirc, dvcircdR, epifreq, and verticalfreq; NOT NECESSARILY FASTER, TIME TO MAKE SURE)
     OUTPUT:
        instance
     HISTORY:
        2010-07-21 - Written - Bovy (NYU)
        2013-01-24 - Started with new implementation - Bovy (IAS)
     """
     Potential.__init__(self,amp=1.)
     self.hasC= True
     self._origPot= RZPot
     self._rgrid= numpy.linspace(*rgrid)
     self._logR= logR
     if self._logR:
         self._rgrid= numpy.exp(self._rgrid)
         self._logrgrid= numpy.log(self._rgrid)
     self._zgrid= numpy.linspace(*zgrid)
     self._interpPot= interpPot
     self._interpRforce= interpRforce
     self._interpzforce= interpzforce
     self._interpDens= interpDens
     self._interpvcirc= interpvcirc
     self._interpdvcircdr= interpdvcircdr
     self._interpepifreq= interpepifreq
     self._interpverticalfreq= interpverticalfreq
     self._enable_c= enable_c*ext_loaded
     self._zsym= zsym
     if interpPot:
         if use_c*ext_loaded:
             self._potGrid, err= calc_potential_c(self._origPot,self._rgrid,self._zgrid)
         else:
             from galpy.potential import evaluatePotentials
             potGrid= numpy.zeros((len(self._rgrid),len(self._zgrid)))
             for ii in range(len(self._rgrid)):
                 for jj in range(len(self._zgrid)):
                     potGrid[ii,jj]= evaluatePotentials(self._rgrid[ii],self._zgrid[jj],self._origPot)
             self._potGrid= potGrid
         if self._logR:
             self._potInterp= interpolate.RectBivariateSpline(self._logrgrid,
                                                              self._zgrid,
                                                              self._potGrid,
                                                              kx=3,ky=3,s=0.)
         else:
             self._potInterp= interpolate.RectBivariateSpline(self._rgrid,
                                                              self._zgrid,
                                                              self._potGrid,
                                                              kx=3,ky=3,s=0.)
         if enable_c*ext_loaded:
             self._potGrid_splinecoeffs= calc_2dsplinecoeffs_c(self._potGrid)
     if interpRforce:
         if use_c*ext_loaded:
             self._rforceGrid, err= calc_potential_c(self._origPot,self._rgrid,self._zgrid,rforce=True)
         else:
             from galpy.potential import evaluateRforces
             rforceGrid= numpy.zeros((len(self._rgrid),len(self._zgrid)))
             for ii in range(len(self._rgrid)):
                 for jj in range(len(self._zgrid)):
                     rforceGrid[ii,jj]= evaluateRforces(self._rgrid[ii],self._zgrid[jj],self._origPot)
             self._rforceGrid= rforceGrid
         if self._logR:
             self._rforceInterp= interpolate.RectBivariateSpline(self._logrgrid,
                                                                 self._zgrid,
                                                                 self._rforceGrid,
                                                                 kx=3,ky=3,s=0.)
         else:
             self._rforceInterp= interpolate.RectBivariateSpline(self._rgrid,
                                                                 self._zgrid,
                                                                 self._rforceGrid,
                                                                 kx=3,ky=3,s=0.)
         if enable_c*ext_loaded:
             self._rforceGrid_splinecoeffs= calc_2dsplinecoeffs_c(self._rforceGrid)
     if interpzforce:
         if use_c*ext_loaded:
             self._zforceGrid, err= calc_potential_c(self._origPot,self._rgrid,self._zgrid,zforce=True)
         else:
             from galpy.potential import evaluatezforces
             zforceGrid= numpy.zeros((len(self._rgrid),len(self._zgrid)))
             for ii in range(len(self._rgrid)):
                 for jj in range(len(self._zgrid)):
                     zforceGrid[ii,jj]= evaluatezforces(self._rgrid[ii],self._zgrid[jj],self._origPot)
             self._zforceGrid= zforceGrid
         if self._logR:
             self._zforceInterp= interpolate.RectBivariateSpline(self._logrgrid,
                                                                 self._zgrid,
                                                                 self._zforceGrid,
                                                                 kx=3,ky=3,s=0.)
         else:
             self._zforceInterp= interpolate.RectBivariateSpline(self._rgrid,
                                                                 self._zgrid,
                                                                 self._zforceGrid,
                                                                 kx=3,ky=3,s=0.)
         if enable_c*ext_loaded:
             self._zforceGrid_splinecoeffs= calc_2dsplinecoeffs_c(self._zforceGrid)
     if interpDens:
         if False:
             raise NotImplementedError("Using C to calculate an interpolation grid for the density is not supported currently")
             self._densGrid, err= calc_dens_c(self._origPot,self._rgrid,self._zgrid)
         else:
             from galpy.potential import evaluateDensities
             densGrid= numpy.zeros((len(self._rgrid),len(self._zgrid)))
             for ii in range(len(self._rgrid)):
                 for jj in range(len(self._zgrid)):
                     densGrid[ii,jj]= evaluateDensities(self._rgrid[ii],self._zgrid[jj],self._origPot)
             self._densGrid= densGrid
         if self._logR:
             self._densInterp= interpolate.RectBivariateSpline(self._logrgrid,
                                                               self._zgrid,
                                                               numpy.log(self._densGrid+10.**-10.),
                                                               kx=3,ky=3,s=0.)
         else:
             self._densInterp= interpolate.RectBivariateSpline(self._rgrid,
                                                               self._zgrid,
                                                               numpy.log(self._densGrid+10.**-10.),
                                                               kx=3,ky=3,s=0.)
         if False:
             self._densGrid_splinecoeffs= calc_2dsplinecoeffs_c(self._densGrid)
     if interpvcirc:
         from galpy.potential import vcirc
         if not numcores is None:
             self._vcircGrid= multi.parallel_map((lambda x: vcirc(self._origPot,self._rgrid[x])),
                                                 range(len(self._rgrid)),numcores=numcores)
         else:
             self._vcircGrid= numpy.array([vcirc(self._origPot,r) for r in self._rgrid])
         if self._logR:
             self._vcircInterp= interpolate.InterpolatedUnivariateSpline(self._logrgrid,self._vcircGrid,k=3)
         else:
             self._vcircInterp= interpolate.InterpolatedUnivariateSpline(self._rgrid,self._vcircGrid,k=3)
     if interpdvcircdr:
         from galpy.potential import dvcircdR
         if not numcores is None:
             self._dvcircdrGrid= multi.parallel_map((lambda x: dvcircdR(self._origPot,self._rgrid[x])),
                                                    range(len(self._rgrid)),numcores=numcores)
         else:
             self._dvcircdrGrid= numpy.array([dvcircdR(self._origPot,r) for r in self._rgrid])
         if self._logR:
             self._dvcircdrInterp= interpolate.InterpolatedUnivariateSpline(self._logrgrid,self._dvcircdrGrid,k=3)
         else:
             self._dvcircdrInterp= interpolate.InterpolatedUnivariateSpline(self._rgrid,self._dvcircdrGrid,k=3)
     if interpepifreq:
         from galpy.potential import epifreq
         if not numcores is None:
             self._epifreqGrid= multi.parallel_map((lambda x: epifreq(self._origPot,self._rgrid[x])),
                                                   range(len(self._rgrid)),numcores=numcores)
         else:
             self._epifreqGrid= numpy.array([epifreq(self._origPot,r) for r in self._rgrid])
         indx= True-numpy.isnan(self._epifreqGrid)
         if numpy.sum(indx) < 4:
             if self._logR:
                 self._epifreqInterp= interpolate.InterpolatedUnivariateSpline(self._logrgrid[indx],self._epifreqGrid[indx],k=1)
             else:
                 self._epifreqInterp= interpolate.InterpolatedUnivariateSpline(self._rgrid[indx],self._epifreqGrid[indx],k=1)
         else:
             if self._logR:
                 self._epifreqInterp= interpolate.InterpolatedUnivariateSpline(self._logrgrid[indx],self._epifreqGrid[indx],k=3)
             else:
                 self._epifreqInterp= interpolate.InterpolatedUnivariateSpline(self._rgrid[indx],self._epifreqGrid[indx],k=3)
     if interpverticalfreq:
         from galpy.potential import verticalfreq
         if not numcores is None:
             self._verticalfreqGrid= multi.parallel_map((lambda x: verticalfreq(self._origPot,self._rgrid[x])),
                                                    range(len(self._rgrid)),numcores=numcores)
         else:
             self._verticalfreqGrid= numpy.array([verticalfreq(self._origPot,r) for r in self._rgrid])
         if self._logR:
             self._verticalfreqInterp= interpolate.InterpolatedUnivariateSpline(self._logrgrid,self._verticalfreqGrid,k=3)
         else:
             self._verticalfreqInterp= interpolate.InterpolatedUnivariateSpline(self._rgrid,self._verticalfreqGrid,k=3)
     return None
Exemple #26
0
def add_MCs(pot=MWPotential2014,Mmin=10**6.,rand_rotate=False,ro=_REFR0):
    
    '''
    Setup Molecular clouds and return their
    M,rs,R,vR,vT,z,vz,phi today
    rand_rotate : if True, add random rotation to phi between [0,2pi]
    '''
    
    hdulist=fits.open('molecular_clouds/J_ApJ_834_57_table1.dat.gz.fits')
    aa=hdulist[1].data
    l=aa['GLON']
    b=aa['GLAT']
    #Near or far distance flag (0=near; 1=far) 
    flag=aa['INF']
    Dnear=aa['Dnear']
    Dfar=aa['Dfar']
    znear=aa['znear']
    zfar=aa['zfar']
    #R_sph_gal=aa['Rgal']
    Rnear=aa['Rnear']
    Rfar=aa['Rfar']
    Mnear=aa['Mnear']
    Mfar=aa['Mfar']
    
    D_all=np.empty(len(l))
    zfile=np.empty(len(l))
    rs_all=np.empty(len(l))
    M_all=np.empty(len(l))


    for ii in range(len(l)):
        if flag[ii] == 0 :
            D_all[ii]=Dnear[ii]
            zfile[ii]=znear[ii]
            #rs_all[ii]=Rnear[ii]*0.001  #convert to kpc
            M_all[ii]=Mnear[ii]
        
        
        else :
            D_all[ii]=Dfar[ii]
            zfile[ii]=zfar[ii]
            #rs_all[ii]=Rfar[ii]*0.001 #convert to kpc
            M_all[ii]=Mfar[ii]
    

    R_all,phi_all,z_all= lbd_to_galcencyl(l,b,D_all*(8.5/8.))
    
    def rs(M):
        return 0.1*(M/10**7.)**0.5

    for ii in range(len(M_all)):
        rs_all[ii]=rs(M_all[ii])


    R_all/=ro
    z_all/=ro
    
    M=[]
    rs=[]
    z=[]
    R=[]
    phi=[]

    for ii in range(len(l)):
        if M_all[ii] >= Mmin :
            M.append(M_all[ii])
            rs.append(rs_all[ii])
            z.append(z_all[ii])
            phi.append(phi_all[ii])
            R.append(R_all[ii])
        
    M=np.array(M)
    rs=np.array(rs)
    z=np.array(z)
    R=np.array(R)
    phi=np.array(phi)
    
    if rand_rotate :
        phi+=2*np.pi*np.random.uniform(low=0.,high=1.,size=len(phi))
    print (phi)
    vT=np.empty(len(M))

    for ii in range(len(M)):
        vT[ii]=vcirc(pot,R[ii])
        
    vR=np.zeros(len(M))
    vz=np.zeros(len(M))
    
    coord=[]
    
    for ii in range(len(M)):
        coord.append([R[ii],vR[ii],vT[ii],z[ii],vz[ii],phi[ii]])
    
    return (M,rs,coord)
def VpratioMW(Rpkpc):
    a = vcirc(MWPotential2014, Rpkpc / par.Rskpc)
    return a
def VpratioMWBH(Rpkpc):
    MWPotential2014wBH= [MWPotential2014,KeplerPotential(amp=4*10**6./bovy_conversion.mass_in_msol(par.Vs,par.Rskpc))]
    a = vcirc(MWPotential2014wBH,Rpkpc/par.Rskpc)
    return a;
Exemple #29
0
def v_orbit(r):
    return 220.0 * vcirc(mp, r / R_0)