Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_test_emu.c
Go to the documentation of this file.
1 #include "ccl.h"
2 #include "ctest.h"
3 #include <stdio.h>
4 #include <math.h>
5 
6 /* Automated test for power spectrum emulation within CCL
7  using the Lawrence et al. (2017) code.
8  The test compares the smoothed simulated power spectra
9  provided by the paper authors to the CCL output of the
10  power spectrum via the emulator. This test corresponds
11  to Figure 6 of the emulator paper, for a specific subset
12  of the cosmologies: {1,3,5,6,8,10}. Other cosmologies
13  are not allowed because CLASS fails when w(z) crosses -1
14  and we need the linear power spectrum from CLASS in general
15  for sigma8 computation.
16 */
17 
18 
19 #define EMU_TOLERANCE 3.0E-2
20 //This is the tolerance we have required based on the emulator
21 //paper results (Section 3.3, Fig 6).
22 
23 CTEST_DATA(emu) {
24  double Neff;
25  double* mnu;
27  double sigma8[6];
28  double Omega_c[6];
29  double Omega_b[6];
30  double n_s[6];
31  double h[6];
32  double w_0[6];
33  double w_a[6];
34 };
35 
37 
38  //This test corresponds to emulator cosmologies
39  //without neutrinos. Other cosmologies are
40  //implemented in ccl_test_emu_nu.c
41  data->Neff=3.04;
42  double mnuval = 0.;
43  data->mnu=&mnuval;
44  data->mnu_type=ccl_mnu_sum;
45 
46  double *sigma8;
47  double *Omega_c;
48  double *Omega_b;
49  double *n_s;
50  double *h;
51  double *w_0;
52  double *w_a;
53  int status=0;
54  char fname[256],str[1024];
55  char* rtn;
56  FILE *f;
57  int i;
58  sigma8=malloc(6*sizeof(double));
59  Omega_c=malloc(6*sizeof(double));
60  Omega_b=malloc(6*sizeof(double));
61  n_s=malloc(6*sizeof(double));
62  h=malloc(6*sizeof(double));
63  w_0=malloc(6*sizeof(double));
64  w_a=malloc(6*sizeof(double));
65 
66  //Each line of this file corresponds to the cosmological parameters for
67  //cosmologies {1,3,5,6,8,10} of the emulator set. Notice that Omega_i
68  //are big Omegas and not little omegas (Omega_i*h**2=omega_i)
69  sprintf(fname,"./tests/benchmark/emu_cosmologies.txt");
70  f=fopen(fname,"r");
71  if(f==NULL) {
72  fprintf(stderr,"Error opening file %s\n",fname);
73  exit(1);
74  }
75 
76  double tmp;
77  for(int i=0;i<6;i++) {
78 
79  status=fscanf(f,"%le %le %le %le %le %le %le\n",&Omega_c[i],&Omega_b[i],&h[i],&sigma8[i],&n_s[i],&w_0[i],&w_a[i]);
80  if(status!=7) {
81  fprintf(stderr,"Error reading file %s, line %d\n",fname,i);
82  exit(1);
83  }
84  data->w_0[i] = w_0[i];
85  data->w_a[i] = w_a[i];
86  data->h[i] = h[i];
87  data->sigma8[i] = sigma8[i];
88  data->Omega_c[i] = Omega_c[i];
89  data->Omega_b[i] = Omega_b[i];
90  data->n_s[i] = n_s[i];
91  }
92  fclose(f);
93 }
94 
95 static int linecount(FILE *f)
96 {
98  // Counts #lines from file
99  int i0=0;
100  char ch[1000];
101  while((fgets(ch,sizeof(ch),f))!=NULL) {
102  i0++;
103  }
104  return i0;
105 }
106 
107 static void compare_emu(int i_model,struct emu_data * data)
108 {
109  int nk,i,j;
110  int status=0;
111  char fname[256],str[1024];
112  char* rtn;
113  FILE *f;
114  int i_model_vec[6]={1,3,5,6,8,10};
115  //The emulator cosmologies we can compare to
116  //without CLASS failing due to w(z) crossing -1.
117 
121 
122  //None of the current cosmologies being checked include neutrinos
123  ccl_parameters params = ccl_parameters_create(data->Omega_c[i_model-1],data->Omega_b[i_model-1],0.0,data->Neff, data->mnu, data->mnu_type, data->w_0[i_model-1],data->w_a[i_model-1],data->h[i_model-1],data->sigma8[i_model-1],data->n_s[i_model-1],-1,-1,-1,-1,NULL,NULL, &status);
124  params.Omega_l=params.Omega_l+params.Omega_g;
125  params.Omega_g=0;
126  ccl_cosmology * cosmo = ccl_cosmology_create(params, config);
127  ASSERT_NOT_NULL(cosmo);
128  //Each of these files has the smoothed simulated power spectrum for
129  //the corresponding cosmology, kindly provided by E. Lawrence.
130  sprintf(fname,"./tests/benchmark/emu_smooth_pk_M%d.txt",i_model_vec[i_model-1]);
131  f=fopen(fname,"r");
132  if(f==NULL) {
133  fprintf(stderr,"Error opening file %s\n",fname);
134  exit(1);
135  }
136  nk=linecount(f)-1; rewind(f);
137 
138  double k=0.,pk_bench=0.,pk_ccl,err;
139  double z=0.; //Other redshift checks are possible but not currently implemented
140  int stat=0;
141 
142  for(i=0;i<nk;i++) {
143  stat=fscanf(f,"%le %le\n",&k, &pk_bench);
144  if(stat!=2) {
145  fprintf(stderr,"Error reading file %s, line %d\n",fname,i);
146  exit(1);
147  }
148  pk_ccl=ccl_nonlin_matter_power(cosmo,k,1./(1+z),&status);
149  if (status) printf("%s\n",cosmo->status_message);
150  err=fabs(pk_ccl/pk_bench-1);
152  }
153 
154  fclose(f);
155 
156  ccl_cosmology_free(cosmo);
157 }
158 
159 //Cosmology M001
160 CTEST2(emu,model_1) {
161  int model=1;
162  compare_emu(model,data);
163 }
164 
165 
166 CTEST2(emu,model_2) {
167  int model=2;
168  compare_emu(model,data);
169 }
170 
171 /*
172 CTEST2(emu,model_3) {
173  int model=3;
174  compare_emu(model,data);
175 }
176 */
177 CTEST2(emu,model_4) {
178  int model=4;
179  compare_emu(model,data);
180 }
181 
182 /*
183 CTEST2(emu,model_5) {
184  int model=5;
185  compare_emu(model,data);
186 }
187 */
188 CTEST2(emu,model_6) {
189  int model=6;
190  compare_emu(model,data);
191 }
double w_0[6]
Definition: ccl_test_emu.c:32
double Omega_b[6]
Definition: ccl_test_emu.c:29
double Omega_g
Definition: ccl_core.h:53
double Omega_c[6]
Definition: ccl_test_emu.c:28
double * mnu
Definition: ccl_test_emu.c:25
matter_power_spectrum_t matter_power_spectrum_method
Definition: ccl_config.h:112
double ccl_nonlin_matter_power(ccl_cosmology *cosmo, double k, double a, int *status)
Definition: ccl_power.c:1562
double h[6]
Definition: ccl_test_emu.c:31
#define CTEST_SETUP(sname)
Definition: ctest.h:73
#define CTEST_DATA(sname)
Definition: ctest.h:71
static void compare_emu(int i_model, struct emu_data *data)
Definition: ccl_test_emu.c:107
double Omega_l
Definition: ccl_core.h:63
double sigma8[6]
Definition: ccl_test_emu.c:27
double Neff
Definition: ccl_test_emu.c:24
transfer_function_t transfer_function_method
Definition: ccl_config.h:111
static int linecount(FILE *f)
Definition: ccl_test_emu.c:95
ccl_cosmology * ccl_cosmology_create(ccl_parameters params, ccl_configuration config)
Definition: ccl_core.c:173
double w_a[6]
Definition: ccl_test_emu.c:33
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]
#define EMU_TOLERANCE
Definition: ccl_test_emu.c:19
double n_s[6]
Definition: ccl_test_emu.c:30
const ccl_configuration default_config
Definition: ccl_core.c:21
#define ASSERT_NOT_NULL(real)
Definition: ctest.h:139
ccl_mnu_convention mnu_type
Definition: ccl_test_emu.c:26
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