OpenFPM_pdata  1.0.0
Project that contain the implementation of distributed structures
 All Data Structures Functions Variables Typedefs Enumerations Friends Pages
grid_key_dx_iterator_stencil.hpp
1 /*
2  * grid_key_dx_iterator_stencil.hpp
3  *
4  * Created on: Jun 23, 2017
5  * Author: i-bird
6  */
7 
8 #ifndef OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_STENCIL_HPP_
9 #define OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_STENCIL_HPP_
10 
11 
12 #include "Grid/grid_sm.hpp"
13 
27 template<unsigned int dim, unsigned int Np>
29 {
30 #ifdef SE_CLASS1
31  // Actual status of the iterator, when the iterator is not initialized cannot be used
32  // and reinitialize must be called
33  bool initialized = false;
34 #endif
35 
38 
40  long int stencil_offset[Np];
41 
50  inline size_t get_gk(size_t i) const
51  {
52  return gk.get(i);
53  }
54 
55 protected:
56 
58 
59 public:
60 
68  {
69 #ifdef SE_CLASS1
70  initialized = false;
71 #endif
72  }
73 
79  : grid_base(g_it.grid_base)
80  {
82 
83  for (size_t i = 0 ; i < dim ; i++)
84  {gk.set_d(i,g_it.get_gk(i));}
85 
86  // Copy the offset part
87 
88  for (size_t i = 0 ; i < Np ; i++)
89  {stencil_offset[i] = g_it.stencil_offset[i];}
90 
91 #ifdef SE_CLASS1
92  initialized = true;
93 #endif
94  }
95 
100  template<typename T> grid_key_dx_iterator_stencil(const grid_sm<dim,T> & g, const grid_key_dx<dim> (& stencil)[Np])
101  : grid_base(g)
102  {
103  reset();
104 
105  // calculate the offsets
106 
107  for (size_t i = 0 ; i < Np ; i++)
108  {
109  grid_key_dx<dim> zero;
110 
111  for (size_t k = 0 ; k < dim ; k++) {zero.set_d(k,0);}
112 
113  zero = zero + stencil[i];
114 
115  stencil_offset[i] = g.LinId(zero);
116  }
117 
118 #ifdef SE_CLASS1
119  initialized = true;
120 #endif
121 
122 
123  }
124 
130  {
131  grid_base = key_it.grid_base;
132 
134 
135  for (size_t i = 0 ; i < dim ; i++)
136  {gk.set_d(i,key_it.get_gk(i));}
137 
138  for (size_t i = 0 ; i < Np ; i++)
139  {stencil_offset[i] = key_it.stencil_offset[i];}
140 
141  return *this;
142  }
143 
151  {
153 
154  size_t id = gk.get(0);
155  gk.set_d(0,id+1);
156 
157  // update the offsets
158  for (size_t i = 0 ; i < Np ; i++)
159  stencil_offset[i] += 1;
160 
162 
163  size_t i = 0;
164  for ( ; i < dim-1 ; i++)
165  {
166  size_t id = gk.get(i);
167  if (id >= grid_base.size(i))
168  {
169  // ! overflow, increment the next index
170 
171  size_t idr = gk.get(i);
172  gk.set_d(i,0);
173  id = gk.get(i+1);
174  gk.set_d(i+1,id+1);
175 
176  size_t str_dw = (i == 0)?1:grid_base.size_s(i-1);
177 
178  // update the offsets
179  for (size_t k = 0 ; k < Np ; k++)
180  {stencil_offset[k] += -str_dw*idr + grid_base.size_s(i);}
181  }
182  else
183  {
184  break;
185  }
186  }
187 
188  return *this;
189  }
190 
199  inline void set(int d, size_t sz)
200  {
201  // set the counter dim to sz
202 
203  gk.set_d(d,sz);
204  }
205 
214  inline bool isNext()
215  {
216  if (gk.get(dim-1) < (long int)grid_base.size(dim-1))
217  {
219 
220  return true;
221  }
222 
224  return false;
225  }
226 
234  inline const grid_key_dx<dim> & getLoc() const
235  {
236  return gk;
237  }
238 
239 
247  template<unsigned int id> inline size_t get() const
248  {
249  return stencil_offset[id];
250  }
251 
258  {
259  grid_base = key.grid_base;
260  reset();
261  }
262 
266  inline void reset()
267  {
268  // Initialize to 0 the index
269 
270  for (size_t i = 0 ; i < dim ; i++)
271  {gk.set_d(i,0);}
272 
273  // here we check if grid have a size equal to zero or negative
274  // in this case the grid has no points
275 
276  for (size_t i = 0 ; i < dim ; i++)
277  {
278  // If the size of the grid is zero in any dimension set the iterator
279  // to the end point
280  if (grid_base.size(i) == 0)
281  gk.set_d(dim-1,grid_base.size(dim-1));
282  }
283  }
284 };
285 
286 
287 #endif /* OPENFPM_DATA_SRC_GRID_ITERATORS_GRID_KEY_DX_ITERATOR_STENCIL_HPP_ */
grid_sm< dim, void > grid_base
information about the grid
mem_id LinId(const grid_key_dx< N > &gk, const char sum_id[N]) const
Linearization of the grid_key_dx with a specified shift.
Definition: grid_sm.hpp:326
void reset()
Reset the iterator (it restart from the beginning)
grid_key_dx is the key to access any element in the grid
Definition: grid_key.hpp:18
size_t size_s(unsigned int i) const
Definition: grid_sm.hpp:642
size_t size() const
Return the size of the grid.
Definition: grid_sm.hpp:561
void set(int d, size_t sz)
Set the dimension.
grid_key_dx_iterator_stencil(const grid_sm< dim, T > &g, const grid_key_dx< dim >(&stencil)[Np])
Constructor require a grid_sm<dim,T>
grid_key_dx_iterator_stencil< dim, Np > operator=(const grid_key_dx_iterator_stencil< dim, Np > &key_it)
Constructor from another grid_key_dx_iterator.
grid_key_dx_iterator_stencil< dim, Np > & operator++()
Get the next element.
size_t get_gk(size_t i) const
return the index i of the gk key
grid_key_dx_iterator_stencil(const grid_key_dx_iterator_stencil< dim, Np > &g_it)
Constructor from a grid_key_dx_iterator<dim>
void reinitialize(const grid_key_dx_iterator_stencil< dim, Np > &key)
Reinitialize the grid_key_dx_iterator.
const grid_key_dx< dim > & getLoc() const
Get the actual position.
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:201
long int stencil_offset[Np]
set of offsets for the stencil
void set_d(size_t i, mem_id id)
Set the i index.
Definition: grid_key.hpp:396
bool isNext()
Check if there is the next element.
grid_key_dx_iterator_stencil()
Default constructor.