#9588 Accumulate N cells for AABB tree

This commit is contained in:
Magne Sjaastad
2022-12-19 15:24:53 +01:00
committed by GitHub
parent 42f3316619
commit 38bfa9ef1f
11 changed files with 211 additions and 60 deletions

View File

@@ -267,8 +267,18 @@ void RigMainGrid::computeCachedData( std::string* aabbTreeInfo )
initAllSubCellsMainGridCellIndex();
m_cellSearchTree = nullptr;
buildCellSearchTree();
if ( aabbTreeInfo ) *aabbTreeInfo = m_cellSearchTree->info();
const double maxNumberOfLeafNodes = 4000000;
const double factor = std::ceil( cellCount() / maxNumberOfLeafNodes );
const size_t cellsPerBoundingBox = std::max( size_t( 1 ), static_cast<size_t>( factor ) );
buildCellSearchTreeOptimized( cellsPerBoundingBox );
if ( aabbTreeInfo )
{
*aabbTreeInfo += "Cells per bounding box : " + std::to_string( cellsPerBoundingBox ) + "\n";
*aabbTreeInfo += m_cellSearchTree->info();
}
}
//--------------------------------------------------------------------------------------------------
@@ -779,14 +789,10 @@ void RigMainGrid::buildCellSearchTree()
const std::array<size_t, 8>& cellIndices = m_cells[cIdx].cornerIndices();
cvf::BoundingBox cellBB;
cellBB.add( m_nodes[cellIndices[0]] );
cellBB.add( m_nodes[cellIndices[1]] );
cellBB.add( m_nodes[cellIndices[2]] );
cellBB.add( m_nodes[cellIndices[3]] );
cellBB.add( m_nodes[cellIndices[4]] );
cellBB.add( m_nodes[cellIndices[5]] );
cellBB.add( m_nodes[cellIndices[6]] );
cellBB.add( m_nodes[cellIndices[7]] );
for ( size_t i : cellIndices )
{
cellBB.add( m_nodes[i] );
}
if ( cellBB.isValid() )
{
@@ -812,6 +818,97 @@ void RigMainGrid::buildCellSearchTree()
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RigMainGrid::buildCellSearchTreeOptimized( size_t cellsPerBoundingBox )
{
int threadCount = RiaOpenMPTools::availableThreadCount();
std::vector<std::vector<std::vector<int>>> threadCellIndicesForBoundingBoxes( threadCount );
std::vector<std::vector<cvf::BoundingBox>> threadCellBoundingBoxes( threadCount );
#pragma omp parallel
{
int myThread = RiaOpenMPTools::currentThreadIndex();
#pragma omp for
for ( int i = 0; i < static_cast<int>( cellCountI() ); i++ )
{
for ( size_t j = 0; j < cellCountJ(); j++ )
{
size_t k = 0;
while ( k < cellCountK() )
{
size_t kCount = 0;
std::vector<int> aggregatedCellIndices;
cvf::BoundingBox accumulatedBB;
while ( ( kCount < cellsPerBoundingBox ) && ( k + kCount < cellCountK() ) )
{
size_t cellIdx = cellIndexFromIJK( i, j, k + kCount );
const auto& rigCell = cell( cellIdx );
if ( !rigCell.isInvalid() )
{
aggregatedCellIndices.push_back( static_cast<int>( cellIdx ) );
// Add all cells in sub grid contained in this main grid cell
if ( auto subGrid = rigCell.subGrid() )
{
for ( size_t localIdx = 0; localIdx < subGrid->cellCount(); localIdx++ )
{
const auto& localCell = subGrid->cell( localIdx );
if ( localCell.mainGridCellIndex() == cellIdx )
{
aggregatedCellIndices.push_back(
static_cast<int>( subGrid->reservoirCellIndex( localIdx ) ) );
}
}
}
const std::array<size_t, 8>& cellIndices = rigCell.cornerIndices();
cvf::BoundingBox cellBB;
for ( size_t i : cellIndices )
{
cellBB.add( m_nodes[i] );
}
if ( cellBB.isValid() ) accumulatedBB.add( cellBB );
}
kCount++;
}
k += kCount;
kCount = 0;
threadCellIndicesForBoundingBoxes[myThread].emplace_back( aggregatedCellIndices );
threadCellBoundingBoxes[myThread].emplace_back( accumulatedBB );
}
}
}
}
std::vector<std::vector<int>> cellIndicesForBoundingBoxes;
std::vector<cvf::BoundingBox> cellBoundingBoxes;
for ( auto i = 0; i < threadCount; i++ )
{
cellIndicesForBoundingBoxes.insert( cellIndicesForBoundingBoxes.end(),
threadCellIndicesForBoundingBoxes[i].begin(),
threadCellIndicesForBoundingBoxes[i].end() );
cellBoundingBoxes.insert( cellBoundingBoxes.end(),
threadCellBoundingBoxes[i].begin(),
threadCellBoundingBoxes[i].end() );
}
m_cellSearchTree = new cvf::BoundingBoxTree;
m_cellSearchTree->buildTreeFromBoundingBoxesOptimized( cellBoundingBoxes, cellIndicesForBoundingBoxes );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@@ -116,6 +116,7 @@ public:
private:
void initAllSubCellsMainGridCellIndex();
void buildCellSearchTree();
void buildCellSearchTreeOptimized( size_t cellsPerBoundingBox );
bool hasFaultWithName( const QString& name ) const;
static std::array<double, 6> defaultMapAxes();

View File

@@ -753,14 +753,11 @@ private:
const std::array<size_t, 8>& cellIndices = wellCell.cornerIndices();
cvf::BoundingBox& cellBB = m_cellBoundingBoxes[cIdx];
cellBB.add( nodes[cellIndices[0]] );
cellBB.add( nodes[cellIndices[1]] );
cellBB.add( nodes[cellIndices[2]] );
cellBB.add( nodes[cellIndices[3]] );
cellBB.add( nodes[cellIndices[4]] );
cellBB.add( nodes[cellIndices[5]] );
cellBB.add( nodes[cellIndices[6]] );
cellBB.add( nodes[cellIndices[7]] );
for ( size_t i : cellIndices )
{
cellBB.add( nodes[i] );
}
}
m_cellSearchTree.buildTreeFromBoundingBoxes( m_cellBoundingBoxes, nullptr );