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
CellListMem.hpp
1 /*
2  * CelListMem.hpp
3  *
4  * Created on: Mar 22, 2015
5  * Last modified: June 25, 2015
6  * Authors: Pietro Incardona, Yaroslav Zaluzhnyi
7  */
8 
9 #ifndef CELLISTMEM_HPP_
10 #define CELLISTMEM_HPP_
11 
12 #include <unordered_map>
13 
33 template<unsigned int dim, typename T, typename transform, typename base>
34 class CellList<dim,T,MEMORY,transform,base> : public CellDecomposer_sm<dim,T,transform>
35 {
36  // The array contain the neighborhood of the cell-id in case of asymmetric interaction
37  //
38  // * * *
39  // * x *
40  // * * *
41 
42  long int NNc_full[openfpm::math::pow(3,dim)];
43 
44  // The array contain the neighborhood of the cell-id in case of symmetric interaction
45  //
46  // * * *
47  // x *
48  //
49  long int NNc_sym[openfpm::math::pow(3,dim)/2+1];
50 
51  // The array contain the neighborhood of the cell-id in case of symmetric interaction (Optimized)
52  //
53  // * *
54  // x *
55  //
56  long int NNc_cr[openfpm::math::pow(2,dim)];
57 
58  // each cell has a dynamic structure
59  // that store the elements in the cell
60  std::unordered_map<size_t,base> cl_base;
61 
62  //Origin point
63  Point<dim,T> orig;
64 
65 public:
66 
67  // Object type that the structure store
68  typedef T value_type;
69 
76  {
77  CellDecomposer_sm<dim,T>::getGrid();
78  }
79 
88  void Initialize(Box<dim,T> & box, size_t (&div)[dim], Point<dim,T> & orig, const size_t pad = 1)
89  {
90  SpaceBox<dim,T> sbox;
91  Initialize(sbox,div,orig);
92  }
93 
102  void Initialize(SpaceBox<dim,T> & box, size_t (&div)[dim], Point<dim,T> & orig, const size_t pad = 1)
103  {
104  // Add padding
105  size_t div_pad[dim];
106  for (size_t i = 0 ; i < dim ; i++)
107  div_pad[i] = div[i] + 2;
108 
109  CellDecomposer_sm<dim,T>::setDimensions(box,div_pad, pad);
110 
111  this->orig = orig;
112 
113  //filling a map with "base" structures
114  for (int i = 0; i < this->tot_n_cell; i++)
115  cl_base[i] = base();
116 
117 
118  // Calculate the NNc-arrays (for neighborhood):
119 
120  // compile-time array {0,0,0,....} and {3,3,3,...}
121 
122  typedef typename generate_array<size_t,dim, Fill_zero>::result NNzero;
123  typedef typename generate_array<size_t,dim, Fill_two>::result NNtwo;
124  typedef typename generate_array<size_t,dim, Fill_one>::result NNone;
125 
126  // Generate the sub-grid iterator
127 
128  grid_key_dx_iterator_sub<dim> gr_sub3(this->gr_cell,NNzero::data,NNtwo::data);
129 
130  // Calculate the NNc array
131 
132  size_t middle = this->gr_cell.LinId(NNone::data);
133  size_t i = 0;
134  while (gr_sub3.isNext())
135  {
136  NNc_full[i] = (long int)this->gr_cell.LinId(gr_sub3.get()) - middle;
137 
138  ++gr_sub3;
139  i++;
140  }
141 
142  // Calculate the NNc_sym array
143 
144  i = 0;
145  gr_sub3.reset();
146  while (gr_sub3.isNext())
147  {
148  auto key = gr_sub3.get();
149 
150  size_t lin = this->gr_cell.LinId(key);
151 
152  // Only the first half is considered
153  if (lin < middle)
154  {
155  ++gr_sub3;
156  continue;
157  }
158 
159  NNc_sym[i] = lin - middle;
160 
161  ++gr_sub3;
162  i++;
163  }
164 
165  // Calculate the NNc_cross array
166 
167  i = 0;
168  grid_key_dx_iterator_sub<dim> gr_sub2(this->gr_cell,NNzero::data,NNone::data);
169 
170  while (gr_sub2.isNext())
171  {
172  auto key = gr_sub2.get();
173 
174  NNc_cr[i] = (long int)this->gr_cell.LinId(key);
175 
176  ++gr_sub2;
177  i++;
178  }
179  }
180 
185  {
186  }
187 
188 
196  CellList(Box<dim,T> & box, size_t (&div)[dim], Point<dim,T> & orig, const size_t pad = 1)
197  {
198  SpaceBox<dim,T> sbox(box);
199  Initialize(sbox,div,orig,pad);
200  }
201 
209  CellList(SpaceBox<dim,T> & box, size_t (&div)[dim], Point<dim,T> & orig, const size_t pad = 1)
210  {
211  Initialize(box,div,orig,pad);
212  }
213 
214 
221  void add(const T (& pos)[dim], typename base::value_type ele)
222  {
223  // calculate the Cell id
224 
225  size_t cell_id = this->getCell(pos);
226 
227  // Get the number of element the cell is storing
228 
229  cl_base[cell_id].add(ele);
230  }
231 
238  void add(const Point<dim,T> & pos, typename base::value_type ele)
239  {
240  // calculate the Cell id
241 
242  size_t cell_id = this->getCell(pos);
243 
244  // add a new element
245 
246  cl_base[cell_id].add(ele);
247  }
248 
249 
256  void remove(size_t cell, size_t ele)
257  {
258  cl_base[cell].remove(ele);
259  }
260 
268  size_t getNelements(size_t cell_id)
269  {
270  return cl_base[cell_id].size();
271  }
272 
279  auto get(size_t cell, size_t ele) -> decltype(cl_base[cell].get(ele))
280  {
281  return cl_base[cell].get(ele);
282  }
289  {
290  cl_base.swap(cl.cl_base);
291  }
292 
299  {
300  return CellIterator<CellList<dim,T,MEMORY,base>>(cell,*this);
301  }
302 
308  template<unsigned int impl> CellNNIterator<dim,CellList<dim,T,MEMORY,base>,FULL,impl> getNNIterator(size_t cell)
309  {
310  CellNNIterator<dim,CellList<dim,T,MEMORY,base>,FULL,impl> cln(cell,NNc_full,*this);
311 
312  return cln;
313  }
314 
315  template<unsigned int impl> CellNNIterator<dim,CellList<dim,T,MEMORY,base>,SYM,impl> getNNIteratorSym(size_t cell)
316  {
317  CellNNIterator<dim,CellList<dim,T,MEMORY,base>,SYM,impl> cln(cell,NNc_sym,*this);
318 
319  return cln;
320  }
321 
322  template<unsigned int impl> CellNNIterator<dim,CellList<dim,T,MEMORY,base>,CRS,impl> getNNIteratorCross(size_t cell)
323  {
324  CellNNIterator<dim,CellList<dim,T,MEMORY,base>,CRS,impl> cln(cell,NNc_cr,*this);
325 
326  return cln;
327  }
328 };
329 
330 
331 #endif /* CELLISTMEM_HPP_ */
void add(const T(&pos)[dim], typename base::value_type ele)
Add an element in the cell list.
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.
CellIterator< CellList< dim, T, MEMORY, base > > getIterator(size_t cell)
Get the Cell iterator.
CellNNIterator< dim, CellList< dim, T, MEMORY, base >, FULL, impl > getNNIterator(size_t cell)
Get the Nearest Neighborhood iterator.
bool isNext()
Check if there is the next element.
This class implement the point shape in an N-dimensional space.
Definition: Point.hpp:20
CellList(SpaceBox< dim, T > &box, size_t(&div)[dim], Point< dim, T > &orig, const size_t pad=1)
Cell list.
void add(const Point< dim, T > &pos, typename base::value_type ele)
Add an element in the cell list.
size_t getNelements(size_t cell_id)
Return the number of element in the cell.
grid_key_dx< dim > get()
Return the actual grid key iterator.
void swap(CellList< dim, T, MEMORY, base > &cl)
Swap the memory.
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
void Initialize(SpaceBox< dim, T > &box, size_t(&div)[dim], Point< dim, T > &orig, const size_t pad=1)
Cell list structure.
Definition: CellList.hpp:40
void Initialize(Box< dim, T > &box, size_t(&div)[dim], Point< dim, T > &orig, const size_t pad=1)
Definition: CellListMem.hpp:88
Iterator for the neighborhood of the cell structures.
grid_sm< dim, void > & getGrid()
Return the underlying grid information of the cell list.
Definition: CellListMem.hpp:75