CosmoBolognaLib
Free Software C++/Python libraries for cosmological calculations
Sigma.cpp
Go to the documentation of this file.
1 /********************************************************************
2  * Copyright (C) 2010 by Federico Marulli *
3  * federico.marulli3@unibo.it *
4  * *
5  * This program is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU General Public License as *
7  * published by the Free Software Foundation; either version 2 of *
8  * the License, or (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public *
16  * License along with this program; if not, write to the Free *
17  * Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ********************************************************************/
20 
36 #include "Cosmology.h"
37 
38 using namespace std;
39 
40 using namespace cbl;
41 
42 
43 // =====================================================================================
44 
45 
46 double cbl::cosmology::Cosmology::m_func_sigma (const string method_Pk, const double redshift, const bool store_output, const string output_root, const string interpType, const double kmax, const string input_file, const bool is_parameter_file, function<double(double)> filter, const bool unit1) const
47 {
48  function<double(double)> func;
49 
50  vector<double> kk, Pk;
51 
52  double fact = (m_unit || unit1) ? 1. : m_hh;
53 
54 
55  // the power spectrum is read from file
56 
57  if (input_file!=par::defaultString && !is_parameter_file) {
58 
59  string line;
60  double KK, PK;
61 
62  ifstream fin(input_file.c_str()); checkIO(fin, input_file);
63 
64  while (getline(fin, line)) {
65  if (line.find("#")==string::npos) { // skip comments
66  stringstream ss(line);
67  vector<double> num;
68  ss >> KK >> PK;
69  if (KK<kmax) {
70  kk.emplace_back(KK);
71  Pk.emplace_back(PK);
72  }
73  }
74  }
75 
76  fin.clear(); fin.close();
77 
78  func = glob::FuncGrid(kk, Pk, interpType, cbl::BinType::_logarithmic_);
79  }
80 
81 
82  // alternatively, the power spectrum is computed using either the internal cosmological parameters, or using a parameter file
83 
84  else if (method_Pk=="EisensteinHu" && input_file==par::defaultString) {
85 
86  auto ff = [&] (const double kk)
87  {
88  EisensteinHu eh;
89  eh.TFmdm_set_cosm(m_Omega_matter, m_Omega_baryon, m_Omega_neutrinos, m_massive_neutrinos, m_Omega_DE, m_hh, redshift, m_scalar_amp, m_scalar_pivot, m_n_spec);
90 
91  if (eh.Pk(kk)!=eh.Pk(kk)) ErrorCBL("eh.Pk=nan!", "m_func_sigma", "Sigma.cpp");
92 
93  return eh.Pk(kk);
94  };
95 
96  func = ff;
97  }
98 
99  else if (method_Pk=="CAMB" || method_Pk=="MGCAMB" || method_Pk=="CLASS") {
100 
101  vector<double> lgkk, lgPk;
102  Table_PkCodes(method_Pk, false, lgkk, lgPk, redshift, store_output, output_root, kmax, input_file);
103 
104  for (size_t i=0; i<lgkk.size(); i++) {
105  const double KK = pow(10., lgkk[i]);
106  if (KK<kmax) {
107  kk.emplace_back(KK);
108  Pk.emplace_back(pow(10., lgPk[i]));
109  }
110  }
111 
112  func = glob::FuncGrid(kk, Pk, interpType, cbl::BinType::_linear_);
113  }
114 
115  else if (method_Pk=="EisensteinHu" && input_file!=par::defaultString)
116  ErrorCBL("in the EisensteiHu case, no input files can be read!", "m_func_sigma", "Sigma.cpp");
117 
118  else
119  ErrorCBL("the chosen method_Pk is not available!", "m_func_sigma", "Sigma.cpp");
120 
121  auto ff = [&] (const double kk)
122  {
123  return func(kk/fact)*kk*kk*filter(kk);
124  };
125 
126  // compute the mass variance
127  //return ( 1./(2.*pow(par::pi, 2))*wrapper::gsl::GSL_integrate_qag(ff, 0., 1., 1.e-4)+wrapper::gsl::GSL_integrate_qagiu(ff, 1., 1.e-5) ) * pow(fact, -3.);
128 
129  return 1./(2.*pow(par::pi, 2))*wrapper::gsl::GSL_integrate_qag(ff, 1.e-4, kmax, 1.e-3) * pow(fact, -3.);
130 }
131 
132 
133 
134 // =====================================================================================
135 
136 
137 double cbl::cosmology::Cosmology::m_sigma2R_notNormalised (const double radius, const string method_Pk, const double redshift, const bool store_output, const string output_root, const string interpType, const double kmax, const string input_file, const bool is_parameter_file, const bool unit1) const
138 {
139  auto filter = [&] (const double k)
140  {
141  return pow(TopHat_WF(k*radius),2);
142  };
143 
144  return cosmology::Cosmology::m_func_sigma(method_Pk, redshift, store_output, output_root, interpType, kmax, input_file, is_parameter_file, filter, unit1);
145 
146 }
147 
148 
149 // =====================================================================================
150 
151 
152 double cbl::cosmology::Cosmology::sigma2R (const double radius, const string method_Pk, const double redshift, const bool store_output, const string output_root, const string interpType, const double kmax, const string input_file, const bool is_parameter_file, const bool unit1) const
153 {
154  if (radius<0) ErrorCBL("the radius must be >0!", "sigma2R", "Sigma.cpp");
155 
156  // the normalisation factor
157  double fact = 1.;
158 
159  // if the power spectrum is read from file or m_sigma8<0, then the
160  // mass variance does not need to be normalised (hence fact=1 and
161  // sigma2R=sigma2R_unnormalised); otherwise the normalisation factor
162  // is computed
163  if (input_file==par::defaultString || is_parameter_file) {
164  if (m_sigma8>0)
165  // sigma_8 = sigma(8Mpc/h)
166  fact = pow(m_sigma8, 2)/m_sigma2R_notNormalised(8., method_Pk, 0., store_output, output_root, interpType, kmax, input_file, is_parameter_file, true); // normalization factor
167  }
168 
169  return m_sigma2R_notNormalised(radius, method_Pk, redshift, store_output, output_root, interpType, kmax, input_file, is_parameter_file, unit1)*fact;
170 }
171 
172 
173 // =====================================================================================
174 
175 
176 double cbl::cosmology::Cosmology::dnsigma2R (const int nd, const double radius, const string method_Pk, const double redshift, const bool store_output, const string output_root, const string interpType, const double kmax, const string input_file, const bool is_parameter_file, const bool unit1) const
177 {
178  if (radius<0) ErrorCBL("the radius must be >0!", "dnsigma2R", "Sigma.cpp");
179 
180  // the normalisation factor
181  double fact = 1.;
182 
183  // if the power spectrum is read from file or m_sigma8<0, then the
184  // mass variance does not need to be normalised (hence fact=1 and
185  // sigma2R=sigma2R_unnormalised); otherwise the normalisation factor
186  // is computed
187  if (input_file==par::defaultString || is_parameter_file) {
188  if (m_sigma8>0)
189  // sigma_8 = sigma(8Mpc/h)
190  fact = pow(m_sigma8, 2)/m_sigma2R_notNormalised(8., method_Pk, 0., store_output, output_root, interpType, kmax, input_file, is_parameter_file, true); // normalization factor
191  }
192 
193  if (nd==1) {
194 
195  auto filter = [&] (const double k)
196  {
197  return 2.*cbl::TopHat_WF(k*radius)*cbl::TopHat_WF_D1(k*radius)*k;
198  };
199 
200  return cbl::cosmology::Cosmology::m_func_sigma(method_Pk, redshift, store_output, output_root, interpType, kmax, input_file, is_parameter_file, filter, unit1)*fact;
201 
202  }
203 
204  else
205  return ErrorCBL("", "dnsigma2R", "Sigma.cpp", glob::ExitCode::_workInProgress_);
206 }
207 
208 
209 // =====================================================================================
210 
211 
212 double cbl::cosmology::Cosmology::m_sigma2M_notNormalised (const double mass, const string method_Pk, const double redshift, const bool store_output, const string output_root, const string interpType, const double kmax, const string input_file, const bool is_parameter_file, const bool unit1) const
213 {
214  if (mass<0) ErrorCBL("the mass must be >0!", "m_sigma2M_notNormalised", "Sigma.cpp");
215 
216  const double radius = (input_file!=par::defaultString && !is_parameter_file) ? cbl::Radius(mass, m_RhoZero) : cbl::Radius(mass, rho_m(redshift, unit1));
217 
218  auto filter = [&] (const double k)
219  {
220  return pow(TopHat_WF(k*radius), 2);
221  };
222 
223  return Cosmology::m_func_sigma(method_Pk, redshift, store_output, output_root, interpType, kmax, input_file, is_parameter_file, filter, unit1);
224 }
225 
226 
227 // =====================================================================================
228 
229 
230 double cbl::cosmology::Cosmology::sigma2M (const double mass, const string method_Pk, const double redshift, const bool store_output, const string output_root, const string interpType, const double kmax, const string input_file, const bool is_parameter_file, const bool unit1) const
231 {
232  if (mass<0) ErrorCBL("the mass must be >0!", "sigma2M", "Sigma.cpp");
233 
234  // the normalisation factor
235  double fact = 1.;
236 
237  // if the power spectrum is read from file or m_sigma8<0, then the
238  // mass variance does not need to be normalised (hence fact=1 and
239  // sigma2M=sigma2M_unnormalised); otherwise the normalisation factor
240  // is computed
241  if (input_file==par::defaultString || is_parameter_file) {
242  if (m_sigma8>0)
243  // (sigma8 = sigma(8Mpc/h))
244  fact = pow(m_sigma8, 2)/m_sigma2M_notNormalised(Mass(8., rho_m(0., true)), method_Pk, 0., store_output, output_root, interpType, kmax, input_file, is_parameter_file, true);
245  }
246 
247  return m_sigma2M_notNormalised(mass, method_Pk, redshift, store_output, output_root, interpType, kmax, input_file, is_parameter_file, unit1)*fact;
248 }
249 
250 
251 // =====================================================================================
252 
253 
254 double cbl::cosmology::Cosmology::dnsigma2M (const int nd, const double mass, const string method_Pk, const double redshift, const bool store_output, const string output_root, const string interpType, const double kmax, const string input_file, const bool is_parameter_file, const bool unit1) const
255 {
256  if (mass<0) ErrorCBL("the mass must be >0!", "dnsigma2M", "Sigma.cpp");
257 
258  // the normalisation factor
259  double fact = 1.;
260 
261  // if the power spectrum is read from file or m_sigma8<0, then the
262  // mass variance does not need to be normalised (hence fact=1 and
263  // sigma2M=sigma2M_unnormalised); otherwise the normalisation factor
264  // is computed
265  if (input_file==par::defaultString || is_parameter_file) {
266  if (m_sigma8>0)
267  // (sigma8 = sigma(8Mpc/h))
268  fact = pow(m_sigma8, 2)/m_sigma2M_notNormalised(Mass(8., rho_m(0., true)), method_Pk, 0., store_output, output_root, interpType, kmax, input_file, is_parameter_file, true);
269  }
270 
271  if (nd==1) {
272 
273  //return wrapper::gsl::GSL_derivative(bind(&Cosmology::sigma2M, this, placeholders::_1, method_Pk, redshift, output_root, interpType, kmax, input_file, is_parameter_file, unit1), mass, 1.e11);
274  //return wrapper::gsl::GSL_derivative(bind(&Cosmology::sigma2M, this, placeholders::_1, method_Pk, redshift, output_root, interpType, kmax, input_file, is_parameter_file), mass, mass*1.e-3);
275 
276  const double rho = (input_file!=par::defaultString && !is_parameter_file) ? m_RhoZero : rho_m(redshift, unit1);
277 
278  const double radius = cbl::Radius(mass, rho);
279 
280  const double dRdM = pow(3./(4.*cbl::par::pi*rho), 1./3.)*pow(mass, -2./3.)/3.;
281 
282  auto filter = [&] (const double k)
283  {
284  return 2.*cbl::TopHat_WF(k*radius)*cbl::TopHat_WF_D1(k*radius)*k*dRdM;
285  };
286 
287  return cosmology::Cosmology::m_func_sigma(method_Pk, redshift, store_output, output_root, interpType, kmax, input_file, is_parameter_file, filter, unit1)*fact;
288 
289  }
290  else
291  return ErrorCBL("", "dnsigma2M", "Sigma.cpp", glob::ExitCode::_workInProgress_);
292 }
293 
294 
295 // =====================================================================================
296 
297 
298 std::string cbl::cosmology::Cosmology::create_grid_sigmaM (const string method_SS, const double redshift, const bool store_output,const string output_root, const string interpType, const double k_max, const string input_file, const bool is_parameter_file) const
299 {
300  string norm = (m_sigma8>0) ? "_sigma8"+conv(m_sigma8, par::fDP3) : "_scalar_amp"+conv(m_scalar_amp, par::ee3);
301 
302  cbl::Path path;
303  string dir_grid = path.DirCosmo()+"/Cosmology/Tables/grid_SigmaM/unit"+conv(m_unit,par::fINT)+"/";
304  string MK = "mkdir -p "+dir_grid; if (system (MK.c_str())) {};
305 
306  string file_grid = dir_grid+"grid_"+method_SS+norm+"_h"+conv(m_hh, par::fDP6)+"_OmB"+conv(m_Omega_baryon, par::fDP6)+"_OmCDM"+conv(m_Omega_CDM, par::fDP6)+"_OmL"+conv(m_Omega_DE, par::fDP6)+"_OmN"+conv(m_Omega_neutrinos, par::fDP6)+"_Z"+conv(redshift, par::fDP6)+"_scalar_amp"+conv(m_scalar_amp, par::ee3)+"_scalar_pivot"+conv(m_scalar_pivot, par::fDP6)+"_n"+conv(m_n_spec, par::fDP6)+"_w0"+conv(m_w0, par::fDP6)+"_wa"+conv(m_wa, par::fDP6)+".dat";
307 
308  ifstream fin(file_grid.c_str());
309 
310  if (!fin) {
311 
312  coutCBL << endl << "I'm creating the grid file with sigma(M): " << file_grid.c_str() << "..." << endl;
313 
314  ofstream fout(file_grid.c_str()); checkIO(fout, file_grid);
315 
316  vector<double> MM = logarithmic_bin_vector(1000, 1.e6, 3.e16);
317 
318  double SSS, Sigma, Dln_Sigma;
319 
320  int dp = cout.precision();
321  cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(1);
322 
323  for (size_t k=0; k<MM.size(); k++) {
324  coutCBL << "\r............." << double(k)/double(MM.size())*100. << "% completed \r"; cout.flush();
325 
326  SSS = sigma2M(MM[k], method_SS, redshift, store_output, output_root, interpType, k_max, input_file, is_parameter_file, true);
327  Sigma = sqrt(SSS);
328  Dln_Sigma = dnsigma2M(1, MM[k], method_SS, redshift, store_output, output_root, interpType, k_max, input_file, is_parameter_file, true)*(MM[k]/(2.*SSS));
329  fout << MM[k] << " " << Sigma << " " << Dln_Sigma << endl;
330  }
331 
332  cout.unsetf(ios::fixed); cout.unsetf(ios::showpoint); cout.precision(dp);
333  fout.clear(); fout.close(); coutCBL << endl << "I wrote the file: " << file_grid << endl;
334  }
335 
336  fin.clear(); fin.close();
337 
338  return file_grid;
339 }
The class Cosmology.
#define coutCBL
CBL print message.
Definition: Kernel.h:734
The class Path.
Definition: Path.h:145
std::string DirCosmo()
get the directory where the CosmoBolognaLbi are stored
Definition: Path.h:178
double m_func_sigma(const std::string method_Pk, const double redshift, const bool store_output=true, const std::string output_root="test", const std::string interpType="Linear", const double k_max=100., const std::string input_file=par::defaultString, const bool is_parameter_file=true, std::function< double(double)> filter={}, const bool unit1=false) const
function to compute the not-yet-normalised mass variances and their derivatives
Definition: Sigma.cpp:46
double m_sigma2M_notNormalised(const double mass, const std::string method_Pk, const double redshift, const bool store_output=true, const std::string output_root="test", const std::string interpType="Linear", const double k_max=100., const std::string input_file=par::defaultString, const bool is_parameter_file=true, const bool unit1=false) const
the not-yet-normalised mass variance,
Definition: Sigma.cpp:212
std::string create_grid_sigmaM(const std::string method_SS, const double redshift, const bool store_output=true, const std::string output_root="test", const std::string interpType="Linear", const double k_max=100., const std::string input_file=par::defaultString, const bool is_parameter_file=true) const
auxiliary function to create a grid file with σ(M)
Definition: Sigma.cpp:298
double m_sigma2R_notNormalised(const double radius, const std::string method_Pk, const double redshift, const bool store_output=true, const std::string output_root="test", const std::string interpType="Linear", const double k_max=100., const std::string input_file=par::defaultString, const bool is_parameter_file=true, const bool unit1=false) const
the not-yet-normalised mass variance,
Definition: Sigma.cpp:137
double dnsigma2M(const int nd, const double mass, const std::string method_Pk, const double redshift, const bool store_output=true, const std::string output_root="test", const std::string interpType="Linear", const double k_max=100., const std::string input_file=par::defaultString, const bool is_parameter_file=true, const bool unit1=false) const
the first derivative of the mass variance,
Definition: Sigma.cpp:254
double dnsigma2R(const int nd, const double radius, const std::string method_Pk, const double redshift, const bool store_output=true, const std::string output_root="test", const std::string interpType="Linear", const double k_max=100., const std::string input_file=par::defaultString, const bool is_parameter_file=true, const bool unit1=false) const
the nth-order derivative of the mass variance,
Definition: Sigma.cpp:176
double sigma2M(const double mass, const std::string method_Pk, const double redshift, const bool store_output=true, const std::string output_root="test", const std::string interpType="Linear", const double k_max=100., const std::string input_file=par::defaultString, const bool is_parameter_file=true, const bool unit1=false) const
the mass variance,
Definition: Sigma.cpp:230
double sigma2R(const double radius, const std::string method_Pk, const double redshift, const bool store_output=true, const std::string output_root="test", const std::string interpType="Linear", const double k_max=100., const std::string input_file=par::defaultString, const bool is_parameter_file=true, const bool unit1=false) const
the mass variance,
Definition: Sigma.cpp:152
The class EisensteinHu.
Definition: EisensteinHu.h:54
double Pk(double kk)
get the power spectrum in unit of h^{-3}Mpc^{-3}
Definition: EisensteinHu.h:407
int TFmdm_set_cosm(double omega_matter, double omega_baryon, double omega_hdm, int degen_hdm, double omega_lambda, double hubble, double redshift, double As=2.56e-9, double k_pivot=0.05, double n_spec=0.96)
set the cosmological parameters
Definition: EisensteinHu.h:231
The class FuncGrid.
Definition: FuncGrid.h:55
static const char fDP6[]
conversion symbol for: double -> std::string
Definition: Constants.h:145
static const char ee3[]
conversion symbol for: double -> std::string
Definition: Constants.h:160
static const char fDP3[]
conversion symbol for: double -> std::string
Definition: Constants.h:136
static const char fINT[]
conversion symbol for: int -> std::string
Definition: Constants.h:121
static const std::string defaultString
default std::string value
Definition: Constants.h:336
static const double pi
: the ratio of a circle's circumference to its diameter
Definition: Constants.h:199
double GSL_integrate_qag(gsl_function Func, const double a, const double b, const double rel_err=1.e-3, const double abs_err=0, const int limit_size=1000, const int rule=6)
integral, computed using the GSL qag method
Definition: GSLwrapper.cpp:180
The global namespace of the CosmoBolognaLib
Definition: CAMB.h:38
std::string conv(const T val, const char *fact)
convert a number to a std::string
Definition: Kernel.h:903
T Mass(const T RR, const T Rho)
the mass of a sphere of a given radius and density
Definition: Func.h:1243
T Radius(const T Mass, const T Rho)
the radius of a sphere of a given mass and density
Definition: Func.h:1220
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
void checkIO(const std::ifstream &fin, const std::string file="NULL")
check if an input file can be opened
Definition: Kernel.cpp:160
T TopHat_WF(const T kR)
the top-hat window function
Definition: Func.h:1195
int ErrorCBL(const std::string msg, const std::string functionCBL, const std::string fileCBL, const cbl::glob::ExitCode exitCode=cbl::glob::ExitCode::_error_)
throw an exception: it is used for handling exceptions inside the CosmoBolognaLib
Definition: Kernel.h:780
T TopHat_WF_D1(const T kR)
the derivative of the top-hat window function
Definition: Func.h:1208
double Sigma(const std::vector< double > vect)
the standard deviation of a std::vector
Definition: Func.cpp:925
@ _logarithmic_
logarithmic binning
@ _linear_
linear binning