Esempio n. 1
0
 def plot_Xscale_3D(self, ax, gp):
     rmin = np.log10(min(gp.xipol))
     rmax = np.log10(max(gp.xipol))
     gp.xfine = np.logspace(rmin, rmax, gp.nfine)
     if gp.investigate == 'walk':
         if gp.pops == 1:
             rhodm, nu1 = ga.rho_walk(gp.xepol, gp)
         else:
             rhodm, nu1, nu2 = ga.rho_walk(gp.xepol, gp)
     elif gp.investigate == 'gaia':
         rhodm, nu1 = ga.rho_gaia(gp.xepol, gp)
     for pop in range(gp.pops):
         # use our models
         nuprof = self.Mmedi.get_prof('nu', pop+1)
         tck = splrep(gp.xepol, nuprof)
         nuproffine = splev(gp.xfine, tck)
         if gp.investigate == 'walk' or gp.investigate == 'gaia':
             # or rather use analytic values, where available
             if pop == 0:
                 nuprof = nu1
             elif pop == 1:
                 nuprof = nu2
         if gp.geom == 'sphere':
             Mprof = gip.rho_SUM_Mr(gp.xfine, nuproffine)
             Mmax = max(Mprof) # Mprof[-1]
             ihalf = -1
             for kk in range(len(Mprof)):
                 # half-light radius (3D) is where mass is more than half
                 # ihalf gives the iindex of where this happens
                 if Mprof[kk] >= Mmax/2 and ihalf < 0:
                     xx = (gp.xfine[kk-1]+gp.xfine[kk])/2
                     print('rhalf = ', xx, ' pc')
                     ax.axvline(xx, color='green', lw=0.5, alpha=0.7)
                     ihalf = kk
Esempio n. 2
0
def run(gp):
    import gr_params
    gpr = gr_params.grParams(gp)
    xall,yall,zall = np.loadtxt(gp.files.get_com_file(0),skiprows=1,\
                                usecols=(0,1,2),unpack=True) # 2*[rscale0]
    rscale0 = gf.read_Xscale(gp.files.get_scale_file(0)+'_3D')
    xall *= rscale0
    yall *= rscale0
    zall *= rscale0

    # calculate 3D
    r = np.sqrt(xall**2+yall**2+zall**2) #[pc]

    # set number and size of (linearly spaced) bins
    rmin = 0. # [pc]
    rmax = max(r) if gp.maxR < 0 else 1.0*gp.maxR           # [pc]
    print('rmax [rscale] = ', rmax)
    r = r[(r<rmax)] # [pc]

    binmin, binmax, rbin = gh.determine_radius(r, rmin, rmax, gp) # [pc]
    vol = volume_spherical_shell(binmin, binmax, gp) # [pc^3]

    for pop in range(gp.pops+1):
        print('#######  working on component ',pop)
        print('input: ',gp.files.get_com_file(pop)+'_3D')
        # start from data centered on COM already:
        if gf.bufcount(gp.files.get_com_file(pop)+'_3D')<2: continue
        x,y,z,v = np.loadtxt(gp.files.get_com_file(pop)+'_3D',\
                           skiprows=1,usecols=(0,1,2,3),unpack=True)
        # 3*[rscale], [km/s]
        rscalei = gf.read_Xscale(gp.files.get_scale_file(pop)) # [pc]
        x *= rscalei
        y *= rscalei
        z *= rscalei
        # calculate 2D radius on the skyplane
        r = np.sqrt(x**2+y**2+z**2) # [pc]

        # set maximum radius (if gp.maxR is set)
        rmax = max(r) if gp.maxR<0 else 1.0*gp.maxR # [pc]
        print('rmax [pc] = ', rmax)
        sel = (r<=rmax)
        x = x[sel]; y = y[sel]; z = z[sel]; v = v[sel]; r = r[sel] # [rscale]
        totmass_tracers = 1.*len(x) # [Munit], Munit = 1/star

        rs = r                   # + possible starting offset, [rscale]
        vlos = v                 # + possible starting offset, [km/s]

        gf.write_tracer_file(gp.files.get_ntracer_file(pop)+'_3D', totmass_tracers)
        de, em = gf.write_headers_3D(gp, pop)

        # gpr.n=30 iterations for getting random picked radius values
        density = np.zeros((gp.nipol,gpr.n))
        a       = np.zeros((gp.nipol,gpr.n)) # shared by density, siglos, kappa calcs
        for k in range(gpr.n):
            rsi = gpr.Rerr * np.random.randn(len(rs)) + rs # [pc]
            vlosi = gpr.vrerr*np.random.randn(len(vlos)) + vlos # [km/s]
            for i in range(gp.nipol):
                ind1 = np.argwhere(np.logical_and(rsi>=binmin[i], rsi<binmax[i])).flatten() # [1]
                density[i][k] = (1.*len(ind1))/vol[i]*totmass_tracers # [Munit/rscale^2]
                vlos1 = vlosi[ind1]                           # [km/s]
                a[i][k] = 1.*len(ind1)                        # [1]


        dens0 = np.sum(density[0])/(1.*gpr.n) # [Munit/rscale^3]
        print('dens0 = ',dens0,' [Munit/rscale^3]')

        dens0pc = dens0/rscale0**3
        gf.write_Sig_scale(gp.files.get_scale_file(pop)+'_3D', dens0pc, totmass_tracers)

        tpb0   = np.sum(a[0])/float(gpr.n)     # [1] tracers per bin
        denserr0 = dens0/np.sqrt(tpb0)       # [Munit/rscale^3]
        p_dens  = np.zeros(gp.nipol)
        p_edens = np.zeros(gp.nipol)
        for b in range(gp.nipol):
            dens = np.sum(density[b])/float(gpr.n) # [Munit/rscale^3]
            tpb  = np.sum(a[b])/float(gpr.n)       # [1]
            denserr = dens/np.sqrt(tpb)            # [Munit/rscale^3]

            if(np.isnan(denserr)):
                p_dens[b] = p_dens[b-1]                          # [1]
                p_edens[b]= p_edens[b-1]                         # [1]
            else:
                p_dens[b] = dens/dens0                           # [1]
                p_edens[b]= denserr/dens0 # [1] #100/rbin would be artificial guess

            print(rbin[b], binmin[b], binmax[b], p_dens[b], p_edens[b], file=de)
            # [rscale], 2*[dens0]
            indr = (r<binmax[b])
            menclosed = float(np.sum(indr))/totmass_tracers # for normalization to 1
            # [totmass_tracers]
            merr = menclosed/np.sqrt(tpb) # artificial menclosed/10 # [totmass_tracers]
            print(rbin[b], binmin[b], binmax[b], menclosed, merr, file=em)
            # [rscale], 2*[totmass_tracers]
        de.close()
        em.close()

        if gpr.showplots:
            print('plotting for pop ', pop)
            #show_plots_dens(rbin, p_dens, p_edens, gp)
            mf1 = 0.02 #1/totmass_tracers
            mf2 = 0.02
            rho_dm, rho_star1, rho_star2 = ga.rho_walk(rbin*rscale0, gp, mf1, mf2)

            if pop == 0:
                loglog(rbin*rscale0, rho_star1+rho_star2, 'k.-', lw=0.5)
            elif pop == 1:
                loglog(rbin*rscale0, rho_star1, 'b.-', lw = 0.5)
            elif pop == 2:
                loglog(rbin*rscale0, rho_star2, 'g.-', lw = 0.5)

            loglog(rbin*rscale0, dens0pc*p_dens, 'r.-')
            pdb.set_trace()
            clf()
Esempio n. 3
0
 def set_analytic(self, x0, gp):
     r0 = x0 # [pc], spherical case
     self.analytic.x0 = r0
     anbeta = []; annu = [];  anSig = []
     if gp.investigate == 'gaia':
         anrho = ga.rho_gaia(r0, gp)[0]
         anM = gip.rho_SUM_Mr(r0, anrho)
         annr = ga.nr3Dtot_gaia(r0, gp)
         tmp_annu = ga.rho_gaia(r0, gp)[1]
         annu.append( tmp_annu )
         anSig.append( gip.rho_INT_Sig(r0, tmp_annu, gp) )
         for pop in np.arange(1, gp.pops+1):
             beta = ga.beta_gaia(r0, gp)[pop]
             anbeta.append(beta)
             nu = ga.rho_gaia(r0,gp)[pop]
             annu.append(nu)
             anSig.append(gip.rho_INT_Sig(r0, nu, gp))
     elif gp.investigate == 'walk':
         anrho = ga.rho_walk(r0, gp)[0]
         anM = gip.rho_SUM_Mr(r0, anrho)
         annr = ga.nr3Dtot_deriv_walk(r0, gp) # TODO too high in case of core
         tmp_annu = ga.rho_walk(r0, gp)[1]
         annu.append( tmp_annu )
         anSig.append( gip.rho_INT_Sig(r0, tmp_annu, gp) )
         for pop in np.arange(1, gp.pops+1):
             beta = ga.beta_walk(r0, gp)[pop]
             anbeta.append(beta)
             nu = ga.rho_walk(r0, gp)[pop]
             dum,dum,dum,nudat,nuerr = np.transpose(np.loadtxt(gp.files.nufiles[pop], unpack=False, skiprows=1))
             locrhalf = np.argmin(abs(gp.xipol-gp.Xscale[pop]))
             nuhalf = nudat[locrhalf]*gp.nu0pc[pop]
             annuhalf = nu[np.argmin(abs(r0-locrhalf))]
             annu.append(nu*nuhalf/annuhalf)
             dum,dum,dum,Sigdat,Sigerr = np.transpose(np.loadtxt(gp.files.Sigfiles[pop], unpack=False, skiprows=1))
             locrhalf = np.argmin(abs(gp.xipol-gp.Xscale[pop]))
             Sighalf = Sigdat[locrhalf]*gp.Sig0pc[pop]
             Sig = gip.rho_INT_Sig(r0, nu, gp)
             anSighalf = Sig[np.argmin(abs(r0-locrhalf))]
             anSig.append(Sig*Sighalf/anSighalf)
     elif gp.investigate == 'triax':
         anrho = ga.rho_triax(r0, gp) # one and only
         anM = gip.rho_SUM_Mr(r0, anrho)
         annr = ga.nr3Dtot_deriv_triax(r0, gp)
         tmp_annu = ga.rho_triax(r0, gp) # TODO, M/L=1 assumed here, wrong
         annu.append(tmp_annu)
         anSig.append( gip.rho_INT_Sig(r0, tmp_annu, gp))
         for pop in np.arange(1, gp.pops+1):
             beta = ga.beta_triax(r0)
             anbeta.append(beta)
             nu = ga.rho_triax(r0, gp) # TODO, assumes M/L=1
             annu.append(nu)
             anSig.append( gip.rho_INT_Sig(r0, nu, gp))
     self.analytic.set_prof('rho', anrho, 0, gp)
     self.analytic.set_prof('M', anM, 0, gp)
     self.analytic.set_prof('nr', annr, 0, gp)
     self.analytic.set_prof('nu', annu[0], 0, gp)
     self.analytic.set_prof('nrnu', -gh.derivipol(np.log(annu[0]), np.log(r0)), 0, gp)
     self.analytic.set_prof('Sig', anSig[0], 0, gp)
     for pop in np.arange(1, gp.pops+1):
         self.analytic.set_prof('beta', anbeta[pop-1], pop, gp)
         self.analytic.set_prof('betastar', anbeta[pop-1]/(2.-anbeta[pop-1]), pop, gp)
         self.analytic.set_prof('nu', annu[pop], pop, gp)
         nrnu = -gh.derivipol(np.log(annu[pop]), np.log(r0))
         self.analytic.set_prof('nrnu', nrnu, pop, gp)
         self.analytic.set_prof('Sig', anSig[pop] , pop, gp)#/ Signorm, pop, gp)
         self.analytic.set_prof('sig', -np.ones(len(r0)), pop, gp)
     return
Esempio n. 4
0
def run(gp):
    import gr_params
    gpr = gr_params.grParams(gp)
    xall,yall,zall = np.loadtxt(gp.files.get_com_file(0),skiprows=1,\
                                usecols=(0,1,2),unpack=True) # 2*[rscale0]
    rscale0 = gf.read_Xscale(gp.files.get_scale_file(0) + '_3D')
    xall *= rscale0
    yall *= rscale0
    zall *= rscale0

    # calculate 3D
    r = np.sqrt(xall**2 + yall**2 + zall**2)  #[pc]

    # set number and size of (linearly spaced) bins
    rmin = 0.  # [pc]
    rmax = max(r) if gp.maxR < 0 else 1.0 * gp.maxR  # [pc]
    print('rmax [rscale] = ', rmax)
    r = r[(r < rmax)]  # [pc]

    binmin, binmax, rbin = gh.determine_radius(r, rmin, rmax, gp)  # [pc]
    vol = volume_spherical_shell(binmin, binmax, gp)  # [pc^3]

    for pop in range(gp.pops + 1):
        print('#######  working on component ', pop)
        print('input: ', gp.files.get_com_file(pop) + '_3D')
        # start from data centered on COM already:
        if gf.bufcount(gp.files.get_com_file(pop) + '_3D') < 2: continue
        x,y,z,v = np.loadtxt(gp.files.get_com_file(pop)+'_3D',\
                           skiprows=1,usecols=(0,1,2,3),unpack=True)
        # 3*[rscale], [km/s]
        rscalei = gf.read_Xscale(gp.files.get_scale_file(pop))  # [pc]
        x *= rscalei
        y *= rscalei
        z *= rscalei
        # calculate 2D radius on the skyplane
        r = np.sqrt(x**2 + y**2 + z**2)  # [pc]

        # set maximum radius (if gp.maxR is set)
        rmax = max(r) if gp.maxR < 0 else 1.0 * gp.maxR  # [pc]
        print('rmax [pc] = ', rmax)
        sel = (r <= rmax)
        x = x[sel]
        y = y[sel]
        z = z[sel]
        v = v[sel]
        r = r[sel]  # [rscale]
        totmass_tracers = 1. * len(x)  # [Munit], Munit = 1/star

        rs = r  # + possible starting offset, [rscale]
        vlos = v  # + possible starting offset, [km/s]

        gf.write_tracer_file(
            gp.files.get_ntracer_file(pop) + '_3D', totmass_tracers)
        de, em = gf.write_headers_3D(gp, pop)

        # gpr.n=30 iterations for getting random picked radius values
        density = np.zeros((gp.nipol, gpr.n))
        a = np.zeros(
            (gp.nipol, gpr.n))  # shared by density, siglos, kappa calcs
        for k in range(gpr.n):
            rsi = gpr.Rerr * np.random.randn(len(rs)) + rs  # [pc]
            vlosi = gpr.vrerr * np.random.randn(len(vlos)) + vlos  # [km/s]
            for i in range(gp.nipol):
                ind1 = np.argwhere(
                    np.logical_and(rsi >= binmin[i],
                                   rsi < binmax[i])).flatten()  # [1]
                density[i][k] = (
                    1. *
                    len(ind1)) / vol[i] * totmass_tracers  # [Munit/rscale^2]
                vlos1 = vlosi[ind1]  # [km/s]
                a[i][k] = 1. * len(ind1)  # [1]

        dens0 = np.sum(density[0]) / (1. * gpr.n)  # [Munit/rscale^3]
        print('dens0 = ', dens0, ' [Munit/rscale^3]')

        dens0pc = dens0 / rscale0**3
        gf.write_Sig_scale(
            gp.files.get_scale_file(pop) + '_3D', dens0pc, totmass_tracers)

        tpb0 = np.sum(a[0]) / float(gpr.n)  # [1] tracers per bin
        denserr0 = dens0 / np.sqrt(tpb0)  # [Munit/rscale^3]
        p_dens = np.zeros(gp.nipol)
        p_edens = np.zeros(gp.nipol)
        for b in range(gp.nipol):
            dens = np.sum(density[b]) / float(gpr.n)  # [Munit/rscale^3]
            tpb = np.sum(a[b]) / float(gpr.n)  # [1]
            denserr = dens / np.sqrt(tpb)  # [Munit/rscale^3]

            if (np.isnan(denserr)):
                p_dens[b] = p_dens[b - 1]  # [1]
                p_edens[b] = p_edens[b - 1]  # [1]
            else:
                p_dens[b] = dens / dens0  # [1]
                p_edens[
                    b] = denserr / dens0  # [1] #100/rbin would be artificial guess

            print(rbin[b],
                  binmin[b],
                  binmax[b],
                  p_dens[b],
                  p_edens[b],
                  file=de)
            # [rscale], 2*[dens0]
            indr = (r < binmax[b])
            menclosed = float(
                np.sum(indr)) / totmass_tracers  # for normalization to 1
            # [totmass_tracers]
            merr = menclosed / np.sqrt(
                tpb)  # artificial menclosed/10 # [totmass_tracers]
            print(rbin[b], binmin[b], binmax[b], menclosed, merr, file=em)
            # [rscale], 2*[totmass_tracers]
        de.close()
        em.close()

        if gpr.showplots:
            print('plotting for pop ', pop)
            #show_plots_dens(rbin, p_dens, p_edens, gp)
            mf1 = 0.02  #1/totmass_tracers
            mf2 = 0.02
            rho_dm, rho_star1, rho_star2 = ga.rho_walk(rbin * rscale0, gp, mf1,
                                                       mf2)

            if pop == 0:
                loglog(rbin * rscale0, rho_star1 + rho_star2, 'k.-', lw=0.5)
            elif pop == 1:
                loglog(rbin * rscale0, rho_star1, 'b.-', lw=0.5)
            elif pop == 2:
                loglog(rbin * rscale0, rho_star2, 'g.-', lw=0.5)

            loglog(rbin * rscale0, dens0pc * p_dens, 'r.-')
            pdb.set_trace()
            clf()