Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_sample_run.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <math.h>
4 
5 #include <ccl.h>
6 #include <ccl_redshifts.h>
7 
8 #define OC 0.25
9 #define OB 0.05
10 #define OK 0.00
11 #define ON 0.00
12 #define HH 0.70
13 #define W0 -1.0
14 #define WA 0.00
15 #define NS 0.96
16 #define NORMPS 0.80
17 #define ZD 0.5
18 #define NZ 128
19 #define Z0_GC 0.50
20 #define SZ_GC 0.05
21 #define Z0_SH 0.65
22 #define SZ_SH 0.05
23 #define NL 513
24 #define PS 0.1
25 #define NEFF 3.046
26 
27 
28 
29 // The user defines a structure of parameters to the user-defined function for the photo-z probability
31 {
33 };
34 
35 // Define the function we want to use for sigma_z which is included in the above struct.
36 double sigmaz_sources(double z)
37 {
38  return 0.05*(1.0+z);
39 }
40 
41 // The user defines a function of the form double function ( z_ph, z_spec, void * user_pz_params) where user_pz_params is a pointer to the parameters of the user-defined function. This returns the probabilty of obtaining a given photo-z given a particular spec_z.
42 double user_pz_probability(double z_ph, double z_s, void * user_par, int * status)
43 {
44  double sigma_z = ((struct user_func_params *) user_par)->sigma_z(z_s);
45  return exp(- (z_ph-z_s)*(z_ph-z_s) / (2.*sigma_z*sigma_z)) / (pow(2.*M_PI,0.5)*sigma_z);
46 }
47 
48 // The user defines a structure of parameters to the user-defined function for the true dNdz
49 struct user_dN_params {
50  double alpha;
51  double beta;
52  double z0;
53 };
54 
55 // The user defines a function of the form double function ( z, void * params, int *status) where params is a pointer to the parameters of the user-defined function. This returns the true dNdz.
56 
57 double user_dNdz(double z, void * user_par, int *status)
58 {
59  struct user_dN_params * p = (struct user_dN_params *) user_par;
60 
61  return pow(z, p->alpha) * exp(- pow(z/(p->z0), p->beta) );
62 
63 }
64 
65 int main(int argc,char **argv)
66 {
67  //status flag
68  int status =0;
69 
70  // Set neutrino masses
71  double* MNU;
72  double mnuval = 0.;
73  MNU = &mnuval;
75 
76  //whether comoving or physical
77  int isco=0;
78 
79  // Initialize cosmological parameters
82  ccl_parameters params = ccl_parameters_create(OC, OB, OK, NEFF, MNU, MNUTYPE, W0, WA, HH, NORMPS, NS,-1,-1,-1,-1,NULL,NULL, &status);
83  //printf("in sample run w0=%1.12f, wa=%1.12f\n", W0, WA);
84 
85  // Initialize cosmology object given cosmo params
87 
88  // Compute radial distances (see include/ccl_background.h for more routines)
89  printf("Comoving distance to z = %.3lf is chi = %.3lf Mpc\n",
90  ZD,ccl_comoving_radial_distance(cosmo,1./(1+ZD), &status));
91  printf("Luminosity distance to z = %.3lf is chi = %.3lf Mpc\n",
92  ZD,ccl_luminosity_distance(cosmo,1./(1+ZD), &status));
93  printf("Distance modulus to z = %.3lf is mu = %.3lf Mpc\n",
94  ZD,ccl_distance_modulus(cosmo,1./(1+ZD), &status));
95 
96 
97  //Consistency check
98  printf("Scale factor is a=%.3lf \n",1./(1+ZD));
99  printf("Consistency: Scale factor at chi=%.3lf Mpc is a=%.3lf\n",
100  ccl_comoving_radial_distance(cosmo,1./(1+ZD), &status),
101  ccl_scale_factor_of_chi(cosmo,ccl_comoving_radial_distance(cosmo,1./(1+ZD), &status), &status));
102 
103  // Compute growth factor and growth rate (see include/ccl_background.h for more routines)
104  printf("Growth factor and growth rate at z = %.3lf are D = %.3lf and f = %.3lf\n",
105  ZD, ccl_growth_factor(cosmo,1./(1+ZD), &status),ccl_growth_rate(cosmo,1./(1+ZD), &status));
106 
107  // Compute Omega_m, Omega_L, Omega_r, rho_crit, rho_m at different times
108  printf("z\tOmega_m\tOmega_L\tOmega_r\trho_crit\trho_m\tRHO_CRITICAL\n");
109  double Om, OL, Or, rhoc, rhom;
110  for (int z=10000;z!=0;z/=3){
111  Om = ccl_omega_x(cosmo, 1./(z+1), ccl_species_m_label, &status);
112  OL = ccl_omega_x(cosmo, 1./(z+1), ccl_species_l_label, &status);
113  Or = ccl_omega_x(cosmo, 1./(z+1), ccl_species_g_label, &status);
114  rhoc = ccl_rho_x(cosmo, 1./(z+1), ccl_species_crit_label, isco, &status);
115  rhom = ccl_rho_x(cosmo, 1./(z+1), ccl_species_m_label, isco, &status);
116  printf("%i\t%.3f\t%.3f\t%.3f\t%.3e\t%.3e\t%.3e\n", z, Om, OL, Or, rhoc, rhom, RHO_CRITICAL);
117  }
118  Om = ccl_omega_x(cosmo, 1., ccl_species_m_label, &status);
119  OL = ccl_omega_x(cosmo, 1., ccl_species_l_label, &status);
120  Or = ccl_omega_x(cosmo, 1., ccl_species_g_label, &status);
121  rhoc = ccl_rho_x(cosmo, 1., ccl_species_crit_label, isco, &status);
122  rhom = ccl_rho_x(cosmo, 1., ccl_species_m_label, isco, &status);
123  printf("%i\t%.3f\t%.3f\t%.3f\t%.3e\t%.3e\t%.3e\n", 0, Om, OL, Or, rhoc, rhom, RHO_CRITICAL);
124 
125  // Compute sigma8
126  printf("Initializing power spectrum...\n");
127  printf("sigma8 = %.3lf\n\n", ccl_sigma8(cosmo, &status));
128 
129  //Create tracers for angular power spectra
130  double z_arr_gc[NZ],z_arr_sh[NZ],nz_arr_gc[NZ],nz_arr_sh[NZ],bz_arr[NZ];
131  for(int i=0;i<NZ;i++) {
132  z_arr_gc[i]=Z0_GC-5*SZ_GC+10*SZ_GC*(i+0.5)/NZ;
133  nz_arr_gc[i]=exp(-0.5*pow((z_arr_gc[i]-Z0_GC)/SZ_GC,2));
134  bz_arr[i]=1+z_arr_gc[i];
135  z_arr_sh[i]=Z0_SH-5*SZ_SH+10*SZ_SH*(i+0.5)/NZ;
136  nz_arr_sh[i]=exp(-0.5*pow((z_arr_sh[i]-Z0_SH)/SZ_SH,2));
137  }
138  double *a_arr_resample=(double *)malloc(2*NZ*sizeof(double));
139  double *nz_resampled=(double *)malloc(2*NZ*sizeof(double));
140  for(int i=0;i<2*NZ;i++) {
141  double z=(i+0.5)/NZ;
142  a_arr_resample[i]=1./(1+z);
143  }
144 
145  //CMB lensing tracer
146  CCL_ClTracer *ct_cl=ccl_cl_tracer_cmblens(cosmo,1100.,&status);
147 
148  //Galaxy clustering tracer
149  CCL_ClTracer *ct_gc=ccl_cl_tracer_number_counts_simple(cosmo,NZ,z_arr_gc,nz_arr_gc,NZ,z_arr_gc,bz_arr, &status);
150 
151  //Cosmic shear tracer
152  CCL_ClTracer *ct_wl=ccl_cl_tracer_lensing_simple(cosmo,NZ,z_arr_sh,nz_arr_sh, &status);
153  printf("ell C_ell(c,c) C_ell(c,g) C_ell(c,s) C_ell(g,g) C_ell(g,s) C_ell(s,s) | r(g,s)\n");
154 
155  //This function allows you to retrieve some of the tracer's internal functions of redshift
156  ccl_get_tracer_fas(cosmo,ct_gc,2*NZ,a_arr_resample,nz_resampled,CCL_CLT_NZ,&status);
157 
158  int ells[NL];
159  double cells_cc_limber[NL];
160  double cells_cg_limber[NL];
161  double cells_cl_limber[NL];
162  double cells_ll_limber[NL];
163  double cells_gl_limber[NL];
164  double cells_gg_limber[NL];
165  for(int ii=0;ii<NL;ii++)
166  ells[ii]=ii;
167 
168  double linstep = 40;
169  double logstep = 1.15;
170  double dchi = (ct_gc->chimax-ct_gc->chimin)/1000.; // must be below 3 to converge toward limber computation at high ell
171  double dlk = 0.003;
172  double zmin = 0.05;
173  CCL_ClWorkspace *w=ccl_cl_workspace_default(NL+1,-1,CCL_NONLIMBER_METHOD_NATIVE,logstep,linstep,dchi,dlk,zmin,&status);
174  ccl_angular_cls(cosmo,w,ct_cl,ct_cl,NL,ells,cells_cc_limber,&status);
175  ccl_angular_cls(cosmo,w,ct_cl,ct_gc,NL,ells,cells_cg_limber,&status);
176  ccl_angular_cls(cosmo,w,ct_cl,ct_wl,NL,ells,cells_cl_limber,&status);
177  ccl_angular_cls(cosmo,w,ct_gc,ct_gc,NL,ells,cells_gg_limber,&status);
178  ccl_angular_cls(cosmo,w,ct_gc,ct_wl,NL,ells,cells_gl_limber,&status);
179  ccl_angular_cls(cosmo,w,ct_wl,ct_wl,NL,ells,cells_ll_limber,&status);
180 
181 
182  for(int l=2;l<=NL;l*=2)
183  printf("%d %.3lE %.3lE %.3lE %.3lE %.3lE %.3lE | %.3lE\n",l,cells_cc_limber[l],cells_cg_limber[l],cells_cl_limber[l],cells_gg_limber[l],cells_gl_limber[l],cells_ll_limber[l],cells_gl_limber[l]/sqrt(cells_gg_limber[l]*cells_ll_limber[l]));
184  printf("\n");
185 
186  //Free up tracers
187  ccl_cl_tracer_free(ct_cl);
188  ccl_cl_tracer_free(ct_gc);
189  ccl_cl_tracer_free(ct_wl);
190 
191  // Free arrays
192  free(a_arr_resample);
193  free(nz_resampled);
194 
195  //Halo mass function
196  printf("M\tdN/dlog10M(z = 0, 0.5, 1))\n");
197  for(int logM=9;logM<=15;logM+=1) {
198  printf("%.1e\t",pow(10,logM));
199  for(double z=0; z<=1; z+=0.5) {
200  printf("%e\t", ccl_massfunc(cosmo, pow(10,logM),1.0/(1.0+z), 200., &status));
201  }
202  printf("\n");
203  }
204  printf("\n");
205 
206  //Halo bias
207  printf("Halo bias: z, M, b1(M,z)\n");
208  for(int logM=9;logM<=15;logM+=1) {
209  for(double z=0; z<=1; z+=0.5) {
210  printf("%.1e %.1e %.2e\n",1.0/(1.0+z),pow(10,logM),ccl_halo_bias(cosmo,pow(10,logM),1.0/(1.0+z), 200., &status));
211  }
212  }
213  printf("\n");
214 
215  // If you don't have an external N(z) you are loading, CCL also has functionality
216  // to produce this for you from an analytic form.
217 
218  // The user declares and sets an instance of parameters to their photo_z and dNdz function:
219  struct user_func_params my_params_example;
220  my_params_example.sigma_z = sigmaz_sources;
221  struct user_dN_params my_dN_params_example;
222  my_dN_params_example.alpha = 1.24;
223  my_dN_params_example.beta = 1.01;
224  my_dN_params_example.z0 = 0.51;
225 
226  // Declare a variable of the type of to hold the struct to be created.
227  pz_info * pz_info_example;
228 
229  // Create the struct to hold the user information about photo_z's.
230  pz_info_example = ccl_create_photoz_info(&my_params_example, &user_pz_probability);
231 
232  // Alternatively, we could have used the built-in Gaussian photo-z pdf,
233  // which assumes sigma_z = sigma_z0 * (1 + z) (not used in what follows).
234  double sigma_z0 = 0.05;
235  pz_info *pz_info_gaussian;
236  pz_info_gaussian = ccl_create_gaussian_photoz_info(sigma_z0);
237 
238  // Declare a variable of the type of dN_info to hold the struct to be created
239  dNdz_info * dN_info_example;
240 
241  // Create a simple analytic true redshift distribution:
242  dN_info_example = ccl_create_dNdz_info(&my_dN_params_example, &user_dNdz);
243 
244  // Alternatively, we could have used the built-in Smail-type dNdz
245  // (not used in what follows
246  dNdz_info *dN_info_gaussian;
247  double alpha = 1.24;
248  double beta = 1.01;
249  double z0 = 0.51;
250  dN_info_gaussian = ccl_create_Smail_dNdz_info(alpha, beta, z0);
251 
252  double z_test;
253  double dNdz_tomo;
254  int z;
255  FILE * output;
256 
257  //Try splitting dNdz (lensing) into 5 redshift bins
258  double tmp1,tmp2,tmp3,tmp4,tmp5;
259  printf("Trying splitting dNdz (lensing) into 5 redshift bins. "
260  "Output written into file tests/example_tomographic_bins.out\n");
261  output = fopen("./tests/example_tomographic_bins.out", "w");
262 
263  if(!output) {
264  fprintf(stderr, "Could not write to 'tests' subdirectory"
265  " - please run this program from the main CCL directory\n");
266  exit(1);
267  }
268  status = 0;
269  for (z=0; z<100; z=z+1) {
270  z_test = 0.035*z;
271  ccl_dNdz_tomog(z_test, 0.,6., pz_info_example, dN_info_example, &dNdz_tomo,&status);
272  ccl_dNdz_tomog(z_test, 0.,0.6, pz_info_example,dN_info_example, &tmp1,&status);
273  ccl_dNdz_tomog(z_test, 0.6,1.2, pz_info_example,dN_info_example, &tmp2,&status);
274  ccl_dNdz_tomog(z_test, 1.2,1.8, pz_info_example,dN_info_example, &tmp3,&status);
275  ccl_dNdz_tomog(z_test, 1.8,2.4, pz_info_example,dN_info_example, &tmp4,&status);
276  ccl_dNdz_tomog(z_test, 2.4,3.0, pz_info_example,dN_info_example, &tmp5,&status);
277  fprintf(output, "%f %f %f %f %f %f %f\n", z_test,tmp1,tmp2,tmp3,tmp4,tmp5,dNdz_tomo);
278  }
279 
280  fclose(output);
281 
282  //Free up photo-z info
283  ccl_free_photoz_info(pz_info_example);
284  ccl_free_photoz_info(pz_info_gaussian);
285 
286  // Free the dNdz information
287  ccl_free_dNdz_info(dN_info_example);
288  ccl_free_dNdz_info(dN_info_gaussian);
289 
290  //Always clean up!!
291  ccl_cosmology_free(cosmo);
292 
293  return 0;
294 }
double ccl_scale_factor_of_chi(ccl_cosmology *cosmo, double chi, int *status)
#define OK
double double
Definition: precision.hpp:19
#define OB
Definition: ccl_sample_run.c:9
double user_dNdz(double z, void *user_par, int *status)
int main(int argc, char **argv)
double ccl_omega_x(ccl_cosmology *cosmo, double a, ccl_species_x_label label, int *status)
#define Z0_SH
double ccl_growth_factor(ccl_cosmology *cosmo, double a, int *status)
double ccl_massfunc(ccl_cosmology *cosmo, double smooth_mass, double a, double odelta, int *status)
Definition: ccl_massfunc.c:562
#define NORMPS
double ccl_luminosity_distance(ccl_cosmology *cosmo, double a, int *status)
double ccl_growth_rate(ccl_cosmology *cosmo, double a, int *status)
void ccl_cl_tracer_free(CCL_ClTracer *clt)
Definition: ccl_cls.c:592
#define WA
#define M_PI
Definition: ccl_constants.h:22
#define NEFF
dNdz_info * ccl_create_dNdz_info(void *params, double(*dNdz_func)(double, void *, int *))
Definition: ccl_redshifts.c:94
int ccl_get_tracer_fas(ccl_cosmology *cosmo, CCL_ClTracer *clt, int na, double *a, double *fa, int func_code, int *status)
Definition: ccl_cls.c:1091
transfer_function_t transfer_function_method
Definition: ccl_config.h:111
void ccl_angular_cls(ccl_cosmology *cosmo, CCL_ClWorkspace *w, CCL_ClTracer *clt1, CCL_ClTracer *clt2, int nl_out, int *l, double *cl, int *status)
Definition: ccl_cls.c:943
#define HH
dNdz_info * ccl_create_Smail_dNdz_info(double alpha, double beta, double z0)
Grid< NDIM, T > sqrt(Grid< NDIM, T > lhs)
Definition: grid.h:231
ccl_cosmology * ccl_cosmology_create(ccl_parameters params, ccl_configuration config)
Definition: ccl_core.c:173
static double beta[2][28][8]
Definition: ccl_emu17.c:32
#define W0
pz_info * ccl_create_gaussian_photoz_info(double sigma_z0)
Definition: ccl_redshifts.c:67
ccl_mnu_convention
Definition: ccl_core.h:142
CCL_ClTracer * ccl_cl_tracer_number_counts_simple(ccl_cosmology *cosmo, int nz_n, double *z_n, double *n, int nz_b, double *z_b, double *b, int *status)
Definition: ccl_cls.c:633
void ccl_cosmology_free(ccl_cosmology *cosmo)
Definition: ccl_core.c:829
#define ZD
dictionary params
Definition: halomod_bm.py:27
double user_pz_probability(double z_ph, double z_s, void *user_par, int *status)
double ccl_rho_x(ccl_cosmology *cosmo, double a, ccl_species_x_label label, int is_comoving, int *status)
static double z[8]
static int p
Definition: ccl_emu17.c:25
double ccl_halo_bias(ccl_cosmology *cosmo, double smooth_mass, double a, double odelta, int *status)
Definition: ccl_massfunc.c:582
double sigmaz_sources(double z)
double(* sigma_z)(double)
double ccl_sigma8(ccl_cosmology *cosmo, int *status)
Definition: ccl_power.c:1777
const ccl_configuration default_config
Definition: ccl_core.c:21
float pow(float base, unsigned long int exp)
Definition: precision.hpp:39
CCL_ClTracer * ccl_cl_tracer_cmblens(ccl_cosmology *cosmo, double z_source, int *status)
Definition: ccl_cls.c:614
pz_info * ccl_create_photoz_info(void *params, double(*pz_func)(double, double, void *, int *))
Definition: ccl_redshifts.c:21
#define SZ_SH
#define NZ
#define NS
void ccl_free_photoz_info(pz_info *my_photoz_info)
Definition: ccl_redshifts.c:84
double chimax
Definition: ccl_cls.h:34
double chimin
Definition: ccl_cls.h:35
#define RHO_CRITICAL
Definition: ccl_constants.h:61
#define OC
Definition: ccl_sample_run.c:8
#define SZ_GC
void ccl_dNdz_tomog(double z, double bin_zmin, double bin_zmax, pz_info *photo_info, dNdz_info *dN_info, double *tomoout, int *status)
double ccl_distance_modulus(ccl_cosmology *cosmo, double a, int *status)
ccl_parameters ccl_parameters_create(double Omega_c, double Omega_b, double Omega_k, double Neff, double *mnu, ccl_mnu_convention mnu_type, double w0, double wa, double h, double norm_pk, double n_s, double bcm_log10Mc, double bcm_etab, double bcm_ks, int nz_mgrowth, double *zarr_mgrowth, double *dfarr_mgrowth, int *status)
Definition: ccl_core.c:294
double ccl_comoving_radial_distance(ccl_cosmology *cosmo, double a, int *status)
static double w[2][28][111]
Definition: ccl_emu17.c:33
#define MNU
#define Z0_GC
#define NL
void ccl_free_dNdz_info(dNdz_info *dN_info)
CCL_ClTracer * ccl_cl_tracer_lensing_simple(ccl_cosmology *cosmo, int nz_n, double *z_n, double *n, int *status)
Definition: ccl_cls.c:653