MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
test_powerspectrum.c
Go to the documentation of this file.
1 /* This file tests the power spectrum routines only.
2  * The actual PM code is too complicated for now. */
3 #include <stdarg.h>
4 #include <stddef.h>
5 #include <setjmp.h>
6 #include <cmocka.h>
7 #include <stdio.h>
8 #include <stdint.h>
9 #include <stdlib.h>
10 #include <omp.h>
11 #include <math.h>
12 #include <mpi.h>
13 #include "stub.h"
14 #include <bigfile-mpi.h>
15 
17 
18 #define NUM_THREADS 4
19 
20 /*Test the total powerspectrum on one processor only*/
21 static void test_total_powerspectrum(void **state) {
22  (void) state;
23  /*Check allocation*/
24  int nmpi;
25  Power PowerSpectrum;
26  MPI_Comm_size(MPI_COMM_WORLD, &nmpi);
27 
28  powerspectrum_alloc(&PowerSpectrum,15,NUM_THREADS, 0, 3.085678e24);
29  assert_true(PowerSpectrum.Nmodes);
30  assert_true(PowerSpectrum.Power);
31  assert_true(PowerSpectrum.kk);
32  powerspectrum_zero(&PowerSpectrum);
33  assert_true(PowerSpectrum.Nmodes[0] == 0);
34  assert_true(PowerSpectrum.Nmodes[PowerSpectrum.size-1] == 0);
35 
36  //Construct input power (this would be done by the power spectrum routine in petapm)
37  int ii, th;
38  for(ii=0; ii<15; ii++) {
39  for(th = 0; th < NUM_THREADS; th++) {
40  PowerSpectrum.Nmodes[ii+PowerSpectrum.size*th] = ii;
41  PowerSpectrum.Power[ii+PowerSpectrum.size*th] = ii*sin(ii)*sin(ii);
42  PowerSpectrum.kk[ii+PowerSpectrum.size*th] = ii*ii;
43  }
44  }
45  PowerSpectrum.Norm = 1;
46  /*Now every thread and every MPI has the same data. Sum it.*/
47  powerspectrum_sum(&PowerSpectrum);
48 
49  /*Check summation was done correctly*/
50  assert_true(PowerSpectrum.Nmodes[0] == NUM_THREADS*nmpi);
51  assert_true(PowerSpectrum.Nmodes[13] == NUM_THREADS*nmpi*14);
52 
53  assert_true(fabs(PowerSpectrum.Power[0] - sin(1)*sin(1)) < 1e-5);
54  assert_true(fabs(PowerSpectrum.Power[12] - sin(13)*sin(13)) < 1e-5);
55  assert_true(fabs(PowerSpectrum.kk[12] - 2 * M_PI *13) < 1e-5);
56  assert_true(fabs(PowerSpectrum.kk[0] - 2 * M_PI ) < 1e-5);
57 
58 }
59 
60 int main(void)
61 {
62  const struct CMUnitTest tests[] = {
63  cmocka_unit_test(test_total_powerspectrum),
64  };
65  return cmocka_run_group_tests_mpi(tests, NULL, NULL);
66 }
void powerspectrum_sum(Power *ps)
Definition: powerspectrum.c:54
void powerspectrum_zero(Power *ps)
Definition: powerspectrum.c:35
void powerspectrum_alloc(Power *ps, const int nbins, const int nthreads, const int MassiveNuLinResp, const double BoxSize_in_cm)
Definition: powerspectrum.c:16
double * Power
Definition: powerspectrum.h:10
int64_t * Nmodes
Definition: powerspectrum.h:11
static void test_total_powerspectrum(void **state)
#define NUM_THREADS
int main(void)