Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_test_correlation_3d.c
Go to the documentation of this file.
1 #include "ccl.h"
2 #include "ctest.h"
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <math.h>
6 
7 CTEST_DATA(corrs_3d) {
8  double Omega_c;
9  double Omega_b;
10  double h;
11  double A_s;
12  double n_s;
13  double sigma8;
14  double Neff;
15  double* mnu;
17  double Omega_v[5];
18  double Omega_k[5];
19  double w_0[5];
20  double w_a[5];
21 };
22 
23 CTEST_SETUP(corrs_3d) {
24  data->Omega_c = 0.25;
25  data->Omega_b = 0.05;
26  data->h = 0.7;
27  data->A_s = 0.8;
28  data->n_s = 0.96;
29  data->sigma8=0.8;
30  data->Neff=3.046;
31  double mnuval = 0.;
32  data->mnu= &mnuval;
33  data->mnu_type = ccl_mnu_sum;
34 
35  double Omega_v[5]={0.7, 0.7, 0.7, 0.65, 0.75};
36  double w_0[5] = {-1.0, -0.9, -0.9, -0.9, -0.9};
37  double w_a[5] = {0.0, 0.0, 0.1, 0.1, 0.1};
38  for(int i=0;i<5;i++) {
39  data->Omega_v[i] = Omega_v[i];
40  data->w_0[i] = w_0[i];
41  data->w_a[i] = w_a[i];
42  data->Omega_k[i] = 1.0 - data->Omega_c - data->Omega_b - data->Omega_v[i];
43  }
44 }
45 
46 static int linecount(FILE *f)
47 {
49  // Counts #lines from file
50  int i0=0;
51  char ch[1000];
52  while((fgets(ch,sizeof(ch),f))!=NULL) {
53  i0++;
54  }
55  return i0;
56 }
57 
58 static void compare_correlation_3d(int i_model,struct corrs_3d_data * data)
59 {
60  int nk,nr,i,j;
61  int status=0;
62  char fname[256],str[1024];
63  char* rtn;
64  FILE *f;
68  ccl_parameters params = ccl_parameters_create(data->Omega_c,data->Omega_b,data->Omega_k[i_model-1],
69  data->Neff, data->mnu, data->mnu_type, data->w_0[i_model-1],data->w_a[i_model-1],
70  data->h,data->A_s,data->n_s,-1, -1, -1, -1,NULL,NULL, &status);
71  params.Omega_g=0.0;
72  params.Omega_l=data->Omega_v[i_model-1];
73  params.sigma8=data->sigma8;
74  ccl_cosmology * cosmo = ccl_cosmology_create(params, config);
75  ASSERT_NOT_NULL(cosmo);
76 
77  sprintf(fname,"./tests/benchmark/model%d_xi.txt",i_model);
78  f=fopen(fname,"r");
79  if(f==NULL) {
80  fprintf(stderr,"Error opening file %s\n",fname);
81  exit(1);
82  }
83  nr=linecount(f)-1; rewind(f);
84 
85  // tolerence on abs difference in r^2 xi(r) for the range r = 0.1 - 100 Mpc (40 points in r) for z=0,1,2,3,4,5
86  double CORR_TOLERANCE1[6] = {2.84e-2, 3.57e-3, 1.37e-3, 1.26e-3, 2.0e-4, 1.8e-4};
87  // tolerence on abs difference in r^2 xi(r) for the range r = 50 - 250 Mpc (100 points in r) for z=0,1,2,3,4,5
88  double CORR_TOLERANCE2[6] = {3.15e-2, 9.87e-3, 5.04e-3, 3.05e-3, 9.98e-4, 7.14e-4};
89 
90  int N1=40;
91  double *r_arr1=malloc(N1*sizeof(double));
92  double *r_arr2=malloc((nr-N1)*sizeof(double));
93  double (*ximm_bench_arr)[6]=malloc(6*nr*sizeof(double));
94 
95  rtn = fgets(str, 1024, f);
96  for(i=0;i<nr;i++) {
97  double r_h;
98  int stat;
99  stat=fscanf(f,"%lf",&r_h);
100  if(stat!=1) {
101  fprintf(stderr,"Error reading file %s, line %d\n",fname,i+2);
102  exit(1);
103  }
104 
105  if(i<N1)
106  r_arr1[i]=r_h;
107  else
108  r_arr2[i-N1]=r_h;
109 
110  for(j=0;j<6;j++) {
111  double ximm_bench;
112  stat=fscanf(f,"%lf",&ximm_bench);
113  if(stat!=1) {
114  fprintf(stderr,"Error reading file %s, line %d\n",fname,i+2);
115  exit(1);
116  }
117 
118  ximm_bench_arr[i][j]=ximm_bench;
119  }
120  }
121 
122  double *ximm_ccl_out1=malloc(N1*sizeof(double));
123  double *ximm_ccl_out2=malloc((nr-N1)*sizeof(double));
124 
125  for(j=0;j<6;j++) {
126  double z = j+0.;
127  ccl_correlation_3d(cosmo,1.0/(j+1),N1,r_arr1,ximm_ccl_out1,0,NULL,&status);
128  ccl_correlation_3d(cosmo,1.0/(j+1),nr-N1,r_arr2,ximm_ccl_out2,0,NULL,&status);
129 
130  if (status) printf("%s\n",cosmo->status_message);
131  for(i=0;i<nr;i++){
132  double err;
133  if(i<N1){
134  err=fabs(r_arr1[i]*r_arr1[i]*(ximm_ccl_out1[i]-ximm_bench_arr[i][j]));
135  ASSERT_DBL_NEAR_TOL(0.,err,CORR_TOLERANCE1[j]);
136  }
137  else{
138  err=fabs(r_arr2[i-N1]*r_arr2[i-N1]*(ximm_ccl_out2[i-N1]-ximm_bench_arr[i][j]));
139  ASSERT_DBL_NEAR_TOL(0.,err,CORR_TOLERANCE2[j]);
140  }
141  }
142  }
143  fclose(f);
144 
145  free(r_arr1);
146  free(r_arr2);
147  free(ximm_bench_arr);
148  free(ximm_ccl_out1);
149  free(ximm_ccl_out2);
150 
151  ccl_cosmology_free(cosmo);
152 }
153 
154 CTEST2(corrs_3d,model_1) {
155  int model=1;
156  compare_correlation_3d(model,data);
157 }
158 
159 CTEST2(corrs_3d,model_2) {
160  int model=2;
161  compare_correlation_3d(model,data);
162 }
163 
164 CTEST2(corrs_3d,model_3) {
165  int model=3;
166  compare_correlation_3d(model,data);
167 }
double Omega_g
Definition: ccl_core.h:53
double double
Definition: precision.hpp:19
matter_power_spectrum_t matter_power_spectrum_method
Definition: ccl_config.h:112
#define CTEST_SETUP(sname)
Definition: ctest.h:73
#define CTEST_DATA(sname)
Definition: ctest.h:71
double Omega_l
Definition: ccl_core.h:63
ccl_mnu_convention mnu_type
static void compare_correlation_3d(int i_model, struct corrs_3d_data *data)
transfer_function_t transfer_function_method
Definition: ccl_config.h:111
ccl_cosmology * ccl_cosmology_create(ccl_parameters params, ccl_configuration config)
Definition: ccl_core.c:173
static int linecount(FILE *f)
ccl_mnu_convention
Definition: ccl_core.h:142
void ccl_cosmology_free(ccl_cosmology *cosmo)
Definition: ccl_core.c:829
dictionary params
Definition: halomod_bm.py:27
#define ASSERT_DBL_NEAR_TOL(exp, real, tol)
Definition: ctest.h:152
static double z[8]
double sigma8
Definition: ccl_core.h:62
const ccl_configuration default_config
Definition: ccl_core.c:21
#define ASSERT_NOT_NULL(real)
Definition: ctest.h:139
void ccl_correlation_3d(ccl_cosmology *cosmo, double a, int n_r, double *r, double *xi, int do_taper_pk, double *taper_pk_limits, 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
#define CTEST2(sname, tname)
Definition: ctest.h:107
char status_message[500]
Definition: ccl_core.h:136