ie_ghost.hpp 39.3 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4 5 6 7 8 9 10 11
/*
 * ie_ghost.hpp
 *
 *  Created on: Aug 8, 2015
 *      Author: i-bird
 */

#ifndef SRC_DECOMPOSITION_IE_GHOST_HPP_
#define SRC_DECOMPOSITION_IE_GHOST_HPP_

#include "common.hpp"
incardon's avatar
incardon committed
12
#include "nn_processor.hpp"
incardon's avatar
incardon committed
13
#include "Decomposition/shift_vect_converter.hpp"
incardon's avatar
incardon committed
14
#include "Decomposition/cuda/ie_ghost_gpu.cuh"
incardon's avatar
incardon committed
15

incardon's avatar
incardon committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
//! Processor id and box id
struct proc_box_id
{
	size_t proc_id;
	size_t box_id;
	size_t shift_id;

	//! operator to reorder
	bool operator<(const proc_box_id & pbi) const
	{
		if (proc_id < pbi.proc_id)
		{return true;}
		else if (proc_id == pbi.proc_id)
		{
			return shift_id < pbi.shift_id;
		}

		return false;
	}
};
incardon's avatar
incardon committed
36

incardon's avatar
incardon committed
37

incardon's avatar
incardon committed
38 39 40 41 42 43 44 45
/*! \brief structure that store and compute the internal and external local ghost box
 *
 * \tparam dim is the dimensionality of the physical domain we are going to decompose.
 * \tparam T type of the space we decompose, Real, Integer, Complex ...
 *
 * \see CartDecomposition
 *
 */
incardon's avatar
incardon committed
46
template<unsigned int dim, typename T, typename Memory, template<typename> class layout_base >
incardon's avatar
incardon committed
47 48 49 50 51 52 53 54 55 56 57
class ie_ghost
{
	//! for each sub-domain (first vector), contain the list (nested vector) of the neighborhood processors
	//! and for each processor contain the boxes calculated from the intersection
	//! of the sub-domains + ghost with the near-by processor sub-domain () and the other way around
	//! \see calculateGhostBoxes
	openfpm::vector< openfpm::vector< Box_proc<dim,T> > > box_nn_processor_int;

	//! It store the same information of box_nn_processor_int organized by processor id
	openfpm::vector< Box_dom<dim,T> > proc_int_box;

58
	//! External ghost boxes for this processor
incardon's avatar
incardon committed
59 60
	openfpm::vector<p_box<dim,T> > vb_ext;

61
	//! Internal ghost boxes for this processor domain
incardon's avatar
incardon committed
62 63 64 65
	openfpm::vector<aggregate<unsigned int,unsigned int,unsigned int>,Memory,typename layout_base<aggregate<unsigned int,unsigned int,unsigned int>>::type,layout_base> vb_int;

	//! Internal ghost boxes for this processor domain
	openfpm::vector<Box<dim,T>,Memory,typename layout_base<Box<dim,T>>::type,layout_base> vb_int_box;
incardon's avatar
incardon committed
66 67

	//! Cell-list that store the geometrical information of the internal ghost boxes
incardon's avatar
incardon committed
68 69 70 71
	CellList<dim,T,Mem_fast<Memory,int>,shift<dim,T>> geo_cell;

	typedef openfpm::vector<Box<dim,T>,Memory,typename layout_base<Box<dim,T>>::type,layout_base> proc_boxes;

72
	//! shift vectors
73
	openfpm::vector<Point<dim,T>,Memory,typename layout_base<Point<dim,T>>::type,layout_base> shifts;
74

75
	//! Temporal buffers to return temporal information for ghost_processorID
76
	openfpm::vector<std::pair<size_t,size_t>> ids_p;
77 78

	//! Temporal buffers to return temporal information
79 80
	openfpm::vector<size_t> ids;

incardon's avatar
incardon committed
81
	//! shift converter
82
	shift_vect_converter<dim,T,Memory,layout_base> sc_convert;
83

incardon's avatar
incardon committed
84 85 86
	//! host to device transfer
	bool host_dev_transfer = false;

87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	/*! \brief Given a local sub-domain i, it give the id of such sub-domain in the sent list
	 *         for the processor p_id
	 *
	 * Processor 5 send its sub-domains to processor 6 and will receive the list from 6
	 *
	 * This function search if a local sub-domain has been sent to a processor p_id, if
	 * found it return at witch position is in the list of the sent sub-domains
	 *
	 * \param nn_p structure that store the processor graph as near processor
	 * \param p_id near processor rank
	 * \param i sub-domain
	 *
	 * \return Given a local sub-domain i, it give the id of such sub-domain in the sent list
	 *         for the processor p_id
	 *
	 */
incardon's avatar
incardon committed
103
	inline size_t link_ebx_ibx(const nn_prcs<dim,T,layout_base,Memory> & nn_p, size_t p_id, size_t i)
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	{
		// Search for the correct id
		size_t k = 0;
		size_t p_idp = nn_p.ProctoID(p_id);
		for (k = 0 ; k < nn_p.getSentSubdomains(p_idp).size() ; k++)
		{
			if (nn_p.getSentSubdomains(p_idp).get(k) == i)
				break;
		}
		if (k == nn_p.getSentSubdomains(p_idp).size())
			std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " sub-domain not found\n";

		return k;
	}

	/*! \brief This is the external and internal ghost box link formula
	*
	* This formula is pretty important and require an extensive explanation
	*
	* \verbatim

	+------------+
	|            |
	|            +---+---------+
	| Processor 5|   |         |
	|            | E | Proc 6  |
	|  Sub 0     | 0 |         |
	|            | _ | Sub 9   |
	|            | 9 |         |
	|            |   |         |
	|            +---+---------+
	|            |
	+------------+

	* \endverbatim
	*
	* E0_6 is an external ghost box from the prospective of processor 5 and an internal
	* ghost boxes from the prospective of processor 6. So for every external
	* ghost box that processor 5 compute, exist an internal ghost box in processor 6
	*
	* Here we link this information with an unique id, for processor 5 and 6.
	* Consider Processor 5 sending to processor 6
	* its sub-domains, including the one in figure with id 0 in the list, and
	* receive from processor 6 the sub-domain in figure as id 9. Consider also
148 149 150
	*  we have 16 processor. E0_9 come from the intersection of the expanded sub-domain
	* 0 with 9 (Careful the id is related to the send and receive position in the list)
	* and the intersection is in the sector 0
151 152
	*
	*
153
	* The id of the external box (for processor 5) is calculated as
154
	*
155 156 157 158 159 160 161
	* ((k * N_b + b) * v_cl.getProcessingUnits() + p_id) * openfpm::math::pow(3,dim) + c.lin()
	*
	* The parameter assume a different meaning if they the formula is used for calculating
	* external/internal ghost boxes id
	*
	* \param k expanded sub-domain sent/received to/from p_id ( 0 )
	* \param b sub-domain received/sent from/to p_id ( 9 )
162 163
	* \param p_id processor id ( 6 )
	* \param c sector where the sub-domain b live
164
	* \param N_b number of sub-domain received/sent from/to p_id
165
	* \param v_cl Vcluster
166 167 168
	* \param ei indicate if the formula is used to calculate external (true) or internal (false) ids
	*
	* \return id of the external/internal ghost
169
	*
170
	* \note To an explanation about the sectors see getShiftVectors
171 172
	*
	*/
173
	inline size_t ebx_ibx_form(size_t k, size_t b, size_t p_id, const comb<dim> & c ,size_t N_b, Vcluster<> & v_cl, const bool ei)
174
	{
175 176 177 178 179 180
		comb<dim> cext = c;

		if (ei == true)
			cext.sign_flip();

		return ((k * N_b + b) * v_cl.getProcessingUnits() + p_id) * openfpm::math::pow(3,dim) + cext.lin();
181 182
	}

incardon's avatar
incardon committed
183 184
protected:

185 186 187 188 189
	/*! \brief Here we generare the shift vectors
	 *
	 * \param domain box that describe the domain
	 *
	 */
incardon's avatar
incardon committed
190
	void generateShiftVectors(const Box<dim,T> & domain, size_t (& bc)[dim])
191
	{
incardon's avatar
incardon committed
192
		sc_convert.generateShiftVectors(domain,bc,shifts);
193 194
	}

incardon's avatar
incardon committed
195 196 197 198 199
	/*! \brief Initialize the geo cell list structure
	 *
	 * The geo cell list structure exist to speed up the labelling the points if they fall on some
	 * internal ghost
	 *
200 201 202
	 * \param domain where the cell list is defined
	 * \param div number of division of the cell list
	 *
incardon's avatar
incardon committed
203
	 */
204
	void Initialize_geo_cell(const Box<dim,T> & domain, const size_t (&div)[dim])
incardon's avatar
incardon committed
205 206
	{
		// Initialize the geo_cell structure
incardon's avatar
incardon committed
207
		geo_cell.Initialize(domain,div,0);
incardon's avatar
incardon committed
208 209
	}

incardon's avatar
incardon committed
210 211
	/*! \brief Create the box_nn_processor_int (bx part)  structure
	 *
Pietro Incardona's avatar
Pietro Incardona committed
212 213
	 * For each sub-domain of the local processor it store the intersection between the enlarged
	 * sub-domain of the calling processor with the adjacent processors sub-domains (External ghost box)
incardon's avatar
incardon committed
214
	 *
215
	 * \param v_cl Virtual cluster
incardon's avatar
incardon committed
216
	 * \param ghost margins
incardon's avatar
incardon committed
217
	 * \param sub_domains vector of local sub-domains
218
	 * \param box_nn_processor it will store for each sub-domain the near processors
incardon's avatar
incardon committed
219
	 * \param nn_p contain the sub-domains of the near processors
incardon's avatar
incardon committed
220 221 222 223 224
	 *
	 * \note Are the G8_0 G9_0 G9_1 G5_0 boxes in calculateGhostBoxes
	 * \see calculateGhostBoxes
	 *
	 */
225
	void create_box_nn_processor_ext(Vcluster<> & v_cl,
incardon's avatar
incardon committed
226
			                         Ghost<dim,T> & ghost,
incardon's avatar
incardon committed
227
									 openfpm::vector<SpaceBox<dim,T>,Memory,typename layout_base<SpaceBox<dim, T>>::type,layout_base> & sub_domains,
incardon's avatar
incardon committed
228
									 const openfpm::vector<openfpm::vector<long unsigned int> > & box_nn_processor,
incardon's avatar
incardon committed
229
									 const nn_prcs<dim,T,layout_base,Memory> & nn_p)
incardon's avatar
incardon committed
230
	{
incardon's avatar
incardon committed
231 232
		box_nn_processor_int.resize(sub_domains.size());
		proc_int_box.resize(nn_p.getNNProcessors());
incardon's avatar
incardon committed
233 234 235 236 237 238 239 240 241

		// For each sub-domain
		for (size_t i = 0 ; i < sub_domains.size() ; i++)
		{
			SpaceBox<dim,T> sub_with_ghost = sub_domains.get(i);

			// enlarge the sub-domain with the ghost
			sub_with_ghost.enlarge(ghost);

242
			// resize based on the number of near processors
incardon's avatar
incardon committed
243 244
			box_nn_processor_int.get(i).resize(box_nn_processor.get(i).size());

245
			// For each processor near to this sub-domain
incardon's avatar
incardon committed
246 247
			for (size_t j = 0 ; j < box_nn_processor.get(i).size() ; j++)
			{
248
				// near processor
incardon's avatar
incardon committed
249 250
				size_t p_id = box_nn_processor.get(i).get(j);

Pietro Incardona's avatar
Pietro Incardona committed
251
				// used later
incardon's avatar
incardon committed
252
				Box_dom<dim,T> & proc_int_box_g = proc_int_box.get(nn_p.ProctoID(p_id));
incardon's avatar
incardon committed
253

254 255 256 257
				// Number of received sub-domains
				size_t n_r_sub = nn_p.getNRealSubdomains(p_id);

				// get the set of sub-domains, sector position, and real sub-domain id of the near processor p_id
258 259
				const openfpm::vector< ::Box<dim,T> > & nn_processor_subdomains_g = nn_p.getNearSubdomains(p_id);
				const openfpm::vector< comb<dim> > & nnpsg_pos = nn_p.getNearSubdomainsPos(p_id);
260
				const openfpm::vector< size_t > & r_sub = nn_p.getNearSubdomainsRealId(p_id);
incardon's avatar
incardon committed
261

Pietro Incardona's avatar
Pietro Incardona committed
262
				// used later
incardon's avatar
incardon committed
263 264 265 266 267 268
				openfpm::vector< ::Box<dim,T> > & box_nn_processor_int_gg = box_nn_processor_int.get(i).get(j).bx;

				// for each near processor sub-domain intersect with the enlarged local sub-domain and store it
				for (size_t b = 0 ; b < nn_processor_subdomains_g.size() ; b++)
				{
					::Box<dim,T> bi;
Pietro Incardona's avatar
Pietro Incardona committed
269
					::Box<dim,T> sub_bb(nn_processor_subdomains_g.get(b));
incardon's avatar
incardon committed
270

Pietro Incardona's avatar
Pietro Incardona committed
271
					bool intersect = sub_with_ghost.Intersect(sub_bb,bi);
incardon's avatar
incardon committed
272 273 274

					if (intersect == true)
					{
incardon's avatar
incardon committed
275
						struct p_box<dim,T> pb;
incardon's avatar
incardon committed
276 277 278

						pb.box = bi;
						pb.proc = p_id;
incardon's avatar
incardon committed
279
						pb.lc_proc = nn_p.ProctoID(p_id);
280
						pb.shift_id = (size_t)-1;
incardon's avatar
incardon committed
281 282 283 284 285 286 287 288 289 290 291

						//
						// Updating
						//
						// vb_ext
						// box_nn_processor_int
						// proc_int_box
						//
						// They all store the same information but organized in different ways
						// read the description of each for more information
						//
292

incardon's avatar
incardon committed
293 294 295
						vb_ext.add(pb);
						box_nn_processor_int_gg.add(bi);
						proc_int_box_g.ebx.add();
Pietro Incardona's avatar
Pietro Incardona committed
296
						proc_int_box_g.ebx.last().bx = bi;
incardon's avatar
incardon committed
297
						proc_int_box_g.ebx.last().sub = i;
Pietro Incardona's avatar
Pietro Incardona committed
298
						proc_int_box_g.ebx.last().cmb = nnpsg_pos.get(b);
incardon's avatar
incardon committed
299

300 301
						// Search where the sub-domain i is in the sent list for processor p_id
						size_t k = link_ebx_ibx(nn_p,p_id,i);
incardon's avatar
incardon committed
302

303
						proc_int_box_g.ebx.last().id = ebx_ibx_form(k,r_sub.get(b),p_id,nnpsg_pos.get(b),n_r_sub,v_cl,true);
incardon's avatar
incardon committed
304 305 306
					}
				}
			}
incardon's avatar
incardon committed
307
		}
incardon's avatar
incardon committed
308 309
	}

incardon's avatar
incardon committed
310

incardon's avatar
incardon committed
311 312 313 314 315 316
	/*! \brief Create the box_nn_processor_int (nbx part) structure, the geo_cell list and proc_int_box
	 *
	 * This structure store for each sub-domain of this processors the boxes that come from the intersection
	 * of the near processors sub-domains enlarged by the ghost size (Internal ghost box). These boxes
	 * fill a geometrical cell list. The proc_int_box store the same information ordered by near processors
	 *
incardon's avatar
incardon committed
317
	 * \param v_cl Virtual cluster
incardon's avatar
incardon committed
318
	 * \param ghost margins
incardon's avatar
incardon committed
319
	 * \param sub_domains
incardon's avatar
incardon committed
320 321
	 * \param box_nn_processor sub-domains of the near processors
	 * \param nn_p structure that store the near processor sub-domains
incardon's avatar
incardon committed
322 323 324 325 326
	 *
	 * \note Are the B8_0 B9_0 B9_1 B5_0 boxes in calculateGhostBoxes
	 * \see calculateGhostBoxes
	 *
	 */
327
	void create_box_nn_processor_int(Vcluster<> & v_cl,
incardon's avatar
incardon committed
328
			                         Ghost<dim,T> & ghost,
incardon's avatar
incardon committed
329
									 openfpm::vector<SpaceBox<dim,T>,Memory,typename layout_base<SpaceBox<dim, T>>::type,layout_base> & sub_domains,
incardon's avatar
incardon committed
330
									 const openfpm::vector<openfpm::vector<long unsigned int> > & box_nn_processor,
incardon's avatar
incardon committed
331
									 const nn_prcs<dim,T,layout_base,Memory> & nn_p)
incardon's avatar
incardon committed
332 333 334 335 336 337 338 339 340 341
	{
		box_nn_processor_int.resize(sub_domains.size());
		proc_int_box.resize(nn_p.getNNProcessors());

		// For each sub-domain
		for (size_t i = 0 ; i < sub_domains.size() ; i++)
		{
			// For each processor contiguous to this sub-domain
			for (size_t j = 0 ; j < box_nn_processor.get(i).size() ; j++)
			{
342
				// Near processor
incardon's avatar
incardon committed
343 344
				size_t p_id = box_nn_processor.get(i).get(j);

345
				// get the set of sub-domains of the near processor p_id
346 347 348 349
				const openfpm::vector< ::Box<dim,T> > & nn_p_box = nn_p.getNearSubdomains(p_id);

				// get the sector position for each sub-domain in the list
				const openfpm::vector< comb<dim> > nn_p_box_pos = nn_p.getNearSubdomainsPos(p_id);
incardon's avatar
incardon committed
350

351 352 353
				// get the real sub-domain id for each sub-domain
				const openfpm::vector<size_t> r_sub = nn_p.getNearSubdomainsRealId(p_id);

incardon's avatar
incardon committed
354
				// get the local processor id
355
				size_t lc_proc = nn_p.getNearProcessor(p_id);
incardon's avatar
incardon committed
356 357 358 359 360 361 362 363 364 365 366 367 368 369

				// For each near processor sub-domains enlarge and intersect with the local sub-domain and store the result
				for (size_t k = 0 ; k < nn_p_box.size() ; k++)
				{
					// enlarge the near-processor sub-domain
					::Box<dim,T> n_sub = nn_p_box.get(k);

					// local sub-domain
					::SpaceBox<dim,T> l_sub = sub_domains.get(i);

					// Create a margin of ghost size around the near processor sub-domain
					n_sub.enlarge(ghost);

					// Intersect with the local sub-domain
incardon's avatar
incardon committed
370 371
					Box<dim,T> b_int;
					bool intersect = n_sub.Intersect(l_sub,b_int);
incardon's avatar
incardon committed
372 373 374 375

					// store if it intersect
					if (intersect == true)
					{
incardon's avatar
incardon committed
376 377 378 379
						vb_int.add();

						size_t last = vb_int.size() - 1;

incardon's avatar
incardon committed
380
						// the box fill with the processor id
incardon's avatar
incardon committed
381
						vb_int.template get<proc_>(last) = p_id;
incardon's avatar
incardon committed
382 383

						// fill the local processor id
incardon's avatar
incardon committed
384
						vb_int.template get<lc_proc_>(last) = lc_proc;
incardon's avatar
incardon committed
385

386
						// fill the shift id
incardon's avatar
incardon committed
387
						vb_int.template get<shift_id_>(last) = convertShift(nn_p_box_pos.get(k));
388

incardon's avatar
incardon committed
389 390 391 392 393 394 395 396 397 398 399 400 401
						//
						// Updating
						//
						// vb_int
						// box_nn_processor_int
						// proc_int_box
						//
						// They all store the same information but organized in different ways
						// read the description of each for more information
						//

						// add the box to the near processor sub-domain intersections
						openfpm::vector< ::Box<dim,T> > & p_box_int = box_nn_processor_int.get(i).get(j).nbx;
incardon's avatar
incardon committed
402 403
						p_box_int.add(b_int);
						vb_int_box.add(b_int);
incardon's avatar
incardon committed
404 405 406

						// store the box in proc_int_box storing from which sub-domain they come from
						Box_sub<dim,T> sb;
incardon's avatar
incardon committed
407
						sb.bx = b_int;
incardon's avatar
incardon committed
408
						sb.sub = i;
409
						sb.r_sub = r_sub.get(k);
Pietro Incardona's avatar
Pietro Incardona committed
410
						sb.cmb = nn_p_box_pos.get(k);
incardon's avatar
incardon committed
411

incardon's avatar
incardon committed
412
						size_t p_idp = nn_p.ProctoID(p_id);
incardon's avatar
incardon committed
413

414 415
						// Search where the sub-domain i is in the sent list for processor p_id
						size_t s = link_ebx_ibx(nn_p,p_id,i);
incardon's avatar
incardon committed
416

417
						// calculate the id of the internal box
418
						sb.id = ebx_ibx_form(r_sub.get(k),s,v_cl.getProcessUnitID(),nn_p_box_pos.get(k),nn_p.getSentSubdomains(p_idp).size(),v_cl,false);
incardon's avatar
incardon committed
419

420
						Box_dom<dim,T> & pr_box_int = proc_int_box.get(nn_p.ProctoID(p_id));
incardon's avatar
incardon committed
421 422 423 424 425
						pr_box_int.ibx.add(sb);

						// update the geo_cell list

						// get the cells this box span
incardon's avatar
incardon committed
426 427
						const grid_key_dx<dim> p1 = geo_cell.getCellGrid(b_int.getP1());
						const grid_key_dx<dim> p2 = geo_cell.getCellGrid(b_int.getP2());
incardon's avatar
incardon committed
428 429 430 431 432 433 434 435 436

						// Get the grid and the sub-iterator
						auto & gi = geo_cell.getGrid();
						grid_key_dx_iterator_sub<dim> g_sub(gi,p1,p2);

						// add the box-id to the cell list
						while (g_sub.isNext())
						{
							auto key = g_sub.get();
incardon's avatar
incardon committed
437 438 439 440
							size_t cell = gi.LinId(key);

							geo_cell.addCell(cell,vb_int.size()-1);

incardon's avatar
incardon committed
441 442 443 444 445 446
							++g_sub;
						}
					}
				}
			}
		}
incardon's avatar
incardon committed
447 448

		reorder_geo_cell();
incardon's avatar
incardon committed
449
	}
incardon's avatar
incardon committed
450

incardon's avatar
incardon committed
451
	/*! \brief in this function we reorder the list in each cells by processor id
incardon's avatar
incardon committed
452
	 *
incardon's avatar
incardon committed
453
	 * suppose in one cell we have 7 boxes each box contain the processor id
incardon's avatar
incardon committed
454
	 *
incardon's avatar
incardon committed
455 456 457 458 459 460 461
	 * 1,5,9,5,1,1,6
	 *
	 * after reorder we have the following sequence
	 *
	 * 1,1,1,5,5,6,9
	 *
	 * This simplify the procedure to get a unique list of processor ids
incardon's avatar
incardon committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	 * indicating on which processor a particle must be replicated as ghost
	 *
	 */
	void reorder_geo_cell()
	{
		openfpm::vector<proc_box_id> tmp_sort;

		size_t div[dim];

		for (size_t i = 0 ; i < dim ; i++)	{div[i] = geo_cell.getDiv()[i];}

		grid_sm<dim,void> gs(div);

		grid_key_dx_iterator<dim> it(gs);

		while (it.isNext())
		{
			size_t cell = gs.LinId(it.get());

			size_t sz = geo_cell.getNelements(cell);
			tmp_sort.resize(sz);

			for (size_t i = 0 ; i < sz ; i++)
			{
				tmp_sort.get(i).box_id = geo_cell.get(cell,i);
				tmp_sort.get(i).proc_id = vb_int.template get<proc_>(tmp_sort.get(i).box_id);
				tmp_sort.get(i).shift_id = vb_int.template get<shift_id_>(tmp_sort.get(i).box_id);
			}

			tmp_sort.sort();

			// now we set again the cell in an ordered way

			for (size_t i = 0 ; i < sz ; i++)
			{geo_cell.get(cell,i) = tmp_sort.get(i).box_id;}

			++it;
		}
	}
Pietro Incardona's avatar
Pietro Incardona committed
501

incardon's avatar
incardon committed
502 503
public:

Pietro Incardona's avatar
Pietro Incardona committed
504 505 506 507
	//! Default constructor
	ie_ghost() {};

	//! Copy constructor
incardon's avatar
incardon committed
508
	ie_ghost(const ie_ghost<dim,T,Memory,layout_base> & ie)
Pietro Incardona's avatar
Pietro Incardona committed
509
	{
incardon's avatar
incardon committed
510
		this->operator=(ie);
Pietro Incardona's avatar
Pietro Incardona committed
511 512 513
	}

	//! Copy constructor
incardon's avatar
incardon committed
514
	ie_ghost(ie_ghost<dim,T,Memory,layout_base> && ie)
Pietro Incardona's avatar
Pietro Incardona committed
515 516 517 518 519
	{
		this->operator=(ie);
	}

	//! Copy operator
incardon's avatar
incardon committed
520
	inline ie_ghost<dim,T,Memory,layout_base> & operator=(ie_ghost<dim,T,Memory,layout_base> && ie)
Pietro Incardona's avatar
Pietro Incardona committed
521 522 523 524 525
	{
		box_nn_processor_int.swap(ie.box_nn_processor_int);
		proc_int_box.swap(ie.proc_int_box);
		vb_ext.swap(ie.vb_ext);
		vb_int.swap(ie.vb_int);
incardon's avatar
incardon committed
526
		vb_int_box.swap(ie.vb_int_box);
Pietro Incardona's avatar
Pietro Incardona committed
527 528 529 530 531 532 533 534 535
		geo_cell.swap(ie.geo_cell);
		shifts.swap(ie.shifts);
		ids_p.swap(ie.ids_p);
		ids.swap(ie.ids);

		return *this;
	}

	//! Copy operator
incardon's avatar
incardon committed
536
	inline ie_ghost<dim,T,Memory,layout_base> & operator=(const ie_ghost<dim,T,Memory,layout_base> & ie)
Pietro Incardona's avatar
Pietro Incardona committed
537 538 539 540 541
	{
		box_nn_processor_int = ie.box_nn_processor_int;
		proc_int_box = ie.proc_int_box;
		vb_ext = ie.vb_ext;
		vb_int = ie.vb_int;
incardon's avatar
incardon committed
542
		vb_int_box = ie.vb_int_box;
Pietro Incardona's avatar
Pietro Incardona committed
543
		geo_cell = ie.geo_cell;
Pietro Incardona's avatar
Pietro Incardona committed
544 545 546 547 548 549 550
		shifts = ie.shifts;
		ids_p = ie.ids_p;
		ids = ie.ids;

		return *this;
	}

incardon's avatar
incardon committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575


	/*! \brief duplicate this structure changing layout and Memory
	 *
	 * \return a structure with Memory type and layout changed
	 *
	 */
	template<typename Memory2, template <typename> class layout_base2>
	inline ie_ghost<dim,T,Memory2,layout_base2> duplicate()
	{
		ie_ghost<dim,T,Memory2,layout_base2> tmp;

		tmp.private_get_box_nn_processor_int() = box_nn_processor_int;
		tmp.private_get_proc_int_box() = proc_int_box;
		tmp.private_get_vb_ext() = vb_ext;
		tmp.private_get_vb_int() = vb_int;
		tmp.private_get_vb_int_box() = vb_int_box;
		tmp.private_geo_cell() = geo_cell;
		tmp.private_get_shifts() = shifts;
		tmp.private_get_ids_p() = ids_p;
		tmp.private_get_ids() = ids;

		return tmp;
	}

576 577 578
	/*! It return the shift vector
	 *
	 * Consider a domain with some ghost, at the border of the domain the
579
	 * ghost must be treated in a special way, depending on the periodicity
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
	 * of the boundary
	 *
		\verbatim

															[1,1]
			+---------+------------------------+---------+
			| (1,-1)  |                        | (1,1)   |
			|   |     |    (1,0) --> 7         |   |     |
			|   v     |                        |   v     |
			|   6     |                        |   8     |
			+--------------------------------------------+
			|         |                        |         |
			|         |                        |         |
			|         |                        |         |
			| (-1,0)  |                        | (1,0)   |
			|    |    |                        |   |     |
			|    v    |      (0,0) --> 4       |   v     |
			|    3    |                        |   5     |
			|         |                        |         |
		 B	|         |                        |     A   |
		*	|         |                        |    *    |
			|         |                        |         |
			|         |                        |         |
			|         |                        |         |
			+--------------------------------------------+
			| (-1,-1) |                        | (-1,1)  |
			|    |    |   (-1,0) --> 1         |    |    |
			|    v    |                        |    v    |
			|    0    |                        |    2    |
			+---------+------------------------+---------+


		\endverbatim
	 *
	 *
	 * if a particle is bound in (1,0) linearized to 5, before communicate this particle (A in figure)
	 * must be shifted on -1.0 on x (B in figure)
	 *
	 * This function return the set of shift vectors that determine such shift, for example
	 * in the example above the shift at position 5 will be (0,-1.0)
	 *
621 622
	 * \return the shift vectors
	 *
623
	 */
624
	const openfpm::vector<Point<dim,T>,Memory,typename layout_base<Point<dim,T>>::type,layout_base> & getShiftVectors()
625
	{
incardon's avatar
incardon committed
626 627 628 629 630
		if (host_dev_transfer == false)
		{
			shifts.template hostToDevice<0>();
		}

631 632 633
		return shifts;
	}

incardon's avatar
incardon committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647
	/*! It return the converted shift vector
	 *
	 * In high dimensions the number of shifts vectors explode exponentially, so we are
	 * expecting that some of the boundary is non periodic to reduce the numbers of shift
	 * vectors
	 *
	 * \return the shift vectors
	 *
	 */
	size_t convertShift(const comb<dim> & cmb)
	{
		return sc_convert.linId(cmb);
	}

incardon's avatar
incardon committed
648 649 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
	/*! \brief Get the number of Internal ghost boxes for one processor
	 *
	 * \param id near processor list id (the id go from 0 to getNNProcessor())
	 * \return the number of internal ghost
	 *
	 */
	inline size_t getProcessorNIGhost(size_t id) const
	{
		return proc_int_box.get(id).ibx.size();
	}

	/*! \brief Get the number of External ghost boxes for one processor id
	 *
	 * \param id near processor list id (the id go from 0 to getNNProcessor())
	 * \return the number of external ghost
	 *
	 */
	inline size_t getProcessorNEGhost(size_t id) const
	{
		return proc_int_box.get(id).ebx.size();
	}

	/*! \brief Get the j Internal ghost box for one processor
	 *
	 * \param id near processor list id (the id go from 0 to getNNProcessor())
	 * \param j box (each near processor can produce more than one internal ghost box)
	 * \return the box
	 *
	 */
	inline const ::Box<dim,T> & getProcessorIGhostBox(size_t id, size_t j) const
	{
679
		return proc_int_box.get(id).ibx.get(j).bx;
incardon's avatar
incardon committed
680 681 682 683 684 685 686 687 688 689 690
	}

	/*! \brief Get the j External ghost box
	 *
	 * \param id near processor list id (the id go from 0 to getNNProcessor())
	 * \param j box (each near processor can produce more than one external ghost box)
	 * \return the box
	 *
	 */
	inline const ::Box<dim,T> & getProcessorEGhostBox(size_t id, size_t j) const
	{
691
		return proc_int_box.get(id).ebx.get(j).bx;
incardon's avatar
incardon committed
692 693
	}

Pietro Incardona's avatar
Pietro Incardona committed
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717
	/*! \brief Get the j External ghost box sector
	 *
	 * \param id near processor list id (the id go from 0 to getNNProcessor())
	 * \param j box (each near processor can produce more than one external ghost box)
	 * \return the sector
	 *
	 */
	inline const comb<dim> & getProcessorEGhostPos(size_t id, size_t j) const
	{
		return proc_int_box.get(id).ebx.get(j).cmb;
	}

	/*! \brief Get the ghost box sector of the external ghost box linked with the j internal ghost box
	 *
	 * \param id near processor list id (the id go from 0 to getNNProcessor())
	 * \param j box (each near processor can produce more than one internal ghost box)
	 * \return the sector
	 *
	 */
	inline const comb<dim> & getProcessorIGhostPos(size_t id, size_t j) const
	{
		return proc_int_box.get(id).ibx.get(j).cmb;
	}

incardon's avatar
incardon committed
718
	/*! \brief Get the j Internal ghost box id
Pietro Incardona's avatar
Pietro Incardona committed
719 720 721 722
	 *
	 * Every internal ghost box has a linked external ghost box, because they overlap
	 * and they must contain the same information (Think on a ghost_get). So if exist
	 *  an internal ghost box with id x, exist also an external ghost box with id x
incardon's avatar
incardon committed
723 724 725
	 *
	 * \param id near processor list id (the id go from 0 to getNNProcessor())
	 * \param j box (each near processor can produce more than one internal ghost box)
Pietro Incardona's avatar
Pietro Incardona committed
726
	 * \return the box id
incardon's avatar
incardon committed
727 728 729 730 731 732 733 734
	 *
	 */
	inline size_t getProcessorIGhostId(size_t id, size_t j) const
	{
		return proc_int_box.get(id).ibx.get(j).id;
	}

	/*! \brief Get the j External ghost box id
Pietro Incardona's avatar
Pietro Incardona committed
735 736 737 738
	 *
	 * Every external ghost box has a linked internal ghost box, because they overlap
	 * and they must contain the same information (Think on a ghost_get). So if exist
	 *  an internal ghost box with id x, exist also an external ghost box with id x
incardon's avatar
incardon committed
739 740 741 742 743 744 745 746 747 748 749
	 *
	 * \param id near processor list id (the id go from 0 to getNNProcessor())
	 * \param j box (each near processor can produce more than one external ghost box)
	 * \return the box
	 *
	 */
	inline size_t getProcessorEGhostId(size_t id, size_t j) const
	{
		return proc_int_box.get(id).ebx.get(j).id;
	}

750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
	/*! \brief Get the sub-domain send-id at witch belong the internal ghost box
	 *
	 * The internal ghost box is create from the intersection a local sub-domain
	 * and an extended sub-domain communicated from another processor. This function
	 * return the id of the sub-domain in the receiving list
	 *
	 * \param id adjacent processor list id (the id go from 0 to getNNProcessor())
	 * \param j box (each near processor can produce more than one internal ghost box)
	 * \return sub-domain at which belong the internal ghost box
	 *
	 */
	inline size_t getProcessorIGhostSSub(size_t id, size_t j) const
	{
		return proc_int_box.get(id).ibx.get(j).r_sub;
	}
incardon's avatar
incardon committed
765 766 767 768 769 770 771 772

	/*! \brief Get the local sub-domain at witch belong the internal ghost box
	 *
	 * \param id adjacent processor list id (the id go from 0 to getNNProcessor())
	 * \param j box (each near processor can produce more than one internal ghost box)
	 * \return sub-domain at which belong the internal ghost box
	 *
	 */
incardon's avatar
incardon committed
773
	inline size_t getProcessorIGhostSub(size_t id, size_t j) const
incardon's avatar
incardon committed
774 775 776 777 778 779 780 781 782 783 784
	{
		return proc_int_box.get(id).ibx.get(j).sub;
	}

	/*! \brief Get the local sub-domain at witch belong the external ghost box
	 *
	 * \param id near processor list id (the id go from 0 to getNNProcessor())
	 * \param j box (each near processor can produce more than one external ghost box)
	 * \return sub-domain at which belong the external ghost box
	 *
	 */
incardon's avatar
incardon committed
785
	inline size_t getProcessorEGhostSub(size_t id, size_t j) const
incardon's avatar
incardon committed
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
	{
		return proc_int_box.get(id).ebx.get(j).sub;
	}

	/*! \brief Return the total number of the calculated internal ghost boxes
	 *
	 * \return the number of internal ghost boxes
	 *
	 */
	inline size_t getNIGhostBox() const
	{
		return vb_int.size();
	}

	/*! \brief Given the internal ghost box id, it return the internal ghost box
801 802
	 *
	 * \param b_id internal ghost box id
incardon's avatar
incardon committed
803 804 805 806
	 *
	 * \return the internal ghost box
	 *
	 */
incardon's avatar
incardon committed
807
	inline ::Box<dim,T> getIGhostBox(size_t b_id) const
incardon's avatar
incardon committed
808
	{
incardon's avatar
incardon committed
809
		return vb_int_box.get(b_id);
incardon's avatar
incardon committed
810 811 812 813 814
	}

	/*! \brief Given the internal ghost box id, it return the near processor at witch belong
	 *         or the near processor that produced this internal ghost box
	 *
incardon's avatar
incardon committed
815
	 * \param b_id internal ghost box id
816
	 *
incardon's avatar
incardon committed
817 818 819 820 821
	 * \return the processor id of the ghost box
	 *
	 */
	inline size_t getIGhostBoxProcessor(size_t b_id) const
	{
incardon's avatar
incardon committed
822
		return vb_int.template get<proc_>(b_id);
incardon's avatar
incardon committed
823 824 825 826 827 828 829 830 831 832 833 834 835
	}

	/*! \brief Get the number of the calculated external ghost boxes
	 *
	 * \return the number of external ghost boxes
	 *
	 */
	inline size_t getNEGhostBox() const
	{
		return vb_ext.size();
	}

	/*! \brief Given the external ghost box id, it return the external ghost box
836 837
	 *
	 * \param b_id external ghost box id
incardon's avatar
incardon committed
838 839 840 841 842 843 844 845 846 847 848 849
	 *
	 * \return the external ghost box
	 *
	 */
	inline ::Box<dim,T> getEGhostBox(size_t b_id) const
	{
		return vb_ext.get(b_id).box;
	}

	/*! \brief Given the external ghost box id, it return the near processor at witch belong
	 *         or the near processor that produced this external ghost box
	 *
850 851
	 * \param b_id external ghost box id
	 *
incardon's avatar
incardon committed
852 853 854 855 856 857 858 859 860 861 862
	 * \return the processor id of the external ghost box
	 *
	 */
	inline size_t getEGhostBoxProcessor(size_t b_id) const
	{
		return vb_ext.get(b_id).proc;
	}

	/*! /brief Given a point it return the set of boxes in which the point fall
	 *
	 * \param p Point to check
863
	 *
incardon's avatar
incardon committed
864 865 866
	 * \return An iterator with the id's of the internal boxes in which the point fall
	 *
	 */
867
	auto getInternalIDBoxes(Point<dim,T> & p) -> decltype(geo_cell.getCellIterator(geo_cell.getCell(p)))
incardon's avatar
incardon committed
868
	{
869
		return geo_cell.getCellIterator(geo_cell.getCell(p));
incardon's avatar
incardon committed
870 871 872 873 874 875 876 877 878
	}

	/*! \brief if the point fall into the ghost of some near processor it return the processors id's in which
	 *  it fall
	 *
	 * \param p Point
	 * \return iterator of the processors id's
	 *
	 */
879
	inline auto labelPoint(Point<dim,T> & p) -> decltype(geo_cell.getCellIterator(geo_cell.getCell(p)))
incardon's avatar
incardon committed
880
	{
881
		return geo_cell.getCellIterator(geo_cell.getCell(p));
incardon's avatar
incardon committed
882 883
	}

incardon's avatar
incardon committed
884 885 886 887 888 889 890 891 892 893 894 895
	/*! \brief Get the number of processor a particle must sent
	 *
	 * \param p position of the particle
	 *
	 */
	template<typename output_type> inline void ghost_processor_ID(const Point<dim,T> & p, output_type & output, unsigned int base, unsigned int pi)
	{
		ID_operation<output_type> op(output);

		ghost_processorID_general_impl(p,base,pi,geo_cell,vb_int_box,vb_int,op);
	}

incardon's avatar
incardon committed
896 897 898 899 900 901 902
	/*! \brief Get the number of processor a particle must sent
	 *
	 * \param p position of the particle
	 *
	 */
	inline unsigned int ghost_processorID_N(const Point<dim,T> & p)
	{
incardon's avatar
incardon committed
903
		return ghost_processorID_N_impl(p,geo_cell,vb_int_box,vb_int);
incardon's avatar
incardon committed
904 905
	}

906 907 908 909 910 911 912 913 914
	/*! \brief Given a position it return if the position belong to any neighborhood processor ghost
	 * (Internal ghost)
	 *
	 * if the particle come from an internal ghost from the periodicity of the domain, position must be shifted
	 * this function return the id of the shift vector
	 *
	 * \see getShiftVector
	 *
	 * \tparam id type of id to get box_id processor_id lc_processor_id shift_id
incardon's avatar
incardon committed
915
	 *
916 917 918 919
	 * \param p Particle position
	 * \param opt intersection boxes of the same processor can overlap, so in general the function
	 *        can produce more entry with the same processor, the UNIQUE option eliminate double entries
	 *        (UNIQUE) is for particle data (MULTIPLE) is for grid data [default MULTIPLE]
incardon's avatar
incardon committed
920
	 * \return return the processor ids (not the rank, the id in the near processor list)
921
	 *
922 923 924 925 926 927 928
	 */
	template <typename id1, typename id2> inline const openfpm::vector<std::pair<size_t,size_t>> ghost_processorID_pair(Point<dim,T> & p, const int opt = MULTIPLE)
	{
		ids_p.clear();

		// Check with geo-cell if a particle is inside one Cell containing boxes

929
		auto cell_it = geo_cell.getCellIterator(geo_cell.getCell(p));
930 931 932 933 934 935 936

		// For each element in the cell, check if the point is inside the box
		// if it is, store the processor id
		while (cell_it.isNext())
		{
			size_t bid = cell_it.get();

incardon's avatar
incardon committed
937
			if (Box<dim,T>(vb_int_box.get(bid)).isInsideNP(p) == true)
938 939 940 941 942 943 944 945 946
			{
				ids_p.add(std::pair<size_t,size_t>(id1::id(vb_int.get(bid),bid),id2::id(vb_int.get(bid),bid)));
			}

			++cell_it;
		}

		// Make the id unique
		if (opt == UNIQUE)
947 948
		{
			ids_p.sort();
949
			ids_p.unique();
950
		}
951 952 953

		return ids_p;
	}
incardon's avatar
incardon committed
954 955 956 957

	/*! \brief Given a position it return if the position belong to any neighborhood processor ghost
	 * (Internal ghost)
	 *
958 959 960 961 962 963
	 * if the particle come from an internal ghost from the periodicity of the domain, position must be shifted
	 * this function return the id of the shift vector
	 *
	 * \see getShiftVector
	 *
	 * \tparam id type of id to get box_id processor_id lc_processor_id shift_id
incardon's avatar
incardon committed
964 965 966 967 968
	 * \param p Particle position
	 * \param opt intersection boxes of the same processor can overlap, so in general the function
	 *        can produce more entry with the same processor, the UNIQUE option eliminate double entries
	 *        (UNIQUE) is for particle data (MULTIPLE) is for grid data [default MULTIPLE]
	 *
incardon's avatar
incardon committed
969
	 * \return the processor ids
970
	 *
incardon's avatar
incardon committed
971
	 */
incardon's avatar
incardon committed
972
	template <typename id> inline const openfpm::vector<size_t> ghost_processorID(const Point<dim,T> & p, const int opt = MULTIPLE)
incardon's avatar
incardon committed
973 974 975 976 977
	{
		ids.clear();

		// Check with geo-cell if a particle is inside one Cell containing boxes

978
		auto cell_it = geo_cell.getCellIterator(geo_cell.getCell(p));
incardon's avatar
incardon committed
979 980 981 982 983 984 985

		// For each element in the cell, check if the point is inside the box
		// if it is, store the processor id
		while (cell_it.isNext())
		{
			size_t bid = cell_it.get();

incardon's avatar
incardon committed
986
			if (Box<dim,T>(vb_int_box.get(bid)).isInsideNP(p) == true)
incardon's avatar
incardon committed
987 988 989 990 991 992 993 994 995
			{
				ids.add(id::id(vb_int.get(bid),bid));
			}

			++cell_it;
		}

		// Make the id unique
		if (opt == UNIQUE)
996 997 998 999
		{
			ids_p.sort();
			ids_p.unique();
		}
incardon's avatar
incardon committed
1000 1001 1002 1003

		return ids;
	}

1004 1005 1006
	/*! \brief Given a position it return if the position belong to any neighborhood processor ghost
	 * (Internal ghost)
	 *
1007 1008 1009
	 * \tparam id1 first index type to get box_id processor_id lc_processor_id
	 * \tparam id2 second index type to get box_id processor_id lc_processor_id
	 *
1010
	 * \param p Particle position
incardon's avatar
incardon committed
1011
	 * \param opt indicate if the entries in the vector must be unique
1012
	 *
1013
	 * \return a vector of pair containing the requested information
1014 1015
	 *
	 */
incardon's avatar
incardon committed
1016
	template<typename id1, typename id2, typename Mem> inline const openfpm::vector<std::pair<size_t,size_t>> & ghost_processorID_pair(const encapc<1,Point<dim,T>,Mem> & p, const int opt = MULTIPLE)
1017 1018 1019 1020 1021
	{
		ids_p.clear();

		// Check with geo-cell if a particle is inside one Cell containing boxes

1022
		auto cell_it = geo_cell.getCellIterator(geo_cell.getCell(p));
1023 1024 1025 1026 1027 1028 1029

		// For each element in the cell, check if the point is inside the box
		// if it is, store the processor id
		while (cell_it.isNext())
		{
			size_t bid = cell_it.get();

incardon's avatar
incardon committed
1030
			if (Box<dim,T>(vb_int_box.get(bid)).isInsideNP(p) == true)
1031 1032 1033 1034 1035 1036 1037 1038 1039
			{
				ids_p.add(std::pair<size_t,size_t>(id1::id(vb_int.get(bid),bid),id2::id(vb_int.get(bid),bid)));
			}

			++cell_it;
		}

		// Make the id unique
		if (opt == UNIQUE)
1040 1041
		{
			ids_p.sort();
1042
			ids_p.unique();
1043
		}
1044 1045 1046 1047

		return ids_p;
	}

incardon's avatar
incardon committed
1048 1049 1050 1051
	/*! \brief Given a position it return if the position belong to any neighborhood processor ghost
	 * (Internal ghost)
	 *
	 * \tparam id type of if to get box_id processor_id lc_processor_id
incardon's avatar
incardon committed
1052
	 *
incardon's avatar
incardon committed
1053
	 * \param p Particle position
incardon's avatar
incardon committed
1054
	 * \param opt it indicate if the entry in the vector must be unique or not
incardon's avatar
incardon committed
1055
	 *
incardon's avatar
incardon committed
1056
	 * \return the processor ids
incardon's avatar
incardon committed
1057 1058
	 *
	 */
incardon's avatar
incardon committed
1059
	template<typename id, typename Mem> inline const openfpm::vector<size_t> & ghost_processorID(const encapc<1,Point<dim,T>,Mem> & p, const int opt = MULTIPLE)
incardon's avatar
incardon committed
1060 1061 1062 1063 1064
	{
		ids.clear();

		// Check with geo-cell if a particle is inside one Cell containing boxes

1065
		auto cell_it = geo_cell.getCellIterator(geo_cell.getCell(p));
incardon's avatar
incardon committed
1066 1067 1068 1069 1070 1071 1072

		// For each element in the cell, check if the point is inside the box
		// if it is, store the processor id
		while (cell_it.isNext())
		{
			size_t bid = cell_it.get();

1073
			if (vb_int.get(bid).box.isInsideNP(p) == true)
incardon's avatar
incardon committed
1074 1075 1076 1077 1078 1079 1080 1081 1082
			{
				ids.add(id::id(vb_int.get(bid),bid));
			}

			++cell_it;
		}

		// Make the id unique
		if (opt == UNIQUE)
1083 1084 1085 1086
		{
			ids_p.sort();
			ids_p.unique();
		}
incardon's avatar
incardon committed
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098

		return ids;
	}

	/*! \brief write the information about the ghost in vtk format
	 *
	 * 1) internal_ghost_X.vtk Internal ghost boxes for the local processor (X)
	 * 2) external_ghost_X.vtk External ghost boxes for the local processor (X)
	 *
	 * \param output directory
	 * \param p_id processor rank
	 *
incardon's avatar
incardon committed
1099 1100 1101
	 *
	 * \return true if the write succeed
	 *
incardon's avatar
incardon committed
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
	 */
	bool write(std::string output, size_t p_id) const
	{
		//! internal_ghost_X.vtk Internal ghost boxes for the local processor (X)
		VTKWriter<openfpm::vector<::Box<dim,T>>,VECTOR_BOX> vtk_box3;
		for (size_t p = 0 ; p < box_nn_processor_int.size() ; p++)
		{
			for (size_t s = 0 ; s < box_nn_processor_int.get(p).size() ; s++)
			{
				vtk_box3.add(box_nn_processor_int.get(p).get(s).nbx);
			}
		}
		vtk_box3.write(output + std::string("internal_ghost_") + std::to_string(p_id) + std::string(".vtk"));

		//! external_ghost_X.vtk External ghost boxes for the local processor (X)
		VTKWriter<openfpm::vector<::Box<dim,T>>,VECTOR_BOX> vtk_box4;
		for (size_t p = 0 ; p < box_nn_processor_int.size() ; p++)
		{
			for (size_t s = 0 ; s < box_nn_processor_int.get(p).size() ; s++)
			{
				vtk_box4.add(box_nn_processor_int.get(p).get(s).bx);
			}
		}
		vtk_box4.write(output + std::string("external_ghost_") + std::to_string(p_id) + std::string(".vtk"));

		return true;
	}
incardon's avatar
incardon committed
1129

incardon's avatar
incardon committed
1130
	/*! \brief Check if the ie_ghosts contain the same information
incardon's avatar
incardon committed
1131
	 *
incardon's avatar
incardon committed
1132 1133 1134
	 * \param ig Element to check
	 *
	 * \return true if they are equal
incardon's avatar
incardon committed
1135 1136
	 *
	 */
incardon's avatar
incardon committed
1137
	bool is_equal(ie_ghost<dim,T,Memory,layout_base> & ig)
incardon's avatar
incardon committed
1138 1139 1140 1141 1142 1143 1144 1145
	{
		if (getNEGhostBox() != ig.getNEGhostBox())
			return false;

		if (getNIGhostBox() != ig.getNIGhostBox())
			return false;

		for (size_t i = 0 ; i < getNIGhostBox() ; i++)
incardon's avatar
incardon committed
1146 1147 1148 1149 1150 1151 1152 1153
		{
			if (getIGhostBox(i) != ig.getIGhostBox(i))
				return false;
			if (getIGhostBoxProcessor(i) != ig.getIGhostBoxProcessor(i))
				return false;
		}

		for (size_t i = 0 ; i < proc_int_box.size() ; i++)
incardon's avatar
incardon committed
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165
		{
			if (getProcessorNIGhost(i) != ig.getProcessorNIGhost(i))
				return false;
			for (size_t j = 0 ; j < getProcessorNIGhost(i) ; j++)
			{
				if (getProcessorIGhostBox(i,j) != ig.getProcessorIGhostBox(i,j))
					return false;
				if (getProcessorIGhostId(i,j) != ig.getProcessorIGhostId(i,j))
					return false;
				if (getProcessorIGhostSub(i,j) != ig.getProcessorIGhostSub(i,j))
					return false;
			}
incardon's avatar
incardon committed
1166 1167 1168 1169 1170
		}

		for (size_t i = 0 ; i < getNEGhostBox() ; i++)
		{
			if (getEGhostBox(i) != ig.getEGhostBox(i))
incardon's avatar
incardon committed
1171
				return false;
incardon's avatar
incardon committed
1172
			if (getEGhostBoxProcessor(i) != ig.getEGhostBoxProcessor(i))
incardon's avatar
incardon committed
1173 1174 1175
				return false;
		}

incardon's avatar
incardon committed
1176
		for (size_t i = 0 ; i < proc_int_box.size() ; i++)
incardon's avatar
incardon committed
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
		{
			if (getProcessorNEGhost(i) != ig.getProcessorNEGhost(i))
				return false;
			for (size_t j = 0 ; j < getProcessorNEGhost(i) ; j++)
			{
				if (getProcessorEGhostBox(i,j) != ig.getProcessorEGhostBox(i,j))
					return false;
				if (getProcessorEGhostId(i,j) != ig.getProcessorEGhostId(i,j))
					return false;
				if (getProcessorEGhostSub(i,j) != ig.getProcessorEGhostSub(i,j))
					return false;
			}
		}
Pietro Incardona's avatar
Pietro Incardona committed
1190 1191

		return true;
incardon's avatar
incardon committed
1192
	}
incardon's avatar
incardon committed
1193 1194 1195 1196

	/*! \brief Check if the ie_loc_ghosts contain the same information with the exception of the ghost part
	 * It is anyway required that the ghost come from the same sub-domains decomposition
	 *
incardon's avatar
incardon committed
1197 1198 1199
	 * \param ig Element to check
	 *
	 * \return true if they are equal
incardon's avatar
incardon committed
1200 1201
	 *
	 */
incardon's avatar
incardon committed
1202
	bool is_equal_ng(ie_ghost<dim,T,Memory,layout_base> & ig)
incardon's avatar
incardon committed
1203
	{
Pietro Incardona's avatar
Pietro Incardona committed
1204
		return true;
incardon's avatar
incardon committed
1205
	}
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215

	/*! \brief Reset the nn_prcs structure
	 *
	 */
	void reset()
	{
		box_nn_processor_int.clear();
		proc_int_box.clear();
		vb_ext.clear();
		vb_int.clear();
1216
		vb_int_box.clear();
1217 1218 1219 1220 1221
		geo_cell.clear();
		shifts.clear();
		ids_p.clear();
		ids.clear();
	}
incardon's avatar
incardon committed
1222 </