mapperNBS.hpp
Go to the documentation of this file.
1 /*------------------------------- phasicFlow ---------------------------------
2  O C enter of
3  O O E ngineering and
4  O O M ultiscale modeling of
5  OOOOOOO F luid flow
6 ------------------------------------------------------------------------------
7  Copyright (C): www.cemf.ir
8  email: hamid.r.norouzi AT gmail.com
9 ------------------------------------------------------------------------------
10 Licence:
11  This file is part of phasicFlow code. It is a free software for simulating
12  granular and multiphase flows. You can redistribute it and/or modify it under
13  the terms of GNU General Public License v3 or any other later versions.
14 
15  phasicFlow is distributed to help others in their research in the field of
16  granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
17  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 
19 -----------------------------------------------------------------------------*/
20 
21 
22 #ifndef __mapperNBS_hpp__
23 #define __mapperNBS_hpp__
24 
25 #include "cells.hpp"
27 #include "baseAlgorithms.hpp"
28 #include "ViewAlgorithms.hpp"
29 
30 namespace pFlow
31 {
32 
33 template<typename executionSpace>
34 class mapperNBS
35 :
36  public cells<int32>
37 {
38 public:
39 
40  using IdType = int32;
41 
42  using IndexType = int32;
43 
45 
46  using CellType = typename Cells::CellType;
47 
48  using execution_space = executionSpace;
49 
50  using memory_space = typename execution_space::memory_space;
51 
53 
55 
57  {
58  private:
60 
62 
63  public:
64 
66  :
67  head_(head),
68  next_(next)
69  {}
70 
72  Cells cellsSize()const {
73  return Cells(head_.extent(0), head_.extent(1), head_.extent(2));}
74 
77  return head_(i,j,k); }
78 
80  int32 getNext(int32 n)const {
81  if(n<0) return n;
82  return next_(n); }
83  };
84 
85 protected:
86 
88 
90 
92 
93  bool nextOwner_ = true;
94 
95  // borrowed ownership
97 
98  using rangePolicyType =
99  Kokkos::RangePolicy<
100  Kokkos::IndexType<int32>,
101  Kokkos::Schedule<Kokkos::Static>,
103 
105  void nullifyHead()
106  {
107  fill(
108  head_,
109  range(0,this->nx()),
110  range(0,this->ny()),
111  range(0,this->nz()),
112  static_cast<int32>(-1)
113  );
114  }
115 
116  void nullifyNext(range nextRng)
117  {
118  if(!nextOwner_)return;
119  fill(
120  next_,
121  nextRng,
122  static_cast<int32>(-1)
123  );
124  }
125 
126  void nullify()
127  {
128  nullifyHead();
129 
131  }
132 
133  void nullify(range nextRng)
134  {
135  nullifyHead();
136 
137  nullifyNext(nextRng);
138  }
139 
140 
141  void checkAllocateNext(int newCap)
142  {
143  if( capacity_ < newCap)
144  {
145  capacity_ = newCap;
146  if(!nextOwner_)return;
148  }
149  }
150 
152  {
153  reallocNoInit(head_, this->nx(), this->ny(), this->nz());
154  }
155 
156 
157 
158 public:
159 
160  TypeInfoNV("mapperNBS");
161 
164 
166  const box& domain,
167  real cellSize,
168  const ViewType1D<realx3, memory_space>& position,
169  bool nextOwner = true)
170  :
172  pointPosition_(position),
173  head_(
174  "mapperNBS::head_",
175  this->nx(),
176  this->ny(),
177  this->nz()
178  ),
179  next_("mapperNBS::next_",1), //,position.size()),
180  nextOwner_(nextOwner)
181  {
183  }
184 
186  const box& domain,
187  int32 nx,
188  int32 ny,
189  int32 nz,
190  const ViewType1D<realx3, memory_space>& position,
191  bool nextOwner = true)
192  :
193  Cells(domain, nx, ny, nz),
194  pointPosition_(position),
195  head_("mapperNBS::head_",nx,ny,nz),
196  next_("mapperNBS::next_",1),
197  nextOwner_(nextOwner)
198  {
200  }
201 
202 
204  mapperNBS(const mapperNBS&) = default;
205 
207  mapperNBS& operator = (const mapperNBS&) = default;
208 
210  ~mapperNBS()=default;
211 
214  auto capacity()const
215  {
216  return capacity_;
217  }
218 
219  cellIterator getCellIterator()const
220  {
221  return cellIterator(head_, next_);
222  }
223 
225  {
226  checkAllocateNext(newCap);
227  return true;
228  }
229 
231  auto& head()
232  {
233  return head_;
234  }
235 
237  auto& next()
238  {
239  return next_;
240  }
241 
243  const auto& head()const
244  {
245  return head_;
246  }
247 
249  const auto& next()const
250  {
251  return next_;
252  }
253 
256  {
257  return pointPosition_;
258  }
259 
262  {
263  if(!nextOwner_)
264  {
265  next_ = next;
266  capacity_ = next.size();
267  }
268  }
269 
270 
271 
272  // - build based on all points in active range
274  void build(range activeRange)
275  {
276  checkAllocateNext(activeRange.second);
277  nullify(activeRange);
278 
279  Cells cellIndex = static_cast<Cells>(*this);
280  auto points = pointPosition_;
281  auto next = next_;
282  auto head = head_;
283 
284  rangePolicyType rPolicy(activeRange.first, activeRange.second);
285 
286  Kokkos::parallel_for(
287  "mapperNBS::build",
288  rPolicy,
289  LAMBDA_HD(int32 i){
290  CellType ind = cellIndex.pointIndex(points[i]);
291  int32 old = Kokkos::atomic_exchange(&head(ind.x(), ind.y(), ind.z()), i);
292  next[i] = old;
293  });
294  Kokkos::fence();
295  }
296 
297 
298  template<typename IncludeFunction>
300  void build(range activeRange, IncludeFunction incld)
301  {
302  checkAllocateNext(activeRange.second);
303  nullify(activeRange);
304 
305  Cells cellIndex = static_cast<Cells>(*this);
306  auto points = pointPosition_;
307  auto next = next_;
308  auto head = head_;
309 
310  rangePolicyType rPolicy(activeRange.first, activeRange.second);
311 
312  Kokkos::parallel_for(
313  "mapperNBS::build_Include",
314  rPolicy,
315  LAMBDA_HD(int32 i){
316  if( incld(i) )
317  {
318  CellType ind = cellIndex.pointIndex(points[i]);
319  auto old = Kokkos::atomic_exchange(&head(ind.x(), ind.y(), ind.z()), i);
320  next[i] = old;
321  }
322  });
323  Kokkos::fence();
324 
325  }
326 
327 
329  void buildCheckInDomain(range activeRange)
330  {
331  checkAllocateNext(activeRange.second);
332  nullify(activeRange);
333 
334  Cells cellIndex = static_cast<Cells>(*this);
335  auto points = pointPosition_;
336  auto next = next_;
337  auto head = head_;
338 
339  rangePolicyType rPolicy(activeRange.first, activeRange.second);
340 
341  Kokkos::parallel_for(
342  "mapperNBS::buildCheckInDomain",
343  rPolicy,
344  LAMBDA_HD(int32 i){
345  CellType ind;
346  if( cellIndex.pointIndexInDomain(points[i], ind) )
347  {
348  int32 old = Kokkos::atomic_exchange(&head(ind.x(), ind.y(), ind.z()), i);
349  next[i] = old;
350  }
351  });
352 
353  Kokkos::fence();
354 
355  }
356 
357  template<typename IncludeFunction>
359  void buildCheckInDomain(range activeRange, IncludeFunction incld)
360  {
361  checkAllocateNext(activeRange.second);
362  nullify(activeRange);
363 
364  Cells cellIndex = static_cast<Cells>(*this);
365  auto points = pointPosition_;
366  auto next = next_;
367  auto head = head_;
368 
369  rangePolicyType rPolicy(activeRange.first, activeRange.second);
370 
371  Kokkos::parallel_for(
372  "mapperNBS::buildCheckInDomain_Include",
373  rPolicy,
374  LAMBDA_HD(int32 i){
375  CellType ind;
376  if( incld(i) && cellIndex.pointIndexInDomain(points[i], ind) )
377  {
378  auto old = Kokkos::atomic_exchange(&head(ind.x(), ind.y(), ind.z()), i);
379  next[i] = old;
380  }
381  });
382  Kokkos::fence();
383  }
384 
385 };
386 
387 } // pFlow
388 
389 #endif // __mapperNBS_hpp__
pFlow::mapperNBS< DefaultHostExecutionSpace >::rangePolicyType
Kokkos::RangePolicy< Kokkos::IndexType< int32 >, Kokkos::Schedule< Kokkos::Static >, execution_space > rangePolicyType
Definition: mapperNBS.hpp:102
pFlow::mapperNBS::mapperNBS
mapperNBS(const box &domain, real cellSize, const ViewType1D< realx3, memory_space > &position, bool nextOwner=true)
Definition: mapperNBS.hpp:165
pFlow::mapperNBS::head_
ViewType3D< int32, memory_space > head_
Definition: mapperNBS.hpp:89
pFlow::mapperNBS::cellIterator::start
INLINE_FUNCTION_HD int32 start(IndexType i, IndexType j, IndexType k) const
Definition: mapperNBS.hpp:76
pFlow::cells< int32 >::nz
INLINE_FUNCTION_HD int32 nz() const
Definition: cells.hpp:139
pFlow::mapperNBS::next_
ViewType1D< int32, memory_space > next_
Definition: mapperNBS.hpp:91
pFlow::real
float real
Definition: builtinTypes.hpp:46
pFlow::fill
void fill(Vector< T, Allocator > &vec, const T &val)
Definition: VectorAlgorithm.hpp:44
pFlow::mapperNBS::nullifyNext
void nullifyNext(range nextRng)
Definition: mapperNBS.hpp:116
pFlow::mapperNBS::capacity_
int32 capacity_
Definition: mapperNBS.hpp:87
pFlow::mapperNBS::nextOwner_
bool nextOwner_
Definition: mapperNBS.hpp:93
pFlow::mapperNBS::~mapperNBS
INLINE_FUNCTION_HD ~mapperNBS()=default
contactSearchFunctions.hpp
pFlow::mapperNBS::head
INLINE_FUNCTION_HD auto & head()
Definition: mapperNBS.hpp:231
ViewAlgorithms.hpp
pFlow::reallocNoInit
INLINE_FUNCTION_H void reallocNoInit(ViewType1D< Type, Properties... > &view, int32 len)
Definition: KokkosUtilities.hpp:60
pFlow::mapperNBS::setNext
INLINE_FUNCTION_H void setNext(ViewType1D< int32, memory_space > &next)
Definition: mapperNBS.hpp:261
pFlow::cells< int32 >::nx
INLINE_FUNCTION_HD int32 nx() const
Definition: cells.hpp:127
pFlow::mapperNBS::mapperNBS
INLINE_FUNCTION_HD mapperNBS()
Definition: mapperNBS.hpp:163
pFlow::mapperNBS< DefaultHostExecutionSpace >::NextType
ViewType1D< int32, memory_space > NextType
Definition: mapperNBS.hpp:54
pFlow::cells::pointIndexInDomain
INLINE_FUNCTION_HD bool pointIndexInDomain(const realx3 p, CellType &index) const
Definition: cells.hpp:164
pFlow::mapperNBS::cellIterator::next_
NextType next_
Definition: mapperNBS.hpp:61
pFlow::triple::y
INLINE_FUNCTION_HD T & y()
Definition: triple.hpp:141
pFlow::mapperNBS
Definition: mapperNBS.hpp:34
pFlow
Definition: demComponent.hpp:28
pFlow::cells::CellType
triple< indexType > CellType
Definition: cells.hpp:36
pFlow::mapperNBS::next
const INLINE_FUNCTION_HD auto & next() const
Definition: mapperNBS.hpp:249
pFlow::mapperNBS::TypeInfoNV
TypeInfoNV("mapperNBS")
pFlow::mapperNBS::mapperNBS
mapperNBS(const box &domain, int32 nx, int32 ny, int32 nz, const ViewType1D< realx3, memory_space > &position, bool nextOwner=true)
Definition: mapperNBS.hpp:185
pFlow::mapperNBS::cellIterator::head_
HeadType head_
Definition: mapperNBS.hpp:59
pFlow::mapperNBS::buildCheckInDomain
INLINE_FUNCTION_H void buildCheckInDomain(range activeRange)
Definition: mapperNBS.hpp:329
pFlow::mapperNBS< DefaultHostExecutionSpace >::IdType
int32 IdType
Definition: mapperNBS.hpp:40
n
int32 n
Definition: NBSCrossLoop.hpp:24
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::mapperNBS::particlesCapcityChanged
bool particlesCapcityChanged(int32 newCap)
Definition: mapperNBS.hpp:224
pFlow::mapperNBS::allocateHead
void allocateHead()
Definition: mapperNBS.hpp:151
pFlow::cells< int32 >::domain
const auto & domain() const
Definition: cells.hpp:152
pFlow::mapperNBS::nullify
void nullify()
Definition: mapperNBS.hpp:126
pFlow::mapperNBS< DefaultHostExecutionSpace >::IndexType
int32 IndexType
Definition: mapperNBS.hpp:42
pFlow::mapperNBS::cellIterator::getNext
INLINE_FUNCTION_HD int32 getNext(int32 n) const
Definition: mapperNBS.hpp:80
pFlow::mapperNBS::checkAllocateNext
void checkAllocateNext(int newCap)
Definition: mapperNBS.hpp:141
pFlow::mapperNBS::pointPosition
INLINE_FUNCTION_HD auto & pointPosition()
Definition: mapperNBS.hpp:255
INLINE_FUNCTION_H
#define INLINE_FUNCTION_H
Definition: pFlowMacros.hpp:53
pFlow::mapperNBS::next
INLINE_FUNCTION_HD auto & next()
Definition: mapperNBS.hpp:237
cells.hpp
pFlow::mapperNBS::cellIterator::cellsSize
INLINE_FUNCTION_HD Cells cellsSize() const
Definition: mapperNBS.hpp:72
pFlow::mapperNBS::head
const INLINE_FUNCTION_HD auto & head() const
Definition: mapperNBS.hpp:243
pFlow::mapperNBS::nullify
void nullify(range nextRng)
Definition: mapperNBS.hpp:133
pFlow::triple::z
INLINE_FUNCTION_HD T & z()
Definition: triple.hpp:144
pFlow::mapperNBS< DefaultHostExecutionSpace >::HeadType
ViewType3D< int32, memory_space > HeadType
Definition: mapperNBS.hpp:52
pFlow::mapperNBS< DefaultHostExecutionSpace >::execution_space
DefaultHostExecutionSpace execution_space
Definition: mapperNBS.hpp:48
pFlow::mapperNBS::cellIterator
Definition: mapperNBS.hpp:56
pFlow::box
Definition: box.hpp:32
LAMBDA_HD
#define LAMBDA_HD
Definition: pFlowMacros.hpp:54
pFlow::mapperNBS::Cells
cells< IndexType > Cells
Definition: mapperNBS.hpp:44
pFlow::mapperNBS::operator=
INLINE_FUNCTION_HD mapperNBS & operator=(const mapperNBS &)=default
pFlow::cells< int32 >::ny
INLINE_FUNCTION_HD int32 ny() const
Definition: cells.hpp:133
pFlow::ViewType1D
Kokkos::View< T *, properties... > ViewType1D
Definition: KokkosTypes.hpp:62
pFlow::cells< int32 >::cellSize
INLINE_FUNCTION_HD realx3 cellSize() const
Definition: cells.hpp:115
pFlow::mapperNBS::cellIterator::cellIterator
cellIterator(ViewType3D< int32, memory_space > head, ViewType1D< int32, memory_space > next)
Definition: mapperNBS.hpp:65
pFlow::triple::x
INLINE_FUNCTION_HD T & x()
Definition: triple.hpp:138
pFlow::cells
Definition: cells.hpp:32
pFlow::mapperNBS::nullifyHead
INLINE_FUNCTION_H void nullifyHead()
Definition: mapperNBS.hpp:105
INLINE_FUNCTION_HD
#define INLINE_FUNCTION_HD
Definition: pFlowMacros.hpp:51
pFlow::triple< int32 >
pFlow::cells::pointIndex
INLINE_FUNCTION_HD CellType pointIndex(const realx3 &p) const
Definition: cells.hpp:158
baseAlgorithms.hpp
pFlow::mapperNBS< DefaultHostExecutionSpace >::memory_space
typename execution_space::memory_space memory_space
Definition: mapperNBS.hpp:50
pFlow::mapperNBS::build
INLINE_FUNCTION_H void build(range activeRange)
Definition: mapperNBS.hpp:274
pFlow::mapperNBS::build
INLINE_FUNCTION_H void build(range activeRange, IncludeFunction incld)
Definition: mapperNBS.hpp:300
pFlow::range
kPair< int, int > range
Definition: KokkosTypes.hpp:54
pFlow::mapperNBS::getCellIterator
cellIterator getCellIterator() const
Definition: mapperNBS.hpp:219
pFlow::ViewType3D
Kokkos::View< T ***, properties... > ViewType3D
Definition: KokkosTypes.hpp:68
pFlow::mapperNBS::capacity
INLINE_FUNCTION_HD auto capacity() const
Definition: mapperNBS.hpp:214
pFlow::mapperNBS::buildCheckInDomain
INLINE_FUNCTION_H void buildCheckInDomain(range activeRange, IncludeFunction incld)
Definition: mapperNBS.hpp:359
pFlow::mapperNBS::pointPosition_
ViewType1D< realx3, memory_space > pointPosition_
Definition: mapperNBS.hpp:96