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 
31 {
33  {
35  "number of elements in pointFlag and pointPosition is not equal \n";
36  return false;
37  }
38 
40 
41  int32 minActive, maxActive;
43  0,
44  numPoints_,
45  static_cast<int8>(pointStructure::ACTIVE),
47  minActive,
48  maxActive
49  );
50 
51  activeRange_ = {minActive, maxActive};
52 
53  return true;
54 }
55 
58 {
59  maxPoints_ = pointFlag_.capacity();
60  numPoints_ = pointFlag_.size();
61 }
62 
65 {
66  return pointPosition_;
67 }
68 
71 {
72  return pointFlag_;
73 }
74 
77 {
78 
79 
80  if( capacity() - activeRange_.second >= numNewPoints )
81  {
82  // fill the sequence starting from activeRange_.second-1
83  return makeUnique<int32IndexContainer>(
84  activeRange_.second,
85  activeRange_.second+numNewPoints);
86  }
87 
88  // second, check if there is space at the beginning
89  if( activeRange_.first >= numNewPoints)
90  {
91  return makeUnique<int32IndexContainer>(
92  0,
93  numNewPoints);
94  }
95 
96  // otherwise scan the points from first to the end to find empty spaces
97  int32Vector newPoints(
98  numNewPoints,
99  RESERVE());
100 
101  newPoints.clear();
102  int32 numAdded = 0;
103  ForAll(i, pointFlag_)
104  {
105  if(!isActive(i))
106  {
107  newPoints.push_back(static_cast<int32>(i));
108  numAdded++;
109  }
110 
111  if(numAdded == numNewPoints)
112  {
113  return makeUnique<int32IndexContainer>(
114  newPoints.data(),
115  numNewPoints);
116  }
117  }
118 
119  // check if there is space at the end for the remaining of points
120  if( numAdded <numNewPoints && capacity() - size() >= numNewPoints - numAdded )
121  {
122  int32 ind = size();
123  for(int32 i=numAdded; i<numNewPoints; i++)
124  {
125  newPoints.push_back(ind);
126  ind++;
127  }
128 
129  return makeUnique<int32IndexContainer>(
130  newPoints.data(),
131  numNewPoints);
132  }
133  else
134  {
135  fatalErrorInFunction<<"not enough capacity for inserting particles into the point structure\n";
136  return nullptr;
137  }
138 
139  return nullptr;
140 }
141 
143 :
144  eventSubscriber(),
145  pointFlag_("pointFlag", "pointFlag", maxPoints_, 0 , RESERVE()),
146  pointPosition_("pointPosition", "pointPosition", maxPoints_, 0, RESERVE()),
147  activeRange_(0,0)
148 {}
149 
151 (
152  const int8Vector& flgVec,
153  const realx3Vector& posVec
154 )
155 :
156  eventSubscriber(),
157  maxPoints_(posVec.capacity()),
158  pointFlag_("pointFlag", "pointFlag", maxPoints_, 0 , RESERVE()),
159  pointPosition_("pointPosition", "pointPosition", maxPoints_, 0, RESERVE()),
160  activeRange_(0,0)
161 {
162 
163  pointFlag_.assign(flgVec);
164 
165  pointPosition_.assign(posVec);
166 
167  if( !evaluatePointStructure() )
168  {
169  fatalExit;
170  }
171 }
172 
174 (
175  const realx3Vector& posVec
176 )
177 :
178  eventSubscriber(),
179  maxPoints_(posVec.capacity()),
180  pointFlag_("pointFlag", "pointFlag", maxPoints_, 0 , RESERVE()),
181  pointPosition_("pointPosition", "pointPosition", maxPoints_, 0, RESERVE()),
182  activeRange_(0,0)
183 {
184 
185  pointPosition_.assign(posVec);
186 
187  pointFlag_.resize(pointPosition_.size(), static_cast<int8>(pointStructure::ACTIVE) );
188  //pointFlag_.syncViews();
189 
190  if( !evaluatePointStructure() )
191  {
192  fatalExit;
193  }
194 }
195 
198 {
199  return pointPosition_;
200 }
201 
204 {
205  return pointFlag_;
206 }
207 
208 // - size of data structure
211 {
212  return numPoints_;
213 }
214 
217 {
218  return maxPoints_;
219 }
220 
223 {
224  return numActivePoints_;
225 }
226 
229 {
230  return numActivePoints_ == numPoints_;
231 }
232 
233 
236 {
237  if(numPoints_==0) return 0;
238 
239  int32 minRange, maxRange;
240  int32 numMarked =
242  domain,
243  activeRange_.first,
244  activeRange_.second,
246  pointPosition_.deviceVectorAll(),
247  pointFlag_.deviceVectorAll(),
248  activePointsMaskD(),
249  minRange, maxRange );
250 
251  if(numMarked)
252  {
253  pointFlag_.modifyOnDevice();
254  pointFlag_.syncViews();
255  }
256 
257  if( numMarked<=numActivePoints_)
258  {
259  numActivePoints_ -= numMarked;
260  }
261  else
262  {
264  "number of points marked as delete ("<<numMarked<<
265  ") is greater than the activePoints ("<<numActivePoints_<<
266  ").\n";
267  fatalExit;
268  }
269 
270  range newRange = {minRange, maxRange};
271 
272  if( activeRange_ != newRange )
273  {
274  activeRange_ = newRange;
276 
277  // notify all the registered objects about active range change
278  if( !this->notify(msg) )
279  {
280  fatalExit<<
281  "something went wrong in notifying registered object about range change. \n";
282  fatalExit;
283  }
284  }
285 
286  return numMarked;
287 
288 }
289 
292 {
294 
295  return true;
296 }
297 
300 (
301  const realx3Vector& pos,
302  const setFieldList& setField,
303  repository& owner,
304  const List<eventObserver*>& exclusionList
305 )
306 {
307 
308 
309  auto numNew = pos.size();
310  if( numNew==0)
311  {
312  return makeUnique<int32IndexContainer>();
313  }
314 
315  auto newPointsPtr = getNewPointsIndices( numNew );
316 
317  if(!newPointsPtr)return nullptr;
318 
319  auto oldSize = size();
320  auto oldCapacity = capacity();
321  auto oldRange = activeRange();
322 
323  tobeInsertedIndex_ = newPointsPtr();
324 
325  // set the position of new points
326 
327  if(!pointPosition_.insertSetElement(
328  newPointsPtr(),
329  pos)
330  )return nullptr;
331 
332  if(!pointFlag_.insertSetElement(
333  newPointsPtr(),
334  static_cast<int8>(PointFlag::ACTIVE))
335  )return nullptr;
336 
337 
338 
339  setNumMaxPoints();
340  auto minInd = newPointsPtr().min();
341  auto maxInd = newPointsPtr().max();
342 
343 
344  List<eventObserver*> localExlusion(exclusionList);
345 
346  for(auto sfEntry:setField)
347  {
348  if(void* fieldPtr =
349  sfEntry.setPointFieldSelectedAll(
350  owner,
351  newPointsPtr(),
352  false );
353  fieldPtr)
354 
355  localExlusion.push_back(
356  static_cast<eventObserver*>(fieldPtr)
357  );
358  else
359  return nullptr;
360  }
361 
362  // changes the active rage based on the new inserted points
363  activeRange_ = { min(activeRange_.first, minInd ),
364  max(activeRange_.second, maxInd+1)};
365 
366  numActivePoints_ += numNew;
367 
369 
370  if( oldRange != activeRange_ )
372 
373  if( oldSize != size() )
375 
376  if( oldCapacity != capacity() )
378 
379  // notify all the registered objects except the exclusionList
380  if( !this->notify(msg, localExlusion) ) return nullptr;
381 
382  return newPointsPtr;
383 }
384 
385 
386 
389 (
390  iIstream& is
391 )
392 {
393  auto psCapacity = is.lookupDataOrSet("pointStructureCapacity", maxSizeDefault_);
394  pointPosition_.reallocate(psCapacity);
395  pointFlag_.reallocate(psCapacity);
396 
397  if( !pointPosition_.read(is))
398  {
399  ioErrorInFile(is.name(), is.lineNumber())<<
400  "Error in reading pointPosition in pointStructure \n";
401  return false;
402  }
403 
404  if(! pointFlag_.read(is))
405  {
406  ioErrorInFile(is.name(), is.lineNumber())<<
407  "Error in reading pointFlag in pointStructure \n";
408  return false;
409  }
410 
411 
412  return evaluatePointStructure();
413 }
414 
417 (
418  iOstream& os
419 )const
420 {
421  os.writeWordEntry("pointStructureCapacity", maxPoints_);
422 
423  if(!pointPosition_.write(os))
424  {
425  ioErrorInFile(os.name(), os.lineNumber())<<
426  "error in writing pointPosition to file \n";
427  return false;
428  }
429 
430  if(!pointFlag_.write(os))
431  {
432  ioErrorInFile(os.name(), os.lineNumber())<<
433  "error in writing pointFlag to file \n";
434  return false;
435  }
436  return true;
437 }
438 
439 
440 /*pFlow::uniquePtr<pFlow::int32Vector>
441 pFlow::pointStructure::newPointsIndices(
442  int32 numNewPoints
443 )const
444 {
445 
446  auto newPointsPtr = makeUnique<int32Vector>(
447  "pointStructure::newPointsPtr",
448  numNewPoints);
449 
450  auto& newPoints = newPointsPtr();
451 
452 
453  // first, check if there is space at the end
454  if( capacity() - activeRange_.second >= numNewPoints )
455  {
456  // fill the sequence starting from activeRange_.second-1
457  fillSequence(newPoints, activeRange_.second-1);
458  return newPointsPtr;
459  }
460 
461  // second, check if there is space at the beggining
462  if( activeRange_.first >= numNewPoints)
463  {
464  // fill the sequence starting from 0
465  fillSequence(newPoints, 0);
466  return newPointsPtr;
467  }
468 
469  // otherwise scan the points from first to the end to find empty spaces
470  newPoints.clear();
471  int32 numAdded = 0;
472  ForAll(i, pointFlag_)
473  {
474  if(!isActive(i))
475  {
476  newPoints.push_back(static_cast<int32>(i));
477  numAdded++;
478  }
479 
480  if(numAdded == numNewPoints)
481  {
482 
483  return newPointsPtr;
484  }
485  }
486 
487  // check if there is space at the end for the remaining of points
488  if( capacity() - size() >= numNewPoints - numAdded )
489  {
490  int32 ind = size();
491  for(int32 i=numAdded; i<numNewPoints; i++)
492  {
493  newPoints.push_back(ind);
494  ind++;
495  }
496 
497  return newPointsPtr;
498  }
499  else
500  {
501  fatalErrorInFunction<<"not enough capacity for inserting particles into the point structure\n";
502  return nullptr;
503  }
504 
505  return nullptr;
506 }*/
pFlow::pointStructure::evaluatePointStructure
FUNCTION_H bool evaluatePointStructure()
Definition: pointStructure.cpp:30
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::pointStructure::DELETED
@ DELETED
Definition: pointStructure.hpp:52
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::eventMessage
Definition: eventMessage.hpp:29
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:235
pFlow::pointStructure::pointFlag
FUNCTION_H int8Field_HD & pointFlag()
Definition: pointStructure.cpp:70
pFlow::pointStructure::readPointStructure
FUNCTION_H bool readPointStructure(iIstream &is)
Definition: pointStructure.cpp:389
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:417
pFlow::Vector::size
auto size() const
Definition: Vector.hpp:299
pFlow::eventObserver
Definition: eventObserver.hpp:33
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:291
pFlow::pointStructure::pointPosition
FUNCTION_H realx3Field_D & pointPosition()
Definition: pointStructure.cpp:64
pFlow::pointStructure::numActive
FUNCTION_H label numActive() const
Definition: pointStructure.cpp:222
FUNCTION_H
#define FUNCTION_H
Definition: pFlowMacros.hpp:58
pFlow::pointStructure::allActive
FUNCTION_H bool allActive() const
Definition: pointStructure.cpp:228
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::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:304
pFlow::setFieldList
Definition: setFieldList.hpp:32
Time.hpp
pFlow::pointStructure::getNewPointsIndices
FUNCTION_H uniquePtr< int32IndexContainer > getNewPointsIndices(int32 numNewPoints) const
Definition: pointStructure.cpp:76
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
Definition: IOstream.cpp:31
pFlow::pointStructure::capacity
FUNCTION_H label capacity() const
Definition: pointStructure.cpp:216
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:300
pFlow::pointStructure::pointStructure
pointStructure()
Definition: pointStructure.cpp:142
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:49
pFlow::repository
Definition: repository.hpp:34
pFlow::IOstream::lineNumber
int32 lineNumber() const
Definition: IOstream.hpp:187
pFlow::pointStructure::size
FUNCTION_H label size() const
Definition: pointStructure.cpp:210
iOstream.hpp
pFlow::VectorSingle::size
INLINE_FUNCTION_H size_t size() const
Definition: VectorSingle.hpp:360
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::range
kPair< int, int > range
Definition: KokkosTypes.hpp:54
pFlow::iOstream::writeWordEntry
iOstream & writeWordEntry(const word &key, const T &value)
Definition: iOstream.hpp:217
pFlow::min
T min(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:138
pFlow::pointStructure::setNumMaxPoints
FUNCTION_H void setNumMaxPoints()
Definition: pointStructure.cpp:57
pFlow::pointStructure::pointFlag_
int8Field_HD pointFlag_
Definition: pointStructure.hpp:155
error.hpp