Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_test_halomod.c
Go to the documentation of this file.
1 #include "ccl.h"
2 #include "ccl_halomod.h"
3 #include "ctest.h"
4 #include <stdio.h>
5 #include <math.h>
6 #include <string.h>
7 
8 // Relative error tolerance in the halomodel matter power spectrum
9 #define HALOMOD_TOLERANCE 1E-3
10 #define NUMK 256
11 
12 // Data structure for the CTEST
13 CTEST_DATA(halomod){
14 
15  // Cosmological parameters
16  double Omega_c[3];
17  double Omega_b[3];
18  double Omega_k;
19  double Neff;
20  double* mnu;
22  double w_0;
23  double w_a;
24  double h[3];
25  double sigma_8[3];
26  double n_s[3];
27 
28  // Arrays for power-spectrum data
29  double k[3][2][NUMK];
30  double Pk[3][2][NUMK];
31 
32 };
33 
34 // Function to read in the benchmark data
35 static void read_halomod_test_file(double k[3][2][NUMK], double Pk[3][2][NUMK]){
36 
37  // Variables for reading in unwanted stuff and file name
38  double spam;
39  char infile[256];
40 
41  // Loop over cosmological models
42  for (int model=0; model<3; model++){
43 
44  // Loop over redshifts
45  for (int i=0; i<2; i++){
46 
47  // File names for different redshifts and cosmological models
48  if(model==0 && i==0){strncpy(infile, "./tests/benchmark/pk_hm_c1_z0.txt", 256);}
49  if(model==0 && i==1){strncpy(infile, "./tests/benchmark/pk_hm_c1_z1.txt", 256);}
50  if(model==1 && i==0){strncpy(infile, "./tests/benchmark/pk_hm_c2_z0.txt", 256);}
51  if(model==1 && i==1){strncpy(infile, "./tests/benchmark/pk_hm_c2_z1.txt", 256);}
52  if(model==2 && i==0){strncpy(infile, "./tests/benchmark/pk_hm_c3_z0.txt", 256);}
53  if(model==2 && i==1){strncpy(infile, "./tests/benchmark/pk_hm_c3_z1.txt", 256);}
54 
55  // Open the file
56  FILE * f = fopen(infile, "r");
57  ASSERT_NOT_NULL(f);
58 
59  // Loop over wavenumbers, which are k/h in benchmark data, power is Delta^2(k)
60  for (int j=0; j<NUMK; j++) {
61 
62  // Read in data from the benchmark file
63  int count = fscanf(f, "%le\t %le\t %le\t %le\t %le\n", &k[model][i][j], &spam, &spam, &spam, &Pk[model][i][j]);
64 
65  // Check that we have read in enough columns from the benchmark file
66  ASSERT_EQUAL(5, count);
67 
68  }
69 
70  // Close the file
71  fclose(f);
72 
73  }
74 
75  }
76 
77 }
78 
79 // set up the cosmological parameters structure to be used in the test case
80 CTEST_SETUP(halomod){
81 
82  // Move the cosmological parameters to the data structure
83  data->Omega_k = 0.00;
84  data->Neff = 0.00;
85  double mnuval = 0.00;
86  data->mnu = &mnuval;
87  data->mnu_type = ccl_mnu_sum;
88  data->w_0 = -1.00;
89  data->w_a = 0.00;
90 
91  // Cosmological parameters that are different for different tests
92  double Omega_c[3] = { 0.2500, 0.2265, 0.2685 };
93  double Omega_b[3] = { 0.0500, 0.0455, 0.0490 };
94  double h[3] = { 0.7000, 0.7040, 0.6711 };
95  double sigma_8[3] = { 0.8000, 0.8100, 0.8340 };
96  double n_s[3] = { 0.9600, 0.9670, 0.9624 };
97 
98  // Fill in the values from these constant arrays
99  for (int model=0; model<3; model++){
100  data->Omega_c[model] = Omega_c[model];
101  data->Omega_b[model] = Omega_b[model];
102  data->h[model] = h[model];
103  data->sigma_8[model] = sigma_8[model];
104  data->n_s[model] = n_s[model];
105  }
106 
107  // read the file of benchmark data
108  read_halomod_test_file(data->k, data->Pk);
109 
110 }
111 
112 // Function to actually do the comparison
113 static void compare_halomod(int model, struct halomod_data * data)
114 {
115 
116  // Status variables
117  int stat = 0;
118  int* status = &stat;
119 
120  // Set the cosmology
121  ccl_parameters params = ccl_parameters_create(data->Omega_c[model], data->Omega_b[model],data->Omega_k,
122  data->Neff, data->mnu, data->mnu_type, data->w_0,
123  data->w_a, data->h[model],data->sigma_8[model], data->n_s[model],
124  -1, -1, -1, -1, NULL, NULL, status);
125 
126  // Set the default configuration, but with Eisenstein & Hu linear P(k) and Sheth & Tormen mass function and Duffy (2008) halo concentrations
131 
132  // Set the configuration
133  ccl_cosmology * cosmo = ccl_cosmology_create(params, config);
134 
135  // Check that the cosmology is assigned correctly
136  ASSERT_NOT_NULL(cosmo);
137 
138  // Loop over redshifts
139  for (int i=0; i<2; i++) {
140 
141  // Variables for the test
142  double a;
143 
144  if(i==0){a = 1.0;}
145  if(i==1){a = 0.5;}
146 
147  // Loop over wavenumbers
148  for (int j=0; j<NUMK; j++) {
149 
150  // Set variables inside loop, convert CCL outputs to the same units as benchmark
151  double k = data->k[model][i][j]*params.h; // Convert the benchmark data k/h to pure k
152  double Pk = data->Pk[model][i][j]/pow(params.h,3); // Convert the benchmark data Pk units to remove factors of h
153 
154  double Pk_ccl = ccl_halomodel_matter_power(cosmo, k, a, status); // Get CCL P(k)
155  double absolute_tolerance = HALOMOD_TOLERANCE*Pk; // Convert relative -> absolute tolerance
156 
157  // Do the check
158  ASSERT_DBL_NEAR_TOL(Pk, Pk_ccl, absolute_tolerance);
159 
160  }
161 
162  }
163 
164  // Free the cosmology object
165  free(cosmo);
166 
167 }
168 
169 CTEST2(halomod, model_1) {
170  int model = 0;
171  compare_halomod(model, data);
172 }
173 
174 CTEST2(halomod, model_2) {
175  int model = 1;
176  compare_halomod(model, data);
177 }
178 
179 CTEST2(halomod, model_3) {
180  int model = 2;
181  compare_halomod(model, data);
182 }
183 
double ccl_halomodel_matter_power(ccl_cosmology *cosmo, double k, double a, int *status)
Definition: ccl_halomod.c:314
#define NUMK
halo_concentration_t halo_concentration_method
Definition: ccl_config.h:115
ccl_mnu_convention mnu_type
double Omega_c[3]
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 read_halomod_test_file(double k[3][2][256], double Pk[3][2][256])
static void compare_halomod(int model, struct halomod_data *data)
#define HALOMOD_TOLERANCE
double sigma_8[3]
double h
Definition: ccl_core.h:33
double Omega_b[3]
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
ccl_mnu_convention
Definition: ccl_core.h:142
dictionary params
Definition: halomod_bm.py:27
#define ASSERT_DBL_NEAR_TOL(exp, real, tol)
Definition: ctest.h:152
double k[3][2][256]
double Pk[3][2][256]
mass_function_t mass_function_method
Definition: ccl_config.h:114
const ccl_configuration default_config
Definition: ccl_core.c:21
float pow(float base, unsigned long int exp)
Definition: precision.hpp:39
#define ASSERT_EQUAL(exp, real)
Definition: ctest.h:121
#define ASSERT_NOT_NULL(real)
Definition: ctest.h:139
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