8 #ifndef OPENFPM_NUMERICS_SRC_INTERPOLATION_INTERPOLATION_UNIT_TESTS_HPP_ 9 #define OPENFPM_NUMERICS_SRC_INTERPOLATION_INTERPOLATION_UNIT_TESTS_HPP_ 11 #define BOOST_TEST_DYN_LINK 12 #include <boost/test/unit_test.hpp> 14 #include "interpolation/mp4_kernel.hpp" 15 #include "interpolation/lambda_kernel.hpp" 16 #include "interpolation/z_spline.hpp" 17 #include "interpolation.hpp" 18 #include <boost/math/special_functions/pow.hpp> 19 #include <Vector/vector_dist.hpp> 20 #include <Grid/grid_dist_id.hpp> 22 BOOST_AUTO_TEST_SUITE( interpolation_test )
24 template<
typename gr
id,
unsigned int mom_p>
void momenta_grid(
grid & gd,
typename grid::stype (& mom_tot)[grid::dims])
26 auto it = gd.getDomainGhostIterator();
28 for (
size_t i = 0 ; i < grid::dims ; i++)
34 auto key_g = gd.getGKey(key);
36 for (
size_t i = 0 ; i < grid::dims ; i++)
38 typename grid::stype coord = gd.spacing(i)*key_g.get(i);
40 mom_tot[i] += boost::math::pow<mom_p>(coord)*gd.template getProp<0>(key);
47 template<
typename gr
id,
unsigned int mom_p>
void momenta_grid_domain(
grid & gd,
typename grid::stype (& mom_tot)[grid::dims])
49 auto it = gd.getDomainIterator();
51 for (
size_t i = 0 ; i < grid::dims ; i++)
57 auto key_g = gd.getGKey(key);
59 for (
size_t i = 0 ; i < grid::dims ; i++)
61 typename grid::stype coord = gd.spacing(i)*key_g.get(i);
63 mom_tot[i] += boost::math::pow<mom_p>(coord)*gd.template getProp<0>(key);
70 template<
typename vector,
unsigned int mom_p>
void momenta_vector(vector & vd,
typename vector::stype (& mom_tot)[vector::dims])
72 auto it = vd.getDomainIterator();
74 for (
size_t i = 0 ; i < vector::dims ; i++)
81 for (
size_t i = 0 ; i < vector::dims ; i++)
83 typename vector::stype coord = vd.getPos(key)[i];
85 mom_tot[i] += boost::math::pow<mom_p>(coord)*vd.template getProp<0>(key);
92 template<
unsigned int dim,
typename T,
typename Kernel,
typename gr
id,
typename vector>
93 void interp_test(
grid & gd, vector & vd,
bool single_particle,
unsigned int numberOfMomenta)
97 auto it2 = gd.getDomainGhostIterator();
101 auto key = it2.get();
103 gd.template get<0>(key) = 0.0;
110 if (single_particle ==
false)
111 {inte.template p2m<0,0>(vd,gd);}
114 auto it = vd.getDomainIterator();
120 inte.template p2m<0,0>(vd,gd,p);
128 vd.write(
"Particles");
131 if(numberOfMomenta>=0){
132 momenta_grid<grid,0>(gd,mg);
133 momenta_vector<vector,0>(vd,mv);
134 for (
size_t i = 0 ; i < dim ; i++)
135 {BOOST_REQUIRE_CLOSE(mg[i],mv[i],0.001);}
138 if(numberOfMomenta>=1){
139 momenta_grid<grid,1>(gd,mg);
140 momenta_vector<vector,1>(vd,mv);
141 for (
size_t i = 0 ; i < dim ; i++)
142 {BOOST_REQUIRE_CLOSE(mg[i],mv[i],0.001);}
145 if(numberOfMomenta>=2){
146 momenta_grid<grid,2>(gd,mg);
147 momenta_vector<vector,2>(vd,mv);
148 for (
size_t i = 0 ; i < dim ; i++)
149 {BOOST_REQUIRE_CLOSE(mg[i],mv[i],0.001);}
152 if(numberOfMomenta>=3){
153 momenta_grid<grid,3>(gd,mg);
154 momenta_vector<vector,3>(vd,mv);
155 for (
size_t i = 0 ; i < dim ; i++)
156 {BOOST_REQUIRE_CLOSE(mg[i],mv[i],0.001);}
159 if(numberOfMomenta>=4){
160 momenta_grid<grid,4>(gd,mg);
161 momenta_vector<vector,4>(vd,mv);
162 for (
size_t i = 0 ; i < dim ; i++)
163 {BOOST_REQUIRE_CLOSE(mg[i],mv[i],0.001);}
169 BOOST_AUTO_TEST_CASE( interpolation_full_single_test_2D )
172 size_t sz[2] = {64,64};
177 size_t bc_v[2] = {PERIODIC,PERIODIC};
184 auto it = vd.getDomainIterator();
190 vd.getPos(p)[0] = (double)rand()/RAND_MAX;
191 vd.getPos(p)[1] = (double)rand()/RAND_MAX;
193 vd.getProp<0>(p) = 5.0;
200 interp_test<2,float,mp4_kernel<float>>(gd,vd,
true,2);
202 BOOST_AUTO_TEST_CASE( interpolation_full_single_test_2D_double )
205 size_t sz[2] = {64,64};
210 size_t bc_v[2] = {PERIODIC,PERIODIC};
223 vd.getPos(p)[0] = (double)rand()/RAND_MAX;
224 vd.getPos(p)[1] = (double)rand()/RAND_MAX;
226 vd.getProp<0>(p) = 5.0;
233 interp_test<2,double,lambda4_4kernel<double>>(gd,vd,
true,2);
237 BOOST_AUTO_TEST_CASE( interpolation_full_test_2D )
240 size_t sz[2] = {64,64};
245 size_t bc_v[2] = {PERIODIC,PERIODIC};
259 vd.getPos(p)[0] = (double)rand()/RAND_MAX;
260 vd.getPos(p)[1] = (double)rand()/RAND_MAX;
262 vd.getProp<0>(p) = 5.0;
270 interp_test<2,float,mp4_kernel<float>>(gd,vd,
false,2);
275 auto & v_cl = create_vcluster();
285 auto it4 = vd.getGridIterator(sz);
289 auto key = it4.get();
293 vd.getLastPos()[0] = key.get(0) * it4.getSpacing(0) + domain.getLow(0) + 0.1*it4.getSpacing(0);
294 vd.getLastPos()[1] = key.get(1) * it4.getSpacing(1) + domain.getLow(1) + 0.1*it4.getSpacing(1);
296 vd.getLastProp<0>() = 0.0;
303 auto it5 = gd.getDomainGhostIterator();
307 auto key = it5.get();
309 gd.get<0>(key) = 0.0;
318 auto it6 = gd.getSubDomainIterator(start,stop);
321 auto key = it6.
get();
323 gd.get<0>(key) = 5.0;
331 inte.m2p<0,0>(gd,vd);
333 momenta_grid_domain<decltype(gd),0>(gd,mg);
334 momenta_vector<decltype(vd),0>(vd,mv);
342 BOOST_REQUIRE_CLOSE(mg[0],mv[0],0.001);
343 BOOST_REQUIRE_CLOSE(mg[1],mv[1],0.001);
345 momenta_grid_domain<decltype(gd),1>(gd,mg);
346 momenta_vector<decltype(vd),1>(vd,mv);
354 BOOST_REQUIRE_CLOSE(mg[0],mv[0],0.001);
355 BOOST_REQUIRE_CLOSE(mg[1],mv[1],0.001);
357 momenta_grid_domain<decltype(gd),2>(gd,mg);
358 momenta_vector<decltype(vd),2>(vd,mv);
366 BOOST_REQUIRE_CLOSE(mg[0],mv[0],0.001);
367 BOOST_REQUIRE_CLOSE(mg[1],mv[1],0.001);
372 BOOST_AUTO_TEST_CASE( interpolation_full_single_test_3D )
375 size_t sz[3] = {64,64,64};
380 size_t bc_v[3] = {PERIODIC,PERIODIC,PERIODIC};
394 vd.getPos(p)[0] = (double)rand()/RAND_MAX;
396 vd.getPos(p)[1] = (double)rand()/RAND_MAX;
398 vd.getPos(p)[2] = (double)rand()/RAND_MAX;
400 vd.getProp<0>(p) = 5.0;
408 interp_test<3,double,mp4_kernel<float>>(gd,vd,
true,2);
411 BOOST_AUTO_TEST_CASE( interpolation_getSubCheck )
414 size_t sz[3] = {64,64,64};
419 size_t bc_v[3] = {NON_PERIODIC,NON_PERIODIC,NON_PERIODIC};
424 vector vd(0,domain,bc_v,gv);
431 auto & dec = vd.getDecomposition();
433 int nl = dec.getNLocalSub();
435 for (
int i = 0 ; i < nl ; i++)
437 int nll = dec.getLocalNIGhost(i);
438 for (
int j = 0 ; j < nll ; j++)
440 auto ibx = dec.getLocalIGhostBox(i,j);
441 int x = dec.getLocalIGhostSub(i,j);
442 auto bx = dec.getSubDomain(x);
445 for (
int s = 0; s < 3 ; s++)
448 for (
int s1 = 0; s1 < 3 ; s1++)
450 p.
get(s1) = (ibx.getHigh(s1) - ibx.getLow(s1)) / 2.0 + ibx.getLow(s1);
453 if (ibx.getLow(s) == bx.getHigh(s))
455 p.
get(s) = ibx.getLow(s);
456 int sub = inte.getSub(p);
457 BOOST_REQUIRE_EQUAL(sub,i);
459 else if (ibx.getHigh(s) == bx.getLow(s))
461 p.
get(s) = ibx.getHigh(s);
462 int sub = inte.getSub(p);
463 BOOST_REQUIRE_EQUAL(sub,x);
470 BOOST_AUTO_TEST_CASE( interpolation_full_test_3D )
473 size_t sz[3] = {64,64,64};
478 size_t bc_v[3] = {PERIODIC,PERIODIC,PERIODIC};
492 vd.getPos(p)[0] = (double)rand()/RAND_MAX;
493 vd.getPos(p)[1] = (double)rand()/RAND_MAX;
494 vd.getPos(p)[2] = (double)rand()/RAND_MAX;
496 vd.getProp<0>(p) = 5.0;
506 interp_test<3,double,mp4_kernel<float>>(gd,vd,
false,2);
508 auto & v_cl = create_vcluster();
519 auto it4 = vd.getGridIterator(sz);
523 auto key = it4.get();
527 vd.getLastPos()[0] = key.get(0) * it4.getSpacing(0) + domain.getLow(0) + 0.1*it4.getSpacing(0);
528 vd.getLastPos()[1] = key.get(1) * it4.getSpacing(1) + domain.getLow(1) + 0.1*it4.getSpacing(1);
529 vd.getLastPos()[2] = key.get(2) * it4.getSpacing(2) + domain.getLow(2) + 0.1*it4.getSpacing(2);
531 vd.getLastProp<0>() = 0.0;
538 auto it5 = gd.getDomainGhostIterator();
542 auto key = it5.get();
544 gd.get<0>(key) = 0.0;
553 auto it6 = gd.getSubDomainIterator(start,stop);
556 auto key = it6.
get();
558 gd.get<0>(key) = 5.0;
566 inte.m2p<0,0>(gd,vd);
568 momenta_grid_domain<decltype(gd),0>(gd,mg);
569 momenta_vector<decltype(vd),0>(vd,mv);
579 BOOST_REQUIRE_CLOSE(mg[0],mv[0],0.001);
580 BOOST_REQUIRE_CLOSE(mg[1],mv[1],0.001);
581 BOOST_REQUIRE_CLOSE(mg[2],mv[2],0.001);
583 momenta_grid_domain<decltype(gd),1>(gd,mg);
584 momenta_vector<decltype(vd),1>(vd,mv);
594 BOOST_REQUIRE_CLOSE(mg[0],mv[0],0.001);
595 BOOST_REQUIRE_CLOSE(mg[1],mv[1],0.001);
596 BOOST_REQUIRE_CLOSE(mg[2],mv[2],0.001);
598 momenta_grid_domain<decltype(gd),2>(gd,mg);
599 momenta_vector<decltype(vd),2>(vd,mv);
609 BOOST_REQUIRE_CLOSE(mg[0],mv[0],0.001);
610 BOOST_REQUIRE_CLOSE(mg[1],mv[1],0.001);
611 BOOST_REQUIRE_CLOSE(mg[2],mv[2],0.001);
616 BOOST_AUTO_TEST_CASE( int_kernel_test )
624 tot += mp4.value(-1.3f,0);
625 tot += mp4.value(-0.3f,1);
626 tot += mp4.value(0.7f,2);
627 tot += mp4.value(1.7f,3);
629 BOOST_REQUIRE_CLOSE(tot,1.0f,0.001);
635 tot += -1.3f*mp4.value(-1.3f,0);
636 tot += -0.3f*mp4.value(-0.3f,1);
637 tot += 0.7f*mp4.value(0.7f,2);
638 tot += 1.7f*mp4.value(1.7f,3);
640 BOOST_REQUIRE_SMALL(tot,0.001f);
646 tot += (1.3f)*(1.3f)*mp4.value(-1.3f,0);
647 tot += (0.3f)*(0.3f)*mp4.value(-0.3f,1);
648 tot += (0.7f)*(0.7f)*mp4.value(0.7f,2);
649 tot += (1.7f)*(1.7f)*mp4.value(1.7f,3);
651 BOOST_REQUIRE_SMALL(tot,0.001f);
660 tot += zk1.value(-0.3f,0);
661 tot += zk1.value(0.7f,1);
663 BOOST_REQUIRE_CLOSE(tot,1.0f,0.001);
675 tot += zk3.value(-2.3f,0);
676 tot += zk3.value(-1.3f,1);
677 tot += zk3.value(-0.3f,2);
678 tot += zk3.value(0.7f,3);
679 tot += zk3.value(1.7f,4);
680 tot += zk3.value(2.7f,5);
682 BOOST_REQUIRE_CLOSE(tot,1.0f,0.001);
688 tot += -2.3*zk3.value(-2.3f,0);
689 tot += -1.3*zk3.value(-1.3f,1);
690 tot += -0.3*zk3.value(-0.3f,2);
691 tot += 0.7*zk3.value(0.7f,3);
692 tot += 1.7*zk3.value(1.7f,4);
693 tot += 2.7*zk3.value(2.7f,5);
695 BOOST_REQUIRE_SMALL(tot,0.001f);
701 tot += 2.3*2.3*zk3.value(-2.3f,0);
702 tot += 1.3*1.3*zk3.value(-1.3f,1);
703 tot += 0.3*0.3*zk3.value(-0.3f,2);
704 tot += 0.7*0.7*zk3.value(0.7f,3);
705 tot += 1.7*1.7*zk3.value(1.7f,4);
706 tot += 2.7*2.7*zk3.value(2.7f,5);
708 BOOST_REQUIRE_SMALL(tot,0.001f);
714 tot += -2.3*-2.3*-2.3*zk3.value(-2.3f,0);
715 tot += -1.3*-1.3*-1.3*zk3.value(-1.3f,1);
716 tot += -0.3*-0.3*-0.3*zk3.value(-0.3f,2);
717 tot += 0.7*0.7*0.7*zk3.value(0.7f,3);
718 tot += 1.7*1.7*1.7*zk3.value(1.7f,4);
719 tot += 2.7*2.7*2.7*zk3.value(2.7f,5);
721 BOOST_REQUIRE_SMALL(tot,0.001f);
732 tot += zk4.value(-3.3f,0);
733 tot += zk4.value(-2.3f,1);
734 tot += zk4.value(-1.3f,2);
735 tot += zk4.value(-0.3f,3);
736 tot += zk4.value(0.7f,4);
737 tot += zk4.value(1.7f,5);
738 tot += zk4.value(2.7f,6);
739 tot += zk4.value(3.7f,7);
741 BOOST_REQUIRE_CLOSE(tot,1.0f,0.001);
747 tot += -3.3*zk4.value(-3.3f,0);
748 tot += -2.3*zk4.value(-2.3f,1);
749 tot += -1.3*zk4.value(-1.3f,2);
750 tot += -0.3*zk4.value(-0.3f,3);
751 tot += 0.7*zk4.value(0.7f,4);
752 tot += 1.7*zk4.value(1.7f,5);
753 tot += 2.7*zk4.value(2.7f,6);
754 tot += 3.7*zk4.value(3.7f,7);
756 BOOST_REQUIRE_SMALL(tot,0.001f);
762 tot += 3.3*3.3*zk4.value(-3.3f,0);
763 tot += 2.3*2.3*zk4.value(-2.3f,1);
764 tot += 1.3*1.3*zk4.value(-1.3f,2);
765 tot += 0.3*0.3*zk4.value(-0.3f,3);
766 tot += 0.7*0.7*zk4.value(0.7f,4);
767 tot += 1.7*1.7*zk4.value(1.7f,5);
768 tot += 2.7*2.7*zk4.value(2.7f,6);
769 tot += 3.7*3.7*zk4.value(3.7f,7);
771 BOOST_REQUIRE_SMALL(tot,0.001f);
777 tot += -3.3*-3.3*-3.3*zk4.value(-3.3f,0);
778 tot += -2.3*-2.3*-2.3*zk4.value(-2.3f,1);
779 tot += -1.3*-1.3*-1.3*zk4.value(-1.3f,2);
780 tot += -0.3*-0.3*-0.3*zk4.value(-0.3f,3);
781 tot += 0.7*0.7*0.7*zk4.value(0.7f,4);
782 tot += 1.7*1.7*1.7*zk4.value(1.7f,5);
783 tot += 2.7*2.7*2.7*zk4.value(2.7f,6);
784 tot += 3.7*3.7*3.7*zk4.value(3.7f,7);
786 BOOST_REQUIRE_SMALL(tot,0.001f);
792 tot += -3.3*-3.3*-3.3*-3.3*zk4.value(-3.3f,0);
793 tot += -2.3*-2.3*-2.3*-2.3*zk4.value(-2.3f,1);
794 tot += -1.3*-1.3*-1.3*-1.3*zk4.value(-1.3f,2);
795 tot += -0.3*-0.3*-0.3*-0.3*zk4.value(-0.3f,3);
796 tot += 0.7*0.7*0.7*0.7*zk4.value(0.7f,4);
797 tot += 1.7*1.7*1.7*1.7*zk4.value(1.7f,5);
798 tot += 2.7*2.7*2.7*2.7*zk4.value(2.7f,6);
799 tot += 3.7*3.7*3.7*3.7*zk4.value(3.7f,7);
801 BOOST_REQUIRE_SMALL(tot,0.001f);
804 BOOST_AUTO_TEST_SUITE_END()
grid_key_dx is the key to access any element in the grid
auto get(const grid_dist_key_dx< dim, bg_key > &v1) const -> typename std::add_lvalue_reference< decltype(loc_grid.get(v1.getSub()).template get< p >(v1.getKey()))>::type
Get the reference of the selected element.
__device__ __host__ index_type get(index_type i) const
Get the i index.
This class implement the point shape in an N-dimensional space.
This is a distributed grid.
__device__ __host__ const T & get(unsigned int i) const
Get coordinate.
KeyT const ValueT ValueT OffsetIteratorT OffsetIteratorT int
[in] The number of segments that comprise the sorting data
grid_dist_iterator< dim, device_grid, decltype(device_grid::type_of_subiterator()), FREE > getDomainIterator() const
It return an iterator that span the full grid domain (each processor span its local domain)
This class represent an N-dimensional box.
Main class for interpolation Particle to mest p2m and Mesh to particle m2p.
void map(size_t opt=0)
It move all the grid parts that do not belong to the local processor to the respective processor.