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

#ifndef SRC_DECOMPOSITION_GHOST_DEC_IE_GHOST_HPP_
#define SRC_DECOMPOSITION_GHOST_DEC_IE_GHOST_HPP_

#include "Space/Shape/Box.hpp"
#include "Space/Ghost.hpp"
#include "Space/SpaceBox.hpp"
#include "common.hpp"
15
#include "VTKWriter/VTKWriter.hpp"
16
#include "nn_processor.hpp"
incardon's avatar
incardon committed
17
18

/*! \brief structure that store and compute the internal and external local ghost box
19
20
21
 *
 * \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 ...
incardon's avatar
incardon committed
22
23
24
25
26
27
28
29
30
 *
 * \see CartDecomposition
 *
 */
template<unsigned int dim, typename T>
class ie_loc_ghost
{
	openfpm::vector<lBox_dom<dim,T>> loc_ghost_box;

31
32
	//! temporal added sub-domains
	openfpm::vector<Box_loc_sub<dim,T>> sub_domains_tmp;
incardon's avatar
incardon committed
33
34
35
36
37
38
39

	/*! \brief Create the external local ghost boxes
	 *
	 * \param ghost margin to enlarge
	 * \param local sub-domain
	 *
	 */
40
	void create_loc_ghost_ebox(Ghost<dim,T> & ghost, openfpm::vector<SpaceBox<dim,T>> & sub_domains, openfpm::vector<Box_loc_sub<dim,T>> & sub_domains_prc)
incardon's avatar
incardon committed
41
	{
42
43
44
		comb<dim> zero;
		zero.zero();

incardon's avatar
incardon committed
45
46
47
48
49
50
51
52
53
54
55
		loc_ghost_box.resize(sub_domains.size());

		// 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);

			// intersect with the other local sub-domains
56
			for (size_t j = 0 ; j < sub_domains_prc.size() ; j++)
incardon's avatar
incardon committed
57
			{
58
				size_t rj = sub_domains_prc.get(j).sub;
incardon's avatar
incardon committed
59

60
61
62
				if (rj == i && sub_domains_prc.get(j).cmb == zero)
					continue;

incardon's avatar
incardon committed
63
64
				::Box<dim,T> bi;

65
				bool intersect = sub_with_ghost.Intersect(sub_domains_prc.get(j).bx,bi);
incardon's avatar
incardon committed
66
67
68
69

				if (intersect == true)
				{
					Box_sub<dim,T> b;
70
					b.sub = rj;
incardon's avatar
incardon committed
71
72
73
74
75
76
					b = bi;

					// local external ghost box
					loc_ghost_box.get(i).ebx.add(b);

					// search this box in the internal box of the sub-domain j
77
					for (size_t k = 0; k < loc_ghost_box.get(rj).ibx.size() ; k++)
incardon's avatar
incardon committed
78
					{
79
						if (loc_ghost_box.get(rj).ibx.get(k).sub == i && loc_ghost_box.get(rj).ibx.get(k).cmb == sub_domains_prc.get(j).cmb.operator-())
incardon's avatar
incardon committed
80
						{
81
							loc_ghost_box.get(rj).ibx.get(k).k = loc_ghost_box.get(i).ebx.size()-1;
incardon's avatar
incardon committed
82
83
84
85
86
87
88
89
90
91
92
93
94
							break;
						}
					}
				}
			}
		}
	}

	/*! \brief Create the internal local ghost boxes
	 *
	 * \param ghost margin to enlarge
	 *
	 */
95
	void create_loc_ghost_ibox(Ghost<dim,T> & ghost, openfpm::vector<SpaceBox<dim,T>> & sub_domains, openfpm::vector<Box_loc_sub<dim,T>> & sub_domains_prc)
incardon's avatar
incardon committed
96
	{
97
98
99
		comb<dim> zero;
		zero.zero();

incardon's avatar
incardon committed
100
101
102
103
104
105
		loc_ghost_box.resize(sub_domains.size());

		// For each sub-domain
		for (size_t i = 0 ; i < sub_domains.size() ; i++)
		{
			// intersect with the others local sub-domains
106
			for (size_t j = 0 ; j < sub_domains_prc.size() ; j++)
incardon's avatar
incardon committed
107
			{
108
109
				SpaceBox<dim,T> sub_with_ghost = sub_domains_prc.get(j).bx;
				size_t rj = sub_domains_prc.get(j).sub;
110

111
				// Avoid to intersect the box with itself
112
113
114
				if (rj == i && sub_domains_prc.get(j).cmb == zero)
					continue;

incardon's avatar
incardon committed
115
116
117
118
119
120
121
122
123
124
				// enlarge the sub-domain with the ghost
				sub_with_ghost.enlarge(ghost);

				::Box<dim,T> bi;

				bool intersect = sub_with_ghost.Intersect(::SpaceBox<dim,T>(sub_domains.get(i)),bi);

				if (intersect == true)
				{
					Box_sub_k<dim,T> b;
125
					b.sub = rj;
incardon's avatar
incardon committed
126
127
					b = bi;
					b.k = -1;
128
					b.cmb = sub_domains_prc.get(j).cmb;
incardon's avatar
incardon committed
129
130
131
132
133
134
135

					loc_ghost_box.get(i).ibx.add(b);
				}
			}
		}
	}

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
	/*! \brief In case of periodic boundary conditions we have to add boxes
	 *         at the borders
	 *
	 * \param list of sub-domains
	 * \param domain Domain box
	 * \param boundary boundary conditions
	 * \param ghost ghost part
	 *
	 */
	void applyBC(openfpm::vector<Box_loc_sub<dim,T>> & sub_domains, const Box<dim,T> & domain, const Ghost<dim,T> & ghost, const size_t (&bc)[dim])
	{
		HyperCube<dim> hyp;

		// first we create boxes at the border of the domain used to detect the sub-domain
		// that must be adjusted, each of this boxes define a shift in case of periodic boundary condition
		for (long int i = dim-1 ; i >= 0 ; i--)
		{
			std::vector<comb<dim>> cmbs = hyp.getCombinations_R(i);

			for (size_t j = 0 ; j < cmbs.size() ; j++)
			{
				if (nn_prcs<dim,T>::check_valid(cmbs[j],bc) == false)
					continue;

				Box<dim,T> bp;
				Point<dim,T> shift;

				for (size_t k = 0 ; k < dim ; k++)
				{
					switch (cmbs[j][k])
					{
					case 1:
						bp.setLow(k,domain.getHigh(k)+ghost.getLow(k));
						bp.setHigh(k,domain.getHigh(k));
						shift.get(k) = -domain.getHigh(k);
						break;
					case 0:
						bp.setLow(k,domain.getLow(k));
						bp.setHigh(k,domain.getHigh(k));
						shift.get(k) = 0;
						break;
					case -1:
						bp.setLow(k,domain.getLow(k));
						bp.setHigh(k,ghost.getHigh(k));
						shift.get(k) = domain.getHigh(k);
						break;
					}
				}

				// Detect all the sub-domain involved, shift them and add to the list
				// Detection is performed intersecting the sub-domains with the ghost
				// parts near the domain borders
				for (size_t k = 0 ; k < sub_domains.size() ; k++)
				{
					Box<dim,T> sub = sub_domains.get(k).bx;
					Box<dim,T> b_int;

					if (sub.Intersect(bp,b_int) == true)
					{
						sub += shift;
						add_subdomain(Box_loc_sub<dim,T>(sub,k,cmbs[j]));
					}
				}
			}
		}

		flush(sub_domains);
	}



	/*! \brief add sub-domains to a temporal list
	 *
	 * \param bx Box to add
	 *
	 */
	inline void add_subdomain(const Box_loc_sub<dim,T> & bx)
	{
		sub_domains_tmp.add(bx);
	}

	/*! \brief Flush the temporal added sub-domain to the sub-domain list
	 *
	 *
	 */
	void flush(openfpm::vector<Box_loc_sub<dim,T>> & sub_domains)
	{
		for (size_t i = 0 ; i < sub_domains_tmp.size() ; i++)
		{
			sub_domains.add(sub_domains_tmp.get(i));
		}

		sub_domains_tmp.clear();
	}

incardon's avatar
incardon committed
231
232
public:

233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
	/*! \brief Create external and internal local ghosts
	 *
	 * \param ghost boundary
	 * \param sub_domain
	 * \param bc Boundary conditions
	 *
	 */
	void create(openfpm::vector<SpaceBox<dim,T>> & sub_domains, Box<dim,T> & domain , Ghost<dim,T> & ghost , const size_t (&bc)[dim] )
	{
		// It will store local sub-domains + borders
		openfpm::vector<Box_loc_sub<dim,T>> sub_domains_prc;

		comb<dim> zero;
		zero.zero();

		// Copy sub_domains into sub_domains_prc
		for (size_t i = 0 ; i < sub_domains.size() ; i++)
		{
			Box_loc_sub<dim,T> bls(SpaceBox<dim,T>(sub_domains.get(i)),i,zero);
			sub_domains_prc.add(bls);
			sub_domains_prc.last().sub = i;
		}

		applyBC(sub_domains_prc,domain,ghost,bc);

		create_loc_ghost_ibox(ghost,sub_domains,sub_domains_prc);
		create_loc_ghost_ebox(ghost,sub_domains,sub_domains_prc);
	}

262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
	//! Default constructor
	ie_loc_ghost()	{};

	//! Constructor from another ie_loc_ghost
	ie_loc_ghost(const ie_loc_ghost<dim,T> & ilg)
	{
		this->operator=(ilg);
	};

	//! Constructor from temporal ie_loc_ghost
	ie_loc_ghost(ie_loc_ghost<dim,T> && ilg)
	{
		this->operator=(ilg);
	}

	/*! \brief copy the ie_loc_ghost
	 *
	 * \param ilg object to copy
	 *
	 */
	ie_loc_ghost<dim,T> & operator=(const ie_loc_ghost<dim,T> & ilg)
	{
		loc_ghost_box = ilg.loc_ghost_box;
		return *this;
	}

	/*! \brief copy the ie_loc_ghost
	 *
	 * \param ilg object to copy
	 *
	 */
	ie_loc_ghost<dim,T> & operator=(ie_loc_ghost<dim,T> && ilg)
	{
		loc_ghost_box.swap(ilg.loc_ghost_box);
		return *this;
	}

299
300
301
302
303
304
305
306
307
308
	/*! \brief Get the number of local sub-domains
	 *
	 * \return the number of local sub-domains
	 *
	 */
	inline size_t getNLocalSub()
	{
		return loc_ghost_box.size();
	}

incardon's avatar
incardon committed
309
310
311
312
	/*! \brief Get the number of external local ghost box for each sub-domain
	 *
	 * \param id sub-domain id
	 *
incardon's avatar
incardon committed
313
	 * \return the number of external ghost box
incardon's avatar
incardon committed
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
	 *
	 */
	inline size_t getLocalNEGhost(size_t id)
	{
		return loc_ghost_box.get(id).ebx.size();
	}

	/*! \brief Get the number of internal local ghost box for each sub-domain
	 *
	 * \param id sub-domain id
	 *
	 * \return the number of external ghost box
	 *
	 */
	inline size_t getLocalNIGhost(size_t id)
	{
		return loc_ghost_box.get(id).ibx.size();
	}

	/*! \brief For the sub-domain i intersected with the sub-domain j enlarged, the associated
	 *       external ghost box is located in getLocalIGhostBox(j,k) with
	 *       getLocalIGhostSub(j,k) == i, this function return k
	 *
	 *
	 */
	inline size_t getLocalIGhostE(size_t i, size_t j)
	{
		return loc_ghost_box.get(i).ibx.get(j).k;
	}

	/*! \brief Get the j internal local ghost box for the i sub-domain of the local processor
	 *
	 * \note For the sub-domain i intersected with the sub-domain j enlarged, the associated
	 *       external ghost box is located in getLocalIGhostBox(j,k) with
	 *       getLocalIGhostSub(j,k) == i
	 *
	 * To get k use getLocalIGhostE
	 *
	 * \see getLocalIGhostE
	 *
	 * \param i sub-domain
	 * \param j box
	 * \return the box
	 *
	 */
	inline const ::Box<dim,T> & getLocalIGhostBox(size_t i, size_t j) const
	{
361
		return loc_ghost_box.get(i).ibx.get(j).bx;
incardon's avatar
incardon committed
362
363
	}

364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
	/*! \brief Get the j internal local ghost box boundary position for the i sub-domain of the local processor
	 *
	 * \note For the sub-domain i intersected with the sub-domain j enlarged, the associated
	 *       external ghost box is located in getLocalIGhostBox(j,k) with
	 *       getLocalIGhostSub(j,k) == i
	 *
	 * To get k use getLocalIGhostE
	 *
	 * \see getLocalIGhostE
	 *
	 * Some of the intersection boxes has special position, because they are at the boundary, this function
	 * return their position at the border
	 *
		\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     |
			|         |                        |         |
		 	|         |                        |         |
			|         |                        |         |
			|         |                        |         |
			|         |                        |         |
			|         |                        |         |
			+--------------------------------------------+
			| (-1,-1) |                        | (-1,1)  |
			|    |    |   (-1,0) --> 1         |    |    |
			|    v    |                        |    v    |
			|    0    |                        |    2    |
			+---------+------------------------+---------+


		\endverbatim
	 *
	 * \param i sub-domain
	 * \param j box
	 * \return the box
	 *
	 */
	inline const comb<dim> & getLocalIGhostPos(size_t i, size_t j) const
	{
		return loc_ghost_box.get(i).ibx.get(j).cmb;
	}

incardon's avatar
incardon committed
419
420
421
422
423
424
425
426
427
	/*! \brief Get the j external local ghost box for the local processor
	 *
	 * \param i sub-domain
	 * \param j box
	 * \return the box
	 *
	 */
	inline const ::Box<dim,T> & getLocalEGhostBox(size_t i, size_t j) const
	{
428
		return loc_ghost_box.get(i).ebx.get(j).bx;
incardon's avatar
incardon committed
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
	}

	/*! \brief Considering that sub-domain has N internal local ghost box identified
	 *         with the 0 <= k < N that come from the intersection of 2 sub-domains i and j
	 *         where j is enlarged, given the sub-domain i and the id k, it return the id of
	 *         the other sub-domain that produced the intersection
	 *
	 * \param i sub-domain
	 * \param k id
	 * \return the box
	 *
	 */
	inline size_t getLocalIGhostSub(size_t i, size_t j) const
	{
		return loc_ghost_box.get(i).ibx.get(j).sub;
	}

	/*! \brief Considering that sub-domain has N external local ghost box identified
	 *         with the 0 <= k < N that come from the intersection of 2 sub-domains i and j
	 *         where j is enlarged, given the sub-domain i and the id k, it return the id of
	 *         the other sub-domain that produced the intersection
	 *
	 * \param i sub-domain
	 * \param k id
	 * \return the box
	 *
	 */
	inline size_t getLocalEGhostSub(size_t i, size_t j) const
	{
		return loc_ghost_box.get(i).ebx.get(j).sub;
	}

	/*! \brief Write the decomposition as VTK file
	 *
	 * The function generate several files
	 *
	 * 5) local_internal_ghost_X.vtk internal local ghost boxes for the local processor (X)
	 * 6) local_external_ghost_X.vtk external local ghost boxes for the local processor (X)
	 *
	 * where X is the local processor rank
	 *
	 * \param output directory where to write the files
	 * \param p_id id of the local processor
	 *
	 */
	bool write(std::string output, size_t p_id) const
	{
476
477
478
		// Copy the Box_sub_k into a vector of boxes
		openfpm::vector<openfpm::vector<Box<dim,T>>> vv5;

incardon's avatar
incardon committed
479
480
		for (size_t p = 0 ; p < loc_ghost_box.size() ; p++)
		{
481
482
483
484
485
486
487
488
489
490
			vv5.add();
			for (size_t i = 0 ; i < loc_ghost_box.get(p).ibx.size() ; i++)
				vv5.last().add(loc_ghost_box.get(p).ibx.get(i).bx);
		}

		//! local_internal_ghost_X.vtk internal local ghost boxes for the local processor (X)
		VTKWriter<openfpm::vector<Box<dim,T>>,VECTOR_BOX> vtk_box5;
		for (size_t p = 0 ; p < vv5.size() ; p++)
		{
			vtk_box5.add(vv5.get(p));
incardon's avatar
incardon committed
491
492
493
		}
		vtk_box5.write(output + std::string("local_internal_ghost_") + std::to_string(p_id) + std::string(".vtk"));

494
495
496
		// Copy the Box_sub_k into a vector of boxes
		openfpm::vector<openfpm::vector<Box<dim,T>>> vv6;

incardon's avatar
incardon committed
497
498
		for (size_t p = 0 ; p < loc_ghost_box.size() ; p++)
		{
499
500
501
502
503
504
505
506
507
508
			vv6.add();
			for (size_t i = 0 ; i < loc_ghost_box.get(p).ebx.size() ; i++)
				vv6.last().add(loc_ghost_box.get(p).ebx.get(i).bx);
		}

		//! local_external_ghost_X.vtk external local ghost boxes for the local processor (X)
		VTKWriter<openfpm::vector<Box<dim,T>>,VECTOR_BOX> vtk_box6;
		for (size_t p = 0 ; p < vv6.size() ; p++)
		{
			vtk_box6.add(vv6.get(p));
incardon's avatar
incardon committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
		}
		vtk_box6.write(output + std::string("local_external_ghost_") + std::to_string(p_id) + std::string(".vtk"));

		return true;
	}

	/*! \brief function to check the consistency of the information of the decomposition
	 *
	 * \param n_sub Number of sub_domain
	 *
	 * \return false if is inconsistent
	 *
	 */
	bool check_consistency(size_t n_sub)
	{
		//! for each sub-domain
		for (size_t i = 0 ; i < loc_ghost_box.size() ; i++)
		{
			for (size_t j = 0 ; j < loc_ghost_box.get(i).ibx.size() ; j++)
			{
				if (loc_ghost_box.get(i).ibx.get(j).k == -1)
					return false;
			}
		}

		if (n_sub > 1)
		{
			for (size_t i = 0 ; i < loc_ghost_box.size() ; i++)
			{
				if (loc_ghost_box.get(i).ibx.size() == 0)
					return false;
				if (loc_ghost_box.get(i).ebx.size() == 0)
					return false;
			}
		}

		return true;
	}
incardon's avatar
incardon committed
547
548
549
550
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
576
577
578
579
580
581
582
583
584
585
586
587

	/*! \brief Check if the ie_loc_ghosts contain the same information
	 *
	 * \param ele Element to check
	 *
	 */
	bool is_equal(ie_loc_ghost<dim,T> & ilg)
	{
		if (ilg.loc_ghost_box.size() != loc_ghost_box.size())
			return false;

		// Explore all the subdomains
		for (size_t i = 0 ; i < loc_ghost_box.size() ; i++)
		{
			if (getLocalNIGhost(i) != ilg.getLocalNIGhost(i))
				return false;

			if (getLocalNEGhost(i) != ilg.getLocalNEGhost(i))
				return false;

			for (size_t j = 0 ; j < getLocalNIGhost(i) ; j++)
			{
				if (getLocalIGhostE(i,j) != ilg.getLocalIGhostE(i,j))
					return false;
				if (getLocalIGhostBox(i,j) != ilg.getLocalIGhostBox(i,j))
					return false;
				if (getLocalIGhostSub(i,j) != ilg.getLocalIGhostSub(i,j))
					return false;
			}
			for (size_t j = 0 ; j < getLocalNEGhost(i) ; j++)
			{
				if (getLocalEGhostBox(i,j) != ilg.getLocalEGhostBox(i,j))
					return false;
				if (getLocalEGhostSub(i,j) != ilg.getLocalEGhostSub(i,j))
					return false;
			}

		}

		return true;
	}
incardon's avatar
incardon committed
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



	/*! \brief Check if the ie_loc_ghosts contain the same information
	 * with the exception of the ghost part
	 *
	 * \param ele Element to check
	 *
	 */
	bool is_equal_ng(ie_loc_ghost<dim,T> & ilg)
	{
		Box<dim,T> bt;

		if (ilg.loc_ghost_box.size() != loc_ghost_box.size())
			return false;

		// Explore all the subdomains
		for (size_t i = 0 ; i < loc_ghost_box.size() ; i++)
		{
			if (getLocalNIGhost(i) != ilg.getLocalNIGhost(i))
				return false;

			if (getLocalNEGhost(i) != ilg.getLocalNEGhost(i))
				return false;

			for (size_t j = 0 ; j < getLocalNIGhost(i) ; j++)
			{
				if (getLocalIGhostE(i,j) != ilg.getLocalIGhostE(i,j))
					return false;
				if (getLocalIGhostBox(i,j).Intersect(ilg.getLocalIGhostBox(i,j),bt) == false)
					return false;
				if (getLocalIGhostSub(i,j) != ilg.getLocalIGhostSub(i,j))
					return false;
			}
			for (size_t j = 0 ; j < getLocalNEGhost(i) ; j++)
			{
				if (getLocalEGhostBox(i,j).Intersect(ilg.getLocalEGhostBox(i,j),bt) == false)
					return false;
				if (getLocalEGhostSub(i,j) != ilg.getLocalEGhostSub(i,j))
					return false;
			}

		}

		return true;
	}
incardon's avatar
incardon committed
634
635
636
637
};


#endif /* SRC_DECOMPOSITION_GHOST_DEC_IE_GHOST_HPP_ */