diff --git a/examples/SubgridTests/StellarEvolution/makeIC.py b/examples/SubgridTests/StellarEvolution/makeIC.py index 0ad23b4a9c6d2d730e7b8ce8cd37a2362337e2b9..abc2d8126ae8ac1173f8918ffc818f9cb6bcb4fe 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/engine.c b/src/engine.c index 5c41c9ad7343e6cd4b68183506eaa5bbb2e953ae..d418515db4ccca8a72820c5fbb9dacc7bc0803f2 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" @@ -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 76370156e4cda9c7abab8f538bd9d2517be1ebf8..b2cd34fb4e8a9f3dea87995e43cb541bdfaa4b17 100644 --- a/src/feedback/EAGLE/feedback.c +++ b/src/feedback/EAGLE/feedback.c @@ -995,3 +995,115 @@ 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) { + + init_imf(fp); + + /* 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; + + 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 14f6821b065db5d05e408c1438af28d7492a5d94..4f341a00fcc333190210ab446c738090647855ed 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,4 +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_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 1085e4555152fd9d47d924d20f409c060f452d4c..7805603f3dee66e17d6f1e302a7977f2d47f5fe1 100644 --- a/src/feedback/none/feedback.h +++ b/src/feedback/none/feedback.h @@ -114,4 +114,27 @@ __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 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 */