def GetSpectra(params, type='lensed', lmax=2000): cosmo = classy.Class() cosmo.set(params) cosmo.compute() computed_cls = cosmo.lensed_cl(lmax) c2d = computed_cls['ell'] * (computed_cls['ell'] + 1) / 2. / np.pi * (2.725e6**2) cosmo.struct_cleanup() cosmo.empty() return computed_cls, c2d
def setup(options): # Read options from the ini file which are fixed across # the length of the chain config = { 'lmax': options.get_int(option_section, 'lmax', default=2000), 'zmax': options.get_double(option_section, 'zmax', default=4.0), 'kmax': options.get_double(option_section, 'kmax', default=50.0), 'debug': options.get_bool(option_section, 'debug', default=False), } # Create the object that connects to Class config['cosmo'] = classy.Class() # Return all this config information return config
def initialise(paths): """ Initialisation routine This function take the paths, initialise a :class:`data` instance, a cosmological code instance. Parameters ---------- paths: dict paths to all the necessary files """ # Report the cosmological code version. # Official version number common_file_path = os.path.join( paths['cosmo'], 'include', 'common.h') with open(common_file_path, 'r') as common_file: for line in common_file: if line.find('_VERSION_') != -1: version = line.split()[-1].replace('"', '') break print('with CLASS %s' % version) # initialise data class data = Data(paths) # Loading up the cosmological backbone. # For the moment, only CLASS has been wrapped. classy_path = '' for elem in os.listdir(os.path.join( paths['cosmo'], "python", "build")): if elem.find("lib.") != -1: classy_path = os.path.join( paths['cosmo'], "python", "build", elem) break # Inserting the previously found path into the list of folders to # search for python modules. sys.path.insert(1, classy_path) import classy cosmo = classy.Class() print('classy location', classy.__file__) return cosmo, data
def setup(options): # Read options from the ini file which are fixed across # the length of the chain config = { 'lmax': options.get_int(option_section, 'lmax', default=2000), 'zmax': options.get_double(option_section, 'zmax', default=4.0), 'kmax': options.get_double(option_section, 'kmax', default=50.0), 'debug': options.get_bool(option_section, 'debug', default=False), 'lensing': options.get_bool(option_section, 'lensing', default=True), } for _, key in options.keys(option_section): if key.startswith('class_'): config[key] = block[option_section, key] # Create the object that connects to Class config['cosmo'] = classy.Class() # Return all this config information return config
def setup(options): #Read options from the ini file which are fixed across #the length of the chain config = { 'lmax': options.get_int(option_section, 'lmax', default=2500), 'zmax': options.get_double(option_section, 'zmax', default=3.), #'zmax_pk': options.get_double(option_section,'zmax_pk', default=3.), #TB experiment 'kmax': options.get_double(option_section, 'kmax', default=1.0), 'debug': options.get_bool(option_section, 'debug', default=False), 'lensing': options.get_string(option_section, 'lensing', default='yes'), 'expansion_model': options.get_string(option_section, 'expansion_model', default='lcdm'), 'gravity_model': options.get_string(option_section, 'gravity_model', default='propto_omega'), 'modes': options.get_string(option_section, 'modes', default='s'), 'output': options.get_string(option_section, 'output', default='tCl,lCl,pCl,mPk,mTk'), 'sBBN file': options.get_string(option_section, 'sBBN_file'), 'k_output_values': options.get_string(option_section, 'k_output_values', default='') #'skip_stability_tests_smg': options.get_string(option_section, 'skip_stability_tests_smg', default = 'no'), #'background_verbose': options.get_int(option_section,'background_verbose', default=1), #'thermodynamics_verbose': options.get_int(option_section,'thermodynamics_verbose', default=10) #'kineticity_safe_smg': options.get_double(option_section,'kineticity_safe_smg', default=1e-5) } #Create the object that connects to Class config['cosmo'] = classy.Class() #Return all this config information return config
"h": p['h'], "Omega_cdm": p['omegaM'] - p['omegaB'], "Omega_b": p['omegaB'], "A_s": 2e-9, "n_s": 1.0, # "w0_fld" : -1.0, # "wa_fld" : 0.0, "back_integration_stepsize": 1.0e-3, # Req. for agreement at <1e-5 with CCL. "Omega_k": 0.0, "N_ur": 2.0, # normally 3 "N_ncdm": 1, # 1 species "m_ncdm": 0.06 } cosm = classy.Class() cosm.set(class_params) cosm.compute() print cosm.angular_distance(1090.) print cosm.Omega_g() * cosm.h()**2. #, cosmo.Omega0_m() #help(cosm) exit() z_class = cosm.get_background()["z"][:] a_class = 1. / (1. + z_class) DA_class = cosm.get_background()["ang.diam.dist."][:] #d_co = cosm.get_background()["comov. dist."][:] cosm.struct_cleanup() # Calculate using our code
def get_class_pk_lin(cosmo): """Run CLASS and return the linear power spectrum. Args: cosmo (:class:`~pyccl.core.Cosmology`): Cosmological parameters. The cosmological parameters with which to run CLASS. Returns: :class:`~pyccl.pk2d.Pk2D`: Power spectrum object.\ The linear power spectrum. """ assert HAVE_CLASS, ("You must have the python wrapper for CLASS " "installed to run CCL with CLASS!") params = { "output": "mPk", "non linear": "none", "P_k_max_1/Mpc": cosmo.cosmo.spline_params.K_MAX_SPLINE, "z_max_pk": 1.0 / cosmo.cosmo.spline_params.A_SPLINE_MINLOG_PK - 1.0, "modes": "s", "lensing": "no", "h": cosmo["h"], "Omega_cdm": cosmo["Omega_c"], "Omega_b": cosmo["Omega_b"], "Omega_k": cosmo["Omega_k"], "n_s": cosmo["n_s"] } # cosmological constant? # set Omega_Lambda = 0.0 if w !=-1 or wa != 0 if cosmo['w0'] != -1 or cosmo['wa'] != 0: params["Omega_Lambda"] = 0 params['w0_fld'] = cosmo['w0'] params['wa_fld'] = cosmo['wa'] # neutrino parameters # massless neutrinos if cosmo["N_nu_rel"] > 1e-4: params["N_ur"] = cosmo["N_nu_rel"] else: params["N_ur"] = 0.0 # massive neutrinos if cosmo["N_nu_mass"] > 0: params["N_ncdm"] = cosmo["N_nu_mass"] masses = lib.parameters_get_nu_masses(cosmo._params, 3) params["m_ncdm"] = ", ".join( ["%g" % m for m in masses[:cosmo["N_nu_mass"]]]) params["T_cmb"] = cosmo["T_CMB"] # if we have sigma8, we need to find A_s if np.isfinite(cosmo["A_s"]): params["A_s"] = cosmo["A_s"] elif np.isfinite(cosmo["sigma8"]): # in this case, CCL will internally normalize for us when we init # the linear power spectrum - so we just get close A_s_fid = 2.43e-9 * (cosmo["sigma8"] / 0.87659)**2 params["A_s"] = A_s_fid else: raise CCLError("Could not normalize the linear power spectrum! " "A_s = %f, sigma8 = %f" % (cosmo['A_s'], cosmo['sigma8'])) model = None try: model = classy.Class() model.set(params) model.compute() # Set k and a sampling from CCL parameters nk = lib.get_pk_spline_nk(cosmo.cosmo) na = lib.get_pk_spline_na(cosmo.cosmo) status = 0 a_arr, status = lib.get_pk_spline_a(cosmo.cosmo, na, status) check(status) # FIXME - getting the lowest CLASS k value from the python interface # appears to be broken - setting to 1e-5 which is close to the # old value lk_arr = np.log( np.logspace(-5, np.log10(cosmo.cosmo.spline_params.K_MAX_SPLINE), nk)) # we need to cut this to the max value used for calling CLASS msk = lk_arr < np.log(cosmo.cosmo.spline_params.K_MAX_SPLINE) nk = int(np.sum(msk)) lk_arr = lk_arr[msk] # now do interp by hand ln_p_k_and_z = np.zeros((na, nk), dtype=np.float64) for aind in range(na): z = max(1.0 / a_arr[aind] - 1, 1e-10) for kind in range(nk): ln_p_k_and_z[aind, kind] = np.log( model.pk_lin(np.exp(lk_arr[kind]), z)) finally: if model is not None: model.struct_cleanup() model.empty() params["P_k_max_1/Mpc"] = cosmo.cosmo.spline_params.K_MAX_SPLINE # make the Pk2D object pk_lin = Pk2D(pkfunc=None, a_arr=a_arr, lk_arr=lk_arr, pk_arr=ln_p_k_and_z, is_logp=True, extrap_order_lok=1, extrap_order_hik=2, cosmo=cosmo) return pk_lin
'T_cmb': self.Tcmb, 'tau_reio': self.tau_reio, 'YHe': self.YHe, 'reionization_exponent': self.reion_exponent, 'reionization_width': self.reion_width, 'P_k_max_1/Mpc': 200, 'output': 'dTk,mPk,tCl', } # m_dmeff = 1.0 # GeV # sigma_dmeff = 1e-41 # cm^2 # Vrel_dmeff = 30 # km/s at z ~ 1010 params = LCDM.class_params() z_pk = 60. params.update({'z_pk': z_pk}) cl = classy.Class() cl.set(params) cl.compute() import pyccl as ccl cosmo = ccl.Cosmology(Omega_c=params['omega_cdm'], Omega_b=params['omega_b'], h=params['h'], n_s=params['n_s'], A_s=params['A_s']) # trying to adapt from here: https://github.com/damonge/ShCl_like/blob/0350b0cca5de51cd92efb8e27be292bbbb449bce/shcl_like/clccl.py#L141 z_bg = np.concatenate((np.linspace(0, 10, 100), np.geomspace(10, 1500, 50))) a = 1/(1 + z_bg[::-1]) distance = cl.z_of_r(z_bg)
def classy(self): if self._classy is None: self._classy = classy.Class() self._classy.set(self.cosmo.class_params) self._classy.compute() return self._classy