grid_dist_id_unit_test.cpp 42.2 KB
Newer Older
incardon's avatar
incardon committed
1 2 3
#ifndef GRID_DIST_UNIT_TEST_HPP
#define GRID_DIST_UNIT_TEST_HPP

Pietro Incardona's avatar
Pietro Incardona committed
4 5 6 7
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

#include "Point_test.hpp"
incardon's avatar
incardon committed
8 9
#include "grid_dist_id.hpp"
#include "data_type/scalar.hpp"
10
#include "data_type/aggregate.hpp"
incardon's avatar
incardon committed
11

12

incardon's avatar
incardon committed
13 14
BOOST_AUTO_TEST_SUITE( grid_dist_id_test )

incardon's avatar
incardon committed
15
void print_test(std::string test, size_t sz)
incardon's avatar
incardon committed
16
{
17
	if (create_vcluster().getProcessUnitID() == 0)
incardon's avatar
incardon committed
18
		std::cout << test << " " << sz << "\n";
incardon's avatar
incardon committed
19 20
}

21
BOOST_AUTO_TEST_CASE( grid_dist_id_domain_grid_unit_converter3D_test)
incardon's avatar
incardon committed
22
{
23 24
	size_t bc[3] = {NON_PERIODIC, NON_PERIODIC, NON_PERIODIC};

25
	// Domain
26
	Box<3,float> domain({-0.3,-0.3,-0.3},{1.0,1.0,1.0});
27

28
	Vcluster & v_cl = create_vcluster();
incardon's avatar
incardon committed
29

incardon's avatar
incardon committed
30 31 32 33
	// Skip this test on big scale
	if (v_cl.getProcessingUnits() >= 32)
		return;

incardon's avatar
incardon committed
34
	// Test several grid dimensions
incardon's avatar
incardon committed
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
	long int k = 293;
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
	long int small_step = 21;

	print_test( "Testing 3D grid converter k<=",k);

	// 3D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D grid converter k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

		// Ghost
		Ghost<3,float> g(0.01);

		// Distributed grid with id decomposition
		grid_dist_id<3, float, scalar<float>, CartDecomposition<3,float>> g_dist(sz,domain,g);

		// get the decomposition
		auto & dec = g_dist.getDecomposition();

		// check the consistency of the decomposition
		bool val = dec.check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

		// for each local volume
		// Get the number of local grid needed
		size_t n_grid = dec.getNSubDomain();

		size_t vol = 0;

		// vector of boxes
		openfpm::vector<Box<3,size_t>> vb;

		// Allocate the grids
		for (size_t i = 0 ; i < n_grid ; i++)
		{
			// Get the local hyper-cube
			SpaceBox<3,float> sub = dec.getSubDomain(i);
81
//			sub -= domain.getP1();
82

83
			Box<3,size_t> g_box = g_dist.getCellDecomposer().convertDomainSpaceIntoGridUnits(sub,bc);
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104

			vb.add(g_box);

			vol += g_box.getVolumeKey();
		}

		// Create a writer and write
		VTKWriter<openfpm::vector<Box<3,size_t>>,VECTOR_BOX> vtk_box2;
		vtk_box2.add(vb);
		vtk_box2.write(std::to_string(v_cl.getProcessUnitID()) + "vtk_box_3D.vtk");

		v_cl.sum(vol);
		v_cl.execute();

		BOOST_REQUIRE_EQUAL(vol,sz[0]*sz[1]*sz[2]);
	}
}


BOOST_AUTO_TEST_CASE( grid_dist_id_domain_grid_unit_converter_test)
{
105 106
	size_t bc[2] = {NON_PERIODIC, NON_PERIODIC};

107 108 109
	// Domain
	Box<2,float> domain({0.0,0.0},{1.0,1.0});

110
	Vcluster & v_cl = create_vcluster();
111 112 113 114 115

	// Skip this test on big scale
	if (v_cl.getProcessingUnits() >= 32)
		return;

incardon's avatar
incardon committed
116
	for (size_t k = 1024 ; k >= 2 ; k--)
incardon's avatar
incardon committed
117
	{
118
		BOOST_TEST_CHECKPOINT( "Testing grid converter 3D k=" << k );
incardon's avatar
incardon committed
119

incardon's avatar
incardon committed
120 121 122 123
		// grid size
		size_t sz[2];
		sz[0] = k;
		sz[1] = k;
incardon's avatar
incardon committed
124

incardon's avatar
incardon committed
125 126
		// Ghost
		Ghost<2,float> g(0.01);
incardon's avatar
incardon committed
127

incardon's avatar
incardon committed
128 129
		// Distributed grid with id decomposition
		grid_dist_id<2, float, scalar<float>, CartDecomposition<2,float>> g_dist(sz,domain,g);
incardon's avatar
incardon committed
130

incardon's avatar
incardon committed
131 132
		// get the decomposition
		auto & dec = g_dist.getDecomposition();
incardon's avatar
incardon committed
133

incardon's avatar
incardon committed
134 135 136 137
		// check the consistency of the decomposition
		bool val = dec.check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

incardon's avatar
incardon committed
138 139
		// for each local volume
		// Get the number of local grid needed
Pietro Incardona's avatar
Pietro Incardona committed
140
		size_t n_grid = dec.getNSubDomain();
incardon's avatar
incardon committed
141

incardon's avatar
incardon committed
142 143 144 145 146 147
		size_t vol = 0;

		// Allocate the grids
		for (size_t i = 0 ; i < n_grid ; i++)
		{
			// Get the local hyper-cube
Pietro Incardona's avatar
Pietro Incardona committed
148
			SpaceBox<2,float> sub = dec.getSubDomain(i);
incardon's avatar
incardon committed
149

150
			Box<2,size_t> g_box = g_dist.getCellDecomposer().convertDomainSpaceIntoGridUnits(sub,bc);
incardon's avatar
incardon committed
151

incardon's avatar
incardon committed
152 153
			vol += g_box.getVolumeKey();
		}
incardon's avatar
incardon committed
154

155
		v_cl.sum(vol);
incardon's avatar
incardon committed
156 157 158 159 160 161
		v_cl.execute();

		BOOST_REQUIRE_EQUAL(vol,sz[0]*sz[1]);
	}
}

incardon's avatar
incardon committed
162

incardon's avatar
incardon committed
163
void Test2D(const Box<2,float> & domain, long int k)
incardon's avatar
incardon committed
164
{
165 166
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
167
	long int small_step = 21;
168

169 170
	print_test( "Testing 2D grid k<=",k);

incardon's avatar
incardon committed
171
	// 2D test
172
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
incardon's avatar
incardon committed
173 174
	{
		BOOST_TEST_CHECKPOINT( "Testing 2D grid k=" << k );
175 176 177

		//! [Create and access a distributed grid]

incardon's avatar
incardon committed
178 179 180 181
		// grid size
		size_t sz[2];
		sz[0] = k;
		sz[1] = k;
incardon's avatar
incardon committed
182

183
		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/2.0f);
incardon's avatar
incardon committed
184

incardon's avatar
incardon committed
185
		// Ghost
186
		Ghost<2,float> g(0.01 / factor);
incardon's avatar
incardon committed
187

incardon's avatar
incardon committed
188
		// Distributed grid with id decomposition
189
		grid_dist_id<2, float, scalar<float>> g_dist(sz,domain,g);
incardon's avatar
incardon committed
190

incardon's avatar
incardon committed
191 192 193 194
		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

incardon's avatar
incardon committed
195 196 197 198 199 200 201 202 203 204 205 206
		// Grid sm
		grid_sm<2,void> info(sz);

		// get the domain iterator
		size_t count = 0;

		auto dom = g_dist.getDomainIterator();

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);
incardon's avatar
incardon committed
207

incardon's avatar
incardon committed
208 209 210 211 212 213 214 215
			g_dist.template get<0>(key) = info.LinId(key_g);

			// Count the point
			count++;

			++dom;
		}

216 217
		//! [Create and access a distributed grid]

incardon's avatar
incardon committed
218 219 220 221
		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();

		// reduce
222
		vcl.sum(count);
incardon's avatar
incardon committed
223 224 225
		vcl.execute();

		// Check
226
		BOOST_REQUIRE_EQUAL(count,(size_t)k*k);
incardon's avatar
incardon committed
227 228 229

		auto dom2 = g_dist.getDomainIterator();

230 231 232
		grid_key_dx<2> start = dom2.getStart();
		grid_key_dx<2> stop = dom2.getStop();

Pietro Incardona's avatar
Pietro Incardona committed
233 234
		BOOST_REQUIRE_EQUAL((long int)stop.get(0),(long int)g_dist.size(0)-1);
		BOOST_REQUIRE_EQUAL((long int)stop.get(1),(long int)g_dist.size(1)-1);
235 236 237 238

		BOOST_REQUIRE_EQUAL(start.get(0),0);
		BOOST_REQUIRE_EQUAL(start.get(1),0);

incardon's avatar
incardon committed
239 240
		bool match = true;

incardon's avatar
incardon committed
241 242 243 244 245 246
		// check that the grid store the correct information
		while (dom2.isNext())
		{
			auto key = dom2.get();
			auto key_g = g_dist.getGKey(key);

incardon's avatar
incardon committed
247
			match &= (g_dist.template get<0>(key) == info.LinId(key_g))?true:false;
incardon's avatar
incardon committed
248 249 250 251

			++dom2;
		}

incardon's avatar
incardon committed
252 253
		BOOST_REQUIRE_EQUAL(match,true);

incardon's avatar
incardon committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
		g_dist.template ghost_get<0>();

		// check that the communication is correctly completed

		auto domg = g_dist.getDomainGhostIterator();

		// check that the grid with the ghost past store the correct information
		while (domg.isNext())
		{
			auto key = domg.get();
			auto key_g = g_dist.getGKey(key);

			// In this case the boundary condition are non periodic
			if (g_dist.isInside(key_g))
			{
Pietro Incardona's avatar
Pietro Incardona committed
269
				match &= (g_dist.template get<0>(key) == info.LinId(key_g))?true:false;
incardon's avatar
incardon committed
270 271 272 273
			}

			++domg;
		}
Pietro Incardona's avatar
Pietro Incardona committed
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 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 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 361 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

		BOOST_REQUIRE_EQUAL(match,true);
	}
}


void Test1D(const Box<1,float> & domain, long int k)
{
	Vcluster & v_cl = create_vcluster();
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
	long int small_step = 21;

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

	print_test( "Testing 1D grid k<=",k);

	// 1D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 1D grid k=" << k );

		//! [Create and access a distributed grid]

		// grid size
		size_t sz[1];
		sz[0] = k;

		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f);

		// Ghost
		Ghost<1,float> g(0.01 / factor);

		// Distributed grid with id decomposition
		grid_dist_id<1, float, scalar<float>> g_dist(sz,domain,g);

		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

		// Grid sm
		grid_sm<1,void> info(sz);

		// get the domain iterator
		size_t count = 0;

		auto dom = g_dist.getDomainIterator();

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			g_dist.template get<0>(key) = info.LinId(key_g);

			// Count the point
			count++;

			++dom;
		}

		//! [Create and access a distributed grid]

		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();

		// reduce
		vcl.sum(count);
		vcl.execute();

		// Check
		BOOST_REQUIRE_EQUAL(count,(size_t)k);

		auto dom2 = g_dist.getDomainIterator();

		grid_key_dx<1> start = dom2.getStart();
		grid_key_dx<1> stop = dom2.getStop();

		BOOST_REQUIRE_EQUAL((long int)stop.get(0),(long int)g_dist.size(0)-1);

		BOOST_REQUIRE_EQUAL(start.get(0),0);

		bool match = true;

		// check that the grid store the correct information
		while (dom2.isNext())
		{
			auto key = dom2.get();
			auto key_g = g_dist.getGKey(key);

			match &= (g_dist.template get<0>(key) == info.LinId(key_g))?true:false;

			++dom2;
		}

		BOOST_REQUIRE_EQUAL(match,true);

		g_dist.template ghost_get<0>();

		// check that the communication is correctly completed

		auto domg = g_dist.getDomainGhostIterator();

		// check that the grid with the ghost past store the correct information
		while (domg.isNext())
		{
			auto key = domg.get();
			auto key_g = g_dist.getGKey(key);

			// In this case the boundary condition are non periodic
			if (g_dist.isInside(key_g))
			{
				match &= (g_dist.template get<0>(key) == info.LinId(key_g))?true:false;
			}

			++domg;
		}

		BOOST_REQUIRE_EQUAL(match,true);
incardon's avatar
incardon committed
394 395 396
	}
}

incardon's avatar
incardon committed
397 398 399 400
void Test3D_sub(const Box<3,float> & domain, long int k)
{
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
401
	long int small_step = 21;
incardon's avatar
incardon committed
402 403

	// this test is only performed when the number of processor is <= 32
404
	if (create_vcluster().getProcessingUnits() > 32)
incardon's avatar
incardon committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
		return;

	print_test( "Testing 3D grid sub k<=",k);

	// 3D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D grid sub k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

		// factor
421
		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
incardon's avatar
incardon committed
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438

		// Ghost
		Ghost<3,float> g(0.01 / factor);

		// Distributed grid with id decomposition
		grid_dist_id<3, float, scalar<float>, CartDecomposition<3,float>> g_dist(sz,domain,g);

		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

		// Grid sm
		grid_sm<3,void> info(sz);

		// get the domain iterator
		size_t count = 0;

incardon's avatar
incardon committed
439 440 441 442 443
		grid_key_dx<3> one(1,1,1);
		grid_key_dx<3> one_end(k-2,k-2,k-2);

		// Sub-domain iterator
		auto dom = g_dist.getSubDomainIterator(one,one_end);
incardon's avatar
incardon committed
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			g_dist.template get<0>(key) = info.LinId(key_g);

			// Count the point
			count++;

			++dom;
		}

		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();

		// reduce
		vcl.sum(count);
		vcl.execute();

		// Check
466
		BOOST_REQUIRE_EQUAL(count,(size_t)(k-2)*(k-2)*(k-2));
incardon's avatar
incardon committed
467

incardon's avatar
incardon committed
468
		// check with a 1x1x1 square
incardon's avatar
incardon committed
469 470
		{

incardon's avatar
incardon committed
471 472
		grid_key_dx<3> one(k/2,k/2,k/2);
		grid_key_dx<3> one_end(k/2,k/2,k/2);
incardon's avatar
incardon committed
473

incardon's avatar
incardon committed
474
		count = 0;
incardon's avatar
incardon committed
475

incardon's avatar
incardon committed
476 477
		// get the sub-domain iterator
		auto dom = g_dist.getSubDomainIterator(one,one_end);
incardon's avatar
incardon committed
478

incardon's avatar
incardon committed
479 480 481 482
		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);
incardon's avatar
incardon committed
483

incardon's avatar
incardon committed
484 485 486 487
			// key_g
			BOOST_REQUIRE_EQUAL(key_g.get(0),k/2);
			BOOST_REQUIRE_EQUAL(key_g.get(1),k/2);
			BOOST_REQUIRE_EQUAL(key_g.get(2),k/2);
incardon's avatar
incardon committed
488

incardon's avatar
incardon committed
489
			auto key_s_it = dom.getGKey(key);
incardon's avatar
incardon committed
490

incardon's avatar
incardon committed
491 492 493
			BOOST_REQUIRE_EQUAL(key_g.get(0),key_s_it.get(0));
			BOOST_REQUIRE_EQUAL(key_g.get(1),key_s_it.get(1));
			BOOST_REQUIRE_EQUAL(key_g.get(2),key_s_it.get(2));
incardon's avatar
incardon committed
494

incardon's avatar
incardon committed
495 496
			// Count the point
			count++;
incardon's avatar
incardon committed
497

incardon's avatar
incardon committed
498
			++dom;
incardon's avatar
incardon committed
499 500
		}

incardon's avatar
incardon committed
501 502 503
		// reduce
		vcl.sum(count);
		vcl.execute();
incardon's avatar
incardon committed
504

505
		BOOST_REQUIRE_EQUAL(count,1ul);
incardon's avatar
incardon committed
506
		}
incardon's avatar
incardon committed
507 508 509
	}
}

incardon's avatar
incardon committed
510 511
void Test3D(const Box<3,float> & domain, long int k)
{
512 513
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
514
	long int small_step = 21;
515

516 517
	print_test( "Testing 3D grid k<=",k);

incardon's avatar
incardon committed
518
	// 3D test
519
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
incardon's avatar
incardon committed
520 521 522 523 524 525 526 527 528
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D grid k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

incardon's avatar
incardon committed
529
		// factor
530
		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
incardon's avatar
incardon committed
531

incardon's avatar
incardon committed
532
		// Ghost
incardon's avatar
incardon committed
533
		Ghost<3,float> g(0.01 / factor);
incardon's avatar
incardon committed
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566

		// Distributed grid with id decomposition
		grid_dist_id<3, float, scalar<float>, CartDecomposition<3,float>> g_dist(sz,domain,g);

		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

		// Grid sm
		grid_sm<3,void> info(sz);

		// get the domain iterator
		size_t count = 0;

		auto dom = g_dist.getDomainIterator();

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			g_dist.template get<0>(key) = info.LinId(key_g);

			// Count the point
			count++;

			++dom;
		}

		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();

		// reduce
567
		vcl.sum(count);
incardon's avatar
incardon committed
568 569 570
		vcl.execute();

		// Check
571
		BOOST_REQUIRE_EQUAL(count,(size_t)k*k*k);
incardon's avatar
incardon committed
572

incardon's avatar
incardon committed
573 574
		bool match = true;

incardon's avatar
incardon committed
575 576 577 578 579 580 581 582
		auto dom2 = g_dist.getDomainIterator();

		// check that the grid store the correct information
		while (dom2.isNext())
		{
			auto key = dom2.get();
			auto key_g = g_dist.getGKey(key);

incardon's avatar
incardon committed
583
			match &= (g_dist.template get<0>(key) == info.LinId(key_g))?true:false;
incardon's avatar
incardon committed
584 585 586 587

			++dom2;
		}

incardon's avatar
incardon committed
588 589
		BOOST_REQUIRE_EQUAL(match,true);

590 591
		//! [Synchronize the ghost and check the information]

incardon's avatar
incardon committed
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
		g_dist.template ghost_get<0>();

		// check that the communication is correctly completed

		auto domg = g_dist.getDomainGhostIterator();

		// check that the grid with the ghost past store the correct information
		while (domg.isNext())
		{
			auto key = domg.get();
			auto key_g = g_dist.getGKey(key);

			// In this case the boundary condition are non periodic
			if (g_dist.isInside(key_g))
			{
incardon's avatar
incardon committed
607
				match &= (g_dist.template get<0>(key) == info.LinId(key_g))?true:false;
incardon's avatar
incardon committed
608 609 610 611
			}

			++domg;
		}
incardon's avatar
incardon committed
612 613

		BOOST_REQUIRE_EQUAL(match,true);
614 615

		//! [Synchronize the ghost and check the information]
incardon's avatar
incardon committed
616
	}
incardon's avatar
incardon committed
617 618
}

incardon's avatar
incardon committed
619 620 621 622 623 624

void Test3D_gg(const Box<3,float> & domain, long int k, long int gk)
{
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;

625
	// this test is only performed when the number of processor is <= 32
626
	if (create_vcluster().getProcessingUnits() > 32)
627 628
		return;

incardon's avatar
incardon committed
629 630 631
	print_test( "Testing 3D grid k<=",k);

	// 3D test
632
	for ( ; k > 64 ; k /= 2 )
incardon's avatar
incardon committed
633 634 635 636 637 638 639 640 641 642
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D grid ghost integer k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

		// Ghost
643
		Ghost<3,long int> g(gk);
incardon's avatar
incardon committed
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659

		// Distributed grid with id decomposition
		grid_dist_id<3, float, scalar<float>, CartDecomposition<3,float>> g_dist(sz,domain,g);

		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

		auto lg = g_dist.getLocalGridsInfo();

		// for each local grid check that the border is 1 point
		// (Warning this property can only be ensured with k is a multiple of 2)
		// in the other case it will be mostly like that but cannot be ensured

		for (size_t i = 0 ; i < lg.size() ; i++)
		{
incardon's avatar
incardon committed
660 661 662 663 664
			for (size_t j = 0 ; j < 3 ; j++)
			{
				BOOST_REQUIRE(lg.get(i).Dbox.getLow(j) >= gk);
				BOOST_REQUIRE((lg.get(i).GDbox.getHigh(j) - lg.get(i).Dbox.getHigh(j)) >= gk);
			}
incardon's avatar
incardon committed
665 666 667 668
		}
	}
}

669 670 671 672 673
/*! \brief Test when the domain is not from 0.0 to 1.0
 *
 *
 */

Pietro Incardona's avatar
Pietro Incardona committed
674
void Test3D_domain(const Box<3,float> & domain, long int k, const periodicity<3> & pr)
675 676 677 678 679
{
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
	long int small_step = 21;

Pietro Incardona's avatar
Pietro Incardona committed
680
	print_test( "Testing 3D grid shift domain k<=",k);
681 682 683 684

	// 3D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
Pietro Incardona's avatar
Pietro Incardona committed
685
		BOOST_TEST_CHECKPOINT( "Testing 3D grid shift domain k=" << k );
686 687 688 689 690 691 692 693

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

		// factor
694
		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
695 696 697 698 699

		// Ghost
		Ghost<3,float> g(0.01 / factor);

		// Distributed grid with id decomposition
Pietro Incardona's avatar
Pietro Incardona committed
700 701 702
		grid_dist_id<3, float, aggregate<long int,long int>, CartDecomposition<3,float>> g_dist(sz,domain,g,pr);

		auto & v_cl = create_vcluster();
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776

		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

		// Grid sm
		grid_sm<3,void> info(sz);

		// get the domain iterator
		size_t count = 0;

		auto dom = g_dist.getDomainIterator();

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			g_dist.template get<0>(key) = count;
			g_dist.template get<1>(key) = info.LinId(key_g);

			// Count the point
			count++;

			++dom;
		}

		size_t count2 = count;
		openfpm::vector<size_t> pnt;

		// Get the total size of the local grids on each processors
		// and the total size
		v_cl.sum(count2);
		v_cl.allGather(count,pnt);
		v_cl.execute();
		size_t s_pnt = 0;

		// calculate the starting point for this processor
		for (size_t i = 0 ; i < v_cl.getProcessUnitID() ; i++)
			s_pnt += pnt.get(i);

		// Check
		BOOST_REQUIRE_EQUAL(count2,(size_t)k*k*k);

		// sync the ghost
		g_dist.template ghost_get<0,1>();

		bool match = true;

		// check that the communication is correctly completed

		auto domg = g_dist.getDomainGhostIterator();

		// check that the grid with the ghost past store the correct information
		while (domg.isNext())
		{
			auto key = domg.get();
			auto key_g = g_dist.getGKey(key);

			// In this case the boundary condition are non periodic
			if (g_dist.isInside(key_g))
			{
				match &= (g_dist.template get<1>(key) == info.LinId(key_g))?true:false;
			}

			++domg;
		}

		BOOST_REQUIRE_EQUAL(match,true);
	}
}



incardon's avatar
incardon committed
777
void Test2D_complex(const Box<2,float> & domain, long int k)
incardon's avatar
incardon committed
778
{
incardon's avatar
incardon committed
779
	typedef Point_test<float> p;
incardon's avatar
incardon committed
780

incardon's avatar
incardon committed
781 782
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
783
	long int small_step = 21;
incardon's avatar
incardon committed
784

785 786
	print_test( "Testing 2D complex grid k<=",k);

incardon's avatar
incardon committed
787 788 789 790
	// 2D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 2D complex grid k=" << k );
incardon's avatar
incardon committed
791

792 793
		//! [Create and access a distributed grid complex]

incardon's avatar
incardon committed
794 795 796 797
		// grid size
		size_t sz[2];
		sz[0] = k;
		sz[1] = k;
incardon's avatar
incardon committed
798

799
		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/2.0f);
incardon's avatar
incardon committed
800

incardon's avatar
incardon committed
801 802
		// Ghost
		Ghost<2,float> g(0.01 / factor);
803

incardon's avatar
incardon committed
804
		// Distributed grid with id decomposition
805
		grid_dist_id<2, float, Point_test<float>> g_dist(sz,domain,g);
806

incardon's avatar
incardon committed
807 808 809
		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);
incardon's avatar
incardon committed
810

incardon's avatar
incardon committed
811 812
		// Grid sm
		grid_sm<2,void> info(sz);
incardon's avatar
incardon committed
813

incardon's avatar
incardon committed
814 815
		// get the domain iterator
		size_t count = 0;
incardon's avatar
incardon committed
816

incardon's avatar
incardon committed
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847
		auto dom = g_dist.getDomainIterator();

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			size_t k = info.LinId(key_g);

			g_dist.template get<p::x>(key) = 1 + k;
			g_dist.template get<p::y>(key) = 567 + k;
			g_dist.template get<p::z>(key) = 341 + k;
			g_dist.template get<p::s>(key) = 5670 + k;
			g_dist.template get<p::v>(key)[0] = 921 + k;
			g_dist.template get<p::v>(key)[1] = 5675 + k;
			g_dist.template get<p::v>(key)[2] = 117 + k;
			g_dist.template get<p::t>(key)[0][0] = 1921 + k;
			g_dist.template get<p::t>(key)[0][1] = 25675 + k;
			g_dist.template get<p::t>(key)[0][2] = 3117 + k;
			g_dist.template get<p::t>(key)[1][0] = 4921 + k;
			g_dist.template get<p::t>(key)[1][1] = 55675 + k;
			g_dist.template get<p::t>(key)[1][2] = 6117 + k;
			g_dist.template get<p::t>(key)[2][0] = 7921 + k;
			g_dist.template get<p::t>(key)[2][1] = 85675 + k;
			g_dist.template get<p::t>(key)[2][2] = 9117 + k;

			// Count the point
			count++;

			++dom;
		}
incardon's avatar
incardon committed
848

849 850
		//! [Create and access a distributed grid complex]

incardon's avatar
incardon committed
851 852
		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();
incardon's avatar
incardon committed
853

incardon's avatar
incardon committed
854
		// reduce
855
		vcl.sum(count);
incardon's avatar
incardon committed
856
		vcl.execute();
incardon's avatar
incardon committed
857

incardon's avatar
incardon committed
858
		// Check
859
		BOOST_REQUIRE_EQUAL(count,(size_t)k*k);
incardon's avatar
incardon committed
860 861

		auto dom2 = g_dist.getDomainIterator();
incardon's avatar
incardon committed
862

incardon's avatar
incardon committed
863
		bool match = true;
incardon's avatar
incardon committed
864

incardon's avatar
incardon committed
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894
		// check that the grid store the correct information
		while (dom2.isNext())
		{
			auto key = dom2.get();
			auto key_g = g_dist.getGKey(key);

			size_t k = info.LinId(key_g);

			match &= (g_dist.template get<p::x>(key) == 1 + k)?true:false;
			match &= (g_dist.template get<p::y>(key) == 567 + k)?true:false;
			match &= (g_dist.template get<p::z>(key) == 341 + k)?true:false;
			match &= (g_dist.template get<p::s>(key) == 5670 + k)?true:false;
			match &= (g_dist.template get<p::v>(key)[0] == 921 + k)?true:false;
			match &= (g_dist.template get<p::v>(key)[1] == 5675 + k)?true:false;
			match &= (g_dist.template get<p::v>(key)[2] == 117 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[0][0] == 1921 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[0][1] == 25675 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[0][2] == 3117 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[1][0] == 4921 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[1][1] == 55675 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[1][2] == 6117 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[2][0] == 7921 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[2][1] == 85675 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[2][2] == 9117 + k)?true:false;

			++dom2;
		}

		BOOST_REQUIRE_EQUAL(match,true);

895 896
		//! [Synchronized distributed grid complex]

incardon's avatar
incardon committed
897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935
		g_dist.template ghost_get<p::x,p::y,p::z,p::s,p::v,p::t>();

		// check that the communication is correctly completed

		auto domg = g_dist.getDomainGhostIterator();

		// check that the grid with the ghost past store the correct information
		while (domg.isNext())
		{
			auto key = domg.get();
			auto key_g = g_dist.getGKey(key);

			// In this case the boundary condition are non periodic
			if (g_dist.isInside(key_g))
			{
				size_t k = info.LinId(key_g);

				match &= (g_dist.template get<p::x>(key) == 1 + k)?true:false;
				match &= (g_dist.template get<p::y>(key) == 567 + k)?true:false;
				match &= (g_dist.template get<p::z>(key) == 341 + k)?true:false;
				match &= (g_dist.template get<p::s>(key) == 5670 + k)?true:false;

				match &= (g_dist.template get<p::v>(key)[0] == 921 + k)?true:false;
				match &= (g_dist.template get<p::v>(key)[1] == 5675 + k)?true:false;
				match &= (g_dist.template get<p::v>(key)[2] == 117 + k)?true:false;

				match &= (g_dist.template get<p::t>(key)[0][0] == 1921 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[0][1] == 25675 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[0][2] == 3117 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[1][0] == 4921 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[1][1] == 55675 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[1][2] == 6117 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[2][0] == 7921 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[2][1] == 85675 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[2][2] == 9117 + k)?true:false;
			}

			++domg;
		}
936 937

		//! [Synchronized distributed grid complex]
incardon's avatar
incardon committed
938
	}
incardon's avatar
incardon committed
939 940
}

incardon's avatar
incardon committed
941
void Test3D_complex(const Box<3,float> & domain, long int k)
incardon's avatar
incardon committed
942
{
incardon's avatar
incardon committed
943
	typedef Point_test<float> p;
incardon's avatar
incardon committed
944

incardon's avatar
incardon committed
945 946
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
947
	long int small_step = 21;
incardon's avatar
incardon committed
948

949 950
	print_test( "Testing 3D grid complex k<=",k);

incardon's avatar
incardon committed
951 952 953 954 955 956 957 958 959 960
	// 2D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D complex grid k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;
incardon's avatar
incardon committed
961

incardon's avatar
incardon committed
962
		// factor
963
		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
incardon's avatar
incardon committed
964

incardon's avatar
incardon committed
965 966
		// Ghost
		Ghost<3,float> g(0.01 / factor);
incardon's avatar
incardon committed
967

incardon's avatar
incardon committed
968 969
		// Distributed grid with id decomposition
		grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> g_dist(sz,domain,g);
incardon's avatar
incardon committed
970

incardon's avatar
incardon committed
971 972 973
		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);
incardon's avatar
incardon committed
974

incardon's avatar
incardon committed
975 976
		// Grid sm
		grid_sm<3,void> info(sz);
incardon's avatar
incardon committed
977

incardon's avatar
incardon committed
978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016
		// get the domain iterator
		size_t count = 0;

		auto dom = g_dist.getDomainIterator();

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			size_t k = info.LinId(key_g);

			g_dist.template get<p::x>(key) = 1 + k;
			g_dist.template get<p::y>(key) = 567 + k;
			g_dist.template get<p::z>(key) = 341 + k;
			g_dist.template get<p::s>(key) = 5670 + k;
			g_dist.template get<p::v>(key)[0] = 921 + k;
			g_dist.template get<p::v>(key)[1] = 5675 + k;
			g_dist.template get<p::v>(key)[2] = 117 + k;
			g_dist.template get<p::t>(key)[0][0] = 1921 + k;
			g_dist.template get<p::t>(key)[0][1] = 25675 + k;
			g_dist.template get<p::t>(key)[0][2] = 3117 + k;
			g_dist.template get<p::t>(key)[1][0] = 4921 + k;
			g_dist.template get<p::t>(key)[1][1] = 55675 + k;
			g_dist.template get<p::t>(key)[1][2] = 6117 + k;
			g_dist.template get<p::t>(key)[2][0] = 7921 + k;
			g_dist.template get<p::t>(key)[2][1] = 85675 + k;
			g_dist.template get<p::t>(key)[2][2] = 9117 + k;

			// Count the point
			count++;

			++dom;
		}

		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();

		// reduce
1017
		vcl.sum(count);
incardon's avatar
incardon committed
1018 1019 1020
		vcl.execute();

		// Check
1021
		BOOST_REQUIRE_EQUAL(count,(size_t)k*k*k);
incardon's avatar
incardon committed
1022

incardon's avatar
incardon committed
1023
		bool match = true;
incardon's avatar
incardon committed
1024

incardon's avatar
incardon committed
1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
		auto dom2 = g_dist.getDomainIterator();

		// check that the grid store the correct information
		while (dom2.isNext())
		{
			auto key = dom2.get();
			auto key_g = g_dist.getGKey(key);

			size_t k = info.LinId(key_g);

			match &= (g_dist.template get<p::x>(key) == 1 + k)?true:false;
			match &= (g_dist.template get<p::y>(key) == 567 + k)?true:false;
			match &= (g_dist.template get<p::z>(key) == 341 + k)?true:false;
			match &= (g_dist.template get<p::s>(key) == 5670 + k)?true:false;
			match &= (g_dist.template get<p::v>(key)[0] == 921 + k)?true:false;
			match &= (g_dist.template get<p::v>(key)[1] == 5675 + k)?true:false;
			match &= (g_dist.template get<p::v>(key)[2] == 117 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[0][0] == 1921 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[0][1] == 25675 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[0][2] == 3117 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[1][0] == 4921 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[1][1] == 55675 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[1][2] == 6117 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[2][0] == 7921 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[2][1] == 85675 + k)?true:false;
			match &= (g_dist.template get<p::t>(key)[2][2] == 9117 + k)?true:false;

			++dom2;
		}

		BOOST_REQUIRE_EQUAL(match,true);

		g_dist.template ghost_get<p::x,p::y,p::z,p::s,p::v,p::t>();
incardon's avatar
incardon committed
1058

incardon's avatar
incardon committed
1059
		// check that the communication is correctly completed
incardon's avatar
incardon committed
1060

incardon's avatar
incardon committed
1061
		auto domg = g_dist.getDomainGhostIterator();
incardon's avatar
incardon committed
1062

incardon's avatar
incardon committed
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
		// check that the grid with the ghost past store the correct information
		while (domg.isNext())
		{
			auto key = domg.get();
			auto key_g = g_dist.getGKey(key);

			size_t k = info.LinId(key_g);

			// In this case the boundary condition are non periodic
			if (g_dist.isInside(key_g))
			{
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091
				match &= (g_dist.template get<p::x>(key) == 1 + k)?true:false;
				match &= (g_dist.template get<p::y>(key) == 567 + k)?true:false;
				match &= (g_dist.template get<p::z>(key) == 341 + k)?true:false;
				match &= (g_dist.template get<p::s>(key) == 5670 + k)?true:false;

				match &= (g_dist.template get<p::v>(key)[0] == 921 + k)?true:false;
				match &= (g_dist.template get<p::v>(key)[1] == 5675 + k)?true:false;
				match &= (g_dist.template get<p::v>(key)[2] == 117 + k)?true:false;

				match &= (g_dist.template get<p::t>(key)[0][0] == 1921 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[0][1] == 25675 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[0][2] == 3117 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[1][0] == 4921 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[1][1] == 55675 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[1][2] == 6117 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[2][0] == 7921 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[2][1] == 85675 + k)?true:false;
				match &= (g_dist.template get<p::t>(key)[2][2] == 9117 + k)?true:false;
incardon's avatar
incardon committed
1092 1093 1094 1095 1096 1097 1098 1099 1100
			}

			++domg;
		}

		BOOST_REQUIRE_EQUAL(match,true);
	}
}

incardon's avatar
incardon committed
1101 1102 1103 1104 1105 1106
// Test duplicated topology

void Test3D_dup(const Box<3,float> & domain, long int k)
{
	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
incardon's avatar
incardon committed
1107
	long int small_step = 21;
incardon's avatar
incardon committed
1108
	long int k_old = k;
incardon's avatar
incardon committed
1109

1110
	Vcluster & v_cl = create_vcluster();
incardon's avatar
incardon committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128

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

	print_test( "Testing 3D duplicate topology complex k<=",k);

	// 3D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D copy decomposition grid k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

		// factor
1129
		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
incardon's avatar
incardon committed
1130 1131 1132 1133

		// Ghost
		Ghost<3,float> g(0.01 / factor);

incardon's avatar
incardon committed
1134
		//! [Construct two grid with the same decomposition]
incardon's avatar
incardon committed
1135

1136 1137
		// Distributed grid with id decomposition (It work also without the third template parameter)
		// Here is given to show that the 2 grid MUST have the same decomposition strategy
incardon's avatar
incardon committed
1138 1139 1140
		grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> g_dist1(sz,domain,g);

		// another grid with the same decomposition
1141
		grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> g_dist2(g_dist1.getDecomposition(),sz,g);
incardon's avatar
incardon committed
1142

incardon's avatar
incardon committed
1143 1144
		//! [Construct two grid with the same decomposition]

1145 1146 1147 1148 1149
		bool ret = g_dist2.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(ret,true);
		ret = g_dist2.getDecomposition().is_equal(g_dist2.getDecomposition());
		BOOST_REQUIRE_EQUAL(ret,true);

incardon's avatar
incardon committed
1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168

		auto dom_g1 = g_dist1.getDomainIterator();
		auto dom_g2 = g_dist2.getDomainIterator();

		bool check = true;

		while (dom_g1.isNext())
		{
			auto key1 = dom_g1.get();
			auto key2 = dom_g2.get();

			check &= (key1 == key2)?true:false;

			++dom_g1;
			++dom_g2;
		}

		BOOST_REQUIRE_EQUAL(check,true);
	}
incardon's avatar
incardon committed
1169

incardon's avatar
incardon committed
1170 1171
	k = k_old;

incardon's avatar
incardon committed
1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183
	// 3D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing 3D copy decomposition grid k=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

		// factor
1184
		float factor = pow(create_vcluster().getProcessingUnits()/2.0f,1.0f/3.0f);
incardon's avatar
incardon committed
1185 1186 1187 1188 1189 1190 1191 1192

		// Ghost
		Ghost<3,float> g(0.01 / factor);

		// Distributed grid with id decomposition
		grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> * g_dist1 = new grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>>(sz,domain,g);

		// another grid with the same decomposition
1193
		grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>> * g_dist2 = new grid_dist_id<3, float, Point_test<float>, CartDecomposition<3,float>>(g_dist1->getDecomposition(),sz,g);
incardon's avatar
incardon committed
1194 1195 1196

		delete g_dist1;

incardon's avatar
incardon committed
1197 1198
		bool ret = g_dist2->getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(ret,true);
1199 1200

		delete g_dist2;
incardon's avatar
incardon committed
1201
	}
incardon's avatar
incardon committed
1202 1203
}

1204

1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
// Test grid periodic

void Test3D_periodic(const Box<3,float> & domain, long int k)
{
	Vcluster & v_cl = create_vcluster();

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

	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
	long int small_step = 21;

	print_test( "Testing grid periodic k<=",k);

	// 3D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing grid periodick<=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

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

		// Ghost
		Ghost<3,float> g(0.01 / factor);

		// periodicity
		periodicity<3> pr = {{PERIODIC,PERIODIC,PERIODIC}};

		// Distributed grid with id decomposition
Pietro Incardona's avatar
Pietro Incardona committed
1241
		grid_dist_id<3, float, aggregate<long int>, CartDecomposition<3,float>> g_dist(sz,domain,g,pr);
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251

		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

		// Grid sm
		grid_sm<3,void> info(sz);

		size_t count = 0;

Pietro Incardona's avatar
Pietro Incardona committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
		// Set to zero the full grid

		auto dom1 = g_dist.getDomainGhostIterator();

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

			g_dist.template get<0>(key) = -1;

			++dom1;
		}

1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
		auto dom = g_dist.getDomainIterator();

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			g_dist.template get<0>(key) = info.LinId(key_g);

			// Count the points
			count++;

			++dom;
		}

		// Get the virtual cluster machine
		Vcluster & vcl = g_dist.getVC();

		// reduce
		vcl.sum(count);
		vcl.execute();

		// Check
		BOOST_REQUIRE_EQUAL(count,(size_t)k*k*k);

1290 1291 1292 1293 1294 1295 1296
		size_t tot = g_dist.getLocalDomainSize();
		// reduce
		vcl.sum(tot);
		vcl.execute();

		BOOST_REQUIRE_EQUAL(count,tot);

1297 1298 1299 1300 1301 1302 1303 1304
		// sync the ghosts
		g_dist.ghost_get<0>();

		bool match = true;

		// Domain + Ghost iterator
		auto dom_gi = g_dist.getDomainGhostIterator();

Pietro Incardona's avatar
Pietro Incardona committed
1305 1306
		size_t out_cnt = 0;

1307 1308
		while (dom_gi.isNext())
		{
Pietro Incardona's avatar
Pietro Incardona committed
1309 1310
			bool out_p = false;

1311 1312 1313
			auto key = dom_gi.get();
			auto key_g = g_dist.getGKey(key);

Pietro Incardona's avatar
Pietro Incardona committed
1314 1315 1316
			// Return the external boxes
			auto & gb = dom_gi.getGBoxes();

1317 1318 1319 1320
			// transform the key to be periodic
			for (size_t i = 0 ; i < 3 ; i++)
			{
				if (key_g.get(i) < 0)
Pietro Incardona's avatar
Pietro Incardona committed
1321
				{key_g.set_d(i,key_g.get(i) + k);out_p = true;}
1322
				else if (key_g.get(i) >= k)
Pietro Incardona's avatar
Pietro Incardona committed
1323
				{key_g.set_d(i,key_g.get(i) - k);out_p = true;}
1324 1325
			}

Pietro Incardona's avatar
Pietro Incardona committed
1326 1327 1328
			if (g_dist.template get<0>(key) != -1 && out_p == true)
				out_cnt++;

Pietro Incardona's avatar
Pietro Incardona committed
1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347
			// The last points can be invalid because of rounding off problems
			bool can_invalid = false;
			if (key.getKey().get(0) == 0 || key.getKey().get(1) == 0 || key.getKey().get(2) == 0)
				can_invalid = true;
			else if (key.getKey().get(0) == gb.get(key.getSub()).GDbox.getHigh(0) ||
					 key.getKey().get(1) == gb.get(key.getSub()).GDbox.getHigh(1) ||
					 key.getKey().get(2) == gb.get(key.getSub()).GDbox.getHigh(2))
				can_invalid = true;

			if (can_invalid == true)
			{
				if ( g_dist.template get<0>(key) != -1 && info.LinId(key_g) != g_dist.template get<0>(key) )
					match &= false;
			}
			else
			{
				if (info.LinId(key_g) != g_dist.template get<0>(key) )
					match &= false;
			}
1348 1349 1350 1351 1352

			++dom_gi;
		}

		BOOST_REQUIRE_EQUAL(match, true);
Pietro Incardona's avatar
Pietro Incardona committed
1353 1354 1355 1356 1357 1358
		if (k > 83)
		{
			vcl.sum(out_cnt);
			vcl.execute();
			BOOST_REQUIRE(out_cnt != 0ul);
		}
1359 1360 1361
	}
}

incardon's avatar
incardon committed
1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481
// Test grid periodic

void Test3D_periodic_put(const Box<3,float> & domain, long int k)
{
	Vcluster & v_cl = create_vcluster();

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

	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
	long int small_step = 21;

	print_test( "Testing grid periodic put k<=",k);

	// 3D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing grid periodick<=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

		// Ghost
		Ghost<3,long int> g(1);

		// periodicity
		periodicity<3> pr = {{PERIODIC,PERIODIC,PERIODIC}};

		// Distributed grid with id decomposition
		grid_dist_id<3, float, aggregate<long int>, CartDecomposition<3,float>> g_dist(sz,domain,g,pr);

		// check the consistency of the decomposition
		bool val = g_dist.getDecomposition().check_consistency();
		BOOST_REQUIRE_EQUAL(val,true);

		// Grid sm
		grid_sm<3,void> info(sz);

		size_t count = 0;

		{
		auto dom = g_dist.getDomainIterator();

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

			g_dist.template get<0>(key) = -6.0;

			// Count the points
			count++;

			++dom;
		}
		}

		// Set to zero the full grid

		{
		auto dom = g_dist.getDomainIterator();

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

			g_dist.template get<0>(key.move(0,1)) += 1.0;
			g_dist.template get<0>(key.move(0,-1)) += 1.0;
			g_dist.template get<0>(key.move(1,1)) += 1.0;
			g_dist.template get<0>(key.move(1,-1)) += 1.0;
			g_dist.template get<0>(key.move(2,1)) += 1.0;
			g_dist.template get<0>(key.move(2,-1)) += 1.0;

			++dom;
		}
		}

		bool correct = true;

		// Domain + Ghost iterator
		auto dom_gi = g_dist.getDomainIterator();

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

			correct &= (g_dist.template get<0>(key) == 0);

			++dom_gi;
		}

		g_dist.ghost_put<add_,0>();

		if (count != 0)
			BOOST_REQUIRE_EQUAL(correct, false);

		// sync the ghosts
		g_dist.ghost_get<0>();

		correct = true;

		// Domain + Ghost iterator
		auto dom_gi2 = g_dist.getDomainIterator();

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

			correct &= (g_dist.template get<0>(key) == 0);

			++dom_gi2;
		}

		BOOST_REQUIRE_EQUAL(correct, true);
	}
}

Pietro Incardona's avatar
Pietro Incardona committed
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518
void Test_grid_copy(const Box<3,float> & domain, long int k)
{
	typedef Point_test<float> p;

	Vcluster & v_cl = create_vcluster();

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

	long int big_step = k / 30;
	big_step = (big_step == 0)?1:big_step;
	long int small_step = 21;

	print_test( "Testing grid copy k<=",k);

	// 3D test
	for ( ; k >= 2 ; k-= (k > 2*big_step)?big_step:small_step )
	{
		BOOST_TEST_CHECKPOINT( "Testing grid periodick<=" << k );

		// grid size
		size_t sz[3];
		sz[0] = k;
		sz[1] = k;
		sz[2] = k;

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

		// Ghost
		Ghost<3,float> g(0.01 / factor);

		// periodicity
		periodicity<3> pr = {{PERIODIC,PERIODIC,PERIODIC}};

		// Distributed grid with id decomposition
		grid_dist_id<3,float,Point_test<float>> g_dist(sz,domain,g,pr);
1519
		grid_dist_id<3,float,Point_test<float>> g_dist2(g_dist.getDecomposition(),sz,g);
Pietro Incardona's avatar
Pietro Incardona committed
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591

		// Grid sm
		grid_sm<3,void> info(sz);

		// Set to zero the full grid
		auto dom = g_dist.getDomainIterator();

		while (dom.isNext())
		{
			auto key = dom.get();
			auto key_g = g_dist.getGKey(key);

			size_t k = info.LinId(key_g);

			g_dist.template get<p::x>(key) = 1 + k;
			g_dist.template get<p::y>(key) = 567 + k;
			g_dist.template get<p::z>(key) = 341 + k;
			g_dist.template get<p::s>(key) = 5670 + k;
			g_dist.template get<p::v>(key)[0] = 921 + k;
			g_dist.template get<p::v>(key)[1] = 5675 + k;
			g_dist.template get<p::v>(key)[2] = 117 + k;
			g_dist.template get<p::t>(key)[0][0] = 1921 + k;
			g_dist.template get<p::t>(key)[0][1] = 25675 + k;
			g_dist.template get<p::t>(key)[0][2] = 3117 + k;
			g_dist.template get<p::t>(key)[1][0] = 4921 + k;
			g_dist.template get<p::t>(key)[1][1] = 55675 + k;
			g_dist.template get<p::t>(key)[1][2] = 6117 + k;
			g_dist.template get<p::t>(key)[2][0] = 7921 + k;
			g_dist.template get<p::t>(key)[2][1] = 85675 + k;
			g_dist.template get<p::t>(key)[2][2] = 9117 + k;

			++dom;
		}

		g_dist2.copy(g_dist);

		auto dom2 = g_dist2.getDomainIterator();

		bool match = true;

		// check that the grid store the correct information
		while (dom2.isNext())
		{
			auto key = dom2.get();
			auto key_g = g_dist.getGKey(key);

			size_t k = info.LinId(key_g);

			match &= (g_dist2.template get<p::x>(key) == 1 + k)?true:false;
			match &= (g_dist2.template get<p::y>(key) == 567 + k)?true:false;
			match &= (g_dist2.template get<p::z>(key) == 341 + k)?true:false;
			match &= (g_dist2.template get<p::s>(key) == 5670 + k)?true:false;
			match &= (g_dist2.template get<p::v>(key)[0] == 921 + k)?true:false;
			match &= (g_dist2.template get<p::v>(key)[1] == 5675 + k)?true:false;
			match &= (g_dist2.template get<p::v>(key)[2] == 117 + k)?true:false;
			match &= (g_dist2.template get<p::t>(key)[0][0] == 1921 + k)?true:false;
			match &= (g_dist2.template get<p::t>(key)[0][1] == 25675 + k)?true:false;
			match &= (g_dist2.template get<p::t>(key)[0][2] == 3117 + k)?true:false;
			match &= (g_dist2.template get<p::t>(key)[1][0] == 4921 + k)?true:false;
			match &= (g_dist2.template get<p::t>(key)[1][1] == 55675 + k)?true:false;
			match &= (g_dist2.template get<p::t>(key)[1][2] == 6117 + k)?true:false;
			match &= (g_dist2.template get<p::t>(key)[2][0] == 7921 + k)?true:false;
			match &= (g_dist2.template get<p::t>(key)[2][1] == 85675 + k)?true:false;
			match &= (g_dist2.template get<p::t>(key)[2][2] == 9117 + k)?true:false;

			++dom2;
		}

		BOOST_REQUIRE_EQUAL(match,true);
	}
}

1592
#include "grid_dist_id_unit_test_ext_dom.hpp"
1593
#include "grid_dist_id_unit_test_unb_ghost.hpp"
1594

incardon's avatar
incardon committed
1595 1596 1597 1598 1599
BOOST_AUTO_TEST_CASE( grid_dist_id_iterator_test_use)
{
	// Domain
	Box<2,float> domain({0.0,0.0},{1.0,1.0});

1600
	long int k = 1024*1024*create_vcluster().getProcessingUnits();
incardon's avatar
incardon committed
1601 1602 1603 1604 1605 1606 1607
	k = std::pow(k, 1/2.);

	Test2D(domain,k);
	Test2D_complex(domain,k);
	// Domain
	Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0});

1608
	k = 128*128*128*create_vcluster().getProcessingUnits();
incardon's avatar
incardon committed
1609 1610 1611
	k = std::pow(k, 1/3.);
	Test3D(domain3,k);
	Test3D_complex(domain3,k);
incardon's avatar
incardon committed
1612 1613 1614 1615 1616 1617 1618
}

BOOST_AUTO_TEST_CASE( grid_dist_id_dup)
{
	// Domain
	Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0});

1619
	long int k = 128*128*128*create_vcluster().getProcessingUnits();
incardon's avatar
incardon committed
1620
	k = std::pow(k, 1/3.);
incardon's avatar
incardon committed
1621
	Test3D_dup(domain3,k);
incardon's avatar
incardon committed
1622 1623
}

incardon's avatar
incardon committed
1624 1625 1626 1627 1628
BOOST_AUTO_TEST_CASE( grid_dist_id_sub)
{
	// Domain
	Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0});

1629
	long int k = 128*128*128*create_vcluster().getProcessingUnits();
incardon's avatar
incardon committed
1630 1631 1632 1633
	k = std::pow(k, 1/3.);
	Test3D_sub(domain3,k);
}

incardon's avatar
incardon committed
1634

incardon's avatar
incardon committed
1635 1636 1637 1638 1639
BOOST_AUTO_TEST_CASE( grid_dist_id_with_grid_unit_ghost )
{
	// Domain
	Box<2,float> domain({0.0,0.0},{1.0,1.0});

1640
	long int k = 1024*1024*create_vcluster().getProcessingUnits();
incardon's avatar
incardon committed
1641 1642 1643 1644 1645
	k = std::pow(k, 1/2.);

	// Domain
	Box<3,float> domain3({0.0,0.0,0.0},{1.0,1.0,1.0});

1646
	k = 128*128*128*create_vcluster().getProcessingUnits();
incardon's avatar
incardon committed
1647 1648 1649
	k = std::pow(k, 1/3.);
	Test3D_gg(domain3,k,1);
}
1650

1651 1652 1653 1654

BOOST_AUTO_TEST_CASE( grid_dist_id_domain_test_use)
{
	// Domain
Pietro Incardona's avatar
Pietro Incardona committed
1655
	Box<3,float> domain3({-0.3,-0.3,-0.3},{1.1,1.1,1.1});
1656

Pietro Incardona's avatar
Pietro Incardona committed
1657 1658
	periodicity<3> np({{NON_PERIODIC,NON_PERIODIC,NON_PERIODIC}});
	periodicity<3> p({{PERIODIC,PERIODIC,PERIODIC}});
Pietro Incardona's avatar
Pietro Incardona committed
1659

1660
	long int k = 128*128*128*create_vcluster().getProcessingUnits();
1661
	k = std::pow(k, 1/3.);
Pietro Incardona's avatar
Pietro Incardona committed
1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
	Test3D_domain(domain3,k,np);

	auto & v_cl = create_vcluster();
	if (v_cl.getProcessingUnits() > 32)
		return;

	// We use a 128x128x128 and we move tha domain

	for (size_t i = 0 ; i < 10 ; i++)
	{
		Box<3,float> exp({0.0,0.0,0.0},{1.3,1.3,1.3});
		domain3.enlarge(exp);
		Test3D_domain(domain3,128,p);
	}
1676 1677
}

1678 1679 1680 1681 1682
BOOST_AUTO_TEST_CASE( grid_dist_id_extended )
{
	// Domain
	Box<3,float> domain3({0.1,0.1,0.1},{1.1,1.1,1.1});

1683
	long int k = 128*128*128*create_vcluster().getProcessingUnits();