diff --git a/src/feedback/EAGLE/feedback.h b/src/feedback/EAGLE/feedback.h index 0a42bf94c7581f3934519238a3edb7fe91d69522..fea2a0ae2b7e399838ca747ee74b7341b93a398c 100644 --- a/src/feedback/EAGLE/feedback.h +++ b/src/feedback/EAGLE/feedback.h @@ -99,13 +99,10 @@ INLINE static double feedback_get_enrichment_timestep( const struct cosmology* cosmo, const double time, const double dt_star) { if (with_cosmology) { - if (cosmo->a > (double)sp->last_enrichment_time) - return cosmology_get_delta_time_from_scale_factors( - cosmo, (double)sp->last_enrichment_time, cosmo->a); - else - return 0.; + return cosmology_get_delta_time_from_scale_factors( + cosmo, (double)sp->last_enrichment_time, cosmo->a); } else { - return max(time - sp->last_enrichment_time, 0.); + return time - sp->last_enrichment_time; } } @@ -235,19 +232,16 @@ __attribute__((always_inline)) INLINE static void feedback_prepare_feedback( * @param cosmo The current cosmological model. * @param us The unit system. * @param phys_const The #phys_const. - * @param star_age_beg_step The age of the star at the star of the time-step in - * internal units. - * @param dt The time-step size of this star in internal units. * @param time The physical time in internal units. - * @param ti_begin The integer time at the beginning of the step. * @param with_cosmology Are we running with cosmology on? + * @param ti_current The current time (in integer) + * @param time_base The time base. */ __attribute__((always_inline)) INLINE static void feedback_will_do_feedback( struct spart* sp, const struct feedback_props* feedback_props, const int with_cosmology, const struct cosmology* cosmo, const double time, const struct unit_system* us, const struct phys_const* phys_const, - const double star_age_beg_step, const double dt, - const integertime_t ti_begin) { + const integertime_t ti_current, const double time_base) { /* Special case for new-born stars */ if (with_cosmology) { @@ -255,25 +249,28 @@ __attribute__((always_inline)) INLINE static void feedback_will_do_feedback( /* Set the counter to "let's do enrichment" */ sp->count_since_last_enrichment = 0; + + /* Ok, we are done. */ + return; } } else { if (sp->birth_time == (float)time) { /* Set the counter to "let's do enrichment" */ sp->count_since_last_enrichment = 0; + + /* Ok, we are done. */ + return; } } /* Calculate age of the star at current time */ double age_of_star; if (with_cosmology) { - if (cosmo->a > (double)sp->birth_scale_factor) - age_of_star = cosmology_get_delta_time_from_scale_factors( - cosmo, (double)sp->birth_scale_factor, cosmo->a); - else - age_of_star = 0.; + age_of_star = cosmology_get_delta_time_from_scale_factors( + cosmo, (double)sp->birth_scale_factor, cosmo->a); } else { - age_of_star = max(time - (double)sp->birth_time, 0.); + age_of_star = time - (double)sp->birth_time; } /* Is the star still young? */ diff --git a/src/feedback/GEAR/feedback.c b/src/feedback/GEAR/feedback.c index 38662719854f7a507173ed4bef4e0f9620939e48..6a8d15c4c4b6583bc7e14c6cf1868a11c33e8fa9 100644 --- a/src/feedback/GEAR/feedback.c +++ b/src/feedback/GEAR/feedback.c @@ -86,6 +86,57 @@ void feedback_update_part(struct part* restrict p, struct xpart* restrict xp, } } +/** + * @brief Compute the times for the stellar model. + * + * This function assumed to be called in the time step task. + * + * @param sp The #spart to act upon + * @param with_cosmology Are we running with the cosmological expansion? + * @param cosmo The current cosmological model. + * @param star_age_beg_of_step (output) Age of the star at the beginning of the + * step. + * @param dt_enrichment (output) Time step for the stellar evolution. + * @param ti_begin_star (output) Integer time at the beginning of the time step. + * @param ti_current The current time (in integer) + * @param time_base The time base. + * @param time The current time (in double) + */ +void compute_time(struct spart* sp, const struct engine* e, + const int with_cosmology, const struct cosmology* cosmo, + double* star_age_beg_of_step, double* dt_enrichment, + integertime_t* ti_begin_star, const integertime_t ti_current, + const double time_base, const double time) { + const integertime_t ti_step = get_integer_timestep(sp->time_bin); + *ti_begin_star = get_integer_time_begin(ti_current, sp->time_bin); + + /* Get particle time-step */ + double dt_star; + if (with_cosmology) { + dt_star = cosmology_get_delta_time(cosmology, *ti_begin_star, + *ti_begin_star + ti_step); + } else { + dt_star = get_timestep(sp->time_bin, time_base); + } + + /* Calculate age of the star at current time */ + double star_age_end_of_step; + if (with_cosmology) { + if (cosmo->a > (double)sp->birth_scale_factor) + star_age_end_of_step = cosmology_get_delta_time_from_scale_factors( + cosmo, (double)sp->birth_scale_factor, cosmo->a); + else + star_age_end_of_step = 0.; + } else { + star_age_end_of_step = max(time - (double)sp->birth_time, 0.); + } + + /* Get the length of the enrichment time-step */ + *dt_enrichment = feedback_get_enrichment_timestep(sp, with_cosmology, cosmo, + time, dt_star); + *star_age_beg_of_step = star_age_end_of_step - *dt_enrichment; +} + /** * @brief Will this star particle want to do feedback during the next time-step? * @@ -98,21 +149,22 @@ void feedback_update_part(struct part* restrict p, struct xpart* restrict xp, * @param cosmo The current cosmological model. * @param us The unit system. * @param phys_const The #phys_const. - * @param star_age_beg_step The age of the star at the star of the time-step in - * internal units. - * @param dt The time-step size of this star in internal units. + * @param ti_current The current time (in integer) + * @param time_base The time base. * @param time The physical time in internal units. - * @param ti_begin The integer time at the beginning of the step. - * @param with_cosmology Are we running with cosmology on? */ -void feedback_will_do_feedback(struct spart* sp, - const struct feedback_props* feedback_props, - const int with_cosmology, - const struct cosmology* cosmo, const double time, - const struct unit_system* us, - const struct phys_const* phys_const, - const double star_age_beg_step, const double dt, - const integertime_t ti_begin) { +void feedback_will_do_feedback( + struct spart* sp, const struct feedback_props* feedback_props, + const int with_cosmology, const struct cosmology* cosmo, const double time, + const struct unit_system* us, const struct phys_const* phys_const, + const integertime_t ti_current, const double time_base) { + + /* Compute the times */ + double star_age_beg_step = 0; + double dt_enrichment = 0; + integertime_t ti_begin = 0; + compute_time(sp, e, with_cosmology, cosmo, &star_age_beg_step, &dt_enrichment, + &ti_begin, ti_current, time_base, time); /* Zero the energy of supernovae */ sp->feedback_data.energy_ejected = 0; @@ -126,7 +178,7 @@ void feedback_will_do_feedback(struct spart* sp, } #endif /* Has this star been around for a while ? */ - if (star_age_beg_step + dt <= 0.) return; + if (star_age_beg_step + dt_enrichment <= 0.) return; const double star_age_beg_step_safe = star_age_beg_step < 0 ? 0 : star_age_beg_step; @@ -142,7 +194,7 @@ void feedback_will_do_feedback(struct spart* sp, /* Compute the stellar evolution */ stellar_evolution_evolve_spart(sp, model, cosmo, us, phys_const, ti_begin, - star_age_beg_step_safe, dt); + star_age_beg_step_safe, dt_enrichment); /* Transform the number of SN to the energy */ sp->feedback_data.energy_ejected = diff --git a/src/feedback/GEAR/feedback.h b/src/feedback/GEAR/feedback.h index bd0306b158e41ddde1b4d825cc5a30fa81154205..13cfa50cbce21a10e318c0f020bba4b3935f0a9b 100644 --- a/src/feedback/GEAR/feedback.h +++ b/src/feedback/GEAR/feedback.h @@ -32,14 +32,11 @@ void feedback_update_part(struct part* restrict p, struct xpart* restrict xp, const struct engine* restrict e); -void feedback_will_do_feedback(struct spart* sp, - const struct feedback_props* feedback_props, - const int with_cosmology, - const struct cosmology* cosmo, const double time, - const struct unit_system* us, - const struct phys_const* phys_const, - const double star_age_beg_step, const double dt, - const integertime_t ti_begin); +void feedback_will_do_feedback( + struct spart* sp, const struct feedback_props* feedback_props, + const int with_cosmology, const struct cosmology* cosmo, const double time, + const struct unit_system* us, const struct phys_const* phys_const, + const integertime_t ti_current, const double time_base); int feedback_is_active(const struct spart* sp, const double time, const struct cosmology* cosmo, const int with_cosmology); diff --git a/src/feedback/none/feedback.h b/src/feedback/none/feedback.h index 1a0e869417fe5fc2c1d691af7dd9d811310bf95b..1c3dab80b7150db4f2ea9b8f88ade7150c9674d6 100644 --- a/src/feedback/none/feedback.h +++ b/src/feedback/none/feedback.h @@ -170,19 +170,16 @@ __attribute__((always_inline)) INLINE static void feedback_prepare_feedback( * @param cosmo The current cosmological model. * @param us The unit system. * @param phys_const The #phys_const. - * @param star_age_beg_step The age of the star at the star of the time-step in - * internal units. - * @param dt The time-step size of this star in internal units. * @param time The physical time in internal units. - * @param ti_begin The integer time at the beginning of the step. * @param with_cosmology Are we running with cosmology on? + * @param ti_current The current time (in integer) + * @param time_base The time base. */ __attribute__((always_inline)) INLINE static void feedback_will_do_feedback( const struct spart* sp, const struct feedback_props* feedback_props, const int with_cosmology, const struct cosmology* cosmo, const double time, const struct unit_system* us, const struct phys_const* phys_const, - const double star_age_beg_step, const double dt, - const integertime_t ti_begin) {} + const integertime_t ti_current, const double time_base) {} /** * @brief Clean-up the memory allocated for the feedback routines diff --git a/src/runner_time_integration.c b/src/runner_time_integration.c index 4dcb4d25f12b1821b1e53c13b2f107f60d65c002..45fb79962295599f2663675b71883a815c8b0686 100644 --- a/src/runner_time_integration.c +++ b/src/runner_time_integration.c @@ -852,42 +852,10 @@ void runner_do_timestep(struct runner *r, struct cell *c, const int timer) { /* Update feedback related counters */ if (with_feedback) { - const integertime_t ti_step = get_integer_timestep(sp->time_bin); - const integertime_t ti_begin_star = - get_integer_time_begin(e->ti_current, sp->time_bin); - - /* Get particle time-step */ - double dt_star; - if (with_cosmology) { - dt_star = cosmology_get_delta_time(e->cosmology, ti_begin_star, - ti_begin_star + ti_step); - } else { - dt_star = get_timestep(sp->time_bin, e->time_base); - } - - /* Calculate age of the star at current time */ - double star_age_end_of_step; - if (with_cosmology) { - if (cosmo->a > (double)sp->birth_scale_factor) - star_age_end_of_step = - cosmology_get_delta_time_from_scale_factors( - cosmo, (double)sp->birth_scale_factor, cosmo->a); - else - star_age_end_of_step = 0.; - } else { - star_age_end_of_step = max(e->time - (double)sp->birth_time, 0.); - } - - /* Get the length of the enrichment time-step */ - const double dt_enrichment = feedback_get_enrichment_timestep( - sp, with_cosmology, cosmo, e->time, dt_star); - const double star_age_beg_of_step = - star_age_end_of_step - dt_enrichment; - /* Compute the stellar evolution */ - feedback_will_do_feedback( - sp, feedback_props, with_cosmology, cosmo, e->time, us, - phys_const, star_age_beg_of_step, dt_enrichment, ti_begin_star); + feedback_will_do_feedback(sp, feedback_props, with_cosmology, cosmo, + e->time, us, phys_const, e->ti_current, + e->time_base); } /* Number of updated s-particles */