Files
ResInsight/ApplicationLibCode/Commands/RicCreateTemporaryLgrFeature.cpp
T

274 lines
11 KiB
C++
Raw Normal View History

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2017 Statoil ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RicCreateTemporaryLgrFeature.h"
#include "RiaCellDividingTools.h"
#include "RiaGuiApplication.h"
#include "RiaLogging.h"
#include "RiuMessageDialog.h"
#include "ExportCommands/RicExportLgrFeature.h"
2018-10-24 13:13:45 +02:00
#include "ExportCommands/RicExportLgrUi.h"
#include "RicDeleteTemporaryLgrsFeature.h"
#include "RigActiveCellInfo.h"
#include "RigCaseCellResultsData.h"
2018-10-29 10:52:59 +01:00
#include "RigCell.h"
#include "RigEclipseCaseData.h"
#include "RigMainGrid.h"
2025-09-24 09:46:00 +02:00
#include "RigReservoirGridTools.h"
#include "RimEclipseCase.h"
#include "RimEclipseView.h"
#include "RimGridCollection.h"
2018-10-24 13:13:45 +02:00
#include "RimMainPlotCollection.h"
#include "RimWellLogPlotCollection.h"
#include "RimWellPath.h"
#include "RimWellPathCompletions.h"
#include <QAction>
#include <QDir>
#include <cafSelectionManagerTools.h>
#include <cafUtils.h>
2018-10-24 13:13:45 +02:00
#include <cafVecIjk.h>
#include <array>
#include <set>
CAF_CMD_SOURCE_INIT( RicCreateTemporaryLgrFeature, "RicCreateTemporaryLgrFeature" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2023-02-26 10:48:40 +01:00
void RicCreateTemporaryLgrFeature::createLgrsForWellPaths( std::vector<RimWellPath*> wellPaths,
RimEclipseCase* eclipseCase,
size_t timeStep,
2025-11-12 19:48:07 +01:00
const cvf::Vec3st& refinement,
2023-02-26 10:48:40 +01:00
Lgr::SplitType splitType,
const std::set<RigCompletionData::CompletionType>& completionTypes,
2023-02-26 10:48:40 +01:00
QStringList* wellsIntersectingOtherLgrs )
{
2025-09-14 11:22:04 +02:00
auto lgrs =
RicExportLgrFeature::buildLgrsForWellPaths( wellPaths, eclipseCase, timeStep, refinement, splitType, completionTypes, wellsIntersectingOtherLgrs );
for ( const auto& lgr : lgrs )
{
createLgr( lgr, eclipseCase->eclipseCaseData() );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
2023-06-26 14:28:46 +02:00
bool RicCreateTemporaryLgrFeature::isCommandEnabled() const
{
std::vector<RimWellPathCompletions*> completions = caf::selectedObjectsByTypeStrict<RimWellPathCompletions*>();
std::vector<RimWellPath*> wellPaths = caf::selectedObjectsByTypeStrict<RimWellPath*>();
return !completions.empty() || !wellPaths.empty();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicCreateTemporaryLgrFeature::onActionTriggered( bool isChecked )
{
std::vector<RimWellPath*> wellPaths = RicExportLgrFeature::selectedWellPaths();
if ( wellPaths.empty() ) return;
2018-10-26 13:25:36 +02:00
QString dialogTitle = "Create Temporary LGR";
RimEclipseCase* defaultEclipseCase = nullptr;
2018-10-24 13:13:45 +02:00
int defaultTimeStep = 0;
auto activeView = dynamic_cast<RimEclipseView*>( RiaApplication::instance()->activeGridView() );
if ( activeView )
{
defaultEclipseCase = activeView->eclipseCase();
2018-10-24 13:13:45 +02:00
defaultTimeStep = activeView->currentTimeStep();
}
auto dialogData = RicExportLgrFeature::openDialog( dialogTitle, defaultEclipseCase, defaultTimeStep, true );
if ( dialogData )
{
auto eclipseCase = dialogData->caseToApply();
2025-09-14 11:22:04 +02:00
auto refinement = dialogData->refinement();
size_t timeStep = dialogData->timeStep();
auto splitType = dialogData->splitType();
const auto completionTypes = dialogData->completionTypes();
RicDeleteTemporaryLgrsFeature::deleteAllTemporaryLgrs( eclipseCase );
QStringList wellsIntersectingOtherLgrs;
2025-09-14 11:22:04 +02:00
createLgrsForWellPaths( wellPaths, eclipseCase, timeStep, refinement, splitType, completionTypes, &wellsIntersectingOtherLgrs );
2025-09-24 09:46:00 +02:00
RigReservoirGridTools::refreshEclipseCaseDataAndViews( eclipseCase );
if ( !wellsIntersectingOtherLgrs.empty() )
{
RiuMessageDialog::showError( nullptr,
"LGR cells intersected",
"At least one completion intersects with an LGR. No LGR(s) for those cells "
"are produced" );
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicCreateTemporaryLgrFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setText( "Create Temporary LGR" );
actionToSetup->setIcon( QIcon( ":/TempLGR16x16.png" ) );
}
//--------------------------------------------------------------------------------------------------
/// Todo: Guarding, caching LGR corner nodes calculations
//--------------------------------------------------------------------------------------------------
RigGridBase* RicCreateTemporaryLgrFeature::createLgrForGrid( const LgrInfo& lgrInfo, RigMainGrid* mainGrid )
{
2018-10-24 13:13:45 +02:00
int lgrId = lgrInfo.id;
2024-10-23 20:43:08 +02:00
size_t totalCellCount = mainGrid->totalCellCount();
2025-09-14 11:22:04 +02:00
size_t lgrCellCount = lgrInfo.lgrCellCount();
// Create local grid and set properties
RigLocalGrid* localGrid = new RigLocalGrid( mainGrid );
localGrid->setAsTempGrid( true );
localGrid->setAssociatedWellPathName( lgrInfo.associatedWellPathName.toStdString() );
localGrid->setGridId( lgrId );
localGrid->setIndexToStartOfCells( totalCellCount );
localGrid->setGridName( lgrInfo.name.toStdString() );
2025-11-12 19:48:07 +01:00
localGrid->setCellCounts( lgrInfo.lgrCellCounts() );
mainGrid->addLocalGrid( localGrid );
2024-10-23 20:43:08 +02:00
size_t cellStartIndex = mainGrid->totalCellCount();
size_t nodeStartIndex = mainGrid->nodes().size();
// Resize global cell and node arrays
{
RigCell defaultCell;
defaultCell.setHostGrid( localGrid );
2024-10-23 20:43:08 +02:00
mainGrid->reservoirCells().resize( cellStartIndex + lgrCellCount, defaultCell );
mainGrid->nodes().resize( nodeStartIndex + lgrCellCount * 8, cvf::Vec3d( 0, 0, 0 ) );
}
2025-09-14 11:22:04 +02:00
auto refinement = lgrInfo.refinement;
size_t gridLocalCellIndex = 0;
// Loop through all new LGR cells
2025-09-14 11:22:04 +02:00
for ( size_t lgrK = 0; lgrK < lgrInfo.lgrCellCounts().k(); lgrK++ )
{
2025-09-14 11:22:04 +02:00
size_t mainK = lgrInfo.mainGridStartCell.k() + lgrK / refinement.k();
2025-09-14 11:22:04 +02:00
for ( size_t lgrJ = 0; lgrJ < lgrInfo.lgrCellCounts().j(); lgrJ++ )
{
2025-09-14 11:22:04 +02:00
size_t mainJ = lgrInfo.mainGridStartCell.j() + lgrJ / refinement.j();
2025-09-14 11:22:04 +02:00
for ( size_t lgrI = 0; lgrI < lgrInfo.lgrCellCounts().i(); lgrI++, gridLocalCellIndex++ )
{
2025-09-14 11:22:04 +02:00
size_t mainI = lgrInfo.mainGridStartCell.i() + lgrI / refinement.i();
size_t mainCellIndex = mainGrid->cellIndexFromIJK( mainI, mainJ, mainK );
2024-10-23 20:43:08 +02:00
auto& mainGridCell = mainGrid->cell( mainCellIndex );
mainGridCell.setSubGrid( localGrid );
2024-10-23 20:43:08 +02:00
RigCell& cell = mainGrid->cell( cellStartIndex + gridLocalCellIndex );
cell.setGridLocalCellIndex( gridLocalCellIndex );
cell.setParentCellIndex( mainCellIndex );
// Corner coordinates
{
size_t cIdx;
std::array<cvf::Vec3d, 8> vertices = mainGrid->cellCornerVertices( mainCellIndex );
2025-09-14 11:22:04 +02:00
std::vector<cvf::Vec3d> lgrCoords =
RiaCellDividingTools::createHexCornerCoords( vertices, refinement.i(), refinement.j(), refinement.k() );
2025-09-14 11:22:04 +02:00
size_t subI = lgrI % refinement.i();
size_t subJ = lgrJ % refinement.j();
size_t subK = lgrK % refinement.k();
2025-09-14 11:22:04 +02:00
size_t subIndex = subI + subJ * refinement.i() + subK * refinement.i() * refinement.j();
for ( cIdx = 0; cIdx < 8; ++cIdx )
{
auto& node = mainGrid->nodes()[nodeStartIndex + gridLocalCellIndex * 8 + cIdx];
node.set( lgrCoords[subIndex * 8 + cIdx] );
cell.cornerIndices()[cIdx] = nodeStartIndex + gridLocalCellIndex * 8 + cIdx;
}
}
}
}
}
localGrid->setParentGrid( mainGrid );
2025-09-24 09:46:00 +02:00
return localGrid;
}
//--------------------------------------------------------------------------------------------------
2018-10-24 13:13:45 +02:00
///
//--------------------------------------------------------------------------------------------------
2025-09-24 09:46:00 +02:00
RigGridBase* RicCreateTemporaryLgrFeature::createLgr( const LgrInfo& lgrInfo, RigEclipseCaseData* caseData )
{
2025-09-24 09:46:00 +02:00
auto mainGrid = caseData->mainGrid();
2025-09-24 09:46:00 +02:00
auto gridCount = mainGrid->gridCount();
auto mainGridCellCount = mainGrid->totalCellCount();
auto localGrid = createLgrForGrid( lgrInfo, mainGrid );
2025-09-24 09:46:00 +02:00
if ( auto activeInfo = caseData->activeCellInfo( RiaDefines::PorosityModelType::MATRIX_MODEL ) )
{
2025-09-24 09:46:00 +02:00
auto currentReservoirCellCount = activeInfo->reservoirCellCount();
2025-09-24 09:46:00 +02:00
activeInfo->setGridCount( gridCount + 1 );
activeInfo->setReservoirCellCount( currentReservoirCellCount + localGrid->cellCount() );
2025-09-24 09:46:00 +02:00
size_t activeCellCount = 0;
2025-09-24 09:46:00 +02:00
for ( size_t k = 0; k < localGrid->cellCountK(); k++ )
{
2025-09-24 09:46:00 +02:00
for ( size_t j = 0; j < localGrid->cellCountJ(); j++ )
{
for ( size_t i = 0; i < localGrid->cellCountI(); i++ )
{
size_t cellIndex = localGrid->cellIndexFromIJK( i, j, k );
auto gridCell = localGrid->cell( cellIndex );
if ( gridCell.parentCellIndex() != cvf::UNDEFINED_SIZE_T )
{
auto resultIndex = activeInfo->cellResultIndex( ReservoirCellIndex( gridCell.parentCellIndex() ) );
if ( resultIndex.value() != cvf::UNDEFINED_SIZE_T )
2025-09-24 09:46:00 +02:00
{
activeInfo->setCellResultIndex( ReservoirCellIndex( cellIndex + mainGridCellCount ), resultIndex );
2025-09-24 09:46:00 +02:00
activeCellCount++;
}
}
}
}
}
2025-09-24 09:46:00 +02:00
activeInfo->setGridActiveCellCounts( gridCount, activeCellCount );
activeInfo->computeDerivedData();
}
2025-09-24 09:46:00 +02:00
return localGrid;
}