selectRandom.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 "selectRandom.hpp"
23 #include "dictionary.hpp"
24 #include "uniformRandomInt32.hpp"
25 #include "Set.hpp"
26 
27 
29 {
30  // to reduct allocations
31  int32 maxNum = number_+1;
32 
33  selectedPoints_.reserve (maxNum);
34 
36 
37  uniformRandomInt32 intRand (begin_, end_);
38 
39 
40  int32 n = 0;
41  int32 iter = 0;
42  bool finished = false;
43 
44  Set<int32> selctedIndices;
45 
46  while( iter < number_*100)
47  {
48  int32 newInd = intRand.randomNumber();
49 
50  if( auto [it, inserted] = selctedIndices.insert(newInd); inserted )
51  {
52  n++;
53 
54  if(n == number_)
55  {
56  finished = true;
57  break;
58  }
59  }
60  iter++;
61  }
62 
63 
64  if(finished)
65  {
66  for(auto& ind:selctedIndices)
67  {
68  selectedPoints_.push_back(ind);
69  }
70 
71  return true;
72 
73  }else
74  {
75  fatalErrorInFunction<< "Could not find random indices in the range."<<endl;
76  return false;
77  }
78 
79 
80 }
81 
83 (
84  const pointStructure& pStruct,
85  const dictionary& dict
86 )
87 :
89  (
90  pStruct, dict
91  ),
92  begin_
93  (
94  dict.subDict("selectRandomInfo").getVal<int32>("begin")
95  ),
96  end_
97  (
98  dict.subDict("selectRandomInfo").getValOrSet("end", pStruct.size())
99  ),
100  number_
101  (
102  dict.subDict("selectRandomInfo").getValOrSet("number", 1)
103  )
104 {
105  begin_ = max(begin_,1);
106  end_ = min(end_, static_cast<int32>(pStruct.size()));
107  number_ = max(number_,0);
108  if(end_-begin_ < number_)
109  {
110 
111  warningInFunction<< "In dictionary " << dict.globalName()<<
112  " number is greater than the interval defined by begine and end ["<<
113  begin_<<" "<<end_<<"), resetting it to "<<end_-begin_<<endl;
114 
115  number_ = end_-begin_;
116  }
117 
118  if(!selectAllPointsInRange())
119  {
120  fatalExit;
121  }
122 }
pFlow::selectRandom::number_
int32 number_
Definition: selectRandom.hpp:49
pFlow::dictionary::getValOrSet
T getValOrSet(const word &keyword, const T &setVal) const
Definition: dictionary.hpp:325
fatalExit
#define fatalExit
Definition: error.hpp:57
pFlow::selectRandom::selectRandom
selectRandom(const pointStructure &pStruct, const dictionary &dict)
Definition: selectRandom.cpp:83
uniformRandomInt32.hpp
pFlow::selectRandom::selectAllPointsInRange
bool selectAllPointsInRange()
Definition: selectRandom.cpp:28
Set.hpp
warningInFunction
#define warningInFunction
Definition: error.hpp:55
pFlow::Set
std::set< Key, std::less< Key >, std::allocator< Key > > Set
Definition: Set.hpp:31
pFlow::dictionary::globalName
virtual word globalName() const
Definition: dictionary.cpp:349
pFlow::endl
iOstream & endl(iOstream &os)
Definition: iOstream.hpp:312
pFlow::selectRandom::selectedPoints_
int32Vector selectedPoints_
Definition: selectRandom.hpp:40
pFlow::uniformRandomInt32::randomNumber
real randomNumber()
Definition: uniformRandomInt32.hpp:53
pFlow::uniformRandomInt32
Definition: uniformRandomInt32.hpp:32
pFlow::pointStructure
Definition: pointStructure.hpp:44
n
int32 n
Definition: NBSCrossLoop.hpp:24
pFlow::selectRandom::begin_
int32 begin_
Definition: selectRandom.hpp:43
fatalErrorInFunction
#define fatalErrorInFunction
Definition: error.hpp:42
pFlow::int32
int int32
Definition: builtinTypes.hpp:53
dictionary.hpp
pFlow::Vector::reserve
auto reserve(label len)
Definition: Vector.hpp:309
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
pStruct
auto & pStruct
Definition: setPointStructure.hpp:24
pFlow::dictionary::getVal
T getVal(const word &keyword) const
Definition: dictionary.hpp:309
pFlow::Vector::clear
auto clear()
Definition: Vector.hpp:248
pFlow::pStructSelector
Definition: pStructSelector.hpp:36
pFlow::min
T min(const Vector< T, Allocator > &v)
Definition: VectorMath.hpp:138
pFlow::dictionary
Definition: dictionary.hpp:38
pFlow::selectRandom::end_
int32 end_
Definition: selectRandom.hpp:46
selectRandom.hpp