io_properties.h 10.5 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
26
27
/* Local includes. */
#include "inline.h"

28
29
30
31
/**
 * @brief The two sorts of data present in the GADGET IC files: compulsory to
 * start a run or optional.
 */
32
enum DATA_IMPORTANCE { COMPULSORY = 1, OPTIONAL = 0, UNUSED = -1 };
33

34
35
36
37
38
39
40
41
42
43
/* Helper typedefs */
typedef void (*conversion_func_part_float)(const struct engine*,
                                           const struct part*, float*);
typedef void (*conversion_func_part_double)(const struct engine*,
                                            const struct part*, double*);
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*);

44
45
46
47
48
49
50
51
52
/**
 * @brief The properties of a given dataset for i/o
 */
struct io_props {

  /* Name */
  char name[FIELD_BUFFER_SIZE];

  /* Type of the field */
53
  enum IO_DATA_TYPE type;
54
55
56
57
58
59
60
61

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

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

  /* Units of the quantity */
62
  enum unit_conversion_factor units;
63
64
65
66

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

67
68
69
70
71
72
73
74
  /* Pointer to the start of the temporary buffer used in i/o */
  char* start_temp_c;
  float* start_temp_f;
  double* start_temp_d;

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

75
76
  /* The size of the particles */
  size_t partSize;
77
78

  /* The particle arrays */
79
80
81
82
83
  const struct part* parts;
  const struct gpart* gparts;

  /* Are we converting? */
  int conversion;
84
85

  /* Conversion function for part */
86
87
  conversion_func_part_float convert_part_f;
  conversion_func_part_double convert_part_d;
88

89
  /* Conversion function for gpart */
90
91
  conversion_func_gpart_float convert_gpart_f;
  conversion_func_gpart_double convert_gpart_d;
92
93
94
95
96
97
98
99
100
101
102
103
104
105
};

/**
 * @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
106
 * @param dimension Dataset dimension (1D, 3D, ...)
107
108
109
110
111
112
113
 * @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.
 */
114
115
116
117
INLINE static struct io_props io_make_input_field_(
    char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
    enum DATA_IMPORTANCE importance, enum unit_conversion_factor units,
    char* field, size_t partSize) {
118
119
120
121
122
123
124
125
  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;
126
127
  r.parts = NULL;
  r.gparts = NULL;
128
129
130
131
132
  r.conversion = 0;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = NULL;
133
134
135
136
137
138
139

  return r;
}

/**
 * @brief Constructs an #io_props from its parameters
 */
140
141
142
#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]))
143
144
145
146
147
148

/**
 * @brief Construct an #io_props from its parameters
 *
 * @param name Name of the field to read
 * @param type The type of the data
149
 * @param dimension Dataset dimension (1D, 3D, ...)
150
151
152
153
154
155
 * @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.
 */
156
157
158
INLINE static struct io_props io_make_output_field_(
    char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
    enum unit_conversion_factor units, char* field, size_t partSize) {
159
160
161
162
  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
163
  r.importance = UNUSED;
164
165
166
  r.units = units;
  r.field = field;
  r.partSize = partSize;
167
168
  r.parts = NULL;
  r.gparts = NULL;
169
170
171
172
173
  r.conversion = 0;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = NULL;
174
175
176
177
178
179
180

  return r;
}

/**
 * @brief Constructs an #io_props (with conversion) from its parameters
 */
181
182
183
184
#define io_make_output_field_convert_part(name, type, dim, units, part, \
                                          convert)                      \
  io_make_output_field_convert_part_##type(name, type, dim, units,      \
                                           sizeof(part[0]), part, convert)
185
186
187
188
189
190
191
192
193
194
195
196
197
198

/**
 * @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 functionPtr The function used to convert a particle to a float
 *
 * Do not call this function directly. Use the macro defined above.
 */
199
INLINE static struct io_props io_make_output_field_convert_part_FLOAT(
200
    char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
201
202
    enum unit_conversion_factor units, size_t partSize,
    const struct part* parts, conversion_func_part_float functionPtr) {
203
204
205
206
207

  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
208
  r.importance = UNUSED;
209
  r.units = units;
210
  r.field = NULL;
211
212
213
  r.partSize = partSize;
  r.parts = parts;
  r.gparts = NULL;
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
  r.conversion = 1;
  r.convert_part_f = functionPtr;
  r.convert_part_d = NULL;
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = 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 partSize The size in byte of the particle
 * @param parts The particle array
 * @param functionPtr The function used to convert a particle to a float
 *
 * Do not call this function directly. Use the macro defined above.
 */
236
INLINE static struct io_props io_make_output_field_convert_part_DOUBLE(
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
    enum unit_conversion_factor units, size_t partSize,
    const struct part* parts, conversion_func_part_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 = partSize;
  r.parts = parts;
  r.gparts = NULL;
  r.conversion = 1;
  r.convert_part_f = NULL;
  r.convert_part_d = functionPtr;
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = NULL;
256
257
258
259
260
261
262

  return r;
}

/**
 * @brief Constructs an #io_props (with conversion) from its parameters
 */
263
264
265
266
#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)
267
268
269
270
271
272
273
274

/**
 * @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
275
 * @param gpartSize The size in byte of the particle
276
277
 * @param gparts The particle array
 * @param functionPtr The function used to convert a g-particle to a float
278
279
280
 *
 * Do not call this function directly. Use the macro defined above.
 */
281
INLINE static struct io_props io_make_output_field_convert_gpart_FLOAT(
282
    char name[FIELD_BUFFER_SIZE], enum IO_DATA_TYPE type, int dimension,
283
284
    enum unit_conversion_factor units, size_t gpartSize,
    const struct gpart* gparts, conversion_func_gpart_float functionPtr) {
285
286
287
288
289

  struct io_props r;
  strcpy(r.name, name);
  r.type = type;
  r.dimension = dimension;
290
  r.importance = UNUSED;
291
  r.units = units;
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
  r.field = NULL;
  r.partSize = gpartSize;
  r.parts = NULL;
  r.gparts = gparts;
  r.conversion = 1;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
  r.convert_gpart_f = functionPtr;
  r.convert_gpart_d = 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 float
 *
 * Do not call this function directly. Use the macro defined above.
 */
318
INLINE static struct io_props io_make_output_field_convert_gpart_DOUBLE(
319
320
321
322
323
324
325
326
327
328
329
330
    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_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;
331
332
  r.parts = NULL;
  r.gparts = gparts;
333
334
335
336
337
  r.conversion = 1;
  r.convert_part_f = NULL;
  r.convert_part_d = NULL;
  r.convert_gpart_f = NULL;
  r.convert_gpart_d = functionPtr;
338
339
340
341
342

  return r;
}

#endif /* SWIFT_IO_PROPERTIES_H */