Skip to content
Snippets Groups Projects
Commit c96c2585 authored by Matthieu Schaller's avatar Matthieu Schaller
Browse files

Merge branch 'update_feedback_api' into 'master'

Remove extra computation in timestep

See merge request !1238
parents a8d97c32 8ef80222
Branches
Tags
1 merge request!1238Remove extra computation in timestep
...@@ -99,13 +99,10 @@ INLINE static double feedback_get_enrichment_timestep( ...@@ -99,13 +99,10 @@ INLINE static double feedback_get_enrichment_timestep(
const struct cosmology* cosmo, const double time, const double dt_star) { const struct cosmology* cosmo, const double time, const double dt_star) {
if (with_cosmology) { if (with_cosmology) {
if (cosmo->a > (double)sp->last_enrichment_time) return cosmology_get_delta_time_from_scale_factors(
return cosmology_get_delta_time_from_scale_factors( cosmo, (double)sp->last_enrichment_time, cosmo->a);
cosmo, (double)sp->last_enrichment_time, cosmo->a);
else
return 0.;
} else { } 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( ...@@ -235,19 +232,16 @@ __attribute__((always_inline)) INLINE static void feedback_prepare_feedback(
* @param cosmo The current cosmological model. * @param cosmo The current cosmological model.
* @param us The unit system. * @param us The unit system.
* @param phys_const The #phys_const. * @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 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 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( __attribute__((always_inline)) INLINE static void feedback_will_do_feedback(
struct spart* sp, const struct feedback_props* feedback_props, struct spart* sp, const struct feedback_props* feedback_props,
const int with_cosmology, const struct cosmology* cosmo, const double time, const int with_cosmology, const struct cosmology* cosmo, const double time,
const struct unit_system* us, const struct phys_const* phys_const, const struct unit_system* us, const struct phys_const* phys_const,
const double star_age_beg_step, const double dt, const integertime_t ti_current, const double time_base) {
const integertime_t ti_begin) {
/* Special case for new-born stars */ /* Special case for new-born stars */
if (with_cosmology) { if (with_cosmology) {
...@@ -255,25 +249,28 @@ __attribute__((always_inline)) INLINE static void feedback_will_do_feedback( ...@@ -255,25 +249,28 @@ __attribute__((always_inline)) INLINE static void feedback_will_do_feedback(
/* Set the counter to "let's do enrichment" */ /* Set the counter to "let's do enrichment" */
sp->count_since_last_enrichment = 0; sp->count_since_last_enrichment = 0;
/* Ok, we are done. */
return;
} }
} else { } else {
if (sp->birth_time == (float)time) { if (sp->birth_time == (float)time) {
/* Set the counter to "let's do enrichment" */ /* Set the counter to "let's do enrichment" */
sp->count_since_last_enrichment = 0; sp->count_since_last_enrichment = 0;
/* Ok, we are done. */
return;
} }
} }
/* Calculate age of the star at current time */ /* Calculate age of the star at current time */
double age_of_star; double age_of_star;
if (with_cosmology) { if (with_cosmology) {
if (cosmo->a > (double)sp->birth_scale_factor) age_of_star = cosmology_get_delta_time_from_scale_factors(
age_of_star = cosmology_get_delta_time_from_scale_factors( cosmo, (double)sp->birth_scale_factor, cosmo->a);
cosmo, (double)sp->birth_scale_factor, cosmo->a);
else
age_of_star = 0.;
} else { } else {
age_of_star = max(time - (double)sp->birth_time, 0.); age_of_star = time - (double)sp->birth_time;
} }
/* Is the star still young? */ /* Is the star still young? */
......
...@@ -86,6 +86,57 @@ void feedback_update_part(struct part* restrict p, struct xpart* restrict xp, ...@@ -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? * @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, ...@@ -98,21 +149,22 @@ void feedback_update_part(struct part* restrict p, struct xpart* restrict xp,
* @param cosmo The current cosmological model. * @param cosmo The current cosmological model.
* @param us The unit system. * @param us The unit system.
* @param phys_const The #phys_const. * @param phys_const The #phys_const.
* @param star_age_beg_step The age of the star at the star of the time-step in * @param ti_current The current time (in integer)
* internal units. * @param time_base The time base.
* @param dt The time-step size of this star in internal units.
* @param time The physical time 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?
*/ */
void feedback_will_do_feedback(struct spart* sp, void feedback_will_do_feedback(
const struct feedback_props* feedback_props, struct spart* sp, const struct feedback_props* feedback_props,
const int with_cosmology, const int with_cosmology, const struct cosmology* cosmo, const double time,
const struct cosmology* cosmo, const double time, const struct unit_system* us, const struct phys_const* phys_const,
const struct unit_system* us, const integertime_t ti_current, const double time_base) {
const struct phys_const* phys_const,
const double star_age_beg_step, const double dt, /* Compute the times */
const integertime_t ti_begin) { 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 */ /* Zero the energy of supernovae */
sp->feedback_data.energy_ejected = 0; sp->feedback_data.energy_ejected = 0;
...@@ -126,7 +178,7 @@ void feedback_will_do_feedback(struct spart* sp, ...@@ -126,7 +178,7 @@ void feedback_will_do_feedback(struct spart* sp,
} }
#endif #endif
/* Has this star been around for a while ? */ /* 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 = const double star_age_beg_step_safe =
star_age_beg_step < 0 ? 0 : star_age_beg_step; star_age_beg_step < 0 ? 0 : star_age_beg_step;
...@@ -142,7 +194,7 @@ void feedback_will_do_feedback(struct spart* sp, ...@@ -142,7 +194,7 @@ void feedback_will_do_feedback(struct spart* sp,
/* Compute the stellar evolution */ /* Compute the stellar evolution */
stellar_evolution_evolve_spart(sp, model, cosmo, us, phys_const, ti_begin, 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 */ /* Transform the number of SN to the energy */
sp->feedback_data.energy_ejected = sp->feedback_data.energy_ejected =
......
...@@ -32,14 +32,11 @@ ...@@ -32,14 +32,11 @@
void feedback_update_part(struct part* restrict p, struct xpart* restrict xp, void feedback_update_part(struct part* restrict p, struct xpart* restrict xp,
const struct engine* restrict e); const struct engine* restrict e);
void feedback_will_do_feedback(struct spart* sp, void feedback_will_do_feedback(
const struct feedback_props* feedback_props, struct spart* sp, const struct feedback_props* feedback_props,
const int with_cosmology, const int with_cosmology, const struct cosmology* cosmo, const double time,
const struct cosmology* cosmo, const double time, const struct unit_system* us, const struct phys_const* phys_const,
const struct unit_system* us, const integertime_t ti_current, const double time_base);
const struct phys_const* phys_const,
const double star_age_beg_step, const double dt,
const integertime_t ti_begin);
int feedback_is_active(const struct spart* sp, const double time, int feedback_is_active(const struct spart* sp, const double time,
const struct cosmology* cosmo, const int with_cosmology); const struct cosmology* cosmo, const int with_cosmology);
......
...@@ -170,19 +170,16 @@ __attribute__((always_inline)) INLINE static void feedback_prepare_feedback( ...@@ -170,19 +170,16 @@ __attribute__((always_inline)) INLINE static void feedback_prepare_feedback(
* @param cosmo The current cosmological model. * @param cosmo The current cosmological model.
* @param us The unit system. * @param us The unit system.
* @param phys_const The #phys_const. * @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 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 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( __attribute__((always_inline)) INLINE static void feedback_will_do_feedback(
const struct spart* sp, const struct feedback_props* feedback_props, const struct spart* sp, const struct feedback_props* feedback_props,
const int with_cosmology, const struct cosmology* cosmo, const double time, const int with_cosmology, const struct cosmology* cosmo, const double time,
const struct unit_system* us, const struct phys_const* phys_const, const struct unit_system* us, const struct phys_const* phys_const,
const double star_age_beg_step, const double dt, const integertime_t ti_current, const double time_base) {}
const integertime_t ti_begin) {}
/** /**
* @brief Clean-up the memory allocated for the feedback routines * @brief Clean-up the memory allocated for the feedback routines
......
...@@ -852,42 +852,10 @@ void runner_do_timestep(struct runner *r, struct cell *c, const int timer) { ...@@ -852,42 +852,10 @@ void runner_do_timestep(struct runner *r, struct cell *c, const int timer) {
/* Update feedback related counters */ /* Update feedback related counters */
if (with_feedback) { 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,
feedback_will_do_feedback( e->time, us, phys_const, e->ti_current,
sp, feedback_props, with_cosmology, cosmo, e->time, us, e->time_base);
phys_const, star_age_beg_of_step, dt_enrichment, ti_begin_star);
} }
/* Number of updated s-particles */ /* Number of updated s-particles */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment