Commit 051c53e6 authored by Loic Hausammann's avatar Loic Hausammann
Browse files

Logger: Add testReader (still need to check data)

parent bc4eb419
......@@ -64,7 +64,7 @@ int _tools_get_next_record_forward(const struct header *h, void *map,
if (diff_offset == 0) return -1;
/* set absolute offset */
*offset += diff_offset - LOGGER_MASK_SIZE - LOGGER_OFFSET_SIZE;
*offset += diff_offset;
return 0;
}
......@@ -130,10 +130,11 @@ size_t tools_reverse_offset(const struct header *h, void *file_map, size_t offse
/* set offset after current record */
map += header_get_record_size_from_mask(h, mask);
size_t after_current_record = (size_t) (map - file_map);
/* first records do not have a previous partner */
if (prev_offset == cur_offset)
return offset;
return after_current_record;
if (prev_offset > cur_offset)
error("Unexpected offset, header %lu, current %lu", prev_offset,
......@@ -146,14 +147,14 @@ size_t tools_reverse_offset(const struct header *h, void *file_map, size_t offse
#ifdef SWIFT_DEBUG_CHECKS
size_t prev_mask = 0;
map -= LOGGER_MASK_SIZE + LOGGER_OFFSET_SIZE;
map = logger_io_read_mask(h, map, &prev_mask, NULL);
logger_io_read_mask(h, map, &prev_mask, NULL);
if (prev_mask != mask)
error("Unexpected mask: %lu, got %lu", mask, prev_mask);
#endif // SWIFT_DEBUG_CHECKS
return offset;
return after_current_record;
}
/**
......@@ -203,25 +204,24 @@ size_t tools_check_record_consistency(const struct logger_reader *reader, size_t
/* read mask of the pointed record */
size_t pointed_mask = 0;
map = logger_io_read_mask(h, map + pointed_offset, &pointed_mask, NULL);
logger_io_read_mask(h, file_init + pointed_offset, &pointed_mask, NULL);
/* check masks */
if (pointed_mask != mask)
error("Error in the offset (mask %lu != %lu)", mask,
pointed_mask);
error("Error in the offset (mask %lu at %lu != %lu at %lu)", mask, offset,
pointed_mask, pointed_offset);
if (pointed_mask == 128)
return (size_t) (map - file_init);
struct logger_particle part;
size_t tmp = logger_particle_read(&part, reader, offset, 0, logger_reader_const);
logger_particle_read(&part, reader, offset, 0, logger_reader_const);
size_t id = part.id;
tmp = pointed_offset - LOGGER_MASK_SIZE - LOGGER_OFFSET_SIZE;
tmp = logger_particle_read(&part, reader, tmp, 0, logger_reader_const);
logger_particle_read(&part, reader, pointed_offset, 0, logger_reader_const);
if (id != part.id)
error("Offset wrong, id incorrect (%lu != %lu) at %lu", id, part.id, tmp);
error("Offset wrong, id incorrect (%lu != %lu) at %lu", id, part.id, pointed_offset);
return (size_t) (map - file_init);
}
......
/*******************************************************************************
* This file is part of SWIFT.
* Copyright (C) 2015 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/>.
*
******************************************************************************/
#include "swift.h"
#include "logger_header.h"
#include "logger_reader.h"
#define number_parts 100
/**
* @brief Initialize the particles.
*
* @param p The array of #part.
* @param xp The array of #xpart.
*/
void init_particles(struct part *p, struct xpart *xp) {
struct hydro_space hs;
for(int i = 0; i < number_parts; i++) {
/* Set internal energy. */
hydro_set_init_internal_energy(&p[i], 100);
/* Initialize particle. */
hydro_first_init_part(&p[i], &xp[i]);
hydro_init_part(&p[i], &hs);
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].h = 15;
p[i].rho = 50;
p[i].id = i;
hydro_set_mass(&p[i], 1.5);
xp[i].logger_data.last_offset = 0;
}
}
/**
* @brief Write a few particles during multiple time steps.
*
* As only the logger is tested, there is no need to really
* evolve the particles.
*/
void write_particles(struct logger *log) {
const int number_steps = 100;
integertime_t ti_int = 0;
double ti_double = 0.;
const double time_base = 1e-4;
/* Create unit system */
struct unit_system us;
units_init_cgs(&us);
/* Create particles and initialize them. */
struct part *parts;
if ((parts = (struct part *)malloc(sizeof(struct part) * number_parts)) == NULL)
error("Failed to allocate particles array.");
struct xpart *xparts;
if ((xparts = (struct xpart *)malloc(sizeof(struct xpart) * number_parts)) == NULL)
error("Failed to allocate xparticles array.");
init_particles(parts, xparts);
/*
Init logger
*/
/* Do step */
for(int i = 0; i < number_steps; i++) {
/* Mark the current time step in the particle logger file. */
logger_log_timestamp(log, ti_int, ti_double,
&log->timestamp_offset);
/* Make sure that we have enough space in the particle logger file
* to store the particles in current time step. */
logger_ensure_size(log, number_parts, /* number gpart */0, 0);
/* Run step */
for(int j = 0; j < number_parts; j++) {
/* Write particle */
/* TODO Currently writing everything, should adapt it through time */
logger_log_part(log, &parts[j],
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,
&xparts[j].logger_data.last_offset);
}
ti_int += 1;
ti_double = time_base * ti_int;
// TODO write index files
}
/* Mark the current time step in the particle logger file. */
logger_log_timestamp(log, ti_int, ti_double,
&log->timestamp_offset);
}
int main(int argc, char *argv[]) {
/*
First generate the file.
*/
message("Generating the dump.");
/* Create required structures. */
struct logger log;
struct swift_params params;
char filename[200] = "testHeader.yml";
/* Read parameters. */
parser_read_file(filename, &params);
/* Initialize the logger. */
logger_init(&log, &params);
/* get dump filename */
char dump_filename[PARSER_MAX_LINE_SIZE];
message("%s", log.base_name);
strcpy(dump_filename, log.base_name);
strcat(dump_filename, ".dump");
/* Write file header. */
logger_write_file_header(&log);
/* Write particles. */
write_particles(&log);
/* clean memory */
logger_clean(&log);
/*
Then read the file.
*/
message("Reading the header.");
/* Generate required structure for reading. */
struct logger_reader reader;
/* Set verbose level */
reader.verbose = 1;
/* Read the header */
logger_reader_init(&reader, dump_filename, /* verbose */ 1);
/*
Finally check everything
*/
return 0;
}
# Parameter file for the tests
Logger:
delta_step: 10
initial_buffer_size: 0.01 # in GB
buffer_scale: 10
basename: index
\ No newline at end of file
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