OpenFPM_data  0.1.0
Project that contain the implementation and interfaces for basic structure like vectors, grids, graph ... .
 All Data Structures Namespaces Functions Variables Typedefs Friends
CellListBal.hpp
1 
2 /*
3  * CellListBal.hpp
4  *
5  * Created on: Mar 22, 2015
6  * Last modified: June 25, 2015
7  * Authors: Pietro Incardona, Yaroslav Zaluzhnyi
8  */
9 
10 #ifndef CELLLISTBAL_HPP_
11 #define CELLLISTBAL_HPP_
12 
13 #include "CellDecomposer.hpp"
14 #include "Space/SpaceBox.hpp"
15 #include "util/mathutil.hpp"
16 #include "CellNNIterator.hpp"
17 #include "Space/Shape/HyperCube.hpp"
18 
38 template<unsigned int dim, typename T, typename base>
39 class CellList<dim,T,BALANCED,base>: public CellDecomposer_sm<dim,T>
40 {
41  // The array contain the neighborhood of the cell-id in case of asymmetric interaction
42  //
43  // * * *
44  // * x *
45  // * * *
46 
47  long int NNc_full[openfpm::math::pow(3,dim)];
48 
49  // The array contain the neighborhood of the cell-id in case of symmetric interaction
50  //
51  // * * *
52  // x *
53  //
54  long int NNc_sym[openfpm::math::pow(3,dim)/2+1];
55 
56  // The array contain the neighborhood of the cell-id in case of symmetric interaction (Optimized)
57  //
58  // * *
59  // x *
60  //
61  long int NNc_cr[openfpm::math::pow(2,dim)];
62 
63 
64  // each cell has a pointer to a dynamic structure
65  // that store the elements in the cell
66  openfpm::vector<base> cl_base;
67 
68  //Origin point
69  Point<dim,T> orig;
70 
71 public:
72 
73  // Object type that the structure store
74  typedef T value_type;
75 
82  {
83  CellDecomposer_sm<dim,T>::getGrid();
84  }
85 
94  void Initialize(Box<dim,T> & box, size_t (&div)[dim], Point<dim,T> & orig, const size_t pad = 1)
95  {
96  SpaceBox<dim,T> sbox;
97  Initialize(sbox,div,orig);
98  }
99 
108  void Initialize(SpaceBox<dim,T> & box, size_t (&div)[dim], Point<dim,T> & orig, const size_t pad = 1)
109  {
110  // Add padding
111  size_t div_pad[dim];
112  for (size_t i = 0 ; i < dim ; i++)
113  div_pad[i] = div[i] + 2;
114 
115  CellDecomposer_sm<dim,T>::setDimensions(box,div_pad, pad);
116 
117  this->orig = orig;
118 
119  //resize the vector to needed number of cells
120 
121  cl_base.resize(this->tot_n_cell);
122 
123  //filling a vector with "base" structures
124  for (int i = 0; i < this->tot_n_cell; i++)
125  //cl_base.push_back(base());
126  cl_base.get(i) = base();
127 
128 
129  // Calculate the NNc-arrays (for neighborhood):
130 
131  // compile-time array {0,0,0,....} and {3,3,3,...}
132 
133  typedef typename generate_array<size_t,dim, Fill_zero>::result NNzero;
134  typedef typename generate_array<size_t,dim, Fill_two>::result NNtwo;
135  typedef typename generate_array<size_t,dim, Fill_one>::result NNone;
136 
137  // Generate the sub-grid iterator
138 
139  grid_key_dx_iterator_sub<dim> gr_sub3(this->gr_cell,NNzero::data,NNtwo::data);
140 
141  // Calculate the NNc array
142 
143  size_t middle = this->gr_cell.LinId(NNone::data);
144  size_t i = 0;
145  while (gr_sub3.isNext())
146  {
147  NNc_full[i] = (long int)this->gr_cell.LinId(gr_sub3.get()) - middle;
148 
149  ++gr_sub3;
150  i++;
151  }
152 
153  // Calculate the NNc_sym array
154 
155  i = 0;
156  gr_sub3.reset();
157  while (gr_sub3.isNext())
158  {
159  auto key = gr_sub3.get();
160 
161  size_t lin = this->gr_cell.LinId(key);
162 
163  // Only the first half is considered
164  if (lin < middle)
165  {
166  ++gr_sub3;
167  continue;
168  }
169 
170  NNc_sym[i] = lin - middle;
171 
172  ++gr_sub3;
173  i++;
174  }
175 
176  // Calculate the NNc_cross array
177 
178  i = 0;
179  grid_key_dx_iterator_sub<dim> gr_sub2(this->gr_cell,NNzero::data,NNone::data);
180 
181  while (gr_sub2.isNext())
182  {
183  auto key = gr_sub2.get();
184 
185  NNc_cr[i] = (long int)this->gr_cell.LinId(key);
186 
187  ++gr_sub2;
188  i++;
189  }
190  }
191 
196  {
197  }
198 
199 
207  CellList(Box<dim,T> & box, size_t (&div)[dim], Point<dim,T> & orig, const size_t pad = 1)
208  {
209  SpaceBox<dim,T> sbox(box);
210  Initialize(sbox,div,orig,pad);
211  }
212 
220  CellList(SpaceBox<dim,T> & box, size_t (&div)[dim], Point<dim,T> & orig, const size_t pad = 1)
221  {
222  Initialize(box,div,orig,pad);
223  }
224 
231  void add(const T (& pos)[dim], typename base::value_type ele)
232  {
233  // calculate the Cell id
234 
235  size_t cell_id = this->getCell(pos);
236 
237  // add a new element
238 
239  cl_base.get(cell_id)->add(ele);
240  }
241 
248  void add(const Point<dim,T> & pos, typename base::value_type ele)
249  {
250  // calculate the Cell id
251 
252  size_t cell_id = this->getCell(pos);
253 
254  // add a new element
255 
256  cl_base.get(cell_id).add(ele);
257  }
258 
265  void remove(size_t cell, size_t ele)
266  {
267  cl_base.get(cell).remove(ele);
268  }
269 
277  size_t getNelements(size_t cell_id)
278  {
279  return cl_base.get(cell_id).size();
280  }
281 
288  auto get(size_t cell, size_t ele) -> decltype(cl_base.get(cell).get(ele))
289  {
290  return cl_base.get(cell).get(ele);
291  }
292 
293  //Three next functions are optional
294 
305  template<unsigned int i> inline auto get(size_t cell, size_t ele) -> decltype(cl_base.get(cell).get(ele))
306  {
307  return cl_base.template get<i>(cell)->get(ele);
308  }
309 
316  {
317  cl_base.swap(cl.cl_base);
318  }
319 
320 
327  {
328  return CellIterator<CellList<dim,T,BALANCED,base>>(cell,*this);
329  }
330 
336  template<unsigned int impl> CellNNIterator<dim,CellList<dim,T,BALANCED,base>,FULL,impl> getNNIterator(size_t cell)
337  {
338  CellNNIterator<dim,CellList<dim,T,BALANCED,base>,FULL,impl> cln(cell,NNc_full,*this);
339 
340  return cln;
341  }
342 
343  template<unsigned int impl> CellNNIterator<dim,CellList<dim,T,BALANCED,base>,SYM,impl> getNNIteratorSym(size_t cell)
344  {
345  CellNNIterator<dim,CellList<dim,T,BALANCED,base>,SYM,impl> cln(cell,NNc_sym,*this);
346 
347  return cln;
348  }
349 
350  template<unsigned int impl> CellNNIterator<dim,CellList<dim,T,BALANCED,base>,CRS,impl> getNNIteratorCross(size_t cell)
351  {
352  CellNNIterator<dim,CellList<dim,T,BALANCED,base>,CRS,impl> cln(cell,NNc_cr,*this);
353 
354  return cln;
355  }
356 };
357 
358 
359 #endif /* CELLLISTBAL_HPP_ */
This class represent an N-dimensional box.
Definition: SpaceBox.hpp:26
CellList(Box< dim, T > &box, size_t(&div)[dim], Point< dim, T > &orig, const size_t pad=1)
Cell list.
CellList(SpaceBox< dim, T > &box, size_t(&div)[dim], Point< dim, T > &orig, const size_t pad=1)
Cell list.
CellList()
Default constructor.
bool isNext()
Check if there is the next element.
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:20
void add(const T(&pos)[dim], typename base::value_type ele)
Add an element in the cell list.
CellIterator< CellList< dim, T, BALANCED, base > > getIterator(size_t cell)
Get the Cell iterator.
Class for BALANCED cell list implementation.
Definition: CellListBal.hpp:39
void add(const Point< dim, T > &pos, typename base::value_type ele)
Add an element in the cell list.
T get(int i) const
Get coordinate.
Definition: Point.hpp:42
grid_key_dx< dim > get()
Return the actual grid key iterator.
it iterate through the elements of a cell
void reset()
Reset the iterator (it restart from the beginning)
This class represent an N-dimensional box.
Definition: Box.hpp:56
This class is a trick to indicate the compiler a specific specialization pattern. ...
Definition: memory_c.hpp:202
CellNNIterator< dim, CellList< dim, T, BALANCED, base >, FULL, impl > getNNIterator(size_t cell)
Get the Nearest Neighborhood iterator.
size_t getNelements(size_t cell_id)
Return the number of element in the cell.
void Initialize(SpaceBox< dim, T > &box, size_t(&div)[dim], Point< dim, T > &orig, const size_t pad=1)
grid_sm< dim, void > & getGrid()
Return the underlying grid information of the cell list.
Definition: CellListBal.hpp:81
Cell list structure.
Definition: CellList.hpp:40
void swap(CellList< dim, T, BALANCED, base > &cl)
Swap the memory.
Iterator for the neighborhood of the cell structures.
void Initialize(Box< dim, T > &box, size_t(&div)[dim], Point< dim, T > &orig, const size_t pad=1)
Definition: CellListBal.hpp:94