Files
ResInsight/ApplicationLibCode/Commands/ExportCommands/RicExportEclipseSectorModelFeature.cpp
T
Magne Sjaastad 56e98ba1c4 #14476 ApplicationLibCode: Replace CVF_ASSERT with CAF_ASSERT
Migrate all assert macros in ApplicationLibCode to CAF_ASSERT and remove every
use of cvfAssert.h.

CVF_ASSERT is replaced one to one. CVF_TIGHT_ASSERT is also replaced by
CAF_ASSERT, which is semantically exact: CVF_ENABLE_TIGHT_ASSERTS is 1 only
under _DEBUG, and that is what CAF_ASSERT now does. The two CVF_FAIL_MSG sites
become CAF_ASSERT( false && "message" ), preserving the message with the idiom
already used elsewhere in the code base.

Counts before and after: CVF_ASSERT 1044 to 0, CVF_TIGHT_ASSERT 66 to 0,
CVF_FAIL_MSG 2 to 0, cvfAssert.h references 154 to 0.

Include handling: files that included cvfAssert.h directly now include
cafAssert.h instead, includes left dead by the migration are removed, and files
that were relying on cvfAssert.h transitively get an explicit cafAssert.h. Files
that reach cafAssert.h through another caf header are left unchanged; a missing
include here is a compile error, not a silently disabled assert.

ResultStatisticsCache links only LibCore and therefore had no path to
cafAssert.h. Add the cafPdmCore directory as a private include path rather than
linking the library, since cafAssert.h is header only.

Note that this stops these asserts from firing in Release and RelWithDebInfo,
where CVF_ASSERT was previously active.
2026-08-07 15:05:57 +02:00

328 lines
14 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2019- Equinor 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 "RicExportEclipseSectorModelFeature.h"
#include "RiaApplication.h"
#include "RiaLogging.h"
#include "RiaModelExportDefines.h"
#include "RiaQStringFormatter.h"
#include "RiaResultNames.h"
#include "RicExportEclipseSectorModelUi.h"
#include "RicExportFeatureImpl.h"
#include "RifEclipseInputFileTools.h"
#include "RifOpmDeckTools.h"
#include "RifOpmFlowDeckFile.h"
#include "RifReaderEclipseOutput.h"
#include "ProjectDataModel/Jobs/RimKeywordBcprop.h"
#include "ProjectDataModel/Jobs/RimKeywordFactory.h"
#include "Rim3dView.h"
#include "RimDialogData.h"
#include "RimEclipseCase.h"
#include "RimEclipseCellColors.h"
#include "RimEclipseView.h"
#include "RimFaultInView.h"
#include "RimFaultInViewCollection.h"
#include "RimProject.h"
#include "Tools/RimEclipseViewTools.h"
#include "RigActiveCellInfo.h"
#include "RigBoundingBoxIjk.h"
#include "RigEclipseCaseData.h"
#include "RigEclipseCaseDataTools.h"
#include "RigEclipseResultTools.h"
#include "RigGridExportAdapter.h"
#include "RigMainGrid.h"
#include "RigResdataGridConverter.h"
#include "RigSimulationInputSettings.h"
#include "RigSimulationInputTool.h"
#include "Well/RigSimWellData.h"
#include "Well/RigWellResultFrame.h"
#include "Well/RigWellResultPoint.h"
#include "Riu3DMainWindowTools.h"
#include "RiuPropertyViewTabWidget.h"
#include "RiuViewer.h"
#include "cafCmdFeatureManager.h"
#include "cafProgressInfo.h"
#include "cafSelectionManager.h"
#include "opm/input/eclipse/Deck/DeckItem.hpp"
#include "opm/input/eclipse/Deck/DeckKeyword.hpp"
#include "opm/input/eclipse/Deck/DeckRecord.hpp"
#include "opm/input/eclipse/Parser/ParserKeyword.hpp"
#include "opm/input/eclipse/Parser/ParserKeywords/C.hpp"
#include "opm/input/eclipse/Parser/ParserKeywords/E.hpp"
#include "opm/input/eclipse/Parser/ParserKeywords/O.hpp"
#include "opm/input/eclipse/Parser/ParserKeywords/S.hpp"
#include "opm/input/eclipse/Parser/ParserKeywords/W.hpp"
#include "opm/input/eclipse/Utility/Typetools.hpp"
#include <QAction>
#include <QDir>
#include <QFileInfo>
#include <set>
#include <string>
CAF_CMD_SOURCE_INIT( RicExportEclipseSectorModelFeature, "RicExportEclipseInputGridFeature" );
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExportEclipseSectorModelFeature::openDialogAndExecuteCommand( RimEclipseView* view )
{
if ( !view ) return;
RigEclipseCaseData* caseData = view->eclipseCase()->eclipseCaseData();
cvf::UByteArray cellVisibility;
view->calculateCurrentTotalCellVisibility( &cellVisibility, view->currentTimeStep() );
const auto& [min, max] = RimEclipseViewTools::getVisibleCellRange( view, cellVisibility );
RicExportEclipseSectorModelUi* exportSettings = RimProject::current()->dialogData()->exportEclipseSectorModelUi();
exportSettings->setCaseData( caseData, view, min, max );
exportSettings->applyBoundaryDefaults();
exportSettings->removeInvalidKeywords();
RiuPropertyViewTabWidget propertyDialog( Riu3DMainWindowTools::mainWindowWidget(),
exportSettings,
"Export Eclipse Sector Model",
exportSettings->tabNames() );
RicExportFeatureImpl::configureForExport( propertyDialog.dialogButtonBox() );
if ( propertyDialog.exec() == QDialog::Accepted )
{
executeCommand( view, *exportSettings, "ExportInputGrid" );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExportEclipseSectorModelFeature::executeCommand( RimEclipseView* view,
const RicExportEclipseSectorModelUi& exportSettings,
const QString& logPrefix )
{
int resultProgressPercentage = exportSettings.exportParameters() ? std::min( (int)exportSettings.selectedKeywords().size(), 20 ) : 0;
int faultsProgressPercentage = exportSettings.exportFaults() ? 10 : 0;
int gridProgressPercentage = 100 - resultProgressPercentage - faultsProgressPercentage;
caf::ProgressInfo progress( gridProgressPercentage + resultProgressPercentage + faultsProgressPercentage, "Export Eclipse Sector Model" );
CAF_ASSERT( exportSettings.refinement().x() > 0u && exportSettings.refinement().y() > 0u && exportSettings.refinement().z() > 0u );
if ( exportSettings.exportGrid() )
{
auto task = progress.task( "Export Grid", gridProgressPercentage );
exportGrid( view, exportSettings );
}
if ( exportSettings.exportParameters() != RicExportEclipseSectorModelUi::EXPORT_NO_RESULTS )
{
auto task = progress.task( "Export Properties", resultProgressPercentage );
exportParameters( view, exportSettings );
}
if ( exportSettings.exportFaults() != RicExportEclipseSectorModelUi::EXPORT_NO_RESULTS )
{
auto task = progress.task( "Export Faults", faultsProgressPercentage );
exportFaults( view, exportSettings );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExportEclipseSectorModelFeature::exportGrid( RimEclipseView* view, const RicExportEclipseSectorModelUi& exportSettings )
{
cvf::UByteArray cellVisibility;
view->calculateCurrentTotalCellVisibility( &cellVisibility, view->currentTimeStep() );
const cvf::UByteArray* cellVisibilityForActnum = exportSettings.makeInvisibleCellsInactive() ? &cellVisibility : nullptr;
bool worked = RifEclipseInputFileTools::exportGrid( exportSettings.exportGridFilename(),
view->eclipseCase()->eclipseCaseData(),
exportSettings.exportInLocalCoordinates(),
cellVisibilityForActnum,
exportSettings.min(),
exportSettings.max(),
exportSettings.refinement() );
if ( !worked )
{
RiaLogging::error( std::format( "Unable to write grid to '{}'", exportSettings.exportGridFilename() ) );
}
else
{
if ( view->eclipseCase()->eclipseCaseData()->gridCount() > 1u )
{
RiaLogging::warning( "Grid has LGRs but ResInsight only supports exporting the Main Grid" );
}
QFileInfo info( exportSettings.exportGridFilename() );
RiaApplication::instance()->setLastUsedDialogDirectory( "EXPORT_INPUT_GRID", info.absolutePath() );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExportEclipseSectorModelFeature::exportParameters( RimEclipseView* view, const RicExportEclipseSectorModelUi& exportSettings )
{
std::vector<QString> keywords = exportSettings.selectedKeywords;
if ( exportSettings.exportParameters == RicExportEclipseSectorModelUi::EXPORT_TO_SEPARATE_FILE_PER_RESULT )
{
QFileInfo info( exportSettings.exportGridFilename() );
QDir dirPath = info.absoluteDir();
for ( const QString& keyword : keywords )
{
QString fileName = dirPath.absoluteFilePath( keyword + ".GRDECL" );
bool worked = RifEclipseInputFileTools::exportKeywords( fileName,
view->eclipseCase()->eclipseCaseData(),
{ keyword },
exportSettings.writeEchoKeywords(),
exportSettings.min(),
exportSettings.max(),
exportSettings.refinement() );
if ( !worked )
{
RiaLogging::error( std::format( "Unable to write results to '{}'", fileName ) );
}
}
}
else
{
QString fileName = exportSettings.exportParametersFilename();
if ( exportSettings.exportParameters() == RicExportEclipseSectorModelUi::EXPORT_TO_GRID_FILE )
{
fileName = exportSettings.exportGridFilename();
}
bool worked = RifEclipseInputFileTools::exportKeywords( fileName,
view->eclipseCase()->eclipseCaseData(),
keywords,
exportSettings.writeEchoKeywords(),
exportSettings.min(),
exportSettings.max(),
exportSettings.refinement() );
if ( !worked )
{
RiaLogging::error( std::format( "Unable to write results to '{}'", fileName ) );
}
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExportEclipseSectorModelFeature::exportFaults( RimEclipseView* view, const RicExportEclipseSectorModelUi& exportSettings )
{
if ( exportSettings.exportFaults == RicExportEclipseSectorModelUi::EXPORT_TO_SEPARATE_FILE_PER_RESULT )
{
for ( const auto& faultInView : view->faultCollection()->faults() )
{
auto rigFault = faultInView->faultGeometry();
QString fileName = QString( "%1.GRDECL" ).arg( rigFault->name() );
RifEclipseInputFileTools::saveFault( fileName,
view->eclipseCase()->mainGrid(),
rigFault->faultFaces(),
rigFault->name(),
exportSettings.min(),
exportSettings.max(),
exportSettings.refinement() );
}
}
else
{
QString fileName = exportSettings.exportFaultsFilename();
QIODevice::OpenMode openFlag = QIODevice::Truncate;
if ( exportSettings.exportParameters() == RicExportEclipseSectorModelUi::EXPORT_TO_GRID_FILE )
{
openFlag = QIODevice::Append;
fileName = exportSettings.exportGridFilename();
}
QFile exportFile( fileName );
if ( !exportFile.open( QIODevice::Text | QIODevice::WriteOnly | openFlag ) )
{
RiaLogging::error( "Could not open the file : " + fileName.toStdString() );
}
QTextStream stream( &exportFile );
RifEclipseInputFileTools::saveFaults( stream,
view->eclipseCase()->mainGrid(),
exportSettings.min(),
exportSettings.max(),
exportSettings.refinement() );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RicExportEclipseSectorModelFeature::isCommandEnabled() const
{
return selectedView() != nullptr;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExportEclipseSectorModelFeature::onActionTriggered( bool isChecked )
{
RimEclipseView* view = RicExportEclipseSectorModelFeature::selectedView();
openDialogAndExecuteCommand( view );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicExportEclipseSectorModelFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setText( "Export Eclipse Sector Model" );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimEclipseView* RicExportEclipseSectorModelFeature::selectedView()
{
auto contextViewer = dynamic_cast<RiuViewer*>( caf::CmdFeatureManager::instance()->currentContextMenuTargetWidget() );
if ( contextViewer != nullptr )
{
// Command is triggered from viewer
Rim3dView* activeView = RiaApplication::instance()->activeMainOrComparisonGridView();
return dynamic_cast<RimEclipseView*>( activeView );
}
// Command triggered from project tree or file menu
auto view = caf::SelectionManager::instance()->selectedItemAncestorOfType<RimEclipseView>();
return view;
}