active.h 15.2 KB
Newer Older
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
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2016 Matthieu Schaller (matthieu.schaller@durham.ac.uk)
 *
 * 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.
 *
 * 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.
 *
 * 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/>.
 *
 ******************************************************************************/
#ifndef SWIFT_ACTIVE_H
#define SWIFT_ACTIVE_H

/* Config parameters. */
#include "../config.h"

/* Local includes. */
#include "cell.h"
#include "engine.h"
#include "part.h"
29
#include "timeline.h"
30

31
/**
32
 * @brief Check that the #part in a #cell have been drifted to the current time.
33
34
35
 *
 * @param c The #cell.
 * @param e The #engine containing information about the current time.
36
 * @return 1 if the #cell has been drifted to the current time, 0 otherwise.
37
 */
38
__attribute__((always_inline)) INLINE static int cell_are_part_drifted(
39
40
41
    const struct cell *c, const struct engine *e) {

#ifdef SWIFT_DEBUG_CHECKS
42
  if (c->hydro.ti_old_part > e->ti_current)
43
    error(
44
45
        "Cell has been drifted too far forward in time! c->ti_old_part=%lld "
        "(t=%e) "
46
        "and e->ti_current=%lld (t=%e, a=%e)",
47
48
        c->hydro.ti_old_part, c->hydro.ti_old_part * e->time_base,
        e->ti_current, e->ti_current * e->time_base, e->cosmology->a);
49
#endif
Matthieu Schaller's avatar
Matthieu Schaller committed
50

51
  return (c->hydro.ti_old_part == e->ti_current);
52
53
54
55
56
57
58
59
60
61
62
63
64
65
}

/**
 * @brief Check that the #gpart in a #cell have been drifted to the current
 * time.
 *
 * @param c The #cell.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #cell has been drifted to the current time, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int cell_are_gpart_drifted(
    const struct cell *c, const struct engine *e) {

#ifdef SWIFT_DEBUG_CHECKS
66
  if (c->grav.ti_old_part > e->ti_current)
67
68
69
    error(
        "Cell has been drifted too far forward in time! c->ti_old=%lld (t=%e) "
        "and e->ti_current=%lld (t=%e)",
70
71
        c->grav.ti_old_part, c->grav.ti_old_part * e->time_base, e->ti_current,
        e->ti_current * e->time_base);
72
73
#endif

74
  return (c->grav.ti_old_part == e->ti_current);
75
76
}

77
78
/* Are cells / particles active for regular tasks ? */

79
/**
80
 * @brief Does a cell contain any particle finishing their time-step now ?
81
82
83
 *
 * @param c The #cell.
 * @param e The #engine containing information about the current time.
Matthieu Schaller's avatar
Matthieu Schaller committed
84
 * @return 1 if the #cell contains at least an active particle, 0 otherwise.
85
 */
86
__attribute__((always_inline)) INLINE static int cell_is_active_hydro(
87
88
    const struct cell *c, const struct engine *e) {

89
#ifdef SWIFT_DEBUG_CHECKS
90
  if (c->hydro.ti_end_min < e->ti_current)
91
    error(
92
        "cell in an impossible time-zone! c->ti_end_min=%lld (t=%e) and "
93
        "e->ti_current=%lld (t=%e, a=%e)",
94
        c->hydro.ti_end_min, c->hydro.ti_end_min * e->time_base, e->ti_current,
95
        e->ti_current * e->time_base, e->cosmology->a);
96
97
#endif

98
  return (c->hydro.ti_end_min == e->ti_current);
99
100
101
}

/**
102
 * @brief Are *all* particles in a cell finishing their time-step now ?
103
104
105
 *
 * @param c The #cell.
 * @param e The #engine containing information about the current time.
Matthieu Schaller's avatar
Matthieu Schaller committed
106
 * @return 1 if all particles in a #cell are active, 0 otherwise.
107
 */
108
__attribute__((always_inline)) INLINE static int cell_is_all_active_hydro(
109
110
    const struct cell *c, const struct engine *e) {

111
#ifdef SWIFT_DEBUG_CHECKS
112
  if (c->hydro.ti_end_max < e->ti_current)
113
114
115
    error(
        "cell in an impossible time-zone! c->ti_end_max=%lld "
        "e->ti_current=%lld",
116
        c->hydro.ti_end_max, e->ti_current);
117
118
#endif

119
  return (c->hydro.ti_end_max == e->ti_current);
120
121
122
123
124
125
126
127
128
129
130
131
132
}

/**
 * @brief Does a cell contain any g-particle finishing their time-step now ?
 *
 * @param c The #cell.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #cell contains at least an active particle, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int cell_is_active_gravity(
    const struct cell *c, const struct engine *e) {

#ifdef SWIFT_DEBUG_CHECKS
133
  if (c->grav.ti_end_min < e->ti_current)
134
135
    error(
        "cell in an impossible time-zone! c->ti_end_min=%lld (t=%e) and "
136
        "e->ti_current=%lld (t=%e, a=%e)",
137
138
        c->grav.ti_end_min, c->grav.ti_end_min * e->time_base, e->ti_current,
        e->ti_current * e->time_base, e->cosmology->a);
139
140
#endif

141
  return (c->grav.ti_end_min == e->ti_current);
142
143
}

144
/**
Matthieu Schaller's avatar
Matthieu Schaller committed
145
 * @brief Does a cell contain any multipole requiring calculation ?
146
147
148
149
150
151
152
153
 *
 * @param c The #cell.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #cell contains at least an active particle, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int cell_is_active_gravity_mm(
    const struct cell *c, const struct engine *e) {

154
  return (c->grav.ti_end_min == e->ti_current);
155
156
}

157
158
159
160
161
162
163
164
165
166
167
/**
 * @brief Are *all* g-particles in a cell finishing their time-step now ?
 *
 * @param c The #cell.
 * @param e The #engine containing information about the current time.
 * @return 1 if all particles in a #cell are active, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int cell_is_all_active_gravity(
    const struct cell *c, const struct engine *e) {

#ifdef SWIFT_DEBUG_CHECKS
168
  if (c->grav.ti_end_max < e->ti_current)
169
170
171
    error(
        "cell in an impossible time-zone! c->ti_end_max=%lld "
        "e->ti_current=%lld",
172
        c->grav.ti_end_max, e->ti_current);
173
174
#endif

175
  return (c->grav.ti_end_max == e->ti_current);
176
177
}

178
179
180
181
182
183
184
/**
 * @brief Does a cell contain any s-particle finishing their time-step now ?
 *
 * @param c The #cell.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #cell contains at least an active particle, 0 otherwise.
 */
Loic Hausammann's avatar
Loic Hausammann committed
185
__attribute__((always_inline)) INLINE static int cell_is_active_stars(
186
187
    const struct cell *c, const struct engine *e) {

188
189
  // LOIC: Need star-specific implementation

190
  return cell_is_active_gravity(c, e);
191
192
}

193
/**
194
 * @brief Is this particle finishing its time-step now ?
195
196
197
 *
 * @param p The #part.
 * @param e The #engine containing information about the current time.
198
 * @return 1 if the #part is active, 0 otherwise.
199
200
201
202
 */
__attribute__((always_inline)) INLINE static int part_is_active(
    const struct part *p, const struct engine *e) {

203
204
  const timebin_t max_active_bin = e->max_active_bin;
  const timebin_t part_bin = p->time_bin;
205

206
#ifdef SWIFT_DEBUG_CHECKS
207
208
  const integertime_t ti_current = e->ti_current;
  const integertime_t ti_end = get_integer_time_end(ti_current, p->time_bin);
209
210
211
212
213
  if (ti_end < ti_current)
    error(
        "particle in an impossible time-zone! p->ti_end=%lld "
        "e->ti_current=%lld",
        ti_end, ti_current);
214
215
#endif

216
  return (part_bin <= max_active_bin);
217
218
}

219
220
221
222
223
224
225
226
__attribute__((always_inline)) INLINE static int part_is_active_no_debug(
    const struct part *p, const timebin_t max_active_bin) {

  const timebin_t part_bin = p->time_bin;

  return (part_bin <= max_active_bin);
}

227
/**
228
 * @brief Is this g-particle finishing its time-step now ?
229
230
231
 *
 * @param gp The #gpart.
 * @param e The #engine containing information about the current time.
232
 * @return 1 if the #gpart is active, 0 otherwise.
233
234
235
236
 */
__attribute__((always_inline)) INLINE static int gpart_is_active(
    const struct gpart *gp, const struct engine *e) {

237
238
239
240
  const timebin_t max_active_bin = e->max_active_bin;
  const timebin_t gpart_bin = gp->time_bin;

#ifdef SWIFT_DEBUG_CHECKS
241
242
243
244
  const integertime_t ti_current = e->ti_current;
  const integertime_t ti_end = get_integer_time_end(ti_current, gp->time_bin);

  if (ti_end < ti_current)
245
    error(
246
247
248
        "g-particle in an impossible time-zone! gp->ti_end=%lld "
        "e->ti_current=%lld",
        ti_end, ti_current);
249
250
#endif

251
  return (gpart_bin <= max_active_bin);
252
253
}

254
/**
Matthieu Schaller's avatar
Matthieu Schaller committed
255
 * @brief Is this s-particle finishing its time-step now ?
256
257
258
259
260
261
262
263
 *
 * @param sp The #spart.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #spart is active, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int spart_is_active(
    const struct spart *sp, const struct engine *e) {

264
265
266
267
  const timebin_t max_active_bin = e->max_active_bin;
  const timebin_t spart_bin = sp->time_bin;

#ifdef SWIFT_DEBUG_CHECKS
268
269
270
271
272
  const integertime_t ti_current = e->ti_current;
  const integertime_t ti_end = get_integer_time_end(ti_current, sp->time_bin);

  if (ti_end < ti_current)
    error(
Matthieu Schaller's avatar
Matthieu Schaller committed
273
        "s-particle in an impossible time-zone! sp->ti_end=%lld "
274
275
276
277
        "e->ti_current=%lld",
        ti_end, ti_current);
#endif

278
  return (spart_bin <= max_active_bin);
279
280
}

281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
 * @brief Has this particle been inhibited?
 *
 * @param p The #part.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #part is inhibited, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int part_is_inhibited(
    const struct part *p, const struct engine *e) {
  return p->time_bin == time_bin_inhibited;
}

/**
 * @brief Has this gravity particle been inhibited?
 *
 * @param gp The #gpart.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #part is inhibited, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int gpart_is_inhibited(
    const struct gpart *gp, const struct engine *e) {
  return gp->time_bin == time_bin_inhibited;
}

/**
 * @brief Has this star particle been inhibited?
 *
 * @param sp The #spart.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #part is inhibited, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int spart_is_inhibited(
    const struct spart *sp, const struct engine *e) {
  return sp->time_bin == time_bin_inhibited;
}

317
318
319
320
321
322
323
324
325
/* Are cells / particles active for kick1 tasks ? */

/**
 * @brief Does a cell contain any particle starting their time-step now ?
 *
 * @param c The #cell.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #cell contains at least an active particle, 0 otherwise.
 */
326
__attribute__((always_inline)) INLINE static int cell_is_starting_hydro(
327
328
329
    const struct cell *c, const struct engine *e) {

#ifdef SWIFT_DEBUG_CHECKS
330
  if (c->hydro.ti_beg_max > e->ti_current)
331
332
    error(
        "cell in an impossible time-zone! c->ti_beg_max=%lld (t=%e) and "
333
        "e->ti_current=%lld (t=%e, a=%e)",
334
        c->hydro.ti_beg_max, c->hydro.ti_beg_max * e->time_base, e->ti_current,
335
        e->ti_current * e->time_base, e->cosmology->a);
336
337
#endif

338
  return (c->hydro.ti_beg_max == e->ti_current);
339
340
341
342
343
344
345
346
347
348
349
350
351
}

/**
 * @brief Does a cell contain any g-particle starting their time-step now ?
 *
 * @param c The #cell.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #cell contains at least an active particle, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int cell_is_starting_gravity(
    const struct cell *c, const struct engine *e) {

#ifdef SWIFT_DEBUG_CHECKS
352
  if (c->grav.ti_beg_max > e->ti_current)
353
354
    error(
        "cell in an impossible time-zone! c->ti_beg_max=%lld (t=%e) and "
355
        "e->ti_current=%lld (t=%e, a=%e)",
356
357
        c->grav.ti_beg_max, c->grav.ti_beg_max * e->time_base, e->ti_current,
        e->ti_current * e->time_base, e->cosmology->a);
358
359
#endif

360
  return (c->grav.ti_beg_max == e->ti_current);
361
362
363
364
365
366
367
368
369
370
371
372
}

/**
 * @brief Is this particle starting its time-step now ?
 *
 * @param p The #part.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #part is active, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int part_is_starting(
    const struct part *p, const struct engine *e) {

373
374
375
376
  const timebin_t max_active_bin = e->max_active_bin;
  const timebin_t part_bin = p->time_bin;

#ifdef SWIFT_DEBUG_CHECKS
377
  const integertime_t ti_current = e->ti_current;
Matthieu Schaller's avatar
Matthieu Schaller committed
378
379
  const integertime_t ti_beg =
      get_integer_time_begin(ti_current + 1, p->time_bin);
380
381
382
383
384
385
386
387

  if (ti_beg > ti_current)
    error(
        "particle in an impossible time-zone! p->ti_beg=%lld "
        "e->ti_current=%lld",
        ti_beg, ti_current);
#endif

388
  return (part_bin <= max_active_bin);
389
390
391
392
393
394
395
396
397
398
399
400
}

/**
 * @brief Is this g-particle starting its time-step now ?
 *
 * @param gp The #gpart.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #gpart is active, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int gpart_is_starting(
    const struct gpart *gp, const struct engine *e) {

401
402
403
404
  const timebin_t max_active_bin = e->max_active_bin;
  const timebin_t gpart_bin = gp->time_bin;

#ifdef SWIFT_DEBUG_CHECKS
405
  const integertime_t ti_current = e->ti_current;
Matthieu Schaller's avatar
Matthieu Schaller committed
406
407
  const integertime_t ti_beg =
      get_integer_time_begin(ti_current + 1, gp->time_bin);
408
409
410
411
412
413
414
415

  if (ti_beg > ti_current)
    error(
        "g-particle in an impossible time-zone! gp->ti_beg=%lld "
        "e->ti_current=%lld",
        ti_beg, ti_current);
#endif

416
  return (gpart_bin <= max_active_bin);
417
}
Matthieu Schaller's avatar
Matthieu Schaller committed
418
419
420
421
422
423
424
425
426
427
428

/**
 * @brief Is this s-particle starting its time-step now ?
 *
 * @param sp The #spart.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #spart is active, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int spart_is_starting(
    const struct spart *sp, const struct engine *e) {

429
430
431
432
  const timebin_t max_active_bin = e->max_active_bin;
  const timebin_t spart_bin = sp->time_bin;

#ifdef SWIFT_DEBUG_CHECKS
Matthieu Schaller's avatar
Matthieu Schaller committed
433
434
435
436
437
438
439
440
441
442
443
  const integertime_t ti_current = e->ti_current;
  const integertime_t ti_beg =
      get_integer_time_begin(ti_current + 1, sp->time_bin);

  if (ti_beg > ti_current)
    error(
        "s-particle in an impossible time-zone! sp->ti_beg=%lld "
        "e->ti_current=%lld",
        ti_beg, ti_current);
#endif

444
  return (spart_bin <= max_active_bin);
Matthieu Schaller's avatar
Matthieu Schaller committed
445
}
Loikki's avatar
Loikki committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485

/**
 * @brief Should this particle write its data now ?
 *
 * @param p The #part.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #part should write, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int part_should_write(
    const struct part *p, const struct engine *e) {

  return (p->last_output > e->logger_max_steps);  
}

/**
 * @brief Should this particle write its data now ?
 *
 * @param p The #gpart.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #gpart should write, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int gpart_should_write(
    const struct gpart *p, const struct engine *e) {

  return (p->last_output > e->logger_max_steps);  
}

/**
 * @brief Should this particle write its data now ?
 *
 * @param p The #spart.
 * @param e The #engine containing information about the current time.
 * @return 1 if the #spart should write, 0 otherwise.
 */
__attribute__((always_inline)) INLINE static int spart_should_write(
    const struct spart *p, const struct engine *e) {

  return (p->last_output > e->logger_max_steps);  
}

486
#endif /* SWIFT_ACTIVE_H */