www.cemf.ir
pointStructure.cpp
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 #include "pointStructure.hpp"
24 #include "setFieldList.hpp"
25 #include "error.hpp"
26 #include "iOstream.hpp"
27 //#include "Time.hpp"
28 #include "mortonIndexing.hpp"
29 
32 {
34  {
36  "number of elements in pointFlag and pointPosition is not equal \n";
37  return false;
38  }
39 
41 
42  int32 minActive, maxActive;
44  0,
45  numPoints_,
46  static_cast<int8>(pointStructure::ACTIVE),
48  minActive,
49  maxActive
50  );
51 
52  activeRange_ = {minActive, maxActive};
53 
54  return true;
55 }
56 
59 {
60  maxPoints_ = pointFlag_.capacity();
61  numPoints_ = pointFlag_.size();
62 }
63 
66 {
67  return pointPosition_;
68 }
69 
72 {
73  return pointFlag_;
74 }
75 
78 {
79 
80 
81  if( capacity() - activeRange_.second >= numNewPoints )
82  {
83  // fill the sequence starting from activeRange_.second-1
84  return makeUnique<int32IndexContainer>(
85  activeRange_.second,
86  activeRange_.second+numNewPoints);
87 
88  }
89 
90  // second, check if there is space at the beginning
91  if( activeRange_.first >= numNewPoints)
92  {
93  return makeUnique<int32IndexContainer>(
94  0,
95  numNewPoints);
96  }
97 
98  // otherwise scan the points from first to the end to find empty spaces
99  int32Vector newPoints(
100  numNewPoints,
101  RESERVE());
102 
103  newPoints.clear();
104  int32 numAdded = 0;
105  ForAll(i, pointFlag_)
106  {
107  if(!isActive(i))
108  {
109  newPoints.push_back(static_cast<int32>(i));
110  numAdded++;
111  }
112 
113  if(numAdded == numNewPoints)
114  {
115  return makeUnique<int32IndexContainer>(
116  newPoints.data(),
117  numNewPoints);
118  }
119  }
120 
121  // check if there is space at the end for the remaining of points
122  if( numAdded <numNewPoints && capacity() - size() >= numNewPoints - numAdded )
123  {
124  int32 ind = size();
125  for(int32 i=numAdded; i<numNewPoints; i++)
126  {
127  newPoints.push_back(ind);
128  ind++;
129  }
130 
131  return makeUnique<int32IndexContainer>(
132  newPoints.data(),
133  numNewPoints);
134  }
135  else
136  {
137  fatalErrorInFunction<<"not enough capacity for inserting particles into the point structure\n";
138  return nullptr;
139  }
140 
141  return nullptr;
142 }
143 
145 :
146  eventSubscriber(),
147  pointFlag_("pointFlag", "pointFlag", maxPoints_, 0 , RESERVE()),
148  pointPosition_("pointPosition", "pointPosition", maxPoints_, 0, RESERVE()),
149  activeRange_(0,0)
150 {}
151 
153 (
154  const int8Vector& flgVec,
155  const realx3Vector& posVec
156 )
157 :
158  eventSubscriber(),
159  maxPoints_(posVec.capacity()),
160  pointFlag_("pointFlag", "pointFlag", maxPoints_, 0 , RESERVE()),
161  pointPosition_("pointPosition", "pointPosition", maxPoints_, 0, RESERVE()),
162  activeRange_(0,0)
163 {
164 
165  pointFlag_.assign(flgVec);
166 
167  pointPosition_.assign(posVec);
168 
169  if( !evaluatePointStructure() )
170  {
171  fatalExit;
172  }
173 }
174 
176 (
177  const realx3Vector& posVec
178 )
179 :
180  eventSubscriber(),
181  maxPoints_(posVec.capacity()),
182  pointFlag_("pointFlag", "pointFlag", maxPoints_, 0 , RESERVE()),
183  pointPosition_("pointPosition", "pointPosition", maxPoints_, 0, RESERVE()),
184  activeRange_(0,0)
185 {
186 
187  pointPosition_.assign(posVec);
188 
189  pointFlag_.resize(pointPosition_.size(), static_cast<int8>(pointStructure::ACTIVE) );
190  //pointFlag_.syncViews();
191 
192  if( !evaluatePointStructure() )
193  {
194  fatalExit;
195  }
196 }
197 
200 {
201  return pointPosition_;
202 }
203 
206 {
207  return pointFlag_;
208 }
209 
210 // - size of data structure
213 {
214  return numPoints_;
215 }
216 
219 {
220  return maxPoints_;
221 }
222 
225 {
226  return numActivePoints_;
227 }
228 
231 {
232  return numActivePoints_ == numPoints_;
233 }
234 
237 {
238  if( !getSortedIndex(
239  domain,
240  dx,
241  activeRange_,
242  pointPosition_.deviceVectorAll(),
243  pointFlag_.deviceVectorAll(),
244  mortonSortedIndex_) )
245  {
246  fatalErrorInFunction<<"failed to perform morton sorting!"<<endl;
247  return false;
248  }
249 
250  pointPosition_.sortItems(mortonSortedIndex_);
251  pointFlag_.sortItems(mortonSortedIndex_);
252 
253  auto oldSize = size();
254  auto oldCapacity = capacity();
255  auto oldRange = activeRange();
256 
257  // update size, range, capacity
258  setNumMaxPoints();
259  activeRange_ = {0, static_cast<int>(mortonSortedIndex_.size())};
260  numActivePoints_ = mortonSortedIndex_.size();
261 
263 
264  if(oldSize != size() )
265  {
267  }
268 
269  if(oldCapacity != capacity())
270  {
272  }
273 
274  if( oldRange != activeRange_)
275  {
277  }
278 
279  // notify all the registered objects except the exclusionList
280  if( !this->notify(msg) ) return false;
281 
282  return true;
283 }
284 
287 {
288  if(numPoints_==0) return 0;
289 
290  int32 minRange, maxRange;
291  int32 numMarked =
293  domain,
294  activeRange_.first,
295  activeRange_.second,
297  pointPosition_.deviceVectorAll(),
298  pointFlag_.deviceVectorAll(),
299  activePointsMaskD(),
300  minRange, maxRange );
301 
302  if(numMarked)
303  {
304  pointFlag_.modifyOnDevice();
305  pointFlag_.syncViews();
306  }
307 
308  if( numMarked<=numActivePoints_)
309  {
310  numActivePoints_ -= numMarked;
311  }
312  else
313  {
315  "number of points marked as delete ("<<numMarked<<
316  ") is greater than the activePoints ("<<numActivePoints_<<
317  ").\n";
318  fatalExit;
319  }
320 
321  range newRange = {minRange, maxRange};
322 
323  if( activeRange_ != newRange )
324  {
325  activeRange_ = newRange;
327 
328  // notify all the registered objects about active range change
329  if( !this->notify(msg) )
330  {
331  fatalExit<<
332  "something went wrong in notifying registered object about range change. \n";
333  fatalExit;
334  }
335  }
336 
337  return numMarked;
338 
339 }
340 
343 {
345 
346  return true;
347 }
348 
351 (
352  const realx3Vector& pos,
353  const setFieldList& setField,
354  repository& owner,
355  const List<eventObserver*>& exclusionList
356 )
357 {
358 
359  auto numNew = pos.size();
360  if( numNew==0)
361  {
362  return makeUnique<int32IndexContainer>();
363  }
364 
365  auto newPointsPtr = getNewPointsIndices( numNew );
366 
367  if(!newPointsPtr)return nullptr;
368 
369  auto oldSize = size();
370  auto oldCapacity = capacity();
371  auto oldRange = activeRange();
372 
373  tobeInsertedIndex_ = newPointsPtr();
374 
375  // set the position of new points
376 
377  if(!pointPosition_.insertSetElement(
378  newPointsPtr(),
379  pos)
380  )return nullptr;
381 
382  if(!pointFlag_.insertSetElement(
383  newPointsPtr(),
384  static_cast<int8>(PointFlag::ACTIVE))
385  )return nullptr;
386 
387  setNumMaxPoints();
388  auto minInd = newPointsPtr().min();
389  auto maxInd = newPointsPtr().max();
390 
391 
392  List<eventObserver*> localExlusion(exclusionList);
393 
394  for(auto sfEntry:setField)
395  {
396  if(void* fieldPtr =
397  sfEntry.setPointFieldSelectedAll(
398  owner,
399  newPointsPtr(),
400  false );
401  fieldPtr)
402 
403  localExlusion.push_back(
404  static_cast<eventObserver*>(fieldPtr)
405  );
406  else
407  return nullptr;
408  }
409 
410  // changes the active rage based on the new inserted points
411  activeRange_ = { static_cast<int>(min(activeRange_.first, minInd )),
412  static_cast<int>(max(activeRange_.second, maxInd+1))};
413 
414  numActivePoints_ += numNew;
415 
417 
418  if( oldRange != activeRange_ )
420 
421  if( oldSize != size() )
423 
424  if( oldCapacity != capacity() )
426 
427  // notify all the registered objects except the exclusionList
428  if( !this->notify(msg, localExlusion) ) return nullptr;
429 
430  return newPointsPtr;
431 }
432 
433 
434 
437 (
438  iIstream& is
439 )
440 {
441  auto psCapacity = is.lookupDataOrSet("pointStructureCapacity", maxSizeDefault_);
442  pointPosition_.reallocate(psCapacity);
443  pointFlag_.reallocate(psCapacity);
444 
445  if( !pointPosition_.read(is))
446  {
447  ioErrorInFile(is.name(), is.lineNumber())<<
448  "Error in reading pointPosition in pointStructure \n";
449  return false;
450  }
451 
452  if(! pointFlag_.read(is, true))
453  {
454  ioErrorInFile(is.name(), is.lineNumber())<<
455  "Error in reading pointFlag in pointStructure \n";
456  return false;
457  }
458 
459 
460  return evaluatePointStructure();
461 }
462 
465 (
466  iOstream& os
467 )const
468 {
469  os.writeWordEntry("pointStructureCapacity", maxPoints_);
470 
471  if(!pointPosition_.write(os))
472  {
473  ioErrorInFile(os.name(), os.lineNumber())<<
474  "error in writing pointPosition to file \n";
475  return false;
476  }
477 
478  if(!pointFlag_.write(os))
479  {
480  ioErrorInFile(os.name(), os.lineNumber())<<
481  "error in writing pointFlag to file \n";
482  return false;
483  }
484  return true;
485 }
486 
487 
488 /*pFlow::uniquePtr<pFlow::int32Vector>
489 pFlow::pointStructure::newPointsIndices(
490  int32 numNewPoints
491 )const
492 {
493 
494  auto newPointsPtr = makeUnique<int32Vector>(
495  "pointStructure::newPointsPtr",
496  numNewPoints);
497 
498  auto& newPoints = newPointsPtr();
499 
500 
501  // first, check if there is space at the end
502  if( capacity() - activeRange_.second >= numNewPoints )
503  {
504  // fill the sequence starting from activeRange_.second-1
505  fillSequence(newPoints, activeRange_.second-1);
506  return newPointsPtr;
507  }
508 
509  // second, check if there is space at the beggining
510  if( activeRange_.first >= numNewPoints)
511  {
512  // fill the sequence starting from 0
513  fillSequence(newPoints, 0);
514  return newPointsPtr;
515  }
516 
517  // otherwise scan the points from first to the end to find empty spaces
518  newPoints.clear();
519  int32 numAdded = 0;
520  ForAll(i, pointFlag_)
521  {
522  if(!isActive(i))
523  {
524  newPoints.push_back(static_cast<int32>(i));
525  numAdded++;
526  }
527 
528  if(numAdded == numNewPoints)
529  {
530 
531  return newPointsPtr;
532  }
533  }
534 
535  // check if there is space at the end for the remaining of points
536  if( capacity() - size() >= numNewPoints - numAdded )
537  {
538  int32 ind = size();
539  for(int32 i=numAdded; i<numNewPoints; i++)
540  {
541  newPoints.push_back(ind);
542  ind++;
543  }
544 
545  return newPointsPtr;
546  }
547  else
548  {
549  fatalErrorInFunction<<"not enough capacity for inserting particles into the point structure\n";
550  return nullptr;
551  }
552 
553  return nullptr;
554 }*/
pFlow::pointStructure::evaluatePointStructure
FUNCTION_H bool evaluatePointStructure()
Definition: pointStructure.cpp:31
notImplementedFunction
#define notImplementedFunction
Definition: error.hpp:47
pFlow::List
Definition: List.hpp:39
pFlow::pointStructure::numActivePoints_
size_t numActivePoints_
Definition: pointStructure.hpp:161
setFieldList.hpp
pFlow::eventSubscriber
Definition: eventSubscriber.hpp:34
pFlow::iIstream::lookupDataOrSet
T lookupDataOrSet(const word &keyword, const T &setVal)
Definition: iIstreamI.hpp:68
pFlow::eventMessage::INSERT
@ INSERT
Definition: eventMessage.hpp:35
pFlow::pointStructure::pointPosition_
realx3Field_D pointPosition_
Definition: pointStructure.hpp:158
pFlow::real
float real
Definition: builtinTypes.hpp:46
pFlow::pointStructure::DELETED
@ DELETED
Definition: pointStructure.hpp:52
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::range
kRange< int > range
Definition: KokkosTypes.hpp:59
pFlow::eventMessage
Definition: eventMessage.hpp:29
pFlow::eventMessage::REARRANGE
@ REARRANGE
Definition: eventMessage.hpp:36
pFlow::eventMessage::add
void add(unsigned int msg)
Definition: eventMessage.hpp:68
pFlow::pointStructure::markDeleteOutOfBox
FUNCTION_H size_t markDeleteOutOfBox(const box &domain)
Definition: pointStructure.cpp:286
pFlow::pointStructure::pointFlag
FUNCTION_H int8Field_HD & pointFlag()
Definition: pointStructure.cpp:71
pFlow::pointStructure::readPointStructure
FUNCTION_H bool readPointStructure(iIstream &is)
Definition: pointStructure.cpp:437
pFlow::eventMessage::SIZE_CHANGED
@ SIZE_CHANGED
Definition: eventMessage.hpp:37
pFlow::pointStructure::ACTIVE
@ ACTIVE
Definition: pointStructure.hpp:53
pFlow::pointStructure::writePointStructure
FUNCTION_H bool writePointStructure(iOstream &os) const
Definition: pointStructure.cpp:465
pFlow::Vector::size
auto size() const
Definition: Vector.hpp:301
pFlow::getSortedIndex
bool getSortedIndex(box boundingBox, real dx, range activeRange, ViewType1D< realx3 > pos, ViewType1D< int8 > flag, int32IndexContainer &sortedIndex)
Definition: mortonIndexing.cpp:26
pFlow::eventObserver
Definition: eventObserver.hpp:33
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:320
pFlow::Field< VectorSingle, realx3 >
pFlow::pointStructureKernels::scanPointFlag
int32 scanPointFlag(int32 start, int32 end, int8 activeFlag, deviceViewType1D< int8 > flags, int32 &minRange, int32 &maxRange)
Definition: pointStructureKernels.hpp:92
pFlow::pointStructure::updateForDelete
virtual FUNCTION_H bool updateForDelete()
Definition: pointStructure.cpp:342
pFlow::pointStructure::pointPosition
FUNCTION_H realx3Field_D & pointPosition()
Definition: pointStructure.cpp:65
pFlow::pointStructure::numActive
FUNCTION_H label numActive() const
Definition: pointStructure.cpp:224
FUNCTION_H
#define FUNCTION_H
Definition: pFlowMacros.hpp:58
pFlow::pointStructure::allActive
FUNCTION_H bool allActive() const
Definition: pointStructure.cpp:230
RESERVE
Definition: Vector.hpp:38
pFlow::VectorDual::deviceVectorAll
INLINE_FUNCTION_H deviceViewType & deviceVectorAll()
Definition: VectorDual.hpp:335
pFlow::iIstream
Definition: iIstream.hpp:33
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::pointStructure::mortonSortPoints
virtual FUNCTION_H bool mortonSortPoints(const box &domain, real dx)
Definition: pointStructure.cpp:236
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::pointStructureKernels::markDeleteOutOfBox
int32 markDeleteOutOfBox(box domain, int32 start, int32 end, int8 deleteFlag, deviceViewType1D< realx3 > points, deviceViewType1D< int8 > flags, pointStructure::activePointsDevice activePoint, int32 &minRange, int32 &maxRange)
Definition: pointStructureKernels.hpp:32
pFlow::Vector::capacity
auto capacity() const
Definition: Vector.hpp:306
pFlow::setFieldList
Definition: setFieldList.hpp:32
pFlow::pointStructure::getNewPointsIndices
FUNCTION_H uniquePtr< int32IndexContainer > getNewPointsIndices(int32 numNewPoints) const
Definition: pointStructure.cpp:77
ForAll
#define ForAll(i, container)
Definition: pFlowMacros.hpp:71
pFlow::pointStructure::activeRange_
range activeRange_
Definition: pointStructure.hpp:164
pFlow::eventMessage::RANGE_CHANGED
@ RANGE_CHANGED
Definition: eventMessage.hpp:39
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::pointStructure::capacity
FUNCTION_H label capacity() const
Definition: pointStructure.cpp:218
pFlow::max
T max(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:164
pointStructureKernels.hpp
pFlow::Vector::clear
auto clear()
Definition: Vector.hpp:248
pFlow::box
Definition: box.hpp:32
pFlow::uniquePtr
Definition: uniquePtr.hpp:44
ioErrorInFile
#define ioErrorInFile(fileName, lineNumber)
Definition: error.hpp:49
pFlow::eventMessage::CAP_CHANGED
@ CAP_CHANGED
Definition: eventMessage.hpp:38
pFlow::label
std::size_t label
Definition: builtinTypes.hpp:61
pFlow::pointStructure::insertPoints
virtual FUNCTION_H uniquePtr< int32IndexContainer > insertPoints(const realx3Vector &pos, const setFieldList &setField, repository &owner, const List< eventObserver * > &exclusionList={nullptr})
Definition: pointStructure.cpp:351
pFlow::pointStructure::pointStructure
pointStructure()
Definition: pointStructure.cpp:144
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:49
pFlow::repository
Definition: repository.hpp:34
pFlow::IOstream::lineNumber
int32 lineNumber() const
Const access to the current stream line number.
Definition: IOstream.hpp:221
pFlow::pointStructure::size
FUNCTION_H label size() const
Definition: pointStructure.cpp:212
iOstream.hpp
mortonIndexing.hpp
pFlow::VectorSingle::size
INLINE_FUNCTION_H size_t size() const
Definition: VectorSingle.hpp:362
pFlow::pointStructure::numPoints_
size_t numPoints_
Definition: pointStructure.hpp:149
pFlow::maxActive
T maxActive(const pointField< VectorSingle, T, MemorySpace > &pField)
Definition: pointFieldAlgorithms.hpp:100
pFlow::Vector< int32 >
pointStructure.hpp
pFlow::iOstream
Definition: iOstream.hpp:53
pFlow::VectorDual::size
INLINE_FUNCTION_H size_t size() const
Definition: VectorDual.hpp:391
pFlow::iOstream::writeWordEntry
iOstream & writeWordEntry(const word &key, const T &value)
Write a keyword/value entry.
Definition: iOstream.hpp:224
pFlow::min
T min(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:138
pFlow::pointStructure::setNumMaxPoints
FUNCTION_H void setNumMaxPoints()
Definition: pointStructure.cpp:58
pFlow::pointStructure::pointFlag_
int8Field_HD pointFlag_
Definition: pointStructure.hpp:155
error.hpp