diff --git a/examples/CoolingRates/cooling_rates.c b/examples/CoolingRates/cooling_rates.c index 8a2d098c8d4bd935f52103c89c5cda51bb0a573d..f96701f4c5645818bc5c72d0c9b607ce29f1b597 100644 --- a/examples/CoolingRates/cooling_rates.c +++ b/examples/CoolingRates/cooling_rates.c @@ -229,7 +229,7 @@ int main(int argc, char **argv) { // Init cooling cooling_init(params, &us, &internal_const, &cooling); cooling_print(&cooling); - cooling_update(&cosmo, &cooling, /*restart=*/0); + cooling_update(&cosmo, &cooling); // Calculate abundance ratios float abundance_ratio[(chemistry_element_count + 2)]; diff --git a/src/cooling.c b/src/cooling.c index 376373ad80e1d784f183eecafbe51d20a80b3159..34205937bbd7ce144503b10ef047cf5b552f23cc 100644 --- a/src/cooling.c +++ b/src/cooling.c @@ -53,31 +53,3 @@ void cooling_print(const struct cooling_function_data* cooling) { cooling_print_backend(cooling); } - -/** - * @brief Write a cooling struct to the given FILE as a stream of bytes. - * - * @param cooling the struct - * @param stream the file stream - */ -void cooling_struct_dump(const struct cooling_function_data* cooling, - FILE* stream) { - restart_write_blocks((void*)cooling, sizeof(struct cooling_function_data), 1, - stream, "cooling", "cooling function"); -} - -/** - * @brief Restore a hydro_props struct from the given FILE as a stream of - * bytes. - * - * @param cooling the struct - * @param stream the file stream - * @param cosmo #cosmology structure - */ -void cooling_struct_restore(struct cooling_function_data* cooling, FILE* stream, - const struct cosmology* cosmo) { - restart_read_blocks((void*)cooling, sizeof(struct cooling_function_data), 1, - stream, NULL, "cooling function"); - - cooling_restore_tables(cooling, cosmo); -} diff --git a/src/cooling/EAGLE/cooling.c b/src/cooling/EAGLE/cooling.c index 3ca3eb52184bb5c3d7c581d3dab3df9e5863e412..1b93417ddeed51c88451b43b86766f0d09ebdf38 100644 --- a/src/cooling/EAGLE/cooling.c +++ b/src/cooling/EAGLE/cooling.c @@ -76,7 +76,7 @@ static const double newton_log_u_guess_cgs = 1.414213562e6; /* log10(2e12) */ * @param cooling #cooling_function_data structure containing redshift table. */ __attribute__((always_inline)) INLINE void get_redshift_index( - float z, int *z_index, float *dz, + const float z, int *z_index, float *dz, struct cooling_function_data *restrict cooling) { /* Before the earliest redshift or before hydrogen reionization, flag for @@ -124,11 +124,9 @@ __attribute__((always_inline)) INLINE void get_redshift_index( * * @param cosmo The current cosmological model. * @param cooling The #cooling_function_data used in the run. - * @param restart_flag Flag indicating restarted run. */ void cooling_update(const struct cosmology *cosmo, - struct cooling_function_data *cooling, - const int restart_flag) { + struct cooling_function_data *cooling) { /* Current redshift */ const float redshift = cosmo->z; @@ -839,7 +837,6 @@ void cooling_init_backend(struct swift_params *parameter_file, /* Set the redshift indices to invalid values */ cooling->z_index = -10; - cooling->previous_z_index = eagle_cooling_N_redshifts + 2; /* set previous_z_index and to last value of redshift table*/ cooling->previous_z_index = eagle_cooling_N_redshifts - 2; @@ -872,8 +869,8 @@ void cooling_restore_tables(struct cooling_function_data *cooling, /* Force a re-read of the cooling tables */ cooling->z_index = -10; - cooling->previous_z_index = eagle_cooling_N_redshifts + 2; - cooling_update(cosmo, cooling, /*restart_flag=*/1); + cooling->previous_z_index = eagle_cooling_N_redshifts - 2; + cooling_update(cosmo, cooling); } /** @@ -911,3 +908,52 @@ void cooling_clean(struct cooling_function_data *cooling) { free(cooling->table.H_plus_He_heating); free(cooling->table.H_plus_He_electron_abundance); } + +/** + * @brief Write a cooling struct to the given FILE as a stream of bytes. + * + * @param cooling the struct + * @param stream the file stream + */ +void cooling_struct_dump(const struct cooling_function_data *cooling, + 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 cooling routines. Helps debugging. */ + struct cooling_function_data cooling_copy = *cooling; + cooling_copy.Redshifts = NULL; + cooling_copy.nH = NULL; + cooling_copy.Temp = NULL; + cooling_copy.Therm = NULL; + cooling_copy.SolarAbundances = NULL; + cooling_copy.SolarAbundances_inv = NULL; + cooling_copy.table.metal_heating = NULL; + cooling_copy.table.H_plus_He_heating = NULL; + cooling_copy.table.H_plus_He_electron_abundance = NULL; + cooling_copy.table.temperature = NULL; + cooling_copy.table.electron_abundance = NULL; + + restart_write_blocks((void *)&cooling_copy, + sizeof(struct cooling_function_data), 1, stream, + "cooling", "cooling 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 cooling tables by + * re-reading them. + * + * @param cooling the struct + * @param stream the file stream + * @param cosmo #cosmology structure + */ +void cooling_struct_restore(struct cooling_function_data *cooling, FILE *stream, + const struct cosmology *cosmo) { + restart_read_blocks((void *)cooling, sizeof(struct cooling_function_data), 1, + stream, NULL, "cooling function"); + + cooling_restore_tables(cooling, cosmo); +} diff --git a/src/cooling/EAGLE/cooling.h b/src/cooling/EAGLE/cooling.h index 02ee37a416db322c2813cf4da7fe4105d5d8e84f..a811007136168646a98df0c0da7b43227de082f8 100644 --- a/src/cooling/EAGLE/cooling.h +++ b/src/cooling/EAGLE/cooling.h @@ -33,8 +33,7 @@ #include "units.h" void cooling_update(const struct cosmology *cosmo, - struct cooling_function_data *cooling, - const int restart_flag); + struct cooling_function_data *cooling); void cooling_cool_part(const struct phys_const *restrict phys_const, const struct unit_system *restrict us, diff --git a/src/cooling/grackle/cooling.h b/src/cooling/grackle/cooling.h index 03f3f1f14dc008b6ee9feedb09893470a5ccde6f..058ed387bf2097ce789a3807641c3b92b9bc7af0 100644 --- a/src/cooling/grackle/cooling.h +++ b/src/cooling/grackle/cooling.h @@ -54,11 +54,9 @@ * * @param cosmo The current cosmological model. * @param cooling The #cooling_function_data used in the run. - * @param restart_flag Are we calling this directly after a restart? */ INLINE static void cooling_update(const struct cosmology* cosmo, - struct cooling_function_data* cooling, - const int restart_flag) { + struct cooling_function_data* cooling) { // Add content if required. } @@ -685,6 +683,18 @@ __attribute__((always_inline)) INLINE static void cooling_cool_part( hydro_set_physical_internal_energy_dt(p, cosmo, hydro_du_dt + du_dt); } +static INLINE float cooling_get_temperature( + const struct phys_const* restrict phys_const, + const struct hydro_props* restrict hydro_props, + const struct unit_system* restrict us, + const struct cosmology* restrict cosmo, + const struct cooling_function_data* restrict cooling, + const struct part* restrict p, const struct xpart* restrict xp) { + + error("This function needs implementing!!"); + return 0.; +} + /** * @brief Computes the cooling time-step. * @@ -815,15 +825,6 @@ __attribute__((always_inline)) INLINE static void cooling_init_backend( cooling_init_grackle(cooling); } -/** - * @brief Restore cooling tables (if applicable) after - * restart - * - * @param cooling the cooling_function_data structure - * @param cosmo cosmology structure - */ -static INLINE void cooling_restore_tables(struct cooling_function_data* cooling, - const struct cosmology* cosmo) {} /** * @brief Clean-up the memory allocated for the cooling routines * @@ -834,4 +835,35 @@ static INLINE void cooling_clean(struct cooling_function_data* cooling) { // MATTHIEU: To do: free stuff here } +/** + * @brief Write a cooling struct to the given FILE as a stream of bytes. + * + * Nothing to do beyond writing the structure from the stream. + * + * @param cooling the struct + * @param stream the file stream + */ +static INLINE void cooling_struct_dump( + const struct cooling_function_data* cooling, FILE* stream) { + restart_write_blocks((void*)cooling, sizeof(struct cooling_function_data), 1, + stream, "cooling", "cooling function"); +} + +/** + * @brief Restore a hydro_props struct from the given FILE as a stream of + * bytes. + * + * Nothing to do beyond reading the structure from the stream. + * + * @param cooling the struct + * @param stream the file stream + * @param cosmo #cosmology structure + */ +static INLINE void cooling_struct_restore(struct cooling_function_data* cooling, + FILE* stream, + const struct cosmology* cosmo) { + restart_read_blocks((void*)cooling, sizeof(struct cooling_function_data), 1, + stream, NULL, "cooling function"); +} + #endif /* SWIFT_COOLING_GRACKLE_H */ diff --git a/src/cooling/grackle/cooling_io.h b/src/cooling/grackle/cooling_io.h index 684ab347e19c8ea5f1897a54f21951256ef5f50b..88235a20a2f9d150b56f59ee32fa9ab91941e659 100644 --- a/src/cooling/grackle/cooling_io.h +++ b/src/cooling/grackle/cooling_io.h @@ -59,7 +59,7 @@ __attribute__((always_inline)) INLINE static void cooling_write_flavour( * @return Returns the number of fields to write. */ __attribute__((always_inline)) INLINE static int cooling_write_particles( - const struct xpart* xparts, struct io_props* list, + const struct part* parts, const struct xpart* xparts, struct io_props* list, const struct cooling_function_data* cooling) { int num = 0; diff --git a/src/cooling/none/cooling.h b/src/cooling/none/cooling.h index 579cf2ae9c2db290300f8367111f91aab9bd27d0..a9921720d3ddd945c439515c91692dbb0b132445 100644 --- a/src/cooling/none/cooling.h +++ b/src/cooling/none/cooling.h @@ -46,8 +46,7 @@ * @param cooling The #cooling_function_data used in the run. */ INLINE static void cooling_update(const struct cosmology* cosmo, - struct cooling_function_data* cooling, - const int restart_flag) { + struct cooling_function_data* cooling) { // Add content if required. } @@ -191,18 +190,6 @@ static INLINE void cooling_init_backend(struct swift_params* parameter_file, struct cooling_function_data* cooling) { } -/** - * @brief Restore cooling tables (if applicable) after - * restart - * - * Nothing to do here - * - * @param cooling the cooling_function_data structure - * @param cosmo cosmology structure - */ -static INLINE void cooling_restore_tables(struct cooling_function_data* cooling, - const struct cosmology* cosmo) {} - /** * @brief Prints the properties of the cooling model to stdout. * @@ -221,4 +208,29 @@ static INLINE void cooling_print_backend( */ static INLINE void cooling_clean(struct cooling_function_data* cooling) {} +/** + * @brief Write a cooling struct to the given FILE as a stream of bytes. + * + * Empty structure so nothing to do here. + * + * @param cooling the struct + * @param stream the file stream + */ +static INLINE void cooling_struct_dump( + const struct cooling_function_data* cooling, FILE* stream) {} + +/** + * @brief Restore a hydro_props struct from the given FILE as a stream of + * bytes. + * + * Empty structure so nothing to do here. + * + * @param cooling the struct + * @param stream the file stream + * @param cosmo #cosmology structure + */ +static INLINE void cooling_struct_restore(struct cooling_function_data* cooling, + FILE* stream, + const struct cosmology* cosmo) {} + #endif /* SWIFT_COOLING_NONE_H */ diff --git a/src/engine.c b/src/engine.c index 1e0e89043091a8267d1de4ab345c282778768364..d4feee49f88a6a0bae5e7683d4df80394e9c89b3 100644 --- a/src/engine.c +++ b/src/engine.c @@ -2797,7 +2797,7 @@ void engine_init_particles(struct engine *e, int flag_entropy_ICs, /* Update the cooling function */ if ((e->policy & engine_policy_cooling) || (e->policy & engine_policy_temperature)) - cooling_update(e->cosmology, e->cooling_func, /*restart_flag=*/0); + cooling_update(e->cosmology, e->cooling_func); #ifdef WITH_LOGGER /* Mark the first time step in the particle logger file. */ @@ -3057,7 +3057,7 @@ void engine_step(struct engine *e) { /* Update the cooling function */ if ((e->policy & engine_policy_cooling) || (e->policy & engine_policy_temperature)) - cooling_update(e->cosmology, e->cooling_func, /*restart_flag=*/0); + cooling_update(e->cosmology, e->cooling_func); /*****************************************************/ /* OK, we now know what the next end of time-step is */