logger.h 6.59 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"
Loic Hausammann's avatar
Loic Hausammann committed
26
27
#include "inline.h"
#include "timeline.h"
lhausamm's avatar
lhausamm committed
28
#include "units.h"
29

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

36
37
/**
 * Logger entries contain messages representing the particle data at a given
38
 * point in time during the simulation.
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
 *
 * 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.
56
57
 *   3   | u      | 4    | Particle internal energy (or entropy, if Gadget-SPH
 *       |        |      | is used), stored as a single float.
58
59
 *   4   | h      | 4    | Particle smoothing length (or epsilon, if a gpart),
 *       |        |      | stored as a single float.
60
 *   5   | rho    | 4    | Particle density, stored as a single float.
61
62
 *   6   | consts | 12   | Particle constants, i.e. mass and ID.
 *   7   | time   | 8    | Timestamp, not associated with a particle, just
63
64
65
66
67
68
69
 *       |        |      | 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
70
71
 * 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.
72
73
 */

lhausamm's avatar
lhausamm committed
74
75
76
77
78
/* header constants
 * Thoses are definitions from the format and therefore should not be changed!
 * Size in bytes
 */

79
80
81
82
83
84
85
86
87
88
/* Some constants. */
#define logger_mask_x 1
#define logger_mask_v 2
#define logger_mask_a 4
#define logger_mask_u 8
#define logger_mask_h 16
#define logger_mask_rho 32
#define logger_mask_consts 64
#define logger_mask_timestamp 128

lhausamm's avatar
lhausamm committed
89
90
/* size of a mask */
#define logger_mask_size 1
lhausamm's avatar
lhausamm committed
91

lhausamm's avatar
lhausamm committed
92
93
/* size of an offset */
#define logger_offset_size 7
94

lhausamm's avatar
lhausamm committed
95
96
/* size of the version information */
#define logger_version_size 20
lhausamm's avatar
lhausamm committed
97

lhausamm's avatar
lhausamm committed
98
99
/* size of the size information */
#define logger_header_number_size 2
lhausamm's avatar
lhausamm committed
100

lhausamm's avatar
lhausamm committed
101
extern char logger_version[logger_version_size];
102

Loic Hausammann's avatar
Loic Hausammann committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#define LOGGER_STRING_LENGTH 200

/* parameters of the logger */
struct logger_parameters {
  /* size of a label in bytes */
  size_t label_size;

  /* size of an offset in bytes */
  size_t offset_size;

  /* size of a mask in bytes */
  size_t mask_size;

  /* size of a number in bytes */
  size_t number_size;

  /* size of a data type in bytes */
  size_t data_type_size;
Loic Hausammann's avatar
Format    
Loic Hausammann committed
121

Loic Hausammann's avatar
Loic Hausammann committed
122
123
124
125
126
127
128
129
  /* number of different mask */
  size_t nber_mask;

  /* value of each masks */
  size_t *masks;

  /* data size of each mask */
  size_t *masks_data_size;
Loic Hausammann's avatar
Format    
Loic Hausammann committed
130

Loic Hausammann's avatar
Loic Hausammann committed
131
132
133
134
135
136
137
138
139
140
  /* label of each mask */
  char *masks_name;
};

/* structure containing global data */
struct logger {
  /* Number of particle steps between dumping a chunk of data */
  short int delta_step;

  /* Logger basename */
Loic Hausammann's avatar
Format    
Loic Hausammann committed
141
  char base_name[LOGGER_STRING_LENGTH];
Loic Hausammann's avatar
Loic Hausammann committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

  /* File name of the dump file */
  struct dump *dump;

  /* timestamp offset for logger*/
  size_t timestamp_offset;

  /* size of the buffer */
  size_t buffer_size;

  /* scaling factor when buffer is too small */
  float buffer_scale;

  /* logger parameters */
  struct logger_parameters *params;

} SWIFT_STRUCT_ALIGN;

/* required structure for each particle type */
struct logger_part_data {
  /* Number of particle updates since last output */
  short int last_output;

  /* offset of last particle log entry */
  size_t last_offset;
};

Loic Hausammann's avatar
Format    
Loic Hausammann committed
169
INLINE static void logger_part_data_init(struct logger_part_data *logger) {
Loic Hausammann's avatar
Loic Hausammann committed
170
171
172
173
  logger->last_offset = 0;
  logger->last_output = SHRT_MAX;
}

174
175
176
177
178
enum logger_datatype {
  logger_data_int,
  logger_data_float,
  logger_data_double,
  logger_data_char,
179
  logger_data_longlong,
lhausamm's avatar
lhausamm committed
180
181
  logger_data_bool,
  logger_data_count /* should be last */
182
};
183

184
extern const unsigned int logger_datatype_size[];
lhausamm's avatar
lhausamm committed
185

186
/* Function prototypes. */
lhausamm's avatar
lhausamm committed
187
int logger_compute_chunk_size(unsigned int mask);
lhausamm's avatar
lhausamm committed
188
void logger_log_all(struct logger *log, const struct engine *e);
Loic Hausammann's avatar
Format    
Loic Hausammann committed
189
190
191
192
193
void logger_log_part(struct logger *log, const struct part *p,
                     unsigned int mask, size_t *offset);
void logger_log_gpart(struct logger *log, const struct gpart *p,
                      unsigned int mask, size_t *offset);
void logger_init(struct logger *log, struct swift_params *params);
lhausamm's avatar
lhausamm committed
194
195
void logger_clean(struct logger *log);
void logger_log_timestamp(struct logger *log, integertime_t t, size_t *offset);
lhausamm's avatar
lhausamm committed
196
void logger_ensure_size(struct logger *log, size_t total_nr_parts,
Loic Hausammann's avatar
Format    
Loic Hausammann committed
197
198
                        size_t total_nr_gparts, size_t total_nr_sparts);
void logger_write_file_header(struct logger *log, const struct engine *e);
lhausamm's avatar
lhausamm committed
199

Pedro Gonnet's avatar
Pedro Gonnet committed
200
201
202
203
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);
int logger_read_timestamp(unsigned long long int *t, size_t *offset,
                          const char *buff);
lhausamm's avatar
lhausamm committed
204

Loic Hausammann's avatar
Format    
Loic Hausammann committed
205
206
void logger_parameters_init(struct logger_parameters *log_params);
void logger_parameters_clean(struct logger_parameters *log_params);
lhausamm's avatar
lhausamm committed
207

lhausamm's avatar
lhausamm committed
208
209
#endif /* WITH_LOGGER */

210
#endif /* SWIFT_LOGGER_H */