Fast Methods for Cosmological Simulations
FastSim serves as a tool for quick N-body simulations in modified gravity.
ccl_test_massfunc.c
Go to the documentation of this file.
1 #include "ccl.h"
2 #include "ccl_massfunc.h"
3 #include "ctest.h"
4 #include <stdio.h>
5 #include <math.h>
6 
7 // the tolerance in dn/dm
8 #define SIGMA_TOLERANCE 3e-5
9 #define INVSIGMA_TOLERANCE 1e-3
10 #define MASSFUNC_TOLERANCE 5e-5
11 
12 CTEST_DATA(massfunc) {
13  double Omega_c;
14  double Omega_b;
15  double h;
16  double A_s;
17  double n_s;
18  double Neff;
19  double* mnu;
21  double Omega_v[1];
22  double Omega_k[1];
23  double w_0[1];
24  double w_a[1];
25  double sigma8;
26 
27  double mass[13];
28  double massfunc[3][13];
29 };
30 
31 static void read_massfunc_test_file(double mass[13], double massfunc[3][13])
32 {
33  // Masses are in Msun/h
34  FILE * f = fopen("./tests/benchmark/model1_hmf.txt", "r");
35  ASSERT_NOT_NULL(f);
36 
37  // Ignore header line
38  char str[1024];
39  char* rtn;
40  rtn = fgets(str, 1024, f);
41 
42  // file is in fixed format - logmass, sigma, invsigma, and hmf, w/ 13 rows
43  for (int i=0; i<13; i++) {
44  int count = fscanf(f, "%le %le %le %le\n", &mass[i],
45  &massfunc[0][i], &massfunc[1][i], &massfunc[2][i]);
46  // Check that all the stuff in the benchmark is there
47  ASSERT_EQUAL(4, count);
48  }
49  fclose(f);
50 }
51 
52 // set up the cosmological parameters to be used in the test case
53 CTEST_SETUP(massfunc) {
54  // only single model at tihs point
55  data->Omega_c = 0.25;
56  data->Omega_b = 0.05;
57  data->h = 0.7;
58  data->A_s = 2.1e-9;
59  data->n_s = 0.96;
60  data->sigma8 = 0.8;
61  data->Neff=0;
62  double mnuval = 0.;
63  data->mnu=&mnuval;
64  data->mnu_type = ccl_mnu_sum;
65 
66  double Omega_v[1] = { 0.7 };
67  double w_0[1] = {-1.0 };
68  double w_a[1] = { 0.0 };
69  for (int i=0; i<1; i++) {
70  data->Omega_v[i] = Omega_v[i];
71  data->w_0[i] = w_0[i];
72  data->w_a[i]= w_a[i];
73  data->Omega_k[i] = 1.0 - data->Omega_c - data->Omega_b - data->Omega_v[i];
74  }
75 
76  // read the file of benchmark data
77  read_massfunc_test_file(data->mass, data->massfunc);
78 }
79 
80 static void compare_massfunc(int model, struct massfunc_data * data)
81 {
82  int stat = 0;
83  int* status = &stat;
84 
86  data->Neff, data->mnu, data-> mnu_type, data->w_0[model],
87  data->w_a[model], data->h,data->A_s, data->n_s,
88  -1, -1, -1, -1, NULL, NULL, status);
89 
90  params.sigma8 = data->sigma8;
91  params.Omega_g=0.;
92  params.Omega_l=data->Omega_v[model];
95  // test file generated using tinker 2008 currently
97  ccl_cosmology * cosmo = ccl_cosmology_create(params, config);
98 
99  ASSERT_NOT_NULL(cosmo);
100 
101  double a = 1.0;
102  double logmass = 10;
103  double odelta = 200;
104  double rho_m = RHO_CRITICAL*cosmo->params.Omega_m*cosmo->params.h*cosmo->params.h;
105 
106  // compare to benchmark data
107  for (int j=0; j<13; j++) {
108  double mass = pow(10,logmass);
109  double sigma_j = ccl_sigmaM(cosmo, mass, a, status);
110  double loginvsigma_j = log10(1./sigma_j);
111  double logmassfunc_j = log10(ccl_massfunc(cosmo, mass, a, odelta, status)*mass/(rho_m*log(10.)));
112 
113  double absolute_tolerance = SIGMA_TOLERANCE*data->massfunc[0][j];
114  if (fabs(absolute_tolerance)<1e-12) absolute_tolerance = 1e-12;
115  ASSERT_DBL_NEAR_TOL(data->massfunc[0][j], sigma_j, absolute_tolerance);
116 
117  absolute_tolerance = INVSIGMA_TOLERANCE*fabs(data->massfunc[1][j]);
118  if (fabs(absolute_tolerance)<1e-12) absolute_tolerance = 1e-12;
119  ASSERT_DBL_NEAR_TOL(fabs(data->massfunc[1][j]), fabs(loginvsigma_j), absolute_tolerance);
120 
121  absolute_tolerance = MASSFUNC_TOLERANCE*fabs(data->massfunc[2][j]);
122  if (fabs(absolute_tolerance)<1e-12) absolute_tolerance = 1e-12;
123  ASSERT_DBL_NEAR_TOL(fabs(data->massfunc[2][j]), fabs(logmassfunc_j), absolute_tolerance);
124 
125  logmass += 0.5;
126  }
127  free(cosmo);
128 }
129 
130 CTEST2(massfunc, model_1) {
131  int model = 0;
132  compare_massfunc(model, data);
133 }
double Omega_g
Definition: ccl_core.h:53
ccl_parameters params
Definition: ccl_core.h:124
size_t count(InputIterator first, InputIterator last, T const &item)
Definition: catch.hpp:2747
double ccl_massfunc(ccl_cosmology *cosmo, double smooth_mass, double a, double odelta, int *status)
Definition: ccl_massfunc.c:562
#define CTEST_SETUP(sname)
Definition: ctest.h:73
#define CTEST_DATA(sname)
Definition: ctest.h:71
double h
Definition: ccl_core.h:33
double Omega_l
Definition: ccl_core.h:63
double ccl_sigmaM(ccl_cosmology *cosmo, double smooth_mass, double a, int *status)
Definition: ccl_massfunc.c:624
transfer_function_t transfer_function_method
Definition: ccl_config.h:111
#define MASSFUNC_TOLERANCE
ccl_cosmology * ccl_cosmology_create(ccl_parameters params, ccl_configuration config)
Definition: ccl_core.c:173
ccl_mnu_convention
Definition: ccl_core.h:142
double massfunc[3][13]
dictionary params
Definition: halomod_bm.py:27
#define ASSERT_DBL_NEAR_TOL(exp, real, tol)
Definition: ctest.h:152
double sigma8
Definition: ccl_core.h:62
ccl_mnu_convention mnu_type
static void read_massfunc_test_file(double mass[13], double massfunc[3][13])
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
#define SIGMA_TOLERANCE
#define INVSIGMA_TOLERANCE
double Omega_m
Definition: ccl_core.h:21
#define RHO_CRITICAL
Definition: ccl_constants.h:61
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
static void compare_massfunc(int model, struct massfunc_data *data)