MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
test_power.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 "stub.h"
12 #include <libgadget/config.h>
13 #include <libgenic/power.h>
14 #include <libgadget/cosmology.h>
15 
16 struct test_state
17 {
18  struct power_params PowerP;
20 };
21 
22 /*stub*/
23 void _bigfile_utils_create_block_from_c_array(BigFile * bf, void * baseptr, char * name, char * dtype, size_t dims[], ptrdiff_t elsize, int64_t TotNumPart, MPI_Comm comm)
24 {
25  return;
26 }
27 
28 /*Simple test without rescaling*/
29 static void
30 test_read_no_rescale(void ** state)
31 {
32  /*Do setup*/
33  struct power_params PowerP = ((struct test_state *) (*state))->PowerP;
34  Cosmology CP = ((struct test_state *) (*state))->CP;
35  /*Test without rescaling*/
38  /*Test without rescaling*/
39  int nentry = init_powerspectrum(0, 0.01, 3.085678e21, &CP, &PowerP);
40  assert_int_equal(nentry, 347);
41  /*Check that the tabulated power spectrum gives the right answer
42  * First check ranges: these should both be out of range.
43  * Should be the same k as in the file (but /10^3 for Mpc -> kpc)
44  * Note that our PowerSpec function is 2pi^3 larger than that in S-GenIC.*/
45  assert_true(DeltaSpec(9.8e-9, DELTA_TOT) < 2e-30);
46  assert_true(DeltaSpec(300, DELTA_TOT) < 2e-30);
47  //Now check total power: k divided by 10^3,
48  //Conversion for P(k) is 10^9/(2pi)^3
49  assert_true(fabs(pow(DeltaSpec(1.124995061548053968e-02/1e3, DELTA_TOT),2) / 4.745074933325402533/1e9 - 1) < 1e-5);
50  assert_true(fabs(pow(DeltaSpec(1.010157135208153312e+00/1e3, DELTA_TOT),2) / 1.15292e-02/1e9 - 1) < 1e-5);
51  //Check that it gives reasonable results when interpolating
52  int k;
53  for (k = 1; k < 100; k++) {
54  double newk = 0.10022E+01/1e3+ k*(0.10362E+01-0.10022E+01)/1e3/100;
55  assert_true(DeltaSpec(newk,DELTA_TOT) < DeltaSpec(0.10022E+01/1e3,DELTA_TOT));
56  assert_true(DeltaSpec(newk,DELTA_TOT) > DeltaSpec(0.10362E+01/1e3,DELTA_TOT));
57  assert_true(DeltaSpec(newk,DELTA_BAR)/DeltaSpec(0.10362E+01/1e3,DELTA_CDM) < 1);
58  /*Check that the CDM + baryon power is the same as the total power for massless neutrinos*/
59  assert_true(fabs(DeltaSpec(newk,DELTA_CB)/DeltaSpec(newk,DELTA_TOT)-1) < 1e-5);
60  }
61  //Now check transfer functions: ratio of total to species should be ratio of T_s to T_tot squared: large scales where T~ 1
62  //CDM
63  assert_true(fabs(DeltaSpec(2.005305808001081169e-03/1e3,DELTA_CDM)/DeltaSpec(2.005305808001081169e-03/1e3,DELTA_TOT)- 1.193460280018762132e+05/1.193185119820504624e+05) < 1e-5);
64  //Small scales where there are differences
65  //T_tot=0.255697E+06
66  //Baryons
67  assert_true(fabs(DeltaSpec(1.079260830861467901e-01/1e3,DELTA_BAR)/DeltaSpec(1.079260830861467901e-01/1e3,DELTA_CB)- 9.735695830700024089e+03/1.394199788775037632e+04) < 1e-4);
68  //CDM
69  assert_true(fabs(DeltaSpec(1.079260830861467901e-01/1e3,DELTA_CDM)/DeltaSpec(1.079260830861467901e-01/1e3,DELTA_CB)- 1.477251880454670209e+04/1.394199788775037632e+04) < 1e-4);
70 }
71 
72 static void
73 test_growth_numerical(void ** state)
74 {
75  /*Do setup*/
76  struct power_params PowerP = ((struct test_state *) (*state))->PowerP;
77  Cosmology CP = ((struct test_state *) (*state))->CP;
78  /*Test without rescaling*/
81  int nentry = init_powerspectrum(0, 0.01, 3.085678e21, &CP, &PowerP);
82  assert_int_equal(nentry, 347);
83  //Test sub-horizon scales
84  int k, nk = 100;
85  //Smaller scales than BAO
86  double lowk = 0.4;
87  double highk = 10;
88  for (k = 1; k < nk; k++) {
89  double newk = exp(log(lowk) + k*(log(highk) - log(lowk))/nk);
90  newk/=1e3;
91 /* message(1,"k=%g G = %g F = %g G0 = %g\n",newk*1e3,dlogGrowth(newk, DELTA_TOT), F_Omega(0.01),dlogGrowth(newk, 1)); */
92  //Total growth should be very close to F_Omega.
93  assert_true(fabs(dlogGrowth(newk,DELTA_TOT) / (F_Omega(&CP, 0.01) * DeltaSpec(newk, DELTA_TOT)) -1) < 0.01);
94  //Growth of CDM should be lower, growth of baryons should be higher.
95  assert_true(dlogGrowth(newk,DELTA_CDM) < F_Omega(&CP, 0.01) * DeltaSpec(newk, DELTA_CDM));
96  assert_true(fabs(dlogGrowth(newk,DELTA_CDM) / DeltaSpec(newk, DELTA_CDM) - 0.9389) < 0.01);
97  assert_true(dlogGrowth(newk,DELTA_BAR) > 1.318 * DeltaSpec(newk, DELTA_BAR));
98  assert_true(dlogGrowth(newk,DELTA_BAR) < 1.35 * DeltaSpec(newk, DELTA_BAR));
99  }
100  //Test super-horizon scales
101  lowk = 1e-3;
102  highk = 5e-3;
103  for (k = 1; k < nk; k++) {
104  double newk = exp(log(lowk) + k*(log(highk) - log(lowk))/nk);
105  newk/=1e3;
106 /* message(1,"k=%g G = %g F = %g\n",newk*1e3,dlogGrowth(newk, 7), dlogGrowth(newk, 1)); */
107  //Total growth should be around 1.05
108  assert_true(dlogGrowth(newk,DELTA_TOT) < 1.055 * DeltaSpec(newk, DELTA_TOT));
109  assert_true(dlogGrowth(newk,DELTA_TOT) > 1. * DeltaSpec(newk, DELTA_TOT));
110  //CDM and baryons should match total
111  assert_true(fabs(dlogGrowth(newk,DELTA_BAR)/dlogGrowth(newk,DELTA_TOT) -1) < 0.008);
112  assert_true(fabs(dlogGrowth(newk,DELTA_CDM)/dlogGrowth(newk,DELTA_TOT) -1) < 0.008);
113  }
114 }
115 
116 /*Check normalising to a different sigma8 and redshift*/
117 static void
119 {
120  /*Do setup*/
121  struct power_params PowerP = ((struct test_state *) (*state))->PowerP;
122  Cosmology CP = ((struct test_state *) (*state))->CP;
123  /* Test rescaling to an earlier time
124  * (we still use the same z=99 power which should not be rescaled in a real simulation)*/
127  int nentry = init_powerspectrum(0, 0.05, 3.085678e21, &CP, &PowerP);
128  assert_int_equal(nentry, 347);
129  assert_true(fabs(pow(DeltaSpec(1.124995061548053968e-02/1e3, DELTA_TOT),2)* 4 /4.745074933325402533/1e9 - 1) < 1e-2);
130 }
131 
132 
133 static int setup(void ** state)
134 {
135  static struct test_state st = {0};
136  st.PowerP.InputPowerRedshift = -1;
138  st.PowerP.Sigma8 = -1;
139  st.PowerP.FileWithInputSpectrum = GADGET_TESTDATA_ROOT "/examples/class_pk_99.dat";
140  st.PowerP.FileWithTransferFunction = GADGET_TESTDATA_ROOT "/examples/class_tk_99.dat";
141  st.PowerP.WhichSpectrum = 2;
142  st.PowerP.PrimordialIndex = 1.0;
143  st.CP.Omega0 = 0.2814;
144  st.CP.OmegaLambda = 1 - st.CP.Omega0;
145  st.CP.OmegaBaryon = 0.0464;
146  st.CP.HubbleParam = 0.697;
147  st.CP.Omega_fld = 0;
148  st.CP.w0_fld = -1;
149  st.CP.wa_fld = 0;
150  st.CP.CMBTemperature = 2.7255;
151  st.CP.RadiationOn = 1;
152  st.CP.MNu[0] = 0;
153  st.CP.MNu[1] = 0;
154  st.CP.MNu[2] = 0;
155  struct UnitSystem units = get_unitsystem(3.085678e21, 1.989e43, 1e5);
156  init_cosmology(&st.CP, 0.01, units);
157  *state = &st;
158  return 0;
159 }
160 
161 int main(void) {
162  const struct CMUnitTest tests[] = {
163  cmocka_unit_test(test_read_no_rescale),
164  cmocka_unit_test(test_read_rescale_sigma8),
165  cmocka_unit_test(test_growth_numerical)
166  };
167  return cmocka_run_group_tests_mpi(tests, setup, NULL);
168 }
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
const char * name
Definition: densitykernel.c:93
double DeltaSpec(double k, enum TransferType Type)
Definition: power.c:52
int init_powerspectrum(int ThisTask, double InitTime, double UnitLength_in_cm_in, Cosmology *CPin, struct power_params *ppar)
Definition: power.c:394
double dlogGrowth(double kmag, enum TransferType Type)
Definition: power.c:101
static Cosmology * CP
Definition: power.c:27
@ DELTA_CB
Definition: power.h:45
@ DELTA_BAR
Definition: power.h:42
@ DELTA_TOT
Definition: power.h:52
@ DELTA_CDM
Definition: power.h:43
double wa_fld
Definition: cosmology.h:17
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 w0_fld
Definition: cosmology.h:16
char * FileWithTransferFunction
Definition: power.h:12
int DifferentTransferFunctions
Definition: power.h:10
double PrimordialIndex
Definition: power.h:16
int WhichSpectrum
Definition: power.h:9
double Sigma8
Definition: power.h:14
char * FileWithInputSpectrum
Definition: power.h:13
double InputPowerRedshift
Definition: power.h:15
Cosmology CP
Definition: test_power.c:19
struct power_params PowerP
Definition: test_power.c:18
int TotNumPart
Definition: test_exchange.c:24
static void test_growth_numerical(void **state)
Definition: test_power.c:73
static void test_read_rescale_sigma8(void **state)
Definition: test_power.c:118
int main(void)
Definition: test_power.c:161
void _bigfile_utils_create_block_from_c_array(BigFile *bf, void *baseptr, char *name, char *dtype, size_t dims[], ptrdiff_t elsize, int64_t TotNumPart, MPI_Comm comm)
Definition: test_power.c:23
static int setup(void **state)
Definition: test_power.c:133
static void test_read_no_rescale(void **state)
Definition: test_power.c:30
struct UnitSystem get_unitsystem(double UnitLength_in_cm, double UnitMass_in_g, double UnitVelocity_in_cm_per_s)
Definition: unitsystem.c:6