def test_tinker10_dh(): h = MassFunction(hmf_model="Tinker10") h1 = MassFunction(hmf_model="Tinker10", mdef_model="SOMean", mdef_params={"overdensity": 200.1}) assert np.allclose(h.fsigma, h1.fsigma, rtol=1e-2)
def test_tinker08_dh(): h = MassFunction( hmf_model="Tinker08", mdef_model="SOMean", mdef_params={"overdensity": 200}, transfer_model="EH", ) h1 = MassFunction( hmf_model="Tinker08", mdef_model="SOMean", mdef_params={"overdensity": 200.1}, transfer_model="EH", ) assert np.allclose(h.fsigma, h1.fsigma, rtol=1e-2)
def getHMFz(z, H0=70.3, Om0=0.276, Ob0=0.0455, Tcmb0=2.725, Mmin=1e10, Mmax=1e15): """ Fast function to call the HMF from hmf, this function only has 7 variables and will return the dn/d(log10 M) and M array. z: redshift H0: Hubble constant Om0: Matter density Ob0: Baryon density Tcmb0: CMB temperature at z=0 Mmin: minimum mass (solar masses) Mmax: Maximum mass (solar masses) """ new_model = FlatLambdaCDM(H0=H0, Om0=Om0, Ob0=Ob0, Tcmb0=Tcmb0) hmff = MassFunction( cosmo_model=new_model, Mmax=np.log10(Mmax), Mmin=np.log10(Mmin), z=z, hmf_model="ST", ) return hmff.m, hmff.dndlog10m
def test_tinker10_neg_etaphi(): h = MassFunction(hmf_model="Tinker10", hmf_params={ "eta_200": -1, "phi_200": 0 }) h.fsigma
def MF(self): if not hasattr(self, '_MF'): self.logMmin_tab = self.pf['hmf_logMmin'] self.logMmax_tab = self.pf['hmf_logMmax'] self.zmin = self.pf['hmf_zmin'] self.zmax = self.pf['hmf_zmax'] self.dlogM = self.pf['hmf_dlogM'] self.dz = self.pf['hmf_dz'] self.Nz = int((self.zmax - self.zmin) / self.dz + 1) self.z = np.linspace(self.zmin, self.zmax, self.Nz) # Initialize Perturbations class self._MF = MassFunction(Mmin=self.logMmin_tab, Mmax=self.logMmax_tab, dlog10m=self.dlogM, z=self.z[0], hmf_model=self.hmf_func, cosmo_params=self.cosmo_params, growth_params=self.growth_pars, sigma_8=self.cosm.sigma8, n=self.cosm.primordial_index, transfer_params=self.transfer_pars, dlnk=self.pf['hmf_dlnk'], lnk_min=self.pf['hmf_lnk_min'], lnk_max=self.pf['hmf_lnk_max']) return self._MF
def test_fcolls(self): # Note: if Mmax>15, starts going wrong because of numerics at high M pert = MassFunction(Mmin=10, Mmax=15, dlog10m=0.01) fits = ['PS', 'Peacock'] for fit in fits: pert.update(hmf_model=fit) yield self.check_fcoll, pert, fit
def test_ranges_cut(self): hmf = MassFunction(hmf_model="Peacock", dlog10m=0.01) TestCumulants.tol = 0.4 for minm in [9, 10, 11]: # below, equal and greater than peacock cut for maxm in [ 14, 15, 16, 18, 19 ]: # below,equal,greater than peacock cut and integration limit yield self.check, hmf, minm, maxm
def compute_HMF(redshift, hmf_model='SMT'): ''' hmf_model = PS, Jenkins, SMT, Warren, Tinker08 ''' HMF_WMAP = MassFunction(cosmo_model=cosmo, z=redshift, Mmin=1, Mmax=13, hmf_model=hmf_model) HMF_Planck = MassFunction(cosmo_model=cosmoP, z=redshift, Mmin=1, Mmax=13, hmf_model=hmf_model) #hmf.update(hmf_model='Sheth-Tormen') #update baryon density and redshift masses = HMF_WMAP.m / cosmo.h MF_WMAP = HMF_WMAP.dndlog10m * cosmo.h**3 MF_Planck = HMF_Planck.dndlog10m * cosmo.h**3 return (masses, MF_WMAP, MF_Planck)
def test_tinker10_neg_etaphi(): with raises(ValueError): h = MassFunction( hmf_model="Tinker10", hmf_params={ "eta_200": -1, "phi_200": 0 }, transfer_model="EH", ) h.fsigma
def P_k(k, z, cosmo): ''' DM linear power spectrum, using transfer function from Eisenstein & Hu (1998) from hmf ''' hmf = MassFunction(Mmin=9, Mmax=16, z=z, cosmo_model=cosmo, transfer_model='EH_BAO') f = interp1d(hmf.k, hmf.power, kind='cubic') return f(k)
def test_change_dndm(colossus_cosmo): h = MassFunction(mdef_model="SOVirial", hmf_model="Warren", disable_mass_conversion=False) with pytest.warns(UserWarning): h.mdef dndm = h.dndm h.update(mdef_model="FOF") assert not np.allclose(h.dndm, dndm, atol=0, rtol=0.15)
def h(self): ''' Initialzes hmf MassFunction() model. Note that, unlike other cached properties, h is NOT cleared when update() is called. Properties of h are updated using the hmf built-in update methods. This speeds up calculations where the astrophysical model is updated without changing the underlying cosmology ''' return MassFunction(cosmo_model=self.cosmo_model, Mmin=hmf_logMmin, Mmax=hmf_logMmax, dlog10m=hmf_dlog10m, z=self.z)
def test_circular_minimize(): h = MassFunction(sigma_8=0.8, mf_fit="ST") dndm = h.dndm.copy() f = fit.Minimize(priors=[fit.Uniform("sigma_8", 0.6, 1.0)], data=dndm, quantity="dndm", sigma=dndm / 5, guess=[0.9], blobs=None, verbose=0, store_class=False, relax=False) res = f.fit(h) print "Diff: ", np.abs(res.x - 0.8) assert np.abs(res.x - 0.8) < 0.01
def test_circular_emcee(): h = MassFunction(sigma_8=0.8, mf_fit="ST") dndm = h.dndm.copy() f = fit.MCMC(priors=[fit.Uniform("sigma_8", 0.6, 1.0)], data=dndm, quantity="dndm", sigma=dndm / 5, guess=[0.8], blobs=None, verbose=0, store_class=False, relax=False) sampler = f.fit(h, nwalkers=16, nsamples=15, burnin=0, nthreads=0) print "Diff: ", np.abs(np.mean(sampler.chain) - 0.8) assert np.abs(np.mean(sampler.chain) - 0.8) < 0.01
def test_all_fits(): hmf = MassFunction(Mmin=10, Mmax=15, dlog10m=0.1, omegab=0.05, omegac=0.25, omegav=0.7, sigma_8=0.8, n=1, H0=70.0, lnk_min=-16, lnk_max=10, dlnk=0.01, mf_fit='ST', z=0.0) close_hmf = MassFunction(Mmin=10, Mmax=15, dlog10m=0.1, omegab=0.05, omegac=0.25, omegav=0.7, sigma_8=0.8, n=1, H0=70.0, lnk_min=-16, lnk_max=10, dlnk=0.01, mf_fit='ST', z=0.0) hmf.fsigma close_hmf.fsigma ff._allfits.remove("AnguloBound") for redshift in [0.0, 2.0]: for fit in ff._allfits: yield check_close, hmf, close_hmf, fit, redshift
def get_hf(sigma_val=0.8228, boxRedshift=0., delta_wrt='mean'): """ Halo mass function model for the MultiDark simulation. """ #hf0 = MassFunction(cosmo_model=cosmo, sigma_8=sigma_val, z=boxRedshift) omega = lambda zz: cosmoMD.Om0 * (1 + zz)**3. / cosmoMD.efunc(zz)**2 DeltaVir_bn98 = lambda zz: (18. * n.pi**2. + 82. * (omega(zz) - 1) - 39. * (omega(zz) - 1)**2.) / omega(zz) print "DeltaVir", DeltaVir_bn98(boxRedshift), " at z", boxRedshift hf1 = MassFunction(cosmo_model=cosmoMD, sigma_8=sigma_val, z=boxRedshift, delta_h=DeltaVir_bn98(boxRedshift), delta_wrt=delta_wrt, Mmin=7, Mmax=16.5) return hf1
def Tinker08(z=0): """Tinker halo mass function generator Returns ---------- dict A dictionary contains of 'x' and 'y' keys, suitable to use with ..visualization.plot module """ hmf = MassFunction(hmf_model=ff.Tinker08, delta_h=200.0, z=z, filter_model=TopHat, transfer_model=tm.EH, cosmo_model=Planck15) return {'x': hmf.m, 'y': hmf.dndlnm}
def hmf(self, z, Mmin, Mmax, q_out = 'dndlnM', hmf_code='colossus', print_cosmo=False): if(hmf_code=='colossus'): params = {'flat': True, 'H0': self.H0, 'Om0': self.om, 'Ob0': self.ob, 'sigma8': self.sigma8, 'ns': self.ns} col_cosmology.addCosmology('myCosmo', params) colcosmo=col_cosmology.setCosmology('myCosmo') if print_cosmo: print(colcosmo) #Mass_bin = np.logspace(np.log10(Mmin),np.log10(Mmax), num=500) Mh = 10**(np.linspace(np.log10(Mmin),np.log10(Mmax), num=200)) Mh=Mh/self.h #mfunc = mass_function.massFunction(Mh, z, mdef = self.halo_model_mdef, model = self.halo_model, q_out = q_out) mfunc = mass_function.massFunction(Mh, z, mdef = '200m', model = 'tinker08', q_out = q_out) mpc_to_m= 3.086e+22 mfunc*=(mpc_to_m)**-3 dndm=mfunc return Mh, dndm if(hmf_code=='hmf'): from hmf import cosmo as cosmo_hmf my_cosmo = cosmo_hmf.Cosmology() my_cosmo.update(cosmo_params={"H0":self.H0,"Om0":self.om,"Ode0":self.ol,"Ob0":self.ob}) mf=MassFunction(Mmin=np.log10(Mmin), Mmax=np.log10(Mmax), hmf_model= 'ST') mf.update(z=z) mpc_to_m= 3.086e+22 #hm, dndm= mf.m, mf.dndlnm hm, dndm= mf.m/self.h, mf.dndlnm *(mpc_to_m)**-3 #* self.h**4*(mpc_to_m)**-3 # dndm*=(mpc_to_m)**-3 return hm, dndm
def hmf(): return MassFunction( Mmin=10, Mmax=15, dlog10m=0.1, lnk_min=-16, lnk_max=10, dlnk=0.01, hmf_model="PS", z=0.0, sigma_8=0.8, n=1, cosmo_params={ "Om0": 0.3, "H0": 70.0, "Ob0": 0.05 }, transfer_model="EH", )
def __init__(self): self.hmf = MassFunction(Mmin=10, Mmax=15, dlog10m=0.1, lnk_min=-16, lnk_max=10, dlnk=0.01, hmf_model='PS', z=0.0, sigma_8=0.8, n=1, cosmo_params={ "Om0": 0.3, "H0": 70.0, "Ob0": 0.05 }) self.ps_max = self.hmf.fsigma.max()
def __init__(self, redshifts=None): self.hmf = MassFunction(dlog10m=0.02) self.mrange = self.hmf.m * u.solMass self.rho = self.hmf.rho_gtm[0] * u.solMass * u.Mpc**( -3) # mean number of DM halos per Mpc^3 self.m_star = ( 4 * np.pi * self.R_star**3 / 3) * self.rho # used as pivot mass for halo concentration self.kmax = 1e3 / self.h if redshifts is None: self.redshifts = np.array([0.0]) self.linpars.set_matter_power(kmax=self.kmax) else: self.redshifts = redshifts self.linpars.set_matter_power(redshifts=redshifts, kmax=self.kmax) self.linpars.NonLinear = model.NonLinear_none self.lin_results = camb.get_results(self.linpars)
def _prepare_mf(M_min, M_max, mf_kwargs, boxsize=None): # Create the mass function object M = np.linspace(M_min, M_max, 500) if boxsize is not None: mf_kwargs['lnk'] = np.linspace(np.log(2 * np.pi / boxsize), 20, 500) mf_obj = MassFunction(M=M, **mf_kwargs) # Get the total density within limits total_rho = simps(mf_obj.M * mf_obj.dndlnm, M) * np.log(10) frac_in_bounds = total_rho / (mf_obj.cosmo.omegam * 2.7755e11) cumfunc = cumtrapz(mf_obj.dndlnm, M, initial=1e-20) * np.log(10) cdf = spline(M, cumfunc, k=3) icdf = spline(cumfunc, M, k=3) print "prepare mf: ", total_rho, frac_in_bounds, M_min, M_max, cumfunc.min( ), cumfunc.max() return cdf, icdf, mf_obj, frac_in_bounds
def load_massfunction(cosmo_model = default_cosmo, hmf_model_sel = 4, sigma_8 = 0.821, n = 0.972, \ delta_h = 500, delta_wrt_sel = 1, Mmin = 13, Mmax = 16, dlog10m = 0.001, \ transfer_model_sel = 3, lnk_min = -4, lnk_max = 2, dlnk = 0.0026, \ transfer_params = None, takahashi = True): hmf_model_array = ['SMT', 'Jenkins', 'Warren', 'Tinker08', 'Tinker10'] hmf_model = hmf_model_array[hmf_model_sel] delta_wrt_array = ['mean', 'crit'] delta_wrt = delta_wrt_array[delta_wrt_sel] transfer_model_array = ['BBKS', 'BondEfs', 'CAMB', 'EH'] transfer_model = transfer_model_array[transfer_model_sel] h = MassFunction(cosmo_model = cosmo_model, hmf_model = hmf_model, sigma_8 = sigma_8, n = n, \ delta_h = delta_h, delta_wrt = delta_wrt, Mmin = Mmin, Mmax = Mmax, \ dlog10m = dlog10m, transfer_model = transfer_model, lnk_min = lnk_min, \ lnk_max = lnk_max, dlnk = dlnk, transfer_params = transfer_params, \ takahashi = takahashi) return h
def __init__(self): self.hmf = MassFunction(Mmin=7, Mmax=15.001, dlog10m=0.01, omegab=0.05, omegac=0.25, omegav=0.7, sigma_8=0.8, n=1, H0=70.0, lnk_min=-11, lnk_max=11, dlnk=0.01, transfer_options={ "fname": LOCATION + "/data/transfer_for_hmf_tests.dat" }, mf_fit='ST', z=0.0, transfer_fit="FromFile")
def hmf(self): return MassFunction( Mmin=7, Mmax=15.001, dlog10m=0.01, sigma_8=0.8, n=1, cosmo_model=LambdaCDM(Ob0=0.05, Om0=0.3, Ode0=0.7, H0=70.0, Tcmb0=0), lnk_min=-11, lnk_max=11, dlnk=0.01, transfer_params={"fname": "tests/data/transfer_for_hmf_tests.dat"}, hmf_model="ST", z=0.0, transfer_model="FromFile", growth_model="GenMFGrowth", )
def calculate(self, state, want_derived=True, **params_values_dict): self._hmf_kwargs['z'] = self.zarr[0] h = params_values_dict["H0"] / 100 Oc = params_values_dict['omegach2'] / h**2 Ob = params_values_dict['omegabh2'] / h**2 Om = Oc + Ob Ode = 1 - Om self._hmf_kwargs['cosmo_params'] = { 'H0': 100 * h, 'Om0': Om, 'Ob0': Ob, } self.MF = MassFunction(**self._hmf_kwargs) pk_interp = self.provider.get_Pk_interpolator(nonlinear=False) for q in self.quants: state[q] = [] print("HERE") for i, z in enumerate(self.zarr): self.MF.update(z=z, custom_pk=pk_interp.P(z, self.MF.k)) for q in self.quants: state[q].append(getattr(self.MF, q).copy()) state["MF"] = self.MF state['zs'] = self.zarr
def __init__(self): self.hmf = MassFunction(Mmin=7, Mmax=15.001, dlog10m=0.01, sigma_8=0.8, n=1, cosmo_model=LambdaCDM(Ob0=0.05, Om0=0.3, Ode0=0.7, H0=70.0, Tcmb0=0), lnk_min=-11, lnk_max=11, dlnk=0.01, transfer_params={ "fname": LOCATION + "/tests/data/transfer_for_hmf_tests.dat" }, hmf_model='ST', z=0.0, transfer_model="FromFile", growth_model="GenMFGrowth")
import numpy as np import matplotlib.pyplot as plt import copy from astropy import units as u from astropy import constants as const from astropy import cosmology import pickle from hmf import MassFunction # cosmo params cosmo = cosmology.Planck15 cosmo.hmf = MassFunction(Mmin = np.log10(1e6), Mmax = np.log10(1e15), cosmo_model=cosmo, hmf_model = 'SMT') class HMFz: ''' HMF, P(k), and T(k) at given redshift. ''' def __init__(self, z, cosmo = cosmo): hmf = copy.deepcopy(cosmo.hmf) hmf.update(z=z) self.z = z self.hmf = hmf def sigma(self, Mmin = 1e8, Mmax = 1e15, dlog10m = 0.01, m_arr = []): ''' mass variance output: ======= dndm_arr [] m_arr [Msun h^-1]
""" import numpy as np import matplotlib.pyplot as plt from hmf import MassFunction from colossus.lss import mass_function from colossus.cosmology import cosmology cosmology.setCosmology('planck15') Mass_bin = np.logspace(np.log10(1e8), np.log10(1e15), num=100) mfunc = mass_function.massFunction(Mass_bin, 6.0, mdef='200c', model='tinker08', q_out='dndlnM') mf = MassFunction(z=6.0, hmf_model="Tinker08", mdef_model='SOCritical') plt.plot(mf.m, mf.dndm, label="HMF") plt.plot(Mass_bin, mfunc, label="colossus") plt.xscale('log') plt.yscale('log') plt.xlabel(r"Mass, $[h^{-1}M_\odot]$") plt.ylabel(r"$dn/dm$, $[h^{4}{\rm Mpc}^{-3}M_\odot^{-1}]$") plt.legend(loc=0)
def ps(self): return MassFunction(hmf_model="PS", Mmin=0, dlog10m=0.01, transfer_model="EH")