Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_test_distances_astropy_mnu_lowz.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_lowz) {
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_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_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_lowz) {
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  data->mnu_type = ccl_mnu_list;
84 
85  // Values that are different for the different models
86  double Omega_v[5] = { 0.7, 0.7, 0.7, 0.65, 0.75 };
87  double w_0[5] = { -1.0, -0.9, -0.9, -0.9, -0.9 };
88  double w_a[5] = { 0.0, 0.0, 0.1, 0.1, 0.1 };
89 
90  // We use a total of 3 neutrinos instead of 3.046
91  // This is to compare with benchmarks from astropy
92  // which splits equally total N between all species
93  double Neff[5] = {3, 3, 3, 3, 3};
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  // Fill in the values from these constant arrays.
120  for (int i=0; i<5; i++) {
121  data->Omega_v[i] = Omega_v[i];
122  data->w_0[i] = w_0[i];
123  data->w_a[i] = w_a[i];
124  data->Omega_k[i] = 1.0 - data->Omega_c - data->Omega_b - data->Omega_v[i];
125  data->Neff[i] = Neff[i];
126  }
127 
128  // The file of benchmark data.
129  read_dm_test_file(data->z, data->dm);
130  read_chi_test_file(data->z, data->chi);
131 }
132 
133 static void compare_distances_mnu(int model, struct distances_astropy_mnu_lowz_data * data)
134 {
135  int status=0;
136  // Make the parameter set from the input data
137  // Values of some parameters depend on the model index
138 
139  // The arrays of massive neutrions are different lengths, so we need to have an if-statement here to deal with that.
140 
142 
143 
144  if (model==0){
145 
146  params = ccl_parameters_create(data->Omega_c, data->Omega_b, data->Omega_k[model],
147  data->Neff[model], data->mnu0, data-> mnu_type,
148  data->w_0[model], data->w_a[model],
149  data->h, data->A_s, data->n_s,-1,-1,-1,-1,NULL,NULL, &status);
150  } else if (model==1){
151  params = ccl_parameters_create(data->Omega_c, data->Omega_b, data->Omega_k[model],
152  data->Neff[model], data->mnu1, data->mnu_type,
153  data->w_0[model], data->w_a[model],
154  data->h, data->A_s, data->n_s,-1,-1,-1,-1,NULL,NULL, &status);
155  } else if (model==2){
156  params = ccl_parameters_create(data->Omega_c, data->Omega_b, data->Omega_k[model],
157  data->Neff[model], data->mnu2, data->mnu_type,
158  data->w_0[model], data->w_a[model],
159  data->h, data->A_s, data->n_s,-1,-1,-1,-1,NULL,NULL, &status);
160  } else if (model ==3){
161  params = ccl_parameters_create(data->Omega_c, data->Omega_b, data->Omega_k[model],
162  data->Neff[model], data->mnu3, data->mnu_type,
163  data->w_0[model], data->w_a[model],
164  data->h, data->A_s, data->n_s,-1,-1,-1,-1,NULL,NULL, &status);
165  }else if (model ==4){
166  params = ccl_parameters_create(data->Omega_c, data->Omega_b, data->Omega_k[model],
167  data->Neff[model], data->mnu4, data->mnu_type,
168  data->w_0[model], data->w_a[model],
169  data->h, data->A_s, data->n_s,-1,-1,-1,-1,NULL,NULL, &status);
170  }
171 
172  // Make a cosmology object from the parameters with the default configuration
174  ASSERT_NOT_NULL(cosmo);
175 
176  // Compare to benchmark data
177  for (int j=0; j<5; j++) {
178  double a = 1/(1.+data->z[j]);
179  double chi_ij=ccl_comoving_radial_distance(cosmo,a, &status);
180  if (status) printf("%s\n",cosmo->status_message);
181  double absolute_tolerance = DISTANCES_TOLERANCE*data->chi[model][j];
182  if (fabs(absolute_tolerance)<1e-12) absolute_tolerance = 1e-12;
183  ASSERT_DBL_NEAR_TOL(data->chi[model][j], chi_ij, absolute_tolerance);
184 
185  if(a!=1) { //skip this test for a=1 since it will raise an error
186  double dm_ij=ccl_distance_modulus(cosmo,a, &status);
187  if (status) printf("%s\n",cosmo->status_message);
188  //NOTE tolerances are different!
189  absolute_tolerance = 10*DISTANCES_TOLERANCE*data->dm[model][j];
190  if (fabs(absolute_tolerance)<1e-4) absolute_tolerance = 1e-4;
191  ASSERT_DBL_NEAR_TOL(data->dm[model][j], dm_ij, absolute_tolerance);
192  }
193  }
194 
195  ccl_cosmology_free(cosmo);
196 }
197 
198 CTEST2(distances_astropy_mnu_lowz, model_1) {
199  int model = 0;
200  compare_distances_mnu(model, data);
201 }
202 
203 CTEST2(distances_astropy_mnu_lowz, model_2) {
204  int model = 1;
205  compare_distances_mnu(model, data);
206 }
207 
208 CTEST2(distances_astropy_mnu_lowz, model_3) {
209  int model = 2;
210  compare_distances_mnu(model, data);
211 }
212 
213 CTEST2(distances_astropy_mnu_lowz, model_4) {
214  int model = 3;
215  compare_distances_mnu(model, data);
216 }
217 
218 CTEST2(distances_astropy_mnu_lowz, model_5) {
219  int model = 4;
220  compare_distances_mnu(model, data);
221 }
static void read_chi_test_file(double z[5], double chi[5][5])
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
static void compare_distances_mnu(int model, struct distances_astropy_mnu_lowz_data *data)
ccl_cosmology * ccl_cosmology_create(ccl_parameters params, ccl_configuration config)
Definition: ccl_core.c:173
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
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