logger.h 6.34 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*******************************************************************************
 * 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/>.
 *
 ******************************************************************************/
#ifndef SWIFT_LOGGER_H
#define SWIFT_LOGGER_H

lhausamm's avatar
lhausamm committed
22
23
#ifdef WITH_LOGGER

24
/* Includes. */
lhausamm's avatar
lhausamm committed
25
#include "common_io.h"
26
#include "dump.h"
Loic Hausammann's avatar
Loic Hausammann committed
27
28
#include "inline.h"
#include "timeline.h"
lhausamm's avatar
lhausamm committed
29
#include "units.h"
30

Loic Hausammann's avatar
Loic Hausammann committed
31
/* Forward declaration. */
32
struct dump;
Loic Hausammann's avatar
Loic Hausammann committed
33
struct gpart;
34
struct part;
Loic Hausammann's avatar
Loic Hausammann committed
35
struct engine;
36

Loic Hausammann's avatar
Loic Hausammann committed
37
38
39
#define logger_major_version 0
#define logger_minor_version 1

40
41
/**
 * Logger entries contain messages representing the particle data at a given
42
 * point in time during the simulation.
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
 *
 * The logger messages always start with an 8-byte header structured as
 * follows:
 *
 *   data: [ mask |                     offset                     ]
 *   byte: [  01  |  02  |  03  |  04  |  05  |  06  |  07  |  08  ]
 *
 * I.e. a first "mask" byte followed by 7 "offset" bytes. The mask contains
 * information on what kind of data is packed after the header. The mask
 * bits correspond to the following data:
 *
 *   bit | name   | size | comment
 *   -------------------------------------------------------------------------
 *   0   | x      | 24   | The particle position, in absolute coordinates,
 *       |        |      | stored as three doubles.
 *   1   | v      | 12   | Particle velocity, stored as three floats.
 *   2   | a      | 12   | Particle acceleration, stored as three floats.
60
61
 *   3   | u      | 4    | Particle internal energy (or entropy, if Gadget-SPH
 *       |        |      | is used), stored as a single float.
62
63
 *   4   | h      | 4    | Particle smoothing length (or epsilon, if a gpart),
 *       |        |      | stored as a single float.
64
 *   5   | rho    | 4    | Particle density, stored as a single float.
65
66
 *   6   | consts | 12   | Particle constants, i.e. mass and ID.
 *   7   | time   | 8    | Timestamp, not associated with a particle, just
67
68
69
70
71
72
73
 *       |        |      | marks the transitions from one timestep to another.
 *
 * There is no distinction between gravity and SPH particles.
 *
 * The offset refers to the relative location of the previous message for the
 * same particle or for the previous timestamp (if mask bit 7 is set). I.e.
 * the previous log entry will be at the address of the current mask byte minus
lhausamm's avatar
lhausamm committed
74
75
 * the unsigned value stored in the offset. An offset equal to the chunk offset
 * indicated that this is the first message for the given particle/timestamp.
76
77
78
 */

/* Some constants. */
79
80
81
82
83
84
85
86
enum logger_masks_number {
  logger_x = 0,
  logger_v = 1,
  logger_a = 2,
  logger_u = 3,
  logger_h = 4,
  logger_rho = 5,
  logger_consts = 6,
Loic Hausammann's avatar
Loic Hausammann committed
87
88
  logger_timestamp = 7,  /* expect it to be before count. */
  logger_count_mask = 8, /* Need to be the last. */
Loic Hausammann's avatar
Loic Hausammann committed
89
} __attribute__((packed));
90

91
struct mask_data {
Loic Hausammann's avatar
Loic Hausammann committed
92
  /* Number of bytes for a mask. */
93
  int size;
94

Loic Hausammann's avatar
Loic Hausammann committed
95
  /* Mask value. */
96
  unsigned int mask;
97

Loic Hausammann's avatar
Loic Hausammann committed
98
  /* Name of the mask. */
99
100
101
102
103
  char name[100];
};

extern const struct mask_data logger_mask_data[logger_count_mask];

104
105
/* Size of the strings. */
#define logger_string_length 200
Loic Hausammann's avatar
Loic Hausammann committed
106

Loic Hausammann's avatar
Loic Hausammann committed
107
108
109
/* structure containing global data. */
struct logger_writer {
  /* Number of particle steps between dumping a chunk of data. */
Loic Hausammann's avatar
Loic Hausammann committed
110
111
  short int delta_step;

Loic Hausammann's avatar
Loic Hausammann committed
112
  /* Logger basename. */
113
  char base_name[logger_string_length];
Loic Hausammann's avatar
Loic Hausammann committed
114

Loic Hausammann's avatar
Loic Hausammann committed
115
116
  /*  Dump file (In the reader, the dump is cleaned, therefore it is renamed
   * logfile). */
117
  struct dump dump;
Loic Hausammann's avatar
Loic Hausammann committed
118

Loic Hausammann's avatar
Loic Hausammann committed
119
  /* timestamp offset for logger. */
Loic Hausammann's avatar
Loic Hausammann committed
120
121
  size_t timestamp_offset;

Loic Hausammann's avatar
Loic Hausammann committed
122
  /* scaling factor when buffer is too small. */
Loic Hausammann's avatar
Loic Hausammann committed
123
124
  float buffer_scale;

Loic Hausammann's avatar
Loic Hausammann committed
125
  /* Size of a chunk if every mask are activated. */
126
127
  int max_chunk_size;

Loic Hausammann's avatar
Loic Hausammann committed
128
129
} SWIFT_STRUCT_ALIGN;

Loic Hausammann's avatar
Loic Hausammann committed
130
/* required structure for each particle type. */
Loic Hausammann's avatar
Loic Hausammann committed
131
struct logger_part_data {
Loic Hausammann's avatar
Loic Hausammann committed
132
  /* Number of particle updates since last output. */
133
  int steps_since_last_output;
Loic Hausammann's avatar
Loic Hausammann committed
134

Loic Hausammann's avatar
Loic Hausammann committed
135
  /* offset of last particle log entry. */
Loic Hausammann's avatar
Loic Hausammann committed
136
137
138
  size_t last_offset;
};

139
/* Function prototypes. */
lhausamm's avatar
lhausamm committed
140
int logger_compute_chunk_size(unsigned int mask);
Loic Hausammann's avatar
Loic Hausammann committed
141
142
void logger_log_all(struct logger_writer *log, const struct engine *e);
void logger_log_part(struct logger_writer *log, const struct part *p,
Loic Hausammann's avatar
Format    
Loic Hausammann committed
143
                     unsigned int mask, size_t *offset);
Loic Hausammann's avatar
Loic Hausammann committed
144
void logger_log_gpart(struct logger_writer *log, const struct gpart *p,
Loic Hausammann's avatar
Format    
Loic Hausammann committed
145
                      unsigned int mask, size_t *offset);
Loic Hausammann's avatar
Loic Hausammann committed
146
147
void logger_init(struct logger_writer *log, struct swift_params *params);
void logger_free(struct logger_writer *log);
Loic Hausammann's avatar
Loic Hausammann committed
148
149
void logger_log_timestamp(struct logger_writer *log, integertime_t t,
                          double time, size_t *offset);
Loic Hausammann's avatar
Loic Hausammann committed
150
void logger_ensure_size(struct logger_writer *log, size_t total_nr_parts,
Loic Hausammann's avatar
Format    
Loic Hausammann committed
151
                        size_t total_nr_gparts, size_t total_nr_sparts);
Loic Hausammann's avatar
Loic Hausammann committed
152
void logger_write_file_header(struct logger_writer *log);
lhausamm's avatar
lhausamm committed
153

Pedro Gonnet's avatar
Pedro Gonnet committed
154
155
int logger_read_part(struct part *p, size_t *offset, const char *buff);
int logger_read_gpart(struct gpart *p, size_t *offset, const char *buff);
156
157
int logger_read_timestamp(unsigned long long int *t, double *time,
                          size_t *offset, const char *buff);
lhausamm's avatar
lhausamm committed
158

159
160
161
162
163
164
165
/**
 * @brief Initialize the logger data for a particle.
 *
 * @param logger The #logger_part_data.
 */
INLINE static void logger_part_data_init(struct logger_part_data *logger) {
  logger->last_offset = 0;
166
  logger->steps_since_last_output = INT_MAX;
167
168
169
170
171
}

/**
 * @brief Should this particle write its data now ?
 *
Loic Hausammann's avatar
Loic Hausammann committed
172
173
174
175
 * @param logger_data The #logger_part_data of a particle.
 * @param log The #logger.
 *
 * @return 1 if the particule should be writen, 0 otherwise.
176
177
 */
__attribute__((always_inline)) INLINE static int logger_should_write(
Loic Hausammann's avatar
Loic Hausammann committed
178
179
    const struct logger_part_data *logger_data,
    const struct logger_writer *log) {
180
181
182
183

  return (logger_data->steps_since_last_output > log->delta_step);
}

lhausamm's avatar
lhausamm committed
184
185
#endif /* WITH_LOGGER */

186
#endif /* SWIFT_LOGGER_H */