CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Pk_BoltzmannSolver.cpp

This example shows how to compute the matter power spectrum with CAMB and CLASS at different redshifts

// =================================================================================================
// Example code: how to compute the matter power spectrum with CAMB and CLASS at different redshifts
// =================================================================================================
#include "Cosmology.h"
using namespace std;
int main () {
try {
// -----------------------------------------------------------------
// ------------------ set a cosmological model -----------------
// -----------------------------------------------------------------
// -----------------------------------------------------------------
// --------- compute power spectrum with CAMB and CLASS -----------
// -----------------------------------------------------------------
// choose a vector of redshifts
const vector<double> redshifts = {0., 0.5, 1., 1.5};
// choose a vector of scales
const vector<double> kk = cbl::logarithmic_bin_vector(100, 0.001, 1.);
// choose the linear power spectrum
const bool do_NL = false;
// compute the power spectra with different Boltzmann solver and compare them
vector<vector<double>> Pk_CAMB = cosm.Pk_matter(kk, "CAMB", do_NL, redshifts);
vector<vector<double>> Pk_CLASS = cosm.Pk_matter(kk, "CLASS", do_NL, redshifts);
for (size_t ii=0; ii<redshifts.size(); ii++)
cout << "At redshift z = "+cbl::conv(redshifts[ii], cbl::par::fDP2)+", at scale k = "+cbl::conv(kk[50], cbl::par::fDP2)+" Mpc/h, the relative percentage difference is: "+cbl::conv((Pk_CAMB[ii][50]-Pk_CLASS[ii][50])/Pk_CAMB[ii][50]*100., cbl::par::fDP2) << "%"<< endl;
}
catch(cbl::glob::Exception &exc) { std::cerr << exc.what() << std::endl; exit(1); }
return 0;
}
The class Cosmology.
int main()
main function to create the logo of the CosmoBolognaLib
Definition: Logo.cpp:41
The class Cosmology.
Definition: Cosmology.h:277
The class Exception.
Definition: Exception.h:111
const char * what() const noexcept override
the error description
Definition: Exception.h:203
static const char fDP2[]
conversion symbol for: double -> std::string
Definition: Constants.h:133
@ _Planck18_
Planck collaboration 2018, Paper VI: Table 2, TT,TE,EE+lowE+lensing.
std::vector< double > Pk_CAMB(const bool nonlinear, const double redshift, const double kmin, const double kmax, const int npoints, const double ombh2, const double omch2, const double omnuh2, const double massless_nu, const int massive_nu, const double omk, const double H0, const double ns, const double As, const double pivot_scalar=0.05, const double w=-1., const double wa=0., const double tau=2.1e-9, const bool accurate_massive_nu=false, const int neutrino_hierarchy=3, const int dark_energy_model=1, const double cs2_lam=1., const double T_cmb=2.7255, const double helium_fraction=0.24)
Get the matter power spectrum from CAMB.
Definition: CAMB.cpp:42
std::string conv(const T val, const char *fact)
convert a number to a std::string
Definition: Kernel.h:903
std::vector< T > logarithmic_bin_vector(const size_t nn, const T min, const T max)
fill a std::vector with logarithmically spaced values
Definition: Kernel.h:1621