common_io.h 4.77 KB
Newer Older
1
2
/*******************************************************************************
 * This file is part of SWIFT.
3
 * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk),
4
 *                    Matthieu Schaller (matthieu.schaller@durham.ac.uk).
5
 *
6
7
8
9
 * 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.
10
 *
11
12
13
14
 * 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.
15
 *
16
17
 * 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/>.
18
 *
19
 ******************************************************************************/
20
21
#ifndef SWIFT_COMMON_IO_H
#define SWIFT_COMMON_IO_H
22
23
24
25
26
27

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

#if defined(HAVE_HDF5)

28
29
#include "part.h"
#include "units.h"
30

31
32
33
34
35
/**
 * @brief The different types of data used in the GADGET IC files.
 *
 * (This is admittedly a poor substitute to C++ templates...)
 */
36
37
38
39
40
41
42
43
44
45
46
enum DATA_TYPE {
  INT,
  LONG,
  LONGLONG,
  UINT,
  ULONG,
  ULONGLONG,
  FLOAT,
  DOUBLE,
  CHAR
};
47
48

/**
49
50
 * @brief The two sorts of data present in the GADGET IC files: compulsory to
 *start a run or optional.
51
52
 *
 */
53
enum DATA_IMPORTANCE { COMPULSORY = 1, OPTIONAL = 0 };
54

55
56
57
58
59
60
61
/**
 * @brief The different particle types present in a GADGET IC file
 *
 */
enum PARTICLE_TYPE {
  GAS = 0,
  DM = 1,
62
63
  BOUNDARY = 2,
  DUMMY = 3,
64
  STAR = 4,
65
66
  BH = 5,
  NUM_PARTICLE_TYPES
67
68
};

Matthieu Schaller's avatar
Matthieu Schaller committed
69
extern const char* particle_type_names[];
70

71
#define FILENAME_BUFFER_SIZE 150
72
#define FIELD_BUFFER_SIZE 50
73
74
#define PARTICLE_GROUP_BUFFER_SIZE 20

75
76
hid_t hdf5Type(enum DATA_TYPE type);
size_t sizeOfType(enum DATA_TYPE type);
77
int isDoublePrecision(enum DATA_TYPE type);
78

Matthieu Schaller's avatar
Matthieu Schaller committed
79
80
81
82
83
84
void collect_dm_gparts(const struct gpart* const gparts, size_t Ntot,
                       struct gpart* const dmparts, size_t Ndm);
void prepare_dm_gparts(struct gpart* const gparts, size_t Ndm);
void duplicate_hydro_gparts(struct part* const parts,
                            struct gpart* const gparts, size_t Ngas,
                            size_t Ndm);
85

86
87
void readAttribute(hid_t grp, char* name, enum DATA_TYPE type, void* data);

88
89
90
91
92
93
94
95
void writeAttribute(hid_t grp, const char* name, enum DATA_TYPE type,
                    void* data, int num);

void writeAttribute_d(hid_t grp, const char* name, double data);
void writeAttribute_f(hid_t grp, const char* name, float data);
void writeAttribute_i(hid_t grp, const char* name, int data);
void writeAttribute_l(hid_t grp, const char* name, long data);
void writeAttribute_s(hid_t grp, const char* name, const char* str);
96

97
98
void createXMFfile(const char* baseName);
FILE* prepareXMFfile(const char* baseName);
99
100
101
void writeXMFoutputheader(FILE* xmfFile, char* hdfFileName, float time);
void writeXMFoutputfooter(FILE* xmfFile, int outputCount, float time);
void writeXMFgroupheader(FILE* xmfFile, char* hdfFileName, size_t N,
Matthieu Schaller's avatar
Matthieu Schaller committed
102
                         enum PARTICLE_TYPE ptype);
103
104
void writeXMFgroupfooter(FILE* xmfFile, enum PARTICLE_TYPE ptype);
void writeXMFline(FILE* xmfFile, char* fileName, char* partTypeGroupName,
Matthieu Schaller's avatar
Matthieu Schaller committed
105
                  char* name, size_t N, int dim, enum DATA_TYPE type);
106

107
void writeCodeDescription(hid_t h_file);
108
109

void readUnitSystem(hid_t h_file, struct UnitSystem* us);
110
111
void writeUnitSystem(hid_t h_grp, const struct UnitSystem* us,
                     const char* groupName);
112

113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/**
 * @brief The properties of a given dataset for i/o
 */
struct io_props {

  /* Name */
  char name[FIELD_BUFFER_SIZE];

  /* Type of the field */
  enum DATA_TYPE type;

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

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

  /* Units of the quantity */
  enum UnitConversionFactor units;

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

  /* The size of the particles */
  size_t partSize;
};

/**
 * @brief Constructs an #io_props from its attributes
 */
#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]))

struct io_props io_make_input_field_(char name[FIELD_BUFFER_SIZE],
                                     enum DATA_TYPE type, int dimension,
                                     enum DATA_IMPORTANCE importance,
                                     enum UnitConversionFactor units,
                                     char* field, size_t partSize);

153
#endif /* defined HDF5 */
154
155

#endif /* SWIFT_COMMON_IO_H */