logger_io.h 5.46 KB
Newer Older
lhausamm's avatar
lhausamm committed
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) 2012 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_LOGGER_IO_H
#define SWIFT_LOGGER_IO_H

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

lhausamm's avatar
lhausamm committed
25
#ifdef WITH_LOGGER
lhausamm's avatar
lhausamm committed
26
27
28

/* Includes. */
#include "engine.h"
29
#include "io_properties.h"
lhausamm's avatar
lhausamm committed
30
31
32
#include "part.h"
#include "units.h"

Loic Hausammann's avatar
Loic Hausammann committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
94
95
96
97
98
99
100
101
102
/* This enum defines the type of particle to use
   with a given mask.
   The values should be the same than in part_type.h. */
enum mask_type {
  mask_type_gas = 0,
  mask_type_dark_matter = 1,
  /* Only need a single type of dm. */
  mask_type_stars = 4,
  mask_type_black_hole = 5,
  mask_type_timestep = -1,
} __attribute__((packed));

struct mask_data {
  /* Number of bytes for a mask. */
  int size;

  /* Mask value. */
  unsigned int mask;

  /* Type of particle (follow part_type.h and -1 for timestamp). */
  enum mask_type type;

  /* Name of the mask. */
  char name[100];
};

/**
 * @brief Initialize the mask_data with a given field.
 *
 * @param name The name of the field.
 * @param size The size of the field.
 *
 * @return The new mask_data.
 */
INLINE static struct mask_data logger_create_mask_entry(const char* name,
                                                        int size) {
  struct mask_data mask;
  /* Copy the fields */
  strcpy(mask.name, name);
  mask.size = size;
  mask.mask = 0;

  return mask;
}

/**
 * @brief Add a given field to the current mask.
 *
 * @param mask_data The mask_data corresponding to the field that we wish to
 * write.
 * @param buffer_size (in) The current size of the future buffer. (out) The
 * updated size.
 *
 * @return The mask of the current field.
 */
INLINE static size_t logger_add_field_to_mask(struct mask_data mask_data,
                                              size_t* buffer_size) {

  *buffer_size += mask_data.size;
  return mask_data.mask;
}

/**
 * @brief Check if a field should be written according to the mask set in
 * #logger_add_field_to_mask.
 *
 * @param mask_data The mask_data corresponding to the current field.
 * @param mask The mask used for the current record.
 */
INLINE static int logger_should_write_field(struct mask_data mask_data,
103
                                            unsigned int* mask) {
Loic Hausammann's avatar
Loic Hausammann committed
104
105
106
107
108
109
110
111
112

  const int test = mask_data.mask & *mask;
  if (test) {
    *mask &= ~mask_data.mask;
  }

  return test;
}

Loic Hausammann's avatar
Loic Hausammann committed
113
114
void logger_write_index_file(struct logger_writer* log, struct engine* e);
void logger_write_description(struct logger_writer* log, struct engine* e);
115
116
117
118
119

/**
 * @brief Specifies which particle fields to write to a dataset
 *
 * @param parts The particle array.
Loic Hausammann's avatar
Loic Hausammann committed
120
121
 * @param xparts The extra particle array.
 * @param list (out) The parameters to write.
122
123
124
 *
 * In this version, we only want the ids and the offset.
 */
Loic Hausammann's avatar
Loic Hausammann committed
125
126
127
__attribute__((always_inline)) INLINE static int hydro_write_index(
    const struct part* parts, const struct xpart* xparts,
    struct io_props* list) {
128

Loic Hausammann's avatar
Loic Hausammann committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
  /* List what we want to write */
  list[0] =
      io_make_output_field("ParticleIDs", ULONGLONG, 1, UNIT_CONV_NO_UNITS, 0.f,
                           parts, id, "Field not used");
  list[1] =
      io_make_output_field("Offset", UINT64, 1, UNIT_CONV_NO_UNITS, 0.f, xparts,
                           logger_data.last_offset, "Field not used");

  return 2;
}

/**
 * @brief Specifies which particle fields to write to a dataset
 *
 * @param gparts The gparticle array.
 * @param list (out) The parameters to write.
 *
 * In this version, we only want the ids and the offset.
 */
__attribute__((always_inline)) INLINE static int darkmatter_write_index(
    const struct gpart* gparts, struct io_props* list) {
150
151

  /* List what we want to write */
152
153
  list[0] =
      io_make_output_field("ParticleIDs", ULONGLONG, 1, UNIT_CONV_NO_UNITS, 0.f,
Loic Hausammann's avatar
Loic Hausammann committed
154
155
156
157
                           gparts, id_or_neg_offset, "Field not used");
  list[1] =
      io_make_output_field("Offset", UINT64, 1, UNIT_CONV_NO_UNITS, 0.f, gparts,
                           logger_data.last_offset, "Field not used");
158

Loic Hausammann's avatar
Loic Hausammann committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
  return 2;
}

/**
 * @brief Specifies which particle fields to write to a dataset
 *
 * @param sparts The sparticle array.
 * @param list (out) The parameters to write.
 *
 * In this version, we only want the ids and the offset.
 */
__attribute__((always_inline)) INLINE static int stars_write_index(
    const struct spart* sparts, struct io_props* list) {

  /* List what we want to write */
  list[0] =
      io_make_output_field("ParticleIDs", ULONGLONG, 1, UNIT_CONV_NO_UNITS, 0.f,
                           sparts, id, "Field not used");
177
  list[1] =
Loic Hausammann's avatar
Loic Hausammann committed
178
179
180
181
      io_make_output_field("Offset", UINT64, 1, UNIT_CONV_NO_UNITS, 0.f, sparts,
                           logger_data.last_offset, "Field not used");

  return 2;
182
}
Loic Hausammann's avatar
Loic Hausammann committed
183

lhausamm's avatar
lhausamm committed
184
185
186
#endif

#endif /* SWIFT_LOGGER_IO_H */