positionRandom.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 "positionRandom.hpp"
23 #include "uniformRandomReal.hpp"
24 #include "NBSLevel0.hpp"
25 #include "unsortedPairs.hpp"
26 #include "box.hpp"
27 
28 
29 
30 namespace pFlow
31 {
32 
35 
36 
37 
39  ContainerType& pairs,
40  int32Vector_HD& flags);
41 
43 {
44  int32 res =0;
45  for(auto i=0; i<num;i++)
46  {
47  for(auto j=i+1; j<num; j++)
48  {
49  if(sphereSphereCheck(points[i],points[j],diam,diam))res++;
50  }
51  }
52 
53  return res;
54 }
55 
56 }
57 
58 
60 {
61 
62  realVector_D diameter(startNum , diameter_);
63  int32Vector_HD flagHD(startNum, 0);
64  realx3Vector_HD positionHD(startNum);
65 
66  auto minP = region_->minPoint();
67  auto maxP = region_->maxPoint();
68 
69  SearchType search(
70  box(minP, maxP),
71  diameter_,
72  positionHD.deviceVectorAll(),
73  diameter.deviceVectorAll());
74 
75  ContainerType pairs(3*startNum);
76 
77  REPORT(1)<< "Positioning "<<
78  greenText("(Pass #"<< pass+1<<")")<<
79  ": started with "<< startNum <<" points."<<endREPORT;
80 
81  fillPoints(startNum, positionHD, flagHD);
82 
83  search.broadSearch(pairs, range(0, startNum));
84 
85 
86  int32 numCollisions = findCollisions(pairs, flagHD);
87 
88 
89  REPORT(2)<< "Positioned " << cyanText(startNum - numCollisions) <<
90  " without collision \n"<<endREPORT;
91 
92  if(startNum-numCollisions >= numPoints_ )
93  {
94 
95  REPORT(1)<<"Selected "<< cyanText(numPoints_)<< " for the final field.\n"<<endREPORT;
96 
97  positionHD.syncViews();
98  position_.clear();
99  int32 n=0;
100  for(int32 i=0; i<startNum; i++)
101  {
102  if(flagHD[i] == 0 )
103  {
104  position_.push_back( positionHD[i]);
105  n++;
106  if(n==numPoints_)break;
107  }
108 
109  }
110 
111  return true;
112  }
113 
114 
115  return false;
116 
117 
118 }
119 
121 {
122 
123  position_.clear();
124 
125  if(numPoints_ == 0)return true;
126 
127  size_t pass = 0;
128  int32 startNum = numPoints_;
129 
130  while ( pass <maxIterations_)
131  {
132  if( positionOnePass(pass, startNum) )return true;
133  startNum = 1.1*startNum+1;
134  pass++;
135  }
136 
137 
139  " cannot position "<< numPoints_ << " in the domain in " << maxIterations_ << " iterations.\n" <<
140  " you may increase maxIterations for positioning points.\n";
141 
142  return false;
143 }
144 
146 (
147  const realx3 &cntr,
148  real diam
149 )
150 {
151  for(const auto& cp: position_)
152  {
153  if( length(cp-cntr) <= diam ) return true;
154  }
155 
156  return false;
157 }
158 
160 (
161  const dictionary& dict
162 )
163 :
164  positionParticles(dict),
165  prDict_
166  (
167  dict.subDict("positionRandomInfo")
168  ),
169  diameter_
170  (
171  prDict_.getVal<real>("diameter")
172  ),
173  numPoints_
174  (
175  prDict_.getVal<size_t>("numPoints")
176  ),
177  maxIterations_
178  (
179  prDict_.getValOrSet("maxIterations", 10)
180  ),
181  position_
182  (
183  maxNumberOfParticles_, RESERVE()
184  )
185 {
186 
187  reportInterval_ = max(numPoints_/numReports_, static_cast<size_t>(2));
188 
189  if( !positionPointsRandom() )
190  {
191  fatalExit;
192  }
193 
194  if(!region_)
195  {
196  fatalErrorInFunction<<"You must provided a region (box, cylinder, ...) for positioning particles in dictionary "<<
197  dict.globalName()<<endl;
198  fatalExit;
199  }
200 
201 }
202 
204  uint numPoints,
205  realx3Vector_HD& points,
206  int32Vector_HD& flags )
207 {
208 
209  uniformRandomReal rand;
210 
211  auto minP = region_().minPoint();
212  auto maxP = region_().maxPoint();
213 
214  for(size_t i=0; i<numPoints; i++)
215  {
216  if(flags[i] == 0)
217  {
218  bool loop=true;
219  size_t n=0;
220  while (loop)
221  {
222 
223  auto pos = rand(minP, maxP);
224  if( region_().isInside(pos))
225  {
226  points[i] =pos;
227  loop = false;
228  }
229  n++;
230 
231  if(n>100)
232  {
234  "could not find a point inside region"<<region_->name()<<endl;
235  fatalExit;
236  }
237 
238  }
239 
240  }
241  }
242  points.modifyOnHost();
243  points.syncViews();
244 }
245 
247  ContainerType& pairs,
248  int32Vector_HD& flags)
249 {
250  auto allPairs = pairs.getPairs();
251  auto num = pairs.capacity();
252  auto dFlags = flags.deviceVector();
253 
254 
255  int32 numCollisions = 0;
256 
257  Kokkos::parallel_reduce(
258  "positionRandom::findCollisions",
259  num,
260  LAMBDA_HD(int32 i, int32& valueToUpdate){
261  if(allPairs.isValid(i))
262  {
263  auto pair = allPairs.getPair(i);
264  if( dFlags[pair.first] ==0 )
265  {
266  dFlags[pair.first] = 1;
267  valueToUpdate++;
268  }
269  }
270  }, numCollisions);
271 
272  flags.modifyOnDevice();
273  flags.syncViews();
274 
275  return numCollisions;
276 }
277 
278 
endREPORT
#define endREPORT
Definition: streams.hpp:41
pFlow::VectorDual::syncViews
INLINE_FUNCTION_H void syncViews()
Definition: VectorDual.hpp:875
pFlow::real
float real
Definition: builtinTypes.hpp:46
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::NBSLevel0
Definition: NBSLevel0.hpp:32
REPORT
#define REPORT(n)
Definition: streams.hpp:40
pFlow::positionRandom::positionRandom
positionRandom(const dictionary &dict)
Definition: positionRandom.cpp:160
cyanText
#define cyanText(text)
Definition: streams.hpp:34
positionRandom.hpp
pFlow::unsortedPairs::capacity
INLINE_FUNCTION_HD int32 capacity() const
Definition: unsortedPairs.hpp:168
NBSLevel0.hpp
pFlow::positionRandom::position_
realx3Vector position_
Definition: positionRandom.hpp:49
pFlow::positionRandom::fillPoints
void fillPoints(uint numPoints, realx3Vector_HD &points, int32Vector_HD &flags)
Definition: positionRandom.cpp:203
box.hpp
pFlow::dictionary::globalName
virtual word globalName() const
Definition: dictionary.cpp:349
pFlow::positionParticles::region_
uniquePtr< regionBase > region_
Definition: positionParticles.hpp:106
pFlow::NBSLevel0::broadSearch
bool broadSearch(PairsContainer &pairs, range activeRange)
Definition: NBSLevel0.hpp:152
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::VectorDual::modifyOnHost
INLINE_FUNCTION_H void modifyOnHost()
Definition: VectorDual.hpp:796
greenText
#define greenText(text)
Definition: streams.hpp:32
pFlow::unsortedPairs
Definition: unsortedPairs.hpp:32
pFlow
Definition: demComponent.hpp:28
RESERVE
Definition: Vector.hpp:38
pFlow::uniformRandomReal
Definition: uniformRandomReal.hpp:32
pFlow::VectorDual::deviceVectorAll
INLINE_FUNCTION_H deviceViewType & deviceVectorAll()
Definition: VectorDual.hpp:335
pFlow::positionRandom::positionOnePass
bool positionOnePass(int32 pass, int32 startNum)
Definition: positionRandom.cpp:59
n
int32 n
Definition: NBSCrossLoop.hpp:24
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
length
INLINE_FUNCTION_HD T length(const triple< T > &v1)
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
pFlow::positionRandom::positionPointsRandom
bool positionPointsRandom()
Definition: positionRandom.cpp:120
pFlow::VectorDual< int32 >
uniformRandomReal.hpp
pFlow::positionRandom::diameter_
real diameter_
Definition: positionRandom.hpp:43
unsortedPairs.hpp
pFlow::VectorSingle
Definition: VectorSingle.hpp:45
pFlow::unsortedPairs::getPairs
pairAccessor getPairs() const
Definition: unsortedPairs.hpp:185
pFlow::dictionary::subDict
dictionary & subDict(const word &keyword)
Definition: dictionary.cpp:547
pFlow::max
T max(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:164
pFlow::Vector::clear
auto clear()
Definition: Vector.hpp:248
pFlow::sphereSphereCheck
INLINE_FUNCTION_HD bool sphereSphereCheck(const realx3 &p1, const realx3 p2, real d1, real d2)
Definition: contactSearchFunctions.hpp:105
pFlow::box
Definition: box.hpp:32
pFlow::positionParticles
Definition: positionParticles.hpp:102
LAMBDA_HD
#define LAMBDA_HD
Definition: pFlowMacros.hpp:54
pFlow::VectorDual::deviceVector
INLINE_FUNCTION_H deviceViewType & deviceVector()
Definition: VectorDual.hpp:354
pFlow::findCollisions
int32 findCollisions(ContainerType &pairs, int32Vector_HD &flags)
Definition: positionRandom.cpp:246
pFlow::VectorSingle::deviceVectorAll
INLINE_FUNCTION_H viewType & deviceVectorAll()
Definition: VectorSingle.hpp:295
pFlow::triple< real >
pFlow::positionRandom::inCollision
bool inCollision(const realx3 &cntr, real diam)
Definition: positionRandom.cpp:146
pFlow::range
kPair< int, int > range
Definition: KokkosTypes.hpp:54
pFlow::dictionary
Definition: dictionary.hpp:38
pFlow::positionRandom::numPoints_
size_t numPoints_
Definition: positionRandom.hpp:45