Commit edc3e6d5 authored by Loic Hausammann's avatar Loic Hausammann
Browse files

Logger: implement pedro's comments

parent aef685af
# This file is part of SWIFT.
# Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk),
# Matthieu Schaller (matthieu.schaller@durham.ac.uk).
# Loic Hausammann (loic.hausammann@epfl.ch)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......
......@@ -119,7 +119,11 @@ void header_read(struct header *h, struct logger_logfile *log) {
/* Read minor version number */
map = logger_loader_io_read_data(map, sizeof(int), &h->minor_version);
if (h->log->reader->verbose > 0)
struct logger_reader *reader = log->reader;
if (&reader->log != log)
error("Wrong link to the reader.");
if (reader->verbose > 0)
message("File version %i.%i", h->major_version, h->minor_version);
/* read the offset directions */
......
......@@ -29,15 +29,17 @@
* @param reader The #logger_reader.
* @param only_header Read only the header.
*/
void logger_logfile_init(
void logger_logfile_init_from_file(
struct logger_logfile *log, char *filename,
struct logger_reader *reader, int only_header) {
/* Set the pointer to the reader. */
log->reader = reader;
if (&reader->log != log)
error("Wrong link to the reader.");
/* Set pointers to zero */
time_array_init_to_zero(&log->times);
time_array_init(&log->times);
/* Open file, map it and get its size. */
if (reader->verbose > 1)
......@@ -73,7 +75,7 @@ void logger_logfile_init(
/* Initialize the time array */
if (reader->verbose > 1)
message("Reading the time stamps.");
time_array_init(&log->times, log);
time_array_populate(&log->times, log);
/* Print the time array */
if (reader->verbose > 0) {
......@@ -90,6 +92,7 @@ void logger_logfile_init(
void logger_logfile_free(struct logger_logfile *log) {
logger_loader_io_munmap_file(log->log.map, log->log.file_size);
time_array_free(&log->times);
}
......@@ -109,6 +112,8 @@ void logger_logfile_reverse_offset(struct logger_logfile *log, char *filename) {
/* Get pointers */
struct header *header = &log->header;
const struct logger_reader *reader = log->reader;
if (&reader->log != log)
error("Wrong link to the reader.");
/* Check if the offsets need to be reversed */
if (!header_is_backward(header)) {
......
......@@ -35,7 +35,7 @@ struct logger_reader;
* and deals with the log file.
* It maps it, reverse the offsets (if required) and unmap it.
*
* The structure is initialized with #logger_logfile_init and
* The structure is initialized with #logger_logfile_init_from_file and
* freed with #logger_logfile_free.
*/
struct logger_logfile {
......@@ -62,7 +62,7 @@ struct logger_logfile {
};
void logger_logfile_init(struct logger_logfile *log, char *filename, struct logger_reader *reader,
void logger_logfile_init_from_file(struct logger_logfile *log, char *filename, struct logger_reader *reader,
int only_header);
void logger_logfile_reverse_offset(struct logger_logfile *log, char *filename);
void logger_logfile_free(struct logger_logfile *log);
......
......@@ -34,8 +34,8 @@ void logger_reader_init(struct logger_reader *reader, char *filename, int verbos
reader->verbose = verbose;
/* Initialize the log file */
logger_logfile_init(&reader->log, filename, reader,
/* only_header */ 0);
logger_logfile_init_from_file(&reader->log, filename, reader,
/* only_header */ 0);
if (verbose > 1)
message("Initialization done.");
......
......@@ -94,18 +94,18 @@ size_t time_offset_first_record(const struct header *h) {
*
* @param t #time_array to initialize.
*/
void time_array_init_to_zero(struct time_array *t) {
void time_array_init(struct time_array *t) {
t->next = NULL;
t->prev = NULL;
}
/**
* @brief Initialize a time array.
* @brief Initialize a time array from a file.
*
* @param t #time_array to initialize.
* @param log The #logger_logfile.
*/
void time_array_init(struct time_array *t, struct logger_logfile *log) {
void time_array_populate(struct time_array *t, struct logger_logfile *log) {
/* Initialize a few variables. */
t->next = NULL;
......
......@@ -34,7 +34,7 @@ struct logger_reader;
* this structure is required. It contains all the time step
* with their integer time, double time and position in the file.
*
* This structure is initialized with #time_array_init and
* This structure is initialized with #time_array_init and #time_array_populate, and
* freed with #time_array_free.
*
* The time step of an offset can be obtained with
......@@ -64,8 +64,8 @@ struct time_array {
size_t time_read(integertime_t *int_time, double *time,
const struct logger_reader *reader, size_t offset);
void time_array_init_to_zero(struct time_array *t);
void time_array_init(struct time_array *t, struct logger_logfile *log);
void time_array_init(struct time_array *t);
void time_array_populate(struct time_array *t, struct logger_logfile *log);
integertime_t time_array_get_integertime(struct time_array *t, const size_t offset);
......
......@@ -20,18 +20,18 @@ AM_CFLAGS = -I$(top_srcdir)/src -I$(top_srcdir)/logger $(HDF5_CPPFLAGS) $(GSL_IN
AM_LDFLAGS = ../../src/.libs/libswiftsim.a ../.libs/liblogger.a $(HDF5_LDFLAGS) $(HDF5_LIBS) $(FFTW_LIBS) $(TCMALLOC_LIBS) $(JEMALLOC_LIBS) $(TBBMALLOC_LIBS) $(GRACKLE_LIBS) $(GSL_LIBS) $(PROFILER_LIBS)
# List of programs and scripts to run in the test suite
TESTS = testHeader testReader # testIndex
TESTS = testLogfileHeader testLogfileReader # testIndex
# List of test programs to compile
check_PROGRAMS = testHeader testReader
check_PROGRAMS = testLogfileHeader testLogfileReader
# Rebuild tests when SWIFT is updated.
$(check_PROGRAMS): ../../src/.libs/libswiftsim.a
# Sources for the individual programs
testHeader_SOURCES = testHeader.c
testReader_SOURCES = testReader.c
testLogfileHeader_SOURCES = testLogfileHeader.c
testReader_SOURCES = testLogfileReader.c
# testIndex_SOURCES = testIndex.c
# Files necessary for distribution
EXTRA_DIST = testHeader.yml testReader.yml testIndex.yml
EXTRA_DIST = testLogfileHeader.yml testLogfileReader.yml testIndex.yml
/*******************************************************************************
* This file is part of SWIFT.
* Copyright (C) 2015 Matthieu Schaller (matthieu.schaller@durham.ac.uk).
* Copyright (C) 2019 Loic Hausammann (loic.hausammann@epfl.ch)
*
* 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
......@@ -32,7 +32,7 @@ int main(int argc, char *argv[]) {
/* Create required structures. */
struct logger log;
struct swift_params params;
char filename[200] = "testHeader.yml";
char filename[200] = "testLogfileHeader.yml";
/* Read parameters. */
parser_read_file(filename, &params);
......@@ -49,33 +49,34 @@ int main(int argc, char *argv[]) {
logger_write_file_header(&log);
/* clean memory */
logger_clean(&log);
logger_free(&log);
/*
Then read the file.
*/
message("Reading the header.");
/* Generate required structure for reading. */
struct logger_logfile logfile;
struct logger_reader reader;
struct logger_logfile *logfile = &reader.log;
logfile->reader = &reader;
/* Set verbose level */
reader.verbose = 1;
/* Read the header */
logger_logfile_init(&logfile, dump_filename, &reader,
logger_logfile_init_from_file(logfile, dump_filename, &reader,
/* only_header */ 1);
/*
Finally check everything
*/
struct header *h = &logfile.header;
struct header *h = &logfile->header;
message("Checking versions.");
assert(h->major_version == logger_major_version);
assert(h->minor_version == logger_minor_version);
message("Checking offset of first record");
assert(h->offset_first_record == logfile.log.file_size);
assert(h->offset_first_record == logfile->log.file_size);
message("Checking number of masks");
assert(h->number_mask == logger_count_mask);
......
/*******************************************************************************
* This file is part of SWIFT.
* Copyright (C) 2015 Matthieu Schaller (matthieu.schaller@durham.ac.uk).
* Copyright (C) 2019 Loic Hausammann (loic.hausammann@epfl.ch).
*
* 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
......@@ -44,8 +44,8 @@ void init_particles(struct part *p, struct xpart *xp) {
for(int j = 0; j < 3; j++) {
p[i].x[j] = i;
p[i].v[j] = j == 0 ? -1 : 0;
p[i].a_hydro[j] = j == 1 ? 1e-2 : 0;
p[i].v[j] = (j == 0) ? -1 : 0;
p[i].a_hydro[j] = (j == 1) ? 1e-2 : 0;
}
p[i].h = 15;
p[i].rho = 50;
......@@ -55,20 +55,12 @@ void init_particles(struct part *p, struct xpart *xp) {
}
}
/**
* @brief Provides a time given the step number.
*
* @param step The required step.
*/
/** Provides a integer time given the step number.*/
integertime_t get_integer_time(int step) {
return step;
}
/**
* @brief Provides a time given the step number.
*
* @param step The required step.
*/
/** Provides a double time given the step number. */
double get_double_time(int step) {
const double time_base = 1e-4;
return step * time_base;
......@@ -217,6 +209,7 @@ void check_data(struct logger_reader *reader) {
offset = read_record(reader, &lp, &time, &is_particle, offset)) {
if (is_particle) {
/* Check data if it is a particle */
if (lp.id >= number_parts)
error("Wrong id %zi", lp.id);
......@@ -234,6 +227,7 @@ void check_data(struct logger_reader *reader) {
assert(p->mass == lp.mass);
}
else {
/* Check data if it is a timestamp */
assert(time == get_double_time(step));
step += 1;
......@@ -254,7 +248,7 @@ int main(int argc, char *argv[]) {
/* Create required structures. */
struct logger log;
struct swift_params params;
char filename[200] = "testReader.yml";
char filename[200] = "testLogfileReader.yml";
/* Read parameters. */
parser_read_file(filename, &params);
......@@ -275,7 +269,7 @@ int main(int argc, char *argv[]) {
write_particles(&log);
/* clean memory */
logger_clean(&log);
logger_free(&log);
/*
Then read the file.
*/
......
......@@ -6270,7 +6270,7 @@ void engine_clean(struct engine *e, const int fof) {
swift_free("links", e->links);
#if defined(WITH_LOGGER)
logger_clean(e->logger);
logger_free(e->logger);
free(e->logger);
#endif
scheduler_clean(&e->sched);
......
......@@ -447,7 +447,7 @@ void logger_init(struct logger *log, struct swift_params *params) {
*
* @param log The #logger
*/
void logger_clean(struct logger *log) { dump_close(&log->dump); }
void logger_free(struct logger *log) { dump_close(&log->dump); }
/**
* @brief Write a file header to a logger file
......
......@@ -144,7 +144,7 @@ void logger_log_part(struct logger *log, const struct part *p,
void logger_log_gpart(struct logger *log, const struct gpart *p,
unsigned int mask, size_t *offset);
void logger_init(struct logger *log, struct swift_params *params);
void logger_clean(struct logger *log);
void logger_free(struct logger *log);
void logger_log_timestamp(struct logger *log, integertime_t t, double time,
size_t *offset);
void logger_ensure_size(struct logger *log, size_t total_nr_parts,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment