From ffcf3d0456aab80cd3da9e9f9a65b4b72f8841b3 Mon Sep 17 00:00:00 2001
From: Berenger Bramas <bbramas@mpcdf.mpg.de>
Date: Mon, 22 May 2017 11:27:05 +0200
Subject: [PATCH] Update to pass the filename and parent group name

---
 bfps/cpp/full_code/NSVEparticles.cpp          | 31 ++-----------
 .../particles_output_sampling_hdf5.hpp        | 43 ++++++++++++++++---
 bfps/cpp/particles/particles_sampling.hpp     |  6 ++-
 3 files changed, 44 insertions(+), 36 deletions(-)

diff --git a/bfps/cpp/full_code/NSVEparticles.cpp b/bfps/cpp/full_code/NSVEparticles.cpp
index 35585285..b754d270 100644
--- a/bfps/cpp/full_code/NSVEparticles.cpp
+++ b/bfps/cpp/full_code/NSVEparticles.cpp
@@ -73,43 +73,18 @@ int NSVEparticles<rnumber>::do_stats()
     if (!(this->iteration % this->niter_part == 0))
         return EXIT_SUCCESS;
 
-    hid_t file_id, pgroup_id;
-
-    // open particle file, and tracers0 group
-    hid_t plist_id_par = H5Pcreate(H5P_FILE_ACCESS);
-    assert(plist_id_par >= 0);
-    int retTest = H5Pset_fapl_mpio(
-            plist_id_par,
-            this->particles_output_writer_mpi->getComWriter(),
-            MPI_INFO_NULL);
-    assert(retTest >= 0);
-
-    // Parallel HDF5 write
-    file_id = H5Fopen(
-            (this->simname + "_particles.h5").c_str(),
-            H5F_ACC_RDWR | H5F_ACC_DEBUG,
-            plist_id_par);
-    // file_id = H5Fcreate(filename.c_str(), H5F_ACC_TRUNC | H5F_ACC_DEBUG/*H5F_ACC_EXCL*/, H5P_DEFAULT/*H5F_ACC_RDWR*/, plist_id_par);
-    assert(file_id >= 0);
-    H5Pclose(plist_id_par);
-
-    pgroup_id = H5Gopen(
-            file_id,
-            "tracers0",
-            H5P_DEFAULT);
 
     //after fluid stats, cvelocity contains Fourier representation of vel field
     this->fs->cvelocity->ift();
 
-
     // sample velocity
     sample_from_particles_system(*this->fs->cvelocity,// field to save
                                  this->ps,
-                                 pgroup_id, // hdf5 datagroup TODO
+                                 (this->simname + "_particles.h5"), // filename
+                                 "tracers0", // hdf5 parent group
                                  "velocity" // dataset basename TODO
                                  );
-    H5Gclose(pgroup_id);
-    H5Fclose(file_id);
+
     return EXIT_SUCCESS;
 }
 
diff --git a/bfps/cpp/particles/particles_output_sampling_hdf5.hpp b/bfps/cpp/particles/particles_output_sampling_hdf5.hpp
index bcf734d6..05a0aee6 100644
--- a/bfps/cpp/particles/particles_output_sampling_hdf5.hpp
+++ b/bfps/cpp/particles/particles_output_sampling_hdf5.hpp
@@ -18,23 +18,54 @@ class particles_output_sampling_hdf5 : public abstract_particles_output<partsize
                                              size_particle_positions,
                                              size_particle_rhs>;
 
-    hid_t parent_group;
+    hid_t file_id, pgroup_id;
+
     const std::string dataset_name;
     const bool use_collective_io;
 
 public:
     particles_output_sampling_hdf5(MPI_Comm in_mpi_com,
                           const partsize_t inTotalNbParticles,
-                          const hid_t in_parent_group,
+                                   const std::string& in_filename,
+                                   const std::string& in_groupname,
                           const std::string& in_dataset_name,
                           const bool in_use_collective_io = false)
             : Parent(in_mpi_com, inTotalNbParticles, 1),
-              parent_group(in_parent_group),
               dataset_name(in_dataset_name),
-              use_collective_io(in_use_collective_io){}
+              use_collective_io(in_use_collective_io){
+        hid_t plist_id_par = H5Pcreate(H5P_FILE_ACCESS);
+        assert(plist_id_par >= 0);
+        int retTest = H5Pset_fapl_mpio(
+                plist_id_par,
+                Parent::getComWriter(),
+                MPI_INFO_NULL);
+        assert(retTest >= 0);
+
+        // Parallel HDF5 write
+        file_id = H5Fopen(
+                in_filename.c_str(),
+                H5F_ACC_RDWR | H5F_ACC_DEBUG,
+                plist_id_par);
+        assert(file_id >= 0);
+        retTest = H5Pclose(plist_id_par);
+        assert(retTest >= 0);
+
+        pgroup_id = H5Gopen(
+                file_id,
+                in_groupname.c_str(),
+                H5P_DEFAULT);
+        assert(pgroup_id >= 0);
+    }
+
+    ~particles_output_sampling_hdf5(){
+        int retTest = H5Gclose(pgroup_id);
+        assert(retTest >= 0);
+        retTest = H5Fclose(file_id);
+        assert(retTest >= 0);
+    }
 
     void write(
-            const int idx_time_step,
+            const int /*idx_time_step*/,
             const real_number* /*particles_positions*/,
             const std::unique_ptr<real_number[]>* particles_rhs,
             const partsize_t nb_particles,
@@ -65,7 +96,7 @@ public:
             hid_t dataspace = H5Screate_simple(3, datacount, NULL);
             assert(dataspace >= 0);
 
-            hid_t dataset_id = H5Dcreate( parent_group,
+            hid_t dataset_id = H5Dcreate( pgroup_id,
                                           dataset_name.c_str(),
                                           type_id,
                                           dataspace,
diff --git a/bfps/cpp/particles/particles_sampling.hpp b/bfps/cpp/particles/particles_sampling.hpp
index c71c1a01..8ec52c9a 100644
--- a/bfps/cpp/particles/particles_sampling.hpp
+++ b/bfps/cpp/particles/particles_sampling.hpp
@@ -14,7 +14,8 @@
 template <class partsize_t, class particles_rnumber, class rnumber, field_backend be, field_components fc>
 void sample_from_particles_system(const field<rnumber, be, fc>& in_field, // a pointer to a field<rnumber, FFTW, fc>
                                   std::unique_ptr<abstract_particles_system<partsize_t, particles_rnumber>>& ps, // a pointer to an particles_system<double>
-                                  hid_t gid, // an hid_t  identifying an HDF5 group
+                                  const std::string& filename,
+                                  const std::string& parent_groupname,
                                   const std::string& fname){
     const int size_particle_rhs = ncomp(fc);
     const partsize_t nb_particles = ps->getLocalNbParticles();
@@ -26,7 +27,8 @@ void sample_from_particles_system(const field<rnumber, be, fc>& in_field, // a p
 
     particles_output_sampling_hdf5<partsize_t, particles_rnumber, 3, size_particle_rhs> outputclass(MPI_COMM_WORLD,
                                                                                                     ps->getGlobalNbParticles(),
-                                                                                                    gid,
+                                                                                                    filename,
+                                                                                                    parent_groupname,
                                                                                                     datasetname);
     outputclass.save(ps->getParticlesPositions(),
                      &sample_rhs,
-- 
GitLab