From 4cc70e4cb897b281e4b5c517e33b816cfae3ead3 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Wed, 11 Dec 2019 13:51:46 +0100
Subject: [PATCH] Correctly set the last enrichment time of particles when
 starting a run.

---
 src/space.c               |  4 +++-
 src/stars/Default/stars.h |  7 ++++++-
 src/stars/EAGLE/stars.h   | 15 ++++++++++++---
 src/stars/GEAR/stars.h    |  7 ++++++-
 4 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/src/space.c b/src/space.c
index 8e9b4543bd..575016fcdb 100644
--- a/src/space.c
+++ b/src/space.c
@@ -4383,6 +4383,7 @@ void space_first_init_sparts_mapper(void *restrict map_data, int count,
   const float initial_h = s->initial_spart_h;
 
   const int with_feedback = (e->policy & engine_policy_feedback);
+  const int with_cosmology = (e->policy & engine_policy_cosmology);
 
   const struct cosmology *cosmo = e->cosmology;
   const struct stars_props *stars_properties = e->stars_properties;
@@ -4421,7 +4422,8 @@ void space_first_init_sparts_mapper(void *restrict map_data, int count,
   /* Initialise the rest */
   for (int k = 0; k < count; k++) {
 
-    stars_first_init_spart(&sp[k], stars_properties);
+    stars_first_init_spart(&sp[k], stars_properties, with_cosmology, cosmo->a,
+                           e->time);
 
 #ifdef WITH_LOGGER
     logger_part_data_init(&sp[k].logger_data);
diff --git a/src/stars/Default/stars.h b/src/stars/Default/stars.h
index 804fe6085d..85e92c199b 100644
--- a/src/stars/Default/stars.h
+++ b/src/stars/Default/stars.h
@@ -41,9 +41,14 @@ __attribute__((always_inline)) INLINE static float stars_compute_timestep(
  *
  * @param sp The particle to act upon
  * @param stars_properties The properties of the stellar model.
+ * @param with_cosmology Are we running with cosmological time integration.
+ * @param scale_factor The current scale-factor (used if running with
+ * cosmology).
+ * @param time The current time (used if running without cosmology).
  */
 __attribute__((always_inline)) INLINE static void stars_first_init_spart(
-    struct spart* sp, const struct stars_props* stars_properties) {
+    struct spart* sp, const struct stars_props* stars_properties,
+    const int with_cosmology, const double scale_factor, const double time) {
 
   sp->time_bin = 0;
 }
diff --git a/src/stars/EAGLE/stars.h b/src/stars/EAGLE/stars.h
index e9ea6ed0b8..644acfcbf7 100644
--- a/src/stars/EAGLE/stars.h
+++ b/src/stars/EAGLE/stars.h
@@ -58,18 +58,27 @@ __attribute__((always_inline)) INLINE static void stars_init_spart(
  *
  * @param sp The particle to act upon.
  * @param stars_properties Properties of the stars model.
+ * @param with_cosmology Are we running with cosmological time integration.
+ * @param scale_factor The current scale-factor (used if running with
+ * cosmology).
+ * @param time The current time (used if running without cosmology).
  */
 __attribute__((always_inline)) INLINE static void stars_first_init_spart(
-    struct spart* sp, const struct stars_props* stars_properties) {
+    struct spart* sp, const struct stars_props* stars_properties,
+    const int with_cosmology, const double scale_factor, const double time) {
 
   sp->time_bin = 0;
   sp->birth_density = 0.f;
   sp->f_E = -1.f;
+  sp->count_since_last_enrichment = -1;
+
   if (stars_properties->overwrite_birth_time)
     sp->birth_time = stars_properties->spart_first_init_birth_time;
 
-  sp->last_enrichment_time = sp->birth_time;
-  sp->count_since_last_enrichment = -1;
+  if (with_cosmology)
+    sp->last_enrichment_time = scale_factor;
+  else
+    sp->last_enrichment_time = time;
 
   stars_init_spart(sp);
 }
diff --git a/src/stars/GEAR/stars.h b/src/stars/GEAR/stars.h
index 467aaa164b..2fe4428b81 100644
--- a/src/stars/GEAR/stars.h
+++ b/src/stars/GEAR/stars.h
@@ -41,9 +41,14 @@ __attribute__((always_inline)) INLINE static float stars_compute_timestep(
  *
  * @param sp The particle to act upon
  * @param stars_properties The properties of the stellar model.
+ * @param with_cosmology Are we running with cosmological time integration.
+ * @param scale_factor The current scale-factor (used if running with
+ * cosmology).
+ * @param time The current time (used if running without cosmology).
  */
 __attribute__((always_inline)) INLINE static void stars_first_init_spart(
-    struct spart* sp, const struct stars_props* stars_properties) {
+    struct spart* sp, const struct stars_props* stars_properties,
+    const int with_cosmology, const double scale_factor, const double time) {
 
   sp->time_bin = 0;
 }
-- 
GitLab