testLogger.c 8.64 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
/*******************************************************************************
 * 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/>.
 *
 ******************************************************************************/

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

Loic Hausammann's avatar
Loic Hausammann committed
23 24
#if defined(HAVE_POSIX_FALLOCATE) && \
    defined(WITH_LOGGER) /* Are we on a sensible platform? */
25

26 27
/* Some standard headers. */
#include <stdio.h>
28
#include <stdlib.h>
29 30 31 32
#include <string.h>
#include <unistd.h>

/* Local headers. */
33
#include "swift.h"
34

Loic Hausammann's avatar
Loic Hausammann committed
35
void test_log_parts(struct logger_writer *log) {
Loic Hausammann's avatar
Loic Hausammann committed
36
  struct dump *d = &log->dump;
Loic Hausammann's avatar
Loic Hausammann committed
37
  struct engine e;
38 39 40

  /* Write several copies of a part to the dump. */
  struct part p;
Loic Hausammann's avatar
Loic Hausammann committed
41
  struct xpart xp;
42
  bzero(&p, sizeof(struct part));
Loic Hausammann's avatar
Loic Hausammann committed
43
  bzero(&xp, sizeof(struct xpart));
44 45
  p.x[0] = 1.0;
  p.v[0] = 0.1;
Loic Hausammann's avatar
Loic Hausammann committed
46
  xp.logger_data.last_offset = 0;
47 48

  /* Write the full part. */
Loic Hausammann's avatar
Loic Hausammann committed
49 50
  logger_log_part(log, &p, &xp, &e, /* log_all */ 1, /* special flags */ 0);
  printf("Wrote part at offset %#016zx.\n", xp.logger_data.last_offset);
51 52

  /* Write only the position. */
53
  p.x[0] = 2.0;
Loic Hausammann's avatar
Loic Hausammann committed
54 55 56
  p.v[0] = 0.;
  logger_log_part(log, &p, &xp, &e, /* log_all */ 0, /* special flags */ 0);
  printf("Wrote part at offset %#016zx.\n", xp.logger_data.last_offset);
57 58

  /* Write the position and velocity. */
59 60
  p.x[0] = 3.0;
  p.v[0] = 0.3;
Loic Hausammann's avatar
Loic Hausammann committed
61 62
  logger_log_part(log, &p, &xp, &e, /* log_all */ 0, /* special flags */ 0);
  printf("Wrote part at offset %#016zx.\n", xp.logger_data.last_offset);
63 64 65

  /* Recover the last part from the dump. */
  bzero(&p, sizeof(struct part));
Loic Hausammann's avatar
Loic Hausammann committed
66
  size_t offset = xp.logger_data.last_offset;
67
  size_t offset_old = offset;
Loic Hausammann's avatar
Loic Hausammann committed
68
  unsigned int mask = logger_read_part(log, &p, &offset, (const char *)d->data);
69
  printf(
Pedro Gonnet's avatar
Pedro Gonnet committed
70
      "Recovered part at offset %#016zx with mask %#04x: p.x[0]=%e, "
71 72
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v[0]);
73 74
  if (p.x[0] != 3.0 || p.v[0] != 0.3f) {
    printf("FAIL: could not read position and velocity of stored particle.\n");
75 76 77
    abort();
  }

78
  /* Recover the second part from the dump (only position). */
79 80
  bzero(&p, sizeof(struct part));
  offset_old = offset;
Loic Hausammann's avatar
Loic Hausammann committed
81
  mask = logger_read_part(log, &p, &offset, (const char *)d->data);
82
  printf(
Pedro Gonnet's avatar
Pedro Gonnet committed
83
      "Recovered part at offset %#016zx with mask %#04x: p.x[0]=%e, "
84 85
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v[0]);
86 87
  if (p.x[0] != 2.0 || p.v[0] != 0.0) {
    printf("FAIL: could not read position and velocity of stored particle.\n");
88 89 90 91 92 93
    abort();
  }

  /* Recover the first part from the dump. */
  bzero(&p, sizeof(struct part));
  offset_old = offset;
Loic Hausammann's avatar
Loic Hausammann committed
94
  mask = logger_read_part(log, &p, &offset, (const char *)d->data);
95
  printf(
Pedro Gonnet's avatar
Pedro Gonnet committed
96
      "Recovered part at offset %#016zx with mask %#04x: p.x[0]=%e, "
97 98
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v[0]);
99 100 101 102 103 104
  if (p.x[0] != 1.0 || p.v[0] != 0.1f) {
    printf("FAIL: could not read position and velocity of stored particle.\n");
    abort();
  }
}

Loic Hausammann's avatar
Loic Hausammann committed
105
void test_log_gparts(struct logger_writer *log) {
Loic Hausammann's avatar
Loic Hausammann committed
106
  struct dump *d = &log->dump;
Loic Hausammann's avatar
Loic Hausammann committed
107
  struct engine e;
108 109 110 111 112 113

  /* Write several copies of a part to the dump. */
  struct gpart p;
  bzero(&p, sizeof(struct gpart));
  p.x[0] = 1.0;
  p.v_full[0] = 0.1;
Loic Hausammann's avatar
Loic Hausammann committed
114 115
  p.type = swift_type_dark_matter;
  p.logger_data.last_offset = 0;
116 117

  /* Write the full part. */
Loic Hausammann's avatar
Loic Hausammann committed
118 119
  logger_log_gpart(log, &p, &e, /* log_all */ 1, /* special flags */ 0);
  printf("Wrote gpart at offset %#016zx.\n", p.logger_data.last_offset);
120 121 122

  /* Write only the position. */
  p.x[0] = 2.0;
Loic Hausammann's avatar
Loic Hausammann committed
123 124 125
  p.v_full[0] = 0.;
  logger_log_gpart(log, &p, &e, /* log_all */ 0, /* special flags */ 0);
  printf("Wrote gpart at offset %#016zx.\n", p.logger_data.last_offset);
126 127 128 129

  /* Write the position and velocity. */
  p.x[0] = 3.0;
  p.v_full[0] = 0.3;
Loic Hausammann's avatar
Loic Hausammann committed
130 131
  logger_log_gpart(log, &p, &e, /* log_all */ 0, /* special flags */ 0);
  printf("Wrote gpart at offset %#016zx.\n", p.logger_data.last_offset);
132 133

  /* Recover the last part from the dump. */
Loic Hausammann's avatar
Loic Hausammann committed
134
  size_t offset = p.logger_data.last_offset;
135 136
  bzero(&p, sizeof(struct gpart));
  size_t offset_old = offset;
Loic Hausammann's avatar
Loic Hausammann committed
137
  int mask = logger_read_gpart(log, &p, &offset, (const char *)d->data);
138 139 140 141 142 143 144 145 146 147 148 149
  printf(
      "Recovered gpart at offset %#016zx with mask %#04x: p.x[0]=%e, "
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v_full[0]);
  if (p.x[0] != 3.0 || p.v_full[0] != 0.3f) {
    printf("FAIL: could not read position and velocity of stored gpart.\n");
    abort();
  }

  /* Recover the second part from the dump. */
  bzero(&p, sizeof(struct gpart));
  offset_old = offset;
Loic Hausammann's avatar
Loic Hausammann committed
150
  mask = logger_read_gpart(log, &p, &offset, (const char *)d->data);
151 152 153 154 155 156 157 158 159 160 161 162
  printf(
      "Recovered gpart at offset %#016zx with mask %#04x: p.x[0]=%e, "
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v_full[0]);
  if (p.x[0] != 2.0 || p.v_full[0] != 0.0) {
    printf("FAIL: could not read position and velocity of stored gpart.\n");
    abort();
  }

  /* Recover the first part from the dump. */
  bzero(&p, sizeof(struct gpart));
  offset_old = offset;
Loic Hausammann's avatar
Loic Hausammann committed
163
  mask = logger_read_gpart(log, &p, &offset, (const char *)d->data);
164 165 166 167 168 169
  printf(
      "Recovered gpart at offset %#016zx with mask %#04x: p.x[0]=%e, "
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v_full[0]);
  if (p.x[0] != 1.0 || p.v_full[0] != 0.1f) {
    printf("FAIL: could not read position and velocity of stored gpart.\n");
170 171
    abort();
  }
172 173
}

Loic Hausammann's avatar
Loic Hausammann committed
174
void test_log_timestamps(struct logger_writer *log) {
Loic Hausammann's avatar
Loic Hausammann committed
175
  struct dump *d = &log->dump;
176 177

  /* The timestamp to log. */
Loic Hausammann's avatar
Loic Hausammann committed
178
  integertime_t t = 10;
179
  double time = 0.1;
180 181 182 183 184

  /* Start with an offset at the end of the dump. */
  size_t offset = d->count;

  /* Log three consecutive timestamps. */
185
  logger_log_timestamp(log, t, time, &offset);
186 187
  printf("Logged timestamp %020llu at offset %#016zx.\n", t, offset);
  t += 10;
188 189
  time = 0.2;
  logger_log_timestamp(log, t, time, &offset);
190 191
  printf("Logged timestamp %020llu at offset %#016zx.\n", t, offset);
  t += 10;
192 193
  time = 0.3;
  logger_log_timestamp(log, t, time, &offset);
194 195 196 197 198
  printf("Logged timestamp %020llu at offset %#016zx.\n", t, offset);

  /* Recover the three timestamps. */
  size_t offset_old = offset;
  t = 0;
199
  time = 0;
Loic Hausammann's avatar
Loic Hausammann committed
200 201 202 203 204 205
  int mask =
      logger_read_timestamp(log, &t, &time, &offset, (const char *)d->data);
  printf(
      "Recovered timestamp %020llu with time %g at offset %#016zx with mask "
      "%#04x.\n",
      t, time, offset_old, mask);
206 207 208 209
  if (t != 30) {
    printf("FAIL: could not recover correct timestamp.\n");
    abort();
  }
210
  if (time != 0.3) {
Loic Hausammann's avatar
Loic Hausammann committed
211
    printf("FAIL: could not recover correct time.\n");
212 213
    abort();
  }
214 215 216

  offset_old = offset;
  t = 0;
217
  time = 0;
Loic Hausammann's avatar
Loic Hausammann committed
218 219 220 221 222
  mask = logger_read_timestamp(log, &t, &time, &offset, (const char *)d->data);
  printf(
      "Recovered timestamp %020llu with time %g at offset %#016zx with mask "
      "%#04x.\n",
      t, time, offset_old, mask);
223 224 225 226
  if (t != 20) {
    printf("FAIL: could not recover correct timestamp.\n");
    abort();
  }
227 228 229 230
  if (time != 0.2) {
    printf("FAIL: could not recover correct time.\n");
    abort();
  }
231 232 233

  offset_old = offset;
  t = 0;
234
  time = 0;
Loic Hausammann's avatar
Loic Hausammann committed
235 236 237 238 239
  mask = logger_read_timestamp(log, &t, &time, &offset, (const char *)d->data);
  printf(
      "Recovered timestamp %020llu with time %g at offset %#016zx with mask "
      "%#04x.\n",
      t, time, offset_old, mask);
240 241 242 243
  if (t != 10) {
    printf("FAIL: could not recover correct timestamp.\n");
    abort();
  }
244 245 246 247
  if (time != 0.1) {
    printf("FAIL: could not recover correct time.\n");
    abort();
  }
248 249
}

250 251
int main(int argc, char *argv[]) {

252
  /* Prepare a logger. */
Loic Hausammann's avatar
Loic Hausammann committed
253
  struct logger_writer log;
254
  struct swift_params params;
Loic Hausammann's avatar
Loic Hausammann committed
255 256 257
  struct engine e;
  e.policy = engine_policy_hydro | engine_policy_self_gravity;

258
  parser_read_file("logger.yml", &params);
Loic Hausammann's avatar
Loic Hausammann committed
259
  logger_init(&log, &e, &params);
260 261

  /* Test writing/reading parts. */
262
  test_log_parts(&log);
263

264
  /* Test writing/reading gparts. */
265
  test_log_gparts(&log);
266

267
  /* Test writing/reading timestamps. */
268
  test_log_timestamps(&log);
269

270
  /* Be clean */
271 272
  char filename[256];
  sprintf(filename, "%s.dump", log.base_name);
273 274
  remove(filename);

275
  /* Clean the logger. */
Loic Hausammann's avatar
Loic Hausammann committed
276
  logger_free(&log);
277

278 279 280
  /* Return a happy number. */
  return 0;
}
281 282 283

#else

284
int main(int argc, char *argv[]) { return 0; }
285 286

#endif /* HAVE_POSIX_FALLOCATE */