FML
Loading...
Searching...
No Matches
FML::FILEUTILS::LinearTransferData Class Reference

This class reads transfer/power-spectrum data from the output of a Einstein-Boltzmann solver. More...

#include <CAMBReader.h>

Public Member Functions

 LinearTransferData ()
 
 LinearTransferData (std::string fileformat)
 
 LinearTransferData (double Omegab, double OmegaCDM, double OmegaMNu, double kpivot_mpc, double As, double ns, double h, std::string fileformat="CAMB")
 
 operator bool () const
 Has the splines been created?
 
void read_transfer (std::string infofilename, bool verbose=false)
 Read the infofile, read transferfiles listed in that and make splines of T(k,z)
 
DVector2D read_transfer_single (std::string filename) const
 Read a single transfer function.
 
std::pair< DVector, DVector > read_power_single (std::string filename) const
 Read a single power-spectrum file.
 
double get_cdm_transfer_function (double k_hmpc, double a) const
 CDM transfer function (T = Delta/k^2) in units of Mpc^2.
 
double get_baryon_transfer_function (double k_hmpc, double a) const
 Baryon transfer function (T = Delta/k^2) in units of Mpc^2.
 
double get_photon_transfer_function (double k_hmpc, double a) const
 Photon transfer function (T = Delta/k^2) in units of Mpc^2.
 
double get_neutrino_transfer_function (double k_hmpc, double a) const
 Neutrino transfer function (T = Delta/k^2) in units of Mpc^2.
 
double get_massive_neutrino_transfer_function (double k_hmpc, double a) const
 Massive neutrino transfer function (T = Delta/k^2) in units of Mpc^2.
 
double get_cdm_baryon_transfer_function (double k_hmpc, double a) const
 Weighted CDM+Baryon transfer function (T = Delta/k^2) in units of Mpc^2 Since baryons are CDM in the simulations this is the one to use.
 
double get_total_transfer_function (double k_hmpc, double a) const
 Total (CDM+b+nu) transfer function (T = Delta/k^2) in units of Mpc^2.
 
double get_weyl_transfer_function (double k_hmpc, double a) const
 Weyl transfer function.
 
double get_newtonian_velocity_divergence_transfer (int species, double k_hmpc, double a) const
 theta/(aH R), in Newtonian gauge.
 
double get_baryon_growth_rate (double k_hmpc, double a) const
 Weighted baryon growth rate.
 
double get_cdm_baryon_growth_rate (double k_hmpc, double a) const
 Weighted CDM+Baryon growth rate.
 
double get_cdm_growth_rate (double k_hmpc, double a) const
 CDM growth rate.
 
double get_massive_neutrino_growth_rate (double k_hmpc, double a) const
 Massive neutrino growth rate.
 
double get_total_growth_rate (double k_hmpc, double a) const
 Total (CDM+b+nu) growth rate.
 
double get_primordial_power_spectrum (double k_hmpc) const
 Primordial power-spectrum today in units of (Mpc/h)^3.
 
double get_cdm_baryon_power_spectrum (double k_hmpc, double a) const
 (CDM+b) power-spectrum in units of (Mpc/h)^3
 
double get_massive_neutrino_power_spectrum (double k_hmpc, double a) const
 Massive neutrino power-spectrum in units of (Mpc/h)^3.
 
double get_total_power_spectrum (double k_hmpc, double a) const
 Total (CDM+b+nu) power-spectrum in units of (Mpc/h)^3.
 
void set_As (double _As)
 
void set_ns (double _ns)
 
void set_kpivot_mpc (double _kpivot_mpc)
 
double get_As () const
 
double get_ns () const
 
double get_h () const
 
double get_kpivot_mpc () const
 
double get_Omegab () const
 
double get_OmegaCDM () const
 
double get_OmegaMNu () const
 
void output (std::string filename, double a) const
 Output some sample data.
 
void set_fileformat (std::string format)
 Set the fileformat.
 
double get_zmin_splines () const
 Get the range of the splines.
 
double get_zmax_splines () const
 The largest redshift the splines reach.
 
double get_kmin_hmpc_splines () const
 The smallest scale the splines reach.
 
double get_kmax_hmpc_splines () const
 The largest redshift the splines reach.
 
void free ()
 Free up all memory.
 

Data Fields

int n_transfer_header_lines = 0
 
int ncol_transfer_file = 0
 
int transfer_col_k = -1
 
int transfer_col_cdm = -1
 
int transfer_col_baryon = -1
 
int transfer_col_photon = -1
 
int transfer_col_nu = -1
 
int transfer_col_mnu = -1
 
int transfer_col_total = -1
 
int transfer_col_nonu = -1
 
int transfer_col_totde = -1
 
int transfer_col_weyl = -1
 
int transfer_col_vcdm = -1
 
int transfer_col_vb = -1
 
int transfer_col_vbvc = -1
 
int n_pofk_header_lines = 0
 
int ncol_pofk_file = 0
 
int pofk_col_k = -1
 
int pofk_col_pofk = -1
 

Detailed Description

This class reads transfer/power-spectrum data from the output of a Einstein-Boltzmann solver.

We have only implemented CAMB, but other formats should just be providing the column numbers above

Definition at line 115 of file CAMBReader.h.

Constructor & Destructor Documentation

◆ LinearTransferData() [1/3]

FML::FILEUTILS::LinearTransferData::LinearTransferData ( )
inline

Definition at line 185 of file CAMBReader.h.

185: fileformat("CAMB") { set_fileformat(fileformat); };
void set_fileformat(std::string format)
Set the fileformat.
Definition CAMBReader.h:286

◆ LinearTransferData() [2/3]

FML::FILEUTILS::LinearTransferData::LinearTransferData ( std::string fileformat)
inline

Definition at line 186 of file CAMBReader.h.

186: fileformat(fileformat) { set_fileformat(fileformat); };

◆ LinearTransferData() [3/3]

FML::FILEUTILS::LinearTransferData::LinearTransferData ( double Omegab,
double OmegaCDM,
double OmegaMNu,
double kpivot_mpc,
double As,
double ns,
double h,
std::string fileformat = "CAMB" )
inline

Definition at line 187 of file CAMBReader.h.

195 : Omegab(Omegab), OmegaCDM(OmegaCDM), OmegaMNu(OmegaMNu), kpivot_mpc(kpivot_mpc), As(As), ns(ns), h(h),
196 fileformat(fileformat) {
197 set_fileformat(fileformat);
198 }

Member Function Documentation

◆ free()

void FML::FILEUTILS::LinearTransferData::free ( )
inline

Free up all memory.

Definition at line 333 of file CAMBReader.h.

333 {
334 cdm_transfer_function_spline.free();
335 baryon_transfer_function_spline.free();
336 photon_transfer_function_spline.free();
337 nu_transfer_function_spline.free();
338 mnu_transfer_function_spline.free();
339 total_transfer_function_spline.free();
340 nonu_transfer_function_spline.free();
341 totde_transfer_function_spline.free();
342 weyl_transfer_function_spline.free();
343 vcdm_transfer_function_spline.free();
344 vb_transfer_function_spline.free();
345 vbvc_transfer_function_spline.free();
346 transfer_is_read = false;
347 }
void free()
Free up memory associated with the spline.
Definition Spline.cpp:715

◆ get_As()

double FML::FILEUTILS::LinearTransferData::get_As ( ) const
inline

Definition at line 259 of file CAMBReader.h.

259{ return As; }

◆ get_baryon_growth_rate()

double FML::FILEUTILS::LinearTransferData::get_baryon_growth_rate ( double k,
double a ) const
inline

Weighted baryon growth rate.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 818 of file CAMBReader.h.

818 {
819 return dlogT_dloga(baryon_transfer_function_spline, k, a);
820 }

◆ get_baryon_transfer_function()

double FML::FILEUTILS::LinearTransferData::get_baryon_transfer_function ( double k,
double a ) const
inline

Baryon transfer function (T = Delta/k^2) in units of Mpc^2.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 808 of file CAMBReader.h.

808 {
809 double z = 1.0 / a - 1.0;
810 return eval_transfer(baryon_transfer_function_spline, z, k);
811 }

◆ get_cdm_baryon_growth_rate()

double FML::FILEUTILS::LinearTransferData::get_cdm_baryon_growth_rate ( double k,
double a ) const
inline

Weighted CDM+Baryon growth rate.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 838 of file CAMBReader.h.

838 {
839 double deltaB_over_delta_CDM = get_baryon_transfer_function(k, a) / get_cdm_transfer_function(k, a);
840 double fac = Omegab / OmegaCDM * deltaB_over_delta_CDM;
841 return (get_cdm_growth_rate(k, a) + fac * get_baryon_growth_rate(k, a)) / (1.0 + fac);
842 }
double get_cdm_transfer_function(double k_hmpc, double a) const
CDM transfer function (T = Delta/k^2) in units of Mpc^2.
Definition CAMBReader.h:726
double get_cdm_growth_rate(double k_hmpc, double a) const
CDM growth rate.
Definition CAMBReader.h:736
double get_baryon_growth_rate(double k_hmpc, double a) const
Weighted baryon growth rate.
Definition CAMBReader.h:818
double get_baryon_transfer_function(double k_hmpc, double a) const
Baryon transfer function (T = Delta/k^2) in units of Mpc^2.
Definition CAMBReader.h:808

◆ get_cdm_baryon_power_spectrum()

double FML::FILEUTILS::LinearTransferData::get_cdm_baryon_power_spectrum ( double k,
double a ) const
inline

(CDM+b) power-spectrum in units of (Mpc/h)^3

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 896 of file CAMBReader.h.

896 {
897 // (with the matter-weighted total, which cancels: as before to the last digit)
898 const double Ttot_mw = get_total_transfer_function_matter_weighted(k, a);
899 double Tcdm_over_Ttot = get_cdm_baryon_transfer_function(k, a) / Ttot_mw;
900 const double Ttot = Ttot_mw * (k * h) * (k * h);
901 return Ttot * Ttot * get_primordial_power_spectrum(k) * Tcdm_over_Ttot * Tcdm_over_Ttot;
902 }
double get_primordial_power_spectrum(double k_hmpc) const
Primordial power-spectrum today in units of (Mpc/h)^3.
Definition CAMBReader.h:887
double get_cdm_baryon_transfer_function(double k_hmpc, double a) const
Weighted CDM+Baryon transfer function (T = Delta/k^2) in units of Mpc^2 Since baryons are CDM in the ...
Definition CAMBReader.h:828

◆ get_cdm_baryon_transfer_function()

double FML::FILEUTILS::LinearTransferData::get_cdm_baryon_transfer_function ( double k,
double a ) const
inline

Weighted CDM+Baryon transfer function (T = Delta/k^2) in units of Mpc^2 Since baryons are CDM in the simulations this is the one to use.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 828 of file CAMBReader.h.

828 {
829 return (Omegab * get_baryon_transfer_function(k, a) + OmegaCDM * get_cdm_transfer_function(k, a)) /
830 (Omegab + OmegaCDM);
831 }

◆ get_cdm_growth_rate()

double FML::FILEUTILS::LinearTransferData::get_cdm_growth_rate ( double k,
double a ) const
inline

CDM growth rate.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 736 of file CAMBReader.h.

736 {
737 return dlogT_dloga(cdm_transfer_function_spline, k, a);
738 }

◆ get_cdm_transfer_function()

double FML::FILEUTILS::LinearTransferData::get_cdm_transfer_function ( double k,
double a ) const
inline

CDM transfer function (T = Delta/k^2) in units of Mpc^2.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 726 of file CAMBReader.h.

726 {
727 double z = 1.0 / a - 1.0;
728 return eval_transfer(cdm_transfer_function_spline, z, k);
729 }

◆ get_h()

double FML::FILEUTILS::LinearTransferData::get_h ( ) const
inline

Definition at line 261 of file CAMBReader.h.

261{ return h; }

◆ get_kmax_hmpc_splines()

double FML::FILEUTILS::LinearTransferData::get_kmax_hmpc_splines ( ) const
inline

The largest redshift the splines reach.

Definition at line 447 of file CAMBReader.h.

447{ return kmax_hmpc_splines; }

◆ get_kmin_hmpc_splines()

double FML::FILEUTILS::LinearTransferData::get_kmin_hmpc_splines ( ) const
inline

The smallest scale the splines reach.

Definition at line 445 of file CAMBReader.h.

445{ return kmin_hmpc_splines; }

◆ get_kpivot_mpc()

double FML::FILEUTILS::LinearTransferData::get_kpivot_mpc ( ) const
inline

Definition at line 262 of file CAMBReader.h.

262{ return kpivot_mpc; }

◆ get_massive_neutrino_growth_rate()

double FML::FILEUTILS::LinearTransferData::get_massive_neutrino_growth_rate ( double k,
double a ) const
inline

Massive neutrino growth rate.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 705 of file CAMBReader.h.

705 {
706 double z = 1.0 / a - 1.0;
707 double Dnu = mnu_transfer_function_spline(z, std::log(k));
708 if (Dnu == 0.0)
709 return 0.0;
710 // T_nu oscillates in time faster than the redshift spacing of typical tables resolves at high z: with
711 // the 60 tables up to z = 200 (input/camb_data_*_zmax200) f_nu is 8-66% off at z >= 150 and
712 // k >= 0.05 h/Mpc (fine for z <= 49, k <= 0.1). Use denser tables near the redshift of interest
713 static std::atomic<bool> warned{false};
714 if (z > 50.0 and FML::ThisTask == 0 and not warned.exchange(true))
715 std::cout << "Warning: get_massive_neutrino_growth_rate at z = " << z
716 << " > 50: the neutrino growth rate from the tables can be inaccurate at high z (it needs "
717 "a fine redshift spacing of the tables)\n";
718 return dlogT_dloga(mnu_transfer_function_spline, k, a);
719 }
int ThisTask
The MPI task number.
Definition Global.cpp:39

◆ get_massive_neutrino_power_spectrum()

double FML::FILEUTILS::LinearTransferData::get_massive_neutrino_power_spectrum ( double k,
double a ) const
inline

Massive neutrino power-spectrum in units of (Mpc/h)^3.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 909 of file CAMBReader.h.

909 {
910 // (with the matter-weighted total, which cancels: as before to the last digit)
911 const double Ttot_mw = get_total_transfer_function_matter_weighted(k, a);
912 double Tmnu_over_Ttot = get_massive_neutrino_transfer_function(k, a) / Ttot_mw;
913 const double Ttot = Ttot_mw * (k * h) * (k * h);
914 return Ttot * Ttot * get_primordial_power_spectrum(k) * Tmnu_over_Ttot * Tmnu_over_Ttot;
915 }
double get_massive_neutrino_transfer_function(double k_hmpc, double a) const
Massive neutrino transfer function (T = Delta/k^2) in units of Mpc^2.
Definition CAMBReader.h:695

◆ get_massive_neutrino_transfer_function()

double FML::FILEUTILS::LinearTransferData::get_massive_neutrino_transfer_function ( double k,
double a ) const
inline

Massive neutrino transfer function (T = Delta/k^2) in units of Mpc^2.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 695 of file CAMBReader.h.

695 {
696 double z = 1.0 / a - 1.0;
697 return eval_transfer(mnu_transfer_function_spline, z, k);
698 }

◆ get_neutrino_transfer_function()

double FML::FILEUTILS::LinearTransferData::get_neutrino_transfer_function ( double k,
double a ) const
inline

Neutrino transfer function (T = Delta/k^2) in units of Mpc^2.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 685 of file CAMBReader.h.

685 {
686 double z = 1.0 / a - 1.0;
687 return eval_transfer(nu_transfer_function_spline, z, k);
688 }

◆ get_newtonian_velocity_divergence_transfer()

double FML::FILEUTILS::LinearTransferData::get_newtonian_velocity_divergence_transfer ( int species,
double k_hmpc,
double a ) const
inline

theta/(aH R), in Newtonian gauge.

species: 0=CDM, 1=baryon, 2=CDM+baryon. CAMB stores -theta/(aH k_Mpc^2); this is a direct velocity, not dln(delta)/dlna.

Definition at line 224 of file CAMBReader.h.

224 {
225 const double z = 1.0 / a - 1.0;
226 const double k_mpc = k_hmpc * h;
227 auto velocity = [&](const Spline2D & spline) {
228 return -eval_transfer(spline, z, k_hmpc) * k_mpc * k_mpc;
229 };
230 if (species == 0 && transfer_col_vcdm >= 0)
231 return velocity(vcdm_transfer_function_spline);
232 if (species == 1 && transfer_col_vb >= 0)
233 return velocity(vb_transfer_function_spline);
234 if (species == 2 && transfer_col_vcdm >= 0 && transfer_col_vb >= 0)
235 return (OmegaCDM * velocity(vcdm_transfer_function_spline) +
236 Omegab * velocity(vb_transfer_function_spline)) /
237 (OmegaCDM + Omegab);
238 throw std::runtime_error("[LinearTransferData] Direct Newtonian velocity column is unavailable");
239 }
Spline2D spline
Definition BBN.cpp:17
FML_UNITS_INLINE constexpr double velocity
Definition Units.h:48

◆ get_ns()

double FML::FILEUTILS::LinearTransferData::get_ns ( ) const
inline

Definition at line 260 of file CAMBReader.h.

260{ return ns; }

◆ get_Omegab()

double FML::FILEUTILS::LinearTransferData::get_Omegab ( ) const
inline

Definition at line 263 of file CAMBReader.h.

263{ return Omegab; }

◆ get_OmegaCDM()

double FML::FILEUTILS::LinearTransferData::get_OmegaCDM ( ) const
inline

Definition at line 264 of file CAMBReader.h.

264{ return OmegaCDM; }

◆ get_OmegaMNu()

double FML::FILEUTILS::LinearTransferData::get_OmegaMNu ( ) const
inline

Definition at line 265 of file CAMBReader.h.

265{ return OmegaMNu; }

◆ get_photon_transfer_function()

double FML::FILEUTILS::LinearTransferData::get_photon_transfer_function ( double k,
double a ) const
inline

Photon transfer function (T = Delta/k^2) in units of Mpc^2.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 675 of file CAMBReader.h.

675 {
676 double z = 1.0 / a - 1.0;
677 return eval_transfer(photon_transfer_function_spline, z, k);
678 }

◆ get_primordial_power_spectrum()

double FML::FILEUTILS::LinearTransferData::get_primordial_power_spectrum ( double k) const
inline

Primordial power-spectrum today in units of (Mpc/h)^3.

Parameters
[in]kFourier wavenumber in h/Mpc

Definition at line 887 of file CAMBReader.h.

887 {
888 return 2.0 * M_PI * M_PI / (k * k * k) * As * std::pow(k * h / kpivot_mpc, ns - 1.0);
889 }

◆ get_total_growth_rate()

double FML::FILEUTILS::LinearTransferData::get_total_growth_rate ( double k,
double a ) const
inline

Total (CDM+b+nu) growth rate.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 879 of file CAMBReader.h.

879 {
880 return dlogT_dloga(total_transfer_function_spline, k, a);
881 }

◆ get_total_power_spectrum()

double FML::FILEUTILS::LinearTransferData::get_total_power_spectrum ( double k,
double a ) const
inline

Total (CDM+b+nu) power-spectrum in units of (Mpc/h)^3.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 869 of file CAMBReader.h.

869 {
870 double Ttot = get_total_transfer_function(k, a) * (k * h) * (k * h);
871 return Ttot * Ttot * get_primordial_power_spectrum(k);
872 }
double get_total_transfer_function(double k_hmpc, double a) const
Total (CDM+b+nu) transfer function (T = Delta/k^2) in units of Mpc^2.
Definition CAMBReader.h:849

◆ get_total_transfer_function()

double FML::FILEUTILS::LinearTransferData::get_total_transfer_function ( double k,
double a ) const
inline

Total (CDM+b+nu) transfer function (T = Delta/k^2) in units of Mpc^2.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 849 of file CAMBReader.h.

849 {
850 // The total column of the tables if we have it (it has the right weight of the neutrinos also when they
851 // are not fully non-relativistic: the matter-weighted sum with a constant OmegaMNu was 0.2% off at z = 49
852 // and 2% at z = 200 for 0.4 eV)
853 if (transfer_col_total >= 0)
854 return eval_transfer(total_transfer_function_spline, 1.0 / a - 1.0, k);
855 return get_total_transfer_function_matter_weighted(k, a);
856 }

◆ get_weyl_transfer_function()

double FML::FILEUTILS::LinearTransferData::get_weyl_transfer_function ( double k,
double a ) const
inline

Weyl transfer function.

Parameters
[in]kFourier wavenumber in h/Mpc
[in]aScalefactor

Definition at line 665 of file CAMBReader.h.

665 {
666 double z = 1.0 / a - 1.0;
667 return eval_transfer(weyl_transfer_function_spline, z, k);
668 }

◆ get_zmax_splines()

double FML::FILEUTILS::LinearTransferData::get_zmax_splines ( ) const
inline

The largest redshift the splines reach.

Definition at line 443 of file CAMBReader.h.

443{ return zmax_splines; }

◆ get_zmin_splines()

double FML::FILEUTILS::LinearTransferData::get_zmin_splines ( ) const
inline

Get the range of the splines.

The smallest redshift the splines reach.

Definition at line 441 of file CAMBReader.h.

441{ return zmin_splines; }

◆ operator bool()

FML::FILEUTILS::LinearTransferData::operator bool ( ) const
inlineexplicit

Has the splines been created?

Definition at line 201 of file CAMBReader.h.

201{ return transfer_is_read; }

◆ output()

void FML::FILEUTILS::LinearTransferData::output ( std::string filename,
double a ) const
inline

Output some sample data.

Output some of the data we have read in and splines to see that its ok.

Definition at line 920 of file CAMBReader.h.

920 {
921 const int nk = 100;
922 const double kmin = 1e-4;
923 const double kmax = 10.0;
924 std::ofstream fp(filename.c_str());
925 if (not fp.is_open())
926 return;
927 for (int i = 0; i < nk; i++) {
928 double k_hmpc = std::exp(std::log(kmin) + std::log(kmax / kmin) * i / double(nk));
929 fp << std::setw(15) << k_hmpc << " ";
930 if (transfer_col_total >= 0)
931 fp << std::setw(15) << get_total_power_spectrum(k_hmpc, a) << " ";
933 fp << std::setw(15) << get_cdm_baryon_power_spectrum(k_hmpc, a) << " ";
934 if (transfer_col_mnu >= 0 and transfer_col_total >= 0)
935 fp << std::setw(15) << get_massive_neutrino_power_spectrum(k_hmpc, a) << " ";
936 if (transfer_col_mnu >= 0)
937 fp << std::setw(15) << get_total_transfer_function(k_hmpc, a) << " ";
938 if (transfer_col_cdm >= 0 and transfer_col_baryon >= 0)
939 fp << std::setw(15) << get_cdm_baryon_transfer_function(k_hmpc, a) << " ";
940 if (transfer_col_mnu >= 0)
941 fp << std::setw(15) << get_massive_neutrino_transfer_function(k_hmpc, a) << " ";
942 fp << "\n";
943 }
944 }
const std::string filename
Definition Main.cpp:63
double get_cdm_baryon_power_spectrum(double k_hmpc, double a) const
(CDM+b) power-spectrum in units of (Mpc/h)^3
Definition CAMBReader.h:896
double get_massive_neutrino_power_spectrum(double k_hmpc, double a) const
Massive neutrino power-spectrum in units of (Mpc/h)^3.
Definition CAMBReader.h:909
double get_total_power_spectrum(double k_hmpc, double a) const
Total (CDM+b+nu) power-spectrum in units of (Mpc/h)^3.
Definition CAMBReader.h:869
const double kmax
Definition test_fnl.cpp:43

◆ read_power_single()

std::pair< DVector, DVector > FML::FILEUTILS::LinearTransferData::read_power_single ( std::string filename) const
inline

Read a single power-spectrum file.

Read a single power-spectrum file and return the data as (k, P(k))

Parameters
[in]filenameFilename of a CAMB P(k) file

Definition at line 399 of file CAMBReader.h.

399 {
400 FML::assert_mpi(pofk_col_k >= 0, "Fileformat is not correct");
401 FML::assert_mpi(n_pofk_header_lines >= 0, "Fileformat is not correct");
402 FML::assert_mpi(ncol_pofk_file > 0, "Fileformat is not correct");
403
404 // Open CAMB power function file for reading
405 std::ifstream fp(filename);
406 if (not fp) {
407 throw std::runtime_error("Error read_power_single: cannot open [" + filename + "]\n");
408 }
409
410 // Read the header lines
411 for (int i = 0; i < n_pofk_header_lines; i++) {
412 std::string line;
413 std::getline(fp, line);
414 }
415
416 // Read data row by row
417 DVector2D indata;
419 for (;;) {
420 fp >> tmp[0];
421 if (fp.eof())
422 break;
423 for (int i = 1; i < ncol_pofk_file; i++) {
424 fp >> tmp[i];
425 }
426 indata.push_back(tmp);
427 }
428
429 // Transpose the data
430 DVector2D data(ncol_transfer_file, DVector(indata.size()));
431 for (size_t i = 0; i < indata.size(); i++) {
432 for (size_t j = 0; j < indata[i].size(); j++) {
433 data[j][i] = indata[i][j];
434 }
435 }
436
437 return {data[pofk_col_k], data[pofk_col_pofk]};
438 }
FML::SOLVERS::FFTLog::DVector DVector
Definition Main.cpp:4
FML::INTERPOLATION::SPLINE::DVector2D DVector2D
Definition Main.cpp:8
FML::INTERPOLATION::SPLINE::DVector DVector
Definition CAMBReader.h:17

◆ read_transfer()

void FML::FILEUTILS::LinearTransferData::read_transfer ( std::string infofile,
bool verbose = false )
inline

Read the infofile, read transferfiles listed in that and make splines of T(k,z)

Read an infofile with the format (folder num_redshift) and then each line contains (transferfile_i redshift_i).

The files are sorted by redshift if they are not ordered from low to high.

Parameters
[in]infofileFilename of infofile containing info about transfer data from CAMB

Definition at line 454 of file CAMBReader.h.

454 {
455
456 DVector k;
457 DVector2D transfer_function_cdm;
458 DVector2D transfer_function_baryon;
459 DVector2D transfer_function_photon;
460 DVector2D transfer_function_nu;
461 DVector2D transfer_function_mnu;
462 DVector2D transfer_function_total;
463 DVector2D transfer_function_nonu;
464 DVector2D transfer_function_totde;
465 DVector2D transfer_function_weyl;
466 DVector2D transfer_function_vcdm;
467 DVector2D transfer_function_vb;
468 DVector2D transfer_function_vbvc;
469
470 // Open fileinfo file
471 int nredshift;
472 std::string filepath;
473 std::ifstream fp(infofile.c_str());
474 if (not fp) {
475 throw std::runtime_error("Error read_transfer: cannot read [" + infofile + "]\n");
476 }
477
478 // Read fileprefix and nfiles
479 fp >> filepath;
480 fp >> nredshift;
481 if (FML::ThisTask == 0 and verbose) {
482 std::cout << "Reading transfer functions | Filedir [" << filepath << "] | Reading [" << nredshift
483 << "] redshift files\n";
484 }
485
486 // Read all files
487 DVector redshifts(nredshift);
488 zmin_splines = std::numeric_limits<double>::max();
489 zmax_splines = -std::numeric_limits<double>::max();
490 for (int i = 0; i < nredshift; i++) {
491
492 // Read filename
493 std::string filename;
494 fp >> filename;
495
496 // Read redshift
497 double znow;
498 fp >> znow;
499 redshifts[i] = znow;
500 zmin_splines = std::min(redshifts[i], zmin_splines);
501 zmax_splines = std::max(redshifts[i], zmax_splines);
502
503 // Make filename and open file. Assumes all files have the same length
504 std::string fullfilename = filepath + "/" + filename;
505
506 // Read the transfer data
507 auto data = read_transfer_single(fullfilename);
508
509 // Fetch the data we want
510 auto k_tmp = data[transfer_col_k];
511 if (i == 0) {
512 k = k_tmp;
513 kmin_hmpc_splines = *std::min_element(k_tmp.begin(), k_tmp.end());
514 kmax_hmpc_splines = *std::max_element(k_tmp.begin(), k_tmp.end());
515 }
516
517 if (transfer_col_cdm >= 0)
518 transfer_function_cdm.push_back(data[transfer_col_cdm]);
519 if (transfer_col_baryon >= 0)
520 transfer_function_baryon.push_back(data[transfer_col_baryon]);
521 if (transfer_col_photon >= 0)
522 transfer_function_photon.push_back(data[transfer_col_photon]);
523 if (transfer_col_nu >= 0)
524 transfer_function_nu.push_back(data[transfer_col_nu]);
525 if (transfer_col_mnu >= 0)
526 transfer_function_mnu.push_back(data[transfer_col_mnu]);
527 if (transfer_col_total >= 0)
528 transfer_function_total.push_back(data[transfer_col_total]);
529 if (transfer_col_nonu >= 0)
530 transfer_function_nonu.push_back(data[transfer_col_nonu]);
531 if (transfer_col_totde >= 0)
532 transfer_function_totde.push_back(data[transfer_col_totde]);
533 if (transfer_col_weyl >= 0)
534 transfer_function_weyl.push_back(data[transfer_col_weyl]);
535 if (transfer_col_vcdm >= 0)
536 transfer_function_vcdm.push_back(data[transfer_col_vcdm]);
537 if (transfer_col_vb >= 0)
538 transfer_function_vb.push_back(data[transfer_col_vb]);
539 if (transfer_col_vbvc >= 0)
540 transfer_function_vbvc.push_back(data[transfer_col_vbvc]);
541
542 if (k.size() != k_tmp.size())
543 throw std::runtime_error(
544 "Error in read_transfer: the number of k-values in the files are different");
545
546 // Check that k-array is the same in all files as this is assumed when splining below
547 if (i > 0) {
548 for (size_t j = 0; j < k.size(); j++) {
549 double err = std::fabs(k_tmp[j] - k[j]);
550 if (err > 1e-3)
551 throw std::runtime_error("Error in read_transfer: the k-array differs in the different "
552 "files. Not built-in support for this");
553 }
554 }
555
556 if (FML::ThisTask == 0 and verbose) {
557 std::cout << "Filename: [" << fullfilename << "]\n";
558 std::cout << "z = [" << std::setw(10) << znow << "] | We have [" << std::setw(6) << k.size()
559 << "] k-points\n";
560 }
561 }
562 if (FML::ThisTask == 0)
563 std::cout << "\n";
564
565 // The redshifts must be increasing (the splines need it): sort if the infofile is not ordered
566 std::vector<size_t> order(nredshift);
567 std::iota(order.begin(), order.end(), 0);
568 std::stable_sort(order.begin(), order.end(), [&](size_t i, size_t j) {
569 return redshifts[i] < redshifts[j];
570 });
571 for (int i = 1; i < nredshift; i++)
572 if (redshifts[order[i]] == redshifts[order[i - 1]])
573 throw std::runtime_error("Error in read_transfer: two files with the same redshift " +
574 std::to_string(redshifts[order[i]]) + " in " + infofile);
575 if (not std::is_sorted(redshifts.begin(), redshifts.end())) {
576 if (FML::ThisTask == 0)
577 std::cout << "read_transfer: the files in " << infofile
578 << " are not ordered in redshift: sorting them\n";
579 auto reorder = [&](auto & v) {
580 if (v.empty())
581 return;
582 auto tmp = v;
583 for (int i = 0; i < nredshift; i++)
584 v[i] = tmp[order[i]];
585 };
586 reorder(redshifts);
587 reorder(transfer_function_cdm);
588 reorder(transfer_function_baryon);
589 reorder(transfer_function_photon);
590 reorder(transfer_function_nu);
591 reorder(transfer_function_mnu);
592 reorder(transfer_function_total);
593 reorder(transfer_function_nonu);
594 reorder(transfer_function_totde);
595 reorder(transfer_function_weyl);
596 reorder(transfer_function_vcdm);
597 reorder(transfer_function_vb);
598 reorder(transfer_function_vbvc);
599 }
600
601 // Change to log
602 auto logk = k;
603 for (auto & k : logk)
604 k = std::log(k);
605
606 redshifts_of_files = redshifts;
607
608 // Create splines
609 if (transfer_col_cdm >= 0)
610 cdm_transfer_function_spline.create(redshifts, logk, transfer_function_cdm);
611 if (transfer_col_baryon >= 0)
612 baryon_transfer_function_spline.create(redshifts, logk, transfer_function_baryon);
613 if (transfer_col_photon >= 0)
614 photon_transfer_function_spline.create(redshifts, logk, transfer_function_photon);
615 if (transfer_col_nu >= 0)
616 nu_transfer_function_spline.create(redshifts, logk, transfer_function_nu);
617 if (transfer_col_mnu >= 0)
618 mnu_transfer_function_spline.create(redshifts, logk, transfer_function_mnu);
619 if (transfer_col_total >= 0)
620 total_transfer_function_spline.create(redshifts, logk, transfer_function_total);
621 if (transfer_col_nonu >= 0)
622 nonu_transfer_function_spline.create(redshifts, logk, transfer_function_nonu);
623 if (transfer_col_totde >= 0)
624 totde_transfer_function_spline.create(redshifts, logk, transfer_function_totde);
625 if (transfer_col_weyl >= 0)
626 weyl_transfer_function_spline.create(redshifts, logk, transfer_function_weyl);
627 if (transfer_col_vcdm >= 0)
628 vcdm_transfer_function_spline.create(redshifts, logk, transfer_function_vcdm);
629 if (transfer_col_vb >= 0)
630 vb_transfer_function_spline.create(redshifts, logk, transfer_function_vb);
631 if (transfer_col_vbvc >= 0)
632 vbvc_transfer_function_spline.create(redshifts, logk, transfer_function_vbvc);
633
634 // Test spline
635 if (FML::ThisTask == 0 and verbose) {
636 std::cout << "\nSample values massive neutrino transfer function:\n";
637 for (size_t i = 0; i < transfer_function_mnu.size(); i++) {
638 for (size_t j = 0; j < transfer_function_mnu[i].size(); j++) {
639 if (rand() % 1000 == 0) {
640 std::cout << "z: " << std::setw(10) << redshifts[i] << " k: " << std::setw(15)
641 << std::exp(logk[j]) << " Tnu/Tnu0: " << std::setw(15)
642 << transfer_function_mnu[i][j] / (transfer_function_mnu[0][j] + 1e-20) << "\n";
643 }
644 }
645 }
646 std::cout << "\nSample values CDM transfer function:\n";
647 for (size_t i = 0; i < transfer_function_cdm.size(); i++) {
648 for (size_t j = 0; j < transfer_function_cdm[i].size(); j++) {
649 if (rand() % 1000 == 0) {
650 std::cout << "z: " << std::setw(10) << redshifts[i] << " k: " << std::setw(15)
651 << std::exp(logk[j]) << " Tnu/Tnu0: " << std::setw(15)
652 << transfer_function_cdm[i][j] / (transfer_function_cdm[0][j] + 1e-20) << "\n";
653 }
654 }
655 }
656 }
657 transfer_is_read = true;
658 }
DVector2D read_transfer_single(std::string filename) const
Read a single transfer function.
Definition CAMBReader.h:354
void create(const double *x, const double *y, const double *z, int nx, int ny, std::string splinename="NoName", const gsl_interp2d_type *interpoltype=SPLINE_FIDUCIAL_INTERPOL_TYPE_2D)
Create a spline from pointers to x, y and z. z must have nx * ny elements.
Definition Spline.cpp:504

◆ read_transfer_single()

DVector2D FML::FILEUTILS::LinearTransferData::read_transfer_single ( std::string filename) const
inline

Read a single transfer function.

Read a single transfer file and return the data as a 2D array the first entry is k and the other entries can be found as LinearTransferData::transfer_col_cdm etc.

Parameters
[in]filenameFilename of a CAMB T(k) file

Definition at line 354 of file CAMBReader.h.

354 {
355 FML::assert_mpi(transfer_col_k >= 0, "Fileformat is not correct");
356 FML::assert_mpi(n_transfer_header_lines >= 0, "Fileformat is not correct");
357 FML::assert_mpi(ncol_transfer_file > 0, "Fileformat is not correct");
358
359 // Open CAMB transfer function file for reading
360 std::ifstream fp(filename);
361 if (not fp) {
362 throw std::runtime_error("Error read_transfer_single: cannot open [" + filename + "]\n");
363 }
364
365 // Read the header lines
366 for (int i = 0; i < n_transfer_header_lines; i++) {
367 std::string line;
368 std::getline(fp, line);
369 }
370
371 // Read data row by row
372 DVector2D indata;
374 for (;;) {
375 fp >> tmp[0];
376 if (fp.eof())
377 break;
378 for (int i = 1; i < ncol_transfer_file; i++) {
379 fp >> tmp[i];
380 }
381 indata.push_back(tmp);
382 }
383
384 // Transpose the data
385 DVector2D data(ncol_transfer_file, DVector(indata.size()));
386 for (size_t i = 0; i < indata.size(); i++) {
387 for (size_t j = 0; j < indata[i].size(); j++) {
388 data[j][i] = indata[i][j];
389 }
390 }
391
392 return data;
393 }

◆ set_As()

void FML::FILEUTILS::LinearTransferData::set_As ( double _As)
inline

Definition at line 256 of file CAMBReader.h.

256{ As = _As; }

◆ set_fileformat()

void FML::FILEUTILS::LinearTransferData::set_fileformat ( std::string format)
inline

Set the fileformat.

Definition at line 286 of file CAMBReader.h.

286 {
287 fileformat = format;
288
289 struct FileFormatTransfer tmp;
290 struct FileFormatPower tmp1;
291 if (fileformat == "CAMB") {
293 tmp1 = camb_power_format;
294 } else if (fileformat == "AXIONCAMB") {
296 tmp1 = camb_power_format;
297 } else if (fileformat == "CLASS") {
299 tmp1 = class_power_format;
300 } else {
301 throw std::runtime_error("Fileformat [" + fileformat +
302 "] is unknown. Only CAMB, AXIONCAMB and CLASS implemented. Easy to fix (look "
303 "at the top of CAMBReader.h and add your format there + in set_fileformat)\n");
304 }
305
306 // Set transfer format
307 n_transfer_header_lines = tmp.n_transfer_header_lines;
308 ncol_transfer_file = tmp.ncol_transfer_file;
309 transfer_col_k = tmp.transfer_col_k;
310 transfer_col_cdm = tmp.transfer_col_cdm;
311 transfer_col_baryon = tmp.transfer_col_baryon;
312 transfer_col_photon = tmp.transfer_col_photon;
313 transfer_col_nu = tmp.transfer_col_nu;
314 transfer_col_mnu = tmp.transfer_col_mnu;
315 transfer_col_total = tmp.transfer_col_total;
316 transfer_col_nonu = tmp.transfer_col_nonu;
317 transfer_col_totde = tmp.transfer_col_totde;
318 transfer_col_weyl = tmp.transfer_col_weyl;
319 transfer_col_vcdm = tmp.transfer_col_vcdm;
320 transfer_col_vb = tmp.transfer_col_vb;
321 transfer_col_vbvc = tmp.transfer_col_vbvc;
322
323 // Set power format
324 n_pofk_header_lines = tmp1.n_pofk_header_lines;
325 ncol_pofk_file = tmp1.ncol_pofk_file;
326 pofk_col_k = tmp1.pofk_col_k;
327 pofk_col_pofk = tmp1.pofk_col_pofk;
328 }
FileFormatTransfer camb_transfer_format
The format of transferfiles from CAMB.
Definition CAMBReader.h:51
FileFormatPower camb_power_format
The format of pofkfiles from CAMB.
Definition CAMBReader.h:85
FileFormatPower class_power_format
The format of pofkfiles from CLASS (NB: assumes CLASS run with format=camb)
Definition CAMBReader.h:108
FileFormatTransfer class_transfer_format
The format of transferfiles from CLASS (NB: assumes CLASS run with format=camb)
Definition CAMBReader.h:91
FileFormatTransfer axioncamb_transfer_format
The format of transferfiles from CAMB.
Definition CAMBReader.h:68

◆ set_kpivot_mpc()

void FML::FILEUTILS::LinearTransferData::set_kpivot_mpc ( double _kpivot_mpc)
inline

Definition at line 258 of file CAMBReader.h.

258{ kpivot_mpc = _kpivot_mpc; }

◆ set_ns()

void FML::FILEUTILS::LinearTransferData::set_ns ( double _ns)
inline

Definition at line 257 of file CAMBReader.h.

257{ ns = _ns; }

Field Documentation

◆ n_pofk_header_lines

int FML::FILEUTILS::LinearTransferData::n_pofk_header_lines = 0

Definition at line 180 of file CAMBReader.h.

◆ n_transfer_header_lines

int FML::FILEUTILS::LinearTransferData::n_transfer_header_lines = 0

Definition at line 163 of file CAMBReader.h.

◆ ncol_pofk_file

int FML::FILEUTILS::LinearTransferData::ncol_pofk_file = 0

Definition at line 181 of file CAMBReader.h.

◆ ncol_transfer_file

int FML::FILEUTILS::LinearTransferData::ncol_transfer_file = 0

Definition at line 164 of file CAMBReader.h.

◆ pofk_col_k

int FML::FILEUTILS::LinearTransferData::pofk_col_k = -1

Definition at line 182 of file CAMBReader.h.

◆ pofk_col_pofk

int FML::FILEUTILS::LinearTransferData::pofk_col_pofk = -1

Definition at line 183 of file CAMBReader.h.

◆ transfer_col_baryon

int FML::FILEUTILS::LinearTransferData::transfer_col_baryon = -1

Definition at line 167 of file CAMBReader.h.

◆ transfer_col_cdm

int FML::FILEUTILS::LinearTransferData::transfer_col_cdm = -1

Definition at line 166 of file CAMBReader.h.

◆ transfer_col_k

int FML::FILEUTILS::LinearTransferData::transfer_col_k = -1

Definition at line 165 of file CAMBReader.h.

◆ transfer_col_mnu

int FML::FILEUTILS::LinearTransferData::transfer_col_mnu = -1

Definition at line 170 of file CAMBReader.h.

◆ transfer_col_nonu

int FML::FILEUTILS::LinearTransferData::transfer_col_nonu = -1

Definition at line 172 of file CAMBReader.h.

◆ transfer_col_nu

int FML::FILEUTILS::LinearTransferData::transfer_col_nu = -1

Definition at line 169 of file CAMBReader.h.

◆ transfer_col_photon

int FML::FILEUTILS::LinearTransferData::transfer_col_photon = -1

Definition at line 168 of file CAMBReader.h.

◆ transfer_col_total

int FML::FILEUTILS::LinearTransferData::transfer_col_total = -1

Definition at line 171 of file CAMBReader.h.

◆ transfer_col_totde

int FML::FILEUTILS::LinearTransferData::transfer_col_totde = -1

Definition at line 173 of file CAMBReader.h.

◆ transfer_col_vb

int FML::FILEUTILS::LinearTransferData::transfer_col_vb = -1

Definition at line 176 of file CAMBReader.h.

◆ transfer_col_vbvc

int FML::FILEUTILS::LinearTransferData::transfer_col_vbvc = -1

Definition at line 177 of file CAMBReader.h.

◆ transfer_col_vcdm

int FML::FILEUTILS::LinearTransferData::transfer_col_vcdm = -1

Definition at line 175 of file CAMBReader.h.

◆ transfer_col_weyl

int FML::FILEUTILS::LinearTransferData::transfer_col_weyl = -1

Definition at line 174 of file CAMBReader.h.


The documentation for this class was generated from the following file: