From c310e48ccdb9e397ffd1f691daa1a78e4c57742c Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Sat, 1 Dec 2018 16:52:23 +0100
Subject: [PATCH] Add function to clean-up the memory allocated by the cooling
 routines. No more memory leak in the cooling rates test.

---
 examples/CoolingRates/cooling_rates.c |  6 +++++-
 examples/main.c                       |  1 +
 src/cooling/Compton/cooling.h         |  7 +++++++
 src/cooling/EAGLE/cooling.c           | 29 +++++++++++++++++++++++++--
 src/cooling/EAGLE/cooling.h           |  2 ++
 src/cooling/const_du/cooling.h        |  7 +++++++
 src/cooling/const_lambda/cooling.h    |  7 +++++++
 src/cooling/grackle/cooling.h         |  9 +++++++++
 src/cooling/none/cooling.h            |  7 +++++++
 9 files changed, 72 insertions(+), 3 deletions(-)

diff --git a/examples/CoolingRates/cooling_rates.c b/examples/CoolingRates/cooling_rates.c
index c08fc5220a..44b94f3253 100644
--- a/examples/CoolingRates/cooling_rates.c
+++ b/examples/CoolingRates/cooling_rates.c
@@ -135,7 +135,7 @@ int main(int argc, char **argv) {
   // Init cooling
   cooling_init(params, &us, &internal_const, &cooling);
   cooling_print(&cooling);
-  cooling_update(&cosmo, &cooling, 0);
+  cooling_update(&cosmo, &cooling, /*restart=*/0);
 
   // Calculate abundance ratios
   float abundance_ratio[(chemistry_element_count + 2)];
@@ -197,6 +197,10 @@ int main(int argc, char **argv) {
   fclose(output_file);
   message("done cooling rates test");
 
+  /* Clean everything */
+  cosmology_clean(&cosmo);
+  cooling_clean(&cooling);
+
   free(params);
   return 0;
 }
diff --git a/examples/main.c b/examples/main.c
index ecec9637be..dbec184f75 100644
--- a/examples/main.c
+++ b/examples/main.c
@@ -1270,6 +1270,7 @@ int main(int argc, char *argv[]) {
   if (with_verbose_timers) timers_close_file();
   if (with_cosmology) cosmology_clean(e.cosmology);
   if (with_self_gravity) pm_mesh_clean(e.mesh);
+  if (with_cooling) cooling_clean(&cooling_func);
   engine_clean(&e);
   free(params);
 
diff --git a/src/cooling/Compton/cooling.h b/src/cooling/Compton/cooling.h
index 6d22157b45..632e6e07b6 100644
--- a/src/cooling/Compton/cooling.h
+++ b/src/cooling/Compton/cooling.h
@@ -357,4 +357,11 @@ static INLINE void cooling_print_backend(
   message("Cooling function is 'Compton cooling'.");
 }
 
+/**
+ * @brief Clean-up the memory allocated for the cooling routines
+ *
+ * @param cooling the cooling data structure.
+ */
+static INLINE void cooling_clean(struct cooling_function_data* cooling) {}
+
 #endif /* SWIFT_COOLING_COMPTON_H */
diff --git a/src/cooling/EAGLE/cooling.c b/src/cooling/EAGLE/cooling.c
index 875151b5f5..4c6bbc82c9 100644
--- a/src/cooling/EAGLE/cooling.c
+++ b/src/cooling/EAGLE/cooling.c
@@ -1081,7 +1081,7 @@ void cooling_restore_tables(struct cooling_function_data *cooling,
   /* Read relevant cooling tables.
    * Third variable in cooling_update flag to mark restart*/
   allocate_cooling_tables(cooling);
-  cooling_update(cosmo, cooling, 1);
+  cooling_update(cosmo, cooling, /*restart=*/1);
 }
 
 /**
@@ -1089,7 +1089,32 @@ void cooling_restore_tables(struct cooling_function_data *cooling,
  *
  * @param cooling #cooling_function_data struct.
  */
-INLINE void cooling_print_backend(const struct cooling_function_data *cooling) {
+void cooling_print_backend(const struct cooling_function_data *cooling) {
 
   message("Cooling function is 'EAGLE'.");
 }
+
+/**
+ * @brief Clean-up the memory allocated for the cooling routines
+ *
+ * We simply free all the arrays.
+ *
+ * @param cooling the cooling data structure.
+ */
+void cooling_clean(struct cooling_function_data *cooling) {
+
+  /* Free the side arrays */
+  free(cooling->Redshifts);
+  free(cooling->nH);
+  free(cooling->Temp);
+  free(cooling->HeFrac);
+  free(cooling->Therm);
+  free(cooling->SolarAbundances);
+
+  /* Free the tables */
+  free(cooling->table.metal_heating);
+  free(cooling->table.electron_abundance);
+  free(cooling->table.temperature);
+  free(cooling->table.H_plus_He_heating);
+  free(cooling->table.H_plus_He_electron_abundance);
+}
diff --git a/src/cooling/EAGLE/cooling.h b/src/cooling/EAGLE/cooling.h
index 087f63fc63..f365ee9e7f 100644
--- a/src/cooling/EAGLE/cooling.h
+++ b/src/cooling/EAGLE/cooling.h
@@ -103,4 +103,6 @@ void cooling_restore_tables(struct cooling_function_data *,
                             const struct cosmology *);
 
 void dump_cooling_struct(const struct cooling_function_data *);
+
+void cooling_clean(struct cooling_function_data *data);
 #endif /* SWIFT_COOLING_EAGLE_H */
diff --git a/src/cooling/const_du/cooling.h b/src/cooling/const_du/cooling.h
index de2c93edd0..eb9b098105 100644
--- a/src/cooling/const_du/cooling.h
+++ b/src/cooling/const_du/cooling.h
@@ -227,4 +227,11 @@ static INLINE void cooling_print_backend(
           cooling->cooling_rate, cooling->min_energy);
 }
 
+/**
+ * @brief Clean-up the memory allocated for the cooling routines
+ *
+ * @param cooling the cooling data structure.
+ */
+static INLINE void cooling_clean(struct cooling_function_data* cooling) {}
+
 #endif /* SWIFT_COOLING_CONST_DU_H */
diff --git a/src/cooling/const_lambda/cooling.h b/src/cooling/const_lambda/cooling.h
index e8c0f65f5b..eef62330ae 100644
--- a/src/cooling/const_lambda/cooling.h
+++ b/src/cooling/const_lambda/cooling.h
@@ -307,4 +307,11 @@ static INLINE void cooling_print_backend(
             cooling->cooling_tstep_mult);
 }
 
+/**
+ * @brief Clean-up the memory allocated for the cooling routines
+ *
+ * @param cooling the cooling data structure.
+ */
+static INLINE void cooling_clean(struct cooling_function_data* cooling) {}
+
 #endif /* SWIFT_COOLING_CONST_LAMBDA_H */
diff --git a/src/cooling/grackle/cooling.h b/src/cooling/grackle/cooling.h
index 470a4f31ee..ce71a37065 100644
--- a/src/cooling/grackle/cooling.h
+++ b/src/cooling/grackle/cooling.h
@@ -825,5 +825,14 @@ __attribute__((always_inline)) INLINE static void cooling_init_backend(
  */
 static INLINE void cooling_restore_tables(struct cooling_function_data* cooling,
                                           const struct cosmology* cosmo) {}
+/**
+ * @brief Clean-up the memory allocated for the cooling routines
+ *
+ * @param cooling the cooling data structure.
+ */
+static INLINE void cooling_clean(struct cooling_function_data* cooling) {
+
+  // MATTHIEU: To do: free stuff here
+}
 
 #endif /* SWIFT_COOLING_GRACKLE_H */
diff --git a/src/cooling/none/cooling.h b/src/cooling/none/cooling.h
index 9a44ecbd09..868bfad7fc 100644
--- a/src/cooling/none/cooling.h
+++ b/src/cooling/none/cooling.h
@@ -171,4 +171,11 @@ static INLINE void cooling_print_backend(
   message("Cooling function is 'No cooling'.");
 }
 
+/**
+ * @brief Clean-up the memory allocated for the cooling routines
+ *
+ * @param cooling the cooling data structure.
+ */
+static INLINE void cooling_clean(struct cooling_function_data* cooling) {}
+
 #endif /* SWIFT_COOLING_NONE_H */
-- 
GitLab