fof_findgroups.c 21.4 KB
Newer Older
Rainer Weinberger's avatar
Rainer Weinberger committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*!
 * \copyright   This file is part of the public version of the AREPO code.
 * \copyright   Copyright (C) 2009-2019, Max-Planck Institute for Astrophysics
 * \copyright   Developed by Volker Springel (vspringel@MPA-Garching.MPG.DE) and
 *              contributing authors.
 * \copyright   Arepo is free software: you can redistribute it and/or modify
 *              it under the terms of the GNU General Public License as published by
 *              the Free Software Foundation, either version 3 of the License, or
 *              (at your option) any later version.
 *
 *              Arepo is distributed in the hope that it will be useful,
 *              but WITHOUT ANY WARRANTY; without even the implied warranty of
 *              MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *              GNU General Public License for more details.
 *
 *              A copy of the GNU General Public License is available under
 *              LICENSE as part of this program.  See also
 *              <https://www.gnu.org/licenses/>.
 *
 * \file        src/fof/fof_findgroups.c
 * \date        05/2018
 * \brief       Routine to identify friend of friends groups.
 * \details     contains functions:
 *                static void particle2in(data_in * in, int i, int firstnode)
 *                static void out2particle(data_out * out, int i, int mode)
 *                static void kernel_local(void)
 *                static void kernel_imported(void)
 *                double fof_find_groups(MyIDType * vMinID, int *vHead,
 *                  int *vLen, int *vNext, int *vTail, int *vMinIDTask)
 *                static int fof_find_dmparticles_evaluate(int target,
 *                  int mode, int threadid)
 *                static int fof_treefind_fof_primary(MyDouble searchcenter[3],
 *                  MyFloat hsml, int target, int numnodes, int *firstnode,
 *                  int mode, int threadid)
 *                void fof_check_for_full_nodes_recursive(int no)
 *                int fof_return_a_particle_in_cell_recursive(int no)
 *
 *
 * \par Major modifications and contributions:
 *
 * - DD.MM.YYYY Description
 * - 24.05.2018 Prepared file for public release -- Rainer Weinberger
 */

45
46
47
#include <gsl/gsl_math.h>
#include <inttypes.h>
#include <math.h>
Rainer Weinberger's avatar
Rainer Weinberger committed
48
49
50
51
52
53
54
55
56
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "../main/allvars.h"
#include "../main/proto.h"
57

58
#include "../domain/domain.h"
Rainer Weinberger's avatar
Rainer Weinberger committed
59
#include "../subfind/subfind.h"
60
#include "fof.h"
Rainer Weinberger's avatar
Rainer Weinberger committed
61
62
63
64

#ifdef FOF

static int fof_find_dmparticles_evaluate(int target, int mode, int threadid);
65
66
static int fof_treefind_fof_primary(MyDouble searchcenter[3], MyFloat hsml, int target, int numnodes, int *firstnode, int mode,
                                    int threadid);
Rainer Weinberger's avatar
Rainer Weinberger committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97

static int *Tree_Head;

static MyIDType *MinID;
static int *Head, *Len, *Next, *Tail, *MinIDTask;

/*! \brief Local data structure for collecting particle/cell data that is sent
 *         to other processors if needed. Type called data_in and static
 *         pointers DataIn and DataGet needed by generic_comm_helpers2.
 */
typedef struct
{
  MyDouble Pos[3];

  MyIDType MinID;
  int MinIDTask;

  int Firstnode;
} data_in;

static data_in *DataIn, *DataGet;

/*! \brief Routine that fills the relevant particle/cell data into the input
 *         structure defined above. Needed by generic_comm_helpers2.
 *
 *  \param[out] in Data structure to fill.
 *  \param[in] i Index of particle in P array.
 *  \param[in] firstnode First note of communication.
 *
 *  \return void
 */
98
static void particle2in(data_in *in, int i, int firstnode)
Rainer Weinberger's avatar
Rainer Weinberger committed
99
100
101
102
103
{
  in->Pos[0] = P[i].Pos[0];
  in->Pos[1] = P[i].Pos[1];
  in->Pos[2] = P[i].Pos[2];

104
  in->MinID     = MinID[Head[i]];
Rainer Weinberger's avatar
Rainer Weinberger committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
  in->MinIDTask = MinIDTask[Head[i]];

  in->Firstnode = firstnode;
}

/*! \brief Local data structure that holds results acquired on remote
 *         processors. Type called data_out and static pointers DataResult and
 *         DataOut needed by generic_comm_helpers2.
 */
typedef struct
{
  char link_count_flag;
} data_out;

static data_out *DataResult, *DataOut;

/*! \brief Routine to store or combine result data. Needed by
 *         generic_comm_helpers2.
 *
 *  \param[in] out Data to be moved to appropriate variables in global
 *             particle and cell data arrays (P, SphP,...)
 *  \param[in] i Index of particle in P and SphP arrays
 *  \param[in] mode Mode of function: local particles or information that was
 *             communicated from other tasks and has to be added locally?
 *
 *  \return void
 */
132
static void out2particle(data_out *out, int i, int mode)
Rainer Weinberger's avatar
Rainer Weinberger committed
133
{
134
  if(mode == MODE_LOCAL_PARTICLES) /* initial store */
Rainer Weinberger's avatar
Rainer Weinberger committed
135
136
137
    {
      terminate("here not used");
    }
138
  else /* combine */
Rainer Weinberger's avatar
Rainer Weinberger committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
    {
      if(out->link_count_flag)
        Flags[i].Marked = 1;
    }
}

#include "../utils/generic_comm_helpers2.h"

static int link_across;
static int nprocessed;

/*! \brief Routine that defines what to do with local particles.
 *
 *  Calls the *_evaluate function in MODE_LOCAL_PARTICLES.
 *
 *  \return void
 */
static void kernel_local(void)
{
  int i;
  /* do local particles */
  {
    int j, threadid = get_thread_num();

    for(j = 0; j < NTask; j++)
      Thread[threadid].Exportflag[j] = -1;

    while(1)
      {
        if(Thread[threadid].ExportSpace < MinSpace)
          break;

        i = NextParticle++;

        if(i >= NumPart)
          break;

        if(((1 << P[i].Type) & (FOF_PRIMARY_LINK_TYPES)))
          {
            if(Flags[i].Nonlocal && Flags[i].Changed)
              {
                fof_find_dmparticles_evaluate(i, MODE_LOCAL_PARTICLES, threadid);

                nprocessed++;
              }
          }
      }
  }
}

/*! \brief Routine that defines what to do with imported particles.
 *
 *  Calls the *_evaluate function in MODE_IMPORTED_PARTICLES.
 *
 *  \return void
 */
static void kernel_imported(void)
{
  /* now do the particles that were sent to us */
  int i, cnt = 0;
  {
    int threadid = get_thread_num();

    while(1)
      {
        i = cnt++;

        if(i >= Nimport)
          break;

        link_across += fof_find_dmparticles_evaluate(i, MODE_IMPORTED_PARTICLES, threadid);
      }
  }
}

/*! \brief Links particles to groups.
 *
 *  \param[in, out] vMinID Pointer to MinID array.
 *  \param[in, out] vHead Pointer to Head array.
 *  \param[in, out] vLen Pointer to Len array.
 *  \param[in, out] vNext Pointer to Next array.
 *  \param[in, out] vTail Pointer to Tail array.
 *  \param[in, out] vMinIDTask Pointer to MinIDTask array.
 *
 *  \return Time spent in this function.
 */
225
double fof_find_groups(MyIDType *vMinID, int *vHead, int *vLen, int *vNext, int *vTail, int *vMinIDTask)
Rainer Weinberger's avatar
Rainer Weinberger committed
226
{
227
228
229
230
231
  MinID     = vMinID;
  Head      = vHead;
  Len       = vLen;
  Next      = vNext;
  Tail      = vTail;
Rainer Weinberger's avatar
Rainer Weinberger committed
232
233
234
235
236
237
238
239
240
241
242
243
  MinIDTask = vMinIDTask;

  int i, npart, marked;
  long long totmarked, totnpart;
  long long link_across_tot, ntot;
  double t0, t1, tstart, tend;

  tstart = second();

  mpi_printf("FOF: Start linking particles (presently allocated=%g MB)\n", AllocatedBytes / (1024.0 * 1024.0));

  /* allocate a flag field that is used to mark nodes that are fully inside the linking length */
244
  flag_node_inside_linkinglength = (unsigned char *)mymalloc("flag_node_inside_linkinglength", Tree_MaxNodes * sizeof(unsigned char));
Rainer Weinberger's avatar
Rainer Weinberger committed
245
246
247
  memset(flag_node_inside_linkinglength, 0, Tree_MaxNodes * sizeof(unsigned char));
  flag_node_inside_linkinglength -= Tree_MaxPart;

248
  Flags = (struct bit_flags *)mymalloc("Flags", NumPart * sizeof(struct bit_flags));
Rainer Weinberger's avatar
Rainer Weinberger committed
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276

  generic_set_MaxNexport();

  Tree_Head = mymalloc("Tree_Head", Tree_NumNodes * sizeof(int));
  Tree_Head -= Tree_MaxPart;

  /* allocate buffers to arrange communication */
  generic_alloc_partlist_nodelist_ngblist_threadbufs();

  t0 = second();

  /* first, link only among local particles */
  for(i = 0, marked = 0, npart = 0; i < NumPart; i++)
    {
      if(((1 << P[i].Type) & (FOF_PRIMARY_LINK_TYPES)))
        {
          fof_find_dmparticles_evaluate(i, MODE_LOCAL_NO_EXPORT, 0);

          npart++;

          if(Flags[i].Nonlocal)
            marked++;
        }
    }

  sumup_large_ints(1, &marked, &totmarked);
  sumup_large_ints(1, &npart, &totnpart);
  t1 = second();
277
278
  mpi_printf("FOF: links on local processor done (took %g sec).\nFOF: Marked=%lld out of the %lld primaries which are linked\n",
             timediff(t0, t1), totmarked, totnpart);
Rainer Weinberger's avatar
Rainer Weinberger committed
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296

  generic_free_partlist_nodelist_ngblist_threadbufs();

  t0 = second();
  fof_check_for_full_nodes_recursive(Tree_MaxPart);
  t1 = second();
  mpi_printf("FOF: fully linked nodes determined (took %g sec).\n", timediff(t0, t1));
  mpi_printf("FOF: begin linking across processors (presently allocated=%g MB) \n", AllocatedBytes / (1024.0 * 1024.0));

  for(i = 0; i < NumPart; i++)
    Flags[i].Marked = 1;

  do
    {
      t0 = second();

      for(i = 0; i < NumPart; i++)
        {
297
298
          Flags[i].Changed      = Flags[i].Marked;
          Flags[i].Marked       = 0;
Rainer Weinberger's avatar
Rainer Weinberger committed
299
300
301
          Flags[i].MinIDChanged = 0;
        }

302
      NextParticle = 0; /* begin with this index */
Rainer Weinberger's avatar
Rainer Weinberger committed
303
304

      link_across = 0;
305
      nprocessed  = 0;
Rainer Weinberger's avatar
Rainer Weinberger committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359

      generic_comm_pattern(NumPart, kernel_local, kernel_imported);

      sumup_large_ints(1, &link_across, &link_across_tot);
      sumup_large_ints(1, &nprocessed, &ntot);

      t1 = second();

      mpi_printf("FOF: have done %15lld cross links (processed %14lld, took %g sec)\n", link_across_tot, ntot, timediff(t0, t1));

      /* let's check out which particles have changed their MinID */
      for(i = 0; i < NumPart; i++)
        if(Flags[i].Nonlocal)
          {
            if(Flags[Head[i]].MinIDChanged)
              Flags[i].Marked = 1;
          }
    }
  while(link_across_tot > 0);

  Tree_Head += Tree_MaxPart;
  myfree(Tree_Head);
  myfree(Flags);
  /* free flag */
  myfree(flag_node_inside_linkinglength + Tree_MaxPart);

  mpi_printf("FOF: Local groups found.\n");

  tend = second();
  return timediff(tstart, tend);
}

/*! \brief Links dark matter particles.
 *
 *  \param[in] target Index of particle/cell.
 *  \param[in] mode Flag if it operates on local or imported data.
 *  \param[in] threadid ID of thread.
 *
 *  \return Number of links.
 */
static int fof_find_dmparticles_evaluate(int target, int mode, int threadid)
{
  int j, n, links, p, s, ss, numnodes, *firstnode;
  int numngb;
  MyDouble *pos;
  data_in local, *target_data;

  links = 0;

  if(mode == MODE_LOCAL_NO_EXPORT || mode == MODE_LOCAL_PARTICLES)
    {
      particle2in(&local, target, 0);
      target_data = &local;

360
      numnodes  = 1;
Rainer Weinberger's avatar
Rainer Weinberger committed
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
      firstnode = NULL;
    }
  else
    {
      target_data = &DataGet[target];

      generic_get_numnodes(target, &numnodes, &firstnode);
    }

  pos = target_data->Pos;

  numngb = fof_treefind_fof_primary(pos, LinkL, target, numnodes, firstnode, mode, threadid);

  if(mode == MODE_LOCAL_PARTICLES || mode == MODE_LOCAL_NO_EXPORT)
    for(n = 0; n < numngb; n++)
      {
        j = Thread[threadid].Ngblist[n];

379
        if(Head[target] != Head[j]) /* only if not yet linked */
Rainer Weinberger's avatar
Rainer Weinberger committed
380
          {
381
            if(Len[Head[target]] > Len[Head[j]]) /* p group is longer */
Rainer Weinberger's avatar
Rainer Weinberger committed
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
              {
                p = target;
                s = j;
              }
            else
              {
                p = j;
                s = target;
              }
            Next[Tail[Head[p]]] = Head[s];

            Tail[Head[p]] = Tail[Head[s]];

            Len[Head[p]] += Len[Head[s]];

            if(MinID[Head[s]] < MinID[Head[p]])
              {
399
                MinID[Head[p]]     = MinID[Head[s]];
Rainer Weinberger's avatar
Rainer Weinberger committed
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
                MinIDTask[Head[p]] = MinIDTask[Head[s]];
              }

            ss = Head[s];
            do
              Head[ss] = Head[p];
            while((ss = Next[ss]) >= 0);
          }
      }

  if(mode == MODE_IMPORTED_PARTICLES)
    {
      if(numngb > 0)
        DataResult[target].link_count_flag = 1;
      else
        DataResult[target].link_count_flag = 0;
    }

  links += numngb;

  return links;
}

/*! \brief Finds the neighbors among the primary link types which are within a
 *         certain distance.
 *
 *  \param[in] searchcenter Position of search center.
 *  \param[in] hsml Search radius.
 *  \param[in] target Index of partcle.
 *  \param[in] numnodes Number of nodes.
 *  \param[in] fistnode First node.
 *  \param[in] mode
 *             -1: only local particles should be found and no export occurs;
 *              0: export occurs, but local particles are ignored;
 *              1: particles are found for an imported point.
 *  \param[in] threadid ID of thread.
 *
 *  \return Number of particles found.
 */
439
440
static int fof_treefind_fof_primary(MyDouble searchcenter[3], MyFloat hsml, int target, int numnodes, int *firstnode, int mode,
                                    int threadid)
Rainer Weinberger's avatar
Rainer Weinberger committed
441
442
443
444
{
  int k, numngb, no, p, nexport_flag = 0;
  MyDouble dx, dy, dz, dist, r2;

445
446
#define FACT2 0.866025403785 /* sqrt(3)/2 */
#define FACT3 (2.0 * FACT2)  /* sqrt(3)   */
Rainer Weinberger's avatar
Rainer Weinberger committed
447
448
449
450
451
452
453
454
455

  MyDouble xtmp, ytmp, ztmp;

  numngb = 0;

  for(k = 0; k < numnodes; k++)
    {
      if(mode == MODE_LOCAL_PARTICLES || mode == MODE_LOCAL_NO_EXPORT)
        {
456
          no = Tree_MaxPart; /* root node */
Rainer Weinberger's avatar
Rainer Weinberger committed
457
458
459
460
        }
      else
        {
          no = firstnode[k];
461
          no = Nodes[no].u.d.nextnode; /* open it */
Rainer Weinberger's avatar
Rainer Weinberger committed
462
463
464
465
466
467
        }

      while(no >= 0)
        {
          if(no < Tree_MaxPart) /* single particle */
            {
468
              p  = no;
Rainer Weinberger's avatar
Rainer Weinberger committed
469
470
471
472
473
474
475
476
477
              no = Nextnode[no];

              if(!((1 << P[p].Type) & (FOF_PRIMARY_LINK_TYPES)))
                continue;

              if(mode == MODE_LOCAL_PARTICLES)
                continue;

              dist = hsml;
478
              dx   = FOF_NEAREST_LONG_X(Tree_Pos_list[3 * p + 0] - searchcenter[0]);
Rainer Weinberger's avatar
Rainer Weinberger committed
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
              if(dx > dist)
                continue;
              dy = FOF_NEAREST_LONG_Y(Tree_Pos_list[3 * p + 1] - searchcenter[1]);
              if(dy > dist)
                continue;
              dz = FOF_NEAREST_LONG_Z(Tree_Pos_list[3 * p + 2] - searchcenter[2]);
              if(dz > dist)
                continue;
              if(dx * dx + dy * dy + dz * dz > dist * dist)
                continue;

              if(mode == MODE_IMPORTED_PARTICLES)
                {
                  if(MinID[Head[p]] > DataGet[target].MinID)
                    {
494
495
                      MinID[Head[p]]              = DataGet[target].MinID;
                      MinIDTask[Head[p]]          = DataGet[target].MinIDTask;
Rainer Weinberger's avatar
Rainer Weinberger committed
496
497
498
499
500
501
502
503
504
505
                      Flags[Head[p]].MinIDChanged = 1;
                      numngb++;
                    }
                }
              else
                {
                  /* this will only be done for MODE_LOCAL_NO_EXPORT */
                  Thread[threadid].Ngblist[numngb++] = p;
                }
            }
506
          else if(no < Tree_MaxPart + Tree_MaxNodes) /* internal node */
Rainer Weinberger's avatar
Rainer Weinberger committed
507
508
509
            {
              if(mode == MODE_IMPORTED_PARTICLES)
                {
510
511
                  if(no <
                     Tree_FirstNonTopLevelNode) /* we reached a top-level node again, which means that we are done with the branch */
Rainer Weinberger's avatar
Rainer Weinberger committed
512
513
514
515
516
                    break;

                  if(Tree_Head[no] >= 0)
                    if(MinID[Tree_Head[no]] <= DataGet[target].MinID)
                      {
517
                        no = Nodes[no].u.d.sibling; /* the node can be discarded */
Rainer Weinberger's avatar
Rainer Weinberger committed
518
519
520
521
522
                        continue;
                      }
                }

              struct NODE *current = &Nodes[no];
523
524
              int nocur            = no;
              no                   = current->u.d.sibling; /* in case the node can be discarded */
Rainer Weinberger's avatar
Rainer Weinberger committed
525
526
527
528
529
530
531
532
533
534
535

              if(mode == MODE_LOCAL_PARTICLES)
                {
                  if(nocur >= Tree_FirstNonTopLevelNode)
                    {
                      /* we have a node with only local particles, hence we can skip it for mode == 0 */
                      continue;
                    }
                }

              dist = hsml + 0.5 * current->len;
536
              dx   = FOF_NEAREST_LONG_X(current->center[0] - searchcenter[0]);
Rainer Weinberger's avatar
Rainer Weinberger committed
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
              if(dx > dist)
                continue;
              dy = FOF_NEAREST_LONG_Y(current->center[1] - searchcenter[1]);
              if(dy > dist)
                continue;
              dz = FOF_NEAREST_LONG_Z(current->center[2] - searchcenter[2]);
              if(dz > dist)
                continue;

              /* now test against the minimal sphere enclosing everything */
              dist += FACT1 * current->len;
              r2 = dx * dx + dy * dy + dz * dz;
              if(r2 > dist * dist)
                continue;

              if(mode != MODE_LOCAL_PARTICLES)
                {
                  /* test whether the node is contained within the sphere */
                  dist = hsml - FACT2 * current->len;
                  if(dist > 0)
                    if(r2 < dist * dist && hsml > FACT3 * current->len)
                      {
                        if(flag_node_inside_linkinglength[nocur] & (1 << BITFLAG_INSIDE_LINKINGLENGTH)) /* already flagged */
                          {
                            /* sufficient to return only one particle inside this cell */
                            p = fof_return_a_particle_in_cell_recursive(nocur);

                            if(p >= 0)
                              {
                                if(mode == MODE_IMPORTED_PARTICLES)
                                  {
                                    if(MinID[Head[p]] > DataGet[target].MinID)
                                      {
570
571
                                        MinID[Head[p]]              = DataGet[target].MinID;
                                        MinIDTask[Head[p]]          = DataGet[target].MinIDTask;
Rainer Weinberger's avatar
Rainer Weinberger committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
                                        Flags[Head[p]].MinIDChanged = 1;
                                        numngb++;
                                      }
                                  }
                                else
                                  Thread[threadid].Ngblist[numngb++] = p;
                              }

                            continue;
                          }
                        else
                          {
                            /* flag it now */
                            flag_node_inside_linkinglength[nocur] |= (1 << BITFLAG_INSIDE_LINKINGLENGTH);
                          }
                      }
                }

590
              no = current->u.d.nextnode; /* ok, we need to open the node */
Rainer Weinberger's avatar
Rainer Weinberger committed
591
            }
592
          else if(no >= Tree_ImportedNodeOffset) /* point from imported nodelist */
Rainer Weinberger's avatar
Rainer Weinberger committed
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
            {
              terminate("do not expect imported points here");
            }
          else
            {
              if(mode == MODE_LOCAL_PARTICLES)
                {
                  if(target >= 0)
                    tree_treefind_export_node_threads(no, target, threadid);
                }
              else if(mode == MODE_LOCAL_NO_EXPORT)
                {
                  nexport_flag = 1;
                }
              else if(mode == MODE_IMPORTED_PARTICLES)
                terminate("stop no=%d Tree_MaxPart=%d Tree_MaxNodes=%d", no, Tree_MaxPart, Tree_MaxNodes);

              no = Nextnode[no - Tree_MaxNodes];
              continue;
            }
        }
    }

  if(mode == MODE_LOCAL_NO_EXPORT)
    {
      if(nexport_flag == 0)
        Flags[target].Nonlocal = 0;
      else
        Flags[target].Nonlocal = 1;
    }

  return numngb;
}

/*! \brief Walks a tree recursively and sets Tree_Head of node.
 *
 *  \param[in] no Index of node we are in.
 *
 *  \return void
 */
void fof_check_for_full_nodes_recursive(int no)
{
635
  if(no >= Tree_MaxPart && no < Tree_MaxPart + Tree_MaxNodes) /* internal node */
Rainer Weinberger's avatar
Rainer Weinberger committed
636
    {
637
      int head = -1; /* no particle yet */
Rainer Weinberger's avatar
Rainer Weinberger committed
638
639
640
641
642

      int p = Nodes[no].u.d.nextnode;

      while(p != Nodes[no].u.d.sibling)
        {
643
          if(p < Tree_MaxPart) /* a particle */
Rainer Weinberger's avatar
Rainer Weinberger committed
644
645
646
647
648
649
650
651
652
653
654
655
656
657
            {
              if((1 << P[p].Type) & (FOF_PRIMARY_LINK_TYPES))
                {
                  if(head == -1)
                    head = Head[p];
                  else if(head >= 0)
                    {
                      if(head != Head[p])
                        head = -2;
                    }
                }

              p = Nextnode[p];
            }
658
          else if(p < Tree_MaxPart + Tree_MaxNodes) /* an internal node  */
Rainer Weinberger's avatar
Rainer Weinberger committed
659
660
661
662
663
664
665
666
667
668
669
670
671
            {
              fof_check_for_full_nodes_recursive(p);

              if(head == -1)
                head = Tree_Head[p];
              else if(head >= 0)
                {
                  if(head != Tree_Head[p])
                    head = -2;
                }

              p = Nodes[p].u.d.sibling;
            }
672
          else /* a pseudo particle */
Rainer Weinberger's avatar
Rainer Weinberger committed
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
            p = Nextnode[p - Tree_MaxNodes];
        }

      Tree_Head[no] = head;
    }
}

/*! \brief Finds a particle in node.
 *
 *  \param[in] no Index of node.
 *
 *  \return Particle index; -1 if no particle was found.
 */
int fof_return_a_particle_in_cell_recursive(int no)
{
688
  if(no >= Tree_MaxPart && no < Tree_MaxPart + Tree_MaxNodes) /* internal node */
Rainer Weinberger's avatar
Rainer Weinberger committed
689
690
691
692
693
    {
      int p = Nodes[no].u.d.nextnode;

      while(p != Nodes[no].u.d.sibling)
        {
694
          if(p < Tree_MaxPart) /* a particle */
Rainer Weinberger's avatar
Rainer Weinberger committed
695
696
697
698
699
700
701
702
            {
              if((1 << P[p].Type) & (FOF_PRIMARY_LINK_TYPES))
                {
                  return p;
                }

              p = Nextnode[p];
            }
703
          else if(p < Tree_MaxPart + Tree_MaxNodes) /* an internal node  */
Rainer Weinberger's avatar
Rainer Weinberger committed
704
705
706
707
708
709
710
711
            {
              int ret = fof_return_a_particle_in_cell_recursive(p);

              if(ret >= 0)
                return ret;

              p = Nodes[p].u.d.sibling;
            }
712
          else /* a pseudo particle */
Rainer Weinberger's avatar
Rainer Weinberger committed
713
714
715
716
717
718
719
720
            p = Nextnode[p - Tree_MaxNodes];
        }
    }

  return -1;
}

#endif /* #ifdef FOF */