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


This example shows how to convert redshifts into comoving distances

// ===================================================================================================================
// Example code: how to convert redshifts into comoving distances -- testing the performances of two different methods
// ===================================================================================================================
#include "Cosmology.h"
int main () {
try {
if (system ("clear")) {}
// -------------------------------------------------------------
// ---------------- set the cosmological parameters ------------
// -------------------------------------------------------------
// ---------------------------------------------------------------------------------------
// ---------------- set the redshifts to be converted into comoving distances ------------
// ---------------------------------------------------------------------------------------
int step = 10000;
double z_min = 0.1;
double z_max = 2.;
std::vector<double> redshift = cbl::linear_bin_vector<double>(step, z_min, z_max);
// --------------------------------------------------------------------------------------------------------
// ---------------- convert the redshifts into comoving distances using a standard integration ------------
// --------------------------------------------------------------------------------------------------------
std::cout << "Computing comoving distances with a standard integration..." << std::endl;
time_t start, end;
double D_C;
time (&start);
for (auto &&zz : redshift)
D_C = cosmology.D_C(zz);
std::cout << "D_C = " << D_C << std::endl;
time (&end);
double T1 = difftime(end, start);
std::cout << "The computetional time is: " << T1 << " sec" << std::endl << std::endl;
// -----------------------------------------------------------------------------------------------------
// ---------------- convert the redshifts into comoving distances using a smart integration ------------
// -----------------------------------------------------------------------------------------------------
std::cout << "Computing comoving distances with a faster method..." << std::endl;
time (&start);
for (auto &&zz : redshift)
D_C = cosmology.D_C_LCDM(zz);
std::cout << "D_C = " << D_C << std::endl;
time (&end);
double T2 = difftime(end, start);
std::cout << "The computational time is: " << T2 << " sec" << std::endl << std::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
@ _Planck18_
Planck collaboration 2018, Paper VI: Table 2, TT,TE,EE+lowE+lensing.