Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_sample_photoz.c
Go to the documentation of this file.
1 #include <math.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 
5 #include <ccl.h>
6 #include <ccl_redshifts.h>
7 
8 // This is an example code showing how to incldue a user-defined photo-z
9 // function and user-defined true redshift distribution
10 
11 // The user defines a structure of parameters to the user-defined function for the photo-z probability
14 };
15 
16 // Define the function we want to use for sigma_z which is included in the above struct.
17 double sigmaz_sources(double z)
18 {
19  return 0.05*(1.0+z);
20 }
21 
22 // The user defines a function of the form double function ( z_ph, z_spec, void * user_pz_params, int *status) 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.
23 
24 double user_pz_probability(double z_ph, double z_s, void * user_par, int *status)
25 {
26  struct user_pz_params * p = (struct user_pz_params *) user_par;
27 
28  return exp(- (z_ph-z_s)*(z_ph-z_s) / (2.*(p->sigma_z(z_s))*(p->sigma_z(z_s)))) / (pow(2.*M_PI,0.5)*(p->sigma_z(z_s))*(p->sigma_z(z_s)));
29 }
30 
31 // The user defines a structure of parameters to the user-defined function for the true dNdz
33  double alpha;
34  double beta;
35  double z0;
36 };
37 
38 // 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.
39 
40 double user_dNdz(double z, void * user_par, int *status)
41 {
42  struct user_dN_params * p = (struct user_dN_params *) user_par;
43 
44  return pow(z, p->alpha) * exp(- pow(z/(p->z0), p->beta) );
45 
46 }
47 
48 int main(int argc,char **argv)
49 {
50  // The user declares and sets an instance of parameters to their photo_z function:
51  struct user_pz_params my_pz_params_example;
52  my_pz_params_example.sigma_z = sigmaz_sources;
53  struct user_dN_params my_dN_params_example;
54  my_dN_params_example.alpha = 1.24;
55  my_dN_params_example.beta = 1.01;
56  my_dN_params_example.z0 = 0.51;
57 
58  // Declare a variable of the type of user_pz_info to hold the struct to be created.
59  pz_info * my_pz_info;
60 
61  // Create the struct to hold the user information about photo_z's.
62  my_pz_info = ccl_create_photoz_info(&my_pz_params_example, &user_pz_probability);
63 
64  // Declare a variable of the type of user_dN_info to hold the struct to be created
65  dNdz_info * my_dN_info;
66 
67  // Create a simple analytic true redshift distribution:
68  my_dN_info = ccl_create_dNdz_info(&my_dN_params_example, &user_dNdz);
69 
70  int status = 0;
71  double z_test;
72  int z;
73  double tmp1,tmp2,tmp3,tmp4,tmp5;
74  double dNdz_tomo;
75  FILE * output;
76  output = fopen("./tests/example_tomographic_bins.out", "w");
77  for (z=0; z<100; z=z+1) {
78  z_test = 0.035*z;
79 
80  ccl_dNdz_tomog(z_test, 0.,6.,my_pz_info, my_dN_info, &dNdz_tomo, &status);
81  if (status!=0) {
82  printf("Error in initiating the tomographic bins. Exiting.\n");
83  exit(1);
84  }
85 
86  ccl_dNdz_tomog(z_test, 0.,0.6,my_pz_info, my_dN_info, &tmp1, &status);
87  if (status!=0) {
88  printf("Error in initiating the tomographic bins. Exiting.\n");
89  exit(1);
90  }
91 
92  ccl_dNdz_tomog(z_test, 0.6,1.2,my_pz_info, my_dN_info, &tmp2, &status);
93  if (status!=0) {
94  printf("Error in initiating the tomographic bins. Exiting.\n");
95  exit(1);
96  }
97  ccl_dNdz_tomog(z_test, 1.2,1.8,my_pz_info, my_dN_info, &tmp3, &status);
98  if (status!=0) {
99  printf("Error in initiating the tomographic bins. Exiting.\n");
100  exit(1);
101  }
102  ccl_dNdz_tomog(z_test, 1.8,2.4,my_pz_info, my_dN_info, &tmp4, &status);
103  if (status!=0) {
104  printf("Error in initiating the tomographic bins. Exiting.\n");
105  exit(1);
106  }
107  ccl_dNdz_tomog(z_test, 2.4,3.0,my_pz_info, my_dN_info, &tmp5, &status);
108  if (status!=0) {
109  printf("Error in initiating the tomographic bins. Exiting.\n");
110  exit(1);
111  }
112  fprintf(output, "%f %f %f %f %f %f %f\n", z_test,tmp1,tmp2,tmp3,tmp4,tmp5,dNdz_tomo);
113  }
114 
115  fclose(output);
116 
117  // Free the photo_z information
118  ccl_free_photoz_info(my_pz_info);
119 
120  // Free the dNdz information
121  ccl_free_dNdz_info(my_dN_info);
122 
123  return 0;
124 }
double double
Definition: precision.hpp:19
double(* sigma_z)(double)
#define M_PI
Definition: ccl_constants.h:22
dNdz_info * ccl_create_dNdz_info(void *params, double(*dNdz_func)(double, void *, int *))
Definition: ccl_redshifts.c:94
double user_dNdz(double z, void *user_par, int *status)
static double z[8]
int main(int argc, char **argv)
static int p
Definition: ccl_emu17.c:25
double user_pz_probability(double z_ph, double z_s, void *user_par, int *status)
float pow(float base, unsigned long int exp)
Definition: precision.hpp:39
pz_info * ccl_create_photoz_info(void *params, double(*pz_func)(double, double, void *, int *))
Definition: ccl_redshifts.c:21
double sigmaz_sources(double z)
void ccl_free_photoz_info(pz_info *my_photoz_info)
Definition: ccl_redshifts.c:84
void ccl_dNdz_tomog(double z, double bin_zmin, double bin_zmax, pz_info *photo_info, dNdz_info *dN_info, double *tomoout, int *status)
void ccl_free_dNdz_info(dNdz_info *dN_info)