OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
 
Loading...
Searching...
No Matches
grid_key_dx_iterator_sub.hpp
1/*
2 * grid_key_dx_iterator_sub.hpp
3 *
4 * Created on: Dec 15, 2015
5 * Author: i-bird
6 */
7
8#ifndef OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_SUB_HPP_
9#define OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_SUB_HPP_
10
11#include "grid_key_dx_iterator.hpp"
12#include "Grid/grid_key.hpp"
13
14/* \brief grid_key_dx_iterator_sub can adjust the domain if the border go out-of-side
15 * in this case a warning is produced
16 *
17 * \tparam dim dimensionality
18 *
19 */
20template <unsigned int dim, typename linearizer>
22{
23public:
24
30 inline static void pw1(size_t i, const grid_key_dx<dim> & gk_start) {std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " start with index smaller than zero x[" << i << "]=" << gk_start.get(i) << "\n";}
31
38 inline static void pw2(size_t i, const grid_key_dx<dim> & gk_start) {std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " Cropping start point x[" << i << "]=" << gk_start.get(i) << " to x[" << i << "]=0 \n" ;}
39
40
45 inline static void pw3() {std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " stop with smaller index than start \n";}
46
47
55 inline static void pw4(size_t i, const grid_key_dx<dim> & gk_stop, const linearizer & grid_base) {std::cerr << "Warning: " << __FILE__ << ":" << __LINE__ << " stop index bigger than cell domain x[" << i << "]=" << gk_stop.get(i) << " > " << grid_base.size(i) << "\n";}
56
61 inline static void pw5() {std::cerr << "Warning grid_key_dx_iterator_sub: " << __FILE__ << ":" << __LINE__ << " the starting point is not smaller or equal than than the stop point in all the coordinates" << "\n";}
62};
63
64/* \brief grid_key_dx_iterator_sub can adjust the domain if the border go out-of-side
65 * in this case a warning is produced
66 *
67 * \tparam dim dimensionality
68 * \tparam gb type of grid
69 *
70 */
71template <unsigned int dim, typename linearizer>
73{
74public:
75
82 inline static void pw1(size_t i, const grid_key_dx<dim> & gk_start) {}
83
90 inline static void pw2(size_t i, const grid_key_dx<dim> & gk_start) {}
91
96 inline static void pw3() {}
97
105 inline static void pw4(size_t i, const grid_key_dx<dim> & gk_stop, const linearizer & grid_base) {}
106
111 inline static void pw5() {}
112};
113
114template<unsigned int dim, typename stl_type, typename linearizer>
116{
117 static inline void inc(grid_key_dx<dim> & gk,
118 grid_key_dx<dim> & gk_start,
119 grid_key_dx<dim> & gk_stop,
120 stl_type & stl_code,
121 linearizer & grid_base)
122 {
123 long int i = 0;
124 for ( ; i < dim-1 ; i++)
125 {
126 /* coverity[dead_error_begin] */
127 size_t id = gk.get(i);
128 if ((long int)id > gk_stop.get(i))
129 {
130 // ! overflow, increment the next index
131
132 size_t idr = gk.get(i) - gk_start.get(i);
133 gk.set_d(i,gk_start.get(i));
134 id = gk.get(i+1);
135 gk.set_d(i+1,id+1);
136
137 stl_code.adjust_offset(i,idr,grid_base);
138 }
139 else
140 {
141 break;
142 }
143 }
144 }
145};
146
147
161template<unsigned int dim, typename stencil, typename linearizer, typename warn>
162class grid_key_dx_iterator_sub : public grid_key_dx_iterator<dim,stencil,linearizer>
163{
164#ifdef SE_CLASS1
165 bool initialized = false;
166#endif
167
169 linearizer grid_base;
170
173
176
181 {
182 // Check that start and stop are inside the domain otherwise crop them
183
184 for (size_t i = 0 ; i < dim ; i++)
185 {
186 // if start smaller than 0
187 if (gk_start.get(i) < 0)
188 {
189#ifdef SE_CLASS1
190 warn::pw1(i,gk_start);
191#endif
192 if (gk_start.get(i) < gk_stop.get(i))
193 {
194#ifdef SE_CLASS1
195 warn::pw2(i,gk_start);
196#endif
197 gk_start.set_d(i,0);
198 }
199 else
200 {
201#ifdef SE_CLASS1
202 warn::pw3();
203#endif
204 // No points are available
205 gk_start.set_d(dim-1,gk_stop.get(dim-1)+1);
206 break;
207 }
208 }
209
210 // if stop bigger than the domain
211 if (gk_stop.get(i) >= (long int)grid_base.size(i))
212 {
213#ifdef SE_CLASS1
214 warn::pw4(i,gk_stop,grid_base);
215#endif
216
217 if (gk_start.get(i) < (long int)grid_base.size(i))
218 gk_stop.set_d(i,grid_base.size(i)-1);
219 else
220 {
221 // No point are available
222 gk_start.set_d(dim-1,gk_stop.get(dim-1)+1);
223 break;
224 }
225 }
226 }
227
229 for (unsigned int i = 0 ; i < dim ; i++)
230 {
231 this->gk.set_d(i,gk_start.get(i));
232 }
233
234#ifdef SE_CLASS1
235 initialized = true;
236#endif
237 }
238
244 {
246
247// post_increment_sub_impl<dim,stencil>::inc(this->gk,gk_start,gk_stop,this->stl_code,grid_base);
248
249 long int i = 0;
250 for ( ; i < dim-1 ; i++)
251 {
252 /* coverity[dead_error_begin] */
253 size_t id = this->gk.get(i);
254 if ((long int)id > gk_stop.get(i))
255 {
256 // ! overflow, increment the next index
257
258 size_t idr = this->gk.get(i) - gk_start.get(i);
259 this->gk.set_d(i,gk_start.get(i));
260 id = this->gk.get(i+1);
261 this->gk.set_d(i+1,id+1);
262
263 this->stl_code.adjust_offset(i,idr,grid_base);
264 }
265 else
266 {
267 break;
268 }
269 }
270 }
271
272public:
273
281 {}
282
290 :grid_key_dx_iterator<dim,stencil>(g_s_it),grid_base(g_s_it.grid_base),gk_start(g_s_it.gk_start), gk_stop(g_s_it.gk_stop)
291 {
292#ifdef SE_CLASS1
295
296 for (unsigned int i = 0 ; i < dim ; i++)
297 {
298 if (gk_start.get(i) > gk_stop.get(i))
299 {
300 warn::pw5();
301 }
302 }
303#endif
304
305 Initialize();
306 }
307
308
322 grid_key_dx_iterator_sub(const linearizer & g,
323 const grid_key_dx<dim> & start,
324 const grid_key_dx<dim> & stop)
325 : grid_key_dx_iterator<dim,stencil,linearizer>(g),grid_base(g),gk_start(start), gk_stop(stop)
326 {
327#ifdef SE_CLASS1
330
331 for (unsigned int i = 0 ; i < dim ; i++)
332 {
333 if (gk_start.get(i) > gk_stop.get(i))
334 {
335 warn::pw5();
336 }
337 }
338#endif
339
340 Initialize();
341 }
342
343
358 grid_key_dx_iterator_sub(const linearizer & g,
359 const grid_key_dx<dim> & start,
360 const grid_key_dx<dim> & stop,
361 const grid_key_dx<dim> (& stencil_pnt)[stencil::nsp])
362 :grid_key_dx_iterator<dim,stencil,linearizer>(g,stencil_pnt),grid_base(g),gk_start(start), gk_stop(stop)
363 {
364#ifdef SE_CLASS1
367
368 for (unsigned int i = 0 ; i < dim ; i++)
369 {
370 if (gk_start.get(i) > gk_stop.get(i))
371 {
372 warn::pw5();
373 }
374 }
375#endif
376
377 Initialize();
379 }
380
390 grid_key_dx_iterator_sub(const grid_key_dx<dim> (& stencil_pnt)[stencil::nsp])
391 :grid_key_dx_iterator<dim,stencil>(stencil_pnt)
392 {
393 }
394
407 grid_key_dx_iterator_sub(const linearizer & g, const size_t m)
409 {
410 // Initialize the start and stop point
411 for (unsigned int i = 0 ; i < dim ; i++)
412 {
413 gk_start.set_d(i,m);
414 gk_stop.set_d(i,g.size(i)-m-1);
415 }
416
417 //
418 Initialize();
419 }
420
431 grid_key_dx_iterator_sub(const linearizer & g,
432 const size_t (& start)[dim],
433 const size_t (& stop)[dim])
434 :grid_key_dx_iterator<dim>(g),grid_base(g),gk_start(start), gk_stop(stop)
435 {
436#ifdef SE_CLASS1
439
440 for (unsigned int i = 0 ; i < dim ; i++)
441 {
442 if (start[i] > stop[i])
443 {
444 warn::pw5();
445 }
446 }
447#endif
448
449 Initialize();
450 }
451
460 {
461#ifdef SE_CLASS1
462 if (initialized == false)
463 {std::cerr << "Error: " << __FILE__ << __LINE__ << " using unitialized iterator" << "\n";}
464#endif
465
467
468 size_t id = this->gk.get(0);
469 this->gk.set_d(0,id+1);
470
471 this->stl_code.increment();
472
474
476
477 return *this;
478 }
479
486 {
487#ifdef SE_CLASS1
488 if (initialized == false)
489 {std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " using unitialized iterator" << "\n";}
490#endif
491
492 for (size_t i = 0 ; i < nsteps ; i++)
493 {
494 this->operator ++();
495 }
496
497 return *this;
498 }
499
507 inline bool isNext()
508 {
509#ifdef SE_CLASS1
510 if (initialized == false)
511 {std::cerr << "Error: " << __FILE__ << ":" << __LINE__ << " using unitialized iterator" << "\n";}
512#endif
513
514 if (this->gk.get(dim-1) <= gk_stop.get(dim-1))
515 {
517
518 return true;
519 }
520
522 return false;
523 }
524
530 inline grid_key_dx<dim> get() const
531 {
532#ifdef SE_CLASS1
533 if (initialized == false)
534 {std::cerr << "Error: " << __FILE__ << __LINE__ << " using unitialized iterator" << "\n";}
535#endif
536
538 }
539
548 inline void reinitialize(const grid_key_dx_iterator_sub<dim> & g_s_it)
549 {
550 // Reinitialize the iterator
551
553 grid_base = g_s_it.getGridInfo();
554 gk_start = g_s_it.getStart();
555 gk_stop = g_s_it.getStop();
556
557
558#ifdef SE_CLASS1
561
562 for (unsigned int i = 0 ; i < dim ; i++)
563 {
564 if (gk_start.get(i) > gk_stop.get(i))
565 {
566 warn::pw5();
567 }
568 }
569
570 initialized = true;
571#endif
572
573 Initialize();
575 }
576
582 inline size_t getVolume()
583 {
585 }
586
590 inline void reset()
591 {
593
594 for (size_t i = 0 ; i < dim ; i++)
595 {this->gk.set_d(i,gk_start.get(i));}
596 }
597
603 inline const linearizer & getGridInfo() const
604 {
605 return grid_base;
606 }
607
614 {
615 return gk_start;
616 }
617
623 const grid_key_dx<dim> & getStop() const
624 {
625 return gk_stop;
626 }
627
633 template<unsigned int tot_add>
634 inline void private_sum()
635 {
636 this->stl_code.template private_sum<tot_add>();
637 }
638
644 inline void private_adjust(size_t tot_add)
645 {
646 this->stl_code.private_adjust(tot_add);
647 }
648
649 /* \brief Set the iterator in a way that isNext return false
650 *
651 */
652 void invalidate()
653 {
654 this->gk.set_d(dim-1,1);
655 this->gk_stop.set_d(dim-1,0);
656
657#ifdef SE_CLASS1
658 this->initialized = true;
659#endif
660 }
661};
662
663
665
679template<typename warn>
681{
682
683public:
684
686 {}
687
696 {return *this;}
697
705 inline bool isNext()
706 {return false;}
707
715 inline size_t getVolume()
716 {return 0;}
717
724 inline void reset()
725 {}
726};
727
728#endif /* OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_SUB_HPP_ */
T getVolumeKey() const
Get the volume spanned by the Box P1 and P2 interpreted as grid key.
Definition Box.hpp:1351
static void pw1(size_t i, const grid_key_dx< dim > &gk_start)
print warning type1
static void pw2(size_t i, const grid_key_dx< dim > &gk_start)
print warning type2
static void pw3()
print warning type3
static void pw4(size_t i, const grid_key_dx< dim > &gk_stop, const linearizer &grid_base)
print warning type4
grid_key_dx_iterator< 0 > & operator++()
Next point.
Declaration grid_key_dx_iterator_sub.
grid_key_dx< dim > get() const
Return the actual grid key iterator.
void private_sum()
Sum a template constant.
void private_adjust(size_t tot_add)
Sum a template constant.
bool isNext()
Check if there is the next element.
grid_key_dx_iterator_sub(const linearizer &g, const size_t m)
Constructor require a grid grid<dim,T>
void reinitialize(const grid_key_dx_iterator_sub< dim > &g_s_it)
Reinitialize the iterator.
const grid_key_dx< dim > & getStart() const
Starting point.
grid_key_dx< dim > gk_start
start point
size_t getVolume()
Get the volume spanned by this sub-grid iterator.
grid_key_dx_iterator_sub(const linearizer &g, const grid_key_dx< dim > &start, const grid_key_dx< dim > &stop)
Constructor require a grid grid<dim,T>
grid_key_dx_iterator_sub()
Default constructor.
grid_key_dx_iterator_sub(const linearizer &g, const size_t(&start)[dim], const size_t(&stop)[dim])
Constructor require a grid grid<dim,T>
grid_key_dx_iterator_sub(const linearizer &g, const grid_key_dx< dim > &start, const grid_key_dx< dim > &stop, const grid_key_dx< dim >(&stencil_pnt)[stencil::nsp])
Constructor require a grid grid<dim,T>
const grid_key_dx< dim > & getStop() const
Stop point.
linearizer grid_base
grid base where we are iterating
grid_key_dx_iterator_sub(const grid_key_dx< dim >(&stencil_pnt)[stencil::nsp])
Constructor.
grid_key_dx< dim > gk_stop
stop point
void post_increment()
After incremented we have to check we did not overflo any dimension and in case adjust the dimensions...
const linearizer & getGridInfo() const
Return the grid information related to this grid.
void reset()
Reset the iterator (it restart from the beginning)
grid_key_dx_iterator_sub(const grid_key_dx_iterator_sub< dim, stencil, linearizer > &g_s_it)
Constructor from another grid_key_dx_iterator_sub.
grid_key_dx_iterator_sub< dim, stencil, warn > & operator+=(int nsteps)
increment the operator by more than one
grid_key_dx_iterator_sub< dim, stencil, linearizer, warn > & operator++()
Get the next element.
void reinitialize(const grid_key_dx_iterator< dim > &key)
Reinitialize the grid_key_dx_iterator.
const grid_key_dx< dim > & get() const
Get the actual key.
void calc_stencil_offset(const grid_key_dx< dim > &start_p)
Calculate the stencil offset.
grid_key_dx is the key to access any element in the grid
Definition grid_key.hpp:19
__device__ __host__ void set_d(index_type i, index_type id)
Set the i index.
Definition grid_key.hpp:516
const long int(& get_k() const)[dim]
Return the internal k structure.
Definition grid_key.hpp:415
__device__ __host__ index_type get(index_type i) const
Get the i index.
Definition grid_key.hpp:503
Declaration print_warning_on_adjustment.
static void pw5()
print warning type5
static void pw4(size_t i, const grid_key_dx< dim > &gk_stop, const linearizer &grid_base)
print warning type4
static void pw2(size_t i, const grid_key_dx< dim > &gk_start)
print warning type2
static void pw1(size_t i, const grid_key_dx< dim > &gk_start)
print warning type1
static void pw3()
print warning type3