space.h 15.2 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_SPACE_H
#define SWIFT_SPACE_H
Pedro Gonnet's avatar
Pedro Gonnet committed
25

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

/* Some standard headers. */
30 31
#include <stddef.h>

32
/* Includes. */
33
#include "hydro_space.h"
34
#include "lock.h"
35
#include "parser.h"
36
#include "part.h"
Loic Hausammann's avatar
Loic Hausammann committed
37
#include "space_unique_id.h"
38
#include "velociraptor_struct.h"
39

40 41
/* Avoid cyclic inclusions */
struct cell;
42
struct cosmology;
43
struct gravity_props;
Loic Hausammann's avatar
Loic Hausammann committed
44
struct star_formation;
45
struct hydro_props;
46

Pedro Gonnet's avatar
Pedro Gonnet committed
47
/* Some constants. */
48 49 50
#define space_cellallocchunk 1000
#define space_splitsize_default 400
#define space_maxsize_default 8000000
51 52
#define space_extra_parts_default 0
#define space_extra_gparts_default 0
53
#define space_extra_sparts_default 100
54
#define space_extra_bparts_default 0
Loic Hausammann's avatar
Loic Hausammann committed
55
#define space_extra_sinks_default 0
56
#define space_expected_max_nr_strays_default 100
57 58
#define space_subsize_pair_hydro_default 256000000
#define space_subsize_self_hydro_default 32000
59 60
#define space_subsize_pair_stars_default 256000000
#define space_subsize_self_stars_default 32000
61
#define space_subsize_pair_grav_default 256000000
62
#define space_subsize_self_grav_default 32000
63
#define space_subdepth_diff_grav_default 4
64
#define space_max_top_level_cells_default 12
65
#define space_stretch 1.10f
66
#define space_maxreldx 0.1f
Pedro Gonnet's avatar
Pedro Gonnet committed
67

68 69 70
/* Maximum allowed depth of cell splits. */
#define space_cell_maxdepth 52

71 72
/* Globals needed in contexts without a space struct. Remember to dump and
 * restore these. */
Pedro Gonnet's avatar
Pedro Gonnet committed
73
extern int space_splitsize;
74
extern int space_maxsize;
75 76
extern int space_subsize_pair_hydro;
extern int space_subsize_self_hydro;
77 78
extern int space_subsize_pair_stars;
extern int space_subsize_self_stars;
79
extern int space_subsize_pair_grav;
80
extern int space_subsize_self_grav;
81
extern int space_subdepth_diff_grav;
82 83 84
extern int space_extra_parts;
extern int space_extra_gparts;
extern int space_extra_sparts;
85
extern int space_extra_bparts;
Loic Hausammann's avatar
Loic Hausammann committed
86
extern int space_extra_sinks;
Pedro Gonnet's avatar
Pedro Gonnet committed
87

88
/**
89
 * @brief The space in which the cells and particles reside.
90
 */
Pedro Gonnet's avatar
Pedro Gonnet committed
91 92
struct space {

93
  /*! Spatial extent. */
94 95
  double dim[3];

96 97 98
  /*! Is the space periodic? */
  int periodic;

99 100 101
  /*! Extra space information needed for some hydro schemes. */
  struct hydro_space hs;

102
  /*! Are we doing hydrodynamics? */
103
  int with_hydro;
104

105
  /*! Are we doing gravity? */
106
  int with_self_gravity;
107

108 109 110
  /*! Are we doing star formation? */
  int with_star_formation;

111 112 113
  /*! Are we running with some DM background particles? */
  int with_DM_background;

114 115
  /*! Width of the top-level cells. */
  double width[3];
116

117
  /*! Inverse of the top-level cell width */
118 119
  double iwidth[3];

120
  /*! The minimum top-level cell width allowed. */
121
  double cell_min;
122

123
  /*! Space dimensions in number of top-cells. */
124 125 126 127
  int cdim[3];

  /*! Maximal depth reached by the tree */
  int maxdepth;
128

129 130 131 132 133 134
  /*! Number of top-level cells. */
  int nr_cells;

  /*! Total number of cells (top- and sub-) */
  int tot_cells;

135
  /*! Number of *local* top-level cells */
136 137
  int nr_local_cells;

138 139 140
  /*! Number of *local* top-level cells with tasks */
  int nr_local_cells_with_tasks;

141 142
  /*! Number of top-level cells that have >0 particle (of any kind) */
  int nr_cells_with_particles;
143

144 145 146
  /*! Number of top-level cells that have >0 particle (of any kind) */
  int nr_local_cells_with_particles;

147 148
  /*! The (level 0) cells themselves. */
  struct cell *cells_top;
149

150 151
  /*! Buffer of unused cells for the sub-cells. */
  struct cell *cells_sub;
152

153
  /*! The multipoles associated with the top-level (level 0) cells */
154
  struct gravity_tensors *multipoles_top;
155 156

  /*! Buffer of unused multipoles for the sub-cells. */
157
  struct gravity_tensors *multipoles_sub;
158

159
  /*! The indices of the *local* top-level cells */
160 161
  int *local_cells_top;

162 163 164
  /*! The indices of the *local* top-level cells with tasks */
  int *local_cells_with_tasks_top;

Peter W. Draper's avatar
Peter W. Draper committed
165
  /*! The indices of the top-level cells that have >0 particles (of any kind) */
166
  int *cells_with_particles_top;
167

Peter W. Draper's avatar
Peter W. Draper committed
168
  /*! The indices of the top-level cells that have >0 particles (of any kind) */
169 170
  int *local_cells_with_particles_top;

171 172
  /*! The total number of #part in the space. */
  size_t nr_parts;
173

174 175
  /*! The total number of #gpart in the space. */
  size_t nr_gparts;
176

177 178 179
  /*! The total number of #spart in the space. */
  size_t nr_sparts;

180 181 182
  /*! The total number of #bpart in the space. */
  size_t nr_bparts;

183 184 185
  /*! The total number of #sink in the space. */
  size_t nr_sinks;

186 187 188 189 190 191 192 193
  /*! The total number of #part we allocated memory for */
  size_t size_parts;

  /*! The total number of #gpart we allocated memory for */
  size_t size_gparts;

  /*! The total number of #spart we allocated memory for */
  size_t size_sparts;
194

195 196 197
  /*! The total number of #bpart we allocated memory for */
  size_t size_bparts;

198 199 200
  /*! The total number of #sink we allocated memory for. */
  size_t size_sinks;

201 202 203 204 205 206 207 208 209
  /*! Number of inhibted gas particles in the space */
  size_t nr_inhibited_parts;

  /*! Number of inhibted gravity particles in the space */
  size_t nr_inhibited_gparts;

  /*! Number of inhibted star particles in the space */
  size_t nr_inhibited_sparts;

210 211 212
  /*! Number of inhibted black hole particles in the space */
  size_t nr_inhibited_bparts;

213 214 215
  /*! Number of inhibted sinks in the space */
  size_t nr_inhibited_sinks;

216
  /*! Number of extra #part we allocated (for on-the-fly creation) */
217 218
  size_t nr_extra_parts;

219
  /*! Number of extra #gpart we allocated (for on-the-fly creation) */
220 221
  size_t nr_extra_gparts;

222
  /*! Number of extra #spart we allocated (for on-the-fly creation) */
223 224
  size_t nr_extra_sparts;

225 226 227
  /*! Number of extra #bpart we allocated (for on-the-fly creation) */
  size_t nr_extra_bparts;

228 229 230
  /*! Number of extra #sink we allocated (for on-the-fly creation) */
  size_t nr_extra_sinks;

231 232
  /*! The particle data (cells have pointers to this). */
  struct part *parts;
233

234 235 236 237 238
  /*! The extended particle data (cells have pointers to this). */
  struct xpart *xparts;

  /*! The g-particle data (cells have pointers to this). */
  struct gpart *gparts;
239

240 241 242
  /*! The s-particle data (cells have pointers to this). */
  struct spart *sparts;

243 244 245
  /*! The b-particle data (cells have pointers to this). */
  struct bpart *bparts;

246 247 248
  /*! The sink particle data (cells have pointers to this). */
  struct sink *sinks;

249 250 251 252 253 254 255 256 257
  /*! Minimal mass of all the #part */
  float min_part_mass;

  /*! Minimal mass of all the dark-matter #gpart */
  float min_gpart_mass;

  /*! Minimal mass of all the #spart */
  float min_spart_mass;

Loic Hausammann's avatar
Loic Hausammann committed
258 259 260
  /*! Minimal mass of all the #sink */
  float min_sink_mass;

261 262 263
  /*! Minimal mass of all the #bpart */
  float min_bpart_mass;

264 265 266 267 268 269 270 271 272
  /*! Sum of the norm of the velocity of all the #part */
  float sum_part_vel_norm;

  /*! Sum of the norm of the velocity of all the dark-matter #gpart */
  float sum_gpart_vel_norm;

  /*! Sum of the norm of the velocity of all the #spart */
  float sum_spart_vel_norm;

Loic Hausammann's avatar
Loic Hausammann committed
273 274 275
  /*! Sum of the norm of the velocity of all the #sink */
  float sum_sink_vel_norm;

276 277 278
  /*! Sum of the norm of the velocity of all the #bpart */
  float sum_bpart_vel_norm;

279 280 281
  /*! Initial value of the smoothing length read from the parameter file */
  float initial_spart_h;

282 283 284
  /*! Initial value of the smoothing length read from the parameter file */
  float initial_bpart_h;

285
  /*! General-purpose lock for this space. */
286
  swift_lock_type lock;
287

288
  /*! Number of queues in the system. */
289 290
  int nr_queues;

291
  /*! The associated engine. */
292
  struct engine *e;
Pedro Gonnet's avatar
Pedro Gonnet committed
293

294 295 296
  /*! The group information returned by VELOCIraptor for each #gpart. */
  struct velociraptor_gpart_data *gpart_group_data;

Loic Hausammann's avatar
Loic Hausammann committed
297 298 299
  /*! Structure dealing with the computation of a unique ID */
  struct unique_id unique_id;

300 301 302
#ifdef WITH_MPI

  /*! Buffers for parts that we will receive from foreign cells. */
303
  struct part *parts_foreign;
304
  size_t nr_parts_foreign, size_parts_foreign;
305 306

  /*! Buffers for g-parts that we will receive from foreign cells. */
307 308
  struct gpart *gparts_foreign;
  size_t nr_gparts_foreign, size_gparts_foreign;
309

310
  /*! Buffers for s-parts that we will receive from foreign cells. */
311 312 313
  struct spart *sparts_foreign;
  size_t nr_sparts_foreign, size_sparts_foreign;

314 315 316
  /*! Buffers for b-parts that we will receive from foreign cells. */
  struct bpart *bparts_foreign;
  size_t nr_bparts_foreign, size_bparts_foreign;
317

318
#endif
319
};
Pedro Gonnet's avatar
Pedro Gonnet committed
320

Pedro Gonnet's avatar
Pedro Gonnet committed
321
/* Function prototypes. */
322
void space_free_buff_sort_indices(struct space *s);
323 324 325
void space_parts_sort(struct part *parts, struct xpart *xparts, int *ind,
                      int *counts, int num_bins, ptrdiff_t parts_offset);
void space_gparts_sort(struct gpart *gparts, struct part *parts,
Loic Hausammann's avatar
Loic Hausammann committed
326 327 328
                       struct sink *sinks, struct spart *sparts,
                       struct bpart *bparts, int *ind, int *counts,
                       int num_bins);
329 330
void space_sparts_sort(struct spart *sparts, int *ind, int *counts,
                       int num_bins, ptrdiff_t sparts_offset);
331 332
void space_bparts_sort(struct bpart *bparts, int *ind, int *counts,
                       int num_bins, ptrdiff_t bparts_offset);
Loic Hausammann's avatar
Loic Hausammann committed
333 334
void space_sinks_sort(struct sink *sinks, int *ind, int *counts, int num_bins,
                      ptrdiff_t sinks_offset);
335
void space_getcells(struct space *s, int nr_cells, struct cell **cells);
lhausamm's avatar
lhausamm committed
336
void space_init(struct space *s, struct swift_params *params,
337
                const struct cosmology *cosmo, double dim[3],
338
                const struct hydro_props *hydro_properties, struct part *parts,
Loic Hausammann's avatar
Loic Hausammann committed
339 340
                struct gpart *gparts, struct sink *sinks, struct spart *sparts,
                struct bpart *bparts, size_t Npart, size_t Ngpart, size_t Nsink,
341
                size_t Nspart, size_t Nbpart, int periodic, int replicate,
342
                int remap_ids, int generate_gas_in_ics, int hydro, int gravity,
Loic Hausammann's avatar
Loic Hausammann committed
343 344
                int star_formation, int DM_background, int verbose, int dry_run,
                int nr_nodes);
345
void space_sanitize(struct space *s);
346 347 348 349 350
void space_map_cells_pre(struct space *s, int full,
                         void (*fun)(struct cell *c, void *data), void *data);
void space_map_parts(struct space *s,
                     void (*fun)(struct part *p, struct cell *c, void *data),
                     void *data);
351
void space_map_parts_xparts(struct space *s,
352 353
                            void (*fun)(struct part *p, struct xpart *xp,
                                        struct cell *c));
354 355
void space_map_cells_post(struct space *s, int full,
                          void (*fun)(struct cell *c, void *data), void *data);
356
void space_rebuild(struct space *s, int repartitioned, int verbose);
357
void space_recycle(struct space *s, struct cell *c);
358 359
void space_recycle_list(struct space *s, struct cell *cell_list_begin,
                        struct cell *cell_list_end,
360 361
                        struct gravity_tensors *multipole_list_begin,
                        struct gravity_tensors *multipole_list_end);
362
void space_split(struct space *s, int verbose);
363
void space_reorder_extras(struct space *s, int verbose);
364
void space_split_mapper(void *map_data, int num_elements, void *extra_data);
365
void space_list_useful_top_level_cells(struct space *s);
366
void space_parts_get_cell_index(struct space *s, int *ind, int *cell_counts,
367 368
                                size_t *count_inhibited_parts,
                                size_t *count_extra_parts, int verbose);
369
void space_gparts_get_cell_index(struct space *s, int *gind, int *cell_counts,
370 371
                                 size_t *count_inhibited_gparts,
                                 size_t *count_extra_gparts, int verbose);
372
void space_sparts_get_cell_index(struct space *s, int *sind, int *cell_counts,
373 374
                                 size_t *count_inhibited_sparts,
                                 size_t *count_extra_sparts, int verbose);
375 376 377
void space_bparts_get_cell_index(struct space *s, int *sind, int *cell_counts,
                                 size_t *count_inhibited_bparts,
                                 size_t *count_extra_bparts, int verbose);
Loic Hausammann's avatar
Loic Hausammann committed
378 379 380
void space_sinks_get_cell_index(struct space *s, int *sind, int *cell_counts,
                                size_t *count_inhibited_sinks,
                                size_t *count_extra_sinks, int verbose);
381
void space_synchronize_particle_positions(struct space *s);
lhausamm's avatar
lhausamm committed
382 383
void space_first_init_parts(struct space *s, int verbose);
void space_first_init_gparts(struct space *s, int verbose);
lhausamm's avatar
lhausamm committed
384
void space_first_init_sparts(struct space *s, int verbose);
385
void space_first_init_bparts(struct space *s, int verbose);
Loic Hausammann's avatar
Loic Hausammann committed
386
void space_first_init_sinks(struct space *s, int verbose);
387 388
void space_init_parts(struct space *s, int verbose);
void space_init_gparts(struct space *s, int verbose);
389
void space_init_sparts(struct space *s, int verbose);
390
void space_init_bparts(struct space *s, int verbose);
Loic Hausammann's avatar
Loic Hausammann committed
391
void space_init_sinks(struct space *s, int verbose);
392
void space_convert_quantities(struct space *s, int verbose);
393
void space_link_cleanup(struct space *s);
394 395 396 397
void space_check_drift_point(struct space *s, integertime_t ti_drift,
                             int multipole);
void space_check_top_multipoles_drift_point(struct space *s,
                                            integertime_t ti_drift);
398
void space_check_timesteps(const struct space *s);
399
void space_check_limiter(struct space *s);
400
void space_check_swallow(struct space *s);
401
void space_check_sort_flags(struct space *s);
402 403
void space_remap_ids(struct space *s, int nr_nodes, int verbose);
long long space_get_max_parts_id(struct space *s);
404
void space_replicate(struct space *s, int replicate, int verbose);
405
void space_generate_gas(struct space *s, const struct cosmology *cosmo,
406
                        const struct hydro_props *hydro_properties,
407 408
                        const int periodic, const int with_DM_background,
                        const double dim[3], const int verbose);
409 410
void space_check_cosmology(struct space *s, const struct cosmology *cosmo,
                           int rank);
411
void space_reset_task_counters(struct space *s);
412
void space_clean(struct space *s);
413
void space_free_cells(struct space *s);
414

415
void space_free_foreign_parts(struct space *s, const int clear_cell_pointers);
416

417 418
void space_struct_dump(struct space *s, FILE *stream);
void space_struct_restore(struct space *s, FILE *stream);
Loic Hausammann's avatar
Loic Hausammann committed
419
void space_write_cell_hierarchy(const struct space *s, int j);
Loic Hausammann's avatar
Loic Hausammann committed
420 421
void space_compute_star_formation_stats(const struct space *s,
                                        struct star_formation *star_form);
422
#endif /* SWIFT_SPACE_H */