MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
test_neutrinos_lra.c
Go to the documentation of this file.
1 #include <stdarg.h>
2 #include <stddef.h>
3 #include <setjmp.h>
4 #include <cmocka.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <math.h>
8 #include "../neutrinos_lra.h"
9 #include "../omega_nu_single.h"
10 #include "../physconst.h"
11 #include "../utils/endrun.h"
12 #include "stub.h"
13 
14 #define T_CMB0 2.7255
16 double specialJ(const double x, const double vcmnubylight, const double nufrac_low);
17 double fslength(Cosmology * CP, const double logai, const double logaf, const double light);
18 
19 void petaio_save_block(BigFile * bf, char * blockname, BigArray * array, int verbose) {};
20 int petaio_read_block(BigFile * bf, char * blockname, BigArray * array, int required)
21 {
22  return 0;
23 }
24 
25 
27 
28 
29 void setup_cosmology(Cosmology * CP, double MNu[])
30 {
31  CP->CMBTemperature = 2.7255;
32  CP->Omega0 = 0.2793;
33  CP->OmegaLambda = 1- CP->Omega0;
34  CP->OmegaBaryon = 0.0464;
35  CP->HubbleParam = 0.7;
36  CP->RadiationOn = 1;
37  CP->Omega_fld = 0;
38  CP->Omega_ur = 0;
39  int i;
40  for(i = 0; i<3; i++)
41  CP->MNu[i] = MNu[i];
42  struct UnitSystem units = get_unitsystem(3.085678e21, 1.989e43, 1e5);
43  /*Do the main cosmology initialisation*/
44  init_cosmology(CP,0.01, units);
45 }
46 
47 /* Test that the allocations are done correctly.
48  * delta_tot is still empty (but allocated) after this.*/
49 static void test_allocate_delta_tot_table(void **state)
50 {
51  _omega_nu omnu;
52  double MNu[3] = {0, 0, 0};
53  int i;
54  init_omega_nu(&omnu, MNu, 0.01, 0.7,T_CMB0);
55  init_neutrinos_lra(300, 0.01, 1, 0.2793, &omnu, 1, 1);
56  assert_true(delta_tot_table.ia == 0);
57  assert_true(delta_tot_table.namax > 10);
58  assert_true(delta_tot_table.scalefact);
59  assert_true(delta_tot_table.delta_nu_init);
60  assert_true(delta_tot_table.delta_tot);
61  for(i=0; i<delta_tot_table.nk_allocated; i++){
62  assert_true(delta_tot_table.delta_tot[i]);
63  }
64 }
65 
66 /*Check that the fits to the special J function are accurate, by doing explicit integration.*/
67 static void test_specialJ(void **state)
68 {
69  /*Check against mathematica computed values:
70  Integrate[(Sinc[q*x])*(q^2/(Exp[q] + 1)), {q, 0, Infinity}]*/
71  assert_true(specialJ(0,-1,0) == 1);
72  assert_true(fabs(specialJ(1,-1, 0) - 0.2117) < 1e-3);
73  assert_true(fabs(specialJ(2,-1, 0) - 0.0223807) < 1e-3);
74  assert_true(fabs(specialJ(0.5,-1, 0) - 0.614729) < 1e-3);
75  assert_true(fabs(specialJ(0.3,-1, 0) - 0.829763) < 1e-3);
76  /*Test that it is ok when truncated*/
77  /*Mathematica: Jfrac[x_, qc_] := NIntegrate[(Sinc[q*x])*(q^2/(Exp[q] + 1)), {q, qc, Infinity}]/(3*Zeta[3]/2) */
78  assert_true(fabs(specialJ(0,1, 0) - 0.940437) < 1e-4);
79  assert_true(fabs(specialJ(0.5,1e-2, 0.5) - 0.614729/0.5) < 1e-3);
80  assert_true(fabs(specialJ(0.5,1, 0.5) - 0.556557/0.5) < 1e-4);
81  assert_true(fabs(specialJ(1,0.1, 0.5) - 0.211662/0.5) < 1e-4);
82 }
83 
84 /* Check that we accurately work out the free-streaming length.
85  * Free-streaming length for a non-relativistic particle of momentum q = T0, from scale factor ai to af.
86  * The 'light' argument defines the units.
87  * Test values use the following mathematica snippet:
88  kB = 8.61734*10^(-5);
89  Tnu = 2.7255*(4/11.)^(1/3.)*1.00328;
90  omegar = 5.04672*10^(-5);
91  Hubble[a_] := 3.085678*10^(21)/10^5*3.24077929*10^(-18)*Sqrt[0.2793/a^3 + (1 - 0.2793) + omegar/a^4]
92  fs[a_, Mnu_] := kB*Tnu/(a*Mnu)/(a*Hubble[a])
93  fslength[ai_, af_, Mnu_] := 299792*NIntegrate[fs[Exp[loga], Mnu], {loga, Log[ai], Log[af]}]
94  */
95 static void test_fslength(void **state)
96 {
97  Cosmology CP;
98  double MNu[3] = {0.15, 0.15, 0.15};
99  setup_cosmology(&CP, MNu);
100  /*Note that MNu is the mass of a single neutrino species:
101  *we use large masses so that we don't have to compute omega_nu in mathematica.*/
102  double kT = BOLEVK*TNUCMB*T_CMB0;
103  /*fslength function returns fslength * (MNu / kT)*/
104  assert_true(fabs(fslength(&CP, log(0.5), log(1), 299792.)/ 1272.92/(0.45/kT) -1 ) < 1e-5);
105  double MNu2[3] = {0.2, 0.2, 0.2};
106  setup_cosmology(&CP, MNu2);
107  assert_true(fabs(fslength(&CP, log(0.1), log(0.5),299792.)/ 5427.8/(0.6/kT) -1 ) < 1e-5);
108 }
109 
110 int main(void) {
111  const struct CMUnitTest tests[] = {
112  cmocka_unit_test(test_allocate_delta_tot_table),
113  cmocka_unit_test(test_specialJ),
114  cmocka_unit_test(test_fslength),
115  };
116  return cmocka_run_group_tests_mpi(tests, NULL, NULL);
117 }
void init_cosmology(Cosmology *CP, const double TimeBegin, const struct UnitSystem units)
Definition: cosmology.c:15
void init_neutrinos_lra(const int nk_in, const double TimeTransfer, const double TimeMax, const double Omega0, const _omega_nu *const omnu, const double UnitTime_in_s, const double UnitLength_in_cm)
void init_omega_nu(_omega_nu *omnu, const double MNu[], const double a0, const double HubbleParam, const double tcmb0)
double nufrac_low(const double qc)
#define TNUCMB
#define BOLEVK
Definition: physconst.h:13
static Cosmology * CP
Definition: power.c:27
double Omega_ur
Definition: cosmology.h:18
double Omega_fld
Definition: cosmology.h:15
double Omega0
Definition: cosmology.h:10
int RadiationOn
Definition: cosmology.h:23
double HubbleParam
Definition: cosmology.h:20
double CMBTemperature
Definition: cosmology.h:9
double OmegaLambda
Definition: cosmology.h:14
double OmegaBaryon
Definition: cosmology.h:19
double MNu[3]
Definition: cosmology.h:25
double ** delta_tot
Definition: neutrinos_lra.h:27
double * scalefact
Definition: neutrinos_lra.h:29
double * delta_nu_init
Definition: neutrinos_lra.h:31
int petaio_read_block(BigFile *bf, char *blockname, BigArray *array, int required)
void setup_cosmology(Cosmology *CP, double MNu[])
double fslength(Cosmology *CP, const double logai, const double logaf, const double light)
void petaio_save_block(BigFile *bf, char *blockname, BigArray *array, int verbose)
#define T_CMB0
int main(void)
static void test_fslength(void **state)
_delta_tot_table delta_tot_table
Definition: neutrinos_lra.c:75
static void test_specialJ(void **state)
double specialJ(const double x, const double vcmnubylight, const double nufrac_low)
static void test_allocate_delta_tot_table(void **state)
struct UnitSystem get_unitsystem(double UnitLength_in_cm, double UnitMass_in_g, double UnitVelocity_in_cm_per_s)
Definition: unitsystem.c:6