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

#ifndef VECTOR_DIST_UNIT_TEST_HPP_
#define VECTOR_DIST_UNIT_TEST_HPP_

incardon's avatar
incardon committed
11
#include <random>
incardon's avatar
incardon committed
12
#include "Vector/vector_dist.hpp"
Yaroslav's avatar
Yaroslav committed
13
#include "data_type/aggregate.hpp"
incardon's avatar
incardon committed
14
#include "Vector/performance/vector_dist_performance_common.hpp"
incardon's avatar
incardon committed
15

16 17 18 19 20 21 22 23 24 25 26 27
/*! \brief Count the total number of particles
 *
 * \param vd distributed vector
 * \param bc boundary conditions
 *
 */
template<unsigned int dim> size_t total_n_part_lc(vector_dist<dim,float, Point_test<float>, CartDecomposition<dim,float> > & vd, size_t (& bc)[dim])
{
	Vcluster & v_cl = vd.getVC();
	auto it2 = vd.getDomainIterator();
	const CartDecomposition<3,float> & ct = vd.getDecomposition();

28 29
	bool noOut = true;

30 31 32 33 34
	size_t cnt = 0;
	while (it2.isNext())
	{
		auto key = it2.get();

Pietro Incardona's avatar
Pietro Incardona committed
35
		noOut &= ct.isLocal(vd.getPos(key));
36 37 38 39 40 41

		cnt++;

		++it2;
	}

42 43
	BOOST_REQUIRE_EQUAL(noOut,true);

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
	//
	v_cl.sum(cnt);
	v_cl.execute();

	return cnt;
}

/*! \brief Count local and non local
 *
 * \param vd distributed vector
 * \param it iterator
 * \param bc boundary conditions
 * \param box domain box
 * \param dom_ext domain + ghost box
 * \param l_cnt local particles counter
 * \param nl_cnt non local particles counter
 * \param n_out out of domain + ghost particles counter
 *
 */
Pietro Incardona's avatar
Pietro Incardona committed
63
template<unsigned int dim,typename vector_dist> inline void count_local_n_local(vector_dist & vd, vector_dist_iterator & it, size_t (& bc)[dim] , Box<dim,float> & box, Box<dim,float> & dom_ext, size_t & l_cnt, size_t & nl_cnt, size_t & n_out)
64 65 66 67 68 69 70
{
	const CartDecomposition<dim,float> & ct = vd.getDecomposition();

	while (it.isNext())
	{
		auto key = it.get();
		// Check if it is in the domain
Pietro Incardona's avatar
Pietro Incardona committed
71
		if (box.isInsideNP(vd.getPos(key)) == true)
72 73
		{
			// Check if local
Pietro Incardona's avatar
Pietro Incardona committed
74
			if (ct.isLocalBC(vd.getPos(key),bc) == true)
75 76 77 78 79 80 81 82 83
				l_cnt++;
			else
				nl_cnt++;
		}
		else
		{
			nl_cnt++;
		}

incardon's avatar
incardon committed
84 85
		Point<dim,float> xp = vd.getPos(key);

86
		// Check that all particles are inside the Domain + Ghost part
incardon's avatar
incardon committed
87
		if (dom_ext.isInside(xp) == false)
88 89 90 91 92 93
				n_out++;

		++it;
	}
}

incardon's avatar
incardon committed
94 95
BOOST_AUTO_TEST_SUITE( vector_dist_test )

96 97
void print_test(std::string test, size_t sz)
{
98
	if (create_vcluster().getProcessUnitID() == 0)
99 100 101
		std::cout << test << " " << sz << "\n";
}

102
void Test2D_ghost(Box<2,float> & box)
103 104
{
	// Communication object
105
	Vcluster & v_cl = create_vcluster();
106 107 108

	typedef Point_test<float> p;

109
	// Get the default minimum number of sub-sub-domain per processor (granularity of the decomposition)
incardon's avatar
incardon committed
110
	size_t n_sub = 64 * v_cl.getProcessingUnits();
111 112
	// Convert the request of having a minimum n_sub number of sub-sub domain into grid decompsition of the space
	size_t sz = CartDecomposition<2,float>::getDefaultGrid(n_sub);
incardon's avatar
incardon committed
113

114 115
	//! [Create a vector of elements distributed on a grid like way]

116
	size_t g_div[]= {sz,sz};
117

118 119 120
	// number of particles
	size_t np = sz * sz;

121
	// Calculate the number of elements this processor is going to obtain
122 123 124 125 126 127 128 129
	size_t p_np = np / v_cl.getProcessingUnits();

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

	// Get the offset
	size_t offset = v_cl.getProcessUnitID() * p_np + std::min(v_cl.getProcessUnitID(),r);

130
	// Distribute the remain elements
131 132
	if (v_cl.getProcessUnitID() < r)
		p_np++;
133 134 135 136 137

	// Create a grid info
	grid_sm<2,void> g_info(g_div);

	// Calculate the grid spacing
138
	Point<2,float> spacing = box.getP2() - box.getP1();
139 140 141
	spacing = spacing / g_div;

	// middle spacing
Pietro Incardona's avatar
Pietro Incardona committed
142
	Point<2,float> m_spacing = spacing / 2.0;
143

incardon's avatar
incardon committed
144 145 146
	// set the ghost based on the radius cut off (make just a little bit smaller than the spacing)
	Ghost<2,float> g(spacing.get(0) - spacing .get(0) * 0.0001);

147 148 149
	// Boundary conditions
	size_t bc[2]={NON_PERIODIC,NON_PERIODIC};

150
	// Vector of particles
151
	vector_dist<2,float, Point_test<float> > vd(g_info.size(),box,bc,g);
152 153 154

	// size_t
	size_t cobj = 0;
155

156 157
	grid_key_dx_iterator_sp<2> it(g_info,offset,offset+p_np-1);
	auto v_it = vd.getIterator();
158

159
	while (v_it.isNext() && it.isNext())
160
	{
161 162
		auto key = it.get();
		auto key_v = v_it.get();
163 164 165

		// set the particle position

166 167
		vd.getPos(key_v)[0] = key.get(0) * spacing[0] + m_spacing[0] + box.getLow(0);
		vd.getPos(key_v)[1] = key.get(1) * spacing[1] + m_spacing[1] + box.getLow(1);
168

169 170 171
		cobj++;

		++v_it;
172 173 174
		++it;
	}

175 176 177
	//! [Create a vector of elements distributed on a grid like way]

	// Both iterators must signal the end, and the number of elements in the vector, must the equal to the
178 179 180 181 182
	// predicted one
	BOOST_REQUIRE_EQUAL(v_it.isNext(),false);
	BOOST_REQUIRE_EQUAL(it.isNext(),false);
	BOOST_REQUIRE_EQUAL(cobj,p_np);

183 184
	//! [Redistribute the particles and sync the ghost properties]

185 186 187
	// redistribute the particles according to the decomposition
	vd.map();

188
	auto v_it2 = vd.getIterator();
189

190
	while (v_it2.isNext())
191
	{
192
		auto key = v_it2.get();
193 194

		// fill with the processor ID where these particle live
incardon's avatar
incardon committed
195
		vd.getProp<p::s>(key) = vd.getPos(key)[0] + vd.getPos(key)[1] * 16.0f;
196 197 198
		vd.getProp<p::v>(key)[0] = v_cl.getProcessUnitID();
		vd.getProp<p::v>(key)[1] = v_cl.getProcessUnitID();
		vd.getProp<p::v>(key)[2] = v_cl.getProcessUnitID();
199

200
		++v_it2;
incardon's avatar
incardon committed
201
	}
incardon's avatar
incardon committed
202 203

	// do a ghost get
204
	vd.ghost_get<p::s,p::v>();
205

206
	//! [Redistribute the particles and sync the ghost properties]
incardon's avatar
incardon committed
207

208 209 210 211
	// Get the decomposition
	const auto & dec = vd.getDecomposition();

	// Get the ghost external boxes
212
	openfpm::vector<size_t> vb(dec.getNEGhostBox());
213 214 215 216

	// Get the ghost iterator
	auto g_it = vd.getGhostIterator();

217 218
	size_t n_part = 0;

219 220 221 222 223 224
	// Check if the ghost particles contain the correct information
	while (g_it.isNext())
	{
		auto key = g_it.get();

		// Check the received data
incardon's avatar
incardon committed
225
		BOOST_REQUIRE_EQUAL(vd.getPos(key)[0] + vd.getPos(key)[1] * 16.0f,vd.getProp<p::s>(key));
226 227 228

		bool is_in = false;
		size_t b = 0;
229
		size_t lb = 0;
230

231
		// check if the received data are in one of the ghost boxes
232
		for ( ; b < dec.getNEGhostBox() ; b++)
233
		{
incardon's avatar
incardon committed
234 235 236
			Point<2,float> xp = vd.getPos(key);

			if (dec.getEGhostBox(b).isInside(xp) == true )
237 238 239 240 241 242 243
			{
				is_in = true;

				// Add
				vb.get(b)++;
				lb = b;
			}
244 245 246 247
		}
		BOOST_REQUIRE_EQUAL(is_in,true);

		// Check that the particle come from the correct processor
248
		BOOST_REQUIRE_EQUAL(vd.getProp<p::v>(key)[0],dec.getEGhostBoxProcessor(lb));
249

250
		n_part++;
251 252 253
		++g_it;
	}

254 255 256 257
	if (v_cl.getProcessingUnits() > 1)
	{
		BOOST_REQUIRE(n_part != 0);
	}
258

259
    CellDecomposer_sm<2,float,shift<2,float>> cd(SpaceBox<2,float>(box),g_div,0);
260 261 262 263

	for (size_t i = 0 ; i < vb.size() ; i++)
	{
		// Calculate how many particle should be in the box
264
		size_t n_point = cd.getGridPoints(dec.getEGhostBox(i)).getVolumeKey();
265

266
		BOOST_REQUIRE_EQUAL(n_point,vb.get(i));
267
	}
incardon's avatar
incardon committed
268 269
}

270 271 272 273 274 275 276 277 278
BOOST_AUTO_TEST_CASE( vector_dist_ghost )
{
	Box<2,float> box({0.0,0.0},{1.0,1.0});
	Test2D_ghost(box);

	Box<2,float> box2({-1.0,-1.0},{2.5,2.5});
	Test2D_ghost(box2);
}

279 280
void print_test_v(std::string test, size_t sz)
{
281
	if (create_vcluster().getProcessUnitID() == 0)
282 283 284
		std::cout << test << " " << sz << "\n";
}

incardon's avatar
incardon committed
285 286 287 288 289 290 291 292 293 294 295 296 297 298
long int decrement(long int k, long int step)
{
	if (k <= 32)
	{
		return 1;
	}
	else if (k - 2*step+1 <= 0)
	{
		return k - 32;
	}
	else
		return step;
}

299
BOOST_AUTO_TEST_CASE( vector_dist_iterator_test_use_2d )
incardon's avatar
incardon committed
300
{
301
	Vcluster & v_cl = create_vcluster();
incardon's avatar
incardon committed
302 303 304 305 306 307 308

    // set the seed
	// create the random generator engine
	std::srand(v_cl.getProcessUnitID());
    std::default_random_engine eg;
    std::uniform_real_distribution<float> ud(0.0f, 1.0f);

309
    long int k = 524288 * v_cl.getProcessingUnits();
incardon's avatar
incardon committed
310

311
	long int big_step = k / 4;
312
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
313

incardon's avatar
incardon committed
314 315
	print_test_v( "Testing 2D vector k<=",k);

316
	// 2D test
incardon's avatar
incardon committed
317
	for ( ; k >= 2 ; k-= decrement(k,big_step) )
incardon's avatar
incardon committed
318
	{
319
		BOOST_TEST_CHECKPOINT( "Testing 2D vector k=" << k );
320 321 322

		//! [Create a vector of random elements on each processor 2D]

323
		Box<2,float> box({0.0,0.0},{1.0,1.0});
324 325 326 327

		// Boundary conditions
		size_t bc[2]={NON_PERIODIC,NON_PERIODIC};

328
		vector_dist<2,float, Point_test<float> > vd(k,box,bc,Ghost<2,float>(0.0));
incardon's avatar
incardon committed
329

330
		auto it = vd.getIterator();
incardon's avatar
incardon committed
331

332 333 334 335
		while (it.isNext())
		{
			auto key = it.get();

Pietro Incardona's avatar
Pietro Incardona committed
336 337
			vd.getPos(key)[0] = ud(eg);
			vd.getPos(key)[1] = ud(eg);
338 339 340 341 342 343

			++it;
		}

		vd.map();

344 345
		//! [Create a vector of random elements on each processor 2D]

346 347 348
		// Check if we have all the local particles
		size_t cnt = 0;
		const CartDecomposition<2,float> & ct = vd.getDecomposition();
349
		auto it2 = vd.getIterator();
350

351
		while (it2.isNext())
352
		{
353
			auto key = it2.get();
354 355

			// Check if local
Pietro Incardona's avatar
Pietro Incardona committed
356
			BOOST_REQUIRE_EQUAL(ct.isLocal(vd.getPos(key)),true);
357 358 359

			cnt++;

360
			++it2;
361 362 363 364 365
		}

		//
		v_cl.sum(cnt);
		v_cl.execute();
Pietro Incardona's avatar
Pietro Incardona committed
366
		BOOST_REQUIRE_EQUAL((long int)cnt,k);
incardon's avatar
incardon committed
367
	}
368
}
incardon's avatar
incardon committed
369

370 371
BOOST_AUTO_TEST_CASE( vector_dist_iterator_test_use_3d )
{
372
	Vcluster & v_cl = create_vcluster();
incardon's avatar
incardon committed
373

374 375 376 377 378 379
    // set the seed
	// create the random generator engine
	std::srand(v_cl.getProcessUnitID());
    std::default_random_engine eg;
    std::uniform_real_distribution<float> ud(0.0f, 1.0f);

380
    long int k = 524288 * v_cl.getProcessingUnits();
381

382
	long int big_step = k / 4;
383 384
	big_step = (big_step == 0)?1:big_step;

incardon's avatar
incardon committed
385 386
	print_test_v( "Testing 3D vector k<=",k);

387
	// 3D test
incardon's avatar
incardon committed
388
	for ( ; k >= 2 ; k-= decrement(k,big_step) )
incardon's avatar
incardon committed
389
	{
390
		BOOST_TEST_CHECKPOINT( "Testing 3D vector k=" << k );
391 392 393

		//! [Create a vector of random elements on each processor 3D]

394
		Box<3,float> box({0.0,0.0,0.0},{1.0,1.0,1.0});
395 396 397 398

		// Boundary conditions
		size_t bc[3]={NON_PERIODIC,NON_PERIODIC,NON_PERIODIC};

399
		vector_dist<3,float, Point_test<float> > vd(k,box,bc,Ghost<3,float>(0.0));
incardon's avatar
incardon committed
400

401
		auto it = vd.getIterator();
incardon's avatar
incardon committed
402

403 404 405
		while (it.isNext())
		{
			auto key = it.get();
incardon's avatar
incardon committed
406

Pietro Incardona's avatar
Pietro Incardona committed
407 408 409
			vd.getPos(key)[0] = ud(eg);
			vd.getPos(key)[1] = ud(eg);
			vd.getPos(key)[2] = ud(eg);
410 411 412 413 414 415

			++it;
		}

		vd.map();

416 417
		//! [Create a vector of random elements on each processor 3D]

418 419 420
		// Check if we have all the local particles
		size_t cnt = 0;
		const CartDecomposition<3,float> & ct = vd.getDecomposition();
421
		auto it2 = vd.getIterator();
422

423
		while (it2.isNext())
424
		{
425
			auto key = it2.get();
426 427

			// Check if local
Pietro Incardona's avatar
Pietro Incardona committed
428
			BOOST_REQUIRE_EQUAL(ct.isLocal(vd.getPos(key)),true);
429 430 431

			cnt++;

432
			++it2;
433 434 435 436 437
		}

		//
		v_cl.sum(cnt);
		v_cl.execute();
Pietro Incardona's avatar
Pietro Incardona committed
438
		BOOST_REQUIRE_EQUAL(cnt,(size_t)k);
439
	}
incardon's avatar
incardon committed
440 441
}

Pietro Incardona's avatar
Pietro Incardona committed
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 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 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515

BOOST_AUTO_TEST_CASE( vector_dist_iterator_fixed_dec_3d )
{
	Vcluster & v_cl = create_vcluster();

    // set the seed
	// create the random generator engine
	std::srand(v_cl.getProcessUnitID());
    std::default_random_engine eg;
    std::uniform_real_distribution<float> ud(0.0f, 1.0f);

    long int k = 52428 * v_cl.getProcessingUnits();

	long int big_step = k / 4;
	big_step = (big_step == 0)?1:big_step;

	print_test_v( "Testing 3D vector copy decomposition k<=",k);

	// 3D test
	for ( ; k >= 2 ; k-= decrement(k,big_step) )
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D vector copy decomposition k=" << k );

		Box<3,float> box({0.0,0.0,0.0},{1.0,1.0,1.0});

		// Boundary conditions
		size_t bc[3]={NON_PERIODIC,NON_PERIODIC,NON_PERIODIC};

		vector_dist<3,float, aggregate<double,double> > vd(k,box,bc,Ghost<3,float>(0.05));
		vector_dist<3,float, aggregate<double,double> > vd2(vd.getDecomposition(),k);

		auto it = vd.getIterator();

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

			vd.getPos(key)[0] = ud(eg);
			vd.getPos(key)[1] = ud(eg);
			vd.getPos(key)[2] = ud(eg);

			vd2.getPos(key)[0] = vd.getPos(key)[0];
			vd2.getPos(key)[1] = vd.getPos(key)[1];
			vd2.getPos(key)[2] = vd.getPos(key)[2];

			++it;
		}

		vd.map();
		vd2.map();

		vd.ghost_get();
		vd2.ghost_get();

		auto NN = vd.getCellList(0.05);
		auto NN2 = vd2.getCellList(0.05);

		cross_calc<3,0>(NN,NN2,vd,vd2);
		cross_calc<3,1>(NN,NN,vd,vd);


		auto it3 = vd.getIterator();

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

			BOOST_REQUIRE_EQUAL(vd.getProp<0>(key),vd.getProp<1>(key));

			++it3;
		}
	}
}

516 517
BOOST_AUTO_TEST_CASE( vector_dist_periodic_test_use_2d )
{
518
	Vcluster & v_cl = create_vcluster();
519 520 521 522 523 524 525 526 527

    // set the seed
	// create the random generator engine
	std::srand(v_cl.getProcessUnitID());
    std::default_random_engine eg;
    std::uniform_real_distribution<float> ud(0.0f, 1.0f);

    long int k = 524288 * v_cl.getProcessingUnits();

528
	long int big_step = k / 4;
529 530 531 532 533 534 535 536 537 538 539 540 541 542
	big_step = (big_step == 0)?1:big_step;

	print_test_v( "Testing 2D periodic vector k<=",k);

	// 2D test
	for ( ; k >= 2 ; k-= decrement(k,big_step) )
	{
		BOOST_TEST_CHECKPOINT( "Testing 2D periodic vector k=" << k );

		Box<2,float> box({0.0,0.0},{1.0,1.0});

		// Boundary conditions
		size_t bc[2]={PERIODIC,PERIODIC};

543
		// factor
544
		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
545

546
		// ghost
547
		Ghost<2,float> ghost(0.01 / factor);
548

549
		// ghost2 (a little bigger because of round off error)
550
		Ghost<2,float> ghost2(0.05001 / factor);
551 552

		// Distributed vector
Pietro Incardona's avatar
Pietro Incardona committed
553
		vector_dist<2,float, Point_test<float> > vd(k,box,bc,ghost);
554 555 556 557 558 559 560

		auto it = vd.getIterator();

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

Pietro Incardona's avatar
Pietro Incardona committed
561 562
			vd.getPos(key)[0] = ud(eg);
			vd.getPos(key)[1] = ud(eg);
563 564 565 566 567 568

			++it;
		}

		vd.map();

569
		// sync the ghost, only the property zero
570 571
		vd.ghost_get<0>();

572
		// Domain + ghost box
573
		Box<2,float> dom_ext = box;
574
		dom_ext.enlarge(ghost2);
575

576 577 578 579
		// Iterate on all particles domain + ghost
		size_t l_cnt = 0;
		size_t nl_cnt = 0;
		size_t n_out = 0;
580 581


582
		auto it2 = vd.getIterator();
583
		count_local_n_local<2,vector_dist<2,float, Point_test<float> >>(vd,it2,bc,box,dom_ext,l_cnt,nl_cnt,n_out);
584

585 586
		// No particles should be out of domain + ghost
		BOOST_REQUIRE_EQUAL(n_out,0ul);
587

588
		// Ghost must populated because we synchronized them
589
		if (k > 524288)
590
		{
591
			BOOST_REQUIRE(nl_cnt != 0);
592 593
			BOOST_REQUIRE(l_cnt > nl_cnt);
		}
594

595
		// Sum all the particles inside the domain
596 597
		v_cl.sum(l_cnt);
		v_cl.execute();
598 599

		// count that they are equal to the initial total number
600
		BOOST_REQUIRE_EQUAL((long int)l_cnt,k);
601 602 603 604 605 606

		l_cnt = 0;
		nl_cnt = 0;

		// Iterate only on the ghost particles
		auto itg = vd.getGhostIterator();
Pietro Incardona's avatar
Pietro Incardona committed
607
		count_local_n_local<2,vector_dist<2,float, Point_test<float> > >(vd,itg,bc,box,dom_ext,l_cnt,nl_cnt,n_out);
608 609 610 611 612 613

		// No particle on the ghost must be inside the domain
		BOOST_REQUIRE_EQUAL(l_cnt,0ul);

		// Ghost must be populated
		if (k > 524288)
614
		{
615
			BOOST_REQUIRE(nl_cnt != 0);
616
		}
617 618 619 620 621
	}
}

BOOST_AUTO_TEST_CASE( vector_dist_periodic_test_use_3d )
{
622
	Vcluster & v_cl = create_vcluster();
623 624 625 626 627 628 629 630 631

    // set the seed
	// create the random generator engine
	std::srand(v_cl.getProcessUnitID());
    std::default_random_engine eg;
    std::uniform_real_distribution<float> ud(0.0f, 1.0f);

    long int k = 524288 * v_cl.getProcessingUnits();

632
	long int big_step = k / 4;
633 634 635 636 637 638 639 640 641 642 643 644 645 646
	big_step = (big_step == 0)?1:big_step;

	print_test_v( "Testing 3D periodic vector k<=",k);

	// 3D test
	for ( ; k >= 2 ; k-= decrement(k,big_step) )
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D periodic vector k=" << k );

		Box<3,float> box({0.0,0.0,0.0},{1.0,1.0,1.0});

		// Boundary conditions
		size_t bc[3]={PERIODIC,PERIODIC,PERIODIC};

647
		// factor
648
		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
649

650
		// ghost
651
		Ghost<3,float> ghost(0.05 / factor);
652

653
		// ghost2 (a little bigger because of round off error)
654
		Ghost<3,float> ghost2(0.05001 / factor);
655 656

		// Distributed vector
Pietro Incardona's avatar
Pietro Incardona committed
657
		vector_dist<3,float, Point_test<float> > vd(k,box,bc,ghost);
658 659 660 661 662 663 664

		auto it = vd.getIterator();

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

Pietro Incardona's avatar
Pietro Incardona committed
665 666 667
			vd.getPos(key)[0] = ud(eg);
			vd.getPos(key)[1] = ud(eg);
			vd.getPos(key)[2] = ud(eg);
668 669 670 671 672 673 674 675 676

			++it;
		}

		vd.map();

		// sync the ghost
		vd.ghost_get<0>();

677 678 679 680 681
		// Domain + ghost
		Box<3,float> dom_ext = box;
		dom_ext.enlarge(ghost2);

		// Iterate on all particles domain + ghost
682 683 684
		size_t l_cnt = 0;
		size_t nl_cnt = 0;
		size_t n_out = 0;
685 686

		auto it2 = vd.getIterator();
Pietro Incardona's avatar
Pietro Incardona committed
687
		count_local_n_local<3,vector_dist<3,float, Point_test<float> >>(vd,it2,bc,box,dom_ext,l_cnt,nl_cnt,n_out);
688 689 690 691 692 693

		// No particles should be out of domain + ghost
		BOOST_REQUIRE_EQUAL(n_out,0ul);

		// Ghost must populated because we synchronized them
		if (k > 524288)
694
		{
695
			BOOST_REQUIRE(nl_cnt != 0);
696 697
			BOOST_REQUIRE(l_cnt > nl_cnt);
		}
698 699 700 701 702 703 704 705 706 707 708

		// Sum all the particles inside the domain
		v_cl.sum(l_cnt);
		v_cl.execute();
		BOOST_REQUIRE_EQUAL(l_cnt,(size_t)k);

		l_cnt = 0;
		nl_cnt = 0;

		// Iterate only on the ghost particles
		auto itg = vd.getGhostIterator();
Pietro Incardona's avatar
Pietro Incardona committed
709
		count_local_n_local<3,vector_dist<3,float, Point_test<float> > >(vd,itg,bc,box,dom_ext,l_cnt,nl_cnt,n_out);
710 711 712 713 714 715

		// No particle on the ghost must be inside the domain
		BOOST_REQUIRE_EQUAL(l_cnt,0ul);

		// Ghost must be populated
		if (k > 524288)
716
		{
717
			BOOST_REQUIRE(nl_cnt != 0);
718
		}
719 720 721 722 723
	}
}

BOOST_AUTO_TEST_CASE( vector_dist_periodic_test_random_walk )
{
724
	Vcluster & v_cl = create_vcluster();
725 726 727 728 729 730

    // set the seed
	// create the random generator engine
	std::srand(v_cl.getProcessUnitID());
    std::default_random_engine eg;
    std::uniform_real_distribution<float> ud(0.0f, 1.0f);
Pietro Incardona's avatar
Pietro Incardona committed
731
	
Pietro Incardona's avatar
Pietro Incardona committed
732 733
	size_t nsz[] = {0,32,4};
	nsz[0] = 65536 * v_cl.getProcessingUnits();
734

Pietro Incardona's avatar
Pietro Incardona committed
735
	print_test_v( "Testing 3D random walk vector k<=",nsz[0]);
736 737

	// 3D test
Pietro Incardona's avatar
Pietro Incardona committed
738
	for (size_t i = 0 ; i < 3 ; i++ )
739
	{
Pietro Incardona's avatar
Pietro Incardona committed
740
		size_t k = nsz[i];
Pietro Incardona's avatar
Pietro Incardona committed
741

742 743 744 745 746 747 748
		BOOST_TEST_CHECKPOINT( "Testing 3D random walk vector k=" << k );

		Box<3,float> box({0.0,0.0,0.0},{1.0,1.0,1.0});

		// Boundary conditions
		size_t bc[3]={PERIODIC,PERIODIC,PERIODIC};

749
		// factor
750
		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
751

752
		// ghost
753
		Ghost<3,float> ghost(0.01 / factor);
754 755 756 757 758

		// Distributed vector
		vector_dist<3,float, Point_test<float>, CartDecomposition<3,float> > vd(k,box,bc,ghost);

		auto it = vd.getIterator();
759 760 761 762 763

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

Pietro Incardona's avatar
Pietro Incardona committed
764 765 766
			vd.getPos(key)[0] = ud(eg);
			vd.getPos(key)[1] = ud(eg);
			vd.getPos(key)[2] = ud(eg);
767 768 769 770

			++it;
		}

771
		vd.map();
772

773
		// 10 step random walk
774

775
		for (size_t j = 0 ; j < 4 ; j++)
776 777 778 779 780 781 782
		{
			auto it = vd.getDomainIterator();

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

Pietro Incardona's avatar
Pietro Incardona committed
783 784 785
				vd.getPos(key)[0] += 0.02 * ud(eg);
				vd.getPos(key)[1] += 0.02 * ud(eg);
				vd.getPos(key)[2] += 0.02 * ud(eg);
786 787 788 789 790 791

				++it;
			}

			vd.map();

792
			vd.ghost_get<0>();
793 794 795 796 797 798

			// Count the local particles and check that the total number is consistent
			size_t cnt = total_n_part_lc(vd,bc);

			BOOST_REQUIRE_EQUAL((size_t)k,cnt);
		}
799 800 801
	}
}

802 803 804 805 806 807 808
BOOST_AUTO_TEST_CASE( vector_dist_periodic_map )
{
	Box<3,float> box({0.0,0.0,0.0},{1.0,1.0,1.0});

	// Boundary conditions
	size_t bc[3]={PERIODIC,PERIODIC,PERIODIC};

809
	// factor
810
	float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
811

812
	// ghost
813
	Ghost<3,float> ghost(0.05 / factor);
814 815 816 817 818 819 820 821 822 823 824 825

	// Distributed vector
	vector_dist<3,float, Point_test<float>, CartDecomposition<3,float> > vd(1,box,bc,ghost);

	// put particles al 1.0, check that they go to 0.0

	auto it = vd.getIterator();

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

Pietro Incardona's avatar
Pietro Incardona committed
826 827 828
		vd.getPos(key)[0] = 1.0;
		vd.getPos(key)[1] = 1.0;
		vd.getPos(key)[2] = 1.0;
829 830 831 832 833 834 835 836 837 838 839 840

		++it;
	}

	vd.map();

	auto it2 = vd.getIterator();

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

Pietro Incardona's avatar
Pietro Incardona committed
841
		float f = vd.getPos(key)[0];
842
		BOOST_REQUIRE_EQUAL(f, 0.0);
Pietro Incardona's avatar
Pietro Incardona committed
843
		f = vd.getPos(key)[1];
844
		BOOST_REQUIRE_EQUAL(f, 0.0);
Pietro Incardona's avatar
Pietro Incardona committed
845
		f = vd.getPos(key)[2];
846 847 848 849 850 851
		BOOST_REQUIRE_EQUAL(f, 0.0);

		++it2;
	}
}

Pietro Incardona's avatar
Pietro Incardona committed
852

853 854 855 856 857 858 859
BOOST_AUTO_TEST_CASE( vector_dist_not_periodic_map )
{
	Box<3,float> box({0.0,0.0,0.0},{1.0,1.0,1.0});

	// Boundary conditions
	size_t bc[3]={NON_PERIODIC,NON_PERIODIC,NON_PERIODIC};

860
	// factor
861
	float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
862

863
	// ghost
864
	Ghost<3,float> ghost(0.05 / factor);
865 866 867 868 869 870 871 872 873 874 875 876

	// Distributed vector
	vector_dist<3,float, Point_test<float>, CartDecomposition<3,float> > vd(1,box,bc,ghost);

	// put particles al 1.0, check that they go to 0.0

	auto it = vd.getIterator();

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

Pietro Incardona's avatar
Pietro Incardona committed
877 878 879
		vd.getPos(key)[0] = 1.0;
		vd.getPos(key)[1] = 1.0;
		vd.getPos(key)[2] = 1.0;
880 881 882 883 884 885 886 887 888 889 890 891

		++it;
	}

	vd.map();

	auto it2 = vd.getIterator();

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

Pietro Incardona's avatar
Pietro Incardona committed
892
		float f = vd.getPos(key)[0];
893
		BOOST_REQUIRE_EQUAL(f, 1.0);
Pietro Incardona's avatar
Pietro Incardona committed
894
		f = vd.getPos(key)[1];
895
		BOOST_REQUIRE_EQUAL(f, 1.0);
Pietro Incardona's avatar
Pietro Incardona committed
896
		f = vd.getPos(key)[2];
897 898 899 900 901 902
		BOOST_REQUIRE_EQUAL(f, 1.0);

		++it2;
	}
}

Pietro Incardona's avatar
Pietro Incardona committed
903 904
BOOST_AUTO_TEST_CASE( vector_dist_out_of_bound_policy )
{
905
	Vcluster & v_cl = create_vcluster();
Pietro Incardona's avatar
Pietro Incardona committed
906 907 908 909

	if (v_cl.getProcessingUnits() > 8)
		return;

Pietro Incardona's avatar
Pietro Incardona committed
910 911 912
	Box<3,float> box({0.0,0.0,0.0},{1.0,1.0,1.0});

	// Boundary conditions
Pietro Incardona's avatar
Pietro Incardona committed
913 914
	size_t bc[3]={NON_PERIODIC,NON_PERIODIC,NON_PERIODIC};

Pietro Incardona's avatar
Pietro Incardona committed
915
	// factor
916
	float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
Pietro Incardona's avatar
Pietro Incardona committed
917 918 919 920 921

	// ghost
	Ghost<3,float> ghost(0.05 / factor);

	// Distributed vector
Pietro Incardona's avatar
Pietro Incardona committed
922 923 924 925 926 927
	vector_dist<3,float, Point_test<float>, CartDecomposition<3,float> > vd(100,box,bc,ghost);

	// put particles at out of the boundary, they must be detected and and killed

	auto it = vd.getIterator();

928 929
	size_t cnt = 0;

Pietro Incardona's avatar
Pietro Incardona committed
930 931 932 933
	while (it.isNext())
	{
		auto key = it.get();

934 935
		if (cnt < 1)
		{
Pietro Incardona's avatar
Pietro Incardona committed
936 937 938
			vd.getPos(key)[0] = -0.06;
			vd.getPos(key)[1] = -0.06;
			vd.getPos(key)[2] = -0.06;
939 940 941
		}
		else
		{
Pietro Incardona's avatar
Pietro Incardona committed
942 943 944
			vd.getPos(key)[0] = 0.06;
			vd.getPos(key)[1] = 0.06;
			vd.getPos(key)[2] = 0.06;
945
		}
Pietro Incardona's avatar
Pietro Incardona committed
946

947
		cnt++;
Pietro Incardona's avatar
Pietro Incardona committed
948 949 950
		++it;
	}

Pietro Incardona's avatar
Pietro Incardona committed
951 952
	vd.map();

Pietro Incardona's avatar
Pietro Incardona committed
953 954
	// Particles out of the boundary are killed

955
	size_t cnt_l = vd.size_local();
Pietro Incardona's avatar
Pietro Incardona committed
956

957
	v_cl.sum(cnt_l);
Pietro Incardona's avatar
Pietro Incardona committed
958 959
	v_cl.execute();

960 961 962
	BOOST_REQUIRE_EQUAL(cnt_l,100-v_cl.getProcessingUnits());
}

963
void Test_interacting(Box<3,float> & box)
964
{
965
	Vcluster & v_cl = create_vcluster();
966 967 968 969

	if (v_cl.getProcessingUnits() > 8)
		return;

Pietro Incardona's avatar
Pietro Incardona committed
970
    // set the seed
971 972
	// create the random generator engine
	std::srand(v_cl.getProcessUnitID());
Pietro Incardona's avatar
Pietro Incardona committed
973
    std::default_random_engine eg;
974
    std::uniform_real_distribution<float> ud(-0.5f, 0.5f);
975 976

	size_t nsz[] = {0,32,4};
Pietro Incardona's avatar
Pietro Incardona committed
977 978 979
	nsz[0] = 65536 * v_cl.getProcessingUnits();

	print_test_v("Testing 3D random walk interacting particles vector k=", nsz[0]);
980 981 982 983 984 985

	// 3D test
	for (size_t i = 0 ; i < 3 ; i++ )
	{
		size_t k = nsz[i];

Pietro Incardona's avatar
Pietro Incardona committed
986
		BOOST_TEST_CHECKPOINT( "Testing 3D random walk interacting particles vector k=" << k );
987 988 989 990 991

		// Boundary conditions
		size_t bc[3]={PERIODIC,PERIODIC,PERIODIC};

		// factor
992
		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008

		// interaction radius
		float r_cut = 0.01 / factor;

		// ghost
		Ghost<3,float> ghost(r_cut);

		// Distributed vector
		vector_dist<3,float, Point_test<float>, CartDecomposition<3,float> > vd(k,box,bc,ghost);

		auto it = vd.getIterator();

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

Pietro Incardona's avatar
Pietro Incardona committed
1009 1010 1011
			vd.getPos(key)[0] = ud(eg);
			vd.getPos(key)[1] = ud(eg);
			vd.getPos(key)[2] = ud(eg);
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029

			++it;
		}

		vd.map();

		// 4 step random walk

		for (size_t j = 0 ; j < 4 ; j++)
		{
			auto it = vd.getDomainIterator();

			// Move the particles

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