# PhasicFlow: src/phasicFlow/structuredData/pointStructure/selectors/selectRandom/selectRandom.cpp Source File

selectRandom.cpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
#include "selectRandom.hpp"
#include "dictionary.hpp"
#include "uniformRandomInt32.hpp"
#include "Set.hpp"
bool pFlow::selectRandom::selectAllPointsInRange()
{
// to reduct allocations
int32 maxNum = number_+1;
selectedPoints_.reserve (maxNum);
selectedPoints_.clear();
uniformRandomInt32 intRand (begin_, end_);
int32 n = 0;
int32 iter = 0;
bool finished = false;
Set<int32> selctedIndices;
while( iter < number_*100)
{
int32 newInd = intRand.randomNumber();
if( auto [it, inserted] = selctedIndices.insert(newInd); inserted )
{
n++;
if(n == number_)
{
finished = true;
break;
}
}
iter++;
}
if(finished)
{
for(auto& ind:selctedIndices)
{
selectedPoints_.push_back(ind);
}
return true;
}else
{
fatalErrorInFunction<< "Could not find random indices in the range."<<endl;
return false;
}
}
pFlow::selectRandom::selectRandom
(
const pointStructure& pStruct,
const dictionary& dict
)
:
pStructSelector
(
pStruct, dict
),
begin_
(
dict.subDict("selectRandomInfo").getVal<int32>("begin")
),
end_
(
dict.subDict("selectRandomInfo").getValOrSet("end", pStruct.size())
),
number_
(
dict.subDict("selectRandomInfo").getValOrSet("number", 1)
)
{
begin_ = max(begin_,1);
end_ = min(end_, static_cast< int32 >(pStruct.size()));
number_ = max(number_,0);
if(end_-begin_ < number_)
{
warningInFunction<< "In dictionary " << dict.globalName()<<
" number is greater than the interval defined by begine and end ["<<
begin_<<" "<<end_<<"), resetting it to "<<end_-begin_<<endl;
number_ = end_-begin_;
}
if(!selectAllPointsInRange())
{
fatalExit;
}
}
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