14 #include <gsl/gsl_deriv.h> 15 #include <gsl/gsl_errno.h> 16 #include <gsl/gsl_fit.h> 17 #include <gsl/gsl_integration.h> 18 #include <gsl/gsl_odeiv2.h> 19 #include <gsl/gsl_spline.h> 53 Integr_obj(
double(*f) (
double,
void*),
const double a,
const double b,
54 const double epsabs,
const double epsrel,
const size_t limit):
55 a(a), b(b), L(b-a), epsabs(epsabs), epsrel(epsrel), limit(limit)
57 w = gsl_integration_workspace_alloc(limit);
63 gsl_integration_workspace_free (
w);
66 void set_a(
double a_new) { a = a_new; L = a -b; }
67 void set_b(
double b_new) { b = b_new; L = a -b; }
76 gsl_integration_workspace*
w;
91 const double epsabs,
const double epsrel,
size_t limit,
int key):
92 Integr_obj(f, a, b, epsabs, epsrel, limit), key(key) {}
98 gsl_errno = gsl_integration_qag(&F, a, b, epsabs, epsrel, limit, key,
w, &result, &error);
99 if (gsl_errno)
throw std::runtime_error(
"GSL integration error: " + std::string(gsl_strerror(gsl_errno)));
105 return this->operator()(0, params);
123 const double epsabs,
const double epsrel,
size_t limit):
130 gsl_errno = gsl_integration_qagiu(&F, a, epsabs, epsrel, limit,
w, &result, &error);
131 if (gsl_errno)
throw std::runtime_error(
"GSL integration error: " + std::string(gsl_strerror(gsl_errno)));
137 return this->operator()(0, params);
153 const double epsabs,
const double epsrel,
size_t limit,
size_t n):
156 wf = gsl_integration_qawo_table_alloc(1, 1, GSL_INTEG_SINE, n);
161 gsl_integration_qawo_table_free(wf);
166 gsl_integration_qawo_table_set(wf, r, L, GSL_INTEG_SINE);
168 gsl_errno = gsl_integration_qawo(&F, a, epsabs, epsrel, limit,
w, wf, &result, &error);
169 if (gsl_errno)
throw std::runtime_error(
"GSL integration error: " + std::string(gsl_strerror(gsl_errno)));
173 gsl_integration_qawo_table*
wf;
186 const double epsabs,
size_t limit,
size_t n):
189 wc = gsl_integration_workspace_alloc (limit);
194 gsl_integration_workspace_free (wc);
199 gsl_integration_qawo_table_set(wf, r, L, GSL_INTEG_SINE);
201 gsl_errno = gsl_integration_qawf(&F, a, epsabs, limit,
w, wc, wf, &result, &error);
202 if (gsl_errno)
throw std::runtime_error(
"GSL integration error: " + std::string(gsl_strerror(gsl_errno)));
206 gsl_integration_workspace*
wc;
217 ODE_Solver(
int (*
function) (
double t,
const double y[],
double dydt[],
void *
params),
size_t dim,
void* params,
218 const double hstart = 1e-6,
const double epsabs = 1e-6,
const double epsrel = 0):
219 sys({
function, NULL, dim, params}),
220 d(gsl_odeiv2_driver_alloc_y_new(&sys, gsl_odeiv2_step_rk8pd, hstart, epsabs, epsrel)) {}
224 gsl_odeiv2_driver_free (d);
227 void update(
double &t,
const double t1,
double y[])
229 status = gsl_odeiv2_driver_apply (d, &t, t1, y);
230 if (status)
throw std::runtime_error(
"GSL ODE error: " + std::string(gsl_strerror(status)));
235 gsl_odeiv2_driver*
d;
246 return pow(a*
hubble_param(a, *static_cast<const Cosmo_Param*>(params)), -3);
251 return growth_factor(a, *static_cast<const Cosmo_Param*>(params));
262 return lower_bound(vec.begin(), vec.end(), val) - vec.begin();
276 const double r = my_par->
r;
277 const P& P_k = my_par->
P_k;
278 return 1/(2*
PI*
PI)*k/r*P_k(k);
292 const double kr = k*my_par->
r;
293 double j0 = kr < 1e-6 ? 1 - kr*kr/6. : sin(kr) / (kr);
294 const P& P_k = my_par->
P_k;
295 return 1/(2*
PI*
PI)*k*k*j0*P_k(k);
309 const double kr = k*my_par->
r;
310 const double w = kr < 0.1 ?
311 1.-0.1*kr*kr+0.003571429*kr*kr*kr*kr
312 -6.61376E-5*kr*kr*kr*kr*kr*kr
313 +7.51563E-7*kr*kr*kr*kr*kr*kr*kr*kr
315 3.*(sin(kr) - kr*cos(kr))/(kr*kr*kr);
317 const P& P_k = my_par->
P_k;
318 return 1/(2*
PI*
PI)*k*k*w*w*P_k(k);
334 template <
class P,
class T>
337 const size_t req_size = (size_t)ceil((x_max - x_min)/lin_bin);
339 fce_binned.
resize(req_size);
342 for(
size_t i = 0; i < req_size; i++){
343 double r = x_min + i*lin_bin;
345 fce_binned[0][i] = r;
346 fce_binned[1][i] = fce_r(r, &my_param);
361 template <
class P,
class T>
381 template <
class P,
class T>
402 BOOST_LOG_TRIVIAL(debug) <<
"Initializing CCL power spectrum...";
411 return D(static_cast<void*>(cosmo));
422 if (!status)
return D_ccl;
437 if (!status)
return f;
442 status = gsl_deriv_central(&F, log(a), 1e-12, &f, &error);
443 if (!status)
return f;
444 else throw std::runtime_error(
"GSL ODE error: " + std::string(gsl_strerror(status)));
456 int status = gsl_deriv_forward(&F, a, 1e-12, &dDda, &error);
457 if (!status)
return dDda;
458 else throw std::runtime_error(
"GSL ODE error: " + std::string(gsl_strerror(status)));
467 if(!status)
return OL;
478 if (!status)
return pk;
482 if (!status)
return D*D*pk;
491 if (!status)
return pk;
495 template <
typename T,
size_t N>
499 acc = gsl_interp_accel_alloc ();
500 spline = gsl_spline_alloc (gsl_interp_steffen, data.
size());
502 std::vector<double> tmp_x(data[0].
begin(), data[0].
end());
503 std::vector<double> tmp_y(data[1].
begin(), data[1].
end());
505 gsl_spline_init (spline, tmp_x.data(), tmp_y.data(), data.
size());
507 x_min = data[0].front();
508 x_max = data[0].back();
515 gsl_spline_free(spline);
516 gsl_interp_accel_free (acc);
522 template <
typename T,
size_t N>
524 const size_t m_u,
const size_t n_u):
529 BOOST_LOG_TRIVIAL(debug) <<
"Fitting amplitude of P(k) in lower range.";
530 k_min = data[0][m_l];
534 BOOST_LOG_TRIVIAL(debug) <<
"Fitting amplitude of P(k) in upper range.";
535 k_max = data[0][n_u-1];
539 template <
typename T,
size_t N>
543 m_l, m_l + sim.out_opt.bins_per_decade/2,
544 n_u - sim.out_opt.bins_per_decade/2, n_u
547 template <
typename T,
size_t N>
551 0,
get_nearest(sim.other_par.nyquist.at(
"particle")/2, data[0]) + 1
556 return exp(-k*k/k2_G);
559 template <
typename T,
size_t N>
568 std::vector<double> Pk_res,
w;
573 std::vector<double> A_vec(n-m, 1);
578 for(
size_t i = m; i < n; i++){
579 FTYPE_t k = data[0][i];
581 Pk_res.push_back(data[1][i] / Pk);
582 if (N == 3) w.push_back(
pow2(Pk/data[2][i]));
587 for(
size_t i = m; i < n; i++){
589 Pk_res.push_back(data[1][i] / Pk);
590 if (N == 3) w.push_back(
pow2(Pk/data[2][i]));
595 double A_sigma2, sumsq;
598 if (N == 3) gsl_errno = gsl_fit_wmul(A_vec.data(), 1, w.data(), 1, Pk_res.data(), 1, n-
m, &A, &A_sigma2, &sumsq);
599 else gsl_errno = gsl_fit_mul(A_vec.data(), 1, Pk_res.data(), 1, n-
m, &A, &A_sigma2, &sumsq);
600 if (gsl_errno)
throw std::runtime_error(
"GSL integration error: " + std::string(gsl_strerror(gsl_errno)));
602 BOOST_LOG_TRIVIAL(debug) <<
"\t[" << (N == 3 ?
"weighted-" :
"") <<
"fit A = " << A <<
", err = " << 100*
sqrt(A_sigma2)/A <<
"%%]";
605 template <
typename T,
size_t N>
612 std::vector<double> k, Pk,
w;
618 for(
size_t i = m; i < n; i++){
619 k.push_back(log(data[0][i]));
620 Pk.push_back(log(data[1][i]));
621 if (N == 3)w.push_back(
pow2(data[1][i]/data[2][i]));
623 double cov00, cov01, cov11, sumsq;
625 if (N == 3) gsl_errno = gsl_fit_wlinear(k.data(), 1, w.data(), 1, Pk.data(), 1, n-
m, &A, &
n_s, &cov00, &cov01, &cov11, &sumsq);
626 else gsl_errno = gsl_fit_linear(k.data(), 1, Pk.data(), 1, n-
m, &A, &
n_s, &cov00, &cov01, &cov11, &sumsq);
627 if (gsl_errno)
throw std::runtime_error(
"GSL integration error: " + std::string(gsl_strerror(gsl_errno)));
630 BOOST_LOG_TRIVIAL(debug) <<
"\t[" << (N == 3 ?
"weighted-" :
"") <<
"fit A = " << A <<
", err = " << 100*
sqrt(cov00) <<
"%%" 631 <<
"n_s = " << n_s <<
", err = " << 100*
sqrt(cov11)/std::abs(n_s) <<
"%%, corr = " << 100*cov01/
sqrt(cov00*cov11) <<
"%%]";
634 template <
typename T,
size_t N>
645 template <
typename T,
size_t N>
647 Extrap_Pk<
T, N>(data, sim), A_nl(A_nl), a_eff(a_eff), k_split(this->
k_max) {}
649 template <
typename T,
size_t N>
658 const FTYPE_t k_ref = 0.1;
659 double ratio = P_k(k_ref) /
lin_pow_spec(1, k_ref, cosmo);
660 BOOST_LOG_TRIVIAL(debug) <<
"\tSet absolute error to " <<
GSL_EPSABS*ratio;
667 BOOST_LOG_TRIVIAL(debug) <<
"Computing correlation function via GSL integration QAWF...";
669 Integr_obj_qawf xi_r(&xi_integrand_W<P>, 0, gsp_epsabs,
GSL_LIMIT,
GSL_N);
688 BOOST_LOG_TRIVIAL(debug) <<
"Computing mass fluctuations via GSL integration QAWF...";
690 Integr_obj_qagiu sigma_r(&sigma_integrand_G<P>, 0, gsp_epsabs,
GSL_LIMIT,
GSL_N);
double operator()(double r, void *params)
void gen_sigma_func_binned_gsl(const Sim_Param &sim, const P &P_k, Data_Vec< double, 2 > &sigma_binned, T &sigma_r)
compute amplitude of density fluctuation and store results
void gen_corr_func_binned_gsl_qawf_lin(const Sim_Param &sim, double a, Data_Vec< double, 2 > &corr_func_binned)
compute linear correlation function and store results
Integr_obj_qagiu(double(*f)(double, void *), const double a, const double epsabs, const double epsrel, size_t limit)
integrand_param(double r, const P &P_k)
double lin_pow_spec(double a, double k, const Cosmo_Param &cosmo)
double growth_factor(double a, const Cosmo_Param &cosmo)
double operator()(void *params)
void norm_pwr(Cosmo_Param &cosmo)
< end of anonymous namespace (private definitions)
handle cosmological functions like power spectrum, growth, etc.
double ccl_omega_x(ccl_cosmology *cosmo, double a, ccl_species_x_label label, int *status)
Integr_obj_qawf(double(*f)(double, void *), const double a, const double epsabs, size_t limit, size_t n)
double ccl_nonlin_matter_power(ccl_cosmology *cosmo, double k, double a, int *status)
void gen_fce_r_binned_gsl(const double x_min, const double x_max, const double lin_bin, const P &P_k, Data_Vec< double, 2 > &fce_binned, T &fce_r)
compute given function in linear range and store results
: class storing simulation parameters
size_t size() const noexcept
double ccl_growth_factor(ccl_cosmology *cosmo, double a, int *status)
T hubble_param(T a, const Cosmo_Param &cosmo)
void gen_corr_func_binned_gsl_qawf(const Sim_Param &sim, const P &P_k, Data_Vec< double, 2 > &corr_func_binned)
compute correlation function and store results
gsl_integration_qawo_table * wf
double ccl_growth_rate(ccl_cosmology *cosmo, double a, int *status)
define container Data_Vec
double non_lin_pow_spec(double a, double k, const Cosmo_Param &cosmo)
constexpr double GSL_EPSABS
absolute error at z = 0, scales for higher redshifts
double xi_integrand_W(double k, void *params)
integrand for correlation function when weight-function 'sin(kr)' is used in integration ...
basic integration object, wrapper for GSL integration functions
double sigma_integrand_G(double k, void *params)
integrand for amplitude of mass fluctuaton when non-weighted integration is used
QAWO adaptive integration for oscillatory functions.
gsl_integration_workspace * wc
void gen_sigma_func_binned_gsl_qawf_nl(const Sim_Param &sim, double a, Data_Vec< double, 2 > &sigma_binned)
compute non-linear amplitude of density fluctuation and store results
QAG adaptive integration.
void update(double &t, const double t1, double y[])
double ln_growth_factor(double log_a, void *parameters)
constexpr size_t GSL_LIMIT
max. number of subintervals for adaptive integration
QAWF adaptive integration for Fourier integrals.
void init(const Data_Vec< T, N > &data)
declaration in params.hpp
cosmological & CCL parameters
general integrand with callable function and one parameter
Grid< NDIM, T > sqrt(Grid< NDIM, T > lhs)
static double get_gsl_error(const Cosmo_Param &cosmo, const P &P_k)
ODE_Solver(int(*function)(double t, const double y[], double dydt[], void *params), size_t dim, void *params, const double hstart=1e-6, const double epsabs=1e-6, const double epsrel=0)
static CCL_BEGIN_DECLS double x[111][8]
double operator()(double r, void *params)
size_t get_nearest(const T val, const std::vector< T > &vec)
double Omega_lambda(double a, const Cosmo_Param &cosmo)
double ccl_linear_matter_power(ccl_cosmology *cosmo, double k, double a, int *status)
various simulation parameters
void gen_sigma_func_binned_gsl_qawf_lin(const Sim_Param &sim, double a, Data_Vec< double, 2 > &sigma_binned)
compute linear amplitude of density fluctuation and store results
Integr_obj(double(*f)(double, void *), const double a, const double b, const double epsabs, const double epsrel, const size_t limit)
Integr_obj_qag(double(*f)(double, void *), const double a, const double b, const double epsabs, const double epsrel, size_t limit, int key)
void gen_corr_func_binned_gsl(const Sim_Param &sim, const P &P_k, Data_Vec< double, 2 > &corr_func_binned, T &xi_r)
compute correlation function and store results
void gen_corr_func_binned_gsl_qawf_nl(const Sim_Param &sim, double a, Data_Vec< double, 2 > &corr_func_binned)
compute non-linear correlation function and store results
double operator()(double x) const
Integr_obj_qawo(double(*f)(double, void *), const double a, const double b, const double epsabs, const double epsrel, size_t limit, size_t n)
constexpr size_t GSL_N
max. number of bisections of integration interval (QAWO / QAWF)
double ccl_sigma8(ccl_cosmology *cosmo, int *status)
float pow(float base, unsigned long int exp)
double operator()(void *params)
double growth_rate(double a, const Cosmo_Param &cosmo)
QAGI adaptive integration on infinite intervals.
double operator()(double r, void *params)
gsl_integration_workspace * w
double operator()(double r, void *params)
double xi_integrand_G(double k, void *params)
integrand for correlation function when non-weighted integration is used
: Explicit embedded Runge-Kutta Prince-Dormand (8, 9) method.
double growth_factor_integrand(double a, void *params)
void gen_sigma_binned_gsl_qawf(const Sim_Param &sim, const P &P_k, Data_Vec< double, 2 > &sigma_binned)
compute amplitude of density fluctuation and store results
static double w[2][28][111]
static double truncation_fce(double k, double k2_G)
double norm_growth_factor(const Cosmo_Param &cosmo)
when computing growth factor outside CCL range we need to normalize the growth factor; ...
double growth_change(double a, const Cosmo_Param &cosmo)