From 48bb7c68e3e28dd63688fd259d5e9df1f4441116 Mon Sep 17 00:00:00 2001
From: Loic Hausammann <loic.hausammann@protonmail.ch>
Date: Mon, 9 Mar 2020 16:15:32 +0000
Subject: [PATCH] Sf crash if no star

---
 configure.ac                                  |  6 +++---
 examples/main.c                               | 20 +++++++++++++++++++
 src/Makefile.am                               |  2 +-
 src/runner_others.c                           |  7 +++++++
 src/star_formation/EAGLE/star_formation.h     | 13 ++++++++++++
 src/star_formation/GEAR/star_formation.h      | 16 +++++++++++++++
 src/star_formation/none/star_formation.h      | 12 +++++++++++
 src/task_order.h                              |  4 ++--
 src/task_order/{none => default}/task_order.h |  8 ++++----
 9 files changed, 78 insertions(+), 10 deletions(-)
 rename src/task_order/{none => default}/task_order.h (94%)

diff --git a/configure.ac b/configure.ac
index eff47c85f7..48c66cf3be 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1458,7 +1458,7 @@ with_subgrid_pressure_floor=none
 with_subgrid_stars=none
 with_subgrid_star_formation=none
 with_subgrid_feedback=none
-with_subgrid_task_order=none
+with_subgrid_task_order=default
 
 case "$with_subgrid" in
    yes)
@@ -1475,7 +1475,7 @@ case "$with_subgrid" in
 	with_subgrid_feedback=GEAR
 	with_subgrid_black_holes=none
   # GEAR's order is not used anymore
-	with_subgrid_task_order=none
+	with_subgrid_task_order=default
 	enable_fof=no
    ;;
    EAGLE)
@@ -2009,7 +2009,7 @@ EAGLE)
 AC_DEFINE([TASK_ORDER_EAGLE], [1], [EAGLE task order])
 ;;
 default)
-AC_DEFINE([TASK_ORDER_NONE], [1], [Default (i.e. EAGLE/OWLS) task order])
+AC_DEFINE([TASK_ORDER_DEFAULT], [1], [Default (i.e. EAGLE/OWLS) task order])
 ;;
 GEAR)
 AC_DEFINE([TASK_ORDER_GEAR], [1], [GEAR task order])
diff --git a/examples/main.c b/examples/main.c
index e57a062f2c..770bdd790d 100644
--- a/examples/main.c
+++ b/examples/main.c
@@ -171,6 +171,7 @@ int main(int argc, char *argv[]) {
   int with_structure_finding = 0;
   int with_logger = 0;
   int with_eagle = 0;
+  int with_gear = 0;
   int verbose = 0;
   int nr_threads = 1;
   int with_verbose_timers = 0;
@@ -239,6 +240,13 @@ int main(int argc, char *argv[]) {
           "--star-formation --cooling --feedback --black-holes --fof.",
           NULL, 0, 0),
 
+      OPT_BOOLEAN(
+          0, "gear", &with_gear,
+          "Run with all the options needed for the GEAR model. This is "
+          "equivalent to --hydro --limiter --sync --self-gravity --stars "
+          "--star-formation --cooling --feedback.",
+          NULL, 0, 0),
+
       OPT_GROUP("  Control options:\n"),
       OPT_BOOLEAN('a', "pin", &with_aff,
                   "Pin runners using processor affinity.", NULL, 0, 0),
@@ -310,6 +318,18 @@ int main(int argc, char *argv[]) {
     with_fof = 1;
   }
 
+  /* Deal with GEAR */
+  if (with_gear) {
+    with_hydro = 1;
+    with_timestep_limiter = 1;
+    with_timestep_sync = 1;
+    with_self_gravity = 1;
+    with_stars = 1;
+    with_star_formation = 1;
+    with_cooling = 1;
+    with_feedback = 1;
+  }
+
   /* Write output parameter file */
   if (myrank == 0 && output_parameters_filename != NULL) {
     io_write_output_field_parameter(output_parameters_filename);
diff --git a/src/Makefile.am b/src/Makefile.am
index 7678220bbf..ad4851b02d 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -261,7 +261,7 @@ nobase_noinst_HEADERS = align.h approx_math.h atomic.h barrier.h cycle.h error.h
 		 pressure_floor/GEAR/pressure_floor.h pressure_floor/none/pressure_floor.h \
 		 pressure_floor/GEAR/pressure_floor_iact.h pressure_floor/none/pressure_floor_iact.h \
 		 pressure_floor/GEAR/pressure_floor_struct.h pressure_floor/none/pressure_floor_struct.h \
-	         task_order/GEAR/task_order.h task_order/EAGLE/task_order.h task_order/none/task_order.h
+	         task_order/GEAR/task_order.h task_order/EAGLE/task_order.h task_order/default/task_order.h
 
 
 # Sources and special flags for the gravity library
diff --git a/src/runner_others.c b/src/runner_others.c
index 0ae254b2c0..a130caf222 100644
--- a/src/runner_others.c
+++ b/src/runner_others.c
@@ -356,6 +356,13 @@ void runner_do_star_formation(struct runner *r, struct cell *c, int timer) {
                                    logger_mask_data[logger_consts].mask,
                                /* special flags */ 0);
 #endif
+            } else {
+
+              /* Do something about the fact no star could be formed.
+                 Note that in such cases a tree rebuild to create more free
+                 slots has already been triggered by the function
+                 cell_convert_part_to_spart() */
+              star_formation_no_spart_available(e, p, xp);
             }
           }
 
diff --git a/src/star_formation/EAGLE/star_formation.h b/src/star_formation/EAGLE/star_formation.h
index ce680d4ec1..77b3acf3b0 100644
--- a/src/star_formation/EAGLE/star_formation.h
+++ b/src/star_formation/EAGLE/star_formation.h
@@ -739,4 +739,17 @@ __attribute__((always_inline)) INLINE static void star_formation_split_part(
   if (xp->sf_data.SFR > 0.) xp->sf_data.SFR /= n;
 }
 
+/**
+ * @brief Deal with the case where no spart are available for star formation.
+ *
+ * @param e The #engine.
+ * @param p The #part.
+ * @param xp The #xpart.
+ */
+__attribute__((always_inline)) INLINE static void
+star_formation_no_spart_available(const struct engine* e, const struct part* p,
+                                  const struct xpart* xp) {
+  /* Nothing to do, we just skip it and deal with it next step */
+}
+
 #endif /* SWIFT_EAGLE_STAR_FORMATION_H */
diff --git a/src/star_formation/GEAR/star_formation.h b/src/star_formation/GEAR/star_formation.h
index 8eb60bf38f..f2afc77f9d 100644
--- a/src/star_formation/GEAR/star_formation.h
+++ b/src/star_formation/GEAR/star_formation.h
@@ -309,4 +309,20 @@ __attribute__((always_inline)) INLINE static void star_formation_split_part(
   error("Loic: to be implemented");
 }
 
+/**
+ * @brief Deal with the case where no spart are available for star formation.
+ *
+ * @param e The #engine.
+ * @param p The #part.
+ * @param xp The #xpart.
+ */
+__attribute__((always_inline)) INLINE static void
+star_formation_no_spart_available(const struct engine* e, const struct part* p,
+                                  const struct xpart* xp) {
+  error(
+      "Failed to get a new particle. Please increase "
+      "Scheduler:cell_extra_sparts "
+      "or Scheduler:cell_extra_gparts");
+}
+
 #endif /* SWIFT_GEAR_STAR_FORMATION_H */
diff --git a/src/star_formation/none/star_formation.h b/src/star_formation/none/star_formation.h
index 6155d41564..589cd0f108 100644
--- a/src/star_formation/none/star_formation.h
+++ b/src/star_formation/none/star_formation.h
@@ -238,4 +238,16 @@ star_formation_first_init_part(const struct phys_const* restrict phys_const,
 __attribute__((always_inline)) INLINE static void star_formation_split_part(
     struct part* p, struct xpart* xp, const double n) {}
 
+/**
+ * @brief Deal with the case where no spart are available for star formation.
+ *
+ * @param e The #engine.
+ * @param p The #part.
+ * @param xp The #xpart.
+ */
+__attribute__((always_inline)) INLINE static void
+star_formation_no_spart_available(const struct engine* e, const struct part* p,
+                                  const struct xpart* xp) {
+  /* Nothing to do, we just skip it and deal with it next step */
+}
 #endif /* SWIFT_NONE_STAR_FORMATION_H */
diff --git a/src/task_order.h b/src/task_order.h
index 7924bc2ee3..d5d0165732 100644
--- a/src/task_order.h
+++ b/src/task_order.h
@@ -24,8 +24,8 @@
 /* Local includes */
 #include "scheduler.h"
 
-#ifdef TASK_ORDER_NONE
-#include "task_order/none/task_order.h"
+#ifdef TASK_ORDER_DEFAULT
+#include "task_order/default/task_order.h"
 #elif TASK_ORDER_GEAR
 #include "task_order/GEAR/task_order.h"
 #elif TASK_ORDER_EAGLE
diff --git a/src/task_order/none/task_order.h b/src/task_order/default/task_order.h
similarity index 94%
rename from src/task_order/none/task_order.h
rename to src/task_order/default/task_order.h
index 826999f9dc..511ea5a13a 100644
--- a/src/task_order/none/task_order.h
+++ b/src/task_order/default/task_order.h
@@ -16,11 +16,11 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *
  ******************************************************************************/
-#ifndef SWIFT_TASK_ORDER_NONE_H
-#define SWIFT_TASK_ORDER_NONE_H
+#ifndef SWIFT_TASK_ORDER_DEFAULT_H
+#define SWIFT_TASK_ORDER_DEFAULT_H
 
 /**
- * @file task_order/none/task_order.h
+ * @file task_order/default/task_order.h
  * @brief Defines the order of the subgrid tasks when no subgrid model has
  * been specified. Defaults to the EAGLE model.
  */
@@ -74,4 +74,4 @@ INLINE static void task_order_addunlock_cooling(struct scheduler *s,
   scheduler_addunlock(s, c->hydro.cooling, c->super->kick2);
 }
 
-#endif /* SWIFT_TASK_ORDER_NONE_H */
+#endif /* SWIFT_TASK_ORDER_DEFAULT_H */
-- 
GitLab