/*------------------------------- 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. -----------------------------------------------------------------------------*/ #ifndef __cellsSimple_H__ #define __cellsSimple_H__ #include "types.H" #include "KokkosTypes.H" #include "cells.H" #include "iBox.H" #include "dictionary.H" namespace pFlow { template< typename executionSpace, typename idType, typename indexType = int32 > class cellsSimple : public cells { public: using IdType = idType; using IndexType = indexType; using Cells = cells; using CellType = typename Cells::CellType; using ExecutionSpace = executionSpace; using memory_space = typename ExecutionSpace::memory_space; using iBoxType = iBox; bool constexpr static LOOP_ELEMENT_RANGE = true; class TagFindCellRange2{}; protected: // - box extent real cellExtent_ = 0.6; // - update frequency int32 updateFrequency_=1; int32 currentIter_ = 0; // - number of triangle elements int32 numElements_ = 0; // - number of points int32 numPoints_ = 0; /// a broad search has been occured during last pass? bool performedSearch_ = false; // - ref to vectices ViewType1D vertices_; // - ref to points in the trisurface ViewType1D points_; // cell range of element/triangle bounding box ViewType1D elementBox_; using tpPWContactSearch = Kokkos::TeamPolicy< ExecutionSpace, Kokkos::Schedule, Kokkos::IndexType >; using rpFindCellRange2Type = Kokkos::RangePolicy>; FUNCTION_H void allocateArrays() { Kokkos::realloc( elementBox_, numElements_); } private: bool performSearch() { if(currentIter_ % updateFrequency_ == 0) { currentIter_++; return true; }else { currentIter_++; return false; } } public: TypeNameNV("cellsSimple"); FUNCTION_H cellsSimple( Cells ppCells, real cellExtent, int32 numPoints, int32 numElements, const ViewType1D& points, const ViewType1D& vertices ) : Cells(ppCells), cellExtent_( max(cellExtent, 0.6 ) ), numElements_(numElements), numPoints_(numPoints), vertices_(vertices), points_(points) { allocateArrays(); } cellsSimple( dictionary dict, Cells ppCells, int32 numPoints, int32 numElements, const ViewType1D& points, const ViewType1D& vertices ) : Cells(ppCells), numElements_(numElements), numPoints_(numPoints), vertices_(vertices), points_(points) { updateFrequency_ = dict.getVal( "updateFrequency" ); updateFrequency_ = max(updateFrequency_,1); cellExtent_ = dict.getVal( "cellExtent"); cellExtent_ = max(cellExtent_,0.6); allocateArrays(); } constexpr bool loopElementRange()const { return LOOP_ELEMENT_RANGE; } // - host call // reset triangle elements if they have changed FUNCTION_H bool resetElements( int32 numElements, int32 numPoints, ViewType1D& points, ViewType1D& vertices ) { numElements_ = numElements; numPoints_ = numPoints; points_ = points; vertices_ = vertices; allocateArrays(); return true; } INLINE_FUNCTION_HD iBoxType elementBox(int32 i)const { return elementBox_[i]; } INLINE_FUNCTION_HD int32 numElements()const { return numElements_; } bool enterBoadSearch()const { return currentIter_%updateFrequency_==0; } bool performedSearch()const { return performedSearch_; } template bool broadSearch(PairsContainer& pairs, particleMapType& particleMap, bool force=false) { if(force) currentIter_ = 0; performedSearch_= false; if(!performSearch())return true; // map walls onto the cells this->build(); this->particleWallFindPairs(pairs, particleMap); performedSearch_ = true; return true; } bool build() { Kokkos::parallel_for( "cellsSimple::findcellrange2", rpFindCellRange2Type(0,numElements_), *this); Kokkos::fence(); return true; } template bool particleWallFindPairs(PairsContainer& pairs, particleMapType& particleMap) { int32 getFull = 1; while (getFull) { getFull = findPairsElementRange(pairs, particleMap); if(getFull) { // - resize the container // note that getFull now shows the number of failed insertions. uint32 len = max(getFull, 50); auto oldCap = pairs.capacity(); pairs.increaseCapacityBy(len); Info<<"Contact pair container capacity increased from "<< oldCap << " to " << pairs.capacity() <<" in cellsSimple."< int32 findPairsElementRange(PairsContainer& pairs, particleMapType& particleMap) { int32 getFull =0; const auto pwPairs = pairs; const auto elementBox = elementBox_; auto cellIter = particleMap.getCellIterator(); Kokkos::parallel_reduce( "cellsSimple::findPairsElementRangeModified2", tpPWContactSearch(numElements_, Kokkos::AUTO), LAMBDA_HD( const typename tpPWContactSearch::member_type & teamMember, int32& valueToUpdate){ const int32 iTri = teamMember.league_rank(); const auto triBox = elementBox[iTri]; int32 getFull2 = 0; auto bExtent = boxExtent(triBox); int32 numCellBox = bExtent.x()*bExtent.y()*bExtent.z(); Kokkos::parallel_reduce( Kokkos::TeamThreadRange( teamMember, numCellBox ), [&] ( const int32 linIndex, int32 &innerUpdate ) { CellType cell; indexToCell(linIndex, triBox, cell); int32 n = cellIter.start(cell.x(),cell.y(),cell.z()); while( n>-1) { // id is wall id the pair is (particle id, wall id) if( pairs.insert(static_cast(n), iTri) < 0 ) innerUpdate++; n = cellIter.getNext(n); } }, getFull2 ); if ( teamMember.team_rank() == 0 ) valueToUpdate += getFull2; }, getFull ); return getFull; } INLINE_FUNCTION_HD void operator()(TagFindCellRange2, int32 i) const { auto v = vertices_[i]; auto p1 = points_[v.x()]; auto p2 = points_[v.y()]; auto p3 = points_[v.z()]; realx3 minP, maxP; this->extendBox(p1, p2, p3, cellExtent_, minP, maxP); elementBox_[i] = iBoxType(this->pointIndex(minP), this->pointIndex(maxP)); } }; } #endif