def POTDEF1(z, TheTa2):
     TheTa = np.sqrt(TheTa2**2 + theta1[l]**2)
     R = D_d * TheTa
     MN_Bulge_p = MiyamotoNagaiPotential(amp=M1, a=0, b=b1, normalize=False)
     Sigma = MN_Bulge_p.dens(R, z)
     kappa = Sigma / SIGMA_CRIT
     return (4 / theta2[l]) * TheTa2 * kappa / SIGMA_CRIT**2
Esempio n. 2
0
def MWPotentialSCFbar_nogrow(mbar,Acos,Asin,rs=1.,normalize=False,pat_speed=40.,fin_phi_deg=27.,t_stream_age=5.):
    
    a=rs/ro
    omegaP=pat_speed*(ro/vo)
    
    fin_phi= np.radians(fin_phi_deg)
    #init_phi= fin_phi - o_p*(tpal5age*Gyr_to_s)
    
    init_phi= fin_phi - omegaP*t_stream_age/bovy_conversion.time_in_Gyr(vo,ro)
    
    mrat=mbar/10.**10. #10^10 mass of bar used to compute Acos and Asin
    
    static_bar=potential.SCFPotential(amp=mrat,Acos=Acos,Asin=Asin,a=a,normalize=normalize)
    
    #Note only m=0 terms are considered 
    static_axi_bar=potential.SCFPotential(amp=mrat,Acos=np.atleast_3d(Acos[:,:,0]),a=a)
    
    barrot=potential.SolidBodyRotationWrapperPotential(pot=static_bar,omega=omegaP,ro=ro,vo=vo,pa=init_phi)
    
    if mbar <= 5.*10**9. :
        MWP2014SCFbar=[MWPotential2014[0],MiyamotoNagaiPotential(amp=(6.8-mrat)*10.**10*u.Msun,a=3./8.,b=0.28/8.),MWPotential2014[2],barrot]
        turn_physical_off(MWP2014SCFbar)
        #setup the corresponding axisymmetric bar
        MWP2014SCFnobar= [MWPotential2014[0],MiyamotoNagaiPotential(amp=(6.8-mrat)*10.**10*u.Msun,a=3./8.,b=0.28/8.),MWPotential2014[2],static_axi_bar]
        turn_physical_off(MWP2014SCFnobar)
        
    else : 
        MWP2014SCFbar=[MiyamotoNagaiPotential(amp=(6.8+0.5-mrat)*10.**10*u.Msun,a=3./8.,b=0.28/8.),MWPotential2014[2],barrot]
        turn_physical_off(MWP2014SCFbar)
        
        MWP2014SCFnobar= [MiyamotoNagaiPotential(amp=(6.8+0.5-mrat)*10.**10*u.Msun,a=3./8.,b=0.28/8.),MWPotential2014[2],static_axi_bar]
        turn_physical_off(MWP2014SCFnobar)
        
    return (MWP2014SCFbar,MWP2014SCFnobar)
Esempio n. 3
0
def test_TimeInterpPotential():
    #Just to check that the code above has run properly
    from galpy.potential import LogarithmicHaloPotential, \
        MiyamotoNagaiPotential
    lp= LogarithmicHaloPotential(normalize=1.)
    mp= MiyamotoNagaiPotential(normalize=1.)
    tip= TimeInterpPotential(lp,mp)
    assert numpy.fabs(tip.Rforce(1.,0.1,t=10.)-lp.Rforce(1.,0.1)) < 10.**-8., 'TimeInterPotential does not work as expected'
    assert numpy.fabs(tip.Rforce(1.,0.1,t=200.)-mp.Rforce(1.,0.1)) < 10.**-8., 'TimeInterPotential does not work as expected'
    return None
 def integ(z, TheTa1, TheTa2):
     TheTa = np.sqrt(TheTa1**2 + TheTa2**2)
     R = D_d * TheTa
     MN_Bulge_p = MiyamotoNagaiPotential(amp=MN_M,
                                         a=MN_a,
                                         b=MN_b,
                                         normalize=False)
     Densidad = MN_Bulge_p.dens(R, z)
     Kappa = 2 * Densidad
     return Kappa / (SIGMA_CRIT**2)
Esempio n. 5
0
def minimize_function(x, rho_nfw_target, rho_midplane_target,
                      density_conversion):
    """
    Computes the chi^2 penalty for a galactic potential for the purpose of finding an NFWPotential and MiyamotoNagaiPotential
    circular velocity normalization that yeild the desired midplane and nfw physical densities
    :param x: numpy array of proposed disk and nfw circular velocity normalizations (in that order)
    :param rho_nfw_target: desired nfw_normalization in physical M_sun / pc^2
    :param rho_midplane_target: desired midplane density in physical M_sun / pc^2
    :param density_conversion: a conversion factor between galpy internal density units and physical M_sun / pc^2
    :return: chi^2 penalty
    """

    galactic_potential = [
        PowerSphericalPotentialwCutoff(normalize=0.05, alpha=1.8, rc=1.9 / 8.),
        MiyamotoNagaiPotential(a=3. / 8., b=0.28 / 8., normalize=x[0]),
        NFWPotential(a=2., normalize=x[1])
    ]
    nfw_potential = NFWPotential(a=2., normalize=x[1])

    rho = evaluateDensities(galactic_potential, R=1.,
                            z=0.) * density_conversion
    rho_nfw = evaluateDensities(nfw_potential, R=1, z=0.) * density_conversion
    dx = (rho - rho_midplane_target)**2 / 0.000001**2 + (
        rho_nfw - rho_nfw_target)**2 / 0.000001**2

    return dx**0.5
Esempio n. 6
0
    def setup(self):

        nfw_norm = [0.2, 0.4, 0.6]
        disk_norm = [0.15, 0.3, 0.45]
        scale_height = [0.25, 0.27, 0.29]
        pot_list = []

        for normnfw in nfw_norm:
            for normdisk in disk_norm:
                for scale_h in scale_height:
                    pot = [
                        PowerSphericalPotentialwCutoff(normalize=0.05,
                                                       alpha=1.8,
                                                       rc=1.9 / 8.),
                        MiyamotoNagaiPotential(a=3. / 8.,
                                               b=scale_h / 8.,
                                               normalize=normdisk),
                        NFWPotential(a=2., normalize=normnfw)
                    ]
                    pot_extension = PotentialExtension(
                        pot, 2, 120, 3, compute_action_angle=True)
                    pot_list.append(pot_extension)

        self.disk_norm = disk_norm
        self.nfw_norm = nfw_norm
        self.scale_height = scale_height
        self.pot_list = pot_list

        self.tabulated_potential = TabulatedPotential3D(
            self.pot_list, self.nfw_norm, self.disk_norm, self.scale_height)
Esempio n. 7
0
    def setup(self):

        nfw_norm = [0.2, 0.4, 0.6]
        disk_norm = [0.15, 0.3, 0.45]
        pot_list = []

        for normnfw in nfw_norm:
            for normdisk in disk_norm:
                pot = [
                    PowerSphericalPotentialwCutoff(normalize=0.05,
                                                   alpha=1.8,
                                                   rc=1.9 / 8.),
                    MiyamotoNagaiPotential(a=3. / 8.,
                                           b=0.28 / 8.,
                                           normalize=normdisk),
                    NFWPotential(a=2., normalize=normnfw)
                ]
                pot_extension = PotentialExtension(pot, 2, 120, 10)
                pot_list.append(pot_extension)

        self.disk_norm = disk_norm
        self.nfw_norm = nfw_norm
        self.pot_list = pot_list

        self.tabulated_potential = TabulatedPotential(self.pot_list,
                                                      self.nfw_norm,
                                                      self.disk_norm)
Esempio n. 8
0
def diskmass(out):
    if _dexp:
        dp= DoubleExponentialDiskPotential(normalize=out[0],
                                           hr=out[4],
                                           hz=0.3/_REFR0)
    else:
        dp= MiyamotoNagaiPotential(normalize=out[0],
                                   a=out[4],
                                   b=0.3/_REFR0)
    return integrate.dblquad((lambda x,y: dp.dens(y,x)*y),0.,25./_REFR0,lambda x: 0.,lambda x: 10./_REFR0)[0]*2.*numpy.pi*_convertmass/out[2]**2.
Esempio n. 9
0
def obj(x,data,bp,dp,hp):
    #x=[fd,fh,vc,rs,hdisk]
    if x[0] > 1. or x[0] < 0.: return numpy.finfo(numpy.dtype(numpy.float64)).max
    if x[1] > 1. or x[1] < 0.: return numpy.finfo(numpy.dtype(numpy.float64)).max
    if (1.-x[0]-x[1]) > 1. or (1.-x[0]-x[1]) < 0.: return numpy.finfo(numpy.dtype(numpy.float64)).max
    if x[2] < 0. or x[3] < 0. or x[4] < 0.: return numpy.finfo(numpy.dtype(numpy.float64)).max
    if x[2] > 2. or x[3] > 10. or x[4] > 2.: return numpy.finfo(numpy.dtype(numpy.float64)).max
    if False:
        #Renormalize potentials, intially normalized to 1./3. each
        bp= copy.deepcopy(bp)
        dp= copy.deepcopy(dp)
        hp= copy.deepcopy(hp)
        bp._amp*= (1.-x[0]-x[1])**2.*9.
        dp._amp*= x[0]**2.*9.
        hp._amp*= x[1]**2.*9.
        #Re-define disk scale length and halo scale length
        if _dexp:
            dp._hr= x[4]
        else:
            dp._a= x[4]
        hp._a= x[3]
    else:
        #Set-up
        bp= HernquistPotential(a=0.6/_REFR0,normalize=1.-x[0]-x[1])
        if _dexp:
            dp= DoubleExponentialDiskPotential(normalize=x[0],
                                               hr=x[4],
                                               hz=0.3/_REFR0)
        else:
            dp= MiyamotoNagaiPotential(normalize=x[0],
                                       a=x[4],
                                       b=0.3/_REFR0)
        hp= NFWPotential(normalize=x[1],
                         a=x[3])
    #Re-normalize data
    vcircdata= copy.copy(data)
    vcircdata[:,1]/= x[2]
    vcircdata[:,2]/= x[2]
    #Vcirc chi2
    vcmodel= numpy.zeros(vcircdata.shape[0])
    for ii in range(vcircdata.shape[0]):
        vcmodel[ii]= numpy.sqrt(vcircdata[ii,0]\
                                    *numpy.fabs(evaluateRforces(vcircdata[ii,0],
                                                                0.,[bp,dp,hp])))
    #print vcircdata[:,0], vcmodel
    chi2= numpy.sum((vcircdata[:,1]-vcmodel)**2./vcircdata[:,2]**2.)
    #Add scale length measurement
    chi2+= (x[4]-_diskscale)**2./_diskscaleerr**2.
    #Add dark matter density at the Solar radius
    #print hp.dens(1.,0.),_rhodm*x[2]**2.
    #chi2+= (hp.dens(1.,0.)-_rhodm*x[2]**2.)**2./_rhodmerr**2./x[2]**4.
    return chi2
Esempio n. 10
0
def MWPotential(Ms=0.76, rs=24.8, c=1., T=True):
    '''
        Milky Way potential from Marchetti 2017b -- see galpy for the definitions of the potential components

        Parameters
        ----------
            Ms : float
                NFW profile scale mass in units of e12 Msun
            rs : float
                Radial profile in units of kpc
            c : float
                Axis ratio
            T : bool
                If True, use triaxialNFWPotential
    '''

    # NFW profile
    Ms = Ms * 1e12 * u.Msun
    rs = rs * u.kpc

    #Disk
    Md = 1e11 * u.Msun
    ad = 6.5 * u.kpc
    bd = 260. * u.pc

    #Bulge
    Mb = 3.4 * 1e10 * u.Msun
    Rb = 0.7 * u.kpc

    #BH mass in 1e6 Msun
    Mbh = 4e6 * u.Msun
    if (T):
        halop = TriaxialNFWPotential(amp=Ms, a=rs, c=c, normalize=False)
    else:
        halop = NFWPotential(amp=Ms, a=rs, normalize=False)
    diskp = MiyamotoNagaiPotential(amp=Md, a=ad, b=bd, normalize=False)
    bulgep = HernquistPotential(
        amp=2 * Mb, a=Rb,
        normalize=False)  #Factor 2 because of the galpy definition
    bh = KeplerPotential(amp=Mbh, normalize=False)

    return [halop, diskp, bulgep, bh]
Esempio n. 11
0
    def test_minimize_func(self):

        disk_norm, nfw_norm = solve_normalizations(self.rho_nfw_target,
                                                   self.rho_midplane_target,
                                                   self.density_conversion)

        pot = [
            PowerSphericalPotentialwCutoff(normalize=0.05,
                                           alpha=1.8,
                                           rc=1.9 / 8.),
            MiyamotoNagaiPotential(a=3. / 8., b=0.28 / 8.,
                                   normalize=disk_norm),
            NFWPotential(a=2., normalize=nfw_norm)
        ]
        rho = evaluateDensities(pot, R=1., z=0) * self.density_conversion
        npt.assert_almost_equal(rho, self.rho_midplane_target, 3)

        disk_norm, nfw_norm = solve_normalizations(self.rho_nfw_target,
                                                   self.rho_nfw_target * 0.5,
                                                   self.density_conversion)
        npt.assert_equal(True, disk_norm < 0)
Esempio n. 12
0
from galpy.potential import HernquistPotential
from galpy.potential import LogarithmicHaloPotential
from galpy.potential import MiyamotoNagaiPotential

from .mwahpy_glob import G

#===============================================================================
# CONSTANTS
#===============================================================================

m_bulge = 3.4e10*u.solMass #solar masses
m_disk = 1.0e11*u.solMass
v_halo = 74.61*u.km/u.s #km/s

pot_bulge = HernquistPotential(amp=2*m_bulge, a=0.7*u.kpc, ro=8., vo=220.)
pot_disk = MiyamotoNagaiPotential(amp=G*m_disk, a=6.5*u.kpc, b=0.26*u.kpc, ro=8., vo=220.)
pot_halo = LogarithmicHaloPotential(amp=2*v_halo**2, q=1., core=12.0*u.kpc, ro=8., vo=220.)

#this potential is from Newberg et al. 2010, Orphan Stream Model 5. It's basically Law 2005
mwahpy_default_pot = [pot_bulge, pot_disk, pot_halo]

energy_offset = -60000 #adjusts the energy to be consistent with Donlon et al. 2019

#===============================================================================
# FUNCTIONS
#===============================================================================

#produces a rotation curve for the given potential
def plot_potential(potential, Rrange=[0.01,10.]):
    fig = plt.figure(figsize=(12,8))
    potential.plotRotcurve(Rrange=Rrange, overplot=True)
Esempio n. 13
0
def MWPotentialSCFbar_grow(mbar,
                           Acos,
                           Asin,
                           rs=1.,
                           normalize=False,
                           pat_speed=40.,
                           fin_phi_deg=27.,
                           t_on=-2.,
                           tgrow=2,
                           tstream=5.):
    '''
    SCFbar starts growing at -x Gyr

    tstream : age of the stream/max stripping time
    t_on: time in Gyr in the past at which the bar acquired full strength
    tgrow: no of bar periods it took the bar to grow to full strength starting at tform
            
    '''

    #setup the full strength bar and axisymmetric "bar"
    a = rs / ro
    omegaP = pat_speed * (ro / vo)

    fin_phi = np.radians(fin_phi_deg)

    Tbar = 2. * np.pi / np.abs(omegaP)  #bar period in galpy units.
    t_on = t_on / bovy_conversion.time_in_Gyr(vo, ro)
    tsteady = tgrow * Tbar
    tform = t_on - tsteady  #- because past is negative

    mrat = mbar / 10.**10.  #10^10 mass of bar used to compute Acos and Asin

    static_bar = mySCFPotential(amp=mrat,
                                Acos=Acos,
                                Asin=Asin,
                                a=a,
                                normalize=normalize)

    #Note only m=0 terms are considered
    static_axi_bar = mySCFPotential(amp=mrat,
                                    Acos=np.atleast_3d(Acos[:, :, 0]),
                                    a=a)

    barrot = potential.SolidBodyRotationWrapperPotential(pot=static_bar,
                                                         omega=omegaP,
                                                         ro=ro,
                                                         vo=vo,
                                                         pa=fin_phi)

    if mbar <= 5. * 10**9.:
        MWP2014SCFbar = [
            MWPotential2014[0],
            MiyamotoNagaiPotential(amp=(6.8 - mrat) * 10.**10 * u.Msun,
                                   a=3. / 8.,
                                   b=0.28 / 8.), MWPotential2014[2], barrot
        ]
        turn_physical_off(MWP2014SCFbar)
        #setup the corresponding axisymmetric bar
        MWP2014SCFnobar = [
            MWPotential2014[0],
            MiyamotoNagaiPotential(amp=(6.8 - mrat) * 10.**10 * u.Msun,
                                   a=3. / 8.,
                                   b=0.28 / 8.), MWPotential2014[2],
            static_axi_bar
        ]
        turn_physical_off(MWP2014SCFnobar)

    else:
        MWP2014SCFbar = [
            MiyamotoNagaiPotential(amp=(6.8 + 0.5 - mrat) * 10.**10 * u.Msun,
                                   a=3. / 8.,
                                   b=0.28 / 8.), MWPotential2014[2], barrot
        ]
        turn_physical_off(MWP2014SCFbar)

        MWP2014SCFnobar = [
            MiyamotoNagaiPotential(amp=(6.8 + 0.5 - mrat) * 10.**10 * u.Msun,
                                   a=3. / 8.,
                                   b=0.28 / 8.), MWPotential2014[2],
            static_axi_bar
        ]
        turn_physical_off(MWP2014SCFnobar)

    #if t_on >= t_stream, then stream sees the bar as always on
    if np.abs(t_on) * bovy_conversion.time_in_Gyr(vo, ro) >= tstream:
        return (MWP2014SCFbar, MWP2014SCFnobar)

    elif np.abs(tform) * bovy_conversion.time_in_Gyr(vo, ro) >= tstream:
        print("tform > age of stream")

    elif np.abs(tform) * bovy_conversion.time_in_Gyr(vo, ro) < tstream:

        MWbar_grow = DehnenWrap(amp=1.,
                                pot=MWP2014SCFbar,
                                tform=tform,
                                tsteady=tsteady)
        MWaxibar_destroy = DehnenWrap(amp=-1.,
                                      pot=MWP2014SCFnobar,
                                      tform=tform,
                                      tsteady=tsteady)

        growbarpot = [MWbar_grow, MWP2014SCFnobar, MWaxibar_destroy]

        turn_physical_off(growbarpot)

        return (growbarpot, MWP2014SCFnobar)
Esempio n. 14
0
import sys
import numpy
from galpy.orbit import Orbit
from galpy.potential import  MiyamotoNagaiPotential

if __name__ == '__main__':
    # python orbitint4sigint.py symplec4_c full
    mp= MiyamotoNagaiPotential(normalize=1.,a=0.5,b=0.05)
    ts= numpy.linspace(0.,10000000.,1000001)
    if sys.argv[2] == 'full' or sys.argv[2] == 'planar':
        if sys.argv[2] == 'full':
            o= Orbit([1.,0.1,1.1,0.1,0.1,0.])
        elif sys.argv[2] == 'planar':
            o= Orbit([1.,0.1,1.1,0.1])
        o.integrate(ts,mp,method=sys.argv[1])
    elif sys.argv[2] == 'planardxdv':
        o= Orbit([1.,0.1,1.1,0.1])
        o.integrate_dxdv([0.1,0.1,0.1,0.1],ts,mp,method=sys.argv[1])
    sys.exit(0)
Esempio n. 15
0
    # 'error_colnames':None,
    # 'corr_colnames':None,
}

cart_colnames = {
    # 'main_colnames':None,
    # 'error_colnames':None,
    # 'corr_colnames':None,
}

from galpy.potential import PowerSphericalPotentialwCutoff,\
    MiyamotoNagaiPotential, NFWPotential, verticalfreq, MWPotential2014

scale_height_factor = 2.0
bp = PowerSphericalPotentialwCutoff(alpha=1.8, rc=1.9 / 8., normalize=0.05)
mp = MiyamotoNagaiPotential(a=3. / 8., b=scale_height_factor * 0.28 / 8.,
                            normalize=.6)
np = NFWPotential(a=16 / 8., normalize=.35)
my_mwpotential2014 = [bp, mp, np]
orbit = {
    'potential': my_mwpotential2014, # TC: varied params randomly...
}

special = {
    'component':'sphere',       # parameterisation for the origin
}

advanced = {
    'burnin_steps':500,        # emcee parameters, number of steps for each burnin iteraton
    'sampling_steps':500,
}
Esempio n. 16
0
def MWPotentialSCFbar(mbar,
                      Acos,
                      Asin,
                      rs=1.,
                      normalize=False,
                      pat_speed=40.,
                      fin_phi_deg=27.,
                      t_stream_age=5.,
                      t_on=2.,
                      tgrow=2):
    '''
    t_stream_age : age of the stream/max stripping time
    t_on: time in Gyr in the past at which the bar acquired full strength
    tgrow: no of bar periods it took the bar to grow to full strength starting at tform
            
    '''

    #setup the full strength bar and axisymmetric "bar"
    a = rs / ro
    omegaP = pat_speed * (ro / vo)

    fin_phi = np.radians(fin_phi_deg)

    t_stream_age = t_stream_age / bovy_conversion.time_in_Gyr(vo, ro)

    Tbar = 2. * np.pi / omegaP  #bar period in galpy units.
    t_on = t_on / bovy_conversion.time_in_Gyr(vo, ro)
    tsteady = tgrow * Tbar
    tform = t_on + tsteady

    init_phi = fin_phi - omegaP * t_stream_age / bovy_conversion.time_in_Gyr(
        vo, ro)

    mrat = mbar / 10.**10.  #10^10 mass of bar used to compute Acos and Asin

    static_bar = SCFPotential(amp=mrat,
                              Acos=Acos,
                              Asin=Asin,
                              a=a,
                              normalize=normalize)

    #Note only m=0 terms are considered
    static_axi_bar = SCFPotential(amp=mrat,
                                  Acos=np.atleast_3d(Acos[:, :, 0]),
                                  a=a)

    barrot = potential.SolidBodyRotationWrapperPotential(pot=static_bar,
                                                         omega=omegaP,
                                                         ro=ro,
                                                         vo=vo,
                                                         pa=init_phi)

    if mbar <= 5. * 10**9.:
        MWP2014SCFbar = [
            MWPotential2014[0],
            MiyamotoNagaiPotential(amp=(6.8 - mrat) * 10.**10 * u.Msun,
                                   a=3. / 8.,
                                   b=0.28 / 8.), MWPotential2014[2], barrot
        ]
        turn_physical_off(MWP2014SCFbar)
        #setup the corresponding axisymmetric bar
        MWP2014SCFnobar = [
            MWPotential2014[0],
            MiyamotoNagaiPotential(amp=(6.8 - mrat) * 10.**10 * u.Msun,
                                   a=3. / 8.,
                                   b=0.28 / 8.), MWPotential2014[2],
            static_axi_bar
        ]
        turn_physical_off(MWP2014SCFnobar)

    else:
        MWP2014SCFbar = [
            MiyamotoNagaiPotential(amp=(6.8 + 0.5 - mrat) * 10.**10 * u.Msun,
                                   a=3. / 8.,
                                   b=0.28 / 8.), MWPotential2014[2], barrot
        ]
        turn_physical_off(MWP2014SCFbar)

        MWP2014SCFnobar = [
            MiyamotoNagaiPotential(amp=(6.8 + 0.5 - mrat) * 10.**10 * u.Msun,
                                   a=3. / 8.,
                                   b=0.28 / 8.), MWPotential2014[2],
            static_axi_bar
        ]
        turn_physical_off(MWP2014SCFnobar)

    #setup Dehnen smooth growth wrapper for the bar
    #convert to galpy units

    #if t_on >= t_pal5_age, then Pal 5 sees the bar as always on
    if t_on >= t_stream_age:
        return (MWP2014SCFbar, MWP2014SCFnobar)

    elif tform >= t_stream_age:
        print("tform > age of Pal 5 stream")

    elif tform < t_stream_age:

        #change tform in the past, i.e. instead of from today, to time in the future from 5 Gyr in the past
        tform = t_stream_age - tform
        MWbar_grow = DehnenWrap(amp=1.,
                                pot=MWP2014SCFbar,
                                tform=tform,
                                tsteady=tsteady)
        MWaxibar_destroy = DehnenWrap(amp=-1.,
                                      pot=MWP2014SCFnobar,
                                      tform=tform,
                                      tsteady=tsteady)

        growbarpot = [MWbar_grow, MWP2014SCFnobar, MWaxibar_destroy]

        turn_physical_off(growbarpot)

        return (growbarpot, MWP2014SCFnobar)
Esempio n. 17
0
def test_get_physical():
    #Test that the get_physical function returns the right scaling parameters
    from galpy.util.bovy_conversion import get_physical
    # Potential and variations thereof
    from galpy.potential import MWPotential2014, DehnenBarPotential
    dp = DehnenBarPotential
    assert numpy.fabs(
        get_physical(MWPotential2014[0]).get('ro') - 8.
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a Potential'
    assert numpy.fabs(
        get_physical(MWPotential2014[0]).get('vo') - 220.
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a Potential'
    ro, vo = 9., 230.
    dp = DehnenBarPotential(ro=ro, vo=vo)
    assert numpy.fabs(
        get_physical(dp).get('ro') - ro
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a Potential'
    assert numpy.fabs(
        get_physical(dp).get('vo') - vo
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a Potential'
    assert numpy.fabs(
        get_physical(MWPotential2014).get('ro') - 8.
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a Potential'
    assert numpy.fabs(
        get_physical(MWPotential2014).get('vo') - 220.
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a Potential'
    assert numpy.fabs(
        get_physical(MWPotential2014 + dp).get('ro') - 8.
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a Potential'
    assert numpy.fabs(
        get_physical(MWPotential2014 + dp).get('vo') - 220.
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a Potential'
    assert numpy.fabs(
        get_physical(MWPotential2014 + dp).get('ro') - 8.
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a Potential'
    assert numpy.fabs(
        get_physical(MWPotential2014 + dp).get('vo') - 220.
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a Potential'
    # Orbits
    from galpy.orbit import Orbit
    ro, vo = 10., 210.
    o = Orbit(ro=ro, vo=vo)
    assert numpy.fabs(
        get_physical(o).get('ro') - ro
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for an Orbit'
    assert numpy.fabs(
        get_physical(o).get('vo') - vo
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for an Orbit'
    # even though one shouldn't do this, let's test a list
    assert numpy.fabs(
        get_physical([o, o]).get('ro') - ro
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for an Orbit'
    assert numpy.fabs(
        get_physical([o, o]).get('vo') - vo
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for an Orbit'
    # actionAngle
    from galpy.actionAngle import actionAngleStaeckel
    aAS = actionAngleStaeckel(pot=MWPotential2014, delta=0.45)
    assert numpy.fabs(
        get_physical(aAS).get('ro') - 8.
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for an actionAngle instance'
    assert numpy.fabs(
        get_physical(aAS).get('vo') - 220.
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for an actionAngle instance'
    # This doesn't make much sense, but let's test...
    ro, vo = 19., 130.
    dp = DehnenBarPotential(ro=ro, vo=vo)
    aAS = actionAngleStaeckel(pot=dp, delta=0.45, ro=ro, vo=vo)
    assert numpy.fabs(
        get_physical(aAS).get('ro') - ro
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for an actionAngle instance'
    assert numpy.fabs(
        get_physical(aAS).get('vo') - vo
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for an actionAngle instance'
    # DF
    from galpy.df import quasiisothermaldf
    aAS = actionAngleStaeckel(pot=MWPotential2014, delta=0.45)
    qdf = quasiisothermaldf(1. / 3.,
                            0.2,
                            0.1,
                            1.,
                            1.,
                            aA=aAS,
                            pot=MWPotential2014)
    assert numpy.fabs(
        get_physical(qdf).get('ro') - 8.
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a DF instance'
    assert numpy.fabs(
        get_physical(qdf).get('vo') - 220.
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a DF instance'
    # non-standard ro,vo
    from galpy.potential import MiyamotoNagaiPotential
    ro, vo = 4., 330.
    mp = MiyamotoNagaiPotential(a=0.5, b=0.1, ro=ro, vo=vo)
    aAS = actionAngleStaeckel(pot=mp, delta=0.45, ro=ro, vo=vo)
    qdf = quasiisothermaldf(1. / 3.,
                            0.2,
                            0.1,
                            1.,
                            1.,
                            aA=aAS,
                            pot=mp,
                            ro=ro,
                            vo=vo)
    assert numpy.fabs(
        get_physical(qdf).get('ro') - ro
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a DF instance'
    assert numpy.fabs(
        get_physical(qdf).get('vo') - vo
    ) < 1e-10, 'get_physical does not return the correct unit conversion parameter for a DF instance'
    return None
Esempio n. 18
0
def MWPotentialSCFbar_invert(mbar,
                             Acos,
                             Asin,
                             rs=1.,
                             normalize=False,
                             pat_speed=40.,
                             fin_phi_deg=27.,
                             t_stream_age=5.,
                             t_on=2.,
                             tgrow=2):
    '''
    t_stream_age : age of the stream/max stripping time
    tform: time in Gyr in the past at which the bar started to form
    tgrow: no of bar periods it took the bar to grow to full strength starting at tform
    
        
    '''

    #setup the full strength bar and axisymmetric "bar"
    a = rs / ro
    omegaP = pat_speed * (ro / vo)

    fin_phi = np.radians(fin_phi_deg)

    mrat = mbar / 10.**10.  #10^10 mass of bar used to compute Acos and Asin

    static_bar = potential.SCFPotential(amp=mrat,
                                        Acos=Acos,
                                        Asin=Asin,
                                        a=a,
                                        normalize=normalize)

    #Note only m=0 terms are considered
    static_axi_bar = potential.SCFPotential(amp=mrat,
                                            Acos=np.atleast_3d(Acos[:, :, 0]),
                                            a=a)

    #pa = final phi and omega is negative since we are going back in time
    barrot = potential.SolidBodyRotationWrapperPotential(pot=static_bar,
                                                         omega=-omegaP,
                                                         ro=ro,
                                                         vo=vo,
                                                         pa=fin_phi)

    if mbar <= 5. * 10**9.:
        MWP2014SCFbar = [
            MWPotential2014[0],
            MiyamotoNagaiPotential(amp=(6.8 - mrat) * 10.**10 * u.Msun,
                                   a=3. / 8.,
                                   b=0.28 / 8.), MWPotential2014[2], barrot
        ]
        turn_physical_off(MWP2014SCFbar)
        #setup the corresponding axisymmetric bar
        MWP2014SCFnobar = [
            MWPotential2014[0],
            MiyamotoNagaiPotential(amp=(6.8 - mrat) * 10.**10 * u.Msun,
                                   a=3. / 8.,
                                   b=0.28 / 8.), MWPotential2014[2],
            static_axi_bar
        ]
        turn_physical_off(MWP2014SCFnobar)

    else:
        MWP2014SCFbar = [
            MiyamotoNagaiPotential(amp=(6.8 + 0.5 - mrat) * 10.**10 * u.Msun,
                                   a=3. / 8.,
                                   b=0.28 / 8.), MWPotential2014[2], barrot
        ]
        turn_physical_off(MWP2014SCFbar)

        MWP2014SCFnobar = [
            MiyamotoNagaiPotential(amp=(6.8 + 0.5 - mrat) * 10.**10 * u.Msun,
                                   a=3. / 8.,
                                   b=0.28 / 8.), MWPotential2014[2],
            static_axi_bar
        ]
        turn_physical_off(MWP2014SCFnobar)

    #setup Dehnen smooth growth wrapper for the bar

    #while going back, t_on = tform, then deconstruct the bar to no bar during tsteady
    tform = t_on / bovy_conversion.time_in_Gyr(vo, ro)

    Tbar = 2. * np.pi / omegaP
    tsteady = tgrow * Tbar

    MWaxibar_grow = DehnenWrap(amp=1.,
                               pot=MWP2014SCFnobar,
                               tform=tform,
                               tsteady=tsteady)
    MWbar_destroy = DehnenWrap(amp=-1.,
                               pot=MWP2014SCFbar,
                               tform=tform,
                               tsteady=tsteady)

    growbarpot_invert = [MWP2014SCFbar, MWaxibar_grow, MWbar_destroy]
    turn_physical_off(growbarpot_invert)

    return growbarpot_invert
    # 'main_colnames':None,     # list of names
    # 'error_colnames':None,
    # 'corr_colnames':None,
}

cart_colnames = {
    # 'main_colnames':None,
    # 'error_colnames':None,
    # 'corr_colnames':None,
}

from galpy.potential import MiyamotoNagaiPotential, MWPotential2014
orbit = {
    # 'potential': None,
    #'potential': MWPotential2014,
    #'potential': MiyamotoNagaiPotential(a=0.5,b=0.0375,amp=1.,normalize=1.), # Params from the example webpage. No idea if that's good or not.
    'potential':
    MiyamotoNagaiPotential(a=0.5, b=0.1375, amp=1.,
                           normalize=1.),  # TC: varied params randomly...
}

special = {
    'component': 'sphere',  # parameterisation for the origin
}

advanced = {
    'burnin_steps':
    500,  # emcee parameters, number of steps for each burnin iteraton
    'sampling_steps': 500,
}
Esempio n. 20
0
    r *= ro  #r is now in physical units
    acc = 1e10 * interp_m_over_r2(r) * (conversion._G * 0.001)  #(km/s)^2/kpc
    acc *= (0.001 * conversion._kmsInPcMyr)  #km/s/Myr
    acc *= (-1. / conversion.force_in_kmsMyr(vo, ro))  #in galpy units
    return (acc)


Phi0 = rad_acc(rgrid[0] / ro) * (rgrid[0] / ro)  # in galpy units

ip_halo = potential.interpSphericalPotential(rforce=rad_acc,
                                             rgrid=rgrid / ro,
                                             Phi0=Phi0)
ip_halo.turn_physical_off()

disk_pot = MiyamotoNagaiPotential(amp=5.78 * 10**10 * u.Msun,
                                  a=3.5 * u.kpc,
                                  b=0.5 * u.kpc)
disk_pot.turn_physical_off()

tot_pot = ip_halo + disk_pot

sdf_smooth = mockgd1_util.setup_gd1model(age=options.td,
                                         isob=options.isob,
                                         leading=lead,
                                         sigv=options.sigv)

#if not os.path.exists(folder):
#    os.makedirs(folder,exist_ok=True)

pepperfilename = 'mockgd1_pepper_{}_{}_sigv{}_td{}_{}sampling_InterpSphPotAnalyticDisk.pkl'.format(
    lead_name, sub_type, options.sigv, options.td, len(timpacts))
Esempio n. 21
0
def fitMass():
    #Read data
    vcircdata= numpy.loadtxt('vcirc.txt',comments='#',delimiter='|')
    vcircdata[:,0]/= _REFR0
    vcircdata[:,1]/= _REFV0
    vcircdata[:,2]/= _REFV0
    #Set-up
    bp= HernquistPotential(a=0.6/_REFR0,normalize=1./3.)
    if _dexp:
        dp= DoubleExponentialDiskPotential(normalize=1./3.,
                                           hr=3.25/_REFR0,
                                           hz=0.3/_REFR0)
    else:
        dp= MiyamotoNagaiPotential(normalize=1./3.,
                                   a=3.25/_REFR0,
                                   b=0.3/_REFR0)
    hp= NFWPotential(normalize=1./3.,
                     a=5./_REFR0)
    init= numpy.array([0.6,0.35,218./_REFV0,15./_REFR0,3.25/_REFR0])
    #print _convertrho
    out= optimize.fmin_powell(obj,init,args=(vcircdata,bp,dp,hp),
                              callback=cb)
    print out
    #Calculate mass
    #Halo mass
    halo= halomass(out)
    bulge= halomass(out)
    disk= diskmass(out)
    print halo, disk, bulge, totalmass(out)
    #Sample
    samples= bovy_mcmc.markovpy(out,0.05,
                                (lambda x: -obj(x,vcircdata,bp,dp,hp)),
                                (),
                                isDomainFinite=[[True,True],
                                                [True,True],
                                                [True,True],
                                                [True,True],
                                                [True,True]],
                                domain=[[0.,1.],
                                        [0.,1.],
                                        [0.,2.],
                                        [0.,10.],
                                        [0.,2.]],
                                nwalkers=8,
                                nsamples=10000)
    print "Done with sampling ..."
    print numpy.mean(numpy.array(samples),axis=0)
    print numpy.std(numpy.array(samples),axis=0)
    samples= numpy.random.permutation(samples)[0:500]
    #halo
    totalmasssamples= []
    for s in samples:
        totalmasssamples.append(halomass(s))
    totalmasssamples= numpy.array(totalmasssamples)
    print "halo mass: ", numpy.mean(totalmasssamples), numpy.std(totalmasssamples)
    print sixtyeigthinterval(halomass(out),totalmasssamples,quantile=.68)
    #total
    totalmasssamples= []
    for s in samples:
        totalmasssamples.append(totalmass(s))
    totalmasssamples= numpy.array(totalmasssamples)
    print "total mass: ", numpy.mean(totalmasssamples), numpy.std(totalmasssamples)
    print sixtyeigthinterval(totalmass(out),totalmasssamples,quantile=.68)
    bovy_plot.bovy_print()
    bovy_plot.bovy_hist(totalmasssamples,bins=16,range=[0.,2.])
    bovy_plot.bovy_end_print('totalmass.png')
    return None
    #disk
    totalmasssamples= []
    for s in samples:
        totalmasssamples.append(diskmass(s))
    totalmasssamples= numpy.array(totalmasssamples)
    print "disk mass: ", numpy.mean(totalmasssamples), numpy.std(totalmasssamples)
    #bulge
    totalmasssamples= []
    for s in samples:
        totalmasssamples.append(bulgemass(s))
    totalmasssamples= numpy.array(totalmasssamples)
    print "bulge mass: ", numpy.mean(totalmasssamples), numpy.std(totalmasssamples)
    return None
Esempio n. 22
0
def calc_es():
    savefilename= 'myes.sav'
    if os.path.exists(savefilename):
        savefile= open(savefilename,'rb')
        mye= pickle.load(savefile)
        e= pickle.load(savefile)
        savefile.close()
    else:
       #Read data
        dialect= csv.excel
        dialect.skipinitialspace=True
        reader= csv.reader(open('../data/Dierickx-etal-tab2.txt','r'),delimiter=' ',dialect=dialect)
        vxvs= []
        es= []
        vphis= []
        vxs= []
        vys= []
        vzs= []
        ls= []
        for row in reader:
            thisra= float(row[3])
            thisdec= float(row[4])
            thisd= float(row[17])/1000.
            thispmra= float(row[13])
            thispmdec= float(row[15])
            thisvlos= float(row[11])
            thise= float(row[26])
            vxvs.append([thisra,thisdec,thisd,thispmra,thispmdec,thisvlos])
            es.append(thise)
            vphis.append(float(row[25]))
            vxs.append(float(row[19]))
            vys.append(float(row[21]))
            vzs.append(float(row[23]))
            ls.append(float(row[5]))
        vxvv= nu.array(vxvs)
        e= nu.array(es)
        vphi= nu.array(vphis)
        vx= nu.array(vxs)
        vy= nu.array(vys)
        vz= nu.array(vzs)
        l= nu.array(ls)

        #Define potential
        lp= LogarithmicHaloPotential(normalize=1.)
        mp= MiyamotoNagaiPotential(a=0.5,b=0.0375,amp=1.,normalize=.6)
        np= NFWPotential(a=4.5,normalize=.35)
        hp= HernquistPotential(a=0.6/8,normalize=0.05)
        ts= nu.linspace(0.,20.,10000)
        
        mye= nu.zeros(len(e))
        for ii in range(len(e)):
           #Integrate the orbit
            o= Orbit(vxvv[ii,:],radec=True,vo=220.,ro=8.)
            o.integrate(ts,lp)
            mye[ii]= o.e()
            

        #Save
        savefile= open(savefilename,'wb')
        pickle.dump(mye,savefile)
        pickle.dump(e,savefile)
        savefile.close()

    #plot
    plot.print()
    plot.plot(nu.array([0.,1.]),nu.array([0.,1.]),'k-',
              xlabel=r'$\mathrm{Dierickx\ et\ al.}\ e$',
              ylabel=r'$\mathrm{galpy}\ e$')
    plot.plot(e,mye,'k,',overplot=True)
    plot.end_print('myee.png')

    plot.print()
    plot.hist(e,bins=30,xlabel=r'$\mathrm{Dierickx\ et\ al.}\ e$')
    plot.end_print('ehist.png')

    plot.print()
    plot.hist(mye,bins=30,xlabel=r'$\mathrm{galpy}\ e$')
    plot.end_print('myehist.png')
Esempio n. 23
0
def test_different_potential():
    miya_pot = MiyamotoNagaiPotential(a=0.5, b=0.0375, amp=1., normalize=1.)
    miya_trace_cartesian_orbit = torb.trace_orbit_builder(miya_pot)

    ABS_TOLERANCE = 1e-3
    xyzuvws = np.array([
        [0., 0., 25., 0., 0., 0.],
        [10., 0., -50., 0., 0., 0.],
        [10., 0., -50., 0., 0., -5.],
        [0., 0., 0., 10., 25., 30.,],
    ])
    age = 100.
    times = np.linspace(0, 100, 1001)
    for xyzuvw_start in xyzuvws:
        xyzuvw_end = miya_trace_cartesian_orbit(xyzuvw_start,
                                                times=age,
                                                single_age=True,
                                                )
        xyzuvw_start_again = miya_trace_cartesian_orbit(xyzuvw_end,
                                                        times=-age,
                                                        single_age=True,
                                                        )

        assert np.allclose(xyzuvw_start, xyzuvw_start_again,
                           atol=ABS_TOLERANCE)

        # Confirm that tracing forward with one potential but back with another
        # gives different starting position
        xyzuvw_start_but_with_diff_pot = torb.trace_cartesian_orbit(xyzuvw_end,
                                                                    times=-age,)
        assert not np.allclose(xyzuvw_start, xyzuvw_start_but_with_diff_pot)

    def test_traceforwardThenBack():
        """Check that tracing a point forward then back for the same time step
        returns initial position
        """
        return
        ABS_TOLERANCE = 1e-3
        xyzuvws = np.array([
            [0., 0., 25., 0., 0., 0.],
            # [10.,0.,-50.,0.,0.,0.],
            # [0.,0.,0.,10.,25.,30.,],
        ])
        age = 100.
        times = np.linspace(0, 100, 1001)
        for xyzuvw_start in xyzuvws:
            galpy_start = None
            xyzuvw_end = torb.trace_cartesian_orbit(xyzuvw_start,
                                                    times=age,
                                                    single_age=True,
                                                    )
            xyzuvw_start_again = torb.trace_cartesian_orbit(xyzuvw_end,
                                                            times=-age,
                                                            single_age=True,
                                                            )
            assert np.allclose(xyzuvw_start, xyzuvw_start_again,
                               atol=ABS_TOLERANCE)

    def test_galpy_stationary_conversions():
        """Check if gaply conversions behave as expected where everything
        is at time 0"""

        # Test LSR
        lsr_chron = np.zeros(6)
        lsr_galpy = np.array([1., 0, 1, 0, 0, 0])

        assert np.allclose(lsr_chron,
                           torb.convert_galpycoords2cart(lsr_galpy, ts=0.))
        assert np.allclose(lsr_galpy,
                           torb.convert_cart2galpycoords(lsr_chron, ts=0.))

        # Test galactic centre
        gc_chron = np.array([8000., 0, 0, 0, -220., 0, ])
        gc_galpy = np.ones(6) * 1e-15

        assert np.allclose(gc_chron,
                           torb.convert_galpycoords2cart(gc_galpy, ts=0.))
        assert np.allclose(gc_galpy,
                           torb.convert_cart2galpycoords(gc_chron, ts=0.))

        # Test simple, off origin point
        off_chron = np.array([4000, 8000. * np.sqrt(3) / 2, 0,
                              np.sin(np.pi / 3) * 220.,
                              -np.cos(np.pi / 3) * 220.,
                              0])
        off_galpy = np.array([1., 0, 1, 0, 0, np.pi / 3.])

        assert np.allclose(off_galpy,
                           torb.convert_cart2galpycoords(off_chron, ts=0.))
        assert np.allclose(off_chron,
                           torb.convert_galpycoords2cart(off_galpy, ts=0.))

        # Test random positions
        SPREAD = 100000
        NSAMPLES = int(1e6)
        many_pos_chron = (np.random.rand(NSAMPLES, 6) - 0.5) * SPREAD  # uniform between -10 and 10
        many_pos_galpy = torb.convert_cart2galpycoords(many_pos_chron, ts=0.)

        assert np.allclose(many_pos_chron,
                           torb.convert_galpycoords2cart(many_pos_galpy, ts=0.),
                           atol=1e-2)

    def test_galpy_moving_conversions():
        """Check if gaply conversions behave as expected where time
        is allowed to vary."""
        lsr_chron = np.zeros(6)
        lsr_galpy = np.array([1., 0, 1, 0, 0, 0])
        # Incorporate positive time into lsr position checks
        NSTEPS = 10
        galpy_times = np.linspace(0., 2 * np.pi, NSTEPS)
        lsrs_chron = np.repeat(lsr_chron, NSTEPS).reshape(6, -1).T
        lsrs_galpy = np.repeat(lsr_galpy, NSTEPS).reshape(6, -1).T
        lsrs_galpy[:, -1] = galpy_times
        chron_times = torb.convert_bovytime2myr(galpy_times)

        assert np.allclose(
            lsrs_chron,
            torb.convert_galpycoords2cart(lsrs_galpy, ts=galpy_times))
        assert np.allclose(
            lsrs_galpy,
            torb.convert_cart2galpycoords(lsrs_chron, ts=chron_times)
        )

        # Incorporate negative time into lsr position checks
        galpy_times = np.linspace(0., -2 * np.pi, NSTEPS)
        lsrs_chron = np.repeat(lsr_chron, NSTEPS).reshape(6, -1).T
        lsrs_galpy = np.repeat(lsr_galpy, NSTEPS).reshape(6, -1).T
        lsrs_galpy[:, -1] = galpy_times
        chron_times = torb.convert_bovytime2myr(galpy_times)

        assert np.allclose(
            lsrs_chron,
            torb.convert_galpycoords2cart(lsrs_galpy, ts=galpy_times))
        assert np.allclose(
            lsrs_galpy,
            torb.convert_cart2galpycoords(lsrs_chron, ts=chron_times)
        )

        # Test random positions with random times
        SPREAD = int(1e4)  # pc
        NSAMPLES = 100
        many_pos_chron = (np.random.rand(NSAMPLES, 6) - 0.5) * SPREAD  # uniform between -10 and 10
        many_chron_times = np.random.rand(NSAMPLES) * 100  # Myr
        many_pos_galpy = torb.convert_cart2galpycoords(
            many_pos_chron, ts=many_chron_times
        )
        many_galpy_times = torb.convert_myr2bovytime(many_chron_times)

        for i in range(NSAMPLES):
            assert np.allclose(many_pos_chron[i],
                               torb.convert_galpycoords2cart(
                                   many_pos_galpy[i], ts=many_galpy_times[i]
                               ),
                               atol=1e-2)

    def test_careful_traceback_and_forward():
        """Step by step, project orbit forward, then backward"""
        bovy_times = np.array([0., np.pi / 3.])
        chron_times = torb.convert_bovytime2myr(bovy_times)

        init_pos_chron = np.array([
            4000, 8000. * np.sqrt(3) / 2, 0,
                  np.sin(np.pi / 3) * 220.,
                  -np.cos(np.pi / 3) * 220.,
            0
        ])
        init_pos_galpy = torb.convert_cart2galpycoords(init_pos_chron, ts=0.)

        assert np.allclose(np.array([1., 0, 1, 0, 0, np.pi / 3.]),
                           init_pos_galpy)

        o = Orbit(vxvv=init_pos_galpy, ro=8., vo=220.)
        o.integrate(bovy_times, MWPotential2014, method='odeint')

        orbit_galpy = o.getOrbit()
        assert np.allclose(init_pos_galpy, orbit_galpy[0])
        assert np.allclose(init_pos_galpy
                           + np.array([0., 0., 0., 0., 0., bovy_times[-1]]),
                           orbit_galpy[-1])

        orbit_chron = torb.convert_galpycoords2cart(orbit_galpy,
                                                    ts=bovy_times)
        assert np.allclose(init_pos_chron, orbit_chron[0])
        assert np.allclose(init_pos_chron,
                           orbit_chron[-1])

        # Setup for backwards time integration
        # Currently at time of PI/3
        back_init_pos_chron = orbit_chron[-1]
        back_init_pos_galpy = torb.convert_cart2galpycoords(
            back_init_pos_chron,
            bovy_times=bovy_times[-1],
        )

        assert np.allclose(back_init_pos_galpy,
                           torb.convert_cart2galpycoords(
                               back_init_pos_chron,
                               bovy_times=bovy_times[-1]
                           ))

        back_o = Orbit(vxvv=back_init_pos_galpy, ro=8., vo=220.)
        back_o.integrate(-1 * bovy_times, MWPotential2014, method='odeint')

        back_orbit_galpy = back_o.getOrbit()

        assert np.allclose(back_init_pos_galpy, back_orbit_galpy[0])
        assert np.allclose(back_init_pos_galpy
                           - np.array([0., 0., 0., 0., 0., bovy_times[-1]]),
                           back_orbit_galpy[-1])
        assert np.allclose(init_pos_galpy, back_orbit_galpy[-1])

        back_orbit_chron = torb.convert_galpycoords2cart(
            back_orbit_galpy,
            ts=bovy_times[::-1],
        )

        assert np.allclose(init_pos_chron, back_orbit_chron[-1])

    def test_traceback_and_forward():
        """The test that shows things are broken"""
        time = 10.  # Myr
        times = np.array([0., 10.])
        init_pos_chron = np.array([10., 0., 30., 0., 0., 0.])
        # init_pos_chron = np.zeros(6)
        init_pos_chron = np.array([4000, 8000. * np.sqrt(3) / 2, 0,
                                   np.sin(np.pi / 3) * 220.,
                                   -np.cos(np.pi / 3) * 220.,
                                   0])
        final_pos_chron = torb.trace_cartesian_orbit(
            init_pos_chron, times=times, single_age=False)[-1]
        final_pos_chron = torb.traceforward_from_now(
            init_pos_chron, time=time,
        )
        assert np.allclose(
            init_pos_chron,
            torb.traceback_to_now(
                final_pos_chron, time,
            ),
            atol=1e-5
        )

    def test_multi_traceback_and_forward():
        np.random.seed(0)
        NPOSITIONS = 10
        init_positions = np.random.rand(NPOSITIONS, 6) * 20 - 10
        time_spans = np.random.rand(NPOSITIONS) * 30
        for pos, time in zip(init_positions, time_spans):
            final_pos = torb.traceforward_from_now(pos, time)
            init_pos = torb.traceback_to_now(final_pos, time)
            assert np.allclose(pos, init_pos, atol=1e-3)

        for pos, time in zip(init_positions, time_spans):
            print('time: {}'.format(time))
            final_pos = torb.traceback_from_now(pos, time)
            init_pos = torb.traceforward_to_now(final_pos, time)
            assert np.allclose(pos, init_pos, atol=1e-3)

    def test_interval_tracing():
        np.random.seed(0)
        start = np.random.rand(6) * 20 - 10

        time_steps = [3., -10., -3., 10]

        current_pos = start
        for time_step in time_steps:
            current_pos = torb.base_trace_cartesian_orbit(
                current_pos,
                end_time=time_step,
            )
        assert np.allclose(start, current_pos, atol=1e-3)

    def test_interval_tracing_orig():
        np.random.seed(0)
        start = np.random.rand(6) * 20 - 10

        time_steps = [3., -10., -3., 10]

        current_pos = start
        for time_step in time_steps:
            current_pos = torb.trace_cartesian_orbit(
                current_pos,
                times=time_step,
                single_age=True,
            )
        assert np.allclose(start, current_pos, 1e-3)
Esempio n. 24
0
def calcj(rotcurve):
    if rotcurve == 'flat':
        savefilename = 'myjs.sav'
    elif rotcurve == 'power':
        savefilename = 'myjs_power.sav'
    if os.path.exists(savefilename):
        savefile = open(savefilename, 'rb')
        myjr = pickle.load(savefile)
        myjp = pickle.load(savefile)
        mywr = pickle.load(savefile)
        mywp = pickle.load(savefile)
        mye = pickle.load(savefile)
        myzmax = pickle.load(savefile)
        e = pickle.load(savefile)
        zmax = pickle.load(savefile)
        savefile.close()
    else:
        dialect = csv.excel
        dialect.skipinitialspace = True
        reader = csv.reader(open('../data/gcs.tsv', 'r'),
                            delimiter='|',
                            dialect=dialect)
        vxvs = []
        es = []
        zmaxs = []
        for row in reader:
            if row[0][0] == '#':
                continue
            thisra = row[0]
            thisdec = row[1]
            thisd = read_float(row[2]) / 1000.
            if thisd > 0.2: continue
            thisu = read_float(row[3])
            thisv = read_float(row[4])
            thisw = read_float(row[5])
            thise = read_float(row[6])
            thiszmax = read_float(row[7])
            if thisd == -9999 or thisu == -9999 or thisv == -9999 or thisw == -9999:
                continue
            vxvs.append([
                hms_to_rad(thisra),
                dms_to_rad(thisdec), thisd, thisu, thisv, thisw
            ])
            es.append(thise)
            zmaxs.append(thiszmax)
        vxvv = nu.array(vxvs)
        e = nu.array(es)
        zmax = nu.array(zmaxs)

        #Define potential
        lp = LogarithmicHaloPotential(normalize=1.)
        pp = PowerSphericalPotential(normalize=1., alpha=-2.)
        mp = MiyamotoNagaiPotential(a=0.5, b=0.0375, amp=1., normalize=.6)
        np = NFWPotential(a=4.5, normalize=.35)
        hp = HernquistPotential(a=0.6 / 8, normalize=0.05)
        ts = nu.linspace(0., 20., 10000)

        myjr = nu.zeros(len(e))
        myjp = nu.zeros(len(e))
        mywr = nu.zeros(len(e))
        mywp = nu.zeros(len(e))
        mye = nu.zeros(len(e))
        myzmax = nu.zeros(len(e))
        for ii in range(len(e)):
            #Integrate the orbit
            o = Orbit(vxvv[ii, :], radec=True, uvw=True, vo=220., ro=8.)
            #o.integrate(ts,[mp,np,hp])
            if rotcurve == 'flat':
                o.integrate(ts, lp)
                mye[ii] = o.e()
                myzmax[ii] = o.zmax() * 8.
                print e[ii], mye[ii], zmax[ii], myzmax[ii]
                o = o.toPlanar()
                myjr[ii] = o.jr(lp)[0]
            else:
                o = o.toPlanar()
                myjr[ii] = o.jr(pp)[0]
            myjp[ii] = o.jp()[0]
            mywr[ii] = o.wr()[0]
            mywp[ii] = o.wp()

        #Save
        savefile = open(savefilename, 'wb')
        pickle.dump(myjr, savefile)
        pickle.dump(myjp, savefile)
        pickle.dump(mywr, savefile)
        pickle.dump(mywp, savefile)
        pickle.dump(mye, savefile)
        pickle.dump(myzmax, savefile)
        pickle.dump(e, savefile)
        pickle.dump(zmax, savefile)
        savefile.close()

    #plot
    if rotcurve == 'flat':
        plot.bovy_print()
        plot.bovy_plot(nu.array([0., 1.]),
                       nu.array([0., 1.]),
                       'k-',
                       xlabel=r'$\mathrm{Holmberg\ et\ al.}\ e$',
                       ylabel=r'$\mathrm{galpy}\ e$')
        plot.bovy_plot(e, mye, 'k,', overplot=True)
        plot.bovy_end_print('myee.png')

        plot.bovy_print()
        plot.bovy_plot(
            nu.array([0., 2.5]),
            nu.array([0., 2.5]),
            'k-',
            xlabel=r'$\mathrm{Holmberg\ et\ al.}\ z_{\mathrm{max}}$',
            ylabel=r'$\mathrm{galpy}\ z_{\mathrm{max}}$')
        plot.bovy_plot(zmax, myzmax, 'k,', overplot=True)
        plot.bovy_end_print('myzmaxzmax.png')

    plot.bovy_print()
    plot.bovy_plot(myjp,
                   myjr / 2. / nu.pi,
                   'k,',
                   xlabel=r'$J_{\phi}$',
                   ylabel=r'$J_R/2\pi$',
                   xrange=[0.7, 1.3],
                   yrange=[0., 0.05])
    if rotcurve == 'flat':
        plot.bovy_end_print('jrjp.png')
    else:
        plot.bovy_end_print('jrjp_power.png')