From 3e593e240ebffee7fda40e61b3fa3351b036897c Mon Sep 17 00:00:00 2001 From: Alexei Borissov Date: Tue, 23 Apr 2019 11:20:46 +0100 Subject: [PATCH 1/9] add empty restart functions --- src/engine.c | 7 ++++ src/feedback/EAGLE/feedback.c | 1 + src/feedback/EAGLE/feedback_restart.h | 60 +++++++++++++++++++++++++++ src/feedback/none/feedback_restart.h | 60 +++++++++++++++++++++++++++ src/feedback_restart.h | 34 +++++++++++++++ 5 files changed, 162 insertions(+) create mode 100644 src/feedback/EAGLE/feedback_restart.h create mode 100644 src/feedback/none/feedback_restart.h create mode 100644 src/feedback_restart.h diff --git a/src/engine.c b/src/engine.c index 5c41c9ad7..8b86df623 100644 --- a/src/engine.c +++ b/src/engine.c @@ -88,6 +88,7 @@ #include "star_formation.h" #include "star_formation_logger.h" #include "star_formation_logger_struct.h" +#include "feedback_restart.h" #include "stars_io.h" #include "statistics.h" #include "timers.h" @@ -6078,6 +6079,7 @@ void engine_struct_dump(struct engine *e, FILE *stream) { potential_struct_dump(e->external_potential, stream); cooling_struct_dump(e->cooling_func, stream); starformation_struct_dump(e->star_formation, stream); + feedback_struct_dump(e->feedback_props, stream); chemistry_struct_dump(e->chemistry, stream); parser_struct_dump(e->parameter_file, stream); if (e->output_list_snapshots) @@ -6180,6 +6182,11 @@ void engine_struct_restore(struct engine *e, FILE *stream) { starformation_struct_restore(star_formation, stream); e->star_formation = star_formation; + struct feedback_props *feedback_properties = + (struct feedback_props *)malloc(sizeof(struct feedback_props)); + feedback_struct_restore(feedback_properties, stream); + e->feedback_props = feedback_properties; + struct chemistry_global_data *chemistry = (struct chemistry_global_data *)malloc( sizeof(struct chemistry_global_data)); diff --git a/src/feedback/EAGLE/feedback.c b/src/feedback/EAGLE/feedback.c index 8d81eae0a..23e2d8564 100644 --- a/src/feedback/EAGLE/feedback.c +++ b/src/feedback/EAGLE/feedback.c @@ -991,3 +991,4 @@ void feedback_props_init(struct feedback_props* fp, message("initialized stellar feedback"); } + diff --git a/src/feedback/EAGLE/feedback_restart.h b/src/feedback/EAGLE/feedback_restart.h new file mode 100644 index 000000000..b3f37153d --- /dev/null +++ b/src/feedback/EAGLE/feedback_restart.h @@ -0,0 +1,60 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Coypright (c) 2018 Matthieu Schaller (schaller@strw.leidenuniv.nl) + * + * 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_EAGLE_FEEDBACK_RESTART_H +#define SWIFT_EAGLE_FEEDBACK_RESTART_H + +#include "feedback.h" + +/** + * @brief Restore feedback tables (if applicable) after + * restart + * + * @param feedback the #feedback_props structure + * @param cosmo #cosmology structure + */ +void feedback_restore_tables(struct feedback_props *feedback, + const struct cosmology *cosmo) { + +} + +/** + * @brief Write a feedback struct to the given FILE as a stream of bytes. + * + * @param feedback the struct + * @param stream the file stream + */ +void feedback_struct_dump(const struct feedback_props *feedback, + FILE *stream) { +} + +/** + * @brief Restore a hydro_props struct from the given FILE as a stream of + * bytes. + * + * Read the structure from the stream and restore the feedback tables by + * re-reading them. + * + * @param feedback the struct + * @param stream the file stream + * @param cosmo #cosmology structure + */ +void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) { +} + +#endif /* SWIFT_EAGLE_FEEDBACK_RESTART_H */ diff --git a/src/feedback/none/feedback_restart.h b/src/feedback/none/feedback_restart.h new file mode 100644 index 000000000..b3f37153d --- /dev/null +++ b/src/feedback/none/feedback_restart.h @@ -0,0 +1,60 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Coypright (c) 2018 Matthieu Schaller (schaller@strw.leidenuniv.nl) + * + * 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_EAGLE_FEEDBACK_RESTART_H +#define SWIFT_EAGLE_FEEDBACK_RESTART_H + +#include "feedback.h" + +/** + * @brief Restore feedback tables (if applicable) after + * restart + * + * @param feedback the #feedback_props structure + * @param cosmo #cosmology structure + */ +void feedback_restore_tables(struct feedback_props *feedback, + const struct cosmology *cosmo) { + +} + +/** + * @brief Write a feedback struct to the given FILE as a stream of bytes. + * + * @param feedback the struct + * @param stream the file stream + */ +void feedback_struct_dump(const struct feedback_props *feedback, + FILE *stream) { +} + +/** + * @brief Restore a hydro_props struct from the given FILE as a stream of + * bytes. + * + * Read the structure from the stream and restore the feedback tables by + * re-reading them. + * + * @param feedback the struct + * @param stream the file stream + * @param cosmo #cosmology structure + */ +void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) { +} + +#endif /* SWIFT_EAGLE_FEEDBACK_RESTART_H */ diff --git a/src/feedback_restart.h b/src/feedback_restart.h new file mode 100644 index 000000000..86072b3ae --- /dev/null +++ b/src/feedback_restart.h @@ -0,0 +1,34 @@ +/******************************************************************************* + * This file is part of SWIFT. + * Coypright (c) 2018 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_FEEDBACK_RESTART_H +#define SWIFT_FEEDBACK_RESTART_H + +/* Config parameters. */ +#include "../config.h" + +/* Select the correct feedback model */ +#if defined(FEEDBACK_NONE) +#include "./feedback/none/feedback_restart.h" +#elif defined(FEEDBACK_EAGLE) +#include "./feedback/EAGLE/feedback_restart.h" +#else +#error "Invalid choice of feedback model" +#endif + +#endif /* SWIFT_FEEDBACK_RESTART_H */ -- GitLab From 9ab71506cc123b4f435a14866ad09fbed3818f26 Mon Sep 17 00:00:00 2001 From: Alexei Borissov Date: Tue, 23 Apr 2019 16:41:54 +0100 Subject: [PATCH 2/9] Added restart functions in EAGLE feedback, debugging restart crash with stars --- src/engine.c | 3 + src/engine_drift.c | 3 + src/feedback/EAGLE/feedback_restart.h | 100 +++++++++++++++++++++++++- src/feedback/none/feedback_restart.h | 3 +- 4 files changed, 104 insertions(+), 5 deletions(-) diff --git a/src/engine.c b/src/engine.c index 8b86df623..635d4c996 100644 --- a/src/engine.c +++ b/src/engine.c @@ -6114,6 +6114,9 @@ void engine_struct_restore(struct engine *e, FILE *stream) { space_struct_restore(s, stream); e->s = s; s->e = e; + // debugging print statements + //message("space spart id %llu",s->sparts[0].id); + //message("cells_top spart id %llu",s->cells_top->stars.parts[0].id); struct unit_system *us = (struct unit_system *)malloc(sizeof(struct unit_system)); diff --git a/src/engine_drift.c b/src/engine_drift.c index fee9ed1f1..c90a15b56 100644 --- a/src/engine_drift.c +++ b/src/engine_drift.c @@ -167,6 +167,9 @@ void engine_do_drift_all_spart_mapper(void *map_data, int num_elements, if (c->nodeID == e->nodeID) { /* Drift all the particles */ + // debugging print statements + //message("space spart id %llu", s->sparts[0].id); + //message("cell spart id %llu", c->stars.parts[0].id); cell_drift_spart(c, e, /* force the drift=*/1); } } diff --git a/src/feedback/EAGLE/feedback_restart.h b/src/feedback/EAGLE/feedback_restart.h index b3f37153d..375281544 100644 --- a/src/feedback/EAGLE/feedback_restart.h +++ b/src/feedback/EAGLE/feedback_restart.h @@ -21,6 +21,23 @@ #include "feedback.h" +/** + * @brief Zero pointers in yield_table structs + * + * @param table yield_table struct in which pointers to tables + * set to NULL + */ +void zero_yield_table_pointers(struct yield_table table){ + table.mass = NULL; + table.metallicity = NULL; + table.yield_IMF_resampled = NULL; + table.yield = NULL; + table.ejecta_IMF_resampled = NULL; + table.ejecta = NULL; + table.total_metals_IMF_resampled = NULL; + table.total_metals = NULL; +} + /** * @brief Restore feedback tables (if applicable) after * restart @@ -28,9 +45,47 @@ * @param feedback the #feedback_props structure * @param cosmo #cosmology structure */ -void feedback_restore_tables(struct feedback_props *feedback, - const struct cosmology *cosmo) { +void feedback_restore_tables(struct feedback_props *feedback) { + + /* Initialise the IMF ------------------------------------------------- */ + + init_imf(fp); + + /* Calculate number of type II SN per unit solar mass based on our choice + * of IMF and integration limits for type II SNe. + * Note: No weighting by yields here. */ + fp->num_SNII_per_msun = + integrate_imf(fp->log10_SNII_min_mass_msun, fp->log10_SNII_max_mass_msun, + eagle_imf_integration_no_weight, + /*(stellar_yields=)*/ NULL, fp); + + /* Initialise the yields ---------------------------------------------- */ + + /* Read yield table filepath */ + parser_get_param_string(params, "EAGLEFeedback:filename", + fp->yield_table_path); + + /* Allocate yield tables */ + allocate_yield_tables(fp); + + /* Read the tables */ + read_yield_tables(fp); + + /* Set yield_mass_bins array */ + const float imf_log10_mass_bin_size = + (fp->log10_imf_max_mass_msun - fp->log10_imf_min_mass_msun) / + (eagle_feedback_N_imf_bins - 1); + for (int i = 0; i < eagle_feedback_N_imf_bins; i++) + fp->yield_mass_bins[i] = + imf_log10_mass_bin_size * i + fp->log10_imf_min_mass_msun; + + /* Resample yields from mass bins used in tables to mass bins used in IMF */ + compute_yields(fp); + + /* Resample ejecta contribution to enrichment from mass bins used in tables to + * mass bins used in IMF */ + compute_ejecta(fp); } /** @@ -41,6 +96,42 @@ void feedback_restore_tables(struct feedback_props *feedback, */ void feedback_struct_dump(const struct feedback_props *feedback, FILE *stream) { + + /* To make sure everything is restored correctly, we zero all the pointers to + tables. If they are not restored correctly, we would crash after restart on + the first call to the feedback routines. Helps debugging. */ + struct feedback_props feedback_copy = *feedback; + + /* zero AGB and SNII table pointers */ + zero_yield_table_pointers(feedback_copy.yield_AGB); + zero_yield_table_pointers(feedback_copy.yield_SNII); + + /* zero SNIa table pointers */ + feedback_copy.yield_SNIa_IMF_resampled = NULL; + feedback_copy.yields_SNIa = NULL; + feedback_copy.yield_SNIa_total_metals_IMF_resampled = 0; + + /* zero element name tables */ + feedback_copy.SNIa_element_names = NULL; + feedback_copy.SNII_element_names = NULL; + feedback_copy.AGB_element_names = NULL; + + /* zero mass bins table */ + feedback_copy.yield_mass_bins = NULL; + + /* zero lifetime tracks */ + feedback_copy.lifetimes.mass = NULL; + feedback_copy.lifetimes.metallicity = NULL; + feedback_copy.lifetimes.dyingtime = NULL; + + /* zero IMF tables */ + feedback_copy.imf = NULL; + feedback_copy.imf_mass_bin = NULL; + feedback_copy.imf_mass_bin_log10 = NULL; + + restart_write_blocks((void *)&feedback_copy, + sizeof(struct feedback_props), 1, stream, + "feedback", "feedback function"); } /** @@ -52,9 +143,12 @@ void feedback_struct_dump(const struct feedback_props *feedback, * * @param feedback the struct * @param stream the file stream - * @param cosmo #cosmology structure */ void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) { + restart_read_blocks((void *)feedback, sizeof(struct feedback_props), 1, + stream, NULL, "feedback function"); + + feedback_restore_tables(feedback); } #endif /* SWIFT_EAGLE_FEEDBACK_RESTART_H */ diff --git a/src/feedback/none/feedback_restart.h b/src/feedback/none/feedback_restart.h index b3f37153d..b69170ac0 100644 --- a/src/feedback/none/feedback_restart.h +++ b/src/feedback/none/feedback_restart.h @@ -28,8 +28,7 @@ * @param feedback the #feedback_props structure * @param cosmo #cosmology structure */ -void feedback_restore_tables(struct feedback_props *feedback, - const struct cosmology *cosmo) { +void feedback_restore_tables(struct feedback_props *feedback) { } -- GitLab From 89e60cd3bff357bc176f5db15c1946c2d0a6dd57 Mon Sep 17 00:00:00 2001 From: Alexei Borissov Date: Wed, 24 Apr 2019 14:28:28 +0100 Subject: [PATCH 3/9] Added table reading function to feedback restart --- examples/SubgridTests/StellarEvolution/makeIC.py | 2 ++ src/debug.c | 2 +- src/feedback/EAGLE/feedback_restart.h | 9 ++++----- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/examples/SubgridTests/StellarEvolution/makeIC.py b/examples/SubgridTests/StellarEvolution/makeIC.py index 0ad23b4a9..abc2d8126 100644 --- a/examples/SubgridTests/StellarEvolution/makeIC.py +++ b/examples/SubgridTests/StellarEvolution/makeIC.py @@ -102,8 +102,10 @@ file = h5py.File(fileName, 'w') grp = file.create_group("/Header") grp.attrs["BoxSize"] = [boxsize]*3 grp.attrs["NumPart_Total"] = [numPart, 0, 0, 0, 1, 0] +#grp.attrs["NumPart_Total"] = [numPart, 0, 0, 0, 0, 0] grp.attrs["NumPart_Total_HighWord"] = [0, 0, 0, 0, 0, 0] grp.attrs["NumPart_ThisFile"] = [numPart, 0, 0, 0, 1, 0] +#grp.attrs["NumPart_ThisFile"] = [numPart, 0, 0, 0, 0, 0] grp.attrs["Time"] = 0.0 grp.attrs["NumFilesPerSnapshot"] = 1 grp.attrs["MassTable"] = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0] diff --git a/src/debug.c b/src/debug.c index 6257f7cf4..d2d62181d 100644 --- a/src/debug.c +++ b/src/debug.c @@ -311,7 +311,7 @@ int checkCellhdxmax(const struct cell *c, int *depth) { sp->x[2] < loc_min[2] || sp->x[2] >= loc_max[2]) { message( - "Inconsistent part position p->x=[%e %e %e], c->loc=[%e %e %e] " + "Inconsistent spart position sp->x=[%e %e %e], c->loc=[%e %e %e] " "c->width=[%e %e %e]", sp->x[0], sp->x[1], sp->x[2], c->loc[0], c->loc[1], c->loc[2], c->width[0], c->width[1], c->width[2]); diff --git a/src/feedback/EAGLE/feedback_restart.h b/src/feedback/EAGLE/feedback_restart.h index 375281544..1eba27c05 100644 --- a/src/feedback/EAGLE/feedback_restart.h +++ b/src/feedback/EAGLE/feedback_restart.h @@ -20,6 +20,8 @@ #define SWIFT_EAGLE_FEEDBACK_RESTART_H #include "feedback.h" +#include "imf.h" +#include "yield_tables.h" /** * @brief Zero pointers in yield_table structs @@ -45,7 +47,7 @@ void zero_yield_table_pointers(struct yield_table table){ * @param feedback the #feedback_props structure * @param cosmo #cosmology structure */ -void feedback_restore_tables(struct feedback_props *feedback) { +void feedback_restore_tables(struct feedback_props *fp) { /* Initialise the IMF ------------------------------------------------- */ @@ -61,10 +63,6 @@ void feedback_restore_tables(struct feedback_props *feedback) { /* Initialise the yields ---------------------------------------------- */ - /* Read yield table filepath */ - parser_get_param_string(params, "EAGLEFeedback:filename", - fp->yield_table_path); - /* Allocate yield tables */ allocate_yield_tables(fp); @@ -128,6 +126,7 @@ void feedback_struct_dump(const struct feedback_props *feedback, feedback_copy.imf = NULL; feedback_copy.imf_mass_bin = NULL; feedback_copy.imf_mass_bin_log10 = NULL; + feedback_copy.num_SNII_per_msun = 0; restart_write_blocks((void *)&feedback_copy, sizeof(struct feedback_props), 1, stream, -- GitLab From c679fbaa854796e9652ae224676677470d853b26 Mon Sep 17 00:00:00 2001 From: Alexei Borissov Date: Wed, 24 Apr 2019 14:31:07 +0100 Subject: [PATCH 4/9] removed debugging print statements --- src/engine.c | 3 --- src/engine_drift.c | 3 --- 2 files changed, 6 deletions(-) diff --git a/src/engine.c b/src/engine.c index 635d4c996..8b86df623 100644 --- a/src/engine.c +++ b/src/engine.c @@ -6114,9 +6114,6 @@ void engine_struct_restore(struct engine *e, FILE *stream) { space_struct_restore(s, stream); e->s = s; s->e = e; - // debugging print statements - //message("space spart id %llu",s->sparts[0].id); - //message("cells_top spart id %llu",s->cells_top->stars.parts[0].id); struct unit_system *us = (struct unit_system *)malloc(sizeof(struct unit_system)); diff --git a/src/engine_drift.c b/src/engine_drift.c index c90a15b56..fee9ed1f1 100644 --- a/src/engine_drift.c +++ b/src/engine_drift.c @@ -167,9 +167,6 @@ void engine_do_drift_all_spart_mapper(void *map_data, int num_elements, if (c->nodeID == e->nodeID) { /* Drift all the particles */ - // debugging print statements - //message("space spart id %llu", s->sparts[0].id); - //message("cell spart id %llu", c->stars.parts[0].id); cell_drift_spart(c, e, /* force the drift=*/1); } } -- GitLab From 7e96435646f4ca50a491f1b52acd2fd6fdde235f Mon Sep 17 00:00:00 2001 From: Alexei Borissov Date: Thu, 25 Apr 2019 20:11:06 +0100 Subject: [PATCH 5/9] check that feedback_props are restored correctly --- src/feedback/EAGLE/feedback.c | 112 ++++++++++++++++++++++++++ src/feedback/EAGLE/feedback.h | 2 + src/feedback/EAGLE/feedback_restart.h | 5 ++ 3 files changed, 119 insertions(+) diff --git a/src/feedback/EAGLE/feedback.c b/src/feedback/EAGLE/feedback.c index ba7b708a4..9b7dc1e45 100644 --- a/src/feedback/EAGLE/feedback.c +++ b/src/feedback/EAGLE/feedback.c @@ -996,3 +996,115 @@ void feedback_props_init(struct feedback_props* fp, message("initialized stellar feedback"); } +void print_array_double(FILE *file, double *array, int array_size){ + for (int i = 0; i < array_size; i++) fprintf(file,"%.5e\n",array[i]); +} + +void print_array_float(FILE *file, float *array, int array_size){ + for (int i = 0; i < array_size; i++) fprintf(file,"%.5e\n",array[i]); +} + +void print_array_string(FILE *file, char **array, int array_size){ + for (int i = 0; i < array_size; i++) fprintf(file,"%s\n",array[i]); +} + +void print_feedback_struct(const struct feedback_props *fp){ + FILE *feedback_output; + feedback_output = fopen("feedback_props_out.log","w"); + + fprintf(feedback_output,"switch constants--------------------\n"); + fprintf(feedback_output,"%d\n",fp->with_AGB_enrichment); + fprintf(feedback_output,"%d\n",fp->with_SNII_enrichment); + fprintf(feedback_output,"%d\n",fp->with_SNIa_enrichment); + fprintf(feedback_output,"%d\n",fp->with_SNII_feedback); + fprintf(feedback_output,"%d\n",fp->with_SNIa_feedback); + + fprintf(feedback_output,"yield tables AGB--------------------\n"); + print_array_double(feedback_output,fp->yield_AGB.mass,eagle_feedback_AGB_N_masses); + print_array_double(feedback_output,fp->yield_AGB.metallicity,eagle_feedback_AGB_N_metals); + print_array_double(feedback_output,fp->yield_AGB.yield_IMF_resampled,eagle_feedback_AGB_N_metals*eagle_feedback_N_imf_bins*chemistry_element_count); + print_array_double(feedback_output,fp->yield_AGB.yield,eagle_feedback_AGB_N_metals * eagle_feedback_AGB_N_masses * eagle_feedback_AGB_N_elements ); + print_array_double(feedback_output,fp->yield_AGB.ejecta_IMF_resampled,eagle_feedback_AGB_N_metals * eagle_feedback_N_imf_bins); + print_array_double(feedback_output,fp->yield_AGB.ejecta, eagle_feedback_AGB_N_metals * eagle_feedback_AGB_N_masses); + print_array_double(feedback_output,fp->yield_AGB.total_metals_IMF_resampled,eagle_feedback_AGB_N_metals * eagle_feedback_N_imf_bins); + print_array_double(feedback_output,fp->yield_AGB.total_metals,eagle_feedback_AGB_N_metals * eagle_feedback_AGB_N_masses); + + fprintf(feedback_output,"yield tables SNIa--------------------\n"); + print_array_double(feedback_output,fp->yield_SNII.mass,eagle_feedback_SNII_N_masses); + print_array_double(feedback_output,fp->yield_SNII.metallicity,eagle_feedback_SNII_N_metals); + print_array_double(feedback_output,fp->yield_SNII.yield_IMF_resampled,eagle_feedback_SNII_N_metals*eagle_feedback_N_imf_bins*chemistry_element_count); + print_array_double(feedback_output,fp->yield_SNII.yield,eagle_feedback_SNII_N_metals * eagle_feedback_SNII_N_masses * eagle_feedback_SNII_N_elements ); + print_array_double(feedback_output,fp->yield_SNII.ejecta_IMF_resampled,eagle_feedback_SNII_N_metals * eagle_feedback_N_imf_bins); + print_array_double(feedback_output,fp->yield_SNII.ejecta, eagle_feedback_SNII_N_metals * eagle_feedback_SNII_N_masses); + print_array_double(feedback_output,fp->yield_SNII.total_metals_IMF_resampled,eagle_feedback_SNII_N_metals * eagle_feedback_N_imf_bins); + print_array_double(feedback_output,fp->yield_SNII.total_metals,eagle_feedback_SNII_N_metals * eagle_feedback_SNII_N_masses); + + fprintf(feedback_output,"yield tables SNIa--------------------\n"); + print_array_double(feedback_output,fp->yield_SNIa_IMF_resampled, eagle_feedback_SNIa_N_elements); + fprintf(feedback_output,"%.5e\n",fp->yield_SNIa_total_metals_IMF_resampled); + print_array_double(feedback_output, fp->yields_SNIa, eagle_feedback_SNIa_N_elements); + + fprintf(feedback_output,"element names--------------------\n"); + print_array_string(feedback_output, fp->SNIa_element_names, eagle_feedback_SNIa_N_elements); + print_array_string(feedback_output, fp->SNII_element_names, eagle_feedback_SNII_N_elements); + print_array_string(feedback_output, fp->AGB_element_names, eagle_feedback_AGB_N_elements); + + fprintf(feedback_output,"mass bins--------------------\n"); + print_array_double(feedback_output, fp->yield_mass_bins, eagle_feedback_N_imf_bins); + + fprintf(feedback_output,"yield table path--------------------\n"); + fprintf(feedback_output,"%s\n",fp->yield_table_path); + + fprintf(feedback_output,"lifetime table--------------------\n"); + print_array_double(feedback_output, fp->lifetimes.mass, eagle_feedback_lifetime_N_masses); + print_array_double(feedback_output, fp->lifetimes.metallicity, eagle_feedback_lifetime_N_metals); + for (int i = 0; i < eagle_feedback_lifetime_N_metals; i++) + print_array_double(feedback_output, fp->lifetimes.dyingtime[i], eagle_feedback_lifetime_N_masses); + + fprintf(feedback_output,"constants--------------------\n"); + for (int i = 0; i < chemistry_element_count; i++) + fprintf(feedback_output, "%.5e\n", fp->SNII_yield_factor[i]); + fprintf(feedback_output, "%.5e\n", fp->SNIa_efficiency); + fprintf(feedback_output, "%.5e\n", fp->SNIa_timescale_Gyr); + fprintf(feedback_output, "%.5e\n", fp->SNIa_timescale_Gyr_inv); + fprintf(feedback_output, "%.5e\n", fp->log10_SNIa_max_mass_msun); + fprintf(feedback_output, "%.5e\n", fp->E_SNIa_cgs); + fprintf(feedback_output, "%.5e\n", fp->E_SNIa); + fprintf(feedback_output, "%.5e\n", fp->AGB_ejecta_specific_kinetic_energy); + fprintf(feedback_output, "%.5e\n", fp->mass_to_solar_mass); + fprintf(feedback_output, "%.5e\n", fp->solar_mass_to_mass); + fprintf(feedback_output, "%.5e\n", fp->rho_to_n_cgs); + fprintf(feedback_output, "%.5e\n", fp->temp_to_u_factor); + + fprintf(feedback_output,"imf--------------------\n"); + print_array_float(feedback_output, fp->imf, eagle_feedback_N_imf_bins); + + fprintf(feedback_output,"imf mass bin--------------------\n"); + print_array_float(feedback_output, fp->imf_mass_bin, eagle_feedback_N_imf_bins); + + fprintf(feedback_output,"imf mass bin log10--------------------\n"); + print_array_float(feedback_output, fp->imf_mass_bin_log10, eagle_feedback_N_imf_bins); + + fprintf(feedback_output,"imf mass bounds-------------------\n"); + fprintf(feedback_output, "%.5e\n", fp->imf_min_mass_msun); + fprintf(feedback_output, "%.5e\n", fp->imf_max_mass_msun); + fprintf(feedback_output, "%.5e\n", fp->log10_imf_min_mass_msun); + fprintf(feedback_output, "%.5e\n", fp->log10_imf_max_mass_msun); + + fprintf(feedback_output,"other constants-------------------\n"); + fprintf(feedback_output, "%.5e\n", fp->log10_SNII_min_mass_msun); + fprintf(feedback_output, "%.5e\n", fp->log10_SNII_max_mass_msun); + fprintf(feedback_output, "%.5e\n", fp->num_SNII_per_msun); + fprintf(feedback_output, "%.5e\n", fp->SNII_wind_delay); + fprintf(feedback_output, "%.5e\n", fp->SNe_deltaT_desired); + fprintf(feedback_output, "%.5e\n", fp->E_SNII_cgs); + fprintf(feedback_output, "%.5e\n", fp->E_SNII); + fprintf(feedback_output, "%.5e\n", fp->f_E_min); + fprintf(feedback_output, "%.5e\n", fp->f_E_max); + fprintf(feedback_output, "%.5e\n", fp->Z_0); + fprintf(feedback_output, "%.5e\n", fp->n_0_cgs); + fprintf(feedback_output, "%.5e\n", fp->n_n); + fprintf(feedback_output, "%.5e\n", fp->n_Z); + + fclose(feedback_output); +} diff --git a/src/feedback/EAGLE/feedback.h b/src/feedback/EAGLE/feedback.h index 14f6821b0..a35843f3e 100644 --- a/src/feedback/EAGLE/feedback.h +++ b/src/feedback/EAGLE/feedback.h @@ -172,4 +172,6 @@ __attribute__((always_inline)) INLINE static void feedback_evolve_spart( sp->mass -= sp->feedback_data.to_distribute.mass; } +void print_feedback_struct(const struct feedback_props *); + #endif /* SWIFT_FEEDBACK_EAGLE_H */ diff --git a/src/feedback/EAGLE/feedback_restart.h b/src/feedback/EAGLE/feedback_restart.h index 1eba27c05..7a6f53eae 100644 --- a/src/feedback/EAGLE/feedback_restart.h +++ b/src/feedback/EAGLE/feedback_restart.h @@ -95,6 +95,9 @@ void feedback_restore_tables(struct feedback_props *fp) { void feedback_struct_dump(const struct feedback_props *feedback, FILE *stream) { + // Write a file to check everything is the same after restoring (remove for production) + print_feedback_struct(feedback); + /* To make sure everything is restored correctly, we zero all the pointers to tables. If they are not restored correctly, we would crash after restart on the first call to the feedback routines. Helps debugging. */ @@ -148,6 +151,8 @@ void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) { stream, NULL, "feedback function"); feedback_restore_tables(feedback); + // Write a file to check everything is the same after restoring (remove for production) + print_feedback_struct(feedback); } #endif /* SWIFT_EAGLE_FEEDBACK_RESTART_H */ -- GitLab From e91b44ed8fb2b43a22e2ae0a82efd87a5fdc5119 Mon Sep 17 00:00:00 2001 From: Alexei Borissov Date: Thu, 25 Apr 2019 20:15:34 +0100 Subject: [PATCH 6/9] remove feedback restart printing check --- src/feedback/EAGLE/feedback.c | 113 -------------------------- src/feedback/EAGLE/feedback.h | 2 - src/feedback/EAGLE/feedback_restart.h | 5 -- 3 files changed, 120 deletions(-) diff --git a/src/feedback/EAGLE/feedback.c b/src/feedback/EAGLE/feedback.c index 9b7dc1e45..76370156e 100644 --- a/src/feedback/EAGLE/feedback.c +++ b/src/feedback/EAGLE/feedback.c @@ -995,116 +995,3 @@ void feedback_props_init(struct feedback_props* fp, message("initialized stellar feedback"); } - -void print_array_double(FILE *file, double *array, int array_size){ - for (int i = 0; i < array_size; i++) fprintf(file,"%.5e\n",array[i]); -} - -void print_array_float(FILE *file, float *array, int array_size){ - for (int i = 0; i < array_size; i++) fprintf(file,"%.5e\n",array[i]); -} - -void print_array_string(FILE *file, char **array, int array_size){ - for (int i = 0; i < array_size; i++) fprintf(file,"%s\n",array[i]); -} - -void print_feedback_struct(const struct feedback_props *fp){ - FILE *feedback_output; - feedback_output = fopen("feedback_props_out.log","w"); - - fprintf(feedback_output,"switch constants--------------------\n"); - fprintf(feedback_output,"%d\n",fp->with_AGB_enrichment); - fprintf(feedback_output,"%d\n",fp->with_SNII_enrichment); - fprintf(feedback_output,"%d\n",fp->with_SNIa_enrichment); - fprintf(feedback_output,"%d\n",fp->with_SNII_feedback); - fprintf(feedback_output,"%d\n",fp->with_SNIa_feedback); - - fprintf(feedback_output,"yield tables AGB--------------------\n"); - print_array_double(feedback_output,fp->yield_AGB.mass,eagle_feedback_AGB_N_masses); - print_array_double(feedback_output,fp->yield_AGB.metallicity,eagle_feedback_AGB_N_metals); - print_array_double(feedback_output,fp->yield_AGB.yield_IMF_resampled,eagle_feedback_AGB_N_metals*eagle_feedback_N_imf_bins*chemistry_element_count); - print_array_double(feedback_output,fp->yield_AGB.yield,eagle_feedback_AGB_N_metals * eagle_feedback_AGB_N_masses * eagle_feedback_AGB_N_elements ); - print_array_double(feedback_output,fp->yield_AGB.ejecta_IMF_resampled,eagle_feedback_AGB_N_metals * eagle_feedback_N_imf_bins); - print_array_double(feedback_output,fp->yield_AGB.ejecta, eagle_feedback_AGB_N_metals * eagle_feedback_AGB_N_masses); - print_array_double(feedback_output,fp->yield_AGB.total_metals_IMF_resampled,eagle_feedback_AGB_N_metals * eagle_feedback_N_imf_bins); - print_array_double(feedback_output,fp->yield_AGB.total_metals,eagle_feedback_AGB_N_metals * eagle_feedback_AGB_N_masses); - - fprintf(feedback_output,"yield tables SNIa--------------------\n"); - print_array_double(feedback_output,fp->yield_SNII.mass,eagle_feedback_SNII_N_masses); - print_array_double(feedback_output,fp->yield_SNII.metallicity,eagle_feedback_SNII_N_metals); - print_array_double(feedback_output,fp->yield_SNII.yield_IMF_resampled,eagle_feedback_SNII_N_metals*eagle_feedback_N_imf_bins*chemistry_element_count); - print_array_double(feedback_output,fp->yield_SNII.yield,eagle_feedback_SNII_N_metals * eagle_feedback_SNII_N_masses * eagle_feedback_SNII_N_elements ); - print_array_double(feedback_output,fp->yield_SNII.ejecta_IMF_resampled,eagle_feedback_SNII_N_metals * eagle_feedback_N_imf_bins); - print_array_double(feedback_output,fp->yield_SNII.ejecta, eagle_feedback_SNII_N_metals * eagle_feedback_SNII_N_masses); - print_array_double(feedback_output,fp->yield_SNII.total_metals_IMF_resampled,eagle_feedback_SNII_N_metals * eagle_feedback_N_imf_bins); - print_array_double(feedback_output,fp->yield_SNII.total_metals,eagle_feedback_SNII_N_metals * eagle_feedback_SNII_N_masses); - - fprintf(feedback_output,"yield tables SNIa--------------------\n"); - print_array_double(feedback_output,fp->yield_SNIa_IMF_resampled, eagle_feedback_SNIa_N_elements); - fprintf(feedback_output,"%.5e\n",fp->yield_SNIa_total_metals_IMF_resampled); - print_array_double(feedback_output, fp->yields_SNIa, eagle_feedback_SNIa_N_elements); - - fprintf(feedback_output,"element names--------------------\n"); - print_array_string(feedback_output, fp->SNIa_element_names, eagle_feedback_SNIa_N_elements); - print_array_string(feedback_output, fp->SNII_element_names, eagle_feedback_SNII_N_elements); - print_array_string(feedback_output, fp->AGB_element_names, eagle_feedback_AGB_N_elements); - - fprintf(feedback_output,"mass bins--------------------\n"); - print_array_double(feedback_output, fp->yield_mass_bins, eagle_feedback_N_imf_bins); - - fprintf(feedback_output,"yield table path--------------------\n"); - fprintf(feedback_output,"%s\n",fp->yield_table_path); - - fprintf(feedback_output,"lifetime table--------------------\n"); - print_array_double(feedback_output, fp->lifetimes.mass, eagle_feedback_lifetime_N_masses); - print_array_double(feedback_output, fp->lifetimes.metallicity, eagle_feedback_lifetime_N_metals); - for (int i = 0; i < eagle_feedback_lifetime_N_metals; i++) - print_array_double(feedback_output, fp->lifetimes.dyingtime[i], eagle_feedback_lifetime_N_masses); - - fprintf(feedback_output,"constants--------------------\n"); - for (int i = 0; i < chemistry_element_count; i++) - fprintf(feedback_output, "%.5e\n", fp->SNII_yield_factor[i]); - fprintf(feedback_output, "%.5e\n", fp->SNIa_efficiency); - fprintf(feedback_output, "%.5e\n", fp->SNIa_timescale_Gyr); - fprintf(feedback_output, "%.5e\n", fp->SNIa_timescale_Gyr_inv); - fprintf(feedback_output, "%.5e\n", fp->log10_SNIa_max_mass_msun); - fprintf(feedback_output, "%.5e\n", fp->E_SNIa_cgs); - fprintf(feedback_output, "%.5e\n", fp->E_SNIa); - fprintf(feedback_output, "%.5e\n", fp->AGB_ejecta_specific_kinetic_energy); - fprintf(feedback_output, "%.5e\n", fp->mass_to_solar_mass); - fprintf(feedback_output, "%.5e\n", fp->solar_mass_to_mass); - fprintf(feedback_output, "%.5e\n", fp->rho_to_n_cgs); - fprintf(feedback_output, "%.5e\n", fp->temp_to_u_factor); - - fprintf(feedback_output,"imf--------------------\n"); - print_array_float(feedback_output, fp->imf, eagle_feedback_N_imf_bins); - - fprintf(feedback_output,"imf mass bin--------------------\n"); - print_array_float(feedback_output, fp->imf_mass_bin, eagle_feedback_N_imf_bins); - - fprintf(feedback_output,"imf mass bin log10--------------------\n"); - print_array_float(feedback_output, fp->imf_mass_bin_log10, eagle_feedback_N_imf_bins); - - fprintf(feedback_output,"imf mass bounds-------------------\n"); - fprintf(feedback_output, "%.5e\n", fp->imf_min_mass_msun); - fprintf(feedback_output, "%.5e\n", fp->imf_max_mass_msun); - fprintf(feedback_output, "%.5e\n", fp->log10_imf_min_mass_msun); - fprintf(feedback_output, "%.5e\n", fp->log10_imf_max_mass_msun); - - fprintf(feedback_output,"other constants-------------------\n"); - fprintf(feedback_output, "%.5e\n", fp->log10_SNII_min_mass_msun); - fprintf(feedback_output, "%.5e\n", fp->log10_SNII_max_mass_msun); - fprintf(feedback_output, "%.5e\n", fp->num_SNII_per_msun); - fprintf(feedback_output, "%.5e\n", fp->SNII_wind_delay); - fprintf(feedback_output, "%.5e\n", fp->SNe_deltaT_desired); - fprintf(feedback_output, "%.5e\n", fp->E_SNII_cgs); - fprintf(feedback_output, "%.5e\n", fp->E_SNII); - fprintf(feedback_output, "%.5e\n", fp->f_E_min); - fprintf(feedback_output, "%.5e\n", fp->f_E_max); - fprintf(feedback_output, "%.5e\n", fp->Z_0); - fprintf(feedback_output, "%.5e\n", fp->n_0_cgs); - fprintf(feedback_output, "%.5e\n", fp->n_n); - fprintf(feedback_output, "%.5e\n", fp->n_Z); - - fclose(feedback_output); -} diff --git a/src/feedback/EAGLE/feedback.h b/src/feedback/EAGLE/feedback.h index a35843f3e..14f6821b0 100644 --- a/src/feedback/EAGLE/feedback.h +++ b/src/feedback/EAGLE/feedback.h @@ -172,6 +172,4 @@ __attribute__((always_inline)) INLINE static void feedback_evolve_spart( sp->mass -= sp->feedback_data.to_distribute.mass; } -void print_feedback_struct(const struct feedback_props *); - #endif /* SWIFT_FEEDBACK_EAGLE_H */ diff --git a/src/feedback/EAGLE/feedback_restart.h b/src/feedback/EAGLE/feedback_restart.h index 7a6f53eae..1eba27c05 100644 --- a/src/feedback/EAGLE/feedback_restart.h +++ b/src/feedback/EAGLE/feedback_restart.h @@ -95,9 +95,6 @@ void feedback_restore_tables(struct feedback_props *fp) { void feedback_struct_dump(const struct feedback_props *feedback, FILE *stream) { - // Write a file to check everything is the same after restoring (remove for production) - print_feedback_struct(feedback); - /* To make sure everything is restored correctly, we zero all the pointers to tables. If they are not restored correctly, we would crash after restart on the first call to the feedback routines. Helps debugging. */ @@ -151,8 +148,6 @@ void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) { stream, NULL, "feedback function"); feedback_restore_tables(feedback); - // Write a file to check everything is the same after restoring (remove for production) - print_feedback_struct(feedback); } #endif /* SWIFT_EAGLE_FEEDBACK_RESTART_H */ -- GitLab From 050b205aba7e7f5500780c99c2e5564b5d4a5127 Mon Sep 17 00:00:00 2001 From: Alexei Borissov Date: Fri, 26 Apr 2019 13:07:10 +0100 Subject: [PATCH 7/9] Move location of restart functions --- src/engine.c | 2 +- src/feedback/EAGLE/feedback.c | 127 +++++++++++++++++++++ src/feedback/EAGLE/feedback.h | 9 ++ src/feedback/EAGLE/feedback_restart.h | 153 -------------------------- src/feedback/none/feedback.h | 35 ++++++ src/feedback/none/feedback_restart.h | 59 ---------- src/feedback_restart.h | 34 ------ 7 files changed, 172 insertions(+), 247 deletions(-) delete mode 100644 src/feedback/EAGLE/feedback_restart.h delete mode 100644 src/feedback/none/feedback_restart.h delete mode 100644 src/feedback_restart.h diff --git a/src/engine.c b/src/engine.c index 8b86df623..d561e66af 100644 --- a/src/engine.c +++ b/src/engine.c @@ -88,7 +88,7 @@ #include "star_formation.h" #include "star_formation_logger.h" #include "star_formation_logger_struct.h" -#include "feedback_restart.h" +#include "feedback.h" #include "stars_io.h" #include "statistics.h" #include "timers.h" diff --git a/src/feedback/EAGLE/feedback.c b/src/feedback/EAGLE/feedback.c index 76370156e..e37563a24 100644 --- a/src/feedback/EAGLE/feedback.c +++ b/src/feedback/EAGLE/feedback.c @@ -995,3 +995,130 @@ void feedback_props_init(struct feedback_props* fp, message("initialized stellar feedback"); } + +/** + * @brief Zero pointers in yield_table structs + * + * @param table yield_table struct in which pointers to tables + * set to NULL + */ +void zero_yield_table_pointers(struct yield_table table){ + table.mass = NULL; + table.metallicity = NULL; + table.yield_IMF_resampled = NULL; + table.yield = NULL; + table.ejecta_IMF_resampled = NULL; + table.ejecta = NULL; + table.total_metals_IMF_resampled = NULL; + table.total_metals = NULL; +} + +/** + * @brief Restore feedback tables (if applicable) after + * restart + * + * @param feedback the #feedback_props structure + * @param cosmo #cosmology structure + */ +void feedback_restore_tables(struct feedback_props *fp) { + + /* Initialise the IMF ------------------------------------------------- */ + + init_imf(fp); + + /* Calculate number of type II SN per unit solar mass based on our choice + * of IMF and integration limits for type II SNe. + * Note: No weighting by yields here. */ + fp->num_SNII_per_msun = + integrate_imf(fp->log10_SNII_min_mass_msun, fp->log10_SNII_max_mass_msun, + eagle_imf_integration_no_weight, + /*(stellar_yields=)*/ NULL, fp); + + /* Initialise the yields ---------------------------------------------- */ + + /* Allocate yield tables */ + allocate_yield_tables(fp); + + /* Read the tables */ + read_yield_tables(fp); + + /* Set yield_mass_bins array */ + const float imf_log10_mass_bin_size = + (fp->log10_imf_max_mass_msun - fp->log10_imf_min_mass_msun) / + (eagle_feedback_N_imf_bins - 1); + + for (int i = 0; i < eagle_feedback_N_imf_bins; i++) + fp->yield_mass_bins[i] = + imf_log10_mass_bin_size * i + fp->log10_imf_min_mass_msun; + + /* Resample yields from mass bins used in tables to mass bins used in IMF */ + compute_yields(fp); + + /* Resample ejecta contribution to enrichment from mass bins used in tables to + * mass bins used in IMF */ + compute_ejecta(fp); +} + +/** + * @brief Write a feedback struct to the given FILE as a stream of bytes. + * + * @param feedback the struct + * @param stream the file stream + */ +void feedback_struct_dump(const struct feedback_props *feedback, + FILE *stream) { + + /* To make sure everything is restored correctly, we zero all the pointers to + tables. If they are not restored correctly, we would crash after restart on + the first call to the feedback routines. Helps debugging. */ + struct feedback_props feedback_copy = *feedback; + + /* zero AGB and SNII table pointers */ + zero_yield_table_pointers(feedback_copy.yield_AGB); + zero_yield_table_pointers(feedback_copy.yield_SNII); + + /* zero SNIa table pointers */ + feedback_copy.yield_SNIa_IMF_resampled = NULL; + feedback_copy.yields_SNIa = NULL; + feedback_copy.yield_SNIa_total_metals_IMF_resampled = 0; + + /* zero element name tables */ + feedback_copy.SNIa_element_names = NULL; + feedback_copy.SNII_element_names = NULL; + feedback_copy.AGB_element_names = NULL; + + /* zero mass bins table */ + feedback_copy.yield_mass_bins = NULL; + + /* zero lifetime tracks */ + feedback_copy.lifetimes.mass = NULL; + feedback_copy.lifetimes.metallicity = NULL; + feedback_copy.lifetimes.dyingtime = NULL; + + /* zero IMF tables */ + feedback_copy.imf = NULL; + feedback_copy.imf_mass_bin = NULL; + feedback_copy.imf_mass_bin_log10 = NULL; + feedback_copy.num_SNII_per_msun = 0; + + restart_write_blocks((void *)&feedback_copy, + sizeof(struct feedback_props), 1, stream, + "feedback", "feedback function"); +} + +/** + * @brief Restore a hydro_props struct from the given FILE as a stream of + * bytes. + * + * Read the structure from the stream and restore the feedback tables by + * re-reading them. + * + * @param feedback the struct + * @param stream the file stream + */ +void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) { + restart_read_blocks((void *)feedback, sizeof(struct feedback_props), 1, + stream, NULL, "feedback function"); + + feedback_restore_tables(feedback); +} diff --git a/src/feedback/EAGLE/feedback.h b/src/feedback/EAGLE/feedback.h index 14f6821b0..1530b8aa7 100644 --- a/src/feedback/EAGLE/feedback.h +++ b/src/feedback/EAGLE/feedback.h @@ -172,4 +172,13 @@ __attribute__((always_inline)) INLINE static void feedback_evolve_spart( sp->mass -= sp->feedback_data.to_distribute.mass; } +void zero_yield_table_pointers(struct yield_table table); + +void feedback_restore_tables(struct feedback_props *fp); + +void feedback_struct_dump(const struct feedback_props *feedback, + FILE *stream); + +void feedback_struct_restore(struct feedback_props *feedback, FILE *stream); + #endif /* SWIFT_FEEDBACK_EAGLE_H */ diff --git a/src/feedback/EAGLE/feedback_restart.h b/src/feedback/EAGLE/feedback_restart.h deleted file mode 100644 index 1eba27c05..000000000 --- a/src/feedback/EAGLE/feedback_restart.h +++ /dev/null @@ -1,153 +0,0 @@ -/******************************************************************************* - * This file is part of SWIFT. - * Coypright (c) 2018 Matthieu Schaller (schaller@strw.leidenuniv.nl) - * - * 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_EAGLE_FEEDBACK_RESTART_H -#define SWIFT_EAGLE_FEEDBACK_RESTART_H - -#include "feedback.h" -#include "imf.h" -#include "yield_tables.h" - -/** - * @brief Zero pointers in yield_table structs - * - * @param table yield_table struct in which pointers to tables - * set to NULL - */ -void zero_yield_table_pointers(struct yield_table table){ - table.mass = NULL; - table.metallicity = NULL; - table.yield_IMF_resampled = NULL; - table.yield = NULL; - table.ejecta_IMF_resampled = NULL; - table.ejecta = NULL; - table.total_metals_IMF_resampled = NULL; - table.total_metals = NULL; -} - -/** - * @brief Restore feedback tables (if applicable) after - * restart - * - * @param feedback the #feedback_props structure - * @param cosmo #cosmology structure - */ -void feedback_restore_tables(struct feedback_props *fp) { - - /* Initialise the IMF ------------------------------------------------- */ - - init_imf(fp); - - /* Calculate number of type II SN per unit solar mass based on our choice - * of IMF and integration limits for type II SNe. - * Note: No weighting by yields here. */ - fp->num_SNII_per_msun = - integrate_imf(fp->log10_SNII_min_mass_msun, fp->log10_SNII_max_mass_msun, - eagle_imf_integration_no_weight, - /*(stellar_yields=)*/ NULL, fp); - - /* Initialise the yields ---------------------------------------------- */ - - /* Allocate yield tables */ - allocate_yield_tables(fp); - - /* Read the tables */ - read_yield_tables(fp); - - /* Set yield_mass_bins array */ - const float imf_log10_mass_bin_size = - (fp->log10_imf_max_mass_msun - fp->log10_imf_min_mass_msun) / - (eagle_feedback_N_imf_bins - 1); - - for (int i = 0; i < eagle_feedback_N_imf_bins; i++) - fp->yield_mass_bins[i] = - imf_log10_mass_bin_size * i + fp->log10_imf_min_mass_msun; - - /* Resample yields from mass bins used in tables to mass bins used in IMF */ - compute_yields(fp); - - /* Resample ejecta contribution to enrichment from mass bins used in tables to - * mass bins used in IMF */ - compute_ejecta(fp); -} - -/** - * @brief Write a feedback struct to the given FILE as a stream of bytes. - * - * @param feedback the struct - * @param stream the file stream - */ -void feedback_struct_dump(const struct feedback_props *feedback, - FILE *stream) { - - /* To make sure everything is restored correctly, we zero all the pointers to - tables. If they are not restored correctly, we would crash after restart on - the first call to the feedback routines. Helps debugging. */ - struct feedback_props feedback_copy = *feedback; - - /* zero AGB and SNII table pointers */ - zero_yield_table_pointers(feedback_copy.yield_AGB); - zero_yield_table_pointers(feedback_copy.yield_SNII); - - /* zero SNIa table pointers */ - feedback_copy.yield_SNIa_IMF_resampled = NULL; - feedback_copy.yields_SNIa = NULL; - feedback_copy.yield_SNIa_total_metals_IMF_resampled = 0; - - /* zero element name tables */ - feedback_copy.SNIa_element_names = NULL; - feedback_copy.SNII_element_names = NULL; - feedback_copy.AGB_element_names = NULL; - - /* zero mass bins table */ - feedback_copy.yield_mass_bins = NULL; - - /* zero lifetime tracks */ - feedback_copy.lifetimes.mass = NULL; - feedback_copy.lifetimes.metallicity = NULL; - feedback_copy.lifetimes.dyingtime = NULL; - - /* zero IMF tables */ - feedback_copy.imf = NULL; - feedback_copy.imf_mass_bin = NULL; - feedback_copy.imf_mass_bin_log10 = NULL; - feedback_copy.num_SNII_per_msun = 0; - - restart_write_blocks((void *)&feedback_copy, - sizeof(struct feedback_props), 1, stream, - "feedback", "feedback function"); -} - -/** - * @brief Restore a hydro_props struct from the given FILE as a stream of - * bytes. - * - * Read the structure from the stream and restore the feedback tables by - * re-reading them. - * - * @param feedback the struct - * @param stream the file stream - */ -void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) { - restart_read_blocks((void *)feedback, sizeof(struct feedback_props), 1, - stream, NULL, "feedback function"); - - feedback_restore_tables(feedback); -} - -#endif /* SWIFT_EAGLE_FEEDBACK_RESTART_H */ diff --git a/src/feedback/none/feedback.h b/src/feedback/none/feedback.h index 1085e4555..0cd3ba08f 100644 --- a/src/feedback/none/feedback.h +++ b/src/feedback/none/feedback.h @@ -114,4 +114,39 @@ __attribute__((always_inline)) INLINE static void feedback_evolve_spart( const struct cosmology* cosmo, const struct unit_system* us, const double star_age_beg_step, const double dt) {} +/** + * @brief Restore feedback tables (if applicable) after + * restart + * + * @param feedback the #feedback_props structure + * @param cosmo #cosmology structure + */ +inline void feedback_restore_tables(struct feedback_props *feedback) { + +} + +/** + * @brief Write a feedback struct to the given FILE as a stream of bytes. + * + * @param feedback the struct + * @param stream the file stream + */ +inline void feedback_struct_dump(const struct feedback_props *feedback, + FILE *stream) { +} + +/** + * @brief Restore a hydro_props struct from the given FILE as a stream of + * bytes. + * + * Read the structure from the stream and restore the feedback tables by + * re-reading them. + * + * @param feedback the struct + * @param stream the file stream + * @param cosmo #cosmology structure + */ +inline void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) { +} + #endif /* SWIFT_FEEDBACK_NONE_H */ diff --git a/src/feedback/none/feedback_restart.h b/src/feedback/none/feedback_restart.h deleted file mode 100644 index b69170ac0..000000000 --- a/src/feedback/none/feedback_restart.h +++ /dev/null @@ -1,59 +0,0 @@ -/******************************************************************************* - * This file is part of SWIFT. - * Coypright (c) 2018 Matthieu Schaller (schaller@strw.leidenuniv.nl) - * - * 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_EAGLE_FEEDBACK_RESTART_H -#define SWIFT_EAGLE_FEEDBACK_RESTART_H - -#include "feedback.h" - -/** - * @brief Restore feedback tables (if applicable) after - * restart - * - * @param feedback the #feedback_props structure - * @param cosmo #cosmology structure - */ -void feedback_restore_tables(struct feedback_props *feedback) { - -} - -/** - * @brief Write a feedback struct to the given FILE as a stream of bytes. - * - * @param feedback the struct - * @param stream the file stream - */ -void feedback_struct_dump(const struct feedback_props *feedback, - FILE *stream) { -} - -/** - * @brief Restore a hydro_props struct from the given FILE as a stream of - * bytes. - * - * Read the structure from the stream and restore the feedback tables by - * re-reading them. - * - * @param feedback the struct - * @param stream the file stream - * @param cosmo #cosmology structure - */ -void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) { -} - -#endif /* SWIFT_EAGLE_FEEDBACK_RESTART_H */ diff --git a/src/feedback_restart.h b/src/feedback_restart.h deleted file mode 100644 index 86072b3ae..000000000 --- a/src/feedback_restart.h +++ /dev/null @@ -1,34 +0,0 @@ -/******************************************************************************* - * This file is part of SWIFT. - * Coypright (c) 2018 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_FEEDBACK_RESTART_H -#define SWIFT_FEEDBACK_RESTART_H - -/* Config parameters. */ -#include "../config.h" - -/* Select the correct feedback model */ -#if defined(FEEDBACK_NONE) -#include "./feedback/none/feedback_restart.h" -#elif defined(FEEDBACK_EAGLE) -#include "./feedback/EAGLE/feedback_restart.h" -#else -#error "Invalid choice of feedback model" -#endif - -#endif /* SWIFT_FEEDBACK_RESTART_H */ -- GitLab From 676e60c51dc50a4acc90278dd9ae8e8f8a9838bc Mon Sep 17 00:00:00 2001 From: Alexei Borissov Date: Mon, 29 Apr 2019 10:11:05 +0100 Subject: [PATCH 8/9] small code corrections --- src/feedback/EAGLE/feedback.c | 13 ------------- src/feedback/EAGLE/feedback.h | 6 +----- src/feedback/none/feedback.h | 17 ++--------------- 3 files changed, 3 insertions(+), 33 deletions(-) diff --git a/src/feedback/EAGLE/feedback.c b/src/feedback/EAGLE/feedback.c index e37563a24..6bbc08a5e 100644 --- a/src/feedback/EAGLE/feedback.c +++ b/src/feedback/EAGLE/feedback.c @@ -1022,20 +1022,8 @@ void zero_yield_table_pointers(struct yield_table table){ */ void feedback_restore_tables(struct feedback_props *fp) { - /* Initialise the IMF ------------------------------------------------- */ - init_imf(fp); - /* Calculate number of type II SN per unit solar mass based on our choice - * of IMF and integration limits for type II SNe. - * Note: No weighting by yields here. */ - fp->num_SNII_per_msun = - integrate_imf(fp->log10_SNII_min_mass_msun, fp->log10_SNII_max_mass_msun, - eagle_imf_integration_no_weight, - /*(stellar_yields=)*/ NULL, fp); - - /* Initialise the yields ---------------------------------------------- */ - /* Allocate yield tables */ allocate_yield_tables(fp); @@ -1099,7 +1087,6 @@ void feedback_struct_dump(const struct feedback_props *feedback, feedback_copy.imf = NULL; feedback_copy.imf_mass_bin = NULL; feedback_copy.imf_mass_bin_log10 = NULL; - feedback_copy.num_SNII_per_msun = 0; restart_write_blocks((void *)&feedback_copy, sizeof(struct feedback_props), 1, stream, diff --git a/src/feedback/EAGLE/feedback.h b/src/feedback/EAGLE/feedback.h index 1530b8aa7..bc1a41e9a 100644 --- a/src/feedback/EAGLE/feedback.h +++ b/src/feedback/EAGLE/feedback.h @@ -160,7 +160,7 @@ __attribute__((always_inline)) INLINE static void feedback_evolve_spart( const double star_age_beg_step, const double dt) { #ifdef SWIFT_DEBUG_CHECKS - if (sp->birth_time == -1.) error("Evolving a star particle that shoul not!"); + if (sp->birth_time == -1.) error("Evolving a star particle that should not!"); #endif /* Compute amount of enrichment and feedback that needs to be done in this @@ -172,10 +172,6 @@ __attribute__((always_inline)) INLINE static void feedback_evolve_spart( sp->mass -= sp->feedback_data.to_distribute.mass; } -void zero_yield_table_pointers(struct yield_table table); - -void feedback_restore_tables(struct feedback_props *fp); - void feedback_struct_dump(const struct feedback_props *feedback, FILE *stream); diff --git a/src/feedback/none/feedback.h b/src/feedback/none/feedback.h index 0cd3ba08f..903bb2b9f 100644 --- a/src/feedback/none/feedback.h +++ b/src/feedback/none/feedback.h @@ -114,17 +114,6 @@ __attribute__((always_inline)) INLINE static void feedback_evolve_spart( const struct cosmology* cosmo, const struct unit_system* us, const double star_age_beg_step, const double dt) {} -/** - * @brief Restore feedback tables (if applicable) after - * restart - * - * @param feedback the #feedback_props structure - * @param cosmo #cosmology structure - */ -inline void feedback_restore_tables(struct feedback_props *feedback) { - -} - /** * @brief Write a feedback struct to the given FILE as a stream of bytes. * @@ -132,8 +121,7 @@ inline void feedback_restore_tables(struct feedback_props *feedback) { * @param stream the file stream */ inline void feedback_struct_dump(const struct feedback_props *feedback, - FILE *stream) { -} + FILE *stream) {} /** * @brief Restore a hydro_props struct from the given FILE as a stream of @@ -146,7 +134,6 @@ inline void feedback_struct_dump(const struct feedback_props *feedback, * @param stream the file stream * @param cosmo #cosmology structure */ -inline void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) { -} +inline void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) {} #endif /* SWIFT_FEEDBACK_NONE_H */ -- GitLab From 5859be62a8c54e1a7407b82ddff48687a3b64aca Mon Sep 17 00:00:00 2001 From: Matthieu Schaller Date: Mon, 29 Apr 2019 11:49:43 +0200 Subject: [PATCH 9/9] Applied code formatting tool. --- src/engine.c | 2 +- src/feedback/EAGLE/feedback.c | 20 +++++++++----------- src/feedback/EAGLE/feedback.h | 5 ++--- src/feedback/none/feedback.h | 7 ++++--- 4 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/engine.c b/src/engine.c index d561e66af..d418515db 100644 --- a/src/engine.c +++ b/src/engine.c @@ -65,6 +65,7 @@ #include "entropy_floor.h" #include "equation_of_state.h" #include "error.h" +#include "feedback.h" #include "gravity.h" #include "gravity_cache.h" #include "hydro.h" @@ -88,7 +89,6 @@ #include "star_formation.h" #include "star_formation_logger.h" #include "star_formation_logger_struct.h" -#include "feedback.h" #include "stars_io.h" #include "statistics.h" #include "timers.h" diff --git a/src/feedback/EAGLE/feedback.c b/src/feedback/EAGLE/feedback.c index 6bbc08a5e..b2cd34fb4 100644 --- a/src/feedback/EAGLE/feedback.c +++ b/src/feedback/EAGLE/feedback.c @@ -1002,7 +1002,7 @@ void feedback_props_init(struct feedback_props* fp, * @param table yield_table struct in which pointers to tables * set to NULL */ -void zero_yield_table_pointers(struct yield_table table){ +void zero_yield_table_pointers(struct yield_table table) { table.mass = NULL; table.metallicity = NULL; table.yield_IMF_resampled = NULL; @@ -1020,8 +1020,8 @@ void zero_yield_table_pointers(struct yield_table table){ * @param feedback the #feedback_props structure * @param cosmo #cosmology structure */ -void feedback_restore_tables(struct feedback_props *fp) { - +void feedback_restore_tables(struct feedback_props* fp) { + init_imf(fp); /* Allocate yield tables */ @@ -1053,8 +1053,7 @@ void feedback_restore_tables(struct feedback_props *fp) { * @param feedback the struct * @param stream the file stream */ -void feedback_struct_dump(const struct feedback_props *feedback, - FILE *stream) { +void feedback_struct_dump(const struct feedback_props* feedback, FILE* stream) { /* To make sure everything is restored correctly, we zero all the pointers to tables. If they are not restored correctly, we would crash after restart on @@ -1088,9 +1087,8 @@ void feedback_struct_dump(const struct feedback_props *feedback, feedback_copy.imf_mass_bin = NULL; feedback_copy.imf_mass_bin_log10 = NULL; - restart_write_blocks((void *)&feedback_copy, - sizeof(struct feedback_props), 1, stream, - "feedback", "feedback function"); + restart_write_blocks((void*)&feedback_copy, sizeof(struct feedback_props), 1, + stream, "feedback", "feedback function"); } /** @@ -1103,9 +1101,9 @@ void feedback_struct_dump(const struct feedback_props *feedback, * @param feedback the struct * @param stream the file stream */ -void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) { - restart_read_blocks((void *)feedback, sizeof(struct feedback_props), 1, - stream, NULL, "feedback function"); +void feedback_struct_restore(struct feedback_props* feedback, FILE* stream) { + restart_read_blocks((void*)feedback, sizeof(struct feedback_props), 1, stream, + NULL, "feedback function"); feedback_restore_tables(feedback); } diff --git a/src/feedback/EAGLE/feedback.h b/src/feedback/EAGLE/feedback.h index bc1a41e9a..4f341a00f 100644 --- a/src/feedback/EAGLE/feedback.h +++ b/src/feedback/EAGLE/feedback.h @@ -172,9 +172,8 @@ __attribute__((always_inline)) INLINE static void feedback_evolve_spart( sp->mass -= sp->feedback_data.to_distribute.mass; } -void feedback_struct_dump(const struct feedback_props *feedback, - FILE *stream); +void feedback_struct_dump(const struct feedback_props* feedback, FILE* stream); -void feedback_struct_restore(struct feedback_props *feedback, FILE *stream); +void feedback_struct_restore(struct feedback_props* feedback, FILE* stream); #endif /* SWIFT_FEEDBACK_EAGLE_H */ diff --git a/src/feedback/none/feedback.h b/src/feedback/none/feedback.h index 903bb2b9f..7805603f3 100644 --- a/src/feedback/none/feedback.h +++ b/src/feedback/none/feedback.h @@ -120,8 +120,8 @@ __attribute__((always_inline)) INLINE static void feedback_evolve_spart( * @param feedback the struct * @param stream the file stream */ -inline void feedback_struct_dump(const struct feedback_props *feedback, - FILE *stream) {} +inline void feedback_struct_dump(const struct feedback_props* feedback, + FILE* stream) {} /** * @brief Restore a hydro_props struct from the given FILE as a stream of @@ -134,6 +134,7 @@ inline void feedback_struct_dump(const struct feedback_props *feedback, * @param stream the file stream * @param cosmo #cosmology structure */ -inline void feedback_struct_restore(struct feedback_props *feedback, FILE *stream) {} +inline void feedback_struct_restore(struct feedback_props* feedback, + FILE* stream) {} #endif /* SWIFT_FEEDBACK_NONE_H */ -- GitLab