www.cemf.ir
internalPoints.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 #include "Time.hpp"
22 #include "internalPoints.hpp"
23 #include "domain.hpp"
24 #include "Vectors.hpp"
25 #include "anyList.hpp"
27 
29 {
30  if(!pFlagSync_)
31  {
33  pFlagSync_ = true;
34  }
35 }
36 
38 {
39 
40  if(delPoints.empty())return true;
41 
42  auto oldRange = pFlagsD_.activeRange();
43  auto oldSize = size();
44 
45  if(!pFlagsD_.deletePoints(delPoints.deviceView()))
46  {
48  "Error in deleting points from internal points"<<endl;
49  return false;
50  }
51  unSyncFlag();
52 
53  auto newRange = pFlagsD_.activeRange();
54  auto newSize = size();
55 
56  anyList varList;
57 
58  varList.emplaceBack(
60  delPoints);
62 
63  if(oldSize!= newSize)
64  {
66  varList.emplaceBack(
68  newSize);
69  }
70 
71  if(oldRange!=newRange)
72  {
74  varList.emplaceBack(
76  newRange);
77  }
78  auto iter = time().currentIter();
79  auto t = time().currentTime();
80  auto dt = time().dt();
81 
82  if( !notify(iter, t, dt, msg, varList) )
83  {
85  return false;
86  }
87 
88  return true;
89 }
90 
92 (
93  const uint32Vector_D& changePoints,
94  realx3 transferVector,
95  uint32 fromBoundaryIndex,
96  uint32 toBoundaryIndex
97 )
98 {
99  if(toBoundaryIndex>5)
100  {
102  "Invalid boundary index "<< toBoundaryIndex<<endl;
103  return false;
104  }
105 
106  // change the flag
107  pFlagsD_.changeFlags(changePoints.deviceView(), toBoundaryIndex);
108  unSyncFlag();
109 
110  // change the position
112  (
113  pointPosition_.deviceViewAll(),
114  changePoints.deviceView(),
115  transferVector
116  );
117 
118  anyList varList;
119  message msg;
120 
121  varList.emplaceBack(
123  changePoints);
124  varList.emplaceBack("fromBoundaryIndex", fromBoundaryIndex);
125  varList.emplaceBack("toBoundaryIndex", toBoundaryIndex);
126 
128 
129  if( !notify(
130  time().currentIter(),
131  time().currentTime(),
132  time().dt(),
133  msg,
134  varList))
135  {
137  "Error in notify for item transfer from "<< fromBoundaryIndex<<
138  " to "<<toBoundaryIndex<< " boundary"<<endl;
139  return false;
140  }
141 
142  return true;
143 }
144 
146 {
147  if(!pointPosition_.reorderItems(sortedIndices))
148  {
150  return false;
151  }
152 
153  pFlagsD_.resetFlags(pFlagsD_.capacity(), 0u, sortedIndices.size());
154  unSyncFlag();
155 
156  return true;
157 }
158 
160  : subscriber("internalPoints"),
161  pointPosition_("internalPoints", "internalPoints", initialCapacity_, 0, RESERVE()),
162  pFlagsD_(initialCapacity_, 0, 0)
163 {}
164 
165 
167 (
168  const realx3Vector& posVec
169 )
170 :
171  subscriber("internalPoints"),
172  pointPosition_
173  (
174  "internalPoints",
175  "internalPoints",
176  posVec.capacity(),
177  0,
178  RESERVE()
179  ),
180  pFlagsD_
181  (
182  static_cast<uint32>(posVec.capacity()),
183  static_cast<uint32>(0),
184  static_cast<uint32>(posVec.size())
185  )
186 {
187 
188  pointPosition_.assign(posVec);
189 
190 }
191 
192 
195 {
196  return pFlagsD_;
197 }
198 
199 
202 {
203  syncPFlag();
204  return pFlagsH_;
205 }
206 
207 
208 const typename pFlow::internalPoints::PointsType&
210 {
211  return pointPosition_;
212 }
213 
214 
217 {
218  return pointPosition_;
219 }
220 
223 {
224  auto maskH = activePointsMaskHost();
225  auto pointsH = pointPositionHost();
226 
227  PointsTypeHost aPoints
228  (
229  pointPosition_.name(),
230  pointPosition_.fieldKey(),
231  maskH.numActive(),
232  maskH.numActive(),
233  RESERVE()
234  );
235 
236  auto aRange = maskH.activeRange();
237  uint32 n = 0;
238 
239  for(auto i=aRange.start(); i<aRange.end(); i++)
240  {
241 
242  if( maskH.isActive(i) )
243  {
244  aPoints[n] = pointsH[i];
245  n++;
246  }
247  }
248 
249  return aPoints;
250 }
251 
253 (
255 )
256 {
257  if(!pFlagsD_.deletePoints(delPoints))
258  {
260  "Error in deleting points from internal points"<<endl;
261  return false;
262  }
263  unSyncFlag();
264  WARNING<<"Notify the observersin in internalPoints"<<END_WARNING;
265 
266  return true;
267 }
268 
269 
271 (
272  const domain& dm,
273  const std::array<real,6>& dist
274 )
275 {
276  unSyncFlag();
277  return pFlagsD_.markPointRegions
278  (
279  dm,
280  pointPosition_.deviceViewAll(),
281  dist[0],
282  dist[1],
283  dist[2],
284  dist[3],
285  dist[4],
286  dist[5]
287  );
288 }
289 
291 (
298 )
299 {
300  pFlagsD_.fillNeighborsLists(
301  leftList,
302  rightList,
303  bottomList,
304  topList,
305  rearList,
306  frontList);
307 
308 }
309 
310 bool
312  const realx3Vector& points,
313  anyList& varList
314 )
315 {
316  uint32 numNew = static_cast<uint32>(points.size());
317 
318  auto aRange = pFlagsD_.activeRange();
319  uint32 emptySpots = pFlagsD_.capacity() - pFlagsD_.numActive();
320 
321  if(emptySpots!= 0) emptySpots--;
322 
323  message msg;
324 
325  if( numNew > emptySpots )
326  {
327  // increase the capacity to hold new points
328  aRange = pFlagsD_.activeRange();
329  uint32 newCap = pFlagsD_.changeCapacity(numNew);
330  unSyncFlag();
331  varList.emplaceBack(
333  newCap);
334  }
335 
336 
337  // first check if it is possible to add to the beggining of list
338  if(numNew <= aRange.start())
339  {
342  0u, numNew);
343  }
344  // check if it is possible to add to the end of the list
345  else if( numNew <= pFlagsD_.capacity() - aRange.end() )
346  {
349  aRange.end(), aRange.end()+numNew);
350  }
351  // we should fill the scattered empty spots
352  else
353  {
354  auto newIndices = pFlagsD_.getEmptyPoints(numNew);
355  if(numNew != newIndices.size())
356  {
357  fatalErrorInFunction<<"not enough empty points in pointFlag"<<
358  numNew<< " "<<newIndices.size() <<endl;
359  pOutput<< pFlagsD_.capacity()<<endl;
360  pOutput<< pFlagsD_.numActive()<<endl;
361  return false;
362  }
363 
366  newIndices
367  );
368  }
369 
370  const auto& indices = varList.getObject<uint32IndexContainer>(
372  );
373 
374  auto nAdded = pFlagsD_.addInternalPoints(indices.deviceView());
375  unSyncFlag();
376 
377  if(nAdded != numNew )
378  {
380  return false;
381  }
382 
383  pointPosition_.reserve( pFlagsD_.capacity() );
384  if(!pointPosition_.insertSetElement(indices, points))
385  {
387  "Error in inserting new positions into pointPosition"<<
388  " internalPoints field"<<endl;
389  return false;
390  }
391 
392  auto newARange = pFlagsD_.activeRange();
393 
394  if( aRange.end() != newARange.end() )
395  {
396  varList.emplaceBack(
398  newARange);
399  varList.emplaceBack(
401  newARange.end());
402  }
403  else if(aRange.start() != newARange.start())
404  {
405  varList.emplaceBack(
407  newARange);
408  }
409 
410  auto tInfo = time().TimeInfo();
411 
412  if(!notify(
413  tInfo.iter(),
414  tInfo.t(),
415  tInfo.dt(),
416  msg,
417  varList))
418  {
420  "Error in notifying observers of "<< subscriberName()<<endl;
421  return false;
422  }
423 
424  return true;
425 }
426 
428  const realx3Vector_D &points,
429  message& msg,
430  anyList &varList
431 )
432 {
433 
434  uint32 numNew = static_cast<uint32>(points.size());
435 
436  auto aRange = pFlagsD_.activeRange();
437  uint32 emptySpots = pFlagsD_.capacity() - pFlagsD_.numActive();
438 
439  if(emptySpots!= 0) emptySpots--;
440 
441  if( numNew > emptySpots )
442  {
443  // increase the capacity to hold new points
444  aRange = pFlagsD_.activeRange();
445  uint32 newCap = pFlagsD_.changeCapacity(numNew);
446  unSyncFlag();
447  varList.emplaceBack(
449  newCap);
450  }
451 
452 
453  // first check if it is possible to add to the beggining of list
454  if(numNew <= aRange.start())
455  {
458  0u, numNew);
459  }// check if it is possible to add to the end of the list
460  else if( numNew <= pFlagsD_.capacity() - aRange.end() )
461  {
464  aRange.end(), aRange.end()+numNew);
465  }// we should fill the scattered empty spots
466  else
467  {
468 
469  auto newIndices = pFlagsD_.getEmptyPoints(numNew);
470  if(numNew != newIndices.size())
471  {
472  fatalErrorInFunction<<"not enough empty points in pointFlag"<<
473  numNew<< " "<<newIndices.size() <<endl;
474  pOutput<< pFlagsD_.capacity()<<endl;
475  pOutput<< pFlagsD_.numActive()<<endl;
476  return false;
477  }
478 
481  newIndices
482  );
483  }
484 
485  const auto& indices = varList.getObject<uint32IndexContainer>(
487  );
488 
489  auto nAdded = pFlagsD_.addInternalPoints(indices.deviceView());
490  unSyncFlag();
491 
492  if(nAdded != numNew )
493  {
495  return false;
496  }
497 
498  pointPosition_.reserve( pFlagsD_.capacity() );
499 
500  if(!pointPosition_.insertSetElement(indices, points.deviceView()))
501  {
503  "Error in inserting new positions into pointPosition"<<
504  " internalPoints field"<<endl;
505  return false;
506  }
507 
508  auto newARange = pFlagsD_.activeRange();
509 
510  if( aRange.end() != newARange.end() )
511  {
512  varList.emplaceBack(
514  newARange);
515  varList.emplaceBack(
517  newARange.end());
518  }
519  else if(aRange.start() != newARange.start())
520  {
521  varList.emplaceBack(
523  newARange);
524  }
525 
526  return true;
527 }
528 
530 (
531  iIstream& is
532 )
533 {
534 
535  PointsTypeHost fRead(pointPosition_.name(), pointPosition_.fieldKey());
536 
537  if( !fRead.read(is))
538  {
540  "Error in reading pointPosition from stream "<< is.name()<<endl;
541  return false;
542  }
543 
544  pointPosition_.assignFromHost(fRead);
545 
546  pFlagsD_ = pFlagTypeDevice(pointPosition_.capacity(), 0, pointPosition_.size());
547  unSyncFlag();
548  syncPFlag();
549 
550  return true;
551 }
552 
553 
554 
556 (
557  iOstream& os
558 )const
559 {
560  auto aPoints = this->activePointsHost();
561  return aPoints.write(os);
562 }
563 
564 
566 (
567  iIstream& is,
568  const IOPattern& iop
569 )
570 {
571 
572  PointsTypeHost fRead(pointPosition_.name(), pointPosition_.fieldKey());
573 
574  if( !fRead.read(is, iop))
575  {
577  "Error in reading pointPosition from stream "<< is.name()<<endl;
578  return false;
579  }
580 
581  pointPosition_.assignFromHost(fRead);
582 
583  createDeviceFlag(pointPosition_.capacity(), 0, pointPosition_.size());
584  syncPFlag();
585 
586  return true;
587 }
588 
590 (
591  iOstream& os,
592  const IOPattern& iop
593 )const
594 {
595  auto aPoints = activePointsHost();
596  return aPoints.write(os,iop);
597 }
pFlow::internalPoints::activePointsHost
PointsTypeHost activePointsHost() const
Definition: internalPoints.cpp:222
anyList.hpp
pFlow::pOutput
processorOstream pOutput
pFlow::internalPoints::pFlagsD_
pFlagTypeDevice pFlagsD_
flag of points on device
Definition: internalPoints.hpp:68
pFlow::DefaultHostExecutionSpace
Kokkos::DefaultHostExecutionSpace DefaultHostExecutionSpace
Default Host execution space, on top of all host execution spaces.
Definition: KokkosTypes.hpp:57
pFlow::scatteredFieldAccess
Definition: scatteredFieldAccess.hpp:32
pFlow::message
Definition: message.hpp:33
domain.hpp
pFlow::anyList::getObject
T & getObject(size_t i)
Get the reference to variable by index.
Definition: anyList.hpp:134
pFlow::internalPoints::pointPosition
const PointsType & pointPosition() const
Definition: internalPoints.cpp:209
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::internalPoints::deletePoints
bool deletePoints(const uint32Vector_D &delPoints)
Definition: internalPoints.cpp:37
pFlow::internalPoints::fillNeighborsLists
void fillNeighborsLists(ViewType1D< uint32, memory_space > leftList, ViewType1D< uint32, memory_space > rightList, ViewType1D< uint32, memory_space > bottomList, ViewType1D< uint32, memory_space > topList, ViewType1D< uint32, memory_space > rearList, ViewType1D< uint32, memory_space > frontList)
Definition: internalPoints.cpp:291
pFlow::Vector::size
auto size() const
Size of the vector.
Definition: Vector.hpp:265
Vectors.hpp
pFlow::internalPoints::pFlagSync_
bool pFlagSync_
if both host and device flags sync
Definition: internalPoints.hpp:74
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::message::SIZE_CHANGED
@ SIZE_CHANGED
Definition: message.hpp:40
pFlow::Field< realx3 >
pFlow::subscriber
Definition: subscriber.hpp:37
pFlow::VectorSingle::empty
INLINE_FUNCTION_H bool empty() const
If vector is empty.
Definition: VectorSingle.cpp:311
pFlow::pointFlag::clone
pointFlag< ExeSpace > clone() const
Definition: pointFlag.hpp:291
pFlow::VectorSingle::deviceView
INLINE_FUNCTION_H auto deviceView() const
Device view range [0, size)
Definition: VectorSingle.cpp:263
RESERVE
Definition: Vector.hpp:40
END_WARNING
#define END_WARNING
Definition: streams.hpp:44
pFlow::anyList
Definition: anyList.hpp:35
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::internalPoints::activePointsMaskDevice
const pFlagTypeDevice & activePointsMaskDevice() const
Definition: internalPoints.cpp:194
pFlow::Vector::capacity
auto capacity() const
Capacity of the vector.
Definition: Vector.hpp:271
pFlow::internalPoints::changePointsFlagPosition
bool changePointsFlagPosition(const uint32Vector_D &changePoints, realx3 transferVector, uint32 fromBoundaryIndex, uint32 toBoundaryIndex)
Definition: internalPoints.cpp:92
pFlow::internalPoints::PointsTypeHost
typename PointsType::FieldTypeHost PointsTypeHost
Definition: internalPoints.hpp:48
pFlow::internalPoints::sortPoints
bool sortPoints(const uint32IndexContainer &sortedIndices)
Definition: internalPoints.cpp:145
pFlow::IOPattern
Definition: IOPattern.hpp:32
Time.hpp
pFlow::internalPointsKernels::changePosition
void changePosition(deviceViewType1D< realx3 > points, deviceViewType1D< uint32 > indices, realx3 transferVector)
Definition: internalPointsKernels.cpp:24
pFlow::internalPoints::pFlagsH_
pFlagTypeHost pFlagsH_
flag of points on host
Definition: internalPoints.hpp:71
pFlow::VectorSingle< uint32 >
pFlow::message::ITEM_INSERT
@ ITEM_INSERT
Definition: message.hpp:42
pFlow::internalPoints::updateFlag
uint32 updateFlag(const domain &dm, const std::array< real, 6 > &dist)
Definition: internalPoints.cpp:271
pFlow::IOstream::name
virtual const word & name() const
Return the name of the stream.
Definition: IOstream.cpp:31
pFlow::internalPoints::insertPoints
bool insertPoints(const realx3Vector &points, anyList &varList)
Definition: internalPoints.cpp:311
internalPointsKernels.hpp
pFlow::ViewType1D
Kokkos::View< T *, properties... > ViewType1D
1D veiw as a vector
Definition: KokkosTypes.hpp:93
pFlow::message::RANGE_CHANGED
@ RANGE_CHANGED
Definition: message.hpp:43
n
uint32 n
Definition: NBSLoop.hpp:24
pFlow::message::ITEM_DELETE
@ ITEM_DELETE
Definition: message.hpp:41
pFlow::message::addAndName
const word & addAndName(EVENT evnt)
Definition: message.hpp:119
pFlow::internalPoints::syncPFlag
void syncPFlag() const
Definition: internalPoints.cpp:28
pFlow::message::CAP_CHANGED
@ CAP_CHANGED
Definition: message.hpp:39
pFlow::internalPoints::read
bool read(iIstream &is)
Read.
Definition: internalPoints.cpp:530
pFlow::internalPoints::write
bool write(iOstream &os) const
Write.
Definition: internalPoints.cpp:556
pFlow::indexContainer::size
INLINE_FUNCTION_HD auto size() const
Size.
Definition: indexContainer.hpp:175
pFlow::domain
Definition: domain.hpp:31
pFlow::internalPoints::insertPointsOnly
bool insertPointsOnly(const realx3Vector_D &points, message &msg, anyList &varList)
Definition: internalPoints.cpp:427
pFlow::internalPoints::activePointsMaskHost
const pFlagTypeHost & activePointsMaskHost() const
Definition: internalPoints.cpp:201
pFlow::triple< real >
pFlow::Vector< realx3 >
pFlow::pFlagTypeDevice
pointFlag< DefaultExecutionSpace > pFlagTypeDevice
Definition: pointFlag.hpp:387
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
internalPoints.hpp
pFlow::internalPoints::internalPoints
internalPoints()
Definition: internalPoints.cpp:159
pFlow::anyList::emplaceBack
reference emplaceBack(const word &name, Args &&... args)
Create variable using constructor in-place.
Definition: anyList.hpp:84
pFlow::pointFlag< DefaultExecutionSpace >
pFlow::indexContainer
It holds two vectors of indecis on Host and Device.
Definition: indexContainer.hpp:39
pFlow::message::ITEM_FLAGCHANGED
@ ITEM_FLAGCHANGED
Definition: message.hpp:45
pFlow::VectorSingle::size
INLINE_FUNCTION_H uint32 size() const
Size of the vector.
Definition: VectorSingle.cpp:297
pFlow::message::add
message & add(EVENT evnt)
Definition: message.hpp:113
pFlow::message::eventName
static const word & eventName(size_t event)
Definition: message.hpp:196
WARNING
#define WARNING
Definition: streams.hpp:43