vector_dist.hpp 55.4 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4 5 6 7 8 9 10
/*
 * Vector.hpp
 *
 *  Created on: Mar 5, 2015
 *      Author: Pietro Incardona
 */

#ifndef VECTOR_HPP_
#define VECTOR_HPP_

incardon's avatar
incardon committed
11
#include "HDF5_wr/HDF5_wr.hpp"
incardon's avatar
incardon committed
12
#include "VCluster/VCluster.hpp"
incardon's avatar
incardon committed
13
#include "Space/Shape/Point.hpp"
incardon's avatar
incardon committed
14
#include "Vector/Iterators/vector_dist_iterator.hpp"
incardon's avatar
incardon committed
15 16
#include "Space/Shape/Box.hpp"
#include "Vector/vector_dist_key.hpp"
incardon's avatar
incardon committed
17
#include "memory/PtrMemory.hpp"
incardon's avatar
incardon committed
18
#include "NN/CellList/CellList.hpp"
incardon's avatar
incardon committed
19
#include "NN/CellList/CellListFast_gen.hpp"
20
#include "util/common.hpp"
incardon's avatar
incardon committed
21
#include "util/object_util.hpp"
incardon's avatar
incardon committed
22
#include "memory/ExtPreAlloc.hpp"
23
#include "CSVWriter/CSVWriter.hpp"
24
#include "VTKWriter/VTKWriter.hpp"
25
#include "Decomposition/common.hpp"
incardon's avatar
incardon committed
26
#include "Grid/Iterators/grid_dist_id_iterator_dec.hpp"
27
#include "Grid/grid_key_dx_iterator_hilbert.hpp"
28
#include "Vector/vector_dist_ofb.hpp"
29
#include "Decomposition/CartDecomposition.hpp"
30
#include "data_type/aggregate.hpp"
31
#include "NN/VerletList/VerletList.hpp"
32
#include "vector_dist_comm.hpp"
incardon's avatar
incardon committed
33
#include "DLB/LB_Model.hpp"
incardon's avatar
incardon committed
34
#include "Vector/vector_map_iterator.hpp"
incardon's avatar
incardon committed
35 36
#include "NN/CellList/ParticleIt_Cells.hpp"
#include "NN/CellList/ProcKeys.hpp"
incardon's avatar
incardon committed
37

incardon's avatar
incardon committed
38 39 40 41 42 43 44 45 46 47 48 49 50
#define VECTOR_DIST_ERROR_OBJECT std::runtime_error("Runtime vector distributed error");

#ifdef SE_CLASS3
#include "se_class3_vector.hpp"
#endif

#ifdef SE_CLASS3
	#define SE_CLASS3_VDIST_CONSTRUCTOR ,se3(getDecomposition(),*this)
#else
	#define SE_CLASS3_VDIST_CONSTRUCTOR
#endif


incardon's avatar
incardon committed
51 52 53
#define NO_ID false
#define ID true

incardon's avatar
incardon committed
54 55
#define DEC_GRAN(gr) ((size_t)gr << 32)

incardon's avatar
incardon committed
56
// Perform a ghost get or a ghost put
incardon's avatar
incardon committed
57 58 59
#define GET	1
#define PUT 2

incardon's avatar
incardon committed
60 61 62 63
// Write the particles with ghost
#define NO_GHOST 0
#define WITH_GHOST 2

incardon's avatar
incardon committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
//! General function t get a cell-list
template<unsigned int dim, typename St, typename CellL, typename Vector>
struct gcl
{
	/*! \brief Get the Cell list based on the type
	 *
	 * \param vd Distributed vector
	 * \param r_cut Cut-off radius
	 * \param g Ghost
	 *
	 * \return the constructed cell-list
	 *
	 */
	static inline CellL get(Vector & vd, const St & r_cut, const Ghost<dim,St> & g)
	{
incardon's avatar
incardon committed
79
		return vd.template getCellList<CellL>(r_cut);
incardon's avatar
incardon committed
80 81 82 83
	}
};

//! General function t get a cell-list
incardon's avatar
incardon committed
84 85
template<unsigned int dim, typename St, typename Vector, typename Mem_type>
struct gcl<dim,St,CellList_gen<dim, St, Process_keys_hilb,Mem_type, shift<dim, St> >,Vector>
incardon's avatar
incardon committed
86 87 88 89 90 91 92 93 94 95
{
	/*! \brief Get the Cell list based on the type
	 *
	 * \param vd Distributed vector
	 * \param r_cut Cut-off radius
	 * \param g Ghost
	 *
	 * \return the constructed cell-list
	 *
	 */
incardon's avatar
incardon committed
96
	static inline CellList_gen<dim, St, Process_keys_hilb, Mem_type, shift<dim, St> > get(Vector & vd, const St & r_cut, const Ghost<dim,St> & g)
incardon's avatar
incardon committed
97 98 99 100 101
	{
		return vd.getCellList_hilb(r_cut,g);
	}
};

102 103 104
#define CELL_MEMFAST(dim,St) CellList_gen<dim, St, Process_keys_lin, Mem_fast, shift<dim, St> >
#define CELL_MEMBAL(dim,St) CellList_gen<dim, St, Process_keys_lin, Mem_bal, shift<dim, St> >
#define CELL_MEMMW(dim,St) CellList_gen<dim, St, Process_keys_lin, Mem_mw, shift<dim, St> >
incardon's avatar
incardon committed
105

incardon's avatar
incardon committed
106 107 108 109 110
#define CELL_MEMFAST_HILB(dim,St) CellList_gen<dim, St, Process_keys_hilb, Mem_fast, shift<dim, St> >
#define CELL_MEMBAL_HILB(dim,St) CellList_gen<dim, St, Process_keys_hilb, Mem_bal, shift<dim, St> >
#define CELL_MEMMW_HILB(dim,St) CellList_gen<dim, St, Process_keys_hilb, Mem_mw, shift<dim, St> >


incardon's avatar
incardon committed
111
/*! \brief Distributed vector
112
 *
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
 * This class reppresent a distributed vector, the distribution of the structure
 * is based on the positional information of the elements the vector store
 *
 * ## Create a vector of random elements on each processor 2D
 * \snippet vector_dist_unit_test.hpp Create a vector of random elements on each processor 2D
 *
 * ## Create a vector of random elements on each processor 3D
 * \snippet vector_dist_unit_test.hpp Create a vector of random elements on each processor 3D
 *
 * ## Create a vector of elements distributed on a grid like way
 * \snippet vector_dist_unit_test.hpp Create a vector of elements distributed on a grid like way
 *
 * ## Redistribute the particles and sync the ghost properties
 * \snippet vector_dist_unit_test.hpp Redistribute the particles and sync the ghost properties
 *
 * \tparam dim Dimensionality of the space where the elements lives
 * \tparam St type of space float, double ...
 * \tparam prop properties the vector element store in OpenFPM data structure format
 * \tparam Decomposition Decomposition strategy to use CartDecomposition ...
 * \tparam Memory Memory pool where store the information HeapMemory ...
incardon's avatar
incardon committed
133 134 135
 *
 */

incardon's avatar
incardon committed
136 137 138 139 140 141 142
template<unsigned int dim,
         typename St,
		 typename prop,
		 typename layout = typename memory_traits_lin<prop>::type,
		 template <typename> class layout_base = memory_traits_lin,
		 typename Decomposition = CartDecomposition<dim,St>,
		 typename Memory = HeapMemory>
incardon's avatar
incardon committed
143
class vector_dist : public vector_dist_comm<dim,St,prop,layout,layout_base,Decomposition,Memory>
incardon's avatar
incardon committed
144
{
incardon's avatar
incardon committed
145 146 147
public:

	//! Self type
incardon's avatar
incardon committed
148
	typedef vector_dist<dim,St,prop,layout,layout_base,Decomposition,Memory> self;
incardon's avatar
incardon committed
149 150 151 152

	//! property object
	typedef prop value_type;

incardon's avatar
incardon committed
153 154
private:

155
	//! Ghost marker, all the particle with id > g_m are ghost all with g_m < are real particle
156 157
	size_t g_m = 0;

158 159
	//! Particle position vector, (It has 2 elements) the first has real particles assigned to a processor
	//! the second element contain unassigned particles
incardon's avatar
incardon committed
160
	openfpm::vector<Point<dim, St>,Memory> v_pos;
incardon's avatar
incardon committed
161

162 163
	//! Particle properties vector, (It has 2 elements) the first has real particles assigned to a processor
	//! the second element contain unassigned particles
incardon's avatar
incardon committed
164
	openfpm::vector<prop,Memory,layout,layout_base> v_prp;
incardon's avatar
incardon committed
165

166
	//! Virtual cluster
incardon's avatar
incardon committed
167 168
	Vcluster & v_cl;

incardon's avatar
incardon committed
169 170 171
	//! option used to create this vector
	size_t opt = 0;

172 173 174
	//! Name of the properties
	openfpm::vector<std::string> prp_names;

incardon's avatar
incardon committed
175 176 177 178 179
#ifdef SE_CLASS3

	se_class3_vector<prop::max_prop,dim,St,Decomposition,self> se3;

#endif
180

Pietro Incardona's avatar
Pietro Incardona committed
181
	/*! \brief Initialize the structures
incardon's avatar
incardon committed
182
	 *
Pietro Incardona's avatar
Pietro Incardona committed
183
	 * \param np number of particles
incardon's avatar
incardon committed
184 185
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
186
	void init_structures(size_t np)
incardon's avatar
incardon committed
187
	{
188
		// convert to a local number of elements
189 190 191 192 193 194 195 196
		size_t p_np = np / v_cl.getProcessingUnits();

		// Get non divisible part
		size_t r = np % v_cl.getProcessingUnits();

		// Distribute the remain particles
		if (v_cl.getProcessUnitID() < r)
			p_np++;
197

incardon's avatar
incardon committed
198
		// resize the position vector
199
		v_pos.resize(p_np);
incardon's avatar
incardon committed
200 201

		// resize the properties vector
202 203 204
		v_prp.resize(p_np);

		g_m = p_np;
Pietro Incardona's avatar
Pietro Incardona committed
205
	}
incardon's avatar
incardon committed
206

incardon's avatar
incardon committed
207
	/*! \brief Check if the parameters describe a valid vector. In case it does not report an error
208 209 210 211 212 213 214 215
	 *
	 * \param box Box to check
	 *
	 */
	void check_parameters(Box<dim,St> & box)
	{
		// if the box is not valid return an error
		if (box.isValid() == false)
incardon's avatar
incardon committed
216
		{
217
			std::cerr << __FILE__ << ":" << __LINE__ << "  Error the domain is not valid " << box.toString() << std::endl;
incardon's avatar
incardon committed
218 219
			ACTION_ON_ERROR(VECTOR_DIST_ERROR_OBJECT);
		}
220 221 222

	}

incardon's avatar
incardon committed
223 224 225 226 227
	/*! \brief It check that the r_cut is not bugger than the ghost
	 *
	 * \param r_cut cut-off radius
	 *
	 */
incardon's avatar
incardon committed
228
	void check_ghost_compatible_rcut(St r_cut)
incardon's avatar
incardon committed
229 230 231
	{
		for (size_t i = 0 ; i < dim ; i++)
		{
incardon's avatar
incardon committed
232 233 234 235 236
			if (fabs(getDecomposition().getGhost().getLow(i)) < r_cut)
			{
				std::cerr << __FILE__ << ":" << __LINE__ << " Error the cut off radius " << r_cut << " is bigger that the ghost layer on the dimension " << i << " lower=" << getDecomposition().getGhost().getLow(i) << std::endl;
				ACTION_ON_ERROR(VECTOR_DIST_ERROR_OBJECT);
			}
incardon's avatar
incardon committed
237 238 239
		}
	}

240

Pietro Incardona's avatar
Pietro Incardona committed
241 242 243 244 245 246 247 248
public:

	//! space type
	typedef St stype;

	//! dimensions of space
	static const unsigned int dims = dim;

249 250 251 252
	/*! \brief Operator= for distributed vector
	 *
	 * \param v vector to copy
	 *
Pietro Incardona's avatar
Pietro Incardona committed
253 254
	 * \return itself
	 *
255
	 */
incardon's avatar
incardon committed
256
	vector_dist<dim,St,prop,layout,layout_base,Decomposition,Memory> & operator=(const vector_dist<dim,St,prop,layout,layout_base,Decomposition,Memory> & v)
257
	{
incardon's avatar
incardon committed
258
		static_cast<vector_dist_comm<dim,St,prop,layout,layout_base,Decomposition,Memory> *>(this)->operator=(static_cast<vector_dist_comm<dim,St,prop,layout,layout_base,Decomposition,Memory>>(v));
259 260 261 262 263

		g_m = v.g_m;
		v_pos = v.v_pos;
		v_prp = v.v_prp;

incardon's avatar
incardon committed
264 265 266 267 268 269
#ifdef SE_CLASS3
		se3 = v.se3;
#endif

		opt = v.opt;

270 271 272 273 274 275 276
		return *this;
	}

	/*! \brief Operator= for distributed vector
	 *
	 * \param v vector to copy
	 *
Pietro Incardona's avatar
Pietro Incardona committed
277 278
	 * \return itself
	 *
279
	 */
incardon's avatar
incardon committed
280
	vector_dist<dim,St,prop,layout,layout_base,Decomposition,Memory> & operator=(vector_dist<dim,St,prop,layout,layout_base,Decomposition,Memory> && v)
281
	{
incardon's avatar
incardon committed
282
		static_cast<vector_dist_comm<dim,St,prop,layout,layout_base,Decomposition,Memory> *>(this)->operator=(static_cast<vector_dist_comm<dim,St,prop,layout,layout_base,Decomposition,Memory> >(v));
283 284 285 286 287

		g_m = v.g_m;
		v_pos.swap(v.v_pos);
		v_prp.swap(v.v_prp);

incardon's avatar
incardon committed
288 289 290 291 292 293
#ifdef SE_CLASS3
		se3 = v.se3;
#endif

		opt = v.opt;

294 295 296
		return *this;
	}

incardon's avatar
incardon committed
297

Pietro Incardona's avatar
Pietro Incardona committed
298
	/*! \brief Copy Constructor
299
	 *
Pietro Incardona's avatar
Pietro Incardona committed
300
	 * \param v vector to copy
301 302
	 *
	 */
incardon's avatar
incardon committed
303
	vector_dist(const vector_dist<dim,St,prop,layout,layout_base,Decomposition,Memory> & v)
incardon's avatar
incardon committed
304
	:vector_dist_comm<dim,St,prop,layout,layout_base,Decomposition,Memory>(v.getDecomposition()),v_cl(v.v_cl) SE_CLASS3_VDIST_CONSTRUCTOR
305 306 307 308 309 310 311 312
	{
#ifdef SE_CLASS2
		check_new(this,8,VECTOR_DIST_EVENT,4);
#endif

		this->operator=(v);
	}

Pietro Incardona's avatar
Pietro Incardona committed
313
	/*! \brief Copy constructor
314
	 *
Pietro Incardona's avatar
Pietro Incardona committed
315
	 * \param v vector to copy
316 317
	 *
	 */
incardon's avatar
incardon committed
318
	vector_dist(vector_dist<dim,St,prop,layout,layout_base,Decomposition,Memory> && v) noexcept
incardon's avatar
incardon committed
319
	:v_cl(v.v_cl) SE_CLASS3_VDIST_CONSTRUCTOR
320 321 322 323 324 325
	{
#ifdef SE_CLASS2
		check_new(this,8,VECTOR_DIST_EVENT,4);
#endif

		this->operator=(v);
incardon's avatar
incardon committed
326 327 328 329

#ifdef SE_CLASS3
		se3.Initialize();
#endif
330
	}
Pietro Incardona's avatar
Pietro Incardona committed
331

Pietro Incardona's avatar
Pietro Incardona committed
332
	/*! \brief Constructor with predefined decomposition
Pietro Incardona's avatar
Pietro Incardona committed
333
	 *
Pietro Incardona's avatar
Pietro Incardona committed
334 335
	 * \param dec is the decomposition
	 * \param np number of particles
Pietro Incardona's avatar
Pietro Incardona committed
336 337 338
	 *
	 */
	vector_dist(const Decomposition & dec, size_t np) :
incardon's avatar
incardon committed
339
	vector_dist_comm<dim,St,prop,layout,layout_base,Decomposition,Memory>(dec), v_cl(create_vcluster()) SE_CLASS3_VDIST_CONSTRUCTOR
Pietro Incardona's avatar
Pietro Incardona committed
340 341 342 343 344 345
	{
#ifdef SE_CLASS2
		check_new(this,8,VECTOR_DIST_EVENT,4);
#endif

		init_structures(np);
incardon's avatar
incardon committed
346 347 348 349

#ifdef SE_CLASS3
		se3.Initialize();
#endif
Pietro Incardona's avatar
Pietro Incardona committed
350 351 352
	}


incardon's avatar
incardon committed
353
	/*! \brief Constructor of a distributed vector
Pietro Incardona's avatar
Pietro Incardona committed
354 355 356
	 *
	 * \param np number of elements
	 * \param box domain where the vector of elements live
Pietro Incardona's avatar
Pietro Incardona committed
357
	 * \param bc boundary conditions
Pietro Incardona's avatar
Pietro Incardona committed
358
	 * \param g Ghost margins
359
	 * \param opt [Optional] additional options. BIND_DEC_TO_GHOST Bind the decomposition to be multiple of the
360 361
	 *          ghost size. This is required if we want to use symmetric to eliminate
	 *          ghost communications.
362
	 * \param gdist [Optional] override the default distribution grid
Pietro Incardona's avatar
Pietro Incardona committed
363 364
	 *
	 */
365
	vector_dist(size_t np, Box<dim, St> box, const size_t (&bc)[dim], const Ghost<dim, St> & g, size_t opt = 0, const grid_sm<dim,void> & gdist = grid_sm<dim,void>())
incardon's avatar
incardon committed
366
	:v_cl(create_vcluster()),opt(opt) SE_CLASS3_VDIST_CONSTRUCTOR
Pietro Incardona's avatar
Pietro Incardona committed
367 368 369 370 371
	{
#ifdef SE_CLASS2
		check_new(this,8,VECTOR_DIST_EVENT,4);
#endif

incardon's avatar
incardon committed
372 373 374
		if (opt >> 32 != 0)
			this->setDecompositionGranularity(opt >> 32);

375 376
		check_parameters(box);

Pietro Incardona's avatar
Pietro Incardona committed
377
		init_structures(np);
378
		this->init_decomposition(box,bc,g,opt,gdist);
incardon's avatar
incardon committed
379 380 381 382

#ifdef SE_CLASS3
		se3.Initialize();
#endif
Pietro Incardona's avatar
Pietro Incardona committed
383 384
	}

385 386 387 388 389 390 391
	~vector_dist()
	{
#ifdef SE_CLASS2
		check_delete(this);
#endif
	}

392 393 394 395 396 397 398 399 400
	/*! \brief remove all the elements
	 *
	 *
	 */
	void clear()
	{
		resize(0);
	}

401
	/*! \brief return the local size of the vector
402
	 *
403
	 * \return local size
404 405
	 *
	 */
incardon's avatar
incardon committed
406
	size_t size_local() const
407
	{
408
		return g_m;
409 410
	}

incardon's avatar
incardon committed
411 412 413 414 415
	/*! \brief return the local size of the vector
	 *
	 * \return local size
	 *
	 */
incardon's avatar
incardon committed
416
	size_t size_local_with_ghost() const
incardon's avatar
incardon committed
417
	{
418
		return v_pos.size();
incardon's avatar
incardon committed
419 420
	}

incardon's avatar
incardon committed
421 422
#ifndef ONLY_READWRITE_GETTER

423
	/*! \brief Get the position of an element
incardon's avatar
incardon committed
424
	 *
425 426 427 428 429
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \param vec_key element
	 *
	 * \return the position of the element in space
incardon's avatar
incardon committed
430 431
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
432
	inline auto getPos(vect_dist_key_dx vec_key) -> decltype(v_pos.template get<0>(vec_key.getKey()))
incardon's avatar
incardon committed
433
	{
434 435 436 437
#ifdef SE_CLASS3
		check_for_pos_nan_inf<prop::max_prop_real,prop::max_prop>(*this,vec_key.getKey());
#endif

Pietro Incardona's avatar
Pietro Incardona committed
438
		return v_pos.template get<0>(vec_key.getKey());
incardon's avatar
incardon committed
439 440
	}

441 442 443 444 445 446 447 448 449 450 451
	/*! \brief Get the position of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \param vec_key element
	 *
	 * \return the position of the element in space
	 *
	 */
	inline auto getPos(vect_dist_key_dx vec_key) const -> decltype(v_pos.template get<0>(vec_key.getKey()))
	{
452 453 454
#ifdef SE_CLASS3
		check_for_pos_nan_inf<prop::max_prop_real,prop::max_prop>(*this,vec_key.getKey());
#endif
455 456 457
		return v_pos.template get<0>(vec_key.getKey());
	}

458 459 460 461 462 463 464 465 466
	/*! \brief Get the position of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \param vec_key element
	 *
	 * \return the position of the element in space
	 *
	 */
467 468
	inline auto getPos(size_t vec_key) -> decltype(v_pos.template get<0>(vec_key))
	{
469 470 471
#ifdef SE_CLASS3
		check_for_pos_nan_inf<prop::max_prop_real,prop::max_prop>(*this,vec_key);
#endif
472 473 474 475 476 477 478 479 480 481 482 483
		return v_pos.template get<0>(vec_key);
	}

	/*! \brief Get the position of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \param vec_key element
	 *
	 * \return the position of the element in space
	 *
	 */
incardon's avatar
incardon committed
484
	inline auto getPos(size_t vec_key) const -> decltype(v_pos.template get<0>(vec_key))
485
	{
486 487 488
#ifdef SE_CLASS3
		check_for_pos_nan_inf<prop::max_prop_real,prop::max_prop>(*this,vec_key);
#endif
489 490 491
		return v_pos.template get<0>(vec_key);
	}

492
	/*! \brief Get the property of an element
incardon's avatar
incardon committed
493
	 *
494 495 496
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \tparam id property id
incardon's avatar
incardon committed
497 498
	 * \param vec_key vector element
	 *
499 500
	 * \return return the selected property of the vector element
	 *
incardon's avatar
incardon committed
501
	 */
502
	template<unsigned int id> inline auto getProp(vect_dist_key_dx vec_key) -> decltype(v_prp.template get<id>(vec_key.getKey()))
incardon's avatar
incardon committed
503
	{
504 505 506
#ifdef SE_CLASS3
		check_for_prop_nan_inf<id,prop::max_prop+SE3_STATUS>(*this,vec_key.getKey());
#endif
507
		return v_prp.template get<id>(vec_key.getKey());
incardon's avatar
incardon committed
508 509
	}

510 511 512 513 514 515 516 517 518 519
	/*! \brief Get the property of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \tparam id property id
	 * \param vec_key vector element
	 *
	 * \return return the selected property of the vector element
	 *
	 */
incardon's avatar
incardon committed
520
	template<unsigned int id> inline auto getProp(vect_dist_key_dx vec_key) const -> decltype(v_prp.template get<id>(vec_key.getKey()))
521
	{
522 523 524
#ifdef SE_CLASS3
		check_for_prop_nan_inf<id,prop::max_prop+SE3_STATUS>(*this,vec_key.getKey());
#endif
525 526 527
		return v_prp.template get<id>(vec_key.getKey());
	}

528 529 530 531 532 533 534 535 536 537 538 539
	/*! \brief Get the property of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \tparam id property id
	 * \param vec_key vector element
	 *
	 * \return return the selected property of the vector element
	 *
	 */
	template<unsigned int id> inline auto getProp(size_t vec_key) -> decltype(v_prp.template get<id>(vec_key))
	{
540 541 542
#ifdef SE_CLASS3
		check_for_prop_nan_inf<id,prop::max_prop+SE3_STATUS>(*this,vec_key);
#endif
543 544 545
		return v_prp.template get<id>(vec_key);
	}

546 547 548 549 550 551 552 553 554 555
	/*! \brief Get the property of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \tparam id property id
	 * \param vec_key vector element
	 *
	 * \return return the selected property of the vector element
	 *
	 */
incardon's avatar
incardon committed
556
	template<unsigned int id> inline auto getProp(size_t vec_key) const -> decltype(v_prp.template get<id>(vec_key))
557
	{
558 559 560
#ifdef SE_CLASS3
		check_for_prop_nan_inf<id,prop::max_prop+SE3_STATUS>(*this,vec_key);
#endif
561 562 563
		return v_prp.template get<id>(vec_key);
	}

incardon's avatar
incardon committed
564 565
#endif

incardon's avatar
incardon committed
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
///////////////////// Read and write with no check

	/*! \brief Get the position of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \param vec_key element
	 *
	 * \return the position of the element in space
	 *
	 */
	inline auto getPosNC(vect_dist_key_dx vec_key) -> decltype(v_pos.template get<0>(vec_key.getKey()))
	{
		return v_pos.template get<0>(vec_key.getKey());
	}

	/*! \brief Get the position of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \param vec_key element
	 *
	 * \return the position of the element in space
	 *
	 */
	inline auto getPosNC(vect_dist_key_dx vec_key) const -> decltype(v_pos.template get<0>(vec_key.getKey()))
	{
		return v_pos.template get<0>(vec_key.getKey());
	}

	/*! \brief Get the position of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \param vec_key element
	 *
	 * \return the position of the element in space
	 *
	 */
	inline auto getPosNC(size_t vec_key) -> decltype(v_pos.template get<0>(vec_key))
	{
		return v_pos.template get<0>(vec_key);
	}

	/*! \brief Get the position of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \param vec_key element
	 *
	 * \return the position of the element in space
	 *
	 */
	inline auto getPosNC(size_t vec_key) const -> decltype(v_pos.template get<0>(vec_key))
	{
		return v_pos.template get<0>(vec_key);
	}

	/*! \brief Get the property of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \tparam id property id
	 * \param vec_key vector element
	 *
	 * \return return the selected property of the vector element
	 *
	 */
	template<unsigned int id> inline auto getPropNC(vect_dist_key_dx vec_key) -> decltype(v_prp.template get<id>(vec_key.getKey()))
	{
		return v_prp.template get<id>(vec_key.getKey());
	}

	/*! \brief Get the property of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \tparam id property id
	 * \param vec_key vector element
	 *
	 * \return return the selected property of the vector element
	 *
	 */
incardon's avatar
incardon committed
649
	template<unsigned int id> inline auto getPropNC(vect_dist_key_dx vec_key) const -> decltype(v_prp.template get<id>(vec_key.getKey()))
incardon's avatar
incardon committed
650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
	{
		return v_prp.template get<id>(vec_key.getKey());
	}

	/*! \brief Get the property of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \tparam id property id
	 * \param vec_key vector element
	 *
	 * \return return the selected property of the vector element
	 *
	 */
	template<unsigned int id> inline auto getPropNC(size_t vec_key) -> decltype(v_prp.template get<id>(vec_key))
	{
		return v_prp.template get<id>(vec_key);
	}

	/*! \brief Get the property of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \tparam id property id
	 * \param vec_key vector element
	 *
	 * \return return the selected property of the vector element
	 *
	 */
incardon's avatar
incardon committed
679
	template<unsigned int id> inline auto getPropNC(size_t vec_key) const -> decltype(v_prp.template get<id>(vec_key))
incardon's avatar
incardon committed
680 681 682 683
	{
		return v_prp.template get<id>(vec_key);
	}

incardon's avatar
incardon committed
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750
///////////////////// Read and Write function

	/*! \brief Get the position of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \param vec_key element
	 *
	 * \return the position of the element in space
	 *
	 */
	inline auto getPosWrite(vect_dist_key_dx vec_key) -> decltype(v_pos.template get<0>(vec_key.getKey()))
	{
#ifdef SE_CLASS3
		se3.template write<prop::max_prop_real>(*this,vec_key.getKey());
#endif

		return v_pos.template get<0>(vec_key.getKey());
	}

	/*! \brief Get the position of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \param vec_key element
	 *
	 * \return the position of the element in space
	 *
	 */
	inline auto getPosRead(vect_dist_key_dx vec_key) const -> decltype(v_pos.template get<0>(vec_key.getKey()))
	{
#ifdef SE_CLASS3
		se3.template read<prop::max_prop_real>(*this,vec_key.getKey());
#endif

		return v_pos.template get<0>(vec_key.getKey());
	}

	/*! \brief Get the property of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \tparam id property id
	 * \param vec_key vector element
	 *
	 * \return return the selected property of the vector element
	 *
	 */
	template<unsigned int id> inline auto getPropWrite(vect_dist_key_dx vec_key) -> decltype(v_prp.template get<id>(vec_key.getKey()))
	{
#ifdef SE_CLASS3
		se3.template write<id>(*this,vec_key.getKey());
#endif

		return v_prp.template get<id>(vec_key.getKey());
	}

	/*! \brief Get the property of an element
	 *
	 * see the vector_dist iterator usage to get an element key
	 *
	 * \tparam id property id
	 * \param vec_key vector element
	 *
	 * \return return the selected property of the vector element
	 *
	 */
incardon's avatar
incardon committed
751
	template<unsigned int id> inline auto getPropRead(vect_dist_key_dx vec_key) const -> decltype(v_prp.template get<id>(vec_key.getKey()))
incardon's avatar
incardon committed
752 753 754 755 756 757 758 759 760 761
	{
#ifdef SE_CLASS3
		se3.template read<id>(*this,vec_key.getKey());
#endif

		return v_prp.template get<id>(vec_key.getKey());
	}

//////////////////////////////////////////////

762 763 764 765 766 767 768 769 770 771 772 773 774 775
	/*! \brief Add local particle
	 *
	 * It add a local particle, with "local" we mean in this processor
	 * the particle can be also created out of the processor domain, in this
	 * case a call to map is required. Added particles are always created at the
	 * end and can be accessed with getLastPos and getLastProp
	 *
	 */
	void add()
	{
		v_prp.insert(g_m);
		v_pos.insert(g_m);

		g_m++;
incardon's avatar
incardon committed
776 777 778 779 780

#ifdef SE_CLASS3
		for (size_t i = 0 ; i < prop::max_prop_real+1 ; i++)
			v_prp.template get<prop::max_prop_real>(g_m-1)[i] = UNINITIALIZED;
#endif
781 782
	}

783 784
#ifndef ONLY_READWRITE_GETTER

785 786 787 788 789
	/*! \brief Get the position of the last element
	 *
	 * \return the position of the element in space
	 *
	 */
Pietro Incardona's avatar
Pietro Incardona committed
790
	inline auto getLastPos() -> decltype(v_pos.template get<0>(0))
791
	{
Pietro Incardona's avatar
Pietro Incardona committed
792
		return v_pos.template get<0>(g_m - 1);
793 794 795 796 797 798 799 800 801 802 803
	}

	/*! \brief Get the property of the last element
	 *
	 * \tparam id property id
	 *
	 * \return return the selected property of the vector element
	 *
	 */
	template<unsigned int id> inline auto getLastProp() -> decltype(v_prp.template get<id>(0))
	{
804
		return v_prp.template get<id>(g_m - 1);
805 806
	}

807 808
#endif

incardon's avatar
incardon committed
809 810 811 812 813 814 815 816 817 818
////////////////////////////// READ AND WRITE VERSION //////////

	/*! \brief Get the position of the last element
	 *
	 * \return the position of the element in space
	 *
	 */
	inline auto getLastPosRead() -> decltype(v_pos.template get<0>(0))
	{
#ifdef SE_CLASS3
819
		se3.template read<prop::max_prop_real>(*this,g_m-1);
incardon's avatar
incardon committed
820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849
#endif

		return v_pos.template get<0>(g_m - 1);
	}

	/*! \brief Get the property of the last element
	 *
	 * \tparam id property id
	 *
	 * \return return the selected property of the vector element
	 *
	 */
	template<unsigned int id> inline auto getLastPropRead() -> decltype(v_prp.template get<id>(0))
	{
#ifdef SE_CLASS3
		se3.read<id>(*this,g_m-1);
#endif

		return v_prp.template get<id>(g_m - 1);
	}


	/*! \brief Get the position of the last element
	 *
	 * \return the position of the element in space
	 *
	 */
	inline auto getLastPosWrite() -> decltype(v_pos.template get<0>(0))
	{
#ifdef SE_CLASS3
850
		se3.template write<prop::max_prop_real>(*this,g_m-1);
incardon's avatar
incardon committed
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
#endif

		return v_pos.template get<0>(g_m - 1);
	}

	/*! \brief Get the property of the last element
	 *
	 * \tparam id property id
	 *
	 * \return return the selected property of the vector element
	 *
	 */
	template<unsigned int id> inline auto getLastPropWrite() -> decltype(v_prp.template get<id>(0))
	{
#ifdef SE_CLASS3
866
		se3.template write<id>(*this,g_m-1);
incardon's avatar
incardon committed
867 868 869 870 871 872 873
#endif

		return v_prp.template get<id>(g_m - 1);
	}

////////////////////////////////////////////////////////////////

874 875 876 877 878 879 880 881 882
	/*! \brief Construct a cell list symmetric based on a cut of radius
	 *
	 * \tparam CellL CellList type to construct
	 *
	 * \param r_cut interation radius, or size of each cell
	 *
	 * \return the Cell list
	 *
	 */
883
	template<typename CellL = CellList<dim, St, Mem_fast, shift<dim, St> > > CellL getCellListSym(St r_cut)
884
	{
incardon's avatar
incardon committed
885
#ifdef SE_CLASS1
incardon's avatar
incardon committed
886
		if (!(opt & BIND_DEC_TO_GHOST))
incardon's avatar
incardon committed
887
		{
incardon's avatar
incardon committed
888
			if (getDecomposition().getGhost().getLow(dim-1) == 0.0)
889 890 891 892
			{
				std::cerr << __FILE__ << ":" << __LINE__ << " Error the vector has been constructed without BIND_DEC_TO_GHOST, If you construct a vector without BIND_DEC_TO_GHOST the ghost must be full without reductions " << std::endl;
				ACTION_ON_ERROR(VECTOR_DIST_ERROR_OBJECT);
			}
incardon's avatar
incardon committed
893 894 895
		}
#endif

896 897 898 899
		// Cell list
		CellL cell_list;

		size_t pad = 0;
incardon's avatar
incardon committed
900 901
		CellDecomposer_sm<dim,St,shift<dim,St>> cd_sm;
		cl_param_calculateSym(getDecomposition().getDomain(),cd_sm,getDecomposition().getGhost(),r_cut,pad);
902

incardon's avatar
incardon committed
903
		// Processor bounding box
904 905
		Box<dim, St> pbox = getDecomposition().getProcessorBounds();

incardon's avatar
incardon committed
906 907 908
		// Ghost padding extension
		Ghost<dim,size_t> g_ext(0);
		cell_list.Initialize(cd_sm,pbox,pad);
909
		cell_list.set_ndec(getDecomposition().get_ndec());
910

911
		updateCellListSym(cell_list);
incardon's avatar
incardon committed
912 913

		return cell_list;
914 915
	}

916

917 918 919 920
	/*! \brief Construct a cell list starting from the stored particles
	 *
	 * \tparam CellL CellList type to construct
	 *
921
	 * \param r_cut interation radius, or size of each cell
incardon's avatar
incardon committed
922
	 * \param no_se3 avoid SE_CLASS3 checking
923 924 925
	 *
	 * \return the Cell list
	 *
926
	 */
incardon's avatar
incardon committed
927 928
	template<typename CellL = CellList_gen<dim, St, Process_keys_lin, Mem_fast, shift<dim, St> > >
	CellL getCellList(St r_cut, bool no_se3 = false)
929
	{
incardon's avatar
incardon committed
930
#ifdef SE_CLASS3
931 932
		if (no_se3 == false)
		{se3.getNN();}
incardon's avatar
incardon committed
933 934 935 936 937
#endif
#ifdef SE_CLASS1
		check_ghost_compatible_rcut(r_cut);
#endif

Pietro Incardona's avatar
Pietro Incardona committed
938
		// Get ghost and anlarge by 1%
939
		Ghost<dim,St> g = getDecomposition().getGhost();
940
		g.magnify(1.013);
Pietro Incardona's avatar
Pietro Incardona committed
941

942
		return getCellList<CellL>(r_cut, g,no_se3);
943 944
	}

945 946 947 948 949 950 951 952 953
	/*! \brief Construct an hilbert cell list starting from the stored particles
	 *
	 * \tparam CellL CellList type to construct
	 *
	 * \param r_cut interation radius, or size of each cell
	 *
	 * \return the Cell list
	 *
	 */
incardon's avatar
incardon committed
954 955
	template<typename CellL = CellList_gen<dim, St, Process_keys_hilb, Mem_fast, shift<dim, St> > >
	CellL getCellList_hilb(St r_cut)
956
	{
incardon's avatar
incardon committed
957 958 959 960 961 962 963
#ifdef SE_CLASS3
		se3.getNN();
#endif
#ifdef SE_CLASS1
		check_ghost_compatible_rcut(r_cut);
#endif

964
		// Get ghost and anlarge by 1%
965
		Ghost<dim,St> g = getDecomposition().getGhost();
Pietro Incardona's avatar
Pietro Incardona committed
966
		g.magnify(1.013);
967 968 969 970

		return getCellList_hilb(r_cut, g);
	}

Pietro Incardona's avatar
Pietro Incardona committed
971 972 973 974 975
	/*! \brief Update a cell list using the stored particles
	 *
	 * \tparam CellL CellList type to construct
	 *
	 * \param cell_list Cell list to update
incardon's avatar
incardon committed
976
	 * \param no_se3 avoid se class 3 checking
Pietro Incardona's avatar
Pietro Incardona committed
977 978
	 *
	 */
979
	template<typename CellL> void updateCellList(CellL & cell_list, bool no_se3 = false)
Pietro Incardona's avatar
Pietro Incardona committed
980
	{
incardon's avatar
incardon committed
981
#ifdef SE_CLASS3
982 983
		if (no_se3 == false)
		{se3.getNN();}
incardon's avatar
incardon committed
984 985
#endif

incardon's avatar
incardon committed
986 987 988 989 990 991
		// This function assume equal spacing in all directions
		// but in the worst case we take the maximum
		St r_cut = 0;
		for (size_t i = 0 ; i < dim ; i++)
			r_cut = std::max(r_cut,cell_list.getCellBox().getHigh(i));

incardon's avatar
incardon committed
992 993 994
		// Here we have to check that the Cell-list has been constructed
		// from the same decomposition
		bool to_reconstruct = cell_list.get_ndec() != getDecomposition().get_ndec();
incardon's avatar
incardon committed
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007

		if (to_reconstruct == false)
		{
			populate_cell_list(v_pos,cell_list,g_m,CL_NON_SYMMETRIC);

			cell_list.set_gm(g_m);
		}
		else
		{
			CellL cli_tmp = gcl<dim,St,CellL,self>::get(*this,r_cut,getDecomposition().getGhost());

			cell_list.swap(cli_tmp);
		}
incardon's avatar
incardon committed
1008 1009 1010 1011 1012 1013 1014 1015 1016
	}

	/*! \brief Update a cell list using the stored particles
	 *
	 * \tparam CellL CellList type to construct
	 *
	 * \param cell_list Cell list to update
	 *
	 */
1017
	template<typename CellL = CellList<dim, St, Mem_fast, shift<dim, St> > > void updateCellListSym(CellL & cell_list)
incardon's avatar
incardon committed
1018
	{
incardon's avatar
incardon committed
1019 1020 1021
#ifdef SE_CLASS3
		se3.getNN();
#endif
incardon's avatar
incardon committed
1022

incardon's avatar
incardon committed
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
		// This function assume equal spacing in all directions
		// but in the worst case we take the maximum
		St r_cut = 0;
		for (size_t i = 0 ; i < dim ; i++)
			r_cut = std::max(r_cut,cell_list.getCellBox().getHigh(i));

		// Here we have to check that the Cell-list has been constructed
		// from the same decomposition
		bool to_reconstruct = cell_list.get_ndec() != getDecomposition().get_ndec();

		if (to_reconstruct == false)
		{
			populate_cell_list(v_pos,cell_list,g_m,CL_SYMMETRIC);

			cell_list.set_gm(g_m);
		}
		else
		{
			CellL cli_tmp = gcl<dim,St,CellL,self>::get(*this,r_cut,getDecomposition().getGhost());

			cell_list.swap(cli_tmp);
		}
Pietro Incardona's avatar
Pietro Incardona committed
1045 1046
	}

1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
	/*! \brief Construct a cell list starting from the stored particles
	 *
	 * It differ from the get getCellList for an additional parameter, in case the
	 * domain + ghost is not big enough to contain additional padding particles, a Cell list
	 * with bigger space can be created
	 * (padding particles in general are particles added by the user out of the domains)
	 *
	 * \tparam CellL CellList type to construct
	 *
	 * \param r_cut interation radius, or size of each cell
	 * \param enlarge In case of padding particles the cell list must be enlarged, like a ghost this parameter say how much must be enlarged
incardon's avatar
incardon committed
1058
	 * \param no_se3 avoid se_class3 cheking default false
1059
	 *
Pietro Incardona's avatar
Pietro Incardona committed
1060 1061
	 * \return the CellList
	 *
1062
	 */
incardon's avatar
incardon committed
1063 1064
	template<typename CellL = CellList_gen<dim, St, Process_keys_lin, Mem_fast, shift<dim, St> > >
	CellL getCellList(St r_cut, const Ghost<dim, St> & enlarge, bool no_se3 = false)
1065
	{
incardon's avatar
incardon committed
1066
#ifdef SE_CLASS3
1067 1068
		if (no_se3 == false)
		{se3.getNN();}
incardon's avatar
incardon committed
1069 1070
#endif

1071 1072
		CellL cell_list;

1073 1074
		// Division array
		size_t div[dim];
1075

1076
		// get the processor bounding box
1077
		Box<dim, St> pbox = getDecomposition().getProcessorBounds();
1078

1079
		// Processor bounding box
1080
		cl_param_calculate(pbox, div, r_cut, enlarge);
1081

1082
		cell_list.Initialize(pbox, div, g_m);
incardon's avatar
incardon committed
1083
		cell_list.set_ndec(getDecomposition().get_ndec());
1084

1085
		updateCellList(cell_list,no_se3);
1086 1087 1088

		return cell_list;
	}
1089

1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101
	/*! \brief Construct an hilbert cell list starting from the stored particles
	 *
	 * It differ from the get getCellList for an additional parameter, in case the
	 * domain + ghost is not big enough to contain additional padding particles, a Cell list
	 * with bigger space can be created
	 * (padding particles in general are particles added by the user out of the domains)
	 *
	 * \tparam CellL CellList type to construct
	 *
	 * \param r_cut interation radius, or size of each cell
	 * \param enlarge In case of padding particles the cell list must be enlarged, like a ghost this parameter say how much must be enlarged
	 *
Pietro Incardona's avatar
Pietro Incardona committed
1102 1103
	 * \return The Cell-list
	 *
1104
	 */
1105
	template<typename CellL = CellList_gen<dim, St, Process_keys_hilb, Mem_fast, shift<dim, St> > > CellL getCellList_hilb(St r_cut, const Ghost<dim, St> & enlarge)
1106
	{
incardon's avatar
incardon committed
1107 1108 1109 1110
#ifdef SE_CLASS3
		se3.getNN();
#endif

1111 1112 1113
		CellL cell_list;

		// Division array
1114 1115
		size_t div[dim];

1116
		// get the processor bounding box
1117
		Box<dim, St> pbox = getDecomposition().getProcessorBounds();
1118

1119
		// Processor bounding box
1120
		cl_param_calculate(pbox,div, r_cut, enlarge);
1121

1122
		cell_list.Initialize(pbox, div, g_m);
incardon's avatar
incardon committed
1123
		cell_list.set_ndec(getDecomposition().get_ndec());
1124

Pietro Incardona's avatar
Pietro Incardona committed
1125
		updateCellList(cell_list);
1126 1127 1128

		return cell_list;
	}
incardon's avatar
incardon committed
1129

incardon's avatar
incardon committed
1130 1131 1132 1133
	/*! \brief for each particle get the symmetric verlet list
	 *
	 * \param r_cut cut-off radius
	 *
incardon's avatar
incardon committed
1134 1135
	 * \return the verlet list
	 *
incardon's avatar
incardon committed
1136 1137 1138
	 */
	VerletList<dim,St,FAST,shift<dim,St> > getVerletSym(St r_cut)
	{
incardon's avatar
incardon committed
1139 1140 1141 1142
#ifdef SE_CLASS3
		se3.getNN();
#endif

incardon's avatar
incardon committed
1143
		VerletList<dim,St,FAST,shift<dim,St>> ver;
1144

incardon's avatar
incardon committed
1145 1146 1147 1148 1149
		// Processor bounding box
		Box<dim, St> pbox = getDecomposition().getProcessorBounds();

		ver.InitializeSym(getDecomposition().getDomain(),pbox,getDecomposition().getGhost(),r_cut,v_pos,g_m);

incardon's avatar
incardon committed
1150 1151
		ver.set_ndec(getDecomposition().get_ndec());

incardon's avatar
incardon committed
1152 1153
		return ver;
	}
1154

incardon's avatar
incardon committed
1155 1156 1157 1158 1159 1160 1161 1162 1163
	/*! \brief for each particle get the symmetric verlet list
	 *
	 * \param r_cut cut-off radius
	 *
	 * \return the verlet list
	 *
	 */
	VerletList<dim,St,FAST,shift<dim,St> > getVerletCrs(St r_cut)
	{
1164
#ifdef SE_CLASS1
incardon's avatar
incardon committed
1165
		if (!(opt & BIND_DEC_TO_GHOST))
1166 1167 1168 1169 1170 1171
		{
			std::cerr << __FILE__ << ":" << __LINE__ << " Error the vector has been constructed without BIND_DEC_TO_GHOST, getVerletCrs require the vector to be constructed with BIND_DEC_TO_GHOST option " << std::endl;
			ACTION_ON_ERROR(VECTOR_DIST_ERROR_OBJECT);
		}
#endif

incardon's avatar
incardon committed
1172 1173 1174 1175
#ifdef SE_CLASS3
		se3.getNN();
#endif

incardon's avatar
incardon committed
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
		VerletList<dim,St,FAST,shift<dim,St>> ver;

		// Processor bounding box
		Box<dim, St> pbox = getDecomposition().getProcessorBounds();

		// Initialize the verlet list
		ver.InitializeCrs(getDecomposition().getDomain(),pbox,getDecomposition().getGhost(),r_cut,v_pos,g_m);

		// Get the internal cell list
		auto & NN = ver.getInternalCellList();

		// Shift
1188
		grid_key_dx<dim> shift;
incardon's avatar
incardon committed
1189 1190 1191

		// Add padding
		for (size_t i = 0 ; i < dim ; i++)
1192
			shift.set_d(i,NN.getPadding(i));
incardon's avatar
incardon committed
1193 1194 1195

		grid_sm<dim,void> gs = NN.getInternalGrid();

1196 1197
		getDecomposition().setNNParameters(shift,gs);

incardon's avatar
incardon committed
1198
		ver.createVerletCrs(r_cut,g_m,v_pos,
1199 1200
				            getDecomposition().getCRSDomainCells(),
							getDecomposition().getCRSAnomDomainCells());
incardon's avatar
incardon committed
1201

incardon's avatar
incardon committed
1202 1203
		ver.set_ndec(getDecomposition().get_ndec());

incardon's avatar
incardon committed
1204 1205 1206
		return ver;
	}

1207 1208 1209 1210
	/*! \brief for each particle get the verlet list
	 *
	 * \param r_cut cut-off radius
	 *
incardon's avatar
incardon committed
1211 1212
	 * \return a VerletList object
	 *
1213
	 */
incardon's avatar
incardon committed
1214
	VerletList<dim,St,FAST,shift<dim,St> > getVerlet(St r_cut)
1215
	{
incardon's avatar
incardon committed
1216 1217 1218 1219
#ifdef SE_CLASS3
		se3.getNN();
#endif

1220 1221 1222
		VerletList<dim,St,FAST,shift<dim,St>> ver;

		// get the processor bounding box
1223 1224 1225 1226 1227
		Box<dim, St> bt = getDecomposition().getProcessorBounds();

		// Get the ghost
		Ghost<dim,St> g = getDecomposition().getGhost();
		g.magnify(1.013);
1228

1229 1230
		// enlarge the box where the Verlet is defined
		bt.enlarge(g);
1231

incardon's avatar
incardon committed
1232
		ver.Initialize(bt,getDecomposition().getProcessorBounds(),r_cut,v_pos,g_m,VL_NON_SYMMETRIC);
1233

incardon's avatar
incardon committed
1234 1235
		ver.set_ndec(getDecomposition().get_ndec());

1236 1237 1238
		return ver;
	}

1239 1240 1241 1242 1243
	/*! \brief for each particle get the verlet list
	 *
	 * \param r_cut cut-off radius
	 * \param ver Verlet to update
	 * \param r_cut cutoff radius
incardon's avatar
incardon committed
1244
	 * \param opt option like VL_SYMMETRIC and VL_NON_SYMMETRIC or VL_CRS_SYMMETRIC
1245 1246 1247 1248
	 *
	 */
	void updateVerlet(VerletList<dim,St,FAST,shift<dim,St> > & ver, St r_cut, size_t opt = VL_NON_SYMMETRIC)
	{
incardon's avatar
incardon committed
1249 1250 1251 1252
#ifdef SE_CLASS3
		se3.getNN();
#endif

incardon's avatar
incardon committed
1253 1254 1255 1256 1257 1258
		if (opt == VL_SYMMETRIC)
		{
			auto & NN = ver.getInternalCellList();

			// Here we have to check that the Box defined by the Cell-list is the same as the domain box of this
			// processor. if it is not like that we have to completely reconstruct from stratch
incardon's avatar
incardon committed
1259
			bool to_reconstruct = NN.get_ndec() != getDecomposition().get_ndec();
incardon's avatar
incardon committed
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271

			if (to_reconstruct == false)
				ver.update(getDecomposition().getDomain(),r_cut,v_pos,g_m, opt);
			else
			{
				VerletList<dim,St,FAST,shift<dim,St> > ver_tmp;

				ver_tmp = getVerlet(r_cut);
				ver.swap(ver);
			}
		}
		else if (opt == VL_CRS_SYMMETRIC)
incardon's avatar
incardon committed
1272
		{
1273 1274 1275 1276 1277 1278 1279 1280
#ifdef SE_CLASS1
			if ((opt & BIND_DEC_TO_GHOST))
			{
				std::cerr << __FILE__ << ":" << __LINE__ << " Error the vector has been constructed without BIND_DEC_TO_GHOST, updateVerlet with the option VL_CRS_SYMMETRIC require the vector to be constructed with BIND_DEC_TO_GHOST option " << std::endl;
				ACTION_ON_ERROR(VECTOR_DIST_ERROR_OBJECT);
			}
#endif

incardon's avatar
incardon committed
1281 1282
			auto & NN = ver.getInternalCellList();

incardon's avatar
incardon committed
1283 1284
			// Here we have to check that the Box defined by the Cell-list is the same as the domain box of this
			// processor. if it is not like that we have to completely reconstruct from stratch
incardon's avatar
incardon committed
1285
			bool to_reconstruct = NN.get_ndec() != getDecomposition().get_ndec();
incardon's avatar
incardon committed
1286 1287 1288 1289

			if (to_reconstruct == false)
			{
				// Shift
1290
				grid_key_dx<dim> shift;
incardon's avatar
incardon committed
1291 1292 1293

				// Add padding
				for (size_t i = 0 ; i < dim ; i++)
1294
					shift.set_d(i,NN.getPadding(i));
incardon's avatar
incardon committed
1295 1296 1297

				grid_sm<dim,void> gs = NN.getInternalGrid();

1298 1299
				getDecomposition().setNNParameters(shift,gs);

incardon's avatar
incardon committed
1300
				ver.updateCrs(getDecomposition().getDomain(),r_cut,v_pos,g_m,
1301 1302
						      getDecomposition().getCRSDomainCells(),
							  getDecomposition().getCRSAnomDomainCells());
incardon's avatar
incardon committed
1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317
			}
			else
			{
				VerletList<dim,St,FAST,shift<dim,St> > ver_tmp;

				ver_tmp = getVerletCrs(r_cut);
				ver.swap(ver_tmp);
			}
		}
		else
		{
			auto & NN = ver.getInternalCellList();

			// Here we have to check that the Box defined by the Cell-list is the same as the domain box of this
			// processor. if it is not like that we have to completely reconstruct from stratch
incardon's avatar
incardon committed
1318
			bool to_reconstruct = NN.get_ndec() != getDecomposition().get_ndec();
incardon's avatar
incardon committed
1319 1320 1321 1322 1323 1324

			if (to_reconstruct == false)
				ver.update(getDecomposition().getDomain(),r_cut,v_pos,g_m, opt);
			else
			{
				VerletList<dim,St,FAST,shift<dim,St> > ver_tmp;
incardon's avatar
incardon committed
1325

incardon's avatar
incardon committed
1326 1327 1328 1329
				ver_tmp = getVerlet(r_cut);
				ver.swap(ver_tmp);
			}
		}
1330 1331
	}

incardon's avatar
incardon committed
1332

1333 1334 1335 1336 1337 1338 1339
	/*! \brief Construct a cell list starting from the stored particles and reorder a vector according to the Hilberts curve
	 *
	 * \tparam CellL CellList type to construct
	 *
	 * \param m an order of a hilbert curve
	 *
	 */
1340
	template<typename CellL=CellList_gen<dim,St,Process_keys_lin,Mem_fast,shift<dim,St> > > void reorder (int32_t m)
1341
	{
1342
		reorder(m,getDecomposition().getGhost());
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
	}


	/*! \brief Construct a cell list starting from the stored particles and reorder a vector according to the Hilberts curve
	 *
	 *
	 *It differs from the reorder(m) for an additional parameter, in case the
	 * domain + ghost is not big enough to contain additional padding particles, a Cell list
	 * with bigger space can be created
	 * (padding particles in general are particles added by the user out of the domains)
	 *
	 * \param m order of a curve
	 * \param enlarge In case of padding particles the cell list must be enlarged, like a ghost this parameter say how much must be enlarged
	 *
	 */
1358
	template<typename CellL=CellList_gen<dim,St,Process_keys_lin,Mem_fast,shift<dim,St> > > void reorder(int32_t m, const Ghost<dim,St> & enlarge)
1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369
	{
		// reset the ghost part
		v_pos.resize(g_m);
		v_prp.resize(g_m);


		CellL cell_list;

		// calculate the parameters of the cell list

		// get the processor bounding box
1370
		Box<dim,St> pbox = getDecomposition().getProcessorBounds();
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
		// extend by the ghost
		pbox.enlarge(enlarge);

		size_t div[dim];

		// Calculate the division array and the cell box
		for (size_t i = 0 ; i < dim ; i++)
		{
			div[i] = 1 << m;
		}

1382
		cell_list.Initialize(pbox,div,g_m);
1383 1384 1385 1386 1387 1388 1389 1390 1391

		// for each particle add the particle to the cell list

		auto it = getIterator();

		while (it.isNext())
		{
			auto key = it.get();

incardon's avatar
incardon committed
1392 1393 1394
			Point<dim,St> xp = this->getPos(key);

			cell_list.add(xp,key.getKey());
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442

			++it;
		}

		// Use cell_list to reorder v_pos

		//destination vector
		openfpm::vector<Point<dim,St>> v_pos_dest;
		openfpm::vector<prop> v_prp_dest;

		v_pos_dest.resize(v_pos.size());
		v_prp_dest.resize(v_prp.size());

		//hilberts curve iterator
		grid_key_dx_iterator_hilbert<dim> h_it(m);

		//Index for v_pos_dest
		size_t count = 0;

		grid_key_dx<dim> ksum;

		for (size_t i = 0; i < dim ; i++)
			ksum.set_d(i,cell_list.getPadding(i));

		while (h_it.isNext())
		{
		  auto key = h_it.get();
		  key += ksum;

		  size_t lin = cell_list.getGrid().LinId(key);

		  // for each particle in the Cell "lin"
		  for (size_t i = 0; i < cell_list.getNelements(lin); i++)
		  {
			  //reorder
			  auto v = cell_list.get(lin,i);
			  v_pos_dest.get(count) = v_pos.get(v);
			  v_prp_dest.get(count) = v_prp.get(v);

			  count++;
		  }
		  ++h_it;
		}

		v_pos.swap(v_pos_dest);
		v_prp.swap(v_prp_dest);
	}

1443 1444
	/*! \brief It return the number of particles contained by the previous processors
	 *
Pietro Incardona's avatar
Pietro Incardona committed
1445
	 * \warning It only work with the initial decomposition
1446 1447 1448 1449 1450 1451 1452 1453 1454
	 *
	 * Given 1000 particles and 3 processors, you will get
	 *
	 * * Processor 0: 0
	 * * Processor 1: 334
	 * * Processor 2: 667
	 *
	 * \param np initial number of particles
	 *
Pietro Incardona's avatar
Pietro Incardona committed
1455 1456
	 * \return number of particles contained by the previous processors
	 *
1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477
	 */
	size_t init_size_accum(size_t np)
	{
		size_t accum = 0;

		// convert to a local number of elements
		size_t p_np = np / v_cl.getProcessingUnits();

		// Get non divisible part
		size_t r = np % v_cl.getProcessingUnits();

		accum = p_np * v_cl.getProcessUnitID();

		// Distribute the remain particles
		if (v_cl.getProcessUnitID() <= r)
			accum += v_cl.getProcessUnitID();
		else
			accum += r;

		return accum;
	}
incardon's avatar
incardon committed
1478

1479
	/*! \brief Get an iterator that traverse domain and ghost particles
incardon's avatar
incardon committed
1480 1481 1482 1483
	 *
	 * \return an iterator
	 *
	 */
1484
	vector_dist_iterator getIterator()
incardon's avatar
incardon committed
1485
	{
incardon's avatar
incardon committed
1486 1487 1488
#ifdef SE_CLASS3
		se3.getIterator();
#endif
1489
		return vector_dist_iterator(0, v_pos.size());
incardon's avatar
incardon committed
1490 1491
	}

1492 1493 1494 1495
	/*! /brief Get a grid Iterator
	 *
	 * Usefull function to place particles on a grid or grid-like (grid + noise)
	 *
Pietro Incardona's avatar
Pietro Incardona committed
1496 1497
	 * \param sz size of the grid
	 *
1498 1499 1500
	 * \return a Grid iterator
	 *
	 */
1501
	inline grid_dist_id_iterator_dec<Decomposition> getGridIterator(const size_t (&sz)[dim])
1502 1503 1504
	{
		grid_key_dx<dim> start;
		grid_key_dx<dim> stop;
1505
		for (size_t i = 0; i < dim; i++)
1506
		{
1507
			start.set_d(i, 0);
1508
			stop.set_d(i, sz[i] - 1);
1509 1510
		}

1511
		grid_dist_id_iterator_dec<Decomposition> it_dec(getDecomposition(), sz, start, stop);
1512 1513 1514
		return it_dec;
	}

1515 1516 1517 1518 1519
	/*! \brief Get the iterator across the position of the ghost particles
	 *
	 * \return an iterator
	 *
	 */
1520
	vector_dist_iterator getGhostIterator() const
incardon's avatar
incardon committed
1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
	{
#ifdef SE_CLASS3
		se3.getIterator();
#endif

		return vector_dist_iterator(g_m, v_pos.size());
	}

	/*! \brief Get the iterator across the position of the ghost particles
	 *
	 * \return an iterator
	 *
	 */
	vector_dist_iterator getGhostIterator_no_se3() const
1535
	{
1536
		return vector_dist_iterator(g_m, v_pos.size());
1537 1538
	}

incardon's avatar
incardon committed
1539
	/*! \brief Get an iterator that traverse the particles in the domain
incardon's avatar
incardon committed
1540
	 *         using a cell list
incardon's avatar
incardon committed
1541
	 *
incardon's avatar
incardon committed
1542 1543 1544
	 * \param NN Cell-list
	 *
	 * \return an iterator over the particles
incardon's avatar
incardon committed
1545 1546
	 *
	 */
incardon's avatar
incardon committed
1547 1548
	template<typename CellList> ParticleIt_Cells<dim,CellList>
	getDomainIteratorCells(CellList & NN)
incardon's avatar
incardon committed
1549 1550 1551 1552 1553 1554
	{
#ifdef SE_CLASS3
		se3.getIterator();
#endif

		// Shift
1555
		grid_key_dx<dim> shift;
incardon's avatar
incardon committed
1556 1557 1558

		// Add padding
		for (size_t i = 0 ; i < dim ; i++)
1559
			shift.set_d(i,NN.getPadding(i));
incardon's avatar
incardon committed
1560 1561 1562

		grid_sm<dim,void> gs = NN.getInternalGrid();

1563 1564
		getDecomposition().setNNParameters(shift,gs);

incardon's avatar
incardon committed
1565
		return ParticleIt_Cells<dim,CellList>(NN,getDecomposition().getDomainCells(),g_m);
incardon's avatar
incardon committed
1566 1567
	}

1568
	/*! \brief Get an iterator that traverse the particles in the domain
1569 1570 1571 1572
	 *
	 * \return an iterator
	 *
	 */