MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
test_thermal.c
Go to the documentation of this file.
1 /*Tests for the thermal velocity module, ported from S-GenIC.*/
2 
3 #include <stdarg.h>
4 #include <stddef.h>
5 #include <setjmp.h>
6 #include <cmocka.h>
7 #include <math.h>
8 #include <stdio.h>
9 #include <stdint.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include "stub.h"
13 #include <libgadget/config.h>
14 #include <libgenic/thermal.h>
15 
16 /*Check that the neutrino velocity NU_V0 is sensible*/
17 static void
18 test_mean_velocity(void ** state)
19 {
20  /*Check has units of velocity*/
21  assert_true(fabs(NU_V0(1, 1, 1e3) - 100*NU_V0(1, 1, 1e5)) < 1e-6);
22  /*Check scales linearly with neutrino mass*/
23  assert_true(fabs(10*NU_V0(1, 0.1, 1e5) - NU_V0(1, 1, 1e5)) < 1e-6);
24  /*Check scales as z (gadget's cosmological velocity unit is accounted for outside)*/
25  assert_true(fabs(0.5*NU_V0(0.5, 1, 1e5) - NU_V0(1, 1, 1e5)) < 1e-6);
26 }
27 
28 static void
29 test_thermal_vel(void ** state)
30 {
31  /*Seed table with velocity of 100 km/s*/
32  struct thermalvel nu_vels;
33  init_thermalvel(&nu_vels, 100, 5000/100, 0);
34 
35  /*Test getting the distribution*/
36  assert_true(fabs(nu_vels.fermi_dirac_vel[0]) < 1e-6);
37  assert_true(fabs(nu_vels.fermi_dirac_vel[LENGTH_FERMI_DIRAC_TABLE - 1] - MAX_FERMI_DIRAC) < 1e-3);
38 
39  /*Number verified by mathematica*/
40  int ii = 0;
41  while(nu_vels.fermi_dirac_cumprob[ii] < 0.5) {
42  ii++;
43  }
44  assert_true(fabs(nu_vels.fermi_dirac_vel[ii] - 2.839075) < 0.002);
45  /*Check some statistical properties (max, min, mean)*/
46  double mean = 0;
47  double max = 0;
48  double min = 1e10;
49  int nsample;
50  float Vel[3] = {0};
51  int64_t MaxID = 100000;
52  gsl_rng * g_rng = gsl_rng_alloc(gsl_rng_ranlxd1);
53  for (nsample=0; nsample < MaxID; nsample++)
54  {
55  add_thermal_speeds(&nu_vels, g_rng, Vel);
56  double v2 = sqrt(Vel[0]*Vel[0]+Vel[1]*Vel[1]+Vel[2]*Vel[2]);
57  if(v2 > max)
58  max = v2;
59  if(v2 < min)
60  min = v2;
61  mean+=v2;
62  memset(Vel, 0, 3*sizeof(float));
63  }
64  gsl_rng_free(g_rng);
65  mean/=nsample;
66  /*Mean should be roughly 3*zeta(4)/zeta(3)*7/8/(3/4)* m_vamp*/
67  assert_true(fabs(mean - 3*pow(M_PI,4)/90./1.202057*(7./8)/(3/4.)*100) < 1);
68  assert_true(min > 0);
69  assert_true( max < MAX_FERMI_DIRAC*100);
70 }
71 
72 int main(void) {
73  const struct CMUnitTest tests[] = {
74  cmocka_unit_test(test_mean_velocity),
75  cmocka_unit_test(test_thermal_vel)
76  };
77  return cmocka_run_group_tests_mpi(tests, NULL, NULL);
78 }
double fermi_dirac_cumprob[LENGTH_FERMI_DIRAC_TABLE]
Definition: thermal.h:13
double fermi_dirac_vel[LENGTH_FERMI_DIRAC_TABLE]
Definition: thermal.h:12
int main(void)
Definition: test_thermal.c:72
static void test_thermal_vel(void **state)
Definition: test_thermal.c:29
static void test_mean_velocity(void **state)
Definition: test_thermal.c:18
double NU_V0(const double Time, const double kBTNubyMNu, const double UnitVelocity_in_cm_per_s)
Definition: thermal.c:24
double init_thermalvel(struct thermalvel *thermals, const double v_amp, double max_fd, const double min_fd)
Definition: thermal.c:47
void add_thermal_speeds(struct thermalvel *thermals, gsl_rng *g_rng, float Vel[])
Definition: thermal.c:109
#define MAX_FERMI_DIRAC
Definition: thermal.h:7
#define LENGTH_FERMI_DIRAC_TABLE
Definition: thermal.h:8