From 60383c3c93865e794538d6f56efdcc0beaee3073 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Fri, 1 Jun 2018 16:40:13 +0200 Subject: [PATCH 01/32] Add snaplist parameter --- src/common_io.c | 44 ++++++++++++++++++++++++++ src/common_io.h | 9 ++++++ src/engine.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++ src/engine.h | 3 ++ 4 files changed, 139 insertions(+) diff --git a/src/common_io.c b/src/common_io.c index 683111075..1c6e9434d 100644 --- a/src/common_io.c +++ b/src/common_io.c @@ -966,3 +966,47 @@ void io_write_output_field_parameter(const char* filename) { "'%s'.\n", filename); } + + +void time_array_read_file(struct time_array *times, const char* filename) { + /* initialize times */ + times->size = 0; + + /* Open file */ + FILE* file = fopen(filename, "r"); + if (file == NULL) error("Error opening file '%s'", filename); + + /* Read file */ + ssize_t read; + size_t len = 0; + char *line = NULL; + while ((read = getline(&line, &len, file)) != -1) { + + /* Check data size */ + if (times->size == TIME_ARRAY_MAX_SIZE) + error("Not enough memory to write the time array buffer. " + "Please decrease the number of time required in '%s'.", + filename); + + /* Write data to time array */ + if (sscanf(line, "%lf", ×->times[times->size]) != 1) { + error( + "Tried parsing double but found '%s' with illegal double " + "characters in file '%s'.", + line, filename); + } + + /* Update size */ + times->size += 1; + } + + time_array_print(times); +} + +void time_array_print(const struct time_array *times) { + + printf("/*\t Time Array\t */\n"); + for(size_t ind = 0; ind < times->size; ind++) { + printf("\t%lf\n", times->times[ind]); + } +} diff --git a/src/common_io.h b/src/common_io.h index 152b40a8d..039a794e8 100644 --- a/src/common_io.h +++ b/src/common_io.h @@ -30,6 +30,7 @@ #define PARTICLE_GROUP_BUFFER_SIZE 50 #define FILENAME_BUFFER_SIZE 150 #define IO_BUFFER_ALIGNMENT 1024 +#define TIME_ARRAY_MAX_SIZE 8192 /* Avoid cyclic inclusion problems */ struct part; @@ -62,6 +63,11 @@ enum IO_DATA_TYPE { */ enum IO_STF_OUTPUT_FORMAT { STEPS = 0, TIME }; +struct time_array { + double times[TIME_ARRAY_MAX_SIZE]; + size_t size; +}; + #if defined(HAVE_HDF5) hid_t io_hdf5_type(enum IO_DATA_TYPE type); @@ -113,4 +119,7 @@ void io_check_output_fields(const struct swift_params* params, void io_write_output_field_parameter(const char* filename); +void time_array_read_file(struct time_array *times, const char* filename); +void time_array_print(const struct time_array *times); + #endif /* SWIFT_COMMON_IO_H */ diff --git a/src/engine.c b/src/engine.c index 369dba72b..cba6888ca 100644 --- a/src/engine.c +++ b/src/engine.c @@ -5765,6 +5765,7 @@ 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->time_array_snapshots = NULL; e->ti_next_snapshot = 0; parser_get_param_string(params, "Snapshots:basename", e->snapshot_base_name); e->snapshot_compression = @@ -5829,6 +5830,8 @@ void engine_init(struct engine *e, struct space *s, struct swift_params *params, e->time_base_inv = e->cosmology->time_base_inv; e->ti_current = 0; } + + engine_read_time_files(e, params); } /** @@ -6435,6 +6438,11 @@ 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->time_array_snapshots) { + engine_read_next_snapshot_time(e); + return; + } /* Find upper-bound on last output */ double time_end; @@ -6735,6 +6743,8 @@ void engine_clean(struct engine *e) { } free(e->runners); free(e->snapshot_units); + if (e->time_array_snapshots) + free(e->time_array_snapshots); free(e->links); free(e->cell_loc); scheduler_clean(&e->sched); @@ -6876,3 +6886,76 @@ void engine_struct_restore(struct engine *e, FILE *stream) { e->forcerebuild = 1; e->forcerepart = 0; } + + +void engine_read_time_files(struct engine *e, const struct swift_params *params) { + char filename[PARSER_MAX_LINE_SIZE]; + + /* Read snapshot time array */ + e->time_array_snapshots = (struct time_array*) malloc(sizeof(struct time_array)); + + strcpy(filename, ""); + parser_get_opt_param_string(params, "Snapshots:snaplist", + filename, ""); + + if (strcmp(filename, "")) { + message("Reading snaplist file."); + time_array_read_file(e->time_array_snapshots, filename); + } +} + + +void engine_read_next_snapshot_time(struct engine *e) { + int is_cosmo = e->policy & engine_policy_cosmology; + const struct time_array *t = e->time_array_snapshots; + + /* Find upper-bound on last output */ + double time_end; + if (is_cosmo) + time_end = e->cosmology->a_end * e->delta_time_snapshot; + else + time_end = e->time_end + e->delta_time_snapshot; + + /* Find next snasphot above current time */ + double time; + time = t->times[0]; + size_t ind = 0; + while (time < time_end) { + + /* Output time on the integer timeline */ + if (is_cosmo) + e->ti_next_snapshot = log(time / e->cosmology->a_begin) / e->time_base; + else + e->ti_next_snapshot = (time - e->time_begin) / e->time_base; + + /* Found it? */ + if (e->ti_next_snapshot > e->ti_current) break; + + ind += 1; + if (ind == t->size) + break; + + time = t->times[ind]; + } + + /* Deal with last snapshot */ + if (e->ti_next_snapshot >= max_nr_timesteps || ind == t->size) { + e->ti_next_snapshot = -1; + if (e->verbose) message("No further output time."); + } else { + + /* Be nice, talk... */ + if (is_cosmo) { + const double next_snapshot_time = + exp(e->ti_next_snapshot * e->time_base) * e->cosmology->a_begin; + if (e->verbose) + message("Next snapshot time set to a=%e.", next_snapshot_time); + } else { + const double next_snapshot_time = + e->ti_next_snapshot * e->time_base + e->time_begin; + if (e->verbose) + message("Next snapshot time set to t=%e.", next_snapshot_time); + } + } + +} diff --git a/src/engine.h b/src/engine.h index a0cd9eab3..7f2bcc020 100644 --- a/src/engine.h +++ b/src/engine.h @@ -214,6 +214,7 @@ struct engine { double a_first_snapshot; double time_first_snapshot; double delta_time_snapshot; + struct time_array *time_array_snapshots; /* Integer time of the next snapshot */ integertime_t ti_next_snapshot; @@ -365,6 +366,7 @@ void engine_addlink(struct engine *e, struct link **l, struct task *t); void engine_barrier(struct engine *e); void engine_compute_next_snapshot_time(struct engine *e); void engine_compute_next_stf_time(struct engine *e); +void engine_read_next_snapshot_time(struct engine *e); void engine_compute_next_statistics_time(struct engine *e); void engine_recompute_displacement_constraint(struct engine *e); void engine_unskip(struct engine *e); @@ -373,6 +375,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_time_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, -- GitLab From 7e031e161e698038a5b3338c45a2988aeb7f0f3d Mon Sep 17 00:00:00 2001 From: lhausamm Date: Fri, 1 Jun 2018 17:24:00 +0200 Subject: [PATCH 02/32] Add cosmological function between a and z --- src/cosmology.c | 11 +++++++++++ src/cosmology.h | 8 ++++++++ 2 files changed, 19 insertions(+) diff --git a/src/cosmology.c b/src/cosmology.c index e0ea1e9cd..1f74f696a 100644 --- a/src/cosmology.c +++ b/src/cosmology.c @@ -639,6 +639,17 @@ double cosmology_get_delta_time(const struct cosmology *c, return t2 - t1; } + +/** + * @brief Compute the scale factor from the redshift. + * + * @brief c The current #cosmology. + * @brief redshift The redshift to compute. + */ +double cosmology_get_a_from_z(const struct cosmology *c, double redshift) { + return 1. / (1. + redshift); +} + /** * @brief Prints the #cosmology model to stdout. */ diff --git a/src/cosmology.h b/src/cosmology.h index 97d62ec98..3b998c4b2 100644 --- a/src/cosmology.h +++ b/src/cosmology.h @@ -180,6 +180,13 @@ double cosmology_get_therm_kick_factor(const struct cosmology *cosmo, double cosmology_get_delta_time(const struct cosmology *c, integertime_t ti_start, integertime_t ti_end); +double cosmology_get_delta_time(const struct cosmology *c, double a1, + double a2); + +double cosmology_get_a_from_z(const struct cosmology *c, + double redshift); + +double cosmology_get_time_since_big_bang(const struct cosmology *c, double a); void cosmology_init(struct swift_params *params, const struct unit_system *us, const struct phys_const *phys_const, struct cosmology *c); @@ -188,6 +195,7 @@ void cosmology_init_no_cosmo(struct cosmology *c); void cosmology_print(const struct cosmology *c); void cosmology_clean(struct cosmology *c); + #ifdef HAVE_HDF5 void cosmology_write_model(hid_t h_grp, const struct cosmology *c); #endif -- GitLab From 81cf00f7f3e63d49ae9542be0c129f7fda660369 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Fri, 1 Jun 2018 17:24:13 +0200 Subject: [PATCH 03/32] snaplist: add transformation between units (in progress) --- src/common_io.c | 42 +++++++++++++++++++++++++++++++++++++++--- src/common_io.h | 9 ++++++++- src/engine.c | 9 ++++++--- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/src/common_io.c b/src/common_io.c index 1c6e9434d..8066a305f 100644 --- a/src/common_io.c +++ b/src/common_io.c @@ -968,7 +968,8 @@ void io_write_output_field_parameter(const char* filename) { } -void time_array_read_file(struct time_array *times, const char* filename) { +void time_array_read_file(struct time_array *times, const char* filename, + struct cosmology *cosmo) { /* initialize times */ times->size = 0; @@ -976,10 +977,35 @@ void time_array_read_file(struct time_array *times, const char* filename) { FILE* file = fopen(filename, "r"); if (file == NULL) error("Error opening file '%s'", filename); - /* Read file */ + /* Read header */ ssize_t read; size_t len = 0; char *line = NULL; + 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 = TIME_ARRAY_REDSHIFT; + else if (!strcmp(line, "# Age")) + type = TIME_ARRAY_AGE; + else if (!strcmp(line, "# Scale Factor")) + type = TIME_ARRAY_SCALE_FACTOR; + else + error("Unable to interpret the header (%s) in file '%s'", + line, filename); + + if (!cosmo && + (type == TIME_ARRAY_SCALE_FACTOR || type == TIME_ARRAY_REDSHIFT)) + error("Unable to compute a redshift or a scale factor without cosmology. " + "Please change the header in '%s'", filename); + + + /* Read file */ while ((read = getline(&line, &len, file)) != -1) { /* Check data size */ @@ -988,19 +1014,29 @@ void time_array_read_file(struct time_array *times, const char* filename) { "Please decrease the number of time required in '%s'.", filename); + double *time = ×->times[times->size]; /* Write data to time array */ - if (sscanf(line, "%lf", ×->times[times->size]) != 1) { + 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 == TIME_ARRAY_REDSHIFT) + *time = cosmology_get_a_from_z(cosmo, *time); + + if (cosmo && type == TIME_ARRAY_AGE) + error("Not implemented"); + /* Update size */ times->size += 1; } time_array_print(times); + + fclose(file); } void time_array_print(const struct time_array *times) { diff --git a/src/common_io.h b/src/common_io.h index 039a794e8..cb6308c46 100644 --- a/src/common_io.h +++ b/src/common_io.h @@ -33,6 +33,7 @@ #define TIME_ARRAY_MAX_SIZE 8192 /* Avoid cyclic inclusion problems */ +struct cosmology; struct part; struct gpart; struct spart; @@ -63,6 +64,12 @@ enum IO_DATA_TYPE { */ enum IO_STF_OUTPUT_FORMAT { STEPS = 0, TIME }; +enum TIME_ARRAY_TYPE { + TIME_ARRAY_AGE, + TIME_ARRAY_REDSHIFT, + TIME_ARRAY_SCALE_FACTOR, +}; + struct time_array { double times[TIME_ARRAY_MAX_SIZE]; size_t size; @@ -119,7 +126,7 @@ void io_check_output_fields(const struct swift_params* params, void io_write_output_field_parameter(const char* filename); -void time_array_read_file(struct time_array *times, const char* filename); +void time_array_read_file(struct time_array *times, const char* filename, struct cosmology *cosmo); void time_array_print(const struct time_array *times); #endif /* SWIFT_COMMON_IO_H */ diff --git a/src/engine.c b/src/engine.c index cba6888ca..2b1b4804f 100644 --- a/src/engine.c +++ b/src/engine.c @@ -6890,6 +6890,9 @@ void engine_struct_restore(struct engine *e, FILE *stream) { void engine_read_time_files(struct engine *e, const struct swift_params *params) { char filename[PARSER_MAX_LINE_SIZE]; + struct cosmology *cosmo = NULL; + if (e->policy & engine_policy_cosmology) + cosmo = e->cosmology; /* Read snapshot time array */ e->time_array_snapshots = (struct time_array*) malloc(sizeof(struct time_array)); @@ -6900,7 +6903,7 @@ void engine_read_time_files(struct engine *e, const struct swift_params *params) if (strcmp(filename, "")) { message("Reading snaplist file."); - time_array_read_file(e->time_array_snapshots, filename); + time_array_read_file(e->time_array_snapshots, filename, cosmo); } } @@ -6912,9 +6915,9 @@ void engine_read_next_snapshot_time(struct engine *e) { /* Find upper-bound on last output */ double time_end; if (is_cosmo) - time_end = e->cosmology->a_end * e->delta_time_snapshot; + time_end = e->cosmology->a_end; else - time_end = e->time_end + e->delta_time_snapshot; + time_end = e->time_end; /* Find next snasphot above current time */ double time; -- GitLab From cb0d7c26642467b44f4a654141fc5ca1f3e78d97 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 10:19:28 +0200 Subject: [PATCH 04/32] Add function giving scale factor from time --- src/cosmology.c | 41 +++++++++++++++++++++++++++++++++++++++++ src/cosmology.h | 2 ++ 2 files changed, 43 insertions(+) diff --git a/src/cosmology.c b/src/cosmology.c index 1f74f696a..7da8f7594 100644 --- a/src/cosmology.c +++ b/src/cosmology.c @@ -650,6 +650,47 @@ double cosmology_get_a_from_z(const struct cosmology *c, double redshift) { return 1. / (1. + redshift); } +/** + * @brief Compute scale factor from time since big bang. + * + * WARNING: This function is slow, therefore it should not be called more than + * once per time step. + * + * Find the scale factor from the time since big bang using the bisection method + * on $ t - f(a) = 0 $ where $ f(a) $ is #cosmology_get_time_since_big_bang + * + * By knowing that this function is a decreasing monotonic function, we can + * avoid a few checks. + * + * @param cosmo The current #cosmology. + * @param t since the big bang + * @return The scale factor. + */ +double cosmology_get_scale_factor(const struct cosmology *cosmo, double t) { + /* convergence limit and current error */ + const double limit = 1e-8; + double eps = 1.; + + /* limits in scale factor */ + double a_low = 1e-9; + double a_up = 1.; + + /* do the bisection method */ + while (eps > limit) { + double a_tmp = 0.5 * (a_low + a_up); + double f_tmp = t - cosmology_get_time_since_big_bang(cosmo, a_tmp); + + if (f_tmp > 0) + a_low = a_tmp; + else + a_up = a_tmp; + + eps = a_up - a_low; + } + + return 0.5 * (a_low + a_up); +} + /** * @brief Prints the #cosmology model to stdout. */ diff --git a/src/cosmology.h b/src/cosmology.h index 3b998c4b2..6f6f14620 100644 --- a/src/cosmology.h +++ b/src/cosmology.h @@ -186,6 +186,8 @@ double cosmology_get_delta_time(const struct cosmology *c, double a1, double cosmology_get_a_from_z(const struct cosmology *c, double redshift); +double cosmology_get_scale_factor(const struct cosmology *cosmo, double t); + double cosmology_get_time_since_big_bang(const struct cosmology *c, double a); void cosmology_init(struct swift_params *params, const struct unit_system *us, const struct phys_const *phys_const, struct cosmology *c); -- GitLab From 0138305bde3b8cc1e64b9d6f18d02def2365cdb4 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 10:19:51 +0200 Subject: [PATCH 05/32] Snaplist: last transformation implemented --- src/common_io.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common_io.c b/src/common_io.c index 8066a305f..f30eef1cf 100644 --- a/src/common_io.c +++ b/src/common_io.c @@ -991,7 +991,7 @@ void time_array_read_file(struct time_array *times, const char* filename, int type; if (!strcmp(line, "# Redshift")) type = TIME_ARRAY_REDSHIFT; - else if (!strcmp(line, "# Age")) + else if (!strcmp(line, "# Time")) type = TIME_ARRAY_AGE; else if (!strcmp(line, "# Scale Factor")) type = TIME_ARRAY_SCALE_FACTOR; @@ -1028,7 +1028,7 @@ void time_array_read_file(struct time_array *times, const char* filename, *time = cosmology_get_a_from_z(cosmo, *time); if (cosmo && type == TIME_ARRAY_AGE) - error("Not implemented"); + *time = cosmology_get_scale_factor(cosmo, *time); /* Update size */ times->size += 1; -- GitLab From 0913599af475edef76583045b08ca4d60d8681e4 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 10:38:50 +0200 Subject: [PATCH 06/32] Split common_io and snaplist --- src/Makefile.am | 5 ++-- src/common_io.c | 80 ------------------------------------------------- src/common_io.h | 4 --- src/engine.c | 15 +++++----- src/engine.h | 2 +- src/swift.h | 1 + 6 files changed, 13 insertions(+), 94 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 7e54ecab6..35d691737 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 + mesh_gravity.h cbrt.h velociraptor_interface.h swift_velociraptor_part.h snaplist.h # Common source files AM_SOURCES = space.c runner.c queue.c task.c cell.c engine.c \ @@ -60,7 +60,8 @@ AM_SOURCES = space.c runner.c queue.c task.c cell.c engine.c \ statistics.c runner_doiact_vec.c profiler.c dump.c logger.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 + chemistry.c cosmology.c restart.c mesh_gravity.c velociraptor_interface.c \ + snaplist.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/common_io.c b/src/common_io.c index f30eef1cf..683111075 100644 --- a/src/common_io.c +++ b/src/common_io.c @@ -966,83 +966,3 @@ void io_write_output_field_parameter(const char* filename) { "'%s'.\n", filename); } - - -void time_array_read_file(struct time_array *times, const char* filename, - struct cosmology *cosmo) { - /* initialize times */ - times->size = 0; - - /* Open file */ - FILE* file = fopen(filename, "r"); - if (file == NULL) error("Error opening file '%s'", filename); - - /* Read header */ - ssize_t read; - size_t len = 0; - char *line = NULL; - 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 = TIME_ARRAY_REDSHIFT; - else if (!strcmp(line, "# Time")) - type = TIME_ARRAY_AGE; - else if (!strcmp(line, "# Scale Factor")) - type = TIME_ARRAY_SCALE_FACTOR; - else - error("Unable to interpret the header (%s) in file '%s'", - line, filename); - - if (!cosmo && - (type == TIME_ARRAY_SCALE_FACTOR || type == TIME_ARRAY_REDSHIFT)) - error("Unable to compute a redshift or a scale factor without cosmology. " - "Please change the header in '%s'", filename); - - - /* Read file */ - while ((read = getline(&line, &len, file)) != -1) { - - /* Check data size */ - if (times->size == TIME_ARRAY_MAX_SIZE) - error("Not enough memory to write the time array buffer. " - "Please decrease the number of time required in '%s'.", - filename); - - double *time = ×->times[times->size]; - /* Write data to time array */ - 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 == TIME_ARRAY_REDSHIFT) - *time = cosmology_get_a_from_z(cosmo, *time); - - if (cosmo && type == TIME_ARRAY_AGE) - *time = cosmology_get_scale_factor(cosmo, *time); - - /* Update size */ - times->size += 1; - } - - time_array_print(times); - - fclose(file); -} - -void time_array_print(const struct time_array *times) { - - printf("/*\t Time Array\t */\n"); - for(size_t ind = 0; ind < times->size; ind++) { - printf("\t%lf\n", times->times[ind]); - } -} diff --git a/src/common_io.h b/src/common_io.h index cb6308c46..fe69dd0b7 100644 --- a/src/common_io.h +++ b/src/common_io.h @@ -30,7 +30,6 @@ #define PARTICLE_GROUP_BUFFER_SIZE 50 #define FILENAME_BUFFER_SIZE 150 #define IO_BUFFER_ALIGNMENT 1024 -#define TIME_ARRAY_MAX_SIZE 8192 /* Avoid cyclic inclusion problems */ struct cosmology; @@ -126,7 +125,4 @@ void io_check_output_fields(const struct swift_params* params, void io_write_output_field_parameter(const char* filename); -void time_array_read_file(struct time_array *times, const char* filename, struct cosmology *cosmo); -void time_array_print(const struct time_array *times); - #endif /* SWIFT_COMMON_IO_H */ diff --git a/src/engine.c b/src/engine.c index 2b1b4804f..9392b2ff9 100644 --- a/src/engine.c +++ b/src/engine.c @@ -78,6 +78,7 @@ #include "runner.h" #include "serial_io.h" #include "single_io.h" +#include "snaplist.h" #include "sort_part.h" #include "sourceterms.h" #include "statistics.h" @@ -5765,7 +5766,7 @@ 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->time_array_snapshots = NULL; + e->snaplist_snapshots = NULL; e->ti_next_snapshot = 0; parser_get_param_string(params, "Snapshots:basename", e->snapshot_base_name); e->snapshot_compression = @@ -6439,7 +6440,7 @@ void engine_print_policy(struct engine *e) { */ void engine_compute_next_snapshot_time(struct engine *e) { /* Do snaplist file case */ - if (e->time_array_snapshots) { + if (e->snaplist_snapshots) { engine_read_next_snapshot_time(e); return; } @@ -6743,8 +6744,8 @@ void engine_clean(struct engine *e) { } free(e->runners); free(e->snapshot_units); - if (e->time_array_snapshots) - free(e->time_array_snapshots); + if (e->snaplist_snapshots) + free(e->snaplist_snapshots); free(e->links); free(e->cell_loc); scheduler_clean(&e->sched); @@ -6895,7 +6896,7 @@ void engine_read_time_files(struct engine *e, const struct swift_params *params) cosmo = e->cosmology; /* Read snapshot time array */ - e->time_array_snapshots = (struct time_array*) malloc(sizeof(struct time_array)); + e->snaplist_snapshots = (struct snaplist*) malloc(sizeof(struct snaplist)); strcpy(filename, ""); parser_get_opt_param_string(params, "Snapshots:snaplist", @@ -6903,14 +6904,14 @@ void engine_read_time_files(struct engine *e, const struct swift_params *params) if (strcmp(filename, "")) { message("Reading snaplist file."); - time_array_read_file(e->time_array_snapshots, filename, cosmo); + snaplist_read_file(e->snaplist_snapshots, filename, cosmo); } } void engine_read_next_snapshot_time(struct engine *e) { int is_cosmo = e->policy & engine_policy_cosmology; - const struct time_array *t = e->time_array_snapshots; + const struct snaplist *t = e->snaplist_snapshots; /* Find upper-bound on last output */ double time_end; diff --git a/src/engine.h b/src/engine.h index 7f2bcc020..fffa2b782 100644 --- a/src/engine.h +++ b/src/engine.h @@ -214,7 +214,7 @@ struct engine { double a_first_snapshot; double time_first_snapshot; double delta_time_snapshot; - struct time_array *time_array_snapshots; + struct snaplist *snaplist_snapshots; /* Integer time of the next snapshot */ integertime_t ti_next_snapshot; diff --git a/src/swift.h b/src/swift.h index 322237688..b6b7ed68e 100644 --- a/src/swift.h +++ b/src/swift.h @@ -63,6 +63,7 @@ #include "scheduler.h" #include "serial_io.h" #include "single_io.h" +#include "snaplist.h" #include "sourceterms.h" #include "space.h" #include "task.h" -- GitLab From 60d9e147dbd6d0aa28db68b1dfd91edbbd4b4e80 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 11:12:09 +0200 Subject: [PATCH 07/32] Add snaplist restart --- src/engine.c | 46 +++++++++++++-- src/engine.h | 4 +- src/snaplist.c | 150 +++++++++++++++++++++++++++++++++++++++++++++++++ src/snaplist.h | 49 ++++++++++++++++ 4 files changed, 242 insertions(+), 7 deletions(-) create mode 100644 src/snaplist.c create mode 100644 src/snaplist.h diff --git a/src/engine.c b/src/engine.c index 9392b2ff9..6c11d9f97 100644 --- a/src/engine.c +++ b/src/engine.c @@ -5832,7 +5832,7 @@ void engine_init(struct engine *e, struct space *s, struct swift_params *params, e->ti_current = 0; } - engine_read_time_files(e, params); + engine_read_snaplist_files(e, params); } /** @@ -6744,8 +6744,10 @@ void engine_clean(struct engine *e) { } free(e->runners); free(e->snapshot_units); - if (e->snaplist_snapshots) + if (e->snaplist_snapshots) { + snaplist_clean(e->snaplist_snapshots); free(e->snaplist_snapshots); + } free(e->links); free(e->cell_loc); scheduler_clean(&e->sched); @@ -6788,6 +6790,8 @@ 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); } /** @@ -6883,28 +6887,58 @@ 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; + } + /* Want to force a rebuild before using this engine. Wait to repartition.*/ e->forcerebuild = 1; e->forcerepart = 0; } - -void engine_read_time_files(struct engine *e, const struct swift_params *params) { +/** + * @brief Read all the snaplist files + * + * @param e The #engine. + * @param params The #swift_params. + */ +void engine_read_snaplist_files(struct engine *e, const struct swift_params *params) { char filename[PARSER_MAX_LINE_SIZE]; + struct snaplist *list; + + /* get cosmo */ struct cosmology *cosmo = NULL; if (e->policy & engine_policy_cosmology) cosmo = e->cosmology; - /* Read snapshot time array */ + /* Deal with snapshots */ e->snaplist_snapshots = (struct snaplist*) malloc(sizeof(struct snaplist)); + list = e->snaplist_snapshots; strcpy(filename, ""); parser_get_opt_param_string(params, "Snapshots:snaplist", filename, ""); + /* Read snaplist for snapshots */ if (strcmp(filename, "")) { message("Reading snaplist file."); - snaplist_read_file(e->snaplist_snapshots, filename, cosmo); + snaplist_read_file(list, filename, cosmo, engine_max_snaplist_snapshots); + + if (list->size < 2) + error("You need to provide more snapshots in '%s'", filename); + + /* Set data for later checks */ + if (cosmo) { + e->delta_time_snapshot = list->times[1] / list->times[0]; + e->a_first_snapshot = list->times[0]; + } + else { + e->delta_time_snapshot = list->times[1] - list->times[0]; + e->time_first_snapshot = list->times[0]; + } } } diff --git a/src/engine.h b/src/engine.h index fffa2b782..55c3493ca 100644 --- a/src/engine.h +++ b/src/engine.h @@ -97,6 +97,8 @@ 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). @@ -375,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_time_files(struct engine *e, const struct swift_params *params); +void engine_read_snaplist_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/snaplist.c b/src/snaplist.c new file mode 100644 index 000000000..33ed5b72c --- /dev/null +++ b/src/snaplist.c @@ -0,0 +1,150 @@ +/******************************************************************************* + * 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 . + * + ******************************************************************************/ + +/* Config parameters. */ +#include "../config.h" + +/* This object's header. */ +#include "snaplist.h" + +/* Local includes. */ +#include "cosmology.h" +#include "error.h" +#include "restart.h" + +/* Some standard headers. */ +#include + +/** + * @brief Read a file containing a list of time + * + * @param snaplist The #snaplist to fill. + * @param filename The file to read. + * @param cosmo The #cosmology model. + * @param max_size The maximal size for the time array. + */ +void snaplist_read_file(struct snaplist *snaplist, const char* filename, + struct cosmology *cosmo, size_t max_size) { + /* initialize snaplist */ + snaplist->size = 0; + snaplist->times = (double *) malloc(sizeof(double) * max_size); + snaplist->max_size = max_size; + + /* Open file */ + FILE* file = fopen(filename, "r"); + if (file == NULL) error("Error opening file '%s'", filename); + + /* Read header */ + ssize_t read; + size_t len = 0; + char *line = NULL; + 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 = SNAPLIST_REDSHIFT; + else if (!strcmp(line, "# Time")) + type = SNAPLIST_AGE; + else if (!strcmp(line, "# Scale Factor")) + type = SNAPLIST_SCALE_FACTOR; + else + error("Unable to interpret the header (%s) in file '%s'", + line, filename); + + if (!cosmo && + (type == SNAPLIST_SCALE_FACTOR || type == SNAPLIST_REDSHIFT)) + error("Unable to compute a redshift or a scale factor without cosmology. " + "Please change the header in '%s'", filename); + + + /* Read file */ + while ((read = getline(&line, &len, file)) != -1) { + + /* Check data size */ + if (snaplist->size == max_size) + error("Not enough memory to write the snaplist buffer. " + "Please decrease the total number of output required in '%s'.", + filename); + + double *time = &snaplist->times[snaplist->size]; + /* Write data to snaplist */ + 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 == SNAPLIST_REDSHIFT) + *time = cosmology_get_a_from_z(cosmo, *time); + + if (cosmo && type == SNAPLIST_AGE) + *time = cosmology_get_scale_factor(cosmo, *time); + + /* Update size */ + snaplist->size += 1; + } + + fclose(file); +} + +/** + * @brief Print a snaplist + */ +void snaplist_print(const struct snaplist *snaplist) { + + printf("/*\t Time Array\t */\n"); + for(size_t ind = 0; ind < snaplist->size; ind++) { + printf("\t%lf\n", snaplist->times[ind]); + } +} + +/** + * @brief Clean a snaplist + */ +void snaplist_clean(struct snaplist *snaplist) { + free(snaplist->times); +} + +/** + * @brief Dump a snaplist in a restart file + */ +void snaplist_struct_dump(struct snaplist *list, FILE *stream) { + restart_write_blocks(list, sizeof(struct snaplist), 1, stream, "snaplist", "snaplist struct"); + + restart_write_blocks(list, list->max_size, sizeof(double), stream, "snaplist", "times"); +} + + +/** + * @brief Restore a snaplist from a restart file + */ +void snaplist_struct_restore(struct snaplist * list, FILE *stream) { + restart_read_blocks(list, sizeof(struct snaplist), 1, stream, NULL, "snaplist struct"); + + list->times = (double *) malloc(sizeof(double) * list->max_size); + restart_read_blocks(list->times, list->max_size, sizeof(double), stream, NULL, "times"); +} diff --git a/src/snaplist.h b/src/snaplist.h new file mode 100644 index 000000000..616488640 --- /dev/null +++ b/src/snaplist.h @@ -0,0 +1,49 @@ +/******************************************************************************* + * 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 . + * + ******************************************************************************/ +#ifndef SWIFT_SNAPLIST_H +#define SWIFT_SNAPLIST_H + +/* Config parameters. */ +#include "../config.h" + +/* Local includes */ +#include "cosmology.h" + +enum SNAPLIST_TYPE { + SNAPLIST_AGE, + SNAPLIST_REDSHIFT, + SNAPLIST_SCALE_FACTOR, +}; + +struct snaplist { + double *times; + size_t size; + size_t max_size; +}; + + +void snaplist_read_file(struct snaplist *snaplist, const char* filename, struct cosmology *cosmo, + size_t max_size); +void snaplist_print(const struct snaplist *snaplist); +void snaplist_clean(struct snaplist *snaplist); +void snaplist_struct_dump(struct snaplist *list, FILE *stream); +void snaplist_struct_restore(struct snaplist * list, FILE *stream); + +#endif // SWIFT_SNAPLIST_H -- GitLab From 104cbb1b3c927875d7d130d1c4bd1f40b471717f Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 11:35:07 +0200 Subject: [PATCH 08/32] add a new check for next output time --- src/engine.c | 305 ++++++++++++++++++++++++++++++++++----------------- src/engine.h | 2 + 2 files changed, 208 insertions(+), 99 deletions(-) diff --git a/src/engine.c b/src/engine.c index 6c11d9f97..fce807675 100644 --- a/src/engine.c +++ b/src/engine.c @@ -5767,6 +5767,7 @@ void engine_init(struct engine *e, struct space *s, struct swift_params *params, e->delta_time_snapshot = parser_get_param_double(params, "Snapshots:delta_time"); e->snaplist_snapshots = NULL; + e->snaplist_stats = NULL; e->ti_next_snapshot = 0; parser_get_param_string(params, "Snapshots:basename", e->snapshot_base_name); e->snapshot_compression = @@ -6496,12 +6497,77 @@ void engine_compute_next_snapshot_time(struct engine *e) { } } +/** + * @brief Read the next time (on the time line) for a dump + * + * @param e The #engine. + */ +void engine_read_next_snapshot_time(struct engine *e) { + int is_cosmo = e->policy & engine_policy_cosmology; + const struct snaplist *t = e->snaplist_snapshots; + + /* Find upper-bound on last output */ + double time_end; + if (is_cosmo) + time_end = e->cosmology->a_end; + else + time_end = e->time_end; + + /* Find next snasphot above current time */ + double time; + time = t->times[0]; + size_t ind = 0; + while (time < time_end) { + + /* Output time on the integer timeline */ + if (is_cosmo) + e->ti_next_snapshot = log(time / e->cosmology->a_begin) / e->time_base; + else + e->ti_next_snapshot = (time - e->time_begin) / e->time_base; + + /* Found it? */ + if (e->ti_next_snapshot > e->ti_current) break; + + ind += 1; + if (ind == t->size) + break; + + time = t->times[ind]; + } + + /* Deal with last snapshot */ + if (e->ti_next_snapshot >= max_nr_timesteps || + ind == t->size || time >= time_end) { + e->ti_next_snapshot = -1; + if (e->verbose) message("No further output time."); + } else { + + /* Be nice, talk... */ + if (is_cosmo) { + const double next_snapshot_time = + exp(e->ti_next_snapshot * e->time_base) * e->cosmology->a_begin; + if (e->verbose) + message("Next snapshot time set to a=%e.", next_snapshot_time); + } else { + const double next_snapshot_time = + e->ti_next_snapshot * e->time_base + e->time_begin; + if (e->verbose) + message("Next snapshot time set to t=%e.", next_snapshot_time); + } + } + +} + /** * @brief Computes the next time (on the time line) for a statistics dump * * @param e The #engine. */ void engine_compute_next_statistics_time(struct engine *e) { + if (e->snaplist_stats) { + engine_read_next_statistics_time(e); + return; + } /* Find upper-bound on last output */ double time_end; @@ -6611,6 +6677,133 @@ void engine_compute_next_stf_time(struct engine *e) { } } +/** + * @brief Read the next time (on the time line) for a statistics dump + * + * @param e The #engine. + */ +void engine_read_next_statistics_time(struct engine *e) { + int is_cosmo = e->policy & engine_policy_cosmology; + const struct snaplist *t = e->snaplist_stats; + + /* Find upper-bound on last output */ + double time_end; + if (is_cosmo) + time_end = e->cosmology->a_end; + else + time_end = e->time_end; + + /* Find next snasphot above current time */ + double time = t->times[0]; + size_t ind = 0; + while (time < time_end) { + + /* Output time on the integer timeline */ + if (is_cosmo) + e->ti_next_stats = log(time / e->cosmology->a_begin) / e->time_base; + else + e->ti_next_stats = (time - e->time_begin) / e->time_base; + + /* Found it? */ + if (e->ti_next_stats > e->ti_current) break; + + ind += 1; + if (ind == t->size) + break; + + time = t->times[ind]; + } + + /* Deal with last statistics */ + if (e->ti_next_stats >= max_nr_timesteps || + ind == t->size || time >= time_end) { + e->ti_next_stats = -1; + if (is_cosmo) { + const double next_statistics_time = + exp(e->ti_next_stats * e->time_base) * e->cosmology->a_begin; + if (e->verbose) + message("Next output time for stats set to a=%e.", + next_statistics_time); + } else { + const double next_statistics_time = + 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); + } + } +} + +/** + * @brief Read all the snaplist files + * + * @param e The #engine. + * @param params The #swift_params. + */ +void engine_read_snaplist_files(struct engine *e, const struct swift_params *params) { + char filename[PARSER_MAX_LINE_SIZE]; + struct snaplist *list; + + /* get cosmo */ + struct cosmology *cosmo = NULL; + if (e->policy & engine_policy_cosmology) + cosmo = e->cosmology; + + /* Deal with snapshots */ + e->snaplist_snapshots = (struct snaplist*) malloc(sizeof(struct snaplist)); + list = e->snaplist_snapshots; + + strcpy(filename, ""); + parser_get_opt_param_string(params, "Snapshots:snaplist", + filename, ""); + + /* Read snaplist for snapshots */ + if (strcmp(filename, "")) { + message("Reading snaplist file."); + snaplist_read_file(list, filename, cosmo, engine_max_snaplist_snapshots); + + if (list->size < 2) + error("You need to provide more snapshots in '%s'", filename); + + /* Set data for later checks */ + if (cosmo) { + e->delta_time_snapshot = list->times[1] / list->times[0]; + e->a_first_snapshot = list->times[0]; + } + else { + e->delta_time_snapshot = list->times[1] - list->times[0]; + e->time_first_snapshot = list->times[0]; + } + } + + /* Deal with stats */ + e->snaplist_stats = (struct snaplist*) malloc(sizeof(struct snaplist)); + list = e->snaplist_stats; + + strcpy(filename, ""); + parser_get_opt_param_string(params, "Statistics:snaplist", + filename, ""); + + /* Read snaplist for stats */ + if (strcmp(filename, "")) { + message("Reading snaplist file."); + snaplist_read_file(list, filename, cosmo, engine_max_snaplist_stats); + + if (list->size < 2) + error("You need to provide more snapshots in '%s'", filename); + + /* Set data for later checks */ + if (cosmo) { + e->delta_time_statistics = list->times[1] / list->times[0]; + e->a_first_statistics = list->times[0]; + } + else { + e->delta_time_statistics = list->times[1] - list->times[0]; + e->time_first_statistics = list->times[0]; + } + } +} + /** * @brief Computes the maximal time-step allowed by the max RMS displacement * condition. @@ -6748,6 +6941,10 @@ void engine_clean(struct engine *e) { snaplist_clean(e->snaplist_snapshots); free(e->snaplist_snapshots); } + if (e->snaplist_stats) { + snaplist_clean(e->snaplist_stats); + free(e->snaplist_stats); + } free(e->links); free(e->cell_loc); scheduler_clean(&e->sched); @@ -6792,6 +6989,8 @@ void engine_struct_dump(struct engine *e, FILE *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); } /** @@ -6894,106 +7093,14 @@ void engine_struct_restore(struct engine *e, FILE *stream) { e->snaplist_snapshots = snaplist_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; + } + /* Want to force a rebuild before using this engine. Wait to repartition.*/ e->forcerebuild = 1; e->forcerepart = 0; } - -/** - * @brief Read all the snaplist files - * - * @param e The #engine. - * @param params The #swift_params. - */ -void engine_read_snaplist_files(struct engine *e, const struct swift_params *params) { - char filename[PARSER_MAX_LINE_SIZE]; - struct snaplist *list; - - /* get cosmo */ - struct cosmology *cosmo = NULL; - if (e->policy & engine_policy_cosmology) - cosmo = e->cosmology; - - /* Deal with snapshots */ - e->snaplist_snapshots = (struct snaplist*) malloc(sizeof(struct snaplist)); - list = e->snaplist_snapshots; - - strcpy(filename, ""); - parser_get_opt_param_string(params, "Snapshots:snaplist", - filename, ""); - - /* Read snaplist for snapshots */ - if (strcmp(filename, "")) { - message("Reading snaplist file."); - snaplist_read_file(list, filename, cosmo, engine_max_snaplist_snapshots); - - if (list->size < 2) - error("You need to provide more snapshots in '%s'", filename); - - /* Set data for later checks */ - if (cosmo) { - e->delta_time_snapshot = list->times[1] / list->times[0]; - e->a_first_snapshot = list->times[0]; - } - else { - e->delta_time_snapshot = list->times[1] - list->times[0]; - e->time_first_snapshot = list->times[0]; - } - } -} - - -void engine_read_next_snapshot_time(struct engine *e) { - int is_cosmo = e->policy & engine_policy_cosmology; - const struct snaplist *t = e->snaplist_snapshots; - - /* Find upper-bound on last output */ - double time_end; - if (is_cosmo) - time_end = e->cosmology->a_end; - else - time_end = e->time_end; - - /* Find next snasphot above current time */ - double time; - time = t->times[0]; - size_t ind = 0; - while (time < time_end) { - - /* Output time on the integer timeline */ - if (is_cosmo) - e->ti_next_snapshot = log(time / e->cosmology->a_begin) / e->time_base; - else - e->ti_next_snapshot = (time - e->time_begin) / e->time_base; - - /* Found it? */ - if (e->ti_next_snapshot > e->ti_current) break; - - ind += 1; - if (ind == t->size) - break; - - time = t->times[ind]; - } - - /* Deal with last snapshot */ - if (e->ti_next_snapshot >= max_nr_timesteps || ind == t->size) { - e->ti_next_snapshot = -1; - if (e->verbose) message("No further output time."); - } else { - - /* Be nice, talk... */ - if (is_cosmo) { - const double next_snapshot_time = - exp(e->ti_next_snapshot * e->time_base) * e->cosmology->a_begin; - if (e->verbose) - message("Next snapshot time set to a=%e.", next_snapshot_time); - } else { - const double next_snapshot_time = - e->ti_next_snapshot * e->time_base + e->time_begin; - if (e->verbose) - message("Next snapshot time set to t=%e.", next_snapshot_time); - } - } - -} diff --git a/src/engine.h b/src/engine.h index 55c3493ca..3bcdcbdb0 100644 --- a/src/engine.h +++ b/src/engine.h @@ -217,6 +217,7 @@ struct engine { double time_first_snapshot; double delta_time_snapshot; struct snaplist *snaplist_snapshots; + struct snaplist *snaplist_stats; /* Integer time of the next snapshot */ integertime_t ti_next_snapshot; @@ -370,6 +371,7 @@ void engine_compute_next_snapshot_time(struct engine *e); void engine_compute_next_stf_time(struct engine *e); void engine_read_next_snapshot_time(struct engine *e); void engine_compute_next_statistics_time(struct engine *e); +void engine_read_next_statistics_time(struct engine *e); void engine_recompute_displacement_constraint(struct engine *e); void engine_unskip(struct engine *e); void engine_drift_all(struct engine *e); -- GitLab From 5485248579b4252a43942e68bf949c1c2f84a8b5 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 13:53:31 +0200 Subject: [PATCH 09/32] Moved snaplist to outputlist and fixed restart --- src/Makefile.am | 4 +- src/engine.c | 90 +++++++++++++-------------- src/engine.h | 8 +-- src/outputlist.c | 157 +++++++++++++++++++++++++++++++++++++++++++++++ src/outputlist.h | 47 ++++++++++++++ src/swift.h | 2 +- 6 files changed, 255 insertions(+), 53 deletions(-) create mode 100644 src/outputlist.c create mode 100644 src/outputlist.h diff --git a/src/Makefile.am b/src/Makefile.am index 35d691737..fbc1d9fdc 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 fce807675..e4bc2795e 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 3bcdcbdb0..0adb8a104 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 000000000..eace03016 --- /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 . + * + ******************************************************************************/ + +/* 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 + +/** + * @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 000000000..829e6e4a7 --- /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 . + * + ******************************************************************************/ +#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 b6b7ed68e..e10938add 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" -- GitLab From 24b0ac54628cae082c708d0599e4862ff9f0de4b Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 13:54:29 +0200 Subject: [PATCH 10/32] Add new parameters in parameter_example.yml --- examples/parameter_example.yml | 2 + src/snaplist.c | 150 --------------------------------- src/snaplist.h | 49 ----------- 3 files changed, 2 insertions(+), 199 deletions(-) delete mode 100644 src/snaplist.c delete mode 100644 src/snaplist.h diff --git a/examples/parameter_example.yml b/examples/parameter_example.yml index 783b6030b..b3b356375 100644 --- a/examples/parameter_example.yml +++ b/examples/parameter_example.yml @@ -81,6 +81,7 @@ Snapshots: UnitVelocity_in_cgs: 1 # (Optional) Unit system for the outputs (Centimeters per second) UnitCurrent_in_cgs: 1 # (Optional) Unit system for the outputs (Amperes) UnitTemp_in_cgs: 1 # (Optional) Unit system for the outputs (Kelvin) + outputlist: "" # (Optional) File containing the output times (see documentation) # Parameters governing the conserved quantities statistics Statistics: @@ -89,6 +90,7 @@ Statistics: time_first: 0. # (Optional) Time of the first stats output if non-cosmological time-integration (in internal units) energy_file_name: energy # (Optional) File name for energy output timestep_file_name: timesteps # (Optional) File name for timing information output. Note: No underscores "_" allowed in file name + outputlist: "" # (Optional) File containing the output times (see documentation) # Parameters related to the initial conditions InitialConditions: diff --git a/src/snaplist.c b/src/snaplist.c deleted file mode 100644 index 33ed5b72c..000000000 --- a/src/snaplist.c +++ /dev/null @@ -1,150 +0,0 @@ -/******************************************************************************* - * 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 . - * - ******************************************************************************/ - -/* Config parameters. */ -#include "../config.h" - -/* This object's header. */ -#include "snaplist.h" - -/* Local includes. */ -#include "cosmology.h" -#include "error.h" -#include "restart.h" - -/* Some standard headers. */ -#include - -/** - * @brief Read a file containing a list of time - * - * @param snaplist The #snaplist to fill. - * @param filename The file to read. - * @param cosmo The #cosmology model. - * @param max_size The maximal size for the time array. - */ -void snaplist_read_file(struct snaplist *snaplist, const char* filename, - struct cosmology *cosmo, size_t max_size) { - /* initialize snaplist */ - snaplist->size = 0; - snaplist->times = (double *) malloc(sizeof(double) * max_size); - snaplist->max_size = max_size; - - /* Open file */ - FILE* file = fopen(filename, "r"); - if (file == NULL) error("Error opening file '%s'", filename); - - /* Read header */ - ssize_t read; - size_t len = 0; - char *line = NULL; - 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 = SNAPLIST_REDSHIFT; - else if (!strcmp(line, "# Time")) - type = SNAPLIST_AGE; - else if (!strcmp(line, "# Scale Factor")) - type = SNAPLIST_SCALE_FACTOR; - else - error("Unable to interpret the header (%s) in file '%s'", - line, filename); - - if (!cosmo && - (type == SNAPLIST_SCALE_FACTOR || type == SNAPLIST_REDSHIFT)) - error("Unable to compute a redshift or a scale factor without cosmology. " - "Please change the header in '%s'", filename); - - - /* Read file */ - while ((read = getline(&line, &len, file)) != -1) { - - /* Check data size */ - if (snaplist->size == max_size) - error("Not enough memory to write the snaplist buffer. " - "Please decrease the total number of output required in '%s'.", - filename); - - double *time = &snaplist->times[snaplist->size]; - /* Write data to snaplist */ - 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 == SNAPLIST_REDSHIFT) - *time = cosmology_get_a_from_z(cosmo, *time); - - if (cosmo && type == SNAPLIST_AGE) - *time = cosmology_get_scale_factor(cosmo, *time); - - /* Update size */ - snaplist->size += 1; - } - - fclose(file); -} - -/** - * @brief Print a snaplist - */ -void snaplist_print(const struct snaplist *snaplist) { - - printf("/*\t Time Array\t */\n"); - for(size_t ind = 0; ind < snaplist->size; ind++) { - printf("\t%lf\n", snaplist->times[ind]); - } -} - -/** - * @brief Clean a snaplist - */ -void snaplist_clean(struct snaplist *snaplist) { - free(snaplist->times); -} - -/** - * @brief Dump a snaplist in a restart file - */ -void snaplist_struct_dump(struct snaplist *list, FILE *stream) { - restart_write_blocks(list, sizeof(struct snaplist), 1, stream, "snaplist", "snaplist struct"); - - restart_write_blocks(list, list->max_size, sizeof(double), stream, "snaplist", "times"); -} - - -/** - * @brief Restore a snaplist from a restart file - */ -void snaplist_struct_restore(struct snaplist * list, FILE *stream) { - restart_read_blocks(list, sizeof(struct snaplist), 1, stream, NULL, "snaplist struct"); - - list->times = (double *) malloc(sizeof(double) * list->max_size); - restart_read_blocks(list->times, list->max_size, sizeof(double), stream, NULL, "times"); -} diff --git a/src/snaplist.h b/src/snaplist.h deleted file mode 100644 index 616488640..000000000 --- a/src/snaplist.h +++ /dev/null @@ -1,49 +0,0 @@ -/******************************************************************************* - * 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 . - * - ******************************************************************************/ -#ifndef SWIFT_SNAPLIST_H -#define SWIFT_SNAPLIST_H - -/* Config parameters. */ -#include "../config.h" - -/* Local includes */ -#include "cosmology.h" - -enum SNAPLIST_TYPE { - SNAPLIST_AGE, - SNAPLIST_REDSHIFT, - SNAPLIST_SCALE_FACTOR, -}; - -struct snaplist { - double *times; - size_t size; - size_t max_size; -}; - - -void snaplist_read_file(struct snaplist *snaplist, const char* filename, struct cosmology *cosmo, - size_t max_size); -void snaplist_print(const struct snaplist *snaplist); -void snaplist_clean(struct snaplist *snaplist); -void snaplist_struct_dump(struct snaplist *list, FILE *stream); -void snaplist_struct_restore(struct snaplist * list, FILE *stream); - -#endif // SWIFT_SNAPLIST_H -- GitLab From 4b62b3f768eda7607e2700fe2272e3a1fb3ce768 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 14:08:34 +0200 Subject: [PATCH 11/32] Add doc for outputlist --- .../source/GettingStarted/parameter_file.rst | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/doc/RTD/source/GettingStarted/parameter_file.rst b/doc/RTD/source/GettingStarted/parameter_file.rst index cecf2fa08..b4c55b675 100644 --- a/doc/RTD/source/GettingStarted/parameter_file.rst +++ b/doc/RTD/source/GettingStarted/parameter_file.rst @@ -12,6 +12,35 @@ parameters. Each section in this file corresponds to a different option in SWIFT and are not always required depending on the configuration options and the run time parameters. +Output List +~~~~~~~~~~~ + +In the sections ``Snapshots`` and ``Statistics``, you can specify the option ``output_list`` which receives a filename. +This file consists in a list of time where you want to output either a snapshot or a statistic. +With the header, you can choose between writing redshifts, scale factors or times. + +Example of file containing with times: +:: + # Time + 0.5 + 1.5 + 3.0 + 12.5 + +Example of file with scale factors: +:: + # Scale Factor + 0.1 + 0.2 + 0.3 + +Example of file with redshift: +:: + # Redshift + 20 + 15 + 10 + 5 Output Selection ~~~~~~~~~~~~~~~~ -- GitLab From 7eec2ad22ff415ee8dad13337bd9b0ccd5559fe2 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 14:08:53 +0200 Subject: [PATCH 12/32] Modify parameter from outputlist to output_list --- examples/parameter_example.yml | 4 ++-- src/engine.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/parameter_example.yml b/examples/parameter_example.yml index b3b356375..68f5c6e7a 100644 --- a/examples/parameter_example.yml +++ b/examples/parameter_example.yml @@ -81,7 +81,7 @@ Snapshots: UnitVelocity_in_cgs: 1 # (Optional) Unit system for the outputs (Centimeters per second) UnitCurrent_in_cgs: 1 # (Optional) Unit system for the outputs (Amperes) UnitTemp_in_cgs: 1 # (Optional) Unit system for the outputs (Kelvin) - outputlist: "" # (Optional) File containing the output times (see documentation) + output_list: "" # (Optional) File containing the output times (see documentation) # Parameters governing the conserved quantities statistics Statistics: @@ -90,7 +90,7 @@ Statistics: time_first: 0. # (Optional) Time of the first stats output if non-cosmological time-integration (in internal units) energy_file_name: energy # (Optional) File name for energy output timestep_file_name: timesteps # (Optional) File name for timing information output. Note: No underscores "_" allowed in file name - outputlist: "" # (Optional) File containing the output times (see documentation) + output_list: "" # (Optional) File containing the output times (see documentation) # Parameters related to the initial conditions InitialConditions: diff --git a/src/engine.c b/src/engine.c index e4bc2795e..91332f182 100644 --- a/src/engine.c +++ b/src/engine.c @@ -6754,7 +6754,7 @@ void engine_read_outputlist_files(struct engine *e, const struct swift_params *p list = e->outputlist_snapshots; strcpy(filename, ""); - parser_get_opt_param_string(params, "Snapshots:outputlist", + parser_get_opt_param_string(params, "Snapshots:output_list", filename, ""); /* Read outputlist for snapshots */ @@ -6781,7 +6781,7 @@ void engine_read_outputlist_files(struct engine *e, const struct swift_params *p list = e->outputlist_stats; strcpy(filename, ""); - parser_get_opt_param_string(params, "Statistics:outputlist", + parser_get_opt_param_string(params, "Statistics:output_list", filename, ""); /* Read outputlist for stats */ -- GitLab From 488a59fe6158d3ad7040711f1dfb8898d4ad7e0e Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 14:24:21 +0200 Subject: [PATCH 13/32] Improve comment for output_list --- examples/parameter_example.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/parameter_example.yml b/examples/parameter_example.yml index 68f5c6e7a..94e58ee5c 100644 --- a/examples/parameter_example.yml +++ b/examples/parameter_example.yml @@ -81,7 +81,7 @@ Snapshots: UnitVelocity_in_cgs: 1 # (Optional) Unit system for the outputs (Centimeters per second) UnitCurrent_in_cgs: 1 # (Optional) Unit system for the outputs (Amperes) UnitTemp_in_cgs: 1 # (Optional) Unit system for the outputs (Kelvin) - output_list: "" # (Optional) File containing the output times (see documentation) + output_list: "" # (Optional) File containing the output times (see documentation in "Parameter File" section) # Parameters governing the conserved quantities statistics Statistics: @@ -90,7 +90,7 @@ Statistics: time_first: 0. # (Optional) Time of the first stats output if non-cosmological time-integration (in internal units) energy_file_name: energy # (Optional) File name for energy output timestep_file_name: timesteps # (Optional) File name for timing information output. Note: No underscores "_" allowed in file name - output_list: "" # (Optional) File containing the output times (see documentation) + output_list: "" # (Optional) File containing the output times (see documentation in "Parameter File" section) # Parameters related to the initial conditions InitialConditions: -- GitLab From 63ebd0f2a35a5318b5478beadba9a2550cfa6053 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 17:02:56 +0200 Subject: [PATCH 14/32] Use interpolation table for cosmology_get_scale_factor --- src/cosmology.c | 69 +++++++++++++++++++++++++------------------------ src/cosmology.h | 3 +++ 2 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/cosmology.c b/src/cosmology.c index 7da8f7594..a443529c7 100644 --- a/src/cosmology.c +++ b/src/cosmology.c @@ -308,6 +308,8 @@ void cosmology_init_tables(struct cosmology *c) { (double *)malloc(cosmology_table_length * sizeof(double)); c->time_interp_table = (double *)malloc(cosmology_table_length * sizeof(double)); + c->scale_factor_interp_table = + (double *)malloc(cosmology_table_length * sizeof(double)); /* Prepare a table of scale factors for the integral bounds */ const double delta_a = @@ -372,6 +374,30 @@ void cosmology_init_tables(struct cosmology *c) { GSL_INTEG_GAUSS61, space, &result, &abserr); c->universe_age_at_present_day = result; + /* Inverse t(a) */ + const double time_init = c->time_interp_table_offset; + const double delta_t = (c->universe_age_at_present_day - time_init) / cosmology_table_length; + + int i_prev = 0; + for (int i = 0; i < cosmology_table_length; i++) { + /* Current time */ + double time_interp = delta_t * i; + + /* Find next time in time_interp_table */ + while (i_prev < cosmology_table_length && c->time_interp_table[i_prev] <= time_interp) { + i_prev++; + } + + /* Find linear interpolation scaling */ + double scale = time_interp - c->time_interp_table[i_prev - 1]; + scale /= c->time_interp_table[i_prev] - c->time_interp_table[i_prev - 1]; + scale += i_prev; + + /* Compute interpolated scale factor */ + double log_a = c->log_a_begin + scale * (c->log_a_end - c->log_a_begin) / cosmology_table_length; + c->scale_factor_interp_table[i] = exp(log_a) - c->a_begin; + } + /* Free the workspace and temp array */ gsl_integration_workspace_free(space); free(a_table); @@ -653,42 +679,16 @@ double cosmology_get_a_from_z(const struct cosmology *c, double redshift) { /** * @brief Compute scale factor from time since big bang. * - * WARNING: This function is slow, therefore it should not be called more than - * once per time step. - * - * Find the scale factor from the time since big bang using the bisection method - * on $ t - f(a) = 0 $ where $ f(a) $ is #cosmology_get_time_since_big_bang - * - * By knowing that this function is a decreasing monotonic function, we can - * avoid a few checks. - * - * @param cosmo The current #cosmology. - * @param t since the big bang + * @param c The current #cosmology. + * @param t time since the big bang * @return The scale factor. */ -double cosmology_get_scale_factor(const struct cosmology *cosmo, double t) { - /* convergence limit and current error */ - const double limit = 1e-8; - double eps = 1.; - - /* limits in scale factor */ - double a_low = 1e-9; - double a_up = 1.; - - /* do the bisection method */ - while (eps > limit) { - double a_tmp = 0.5 * (a_low + a_up); - double f_tmp = t - cosmology_get_time_since_big_bang(cosmo, a_tmp); - - if (f_tmp > 0) - a_low = a_tmp; - else - a_up = a_tmp; - - eps = a_up - a_low; - } - - return 0.5 * (a_low + a_up); +double cosmology_get_scale_factor(const struct cosmology *c, double t) { + /* scale factor between time_begin and t */ + const double a = + interp_table(c->scale_factor_interp_table, t, c->time_interp_table_offset, + c->universe_age_at_present_day); + return a + c->a_begin; } /** @@ -712,6 +712,7 @@ void cosmology_clean(struct cosmology *c) { free(c->grav_kick_fac_interp_table); free(c->hydro_kick_fac_interp_table); free(c->time_interp_table); + free(c->scale_factor_interp_table); } #ifdef HAVE_HDF5 diff --git a/src/cosmology.h b/src/cosmology.h index 6f6f14620..fdea957c6 100644 --- a/src/cosmology.h +++ b/src/cosmology.h @@ -156,6 +156,9 @@ struct cosmology { /*! Time interpolation table */ double *time_interp_table; + /*! Scale factor interpolation table */ + double *scale_factor_interp_table; + /*! Time between Big Bang and first entry in the table */ double time_interp_table_offset; -- GitLab From 55079a9ffea96f7c29630b34fe03441422517f32 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 17:26:51 +0200 Subject: [PATCH 15/32] Add new test for cosmology --- tests/Makefile.am | 6 ++-- tests/testCosmology.c | 78 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 tests/testCosmology.c diff --git a/tests/Makefile.am b/tests/Makefile.am index ad7dcf92d..46d512cd4 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -28,7 +28,7 @@ TESTS = testGreetings testMaths testReading.sh testSingle testKernel testSymmetr testVoronoi1D testVoronoi2D testVoronoi3D testGravityDerivatives \ testPeriodicBC.sh testPeriodicBCPerturbed.sh testPotentialSelf \ testPotentialPair testEOS testUtilities testSelectOutput.sh \ - testCbrt + testCosmology # List of test programs to compile check_PROGRAMS = testGreetings testReading testSingle testTimeIntegration \ @@ -39,7 +39,7 @@ check_PROGRAMS = testGreetings testReading testSingle testTimeIntegration \ testRiemannHLLC testMatrixInversion testDump testLogger \ testVoronoi1D testVoronoi2D testVoronoi3D testPeriodicBC \ testGravityDerivatives testPotentialSelf testPotentialPair testEOS testUtilities \ - testSelectOutput testCbrt + testSelectOutput testCosmology # Rebuild tests when SWIFT is updated. $(check_PROGRAMS): ../src/.libs/libswiftsim.a @@ -53,6 +53,8 @@ testReading_SOURCES = testReading.c testSelectOutput_SOURCES = testSelectOutput.c +testCosmology_SOURCES = testCosmology.c + testSymmetry_SOURCES = testSymmetry.c # Added because of issues using memcmp on clang 4.x diff --git a/tests/testCosmology.c b/tests/testCosmology.c new file mode 100644 index 000000000..c8c233e9b --- /dev/null +++ b/tests/testCosmology.c @@ -0,0 +1,78 @@ +/******************************************************************************* + * 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 . + * + ******************************************************************************/ + +/* Some standard headers. */ +#include + +/* Includes. */ +#include "swift.h" + +#define N_CHECK 10 +#define TOLERANCE 1e-1 + +void test_params_init(struct swift_params *params) { + parser_init("", params); + parser_set_param(params, "Cosmology:Omega_m:0.3075"); + parser_set_param(params, "Cosmology:Omega_lambda:0.6910"); + parser_set_param(params, "Cosmology:Omega_b:0.0486"); + parser_set_param(params, "Cosmology:h:0.6774"); + parser_set_param(params, "Cosmology:a_begin:0.1"); + parser_set_param(params, "Cosmology:a_end:1.0"); +} + +int main(int argc, char *argv[]) { + + message("Initialization..."); + + /* pseudo initialization of params */ + struct swift_params params; + test_params_init(¶ms); + + /* initialization of unit system */ + struct unit_system us; + units_init_cgs(&us); + + /* initialization of phys_const */ + struct phys_const phys_const; + phys_const_init(&us, ¶ms, &phys_const); + + /* initialization of cosmo */ + struct cosmology cosmo; + cosmology_init(¶ms, &us, &phys_const, &cosmo); + + message("Start checking computation..."); + + double a[N_CHECK] = {0.1, 0.2, 0.3, 0.4, 0.5, + 0.6, 0.7, 0.8, 0.9, 1.0}; + + for(int i=0; i < N_CHECK; i++) { + /* Compute a(t(a)) and check if same results */ + double tmp = cosmology_get_time_since_big_bang(&cosmo, a[i]); + tmp = cosmology_get_scale_factor(&cosmo, tmp); + + /* check accuracy */ + tmp = (tmp - a[i]) / a[i]; + message("Accuracy of %f at a=%f", tmp, a[i]); + assert(fabs(tmp) < TOLERANCE); + } + + message("Everything seems fine with cosmology."); + + return 0; +} -- GitLab From 867f606ae2c242224880cd5059e9512c7dd199e6 Mon Sep 17 00:00:00 2001 From: lhausamm Date: Mon, 4 Jun 2018 17:36:02 +0200 Subject: [PATCH 16/32] Set tolerance testCosmology to a reasonable value --- tests/testCosmology.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/testCosmology.c b/tests/testCosmology.c index c8c233e9b..31c7aea64 100644 --- a/tests/testCosmology.c +++ b/tests/testCosmology.c @@ -24,7 +24,7 @@ #include "swift.h" #define N_CHECK 10 -#define TOLERANCE 1e-1 +#define TOLERANCE 1e-3 void test_params_init(struct swift_params *params) { parser_init("", params); -- GitLab From 3a43c49c6abe6501a38be95a0cd9221bceda2c2f Mon Sep 17 00:00:00 2001 From: lhausamm Date: Tue, 5 Jun 2018 07:39:07 +0200 Subject: [PATCH 17/32] Change output testCosmology and add warning message about accuracy in cosmology.c --- src/cosmology.c | 3 +++ tests/testCosmology.c | 12 +++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/cosmology.c b/src/cosmology.c index a443529c7..538b11870 100644 --- a/src/cosmology.c +++ b/src/cosmology.c @@ -679,6 +679,9 @@ double cosmology_get_a_from_z(const struct cosmology *c, double redshift) { /** * @brief Compute scale factor from time since big bang. * + * WARNING: This method has a low accuracy at high redshift. + * The relative error is around 1e-3 (testCosmology.c is measuring it). + * * @param c The current #cosmology. * @param t time since the big bang * @return The scale factor. diff --git a/tests/testCosmology.c b/tests/testCosmology.c index 31c7aea64..f24b920df 100644 --- a/tests/testCosmology.c +++ b/tests/testCosmology.c @@ -23,7 +23,7 @@ /* Includes. */ #include "swift.h" -#define N_CHECK 10 +#define N_CHECK 20 #define TOLERANCE 1e-3 void test_params_init(struct swift_params *params) { @@ -58,17 +58,15 @@ int main(int argc, char *argv[]) { message("Start checking computation..."); - double a[N_CHECK] = {0.1, 0.2, 0.3, 0.4, 0.5, - 0.6, 0.7, 0.8, 0.9, 1.0}; - for(int i=0; i < N_CHECK; i++) { + double a = 0.1 + 0.9 * i / (N_CHECK - 1.); /* Compute a(t(a)) and check if same results */ - double tmp = cosmology_get_time_since_big_bang(&cosmo, a[i]); + double tmp = cosmology_get_time_since_big_bang(&cosmo, a); tmp = cosmology_get_scale_factor(&cosmo, tmp); /* check accuracy */ - tmp = (tmp - a[i]) / a[i]; - message("Accuracy of %f at a=%f", tmp, a[i]); + tmp = (tmp - a) / a; + message("Accuracy of %g at a=%g", tmp, a); assert(fabs(tmp) < TOLERANCE); } -- GitLab From d6ae56e2dede69dd358dd255682127622b00f6fd Mon Sep 17 00:00:00 2001 From: lhausamm Date: Tue, 5 Jun 2018 08:49:19 +0200 Subject: [PATCH 18/32] Remove unused struct declaration --- src/common_io.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/common_io.h b/src/common_io.h index fe69dd0b7..5e02d670c 100644 --- a/src/common_io.h +++ b/src/common_io.h @@ -32,7 +32,6 @@ #define IO_BUFFER_ALIGNMENT 1024 /* Avoid cyclic inclusion problems */ -struct cosmology; struct part; struct gpart; struct spart; -- GitLab From 81d2952d1f0ce27766b813bd523d7b95e47b8e1f Mon Sep 17 00:00:00 2001 From: lhausamm Date: Fri, 8 Jun 2018 11:18:56 +0200 Subject: [PATCH 19/32] Add additional parameter for outputlist --- .../source/GettingStarted/parameter_file.rst | 8 ++--- examples/parameter_example.yml | 6 ++-- src/engine.c | 29 ++++++++++--------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/doc/RTD/source/GettingStarted/parameter_file.rst b/doc/RTD/source/GettingStarted/parameter_file.rst index b4c55b675..1bf4cbd39 100644 --- a/doc/RTD/source/GettingStarted/parameter_file.rst +++ b/doc/RTD/source/GettingStarted/parameter_file.rst @@ -15,11 +15,11 @@ the run time parameters. Output List ~~~~~~~~~~~ -In the sections ``Snapshots`` and ``Statistics``, you can specify the option ``output_list`` which receives a filename. -This file consists in a list of time where you want to output either a snapshot or a statistic. -With the header, you can choose between writing redshifts, scale factors or times. +In the sections ``Snapshots`` and ``Statistics``, you can specify the options ``output_list_on`` and ``output_list`` which receive an int and a filename. +The ``output_list_on`` enable or not the output list and ``output_list`` is the filename containing the output times. +With the file header, you can choose between writing redshifts, scale factors or times. -Example of file containing with times: +Example of file containing with times (in internal units): :: # Time 0.5 diff --git a/examples/parameter_example.yml b/examples/parameter_example.yml index 94e58ee5c..5d03e04c6 100644 --- a/examples/parameter_example.yml +++ b/examples/parameter_example.yml @@ -81,7 +81,8 @@ Snapshots: UnitVelocity_in_cgs: 1 # (Optional) Unit system for the outputs (Centimeters per second) UnitCurrent_in_cgs: 1 # (Optional) Unit system for the outputs (Amperes) UnitTemp_in_cgs: 1 # (Optional) Unit system for the outputs (Kelvin) - output_list: "" # (Optional) File containing the output times (see documentation in "Parameter File" section) + output_list_on: 0 # (Optional) Enable the output list + output_list: snaplist.txt # (Optional) File containing the output times (see documentation in "Parameter File" section) # Parameters governing the conserved quantities statistics Statistics: @@ -90,7 +91,8 @@ Statistics: time_first: 0. # (Optional) Time of the first stats output if non-cosmological time-integration (in internal units) energy_file_name: energy # (Optional) File name for energy output timestep_file_name: timesteps # (Optional) File name for timing information output. Note: No underscores "_" allowed in file name - output_list: "" # (Optional) File containing the output times (see documentation in "Parameter File" section) + output_list_on: 0 # (Optional) Enable the output list + output_list: statlist.txt # (Optional) File containing the output times (see documentation in "Parameter File" section) # Parameters related to the initial conditions InitialConditions: diff --git a/src/engine.c b/src/engine.c index 91332f182..1cb4e0c22 100644 --- a/src/engine.c +++ b/src/engine.c @@ -6750,15 +6750,16 @@ void engine_read_outputlist_files(struct engine *e, const struct swift_params *p cosmo = e->cosmology; /* Deal with snapshots */ - e->outputlist_snapshots = (struct outputlist*) malloc(sizeof(struct outputlist)); - list = e->outputlist_snapshots; - - strcpy(filename, ""); - parser_get_opt_param_string(params, "Snapshots:output_list", - filename, ""); + int outputlist_on = parser_get_opt_param_int(params, "Snapshots:output_list_on", 0); /* Read outputlist for snapshots */ - if (strcmp(filename, "")) { + if (outputlist_on) { + e->outputlist_snapshots = (struct outputlist*) malloc(sizeof(struct outputlist)); + list = e->outputlist_snapshots; + + parser_get_param_string(params, "Snapshots:output_list", + filename); + message("Reading snapshots output file."); outputlist_read_file(list, filename, cosmo); @@ -6777,15 +6778,17 @@ void engine_read_outputlist_files(struct engine *e, const struct swift_params *p } /* Deal with stats */ - e->outputlist_stats = (struct outputlist*) malloc(sizeof(struct outputlist)); - list = e->outputlist_stats; - strcpy(filename, ""); - parser_get_opt_param_string(params, "Statistics:output_list", - filename, ""); + outputlist_on = parser_get_opt_param_int(params, "Statistics:output_list_on", 0); /* Read outputlist for stats */ - if (strcmp(filename, "")) { + if (outputlist_on) { + e->outputlist_stats = (struct outputlist*) malloc(sizeof(struct outputlist)); + list = e->outputlist_stats; + + parser_get_param_string(params, "Statistics:output_list", + filename); + message("Reading statistics output file."); outputlist_read_file(list, filename, cosmo); -- GitLab From f26390762661bebdf87d55693ad63fda16637236 Mon Sep 17 00:00:00 2001 From: loikki Date: Mon, 9 Jul 2018 08:40:25 +0200 Subject: [PATCH 20/32] Fix rebasing error --- src/cosmology.h | 3 --- src/engine.c | 2 +- src/engine.h | 2 +- src/outputlist.c | 2 +- tests/Makefile.am | 4 ++-- 5 files changed, 5 insertions(+), 8 deletions(-) diff --git a/src/cosmology.h b/src/cosmology.h index fdea957c6..40b16543e 100644 --- a/src/cosmology.h +++ b/src/cosmology.h @@ -183,9 +183,6 @@ double cosmology_get_therm_kick_factor(const struct cosmology *cosmo, double cosmology_get_delta_time(const struct cosmology *c, integertime_t ti_start, integertime_t ti_end); -double cosmology_get_delta_time(const struct cosmology *c, double a1, - double a2); - double cosmology_get_a_from_z(const struct cosmology *c, double redshift); diff --git a/src/engine.c b/src/engine.c index 1cb4e0c22..64b40c17c 100644 --- a/src/engine.c +++ b/src/engine.c @@ -6740,7 +6740,7 @@ void engine_read_next_statistics_time(struct engine *e) { * @param e The #engine. * @param params The #swift_params. */ -void engine_read_outputlist_files(struct engine *e, const struct swift_params *params) { +void engine_read_outputlist_files(struct engine *e, struct swift_params *params) { char filename[PARSER_MAX_LINE_SIZE]; struct outputlist *list; diff --git a/src/engine.h b/src/engine.h index 0adb8a104..ebc7499b3 100644 --- a/src/engine.h +++ b/src/engine.h @@ -377,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_outputlist_files(struct engine *e, const struct swift_params *params); +void engine_read_outputlist_files(struct engine *e, 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 index eace03016..b3ab66712 100644 --- a/src/outputlist.c +++ b/src/outputlist.c @@ -73,7 +73,7 @@ void outputlist_read_file(struct outputlist *outputlist, const char* filename, line[strcspn(line, "\n")] = 0; /* Find type of data in file */ - int type; + int type = -1; if (!strcmp(line, "# Redshift")) type = OUTPUTLIST_REDSHIFT; else if (!strcmp(line, "# Time")) diff --git a/tests/Makefile.am b/tests/Makefile.am index 46d512cd4..2744164e2 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -28,7 +28,7 @@ TESTS = testGreetings testMaths testReading.sh testSingle testKernel testSymmetr testVoronoi1D testVoronoi2D testVoronoi3D testGravityDerivatives \ testPeriodicBC.sh testPeriodicBCPerturbed.sh testPotentialSelf \ testPotentialPair testEOS testUtilities testSelectOutput.sh \ - testCosmology + testCbrt testCosmology # List of test programs to compile check_PROGRAMS = testGreetings testReading testSingle testTimeIntegration \ @@ -39,7 +39,7 @@ check_PROGRAMS = testGreetings testReading testSingle testTimeIntegration \ testRiemannHLLC testMatrixInversion testDump testLogger \ testVoronoi1D testVoronoi2D testVoronoi3D testPeriodicBC \ testGravityDerivatives testPotentialSelf testPotentialPair testEOS testUtilities \ - testSelectOutput testCosmology + testSelectOutput testCbrt testCosmology # Rebuild tests when SWIFT is updated. $(check_PROGRAMS): ../src/.libs/libswiftsim.a -- GitLab From ba86355caac70d77d74e08289e6521d98f36e0c2 Mon Sep 17 00:00:00 2001 From: loikki Date: Mon, 9 Jul 2018 17:37:51 +0200 Subject: [PATCH 21/32] Add units information --- src/cosmology.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/cosmology.c b/src/cosmology.c index 538b11870..f460ec208 100644 --- a/src/cosmology.c +++ b/src/cosmology.c @@ -677,7 +677,7 @@ double cosmology_get_a_from_z(const struct cosmology *c, double redshift) { } /** - * @brief Compute scale factor from time since big bang. + * @brief Compute scale factor from time since big bang (in internal units). * * WARNING: This method has a low accuracy at high redshift. * The relative error is around 1e-3 (testCosmology.c is measuring it). -- GitLab From 9c863d85c86b0da4a94e00045a919c37146048ad Mon Sep 17 00:00:00 2001 From: loikki Date: Wed, 11 Jul 2018 10:30:57 +0200 Subject: [PATCH 22/32] Remove cosmology in `cosmology_get_a_from_z` --- src/cosmology.c | 3 +-- src/cosmology.h | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/src/cosmology.c b/src/cosmology.c index f460ec208..bd80c9809 100644 --- a/src/cosmology.c +++ b/src/cosmology.c @@ -669,10 +669,9 @@ double cosmology_get_delta_time(const struct cosmology *c, /** * @brief Compute the scale factor from the redshift. * - * @brief c The current #cosmology. * @brief redshift The redshift to compute. */ -double cosmology_get_a_from_z(const struct cosmology *c, double redshift) { +double cosmology_get_a_from_z(double redshift) { return 1. / (1. + redshift); } diff --git a/src/cosmology.h b/src/cosmology.h index 40b16543e..0b7ddfaa7 100644 --- a/src/cosmology.h +++ b/src/cosmology.h @@ -183,8 +183,7 @@ double cosmology_get_therm_kick_factor(const struct cosmology *cosmo, double cosmology_get_delta_time(const struct cosmology *c, integertime_t ti_start, integertime_t ti_end); -double cosmology_get_a_from_z(const struct cosmology *c, - double redshift); +double cosmology_get_a_from_z(double redshift); double cosmology_get_scale_factor(const struct cosmology *cosmo, double t); -- GitLab From 91ddbf62fdc67c7609c27f991e24ec4c0d91fd85 Mon Sep 17 00:00:00 2001 From: loikki Date: Wed, 11 Jul 2018 11:03:59 +0200 Subject: [PATCH 23/32] Modify `engine_read_outputlist_files` to `engine_init_outputlists` --- src/engine.c | 6 +++--- src/engine.h | 2 +- src/outputlist.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/engine.c b/src/engine.c index 64b40c17c..7c1648b25 100644 --- a/src/engine.c +++ b/src/engine.c @@ -5833,7 +5833,7 @@ void engine_init(struct engine *e, struct space *s, struct swift_params *params, e->ti_current = 0; } - engine_read_outputlist_files(e, params); + engine_init_outputlists(e, params); } /** @@ -6735,12 +6735,12 @@ void engine_read_next_statistics_time(struct engine *e) { } /** - * @brief Read all the outputlist files + * @brief Initialize all the outputlist required by the engine * * @param e The #engine. * @param params The #swift_params. */ -void engine_read_outputlist_files(struct engine *e, struct swift_params *params) { +void engine_init_outputlists(struct engine *e, struct swift_params *params) { char filename[PARSER_MAX_LINE_SIZE]; struct outputlist *list; diff --git a/src/engine.h b/src/engine.h index ebc7499b3..3a917882b 100644 --- a/src/engine.h +++ b/src/engine.h @@ -377,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_outputlist_files(struct engine *e, struct swift_params *params); +void engine_init_outputlists(struct engine *e, 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 index b3ab66712..95a031ab8 100644 --- a/src/outputlist.c +++ b/src/outputlist.c @@ -104,7 +104,7 @@ void outputlist_read_file(struct outputlist *outputlist, const char* filename, /* Transform input into correct time (e.g. ages or scale factor) */ if (type == OUTPUTLIST_REDSHIFT) - *time = cosmology_get_a_from_z(cosmo, *time); + *time = cosmology_get_a_from_z(*time); if (cosmo && type == OUTPUTLIST_AGE) *time = cosmology_get_scale_factor(cosmo, *time); -- GitLab From 0155022febe76b393154c7024d6264172f435de2 Mon Sep 17 00:00:00 2001 From: Matthieu Schaller Date: Mon, 6 Aug 2018 20:56:45 +0200 Subject: [PATCH 24/32] Post-merge fixes. --- src/cosmology.c | 24 ++++----- src/cosmology.h | 3 -- src/engine.c | 120 ++++++++++++++++++++++++++++++++---------- src/outputlist.c | 58 ++++++++++---------- src/outputlist.h | 11 ++-- tests/testCosmology.c | 6 +-- 6 files changed, 140 insertions(+), 82 deletions(-) diff --git a/src/cosmology.c b/src/cosmology.c index bd80c9809..40e86b3f7 100644 --- a/src/cosmology.c +++ b/src/cosmology.c @@ -309,7 +309,7 @@ void cosmology_init_tables(struct cosmology *c) { c->time_interp_table = (double *)malloc(cosmology_table_length * sizeof(double)); c->scale_factor_interp_table = - (double *)malloc(cosmology_table_length * sizeof(double)); + (double *)malloc(cosmology_table_length * sizeof(double)); /* Prepare a table of scale factors for the integral bounds */ const double delta_a = @@ -376,7 +376,8 @@ void cosmology_init_tables(struct cosmology *c) { /* Inverse t(a) */ const double time_init = c->time_interp_table_offset; - const double delta_t = (c->universe_age_at_present_day - time_init) / cosmology_table_length; + const double delta_t = + (c->universe_age_at_present_day - time_init) / cosmology_table_length; int i_prev = 0; for (int i = 0; i < cosmology_table_length; i++) { @@ -384,7 +385,8 @@ void cosmology_init_tables(struct cosmology *c) { double time_interp = delta_t * i; /* Find next time in time_interp_table */ - while (i_prev < cosmology_table_length && c->time_interp_table[i_prev] <= time_interp) { + while (i_prev < cosmology_table_length && + c->time_interp_table[i_prev] <= time_interp) { i_prev++; } @@ -394,7 +396,9 @@ void cosmology_init_tables(struct cosmology *c) { scale += i_prev; /* Compute interpolated scale factor */ - double log_a = c->log_a_begin + scale * (c->log_a_end - c->log_a_begin) / cosmology_table_length; + double log_a = + c->log_a_begin + + scale * (c->log_a_end - c->log_a_begin) / cosmology_table_length; c->scale_factor_interp_table[i] = exp(log_a) - c->a_begin; } @@ -665,16 +669,6 @@ double cosmology_get_delta_time(const struct cosmology *c, return t2 - t1; } - -/** - * @brief Compute the scale factor from the redshift. - * - * @brief redshift The redshift to compute. - */ -double cosmology_get_a_from_z(double redshift) { - return 1. / (1. + redshift); -} - /** * @brief Compute scale factor from time since big bang (in internal units). * @@ -689,7 +683,7 @@ double cosmology_get_scale_factor(const struct cosmology *c, double t) { /* scale factor between time_begin and t */ const double a = interp_table(c->scale_factor_interp_table, t, c->time_interp_table_offset, - c->universe_age_at_present_day); + c->universe_age_at_present_day); return a + c->a_begin; } diff --git a/src/cosmology.h b/src/cosmology.h index 0b7ddfaa7..7136b6566 100644 --- a/src/cosmology.h +++ b/src/cosmology.h @@ -183,8 +183,6 @@ double cosmology_get_therm_kick_factor(const struct cosmology *cosmo, double cosmology_get_delta_time(const struct cosmology *c, integertime_t ti_start, integertime_t ti_end); -double cosmology_get_a_from_z(double redshift); - double cosmology_get_scale_factor(const struct cosmology *cosmo, double t); double cosmology_get_time_since_big_bang(const struct cosmology *c, double a); @@ -196,7 +194,6 @@ void cosmology_init_no_cosmo(struct cosmology *c); void cosmology_print(const struct cosmology *c); void cosmology_clean(struct cosmology *c); - #ifdef HAVE_HDF5 void cosmology_write_model(hid_t h_grp, const struct cosmology *c); #endif diff --git a/src/engine.c b/src/engine.c index 7c1648b25..ea3426f48 100644 --- a/src/engine.c +++ b/src/engine.c @@ -6569,6 +6569,12 @@ void engine_compute_next_statistics_time(struct engine *e) { return; } + /* Do outputlist file case */ + if (e->outputlist_stats) { + engine_read_next_statistics_time(e); + return; + } + /* Find upper-bound on last output */ double time_end; if (e->policy & engine_policy_cosmology) @@ -6677,6 +6683,66 @@ void engine_compute_next_stf_time(struct engine *e) { } } +/** + * @brief Read the next time (on the time line) for a dump + * + * @param e The #engine. + */ +void engine_read_next_snapshot_time(struct engine *e) { + + int is_cosmo = e->policy & engine_policy_cosmology; + const struct outputlist *t = e->outputlist_snapshots; + + /* Find upper-bound on last output */ + double time_end; + if (is_cosmo) + time_end = e->cosmology->a_end; + else + time_end = e->time_end; + + /* Find next snasphot above current time */ + double time; + time = t->times[0]; + size_t ind = 0; + while (time < time_end) { + + /* Output time on the integer timeline */ + if (is_cosmo) + e->ti_next_snapshot = log(time / e->cosmology->a_begin) / e->time_base; + else + e->ti_next_snapshot = (time - e->time_begin) / e->time_base; + + /* Found it? */ + if (e->ti_next_snapshot > e->ti_current) break; + + ind += 1; + if (ind == t->size) break; + + time = t->times[ind]; + } + + /* Deal with last snapshot */ + if (e->ti_next_snapshot >= max_nr_timesteps || ind == t->size || + time >= time_end) { + e->ti_next_snapshot = -1; + if (e->verbose) message("No further output time."); + } else { + + /* Be nice, talk... */ + if (is_cosmo) { + const double next_snapshot_time = + exp(e->ti_next_snapshot * e->time_base) * e->cosmology->a_begin; + if (e->verbose) + message("Next snapshot time set to a=%e.", next_snapshot_time); + } else { + const double next_snapshot_time = + e->ti_next_snapshot * e->time_base + e->time_begin; + if (e->verbose) + message("Next snapshot time set to t=%e.", next_snapshot_time); + } + } +} + /** * @brief Read the next time (on the time line) for a statistics dump * @@ -6708,16 +6774,19 @@ void engine_read_next_statistics_time(struct engine *e) { if (e->ti_next_stats > e->ti_current) break; ind += 1; - if (ind == t->size) - break; - + if (ind == t->size) break; + time = t->times[ind]; } /* Deal with last statistics */ - if (e->ti_next_stats >= max_nr_timesteps || - ind == t->size || time >= time_end) { + if (e->ti_next_stats >= max_nr_timesteps || ind == t->size || + time >= time_end) { e->ti_next_stats = -1; + if (e->verbose) message("No further output time."); + } else { + + /* Be nice, talk... */ if (is_cosmo) { const double next_statistics_time = exp(e->ti_next_stats * e->time_base) * e->cosmology->a_begin; @@ -6735,7 +6804,7 @@ void engine_read_next_statistics_time(struct engine *e) { } /** - * @brief Initialize all the outputlist required by the engine + * @brief Initialize all the outputlist required by the engine * * @param e The #engine. * @param params The #swift_params. @@ -6746,20 +6815,20 @@ void engine_init_outputlists(struct engine *e, struct swift_params *params) { /* get cosmo */ struct cosmology *cosmo = NULL; - if (e->policy & engine_policy_cosmology) - cosmo = e->cosmology; - + if (e->policy & engine_policy_cosmology) cosmo = e->cosmology; + /* Deal with snapshots */ - int outputlist_on = parser_get_opt_param_int(params, "Snapshots:output_list_on", 0); + int outputlist_on = + parser_get_opt_param_int(params, "Snapshots:output_list_on", 0); /* Read outputlist for snapshots */ if (outputlist_on) { - e->outputlist_snapshots = (struct outputlist*) malloc(sizeof(struct outputlist)); + e->outputlist_snapshots = + (struct outputlist *)malloc(sizeof(struct outputlist)); list = e->outputlist_snapshots; - parser_get_param_string(params, "Snapshots:output_list", - filename); - + parser_get_param_string(params, "Snapshots:output_list", filename); + message("Reading snapshots output file."); outputlist_read_file(list, filename, cosmo); @@ -6770,25 +6839,24 @@ void engine_init_outputlists(struct engine *e, struct swift_params *params) { if (cosmo) { e->delta_time_snapshot = list->times[1] / list->times[0]; e->a_first_snapshot = list->times[0]; - } - else { + } else { e->delta_time_snapshot = list->times[1] - list->times[0]; e->time_first_snapshot = list->times[0]; } } - /* Deal with stats */ - - outputlist_on = parser_get_opt_param_int(params, "Statistics:output_list_on", 0); + /* Deal with stats */ + outputlist_on = + parser_get_opt_param_int(params, "Statistics:output_list_on", 0); /* Read outputlist for stats */ if (outputlist_on) { - e->outputlist_stats = (struct outputlist*) malloc(sizeof(struct outputlist)); + e->outputlist_stats = + (struct outputlist *)malloc(sizeof(struct outputlist)); list = e->outputlist_stats; - parser_get_param_string(params, "Statistics:output_list", - filename); - + parser_get_param_string(params, "Statistics:output_list", filename); + message("Reading statistics output file."); outputlist_read_file(list, filename, cosmo); @@ -6799,8 +6867,7 @@ void engine_init_outputlists(struct engine *e, struct swift_params *params) { if (cosmo) { e->delta_time_statistics = list->times[1] / list->times[0]; e->a_first_statistics = list->times[0]; - } - else { + } else { e->delta_time_statistics = list->times[1] - list->times[0]; e->time_first_statistics = list->times[0]; } @@ -6992,8 +7059,7 @@ void engine_struct_dump(struct engine *e, FILE *stream) { parser_struct_dump(e->parameter_file, stream); if (e->outputlist_snapshots) outputlist_struct_dump(e->outputlist_snapshots, stream); - if (e->outputlist_stats) - outputlist_struct_dump(e->outputlist_stats, stream); + if (e->outputlist_stats) outputlist_struct_dump(e->outputlist_stats, stream); } /** diff --git a/src/outputlist.c b/src/outputlist.c index 95a031ab8..37ebbcb5e 100644 --- a/src/outputlist.c +++ b/src/outputlist.c @@ -1,7 +1,6 @@ /******************************************************************************* * This file is part of SWIFT. - * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk), - * Matthieu Schaller (matthieu.schaller@durham.ac.uk). + * Copyright (c) 2018 Loic Hausamman (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 @@ -39,11 +38,11 @@ * @param filename The file to read. * @param cosmo The #cosmology model. */ -void outputlist_read_file(struct outputlist *outputlist, const char* filename, - struct cosmology *cosmo) { +void outputlist_read_file(struct outputlist *outputlist, const char *filename, + struct cosmology *cosmo) { /* Open file */ - FILE* file = fopen(filename, "r"); + FILE *file = fopen(filename, "r"); if (file == NULL) error("Error opening file '%s'", filename); /* Declare reading variables */ @@ -60,13 +59,15 @@ void outputlist_read_file(struct outputlist *outputlist, const char* filename, /* Return to start of file and initialize time array */ fseek(file, 0, SEEK_SET); - outputlist->times = (double *) malloc(sizeof(double) * nber_line); + 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); + 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) + if ((read = getline(&line, &len, file)) == -1) error("Unable to read header in file '%s'", filename); /* Remove end of line character */ @@ -81,14 +82,14 @@ void outputlist_read_file(struct outputlist *outputlist, const char* filename, else if (!strcmp(line, "# Scale Factor")) type = OUTPUTLIST_SCALE_FACTOR; else - error("Unable to interpret the header (%s) in file '%s'", - line, filename); + 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); - + 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; @@ -97,14 +98,13 @@ void outputlist_read_file(struct outputlist *outputlist, const char* filename, /* 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); + "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(*time); + if (type == OUTPUTLIST_REDSHIFT) *time = 1. / (1. + *time); if (cosmo && type == OUTPUTLIST_AGE) *time = cosmology_get_scale_factor(cosmo, *time); @@ -123,7 +123,7 @@ 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++) { + for (size_t ind = 0; ind < outputlist->size; ind++) { printf("\t%lf\n", outputlist->times[ind]); } } @@ -139,19 +139,21 @@ void outputlist_clean(struct outputlist *outputlist) { * @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, sizeof(struct outputlist), 1, stream, "outputlist", + "outputlist struct"); - restart_write_blocks(list->times, list->size, sizeof(double), stream, "outputlist", "times"); + 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"); +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 index 829e6e4a7..9067f5841 100644 --- a/src/outputlist.h +++ b/src/outputlist.h @@ -1,7 +1,6 @@ /******************************************************************************* * This file is part of SWIFT. - * Copyright (c) 2012 Pedro Gonnet (pedro.gonnet@durham.ac.uk), - * Matthieu Schaller (matthieu.schaller@durham.ac.uk). + * Copyright (c) 2018 Loic Hausamman (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 @@ -37,11 +36,11 @@ struct outputlist { size_t size; }; - -void outputlist_read_file(struct outputlist *outputlist, const char* filename, struct cosmology *cosmo); +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); +void outputlist_struct_restore(struct outputlist *list, FILE *stream); -#endif // SWIFT_OUTPUTLIST_H +#endif /* SWIFT_OUTPUTLIST_H */ diff --git a/tests/testCosmology.c b/tests/testCosmology.c index f24b920df..698351ad9 100644 --- a/tests/testCosmology.c +++ b/tests/testCosmology.c @@ -1,6 +1,6 @@ /******************************************************************************* * This file is part of SWIFT. - * Copyright (C) 2015 Matthieu Schaller (matthieu.schaller@durham.ac.uk). + * Copyright (c) 2018 Loic Hausamman (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 @@ -48,7 +48,7 @@ int main(int argc, char *argv[]) { struct unit_system us; units_init_cgs(&us); - /* initialization of phys_const */ + /* initialization of phys_const */ struct phys_const phys_const; phys_const_init(&us, ¶ms, &phys_const); @@ -58,7 +58,7 @@ int main(int argc, char *argv[]) { message("Start checking computation..."); - for(int i=0; i < N_CHECK; i++) { + for (int i = 0; i < N_CHECK; i++) { double a = 0.1 + 0.9 * i / (N_CHECK - 1.); /* Compute a(t(a)) and check if same results */ double tmp = cosmology_get_time_since_big_bang(&cosmo, a); -- GitLab From f1c30026191b9685378f8ce6f53c5ce283321c91 Mon Sep 17 00:00:00 2001 From: loikki Date: Tue, 7 Aug 2018 10:03:56 +0200 Subject: [PATCH 25/32] Moved around code and made more generic functions --- src/common_io.h | 11 -- src/engine.c | 265 ++++------------------------------------------- src/outputlist.c | 107 +++++++++++++++++++ src/outputlist.h | 5 + 4 files changed, 132 insertions(+), 256 deletions(-) diff --git a/src/common_io.h b/src/common_io.h index 5e02d670c..152b40a8d 100644 --- a/src/common_io.h +++ b/src/common_io.h @@ -62,17 +62,6 @@ enum IO_DATA_TYPE { */ enum IO_STF_OUTPUT_FORMAT { STEPS = 0, TIME }; -enum TIME_ARRAY_TYPE { - TIME_ARRAY_AGE, - TIME_ARRAY_REDSHIFT, - TIME_ARRAY_SCALE_FACTOR, -}; - -struct time_array { - double times[TIME_ARRAY_MAX_SIZE]; - size_t size; -}; - #if defined(HAVE_HDF5) hid_t io_hdf5_type(enum IO_DATA_TYPE type); diff --git a/src/engine.c b/src/engine.c index ea3426f48..f3ea1e566 100644 --- a/src/engine.c +++ b/src/engine.c @@ -6442,7 +6442,8 @@ void engine_print_policy(struct engine *e) { void engine_compute_next_snapshot_time(struct engine *e) { /* Do outputlist file case */ if (e->outputlist_snapshots) { - engine_read_next_snapshot_time(e); + outputlist_read_next_time(e->outputlist_snapshots, e, + "snapshots", &e->ti_next_snapshot); return; } @@ -6497,66 +6498,6 @@ void engine_compute_next_snapshot_time(struct engine *e) { } } -/** - * @brief Read the next time (on the time line) for a dump - * - * @param e The #engine. - */ -void engine_read_next_snapshot_time(struct engine *e) { - int is_cosmo = e->policy & engine_policy_cosmology; - const struct outputlist *t = e->outputlist_snapshots; - - /* Find upper-bound on last output */ - double time_end; - if (is_cosmo) - time_end = e->cosmology->a_end; - else - time_end = e->time_end; - - /* Find next snasphot above current time */ - double time; - time = t->times[0]; - size_t ind = 0; - while (time < time_end) { - - /* Output time on the integer timeline */ - if (is_cosmo) - e->ti_next_snapshot = log(time / e->cosmology->a_begin) / e->time_base; - else - e->ti_next_snapshot = (time - e->time_begin) / e->time_base; - - /* Found it? */ - if (e->ti_next_snapshot > e->ti_current) break; - - ind += 1; - if (ind == t->size) - break; - - time = t->times[ind]; - } - - /* Deal with last snapshot */ - if (e->ti_next_snapshot >= max_nr_timesteps || - ind == t->size || time >= time_end) { - e->ti_next_snapshot = -1; - if (e->verbose) message("No further output time."); - } else { - - /* Be nice, talk... */ - if (is_cosmo) { - const double next_snapshot_time = - exp(e->ti_next_snapshot * e->time_base) * e->cosmology->a_begin; - if (e->verbose) - message("Next snapshot time set to a=%e.", next_snapshot_time); - } else { - const double next_snapshot_time = - e->ti_next_snapshot * e->time_base + e->time_begin; - if (e->verbose) - message("Next snapshot time set to t=%e.", next_snapshot_time); - } - } - -} /** * @brief Computes the next time (on the time line) for a statistics dump @@ -6564,14 +6505,10 @@ void engine_read_next_snapshot_time(struct engine *e) { * @param e The #engine. */ void engine_compute_next_statistics_time(struct engine *e) { - if (e->outputlist_stats) { - engine_read_next_statistics_time(e); - return; - } - /* Do outputlist file case */ if (e->outputlist_stats) { - engine_read_next_statistics_time(e); + outputlist_read_next_time(e->outputlist_stats, e, + "stats", &e->ti_next_stats); return; } @@ -6683,126 +6620,6 @@ void engine_compute_next_stf_time(struct engine *e) { } } -/** - * @brief Read the next time (on the time line) for a dump - * - * @param e The #engine. - */ -void engine_read_next_snapshot_time(struct engine *e) { - - int is_cosmo = e->policy & engine_policy_cosmology; - const struct outputlist *t = e->outputlist_snapshots; - - /* Find upper-bound on last output */ - double time_end; - if (is_cosmo) - time_end = e->cosmology->a_end; - else - time_end = e->time_end; - - /* Find next snasphot above current time */ - double time; - time = t->times[0]; - size_t ind = 0; - while (time < time_end) { - - /* Output time on the integer timeline */ - if (is_cosmo) - e->ti_next_snapshot = log(time / e->cosmology->a_begin) / e->time_base; - else - e->ti_next_snapshot = (time - e->time_begin) / e->time_base; - - /* Found it? */ - if (e->ti_next_snapshot > e->ti_current) break; - - ind += 1; - if (ind == t->size) break; - - time = t->times[ind]; - } - - /* Deal with last snapshot */ - if (e->ti_next_snapshot >= max_nr_timesteps || ind == t->size || - time >= time_end) { - e->ti_next_snapshot = -1; - if (e->verbose) message("No further output time."); - } else { - - /* Be nice, talk... */ - if (is_cosmo) { - const double next_snapshot_time = - exp(e->ti_next_snapshot * e->time_base) * e->cosmology->a_begin; - if (e->verbose) - message("Next snapshot time set to a=%e.", next_snapshot_time); - } else { - const double next_snapshot_time = - e->ti_next_snapshot * e->time_base + e->time_begin; - if (e->verbose) - message("Next snapshot time set to t=%e.", next_snapshot_time); - } - } -} - -/** - * @brief Read the next time (on the time line) for a statistics dump - * - * @param e The #engine. - */ -void engine_read_next_statistics_time(struct engine *e) { - int is_cosmo = e->policy & engine_policy_cosmology; - const struct outputlist *t = e->outputlist_stats; - - /* Find upper-bound on last output */ - double time_end; - if (is_cosmo) - time_end = e->cosmology->a_end; - else - time_end = e->time_end; - - /* Find next snasphot above current time */ - double time = t->times[0]; - size_t ind = 0; - while (time < time_end) { - - /* Output time on the integer timeline */ - if (is_cosmo) - e->ti_next_stats = log(time / e->cosmology->a_begin) / e->time_base; - else - e->ti_next_stats = (time - e->time_begin) / e->time_base; - - /* Found it? */ - if (e->ti_next_stats > e->ti_current) break; - - ind += 1; - if (ind == t->size) break; - - time = t->times[ind]; - } - - /* Deal with last statistics */ - if (e->ti_next_stats >= max_nr_timesteps || ind == t->size || - time >= time_end) { - e->ti_next_stats = -1; - if (e->verbose) message("No further output time."); - } else { - - /* Be nice, talk... */ - if (is_cosmo) { - const double next_statistics_time = - exp(e->ti_next_stats * e->time_base) * e->cosmology->a_begin; - if (e->verbose) - message("Next output time for stats set to a=%e.", - next_statistics_time); - } else { - const double next_statistics_time = - 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); - } - } -} - /** * @brief Initialize all the outputlist required by the engine * @@ -6810,68 +6627,26 @@ void engine_read_next_statistics_time(struct engine *e) { * @param params The #swift_params. */ void engine_init_outputlists(struct engine *e, struct swift_params *params) { - char filename[PARSER_MAX_LINE_SIZE]; - struct outputlist *list; - - /* get cosmo */ - struct cosmology *cosmo = NULL; - if (e->policy & engine_policy_cosmology) cosmo = e->cosmology; - /* Deal with snapshots */ - int outputlist_on = - parser_get_opt_param_int(params, "Snapshots:output_list_on", 0); - - /* Read outputlist for snapshots */ - if (outputlist_on) { - e->outputlist_snapshots = - (struct outputlist *)malloc(sizeof(struct outputlist)); - list = e->outputlist_snapshots; - - parser_get_param_string(params, "Snapshots:output_list", filename); - - 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); - - /* Set data for later checks */ - if (cosmo) { - e->delta_time_snapshot = list->times[1] / list->times[0]; - e->a_first_snapshot = list->times[0]; - } else { - e->delta_time_snapshot = list->times[1] - list->times[0]; - e->time_first_snapshot = list->times[0]; - } - } + double snaps_time_first; + outputlist_init(&e->outputlist_snapshots, e, "Snapshots", &e->delta_time_snapshot, + &snaps_time_first); + + if (e->policy & engine_policy_cosmology) + e->a_first_snapshot = snaps_time_first; + else + e->time_first_snapshot = snaps_time_first; /* Deal with stats */ - outputlist_on = - parser_get_opt_param_int(params, "Statistics:output_list_on", 0); - - /* Read outputlist for stats */ - if (outputlist_on) { - e->outputlist_stats = - (struct outputlist *)malloc(sizeof(struct outputlist)); - list = e->outputlist_stats; + double stats_time_first; + outputlist_init(&e->outputlist_stats, e, "Statistics", &e->delta_time_statistics, + &stats_time_first); - parser_get_param_string(params, "Statistics:output_list", filename); - - 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); - - /* Set data for later checks */ - if (cosmo) { - e->delta_time_statistics = list->times[1] / list->times[0]; - e->a_first_statistics = list->times[0]; - } else { - e->delta_time_statistics = list->times[1] - list->times[0]; - e->time_first_statistics = list->times[0]; - } - } + if (e->policy & engine_policy_cosmology) + e->a_first_statistics = stats_time_first; + else + e->time_first_statistics = stats_time_first; + } /** diff --git a/src/outputlist.c b/src/outputlist.c index 37ebbcb5e..06a93eab9 100644 --- a/src/outputlist.c +++ b/src/outputlist.c @@ -24,6 +24,7 @@ #include "outputlist.h" /* Local includes. */ +#include "engine.h" #include "cosmology.h" #include "error.h" #include "restart.h" @@ -116,6 +117,112 @@ void outputlist_read_file(struct outputlist *outputlist, const char *filename, fclose(file); } + +/** + * @brief Read the next time for an output + * + * @param t The #outputlist + * @param e The #engine. + * @param name The name of the output (e.g. 'stats', 'snapshots', 'stf') + * @param ti_next updated to the next output time + */ +void outputlist_read_next_time(const struct outputlist *t, const struct engine *e, const char* name, integertime_t *ti_next) { + int is_cosmo = e->policy & engine_policy_cosmology; + + /* Find upper-bound on last output */ + double time_end; + if (is_cosmo) + time_end = e->cosmology->a_end; + else + time_end = e->time_end; + + /* Find next snasphot above current time */ + double time = t->times[0]; + size_t ind = 0; + while (time < time_end) { + + /* Output time on the integer timeline */ + if (is_cosmo) + *ti_next = log(time / e->cosmology->a_begin) / e->time_base; + else + *ti_next = (time - e->time_begin) / e->time_base; + + /* Found it? */ + if (*ti_next > e->ti_current) break; + + ind += 1; + if (ind == t->size) break; + + time = t->times[ind]; + } + + /* Deal with last statistics */ + if (*ti_next >= max_nr_timesteps || ind == t->size || + time >= time_end) { + *ti_next = -1; + if (e->verbose) message("No further output time for %s.", name); + } else { + + /* Be nice, talk... */ + if (is_cosmo) { + const double next_time = + exp(*ti_next * e->time_base) * e->cosmology->a_begin; + if (e->verbose) + message("Next output time for %s set to a=%e.", + name, next_time); + } else { + const double next_time = + *ti_next * e->time_base + e->time_begin; + if (e->verbose) + message("Next output time for %s set to t=%e.", + name, next_time); + } + } +} + + +void outputlist_init(struct outputlist **list, const struct engine *e, char* name, + double *delta_time, double *time_first) { + struct swift_params *params = e->parameter_file; + + /* get cosmo */ + struct cosmology *cosmo = NULL; + if (e->policy & engine_policy_cosmology) cosmo = e->cosmology; + + /* Read output on/off */ + char param_name[PARSER_MAX_LINE_SIZE]; + sprintf(param_name, "%s:outputlist_on", name); + int outputlist_on = + parser_get_opt_param_int(params, param_name, 0); + + /* Read outputlist for snapshots */ + if (outputlist_on) { + *list = (struct outputlist *)malloc(sizeof(struct outputlist)); + + /* Read filename */ + char filename[PARSER_MAX_LINE_SIZE]; + sprintf(param_name, "%s:output_list", name); + parser_get_param_string(params, param_name, filename); + + message("Reading %s output file.", name); + outputlist_read_file(*list, filename, cosmo); + + if ((*list)->size < 2) + error("You need to provide more snapshots in '%s'", filename); + + /* Set data for later checks */ + if (cosmo) { + *delta_time = (*list)->times[1] / (*list)->times[0]; + *time_first = (*list)->times[0]; + } else { + *delta_time = (*list)->times[1] - (*list)->times[0]; + *time_first = (*list)->times[0]; + } + } + + +} + /** * @brief Print an #outputlist */ diff --git a/src/outputlist.h b/src/outputlist.h index 9067f5841..f4a5a51d7 100644 --- a/src/outputlist.h +++ b/src/outputlist.h @@ -25,6 +25,8 @@ /* Local includes */ #include "cosmology.h" +struct engine; + enum OUTPUTLIST_TYPE { OUTPUTLIST_AGE, OUTPUTLIST_REDSHIFT, @@ -38,6 +40,9 @@ struct outputlist { void outputlist_read_file(struct outputlist *outputlist, const char *filename, struct cosmology *cosmo); +void outputlist_read_next_time(const struct outputlist *t, const struct engine *e, const char* name, integertime_t *ti_next); +void outputlist_init(struct outputlist **list, const struct engine *e, + char* name, double *delta_time, double *time_first); void outputlist_print(const struct outputlist *outputlist); void outputlist_clean(struct outputlist *outputlist); void outputlist_struct_dump(struct outputlist *list, FILE *stream); -- GitLab From fb966c2a007fa459b43636a608a4ee7fad135954 Mon Sep 17 00:00:00 2001 From: loikki Date: Tue, 7 Aug 2018 10:32:48 +0200 Subject: [PATCH 26/32] Speedup a little bit the outputlist --- src/outputlist.c | 17 ++++++++++++----- src/outputlist.h | 18 ++++++++++++++++-- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/src/outputlist.c b/src/outputlist.c index 06a93eab9..86e8aa988 100644 --- a/src/outputlist.c +++ b/src/outputlist.c @@ -63,7 +63,7 @@ void outputlist_read_file(struct outputlist *outputlist, const char *filename, outputlist->times = (double *)malloc(sizeof(double) * nber_line); if (!outputlist->times) error( - "Unable to malloc outputlist time array. " + "Unable to malloc outputlist. " "Try reducing the number of lines in %s", filename); @@ -114,6 +114,9 @@ void outputlist_read_file(struct outputlist *outputlist, const char *filename, ind += 1; } + /* set current indice to 0 */ + outputlist->cur_ind = 0; + fclose(file); } @@ -126,7 +129,7 @@ void outputlist_read_file(struct outputlist *outputlist, const char *filename, * @param name The name of the output (e.g. 'stats', 'snapshots', 'stf') * @param ti_next updated to the next output time */ -void outputlist_read_next_time(const struct outputlist *t, const struct engine *e, const char* name, integertime_t *ti_next) { +void outputlist_read_next_time(struct outputlist *t, const struct engine *e, const char* name, integertime_t *ti_next) { int is_cosmo = e->policy & engine_policy_cosmology; /* Find upper-bound on last output */ @@ -137,8 +140,8 @@ void outputlist_read_next_time(const struct outputlist *t, const struct engine * time_end = e->time_end; /* Find next snasphot above current time */ - double time = t->times[0]; - size_t ind = 0; + double time = t->times[t->cur_ind]; + size_t ind = t->cur_ind; while (time < time_end) { /* Output time on the integer timeline */ @@ -154,6 +157,7 @@ void outputlist_read_next_time(const struct outputlist *t, const struct engine * if (ind == t->size) break; time = t->times[ind]; + t->cur_ind = ind; } /* Deal with last statistics */ @@ -231,7 +235,10 @@ 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]); + if (ind == outputlist->cur_ind) + printf("\t%lf <-- Current\n", outputlist->times[ind]); + else + printf("\t%lf\n", outputlist->times[ind]); } } diff --git a/src/outputlist.h b/src/outputlist.h index f4a5a51d7..2d9528736 100644 --- a/src/outputlist.h +++ b/src/outputlist.h @@ -27,20 +27,34 @@ struct engine; -enum OUTPUTLIST_TYPE { +/** + * @brief the different outputlist type + */ +enum output_list_type { OUTPUTLIST_AGE, OUTPUTLIST_REDSHIFT, OUTPUTLIST_SCALE_FACTOR, }; + +/** + * @brief the array containing the output times + */ struct outputlist { + + /* Time array */ double *times; + + /* Size of the time array */ size_t size; + + /* Current index */ + size_t cur_ind; }; void outputlist_read_file(struct outputlist *outputlist, const char *filename, struct cosmology *cosmo); -void outputlist_read_next_time(const struct outputlist *t, const struct engine *e, const char* name, integertime_t *ti_next); +void outputlist_read_next_time(struct outputlist *t, const struct engine *e, const char* name, integertime_t *ti_next); void outputlist_init(struct outputlist **list, const struct engine *e, char* name, double *delta_time, double *time_first); void outputlist_print(const struct outputlist *outputlist); -- GitLab From f2a8a4b7d0d40cb6b774d477bb300110e510496d Mon Sep 17 00:00:00 2001 From: loikki Date: Tue, 7 Aug 2018 11:18:26 +0200 Subject: [PATCH 27/32] Add outputlist for stf --- examples/parameter_example.yml | 11 +++++++++++ src/engine.c | 32 +++++++++++++++++++++++++++++++- src/engine.h | 9 ++++++++- src/outputlist.c | 13 ++++++++++--- 4 files changed, 60 insertions(+), 5 deletions(-) diff --git a/examples/parameter_example.yml b/examples/parameter_example.yml index 5d03e04c6..ddb71c594 100644 --- a/examples/parameter_example.yml +++ b/examples/parameter_example.yml @@ -224,3 +224,14 @@ EAGLEChemistry: CalciumOverSilicon: 0.0941736 # Constant ratio of Calcium over Silicon abundance SulphurOverSilicon: 0.6054160 # Constant ratio of Sulphur over Silicon abundance +# Structure finding options (requires velociraptor) +StructureFinding: + config_file_name: stf_input.cfg # Name of the STF config file. + basename: ./stf # Common part of the name of output files. + output_time_format: 0 # Specifies the frequency format of structure finding. 0 for simulation steps (delta_step) and 1 for simulation time intervals (delta_time). + scale_factor_first: 0.92 # Scale-factor of the first snaphot (cosmological run) + time_first: 0.01 # Time of the first structure finding output (in internal units). + delta_step: 1000 # Time difference between consecutive structure finding outputs (in internal units) in simulation steps. + delta_time: 1.10 # Time difference between consecutive structure finding outputs (in internal units) in simulation time intervals. + output_list_on: 0 # (Optional) Enable the output list + output_list: stflist.txt # (Optional) File containing the output times (see documentation in "Parameter File" section) diff --git a/src/engine.c b/src/engine.c index f3ea1e566..184abee38 100644 --- a/src/engine.c +++ b/src/engine.c @@ -5768,6 +5768,7 @@ void engine_init(struct engine *e, struct space *s, struct swift_params *params, parser_get_param_double(params, "Snapshots:delta_time"); e->outputlist_snapshots = NULL; e->outputlist_stats = NULL; + e->outputlist_stf = NULL; e->ti_next_snapshot = 0; parser_get_param_string(params, "Snapshots:basename", e->snapshot_base_name); e->snapshot_compression = @@ -6571,6 +6572,12 @@ void engine_compute_next_statistics_time(struct engine *e) { * @param e The #engine. */ void engine_compute_next_stf_time(struct engine *e) { + /* Do outputlist file case */ + if (e->outputlist_stf) { + outputlist_read_next_time(e->outputlist_stf, e, + "stf", &e->ti_nextSTF); + return; + } /* Find upper-bound on last output */ double time_end; @@ -6646,7 +6653,17 @@ void engine_init_outputlists(struct engine *e, struct swift_params *params) { e->a_first_statistics = stats_time_first; else e->time_first_statistics = stats_time_first; - + + /* Deal with stf */ + double stf_time_first; + outputlist_init(&e->outputlist_stf, e, "StructureFinding", &e->deltaTimeSTF, + &stf_time_first); + + if (e->policy & engine_policy_cosmology) + e->a_first_stf = stf_time_first; + else + e->timeFirstSTFOutput = stats_time_first; + } /** @@ -6790,6 +6807,10 @@ void engine_clean(struct engine *e) { outputlist_clean(e->outputlist_stats); free(e->outputlist_stats); } + if (e->outputlist_stf) { + outputlist_clean(e->outputlist_stf); + free(e->outputlist_stf); + } free(e->links); free(e->cell_loc); scheduler_clean(&e->sched); @@ -6835,6 +6856,8 @@ void engine_struct_dump(struct engine *e, FILE *stream) { if (e->outputlist_snapshots) outputlist_struct_dump(e->outputlist_snapshots, stream); if (e->outputlist_stats) outputlist_struct_dump(e->outputlist_stats, stream); + if (e->outputlist_stf) + outputlist_struct_dump(e->outputlist_stf, stream); } /** @@ -6944,6 +6967,13 @@ void engine_struct_restore(struct engine *e, FILE *stream) { e->outputlist_stats = outputlist_stats; } + if (e->outputlist_stf) { + struct outputlist *outputlist_stf = + (struct outputlist *) malloc(sizeof(struct outputlist)); + outputlist_struct_restore(outputlist_stf, stream); + e->outputlist_stf = outputlist_stf; + } + /* Want to force a rebuild before using this engine. Wait to repartition.*/ e->forcerebuild = 1; e->forcerepart = 0; diff --git a/src/engine.h b/src/engine.h index 3a917882b..4604bf1ab 100644 --- a/src/engine.h +++ b/src/engine.h @@ -214,8 +214,9 @@ struct engine { double a_first_snapshot; double time_first_snapshot; double delta_time_snapshot; + + /* Outputlist for the snapshots */ struct outputlist *outputlist_snapshots; - struct outputlist *outputlist_stats; /* Integer time of the next snapshot */ integertime_t ti_next_snapshot; @@ -233,6 +234,9 @@ struct engine { double deltaTimeSTF; int deltaStepSTF; + /* Outputlist for the structure finding */ + struct outputlist *outputlist_stf; + /* Integer time of the next stf output */ integertime_t ti_nextSTF; @@ -243,6 +247,9 @@ struct engine { double time_first_statistics; double delta_time_statistics; + /* Outputlist for the stats */ + struct outputlist *outputlist_stats; + /* Integer time of the next statistics dump */ integertime_t ti_next_stats; diff --git a/src/outputlist.c b/src/outputlist.c index 86e8aa988..d0ad000cc 100644 --- a/src/outputlist.c +++ b/src/outputlist.c @@ -184,7 +184,15 @@ void outputlist_read_next_time(struct outputlist *t, const struct engine *e, con } } - +/** + * @brief initialize an output list + * + * @param list The output list to initialize + * @param e The #engine + * @param name The name of the section in params + * @param delta_time updated to the initial delta time + * @param time_first updated to the time of first output (scale factor or cosmic time) + */ void outputlist_init(struct outputlist **list, const struct engine *e, char* name, double *delta_time, double *time_first) { struct swift_params *params = e->parameter_file; @@ -195,7 +203,7 @@ void outputlist_init(struct outputlist **list, const struct engine *e, char* nam /* Read output on/off */ char param_name[PARSER_MAX_LINE_SIZE]; - sprintf(param_name, "%s:outputlist_on", name); + sprintf(param_name, "%s:output_list_on", name); int outputlist_on = parser_get_opt_param_int(params, param_name, 0); @@ -224,7 +232,6 @@ void outputlist_init(struct outputlist **list, const struct engine *e, char* nam } } - } /** -- GitLab From 58253089c115ead954ed6add4a139541c44be1c9 Mon Sep 17 00:00:00 2001 From: loikki Date: Tue, 7 Aug 2018 11:25:27 +0200 Subject: [PATCH 28/32] Format --- src/engine.c | 36 ++++++++++++++++-------------------- src/engine.h | 2 +- src/outputlist.c | 29 ++++++++++++----------------- src/outputlist.h | 6 +++--- 4 files changed, 32 insertions(+), 41 deletions(-) diff --git a/src/engine.c b/src/engine.c index 184abee38..001bd824f 100644 --- a/src/engine.c +++ b/src/engine.c @@ -6443,8 +6443,8 @@ void engine_print_policy(struct engine *e) { void engine_compute_next_snapshot_time(struct engine *e) { /* Do outputlist file case */ if (e->outputlist_snapshots) { - outputlist_read_next_time(e->outputlist_snapshots, e, - "snapshots", &e->ti_next_snapshot); + outputlist_read_next_time(e->outputlist_snapshots, e, "snapshots", + &e->ti_next_snapshot); return; } @@ -6499,7 +6499,6 @@ void engine_compute_next_snapshot_time(struct engine *e) { } } - /** * @brief Computes the next time (on the time line) for a statistics dump * @@ -6508,8 +6507,8 @@ void engine_compute_next_snapshot_time(struct engine *e) { void engine_compute_next_statistics_time(struct engine *e) { /* Do outputlist file case */ if (e->outputlist_stats) { - outputlist_read_next_time(e->outputlist_stats, e, - "stats", &e->ti_next_stats); + outputlist_read_next_time(e->outputlist_stats, e, "stats", + &e->ti_next_stats); return; } @@ -6558,7 +6557,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); @@ -6574,8 +6573,7 @@ void engine_compute_next_statistics_time(struct engine *e) { void engine_compute_next_stf_time(struct engine *e) { /* Do outputlist file case */ if (e->outputlist_stf) { - outputlist_read_next_time(e->outputlist_stf, e, - "stf", &e->ti_nextSTF); + outputlist_read_next_time(e->outputlist_stf, e, "stf", &e->ti_nextSTF); return; } @@ -6636,9 +6634,9 @@ void engine_compute_next_stf_time(struct engine *e) { void engine_init_outputlists(struct engine *e, struct swift_params *params) { /* Deal with snapshots */ double snaps_time_first; - outputlist_init(&e->outputlist_snapshots, e, "Snapshots", &e->delta_time_snapshot, - &snaps_time_first); - + outputlist_init(&e->outputlist_snapshots, e, "Snapshots", + &e->delta_time_snapshot, &snaps_time_first); + if (e->policy & engine_policy_cosmology) e->a_first_snapshot = snaps_time_first; else @@ -6646,8 +6644,8 @@ void engine_init_outputlists(struct engine *e, struct swift_params *params) { /* Deal with stats */ double stats_time_first; - outputlist_init(&e->outputlist_stats, e, "Statistics", &e->delta_time_statistics, - &stats_time_first); + outputlist_init(&e->outputlist_stats, e, "Statistics", + &e->delta_time_statistics, &stats_time_first); if (e->policy & engine_policy_cosmology) e->a_first_statistics = stats_time_first; @@ -6657,13 +6655,12 @@ void engine_init_outputlists(struct engine *e, struct swift_params *params) { /* Deal with stf */ double stf_time_first; outputlist_init(&e->outputlist_stf, e, "StructureFinding", &e->deltaTimeSTF, - &stf_time_first); + &stf_time_first); if (e->policy & engine_policy_cosmology) e->a_first_stf = stf_time_first; else e->timeFirstSTFOutput = stats_time_first; - } /** @@ -6856,8 +6853,7 @@ void engine_struct_dump(struct engine *e, FILE *stream) { if (e->outputlist_snapshots) outputlist_struct_dump(e->outputlist_snapshots, stream); if (e->outputlist_stats) outputlist_struct_dump(e->outputlist_stats, stream); - if (e->outputlist_stf) - outputlist_struct_dump(e->outputlist_stf, stream); + if (e->outputlist_stf) outputlist_struct_dump(e->outputlist_stf, stream); } /** @@ -6955,21 +6951,21 @@ void engine_struct_restore(struct engine *e, FILE *stream) { if (e->outputlist_snapshots) { struct outputlist *outputlist_snapshots = - (struct outputlist *) malloc(sizeof(struct outputlist)); + (struct outputlist *)malloc(sizeof(struct outputlist)); outputlist_struct_restore(outputlist_snapshots, stream); e->outputlist_snapshots = outputlist_snapshots; } if (e->outputlist_stats) { struct outputlist *outputlist_stats = - (struct outputlist *) malloc(sizeof(struct outputlist)); + (struct outputlist *)malloc(sizeof(struct outputlist)); outputlist_struct_restore(outputlist_stats, stream); e->outputlist_stats = outputlist_stats; } if (e->outputlist_stf) { struct outputlist *outputlist_stf = - (struct outputlist *) malloc(sizeof(struct outputlist)); + (struct outputlist *)malloc(sizeof(struct outputlist)); outputlist_struct_restore(outputlist_stf, stream); e->outputlist_stf = outputlist_stf; } diff --git a/src/engine.h b/src/engine.h index 4604bf1ab..edc2c0f9f 100644 --- a/src/engine.h +++ b/src/engine.h @@ -249,7 +249,7 @@ struct engine { /* Outputlist for the stats */ struct outputlist *outputlist_stats; - + /* Integer time of the next statistics dump */ integertime_t ti_next_stats; diff --git a/src/outputlist.c b/src/outputlist.c index d0ad000cc..a8831951c 100644 --- a/src/outputlist.c +++ b/src/outputlist.c @@ -24,8 +24,8 @@ #include "outputlist.h" /* Local includes. */ -#include "engine.h" #include "cosmology.h" +#include "engine.h" #include "error.h" #include "restart.h" @@ -120,7 +120,6 @@ void outputlist_read_file(struct outputlist *outputlist, const char *filename, fclose(file); } - /** * @brief Read the next time for an output * @@ -129,7 +128,8 @@ void outputlist_read_file(struct outputlist *outputlist, const char *filename, * @param name The name of the output (e.g. 'stats', 'snapshots', 'stf') * @param ti_next updated to the next output time */ -void outputlist_read_next_time(struct outputlist *t, const struct engine *e, const char* name, integertime_t *ti_next) { +void outputlist_read_next_time(struct outputlist *t, const struct engine *e, + const char *name, integertime_t *ti_next) { int is_cosmo = e->policy & engine_policy_cosmology; /* Find upper-bound on last output */ @@ -161,8 +161,7 @@ void outputlist_read_next_time(struct outputlist *t, const struct engine *e, con } /* Deal with last statistics */ - if (*ti_next >= max_nr_timesteps || ind == t->size || - time >= time_end) { + if (*ti_next >= max_nr_timesteps || ind == t->size || time >= time_end) { *ti_next = -1; if (e->verbose) message("No further output time for %s.", name); } else { @@ -172,14 +171,11 @@ void outputlist_read_next_time(struct outputlist *t, const struct engine *e, con const double next_time = exp(*ti_next * e->time_base) * e->cosmology->a_begin; if (e->verbose) - message("Next output time for %s set to a=%e.", - name, next_time); + message("Next output time for %s set to a=%e.", name, next_time); } else { - const double next_time = - *ti_next * e->time_base + e->time_begin; + const double next_time = *ti_next * e->time_base + e->time_begin; if (e->verbose) - message("Next output time for %s set to t=%e.", - name, next_time); + message("Next output time for %s set to t=%e.", name, next_time); } } } @@ -191,10 +187,11 @@ void outputlist_read_next_time(struct outputlist *t, const struct engine *e, con * @param e The #engine * @param name The name of the section in params * @param delta_time updated to the initial delta time - * @param time_first updated to the time of first output (scale factor or cosmic time) + * @param time_first updated to the time of first output (scale factor or cosmic + * time) */ -void outputlist_init(struct outputlist **list, const struct engine *e, char* name, - double *delta_time, double *time_first) { +void outputlist_init(struct outputlist **list, const struct engine *e, + char *name, double *delta_time, double *time_first) { struct swift_params *params = e->parameter_file; /* get cosmo */ @@ -204,8 +201,7 @@ void outputlist_init(struct outputlist **list, const struct engine *e, char* nam /* Read output on/off */ char param_name[PARSER_MAX_LINE_SIZE]; sprintf(param_name, "%s:output_list_on", name); - int outputlist_on = - parser_get_opt_param_int(params, param_name, 0); + int outputlist_on = parser_get_opt_param_int(params, param_name, 0); /* Read outputlist for snapshots */ if (outputlist_on) { @@ -231,7 +227,6 @@ void outputlist_init(struct outputlist **list, const struct engine *e, char* nam *time_first = (*list)->times[0]; } } - } /** diff --git a/src/outputlist.h b/src/outputlist.h index 2d9528736..dac27e829 100644 --- a/src/outputlist.h +++ b/src/outputlist.h @@ -36,7 +36,6 @@ enum output_list_type { OUTPUTLIST_SCALE_FACTOR, }; - /** * @brief the array containing the output times */ @@ -54,9 +53,10 @@ struct outputlist { void outputlist_read_file(struct outputlist *outputlist, const char *filename, struct cosmology *cosmo); -void outputlist_read_next_time(struct outputlist *t, const struct engine *e, const char* name, integertime_t *ti_next); +void outputlist_read_next_time(struct outputlist *t, const struct engine *e, + const char *name, integertime_t *ti_next); void outputlist_init(struct outputlist **list, const struct engine *e, - char* name, double *delta_time, double *time_first); + char *name, double *delta_time, double *time_first); void outputlist_print(const struct outputlist *outputlist); void outputlist_clean(struct outputlist *outputlist); void outputlist_struct_dump(struct outputlist *list, FILE *stream); -- GitLab From 5f8d7d6e922b46d79722fd3dae5a5daca38f6e31 Mon Sep 17 00:00:00 2001 From: loikki Date: Tue, 7 Aug 2018 12:10:45 +0200 Subject: [PATCH 29/32] Do not overwrite ti_next if no outputlist --- src/engine.c | 44 +++++++++++++++++++++++++++----------------- src/outputlist.c | 46 ++++++++++++++++++++++++---------------------- src/outputlist.h | 2 +- 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/src/engine.c b/src/engine.c index 001bd824f..b5e81c6c9 100644 --- a/src/engine.c +++ b/src/engine.c @@ -5766,9 +5766,6 @@ 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->outputlist_snapshots = NULL; - e->outputlist_stats = NULL; - e->outputlist_stf = NULL; e->ti_next_snapshot = 0; parser_get_param_string(params, "Snapshots:basename", e->snapshot_base_name); e->snapshot_compression = @@ -5908,6 +5905,10 @@ void engine_config(int restart, struct engine *e, struct swift_params *params, error( "Invalid flag (%d) set for output time format of structure finding.", e->stf_output_freq_format); + + /* overwrite input if outputlist */ + if (e->outputlist_stf) + e->stf_output_freq_format = TIME; } /* Get the number of queues */ @@ -6634,33 +6635,42 @@ void engine_compute_next_stf_time(struct engine *e) { void engine_init_outputlists(struct engine *e, struct swift_params *params) { /* Deal with snapshots */ double snaps_time_first; + e->outputlist_snapshots = NULL; outputlist_init(&e->outputlist_snapshots, e, "Snapshots", - &e->delta_time_snapshot, &snaps_time_first); + &e->delta_time_snapshot, &snaps_time_first); - if (e->policy & engine_policy_cosmology) - e->a_first_snapshot = snaps_time_first; - else - e->time_first_snapshot = snaps_time_first; + if (e->outputlist_snapshots) { + if (e->policy & engine_policy_cosmology) + e->a_first_snapshot = snaps_time_first; + else + e->time_first_snapshot = snaps_time_first; + } /* Deal with stats */ double stats_time_first; + e->outputlist_stats = NULL; outputlist_init(&e->outputlist_stats, e, "Statistics", - &e->delta_time_statistics, &stats_time_first); + &e->delta_time_statistics, &stats_time_first); - if (e->policy & engine_policy_cosmology) - e->a_first_statistics = stats_time_first; - else - e->time_first_statistics = stats_time_first; + if (e->outputlist_stats) { + if (e->policy & engine_policy_cosmology) + e->a_first_statistics = stats_time_first; + else + e->time_first_statistics = stats_time_first; + } /* Deal with stf */ double stf_time_first; + e->outputlist_stf = NULL; outputlist_init(&e->outputlist_stf, e, "StructureFinding", &e->deltaTimeSTF, &stf_time_first); - if (e->policy & engine_policy_cosmology) - e->a_first_stf = stf_time_first; - else - e->timeFirstSTFOutput = stats_time_first; + if (e->outputlist_stf) { + if (e->policy & engine_policy_cosmology) + e->a_first_stf = stf_time_first; + else + e->timeFirstSTFOutput = stats_time_first; + } } /** diff --git a/src/outputlist.c b/src/outputlist.c index a8831951c..4d2a8df05 100644 --- a/src/outputlist.c +++ b/src/outputlist.c @@ -203,29 +203,31 @@ void outputlist_init(struct outputlist **list, const struct engine *e, sprintf(param_name, "%s:output_list_on", name); int outputlist_on = parser_get_opt_param_int(params, param_name, 0); - /* Read outputlist for snapshots */ - if (outputlist_on) { - *list = (struct outputlist *)malloc(sizeof(struct outputlist)); - - /* Read filename */ - char filename[PARSER_MAX_LINE_SIZE]; - sprintf(param_name, "%s:output_list", name); - parser_get_param_string(params, param_name, filename); - - message("Reading %s output file.", name); - outputlist_read_file(*list, filename, cosmo); - - if ((*list)->size < 2) - error("You need to provide more snapshots in '%s'", filename); + /* Check if read outputlist */ + if (!outputlist_on) + return; - /* Set data for later checks */ - if (cosmo) { - *delta_time = (*list)->times[1] / (*list)->times[0]; - *time_first = (*list)->times[0]; - } else { - *delta_time = (*list)->times[1] - (*list)->times[0]; - *time_first = (*list)->times[0]; - } + /* Read outputlist for snapshots */ + *list = (struct outputlist *)malloc(sizeof(struct outputlist)); + + /* Read filename */ + char filename[PARSER_MAX_LINE_SIZE]; + sprintf(param_name, "%s:output_list", name); + parser_get_param_string(params, param_name, filename); + + message("Reading %s output file.", name); + outputlist_read_file(*list, filename, cosmo); + + if ((*list)->size < 2) + error("You need to provide more snapshots in '%s'", filename); + + /* Set data for later checks */ + if (cosmo) { + *delta_time = (*list)->times[1] / (*list)->times[0]; + *time_first = (*list)->times[0]; + } else { + *delta_time = (*list)->times[1] - (*list)->times[0]; + *time_first = (*list)->times[0]; } } diff --git a/src/outputlist.h b/src/outputlist.h index dac27e829..2df8d5adc 100644 --- a/src/outputlist.h +++ b/src/outputlist.h @@ -56,7 +56,7 @@ void outputlist_read_file(struct outputlist *outputlist, const char *filename, void outputlist_read_next_time(struct outputlist *t, const struct engine *e, const char *name, integertime_t *ti_next); void outputlist_init(struct outputlist **list, const struct engine *e, - char *name, double *delta_time, double *time_first); + char *name, double *delta_time, double *time_first); void outputlist_print(const struct outputlist *outputlist); void outputlist_clean(struct outputlist *outputlist); void outputlist_struct_dump(struct outputlist *list, FILE *stream); -- GitLab From 18300cf96205e858643801e665ebbd31b3258ebc Mon Sep 17 00:00:00 2001 From: loikki Date: Tue, 7 Aug 2018 12:11:36 +0200 Subject: [PATCH 30/32] format --- src/engine.c | 7 +++---- src/outputlist.c | 5 ++--- src/outputlist.h | 2 +- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/engine.c b/src/engine.c index b5e81c6c9..e036e674a 100644 --- a/src/engine.c +++ b/src/engine.c @@ -5907,8 +5907,7 @@ void engine_config(int restart, struct engine *e, struct swift_params *params, e->stf_output_freq_format); /* overwrite input if outputlist */ - if (e->outputlist_stf) - e->stf_output_freq_format = TIME; + if (e->outputlist_stf) e->stf_output_freq_format = TIME; } /* Get the number of queues */ @@ -6637,7 +6636,7 @@ void engine_init_outputlists(struct engine *e, struct swift_params *params) { double snaps_time_first; e->outputlist_snapshots = NULL; outputlist_init(&e->outputlist_snapshots, e, "Snapshots", - &e->delta_time_snapshot, &snaps_time_first); + &e->delta_time_snapshot, &snaps_time_first); if (e->outputlist_snapshots) { if (e->policy & engine_policy_cosmology) @@ -6650,7 +6649,7 @@ void engine_init_outputlists(struct engine *e, struct swift_params *params) { double stats_time_first; e->outputlist_stats = NULL; outputlist_init(&e->outputlist_stats, e, "Statistics", - &e->delta_time_statistics, &stats_time_first); + &e->delta_time_statistics, &stats_time_first); if (e->outputlist_stats) { if (e->policy & engine_policy_cosmology) diff --git a/src/outputlist.c b/src/outputlist.c index 4d2a8df05..89f4f53c1 100644 --- a/src/outputlist.c +++ b/src/outputlist.c @@ -204,8 +204,7 @@ void outputlist_init(struct outputlist **list, const struct engine *e, int outputlist_on = parser_get_opt_param_int(params, param_name, 0); /* Check if read outputlist */ - if (!outputlist_on) - return; + if (!outputlist_on) return; /* Read outputlist for snapshots */ *list = (struct outputlist *)malloc(sizeof(struct outputlist)); @@ -217,7 +216,7 @@ void outputlist_init(struct outputlist **list, const struct engine *e, message("Reading %s output file.", name); outputlist_read_file(*list, filename, cosmo); - + if ((*list)->size < 2) error("You need to provide more snapshots in '%s'", filename); diff --git a/src/outputlist.h b/src/outputlist.h index 2df8d5adc..dac27e829 100644 --- a/src/outputlist.h +++ b/src/outputlist.h @@ -56,7 +56,7 @@ void outputlist_read_file(struct outputlist *outputlist, const char *filename, void outputlist_read_next_time(struct outputlist *t, const struct engine *e, const char *name, integertime_t *ti_next); void outputlist_init(struct outputlist **list, const struct engine *e, - char *name, double *delta_time, double *time_first); + char *name, double *delta_time, double *time_first); void outputlist_print(const struct outputlist *outputlist); void outputlist_clean(struct outputlist *outputlist); void outputlist_struct_dump(struct outputlist *list, FILE *stream); -- GitLab From 77df3818fd2e1ea92825aca91a19e40a0f4ee1af Mon Sep 17 00:00:00 2001 From: loikki Date: Wed, 8 Aug 2018 08:57:26 +0200 Subject: [PATCH 31/32] Change outputlist to output_list, small typo and remove useless function declaration --- src/engine.c | 100 +++++++++++++++++++++++------------------------ src/engine.h | 16 ++++---- src/outputlist.c | 54 ++++++++++++------------- src/outputlist.h | 30 +++++++------- 4 files changed, 99 insertions(+), 101 deletions(-) diff --git a/src/engine.c b/src/engine.c index e036e674a..36ba85513 100644 --- a/src/engine.c +++ b/src/engine.c @@ -5831,7 +5831,7 @@ void engine_init(struct engine *e, struct space *s, struct swift_params *params, e->ti_current = 0; } - engine_init_outputlists(e, params); + engine_init_output_lists(e, params); } /** @@ -5907,7 +5907,7 @@ void engine_config(int restart, struct engine *e, struct swift_params *params, e->stf_output_freq_format); /* overwrite input if outputlist */ - if (e->outputlist_stf) e->stf_output_freq_format = TIME; + if (e->output_list_stf) e->stf_output_freq_format = TIME; } /* Get the number of queues */ @@ -6442,8 +6442,8 @@ void engine_print_policy(struct engine *e) { */ void engine_compute_next_snapshot_time(struct engine *e) { /* Do outputlist file case */ - if (e->outputlist_snapshots) { - outputlist_read_next_time(e->outputlist_snapshots, e, "snapshots", + if (e->output_list_snapshots) { + output_list_read_next_time(e->output_list_snapshots, e, "snapshots", &e->ti_next_snapshot); return; } @@ -6505,9 +6505,9 @@ void engine_compute_next_snapshot_time(struct engine *e) { * @param e The #engine. */ void engine_compute_next_statistics_time(struct engine *e) { - /* Do outputlist file case */ - if (e->outputlist_stats) { - outputlist_read_next_time(e->outputlist_stats, e, "stats", + /* Do output_list file case */ + if (e->output_list_stats) { + output_list_read_next_time(e->output_list_stats, e, "stats", &e->ti_next_stats); return; } @@ -6571,9 +6571,9 @@ void engine_compute_next_statistics_time(struct engine *e) { * @param e The #engine. */ void engine_compute_next_stf_time(struct engine *e) { - /* Do outputlist file case */ - if (e->outputlist_stf) { - outputlist_read_next_time(e->outputlist_stf, e, "stf", &e->ti_nextSTF); + /* Do output_list file case */ + if (e->output_list_stf) { + output_list_read_next_time(e->output_list_stf, e, "stf", &e->ti_nextSTF); return; } @@ -6626,19 +6626,19 @@ void engine_compute_next_stf_time(struct engine *e) { } /** - * @brief Initialize all the outputlist required by the engine + * @brief Initialize all the output_list required by the engine * * @param e The #engine. * @param params The #swift_params. */ -void engine_init_outputlists(struct engine *e, struct swift_params *params) { +void engine_init_output_lists(struct engine *e, struct swift_params *params) { /* Deal with snapshots */ double snaps_time_first; - e->outputlist_snapshots = NULL; - outputlist_init(&e->outputlist_snapshots, e, "Snapshots", + e->output_list_snapshots = NULL; + output_list_init(&e->output_list_snapshots, e, "Snapshots", &e->delta_time_snapshot, &snaps_time_first); - if (e->outputlist_snapshots) { + if (e->output_list_snapshots) { if (e->policy & engine_policy_cosmology) e->a_first_snapshot = snaps_time_first; else @@ -6647,11 +6647,11 @@ void engine_init_outputlists(struct engine *e, struct swift_params *params) { /* Deal with stats */ double stats_time_first; - e->outputlist_stats = NULL; - outputlist_init(&e->outputlist_stats, e, "Statistics", + e->output_list_stats = NULL; + output_list_init(&e->output_list_stats, e, "Statistics", &e->delta_time_statistics, &stats_time_first); - if (e->outputlist_stats) { + if (e->output_list_stats) { if (e->policy & engine_policy_cosmology) e->a_first_statistics = stats_time_first; else @@ -6660,15 +6660,15 @@ void engine_init_outputlists(struct engine *e, struct swift_params *params) { /* Deal with stf */ double stf_time_first; - e->outputlist_stf = NULL; - outputlist_init(&e->outputlist_stf, e, "StructureFinding", &e->deltaTimeSTF, + e->output_list_stf = NULL; + output_list_init(&e->output_list_stf, e, "StructureFinding", &e->deltaTimeSTF, &stf_time_first); - if (e->outputlist_stf) { + if (e->output_list_stf) { if (e->policy & engine_policy_cosmology) e->a_first_stf = stf_time_first; else - e->timeFirstSTFOutput = stats_time_first; + e->timeFirstSTFOutput = stf_time_first; } } @@ -6805,17 +6805,17 @@ void engine_clean(struct engine *e) { } free(e->runners); free(e->snapshot_units); - if (e->outputlist_snapshots) { - outputlist_clean(e->outputlist_snapshots); - free(e->outputlist_snapshots); + if (e->output_list_snapshots) { + output_list_clean(e->output_list_snapshots); + free(e->output_list_snapshots); } - if (e->outputlist_stats) { - outputlist_clean(e->outputlist_stats); - free(e->outputlist_stats); + if (e->output_list_stats) { + output_list_clean(e->output_list_stats); + free(e->output_list_stats); } - if (e->outputlist_stf) { - outputlist_clean(e->outputlist_stf); - free(e->outputlist_stf); + if (e->output_list_stf) { + output_list_clean(e->output_list_stf); + free(e->output_list_stf); } free(e->links); free(e->cell_loc); @@ -6859,10 +6859,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->outputlist_snapshots) - outputlist_struct_dump(e->outputlist_snapshots, stream); - if (e->outputlist_stats) outputlist_struct_dump(e->outputlist_stats, stream); - if (e->outputlist_stf) outputlist_struct_dump(e->outputlist_stf, stream); + if (e->output_list_snapshots) + output_list_struct_dump(e->output_list_snapshots, stream); + if (e->output_list_stats) output_list_struct_dump(e->output_list_stats, stream); + if (e->output_list_stf) output_list_struct_dump(e->output_list_stf, stream); } /** @@ -6958,25 +6958,25 @@ void engine_struct_restore(struct engine *e, FILE *stream) { parser_struct_restore(parameter_file, stream); e->parameter_file = parameter_file; - 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->output_list_snapshots) { + struct output_list *output_list_snapshots = + (struct output_list *)malloc(sizeof(struct output_list)); + output_list_struct_restore(output_list_snapshots, stream); + e->output_list_snapshots = output_list_snapshots; } - 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; + if (e->output_list_stats) { + struct output_list *output_list_stats = + (struct output_list *)malloc(sizeof(struct output_list)); + output_list_struct_restore(output_list_stats, stream); + e->output_list_stats = output_list_stats; } - if (e->outputlist_stf) { - struct outputlist *outputlist_stf = - (struct outputlist *)malloc(sizeof(struct outputlist)); - outputlist_struct_restore(outputlist_stf, stream); - e->outputlist_stf = outputlist_stf; + if (e->output_list_stf) { + struct output_list *output_list_stf = + (struct output_list *)malloc(sizeof(struct output_list)); + output_list_struct_restore(output_list_stf, stream); + e->output_list_stf = output_list_stf; } /* Want to force a rebuild before using this engine. Wait to repartition.*/ diff --git a/src/engine.h b/src/engine.h index edc2c0f9f..aeb57c65a 100644 --- a/src/engine.h +++ b/src/engine.h @@ -215,8 +215,8 @@ struct engine { double time_first_snapshot; double delta_time_snapshot; - /* Outputlist for the snapshots */ - struct outputlist *outputlist_snapshots; + /* Output_List for the snapshots */ + struct output_list *output_list_snapshots; /* Integer time of the next snapshot */ integertime_t ti_next_snapshot; @@ -234,8 +234,8 @@ struct engine { double deltaTimeSTF; int deltaStepSTF; - /* Outputlist for the structure finding */ - struct outputlist *outputlist_stf; + /* Output_List for the structure finding */ + struct output_list *output_list_stf; /* Integer time of the next stf output */ integertime_t ti_nextSTF; @@ -247,8 +247,8 @@ struct engine { double time_first_statistics; double delta_time_statistics; - /* Outputlist for the stats */ - struct outputlist *outputlist_stats; + /* Output_List for the stats */ + struct output_list *output_list_stats; /* Integer time of the next statistics dump */ integertime_t ti_next_stats; @@ -374,9 +374,7 @@ void engine_addlink(struct engine *e, struct link **l, struct task *t); void engine_barrier(struct engine *e); void engine_compute_next_snapshot_time(struct engine *e); void engine_compute_next_stf_time(struct engine *e); -void engine_read_next_snapshot_time(struct engine *e); void engine_compute_next_statistics_time(struct engine *e); -void engine_read_next_statistics_time(struct engine *e); void engine_recompute_displacement_constraint(struct engine *e); void engine_unskip(struct engine *e); void engine_drift_all(struct engine *e); @@ -384,7 +382,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_init_outputlists(struct engine *e, struct swift_params *params); +void engine_init_output_lists(struct engine *e, 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 index 89f4f53c1..b7eef083f 100644 --- a/src/outputlist.c +++ b/src/outputlist.c @@ -35,11 +35,11 @@ /** * @brief Read a file containing a list of time * - * @param outputlist The #outputlist to fill. + * @param outputlist The #output_list to fill. * @param filename The file to read. * @param cosmo The #cosmology model. */ -void outputlist_read_file(struct outputlist *outputlist, const char *filename, +void output_list_read_file(struct output_list *outputlist, const char *filename, struct cosmology *cosmo) { /* Open file */ @@ -63,7 +63,7 @@ void outputlist_read_file(struct outputlist *outputlist, const char *filename, outputlist->times = (double *)malloc(sizeof(double) * nber_line); if (!outputlist->times) error( - "Unable to malloc outputlist. " + "Unable to malloc output_list. " "Try reducing the number of lines in %s", filename); @@ -77,16 +77,16 @@ void outputlist_read_file(struct outputlist *outputlist, const char *filename, /* Find type of data in file */ int type = -1; if (!strcmp(line, "# Redshift")) - type = OUTPUTLIST_REDSHIFT; + type = OUTPUT_LIST_REDSHIFT; else if (!strcmp(line, "# Time")) - type = OUTPUTLIST_AGE; + type = OUTPUT_LIST_AGE; else if (!strcmp(line, "# Scale Factor")) - type = OUTPUTLIST_SCALE_FACTOR; + type = OUTPUT_LIST_SCALE_FACTOR; else error("Unable to interpret the header (%s) in file '%s'", line, filename); if (!cosmo && - (type == OUTPUTLIST_SCALE_FACTOR || type == OUTPUTLIST_REDSHIFT)) + (type == OUTPUT_LIST_SCALE_FACTOR || type == OUTPUT_LIST_REDSHIFT)) error( "Unable to compute a redshift or a scale factor without cosmology. " "Please change the header in '%s'", @@ -105,9 +105,9 @@ void outputlist_read_file(struct outputlist *outputlist, const char *filename, } /* Transform input into correct time (e.g. ages or scale factor) */ - if (type == OUTPUTLIST_REDSHIFT) *time = 1. / (1. + *time); + if (type == OUTPUT_LIST_REDSHIFT) *time = 1. / (1. + *time); - if (cosmo && type == OUTPUTLIST_AGE) + if (cosmo && type == OUTPUT_LIST_AGE) *time = cosmology_get_scale_factor(cosmo, *time); /* Update size */ @@ -123,12 +123,12 @@ void outputlist_read_file(struct outputlist *outputlist, const char *filename, /** * @brief Read the next time for an output * - * @param t The #outputlist + * @param t The #output_list * @param e The #engine. * @param name The name of the output (e.g. 'stats', 'snapshots', 'stf') * @param ti_next updated to the next output time */ -void outputlist_read_next_time(struct outputlist *t, const struct engine *e, +void output_list_read_next_time(struct output_list *t, const struct engine *e, const char *name, integertime_t *ti_next) { int is_cosmo = e->policy & engine_policy_cosmology; @@ -190,7 +190,7 @@ void outputlist_read_next_time(struct outputlist *t, const struct engine *e, * @param time_first updated to the time of first output (scale factor or cosmic * time) */ -void outputlist_init(struct outputlist **list, const struct engine *e, +void output_list_init(struct output_list **list, const struct engine *e, char *name, double *delta_time, double *time_first) { struct swift_params *params = e->parameter_file; @@ -207,7 +207,7 @@ void outputlist_init(struct outputlist **list, const struct engine *e, if (!outputlist_on) return; /* Read outputlist for snapshots */ - *list = (struct outputlist *)malloc(sizeof(struct outputlist)); + *list = (struct output_list *)malloc(sizeof(struct output_list)); /* Read filename */ char filename[PARSER_MAX_LINE_SIZE]; @@ -215,7 +215,7 @@ void outputlist_init(struct outputlist **list, const struct engine *e, parser_get_param_string(params, param_name, filename); message("Reading %s output file.", name); - outputlist_read_file(*list, filename, cosmo); + output_list_read_file(*list, filename, cosmo); if ((*list)->size < 2) error("You need to provide more snapshots in '%s'", filename); @@ -231,9 +231,9 @@ void outputlist_init(struct outputlist **list, const struct engine *e, } /** - * @brief Print an #outputlist + * @brief Print an #output_list */ -void outputlist_print(const struct outputlist *outputlist) { +void output_list_print(const struct output_list *outputlist) { printf("/*\t Time Array\t */\n"); printf("Number of Line: %lu\n", outputlist->size); @@ -246,29 +246,29 @@ void outputlist_print(const struct outputlist *outputlist) { } /** - * @brief Clean an #outputlist + * @brief Clean an #output_list */ -void outputlist_clean(struct outputlist *outputlist) { +void output_list_clean(struct output_list *outputlist) { free(outputlist->times); } /** - * @brief Dump an #outputlist in a restart file + * @brief Dump an #output_list 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"); +void output_list_struct_dump(struct output_list *list, FILE *stream) { + restart_write_blocks(list, sizeof(struct output_list), 1, stream, "output_list", + "output_list struct"); restart_write_blocks(list->times, list->size, sizeof(double), stream, - "outputlist", "times"); + "output_list", "times"); } /** - * @brief Restore an #outputlist from a restart file + * @brief Restore an #output_list 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"); +void output_list_struct_restore(struct output_list *list, FILE *stream) { + restart_read_blocks(list, sizeof(struct output_list), 1, stream, NULL, + "output_list struct"); list->times = (double *)malloc(sizeof(double) * list->size); restart_read_blocks(list->times, list->size, sizeof(double), stream, NULL, diff --git a/src/outputlist.h b/src/outputlist.h index dac27e829..050bc23f1 100644 --- a/src/outputlist.h +++ b/src/outputlist.h @@ -16,8 +16,8 @@ * along with this program. If not, see . * ******************************************************************************/ -#ifndef SWIFT_OUTPUTLIST_H -#define SWIFT_OUTPUTLIST_H +#ifndef SWIFT_OUTPUT_LIST_H +#define SWIFT_OUTPUT_LIST_H /* Config parameters. */ #include "../config.h" @@ -28,18 +28,18 @@ struct engine; /** - * @brief the different outputlist type + * @brief the different output_list type */ enum output_list_type { - OUTPUTLIST_AGE, - OUTPUTLIST_REDSHIFT, - OUTPUTLIST_SCALE_FACTOR, + OUTPUT_LIST_AGE, + OUTPUT_LIST_REDSHIFT, + OUTPUT_LIST_SCALE_FACTOR, }; /** * @brief the array containing the output times */ -struct outputlist { +struct output_list { /* Time array */ double *times; @@ -51,15 +51,15 @@ struct outputlist { size_t cur_ind; }; -void outputlist_read_file(struct outputlist *outputlist, const char *filename, +void output_list_read_file(struct output_list *outputlist, const char *filename, struct cosmology *cosmo); -void outputlist_read_next_time(struct outputlist *t, const struct engine *e, +void output_list_read_next_time(struct output_list *t, const struct engine *e, const char *name, integertime_t *ti_next); -void outputlist_init(struct outputlist **list, const struct engine *e, +void output_list_init(struct output_list **list, const struct engine *e, char *name, double *delta_time, double *time_first); -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); +void output_list_print(const struct output_list *outputlist); +void output_list_clean(struct output_list *outputlist); +void output_list_struct_dump(struct output_list *list, FILE *stream); +void output_list_struct_restore(struct output_list *list, FILE *stream); -#endif /* SWIFT_OUTPUTLIST_H */ +#endif /* SWIFT_OUTPUT_LIST_H */ -- GitLab From a263016dd17c9ce947f0b84e91dd4fe9d96eeb1a Mon Sep 17 00:00:00 2001 From: loikki Date: Wed, 8 Aug 2018 09:01:44 +0200 Subject: [PATCH 32/32] format --- src/engine.c | 13 +++++++------ src/outputlist.c | 10 +++++----- src/outputlist.h | 6 +++--- 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/engine.c b/src/engine.c index 36ba85513..28dd94ca2 100644 --- a/src/engine.c +++ b/src/engine.c @@ -6444,7 +6444,7 @@ void engine_compute_next_snapshot_time(struct engine *e) { /* Do outputlist file case */ if (e->output_list_snapshots) { output_list_read_next_time(e->output_list_snapshots, e, "snapshots", - &e->ti_next_snapshot); + &e->ti_next_snapshot); return; } @@ -6508,7 +6508,7 @@ void engine_compute_next_statistics_time(struct engine *e) { /* Do output_list file case */ if (e->output_list_stats) { output_list_read_next_time(e->output_list_stats, e, "stats", - &e->ti_next_stats); + &e->ti_next_stats); return; } @@ -6636,7 +6636,7 @@ void engine_init_output_lists(struct engine *e, struct swift_params *params) { double snaps_time_first; e->output_list_snapshots = NULL; output_list_init(&e->output_list_snapshots, e, "Snapshots", - &e->delta_time_snapshot, &snaps_time_first); + &e->delta_time_snapshot, &snaps_time_first); if (e->output_list_snapshots) { if (e->policy & engine_policy_cosmology) @@ -6649,7 +6649,7 @@ void engine_init_output_lists(struct engine *e, struct swift_params *params) { double stats_time_first; e->output_list_stats = NULL; output_list_init(&e->output_list_stats, e, "Statistics", - &e->delta_time_statistics, &stats_time_first); + &e->delta_time_statistics, &stats_time_first); if (e->output_list_stats) { if (e->policy & engine_policy_cosmology) @@ -6662,7 +6662,7 @@ void engine_init_output_lists(struct engine *e, struct swift_params *params) { double stf_time_first; e->output_list_stf = NULL; output_list_init(&e->output_list_stf, e, "StructureFinding", &e->deltaTimeSTF, - &stf_time_first); + &stf_time_first); if (e->output_list_stf) { if (e->policy & engine_policy_cosmology) @@ -6861,7 +6861,8 @@ void engine_struct_dump(struct engine *e, FILE *stream) { parser_struct_dump(e->parameter_file, stream); if (e->output_list_snapshots) output_list_struct_dump(e->output_list_snapshots, stream); - if (e->output_list_stats) output_list_struct_dump(e->output_list_stats, stream); + if (e->output_list_stats) + output_list_struct_dump(e->output_list_stats, stream); if (e->output_list_stf) output_list_struct_dump(e->output_list_stf, stream); } diff --git a/src/outputlist.c b/src/outputlist.c index b7eef083f..d141c9f0b 100644 --- a/src/outputlist.c +++ b/src/outputlist.c @@ -40,7 +40,7 @@ * @param cosmo The #cosmology model. */ void output_list_read_file(struct output_list *outputlist, const char *filename, - struct cosmology *cosmo) { + struct cosmology *cosmo) { /* Open file */ FILE *file = fopen(filename, "r"); @@ -129,7 +129,7 @@ void output_list_read_file(struct output_list *outputlist, const char *filename, * @param ti_next updated to the next output time */ void output_list_read_next_time(struct output_list *t, const struct engine *e, - const char *name, integertime_t *ti_next) { + const char *name, integertime_t *ti_next) { int is_cosmo = e->policy & engine_policy_cosmology; /* Find upper-bound on last output */ @@ -191,7 +191,7 @@ void output_list_read_next_time(struct output_list *t, const struct engine *e, * time) */ void output_list_init(struct output_list **list, const struct engine *e, - char *name, double *delta_time, double *time_first) { + char *name, double *delta_time, double *time_first) { struct swift_params *params = e->parameter_file; /* get cosmo */ @@ -256,8 +256,8 @@ void output_list_clean(struct output_list *outputlist) { * @brief Dump an #output_list in a restart file */ void output_list_struct_dump(struct output_list *list, FILE *stream) { - restart_write_blocks(list, sizeof(struct output_list), 1, stream, "output_list", - "output_list struct"); + restart_write_blocks(list, sizeof(struct output_list), 1, stream, + "output_list", "output_list struct"); restart_write_blocks(list->times, list->size, sizeof(double), stream, "output_list", "times"); diff --git a/src/outputlist.h b/src/outputlist.h index 050bc23f1..ddc08c84d 100644 --- a/src/outputlist.h +++ b/src/outputlist.h @@ -52,11 +52,11 @@ struct output_list { }; void output_list_read_file(struct output_list *outputlist, const char *filename, - struct cosmology *cosmo); + struct cosmology *cosmo); void output_list_read_next_time(struct output_list *t, const struct engine *e, - const char *name, integertime_t *ti_next); + const char *name, integertime_t *ti_next); void output_list_init(struct output_list **list, const struct engine *e, - char *name, double *delta_time, double *time_first); + char *name, double *delta_time, double *time_first); void output_list_print(const struct output_list *outputlist); void output_list_clean(struct output_list *outputlist); void output_list_struct_dump(struct output_list *list, FILE *stream); -- GitLab