# PhasicFlow: utilities/particlesPhasicFlow/positionRandom/positionRandom.cpp Source File

positionRandom.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 "positionRandom.hpp"
#include "uniformRandomReal.hpp"
#include "NBSLevel0.hpp"
#include "unsortedPairs.hpp"
#include "box.hpp"
namespace pFlow
{
using SearchType = NBSLevel0<DefaultExecutionSpace> ;
using ContainerType = unsortedPairs<DefaultExecutionSpace, int32>;
int32 findCollisions(
ContainerType& pairs,
int32Vector_HD& flags);
int32 findCollisions(int32 num, realx3Vector_HD& points, real diam)
{
int32 res =0;
for(auto i=0; i<num;i++)
{
for(auto j=i+1; j<num; j++)
{
if(sphereSphereCheck(points[i],points[j],diam,diam))res++;
}
}
return res;
}
}
bool pFlow::positionRandom::positionOnePass(int32 pass, int32 startNum)
{
realVector_D diameter(startNum , diameter_);
int32Vector_HD flagHD(startNum, 0);
realx3Vector_HD positionHD(startNum);
auto minP = region_->minPoint();
auto maxP = region_->maxPoint();
SearchType search(
box(minP, maxP),
diameter_,
positionHD.deviceVectorAll(),
diameter.deviceVectorAll());
ContainerType pairs(3*startNum);
REPORT(1)<< "Positioning "<<
greenText("(Pass #"<< pass+1<<")")<<
": started with "<< startNum <<" points."<<endREPORT;
fillPoints(startNum, positionHD, flagHD);
search.broadSearch(pairs, range(0, startNum));
int32 numCollisions = findCollisions(pairs, flagHD);
REPORT(2)<< "Positioned " << cyanText(startNum - numCollisions) <<
" without collision \n"<<endREPORT;
if(startNum-numCollisions >= numPoints_ )
{
REPORT(1)<<"Selected "<< cyanText(numPoints_)<< " for the final field.\n"<<endREPORT;
positionHD.syncViews();
position_.clear();
int32 n=0;
for(int32 i=0; i<startNum; i++)
{
if(flagHD[i] == 0 )
{
position_.push_back( positionHD[i]);
n++;
if(n==numPoints_)break;
}
}
return true;
}
return false;
}
bool pFlow::positionRandom::positionPointsRandom()
{
position_.clear();
if(numPoints_ == 0)return true;
size_t pass = 0;
int32 startNum = numPoints_;
while ( pass <maxIterations_)
{
if( positionOnePass(pass, startNum) )return true;
startNum = 1.1*startNum+1;
pass++;
}
fatalErrorInFunction<<
" cannot position "<< numPoints_ << " in the domain in " << maxIterations_ << " iterations.\n" <<
" you may increase maxIterations for positioning points.\n";
return false;
}
bool pFlow::positionRandom::inCollision
(
const realx3 &cntr,
real diam
)
{
for(const auto& cp: position_)
{
if( length(cp-cntr) <= diam ) return true;
}
return false;
}
pFlow::positionRandom::positionRandom
(
const dictionary& dict
)
:
positionParticles(dict),
prDict_
(
dict.subDict("positionRandomInfo")
),
diameter_
(
prDict_.getVal<real>("diameter")
),
numPoints_
(
prDict_.getVal<size_t>("numPoints")
),
maxIterations_
(
prDict_.getValOrSet("maxIterations", 10)
),
position_
(
maxNumberOfParticles_, RESERVE()
)
{
reportInterval_ = max(numPoints_/numReports_, static_cast<size_t>(2));
if( !positionPointsRandom() )
{
fatalExit;
}
if(!region_)
{
fatalErrorInFunction<<"You must provided a region (box, cylinder, ...) for positioning particles in dictionary "<<
dict.globalName()<<endl;
fatalExit;
}
}
void pFlow::positionRandom::fillPoints(
uint numPoints,
realx3Vector_HD& points,
int32Vector_HD& flags )
{
uniformRandomReal rand;
auto minP = region_().minPoint();
auto maxP = region_().maxPoint();
for(size_t i=0; i<numPoints; i++)
{
if(flags[i] == 0)
{
bool loop=true;
size_t n=0;
while (loop)
{
auto pos = rand(minP, maxP);
if( region_().isInside(pos))
{
points[i] =pos;
loop = false;
}
n++;
if(n>100)
{
fatalErrorInFunction<<
"could not find a point inside region"<<region_->name()<<endl;
fatalExit;
}
}
}
}
points.modifyOnHost();
points.syncViews();
}
pFlow::int32 pFlow::findCollisions(
ContainerType& pairs,
int32Vector_HD& flags)
{
auto allPairs = pairs.getPairs();
auto num = pairs.capacity();
auto dFlags = flags.deviceVector();
int32 numCollisions = 0;
Kokkos::parallel_reduce(
"positionRandom::findCollisions",
num,
LAMBDA_HD(int32 i, int32& valueToUpdate){
if(allPairs.isValid(i))
{
auto pair = allPairs.getPair(i);
if( dFlags[pair.first] ==0 )
{
dFlags[pair.first] = 1;
valueToUpdate++;
}
}
}, numCollisions);
flags.modifyOnDevice();
flags.syncViews();
return numCollisions;
}
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