From 51f8d9efbc626bf45e8ac83b065589c02cc3f6d8 Mon Sep 17 00:00:00 2001
From: Robert Izzard <r.izzard@surrey.ac.uk>
Date: Sun, 1 Aug 2021 19:38:07 +0100
Subject: [PATCH] more attempts to clean up timesteps: now check the minimum
 timestep by default, and have separate macros for when we don't want to

also fixed bug where nucsyn checkfuncs weren't wrapper with NUCSYN
---
 src/nucsyn/nucsyn_helium_checkfunction.c   |  2 +
 src/nucsyn/nucsyn_hydrogen_checkfunction.c |  4 +
 src/timestep/nonstellar_timestep.c         | 32 ++++----
 src/timestep/stellar_timestep.c            |  2 +-
 src/timestep/timestep.h                    | 90 +++++++++++++++-------
 src/timestep/timestep_fixed_timesteps.c    | 12 ++-
 src/timestep/timestep_hard_limits.c        |  2 +-
 7 files changed, 96 insertions(+), 48 deletions(-)

diff --git a/src/nucsyn/nucsyn_helium_checkfunction.c b/src/nucsyn/nucsyn_helium_checkfunction.c
index e59f3ca0e..59a5b702c 100644
--- a/src/nucsyn/nucsyn_helium_checkfunction.c
+++ b/src/nucsyn/nucsyn_helium_checkfunction.c
@@ -1,6 +1,7 @@
 #include "../binary_c.h"
 No_empty_translation_unit_warning;
 
+#ifdef NUCSYN
 Boolean nucsyn_helium_checkfunction(struct stardata_t * const stardata MAYBE_UNUSED,
                                     double * const N,
                                     const double temperature,
@@ -13,3 +14,4 @@ Boolean nucsyn_helium_checkfunction(struct stardata_t * const stardata MAYBE_UNU
      */
     return (N[XHe4]>TINY && temperature >= 0.8e8) ? TRUE : FALSE;
 }
+#endif//NUCSYN
diff --git a/src/nucsyn/nucsyn_hydrogen_checkfunction.c b/src/nucsyn/nucsyn_hydrogen_checkfunction.c
index 95a599cae..4fc2669ed 100644
--- a/src/nucsyn/nucsyn_hydrogen_checkfunction.c
+++ b/src/nucsyn/nucsyn_hydrogen_checkfunction.c
@@ -1,6 +1,8 @@
 #include "../binary_c.h"
 No_empty_translation_unit_warning;
 
+#ifdef NUCSYN
+
 Boolean nucsyn_hydrogen_pp_checkfunction(struct stardata_t * const stardata MAYBE_UNUSED,
                                          double * const N,
                                          const double temperature,
@@ -55,3 +57,5 @@ Boolean nucsyn_hydrogen_NeNaMgAl_checkfunction(struct stardata_t * const stardat
              N[XAl26]>TINY)
             && temperature >= 1e6) ? TRUE : FALSE;
 }
+
+#endif//NUCSYN
diff --git a/src/timestep/nonstellar_timestep.c b/src/timestep/nonstellar_timestep.c
index b7a895c97..b21d544bb 100644
--- a/src/timestep/nonstellar_timestep.c
+++ b/src/timestep/nonstellar_timestep.c
@@ -6,25 +6,29 @@ No_empty_translation_unit_warning;
 void nonstellar_timestep(struct stardata_t * const stardata,
                          double * const dtm)
 {
-    Foreach_star(star)
-    {
-        timestep_fixed_timesteps(stardata,
-                                 star,
-                                 dtm);
-    }
+    /*
+     * Timesteps that don't depend on the properties
+     * of the stars.
+     */
+
+
+    /*
+     * Fixed timesteps: associate these with star 0
+     * which will always exist
+     */
+    timestep_fixed_timesteps(stardata,
+                             &stardata->star[0],
+                             dtm);
+
 
     /*
      * Hard wired minimum : do not apply
      * if the timestep is limited by a fixed timestep
      */
-    Foreach_star(star)
+    if(stardata->model.fixed_timestep_triggered == FALSE &&
+       stardata->star[0].dtlimiter != DT_LIMIT_FIXED_TIMESTEP)
     {
-        if(stardata->model.fixed_timestep_triggered == FALSE &&
-           star->dtlimiter != DT_LIMIT_FIXED_TIMESTEP)
-        {
-            *dtm = Max(*dtm,
-                       MINIMUM_STELLAR_TIMESTEP);
-        }
+        *dtm = Max(*dtm,
+                   MINIMUM_STELLAR_TIMESTEP);
     }
-
 }
diff --git a/src/timestep/stellar_timestep.c b/src/timestep/stellar_timestep.c
index d7998d83b..01b0ea9dc 100644
--- a/src/timestep/stellar_timestep.c
+++ b/src/timestep/stellar_timestep.c
@@ -131,11 +131,11 @@ void stellar_timestep(Timestep_prototype_args)
         }
     }
 
+    nonstellar_timestep(stardata,dt);
     timestep_limits(Timestep_call_args);
     timestep_logging(Timestep_call_args);
     timestep_modulation(Timestep_call_args);
     timestep_hard_limits(Timestep_call_args);
-    //nonstellar_timestep(stardata,dt);
 
     star->stellar_timestep = *dt;
 
diff --git a/src/timestep/timestep.h b/src/timestep/timestep.h
index 01f0db997..7e52d2a3f 100644
--- a/src/timestep/timestep.h
+++ b/src/timestep/timestep.h
@@ -117,36 +117,70 @@ enum { FIXED_TIMESTEPS_LIST };
  * a new logging variable.
  */
 
-#define Set_timestep(DT,TO,IN,WHY)              \
-    {                                           \
-        (DT)=(TO);                              \
-        Dprint("Set timestep to %g why %d\n",   \
-               (TO),                            \
-               (WHY));                          \
-        if(WHY!=DT_LIMIT_MAXIMUM_TIMESTEP &&    \
-           WHY!=DT_LIMIT_MINIMUM_TIMESTEP)      \
-        {                                       \
-            (IN)->dtlimiter=(WHY);              \
-        }                                       \
+#define Set_timestep(DT,TO,IN,WHY)                          \
+    {                                                       \
+        (DT)=Max(stardata->preferences->minimum_timestep,   \
+                 (TO));                                     \
+        Dprint("Set timestep to %g why %d\n",               \
+               (TO),                                        \
+               (WHY));                                      \
+        if(WHY!=DT_LIMIT_MAXIMUM_TIMESTEP &&                \
+           WHY!=DT_LIMIT_MINIMUM_TIMESTEP)                  \
+        {                                                   \
+            (IN)->dtlimiter=(WHY);                          \
+        }                                                   \
     }
 
-#define Limit_timestep(DT,TO,IN,WHY)                \
-    __extension__                                   \
-    ({                                              \
-        Boolean _limited;                           \
-        if((DT)>(TO))                               \
-        {                                           \
-            Dprint("Limit timestep to %g why %d\n", \
-                   (TO),                            \
-                   (WHY));                          \
-            Set_timestep((DT),(TO),(IN),(WHY));     \
-            _limited = TRUE;                        \
-        }                                           \
-        else                                        \
-        {                                           \
-            _limited = FALSE;                       \
-        }                                           \
-        _limited;                                   \
+#define Set_timestep_no_minimum(DT,TO,IN,WHY)             \
+    {                                                       \
+        (DT)=(TO);                                          \
+        Dprint("Set timestep to %g why %d\n",               \
+               (TO),                                        \
+               (WHY));                                      \
+        if(WHY!=DT_LIMIT_MAXIMUM_TIMESTEP &&                \
+           WHY!=DT_LIMIT_MINIMUM_TIMESTEP)                  \
+        {                                                   \
+            (IN)->dtlimiter=(WHY);                          \
+        }                                                   \
+    }
+
+#define Limit_timestep(DT,TO,IN,WHY)                            \
+    __extension__                                               \
+    ({                                                          \
+        Boolean _limited;                                       \
+        if((DT)>Max(stardata->preferences->minimum_timestep,    \
+                    (TO)))                                      \
+        {                                                       \
+            Dprint("Limit timestep to %g why %d\n",             \
+                   (TO),                                        \
+                   (WHY));                                      \
+            Set_timestep((DT),(TO),(IN),(WHY));                 \
+            _limited = TRUE;                                    \
+        }                                                       \
+        else                                                    \
+        {                                                       \
+            _limited = FALSE;                                   \
+        }                                                       \
+        _limited;                                               \
+    })
+
+#define Limit_timestep_no_minimum(DT,TO,IN,WHY)                 \
+    __extension__                                               \
+    ({                                                          \
+        Boolean _limited;                                       \
+        if((DT)>(TO))                                           \
+        {                                                       \
+            Dprint("Limit timestep to %g why %d\n",             \
+                   (TO),                                        \
+                   (WHY));                                      \
+            Set_timestep_no_minimum((DT),(TO),(IN),(WHY));      \
+            _limited = TRUE;                                    \
+        }                                                       \
+        else                                                    \
+        {                                                       \
+            _limited = FALSE;                                   \
+        }                                                       \
+        _limited;                                               \
     })
 
 #define Floor_timestep(DT,TO,IN,WHY)                \
diff --git a/src/timestep/timestep_fixed_timesteps.c b/src/timestep/timestep_fixed_timesteps.c
index 4316a23f7..90cfb1291 100644
--- a/src/timestep/timestep_fixed_timesteps.c
+++ b/src/timestep/timestep_fixed_timesteps.c
@@ -46,10 +46,14 @@ void timestep_fixed_timesteps(struct stardata_t * const stardata,
                                              log10(stardata->model.time + dtlim)))
                     );
 
-                Limit_timestep(*dtm,
-                               dtlim,
-                               star,
-                               DT_LIMIT_FIXED_TIMESTEP);
+                /*
+                 * Limit the timestep, but do not impose
+                 * the stardata->preferences->minimum_timestep
+                 */
+                Limit_timestep_no_minimum(*dtm,
+                                          dtlim,
+                                          star,
+                                          DT_LIMIT_FIXED_TIMESTEP);
             }
         }
     }
diff --git a/src/timestep/timestep_hard_limits.c b/src/timestep/timestep_hard_limits.c
index cb70d55e0..234a398e1 100644
--- a/src/timestep/timestep_hard_limits.c
+++ b/src/timestep/timestep_hard_limits.c
@@ -39,7 +39,7 @@ void timestep_hard_limits(Timestep_prototype_args)
     /*
      * If there has been an event, perhaps limit the next timestep?
      */
-    if(0 && stardata->common.n_events > 0)
+    if(0&&stardata->common.n_events > 0)
     {
         Limit_timestep(*dt,
                        stardata->preferences->minimum_timestep,
-- 
GitLab