コード例 #1
0
def powerspectrumfromfile(fname, 
	koverh = None ,
	pstype = "matter" , 
	h  = 0.71 ,
	ns = 0.963 ,
	As = 2.1e-9 , 
	Omegacdm = None , 
	Omegab   = None ):

	"""
	****************************************
	DEPRECATED: USE cambio functions instead
	*****************************************
	returns a tuple of koverh values and the interpolated power
	spectra at these values of hoverh using a CAMB output which 
	may be a power spectrum output or transfer function output. 
	If the output is a transfer function output, then ns,  h, 
	and As must be supplied 

	args:

	returns:
		tuple of (koverh, ps values)

	
	"""

	#decide if file is transfer function or Power spectrum output

	psfile = False
	tffile = False
	Unknown = True

	tmpfile = np.loadtxt(fname)
	shapetuple = np.shape(tmpfile)
	if shapetuple[-1] == 7:
		tffile  = True
		Unknown = False 
	if shapetuple[-1] ==2 :
		psfile  = True 	
		Unknown  = False
	if koverh == None:
		koverh = tmpfile[:,0]
	

	if Unknown:
		#file is not CAMB transfer function or power spectrum output
		raise ValueError("Unknown filename supplied")

	if psfile :
		if pstype != "matter" :
			raise ValueError ("Cannot obtain non-matter power spectrum  from CAMB power spectrum file")
		return (koverh , powerspectrum(koverh, fname )	)


	if tffile :
		if pstype == "matter" :
			transfer  = cio.loadtransfers(rootname = None, 
				filename = fname) 
			ps = cio.matterpowerfromtransfersforsinglespecies( 				koverh ,
				transfer , 
				h ,
				As ,
				ns )


			return (ps [:,0], ps[:,1])

		elif pstype == "cb"    :

			#LOST CODE HERE
			return 0
コード例 #2
0
def __powerspectrum ( koverh , 
	asciifile = None ,
	pstype = "matter", 
	method = "CAMBoutfile",
	z      = 0.0 , 
	cosmo =  None ,
	**params):

	"""
	DO NOT CALL DIRECTLY. CALL powerspectrum instead 
	returns linearly interpolated values of the powerspectrum in the 
	powerspectrumfile with k values in units of h/Mpc. Using
	this with koverh = None, returns the values in the table. 

	args:
		koverh : array-like of floats or Nonetype, mandatory
			k in units of h/Mpc
		asciifile: string, 
			Filename for power spectrum or CAMB transfer function. 
			power sepctrum or transfer function input will be 
			recognized from CAMB file structure.
		method   : string, optional , defaults to "CAMBoutfile" 
			Method of obtaining power spectrum with fixed options
			options:
			-------
			CAMBoutfile   :assume that the asciifile output of CAMB 
				is at desired redshift 
			CAMBoutgrowth :Use the asciifile from CAMB output at 
				z  = 0.0 , and use a growth function to find 
				the power spectrum at z = z
			
			
	returns:
		tuple (koverh , power spectrum)

	notes: should be able to obtain the powerspectrum in a variety of 
		methods with code being added
	"""

	#ensure we are supposed to read CAMB outfiles
	if not method in ["CAMBoutfile","CAMBoutgrowth"]:
		raise ValueError("Method not defined")

#	# Decide whether this ia a matter or transfer file
	#This has been made a function
#	psfile = False
#	tkfile = False
#	Unknown = True
#
#	shapetuple = np.shape(tmpfile)
#	if shapetuple[-1] == 7:
#		tkfile  = True
#		Unknown = False 
#	if shapetuple[-1] ==2 :
#		psfile  = True 	
#		Unknown  = False


	psfile, tkfile, Unknown = cambasciifiletype ( asciifile )
	
	tmpfile = np.loadtxt(asciifile)
	if koverh == None:
		koverh = tmpfile[:,0]

	if Unknown:
		#file is not CAMB transfer function or power spectrum output
		raise ValueError("Unknown filename supplied")

	if psfile: 
		pk = cio.loadpowerspectrum(asciifile)
		if not np.all(np.diff(pk[:,0])>0.):
			raise ValueError("The k values in the power spectrum file are not in ascending order")

		if koverh == None :
			return (pk[:,0], pk[:,1])

		return  koverh, np.interp( koverh, pk[:,0],pk[:,1],left = np.nan, right = np.nan)
	if tkfile:
		#print "AS " , params["As"]
		#print cosmo.Ob0, cosmo.Oc0
	
		if pstype == "cb":
			#print "filename ", asciifile
			pk = cio.cbpowerspectrum ( transferfile = asciifile , 
				Omegacdm  =  cosmo.Oc0,
				Omegab    =  cosmo.Ob0, 
				h         =  cosmo.h, 
				Omeganu   =  cosmo.On0,
				As        =  params["As"], 
				#As        =  cosmo.As, 
				ns        =  cosmo.ns, 
				koverh    =  None )

			return (pk [:,0], pk[:,1])

		if pstype == "cbmatter":
			Omegam = cosmo.Om0
			Omegacb = cosmo.Ob0 + cosmo.Oc0 
			ratiosq = (Omegacb/Omegam)**2.0
			#print "filename ", asciifile
			pk = cio.cbpowerspectrum ( transferfile = asciifile , 
				Omegacdm  =  cosmo.Oc0,
				Omegab    =  cosmo.Ob0, 
				h         =  cosmo.h, 
				Omeganu   =  cosmo.On0,
				As        =  params["As"], 
				#As        =  cosmo.As, 
				ns        =  cosmo.ns, 
				koverh    =  None )

			return (pk [:,0], pk[:,1]*ratiosq)
		if pstype == "matter" :
			if koverh == None :
				koverh = tmpfile[:,0]

			transfer  = cio.loadtransfers( filename = asciifile) 

			transfertuple = (transfer[:,0], transfer[:,-1])
			ps = cio.matterpowerfromtransfersforsinglespecies(
				koverh ,
				transfer  = transfertuple,
				h = cosmo.h ,
				As = params["As"],
				ns = cosmo.ns)


			return (ps [:,0], ps[:,1])

		
		return koverh, pk 
コード例 #3
0
	#dndlnM = dlsinvdlM  *f_sigma/M * rhobg
		#critdensity(h = cosmo.h, unittype = "solarmassperMpc3")*cosmo.Om0 
	return dndlnM   
if __name__=="__main__":

	import numpy as np
	import matplotlib.pyplot as plt
	import camb_utils.cambio  as cio
	import sys
	#pk = cio.loadpowerspectrum ("example_data/LCDM_def_matterpower.dat")
	pk = cio.loadpowerspectrum ("LCDM_matterpower.dat")
	ts = cio.loadtransfers(filename = "example_data/LCDM_def_transfer_out.dat")
	#print np.shape(ts)
	#print pk[:,0]
	pkt = cio.matterpowerfromtransfersforsinglespecies(koverh = pk[:,0], 
		transfer = (ts[:,0],ts[:,-1]), h = 0.71, As = 2.1e-9, ns  = 0.963)
	
	plt.loglog ( pk[:,0], pk[:,1])	
	plt.loglog ( pkt[:,0], pkt[:,1])
	
	plt.figure()
	from astropy.cosmology import Planck13 as cosmo
	#print sigma(ps = (pk[:,0],pk[:,1]) , R = 8.0, cosmo = cosmo)
	#plt.show()
	sys.exit()
	M = 10.**(np.arange(7,16,0.2))
	R = np.arange(0.0005, 50.0,0.1)
	#R = np.array([4,8,12])
	#print sigma (8.0)
	plt.figure()
	plt.plot(R, sigma(R))