From 1ccae05145fa80cba9e824770678a8ecbb5f306b Mon Sep 17 00:00:00 2001
From: Pietro Incardona <i-bird@linux.fritz.box>
Date: Tue, 9 Aug 2016 23:43:39 +0200
Subject: [PATCH] fixing examples

---
 example/Vector/4_complex_prop/Makefile        |  21 ++
 example/Vector/4_complex_prop/main.cpp        | 236 ++++++++++++++++++
 example/Vector/4_multiphase_celllist/Makefile |  21 ++
 example/Vector/4_multiphase_celllist/main.cpp | 197 +++++++++++++++
 4 files changed, 475 insertions(+)
 create mode 100644 example/Vector/4_complex_prop/Makefile
 create mode 100644 example/Vector/4_complex_prop/main.cpp
 create mode 100644 example/Vector/4_multiphase_celllist/Makefile
 create mode 100644 example/Vector/4_multiphase_celllist/main.cpp

diff --git a/example/Vector/4_complex_prop/Makefile b/example/Vector/4_complex_prop/Makefile
new file mode 100644
index 000000000..7fc11a405
--- /dev/null
+++ b/example/Vector/4_complex_prop/Makefile
@@ -0,0 +1,21 @@
+include ../../example.mk
+
+CC=mpic++
+
+LDIR =
+
+OBJ = main.o
+
+%.o: %.cpp
+	$(CC) -O3 -c --std=c++11 -o $@ $< $(INCLUDE_PATH)
+
+vect: $(OBJ)
+	$(CC) -o $@ $^ $(CFLAGS) $(LIBS_PATH) $(LIBS)
+
+all: vect
+
+.PHONY: clean all
+
+clean:
+	rm -f *.o *~ core vect
+
diff --git a/example/Vector/4_complex_prop/main.cpp b/example/Vector/4_complex_prop/main.cpp
new file mode 100644
index 000000000..7d9edeb5c
--- /dev/null
+++ b/example/Vector/4_complex_prop/main.cpp
@@ -0,0 +1,236 @@
+/*!
+ *
+ * \page Vector_4_complex_prop Vector 4 complex property
+ *
+ *
+ * [TOC]
+ *
+ *
+ * # Vector 4 complex property # {#vector_example_cp}
+ *
+ *
+ * This example show how we can use complex properties in a vector
+ *
+ */
+
+#include "Vector/vector_dist.hpp"
+
+int main(int argc, char* argv[])
+{
+	/*!
+	 *
+	 * \page Vector_4_complex_prop Vector 4 complex property
+	 *
+	 *
+	 * ## Initialization and vector creation ##
+	 *
+	 * After we initialize the library we can create a vector with complex properties
+	 * with the following line
+	 *
+	 * \snippet Vector/4_complex_prop/main.cpp vect create
+	 *
+	 * In This this particular case every particle carry a scalar,
+	 * a vector in form of float[3], a Point, a list
+	 * in form of vector of float and a list of custom structures
+	 *
+	 * \snippet Vector/4_complex_prop/main.cpp struct A
+	 *
+	 *
+	 */
+
+    // initialize the library
+	openfpm_init(&argc,&argv);
+
+	// Here we define our domain a 2D box with internals from 0 to 1.0 for x and y
+	Box<2,float> domain({0.0,0.0},{1.0,1.0});
+
+	// Here we define the boundary conditions of our problem
+    size_t bc[2]={PERIODIC,PERIODIC};
+
+	// extended boundary around the domain, and the processor domain
+	Ghost<2,float> g(0.01);
+	
+	//! \cond [struct A] \endcond
+
+	// The a custom structure
+	struct A
+	{
+		float p1;
+		int p2;
+
+		A() {};
+
+		A(float p1, int p2)
+		:p1(p1),p2(p2)
+		{}
+	};
+
+	//! \cond [struct A] \endcond
+
+	// the scalar is the element at position 0 in the aggregate
+	constexpr int scalar = 0;
+
+	// the vector is the element at position 1 in the aggregate
+	constexpr int vector = 1;
+
+	// the tensor is the element at position 2 in the aggregate
+	constexpr int point = 2;
+
+	// A list1
+	constexpr int list = 3;
+
+	// A listA
+	constexpr int listA = 4;
+
+	//! \cond [vect create] \endcond
+
+	vector_dist<2,float, aggregate<float,
+	                               float[3],
+								   Point<3,double>,
+								   openfpm::vector<float>,
+	                               openfpm::vector<A>>>
+	vd(4096,domain,bc,g);
+
+	//! \cond [vect create] \endcond
+
+	/*!
+	 *
+	 * \page Vector_4_complex_prop Vector 4 complex property
+	 *
+	 *
+	 * ## Assign values to properties ##
+	 *
+	 * Assign values to properties does not changes, from the simple case. Consider
+	 * now that each particle has a list, so when we can get the property listA for particle p
+	 * and resize such list with **vd.getProp<listA>(p).resize(...)**. We can add new elements at the
+	 * end with **vd.getProp<listA>(p).add(...)** and get some element with **vd.getProp<listA>(p).get(i)**.
+	 * More in general from vd.getProp<listA>(p) we can use the full openfpm::vector interface.
+	 *
+	 * \snippet Vector/4_complex_prop/main.cpp vect assign
+	 *
+	 */
+
+	//! \cond [vect assign] \endcond
+
+	auto it = vd.getDomainIterator();
+
+	while (it.isNext())
+	{
+		auto p = it.get();
+
+		// we define x, assign a random position between 0.0 and 1.0
+		vd.getPos(p)[0] = (float)rand() / RAND_MAX;
+
+		// we define y, assign a random position between 0.0 and 1.0
+		vd.getPos(p)[1] = (float)rand() / RAND_MAX;
+
+
+		vd.getProp<scalar>(p) = 1.0;
+
+		vd.getProp<vector>(p)[0] = 1.0;
+		vd.getProp<vector>(p)[1] = 1.0;
+		vd.getProp<vector>(p)[2] = 1.0;
+
+		vd.getProp<point>(p).get(0) = 1.0;
+		vd.getProp<point>(p).get(1) = 1.0;
+		vd.getProp<point>(p).get(2) = 1.0;
+
+		size_t n_cp = (float)10 * rand()/RAND_MAX;
+
+		vd.getProp<listA>(p).resize(n_cp);
+
+		for (size_t i = 0 ; i < n_cp ; i++)
+		{
+			vd.getProp<list>(p).add(i + 10);
+			vd.getProp<list>(p).add(i + 20);
+			vd.getProp<list>(p).add(i + 30);
+
+			vd.getProp<listA>(p).get(i) = A(i+10.0,i+20.0);
+			vd.getProp<listA>(p).get(i) = A(i+30.0,i+40.0);
+			vd.getProp<listA>(p).get(i) = A(i+50.0,i+60.0);
+		}
+
+		// next particle
+		++it;
+	}
+
+	//! \cond [vect assign] \endcond
+
+	/*!
+	 *
+	 * \page Vector_4_complex_prop Vector 4 complex property
+	 *
+	 *
+	 * ## Mapping and ghost_get ##
+	 *
+	 * Particles are redistributed across processors but only the scalar,vector and the point
+	 * are communicated (properties 0,1,2). A lot of time complex properties can be recomputed and
+	 * communicate them is not a good idea. The same concept also apply for ghost_get
+	 *
+	 * \note OpenFPM <= 0.5.0 cannot communicate complex properties like a vector or other structure
+	 *                        that are not POD object
+	 *
+	 * \note OpenFPM > 0.5.0 Does not have such limitation
+	 *
+	 *
+	 * \snippet Vector/4_complex_prop/main.cpp vect map ghost
+	 *
+	 */
+
+	//! \cond [vect map ghost] \endcond
+
+	// Particles are redistribued across the processors but only the scalar,vector, and point properties
+	// are transfert
+	vd.map_list<KillParticle,scalar,vector,point>();
+	
+	// Synchronize the ghost
+	vd.ghost_get<scalar,vector,point>();
+
+	//! \cond [vect map ghost] \endcond
+
+	/*!
+	 *
+	 * \page Vector_4_complex_prop Vector 4 complex property
+	 *
+	 *
+	 * ## Output and VTK visualization ##
+	 *
+	 * Vector with complex properties can be still be visualized, because unknown properties are
+	 * automatically excluded
+	 *
+	 * \snippet Vector/4_complex_prop/main.cpp vtk
+	 *
+	 */
+
+	//! \cond [vtk] \endcond
+
+	vd.write("particles");
+
+	//! \cond [vtk] \endcond
+
+	/*!
+	 * \page Vector_4_complex_prop Vector 4 complex property
+	 *
+	 * ## Finalize ## {#finalize}
+	 *
+	 *  At the very end of the program we have always to de-initialize the library
+	 *
+	 * \snippet Vector/4_complex_prop/main.cpp finalize
+	 *
+	 */
+
+	//! \cond [finalize] \endcond
+
+	openfpm_finalize();
+
+	//! \cond [finalize] \endcond
+
+	/*!
+	 * \page Vector_4_complex_prop Vector 4 complex property
+	 *
+	 * # Full code # {#code}
+	 *
+	 * \include Vector/4_complex_prop/main.cpp
+	 *
+	 */
+}
diff --git a/example/Vector/4_multiphase_celllist/Makefile b/example/Vector/4_multiphase_celllist/Makefile
new file mode 100644
index 000000000..c2ffeb025
--- /dev/null
+++ b/example/Vector/4_multiphase_celllist/Makefile
@@ -0,0 +1,21 @@
+include ../../example.mk
+
+CC=mpic++
+
+LDIR =
+
+OBJ = main.o
+
+%.o: %.cpp
+	$(CC) -O3 -c --std=c++11 -o $@ $< $(INCLUDE_PATH)
+
+cell: $(OBJ)
+	$(CC) -o $@ $^ $(CFLAGS) $(LIBS_PATH) $(LIBS)
+
+all: cell
+
+.PHONY: clean all
+
+clean:
+	rm -f *.o *~ core cell
+
diff --git a/example/Vector/4_multiphase_celllist/main.cpp b/example/Vector/4_multiphase_celllist/main.cpp
new file mode 100644
index 000000000..8acfd6f18
--- /dev/null
+++ b/example/Vector/4_multiphase_celllist/main.cpp
@@ -0,0 +1,197 @@
+
+#include "Vector/vector_dist.hpp"
+#include "Decomposition/CartDecomposition.hpp"
+#include "data_type/aggregate.hpp"
+#include "NN/CellList/CellListM.hpp"
+
+/*!
+ * \page Vector_4_mp_cl Vector 4 Multi Phase cell-list
+ *
+ * [TOC]
+ *
+ *
+ * # Vector Multi Phase cell-list # {#e4_ph_cl}
+ *
+ * This example show multi-phase cell lists for the distributed vector
+ *
+ * \warning BETA version
+ *
+ */
+
+int main(int argc, char* argv[])
+{
+	/*!
+	 * \page Vector_4_mp_cl Vector 4 Multi Phase cell-list
+	 *
+	 * ## Initialization ##
+	 *
+	 * Here we Initialize the library, and we create a set of distributed vectors all forced to have the same
+	 * decomposition. Each vector identify one phase
+	 *
+	 * \snippet Vector/1_celllist/main.cpp Initialization and parameters
+	 *
+	 */
+
+	//! \cond [Initialization and parameters] \endcond
+
+	openfpm_init(&argc,&argv);
+	Vcluster & v_cl = create_vcluster();
+
+	// we will place the particles on a grid like way with 128 particles on each direction
+	size_t sz[3] = {128,128,128};
+
+	// The domain
+	Box<3,float> box({0.0,0.0,0.0},{1.0,1.0,1.0});
+
+	// Boundary conditions
+	size_t bc[3]={PERIODIC,PERIODIC,PERIODIC};
+
+	// ghost, big enough to contain the interaction radius
+	Ghost<3,float> ghost(1.0/(128-2));
+
+	//! \cond [Initialization and parameters] \endcond
+
+	/*!
+	 * \page Vector_1_celllist Vector 1 Cell-list
+	 *
+	 * ## %Vector create ##
+	 *
+	 * Here we define a distributed vector in 3D, containing 3 properties, a
+	 * scalar double, a vector double[3], and a tensor or rank 2 double[3][3].
+	 * In this case the vector contain 0 particles initially
+	 *
+	 * \see \ref vector_inst
+	 *
+	 * \snippet Vector/1_celllist/main.cpp vector inst
+	 *
+	 */
+
+	//! \cond [vector inst] \endcond
+
+	openfpm::vector< vector_dist<3,float, aggregate<double,double>> > phases;
+
+	// first phase
+	phases.add( vector_dist<3,float, aggregate<double,double>>(4096,box,bc,ghost) );
+
+	// The other 3 phases
+	phases.add( vector_dist<3,float, aggregate<double,double>>(phases.get(1).getDecomposition(),4096) );
+	phases.add( vector_dist<3,float, aggregate<double,double>>(phases.get(2).getDecomposition(),4096) );
+	phases.add( vector_dist<3,float, aggregate<double,double>>(phases.get(3).getDecomposition(),4096) );
+
+	//! \cond [grid like part] \endcond
+
+	auto it = phases.get(0).getDomainIterator();
+
+	// For all the particles
+	while (it.isNext())
+	{
+		// for all phases
+		for (size_t i = 0 ; i < phases.size() ; i++)
+		{
+			auto key = it.get();
+
+			phases.get(i).getPos(key)[0] = (float)rand() / RAND_MAX;
+			phases.get(i).getPos(key)[1] = (float)rand() / RAND_MAX;
+			phases.get(i).getPos(key)[2] = (float)rand() / RAND_MAX;
+		}
+		// next point
+		++it;
+	}
+
+	// Redistribute all the phases in the mean while also take the iterators of all the phases
+
+	typedef decltype(phases.get(0).getIterator()) iterator;
+	openfpm::vector<iterator> phase_it;
+
+	for (size_t i = 0 ; i < phases.size() ; i++)
+	{
+		phases.get(i).map();
+		phase_it.add(phases.get(i).getDomainIterator());
+	}
+
+	// Construct one single Multi-phase cell list to use in the computation
+
+	CellListM<3,float,2> NN;
+
+	while (it.isNext())
+	{
+		for (size_t i = 0; i < phases.size() ; i++)
+		{
+			auto key = it.get();
+
+			NN.add(phases.get(i).getPos(key), key.getKey(), i);
+
+			++it;
+		}
+	}
+
+	vector_dist<3,float, aggregate<double,double> > & current_phase = phases.get(0);
+
+	// Get the iterator of the particles of phase 0
+	auto it2 = current_phase.getIterator();
+
+	// For each particle ...
+	while (it2.isNext())
+	{
+		// ... p
+		auto p = it2.get();
+
+		// Get the position of the particle p
+		Point<3,float> xp = current_phase.getPos(p);
+
+		// Get an iterator of all the particles neighborhood of p
+		auto Np = NN.getNNIterator(NN.getCell(current_phase.getPos(p)));
+
+		// For each particle near p
+		while (Np.isNext())
+		{
+			// Get the particle q near to p
+			auto q = Np.getP();
+
+			// Get from which phase it come from
+			auto ph_q = Np.getV();
+
+			Point<3,float> xq = phases.get(ph_q).getPos(q);
+
+			// we accumulate all the distances
+			current_phase.getProp<0>(p) = norm(xp - xq);
+
+			++Np;
+		}
+
+		// Next particle p
+		++it2;
+	}
+
+	//! \cond [verletlist] \endcond
+
+	/*!
+	 * \page Vector_1_celllist Vector 1 Cell-list
+	 *
+	 * ## Finalize ## {#finalize}
+	 *
+	 *  At the very end of the program we have always to de-initialize the library
+	 *
+	 * \snippet Vector/1_celllist/main.cpp finalize
+	 *
+	 */
+
+	//! \cond [finalize] \endcond
+
+	openfpm_finalize();
+
+	//! \cond [finalize] \endcond
+
+	/*!
+	 * \page Vector_1_celllist Vector 1 Cell-list
+	 *
+	 * # Full code # {#code}
+	 *
+	 * \include Vector/1_celllist/main.cpp
+	 *
+	 */
+}
+
+
+
+
-- 
GitLab