# PhasicFlow: src/phasicFlow/structuredData/pointStructure/selectors/selectorRandomPoints/selectorRandomPoints.cpp Source File
PhasicFlow is an open-source parallel DEM (discrete element method) package for simulating granular flow. It is developed in C++ and can be exectued on both GPU (like CUDA) and CPU.

selectorRandomPoints.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 "selectorRandomPoints.hpp"
#include "Set.hpp"
#include "dictionary.hpp"
#include "uniformRandomUint32.hpp"
bool
pFlow::selectorRandomPoints::selectAllPointsInRange()
{
// to reduct allocations
uint32 maxNum = number_ + 1;
selectedPoints_.reserve(maxNum);
selectedPoints_.clear();
uniformRandomUint32 intRand(begin_, end_);
uint32 n = 0;
uint32 iter = 0;
bool finished = false;
Set<uint32> selctedIndices;
while (iter < number_ * 100)
{
uint32 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::selectorRandomPoints::selectorRandomPoints(
const pointStructure& pStruct,
const dictionary& dict
)
: selectorRandomPoints(
"randomPoints",
pStruct,
dict.subDict("randomPointsInfo")
)
{
}
pFlow::selectorRandomPoints::selectorRandomPoints(
const word& type,
const pointStructure& pStruct,
const dictionary& dict
)
: pStructSelector(type, pStruct, dict),
begin_(dict.getVal<uint32>("begin")),
end_(dict.getValOrSet("end", pStruct.size())),
number_(dict.getValOrSet("number", 1))
{
begin_ = max(begin_, 1u);
end_ = min(end_, static_cast< uint32 >(pStruct.size()));
number_ = max(number_, 0u);
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;
}
}
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