From 8532b39dbcb19a88624f411443a387185155bb19 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <schaller@strw.leidenuniv.nl>
Date: Thu, 1 Mar 2018 11:39:13 +0100
Subject: [PATCH] Print the cosmological model to the HDF5 snapshots.

---
 src/cosmology.c   | 21 +++++++++++++++++++++
 src/cosmology.h   |  4 ++++
 src/parallel_io.c |  9 +++++++++
 src/serial_io.c   |  9 +++++++++
 src/single_io.c   |  9 +++++++++
 5 files changed, 52 insertions(+)

diff --git a/src/cosmology.c b/src/cosmology.c
index 90dfd6da87..e330031a6c 100644
--- a/src/cosmology.c
+++ b/src/cosmology.c
@@ -30,6 +30,7 @@
 
 /* Local headers */
 #include "adiabatic_index.h"
+#include "common_io.h"
 #include "inline.h"
 #include "restart.h"
 
@@ -627,6 +628,26 @@ void cosmology_clean(struct cosmology *c) {
   free(c->time_interp_table);
 }
 
+#ifdef HAVE_HDF5
+void cosmology_write_model(hid_t h_grp, const struct cosmology *c) {
+
+  io_write_attribute_d(h_grp, "a_beg", c->a_begin);
+  io_write_attribute_d(h_grp, "a_end", c->a_end);
+  io_write_attribute_d(h_grp, "time_beg [internal units]", c->time_begin);
+  io_write_attribute_d(h_grp, "time_end [internal units]", c->time_end);
+  io_write_attribute_d(h_grp, "h", c->h);
+  io_write_attribute_d(h_grp, "H0 [internal units]", c->H0);
+  io_write_attribute_d(h_grp, "Hubble time [internal units]", c->Hubble_time);
+  io_write_attribute_d(h_grp, "Omega_m", c->Omega_m);
+  io_write_attribute_d(h_grp, "Omega_r", c->Omega_r);
+  io_write_attribute_d(h_grp, "Omega_b", c->Omega_b);
+  io_write_attribute_d(h_grp, "Omega_k", c->Omega_k);
+  io_write_attribute_d(h_grp, "Omega_lambda", c->Omega_lambda);
+  io_write_attribute_d(h_grp, "w_0", c->w_0);
+  io_write_attribute_d(h_grp, "w_a", c->w_a);
+}
+#endif
+
 /**
  * @brief Write a cosmology struct to the given FILE as a stream of bytes.
  *
diff --git a/src/cosmology.h b/src/cosmology.h
index 7ecc9afc22..b1b33930ba 100644
--- a/src/cosmology.h
+++ b/src/cosmology.h
@@ -186,6 +186,10 @@ void cosmology_init_no_cosmo(struct cosmology *c);
 void cosmology_print(const struct cosmology *c);
 void cosmology_clean(struct cosmology *c);
 
+#ifdef HAVE_HDF5
+void cosmology_write_model(hid_t h_grp, const struct cosmology *c);
+#endif
+
 /* Dump/restore. */
 void cosmology_struct_dump(const struct cosmology *cosmology, FILE *stream);
 void cosmology_struct_restore(struct cosmology *cosmology, FILE *stream);
diff --git a/src/parallel_io.c b/src/parallel_io.c
index e6e1dfd703..9a534d8e89 100644
--- a/src/parallel_io.c
+++ b/src/parallel_io.c
@@ -929,6 +929,15 @@ void prepare_file(struct engine* e, const char* baseName, long long N_total[6],
     H5Gclose(h_grp);
   }
 
+  /* Print the gravity parameters */
+  if (e->policy & engine_policy_cosmology) {
+    h_grp = H5Gcreate(h_file, "/Cosmology", H5P_DEFAULT, H5P_DEFAULT,
+                      H5P_DEFAULT) if (h_grp < 0)
+        error("Error while creating cosmology group");
+    cosmology_write_model(h_grp, e->cosmology);
+    H5Gclose(h_grp);
+  }
+
   /* Print the runtime parameters */
   h_grp =
       H5Gcreate(h_file, "/Parameters", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
diff --git a/src/serial_io.c b/src/serial_io.c
index 6747b345db..7fb8421086 100644
--- a/src/serial_io.c
+++ b/src/serial_io.c
@@ -837,6 +837,15 @@ void write_output_serial(struct engine* e, const char* baseName,
       H5Gclose(h_grp);
     }
 
+    /* Print the cosmological model */
+    if (e->policy & engine_policy_cosmology) {
+      h_grp = H5Gcreate(h_file, "/Cosmology", H5P_DEFAULT, H5P_DEFAULT,
+                        H5P_DEFAULT);
+      if (h_grp < 0) error("Error while creating cosmology group");
+      cosmology_write_model(h_grp, e->cosmology);
+      H5Gclose(h_grp);
+    }
+
     /* Print the runtime parameters */
     h_grp =
         H5Gcreate(h_file, "/Parameters", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
diff --git a/src/single_io.c b/src/single_io.c
index 862851378d..33b6554468 100644
--- a/src/single_io.c
+++ b/src/single_io.c
@@ -689,6 +689,15 @@ void write_output_single(struct engine* e, const char* baseName,
     H5Gclose(h_grp);
   }
 
+  /* Print the cosmological model  */
+  if (e->policy & engine_policy_cosmology) {
+    h_grp =
+        H5Gcreate(h_file, "/Cosmology", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
+    if (h_grp < 0) error("Error while creating cosmology group");
+    cosmology_write_model(h_grp, e->cosmology);
+    H5Gclose(h_grp);
+  }
+
   /* Print the runtime parameters */
   h_grp =
       H5Gcreate(h_file, "/Parameters", H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
-- 
GitLab