From deb10021fdc71146c6a8ded82ce1c169fb1945fc Mon Sep 17 00:00:00 2001
From: loikki <loic.hausammann@protonmail.ch>
Date: Wed, 15 Apr 2020 11:29:20 +0200
Subject: [PATCH] GEAR: do not dump the feedback arrays

---
 .../small_cosmo_volume.yml                    |  25 +--
 src/engine.c                                  |  12 +-
 src/feedback/GEAR/feedback.c                  |   5 +-
 src/feedback/GEAR/feedback.h                  |   3 +-
 src/feedback/GEAR/stellar_evolution.c         |   5 +-
 src/feedback/GEAR/stellar_evolution.h         |   3 +-
 src/feedback/GEAR/supernovae_ii.c             | 165 ++----------------
 src/feedback/GEAR/supernovae_ii.h             |   3 +-
 8 files changed, 42 insertions(+), 179 deletions(-)

diff --git a/examples/SmallCosmoVolume/SmallCosmoVolume_cooling/small_cosmo_volume.yml b/examples/SmallCosmoVolume/SmallCosmoVolume_cooling/small_cosmo_volume.yml
index bf7f56c252..286b7591dd 100644
--- a/examples/SmallCosmoVolume/SmallCosmoVolume_cooling/small_cosmo_volume.yml
+++ b/examples/SmallCosmoVolume/SmallCosmoVolume_cooling/small_cosmo_volume.yml
@@ -99,18 +99,19 @@ EAGLEEntropyFloor:
   
 # Cooling with Grackle 3.0
 GrackleCooling:
-  CloudyTable: CloudyData_UVB=HM2012.h5 # Name of the Cloudy Table (available on the grackle bitbucket repository)
-  WithUVbackground: 1                   # Enable or not the UV background
-  Redshift: -1                           # Redshift to use (-1 means time based redshift)
-  WithMetalCooling: 1                   # Enable or not the metal cooling
-  ProvideVolumetricHeatingRates: 0      # (optional) User provide volumetric heating rates
-  ProvideSpecificHeatingRates: 0        # (optional) User provide specific heating rates
-  SelfShieldingMethod: 0                # (optional) Grackle (<= 3) or Gear self shielding method
-  MaxSteps: 10000                       # (optional) Max number of step when computing the initial composition
-  ConvergenceLimit: 1e-2                # (optional) Convergence threshold (relative) for initial composition
+  cloudy_table: CloudyData_UVB=HM2012.h5 # Name of the Cloudy Table (available on the grackle bitbucket repository)
+  with_UV_background: 1                   # Enable or not the UV background
+  redshift: -1                           # Redshift to use (-1 means time based redshift)
+  with_metal_cooling: 1                   # Enable or not the metal cooling
+  provide_volumetric_heating_rates: 0      # (optional) User provide volumetric heating rates
+  provide_specific_heating_rates: 0        # (optional) User provide specific heating rates
+  self_shielding_method: 0                # (optional) Grackle (<= 3) or Gear self shielding method
+  max_steps: 10000                       # (optional) Max number of step when computing the initial composition
+  convergence_limit: 1e-2                # (optional) Convergence threshold (relative) for initial composition
+  thermal_time_myr: 5
 
-GearChemistry:
-  InitialMetallicity: 0.01295
+GEARChemistry:
+  initial_metallicity: 0.01295
 
 GEARPressureFloor:
-  Jeans_factor: 10.       # Number of particles required to suppose a resolved clump and avoid the pressure floor.
+  jeans_factor: 10.       # Number of particles required to suppose a resolved clump and avoid the pressure floor.
diff --git a/src/engine.c b/src/engine.c
index 2d9d1b9d4e..226ed0768e 100644
--- a/src/engine.c
+++ b/src/engine.c
@@ -5278,13 +5278,13 @@ void engine_struct_dump(struct engine *e, FILE *stream) {
   potential_struct_dump(e->external_potential, stream);
   cooling_struct_dump(e->cooling_func, stream);
   starformation_struct_dump(e->star_formation, stream);
-  feedback_struct_dump(e->feedback_props, stream);
   black_holes_struct_dump(e->black_holes_properties, stream);
   chemistry_struct_dump(e->chemistry, stream);
 #ifdef WITH_FOF
   fof_struct_dump(e->fof_properties, stream);
 #endif
   parser_struct_dump(e->parameter_file, stream);
+  feedback_struct_dump(e->feedback_props, stream);
   if (e->output_list_snapshots)
     output_list_struct_dump(e->output_list_snapshots, stream);
   if (e->output_list_stats)
@@ -5391,11 +5391,6 @@ void engine_struct_restore(struct engine *e, FILE *stream) {
   starformation_struct_restore(star_formation, stream);
   e->star_formation = star_formation;
 
-  struct feedback_props *feedback_properties =
-      (struct feedback_props *)malloc(sizeof(struct feedback_props));
-  feedback_struct_restore(feedback_properties, stream);
-  e->feedback_props = feedback_properties;
-
   struct black_holes_props *black_holes_properties =
       (struct black_holes_props *)malloc(sizeof(struct black_holes_props));
   black_holes_struct_restore(black_holes_properties, stream);
@@ -5419,6 +5414,11 @@ void engine_struct_restore(struct engine *e, FILE *stream) {
   parser_struct_restore(parameter_file, stream);
   e->parameter_file = parameter_file;
 
+  struct feedback_props *feedback_properties =
+    (struct feedback_props *)malloc(sizeof(struct feedback_props));
+  feedback_struct_restore(feedback_properties, stream, e);
+  e->feedback_props = feedback_properties;
+
   if (e->output_list_snapshots) {
     struct output_list *output_list_snapshots =
         (struct output_list *)malloc(sizeof(struct output_list));
diff --git a/src/feedback/GEAR/feedback.c b/src/feedback/GEAR/feedback.c
index 8373019060..e69395c7e7 100644
--- a/src/feedback/GEAR/feedback.c
+++ b/src/feedback/GEAR/feedback.c
@@ -265,12 +265,13 @@ void feedback_struct_dump(const struct feedback_props* feedback, FILE* stream) {
  * @param feedback the struct
  * @param stream the file stream
  */
-void feedback_struct_restore(struct feedback_props* feedback, FILE* stream) {
+void feedback_struct_restore(struct feedback_props* feedback, FILE* stream,
+                             struct engine *e) {
 
   restart_read_blocks((void*)feedback, sizeof(struct feedback_props), 1, stream,
                       NULL, "feedback function");
 
-  stellar_evolution_restore(&feedback->stellar_model, stream);
+  stellar_evolution_restore(&feedback->stellar_model, stream, e);
 }
 
 /**
diff --git a/src/feedback/GEAR/feedback.h b/src/feedback/GEAR/feedback.h
index c4b26b29e7..e48c14bf1a 100644
--- a/src/feedback/GEAR/feedback.h
+++ b/src/feedback/GEAR/feedback.h
@@ -61,7 +61,8 @@ void feedback_evolve_spart(struct spart* restrict sp,
                            const double time, const integertime_t ti_begin,
                            const int with_cosmology);
 void feedback_struct_dump(const struct feedback_props* feedback, FILE* stream);
-void feedback_struct_restore(struct feedback_props* feedback, FILE* stream);
+void feedback_struct_restore(struct feedback_props* feedback, FILE* stream,
+                             struct engine *e);
 void feedback_clean(struct feedback_props* feedback);
 
 /**
diff --git a/src/feedback/GEAR/stellar_evolution.c b/src/feedback/GEAR/stellar_evolution.c
index 0516e9eb30..ecd971dcaa 100644
--- a/src/feedback/GEAR/stellar_evolution.c
+++ b/src/feedback/GEAR/stellar_evolution.c
@@ -488,7 +488,8 @@ void stellar_evolution_dump(const struct stellar_model* sm, FILE* stream) {
  * @param sm the struct
  * @param stream the file stream
  */
-void stellar_evolution_restore(struct stellar_model* sm, FILE* stream) {
+void stellar_evolution_restore(struct stellar_model* sm, FILE* stream,
+                               struct engine *e) {
 
   /* Restore the initial mass function */
   initial_mass_function_restore(&sm->imf, stream, sm);
@@ -500,7 +501,7 @@ void stellar_evolution_restore(struct stellar_model* sm, FILE* stream) {
   supernovae_ia_restore(&sm->snia, stream, sm);
 
   /* Restore the supernovae II model */
-  supernovae_ii_restore(&sm->snii, stream, sm);
+  supernovae_ii_restore(&sm->snii, stream, sm, e);
 }
 
 /**
diff --git a/src/feedback/GEAR/stellar_evolution.h b/src/feedback/GEAR/stellar_evolution.h
index 82bbea7dde..a04d20246b 100644
--- a/src/feedback/GEAR/stellar_evolution.h
+++ b/src/feedback/GEAR/stellar_evolution.h
@@ -63,7 +63,8 @@ void stellar_evolution_props_init(struct stellar_model* sm,
                                   const struct cosmology* cosmo);
 
 void stellar_evolution_dump(const struct stellar_model* sm, FILE* stream);
-void stellar_evolution_restore(struct stellar_model* sm, FILE* stream);
+void stellar_evolution_restore(struct stellar_model* sm, FILE* stream,
+                               struct engine *e);
 
 void stellar_evolution_clean(struct stellar_model* sm);
 
diff --git a/src/feedback/GEAR/supernovae_ii.c b/src/feedback/GEAR/supernovae_ii.c
index a18d87c2c7..9cc11f72f4 100644
--- a/src/feedback/GEAR/supernovae_ii.c
+++ b/src/feedback/GEAR/supernovae_ii.c
@@ -21,6 +21,7 @@
 #include "supernovae_ii.h"
 
 /* Local headers */
+#include "engine.h"
 #include "hdf5_functions.h"
 #include "interpolation.h"
 #include "stellar_evolution.h"
@@ -403,65 +404,6 @@ void supernovae_ii_init(struct supernovae_ii *snii,
  */
 void supernovae_ii_dump(const struct supernovae_ii *snii, FILE *stream,
                         const struct stellar_model *sm) {
-
-  /* Dump the yields. */
-  for (int i = 0; i < GEAR_CHEMISTRY_ELEMENT_COUNT; i++) {
-    const char *element_name = stellar_evolution_get_element_name(sm, i);
-
-    /* Integrated yields */
-    if (snii->integrated.yields[i].data != NULL) {
-      /* Generate name */
-      char name[200];
-      sprintf(name, "%s_int", element_name);
-
-      /* Write the array */
-      restart_write_blocks((void *)snii->integrated.yields[i].data,
-                           sizeof(float), snii->integrated.yields[i].N, stream,
-                           name, name);
-    }
-
-    /* Raw yields */
-    if (snii->raw.yields[i].data != NULL) {
-      /* Generate name */
-      char name[200];
-      sprintf(name, "%s_raw", element_name);
-
-      /* Write the array */
-      restart_write_blocks((void *)snii->raw.yields[i].data, sizeof(float),
-                           snii->raw.yields[i].N, stream, name, name);
-    }
-  }
-
-  /*! Dump the processed mass (integrated). */
-  if (snii->integrated.ejected_mass_processed.data != NULL) {
-    restart_write_blocks((void *)snii->integrated.ejected_mass_processed.data,
-                         sizeof(float),
-                         snii->integrated.ejected_mass_processed.N, stream,
-                         "processed_mass_int", "processed_mass_int");
-  }
-
-  /*! Dump the processed mass (raw). */
-  if (snii->raw.ejected_mass_processed.data != NULL) {
-    restart_write_blocks((void *)snii->raw.ejected_mass_processed.data,
-                         sizeof(float), snii->raw.ejected_mass_processed.N,
-                         stream, "processed_mass_raw", "processed_mass_raw");
-  }
-
-  /*! Dump the non processed mass (integrated). */
-  if (snii->integrated.ejected_mass_non_processed.data != NULL) {
-    restart_write_blocks(
-        (void *)snii->integrated.ejected_mass_non_processed.data, sizeof(float),
-        snii->integrated.ejected_mass_non_processed.N, stream,
-        "non_processed_mass_int", "non_processed_mass_int");
-  }
-
-  /*! Dump the non processed mass (raw). */
-  if (snii->raw.ejected_mass_non_processed.data != NULL) {
-    restart_write_blocks((void *)snii->raw.ejected_mass_non_processed.data,
-                         sizeof(float), snii->raw.ejected_mass_non_processed.N,
-                         stream, "non_processed_mass_raw",
-                         "non_processed_mass_raw");
-  }
 }
 
 /**
@@ -476,103 +418,18 @@ void supernovae_ii_dump(const struct supernovae_ii *snii, FILE *stream,
  * @param sm The #stellar_model.
  */
 void supernovae_ii_restore(struct supernovae_ii *snii, FILE *stream,
-                           const struct stellar_model *sm) {
-
-  /* Restore the yields */
-  for (int i = 0; i < GEAR_CHEMISTRY_ELEMENT_COUNT; i++) {
-    const char *element_name = stellar_evolution_get_element_name(sm, i);
-
-    /* Integrated yields */
-    if (snii->integrated.yields[i].data != NULL) {
-      /* Generate name */
-      char name[200];
-      sprintf(name, "%s_int", element_name);
-
-      /* Allocate the memory */
-      snii->integrated.yields[i].data =
-          (float *)malloc(sizeof(float) * snii->integrated.yields[i].N);
-      if (snii->integrated.yields[i].data == NULL) {
-        error("Failed to allocate memory for the yields");
-      }
-
-      /* Read the data */
-      restart_read_blocks((void *)snii->integrated.yields[i].data,
-                          sizeof(float), snii->integrated.yields[i].N, stream,
-                          NULL, name);
-    }
-
-    /* Raw yields */
-    if (snii->raw.yields[i].data != NULL) {
-      /* Generate name */
-      char name[200];
-      sprintf(name, "%s_raw", element_name);
-
-      /* Allocate the memory */
-      snii->raw.yields[i].data =
-          (float *)malloc(sizeof(float) * snii->raw.yields[i].N);
-      if (snii->raw.yields[i].data == NULL) {
-        error("Failed to allocate memory for the yields");
-      }
-
-      /* Read the data */
-      restart_read_blocks((void *)snii->raw.yields[i].data, sizeof(float),
-                          snii->raw.yields[i].N, stream, NULL, name);
-    }
-  }
-
-  /* Restore the processed mass (integrated) */
-  if (snii->integrated.ejected_mass_processed.data != NULL) {
-    snii->integrated.ejected_mass_processed.data = (float *)malloc(
-        sizeof(float) * snii->integrated.ejected_mass_processed.N);
-    if (snii->integrated.ejected_mass_processed.data == NULL) {
-      error("Failed to allocate memory for the yields");
-    }
-
-    restart_read_blocks((void *)snii->integrated.ejected_mass_processed.data,
-                        sizeof(float),
-                        snii->integrated.ejected_mass_processed.N, stream, NULL,
-                        "processed_mass_int");
-  }
+                           const struct stellar_model *sm,
+                           struct engine *e) {
 
-  /* Restore the processed mass (raw) */
-  if (snii->raw.ejected_mass_processed.data != NULL) {
-    snii->raw.ejected_mass_processed.data =
-        (float *)malloc(sizeof(float) * snii->raw.ejected_mass_processed.N);
-    if (snii->raw.ejected_mass_processed.data == NULL) {
-      error("Failed to allocate memory for the yields");
-    }
-
-    restart_read_blocks((void *)snii->raw.ejected_mass_processed.data,
-                        sizeof(float), snii->raw.ejected_mass_processed.N,
-                        stream, NULL, "processed_mass_raw");
-  }
-
-  /* Restore the non processed mass (integrated) */
-  if (snii->integrated.ejected_mass_non_processed.data != NULL) {
-    snii->integrated.ejected_mass_non_processed.data = (float *)malloc(
-        sizeof(float) * snii->integrated.ejected_mass_non_processed.N);
-    if (snii->integrated.ejected_mass_non_processed.data == NULL) {
-      error("Failed to allocate memory for the yields");
-    }
-
-    restart_read_blocks(
-        (void *)snii->integrated.ejected_mass_non_processed.data, sizeof(float),
-        snii->integrated.ejected_mass_non_processed.N, stream, NULL,
-        "non_processed_mass_int");
-  }
+  /* Read the supernovae yields (and apply the units) */
+  supernovae_ii_read_yields(snii, e->parameter_file, e->physical_constants, sm);
 
-  /* Restore the non processed mass (raw) */
-  if (snii->raw.ejected_mass_non_processed.data != NULL) {
-    snii->raw.ejected_mass_non_processed.data =
-        (float *)malloc(sizeof(float) * snii->raw.ejected_mass_non_processed.N);
-    if (snii->raw.ejected_mass_non_processed.data == NULL) {
-      error("Failed to allocate memory for the yields");
-    }
-
-    restart_read_blocks((void *)snii->raw.ejected_mass_non_processed.data,
-                        sizeof(float), snii->raw.ejected_mass_non_processed.N,
-                        stream, NULL, "non_processed_mass_raw");
-  }
+  /* Get the IMF parameters */
+  snii->exponent = initial_mass_function_get_exponent(&sm->imf, snii->mass_min,
+                                                      snii->mass_max);
+  snii->coef_exp = initial_mass_function_get_coefficient(
+                                                         &sm->imf, snii->mass_min, snii->mass_max);
+  snii->coef_exp /= snii->exponent;
 }
 
 /**
diff --git a/src/feedback/GEAR/supernovae_ii.h b/src/feedback/GEAR/supernovae_ii.h
index ba8f8d105a..22a9146f35 100644
--- a/src/feedback/GEAR/supernovae_ii.h
+++ b/src/feedback/GEAR/supernovae_ii.h
@@ -70,6 +70,7 @@ void supernovae_ii_init(struct supernovae_ii *snii,
 void supernovae_ii_dump(const struct supernovae_ii *snii, FILE *stream,
                         const struct stellar_model *sm);
 void supernovae_ii_restore(struct supernovae_ii *snii, FILE *stream,
-                           const struct stellar_model *sm);
+                           const struct stellar_model *sm,
+                           struct engine *e);
 void supernovae_ii_clean(struct supernovae_ii *snii);
 #endif  // SWIFT_SUPERNOVAE_II_GEAR_H
-- 
GitLab