diff --git a/src/Makefile.am b/src/Makefile.am index 35d691737fe8b0f8bef6a00cdd6a866e25dc7f74..fbc1d9fdc025dca2a498208ddce98e716fa2fd03 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -48,7 +48,7 @@ include_HEADERS = space.h runner.h queue.h task.h lock.h cell.h part.h const.h \ dump.h logger.h active.h timeline.h xmf.h gravity_properties.h gravity_derivatives.h \ gravity_softened_derivatives.h vector_power.h collectgroup.h hydro_space.h sort_part.h \ chemistry.h chemistry_io.h chemistry_struct.h cosmology.h restart.h space_getsid.h utilities.h \ - mesh_gravity.h cbrt.h velociraptor_interface.h swift_velociraptor_part.h snaplist.h + mesh_gravity.h cbrt.h velociraptor_interface.h swift_velociraptor_part.h outputlist.h # Common source files AM_SOURCES = space.c runner.c queue.c task.c cell.c engine.c \ @@ -61,7 +61,7 @@ AM_SOURCES = space.c runner.c queue.c task.c cell.c engine.c \ part_type.c xmf.c gravity_properties.c gravity.c \ collectgroup.c hydro_space.c equation_of_state.c \ chemistry.c cosmology.c restart.c mesh_gravity.c velociraptor_interface.c \ - snaplist.c + outputlist.c # Include files for distribution, not installation. nobase_noinst_HEADERS = align.h approx_math.h atomic.h barrier.h cycle.h error.h inline.h kernel_hydro.h kernel_gravity.h \ diff --git a/src/engine.c b/src/engine.c index fce807675ad71f176e597b585fe42b0e425ef720..e4bc2795e206e1e2707c122e45952d8788f48922 100644 --- a/src/engine.c +++ b/src/engine.c @@ -69,6 +69,7 @@ #include "map.h" #include "memswap.h" #include "minmax.h" +#include "outputlist.h" #include "parallel_io.h" #include "part.h" #include "partition.h" @@ -78,7 +79,6 @@ #include "runner.h" #include "serial_io.h" #include "single_io.h" -#include "snaplist.h" #include "sort_part.h" #include "sourceterms.h" #include "statistics.h" @@ -5766,8 +5766,8 @@ void engine_init(struct engine *e, struct space *s, struct swift_params *params, parser_get_opt_param_double(params, "Snapshots:time_first", 0.); e->delta_time_snapshot = parser_get_param_double(params, "Snapshots:delta_time"); - e->snaplist_snapshots = NULL; - e->snaplist_stats = NULL; + e->outputlist_snapshots = NULL; + e->outputlist_stats = NULL; e->ti_next_snapshot = 0; parser_get_param_string(params, "Snapshots:basename", e->snapshot_base_name); e->snapshot_compression = @@ -5833,7 +5833,7 @@ void engine_init(struct engine *e, struct space *s, struct swift_params *params, e->ti_current = 0; } - engine_read_snaplist_files(e, params); + engine_read_outputlist_files(e, params); } /** @@ -6440,8 +6440,8 @@ void engine_print_policy(struct engine *e) { * @param e The #engine. */ void engine_compute_next_snapshot_time(struct engine *e) { - /* Do snaplist file case */ - if (e->snaplist_snapshots) { + /* Do outputlist file case */ + if (e->outputlist_snapshots) { engine_read_next_snapshot_time(e); return; } @@ -6504,7 +6504,7 @@ void engine_compute_next_snapshot_time(struct engine *e) { */ void engine_read_next_snapshot_time(struct engine *e) { int is_cosmo = e->policy & engine_policy_cosmology; - const struct snaplist *t = e->snaplist_snapshots; + const struct outputlist *t = e->outputlist_snapshots; /* Find upper-bound on last output */ double time_end; @@ -6564,7 +6564,7 @@ void engine_read_next_snapshot_time(struct engine *e) { * @param e The #engine. */ void engine_compute_next_statistics_time(struct engine *e) { - if (e->snaplist_stats) { + if (e->outputlist_stats) { engine_read_next_statistics_time(e); return; } @@ -6614,7 +6614,7 @@ void engine_compute_next_statistics_time(struct engine *e) { next_statistics_time); } else { const double next_statistics_time = - e->ti_next_stats * e->time_base + e->time_begin; + e->ti_next_stats * e->time_base + e->time_begin; if (e->verbose) message("Next output time for stats set to t=%e.", next_statistics_time); @@ -6684,7 +6684,7 @@ void engine_compute_next_stf_time(struct engine *e) { */ void engine_read_next_statistics_time(struct engine *e) { int is_cosmo = e->policy & engine_policy_cosmology; - const struct snaplist *t = e->snaplist_stats; + const struct outputlist *t = e->outputlist_stats; /* Find upper-bound on last output */ double time_end; @@ -6735,14 +6735,14 @@ void engine_read_next_statistics_time(struct engine *e) { } /** - * @brief Read all the snaplist files + * @brief Read all the outputlist files * * @param e The #engine. * @param params The #swift_params. */ -void engine_read_snaplist_files(struct engine *e, const struct swift_params *params) { +void engine_read_outputlist_files(struct engine *e, const struct swift_params *params) { char filename[PARSER_MAX_LINE_SIZE]; - struct snaplist *list; + struct outputlist *list; /* get cosmo */ struct cosmology *cosmo = NULL; @@ -6750,17 +6750,17 @@ void engine_read_snaplist_files(struct engine *e, const struct swift_params *par cosmo = e->cosmology; /* Deal with snapshots */ - e->snaplist_snapshots = (struct snaplist*) malloc(sizeof(struct snaplist)); - list = e->snaplist_snapshots; + e->outputlist_snapshots = (struct outputlist*) malloc(sizeof(struct outputlist)); + list = e->outputlist_snapshots; strcpy(filename, ""); - parser_get_opt_param_string(params, "Snapshots:snaplist", + parser_get_opt_param_string(params, "Snapshots:outputlist", filename, ""); - /* Read snaplist for snapshots */ + /* Read outputlist for snapshots */ if (strcmp(filename, "")) { - message("Reading snaplist file."); - snaplist_read_file(list, filename, cosmo, engine_max_snaplist_snapshots); + message("Reading snapshots output file."); + outputlist_read_file(list, filename, cosmo); if (list->size < 2) error("You need to provide more snapshots in '%s'", filename); @@ -6777,17 +6777,17 @@ void engine_read_snaplist_files(struct engine *e, const struct swift_params *par } /* Deal with stats */ - e->snaplist_stats = (struct snaplist*) malloc(sizeof(struct snaplist)); - list = e->snaplist_stats; + e->outputlist_stats = (struct outputlist*) malloc(sizeof(struct outputlist)); + list = e->outputlist_stats; strcpy(filename, ""); - parser_get_opt_param_string(params, "Statistics:snaplist", + parser_get_opt_param_string(params, "Statistics:outputlist", filename, ""); - /* Read snaplist for stats */ + /* Read outputlist for stats */ if (strcmp(filename, "")) { - message("Reading snaplist file."); - snaplist_read_file(list, filename, cosmo, engine_max_snaplist_stats); + message("Reading statistics output file."); + outputlist_read_file(list, filename, cosmo); if (list->size < 2) error("You need to provide more snapshots in '%s'", filename); @@ -6937,13 +6937,13 @@ void engine_clean(struct engine *e) { } free(e->runners); free(e->snapshot_units); - if (e->snaplist_snapshots) { - snaplist_clean(e->snaplist_snapshots); - free(e->snaplist_snapshots); + if (e->outputlist_snapshots) { + outputlist_clean(e->outputlist_snapshots); + free(e->outputlist_snapshots); } - if (e->snaplist_stats) { - snaplist_clean(e->snaplist_stats); - free(e->snaplist_stats); + if (e->outputlist_stats) { + outputlist_clean(e->outputlist_stats); + free(e->outputlist_stats); } free(e->links); free(e->cell_loc); @@ -6987,10 +6987,10 @@ void engine_struct_dump(struct engine *e, FILE *stream) { chemistry_struct_dump(e->chemistry, stream); sourceterms_struct_dump(e->sourceterms, stream); parser_struct_dump(e->parameter_file, stream); - if (e->snaplist_snapshots) - snaplist_struct_dump(e->snaplist_snapshots, stream); - if (e->snaplist_stats) - snaplist_struct_dump(e->snaplist_stats, stream); + if (e->outputlist_snapshots) + outputlist_struct_dump(e->outputlist_snapshots, stream); + if (e->outputlist_stats) + outputlist_struct_dump(e->outputlist_stats, stream); } /** @@ -7086,18 +7086,18 @@ void engine_struct_restore(struct engine *e, FILE *stream) { parser_struct_restore(parameter_file, stream); e->parameter_file = parameter_file; - if (e->snaplist_snapshots) { - struct snaplist *snaplist_snapshots = - (struct snaplist *) malloc(sizeof(struct snaplist)); - snaplist_struct_restore(snaplist_snapshots, stream); - e->snaplist_snapshots = snaplist_snapshots; + if (e->outputlist_snapshots) { + struct outputlist *outputlist_snapshots = + (struct outputlist *) malloc(sizeof(struct outputlist)); + outputlist_struct_restore(outputlist_snapshots, stream); + e->outputlist_snapshots = outputlist_snapshots; } - if (e->snaplist_stats) { - struct snaplist *snaplist_stats = - (struct snaplist *) malloc(sizeof(struct snaplist)); - snaplist_struct_restore(snaplist_stats, stream); - e->snaplist_stats = snaplist_stats; + if (e->outputlist_stats) { + struct outputlist *outputlist_stats = + (struct outputlist *) malloc(sizeof(struct outputlist)); + outputlist_struct_restore(outputlist_stats, stream); + e->outputlist_stats = outputlist_stats; } /* Want to force a rebuild before using this engine. Wait to repartition.*/ diff --git a/src/engine.h b/src/engine.h index 3bcdcbdb0ca4e9b2bf13e89499bbd9482f274518..0adb8a104c12f8c8e6722d1abdcbc775509a8b9b 100644 --- a/src/engine.h +++ b/src/engine.h @@ -97,8 +97,6 @@ enum engine_step_properties { #define engine_default_energy_file_name "energy" #define engine_default_timesteps_file_name "timesteps" #define engine_max_parts_per_ghost 1000 -#define engine_max_snaplist_snapshots 8192 -#define engine_max_snaplist_stats 32768 /** * @brief The rank of the engine as a global variable (for messages). @@ -216,8 +214,8 @@ struct engine { double a_first_snapshot; double time_first_snapshot; double delta_time_snapshot; - struct snaplist *snaplist_snapshots; - struct snaplist *snaplist_stats; + struct outputlist *outputlist_snapshots; + struct outputlist *outputlist_stats; /* Integer time of the next snapshot */ integertime_t ti_next_snapshot; @@ -379,7 +377,7 @@ void engine_drift_top_multipoles(struct engine *e); void engine_reconstruct_multipoles(struct engine *e); void engine_print_stats(struct engine *e); void engine_dump_snapshot(struct engine *e); -void engine_read_snaplist_files(struct engine *e, const struct swift_params *params); +void engine_read_outputlist_files(struct engine *e, const struct swift_params *params); void engine_init(struct engine *e, struct space *s, struct swift_params *params, long long Ngas, long long Ngparts, long long Nstars, int policy, int verbose, struct repartition *reparttype, diff --git a/src/outputlist.c b/src/outputlist.c new file mode 100644 index 0000000000000000000000000000000000000000..eace03016f4d9239d6513752987197ba7b3ccbe7 --- /dev/null +++ b/src/outputlist.c @@ -0,0 +1,157 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk), + * 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/>. + * + ******************************************************************************/ + +/* Config parameters. */ +#include "../config.h" + +/* This object's header. */ +#include "outputlist.h" + +/* Local includes. */ +#include "cosmology.h" +#include "error.h" +#include "restart.h" + +/* Some standard headers. */ +#include <string.h> + +/** + * @brief Read a file containing a list of time + * + * @param outputlist The #outputlist to fill. + * @param filename The file to read. + * @param cosmo The #cosmology model. + */ +void outputlist_read_file(struct outputlist *outputlist, const char* filename, + struct cosmology *cosmo) { + + /* Open file */ + FILE* file = fopen(filename, "r"); + if (file == NULL) error("Error opening file '%s'", filename); + + /* Declare reading variables */ + ssize_t read; + size_t len = 0; + char *line = NULL; + + /* Count number of lines */ + size_t nber_line = -1; /* Do not count header */ + while (getline(&line, &len, file) != -1) { + nber_line++; + } + outputlist->size = nber_line; + + /* Return to start of file and initialize time array */ + fseek(file, 0, SEEK_SET); + outputlist->times = (double *) malloc(sizeof(double) * nber_line); + if (!outputlist->times) + error("Unable to malloc outputlist time array. " + "Try reducing the number of lines in %s", filename); + + /* Read header */ + if((read = getline(&line, &len, file)) == -1) + error("Unable to read header in file '%s'", filename); + + /* Remove end of line character */ + line[strcspn(line, "\n")] = 0; + + /* Find type of data in file */ + int type; + if (!strcmp(line, "# Redshift")) + type = OUTPUTLIST_REDSHIFT; + else if (!strcmp(line, "# Time")) + type = OUTPUTLIST_AGE; + else if (!strcmp(line, "# Scale Factor")) + type = OUTPUTLIST_SCALE_FACTOR; + else + error("Unable to interpret the header (%s) in file '%s'", + line, filename); + + if (!cosmo && + (type == OUTPUTLIST_SCALE_FACTOR || type == OUTPUTLIST_REDSHIFT)) + error("Unable to compute a redshift or a scale factor without cosmology. " + "Please change the header in '%s'", filename); + + + /* Read file */ + size_t ind = 0; + while ((read = getline(&line, &len, file)) != -1) { + double *time = &outputlist->times[ind]; + /* Write data to outputlist */ + if (sscanf(line, "%lf", time) != 1) { + error( + "Tried parsing double but found '%s' with illegal double " + "characters in file '%s'.", + line, filename); + } + + /* Transform input into correct time (e.g. ages or scale factor) */ + if (type == OUTPUTLIST_REDSHIFT) + *time = cosmology_get_a_from_z(cosmo, *time); + + if (cosmo && type == OUTPUTLIST_AGE) + *time = cosmology_get_scale_factor(cosmo, *time); + + /* Update size */ + ind += 1; + } + + fclose(file); +} + +/** + * @brief Print an #outputlist + */ +void outputlist_print(const struct outputlist *outputlist) { + + printf("/*\t Time Array\t */\n"); + printf("Number of Line: %lu\n", outputlist->size); + for(size_t ind = 0; ind < outputlist->size; ind++) { + printf("\t%lf\n", outputlist->times[ind]); + } +} + +/** + * @brief Clean an #outputlist + */ +void outputlist_clean(struct outputlist *outputlist) { + free(outputlist->times); +} + +/** + * @brief Dump an #outputlist in a restart file + */ +void outputlist_struct_dump(struct outputlist *list, FILE *stream) { + restart_write_blocks(list, sizeof(struct outputlist), 1, stream, "outputlist", "outputlist struct"); + + restart_write_blocks(list->times, list->size, sizeof(double), stream, "outputlist", "times"); +} + + +/** + * @brief Restore an #outputlist from a restart file + */ +void outputlist_struct_restore(struct outputlist * list, FILE *stream) { + restart_read_blocks(list, sizeof(struct outputlist), 1, stream, NULL, "outputlist struct"); + + list->times = (double *) malloc(sizeof(double) * list->size); + restart_read_blocks(list->times, list->size, sizeof(double), stream, NULL, "times"); + +} diff --git a/src/outputlist.h b/src/outputlist.h new file mode 100644 index 0000000000000000000000000000000000000000..829e6e4a7e2499197c4066936d909dc4e20a8f0e --- /dev/null +++ b/src/outputlist.h @@ -0,0 +1,47 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk), + * 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_OUTPUTLIST_H +#define SWIFT_OUTPUTLIST_H + +/* Config parameters. */ +#include "../config.h" + +/* Local includes */ +#include "cosmology.h" + +enum OUTPUTLIST_TYPE { + OUTPUTLIST_AGE, + OUTPUTLIST_REDSHIFT, + OUTPUTLIST_SCALE_FACTOR, +}; + +struct outputlist { + double *times; + size_t size; +}; + + +void outputlist_read_file(struct outputlist *outputlist, const char* filename, struct cosmology *cosmo); +void outputlist_print(const struct outputlist *outputlist); +void outputlist_clean(struct outputlist *outputlist); +void outputlist_struct_dump(struct outputlist *list, FILE *stream); +void outputlist_struct_restore(struct outputlist * list, FILE *stream); + +#endif // SWIFT_OUTPUTLIST_H diff --git a/src/swift.h b/src/swift.h index b6b7ed68ef0f9f37f960b7166678d866eb1b40ca..e10938addb99956c202b3e4dd2b0592b580fa948 100644 --- a/src/swift.h +++ b/src/swift.h @@ -49,6 +49,7 @@ #include "map.h" #include "mesh_gravity.h" #include "multipole.h" +#include "outputlist.h" #include "parallel_io.h" #include "parser.h" #include "part.h" @@ -63,7 +64,6 @@ #include "scheduler.h" #include "serial_io.h" #include "single_io.h" -#include "snaplist.h" #include "sourceterms.h" #include "space.h" #include "task.h"