testLogger.c 8.83 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;
37 38 39 40

  /* Write several copies of a part to the dump. */
  struct part p;
  bzero(&p, sizeof(struct part));
41 42 43 44 45
  p.x[0] = 1.0;
  p.v[0] = 0.1;

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

  /* Write the full part. */
Loic Hausammann's avatar
Loic Hausammann committed
48 49 50 51 52 53
  logger_log_part(
      log, &p,
      logger_mask_data[logger_x].mask | logger_mask_data[logger_v].mask |
          logger_mask_data[logger_a].mask | logger_mask_data[logger_u].mask |
          logger_mask_data[logger_h].mask | logger_mask_data[logger_rho].mask |
          logger_mask_data[logger_consts].mask,
Loic Hausammann's avatar
Loic Hausammann committed
54
      &offset, /* special flags */ 0);
55 56 57
  printf("Wrote part at offset %#016zx.\n", offset);

  /* Write only the position. */
58
  p.x[0] = 2.0;
Loic Hausammann's avatar
Loic Hausammann committed
59 60
  logger_log_part(log, &p, logger_mask_data[logger_x].mask, &offset,
                  /* special flags */ 0);
61 62 63
  printf("Wrote part at offset %#016zx.\n", offset);

  /* Write the position and velocity. */
64 65
  p.x[0] = 3.0;
  p.v[0] = 0.3;
Loic Hausammann's avatar
Loic Hausammann committed
66 67 68
  logger_log_part(
      log, &p,
      logger_mask_data[logger_x].mask | logger_mask_data[logger_v].mask,
Loic Hausammann's avatar
Loic Hausammann committed
69
      &offset, /* special flags */ 0);
70 71 72 73 74
  printf("Wrote part at offset %#016zx.\n", offset);

  /* Recover the last part from the dump. */
  bzero(&p, sizeof(struct part));
  size_t offset_old = offset;
75
  int mask = logger_read_part(&p, &offset, (const char *)d->data);
76
  printf(
Pedro Gonnet's avatar
Pedro Gonnet committed
77
      "Recovered part at offset %#016zx with mask %#04x: p.x[0]=%e, "
78 79
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v[0]);
80 81
  if (p.x[0] != 3.0 || p.v[0] != 0.3f) {
    printf("FAIL: could not read position and velocity of stored particle.\n");
82 83 84
    abort();
  }

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

  /* Recover the first part from the dump. */
  bzero(&p, sizeof(struct part));
  offset_old = offset;
101
  mask = logger_read_part(&p, &offset, (const char *)d->data);
102
  printf(
Pedro Gonnet's avatar
Pedro Gonnet committed
103
      "Recovered part at offset %#016zx with mask %#04x: p.x[0]=%e, "
104 105
      "p.v[0]=%e.\n",
      offset_old, mask, p.x[0], p.v[0]);
106 107 108 109 110 111
  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
112
void test_log_gparts(struct logger_writer *log) {
Loic Hausammann's avatar
Loic Hausammann committed
113
  struct dump *d = &log->dump;
114 115 116 117 118 119 120 121 122 123 124

  /* 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;

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

  /* Write the full part. */
Loic Hausammann's avatar
Loic Hausammann committed
125 126 127 128 129
  logger_log_gpart(
      log, &p,
      logger_mask_data[logger_x].mask | logger_mask_data[logger_v].mask |
          logger_mask_data[logger_a].mask | logger_mask_data[logger_h].mask |
          logger_mask_data[logger_consts].mask,
Loic Hausammann's avatar
Loic Hausammann committed
130
      &offset, /* special flags */ 0);
131 132 133 134
  printf("Wrote gpart at offset %#016zx.\n", offset);

  /* Write only the position. */
  p.x[0] = 2.0;
Loic Hausammann's avatar
Loic Hausammann committed
135 136
  logger_log_gpart(log, &p, logger_mask_data[logger_x].mask, &offset,
                   /* special flags */ 0);
137 138 139 140 141
  printf("Wrote gpart at offset %#016zx.\n", offset);

  /* Write the position and velocity. */
  p.x[0] = 3.0;
  p.v_full[0] = 0.3;
Loic Hausammann's avatar
Loic Hausammann committed
142 143 144
  logger_log_gpart(
      log, &p,
      logger_mask_data[logger_x].mask | logger_mask_data[logger_v].mask,
Loic Hausammann's avatar
Loic Hausammann committed
145
      &offset, /* special flags */ 0);
146 147 148 149 150
  printf("Wrote gpart at offset %#016zx.\n", offset);

  /* Recover the last part from the dump. */
  bzero(&p, sizeof(struct gpart));
  size_t offset_old = offset;
151
  int mask = logger_read_gpart(&p, &offset, (const char *)d->data);
152 153 154 155 156 157 158 159 160 161 162 163
  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;
164
  mask = logger_read_gpart(&p, &offset, (const char *)d->data);
165 166 167 168 169 170 171 172 173 174 175 176
  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;
177
  mask = logger_read_gpart(&p, &offset, (const char *)d->data);
178 179 180 181 182 183
  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");
184 185
    abort();
  }
186 187
}

Loic Hausammann's avatar
Loic Hausammann committed
188
void test_log_timestamps(struct logger_writer *log) {
Loic Hausammann's avatar
Loic Hausammann committed
189
  struct dump *d = &log->dump;
190 191 192

  /* The timestamp to log. */
  unsigned long long int t = 10;
193
  double time = 0.1;
194 195 196 197 198

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

  /* Log three consecutive timestamps. */
199
  logger_log_timestamp(log, t, time, &offset);
200 201
  printf("Logged timestamp %020llu at offset %#016zx.\n", t, offset);
  t += 10;
202 203
  time = 0.2;
  logger_log_timestamp(log, t, time, &offset);
204 205
  printf("Logged timestamp %020llu at offset %#016zx.\n", t, offset);
  t += 10;
206 207
  time = 0.3;
  logger_log_timestamp(log, t, time, &offset);
208 209 210 211 212
  printf("Logged timestamp %020llu at offset %#016zx.\n", t, offset);

  /* Recover the three timestamps. */
  size_t offset_old = offset;
  t = 0;
213 214
  time = 0;
  int mask = logger_read_timestamp(&t, &time, &offset, (const char *)d->data);
215 216 217 218 219 220
  printf("Recovered timestamp %020llu at offset %#016zx with mask %#04x.\n", t,
         offset_old, mask);
  if (t != 30) {
    printf("FAIL: could not recover correct timestamp.\n");
    abort();
  }
221 222 223 224
  if (time != 0.3) {
    printf("FAIL: could not recover correct time %g.\n", time);
    abort();
  }
225 226 227

  offset_old = offset;
  t = 0;
228 229
  time = 0;
  mask = logger_read_timestamp(&t, &time, &offset, (const char *)d->data);
230 231 232 233 234 235
  printf("Recovered timestamp %020llu at offset %#016zx with mask %#04x.\n", t,
         offset_old, mask);
  if (t != 20) {
    printf("FAIL: could not recover correct timestamp.\n");
    abort();
  }
236 237 238 239
  if (time != 0.2) {
    printf("FAIL: could not recover correct time.\n");
    abort();
  }
240 241 242

  offset_old = offset;
  t = 0;
243 244
  time = 0;
  mask = logger_read_timestamp(&t, &time, &offset, (const char *)d->data);
245 246 247 248 249 250
  printf("Recovered timestamp %020llu at offset %#016zx with mask %#04x.\n", t,
         offset_old, mask);
  if (t != 10) {
    printf("FAIL: could not recover correct timestamp.\n");
    abort();
  }
251 252 253 254
  if (time != 0.1) {
    printf("FAIL: could not recover correct time.\n");
    abort();
  }
255 256
}

257 258
int main(int argc, char *argv[]) {

259
  /* Prepare a logger. */
Loic Hausammann's avatar
Loic Hausammann committed
260
  struct logger_writer log;
261 262 263
  struct swift_params params;
  parser_read_file("logger.yml", &params);
  logger_init(&log, &params);
264 265

  /* Test writing/reading parts. */
266
  test_log_parts(&log);
267

268
  /* Test writing/reading gparts. */
269
  test_log_gparts(&log);
270

271
  /* Test writing/reading timestamps. */
272
  test_log_timestamps(&log);
273

274
  /* Be clean */
275 276
  char filename[256];
  sprintf(filename, "%s.dump", log.base_name);
277 278
  remove(filename);

279
  /* Clean the logger. */
Loic Hausammann's avatar
Loic Hausammann committed
280
  logger_free(&log);
281

282 283 284
  /* Return a happy number. */
  return 0;
}
285 286 287

#else

288
int main(int argc, char *argv[]) { return 0; }
289 290

#endif /* HAVE_POSIX_FALLOCATE */