Skip to content

Commit

Permalink
Merge branch 'dev' into sigurdp/vizfwk-qopenglwidget
Browse files Browse the repository at this point in the history
  • Loading branch information
sigurdp committed Jan 22, 2024
2 parents 7d98322 + e2e861e commit 88a252e
Show file tree
Hide file tree
Showing 30 changed files with 449 additions and 103 deletions.
110 changes: 109 additions & 1 deletion ApplicationLibCode/Application/RiaMemoryCleanup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
/////////////////////////////////////////////////////////////////////////////////

#include "RiaMemoryCleanup.h"
#include "RiaStdStringTools.h"

#include "RigCaseCellResultsData.h"
#include "RigEclipseResultInfo.h"
Expand All @@ -34,6 +35,10 @@
#include "cafPdmUiPushButtonEditor.h"
#include "cafPdmUiTreeSelectionEditor.h"

#include <QDialog>
#include <QTextEdit>
#include <QVBoxLayout>

//==================================================================================================
///
///
Expand All @@ -55,6 +60,9 @@ RiaMemoryCleanup::RiaMemoryCleanup()

CAF_PDM_InitFieldNoDefault( &m_performDelete, "ClearSelectedData", "" );
caf::PdmUiPushButtonEditor::configureEditorForField( &m_performDelete );

CAF_PDM_InitFieldNoDefault( &m_showMemoryReport, "ShowMemoryReport", "" );
caf::PdmUiPushButtonEditor::configureEditorForField( &m_showMemoryReport );
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -107,6 +115,38 @@ void RiaMemoryCleanup::clearSelectedResultsFromMemory()
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 );
dialog->setWindowTitle( "Memory Report" );
dialog->setMinimumSize( 800, 600 );
dialog->show();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -204,6 +244,12 @@ void RiaMemoryCleanup::fieldChangedByUi( const caf::PdmFieldHandle* changedField
m_resultsToDelete.uiCapability()->updateConnectedEditors();
m_performDelete = false;
}
else if ( changedField == &m_showMemoryReport )
{
m_showMemoryReport = false;

showMemoryReport();
}
}

//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -310,10 +356,72 @@ void RiaMemoryCleanup::defineEditorAttribute( const caf::PdmFieldHandle* field,
{
if ( field == &m_performDelete )
{
caf::PdmUiPushButtonEditorAttribute* attrib = dynamic_cast<caf::PdmUiPushButtonEditorAttribute*>( attribute );
auto attrib = dynamic_cast<caf::PdmUiPushButtonEditorAttribute*>( attribute );
if ( attrib )
{
attrib->m_buttonText = "Clear Checked Data From Memory";
}
}
if ( field == &m_showMemoryReport )
{
auto attrib = dynamic_cast<caf::PdmUiPushButtonEditorAttribute*>( attribute );
if ( attrib )
{
attrib->m_buttonText = "Show Memory Report";
}
}
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
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 );
}
5 changes: 5 additions & 0 deletions ApplicationLibCode/Application/RiaMemoryCleanup.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class RiaMemoryCleanup : public caf::PdmObject
void setPropertiesFromView( Rim3dView* view );
void clearSelectedResultsFromMemory();

static void showMemoryReport();

protected:
void fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue ) override;

Expand All @@ -52,10 +54,13 @@ class RiaMemoryCleanup : public caf::PdmObject
void defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering ) override;
void defineEditorAttribute( const caf::PdmFieldHandle* field, QString uiConfigName, caf::PdmUiEditorAttribute* attribute ) override;

static std::pair<QString, QString> createMemoryReport();

private:
caf::PdmPtrField<RimCase*> m_case;
caf::PdmField<std::vector<size_t>> m_resultsToDelete;
std::vector<RigFemResultAddress> m_geomResultAddresses;
std::vector<RigEclipseResultAddress> m_eclipseResultAddresses;
caf::PdmField<bool> m_performDelete;
caf::PdmField<bool> m_showMemoryReport;
};
20 changes: 20 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaStdStringTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ bool RiaStdStringTools::startsWithAlphabetic( const std::string& s )
return isalpha( s[0] ) != 0;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::string RiaStdStringTools::formatThousandGrouping( long value )
{
class my_punct : public std::numpunct<char>
{
protected:
char do_decimal_point() const override { return '.'; }
char do_thousands_sep() const override { return ' '; }
std::string do_grouping() const override { return std::string( "\3" ); }
};

std::ostringstream os;
os.imbue( std::locale( os.getloc(), new my_punct ) );
fixed( os );
os << value;
return os.str();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down
2 changes: 2 additions & 0 deletions ApplicationLibCode/Application/Tools/RiaStdStringTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ class RiaStdStringTools
static bool containsAlphabetic( const std::string& s );
static bool startsWithAlphabetic( const std::string& s );

static std::string formatThousandGrouping( long value );

// Conversion using fastest known approach
static bool toDouble( const std::string_view& s, double& value );
static bool toInt( const std::string_view& s, int& value );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set(SOURCE_GROUP_HEADER_FILES
${CMAKE_CURRENT_LIST_DIR}/RicShowClassNamesFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicShowPlotDataCtxFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicOpenInTextEditorFeature.h
${CMAKE_CURRENT_LIST_DIR}/RicShowMemoryReportFeature.h
)

set(SOURCE_GROUP_SOURCE_FILES
Expand All @@ -44,6 +45,7 @@ set(SOURCE_GROUP_SOURCE_FILES
${CMAKE_CURRENT_LIST_DIR}/RicShowClassNamesFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicShowPlotDataCtxFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicOpenInTextEditorFeature.cpp
${CMAKE_CURRENT_LIST_DIR}/RicShowMemoryReportFeature.cpp
)

list(APPEND COMMAND_CODE_HEADER_FILES ${SOURCE_GROUP_HEADER_FILES})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 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 "RicShowMemoryReportFeature.h"

#include "RiaMemoryCleanup.h"

#include <QAction>

CAF_CMD_SOURCE_INIT( RicShowMemoryReportFeature, "RicShowMemoryReportFeature" );

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicShowMemoryReportFeature::onActionTriggered( bool isChecked )
{
RiaMemoryCleanup::showMemoryReport();
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RicShowMemoryReportFeature::setupActionLook( QAction* actionToSetup )
{
actionToSetup->setText( "Memory Report" );
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2024 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.
//
/////////////////////////////////////////////////////////////////////////////////

#pragma once

#include "cafCmdFeature.h"

//==================================================================================================
///
//==================================================================================================
class RicShowMemoryReportFeature : public caf::CmdFeature
{
CAF_CMD_HEADER_INIT;

private:
void onActionTriggered( bool isChecked ) override;
void setupActionLook( QAction* actionToSetup ) override;
};
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void RicImportSurfacesFeature::onActionTriggered( bool isChecked )
QStringList fileNames = RiuFileDialogTools::getOpenFileNames( Riu3DMainWindowTools::mainWindowWidget(),
"Import Surfaces",
defaultDir,
"Surface files (*.ptl *.ts *.dat);;All Files (*.*)" );
"Surface files (*.ptl *.ts *.dat *.xyz);;All Files (*.*)" );

if ( fileNames.isEmpty() ) return;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ std::pair<bool, std::string> RifFaultReactivationModelExporter::exportToStream(
{ RimFaultReactivation::ElementSets::IntraReservoir, "INTRA_RESERVOIR" },
{ RimFaultReactivation::ElementSets::Reservoir, "RESERVOIR" },
{ RimFaultReactivation::ElementSets::UnderBurden, "UNDERBURDEN" },
{ RimFaultReactivation::ElementSets::FaultZone, "FAULT_ZONE" },
};

bool useGridVoidRatio = rimModel.useGridVoidRatio();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "RigFaultReactivationModel.h"
#include "RigGriddedPart3d.h"

#include "RimFaultReactivationDataAccess.h"
#include "RimFaultReactivationModel.h"

#include <map>
Expand Down
36 changes: 25 additions & 11 deletions ApplicationLibCode/FileInterface/RifReaderOpmCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ bool RifReaderOpmCommon::staticResult( const QString& result, RiaDefines::Porosi
}
}
}

// Always clear data after reading to avoid memory use
m_initFile->clearData();

return true;
}
catch ( std::exception& e )
Expand Down Expand Up @@ -179,6 +183,9 @@ bool RifReaderOpmCommon::dynamicResult( const QString& result,
}
}

// Always clear data after reading to avoid memory use
m_restartFile->clearData();

return true;
}
catch ( std::exception& e )
Expand Down Expand Up @@ -441,22 +448,29 @@ std::vector<RifReaderOpmCommon::TimeDataFile> RifReaderOpmCommon::readTimeSteps(
{
namespace VI = Opm::RestartIO::Helpers::VectorItems;

for ( auto seqNumber : restartFile->listOfReportStepNumbers() )
for ( auto seqNum : restartFile->listOfReportStepNumbers() )
{
auto fileView = std::make_shared<EclIO::RestartFileView>( restartFile, seqNumber );

auto intehead = fileView->intehead();
const std::string inteheadString = "INTEHEAD";
const std::string doubheadString = "DOUBHEAD";

auto year = intehead[VI::intehead::YEAR];
auto month = intehead[VI::intehead::MONTH];
auto day = intehead[VI::intehead::DAY];
if ( restartFile->hasArray( inteheadString, seqNum ) )
{
auto intehead = restartFile->getRestartData<int>( inteheadString, seqNum );
auto year = intehead[VI::intehead::YEAR];
auto month = intehead[VI::intehead::MONTH];
auto day = intehead[VI::intehead::DAY];

auto doubhead = fileView->doubhead();
double daySinceSimStart = 0.0;

auto daySinceSimStart = doubhead[VI::doubhead::TsInit];
if ( restartFile->hasArray( doubheadString, seqNum ) )
{
auto doubhead = restartFile->getRestartData<double>( doubheadString, seqNum );
daySinceSimStart = doubhead[VI::doubhead::TsInit];
}

reportTimeData.emplace_back(
TimeDataFile{ .sequenceNumber = seqNumber, .year = year, .month = month, .day = day, .simulationTimeFromStart = daySinceSimStart } );
reportTimeData.emplace_back(
TimeDataFile{ .sequenceNumber = seqNum, .year = year, .month = month, .day = day, .simulationTimeFromStart = daySinceSimStart } );
}
}
}
catch ( std::exception& e )
Expand Down
Loading

0 comments on commit 88a252e

Please sign in to comment.