mirror of
https://github.com/OPM/ResInsight.git
synced 2026-10-26 15:10:22 -05:00
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.
393 lines
16 KiB
C++
393 lines
16 KiB
C++
/////////////////////////////////////////////////////////////////////////////////
|
|
//
|
|
// Copyright (C) 2018- 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 "RiaMemoryCleanup.h"
|
|
#include "RiaGuiApplication.h"
|
|
#include "RiaStdStringTools.h"
|
|
|
|
#include "RigCaseCellResultsData.h"
|
|
#include "RigEclipseResultInfo.h"
|
|
#include "RigFemPartResultsCollection.h"
|
|
#include "RigFemResultAddress.h"
|
|
#include "RigGeoMechCaseData.h"
|
|
|
|
#include "Rim3dView.h"
|
|
#include "RimEclipseCase.h"
|
|
#include "RimEclipseResultDefinition.h"
|
|
#include "RimGeoMechCase.h"
|
|
#include "RimGeoMechResultDefinition.h"
|
|
#include "RimProject.h"
|
|
|
|
#include "cafPdmUiTreeSelectionEditor.h"
|
|
|
|
#include <QDialog>
|
|
#include <QTextEdit>
|
|
#include <QVBoxLayout>
|
|
|
|
//==================================================================================================
|
|
///
|
|
///
|
|
//==================================================================================================
|
|
|
|
CAF_PDM_SOURCE_INIT( RiaMemoryCleanup, "RiaMemoryCleanup" );
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
RiaMemoryCleanup::RiaMemoryCleanup()
|
|
{
|
|
CAF_PDM_InitFieldNoDefault( &m_case, "DataCase", "Case" );
|
|
m_case = nullptr;
|
|
|
|
CAF_PDM_InitFieldNoDefault( &m_resultsToDelete, "ResultsToDelete", "Results In Memory" );
|
|
m_resultsToDelete.uiCapability()->setUiLabelPosition( caf::PdmUiItemInfo::LabelPosition::TOP );
|
|
m_resultsToDelete.uiCapability()->setUiEditorTypeName( caf::PdmUiTreeSelectionEditor::uiEditorTypeName() );
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
void RiaMemoryCleanup::setPropertiesFromView( Rim3dView* view )
|
|
{
|
|
if ( !view ) return;
|
|
|
|
m_case = view->ownerCase();
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
void RiaMemoryCleanup::clearSelectedResultsFromMemory()
|
|
{
|
|
RimEclipseCase* eclipseCase = dynamic_cast<RimEclipseCase*>( m_case() );
|
|
RimGeoMechCase* geoMechCase = dynamic_cast<RimGeoMechCase*>( m_case() );
|
|
if ( eclipseCase )
|
|
{
|
|
RigCaseCellResultsData* caseData = eclipseCase->results( RiaDefines::PorosityModelType::MATRIX_MODEL );
|
|
if ( caseData )
|
|
{
|
|
std::vector<RigEclipseResultAddress> resultsToDelete = selectedEclipseResults();
|
|
for ( const RigEclipseResultAddress& resultAddr : resultsToDelete )
|
|
{
|
|
caseData->clearScalarResult( resultAddr );
|
|
}
|
|
}
|
|
}
|
|
else if ( geoMechCase )
|
|
{
|
|
RigGeoMechCaseData* data = geoMechCase->geoMechData();
|
|
if ( data )
|
|
{
|
|
RigFemPartResultsCollection* resultsCollection = data->femPartResults();
|
|
if ( resultsCollection )
|
|
{
|
|
std::vector<RigFemResultAddress> resultsToDelete = selectedGeoMechResults();
|
|
for ( RigFemResultAddress result : resultsToDelete )
|
|
{
|
|
resultsCollection->deleteResult( result );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
m_resultsToDelete.v().clear();
|
|
m_eclipseResultAddresses.clear();
|
|
m_geomResultAddresses.clear();
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
class TextDialog : public QDialog
|
|
{
|
|
public:
|
|
TextDialog( const QString& text, QWidget* parent = nullptr )
|
|
: QDialog( parent )
|
|
{
|
|
auto textWidget = new QTextEdit( "", this );
|
|
textWidget->setPlainText( text );
|
|
auto layout = new QVBoxLayout( this );
|
|
layout->addWidget( textWidget );
|
|
setLayout( layout );
|
|
}
|
|
};
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
void RiaMemoryCleanup::showMemoryReport()
|
|
{
|
|
auto [summary, details] = createMemoryReport();
|
|
|
|
QString allText = summary + "\n\n" + details;
|
|
|
|
auto dialog = new TextDialog( allText, RiaGuiApplication::widgetToUseAsParent() );
|
|
dialog->setWindowTitle( "Memory Report" );
|
|
dialog->setMinimumSize( 800, 600 );
|
|
dialog->show();
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
std::vector<RigFemResultAddress> RiaMemoryCleanup::selectedGeoMechResults() const
|
|
{
|
|
std::vector<RigFemResultAddress> results;
|
|
if ( dynamic_cast<const RimGeoMechCase*>( m_case() ) )
|
|
{
|
|
for ( size_t index : m_resultsToDelete() )
|
|
{
|
|
CAF_ASSERT( index < m_geomResultAddresses.size() );
|
|
results.push_back( m_geomResultAddresses[index] );
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
std::vector<RigEclipseResultAddress> RiaMemoryCleanup::selectedEclipseResults() const
|
|
{
|
|
std::vector<RigEclipseResultAddress> results;
|
|
if ( dynamic_cast<const RimEclipseCase*>( m_case() ) )
|
|
{
|
|
for ( size_t index : m_resultsToDelete() )
|
|
{
|
|
CAF_ASSERT( index < m_eclipseResultAddresses.size() );
|
|
results.push_back( m_eclipseResultAddresses[index] );
|
|
}
|
|
}
|
|
return results;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
std::set<RigFemResultAddress> RiaMemoryCleanup::findGeoMechCaseResultsInUse() const
|
|
{
|
|
std::set<RigFemResultAddress> resultsInUse;
|
|
|
|
auto geoMechCase = dynamic_cast<RimGeoMechCase*>( m_case() );
|
|
if ( geoMechCase )
|
|
{
|
|
std::vector<RimFemResultObserver*> geoMechResults = geoMechCase->descendantsIncludingThisOfType<RimFemResultObserver>();
|
|
for ( RimFemResultObserver* resultDef : geoMechResults )
|
|
{
|
|
auto pdmObj = dynamic_cast<caf::PdmObject*>( resultDef );
|
|
if ( pdmObj )
|
|
{
|
|
auto field = dynamic_cast<caf::PdmField<bool>*>( pdmObj->objectToggleField() );
|
|
if ( !field || ( *field )() )
|
|
{
|
|
std::vector<RigFemResultAddress> required = resultDef->observedResults();
|
|
resultsInUse.insert( required.begin(), required.end() );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return resultsInUse;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
std::set<RigEclipseResultAddress> RiaMemoryCleanup::findEclipseResultsInUse() const
|
|
{
|
|
std::set<RigEclipseResultAddress> resultsInUse;
|
|
RimEclipseCase* eclipseCase = dynamic_cast<RimEclipseCase*>( m_case() );
|
|
if ( eclipseCase )
|
|
{
|
|
auto eclipseResultDefs = eclipseCase->descendantsIncludingThisOfType<RimEclipseResultDefinition>();
|
|
for ( RimEclipseResultDefinition* resultDef : eclipseResultDefs )
|
|
{
|
|
RigEclipseResultAddress resultAddr( resultDef->resultType(), resultDef->resultVariable() );
|
|
resultsInUse.insert( resultAddr );
|
|
}
|
|
}
|
|
|
|
return resultsInUse;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
void RiaMemoryCleanup::fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue )
|
|
{
|
|
if ( changedField == &m_case )
|
|
{
|
|
m_resultsToDelete.uiCapability()->updateConnectedEditors();
|
|
}
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
QList<caf::PdmOptionItemInfo> RiaMemoryCleanup::calculateValueOptions( const caf::PdmFieldHandle* fieldNeedingOptions )
|
|
{
|
|
QList<caf::PdmOptionItemInfo> options;
|
|
if ( fieldNeedingOptions == &m_case )
|
|
{
|
|
RimProject* proj = RimProject::current();
|
|
if ( proj )
|
|
{
|
|
std::vector<RimEclipseCase*> eclipseCases = proj->eclipseCases();
|
|
for ( RimEclipseCase* c : eclipseCases )
|
|
{
|
|
options.push_back( caf::PdmOptionItemInfo( c->caseUserDescription(), c, false, c->uiIconProvider() ) );
|
|
}
|
|
|
|
std::vector<RimGeoMechCase*> geoMechCases = proj->geoMechCases();
|
|
for ( RimGeoMechCase* c : geoMechCases )
|
|
{
|
|
options.push_back( caf::PdmOptionItemInfo( c->caseUserDescription(), c, false, c->uiIconProvider() ) );
|
|
}
|
|
}
|
|
}
|
|
else if ( fieldNeedingOptions == &m_resultsToDelete )
|
|
{
|
|
RimEclipseCase* eclipseCase = dynamic_cast<RimEclipseCase*>( m_case() );
|
|
RimGeoMechCase* geoMechCase = dynamic_cast<RimGeoMechCase*>( m_case() );
|
|
if ( eclipseCase )
|
|
{
|
|
std::set<RigEclipseResultAddress> resultsInUse = findEclipseResultsInUse();
|
|
RigCaseCellResultsData* caseData = eclipseCase->results( RiaDefines::PorosityModelType::MATRIX_MODEL );
|
|
if ( caseData )
|
|
{
|
|
m_eclipseResultAddresses = caseData->existingResults();
|
|
|
|
for ( size_t i = 0; i < m_eclipseResultAddresses.size(); ++i )
|
|
{
|
|
const RigEclipseResultAddress& resultAddr = m_eclipseResultAddresses[i];
|
|
if ( caseData->isResultLoaded( resultAddr ) )
|
|
{
|
|
bool inUse = resultsInUse.count( resultAddr );
|
|
|
|
const RigEclipseResultInfo* resInfo = caseData->resultInfo( resultAddr );
|
|
|
|
QString posText = caf::AppEnum<RiaDefines::ResultCatType>::uiText( resInfo->resultType() );
|
|
QString resultsText = QString( "%1, %2" ).arg( posText ).arg( resInfo->resultName() );
|
|
if ( inUse )
|
|
{
|
|
resultsText += QString( " [used in view]" );
|
|
}
|
|
options.push_back( caf::PdmOptionItemInfo( resultsText, (qulonglong)i, inUse ) );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if ( geoMechCase )
|
|
{
|
|
std::set<RigFemResultAddress> resultsInUse = findGeoMechCaseResultsInUse();
|
|
RigGeoMechCaseData* caseData = geoMechCase->geoMechData();
|
|
if ( caseData )
|
|
{
|
|
RigFemPartResultsCollection* results = caseData->femPartResults();
|
|
m_geomResultAddresses = results->loadedResults();
|
|
|
|
for ( size_t i = 0; i < m_geomResultAddresses.size(); ++i )
|
|
{
|
|
const RigFemResultAddress& result = m_geomResultAddresses[i];
|
|
bool inUse = resultsInUse.count( result );
|
|
QString posText = caf::AppEnum<RigFemResultPosEnum>::uiTextFromIndex( result.resultPosType );
|
|
QString resultsText = QString( "%1, %2" ).arg( posText ).arg( QString::fromStdString( result.fieldName ) );
|
|
if ( !result.componentName.empty() )
|
|
{
|
|
resultsText += QString( ", %1" ).arg( QString::fromStdString( result.componentName ) );
|
|
}
|
|
if ( inUse )
|
|
{
|
|
resultsText += QString( " [used in view]" );
|
|
}
|
|
options.push_back( caf::PdmOptionItemInfo( resultsText, (qulonglong)i, inUse ) );
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return options;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
void RiaMemoryCleanup::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering )
|
|
{
|
|
uiOrdering.add( &m_case );
|
|
uiOrdering.add( &m_resultsToDelete );
|
|
uiOrdering.addNewButton( "Clear Checked Data From Memory",
|
|
[this]()
|
|
{
|
|
clearSelectedResultsFromMemory();
|
|
m_resultsToDelete.uiCapability()->updateConnectedEditors();
|
|
} );
|
|
uiOrdering.addNewButton( "Show Memory Report", []() { RiaMemoryCleanup::showMemoryReport(); } );
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------
|
|
///
|
|
//--------------------------------------------------------------------------------------------------
|
|
std::pair<QString, QString> RiaMemoryCleanup::createMemoryReport()
|
|
{
|
|
QString details;
|
|
|
|
auto allCases = RimProject::current()->allGridCases();
|
|
|
|
size_t totalMemory = 0;
|
|
for ( auto gridCase : allCases )
|
|
{
|
|
if ( auto eclipseCase = dynamic_cast<RimEclipseCase*>( gridCase ) )
|
|
{
|
|
RigCaseCellResultsData* caseData = eclipseCase->results( RiaDefines::PorosityModelType::MATRIX_MODEL );
|
|
if ( caseData )
|
|
{
|
|
size_t totalMemoryForCase = 0;
|
|
QString caseReport;
|
|
|
|
auto memoryUse = caseData->resultValueCount();
|
|
for ( const auto& [name, valueCount] : memoryUse )
|
|
{
|
|
if ( valueCount > 0 )
|
|
{
|
|
size_t memory = valueCount * sizeof( double );
|
|
totalMemoryForCase += memory;
|
|
|
|
auto formattedValueCount = RiaStdStringTools::formatThousandGrouping( valueCount );
|
|
|
|
caseReport += QString( " %1 MB\tValue count %2, %3\n" )
|
|
.arg( memory / 1024.0 / 1024.0, 0, 'f', 2 )
|
|
.arg( QString::fromStdString( formattedValueCount ) )
|
|
.arg( QString::fromStdString( name ) );
|
|
}
|
|
}
|
|
|
|
totalMemory += totalMemoryForCase;
|
|
|
|
if ( totalMemoryForCase > 0 )
|
|
{
|
|
details +=
|
|
QString( "%1 - %2 MB\n" ).arg( eclipseCase->caseUserDescription() ).arg( totalMemoryForCase / 1024.0 / 1024.0, 0, 'f', 2 );
|
|
|
|
details += caseReport;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
QString summary = QString( "Total memory used: %1 MB\n\n" ).arg( totalMemory / 1024.0 / 1024.0, 0, 'f', 2 );
|
|
return std::make_pair( summary, details );
|
|
}
|