def run_class(parameters, gettransfer): ''' Run CLASS with the input parameters and return the perturbations and the value of tau_0 (which should be fixed but we still return the value for the purpose of checking) and the earliest transfer (if asked). Print the amount of time taken to run. Args: parameters: parameters to run CLASS gettransfer (boolean): whether to get the earliest transfer Return: (pts, tau_0) if gettransfer=False and (pts, tau_0, transfer) otherwise ''' start_time = time.time() cosmo = Class() cosmo.set(parameters) cosmo.compute() pts = cosmo.get_perturbations()['scalar'] tau_0 = cosmo.get_current_derived_parameters(['conformal_age'])['conformal_age'] print("--- %s seconds ---" % (time.time() - start_time)) # 45999 is the largest redshift possible if (gettransfer): tf = cosmo.get_transfer(45999) return pts, tau_0, tf return pts, tau_0
def ComputeTransferData(settings, redshift): database_key = settings.copy() database_key.update({'redshift': tuple(redshift)}) database = Database.Database(config.DATABASE_DIR) if database_key in database: return database[database_key], redshift else: cosmo = Class() cosmo.set(settings) cosmo.compute() outputData = [cosmo.get_transfer(z) for z in redshift] # Calculate d_g/4+psi for transfer_function_dict in outputData: transfer_function_dict["d_g/4 + psi"] = transfer_function_dict["d_g"]/4 + transfer_function_dict["psi"] # Now filter the relevant fields fields = TRANSFER_QUANTITIES + ["k (h/Mpc)"] outputData = [{field: outputData[i][field] for field in fields} for i in range(len(redshift))] database[database_key] = outputData return outputData, redshift
def ComputeTransferData(settings, redshift): database_key = settings.copy() database_key.update({'redshift': tuple(redshift)}) database = Database.Database(config.DATABASE_DIR) if database_key in database: return database[database_key], redshift else: cosmo = Class() cosmo.set(settings) cosmo.compute() outputData = [cosmo.get_transfer(z) for z in redshift] # Calculate d_g/4+psi for transfer_function_dict in outputData: transfer_function_dict["d_g/4 + psi"] = transfer_function_dict[ "d_g"] / 4 + transfer_function_dict["psi"] # Now filter the relevant fields fields = TRANSFER_QUANTITIES + ["k (h/Mpc)"] outputData = [{field: outputData[i][field] for field in fields} for i in range(len(redshift))] database[database_key] = outputData return outputData, redshift
'tau_reio':0.079, 'output':'tCl,lCl,mPk,dTk,vTk', 'use_big_theta_scf': 'yes', 'scf_has_perturbations': 'yes', 'attractor_ic_scf': 'no', 'write background':'yes', 'compute damping scale':'yes'} try: cosmo.set(params) # Set the parameters to the cosmological code cosmo.compute() # solve physics m_axion = 10**cosmo.log10_m_axion()*h*100/_c_ print "m_axion %f"%(m_axion) # k_res_analytical = 0.79*m_axion*Theta_initial[j]*(10**ac[i]) k_res_analytical = 0.79*m_axion*Theta_initial[j]*(10**ac[i]) one_time = cosmo.get_transfer(0) # transfer functions at each time tau k = one_time['k (h/Mpc)'] delta_phi_scf = np.abs(one_time['delta_phi_scf']) ##evaluated today k_of_delta = interp1d(delta_phi_scf,k) background = cosmo.get_background() # load background table background_tau = background['conf. time [Mpc]'] # read conformal times in background table background_z = background['z'] # read redshift background_Om_scf = background['(.)Omega_scf'] # read redshift background_z_at_tau = interp1d(background_tau,background_z) phi_enveloppe = Theta_initial[j]*10**cosmo.log10_f_axion()*(2/(10**ac[i]))**(-3.*(1+wn)/2/n_axion) ##evaluated today delta_max = np.sqrt(delta_phi_scf*delta_phi_scf*A_s*(k/K_star)**(n_s-1))/(phi_enveloppe) print max(delta_max), np.where(delta_max==max(delta_max)) k_res_numerical = k[np.where(delta_max==max(delta_max))]*h ##evaluated today print "k_res analytical %f Mpc-1, k_res numerical %f Mpc-1"%(k_res_analytical,k_res_numerical) except CosmoComputationError: # this happens when CLASS fails print "bug!"
#print derived.viewkeys() z_rec = derived['z_rec'] z_rec = int(1000. * z_rec) / 1000. # round down at 4 digits after coma M.struct_cleanup() # clean output M.empty() # clean input # # call CLASS again (will compute transfer functions at inout value z_rec) # M = Class() M.set(common_settings) M.set({'z_pk': z_rec}) M.compute() # # load transfer functions at recombination # one_time = M.get_transfer(z_rec) print one_time.viewkeys() k = one_time['k (h/Mpc)'] Theta0 = 0.25 * one_time['d_g'] phi = one_time['phi'] psi = one_time['psi'] theta_b = one_time['t_b'] # compute related quantitites R = 3. / 4. * M.Omega_b() / M.Omega_g() / ( 1 + z_rec) # R = 3/4 * (rho_b/rho_gamma) at z_rec zero_point = -(1. + R) * psi # zero point of oscillations: -(1.+R)*psi # # get Theta0 oscillation amplitude (for vertical scale of plot) # Theta0_amp = max(Theta0.max(), -Theta0.min()) #
# tau_eq = background_tau_at_mr(1.) tau_lambda = background_tau_at_lm(1.) # # check and inform user whether intiial arbitrary choice of z_max_pk was OK max_z_needed = background_z_at_tau(tau[0]) if max_z_needed > z_max_pk: print 'you must increase the value of z_max_pk to at least ',max_z_needed () + 1 # this strange line is just a trick to stop the script execution there else: print 'in a next run with the same values of tau, you may decrease z_max_pk from ',z_max_pk,' to ',max_z_needed # # get transfer functions at each time and build arrays Theta0(tau,k) and phi(tau,k) # for i in range(tau_num): one_time = M.get_transfer(background_z_at_tau(tau[i])) # transfer functions at each time tau if i ==0: # if this is the first time in the loop: create the arrays (k, Theta0, phi) k = one_time['k (h/Mpc)'] k_num = len(k) Theta0 = np.zeros((tau_num,k_num)) phi = np.zeros((tau_num,k_num)) Theta0[i,:] = 0.25*one_time['d_g'][:] phi[i,:] = one_time['phi'][:] # # find the global extra of Theta0(tau,k) and phi(tau,k), used to define color code later # Theta_amp = max(Theta0.max(),-Theta0.min()) phi_amp = max(phi.max(),-phi.min()) # # reshaping of (k,tau) necessary to call the function 'pcolormesh' #
tau_eq = background_tau_at_mr(1.) tau_lambda = background_tau_at_lm(1.) # # check and inform user whether intiial arbitrary choice of z_max_pk was OK max_z_needed = background_z_at_tau(tau[0]) if max_z_needed > z_max_pk: print 'you must increase the value of z_max_pk to at least ', max_z_needed ( ) + 1 # this strange line is just a trick to stop the script execution there else: print 'in a next run with the same values of tau, you may decrease z_max_pk from ', z_max_pk, ' to ', max_z_needed # # get transfer functions at each time and build arrays Theta0(tau,k) and phi(tau,k) # for i in range(tau_num): one_time = M.get_transfer(background_z_at_tau( tau[i])) # transfer functions at each time tau if i == 0: # if this is the first time in the loop: create the arrays (k, Theta0, phi) k = one_time['k (h/Mpc)'] k_num = len(k) Theta0 = np.zeros((tau_num, k_num)) phi = np.zeros((tau_num, k_num)) Theta0[i, :] = 0.25 * one_time['d_g'][:] phi[i, :] = one_time['phi'][:] # # find the global extra of Theta0(tau,k) and phi(tau,k), used to define color code later # Theta_amp = max(Theta0.max(), -Theta0.min()) phi_amp = max(phi.max(), -phi.min()) # # reshaping of (k,tau) necessary to call the function 'pcolormesh' #
from scipy.interpolate import interp1d import FF2 import myfuncs as my from numpy import linalg as LA import pickle #Run ClASS-code and get transfer function for delta_cdm cosmo = Class() cosmo.set({ 'output': 'tCl mPk dTk vTk', 'P_k_max_1/Mpc': 35, 'gauge': 'Synchronous', 'z_pk': '100., 0.' }) cosmo.compute() tr = cosmo.get_transfer(49) delta_cdm = tr['d_cdm'] k = tr['k (h/Mpc)'] * cosmo.h() # Create interpolating function for transfer function dfunc = interp1d(k, delta_cdm, kind='cubic', bounds_error=False, fill_value=0) # define class for saving data class data: def __init__(self, Dat): self.Dat = Dat self.K = [] self.Kused = [] self.Kl = [] self.Klu = []
'z_reio': 7.82, 'n_s': 0.9665, 'A_s': 2.105e-9, 'P_k_max_1/Mpc': 500.0, 'perturbed recombination': 'y', 'non linear': 'halofit' } M = Class() M.set(class_parameters) M.set({'z_pk': z_compute}) M.compute() h = M.h() # get reduced Hubble for conversions to 1/Mpc one_time = M.get_transfer(z_compute) # Transfer functions # Convert to units of Mpc^{-1} k_ary = one_time['k (h/Mpc)'] * h delta_b_ary = one_time['d_b'] delta_chi_ary = one_time['d_chi'] n_s = M.n_s() # Primordial PS k_pivot = 0.05 P_s = 2.105e-9 * (k_ary / k_pivot) ** (n_s - 1)
"P_k_max_1/Mpc": kmax, "reio_parametrization": "reio_none", "YHe": "BBN", "k_output_values": kmax, "k_per_decade_for_pk": 100, } print("Running CLASS") # Run CLASS model = Class() model.set(params) model.compute() # Extract wavenumbers and prepare redshifts k = model.get_transfer(0)["k (h/Mpc)"] * h a = np.exp(np.arange(0, np.log(amin), -0.01))[::-1] z = 1.0 / a - 1.0 nk = len(k) nz = len(z) print("We have", nk, "wavenumbers and", nz, "redshifts") keys = model.get_transfer(0).keys() print("Available transfer functions:") print(keys) # Prepare dictionary pt = {} for key in keys:
#print derived.viewkeys() z_rec = derived['z_rec'] z_rec = int(1000.*z_rec)/1000. # round down at 4 digits after coma M.struct_cleanup() # clean output M.empty() # clean input # # call CLASS again (will compute transfer functions at inout value z_rec) # M = Class() M.set(common_settings) M.set({'z_pk':z_rec}) M.compute() # # load transfer functions at recombination # one_time = M.get_transfer(z_rec) print one_time.viewkeys() k = one_time['k (h/Mpc)'] Theta0 = 0.25*one_time['d_g'] phi = one_time['phi'] psi = one_time['psi'] theta_b = one_time['t_b'] # compute related quantitites R = 3./4.*M.Omega_b()/M.Omega_g()/(1+z_rec) # R = 3/4 * (rho_b/rho_gamma) at z_rec zero_point = -(1.+R)*psi # zero point of oscillations: -(1.+R)*psi # # get Theta0 oscillation amplitude (for vertical scale of plot) # Theta0_amp = max(Theta0.max(),-Theta0.min()) # # use table of background quantitites to find the wavenumbers corresponding to