Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_redshifts.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <gsl/gsl_integration.h>
#include <gsl/gsl_spline.h>
#include <gsl/gsl_errno.h>
#include "ccl.h"
#include "ccl_params.h"
#include "ccl_redshifts.h"
Include dependency graph for ccl_redshifts.c:

Go to the source code of this file.

Classes

struct  pz_params
 
struct  norm_params
 

Functions

pz_infoccl_create_photoz_info (void *params, double(*pz_func)(double, double, void *, int *))
 
static double ccl_photoz (double z_ph, void *params)
 
double gaussian_pz (double z_ph, double z_s, void *params, int *status)
 
pz_infoccl_create_gaussian_photoz_info (double sigma_z0)
 
void ccl_free_photoz_info (pz_info *my_photoz_info)
 
dNdz_infoccl_create_dNdz_info (void *params, double(*dNdz_func)(double, void *, int *))
 
double dNdz_smail (double z, void *params, int *status)
 
dNdz_infoccl_create_Smail_dNdz_info (double alpha, double beta, double z0)
 
static double ccl_dNdz (double z, dNdz_info *params, int *status)
 
void ccl_free_dNdz_info (dNdz_info *my_dNdz_info)
 
static double ccl_norm_integrand (double z, void *params)
 
void ccl_dNdz_tomog (double z, double bin_zmin, double bin_zmax, pz_info *photo_info, dNdz_info *dN_info, double *tomoout, int *status)
 

Function Documentation

dNdz_info* ccl_create_dNdz_info ( void *  params,
double(*)(double, void *, int *)  dNdz_func 
)

This function creates a structure amalgamating the information on an analytic true dNdz, plus some parameters.

Parameters
paramsparameters for the analytic dNdz form
dNdz_funcdNdz function
Returns
a structure with the dNdz and parameters

Definition at line 94 of file ccl_redshifts.c.

References halomod_bm::params, and dNdz_info::your_dN_params.

Referenced by main().

95 {
96  dNdz_info * this_info = malloc(sizeof(dNdz_info));
97  this_info ->your_dN_params = params;
98  this_info -> your_dN_func = dNdz_func;
99 
100  return this_info;
101 }
void * your_dN_params
Definition: ccl_redshifts.h:35
dictionary params
Definition: halomod_bm.py:27
pz_info* ccl_create_gaussian_photoz_info ( double  sigma_z0)

This function creates a structure containing the photo-z model for the built-in Gaussian photo-z pdf.

Parameters
sigma_z0The photo-z uncertainty at z=0. The photo-z uncertainty is assumed to scale like (1 + z).
Returns
a structure with the built-in Gaussian P(z) and parameters

Definition at line 67 of file ccl_redshifts.c.

References gaussian_pz(), pz_info::your_pz_func, and pz_info::your_pz_params.

Referenced by main().

67  {
68 
69  // Allocate memory so that this value persists
70  double* sigma_z0_copy = malloc(sizeof(double));
71  *sigma_z0_copy = sigma_z0;
72 
73  // Construct pz_info struct
74  pz_info * this_info = malloc(sizeof(pz_info));
75  this_info->your_pz_params = sigma_z0_copy;
76  this_info->your_pz_func = &gaussian_pz;
77  return this_info;
78 }
void * your_pz_params
Definition: ccl_redshifts.h:24
double(* your_pz_func)(double, double, void *, int *)
Definition: ccl_redshifts.h:22
double gaussian_pz(double z_ph, double z_s, void *params, int *status)
Definition: ccl_redshifts.c:54
pz_info* ccl_create_photoz_info ( void *  params,
double(*)(double, double, void *, int *)  pz_func 
)

This function creates a structure amalgamating the information on the photo-z model, P(z) plus some parameters.

Parameters
paramsparameters for the P(z) function
pz_funcP(z) function
Returns
a structure with the P(z) and parameters

Definition at line 21 of file ccl_redshifts.c.

References halomod_bm::params, and pz_info::your_pz_params.

Referenced by main().

23 {
24  pz_info * this_info = malloc(sizeof(pz_info));
25  this_info ->your_pz_params = params;
26  this_info -> your_pz_func = pz_func;
27 
28  return this_info;
29 }
void * your_pz_params
Definition: ccl_redshifts.h:24
dictionary params
Definition: halomod_bm.py:27
dNdz_info* ccl_create_Smail_dNdz_info ( double  alpha,
double  beta,
double  z0 
)

This function creates a structure containing the true dNdz for the built-in Smail-type analytic form: dNdz ~ z^alpha exp(- (z/z0)^beta)

Parameters
alpha
z0
beta
Returns
a structure with the built-in Smail-type dNdz and parameters

Definition at line 120 of file ccl_redshifts.c.

References smail_params::alpha, beta, smail_params::beta, dNdz_smail(), dNdz_info::your_dN_func, dNdz_info::your_dN_params, and smail_params::z0.

Referenced by main().

120  {
121 
122  // Allocate a smail type parmaeter structure
123  smail_params * smail_par = malloc(sizeof(smail_params));
124  smail_par->alpha = alpha;
125  smail_par->beta = beta;
126  smail_par->z0 = z0;
127 
128  // Construct dNdz_info struct
129  dNdz_info * this_info = malloc(sizeof(dNdz_info));
130  this_info->your_dN_params = smail_par;
131  this_info->your_dN_func = &dNdz_smail;
132  return this_info;
133 }
double dNdz_smail(double z, void *params, int *status)
void * your_dN_params
Definition: ccl_redshifts.h:35
double(* your_dN_func)(double, void *, int *)
Definition: ccl_redshifts.h:33
static double beta[2][28][8]
Definition: ccl_emu17.c:32
static double ccl_dNdz ( double  z,
dNdz_info params,
int *  status 
)
static

Definition at line 142 of file ccl_redshifts.c.

References pz_params::status, dNdz_info::your_dN_func, dNdz_info::your_dN_params, and z.

Referenced by ccl_dNdz_tomog(), and ccl_norm_integrand().

143 {
144 
145  return (params->your_dN_func)(z, params->your_dN_params, status);
146 }
void * your_dN_params
Definition: ccl_redshifts.h:35
double(* your_dN_func)(double, void *, int *)
Definition: ccl_redshifts.h:33
static double z[8]
void ccl_dNdz_tomog ( double  z,
double  bin_zmin,
double  bin_zmax,
pz_info photo_info,
dNdz_info dN_info,
double tomoout,
int *  status 
)

Return dNdz in a particular tomographic bin, convolved with a photo-z model (defined by the user), and normalized.

Parameters
zredshift
dNdz_typethe choice of dN/dz from Chang+
bin_zminthe minimum redshift of the tomorgraphic bin
bin_zmaxthe maximum redshift of the tomographic bin
photo_infothe P(z) info struct
tomooutthe output dN/dz
statusStatus flag. 0 if there are no errors, nonzero otherwise.
Returns
void

Definition at line 222 of file ccl_redshifts.c.

References norm_params::bin_zmax_, norm_params::bin_zmin_, ccl_cosmology_read_config(), ccl_dNdz(), CCL_ERROR_INTEG, CCL_ERROR_MISSING_CONFIG_FILE, ccl_gsl, ccl_norm_integrand(), ccl_photoz(), ccl_raise_exception(), ccl_raise_gsl_warning(), ccl_splines, norm_params::dN_information, ccl_gsl_params::INTEGRATION_DNDZ_EPSREL, ccl_gsl_params::N_ITERATION, pz_params::pz_information, norm_params::pz_information, pz_params::status, norm_params::status, z, Z_MAX_SOURCES, Z_MIN_SOURCES, and pz_params::z_true.

Referenced by main().

224 {
225  // This uses equation 33 of Joachimi & Schneider 2009, arxiv:0905.0393
226  double numerator_integrand=0, denom_integrand=0, dNdz_t;
227  // This struct contains a spec redshift and a pointer to a photoz information struct.
228  struct pz_params pz_p_val; //parameters for the integral over the photoz's
229  struct norm_params norm_p_val;
230 
231  // Check whether ccl_splines and ccl_gsl exist; exit gracefully if they
232  // can't be loaded
233  if(ccl_splines==NULL || ccl_gsl==NULL) ccl_cosmology_read_config();
234  if(ccl_splines==NULL || ccl_gsl==NULL) {
236  "ccl_redshifts.c: Failed to read config file.");
238  return;
239  }
240 
241  // Set up the parameters to pass to the normalising integral (of type struct norm_params
242  norm_p_val.bin_zmin_=bin_zmin;
243  norm_p_val.bin_zmax_=bin_zmax;
244  norm_p_val.pz_information = photo_info;
245  norm_p_val.status = status;
246  norm_p_val.dN_information = dN_info;
247 
248  dNdz_t = ccl_dNdz(z, dN_info, status);
249 
250  // Set up the parameters for the integral over the photo z function in the numerator (of type struct pz_params)
251  pz_p_val.z_true = z;
252  pz_p_val.status = status;
253  pz_p_val.pz_information = photo_info; // pointer to user information
254 
255  // Integrate over the assumed pdf of photo-z wrt true-z in this bin (this goes in the numerator of the result):
256  gsl_integration_cquad_workspace * workspace = gsl_integration_cquad_workspace_alloc(ccl_gsl->N_ITERATION);
257  gsl_function F;
258  F.function = ccl_photoz;
259  F.params = &pz_p_val;
260  int gslstatus = gsl_integration_cquad(&F, bin_zmin, bin_zmax, 0.0,ccl_gsl->INTEGRATION_DNDZ_EPSREL,workspace,&numerator_integrand, NULL, NULL);
261  if(gslstatus != GSL_SUCCESS) {
262  ccl_raise_gsl_warning(gslstatus, "ccl_redshifts.c: ccl_norm_integrand():");
263  *status |= gslstatus;
264  }
265  gsl_integration_cquad_workspace_free(workspace);
266 
267  // Now get the denominator, which normalizes dNdz over the photometric bin
268  workspace = gsl_integration_cquad_workspace_alloc(ccl_gsl->N_ITERATION);
269  F.function = ccl_norm_integrand;
270  F.params = &norm_p_val;
271  gslstatus = gsl_integration_cquad(&F, Z_MIN_SOURCES, Z_MAX_SOURCES, 0.0,ccl_gsl->INTEGRATION_DNDZ_EPSREL,workspace,&denom_integrand, NULL, NULL);
272  if(gslstatus != GSL_SUCCESS) {
273  ccl_raise_gsl_warning(gslstatus, "ccl_redshifts.c: ccl_norm_integrand():");
274  *status |= gslstatus;
275  }
276 
277  gsl_integration_cquad_workspace_free(workspace);
278  if (*status) {
280  return;
281  }
282  *tomoout = dNdz_t * numerator_integrand / denom_integrand;
283 
284 }
static double ccl_dNdz(double z, dNdz_info *params, int *status)
#define CCL_ERROR_INTEG
Definition: ccl_error.h:15
#define Z_MAX_SOURCES
Definition: ccl_redshifts.h:13
void ccl_raise_gsl_warning(int gslstatus, const char *msg,...)
Definition: ccl_error.c:75
#define CCL_ERROR_MISSING_CONFIG_FILE
Definition: ccl_error.h:28
ccl_spline_params * ccl_splines
Definition: ccl_core.c:47
double INTEGRATION_DNDZ_EPSREL
Definition: ccl_params.h:66
ccl_gsl_params * ccl_gsl
Definition: ccl_core.c:48
#define Z_MIN_SOURCES
Definition: ccl_redshifts.h:12
static double ccl_norm_integrand(double z, void *params)
void ccl_cosmology_read_config(void)
Definition: ccl_core.c:50
size_t N_ITERATION
Definition: ccl_params.h:55
static double z[8]
static double ccl_photoz(double z_ph, void *params)
Definition: ccl_redshifts.c:44
void ccl_raise_exception(int err, const char *msg,...)
Definition: ccl_error.c:35
void ccl_free_dNdz_info ( dNdz_info dN_info)

Free memory holding the structure containing dNdz information.

Parameters
dN_infothat holds user-defined dNdz and parameters
Returns
void

Definition at line 152 of file ccl_redshifts.c.

Referenced by main().

153 {
154  free(my_dNdz_info);
155 }
void ccl_free_photoz_info ( pz_info my_photoz_info)

Free memory holding the structure containing user-input photoz information.

Parameters
my_photoz_infothat holds user-defined P(z) and parameters
Returns
void

Definition at line 84 of file ccl_redshifts.c.

Referenced by main().

85 {
86  free(my_photoz_info);
87 }
static double ccl_norm_integrand ( double  z,
void *  params 
)
static

Definition at line 173 of file ccl_redshifts.c.

References norm_params::bin_zmax_, norm_params::bin_zmin_, ccl_cosmology_read_config(), ccl_dNdz(), CCL_ERROR_MISSING_CONFIG_FILE, ccl_gsl, ccl_photoz(), ccl_raise_exception(), ccl_raise_gsl_warning(), ccl_splines, norm_params::dN_information, ccl_gsl_params::INTEGRATION_DNDZ_EPSREL, ccl_gsl_params::N_ITERATION, p, pz_params::pz_information, pz_params::status, norm_params::status, z, and pz_params::z_true.

Referenced by ccl_dNdz_tomog().

174 {
175 
176  // This is a struct that contains a true redshift and a pointer to the information about the photo_z model
177  struct pz_params pz_val_p; // parameters for the photoz pdf wrt true-z
178 
179  double pz_int=0; // pointer to the value of the integral over the photoz model
180  struct norm_params *p = (struct norm_params *) params; // parameters of the current function (because of form required for gsl integration)
181 
182  double z_min = p->bin_zmin_;
183  double z_max = p->bin_zmax_;
184 
185  // Check whether ccl_splines and ccl_gsl exist; exit gracefully if they
186  // can't be loaded
187  if(ccl_splines==NULL || ccl_gsl==NULL) ccl_cosmology_read_config();
188  if(ccl_splines==NULL || ccl_gsl==NULL) {
190  "ccl_redshift.c: Failed to read config file.");
191  return NAN;
192  }
193 
194  // Set up parameters for the pz part of the intermediary integral.
195  pz_val_p.z_true = z;
196  pz_val_p.status = p->status;
197  pz_val_p.pz_information = p-> pz_information;
198 
199  // Do the intermediary integral over the model relating photo-z to true-z
200  gsl_integration_cquad_workspace * workspace = gsl_integration_cquad_workspace_alloc(ccl_gsl->N_ITERATION);
201  gsl_function F;
202  F.function = ccl_photoz;
203  F.params = &pz_val_p;
204  int gslstatus = gsl_integration_cquad(&F, z_min, z_max, 0.0,ccl_gsl->INTEGRATION_DNDZ_EPSREL,workspace,&pz_int, NULL, NULL);
205  if(gslstatus != GSL_SUCCESS) {
206  ccl_raise_gsl_warning(gslstatus, "ccl_redshifts.c: ccl_specs_norm_integrand():");
207  *p->status|= gslstatus;
208  }
209  gsl_integration_cquad_workspace_free(workspace);
210 
211  return ccl_dNdz(z, p->dN_information, p->status) * pz_int ;
212 }
static double ccl_dNdz(double z, dNdz_info *params, int *status)
void ccl_raise_gsl_warning(int gslstatus, const char *msg,...)
Definition: ccl_error.c:75
pz_info * pz_information
#define CCL_ERROR_MISSING_CONFIG_FILE
Definition: ccl_error.h:28
ccl_spline_params * ccl_splines
Definition: ccl_core.c:47
double INTEGRATION_DNDZ_EPSREL
Definition: ccl_params.h:66
ccl_gsl_params * ccl_gsl
Definition: ccl_core.c:48
void ccl_cosmology_read_config(void)
Definition: ccl_core.c:50
size_t N_ITERATION
Definition: ccl_params.h:55
dictionary params
Definition: halomod_bm.py:27
static double z[8]
double bin_zmin_
static int p
Definition: ccl_emu17.c:25
double bin_zmax_
dNdz_info * dN_information
static double ccl_photoz(double z_ph, void *params)
Definition: ccl_redshifts.c:44
void ccl_raise_exception(int err, const char *msg,...)
Definition: ccl_error.c:35
static double ccl_photoz ( double  z_ph,
void *  params 
)
static

Definition at line 44 of file ccl_redshifts.c.

References p, pz_params::pz_information, pz_params::status, pz_info::your_pz_func, pz_info::your_pz_params, and pz_params::z_true.

Referenced by ccl_dNdz_tomog(), and ccl_norm_integrand().

45 {
46  struct pz_params * p = (struct pz_params *) params;
47  pz_info * user_stuff = (pz_info*) p->pz_information;
48  double z_s = p->z_true;
49 
50  return (user_stuff->your_pz_func)(z_ph, z_s, user_stuff->your_pz_params,p->status);
51 }
void * your_pz_params
Definition: ccl_redshifts.h:24
pz_info * pz_information
Definition: ccl_redshifts.c:40
int * status
Definition: ccl_redshifts.c:41
double(* your_pz_func)(double, double, void *, int *)
Definition: ccl_redshifts.h:22
double z_true
Definition: ccl_redshifts.c:39
dictionary params
Definition: halomod_bm.py:27
static int p
Definition: ccl_emu17.c:25
double dNdz_smail ( double  z,
void *  params,
int *  status 
)

Definition at line 107 of file ccl_redshifts.c.

References beta, and pow().

Referenced by ccl_create_Smail_dNdz_info().

107  {
108  double alpha = ((smail_params*) params)->alpha;
109  double beta = ((smail_params*) params)->beta;
110  double z0 = ((smail_params*) params)->z0;
111 
112  return pow(z, alpha) * exp(- pow(z/z0, beta) );
113 }
static double beta[2][28][8]
Definition: ccl_emu17.c:32
dictionary params
Definition: halomod_bm.py:27
static double z[8]
float pow(float base, unsigned long int exp)
Definition: precision.hpp:39
double gaussian_pz ( double  z_ph,
double  z_s,
void *  params,
int *  status 
)

Definition at line 54 of file ccl_redshifts.c.

References M_PI, and sqrt().

Referenced by ccl_create_gaussian_photoz_info().

54  {
55  double sigma_z0 = *((double*) params);
56 
57  double sigma_z = sigma_z0 * (1. + z_s);
58  return exp(- (z_ph - z_s)*(z_ph - z_s) / (2.*sigma_z*sigma_z)) \
59  / (sqrt(2.*M_PI) *sigma_z);
60 }
#define M_PI
Definition: ccl_constants.h:22
Grid< NDIM, T > sqrt(Grid< NDIM, T > lhs)
Definition: grid.h:231
dictionary params
Definition: halomod_bm.py:27