cell.h 40.6 KB
Newer Older
1
/*******************************************************************************
2
 * This file is part of SWIFT.
3
 * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk)
4
5
6
7
 *                    Matthieu Schaller (matthieu.schaller@durham.ac.uk)
 *               2015 Peter W. Draper (p.w.draper@durham.ac.uk)
 *               2016 John A. Regan (john.a.regan@durham.ac.uk)
 *                    Tom Theuns (tom.theuns@durham.ac.uk)
8
 *
9
10
11
12
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
13
 *
14
15
16
17
 * This program 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.
18
 *
19
20
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 ******************************************************************************/
23
24
#ifndef SWIFT_CELL_H
#define SWIFT_CELL_H
25

26
27
28
/* Config parameters. */
#include "../config.h"

29
/* Includes. */
30
#include <stddef.h>
31
#include <stdint.h>
32
33

/* Local includes. */
34
#include "align.h"
35
#include "kernel_hydro.h"
Pedro Gonnet's avatar
Pedro Gonnet committed
36
#include "lock.h"
37
#include "multipole.h"
38
#include "part.h"
39
#include "sort_part.h"
40
#include "space.h"
Folkert Nobels's avatar
Folkert Nobels committed
41
#include "star_formation_logger_struct.h"
42
#include "task.h"
43
#include "timeline.h"
44

45
/* Avoid cyclic inclusions */
46
struct engine;
47
struct scheduler;
48

49
50
51
/* Max tag size set to 2^29 to take into account some MPI implementations
 * that use 2^31 as the upper bound on MPI tags and the fact that
 * cell_next_tag is multiplied by 2 when passed to an MPI function.
52
53
 * The maximum was lowered by a further factor of 2 to be on the safe side.*/
#define cell_max_tag (1 << 29)
54

55
#define cell_align 128
56

57
58
59
/* Global variables. */
extern int cell_next_tag;

60
61
62
63
/* Struct to temporarily buffer the particle locations and bin id. */
struct cell_buff {
  double x[3];
  int ind;
64
} SWIFT_STRUCT_ALIGN;
65

66
67
68
69
70
71
72
73
74
75
/* Mini struct to link cells to tasks. Used as a linked list. */
struct link {

  /* The task pointer. */
  struct task *t;

  /* The next pointer. */
  struct link *next;
};

76
77
78
79
80
81
82
83
84
85
86
/* Holds the pairs of progeny for each sid. */
struct cell_split_pair {
  int count;
  struct {
    int pid;
    int pjd;
    int sid;
  } pairs[16];
};
extern struct cell_split_pair cell_split_pairs[13];

87
88
89
90
91
/**
 * @brief Packed cell for information correct at rebuild time.
 *
 * Contains all the information for a tree walk in a non-local cell.
 */
92
93
struct pcell {

94
95
  /*! Hydro variables */
  struct {
Matthieu Schaller's avatar
Matthieu Schaller committed
96

97
98
    /*! Maximal smoothing length. */
    double h_max;
99

100
101
    /*! Minimal integer end-of-timestep in this cell for hydro tasks */
    integertime_t ti_end_min;
102

103
104
    /*! Maximal integer end-of-timestep in this cell for hydro tasks */
    integertime_t ti_end_max;
105

106
107
    /*! Maximal integer beginning-of-timestep in this cell for hydro tasks */
    integertime_t ti_beg_max;
108

109
    /*! Integer time of the last drift of the #part in this cell */
110
    integertime_t ti_old_part;
111

112
113
    /*! Number of #part in this cell. */
    int count;
114

115
  } hydro;
116

117
118
  /*! Gravity variables */
  struct {
119

120
121
    /*! This cell's gravity-related tensors */
    struct multipole m_pole;
122

123
124
    /*! Centre of mass. */
    double CoM[3];
125

126
127
    /*! Centre of mass at rebuild time. */
    double CoM_rebuild[3];
128

129
130
    /*! Upper limit of the CoM<->gpart distance. */
    double r_max;
131

132
133
    /*! Upper limit of the CoM<->gpart distance at last rebuild. */
    double r_max_rebuild;
134

135
136
    /*! Minimal integer end-of-timestep in this cell for gravity tasks */
    integertime_t ti_end_min;
Pedro Gonnet's avatar
Pedro Gonnet committed
137

138
139
    /*! Maximal integer end-of-timestep in this cell for gravity tasks */
    integertime_t ti_end_max;
140

141
142
    /*! Maximal integer beginning-of-timestep in this cell for gravity tasks */
    integertime_t ti_beg_max;
143

144
    /*! Integer time of the last drift of the #gpart in this cell */
145
    integertime_t ti_old_part;
146

147
148
    /*! Integer time of the last drift of the #multipole in this cell */
    integertime_t ti_old_multipole;
149

150
    /*! Number of #gpart in this cell. */
151
    int count;
152
153
154

  } grav;

155
156
157
158
159
160
  /*! Stars variables */
  struct {

    /*! Number of #spart in this cell. */
    int count;

Loic Hausammann's avatar
Loic Hausammann committed
161
162
163
    /*! Maximal smoothing length. */
    double h_max;

164
165
166
    /*! Minimal integer end-of-timestep in this cell for stars tasks */
    integertime_t ti_end_min;

167
168
169
    /*! Maximal integer end-of-timestep in this cell for stars tasks */
    integertime_t ti_end_max;

170
171
172
    /*! Integer time of the last drift of the #spart in this cell */
    integertime_t ti_old_part;

173
174
  } stars;

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  /*! Black hole variables */
  struct {

    /*! Number of #spart in this cell. */
    int count;

    /*! Maximal smoothing length. */
    double h_max;

    /*! Minimal integer end-of-timestep in this cell for black hole tasks */
    integertime_t ti_end_min;

    /*! Maximal integer end-of-timestep in this cell for black hole tasks */
    integertime_t ti_end_max;

    /*! Integer time of the last drift of the #spart in this cell */
    integertime_t ti_old_part;

  } black_holes;

195
196
197
  /*! Maximal depth in that part of the tree */
  int maxdepth;

198
199
  /*! Relative indices of the cell's progeny. */
  int progeny[8];
200

201
202
203
204
205
#ifdef SWIFT_DEBUG_CHECKS
  /* Cell ID (for debugging) */
  int cellID;
#endif

206
} SWIFT_STRUCT_ALIGN;
207

208
209
210
/**
 * @brief Cell information at the end of a time-step.
 */
211
struct pcell_step_hydro {
212

213
214
  /*! Minimal integer end-of-timestep in this cell (hydro) */
  integertime_t ti_end_min;
215

216
217
  /*! Minimal integer end-of-timestep in this cell (hydro) */
  integertime_t ti_end_max;
218

219
220
221
  /*! Maximal distance any #part has travelled since last rebuild */
  float dx_max_part;
};
222

223
struct pcell_step_grav {
224

225
226
  /*! Minimal integer end-of-timestep in this cell (gravity) */
  integertime_t ti_end_min;
227

228
229
230
  /*! Minimal integer end-of-timestep in this cell (gravity) */
  integertime_t ti_end_max;
};
Loic Hausammann's avatar
Loic Hausammann committed
231

232
struct pcell_step_stars {
233

234
235
  /*! Minimal integer end-of-timestep in this cell (stars) */
  integertime_t ti_end_min;
236

237
238
  /*! Maximal integer end-of-timestep in this cell (stars) */
  integertime_t ti_end_max;
239

240
241
  /*! Maximal distance any #part has travelled since last rebuild */
  float dx_max_part;
242
243
};

244
245
246
247
248
249
250
251
252
253
254
255
struct pcell_step_black_holes {

  /*! Minimal integer end-of-timestep in this cell (black_holes) */
  integertime_t ti_end_min;

  /*! Maximal integer end-of-timestep in this cell (black_holes) */
  integertime_t ti_end_max;

  /*! Maximal distance any #part has travelled since last rebuild */
  float dx_max_part;
};

Pedro Gonnet's avatar
Pedro Gonnet committed
256
257
/** Bitmasks for the cell flags. Beware when adding flags that you don't exceed
    the size of the flags variable in the struct cell. */
258
259
260
261
262
enum cell_flags {
  cell_flag_split = (1UL << 0),
  cell_flag_do_hydro_drift = (1UL << 1),
  cell_flag_do_hydro_sub_drift = (1UL << 2),
  cell_flag_do_hydro_sub_sort = (1UL << 3),
263
264
  cell_flag_do_hydro_limiter = (1UL << 4),
  cell_flag_do_hydro_sub_limiter = (1UL << 5),
265
266
267
268
269
270
271
272
273
  cell_flag_do_grav_drift = (1UL << 6),
  cell_flag_do_grav_sub_drift = (1UL << 7),
  cell_flag_do_stars_sub_sort = (1UL << 8),
  cell_flag_do_stars_drift = (1UL << 9),
  cell_flag_do_stars_sub_drift = (1UL << 10),
  cell_flag_do_bh_drift = (1UL << 11),
  cell_flag_do_bh_sub_drift = (1UL << 12)
};

274
275
276
277
278
/**
 * @brief Cell within the tree structure.
 *
 * Contains particles, links to tasks, a multipole object and counters.
 */
279
280
struct cell {

281
  /*! The cell location on the grid. */
282
283
  double loc[3];

284
  /*! The cell dimensions. */
285
  double width[3];
286

287
  /*! Pointers to the next level of cells. */
288
289
  struct cell *progeny[8];

290
291
292
  /*! Linking pointer for "memory management". */
  struct cell *next;

293
  /*! Parent cell. */
294
295
  struct cell *parent;

296
297
298
  /*! Pointer to the top-level cell in a hierarchy */
  struct cell *top;

299
  /*! Super cell, i.e. the highest-level parent cell with *any* task */
300
  struct cell *super;
301

302
  /*! Cell flags bit-mask. */
303
  volatile uint32_t flags;
304

305
306
  /*! Hydro variables */
  struct {
307

308
309
    /*! Pointer to the #part data. */
    struct part *parts;
310

311
312
    /*! Pointer to the #xpart data. */
    struct xpart *xparts;
313

314
315
    /*! Pointer for the sorted indices. */
    struct entry *sort[13];
316

317
318
    /*! Super cell, i.e. the highest-level parent cell that has a hydro
     * pair/self tasks */
319
    struct cell *super;
320

321
322
    /*! The task computing this cell's sorts. */
    struct task *sorts;
323

324
325
    /*! The drift task for parts */
    struct task *drift;
326

327
328
329
330
331
332
333
334
335
    /*! Linked list of the tasks computing this cell's hydro density. */
    struct link *density;

    /* Linked list of the tasks computing this cell's hydro gradients. */
    struct link *gradient;

    /*! Linked list of the tasks computing this cell's hydro forces. */
    struct link *force;

336
337
338
    /*! Linked list of the tasks computing this cell's limiter. */
    struct link *limiter;

339
340
341
342
343
344
345
346
347
348
349
350
    /*! Dependency implicit task for the ghost  (in->ghost->out)*/
    struct task *ghost_in;

    /*! Dependency implicit task for the ghost  (in->ghost->out)*/
    struct task *ghost_out;

    /*! The ghost task itself */
    struct task *ghost;

    /*! The extra ghost task for complex hydro schemes */
    struct task *extra_ghost;

351
352
353
    /*! The task to end the force calculation */
    struct task *end_force;

354
355
356
357
358
    /*! Task for cooling */
    struct task *cooling;

    /*! Task for star formation */
    struct task *star_formation;
359

360
361
    /*! Max smoothing length in this cell. */
    double h_max;
362

363
364
365
    /*! Last (integer) time the cell's part were drifted forward in time. */
    integertime_t ti_old_part;

366
367
    /*! Minimum end of (integer) time step in this cell for hydro tasks. */
    integertime_t ti_end_min;
368

369
370
    /*! Maximum end of (integer) time step in this cell for hydro tasks. */
    integertime_t ti_end_max;
371

372
373
    /*! Maximum beginning of (integer) time step in this cell for hydro tasks.
     */
374
    integertime_t ti_beg_max;
375

376
377
    /*! Spin lock for various uses (#part case). */
    swift_lock_type lock;
378

379
380
    /*! Maximum part movement in this cell since last construction. */
    float dx_max_part;
381

382
383
    /*! Maximum particle movement in this cell since the last sort. */
    float dx_max_sort;
384

385
386
    /*! Values of h_max before the drifts, used for sub-cell tasks. */
    float h_max_old;
387

388
389
390
    /*! Values of dx_max before the drifts, used for sub-cell tasks. */
    float dx_max_part_old;

391
392
    /*! Values of dx_max_sort before the drifts, used for sub-cell tasks. */
    float dx_max_sort_old;
393

394
395
396
    /*! Nr of #part in this cell. */
    int count;

397
398
399
    /*! Nr of #part this cell can hold after addition of new #part. */
    int count_total;

400
401
402
403
404
405
406
407
408
    /*! Number of #part updated in this cell. */
    int updated;

    /*! Number of #part inhibited in this cell. */
    int inhibited;

    /*! Is the #part data of this cell being used in a sub-cell? */
    int hold;

409
    /*! Bit mask of sort directions that will be needed in the next timestep. */
410
    uint16_t requires_sorts;
411

412
    /*! Bit mask of sorts that need to be computed for this cell. */
413
    uint16_t do_sort;
414

415
    /*! Bit-mask indicating the sorted directions */
416
    uint16_t sorted;
417

Matthieu Schaller's avatar
Matthieu Schaller committed
418
#ifdef SWIFT_DEBUG_CHECKS
419

420
421
    /*! Last (integer) time the cell's sort arrays were updated. */
    integertime_t ti_sort;
422

423
#endif
424

425
  } hydro;
426

427
428
  /*! Grav variables */
  struct {
429

430
    /*! Pointer to the #gpart data. */
431
    struct gpart *parts;
432

433
434
    /*! This cell's multipole. */
    struct gravity_tensors *multipole;
435

436
437
438
    /*! Super cell, i.e. the highest-level parent cell that has a grav pair/self
     * tasks */
    struct cell *super;
439

440
441
442
    /*! The drift task for gparts */
    struct task *drift;

443
444
445
    /*! Implicit task (going up- and down the tree) for the #gpart drifts */
    struct task *drift_out;

446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
    /*! Linked list of the tasks computing this cell's gravity forces. */
    struct link *grav;

    /*! Linked list of the tasks computing this cell's gravity M-M forces. */
    struct link *mm;

    /*! The multipole initialistation task */
    struct task *init;

    /*! Implicit task for the gravity initialisation */
    struct task *init_out;

    /*! Task computing long range non-periodic gravity interactions */
    struct task *long_range;

    /*! Implicit task for the down propagation */
    struct task *down_in;

    /*! Task propagating the mesh forces to the particles */
    struct task *mesh;

    /*! Task propagating the multipole to the particles */
    struct task *down;

470
471
472
    /*! The task to end the force calculation */
    struct task *end_force;

473
474
    /*! Minimum end of (integer) time step in this cell for gravity tasks. */
    integertime_t ti_end_min;
475

476
477
    /*! Maximum end of (integer) time step in this cell for gravity tasks. */
    integertime_t ti_end_max;
478

479
480
481
    /*! Maximum beginning of (integer) time step in this cell for gravity tasks.
     */
    integertime_t ti_beg_max;
482

483
    /*! Last (integer) time the cell's gpart were drifted forward in time. */
484
    integertime_t ti_old_part;
485

486
487
    /*! Last (integer) time the cell's multipole was drifted forward in time. */
    integertime_t ti_old_multipole;
488

489
    /*! Spin lock for various uses (#gpart case). */
490
    swift_lock_type plock;
491

492
493
    /*! Spin lock for various uses (#multipole case). */
    swift_lock_type mlock;
494

495
496
497
    /*! Nr of #gpart in this cell. */
    int count;

498
499
500
    /*! Nr of #gpart this cell can hold after addition of new #gpart. */
    int count_total;

501
    /*! Number of #gpart updated in this cell. */
502
    int updated;
503

504
505
506
    /*! Number of #gpart inhibited in this cell. */
    int inhibited;

507
    /*! Is the #gpart data of this cell being used in a sub-cell? */
508
    int phold;
509

510
511
    /*! Is the #multipole data of this cell being used in a sub-cell? */
    int mhold;
512

513
514
515
    /*! Number of M-M tasks that are associated with this cell. */
    short int nr_mm_tasks;

516
  } grav;
517

518
519
  /*! Stars variables */
  struct {
520

521
522
    /*! Pointer to the #spart data. */
    struct spart *parts;
523

524
525
526
527
528
    /*! The star ghost task itself */
    struct task *ghost;

    /*! Linked list of the tasks computing this cell's star density. */
    struct link *density;
529

Alexei Borissov's avatar
Alexei Borissov committed
530
531
532
    /*! Linked list of the tasks computing this cell's star feedback. */
    struct link *feedback;

533
    /*! The task computing this cell's sorts before the density. */
534
    struct task *sorts;
Matthieu Schaller's avatar
Matthieu Schaller committed
535

536
    /*! The drift task for sparts */
537
    struct task *drift;
538

539
540
    /*! Implicit tasks marking the entry of the stellar physics block of tasks
     */
541
542
    struct task *stars_in;

543
    /*! Implicit tasks marking the exit of the stellar physics block of tasks */
544
545
    struct task *stars_out;

Loic Hausammann's avatar
Loic Hausammann committed
546
547
548
    /*! Max smoothing length in this cell. */
    double h_max;

549
550
551
    /*! Last (integer) time the cell's spart were drifted forward in time. */
    integertime_t ti_old_part;

552
553
554
    /*! Spin lock for various uses (#spart case). */
    swift_lock_type lock;

555
556
557
    /*! Spin lock for star formation use. */
    swift_lock_type star_formation_lock;

558
559
560
    /*! Nr of #spart in this cell. */
    int count;

561
562
563
    /*! Nr of #spart this cell can hold after addition of new #spart. */
    int count_total;

Loic Hausammann's avatar
Loic Hausammann committed
564
565
566
567
568
569
570
571
572
    /*! Values of h_max before the drifts, used for sub-cell tasks. */
    float h_max_old;

    /*! Maximum part movement in this cell since last construction. */
    float dx_max_part;

    /*! Values of dx_max before the drifts, used for sub-cell tasks. */
    float dx_max_part_old;

Matthieu Schaller's avatar
Matthieu Schaller committed
573
574
575
576
577
578
579
580
581
    /*! Maximum particle movement in this cell since the last sort. */
    float dx_max_sort;

    /*! Values of dx_max_sort before the drifts, used for sub-cell tasks. */
    float dx_max_sort_old;

    /*! Pointer for the sorted indices. */
    struct entry *sort[13];

582
583
    /*! Bit mask of sort directions that will be needed in the next timestep. */
    uint16_t requires_sorts;
Matthieu Schaller's avatar
Matthieu Schaller committed
584
585

    /*! Bit-mask indicating the sorted directions */
586
    uint16_t sorted;
Matthieu Schaller's avatar
Matthieu Schaller committed
587
588

    /*! Bit mask of sorts that need to be computed for this cell. */
589
    uint16_t do_sort;
Matthieu Schaller's avatar
Matthieu Schaller committed
590

591
    /*! Maximum end of (integer) time step in this cell for star tasks. */
Matthieu Schaller's avatar
Matthieu Schaller committed
592
593
    integertime_t ti_end_min;

594
595
596
597
598
599
    /*! Maximum end of (integer) time step in this cell for star tasks. */
    integertime_t ti_end_max;

    /*! Maximum beginning of (integer) time step in this cell for star tasks.
     */
    integertime_t ti_beg_max;
600

601
602
    /*! Number of #spart updated in this cell. */
    int updated;
603

604
605
606
    /*! Number of #spart inhibited in this cell. */
    int inhibited;

607
608
    /*! Is the #spart data of this cell being used in a sub-cell? */
    int hold;
609

Folkert Nobels's avatar
Folkert Nobels committed
610
    /*! Star formation history struct */
611
    struct star_formation_history sfh;
612

Matthieu Schaller's avatar
Matthieu Schaller committed
613
614
615
616
617
#ifdef SWIFT_DEBUG_CHECKS
    /*! Last (integer) time the cell's sort arrays were updated. */
    integertime_t ti_sort;
#endif

618
  } stars;
619

620
621
622
623
624
625
626
627
628
  /*! Black hole variables */
  struct {

    /*! Pointer to the #bpart data. */
    struct bpart *parts;

    /*! The drift task for bparts */
    struct task *drift;

629
630
631
632
633
634
635
    /*! Implicit tasks marking the entry of the BH physics block of tasks
     */
    struct task *black_holes_in;

    /*! Implicit tasks marking the exit of the BH physics block of tasks */
    struct task *black_holes_out;

636
637
638
639
640
641
642
643
644
    /*! The star ghost task itself */
    struct task *ghost;

    /*! Linked list of the tasks computing this cell's star density. */
    struct link *density;

    /*! Linked list of the tasks computing this cell's star feedback. */
    struct link *feedback;

645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
    /*! Max smoothing length in this cell. */
    double h_max;

    /*! Last (integer) time the cell's bpart were drifted forward in time. */
    integertime_t ti_old_part;

    /*! Spin lock for various uses (#bpart case). */
    swift_lock_type lock;

    /*! Nr of #bpart in this cell. */
    int count;

    /*! Nr of #bpart this cell can hold after addition of new #bpart. */
    int count_total;

    /*! Values of h_max before the drifts, used for sub-cell tasks. */
    float h_max_old;

    /*! Maximum part movement in this cell since last construction. */
    float dx_max_part;

666
667
668
    /*! Values of dx_max before the drifts, used for sub-cell tasks. */
    float dx_max_part_old;

669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
    /*! Maximum end of (integer) time step in this cell for black tasks. */
    integertime_t ti_end_min;

    /*! Maximum end of (integer) time step in this cell for black hole tasks. */
    integertime_t ti_end_max;

    /*! Maximum beginning of (integer) time step in this cell for black hole
     * tasks.
     */
    integertime_t ti_beg_max;

    /*! Number of #bpart updated in this cell. */
    int updated;

    /*! Number of #bpart inhibited in this cell. */
    int inhibited;

    /*! Is the #bpart data of this cell being used in a sub-cell? */
    int hold;

  } black_holes;

691
692
693
#ifdef WITH_MPI
  /*! MPI variables */
  struct {
694

695
696
697
    union {
      /* Single list of all send tasks associated with this cell. */
      struct link *send;
698

699
      /* Single list of all recv tasks associated with this cell. */
700
701
      struct link *recv;
    };
702

703
704
705
706
707
708
709
710
711
712
713
    /*! Bit mask of the proxies this cell is registered with. */
    unsigned long long int sendto;

    /*! Pointer to this cell's packed representation. */
    struct pcell *pcell;

    /*! Size of the packed representation */
    int pcell_size;

    /*! MPI tag associated with this cell */
    int tag;
714

715
716
  } mpi;
#endif
717

718
719
  /*! The first kick task */
  struct task *kick1;
720

721
722
  /*! The second kick task */
  struct task *kick2;
723

724
725
726
  /*! The task to compute time-steps */
  struct task *timestep;

727
728
  /*! The task to limit the time-step of inactive particles */
  struct task *timestep_limiter;
729

Loic Hausammann's avatar
Format    
Loic Hausammann committed
730
731
732
  /*! The logger task */
  struct task *logger;

733
734
  /*! Minimum dimension, i.e. smallest edge of this cell (min(width)). */
  float dmin;
735

736
737
738
739
740
741
  /*! ID of the previous owner, e.g. runner. */
  int owner;

  /*! ID of the node this cell lives on. */
  int nodeID;

742
743
744
745
746
747
748
749
750
751
752
753
  /*! Number of tasks that are associated with this cell. */
  short int nr_tasks;

  /*! The depth of this cell in the tree. */
  char depth;

  /*! Is this cell split ? */
  char split;

  /*! The maximal depth of this cell and its progenies */
  char maxdepth;

Loic Hausammann's avatar
Loic Hausammann committed
754
#if defined(SWIFT_DEBUG_CHECKS) || defined(SWIFT_CELL_GRAPH)
755
756
  /* Cell ID (for debugging) */
  int cellID;
Loic Hausammann's avatar
Loic Hausammann committed
757
758
759
#endif

#ifdef SWIFT_DEBUG_CHECKS
760

761
762
763
764
765
766
767
  /*! The list of tasks that have been executed on this cell */
  char tasks_executed[64];

  /*! The list of sub-tasks that have been executed on this cell */
  char subtasks_executed[64];
#endif

768
} SWIFT_STRUCT_ALIGN;
769

770
771
772
773
/* Convert cell location to ID. */
#define cell_getid(cdim, i, j, k) \
  ((int)(k) + (cdim)[2] * ((int)(j) + (cdim)[1] * (int)(i)))

774
/* Function prototypes. */
775
void cell_split(struct cell *c, ptrdiff_t parts_offset, ptrdiff_t sparts_offset,
776
777
                ptrdiff_t bparts_offset, struct cell_buff *buff,
                struct cell_buff *sbuff, struct cell_buff *bbuff,
778
                struct cell_buff *gbuff);
779
void cell_sanitize(struct cell *c, int treated);
780
781
782
783
int cell_locktree(struct cell *c);
void cell_unlocktree(struct cell *c);
int cell_glocktree(struct cell *c);
void cell_gunlocktree(struct cell *c);
784
785
int cell_mlocktree(struct cell *c);
void cell_munlocktree(struct cell *c);
786
787
int cell_slocktree(struct cell *c);
void cell_sunlocktree(struct cell *c);
788
int cell_pack(struct cell *c, struct pcell *pc, const int with_gravity);
Matthieu Schaller's avatar
Matthieu Schaller committed
789
790
int cell_unpack(struct pcell *pc, struct cell *c, struct space *s,
                const int with_gravity);
791
792
int cell_pack_tags(const struct cell *c, int *tags);
int cell_unpack_tags(const int *tags, struct cell *c);
793
794
795
796
797
798
int cell_pack_end_step_hydro(struct cell *c, struct pcell_step_hydro *pcell);
int cell_unpack_end_step_hydro(struct cell *c, struct pcell_step_hydro *pcell);
int cell_pack_end_step_grav(struct cell *c, struct pcell_step_grav *pcell);
int cell_unpack_end_step_grav(struct cell *c, struct pcell_step_grav *pcell);
int cell_pack_end_step_stars(struct cell *c, struct pcell_step_stars *pcell);
int cell_unpack_end_step_stars(struct cell *c, struct pcell_step_stars *pcell);
799
800
801
802
int cell_pack_end_step_black_holes(struct cell *c,
                                   struct pcell_step_black_holes *pcell);
int cell_unpack_end_step_black_holes(struct cell *c,
                                     struct pcell_step_black_holes *pcell);
803
804
int cell_pack_multipoles(struct cell *c, struct gravity_tensors *m);
int cell_unpack_multipoles(struct cell *c, struct gravity_tensors *m);
805
int cell_getsize(struct cell *c);
806
int cell_link_parts(struct cell *c, struct part *parts);
807
int cell_link_gparts(struct cell *c, struct gpart *gparts);
808
int cell_link_sparts(struct cell *c, struct spart *sparts);
809
int cell_link_bparts(struct cell *c, struct bpart *bparts);
810
811
int cell_link_foreign_parts(struct cell *c, struct part *parts);
int cell_link_foreign_gparts(struct cell *c, struct gpart *gparts);
812
int cell_count_parts_for_tasks(const struct cell *c);
813
int cell_count_gparts_for_tasks(const struct cell *c);
814
void cell_clean_links(struct cell *c, void *data);
815
void cell_make_multipoles(struct cell *c, integertime_t ti_current);
816
817
void cell_check_multipole(struct cell *c);
void cell_check_foreign_multipole(const struct cell *c);
818
void cell_clean(struct cell *c);
819
820
void cell_check_part_drift_point(struct cell *c, void *data);
void cell_check_gpart_drift_point(struct cell *c, void *data);
821
void cell_check_spart_drift_point(struct cell *c, void *data);
822
void cell_check_multipole_drift_point(struct cell *c, void *data);
823
void cell_reset_task_counters(struct cell *c);
824
int cell_unskip_hydro_tasks(struct cell *c, struct scheduler *s);
825
int cell_unskip_stars_tasks(struct cell *c, struct scheduler *s);
826
int cell_unskip_black_holes_tasks(struct cell *c, struct scheduler *s);
827
int cell_unskip_gravity_tasks(struct cell *c, struct scheduler *s);
828
void cell_drift_part(struct cell *c, const struct engine *e, int force);
829
void cell_drift_gpart(struct cell *c, const struct engine *e, int force);
830
void cell_drift_spart(struct cell *c, const struct engine *e, int force);
831
void cell_drift_bpart(struct cell *c, const struct engine *e, int force);
832
void cell_drift_multipole(struct cell *c, const struct engine *e);
833
void cell_drift_all_multipoles(struct cell *c, const struct engine *e);
834
void cell_check_timesteps(struct cell *c);
835
void cell_store_pre_drift_values(struct cell *c);
836
837
void cell_activate_subcell_hydro_tasks(struct cell *ci, struct cell *cj,
                                       struct scheduler *s);
838
839
void cell_activate_subcell_grav_tasks(struct cell *ci, struct cell *cj,
                                      struct scheduler *s);
840
841
void cell_activate_subcell_stars_tasks(struct cell *ci, struct cell *cj,
                                       struct scheduler *s);
842
843
void cell_activate_subcell_black_holes_tasks(struct cell *ci, struct cell *cj,
                                             struct scheduler *s);
844
845
void cell_activate_subcell_external_grav_tasks(struct cell *ci,
                                               struct scheduler *s);
846
void cell_activate_super_spart_drifts(struct cell *c, struct scheduler *s);
847
void cell_activate_drift_part(struct cell *c, struct scheduler *s);
848
void cell_activate_drift_gpart(struct cell *c, struct scheduler *s);
Loic Hausammann's avatar
Loic Hausammann committed
849
void cell_activate_drift_spart(struct cell *c, struct scheduler *s);
850
void cell_activate_drift_bpart(struct cell *c, struct scheduler *s);
851
void cell_activate_hydro_sorts(struct cell *c, int sid, struct scheduler *s);
Loic Hausammann's avatar
Loic Hausammann committed
852
void cell_activate_stars_sorts(struct cell *c, int sid, struct scheduler *s);
853
void cell_activate_limiter(struct cell *c, struct scheduler *s);
854
void cell_clear_drift_flags(struct cell *c, void *data);
855
void cell_clear_limiter_flags(struct cell *c, void *data);
Pedro Gonnet's avatar
Pedro Gonnet committed
856
void cell_set_super_mapper(void *map_data, int num_elements, void *extra_data);
857
858
void cell_check_spart_pos(const struct cell *c,
                          const struct spart *global_sparts);
859
void cell_clear_stars_sort_flags(struct cell *c);
860
int cell_has_tasks(struct cell *c);
861
862
void cell_remove_part(const struct engine *e, struct cell *c, struct part *p,
                      struct xpart *xp);
863
864
865
866
void cell_remove_gpart(const struct engine *e, struct cell *c,
                       struct gpart *gp);
void cell_remove_spart(const struct engine *e, struct cell *c,
                       struct spart *sp);
867
868
void cell_remove_bpart(const struct engine *e, struct cell *c,
                       struct bpart *bp);
869
struct spart *cell_add_spart(struct engine *e, struct cell *c);
Matthieu Schaller's avatar
Matthieu Schaller committed
870
871
872
873
struct gpart *cell_convert_part_to_gpart(const struct engine *e, struct cell *c,
                                         struct part *p, struct xpart *xp);
struct gpart *cell_convert_spart_to_gpart(const struct engine *e,
                                          struct cell *c, struct spart *sp);
874
875
struct spart *cell_convert_part_to_spart(struct engine *e, struct cell *c,
                                         struct part *p, struct xpart *xp);
876
void cell_reorder_extra_parts(struct cell *c, const ptrdiff_t parts_offset);
877
878
void cell_reorder_extra_gparts(struct cell *c, struct part *parts,
                               struct spart *sparts);
879
void cell_reorder_extra_sparts(struct cell *c, const ptrdiff_t sparts_offset);
880
881
int cell_can_use_pair_mm(const struct cell *ci, const struct cell *cj,
                         const struct engine *e, const struct space *s);
882
int cell_can_use_pair_mm_rebuild(const struct cell *ci, const struct cell *cj,
Matthieu Schaller's avatar
Matthieu Schaller committed
883
                                 const struct engine *e, const struct space *s);
884

885
/**
Peter W. Draper's avatar
Peter W. Draper committed
886
 * @brief Compute the square of the minimal distance between any two points in
Matthieu Schaller's avatar
Matthieu Schaller committed
887
 * two cells of the same size
888
889
890
891
892
893
 *
 * @param ci The first #cell.
 * @param cj The second #cell.
 * @param periodic Are we using periodic BCs?
 * @param dim The dimensions of the simulation volume
 */
Matthieu Schaller's avatar
Matthieu Schaller committed
894
895
896
__attribute__((always_inline)) INLINE static double cell_min_dist2_same_size(
    const struct cell *restrict ci, const struct cell *restrict cj,
    const int periodic, const double dim[3]) {
897
898

#ifdef SWIFT_DEBUG_CHECKS
Matthieu Schaller's avatar
Matthieu Schaller committed
899
900
901
  if (ci->width[0] != cj->width[0]) error("Cells of different size!");
  if (ci->width[1] != cj->width[1]) error("Cells of different size!");
  if (ci->width[2] != cj->width[2]) error("Cells of different size!");
902
903
#endif

Matthieu Schaller's avatar
Matthieu Schaller committed
904
905
906
907
908
909
910
911
912
913
914
915
  const double cix_min = ci->loc[0];
  const double ciy_min = ci->loc[1];
  const double ciz_min = ci->loc[2];
  const double cjx_min = cj->loc[0];
  const double cjy_min = cj->loc[1];
  const double cjz_min = cj->loc[2];

  const double cix_max = ci->loc[0] + ci->width[0];
  const double ciy_max = ci->loc[1] + ci->width[1];
  const double ciz_max = ci->loc[2] + ci->width[2];
  const double cjx_max = cj->loc[0] + cj->width[0];
  const double cjy_max = cj->loc[1] + cj->width[1];
916
917
918
919
920
  const double cjz_max = cj->loc[2] + cj->width[2];

  if (periodic) {

    const double dx = min4(fabs(nearest(cix_min - cjx_min, dim[0])),
Matthieu Schaller's avatar
Matthieu Schaller committed
921
922
923
924
                           fabs(nearest(cix_min - cjx_max, dim[0])),
                           fabs(nearest(cix_max - cjx_min, dim[0])),
                           fabs(nearest(cix_max - cjx_max, dim[0])));

925
    const double dy = min4(fabs(nearest(ciy_min - cjy_min, dim[1])),
Matthieu Schaller's avatar
Matthieu Schaller committed
926
927
928
929
                           fabs(nearest(ciy_min - cjy_max, dim[1])),
                           fabs(nearest(ciy_max - cjy_min, dim[1])),
                           fabs(nearest(ciy_max - cjy_max, dim[1])));

930
    const double dz = min4(fabs(nearest(ciz_min - cjz_min, dim[2])),
Matthieu Schaller's avatar
Matthieu Schaller committed
931
932
933
934
                           fabs(nearest(ciz_min - cjz_max, dim[2])),
                           fabs(nearest(ciz_max - cjz_min, dim[2])),
                           fabs(nearest(ciz_max - cjz_max, dim[2])));

935
936
937
938
    return dx * dx + dy * dy + dz * dz;

  } else {

Matthieu Schaller's avatar
Matthieu Schaller committed
939
940
941
    const double dx = min(fabs(cix_max - cjx_min), fabs(cix_min - cjx_max));
    const double dy = min(fabs(ciy_max - cjy_min), fabs(ciy_min - cjy_max));
    const double dz = min(fabs(ciz_max - cjz_min), fabs(ciz_min - cjz_max));
942
943
944
945
946

    return dx * dx + dy * dy + dz * dz;
  }
}

947
948
949
950
951
952
953
954
/* Inlined functions (for speed). */

/**
 * @brief Can a sub-pair hydro task recurse to a lower level based
 * on the status of the particles in the cell.
 *
 * @param c The #cell.
 */
Matthieu Schaller's avatar
Matthieu Schaller committed
955
956
__attribute__((always_inline)) INLINE static int
cell_can_recurse_in_pair_hydro_task(const struct cell *c) {
957
958
959
960
961

  /* Is the cell split ? */
  /* If so, is the cut-off radius plus the max distance the parts have moved */
  /* smaller than the sub-cell sizes ? */
  /* Note: We use the _old values as these might have been updated by a drift */
962
  return c->split && ((kernel_gamma * c->hydro.h_max_old +
963
                       c->hydro.dx_max_part_old) < 0.5f * c->dmin);
964
965
966
967
968
969
970
971
}

/**
 * @brief Can a sub-self hydro task recurse to a lower level based
 * on the status of the particles in the cell.
 *
 * @param c The #cell.
 */
Matthieu Schaller's avatar
Matthieu Schaller committed
972
973
__attribute__((always_inline)) INLINE static int
cell_can_recurse_in_self_hydro_task(const struct cell *c) {
974

975
  /* Is the cell split and not smaller than the smoothing length? */
976
  return c->split && (kernel_gamma * c->hydro.h_max_old < 0.5f * c->dmin);
977
978
}

979
980
981
982
/**
 * @brief Can a sub-pair star task recurse to a lower level based
 * on the status of the particles in the cell.
 *
loikki's avatar
loikki committed
983
984
 * @param ci The #cell with stars.
 * @param cj The #cell with hydro parts.
985
986
 */
__attribute__((always_inline)) INLINE static int
987
988
cell_can_recurse_in_pair_stars_task(const struct cell *ci,
                                    const struct cell *cj) {
989

Loic Hausammann's avatar
Loic Hausammann committed
990
991
992
993
  /* Is the cell split ? */
  /* If so, is the cut-off radius plus the max distance the parts have moved */
  /* smaller than the sub-cell sizes ? */
  /* Note: We use the _old values as these might have been updated by a drift */
994