MP-Gadget  5.0.1.dev1-76bc7d4726-dirty
test_fof.c
Go to the documentation of this file.
1 /*Simple test for the exchange function*/
2 
3 #include <stdarg.h>
4 #include <stddef.h>
5 #include <setjmp.h>
6 #include <cmocka.h>
7 #include <math.h>
8 #include <mpi.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <time.h>
12 #include <gsl/gsl_rng.h>
13 
14 #define qsort_openmp qsort
15 
16 #include <libgadget/fof.h>
17 #include <libgadget/walltime.h>
18 #include <libgadget/domain.h>
19 #include <libgadget/forcetree.h>
20 #include <libgadget/partmanager.h>
21 #include "stub.h"
22 
23 static struct ClockTable CT;
24 
25 #define NUMPART1 8
26 static int
27 setup_particles(int NumPart, double BoxSize)
28 {
29 
30  int ThisTask;
31  MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);
32 
33  gsl_rng * r = gsl_rng_alloc(gsl_rng_mt19937);
34  gsl_rng_set(r, 0);
35 
36  particle_alloc_memory(PartManager, BoxSize, 1.5 * NumPart);
37  PartManager->NumPart = NumPart;
38 
39  int i;
40  #pragma omp parallel for
41  for(i = 0; i < PartManager->NumPart; i ++) {
42  P[i].ID = i + PartManager->NumPart * ThisTask;
43  /* DM only*/
44  P[i].Type = 1;
45  P[i].Mass = 1;
46  P[i].TimeBin = 0;
47  P[i].IsGarbage = 0;
48  int j;
49  for(j=0; j<3; j++)
50  P[i].Pos[j] = BoxSize * gsl_rng_uniform(r);
51  P[i].Key = PEANO(P[i].Pos, BoxSize);
52 
53  }
54 
55  gsl_rng_free(r);
56  /* TODO: Here create particles in some halo-like configuration*/
57 
58  return 0;
59 }
60 
61 static int
62 teardown_particles(void **state)
63 {
64  myfree(P);
65  MPI_Barrier(MPI_COMM_WORLD);
66  return 0;
67 }
68 
69 
70 static void
71 test_fof(void **state)
72 {
73  walltime_init(&CT);
74 
75  struct DomainParams dp = {0};
78  dp.TopNodeAllocFactor = 1.;
79  dp.SetAsideFactor = 1;
80  set_domain_par(dp);
82 
83  int NumPart = 1024;
84  /* 20000 kpc*/
85  double BoxSize = 20000;
86  setup_particles(NumPart, BoxSize);
87 
88  int i, NTask, ThisTask;
89  /* Build a tree and domain decomposition*/
90  DomainDecomp ddecomp = {0};
91  domain_decompose_full(&ddecomp);
92 
93  FOFGroups fof = fof_fof(&ddecomp, 1, MPI_COMM_WORLD);
94 
95  /* Example assertion: this checks that the address of a struct is not NULL,
96  * which will always pass! */
97  assert_all_true(&fof);
98 
99  MPI_Comm_size(MPI_COMM_WORLD, &NTask);
100  MPI_Comm_rank(MPI_COMM_WORLD, &ThisTask);
101 
102  /* Assert some more things about the particles,
103  * maybe checking the halo properties*/
104  for(i = 0; i < PartManager->NumPart; i ++) {
105  assert_true(P[i].ID % NTask == ThisTask);
106  }
107 
108  fof_finish(&fof);
109 
110  teardown_particles(state);
111  return;
112 }
113 
114 int main(void) {
115  const struct CMUnitTest tests[] = {
116  cmocka_unit_test(test_fof),
117  };
118  return cmocka_run_group_tests_mpi(tests, NULL, NULL);
119 }
void domain_decompose_full(DomainDecomp *ddecomp)
Definition: domain.c:155
void set_domain_par(DomainParams dp)
Definition: domain.c:78
void fof_finish(FOFGroups *fof)
Definition: fof.c:257
FOFGroups fof_fof(DomainDecomp *ddecomp, const int StoreGrNr, MPI_Comm Comm)
Definition: fof.c:151
void init_forcetree_params(const int FastParticleType)
Definition: forcetree.c:43
#define myfree(x)
Definition: mymalloc.h:19
struct part_manager_type PartManager[1]
Definition: partmanager.c:11
void particle_alloc_memory(struct part_manager_type *PartManager, double BoxSize, int64_t MaxPart)
Definition: partmanager.c:14
#define P
Definition: partmanager.h:88
static peano_t PEANO(double *Pos, double BoxSize)
Definition: peano.h:14
int DomainOverDecompositionFactor
Definition: domain.h:53
int DomainUseGlobalSorting
Definition: domain.h:55
double TopNodeAllocFactor
Definition: domain.h:57
double SetAsideFactor
Definition: domain.h:59
Definition: fof.h:62
int ThisTask
Definition: test_exchange.c:23
int NTask
Definition: test_exchange.c:23
static void test_fof(void **state)
Definition: test_fof.c:71
static struct ClockTable CT
Definition: test_fof.c:23
static int setup_particles(int NumPart, double BoxSize)
Definition: test_fof.c:27
static int teardown_particles(void **state)
Definition: test_fof.c:62
int main(void)
Definition: test_fof.c:114
void walltime_init(struct ClockTable *ct)
Definition: walltime.c:19