Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_test_distances_astropy_mnu_hiz.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 // The tolerance in chi
7 // We use 1.0e-3 here because we compare with benchmarks produced by
8 // astropy, which uses a fitting formula for the neutrino
9 // phasespace integral, which itself differs from the exact expression
10 // at greater than a 1.0e-4 level.
11 #define DISTANCES_TOLERANCE 1.0e-3
12 
13 CTEST_DATA(distances_astropy_mnu_hiz) {
14  double Omega_c;
15  double Omega_b;
16  double h;
17  double A_s;
18  double n_s;
19  double Neff[5];
20  double mnu0[3], mnu1[3], mnu2[3], mnu3[3], mnu4[3];
22  double Omega_v[5];
23  double Omega_k[5];
24  double w_0[5];
25  double w_a[5];
26 
27  double z[5];
28  double chi[5][5];
29  double dm[5][5];
30 };
31 
32 // Read the fixed format file containing all the radial comoving
33 // distance benchmarks
34 static void read_chi_test_file(double z[5], double chi[5][5])
35 {
36  //Distances are in Mpc
37  FILE * f = fopen("./tests/benchmark/chi_hiz_mnu_model1-5.txt", "r");
38  ASSERT_NOT_NULL(f);
39 
40  // Ignore header line
41  char str[1024];
42  fgets(str, 1024, f);
43 
44  // File is fixed format - five rows and six columns
45  for (int i=0; i<5; i++) {
46  int count = fscanf(f, "%le %le %le %le %le %le\n", &z[i],
47  &chi[0][i], &chi[1][i], &chi[2][i], &chi[3][i], &chi[4][i]);
48  // Check that all the stuff in the benchmark is there
49  ASSERT_EQUAL(6, count);
50  }
51  fclose(f);
52 }
53 
54 static void read_dm_test_file(double z[5], double dm[5][5])
55 {
56  //Distances are in Mpc
57  FILE * f = fopen("./tests/benchmark/dm_hiz_mnu_model1-5.txt", "r");
58  ASSERT_NOT_NULL(f);
59 
60  // Ignore header line
61  char str[1024];
62  fgets(str, 1024, f);
63 
64  // File is fixed format - five rows and six columns
65  for (int i=0; i<5; i++) {
66  int count = fscanf(f, "%le %le %le %le %le %le\n", &z[i],
67  &dm[0][i], &dm[1][i], &dm[2][i], &dm[3][i], &dm[4][i]);
68  // Check that all the stuff in the benchmark is there
69  ASSERT_EQUAL(6, count);
70  }
71  fclose(f);
72 }
73 
74 // Set up the cosmological parameters to be used in each of the
75 // models
76 CTEST_SETUP(distances_astropy_mnu_hiz) {
77  // Values that are the same for all 5 models
78  data->Omega_c = 0.25;
79  data->Omega_b = 0.05;
80  data->h = 0.7;
81  data->A_s = 2.1e-9;
82  data->n_s = 0.96;
83 
84  // Values that are different for the different models
85  double Omega_v[5] = { 0.7, 0.7, 0.7, 0.65, 0.75 };
86  double w_0[5] = { -1.0, -0.9, -0.9, -0.9, -0.9 };
87  double w_a[5] = { 0.0, 0.0, 0.1, 0.1, 0.1 };
88 
89  // We use a total of 3 neutrinos instead of 3.046
90  // This is to compare with benchmarks from astropy
91  // which splits equally total N between all species
92  double Neff[5] = {3, 3, 3, 3, 3};
93  data->mnu_type = ccl_mnu_list;
94 
95  double mnu0[3] = {0.04, 0., 0.};
96  double mnu1[3] = {0.05, 0.01, 0.};
97  double mnu2[3] = {0.03, 0.02, 0.04};
98  double mnu3[3] = {0.05, 0., 0.};
99  double mnu4[3] = {0.03, 0.02, 0.};
100 
101  data->mnu0[0] = mnu0[0];
102  data->mnu1[0] = mnu1[0];
103  data->mnu2[0] = mnu2[0];
104  data->mnu3[0] = mnu3[0];
105  data->mnu4[0] = mnu4[0];
106 
107  data->mnu0[1] = mnu0[1];
108  data->mnu1[1] = mnu1[1];
109  data->mnu2[1] = mnu2[1];
110  data->mnu3[1] = mnu3[1];
111  data->mnu4[1] = mnu4[1];
112 
113  data->mnu0[2] = mnu0[2];
114  data->mnu1[2] = mnu1[2];
115  data->mnu2[2] = mnu2[2];
116  data->mnu3[2] = mnu3[2];
117  data->mnu4[2] = mnu4[2];
118 
119 
120  // Fill in the values from these constant arrays.
121  for (int i=0; i<5; i++) {
122  data->Omega_v[i] = Omega_v[i];
123  data->w_0[i] = w_0[i];
124  data->w_a[i] = w_a[i];
125  data->Omega_k[i] = 1.0 - data->Omega_c - data->Omega_b - data->Omega_v[i];
126  data->Neff[i] = Neff[i];
127  }
128 
129  // The file of benchmark data.
130  read_dm_test_file(data->z, data->dm);
131  read_chi_test_file(data->z, data->chi);
132 }
133 
134 static void compare_distances_hiz_mnu(int model, struct distances_astropy_mnu_hiz_data * data)
135 {
136  int status=0;
137  // Make the parameter set from the input data
138  // Values of some parameters depend on the model index
139 
140  // The arrays of massive neutrions are different lengths, so we need to have an if-statement here to deal with that.
141 
143 
144 
145  if (model==0){
146 
147  params = ccl_parameters_create(data->Omega_c, data->Omega_b, data->Omega_k[model],
148  data->Neff[model], data->mnu0, data->mnu_type,
149  data->w_0[model], data->w_a[model],
150  data->h, data->A_s, data->n_s,-1,-1,-1,-1,NULL,NULL, &status);
151  } else if (model==1){
152  params = ccl_parameters_create(data->Omega_c, data->Omega_b, data->Omega_k[model],
153  data->Neff[model], data->mnu1, data->mnu_type,
154  data->w_0[model], data->w_a[model],
155  data->h, data->A_s, data->n_s,-1,-1,-1,-1,NULL,NULL, &status);
156  } else if (model==2){
157  params = ccl_parameters_create(data->Omega_c, data->Omega_b, data->Omega_k[model],
158  data->Neff[model], data->mnu2, data->mnu_type,
159  data->w_0[model], data->w_a[model],
160  data->h, data->A_s, data->n_s,-1,-1,-1,-1,NULL,NULL, &status);
161  } else if (model ==3){
162  params = ccl_parameters_create(data->Omega_c, data->Omega_b, data->Omega_k[model],
163  data->Neff[model], data->mnu3, data->mnu_type,
164  data->w_0[model], data->w_a[model],
165  data->h, data->A_s, data->n_s,-1,-1,-1,-1,NULL,NULL, &status);
166  }else if (model ==4){
167  params = ccl_parameters_create(data->Omega_c, data->Omega_b, data->Omega_k[model],
168  data->Neff[model], data->mnu4, data->mnu_type,
169  data->w_0[model], data->w_a[model],
170  data->h, data->A_s, data->n_s,-1,-1,-1,-1,NULL,NULL, &status);
171  }
172 
173  // Make a cosmology object from the parameters with the default configuration
175  ASSERT_NOT_NULL(cosmo);
176 
177  // Compare to benchmark data
178  for (int j=0; j<5; j++) {
179  double a = 1/(1.+data->z[j]);
180  double chi_ij=ccl_comoving_radial_distance(cosmo,a, &status);
181  if (status) printf("%s\n",cosmo->status_message);
182  double absolute_tolerance = DISTANCES_TOLERANCE*data->chi[model][j];
183  if (fabs(absolute_tolerance)<1e-12) absolute_tolerance = 1e-12;
184  ASSERT_DBL_NEAR_TOL(data->chi[model][j], chi_ij, absolute_tolerance);
185 
186  if(a!=1) { //skip this test for a=1 since it will raise an error
187  double dm_ij=ccl_distance_modulus(cosmo,a, &status);
188  if (status) printf("%s\n",cosmo->status_message);
189  //NOTE tolerances are different!
190  absolute_tolerance = 10*DISTANCES_TOLERANCE*data->dm[model][j];
191  if (fabs(absolute_tolerance)<1e-4) absolute_tolerance = 1e-4;
192  ASSERT_DBL_NEAR_TOL(data->dm[model][j], dm_ij, absolute_tolerance);
193  }
194  }
195 
196  ccl_cosmology_free(cosmo);
197 }
198 
199 CTEST2(distances_astropy_mnu_hiz, model_1) {
200  int model = 0;
201  compare_distances_hiz_mnu(model, data);
202 }
203 
204 CTEST2(distances_astropy_mnu_hiz, model_2) {
205  int model = 1;
206  compare_distances_hiz_mnu(model, data);
207 }
208 
209 CTEST2(distances_astropy_mnu_hiz, model_3) {
210  int model = 2;
211  compare_distances_hiz_mnu(model, data);
212 }
213 
214 CTEST2(distances_astropy_mnu_hiz, model_4) {
215  int model = 3;
216  compare_distances_hiz_mnu(model, data);
217 }
218 
219 CTEST2(distances_astropy_mnu_hiz, model_5) {
220  int model = 4;
221  compare_distances_hiz_mnu(model, data);
222 }
#define DISTANCES_TOLERANCE
size_t count(InputIterator first, InputIterator last, T const &item)
Definition: catch.hpp:2747
#define CTEST_SETUP(sname)
Definition: ctest.h:73
#define CTEST_DATA(sname)
Definition: ctest.h:71
ccl_cosmology * ccl_cosmology_create(ccl_parameters params, ccl_configuration config)
Definition: ccl_core.c:173
static void read_chi_test_file(double z[5], double chi[5][5])
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]
static void read_dm_test_file(double z[5], double dm[5][5])
const ccl_configuration default_config
Definition: ccl_core.c:21
#define ASSERT_EQUAL(exp, real)
Definition: ctest.h:121
#define ASSERT_NOT_NULL(real)
Definition: ctest.h:139
static void compare_distances_hiz_mnu(int model, struct distances_astropy_mnu_hiz_data *data)
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)
#define CTEST2(sname, tname)
Definition: ctest.h:107
char status_message[500]
Definition: ccl_core.h:136