logger.c 16 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
/*******************************************************************************
 * This file is part of SWIFT.
 * Copyright (c) 2017 Pedro Gonnet (pedro.gonnet@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/>.
 *
 ******************************************************************************/

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

23
#ifdef HAVE_POSIX_FALLOCATE /* Are we on a sensible platform? */
lhausamm's avatar
lhausamm committed
24
#ifdef WITH_LOGGER
25

26
/* Some standard headers. */
27
#include <stdint.h>
28
29
#include <stdlib.h>
#include <string.h>
lhausamm's avatar
lhausamm committed
30
#include <hdf5.h>
31
32
33
34
35
36
37
38
39

/* This object's header. */
#include "logger.h"

/* Local headers. */
#include "atomic.h"
#include "dump.h"
#include "error.h"
#include "part.h"
lhausamm's avatar
lhausamm committed
40
41
#include "units.h"
#include "engine.h"
42
43
44

/**
 * @brief Compute the size of a message given its mask.
45
46
47
48
 *
 * @param mask The mask that will be used to dump a #part or #gpart.
 *
 * @return The size of the logger message in bytes.
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
 */
int logger_size(unsigned int mask) {

  /* Start with 8 bytes for the header. */
  int size = 8;

  /* Is this a particle or a timestep? */
  if (mask & logger_mask_timestamp) {

    /* The timestamp should not contain any other bits. */
    if (mask != logger_mask_timestamp)
      error("Timestamps should not include any other data.");

    /* A timestamp consists of an unsigned long long int. */
    size += sizeof(unsigned long long int);

  } else {

    /* Particle position as three doubles. */
    if (mask & logger_mask_x) size += 3 * sizeof(double);

    /* Particle velocity as three floats. */
    if (mask & logger_mask_v) size += 3 * sizeof(float);

    /* Particle accelleration as three floats. */
    if (mask & logger_mask_a) size += 3 * sizeof(float);

    /* Particle internal energy as a single float. */
    if (mask & logger_mask_u) size += sizeof(float);

    /* Particle smoothing length as a single float. */
    if (mask & logger_mask_h) size += sizeof(float);

    /* Particle density as a single float. */
    if (mask & logger_mask_rho) size += sizeof(float);

    /* Particle constants, which is a bit more complicated. */
    if (mask & logger_mask_rho) {
      size += sizeof(float) +     // mass
              sizeof(long long);  // id
    }
  }

  return size;
}
94
95
96
97

/**
 * @brief Dump a #part to the log.
 *
Pedro Gonnet's avatar
Pedro Gonnet committed
98
 * @param p The #part to dump.
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
 * @param mask The mask of the data to dump.
 * @param offset Pointer to the offset of the previous log of this particle.
 * @param dump The #dump in which to log the particle data.
 */
void logger_log_part(struct part *p, unsigned int mask, size_t *offset,
                     struct dump *dump) {

  /* Make sure we're not writing a timestamp. */
  if (mask & logger_mask_timestamp)
    error("You should not log particles as timestamps.");

  /* Start by computing the size of the message. */
  const int size = logger_size(mask);

  /* Allocate a chunk of memory in the dump of the right size. */
  size_t offset_new;
115
  char *buff = (char *)dump_get(dump, size, &offset_new);
116
117

  /* Write the header. */
Pedro Gonnet's avatar
Pedro Gonnet committed
118
  uint64_t temp = (((uint64_t)(offset_new - *offset)) & 0xffffffffffffffULL) |
119
                  ((uint64_t)mask << 56);
120
121
122
123
124
  memcpy(buff, &temp, 8);
  buff += 8;

  /* Particle position as three doubles. */
  if (mask & logger_mask_x) {
125
    memcpy(buff, p->x, 3 * sizeof(double));
126
127
128
129
130
    buff += 3 * sizeof(double);
  }

  /* Particle velocity as three floats. */
  if (mask & logger_mask_v) {
131
    memcpy(buff, p->v, 3 * sizeof(float));
132
133
134
135
136
    buff += 3 * sizeof(float);
  }

  /* Particle accelleration as three floats. */
  if (mask & logger_mask_a) {
137
    memcpy(buff, p->a_hydro, 3 * sizeof(float));
138
139
140
    buff += 3 * sizeof(float);
  }

141
142
#if defined(GADGET2_SPH)

143
144
  /* Particle internal energy as a single float. */
  if (mask & logger_mask_u) {
145
    memcpy(buff, &p->entropy, sizeof(float));
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
    buff += sizeof(float);
  }

  /* Particle smoothing length as a single float. */
  if (mask & logger_mask_h) {
    memcpy(buff, &p->h, sizeof(float));
    buff += sizeof(float);
  }

  /* Particle density as a single float. */
  if (mask & logger_mask_rho) {
    memcpy(buff, &p->rho, sizeof(float));
    buff += sizeof(float);
  }

  /* Particle constants, which is a bit more complicated. */
lhausamm's avatar
lhausamm committed
162
  if (mask & logger_mask_consts) {
163
164
165
166
167
168
    memcpy(buff, &p->mass, sizeof(float));
    buff += sizeof(float);
    memcpy(buff, &p->id, sizeof(long long));
    buff += sizeof(long long);
  }

169
170
#endif

171
172
173
  /* Update the log message offset. */
  *offset = offset_new;
}
174
175
176
177

/**
 * @brief Dump a #gpart to the log.
 *
Pedro Gonnet's avatar
Pedro Gonnet committed
178
 * @param p The #gpart to dump.
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
 * @param mask The mask of the data to dump.
 * @param offset Pointer to the offset of the previous log of this particle.
 * @param dump The #dump in which to log the particle data.
 */
void logger_log_gpart(struct gpart *p, unsigned int mask, size_t *offset,
                      struct dump *dump) {

  /* Make sure we're not writing a timestamp. */
  if (mask & logger_mask_timestamp)
    error("You should not log particles as timestamps.");

  /* Make sure we're not looging fields not supported by gparts. */
  if (mask & (logger_mask_u | logger_mask_rho))
    error("Can't log SPH quantities for gparts.");

  /* Start by computing the size of the message. */
  const int size = logger_size(mask);

  /* Allocate a chunk of memory in the dump of the right size. */
  size_t offset_new;
199
  char *buff = (char *)dump_get(dump, size, &offset_new);
200
201

  /* Write the header. */
Pedro Gonnet's avatar
Pedro Gonnet committed
202
  uint64_t temp = (((uint64_t)(offset_new - *offset)) & 0xffffffffffffffULL) |
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
                  ((uint64_t)mask << 56);
  memcpy(buff, &temp, 8);
  buff += 8;

  /* Particle position as three doubles. */
  if (mask & logger_mask_x) {
    memcpy(buff, p->x, 3 * sizeof(double));
    buff += 3 * sizeof(double);
  }

  /* Particle velocity as three floats. */
  if (mask & logger_mask_v) {
    memcpy(buff, p->v_full, 3 * sizeof(float));
    buff += 3 * sizeof(float);
  }

  /* Particle accelleration as three floats. */
  if (mask & logger_mask_a) {
    memcpy(buff, p->a_grav, 3 * sizeof(float));
    buff += 3 * sizeof(float);
  }

  /* Particle constants, which is a bit more complicated. */
lhausamm's avatar
lhausamm committed
226
  if (mask & logger_mask_consts) {
227
228
229
230
231
232
233
234
235
236
237
    memcpy(buff, &p->mass, sizeof(float));
    buff += sizeof(float);
    memcpy(buff, &p->id_or_neg_offset, sizeof(long long));
    buff += sizeof(long long);
  }

  /* Update the log message offset. */
  *offset = offset_new;
}

void logger_log_timestamp(unsigned long long int timestamp, size_t *offset,
Pedro Gonnet's avatar
Pedro Gonnet committed
238
                          struct dump *dump) {
239
240
241
242
243
244

  /* Start by computing the size of the message. */
  const int size = logger_size(logger_mask_timestamp);

  /* Allocate a chunk of memory in the dump of the right size. */
  size_t offset_new;
245
  char *buff = (char *)dump_get(dump, size, &offset_new);
246
247

  /* Write the header. */
Pedro Gonnet's avatar
Pedro Gonnet committed
248
  uint64_t temp = (((uint64_t)(offset_new - *offset)) & 0xffffffffffffffULL) |
249
250
251
                  ((uint64_t)logger_mask_timestamp << 56);
  memcpy(buff, &temp, 8);
  buff += 8;
Pedro Gonnet's avatar
Pedro Gonnet committed
252

253
254
255
256
257
258
  /* Store the timestamp. */
  memcpy(buff, &timestamp, sizeof(unsigned long long int));

  /* Update the log message offset. */
  *offset = offset_new;
}
Pedro Gonnet's avatar
Pedro Gonnet committed
259

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
 * @brief Write a file header to a logger file
 *
 * @param offset Pointer to the offset of the previous log of this particle.
 * @param dump The #dump in which to log the particle data.
 *
 */
void logger_write_file_header(struct dump *dump) {
  size_t i, j;
  char *buff, *name_buff, *skip_header;
  size_t *file_offset;
  size_t mask_size;

  struct logger_const log_const;
  logger_const_init(&log_const);

  file_offset = &dump->file_offset;
    
  if (*file_offset != 0) error("Something was already written in the dump file");

  /* Write version information */
  buff = dump_get(dump, LOGGER_VERSION_SIZE, file_offset);
  memcpy(buff, LOGGER_VERSION, LOGGER_VERSION_SIZE);
 
  /* write number of bytes used for the offsets */
  buff = dump_get(dump, LOGGER_OFFSET_SIZE, file_offset);
  memcpy(buff, &log_const.offset, LOGGER_OFFSET_SIZE);

  /* will write the offset of the first particle here */
  skip_header = dump_get(dump, log_const.name, file_offset);

  /* write number of bytes used for names */
  buff = dump_get(dump, LOGGER_NAME_SIZE, file_offset);
  memcpy(buff, &log_const.name, LOGGER_NAME_SIZE);


  mask_size = log_const.nber_mask * (log_const.name + log_const.mask);
  name_buff = dump_get(dump, mask_size, file_offset);
  for(i=0; i<log_const.nber_mask; i++) {
    j = i * (log_const.name + log_const.mask);

    // name
    memcpy(name_buff, &log_const.masks_name[j], log_const.name);
    name_buff += j;

    // mask
    memcpy(name_buff, &log_const.masks[i], log_const.mask);
    name_buff += log_const.mask;
  }
  

  /* last step */
  memcpy(skip_header, file_offset, log_const.offset);
  
}

void logger_const_init(struct logger_const* log_const) {
  log_const->name = 10;
  log_const->offset = 7;
  log_const->mask = 1;

  log_const->nber_mask = 8;

  // masks value
  log_const->masks = malloc(sizeof(size_t)*log_const->nber_mask);
  log_const->masks[0] = logger_mask_x;
  log_const->masks[1] = logger_mask_v;
  log_const->masks[2] = logger_mask_a;
  log_const->masks[3] = logger_mask_u;
  log_const->masks[4] = logger_mask_h;
  log_const->masks[5] = logger_mask_rho;
  log_const->masks[6] = logger_mask_consts;
  log_const->masks[7] = logger_mask_timestamp;

  // masks name
  size_t name_size = sizeof(char) * log_const->name;
  log_const->masks_name = malloc(name_size*log_const->nber_mask);
  char *tmp = malloc(sizeof(char) * log_const->name);

  tmp = "position";
  memcpy(log_const->masks_name, &tmp, log_const->name);
  log_const->masks_name += log_const->name * sizeof(char);

  tmp = "velocity";
  memcpy(log_const->masks_name, &tmp, log_const->name);
  log_const->masks_name += log_const->name * sizeof(char);

  tmp = "acceleration";
  memcpy(log_const->masks_name, &tmp, log_const->name);
  log_const->masks_name += log_const->name * sizeof(char);

  tmp = "entropy";
  memcpy(log_const->masks_name, &tmp, log_const->name);
  log_const->masks_name += log_const->name * sizeof(char);

  tmp = "cutoff radius";
  memcpy(log_const->masks_name, &tmp, log_const->name);
  log_const->masks_name += log_const->name * sizeof(char);

  tmp = "density";
  memcpy(log_const->masks_name, &tmp, log_const->name);
  log_const->masks_name += log_const->name * sizeof(char);

  tmp = "consts";
  memcpy(log_const->masks_name, &tmp, log_const->name);
  log_const->masks_name += log_const->name * sizeof(char);

  tmp = "timestamp";
  memcpy(log_const->masks_name, &tmp, log_const->name);
  log_const->masks_name += log_const->name * sizeof(char);

}


Pedro Gonnet's avatar
Pedro Gonnet committed
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/**
 * @brief Read a logger message and store the data in a #part.
 *
 * @param p The #part in which to store the values.
 * @param offset Pointer to the offset of the logger message in the buffer,
 *        will be overwritten with the offset of the previous message.
 * @param buff Pointer to the start of an encoded logger message.
 *
 * @return The mask containing the values read.
 */
int logger_read_part(struct part *p, size_t *offset, const char *buff) {

  /* Jump to the offset. */
  buff = &buff[*offset];

  /* Start by reading the logger mask for this entry. */
  uint64_t temp;
  memcpy(&temp, buff, 8);
  const int mask = temp >> 56;
  *offset -= temp & 0xffffffffffffffULL;
394
  buff += 8;
Pedro Gonnet's avatar
Pedro Gonnet committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417

  /* We are only interested in particle data. */
  if (mask & logger_mask_timestamp)
    error("Trying to read timestamp as particle.");

  /* Particle position as three doubles. */
  if (mask & logger_mask_x) {
    memcpy(p->x, buff, 3 * sizeof(double));
    buff += 3 * sizeof(double);
  }

  /* Particle velocity as three floats. */
  if (mask & logger_mask_v) {
    memcpy(p->v, buff, 3 * sizeof(float));
    buff += 3 * sizeof(float);
  }

  /* Particle accelleration as three floats. */
  if (mask & logger_mask_a) {
    memcpy(p->a_hydro, buff, 3 * sizeof(float));
    buff += 3 * sizeof(float);
  }

418
419
#if defined(GADGET2_SPH)

Pedro Gonnet's avatar
Pedro Gonnet committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
  /* Particle internal energy as a single float. */
  if (mask & logger_mask_u) {
    memcpy(&p->entropy, buff, sizeof(float));
    buff += sizeof(float);
  }

  /* Particle smoothing length as a single float. */
  if (mask & logger_mask_h) {
    memcpy(&p->h, buff, sizeof(float));
    buff += sizeof(float);
  }

  /* Particle density as a single float. */
  if (mask & logger_mask_rho) {
    memcpy(&p->rho, buff, sizeof(float));
    buff += sizeof(float);
  }

  /* Particle constants, which is a bit more complicated. */
  if (mask & logger_mask_rho) {
    memcpy(&p->mass, buff, sizeof(float));
    buff += sizeof(float);
    memcpy(&p->id, buff, sizeof(long long));
    buff += sizeof(long long);
  }

446
447
#endif

Pedro Gonnet's avatar
Pedro Gonnet committed
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
  /* Finally, return the mask of the values we just read. */
  return mask;
}

/**
 * @brief Read a logger message and store the data in a #gpart.
 *
 * @param p The #gpart in which to store the values.
 * @param offset Pointer to the offset of the logger message in the buffer,
 *        will be overwritten with the offset of the previous message.
 * @param buff Pointer to the start of an encoded logger message.
 *
 * @return The mask containing the values read.
 */
int logger_read_gpart(struct gpart *p, size_t *offset, const char *buff) {

  /* Jump to the offset. */
  buff = &buff[*offset];

  /* Start by reading the logger mask for this entry. */
  uint64_t temp;
  memcpy(&temp, buff, 8);
  const int mask = temp >> 56;
  *offset -= temp & 0xffffffffffffffULL;
472
  buff += 8;
Pedro Gonnet's avatar
Pedro Gonnet committed
473
474
475
476

  /* We are only interested in particle data. */
  if (mask & logger_mask_timestamp)
    error("Trying to read timestamp as particle.");
Pedro Gonnet's avatar
Pedro Gonnet committed
477

Pedro Gonnet's avatar
Pedro Gonnet committed
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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
  /* We can't store all part fields in a gpart. */
  if (mask & (logger_mask_u | logger_mask_rho))
    error("Trying to read SPH quantities into a gpart.");

  /* Particle position as three doubles. */
  if (mask & logger_mask_x) {
    memcpy(p->x, buff, 3 * sizeof(double));
    buff += 3 * sizeof(double);
  }

  /* Particle velocity as three floats. */
  if (mask & logger_mask_v) {
    memcpy(p->v_full, buff, 3 * sizeof(float));
    buff += 3 * sizeof(float);
  }

  /* Particle accelleration as three floats. */
  if (mask & logger_mask_a) {
    memcpy(p->a_grav, buff, 3 * sizeof(float));
    buff += 3 * sizeof(float);
  }

  /* Particle constants, which is a bit more complicated. */
  if (mask & logger_mask_rho) {
    memcpy(&p->mass, buff, sizeof(float));
    buff += sizeof(float);
    memcpy(&p->id_or_neg_offset, buff, sizeof(long long));
    buff += sizeof(long long);
  }

  /* Finally, return the mask of the values we just read. */
  return mask;
}

/**
 * @brief Read a logger message for a timestamp.
 *
 * @param t The timestamp in which to store the value.
 * @param offset Pointer to the offset of the logger message in the buffer,
 *        will be overwritten with the offset of the previous message.
 * @param buff Pointer to the start of an encoded logger message.
 *
 * @return The mask containing the values read.
 */
Pedro Gonnet's avatar
Pedro Gonnet committed
522
523
int logger_read_timestamp(unsigned long long int *t, size_t *offset,
                          const char *buff) {
Pedro Gonnet's avatar
Pedro Gonnet committed
524
525
526
527
528
529
530
531
532

  /* Jump to the offset. */
  buff = &buff[*offset];

  /* Start by reading the logger mask for this entry. */
  uint64_t temp;
  memcpy(&temp, buff, 8);
  const int mask = temp >> 56;
  *offset -= temp & 0xffffffffffffffULL;
533
  buff += 8;
Pedro Gonnet's avatar
Pedro Gonnet committed
534
535
536
537

  /* We are only interested in timestamps. */
  if (!(mask & logger_mask_timestamp))
    error("Trying to read timestamp from a particle.");
Pedro Gonnet's avatar
Pedro Gonnet committed
538

Pedro Gonnet's avatar
Pedro Gonnet committed
539
540
541
542
543
544
545
546
547
548
  /* Make sure we don't have extra fields. */
  if (mask != logger_mask_timestamp)
    error("Timestamp message contains extra fields.");

  /* Copy the timestamp value from the buffer. */
  memcpy(t, buff, sizeof(unsigned long long int));

  /* Finally, return the mask of the values we just read. */
  return mask;
}
549

lhausamm's avatar
lhausamm committed
550
551
#endif /* WITH_LOGGER */

552
#endif /* HAVE_POSIX_FALLOCATE */
lhausamm's avatar
lhausamm committed
553