From 9e4b5f100f03d5129f6d898ea70184aaf40eb31c Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Fri, 19 Jul 2024 13:18:40 +0200
Subject: [PATCH] Downstream fix to the LOS code which could overflow the
 parser parameter list size in the middle of a run. A check is now in place at
 the start

---
 src/engine_config.c |  5 ++++-
 src/line_of_sight.c | 34 ++++++++++++++++++++++++++++++++++
 src/line_of_sight.h |  5 ++++-
 src/parser.h        |  2 +-
 4 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/src/engine_config.c b/src/engine_config.c
index e6c68d9db5..fb18cc48cb 100644
--- a/src/engine_config.c
+++ b/src/engine_config.c
@@ -37,6 +37,7 @@
 
 /* Local headers. */
 #include "fof.h"
+#include "line_of_sight.h"
 #include "mpiuse.h"
 #include "part.h"
 #include "pressure_floor.h"
@@ -792,9 +793,11 @@ void engine_config(int restart, int fof, struct engine *e,
     /* Find the time of the first statistics output */
     engine_compute_next_statistics_time(e);
 
-    /* Find the time of the first line of sight output */
+    /* Find the time of the first line of sight output
+     * and verify the outputs */
     if (e->policy & engine_policy_line_of_sight) {
       engine_compute_next_los_time(e);
+      los_io_output_check(e);
     }
 
     /* Find the time of the first stf output */
diff --git a/src/line_of_sight.c b/src/line_of_sight.c
index 40e71f9602..55dc0222c8 100644
--- a/src/line_of_sight.c
+++ b/src/line_of_sight.c
@@ -152,6 +152,40 @@ void los_init(const double dim[3], struct los_props *los_params,
       los_params->range_when_shooting_down_axis[2]);
 }
 
+void los_io_output_check(const struct engine *e) {
+
+  /* What kind of run are we working with? */
+  struct swift_params *params = e->parameter_file;
+  const int with_cosmology = e->policy & engine_policy_cosmology;
+  const int with_cooling = e->policy & engine_policy_cooling;
+  const int with_temperature = e->policy & engine_policy_temperature;
+  const int with_fof = e->policy & engine_policy_fof;
+#ifdef HAVE_VELOCIRAPTOR
+  const int with_stf = (e->policy & engine_policy_structure_finding) &&
+                       (e->s->gpart_group_data != NULL);
+#else
+  const int with_stf = 0;
+#endif
+  const int with_rt = e->policy & engine_policy_rt;
+
+  int num_fields = 0;
+  struct io_props list[100];
+
+  /* Find all the gas output fields */
+  io_select_hydro_fields(e->s->parts, e->s->xparts, with_cosmology, with_cooling,
+                         with_temperature, with_fof, with_stf, with_rt, e,
+                         &num_fields, list);
+
+  /* Loop over each output field */
+  for (int i = 0; i < num_fields; i++) {
+
+    /* Did the user cancel this field? */
+    char field[PARSER_MAX_LINE_SIZE];
+    sprintf(field, "SelectOutputLOS:%.*s", FIELD_BUFFER_SIZE, list[i].name);
+    parser_get_opt_param_int(params, field, 1);
+  }
+}
+
 /**
  *  @brief Create a #line_of_sight object from its attributes
  */
diff --git a/src/line_of_sight.h b/src/line_of_sight.h
index 158c0fce6c..6abf0832f1 100644
--- a/src/line_of_sight.h
+++ b/src/line_of_sight.h
@@ -25,9 +25,11 @@
 #include <config.h>
 
 /* Local includes. */
-#include "engine.h"
 #include "io_properties.h"
 
+/* Pre-declarations */
+struct engine;
+
 /**
  * @brief Maps the LOS axis geometry to the simulation axis geometry.
  *
@@ -117,6 +119,7 @@ void print_los_info(const struct line_of_sight *Los, const int i);
 void do_line_of_sight(struct engine *e);
 void los_init(const double dim[3], struct los_props *los_params,
               struct swift_params *params);
+void los_io_output_check(const struct engine *e);
 
 void los_struct_dump(const struct los_props *internal_los, FILE *stream);
 void los_struct_restore(const struct los_props *internal_los, FILE *stream);
diff --git a/src/parser.h b/src/parser.h
index 072bbe03b7..e97188b48c 100644
--- a/src/parser.h
+++ b/src/parser.h
@@ -32,7 +32,7 @@
 
 /* Some constants. */
 #define PARSER_MAX_LINE_SIZE 256
-#define PARSER_MAX_NO_OF_PARAMS 600
+#define PARSER_MAX_NO_OF_PARAMS 700
 #define PARSER_MAX_NO_OF_SECTIONS 64
 
 /* A parameter in the input file */
-- 
GitLab