From d272ffda587ed0a558411bdfa8d4a5bf0b6f493b Mon Sep 17 00:00:00 2001
From: Pietro Incardona <incardon@mpi-cbg.de>
Date: Tue, 1 Dec 2020 20:03:48 +0100
Subject: [PATCH] Fixing subset

---
 openfpm_numerics                              |   2 +-
 src/Vector/Iterators/vector_dist_iterator.hpp |   4 +-
 src/Vector/vector_dist.hpp                    |   6 +
 src/Vector/vector_dist_subset.hpp             | 134 +++++++++++++++---
 4 files changed, 124 insertions(+), 22 deletions(-)

diff --git a/openfpm_numerics b/openfpm_numerics
index 4719f9ee3..407000dc0 160000
--- a/openfpm_numerics
+++ b/openfpm_numerics
@@ -1 +1 @@
-Subproject commit 4719f9ee323bfc85705367eccffd616cce102526
+Subproject commit 407000dc0e87ebaba949ca678616b1819996cb1c
diff --git a/src/Vector/Iterators/vector_dist_iterator.hpp b/src/Vector/Iterators/vector_dist_iterator.hpp
index 5797a47eb..7afa58286 100644
--- a/src/Vector/Iterators/vector_dist_iterator.hpp
+++ b/src/Vector/Iterators/vector_dist_iterator.hpp
@@ -111,7 +111,7 @@ class vector_dist_iterator_subset
 	//! end point
 	size_t stop;
 
-	openfpm::vector<aggregate<int>> & pid;
+	const openfpm::vector<aggregate<int>> & pid;
 
 	public:
 
@@ -121,7 +121,7 @@ class vector_dist_iterator_subset
 	 * \param stop end point
 	 *
 	 */
-	vector_dist_iterator_subset(size_t start, size_t stop, openfpm::vector<aggregate<int>> & pid)
+	vector_dist_iterator_subset(size_t start, size_t stop, const openfpm::vector<aggregate<int>> & pid)
 	:v_it(start),stop(stop),pid(pid)
 	{
 	}
diff --git a/src/Vector/vector_dist.hpp b/src/Vector/vector_dist.hpp
index cda967b44..4a9d70d94 100644
--- a/src/Vector/vector_dist.hpp
+++ b/src/Vector/vector_dist.hpp
@@ -2334,6 +2334,12 @@ public:
 #endif
 	}
 
+	/*! \brief Stub does not do anything
+	*
+	*/
+	void ghost_get_subset()
+	{}
+
 	/*! \brief It synchronize the properties and position of the ghost particles
 	 *
 	 * \tparam prp list of properties to get synchronize
diff --git a/src/Vector/vector_dist_subset.hpp b/src/Vector/vector_dist_subset.hpp
index 33f7c3e6d..d79051574 100644
--- a/src/Vector/vector_dist_subset.hpp
+++ b/src/Vector/vector_dist_subset.hpp
@@ -7,6 +7,36 @@
 
 #include "vector_dist.hpp"
 
+template<unsigned int dim,
+        typename St,
+        typename prop,
+        typename Decomposition = CartDecomposition<dim,St>,
+        typename Memory = HeapMemory,
+        template<typename> class layout_base = memory_traits_lin>
+class vector_dist_ws : public vector_dist<dim,St,typename AggregateAppend<int,prop>::type,Decomposition,Memory,layout_base>
+{
+    public:
+
+    using vector_dist<dim,St,typename AggregateAppend<int,prop>::type,Decomposition,Memory,layout_base>::vector_dist;
+
+    typedef boost::mpl::int_<AggregateAppend<int,prop>::type::max_prop-1> flag_prop;
+
+    void setSubset(vect_dist_key_dx key, int sub_id)
+    {
+        this->template getProp<flag_prop::value>(key) = sub_id;
+    }
+
+    void ghost_get_subset()
+    {
+        this->template ghost_get<flag_prop::value>(NO_POSITION | SKIP_LABELLING);
+    }
+
+    void getLastSubset(int sub_id)
+    {
+        this->template getProp<flag_prop::value>(this->size_local()-1) = sub_id;
+    }
+};
+
 template<unsigned int dim,
         typename St,
         typename prop,
@@ -15,21 +45,27 @@ template<unsigned int dim,
         template<typename> class layout_base = memory_traits_lin>
 class vector_dist_subset
 {
-    typedef vector_dist<dim,St,prop,Decomposition,Memory,layout_base> ivector_dist;
+    typedef vector_dist_ws<dim,St,prop,Decomposition,Memory,layout_base> ivector_dist;
+
+    typedef boost::mpl::int_<AggregateAppend<int,prop>::type::max_prop-1> flag_prop;
 
     ivector_dist & vd;
 
-    openfpm::vector<aggregate<int>> & pid;
+    openfpm::vector<aggregate<int>> pid;
 
-    size_t g_m = 0;
+    size_t sub_id;
 
-    void update_gm()
+    void check_gm()
     {
-    	g_m = 0;
+        #ifdef SE_CLASS1
         for (size_t i = 0 ; i < pid.size() ; i++)
         {
-            g_m += (pid.template get<0>(i) < vd.size_local())?1:0;
+            if (pid.template get<0>(i) >= vd.size_local())
+            {
+                std::cout << __FILE__ << ":" << __LINE__ << " Error you have ghost particles in your subset" << std::endl;
+            }
         }
+        #endif
     }
 
 public:
@@ -50,27 +86,68 @@ public:
     //!
     typedef int yes_i_am_vector_dist;
 
-    vector_dist_subset(vector_dist<dim,St,prop,Decomposition,Memory,layout_base> & vd,
-                       openfpm::vector<aggregate<int>> & pid)
-                       :vd(vd),pid(pid)
+    vector_dist_subset(vector_dist_ws<dim,St,prop,Decomposition,Memory,layout_base> & vd,
+                       int sub_id)
+                       :vd(vd),pid(pid),sub_id(sub_id)
     {
-    	update_gm();
+        // construct pid vector
+
+        auto it = vd.getDomainIterator();
+
+        while (it.isNext())
+        {
+            auto p = it.get();
+
+            if (vd.template getProp<flag_prop::value>(p) == sub_id)
+            {
+                pid.add();
+                pid.template get<0>(pid.size()-1) = p.getKey();
+            }
+
+            ++it;
+        }
+
+    	check_gm();
+    }
+
+    void ghost_get_subset()
+    {
+        vd.template ghost_get<flag_prop::value>(NO_POSITION | SKIP_LABELLING);
     }
 
+    /*! \brief Return the ids
+     *
+     * \return the ids of the subset
+     * 
+     */
+    openfpm::vector<aggregate<int>> & getIds()
+    {
+        return pid;
+    }
 
     /*! \brief Update the subset indexes
      *
      */
-    inline void update(openfpm::vector<aggregate<int>> & pid)
+    inline void update()
     {
-    	this->pid.resize(pid.size());
+        pid.clear();
 
-        for (size_t i = 0 ; i < pid.size() ; i++)
+        auto it = vd.getDomainIterator();
+
+        while (it.isNext())
         {
-            this->pid.template get<0>(i) = pid.template get<0>(i);
+            auto p = it.get();
+
+            if (vd.template getProp<flag_prop::value>(p) == sub_id)
+            {
+                pid.add();
+                pid.template get<0>(pid.size()-1) = p.getKey();
+            }
+
+            ++it;
         }
 
-        update_gm();
+        check_gm();
     }
 
     /*! \brief Get the decomposition
@@ -100,7 +177,7 @@ public:
  */
     size_t size_local() const
     {
-        return g_m;
+        return pid.size();
     }
 
     /*! \brief return the local size of the original vector
@@ -220,7 +297,7 @@ public:
         se3.getIterator();
 #endif
 
-        return vector_dist_iterator_subset(0, g_m,pid);
+        return vector_dist_iterator_subset(0,pid.size(),pid);
     }
 
 	/*! \brief Construct a cell list starting from the stored particles
@@ -294,7 +371,7 @@ public:
 		cl_param_calculate(pbox, div, r_cut, enlarge);
 
 		cell_list.Initialize(pbox, div);
-		cell_list.set_gm(g_m);
+		cell_list.set_gm(pid.size());
 		cell_list.set_ndec(vd.getDecomposition().get_ndec());
 
 		cell_list.clear();
@@ -312,6 +389,24 @@ public:
 			++it;
 		}
 
+        // Add also the ghost
+
+        auto git = vd.getGhostIterator();
+
+		while (git.isNext())
+		{
+			auto key = git.get();
+
+			Point<dim,St> pos = vd.getPos(key);
+
+            if (vd.template getProp<flag_prop::value>(key) == sub_id)
+            {
+			    cell_list.add(pos,key.getKey());
+            }
+
+			++git;
+		}
+
 		return cell_list;
 	}
 
@@ -327,10 +422,11 @@ public:
 	{
 	    vd = v.vd;
 	    pid = v.pid;
-	    g_m = v.g_m;
 
 		return *this;
 	}
 };
 
+
+
 #endif //OPENFPM_PDATA_VECTOR_DIST_SUBSET_HPP
-- 
GitLab