def test_cls_camb(dict_file='orphics/tests/lcdm_baseline.pkl', atol=1e-20, rtol=1e-8): """ This reads from file with a given filename, and compares the cls from the file with cls created with pars from the same file. """ f = open(dict_file, "rb") d = pickle.load(f) lmax = d['lmax'] pars = d['pars'] cl_base = d['lensed_cls'] #pars['omch2']= 0.12 cl = {} cosmo = cosmology.Cosmology(lmax=lmax, paramDict=pars) ells = np.arange(2, lmax - 1000) cl['ell'] = ells cl['tt'] = cosmo.theory.lCl('TT', ells) cl['ee'] = cosmo.theory.lCl('EE', ells) cl['te'] = cosmo.theory.lCl('TE', ells) cl['bb'] = cosmo.theory.lCl('BB', ells) assert compare_cl_dicts(cl, cl_base, atol=atol, rtol=rtol)
def dcl(step=0.1): params = cosmology.defaultCosmology.copy() params['w0'] = -1 + step / 2. cc = cosmology.Cosmology(params, low_acc=low_acc, pickling=False, skipPower=True, skip_growth=True, lmax=7000) ucltt_up = cc.theory.uCl('TT', ells) lcltt_up = cc.theory.lCl('TT', ells) uclte_up = cc.theory.uCl('TE', ells) lclte_up = cc.theory.lCl('TE', ells) uclee_up = cc.theory.uCl('EE', ells) lclee_up = cc.theory.lCl('EE', ells) params = cosmology.defaultCosmology.copy() params['w0'] = -1 - step / 2. cc = cosmology.Cosmology(params, low_acc=low_acc, pickling=False, skipPower=True, skip_growth=True, lmax=7000) ucltt_dn = cc.theory.uCl('TT', ells) lcltt_dn = cc.theory.lCl('TT', ells) uclte_dn = cc.theory.uCl('TE', ells) lclte_dn = cc.theory.lCl('TE', ells) uclee_dn = cc.theory.uCl('EE', ells) lclee_dn = cc.theory.lCl('EE', ells) dcltt = (ucltt_up - ucltt_dn) / step dlcltt = (lcltt_up - lcltt_dn) / step dclte = (uclte_up - uclte_dn) / step dclee = (uclee_up - uclee_dn) / step return dcltt, dclte, dclee #,dlcltt
def dcl(w): params = cosmology.defaultCosmology.copy() params['w0'] = w cc = cosmology.Cosmology(params,low_acc=low_acc,pickling=False,skipPower=True,skip_growth=True) ucltt = cc.theory.uCl('TT',ells) lcltt = cc.theory.lCl('TT',ells) uclte = cc.theory.uCl('TE',ells) lclte = cc.theory.lCl('TE',ells) uclee = cc.theory.uCl('EE',ells) lclee = cc.theory.lCl('EE',ells) return ucltt,lcltt,uclte,lclte,uclee,lclee
def test_pgm(): import halomodel as mmhm from orphics import cosmology cc = cosmology.Cosmology(hmvec.default_params, skipCls=True, low_acc=False) mmhmod = mmhm.HaloModel(cc) mthresh = np.array([10.5]) zs = np.array([0., 1., 2., 3.]) ms = np.geomspace(1e8, 1e16, 1000) ks = np.geomspace(1e-4, 100, 1001) # ks,pks = np.loadtxt("mm_k_pk.txt",unpack=True) # !!! eeP = mmhmod.P_gg_2h(ks, zs, mthreshHOD=mthresh) + mmhmod.P_gg_1h( ks, zs, mthreshHOD=mthresh) meP = mmhmod.P_mg_2h(ks, zs, mthreshHOD=mthresh) + mmhmod.P_mg_1h( ks, zs, mthreshHOD=mthresh) mmP = mmhmod.P_mm_2h(ks, zs) + mmhmod.P_mm_1h(ks, zs) hcos = hmvec.HaloModel(zs, ks, ms=ms, halofit=None, mdef='vir', nfw_numeric=False) hcos.add_hod("g", mthresh=(10**mthresh[0]) + zs * 0., corr="max") # hcos.add_battaglia_profile("electron",family="AGN",xmax=100,nxs=60000) pme_1h = hcos.get_power_1halo(name="g", name2="nfw") pme_2h = hcos.get_power_2halo(name="g", name2="nfw") pmm_1h = hcos.get_power_1halo("nfw") pmm_2h = hcos.get_power_2halo("nfw") pee_1h = hcos.get_power_1halo("g") pee_2h = hcos.get_power_2halo("g") for i in range(zs.size): pl = io.Plotter(xyscale='loglin', xlabel='$k$', ylabel='$P$') pl.add(ks, (pme_2h[i] + pme_1h[i]) / meP[i], ls="--", color="C%d" % i) pl.add(ks, (pmm_2h[i] + pmm_1h[i]) / mmP[i], ls="-", color="C%d" % i) pl.add(ks, (pee_2h[i] + pee_1h[i]) / eeP[i], ls=":", color="C%d" % i) pl.vline(x=10.) pl.hline(y=1.) pl._ax.set_ylim(0.8, 1.3) pl.done("pgcomp_rat_z_%d.png" % i)
def websky_halos(dirpath="./", mmin=-np.inf, mmax=np.inf): pkfile = open(dirpath + "halos/halos.pksc", "rb") Nhalo = np.fromfile(pkfile, dtype=np.int32, count=1)[0] RTHMAXin = np.fromfile(pkfile, dtype=np.float32, count=1) redshiftbox = np.fromfile(pkfile, dtype=np.float32, count=1) nfloats_perhalo = 10 npkdata = nfloats_perhalo * Nhalo peakdata = np.fromfile(pkfile, dtype=np.float32, count=npkdata) peakdata = np.reshape(peakdata, (Nhalo, nfloats_perhalo)) Rth = peakdata[:, 6] Omega_M = 0.25 h = 0.7 rho = 2.775e11 * Omega_M * h**2 M = 4.0 / 3 * np.pi * Rth**3 * rho sel = np.logical_and(M > mmin, M <= mmax) xpos = peakdata[:, 0][sel] ypos = peakdata[:, 1][sel] zpos = peakdata[:, 2][sel] vzpos = peakdata[:, 5][sel] h = h M = M from orphics import cosmology vecs = np.swapaxes(np.array([xpos, ypos, zpos]), 0, 1) ras, decs = hp.vec2ang(vecs, lonlat=True) params = cosmology.defaultCosmology params["H0"] = 70.0 params["omch2"] = 0.10331 params["ombh2"] = 0.01919 cc = cosmology.Cosmology(params, skipCls=True, skipPower=True, skip_growth=True) cspeed = 2.9979458e8 / 1e3 chis = np.sqrt(xpos**2.0 + ypos**2.0 + zpos**2.0) zs = cc.results.redshift_at_comoving_radial_distance(chis) + vzpos / cspeed return ras, decs, zs
def __init__(self, lens_beam = 7.0,lens_noiseT = 33.,lens_noiseP = 56., lens_tellmin = 2,lens_tellmax = 3000,lens_pellmin = 2, lens_pellmax = 3000,lens_kmin = 80,lens_kmax = 2000, lens_f_sky=0.65 ): # get lensing noise # Initialize cosmology and Clkk. Later parts need dimensionless spectra. self.l_min = lens_tellmin self.l_max = lens_tellmax self.k_min = lens_kmin self.k_max = lens_kmax self.f_sky = lens_f_sky cc = cosmology.Cosmology(lmax=self.l_max,pickling=True,dimensionless=True) theory = cc.theory ells = np.arange(2,self.l_max,1) clkk = theory.gCl('kk',ells) # Make a map template for calculating the noise curve on shape,wcs = maps.rect_geometry(width_deg = 5.,px_res_arcmin=1.5) # Define bin edges for noise curve bin_edges = np.arange(80,lens_kmax,20) nlgen = lensing.NlGenerator(shape,wcs,theory,bin_edges,lensedEqualsUnlensed=True) # Experiment parameters, here for Planck polCombs = ['TT','TE','EE','EB','TB'] _,_,_,_ = nlgen.updateNoise( beamX=lens_beam,noiseTX=lens_noiseT,noisePX=lens_noiseP, tellminX=lens_tellmin,tellmaxX=lens_tellmax, pellminX=lens_pellmin,pellmaxX=lens_pellmax) ls,nls,bells,nlbb,efficiency = nlgen.getNlIterative(polCombs,lens_kmin,lens_kmax, lens_tellmax,lens_pellmin,lens_pellmax, verbose=True,plot=False) self.orphics_kk = clkk self.orphics_ls = ls self.orphics_nls = nls self.noise_k = np.interp(np.arange(self.l_max+1), ls, nls) self.noise_k[np.arange(self.l_max+1) <= lens_kmin] = 1e100 self.noise_k[np.arange(self.l_max+1) >= lens_kmax] = 1e100
from __future__ import print_function from orphics import maps,io,cosmology,stats from enlib import enmap import numpy as np import os,sys from scipy import signal cc = cosmology.Cosmology(lmax=6000,pickling=True) deg = 15. shape,wcs = maps.rect_geometry(width_deg=deg,px_res_arcmin=1.0) modlmap = enmap.modlmap(shape,wcs) modrmap = enmap.modrmap(shape,wcs) lmax = modlmap.max() ells = np.arange(0,lmax,1) ps = cc.theory.lCl('TT',ells).reshape((1,1,ells.size)) mgen = maps.MapGen(shape,wcs,ps) fwhm = 5. kbeam = maps.gauss_beam(modlmap,fwhm) imap = mgen.get_map() bmap2 = maps.convolve_gaussian(imap.copy(),fwhm=fwhm,nsigma=5.0) print(bmap2.shape) bmap = maps.filter_map(imap.copy(),kbeam)
def __init__(self, lens_beam = 7.0,lens_noiseT = 33.,lens_noiseP = 56., lens_tellmin = 2,lens_tellmax = 3000,lens_pellmin = 2, lens_pellmax = 3000,lens_kmin = 80,lens_kmax = 3000, lens_f_sky=0.65, bin_width=80, estimators=('TT','TE','EE','EB','TB'), NlTT=None, NlEE=None, NlBB=None): # get lensing noise # Initialize cosmology and Clkk. Later parts need dimensionless spectra. self.l_min = lens_tellmin # CLASS can only go up to 5500 self.l_max = min(max(lens_tellmax, lens_pellmax)+1000,5500) self.k_min = lens_kmin self.k_max = lens_kmax self.f_sky = lens_f_sky # import orphics only here! from orphics import lensing, io, stats, cosmology, maps # generate cosmology with orphics lmax = self.l_max cc = cosmology.Cosmology(lmax=lmax,pickling=True,dimensionless=False) theory = cc.theory ells = np.arange(2,lmax,1) clkk = theory.gCl('kk',ells) Tcmb = 2.726 # compute noise curves sT = lens_noiseT * (np.pi/60./180.) sP = lens_noiseP * (np.pi/60./180.) theta_FWHM = lens_beam * (np.pi/60./180.) muK = Tcmb*1.0e6 # unitless white noise exp_term = np.exp(ells*(ells+1)*(theta_FWHM**2)/(8*np.log(2))) if NlTT is None: NlTT = sT**2 * exp_term #/ muK**2 else: NlTT = NlTT[ells] if NlEE is None: NlEE = sP**2 * exp_term #/ muK**2 else: NlEE = NlEE[ells] if NlBB is None: NlBB = sP**2 * exp_term #/ muK**2 else: NlBB = NlBB[ells] NlTT[ells > lens_tellmax] = 1e100 NlEE[ells > lens_pellmax] = 1e100 NlBB[ells > lens_pellmax] = 1e100 self.NlTT = NlTT self.NlEE = NlEE self.NlBB = NlBB # Define bin edges for noise curve bin_edges = np.arange(2,lmax,bin_width) # compute orphics lensing noise ls,nlkks,theory_,qest = lensing.lensing_noise( ells=ells, ntt=NlTT, nee=NlEE, nbb=NlBB, ellmin_t=lens_tellmin, ellmin_e=lens_pellmin,ellmin_b=lens_pellmin, ellmax_t=lens_tellmax, ellmax_e=lens_pellmax,ellmax_b=lens_pellmax, bin_edges=bin_edges, estimators=estimators, ellmin_k = 2, ellmax_k = lens_kmax + 500, # calculate out a bit theory=theory, dimensionless=False) self.orphics_kk = clkk self.orphics_ls = ls self.orphics_nls = nlkks['mv'] self.noise_k = np.interp(np.arange(self.l_max+1), ls, nlkks['mv']) self.noise_k[np.arange(self.l_max+1) <= lens_kmin] = 1e100 self.noise_k[np.arange(self.l_max+1) >= lens_kmax] = 1e100
import camb import numpy as np from orphics import io, cosmology import matplotlib.cm as cm cc = cosmology.Cosmology(skipCls=True, skipPower=True, skip_growth=False) z = 0.5 ks = np.logspace(np.log10(0.0001), np.log10(1.0), 100) pl = io.Plotter(xscale='log', ylabel="$D$", xlabel="$k$") #comps = ['delta_baryon','delta_tot']#,'v_newtonian_cdm','v_baryon_cdm','v_newtonian_baryon'] #comps = ['delta_tot']#,'v_newtonian_cdm','v_baryon_cdm','v_newtonian_baryon'] comps = ['growth'] zs = np.arange(0., 3., 0.1) for z in zs: print(z) #comps = ['v_newtonian_cdm','v_baryon_cdm','v_newtonian_baryon'] for comp in comps: gcomp = cc.growth_scale_dependent(ks, z, comp) pl.add(ks, gcomp[:, 0, 0], color=cm.hot(z)) #,label=comp) pl._ax.axhline(y=cc.Dfunc(cc.z2a(z)), ls="--") pl._ax.axhline(y=cc.fsfunc(cc.z2a(z)), ls="--") pl.legend() #pl.done("output/delta_velocity.png") pl.done("output/delta_growth_rate.png")
def test_pmm(): matt = True # set to False if you don't have Matt and Moritz's halomodel.py for comparison if matt: import halomodel as mmhm from orphics import cosmology cc = cosmology.Cosmology(hmvec.default_params, skipCls=True, low_acc=False) mmhmod = mmhm.HaloModel(cc) zs = np.array([0., 2., 3.]) #,1.,2.,3.]) #zs = np.array([0.,2.,4.,6.]) ms = np.geomspace(1e7, 1e17, 2000) #ks = np.geomspace(1e-4,100,1001) ks = np.geomspace(1e-5, 100, 10000) if matt: mmP = mmhmod.P_mm_2h(ks, zs) + mmhmod.P_mm_1h( ks, zs) #,logmlow=log10mlow,logmhigh=log10mhigh)[z_id,:] # print(mmhmod.halobias[:,0]) #print(mmP2h.shape) hcos = hmvec.HaloModel(zs, ks, ms=ms, halofit='mead', mdef='vir', nfw_numeric=True) mmhb = mmhmod.halobias #np.load("mm_halobias.npy",) mmnfn = mmhmod.nfn #np.load("mm_nfn.npy") # pl = io.Plotter(xyscale='loglin') # for i in range(zs.size): # pl.add(ks,(hcos.nPzk[i]-mmhmod.pknl[i])/hcos.nPzk[i]) # pl.add(ks,(hcos.Pzk[i]-mmhmod.pk[i])/hcos.Pzk[i]) # pl.hline(y=0) # pl.done() # pl = io.Plotter(xyscale='loglog') # for i in range(3): # pl.add(ms,hcos.nzm[i,:],color="C%d" % i) # pl.add(ms,mmnfn[:,i],ls='--',color="C%d" % i) # pl.done() # pl = io.Plotter(xyscale='loglog') # for i in range(3): # pl.add(ms,hcos.bh[i,:],color="C%d" % i) # pl.add(ms,mmhb[:,i],ls='--',color="C%d" % i) # pl.done() # pl = io.Plotter(xyscale='loglog') # pl.add(10**mmhmod.logm,mmhmod.halobias[:,1]) # pl.add(ms,hcos.bh[1,:]) # pl.done() pmm_1h = hcos.get_power_1halo(name="nfw") pmm_2h = hcos.get_power_2halo(name="nfw") # sys.exit() print(pmm_1h.shape) for i in range(zs.size): pl = io.Plotter(xyscale='loglog', xlabel='$k$', ylabel='$P$') pl.add(ks, pmm_1h[i], label="z=%.1f" % zs[i], color="C%d" % i, ls="--", alpha=0.2) # pl.add(ks,pmm_2h[i],label="z=%.1f" % zs[i],ls="--",color="C%d" % i) pl.add(ks, pmm_2h[i] + pmm_1h[i], ls="--", color="C%d" % i) pl.add(ks, hcos.nPzk[i], ls="-", color="k", alpha=0.7) if matt: pl.add(ks, mmP[i], ls="-.", color="C%d" % i) pl.vline(x=10.) pl._ax.set_ylim(1e-1, 1e5) pl.done("nonlincomp_z_%d.png" % i) for i in range(zs.size): pl = io.Plotter(xyscale='loglin', xlabel='$k$', ylabel='$P/P_{\\mathrm{NL}}$') pl.add(ks, (pmm_2h[i] + pmm_1h[i]) / hcos.nPzk[i], ls="-", color="C%d" % i) if matt: pl.add(ks, mmP[i] / hcos.nPzk[i], ls="--", color="C%d" % i) pl.hline(y=1) pl.hline(y=0.9) pl.hline(y=1.1) pl.vline(x=10.) pl._ax.set_ylim(0.5, 1.5) pl.done("nonlindiff_z_%d.png" % i) for i in range(zs.size): pl = io.Plotter(xyscale='loglin', xlabel='$k$', ylabel='$P/P_{\\mathrm{L}}$') pl.add(ks, (pmm_2h[i] + pmm_1h[i]) / hcos.Pzk[i], ls="-", color="C%d" % i) if matt: pl.add(ks, mmP[i] / hcos.Pzk[i], ls="--", color="C%d" % i) pl.vline(x=10.) pl.hline(y=1) # pl.hline(y=0.9) # pl.hline(y=1.1) # pl._ax.set_ylim(0.5,1.5) pl.done("lindiff_z_%d.png" % i)