www.cemf.ir
selectorRandomPoints.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 "selectorRandomPoints.hpp"
22 #include "Set.hpp"
23 #include "dictionary.hpp"
24 #include "uniformRandomUint32.hpp"
25 
26 bool
28 {
29  // to reduct allocations
30  uint32 maxNum = number_ + 1;
31 
32  selectedPoints_.reserve(maxNum);
33 
34  selectedPoints_.clear();
35 
37 
38  uint32 n = 0;
39  uint32 iter = 0;
40  bool finished = false;
41 
42  Set<uint32> selctedIndices;
43 
44  while (iter < number_ * 100)
45  {
46  uint32 newInd = intRand.randomNumber();
47 
48  if (auto [it, inserted] = selctedIndices.insert(newInd); inserted)
49  {
50  n++;
51 
52  if (n == number_)
53  {
54  finished = true;
55  break;
56  }
57  }
58  iter++;
59  }
60 
61  if (finished)
62  {
63  for (auto& ind : selctedIndices)
64  {
65  selectedPoints_.push_back(ind);
66  }
67 
68  return true;
69  }
70  else
71  {
72  fatalErrorInFunction << "Could not find random indices in the range."
73  << endl;
74  return false;
75  }
76 }
77 
79  const pointStructure& pStruct,
80  const dictionary& dict
81 )
83  "randomPoints",
84  pStruct,
85  dict.subDict("randomPointsInfo")
86  )
87 {
88 }
89 
91  const word& type,
92  const pointStructure& pStruct,
93  const dictionary& dict
94 )
95  : pStructSelector(type, pStruct, dict),
96  begin_(dict.getVal<uint32>("begin")),
97  end_(dict.getValOrSet("end", pStruct.size())),
98  number_(dict.getValOrSet("number", 1))
99 {
100  begin_ = max(begin_, 1u);
101  end_ = min(end_, static_cast<uint32>(pStruct.size()));
102  number_ = max(number_, 0u);
103 
104  if (end_ - begin_ < number_)
105  {
107  << "In dictionary " << dict.globalName()
108  << " number is greater than the interval defined by begine and end ["
109  << begin_ << " " << end_ << "), resetting it to " << end_ - begin_
110  << endl;
111 
112  number_ = end_ - begin_;
113  }
114 
115  if (!selectAllPointsInRange())
116  {
117  fatalExit;
118  }
119 }
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
Set.hpp
warningInFunction
#define warningInFunction
Report a warning.
Definition: error.hpp:95
pFlow::Vector::reserve
void reserve(size_t cap)
Reserve capacity for vector Preserve the content.
Definition: Vector.hpp:284
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::selectorRandomPoints::begin_
uint32 begin_
Definition: selectorRandomPoints.hpp:43
pFlow::uniformRandomUint32::randomNumber
uint32 randomNumber()
Definition: uniformRandomUint32.hpp:53
pFlow::selectorRandomPoints::selectedPoints_
uint32Vector selectedPoints_
Definition: selectorRandomPoints.hpp:40
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
selectorRandomPoints.hpp
pFlow::max
T max(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:79
pFlow::Set
std::set< Key, std::less< Key >, std::allocator< Key > > Set
Definition: Set.hpp:33
pFlow::dictionary::globalName
virtual word globalName() const
global name of entry, separated with dots
Definition: dictionary.cpp:356
pFlow::selectorRandomPoints::selectorRandomPoints
selectorRandomPoints(const pointStructure &pStruct, const dictionary &dict)
Definition: selectorRandomPoints.cpp:78
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::selectorRandomPoints::number_
uint32 number_
Definition: selectorRandomPoints.hpp:49
pFlow::selectorRandomPoints::end_
uint32 end_
Definition: selectorRandomPoints.hpp:46
pFlow::pointStructure
Definition: pointStructure.hpp:34
pFlow::uniformRandomUint32
Definition: uniformRandomUint32.hpp:32
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
dictionary.hpp
pFlow::selectorRandomPoints
Definition: selectorRandomPoints.hpp:34
pFlow::pStructSelector::pStruct
const pointStructure & pStruct() const
Definition: pStructSelector.cpp:45
pFlow::min
T min(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:28
uniformRandomUint32.hpp
pStruct
auto & pStruct
Definition: setPointStructure.hpp:24
pFlow::internalPoints::size
uint32 size() const
Definition: internalPoints.hpp:168
n
uint32 n
Definition: NBSLoop.hpp:24
pFlow::pStructSelector
Definition: pStructSelector.hpp:36
pFlow::dictionary
Dictionary holds a set of data entries or sub-dictionaries that are enclosed in a curely braces or ar...
Definition: dictionary.hpp:67
pFlow::selectorRandomPoints::selectAllPointsInRange
bool selectAllPointsInRange()
Definition: selectorRandomPoints.cpp:27