| 1 | /* |
| 2 | * grid_sm_test.hpp |
| 3 | * |
| 4 | * Created on: Dec 14, 2015 |
| 5 | * Author: i-bird |
| 6 | */ |
| 7 | |
| 8 | #ifndef OPENFPM_DATA_SRC_GRID_GRID_SM_UNIT_TESTS_HPP_ |
| 9 | #define OPENFPM_DATA_SRC_GRID_GRID_SM_UNIT_TESTS_HPP_ |
| 10 | |
| 11 | #include "iterators/grid_key_dx_iterator_sub_bc.hpp" |
| 12 | #include "grid_key_dx_iterator_hilbert.hpp" |
| 13 | |
| 14 | BOOST_AUTO_TEST_SUITE( grid_sm_test ) |
| 15 | |
| 16 | |
| 17 | BOOST_AUTO_TEST_CASE( grid_sm_linearization ) |
| 18 | { |
| 19 | const grid_key_dx<3> key1(1,2,3); |
| 20 | const grid_key_dx<3> zero(0,0,0); |
| 21 | const grid_key_dx<3> seven(7,7,7); |
| 22 | const comb<3> c({1,0,-1}); |
| 23 | size_t sz[3] = {8,8,8}; |
| 24 | |
| 25 | grid_sm<3,int> gs(sz); |
| 26 | size_t bc[] = {NON_PERIODIC,NON_PERIODIC,NON_PERIODIC}; |
| 27 | |
| 28 | long int lin = gs.LinId<CheckExistence>(key1,c.getComb(),bc); |
| 29 | BOOST_REQUIRE_EQUAL(lin,146); |
| 30 | lin = gs.LinId<CheckExistence>(zero,c.getComb(),bc); |
| 31 | BOOST_REQUIRE_EQUAL(lin,-1); |
| 32 | lin = gs.LinId<CheckExistence>(seven,c.getComb(),bc); |
| 33 | BOOST_REQUIRE_EQUAL(lin,-1); |
| 34 | |
| 35 | for (size_t i = 0 ; i < 3 ; i++) |
| 36 | bc[i] = PERIODIC; |
| 37 | |
| 38 | lin = gs.LinId<CheckExistence>(key1,c.getComb(),bc); |
| 39 | BOOST_REQUIRE_EQUAL(lin,146); |
| 40 | lin = gs.LinId<CheckExistence>(zero,c.getComb(),bc); |
| 41 | BOOST_REQUIRE_EQUAL(lin,71); |
| 42 | lin = gs.LinId<CheckExistence>(seven,c.getComb(),bc); |
| 43 | BOOST_REQUIRE_EQUAL(lin,62); |
| 44 | } |
| 45 | |
| 46 | |
| 47 | BOOST_AUTO_TEST_CASE( grid_iterator_sub_p ) |
| 48 | { |
| 49 | const grid_key_dx<3> key1(4,4,4); |
| 50 | const grid_key_dx<3> key2(-1,-1,-1); |
| 51 | const grid_key_dx<3> key3(9,9,9); |
| 52 | size_t sz[3] = {8,8,8}; |
| 53 | |
| 54 | grid_sm<3,int> gs(sz); |
| 55 | |
| 56 | grid_key_dx_iterator_sub_bc<3,no_stencil> it(gs,key2,key1,{PERIODIC,PERIODIC,PERIODIC}); |
| 57 | |
| 58 | size_t cnt = 0; |
| 59 | |
| 60 | while (it.isNext()) |
| 61 | { |
| 62 | auto key = it.get(); |
| 63 | |
| 64 | for (size_t i = 0 ; i < 3 ; i++) |
| 65 | { |
| 66 | BOOST_REQUIRE_EQUAL(key.get(i) >= (long int)0,true); |
| 67 | BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true); |
| 68 | } |
| 69 | |
| 70 | cnt++; |
| 71 | |
| 72 | ++it; |
| 73 | } |
| 74 | |
| 75 | BOOST_REQUIRE_EQUAL(cnt,216ul); |
| 76 | |
| 77 | grid_key_dx_iterator_sub_bc<3,no_stencil> it2(gs,key2,key3,{PERIODIC,PERIODIC,PERIODIC}); |
| 78 | |
| 79 | cnt = 0; |
| 80 | |
| 81 | while (it2.isNext()) |
| 82 | { |
| 83 | auto key = it2.get(); |
| 84 | |
| 85 | for (size_t i = 0 ; i < 3 ; i++) |
| 86 | { |
| 87 | BOOST_REQUIRE_EQUAL(key.get(i) >= (long int)0,true); |
| 88 | BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true); |
| 89 | } |
| 90 | |
| 91 | cnt++; |
| 92 | |
| 93 | ++it2; |
| 94 | } |
| 95 | |
| 96 | BOOST_REQUIRE_EQUAL(cnt,1331ul); |
| 97 | |
| 98 | cnt = 0; |
| 99 | |
| 100 | const grid_key_dx<3> key4(0,-1,0); |
| 101 | const grid_key_dx<3> key5(2,2,2); |
| 102 | |
| 103 | grid_key_dx_iterator_sub_bc<3,no_stencil> it3(gs,key4,key5,{NON_PERIODIC,PERIODIC,NON_PERIODIC}); |
| 104 | |
| 105 | while (it3.isNext()) |
| 106 | { |
| 107 | auto key = it3.get(); |
| 108 | |
| 109 | for (size_t i = 0 ; i < 3 ; i++) |
| 110 | { |
| 111 | BOOST_REQUIRE_EQUAL(key.get(i) >= (long int)0,true); |
| 112 | BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true); |
| 113 | } |
| 114 | |
| 115 | cnt++; |
| 116 | |
| 117 | ++it3; |
| 118 | } |
| 119 | |
| 120 | BOOST_REQUIRE_EQUAL(cnt,36ul); |
| 121 | |
| 122 | // bc non periodic with out-of-bound |
| 123 | |
| 124 | grid_key_dx_iterator_sub_bc<3,no_stencil,grid_sm<3,void>,do_not_print_warning_on_adjustment<3,grid_sm<3,void>>> it4(gs,key4,key5,{NON_PERIODIC,NON_PERIODIC,NON_PERIODIC}); |
| 125 | |
| 126 | cnt = 0; |
| 127 | |
| 128 | while (it4.isNext()) |
| 129 | { |
| 130 | auto key = it4.get(); |
| 131 | |
| 132 | for (size_t i = 0 ; i < 3 ; i++) |
| 133 | { |
| 134 | BOOST_REQUIRE_EQUAL(key.get(i) >= (long int)0,true); |
| 135 | BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true); |
| 136 | } |
| 137 | |
| 138 | cnt++; |
| 139 | |
| 140 | ++it4; |
| 141 | } |
| 142 | |
| 143 | BOOST_REQUIRE_EQUAL(cnt,27ul); |
| 144 | |
| 145 | // case with no key |
| 146 | |
| 147 | const grid_key_dx<3> key6(-1,-1,-1); |
| 148 | const grid_key_dx<3> key7(-1,-1,8); |
| 149 | |
| 150 | grid_key_dx_iterator_sub_bc<3,no_stencil,grid_sm<3,void>,do_not_print_warning_on_adjustment<3,grid_sm<3,void>>> it5(gs,key6,key7,{NON_PERIODIC,NON_PERIODIC,NON_PERIODIC}); |
| 151 | |
| 152 | cnt = 0; |
| 153 | |
| 154 | while (it5.isNext()) |
| 155 | { |
| 156 | auto key = it5.get(); |
| 157 | |
| 158 | for (size_t i = 0 ; i < 3 ; i++) |
| 159 | { |
| 160 | BOOST_REQUIRE_EQUAL(key.get(i) >= 0,true); |
| 161 | BOOST_REQUIRE_EQUAL(key.get(i) < (long int)sz[i],true); |
| 162 | } |
| 163 | |
| 164 | cnt++; |
| 165 | |
| 166 | ++it5; |
| 167 | } |
| 168 | |
| 169 | BOOST_REQUIRE_EQUAL(cnt,0ul); |
| 170 | } |
| 171 | |
| 172 | BOOST_AUTO_TEST_CASE( grid_key_dx_iterator_hilbert_test ) |
| 173 | { |
| 174 | // 2D test |
| 175 | { |
| 176 | size_t count = 0; |
| 177 | |
| 178 | //An order of a hilberts curve |
| 179 | int32_t m = 2; |
| 180 | |
| 181 | grid_key_dx<2> start (0,0); |
| 182 | |
| 183 | //Create an iterator |
| 184 | grid_key_dx_iterator_hilbert<2> h_it(m); |
| 185 | |
| 186 | while (h_it.isNext()) |
| 187 | { |
| 188 | count++; |
| 189 | |
| 190 | ++h_it; |
| 191 | } |
| 192 | |
| 193 | //(2^m)^dim |
| 194 | BOOST_REQUIRE_EQUAL(count, (size_t)16); |
| 195 | |
| 196 | h_it.reset(); |
| 197 | |
| 198 | bool val = h_it.get() == start; |
| 199 | |
| 200 | BOOST_REQUIRE_EQUAL(val,true); |
| 201 | } |
| 202 | |
| 203 | // 3D test |
| 204 | { |
| 205 | size_t count = 0; |
| 206 | |
| 207 | //An order of a hilberts curve |
| 208 | int32_t m = 2; |
| 209 | |
| 210 | grid_key_dx<3> start (0,0,0); |
| 211 | |
| 212 | //Create an iterator |
| 213 | grid_key_dx_iterator_hilbert<3> h_it(m); |
| 214 | |
| 215 | while (h_it.isNext()) |
| 216 | { |
| 217 | count++; |
| 218 | |
| 219 | ++h_it; |
| 220 | } |
| 221 | |
| 222 | //(2^m)^dim |
| 223 | BOOST_REQUIRE_EQUAL(count, (size_t)64); |
| 224 | |
| 225 | h_it.reset(); |
| 226 | |
| 227 | bool val = h_it.get() == start; |
| 228 | |
| 229 | BOOST_REQUIRE_EQUAL(val,true); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | |
| 234 | BOOST_AUTO_TEST_CASE( grid_iterator_sp_test ) |
| 235 | { |
| 236 | size_t sz[3] = {16,16,16}; |
| 237 | |
| 238 | grid_cpu<3, Point_test<float> > c3(sz); |
| 239 | c3.setMemory(); |
| 240 | |
| 241 | grid_key_dx<3> start(2,2,2); |
| 242 | grid_key_dx<3> stop(10,10,10); |
| 243 | |
| 244 | auto info = c3.getGrid(); |
| 245 | |
| 246 | grid_key_dx_iterator_sp<3> it(info,info.LinId(start),info.LinId(stop)); |
| 247 | |
| 248 | size_t count = 0; |
| 249 | |
| 250 | while (it.isNext()) |
| 251 | { |
| 252 | count++; |
| 253 | |
| 254 | ++it; |
| 255 | } |
| 256 | |
| 257 | BOOST_REQUIRE_EQUAL(count,2185ul); |
| 258 | } |
| 259 | |
| 260 | BOOST_AUTO_TEST_CASE( grid_iterator_test_use) |
| 261 | { |
| 262 | { |
| 263 | //! [Grid iterator test usage] |
| 264 | size_t count = 0; |
| 265 | |
| 266 | // Subdivisions |
| 267 | size_t div[3] = {16,16,16}; |
| 268 | |
| 269 | // grid info |
| 270 | grid_sm<3,void> g_info(div); |
| 271 | |
| 272 | // Create a grid iterator |
| 273 | grid_key_dx_iterator<3> g_it(g_info); |
| 274 | |
| 275 | // Iterate on all the elements |
| 276 | while (g_it.isNext()) |
| 277 | { |
| 278 | grid_key_dx<3> key = g_it.get(); |
| 279 | |
| 280 | // set the grid key to zero without any reason ( to avoid warning compilations ) |
| 281 | key.zero(); |
| 282 | |
| 283 | count++; |
| 284 | |
| 285 | ++g_it; |
| 286 | } |
| 287 | |
| 288 | BOOST_REQUIRE_EQUAL(count, (size_t)16*16*16); |
| 289 | //! [Grid iterator test usage] |
| 290 | } |
| 291 | |
| 292 | { |
| 293 | size_t count = 0; |
| 294 | // Iterate only on the internal elements |
| 295 | |
| 296 | //! [Sub-grid iterator test usage] |
| 297 | // Subdivisions |
| 298 | size_t div[3] = {16,16,16}; |
| 299 | |
| 300 | // grid info |
| 301 | grid_sm<3,void> g_info(div); |
| 302 | |
| 303 | grid_key_dx<3> start(1,1,1); |
| 304 | grid_key_dx<3> stop(14,14,14); |
| 305 | |
| 306 | // Create a grid iterator (start and stop included) |
| 307 | grid_key_dx_iterator_sub<3> g_it(g_info,start,stop); |
| 308 | |
| 309 | // Iterate on all the elements |
| 310 | while (g_it.isNext()) |
| 311 | { |
| 312 | grid_key_dx<3> key = g_it.get(); |
| 313 | |
| 314 | // set the grid key to zero without any reason ( to avoid warning compilations ) |
| 315 | key.zero(); |
| 316 | |
| 317 | count++; |
| 318 | |
| 319 | ++g_it; |
| 320 | } |
| 321 | |
| 322 | BOOST_REQUIRE_EQUAL(count, (size_t)14*14*14); |
| 323 | |
| 324 | //! [Sub-grid iterator test usage] |
| 325 | |
| 326 | // reset the iterator and check that it start from gk_start |
| 327 | g_it.reset(); |
| 328 | |
| 329 | bool val = g_it.get() == start; |
| 330 | |
| 331 | BOOST_REQUIRE_EQUAL(val,true); |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | BOOST_AUTO_TEST_CASE( grid_sub_iterator_test ) |
| 336 | { |
| 337 | //! [Sub-grid iterator test usage] |
| 338 | // Subdivisions |
| 339 | size_t count = 0; |
| 340 | typedef Point_test<float> p; |
| 341 | |
| 342 | size_t div[3] = {16,16,16}; |
| 343 | |
| 344 | // grid info |
| 345 | grid_cpu<3,Point_test<float>> g(div); |
| 346 | g.setMemory(); |
| 347 | |
| 348 | grid_key_dx<3> start(1,1,1); |
| 349 | grid_key_dx<3> stop(14,14,14); |
| 350 | |
| 351 | // Create a grid iterator (start and stop included) |
| 352 | auto g_it = g.getIterator(start,stop); |
| 353 | |
| 354 | // Iterate on all the elements |
| 355 | while (g_it.isNext()) |
| 356 | { |
| 357 | grid_key_dx<3> key = g_it.get(); |
| 358 | |
| 359 | // set the x value |
| 360 | g.template get<p::x>(key) = 1.0; |
| 361 | |
| 362 | count++; |
| 363 | |
| 364 | ++g_it; |
| 365 | } |
| 366 | |
| 367 | BOOST_REQUIRE_EQUAL(count, (size_t)14*14*14); |
| 368 | |
| 369 | //! [Sub-grid iterator test usage] |
| 370 | } |
| 371 | |
| 372 | BOOST_AUTO_TEST_SUITE_END() |
| 373 | |
| 374 | |
| 375 | #endif /* OPENFPM_DATA_SRC_GRID_GRID_SM_UNIT_TESTS_HPP_ */ |
| 376 | |