From eb42cd3d63b386fcc36584f7ca9a878c0e5e784e Mon Sep 17 00:00:00 2001
From: Cristian C Lalescu <Cristian.Lalescu@ds.mpg.de>
Date: Sun, 31 Jan 2016 15:52:34 +0100
Subject: [PATCH] move particle I/O to cpp lib

---
 bfps/NavierStokes.py         | 39 +++----------------
 bfps/cpp/rFFTW_particles.cpp | 73 ++++++++++++++++++++++--------------
 bfps/cpp/rFFTW_particles.hpp | 15 +++++---
 3 files changed, 61 insertions(+), 66 deletions(-)

diff --git a/bfps/NavierStokes.py b/bfps/NavierStokes.py
index c39c87f1..c60e200b 100644
--- a/bfps/NavierStokes.py
+++ b/bfps/NavierStokes.py
@@ -526,40 +526,13 @@ class NavierStokes(_fluid_particle_base):
                     {0}->field = {1};
                     ps{2}->sample_vec_field({0}, acceleration);
                     """.format(interpolator[s], acc_name, s0 + s)
-            output_vel_acc += """
-                if (myrank == 0)
-                {{
-                //VELOCITY begin
-                std::string temp_string = (std::string("/") +
-                                           std::string(ps{0}->name) +
-                                           std::string("/velocity"));
-                hid_t Cdset = H5Dopen(particle_file, temp_string.c_str(), H5P_DEFAULT);
-                hid_t mspace, wspace;
-                int ndims;
-                hsize_t count[3], offset[3];
-                wspace = H5Dget_space(Cdset);
-                ndims = H5Sget_simple_extent_dims(wspace, count, NULL);
-                count[0] = 1;
-                offset[0] = ps{0}->iteration / ps{0}->traj_skip;
-                offset[1] = 0;
-                offset[2] = 0;
-                mspace = H5Screate_simple(ndims, count, NULL);
-                H5Sselect_hyperslab(wspace, H5S_SELECT_SET, offset, NULL, count, NULL);
-                H5Dwrite(Cdset, H5T_NATIVE_DOUBLE, mspace, wspace, H5P_DEFAULT, velocity);
-                H5Dclose(Cdset);
-                //VELOCITY end\n""".format(s0 + s)
+            output_vel_acc += (
+                    'if (myrank == 0)\n' +
+                    '{\n' +
+                    'ps{0}->write(particle_file, "velocity", velocity);\n'.format(s0 + s))
             if not type(acc_name) == type(None):
-                output_vel_acc += """
-                    //ACCELERATION begin
-                    temp_string = (std::string("/") +
-                                   std::string(ps{0}->name) +
-                                   std::string("/acceleration"));
-                    Cdset = H5Dopen(particle_file, temp_string.c_str(), H5P_DEFAULT);
-                    H5Dwrite(Cdset, H5T_NATIVE_DOUBLE, mspace, wspace, H5P_DEFAULT, acceleration);
-                    H5Sclose(mspace);
-                    H5Sclose(wspace);
-                    H5Dclose(Cdset);
-                    //ACCELERATION end\n""".format(s0 + s)
+                output_vel_acc += (
+                        'ps{0}->write(particle_file, "acceleration", acceleration);\n'.format(s0 + s))
             output_vel_acc += '}\n'
         output_vel_acc += 'delete[] velocity;\n'
         if not type(acc_name) == type(None):
diff --git a/bfps/cpp/rFFTW_particles.cpp b/bfps/cpp/rFFTW_particles.cpp
index 34e90003..a71c0574 100644
--- a/bfps/cpp/rFFTW_particles.cpp
+++ b/bfps/cpp/rFFTW_particles.cpp
@@ -109,7 +109,8 @@ void rFFTW_particles<particle_type, rnumber, interp_neighbours>::roll_rhs()
 
 
 template <int particle_type, class rnumber, int interp_neighbours>
-void rFFTW_particles<particle_type, rnumber, interp_neighbours>::AdamsBashforth(int nsteps)
+void rFFTW_particles<particle_type, rnumber, interp_neighbours>::AdamsBashforth(
+        const int nsteps)
 {
     ptrdiff_t ii;
     this->get_rhs(this->state, this->rhs[0]);
@@ -194,7 +195,8 @@ void rFFTW_particles<particle_type, rnumber, interp_neighbours>::step()
 
 
 template <int particle_type, class rnumber, int interp_neighbours>
-void rFFTW_particles<particle_type, rnumber, interp_neighbours>::read(hid_t data_file_id)
+void rFFTW_particles<particle_type, rnumber, interp_neighbours>::read(
+        const hid_t data_file_id)
 {
     if (this->myrank == 0)
     {
@@ -257,42 +259,57 @@ void rFFTW_particles<particle_type, rnumber, interp_neighbours>::read(hid_t data
 }
 
 template <int particle_type, class rnumber, int interp_neighbours>
-void rFFTW_particles<particle_type, rnumber, interp_neighbours>::write(hid_t data_file_id, bool write_rhs)
+void rFFTW_particles<particle_type, rnumber, interp_neighbours>::write(
+        const hid_t data_file_id,
+        const char *dset_name,
+        const double *data)
+{
+    std::string temp_string = (std::string(this->name) +
+                               std::string("/") +
+                               std::string(dset_name));
+    hid_t dset = H5Dopen(data_file_id, temp_string.c_str(), H5P_DEFAULT);
+    hid_t mspace, wspace;
+    hsize_t count[3], offset[3];
+    wspace = H5Dget_space(dset);
+    H5Sget_simple_extent_dims(wspace, count, NULL);
+    count[0] = 1;
+    offset[0] = this->iteration / this->traj_skip;
+    offset[1] = 0;
+    offset[2] = 0;
+    mspace = H5Screate_simple(3, count, NULL);
+    H5Sselect_hyperslab(wspace, H5S_SELECT_SET, offset, NULL, count, NULL);
+    H5Dwrite(dset, H5T_NATIVE_DOUBLE, mspace, wspace, H5P_DEFAULT, data);
+    H5Sclose(mspace);
+    H5Sclose(wspace);
+    H5Dclose(dset);
+}
+
+template <int particle_type, class rnumber, int interp_neighbours>
+void rFFTW_particles<particle_type, rnumber, interp_neighbours>::write(
+        const hid_t data_file_id,
+        const bool write_rhs)
 {
     if (this->myrank == 0)
     {
-        std::string temp_string = (std::string("/") +
-                                   std::string(this->name) +
-                                   std::string("/state"));
-        hid_t dset = H5Dopen(data_file_id, temp_string.c_str(), H5P_DEFAULT);
-        hid_t mspace, wspace;
-        hsize_t count[4], offset[4];
-        wspace = H5Dget_space(dset);
-        H5Sget_simple_extent_dims(wspace, count, NULL);
-        count[0] = 1;
-        offset[0] = this->iteration / this->traj_skip;
-        offset[1] = 0;
-        offset[2] = 0;
-        mspace = H5Screate_simple(3, count, NULL);
-        H5Sselect_hyperslab(wspace, H5S_SELECT_SET, offset, NULL, count, NULL);
-        H5Dwrite(dset, H5T_NATIVE_DOUBLE, mspace, wspace, H5P_DEFAULT, this->state);
-        H5Sclose(mspace);
-        H5Sclose(wspace);
-        H5Dclose(dset);
+        this->write(data_file_id, "state", this->state);
         if (write_rhs)
         {
-            temp_string = (std::string("/") +
-                           std::string(this->name) +
-                           std::string("/rhs"));
-            dset = H5Dopen(data_file_id, temp_string.c_str(), H5P_DEFAULT);
-            wspace = H5Dget_space(dset);
+            std::string temp_string = (
+                    std::string("/") +
+                    std::string(this->name) +
+                    std::string("/rhs"));
+            hid_t dset = H5Dopen(data_file_id, temp_string.c_str(), H5P_DEFAULT);
+            hid_t wspace = H5Dget_space(dset);
+            hsize_t count[4], offset[4];
             H5Sget_simple_extent_dims(wspace, count, NULL);
             //writing to last available position
             offset[0] = count[0] - 1;
+            offset[1] = 0;
+            offset[2] = 0;
+            offset[3] = 0;
             count[0] = 1;
             count[1] = 1;
-            offset[3] = 0;
-            mspace = H5Screate_simple(4, count, NULL);
+            hid_t mspace = H5Screate_simple(4, count, NULL);
             for (int i=0; i<this->integration_steps; i++)
             {
                 offset[1] = i;
diff --git a/bfps/cpp/rFFTW_particles.hpp b/bfps/cpp/rFFTW_particles.hpp
index b577da05..488e6612 100644
--- a/bfps/cpp/rFFTW_particles.hpp
+++ b/bfps/cpp/rFFTW_particles.hpp
@@ -78,21 +78,26 @@ class rFFTW_particles
                 const int INTEGRATION_STEPS = 2);
         ~rFFTW_particles();
 
-        void get_rhs(double *__restrict__ x, double *__restrict__ rhs);
+        void get_rhs(
+                double *__restrict__ x,
+                double *__restrict__ rhs);
 
-        inline void sample_vec_field(rFFTW_interpolator<rnumber, interp_neighbours> *field, double *vec_values)
+        inline void sample_vec_field(
+                rFFTW_interpolator<rnumber, interp_neighbours> *field,
+                double *vec_values)
         {
             field->sample(this->nparticles, this->ncomponents, this->state, vec_values, NULL);
         }
 
         /* input/output */
-        void read(hid_t data_file_id);
-        void write(hid_t data_file_id, bool write_rhs = true);
+        void read(const hid_t data_file_id);
+        void write(const hid_t data_file_id, const char *dset_name, const double *data);
+        void write(const hid_t data_file_id, const bool write_rhs = true);
 
         /* solvers */
         void step();
         void roll_rhs();
-        void AdamsBashforth(int nsteps);
+        void AdamsBashforth(const int nsteps);
 };
 
 #endif//RFFTW_PARTICLES
-- 
GitLab