vector_dist_operators_unit_tests.cpp 25.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * vector_dist_operators_unit_tests.hpp
 *
 *  Created on: Jun 11, 2016
 *      Author: i-bird
 */

#ifndef OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_UNIT_TESTS_HPP_
#define OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_UNIT_TESTS_HPP_

incardon's avatar
incardon committed
11 12 13
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

14 15
#include "Operators/Vector/vector_dist_operators.hpp"

16 17 18 19
constexpr int A = 0;
constexpr int B = 1;
constexpr int C = 2;

20 21 22 23 24
constexpr int VA = 3;
constexpr int VB = 4;
constexpr int VC = 5;

constexpr int TA = 6;
25 26 27

//////////////////// Here we define all the function to checl the operators

Pietro Incardona's avatar
Pietro Incardona committed
28
template <unsigned int prp, typename vector> bool check_values(vector & v,float a)
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
{
	bool ret = true;
	auto it = v.getDomainIterator();

	while (it.isNext())
	{
		ret &= v.template getProp<prp>(it.get()) == a;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
45
template <typename vector> bool check_values_complex_expr(vector & vd)
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
{
	bool ret = true;
	auto it = vd.getDomainIterator();

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

		float base1 = vd.template getProp<B>(key) + 2.0 + vd.template getProp<B>(key) - 2.0*vd.template getProp<C>(key) / 5.0;
		float base2 = vd.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

67 68 69 70 71 72 73 74 75
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_pos_sum(vector & vd, const rtype & p)
{
	bool ret = true;
	auto it = vd.getDomainIterator();

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

76 77 78
		Point<vector::dims,typename vector::stype> xp = vd.getPos(key);

		rtype base1 = rtype(xp) + p;
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
		rtype base2 = vd.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_pos_sub(vector & vd, const rtype & p)
{
	bool ret = true;
	auto it = vd.getDomainIterator();

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

100 101 102
		Point<vector::dims,typename vector::stype> xp = vd.getPos(key);

		rtype base1 = rtype(xp) - p;
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
		rtype base2 = vd.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_pos_sub_minus(vector & vd, const rtype & p)
{
	bool ret = true;
	auto it = vd.getDomainIterator();

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

124 125 126
		Point<vector::dims,typename vector::stype> xp = vd.getPos(key);

		rtype base1 = -(rtype(xp) - p);
127 128 129 130 131 132 133 134 135 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
		rtype base2 = vd.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_point_sub(vector & vd, const rtype & p)
{
	bool ret = true;
	auto it = vd.getDomainIterator();

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

		rtype base1 = -vd.template getProp<B>(key);
		rtype base2 = vd.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
161
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_sum(vector & vd, double d)
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
{
	bool ret = true;
	auto it = vd.getDomainIterator();

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

		rtype base1 = vd.template getProp<B>(key) + d;
		rtype base2 = vd.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
183
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_sum(vector & vd1, vector & vd2)
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = vd1.template getProp<B>(key) + vd2.template getProp<C>(key);
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
205
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_sum_3(vector & vd1)
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = vd1.template getProp<B>(key) + vd1.template getProp<C>(key) + vd1.template getProp<B>(key);
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
227
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_sum_4(vector & vd1)
228 229 230 231 232 233 234 235
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

236
		rtype base1 = (vd1.template getProp<B>(key) + vd1.template getProp<C>(key)) + (vd1.template getProp<B>(key) + vd1.template getProp<C>(key));
237 238
		rtype base2 = vd1.template getProp<A>(key);

239

240 241 242 243 244 245 246 247 248 249
		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
250
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_sub(vector & vd, double d)
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
{
	bool ret = true;
	auto it = vd.getDomainIterator();

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

		rtype base1 = vd.template getProp<B>(key) - d;
		rtype base2 = vd.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
272
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_sub(double d, vector & vd)
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
{
	bool ret = true;
	auto it = vd.getDomainIterator();

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

		rtype base1 = d - vd.template getProp<B>(key);
		rtype base2 = vd.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
294
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_sub(vector & vd1, vector & vd2)
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = vd1.template getProp<C>(key) - vd2.template getProp<B>(key);
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
316
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_sub_31(vector & vd1)
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = vd1.template getProp<B>(key) - (vd1.template getProp<C>(key) + vd1.template getProp<B>(key));
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}


Pietro Incardona's avatar
Pietro Incardona committed
339
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_sub_32(vector & vd1)
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = (vd1.template getProp<C>(key) + vd1.template getProp<B>(key)) - vd1.template getProp<B>(key);
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
361
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_sub_4(vector & vd1)
362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = (vd1.template getProp<C>(key) + vd1.template getProp<B>(key)) - (vd1.template getProp<C>(key) + vd1.template getProp<B>(key));
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}


Pietro Incardona's avatar
Pietro Incardona committed
384
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_mul(vector & vd, double d)
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
{
	bool ret = true;
	auto it = vd.getDomainIterator();

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

		rtype base1 = vd.template getProp<B>(key) * d;
		rtype base2 = vd.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}


Pietro Incardona's avatar
Pietro Incardona committed
407
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_mul(vector & vd1, vector & vd2)
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = vd1.template getProp<C>(key) * vd2.template getProp<B>(key);
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
429
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_mul_3(vector & vd1)
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = vd1.template getProp<B>(key) * (vd1.template getProp<B>(key) + vd1.template getProp<C>(key));
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}


Pietro Incardona's avatar
Pietro Incardona committed
452
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_mul_4(vector & vd1)
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = (vd1.template getProp<B>(key) + vd1.template getProp<C>(key)) * (vd1.template getProp<B>(key) + vd1.template getProp<C>(key));
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}



Pietro Incardona's avatar
Pietro Incardona committed
476
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_div(vector & vd, double d)
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
{
	bool ret = true;
	auto it = vd.getDomainIterator();

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

		rtype base1 = vd.template getProp<B>(key) / d;
		rtype base2 = vd.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
498
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_div(double d, vector & vd)
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519
{
	bool ret = true;
	auto it = vd.getDomainIterator();

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

		rtype base1 = d / vd.template getProp<B>(key);
		rtype base2 = vd.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
520
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_div(vector & vd1, vector & vd2)
521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = vd1.template getProp<C>(key) / vd2.template getProp<B>(key);
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
542
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_div_31(vector & vd1)
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = vd1.template getProp<B>(key) / (vd1.template getProp<B>(key) + vd1.template getProp<C>(key));
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
564
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_div_32(vector & vd1)
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = (vd1.template getProp<C>(key) + vd1.template getProp<B>(key)) / vd1.template getProp<B>(key);
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}


Pietro Incardona's avatar
Pietro Incardona committed
587
template <typename rtype, typename vector, unsigned int A, unsigned int B, unsigned int C> bool check_values_div_4(vector & vd1)
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
{
	bool ret = true;
	auto it = vd1.getDomainIterator();

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

		rtype base1 = (vd1.template getProp<B>(key) + vd1.template getProp<C>(key)) / (vd1.template getProp<B>(key) + vd1.template getProp<C>(key));
		rtype base2 = vd1.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

template <typename vector> bool check_values_scal_norm_dist(vector & vd)
{
	bool ret = true;
	auto it = vd.getDomainIterator();

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

618
		float base1 = vd.template getProp<VB>(key) * vd.template getProp<VC>(key) + norm(vd.template getProp<VC>(key) + vd.template getProp<VB>(key)) + distance(vd.template getProp<VC>(key),vd.template getProp<VB>(key));
619 620 621 622 623 624 625 626 627 628 629 630
		float base2 = vd.template getProp<A>(key);

		ret &=  base1 == base2;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);

	return ret;
}

Pietro Incardona's avatar
Pietro Incardona committed
631
template <typename vector> void fill_values(vector & v)
632 633 634 635 636 637 638
{
	auto it = v.getDomainIterator();

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

639 640 641 642
		v.getPos(p)[0] = (float)rand() / RAND_MAX;
		v.getPos(p)[1] = (float)rand() / RAND_MAX;
		v.getPos(p)[2] = (float)rand() / RAND_MAX;

Pietro Incardona's avatar
Pietro Incardona committed
643 644 645
		v.template getProp<A>(p) = fabs(sin(p.getKey()+1.0));
		v.template getProp<B>(p) = fabs(sin(2.0*p.getKey()+3.0));
		v.template getProp<C>(p) = fabs(sin(3.0*p.getKey()+18.0));
646 647 648

		for (size_t k = 0 ; k < 3 ; k++)
		{
Pietro Incardona's avatar
Pietro Incardona committed
649 650 651
			v.template getProp<VA>(p)[k] = fabs(sin(p.getKey()+1.0+k));
			v.template getProp<VB>(p)[k] = fabs(sin(2.0*p.getKey()+1.0+3.0));
			v.template getProp<VC>(p)[k] = fabs(sin(3.0*p.getKey()+1.0+k));
652 653 654 655 656 657
		}

		++it;
	}
}

658 659

typedef vector_dist<3,float,aggregate<float,float,float,VectorS<3,float>,VectorS<3,float>,VectorS<3,float>,float>> vector_type;
660

661 662 663
//! Exponential kernel
struct exp_kernel
{
664
	//! variance of the exponential kernel
665 666
	float var;

667
	//! Exponential kernel giving variance
668 669 670 671
	exp_kernel(float var)
	:var(var)
	{}

672 673 674 675 676 677 678 679 680 681
	/*! \brief Result of the exponential kernel
	 *
	 * \param p position of the particle p
	 * \param q position of the particle q
	 * \param pA property value at p
	 * \param pB property value at q
	 *
	 * \return the result
	 *
	 */
682 683 684 685 686 687 688
	inline float value(const Point<3,float> & p, const Point<3,float> & q,float pA,float pB)
	{
		float dist = norm(p-q);

		return (pA + pB) * exp(dist * dist / var);
	}

689 690 691 692 693 694 695 696 697 698
	/*! \brief Result of the exponential kernel
	 *
	 * \param p position of the particle p
	 * \param q position of the particle q
	 * \param pA property value at p
	 * \param pB property value at q
	 *
	 * \return the result
	 *
	 */
699
	inline Point<3,float> value(const Point<3,float> & p, const Point<3,float> & q,const Point<3,float> & pA, const Point<3,float> & pB)
700 701 702
	{
		float dist = norm(p-q);

703 704 705
		return (pA + pB) * exp(dist * dist / var);
	}

706 707 708 709 710 711 712 713 714 715 716
	/*! \brief Result of the exponential kernel
	 *
	 * \param p position of the particle p
	 * \param q position of the particle q
	 * \param pA property value at p
	 * \param pB property value at q
	 * \param vd1 original vector
	 *
	 * \return the result
	 *
	 */
717
	inline float value(size_t p, size_t q, float pA, float pB, const vector_type & vd1)
718
	{
719 720 721 722
		Point<3,float> pp = vd1.getPos(p);
		Point<3,float> pq = vd1.getPos(q);

		float dist = norm(pp-pq);
723 724

		return (pA + pB) * exp(dist * dist / var);
725
	}
726

727 728 729 730 731 732 733 734 735 736 737
	/*! \brief Result of the exponential kernel
	 *
	 * \param p position of the particle p
	 * \param q position of the particle q
	 * \param pA property value at p
	 * \param pB property value at q
	 * \param vd1 original vector
	 *
	 * \return the result
	 *
	 */
738 739 740 741 742 743 744 745 746 747
	inline Point<3,float> value(size_t p, size_t q, const Point<3,float> & pA, const Point<3,float> & pB , const vector_type & vd1)
	{
		Point<3,float> pp = vd1.getPos(p);
		Point<3,float> pq = vd1.getPos(q);

		float dist = norm(pp-pq);

		return (pA + pB) * exp(dist * dist / var);
	}

748 749 750 751 752 753 754 755
	/*! \brief Result of the exponential kernel
	 *
	 * \param p position of the particle p
	 * \param q position of the particle q
	 *
	 * \return the result
	 *
	 */
756 757 758 759 760 761
	inline Point<2,float> value(const Point<3,float> & p, const Point<3,float> & q)
	{
		float dist = norm(p-q);

		return exp(dist * dist / var);
	}
762 763 764 765
};

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

766
BOOST_AUTO_TEST_SUITE( vector_dist_operators_test )
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786


BOOST_AUTO_TEST_CASE( vector_dist_operators_test )
{
	if (create_vcluster().getProcessingUnits() > 3)
		return;

	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};

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

	// vector type
	typedef vector_dist<3,float,aggregate<float,float,float,VectorS<3,float>,VectorS<3,float>,VectorS<3,float>>> vtype;

	vector_dist<3,float,aggregate<float,float,float,VectorS<3,float>,VectorS<3,float>,VectorS<3,float>>> vd(100,box,bc,ghost);

787 788 789 790 791 792 793 794
	auto vA = getV<A>(vd);
	auto vB = getV<B>(vd);
	auto vC = getV<C>(vd);

	auto vVA = getV<VA>(vd);
	auto vVB = getV<VB>(vd);
	auto vVC = getV<VC>(vd);

795 796
	auto vPOS = getV<PROP_POS>(vd);

797 798 799
	vA = 1.0;
	vB = 2.0f;
	vC = 3.0;
800 801 802 803 804

	check_values<A>(vd,1.0);
	check_values<B>(vd,2.0);
	check_values<C>(vd,3.0);

805 806 807
	vA = vB;
	check_values<A>(vd,2.0);

808 809
	fill_values(vd);

810
	vA = vB + 2.0 + vB - 2.0*vC / 5.0;
811 812 813 814
	check_values_complex_expr(vd);

	// Various combination of 2 operator

815
	vA = vB + 2.0;
816
	check_values_sum<float,vtype,A,B,C>(vd,2.0);
817
	vA = 2.0 + vB;
818
	check_values_sum<float,vtype,A,B,C>(vd,2.0);
819
	vA = vC + vB;
820 821
	check_values_sum<float,vtype,A,B,C>(vd,vd);

822
	vA = vB - 2.0;
823
	check_values_sub<float,vtype,A,B,C>(vd,2.0);
824
	vA = 2.0 - vB;
825
	check_values_sub<float,vtype,A,B,C>(2.0,vd);
826
	vA = vC - vB;
827 828
	check_values_sub<float,vtype,A,B,C>(vd,vd);

829
	vA = vB * 2.0;
830
	check_values_mul<float,vtype,A,B,C>(vd,2.0);
831
	vA = 2.0 * vB;
832
	check_values_mul<float,vtype,A,B,C>(vd,2.0);
833
	vA = vC * vB;
834 835
	check_values_mul<float,vtype,A,B,C>(vd,vd);

836
	vA = vB / 2.0;
837
	check_values_div<float,vtype,A,B,C>(vd,2.0);
838
	vA = 2.0 / vB;
839
	check_values_div<float,vtype,A,B,C>(2.0,vd);
840
	vA = vC / vB;
841 842 843 844
	check_values_div<float,vtype,A,B,C>(vd,vd);

	// Variuos combination 3 operator

845
	vA = vB + (vC + vB);
846
	check_values_sum_3<float,vtype,A,B,C>(vd);
847
	vA = (vC + vB) + vB;
848
	check_values_sum_3<float,vtype,A,B,C>(vd);
849
	vA = (vC + vB) + (vC + vB);
850 851
	check_values_sum_4<float,vtype,A,B,C>(vd);

852
	vA = vB - (vC + vB);
853
	check_values_sub_31<float,vtype,A,B,C>(vd);
854
	vA = (vC + vB) - vB;
855
	check_values_sub_32<float,vtype,A,B,C>(vd);
856
	vA = (vC + vB) - (vC + vB);
857 858
	check_values_sub_4<float,vtype,A,B,C>(vd);

859
	vA = vB * (vC + vB);
860
	check_values_mul_3<float,vtype,A,B,C>(vd);
861
	vA = (vC + vB) * vB;
862
	check_values_mul_3<float,vtype,A,B,C>(vd);
863
	vA = (vC + vB) * (vC + vB);
864 865
	check_values_mul_4<float,vtype,A,B,C>(vd);

866
	vA = vB / (vC + vB);
867
	check_values_div_31<float,vtype,A,B,C>(vd);
868
	vA = (vC + vB) / vB;
869
	check_values_div_32<float,vtype,A,B,C>(vd);
870
	vA = (vC + vB) / (vC + vB);
871 872 873 874 875 876
	check_values_div_4<float,vtype,A,B,C>(vd);

	// We try with vectors

	// Various combination of 2 operator

877
	vVA = vVB + 2.0;
878
	check_values_sum<VectorS<3,float>,vtype,VA,VB,VC>(vd,2.0f);
879
	vVA = 2.0 + vVB;
880 881 882 883
	check_values_sum<VectorS<3,float>,vtype,VA,VB,VC>(vd,2.0f);
	vVA = vVC + vVB;
	check_values_sum<VectorS<3,float>,vtype,VA,VB,VC>(vd,vd);

884
	vVA = vVB - 2.0;
885
	check_values_sub<VectorS<3,float>,vtype,VA,VB,VC>(vd,2.0f);
886
	vVA = 2.0 - vVB;
887 888 889 890
	check_values_sub<VectorS<3,float>,vtype,VA,VB,VC>(2.0f,vd);
	vVA = vVC - vVB;
	check_values_sub<VectorS<3,float>,vtype,VA,VB,VC>(vd,vd);

891
	vVA = vVB * 2.0;
892
	check_values_mul<VectorS<3,float>,vtype,VA,VB,VC>(vd,2.0f);
893
	vVA = 2.0 * vVB;
894 895 896 897
	check_values_mul<VectorS<3,float>,vtype,VA,VB,VC>(vd,2.0f);
	vVA = vVC * vVB;
	check_values_mul<VectorS<3,float>,vtype,VA,VB,VC>(vd,vd);

898
	vVA = vVB / 2.0;
899
	check_values_div<VectorS<3,float>,vtype,VA,VB,VC>(vd,2.0f);
900
	vVA = 2.0 / vVB;
901 902 903
	check_values_div<VectorS<3,float>,vtype,VA,VB,VC>(2.0f,vd);
	vVA = vVC / vVB;
	check_values_div<VectorS<3,float>,vtype,VA,VB,VC>(vd,vd);
904 905 906

	// Variuos combination 3 operator

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 936 937 938 939
	vVA = vVB + (vVC + vVB);
	check_values_sum_3<VectorS<3,float>,vtype,VA,VB,VC>(vd);
	vVA = (vVC + vVB) + vVB;
	check_values_sum_3<VectorS<3,float>,vtype,VA,VB,VC>(vd);
	vVA = (vVC + vVB) + (vVC + vVB);
	check_values_sum_4<VectorS<3,float>,vtype,VA,VB,VC>(vd);

	vVA = vVB - (vVC + vVB);
	check_values_sub_31<VectorS<3,float>,vtype,VA,VB,VC>(vd);
	vVA = (vVC + vVB) - vVB;
	check_values_sub_32<VectorS<3,float>,vtype,VA,VB,VC>(vd);
	vVA = (vVC + vVB) - (vVC + vVB);
	check_values_sub_4<VectorS<3,float>,vtype,VA,VB,VC>(vd);

	vVA = vVB * (vVC + vVB);
	check_values_mul_3<VectorS<3,float>,vtype,VA,VB,VC>(vd);
	vVA = (vVC + vVB) * vVB;
	check_values_mul_3<VectorS<3,float>,vtype,VA,VB,VC>(vd);
	vVA = (vVC + vVB) * (vVC + vVB);
	check_values_mul_4<VectorS<3,float>,vtype,VA,VB,VC>(vd);
	vA = vVB * (vVC + vVB);
	check_values_mul_3<float,vtype,A,VB,VC>(vd);
	vA = (vVC + vVB) * vVB;
	check_values_mul_3<float,vtype,A,VB,VC>(vd);
	vA = (vVC + vVB) * (vVC + vVB);
	check_values_mul_4<float,vtype,A,VB,VC>(vd);

	vVA = vVB / (vVC + vVB);
	check_values_div_31<VectorS<3,float>,vtype,VA,VB,VC>(vd);
	vVA = (vVC + vVB) / vVB;
	check_values_div_32<VectorS<3,float>,vtype,VA,VB,VC>(vd);
	vVA = (vVC + vVB) / (vVC + vVB);
	check_values_div_4<VectorS<3,float>,vtype,VA,VB,VC>(vd);
940 941 942

	// normalization function

incardon's avatar
incardon committed
943
	vA = vVB * vVC + norm(vVC + vVB) + openfpm::distance(vVC,vVB);
944
	check_values_scal_norm_dist(vd);
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966

	Point<3,float> p0({2.0,2.0,2.0});
	auto p0_e = getVExpr(p0);

	vVA = vPOS + p0_e;
	check_values_pos_sum<VectorS<3,float>,vtype,VA,VB,VC>(vd,p0);

	vVA = vPOS - p0_e;
	check_values_pos_sub<Point<3,float>,vtype,VA,VB,VC>(vd,p0);

	vVA = -(vPOS - p0_e);
	check_values_pos_sub_minus<Point<3,float>,vtype,VA,VB,VC>(vd,p0);

	vVA = -vVB;
	check_values_point_sub<Point<3,float>,vtype,VA,VB,VC>(vd,p0);

	// Just check it compile testing it will test the same code
	// as the previuous one
	vVC = exp(vVB);
	vA = norm(vPOS);
	vVA = vPOS + 2.0;
	vVA = 2.0 + vPOS;
967
	vVA = vPOS + vPOS;
968 969
	vVA = vPOS - 2.0f;
	vVA = 2.0 - vPOS;
970
	vVA = vPOS - vPOS;
971

972
	vVA = vPOS * 2.0;
973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
	vVA = 2.0 * vPOS;
	vVA = vPOS * vPOS;

	vVA = vPOS / 2.0f;
	vVA = 2.0f / vPOS;
	vVA = vPOS / vPOS;

	// Variuos combination 3 operator

	vVA = vPOS + (vPOS + vPOS);
	vVA = (vPOS + vPOS) + vPOS;
	vVA = (vPOS + vPOS) + (vPOS + vPOS);

	vVA = vPOS - (vPOS + vPOS);
	vVA = (vPOS + vPOS) - vPOS;
	vVA = (vVC + vPOS) - (vPOS + vPOS);

	vVA = vPOS * (vPOS + vPOS);
	vVA = (vPOS + vPOS) * vPOS;
	vVA = (vPOS + vPOS) * (vPOS + vPOS);
	vA = vPOS * (vPOS + vPOS);
	vA = (vPOS + vPOS) * vPOS;
	vA = (vPOS + vPOS) * (vPOS + vPOS);

	vVA = vPOS / (vPOS + vPOS);
	vVA = (vPOS + vPOS) / vPOS;
999
	vVA = (vPOS + vPOS) / (vPOS + vPOS);
1000 1001
}

1002
#include "vector_dist_operators_apply_kernel_unit_tests.hpp"
1003

1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 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 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173
void reset(vector_dist<3,float,aggregate<float,float,float,float,float,float,float,float>> & v1)
{
	auto it = v1.getDomainIterator();

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

		v1.template getProp<0>(p) = -1.0;
		v1.template getProp<1>(p) = -1.0;
		v1.template getProp<2>(p) = -1.0;
		v1.template getProp<3>(p) = -1.0;
		v1.template getProp<4>(p) = -1.0;
		v1.template getProp<5>(p) = -1.0;
		v1.template getProp<6>(p) = -1.0;
		v1.template getProp<7>(p) = -1.0;

		++it;
	}
}

template<unsigned int i> void loop_check(vector_dist<3,float,aggregate<float,float,float,float,float,float,float,float>> & v1, float check)
{
	auto it = v1.getDomainIterator();
	bool ret = true;

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

		ret &= v1.template getProp<i>(p) == check;

		++it;
	}

	BOOST_REQUIRE_EQUAL(ret,true);
}

void check(vector_dist<3,float,aggregate<float,float,float,float,float,float,float,float>> & v1, size_t i)
{
	float check = -1.0;

	if (0 < i) check = 0.0;
	else check = -1.0;
	loop_check<0>(v1,check);

	if (1 < i) check = 1.0;
	else check = -1.0;
	loop_check<1>(v1,check);

	if (2 < i) check = 2.0;
	else check = -1.0;
	loop_check<2>(v1,check);

	if (3 < i) check = 3.0;
	else check = -1.0;
	loop_check<3>(v1,check);

	if (4 < i) check = 4.0;
	else check = -1.0;
	loop_check<4>(v1,check);

	if (5 < i) check = 5.0;
	else check = -1.0;
	loop_check<5>(v1,check);

	if (6 < i) check = 6.0;
	else check = -1.0;
	loop_check<6>(v1,check);

	if (7 < i) check = 7.0;
	else check = -1.0;
	loop_check<7>(v1,check);
}

BOOST_AUTO_TEST_CASE( vector_dist_operators_assign_test )
{
	if (create_vcluster().getProcessingUnits() > 3)
		return;

	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};

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

	vector_dist<3,float,aggregate<float,float,float,float,float,float,float,float>> vd(100,box,bc,ghost);

	auto v1 = getV<0>(vd);
	auto v2 = getV<1>(vd);
	auto v3 = getV<2>(vd);
	auto v4 = getV<3>(vd);
	auto v5 = getV<4>(vd);
	auto v6 = getV<5>(vd);
	auto v7 = getV<6>(vd);
	auto v8 = getV<7>(vd);

	reset(vd);

	assign(v1,0.0,
		   v2,1.0);

	check(vd,2);

	reset(vd);

	assign(v1,0.0,
		   v2,1.0,
		   v3,2.0);

	check(vd,3);

	reset(vd);

	assign(v1,0.0,
		   v2,1.0,
		   v3,2.0,
		   v4,3.0);

	check(vd,4);

	reset(vd);

	assign(v1,0.0,
		   v2,1.0,
		   v3,2.0,
		   v4,3.0,
		   v5,4.0);

	check(vd,5);

	reset(vd);

	assign(v1,0.0,
		   v2,1.0,
		   v3,2.0,
		   v4,3.0,
		   v5,4.0,
		   v6,5.0);

	check(vd,6);

	reset(vd);

	assign(v1,0.0,
		   v2,1.0,
		   v3,2.0,
		   v4,3.0,
		   v5,4.0,
		   v6,5.0,
		   v7,6.0);

	check(vd,7);

	reset(vd);

	assign(v1,0.0,
		   v2,1.0,
		   v3,2.0,
		   v4,3.0,
		   v5,4.0,
		   v6,5.0,
		   v7,6.0,
		   v8,7.0);

	check(vd,8);
}

1174
BOOST_AUTO_TEST_SUITE_END()
1175 1176 1177


#endif /* OPENFPM_NUMERICS_SRC_OPERATORS_VECTOR_VECTOR_DIST_OPERATORS_UNIT_TESTS_HPP_ */