NBSLevels.hpp
Go to the documentation of this file.
1 #ifndef __NBSLevels_hpp__
2 #define __NBSLevels_hpp__
3 
4 #include "NBSLevel.hpp"
5 #include "NBSLevel0.hpp"
6 #include "KokkosTypes.hpp"
7 
8 namespace pFlow
9 {
10 
11 template<typename executionSpace>
12 class NBSLevels
13 {
14 
15 public:
16 
18 
20 
21  using IdType = typename NBSLevelType::IdType;
22 
24 
25  using Cells = typename NBSLevelType::Cells;
26 
27  using CellType = typename Cells::CellType;
28 
30 
32 
34 
35 protected:
36 
38 
40 
42 
44 
46 
48 
50 
52 
54 
56 
57  using rangePolicyType =
58  Kokkos::RangePolicy<
59  Kokkos::IndexType<int32>,
60  Kokkos::Schedule<Kokkos::Static>,
62 
64  {
65 
66  int32 maxOvermin = static_cast<int32>(maxSize_/minSize_);
67 
68  if (maxOvermin <=1)
69  return 1;
70  else if(maxOvermin<=3)
71  return 2;
72  else if(maxOvermin<=7)
73  return 3;
74  else if(maxOvermin<15)
75  return 4;
76  else if(maxOvermin<31)
77  return 5;
78  else if(maxOvermin<63)
79  return 6;
80  else if(maxOvermin <127)
81  return 7;
82  else
83  {
85  "size ratio is not valid for multi-grid NBS "<< maxOvermin<<endl;
86  fatalExit;
87  }
88 
89  return -1;
90  }
91 
92  bool setDiameterRange(real sizeRatio)
93  {
94  real lvl_maxD = sizeRatio* maxSize_;
95  real lvl_minD = lvl_maxD/2.0;
96 
97  for(int32 lvl=numLevels_-1; lvl>=0; lvl--)
98  {
99 
100  if(lvl == 0 ) lvl_minD = 0.01*minSize_;
101 
102  sizeRangeLevelsHost_[lvl] = {lvl_minD, lvl_maxD};
103  maxSizeLevelsHost_[lvl] = lvl_maxD;
104  lvl_maxD = lvl_minD;
105  lvl_minD /= 2.0;
106  }
107 
110 
111  REPORT(2)<<"Grids with "<< yellowText(numLevels_)<< " levels have been created."<<endREPORT;
112  for(int32 lvl=0; lvl<numLevels_; lvl++)
113  {
114  REPORT(3)<<"Cell gird No "<< yellowText(lvl)<<" with size range ("
115  <<sizeRangeLevelsHost_[lvl].first<<","<<sizeRangeLevelsHost_[lvl].second<<"]."<<endREPORT;
116  }
117 
118  return true;
119  }
120 
122  const box& domain,
123  real sizeRatio,
124  const ViewType1D<realx3, memory_space>& position,
125  const ViewType1D<real, memory_space>& diam)
126  {
127 
128 
129  for(int32 lvl = 0; lvl<numLevels_; lvl++)
130  {
131  nbsLevels_[lvl] = NBSLevelType(
132  lvl,
133  domain,
134  maxSizeLevelsHost_[lvl]*sizeRatio,
135  sizeRatio,
136  position,
137  diam );
138  }
139 
140  auto next0 = nbsLevels_[0].next();
141  for(int32 lvl=1; lvl<numLevels_; lvl++)
142  {
143  nbsLevels_[lvl].setNext(next0);
144  }
145 
146  return true;
147  }
148 
149 
151  {
152  activeRange_ = active;
153 
154  if(activeRange_.second > nbsLevels_[0].capacity())
155  {
156  nbsLevels_[0].checkAllocateNext(activeRange_.second);
157 
158  auto next0 = nbsLevels_[0].next();
159 
160  for(int32 lvl=1; lvl<numLevels_; lvl++)
161  {
162  nbsLevels_[lvl].setNext(next0);
163  }
164  }
165  }
166 
167  void nullify( range active)
168  {
169  for(int32 lvl=0; lvl<numLevels_; lvl++)
170  {
171  nbsLevels_[lvl].nullify(active);
172  }
173  }
174 
175 public:
176 
178  const box& domain,
179  real minSize,
180  real maxSize,
181  real sizeRatio,
182  const ViewType1D<realx3, memory_space>& position,
183  const ViewType1D<real, memory_space>& diam)
184  :
185  minSize_(minSize),
186  maxSize_(maxSize),
188  nbsLevels_("nbsLevels", numLevels_, numLevels_, RESERVE()),
189  sizeRangeLevels_("sizeRangeLevels", numLevels_),
190  sizeRangeLevelsHost_("sizeRangeLevelsHost", numLevels_),
191  maxSizeLevels_("maxSizeLevels", numLevels_),
192  maxSizeLevelsHost_("maxSizeLevelsHost", numLevels_)
193  {
194 
195  setDiameterRange(sizeRatio);
196 
197  initLevels(domain, sizeRatio, position, diam);
198  }
199 
200  auto getCellIterator(int32 lvl)const
201  {
202  return nbsLevels_[lvl].getCellIterator();
203  }
204 
206  {
207  return numLevels_;
208  }
209 
210  Cells getCells(int32 lvl)const
211  {
212  return nbsLevels_[lvl].getCells();
213  }
214 
215  template<typename PairsContainer>
217  bool findPairs(PairsContainer& pairs)
218  {
219 
220  int32 getFull = 1;
221 
222  // loop until the container size fits the numebr of contact pairs
223  while (getFull > 0)
224  {
225 
226  getFull = findPairsCount(pairs);
227 
228  if(getFull)
229  {
230  // - resize the container
231  // note that getFull now shows the number of failed insertions.
232  uint32 len = max(getFull,100) ;
233 
234  auto oldCap = pairs.capacity();
235 
236  pairs.increaseCapacityBy(len);
237 
238  INFORMATION<< "The contact pair container capacity increased from "<<
239  oldCap << " to "<<pairs.capacity()<<" in NBSLevels."<<endINFO;
240 
241  }
242 
243  Kokkos::fence();
244  }
245 
246  return true;
247  }
248 
249 
250  template<typename PairsContainer>
252  int32 findPairsCount(PairsContainer& pairs)
253  {
254 
255  int32 notInsertedCount = 0;
256 
257  for(int32 lvl=0; lvl<numLevels_; lvl++)
258  {
259 
260  // the same level
261  notInsertedCount+= nbsLevels_[lvl].findPairsCount(pairs);
262 
263  for(int32 crsLvl = lvl+1; crsLvl<numLevels_; crsLvl++)
264  {
265  // cross levels
266  notInsertedCount+=
267  nbsLevels_[lvl].findPairsCountCross(pairs, nbsLevels_[crsLvl]);
268 
269  }
270  }
271 
272  return notInsertedCount;
273  }
274 
276  void build(range activeRange)
277  {
278 
279  // nullify next and heads
280  findParticleLevel(activeRange.first, activeRange.second);
281  manageAllocateNext(activeRange);
282  nullify(activeRange);
283 
284  //
285 
286  rangePolicyType rPolicy(activeRange.first, activeRange.second);
287 
288  auto nbsLevel0 = nbsLevels_[0];
289  auto pointPosition = nbsLevel0.pointPosition();
290  auto particleLevel = particleLevel_;
291  auto next = nbsLevel0.next();
292  auto head0 = nbsLevel0.head();
293 
294  typename NBSLevelType::HeadType head1, head2, head3, head4, head5, head6;
295 
296  if(numLevels_>1) head1 = nbsLevels_[1].head();
297  if(numLevels_>2) head2 = nbsLevels_[2].head();
298  if(numLevels_>3) head3 = nbsLevels_[3].head();
299  if(numLevels_>4) head4 = nbsLevels_[4].head();
300  if(numLevels_>5) head5 = nbsLevels_[5].head();
301  if(numLevels_>6) head6 = nbsLevels_[6].head();
302 
303 
304 
305  Kokkos::parallel_for(
306  "NBSLevels::build",
307  rPolicy,
308  LAMBDA_HD(int32 i){
309 
310  int8 lvl = particleLevel[i];
311  auto ind = nbsLevel0.pointIndex(pointPosition[i]);
312  ind = mapIndexLevels(ind, 0, lvl);
313  int32 old;
314  if(lvl==0)
315  old =Kokkos::atomic_exchange(&head0(ind.x(), ind.y(), ind.z()),i);
316  else if(lvl==1)
317  old =Kokkos::atomic_exchange(&head1(ind.x(), ind.y(), ind.z()),i);
318  else if(lvl==2)
319  old =Kokkos::atomic_exchange(&head2(ind.x(), ind.y(), ind.z()),i);
320  else if(lvl==3)
321  old =Kokkos::atomic_exchange(&head3(ind.x(), ind.y(), ind.z()),i);
322  else if(lvl==4)
323  old =Kokkos::atomic_exchange(&head4(ind.x(), ind.y(), ind.z()),i);
324  else if(lvl==5)
325  old =Kokkos::atomic_exchange(&head5(ind.x(), ind.y(), ind.z()),i);
326  else if(lvl==6)
327  old =Kokkos::atomic_exchange(&head6(ind.x(), ind.y(), ind.z()),i);
328 
329  next(i) = old;
330  });
331 
332  Kokkos::fence();
333  }
334 
335  template<typename IncludeFunction>
337  void build(range activeRange, IncludeFunction incld)
338  {
339  // nullify next and heads
340  findParticleLevel(activeRange.first, activeRange.second);
341  manageAllocateNext(activeRange);
342  nullify(activeRange);
343 
344 
345  rangePolicyType rPolicy(activeRange.first, activeRange.second);
346 
347  auto nbsLevel0 = nbsLevels_[0];
348  auto pointPosition = nbsLevel0.pointPosition();
349  auto particleLevel = particleLevel_;
350  auto next = nbsLevel0.next();
351  auto head0 = nbsLevel0.head();
352 
353  typename NBSLevelType::HeadType head1, head2, head3, head4, head5, head6;
354 
355  if(numLevels_>1) head1 = nbsLevels_[1].head();
356  if(numLevels_>2) head2 = nbsLevels_[2].head();
357  if(numLevels_>3) head3 = nbsLevels_[3].head();
358  if(numLevels_>4) head4 = nbsLevels_[4].head();
359  if(numLevels_>5) head5 = nbsLevels_[5].head();
360  if(numLevels_>6) head6 = nbsLevels_[6].head();
361 
362  Kokkos::parallel_for(
363  "NBSLevels::build",
364  rPolicy,
365  LAMBDA_HD(int32 i){
366  if(!incld(i)) return;
367 
368  int8 lvl = particleLevel[i];
369  auto ind = nbsLevel0.pointIndex(pointPosition[i]);
370 
371  ind = mapIndexLevels(ind, 0, lvl);
372  int32 old;
373  if(lvl==0)
374  old =Kokkos::atomic_exchange(&head0(ind.x(), ind.y(), ind.z()),i);
375  else if(lvl==1)
376  old =Kokkos::atomic_exchange(&head1(ind.x(), ind.y(), ind.z()),i);
377  else if(lvl==2)
378  old =Kokkos::atomic_exchange(&head2(ind.x(), ind.y(), ind.z()),i);
379  else if(lvl==3)
380  old =Kokkos::atomic_exchange(&head3(ind.x(), ind.y(), ind.z()),i);
381  else if(lvl==4)
382  old =Kokkos::atomic_exchange(&head4(ind.x(), ind.y(), ind.z()),i);
383  else if(lvl==5)
384  old =Kokkos::atomic_exchange(&head5(ind.x(), ind.y(), ind.z()),i);
385  else if(lvl==6)
386  old =Kokkos::atomic_exchange(&head6(ind.x(), ind.y(), ind.z()),i);
387 
388  next(i) = old;
389 
390  });
391 
392  Kokkos::fence();
393 
394  }
395 
396  bool findParticleLevel(int32 first, int32 last)
397  {
398 
399  if(last > particleLevel_.size())
400  {
402  }
403 
404  auto diameter = nbsLevels_[0].diameter();
405  auto const maxSizes = maxSizeLevels_;
406  auto particleLevel = particleLevel_;
407  auto const sizeRatio = 0.999*nbsLevels_[0].sizeRatio();
408 
409  int8 maxLvl = sizeRangeLevels_.size();
410 
411  rangePolicyType rPolicy(first, last);
412 
413  Kokkos::parallel_for(
414  "NBSLevels::findParticleLevel",
415  rPolicy,
416  LAMBDA_HD(int32 i)
417  {
418  for(int8 lvl = 0; lvl<maxLvl; lvl++)
419  {
420  if( sizeRatio*diameter[i]<= maxSizes[lvl] )
421  {
422  particleLevel[i] = lvl;
423  return;
424  }
425  }
426  particleLevel[i] = static_cast<int8>(-1);
427  });
428  Kokkos::fence();
429 
430  return true;
431  }
432 
433 
434 }; //NBSLevels
435 
436 }
437 
438 #endif
NBSLevel.hpp
pFlow::NBSLevel::IndexType
typename NBSLevel0Type::IndexType IndexType
Definition: NBSLevel.hpp:28
pFlow::NBSLevels::execution_space
typename NBSLevelType::execution_space execution_space
Definition: NBSLevels.hpp:29
pFlow::NBSLevels::IndexType
typename NBSLevelType::IndexType IndexType
Definition: NBSLevels.hpp:23
endREPORT
#define endREPORT
Definition: streams.hpp:41
pFlow::NBSLevels::maxSizeLevelsHost_
ViewType1D< real, HostSpace > maxSizeLevelsHost_
Definition: NBSLevels.hpp:51
pFlow::real
float real
Definition: builtinTypes.hpp:46
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::NBSLevels
Definition: NBSLevels.hpp:12
REPORT
#define REPORT(n)
Definition: streams.hpp:40
pFlow::NBSLevels::CellType
typename Cells::CellType CellType
Definition: NBSLevels.hpp:27
pFlow::NBSLevels::manageAllocateNext
void manageAllocateNext(range active)
Definition: NBSLevels.hpp:150
pFlow::copy
INLINE_FUNCTION_H void copy(const ViewType1D< dType, dProperties... > &dst, const ViewType1D< sType, sProperties... > &src)
Definition: ViewAlgorithms.hpp:296
pFlow::NBSLevels::nbsLevels_
Vector< NBSLevelType > nbsLevels_
Definition: NBSLevels.hpp:43
pFlow::NBSLevels::particleLevel_
ViewType1D< int8, memory_space > particleLevel_
Definition: NBSLevels.hpp:53
NBSLevel0.hpp
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:59
KokkosTypes.hpp
pFlow::NBSLevel::memory_space
typename NBSLevel0Type::memory_space memory_space
Definition: NBSLevel.hpp:36
pFlow::reallocNoInit
INLINE_FUNCTION_H void reallocNoInit(ViewType1D< Type, Properties... > &view, int32 len)
Definition: KokkosUtilities.hpp:60
pFlow::NBSLevels::realRange
kPair< real, real > realRange
Definition: NBSLevels.hpp:33
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::NBSLevels::setNumLevels
int32 setNumLevels()
Definition: NBSLevels.hpp:63
pFlow::NBSLevels::maxSizeLevels_
ViewType1D< real, memory_space > maxSizeLevels_
Definition: NBSLevels.hpp:49
pFlow::NBSLevels::build
INLINE_FUNCTION_H void build(range activeRange, IncludeFunction incld)
Definition: NBSLevels.hpp:337
pFlow
Definition: demComponent.hpp:28
pFlow::NBSLevels::numLevels_
int32 numLevels_
Definition: NBSLevels.hpp:41
pFlow::NBSLevels::initLevels
bool initLevels(const box &domain, real sizeRatio, const ViewType1D< realx3, memory_space > &position, const ViewType1D< real, memory_space > &diam)
Definition: NBSLevels.hpp:121
RESERVE
Definition: Vector.hpp:38
pFlow::NBSLevels::cellIterator
typename NBSLevelType::cellIterator cellIterator
Definition: NBSLevels.hpp:19
pFlow::NBSLevels::setDiameterRange
bool setDiameterRange(real sizeRatio)
Definition: NBSLevels.hpp:92
pFlow::NBSLevels::build
INLINE_FUNCTION_H void build(range activeRange)
Definition: NBSLevels.hpp:276
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::NBSLevels::minSize_
real minSize_
Definition: NBSLevels.hpp:37
pFlow::NBSLevels::findPairsCount
INLINE_FUNCTION_H int32 findPairsCount(PairsContainer &pairs)
Definition: NBSLevels.hpp:252
pFlow::NBSLevel::cellIterator
typename NBSLevel0Type::cellIterator cellIterator
Definition: NBSLevel.hpp:24
pFlow::NBSLevels::memory_space
typename NBSLevelType::memory_space memory_space
Definition: NBSLevels.hpp:31
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:53
pFlow::mapIndexLevels
INLINE_FUNCTION_HD int32x3 mapIndexLevels(const int32x3 &ind, int32 lowerLevel, int32 upperLevel)
Definition: NBSLevel.hpp:118
pFlow::NBSLevels::numLevels
int32 numLevels() const
Definition: NBSLevels.hpp:205
pFlow::max
T max(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:164
pFlow::NBSLevels::findParticleLevel
bool findParticleLevel(int32 first, int32 last)
Definition: NBSLevels.hpp:396
pFlow::NBSLevels::findPairs
INLINE_FUNCTION_H bool findPairs(PairsContainer &pairs)
Definition: NBSLevels.hpp:217
pFlow::NBSLevels::nullify
void nullify(range active)
Definition: NBSLevels.hpp:167
pFlow::NBSLevels::sizeRangeLevelsHost_
ViewType1D< realRange, HostSpace > sizeRangeLevelsHost_
Definition: NBSLevels.hpp:47
pFlow::NBSLevels::getCellIterator
auto getCellIterator(int32 lvl) const
Definition: NBSLevels.hpp:200
pFlow::box
Definition: box.hpp:32
pFlow::NBSLevels::rangePolicyType
Kokkos::RangePolicy< Kokkos::IndexType< int32 >, Kokkos::Schedule< Kokkos::Static >, execution_space > rangePolicyType
Definition: NBSLevels.hpp:61
pFlow::NBSLevel::IdType
typename NBSLevel0Type::IdType IdType
Definition: NBSLevel.hpp:26
LAMBDA_HD
#define LAMBDA_HD
Definition: pFlowMacros.hpp:54
pFlow::NBSLevels::maxSize_
real maxSize_
Definition: NBSLevels.hpp:39
pFlow::NBSLevel
Definition: NBSLevel.hpp:15
pFlow::ViewType1D
Kokkos::View< T *, properties... > ViewType1D
Definition: KokkosTypes.hpp:62
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:49
pFlow::NBSLevel::HeadType
typename NBSLevel0Type::HeadType HeadType
Definition: NBSLevel.hpp:40
pFlow::NBSLevel::execution_space
typename NBSLevel0Type::execution_space execution_space
Definition: NBSLevel.hpp:34
pFlow::NBSLevels::NBSLevelType
NBSLevel< executionSpace > NBSLevelType
Definition: NBSLevels.hpp:17
yellowText
#define yellowText(text)
Definition: streams.hpp:30
pFlow::Vector
Definition: Vector.hpp:46
endINFO
#define endINFO
Definition: streams.hpp:38
pFlow::NBSLevels::activeRange_
range activeRange_
Definition: NBSLevels.hpp:55
pFlow::NBSLevel::Cells
typename NBSLevel0Type::Cells Cells
Definition: NBSLevel.hpp:30
pFlow::NBSLevels::IdType
typename NBSLevelType::IdType IdType
Definition: NBSLevels.hpp:21
pFlow::NBSLevels::getCells
Cells getCells(int32 lvl) const
Definition: NBSLevels.hpp:210
pFlow::range
kPair< int, int > range
Definition: KokkosTypes.hpp:54
pFlow::NBSLevels::Cells
typename NBSLevelType::Cells Cells
Definition: NBSLevels.hpp:25
pFlow::NBSLevels::sizeRangeLevels_
ViewType1D< realRange, memory_space > sizeRangeLevels_
Definition: NBSLevels.hpp:45
INFORMATION
#define INFORMATION
Definition: streams.hpp:37
pFlow::kPair
Kokkos::pair< T1, T2 > kPair
Definition: KokkosTypes.hpp:52
pFlow::NBSLevels::NBSLevels
NBSLevels(const box &domain, real minSize, real maxSize, real sizeRatio, const ViewType1D< realx3, memory_space > &position, const ViewType1D< real, memory_space > &diam)
Definition: NBSLevels.hpp:177