MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
test_cosmology.c
Go to the documentation of this file.
1 /*Tests for the cosmology module, ported from N-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 <gsl/gsl_sf_hyperg.h>
12 #include <libgadget/physconst.h>
13 #include <libgadget/cosmology.h>
14 #include "stub.h"
15 
16 /*Neutrinos are tested elsewhere*/
17 void init_omega_nu(_omega_nu * const omnu, const double MNu[], const double a0, const double HubbleParam, const double tcmb0) {}
18 
19 double get_omega_nu(const _omega_nu * omnu, const double a)
20 {
21  return 0;
22 }
23 double get_omega_nu_nopart(const _omega_nu * omnu, const double a)
24 {
25  return 0;
26 }
27 
28 void init_hybrid_nu(_hybrid_nu * const hybnu, const double mnu[], const double vcrit, const double light, const double nu_crit_time, const double kBtnu){ }
29 
30 void setup_cosmology(Cosmology * CP, double Omega0, double OmegaBaryon, double H0)
31 {
32  CP->CMBTemperature = 2.7255;
33  CP->Omega0 = Omega0;
34  CP->OmegaLambda = 1- CP->Omega0;
35  CP->OmegaBaryon = OmegaBaryon;
36  CP->HubbleParam = H0;
37  CP->RadiationOn = 1;
38  CP->Omega_fld = 0; /*Energy density of dark energy fluid at z=0*/
39  CP->w0_fld = -1; /*Dark energy equation of state parameter*/
40  CP->wa_fld = 0; /*Dark energy equation of state evolution parameter*/
41  CP->Omega_ur = 0;
42  CP->MNu[0] = CP->MNu[1] = CP->MNu[2] = 0;
43  struct UnitSystem units = get_unitsystem(3.085678e21, 1.989e43, 1e5);
44  /*Do the main cosmology initialisation*/
45  init_cosmology(CP,0.01, units);
46 }
47 
48 static inline double radgrow(double aa, double omegar) {
49  return omegar + 1.5 * 1. * aa;
50 }
51 
52 //Omega_L + Omega_M = 1 => D+ ~ Gauss hypergeometric function
53 static inline double growth(double aa, double omegam) {
54  double omegal = 1-omegam;
55  return aa * gsl_sf_hyperg_2F1(1./3, 1, 11./6, -omegal/omegam*pow(aa,3));
56 }
57 
58 static void test_cosmology(void ** state)
59 {
60  Cosmology CP = {0};
61  //Check that we get the right scalings for total matter domination.
62  //Cosmology(double HubbleParam, double Omega, double OmegaLambda, double MNu, int Hierarchy, bool NoRadiation)
63  setup_cosmology(&CP, 1., 0.0455, 0.7);
64  /*Check some of the setup worked*/
65  assert_true(CP.OmegaK < 1e-5);
66  assert_true(fabs(CP.OmegaG/5.045e-5 - 1) < 2e-3);
67  /*Check the hubble function is sane*/
68  CP.RadiationOn = 0;
69  assert_true(fabs(hubble_function(&CP, 1) - CP.Hubble) < 1e-5);
70  CP.RadiationOn = 1;
71  assert_true(fabs(hubble_function(&CP, 1) - CP.Hubble* sqrt(1+CP.OmegaG)) < 1e-7);
72 
73  assert_true(fabs(CP.Hubble - 0.1) < 1e-6);
74  assert_true((hubble_function(&CP, 1) - CP.Hubble) < 1e-5);
75  assert_true(fabs(hubble_function(&CP, 0.1) - hubble_function(&CP, 1)/pow(0.1,3/2.)) < 1e-2);
76  assert_true(fabs(GrowthFactor(&CP, 0.5,1.)/0.5 -1) < 2e-4);
77  //Check that the velocity correction d ln D1/d lna is constant
78  assert_true(fabs(1.0 - F_Omega(&CP, 1.5)) < 1e-1);
79  assert_true(fabs(1.0 - F_Omega(&CP, 2)) < 1e-2);
80  //Check radiation against exact solution from gr-qc/0504089
81  assert_true(fabs(1/GrowthFactor(&CP, 0.05,1.) - radgrow(1., CP.OmegaG)/radgrow(0.05, CP.OmegaG))< 1e-3);
82  assert_true(fabs(GrowthFactor(&CP, 0.01,0.001) - radgrow(0.01, CP.OmegaG)/radgrow(0.001, CP.OmegaG))< 1e-3);
83 
84  //Check against exact solutions from gr-qc/0504089: No radiation!
85  //Note that the GSL hyperg needs the last argument to be < 1
86  double omegam = 0.5;
87  setup_cosmology(&CP, omegam, 0.0455, 0.7);
88  CP.RadiationOn = 0;
89  //Check growth factor during matter domination
90  assert_true(fabs(1/GrowthFactor(&CP, 0.5, 1.) - growth(1., omegam)/growth(0.5, omegam)) < 1e-3);
91  assert_true(fabs(GrowthFactor(&CP, 0.3, 0.15) - growth(0.3, omegam)/growth(0.15, omegam)) < 1e-3);
92  assert_true(fabs(1/GrowthFactor(&CP, 0.01, 1.) - growth(1, omegam)/growth(0.01, omegam)) < 1e-3);
93  assert_true(fabs(0.01*log(GrowthFactor(&CP, 0.01+1e-5,0.01-1e-5))/2e-5 - F_Omega(&CP, 0.01)) < 1e-3);
94 }
95 
96 int main(void) {
97  const struct CMUnitTest tests[] = {
98  cmocka_unit_test(test_cosmology),
99  };
100  return cmocka_run_group_tests_mpi(tests, NULL, NULL);
101 }
double GrowthFactor(Cosmology *CP, double astart, double aend)
Definition: cosmology.c:82
double hubble_function(const Cosmology *CP, double a)
Definition: cosmology.c:58
void init_cosmology(Cosmology *CP, const double TimeBegin, const struct UnitSystem units)
Definition: cosmology.c:15
double F_Omega(Cosmology *CP, double a)
Definition: cosmology.c:148
static Cosmology * CP
Definition: power.c:27
double wa_fld
Definition: cosmology.h:17
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 OmegaK
Definition: cosmology.h:13
double OmegaLambda
Definition: cosmology.h:14
double OmegaBaryon
Definition: cosmology.h:19
double OmegaG
Definition: cosmology.h:12
double MNu[3]
Definition: cosmology.h:25
double Hubble
Definition: cosmology.h:21
double w0_fld
Definition: cosmology.h:16
void init_omega_nu(_omega_nu *const omnu, const double MNu[], const double a0, const double HubbleParam, const double tcmb0)
static double growth(double aa, double omegam)
double get_omega_nu_nopart(const _omega_nu *omnu, const double a)
void init_hybrid_nu(_hybrid_nu *const hybnu, const double mnu[], const double vcrit, const double light, const double nu_crit_time, const double kBtnu)
double get_omega_nu(const _omega_nu *omnu, const double a)
static void test_cosmology(void **state)
int main(void)
static double radgrow(double aa, double omegar)
void setup_cosmology(Cosmology *CP, double Omega0, double OmegaBaryon, double H0)
struct UnitSystem get_unitsystem(double UnitLength_in_cm, double UnitMass_in_g, double UnitVelocity_in_cm_per_s)
Definition: unitsystem.c:6