From ae60541daf8e40cb2ef104e1defba5b48b928633 Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Wed, 3 Apr 2013 23:27:52 +0000
Subject: [PATCH] The code now writes the time to the HDF5 header as well as
 other legacy GADGET-2 attributes allowing the reading of the file with
 Gadgetviewer.

Former-commit-id: 14c74a23cef421dc98b1590b461528dcadd7ab95
---
 examples/test.c | 15 ++++++++-------
 src/io.c        | 20 +++++++++++++++++---
 src/io.h        |  2 +-
 3 files changed, 26 insertions(+), 11 deletions(-)

diff --git a/examples/test.c b/examples/test.c
index 61ffcf76b2..5a1de5c571 100644
--- a/examples/test.c
+++ b/examples/test.c
@@ -795,15 +795,11 @@ int main ( int argc , char *argv[] ) {
 
     /* Dump the first few particles. */
     // for(k=0; k<10; ++k)
-    //   printParticle(parts, k);
+    //   printParticle(parts, k, N);
     // printParticle( parts , 6178 , N );
     // pairs_single( dim , 8525152967533 , parts , N , periodic );
     // printParticle( parts , 515150 );
     // printParticle( parts , 494849 );
-
-    tic = getticks();
-    write_output("output.hdf5", dim, parts, N, periodic);
-    printf( "main: writing particle properties took %.3f ms.\n" , ((double)(getticks() - tic)) / CPU_TPS * 1000 ); fflush(stdout);
             
     /* Dump the kernel to make sure its ok. */
     // kernel_dump( 100 );
@@ -822,7 +818,7 @@ int main ( int argc , char *argv[] ) {
     tic = getticks();
     space_init( &s , dim , parts , N , periodic , h_max );
     printf( "main: space_init took %.3f ms.\n" , ((double)(getticks() - tic)) / CPU_TPS * 1000 ); fflush(stdout);
-    
+
     /* Set the default time step to 1.0f. */
     printf( "main: dt_max is %f.\n" , dt_max );
     
@@ -850,6 +846,11 @@ int main ( int argc , char *argv[] ) {
     tic = getticks();
     engine_init( &e , &s , dt_max , nr_threads , nr_queues , engine_policy_steal | engine_policy_keep );
     printf( "main: engine_init took %.3f ms.\n" , ((double)(getticks() - tic)) / CPU_TPS * 1000 ); fflush(stdout);
+
+    /* Write the state of the system as it is before starting time integration. */
+    tic = getticks();
+    write_output("output.hdf5", &e);
+    printf( "main: writing particle properties took %.3f ms.\n" , ((double)(getticks() - tic)) / CPU_TPS * 1000 ); fflush(stdout);
     
     /* Init the runner history. */
     #ifdef HIST
@@ -873,7 +874,7 @@ int main ( int argc , char *argv[] ) {
         if(j % 100 == 0) {
             char fileName[200];
             sprintf(fileName, "output_%05i.hdf5", j); 
-            write_output(fileName, dim, parts, N, periodic);
+            write_output(fileName, &e);
             }
         
         /* Dump the first few particles. */
diff --git a/src/io.c b/src/io.c
index a6d3914411..7a1c3a5f3a 100644
--- a/src/io.c
+++ b/src/io.c
@@ -36,6 +36,7 @@
 #include "task.h"
 #include "part.h"
 #include "space.h"
+#include "engine.h"
 
 /**
  * @brief Error macro
@@ -545,10 +546,16 @@ void writeArrayBackEnd(hid_t grp, char* fileName, FILE* xmfFile, char* name, enu
  * Calls #error() if an error occurs.
  *
  */
-void write_output ( char* fileName, double dim[3], struct part *parts,  int N, int periodic)
+void write_output ( char* fileName, struct engine *e)
 {
+  
   hid_t h_file=0, h_grp=0;
+  int N = e->s->nr_parts;
+  int periodic = e->s->periodic;
   int numParticles[6]={N,0};
+  int numParticlesHighWord[6]={0};
+  int numFiles = 1;
+  struct part* parts = e->s->parts;
   FILE* xmfFile;
 
   writeXMFheader(&xmfFile, fileName, N);
@@ -584,9 +591,16 @@ void write_output ( char* fileName, double dim[3], struct part *parts,  int N, i
     error("Error while creating file header\n");
     
   /* Read the relevant information and print status */
-  writeAttribute(h_grp, "BoxSize", DOUBLE, &dim[0], 1);
-  writeAttribute(h_grp, "NumPart_Total", UINT, numParticles, 6);
+  writeAttribute(h_grp, "BoxSize", DOUBLE, e->s->dim, 1);
+  writeAttribute(h_grp, "NumPart_ThisFile", UINT, numParticles, 6);
+  writeAttribute(h_grp, "Time", DOUBLE, &e->time, 1);
 
+  /* GADGET-2 legacy values */
+  writeAttribute(h_grp, "NumPart_Total", UINT, numParticles, 6);
+  writeAttribute(h_grp, "NumPart_Total_HighWord", UINT, numParticlesHighWord, 6);
+  writeAttribute(h_grp, "Flag_Entropy_ICs", UINT, numParticlesHighWord, 6);
+  writeAttribute(h_grp, "NumFilesPerSnapshot", INT, &numFiles, 1);
+  
   /* Close header */
   H5Gclose(h_grp);
 		  
diff --git a/src/io.h b/src/io.h
index 88608e29b5..a99d7684de 100644
--- a/src/io.h
+++ b/src/io.h
@@ -22,6 +22,6 @@
 
 void read_ic ( char* fileName, double dim[3], struct part **parts,  int* N, int* periodic);
 
-void write_output ( char* fileName, double dim[3], struct part *parts,  int N, int periodic);
+void write_output ( char* fileName, struct engine* e);
 
 
-- 
GitLab