Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_test_emu_nu.c
Go to the documentation of this file.
1 #include "ccl.h"
2 #include "ccl_neutrinos.h" //Needed for Omeganu->M computation
3 #include "ctest.h"
4 #include <stdio.h>
5 #include <math.h>
6 
7 /* Automated test for power spectrum emulation within CCL
8  using the Lawrence et al. (2017) code.
9  The test compares the smoothed simulated power spectra
10  provided by the paper authors to the CCL output of the
11  power spectrum via the emulator. This test corresponds
12  to Figure 5 of the emulator paper, for a specific subset
13  of the cosmologies: {38,39,40,42}. Other cosmologies
14  are not allowed because CLASS fails when w(z) crosses -1
15  and we need the linear power spectrum from CLASS in general
16  for sigma8 computation.
17 */
18 
19 #define EMU_TOLERANCE 3.0E-2
20 //This is the tolerance we have required based on the emulator
21 //paper results (Fig 5).
22 
23 CTEST_DATA(emu_nu) {
24  double Neff;
25  double *mnu[4];
27  double sigma8[4];
28  double Omega_c[4];
29  double Omega_b[4];
30  double n_s[4];
31  double h[4];
32  double w_0[4];
33  double w_a[4];
34 };
35 
36 CTEST_SETUP(emu_nu) {
37 
38  data->Neff = 3.04;
39  data->mnu_type = ccl_mnu_list;
40 
41  double *sigma8;
42  double *Omega_c;
43  double *Omega_b;
44  double *Omega_nu;
45  double *n_s;
46  double *h;
47  double *w_0;
48  double *w_a;
49  double *Mnu_out;
50  int status=0;
51  char fname[256],str[1024];
52  char* rtn;
53  FILE *f;
54  int i;
56 
57  sigma8=malloc(4*sizeof(double));
58  Omega_c=malloc(4*sizeof(double));
59  Omega_b=malloc(4*sizeof(double));
60  n_s=malloc(4*sizeof(double));
61  h=malloc(4*sizeof(double));
62  w_0=malloc(4*sizeof(double));
63  w_a=malloc(4*sizeof(double));
64  Omega_nu=malloc(4*sizeof(double));
65 
66  // Omeganuh2_to_mnu will output a pointer to an array of 3 neutrino masses.
67  Mnu_out=malloc(3*sizeof(double));
68 
69  //Each line of this file corresponds to the cosmological parameters for
70  //cosmologies {38,39,40,42} of the emulator set. Notice that Omega_i
71  //are big Omegas and not little omegas (Omega_i*h**2=omega_i)
72  sprintf(fname,"./tests/benchmark/emu_nu_cosmologies.txt");
73  f=fopen(fname,"r");
74  if(f==NULL) {
75  fprintf(stderr,"Error opening file %s\n",fname);
76  exit(1);
77  }
78 
79  double tmp;
80  int omnustatus=0;
81  for(int i=0;i<4;i++) {
82 
83  status=fscanf(f,"%le %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],&Omega_nu[i]);
84  if(status!=8) {
85  fprintf(stderr,"Error reading file %s, line %d\n",fname,i);
86  exit(1);
87  }
88  data->w_0[i] = w_0[i];
89  data->w_a[i] = w_a[i];
90  data->h[i] = h[i];
91  data->sigma8[i] = sigma8[i];
92  data->Omega_c[i] = Omega_c[i];
93  data->Omega_b[i] = Omega_b[i];
94  data->n_s[i] = n_s[i];
95  // Number of neutrino species is fixed to 3
96  Mnu_out = ccl_nu_masses(Omega_nu[i]*h[i]*h[i], ccl_nu_equal, 2.725, &omnustatus);
97  /*if (omnustatus){
98  printf("%s\n",cosmo->status_message);
99  exit(1);
100  }*/
101  data->mnu[i]=Mnu_out;
102  }
103  fclose(f);
104 }
105 
106 static int linecount(FILE *f)
107 {
109  // Counts #lines from file
110  int i0=0;
111  char ch[1000];
112  while((fgets(ch,sizeof(ch),f))!=NULL) {
113  i0++;
114  }
115  return i0;
116 }
117 
118 static void compare_emu_nu(int i_model,struct emu_nu_data * data)
119 {
120  int nk,i,j;
121  int status=0;
122  char fname[256],str[1024];
123  char* rtn;
124  FILE *f;
125  int i_model_vec[4]={38,39,40,42}; //The emulator cosmologies we can compare to
126 
130 
131  //None of the current cosmologies being checked include neutrinos
132  ccl_parameters params = ccl_parameters_create(data->Omega_c[i_model-1],data->Omega_b[i_model-1],0.0,data->Neff, data->mnu[i_model-1], 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);
133  params.Omega_l=params.Omega_l+params.Omega_g;
134  params.Omega_g=0;
135  ccl_cosmology * cosmo = ccl_cosmology_create(params, config);
136  ASSERT_NOT_NULL(cosmo);
137 
138  //These files contain the smoothed power spectra with neutrinos
139  //These are obtained as follows:
140  // (1) Find the z=0 P(k) corresponding to M038. This would be column 304 of the
141  // 'yalt...' file (with column numbering starting in 1).
142  // (2) This column has log10(Delta_cb^2/k^1.5), so convert accordingly to Delta_cb^2.
143  // (3) Take the power spectrum in the file ‘pk_lin...' for M038 (second column for P(k) in Mpc^3)
144  // and convert to Delta_nu^2 by multiplying by k^3/(2pi^2).
145  // (4) Add to obtain Delta_tot=(sqrt(Delta_nu)+sqrt(Delta_cb))^2.
146  // (5) Convert back to P(k)
147  sprintf(fname,"./tests/benchmark/emu_nu_smooth_pk_M%d.txt",i_model_vec[i_model-1]);
148  f=fopen(fname,"r");
149  if(f==NULL) {
150  fprintf(stderr,"Error opening file %s\n",fname);
151  exit(1);
152  }
153  nk=linecount(f)-1; rewind(f);
154 
155  double k=0.,pk_bench=0.,pk_ccl,err;
156  double z=0.; //Other redshift checks are possible but not currently implemented
157  int stat=0;
158 
159  for(i=0;i<nk;i++) {
160  stat=fscanf(f,"%le %le\n",&k, &pk_bench);
161  if(stat!=2) {
162  fprintf(stderr,"Error reading file %s, line %d\n",fname,i);
163  exit(1);
164  }
165  pk_ccl=ccl_nonlin_matter_power(cosmo,k,1./(1+z),&status);
166  if (status) printf("%s\n",cosmo->status_message);
167  err=fabs(pk_ccl/pk_bench-1);
168  //printf("%le %le %le %le\n",k,pk_ccl,pk_bench,err);
170  }
171 
172  fclose(f);
173 
174  ccl_cosmology_free(cosmo);
175 }
176 
177 CTEST2(emu_nu,model_1) {
178  int model=1;
179  compare_emu_nu(model,data);
180 }
181 
182 
183 CTEST2(emu_nu,model_2) {
184  int model=2;
185  compare_emu_nu(model,data);
186  }
187 
188 CTEST2(emu_nu,model_3) {
189  int model=3;
190  compare_emu_nu(model,data);
191 }
192 
193 CTEST2(emu_nu,model_4) {
194  int model=4;
195  compare_emu_nu(model,data);
196  }
double Omega_g
Definition: ccl_core.h:53
#define EMU_TOLERANCE
double * ccl_nu_masses(double OmNuh2, ccl_neutrino_mass_splits mass_split, double T_CMB, int *status)
double w_a[4]
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 * mnu[4]
ccl_mnu_convention mnu_type
#define CTEST_SETUP(sname)
Definition: ctest.h:73
#define CTEST_DATA(sname)
Definition: ctest.h:71
double w_0[4]
double Omega_l
Definition: ccl_core.h:63
double Omega_b[4]
transfer_function_t transfer_function_method
Definition: ccl_config.h:111
static int linecount(FILE *f)
ccl_cosmology * ccl_cosmology_create(ccl_parameters params, ccl_configuration config)
Definition: ccl_core.c:173
double h[4]
ccl_mnu_convention
Definition: ccl_core.h:142
void ccl_cosmology_free(ccl_cosmology *cosmo)
Definition: ccl_core.c:829
double n_s[4]
dictionary params
Definition: halomod_bm.py:27
#define ASSERT_DBL_NEAR_TOL(exp, real, tol)
Definition: ctest.h:152
static void compare_emu_nu(int i_model, struct emu_nu_data *data)
static double z[8]
const ccl_configuration default_config
Definition: ccl_core.c:21
#define ASSERT_NOT_NULL(real)
Definition: ctest.h:139
double sigma8[4]
double Omega_c[4]
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