io_properties.h 20 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
/*******************************************************************************
 * 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_IO_PROPERTIES_H
#define SWIFT_IO_PROPERTIES_H

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

25
/* Local includes. */
26
#include "common_io.h"
27
#include "inline.h"
28
#include "part.h"
29

Matthieu Schaller's avatar
Matthieu Schaller committed
30 31 32
/* Standard includes. */
#include <string.h>

33 34 35 36
/**
 * @brief The two sorts of data present in the GADGET IC files: compulsory to
 * start a run or optional.
 */
37
enum DATA_IMPORTANCE { COMPULSORY = 1, OPTIONAL = 0, UNUSED = -1 };
38

39 40
/* Helper typedefs */
typedef void (*conversion_func_part_float)(const struct engine*,
41 42
                                           const struct part*,
                                           const struct xpart*, float*);
43
typedef void (*conversion_func_part_double)(const struct engine*,
44 45
                                            const struct part*,
                                            const struct xpart*, double*);
46 47 48
typedef void (*conversion_func_part_long_long)(const struct engine*,
                                               const struct part*,
                                               const struct xpart*, long long*);
49 50 51 52
typedef void (*conversion_func_gpart_float)(const struct engine*,
                                            const struct gpart*, float*);
typedef void (*conversion_func_gpart_double)(const struct engine*,
                                             const struct gpart*, double*);
53 54 55
typedef void (*conversion_func_gpart_long_long)(const struct engine*,
                                                const struct gpart*,
                                                long long*);
56 57 58 59
typedef void (*conversion_func_spart_float)(const struct engine*,
                                            const struct spart*, float*);
typedef void (*conversion_func_spart_double)(const struct engine*,
                                             const struct spart*, double*);
60 61 62
typedef void (*conversion_func_spart_long_long)(const struct engine*,
                                                const struct spart*,
                                                long long*);
63

64 65 66 67 68 69 70 71 72
/**
 * @brief The properties of a given dataset for i/o
 */
struct io_props {

  /* Name */
  char name[FIELD_BUFFER_SIZE];

  /* Type of the field */
73
  enum IO_DATA_TYPE type;
74 75 76 77 78 79 80 81

  /* Dimension (1D, 3D, ...) */
  int dimension;

  /* Is it compulsory ? (input only) */
  enum DATA_IMPORTANCE importance;

  /* Units of the quantity */
82
  enum unit_conversion_factor units;
83 84 85 86

  /* Pointer to the field of the first particle in the array */
  char* field;

87 88 89 90
  /* Pointer to the start of the temporary buffer used in i/o */
  char* start_temp_c;
  float* start_temp_f;
  double* start_temp_d;
91
  long long* start_temp_l;
92 93 94 95

  /* Pointer to the engine */
  const struct engine* e;

96 97
  /* The size of the particles */
  size_t partSize;
98 99

  /* The particle arrays */
100
  const struct part* parts;
101
  const struct xpart* xparts;
102
  const struct gpart* gparts;
103
  const struct spart* sparts;
104 105 106

  /* Are we converting? */
  int conversion;
107 108

  /* Conversion function for part */
109 110
  conversion_func_part_float convert_part_f;
  conversion_func_part_double convert_part_d;
111
  conversion_func_part_long_long convert_part_l;
112

113
  /* Conversion function for gpart */
114 115
  conversion_func_gpart_float convert_gpart_f;
  conversion_func_gpart_double convert_gpart_d;
116
  conversion_func_gpart_long_long convert_gpart_l;
117 118 119 120

  /* Conversion function for spart */
  conversion_func_spart_float convert_spart_f;
  conversion_func_spart_double convert_spart_d;
121
  conversion_func_spart_long_long convert_spart_l;
122 123 124 125 126 127 128 129 130 131 132 133 134 135
};

/**
 * @brief Constructs an #io_props from its parameters
 */
#define io_make_input_field(name, type, dim, importance, units, part, field) \
  io_make_input_field_(name, type, dim, importance, units,                   \
                       (char*)(&(part[0]).field), sizeof(part[0]))

/**
 * @brief Construct an #io_props from its parameters
 *
 * @param name Name of the field to read
 * @param type The type of the data
136
 * @param dimension Dataset dimension (1D, 3D, ...)
137 138 139 140 141 142 143
 * @param importance Is this dataset compulsory ?
 * @param units The units of the dataset
 * @param field Pointer to the field of the first particle
 * @param partSize The size in byte of the particle
 *
 * Do not call this function directly. Use the macro defined above.
 */
144
INLINE static struct io_props io_make_input_field_(
145
    const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
146 147
    enum DATA_IMPORTANCE importance, enum unit_conversion_factor units,
    char* field, size_t partSize) {
148 149 150 151 152 153 154 155
  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
  r.importance = importance;
  r.units = units;
  r.field = field;
  r.partSize = partSize;
156
  r.parts = NULL;
157
  r.xparts = NULL;
158
  r.gparts = NULL;
159
  r.sparts = NULL;
160 161 162
  r.conversion = 0;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
163
  r.convert_part_l = NULL;
164 165
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = NULL;
166
  r.convert_gpart_l = NULL;
167 168
  r.convert_spart_f = NULL;
  r.convert_spart_d = NULL;
169
  r.convert_spart_l = NULL;
170 171 172 173 174 175 176

  return r;
}

/**
 * @brief Constructs an #io_props from its parameters
 */
177 178 179
#define io_make_output_field(name, type, dim, units, part, field)          \
  io_make_output_field_(name, type, dim, units, (char*)(&(part[0]).field), \
                        sizeof(part[0]))
180 181 182 183 184 185

/**
 * @brief Construct an #io_props from its parameters
 *
 * @param name Name of the field to read
 * @param type The type of the data
186
 * @param dimension Dataset dimension (1D, 3D, ...)
187 188 189 190 191 192
 * @param units The units of the dataset
 * @param field Pointer to the field of the first particle
 * @param partSize The size in byte of the particle
 *
 * Do not call this function directly. Use the macro defined above.
 */
193
INLINE static struct io_props io_make_output_field_(
194
    const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
195
    enum unit_conversion_factor units, char* field, size_t partSize) {
196 197 198 199
  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
200
  r.importance = UNUSED;
201 202 203
  r.units = units;
  r.field = field;
  r.partSize = partSize;
204 205
  r.parts = NULL;
  r.gparts = NULL;
206
  r.sparts = NULL;
207 208 209
  r.conversion = 0;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
210
  r.convert_part_l = NULL;
211 212
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = NULL;
213
  r.convert_gpart_l = NULL;
214 215
  r.convert_spart_f = NULL;
  r.convert_spart_d = NULL;
216
  r.convert_spart_l = NULL;
217 218 219 220 221 222 223

  return r;
}

/**
 * @brief Constructs an #io_props (with conversion) from its parameters
 */
224 225 226 227
#define io_make_output_field_convert_part(name, type, dim, units, part, xpart, \
                                          convert)                             \
  io_make_output_field_convert_part_##type(                                    \
      name, type, dim, units, sizeof(part[0]), part, xpart, convert)
228 229 230 231 232 233 234 235 236 237

/**
 * @brief Construct an #io_props from its parameters
 *
 * @param name Name of the field to read
 * @param type The type of the data
 * @param dimension Dataset dimension (1D, 3D, ...)
 * @param units The units of the dataset
 * @param partSize The size in byte of the particle
 * @param parts The particle array
238
 * @param xparts The xparticle array
239 240 241 242
 * @param functionPtr The function used to convert a particle to a float
 *
 * Do not call this function directly. Use the macro defined above.
 */
243
INLINE static struct io_props io_make_output_field_convert_part_FLOAT(
244
    const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
245
    enum unit_conversion_factor units, size_t partSize,
246 247
    const struct part* parts, const struct xpart* xparts,
    conversion_func_part_float functionPtr) {
248 249 250 251 252

  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
253
  r.importance = UNUSED;
254
  r.units = units;
255
  r.field = NULL;
256 257
  r.partSize = partSize;
  r.parts = parts;
258
  r.xparts = xparts;
259
  r.gparts = NULL;
260
  r.sparts = NULL;
261 262 263
  r.conversion = 1;
  r.convert_part_f = functionPtr;
  r.convert_part_d = NULL;
264
  r.convert_part_l = NULL;
265 266
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = NULL;
267
  r.convert_gpart_l = NULL;
268 269
  r.convert_spart_f = NULL;
  r.convert_spart_d = NULL;
270
  r.convert_spart_l = NULL;
271 272 273 274 275 276 277 278 279 280 281 282 283

  return r;
}

/**
 * @brief Construct an #io_props from its parameters
 *
 * @param name Name of the field to read
 * @param type The type of the data
 * @param dimension Dataset dimension (1D, 3D, ...)
 * @param units The units of the dataset
 * @param partSize The size in byte of the particle
 * @param parts The particle array
284
 * @param xparts The xparticle array
285
 * @param functionPtr The function used to convert a particle to a double
286 287 288
 *
 * Do not call this function directly. Use the macro defined above.
 */
289
INLINE static struct io_props io_make_output_field_convert_part_DOUBLE(
290
    const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
291
    enum unit_conversion_factor units, size_t partSize,
292 293
    const struct part* parts, const struct xpart* xparts,
    conversion_func_part_double functionPtr) {
294 295 296 297 298 299 300 301 302 303

  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
  r.importance = UNUSED;
  r.units = units;
  r.field = NULL;
  r.partSize = partSize;
  r.parts = parts;
304
  r.xparts = xparts;
305
  r.gparts = NULL;
306
  r.sparts = NULL;
307 308 309
  r.conversion = 1;
  r.convert_part_f = NULL;
  r.convert_part_d = functionPtr;
310
  r.convert_part_l = NULL;
311 312
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = NULL;
313
  r.convert_gpart_l = NULL;
314 315
  r.convert_spart_f = NULL;
  r.convert_spart_d = NULL;
316
  r.convert_spart_l = NULL;
317 318 319 320

  return r;
}

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 360 361 362 363 364 365 366
/**
 * @brief Construct an #io_props from its parameters
 *
 * @param name Name of the field to read
 * @param type The type of the data
 * @param dimension Dataset dimension (1D, 3D, ...)
 * @param units The units of the dataset
 * @param partSize The size in byte of the particle
 * @param parts The particle array
 * @param xparts The xparticle array
 * @param functionPtr The function used to convert a particle to a double
 *
 * Do not call this function directly. Use the macro defined above.
 */
INLINE static struct io_props io_make_output_field_convert_part_LONGLONG(
    const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
    enum unit_conversion_factor units, size_t partSize,
    const struct part* parts, const struct xpart* xparts,
    conversion_func_part_long_long functionPtr) {

  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
  r.importance = UNUSED;
  r.units = units;
  r.field = NULL;
  r.partSize = partSize;
  r.parts = parts;
  r.xparts = xparts;
  r.gparts = NULL;
  r.sparts = NULL;
  r.conversion = 1;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
  r.convert_part_l = functionPtr;
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = NULL;
  r.convert_gpart_l = NULL;
  r.convert_spart_f = NULL;
  r.convert_spart_d = NULL;
  r.convert_spart_l = NULL;

  return r;
}

367 368 369
/**
 * @brief Constructs an #io_props (with conversion) from its parameters
 */
370 371 372 373
#define io_make_output_field_convert_gpart(name, type, dim, units, gpart, \
                                           convert)                       \
  io_make_output_field_convert_gpart_##type(name, type, dim, units,       \
                                            sizeof(gpart[0]), gpart, convert)
374 375 376 377 378 379 380 381

/**
 * @brief Construct an #io_props from its parameters
 *
 * @param name Name of the field to read
 * @param type The type of the data
 * @param dimension Dataset dimension (1D, 3D, ...)
 * @param units The units of the dataset
382
 * @param gpartSize The size in byte of the particle
383 384
 * @param gparts The particle array
 * @param functionPtr The function used to convert a g-particle to a float
385 386 387
 *
 * Do not call this function directly. Use the macro defined above.
 */
388
INLINE static struct io_props io_make_output_field_convert_gpart_FLOAT(
389
    const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
390 391
    enum unit_conversion_factor units, size_t gpartSize,
    const struct gpart* gparts, conversion_func_gpart_float functionPtr) {
392 393 394 395 396

  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
397
  r.importance = UNUSED;
398
  r.units = units;
399 400 401
  r.field = NULL;
  r.partSize = gpartSize;
  r.parts = NULL;
402
  r.xparts = NULL;
403
  r.gparts = gparts;
404
  r.sparts = NULL;
405 406 407
  r.conversion = 1;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
408
  r.convert_part_l = NULL;
409 410
  r.convert_gpart_f = functionPtr;
  r.convert_gpart_d = NULL;
411
  r.convert_gpart_l = NULL;
412 413
  r.convert_spart_f = NULL;
  r.convert_spart_d = NULL;
414
  r.convert_spart_l = NULL;
415 416 417 418 419 420 421 422 423 424 425 426 427

  return r;
}

/**
 * @brief Construct an #io_props from its parameters
 *
 * @param name Name of the field to read
 * @param type The type of the data
 * @param dimension Dataset dimension (1D, 3D, ...)
 * @param units The units of the dataset
 * @param gpartSize The size in byte of the particle
 * @param gparts The particle array
428
 * @param functionPtr The function used to convert a g-particle to a double
429 430 431
 *
 * Do not call this function directly. Use the macro defined above.
 */
432
INLINE static struct io_props io_make_output_field_convert_gpart_DOUBLE(
433
    const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
434 435 436 437 438 439 440 441 442 443 444
    enum unit_conversion_factor units, size_t gpartSize,
    const struct gpart* gparts, conversion_func_gpart_double functionPtr) {

  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
  r.importance = UNUSED;
  r.units = units;
  r.field = NULL;
  r.partSize = gpartSize;
445
  r.parts = NULL;
446
  r.xparts = NULL;
447
  r.gparts = gparts;
448
  r.sparts = NULL;
449 450 451
  r.conversion = 1;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
452
  r.convert_part_l = NULL;
453 454
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = functionPtr;
455
  r.convert_gpart_l = NULL;
456 457
  r.convert_spart_f = NULL;
  r.convert_spart_d = NULL;
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 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
  r.convert_spart_l = NULL;

  return r;
}

/**
 * @brief Construct an #io_props from its parameters
 *
 * @param name Name of the field to read
 * @param type The type of the data
 * @param dimension Dataset dimension (1D, 3D, ...)
 * @param units The units of the dataset
 * @param gpartSize The size in byte of the particle
 * @param gparts The particle array
 * @param functionPtr The function used to convert a g-particle to a double
 *
 * Do not call this function directly. Use the macro defined above.
 */
INLINE static struct io_props io_make_output_field_convert_gpart_LONGLONG(
    const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
    enum unit_conversion_factor units, size_t gpartSize,
    const struct gpart* gparts, conversion_func_gpart_long_long functionPtr) {

  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
  r.importance = UNUSED;
  r.units = units;
  r.field = NULL;
  r.partSize = gpartSize;
  r.parts = NULL;
  r.xparts = NULL;
  r.gparts = gparts;
  r.sparts = NULL;
  r.conversion = 1;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
  r.convert_part_l = NULL;
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = NULL;
  r.convert_gpart_l = functionPtr;
  r.convert_spart_f = NULL;
  r.convert_spart_d = NULL;
  r.convert_spart_l = NULL;
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547

  return r;
}

/**
 * @brief Constructs an #io_props (with conversion) from its parameters
 */
#define io_make_output_field_convert_spart(name, type, dim, units, spart, \
                                           convert)                       \
  io_make_output_field_convert_spart_##type(name, type, dim, units,       \
                                            sizeof(spart[0]), spart, convert)

/**
 * @brief Construct an #io_props from its parameters
 *
 * @param name Name of the field to read
 * @param type The type of the data
 * @param dimension Dataset dimension (1D, 3D, ...)
 * @param units The units of the dataset
 * @param spartSize The size in byte of the particle
 * @param sparts The particle array
 * @param functionPtr The function used to convert a g-particle to a float
 *
 * Do not call this function directly. Use the macro defined above.
 */
INLINE static struct io_props io_make_output_field_convert_spart_FLOAT(
    const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
    enum unit_conversion_factor units, size_t spartSize,
    const struct spart* sparts, conversion_func_spart_float functionPtr) {

  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
  r.importance = UNUSED;
  r.units = units;
  r.field = NULL;
  r.partSize = spartSize;
  r.parts = NULL;
  r.xparts = NULL;
  r.gparts = NULL;
  r.sparts = sparts;
  r.conversion = 1;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
548
  r.convert_part_l = NULL;
549 550
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = NULL;
551
  r.convert_gpart_l = NULL;
552 553
  r.convert_spart_f = functionPtr;
  r.convert_spart_d = NULL;
554
  r.convert_spart_l = NULL;
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591

  return r;
}

/**
 * @brief Construct an #io_props from its parameters
 *
 * @param name Name of the field to read
 * @param type The type of the data
 * @param dimension Dataset dimension (1D, 3D, ...)
 * @param units The units of the dataset
 * @param spartSize The size in byte of the particle
 * @param sparts The particle array
 * @param functionPtr The function used to convert a s-particle to a double
 *
 * Do not call this function directly. Use the macro defined above.
 */
INLINE static struct io_props io_make_output_field_convert_spart_DOUBLE(
    const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
    enum unit_conversion_factor units, size_t spartSize,
    const struct spart* sparts, conversion_func_spart_double functionPtr) {

  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
  r.importance = UNUSED;
  r.units = units;
  r.field = NULL;
  r.partSize = spartSize;
  r.parts = NULL;
  r.xparts = NULL;
  r.gparts = NULL;
  r.sparts = sparts;
  r.conversion = 1;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
592
  r.convert_part_l = NULL;
593 594
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = NULL;
595
  r.convert_gpart_l = NULL;
596 597
  r.convert_spart_f = NULL;
  r.convert_spart_d = functionPtr;
598
  r.convert_spart_l = NULL;
599 600 601 602

  return r;
}

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 635 636 637 638 639 640 641 642 643 644 645 646
/**
 * @brief Construct an #io_props from its parameters
 *
 * @param name Name of the field to read
 * @param type The type of the data
 * @param dimension Dataset dimension (1D, 3D, ...)
 * @param units The units of the dataset
 * @param spartSize The size in byte of the particle
 * @param sparts The particle array
 * @param functionPtr The function used to convert a s-particle to a double
 *
 * Do not call this function directly. Use the macro defined above.
 */
INLINE static struct io_props io_make_output_field_convert_spart_LONGLONG(
    const char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
    enum unit_conversion_factor units, size_t spartSize,
    const struct spart* sparts, conversion_func_spart_long_long functionPtr) {

  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
  r.importance = UNUSED;
  r.units = units;
  r.field = NULL;
  r.partSize = spartSize;
  r.parts = NULL;
  r.xparts = NULL;
  r.gparts = NULL;
  r.sparts = sparts;
  r.conversion = 1;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
  r.convert_part_l = NULL;
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = NULL;
  r.convert_gpart_l = NULL;
  r.convert_spart_f = NULL;
  r.convert_spart_d = NULL;
  r.convert_spart_l = functionPtr;

  return r;
}

647
#endif /* SWIFT_IO_PROPERTIES_H */