Skip to content

Commit

Permalink
Refactor: move fault reactivation data access to fix ownership problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
kriben committed Dec 18, 2023
1 parent 4d12d0f commit 097cee4
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,12 @@ void RicExportInpFileFeature::onActionTriggered( bool isChecked )
auto faultReactivationModel = caf::SelectionManager::instance()->selectedItemOfType<RimFaultReactivationModel>();
if ( faultReactivationModel )
{
const QString frmTitle( "Fault Reactivation Modeling" );
if ( !faultReactivationModel->extractAndExportModelData() )
{
QMessageBox::critical( nullptr, frmTitle, "Unable to get necessary data from the input case." );
return;
}

QString exportFile = faultReactivationModel->baseDir() + "/faultreactivation.inp";
auto [isOk, errorMessage] = RifFaultReactivationModelExporter::exportToFile( exportFile.toStdString(), *faultReactivationModel );
if ( !isOk )
{
QString outErrorText =
const QString frmTitle( "Fault Reactivation Modeling" );
QString outErrorText =
QString( "Failed to export INP model to file %1.\n\n%2" ).arg( exportFile ).arg( QString::fromStdString( errorMessage ) );
QMessageBox::critical( nullptr, frmTitle, outErrorText );
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ void RicRunFaultReactModelingFeature::onActionTriggered( bool isChecked )
return;
}

if ( !model->extractAndExportModelData() )
{
QMessageBox::critical( nullptr, frmTitle, "Unable to get necessary data from the input case." );
return;
}

QString exportFile = model->inputFilename();
auto [result, errText] = RifFaultReactivationModelExporter::exportToFile( exportFile.toStdString(), *model );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,6 @@ void RicShowFaultReactModelFeature::onActionTriggered( bool isChecked )
const QString frmTitle( "Fault Reactivation Modeling" );
const QString exportFile = model->inputFilename();

if ( !model->extractAndExportModelData() )
{
QMessageBox::critical( nullptr, frmTitle, "Unable to get necessary data from the input case." );
return;
}

auto [result, errText] = RifFaultReactivationModelExporter::exportToFile( exportFile.toStdString(), *model );
if ( !result )
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@
#include "RiaWellLogUnitTools.h"

#include "RifInpExportTools.h"
#include "RifJsonEncodeDecode.h"

#include "RimFaultReactivationDataAccess.h"
#include "RimFaultReactivationEnums.h"
#include "RimFaultReactivationModel.h"
#include "RimFaultReactivationTools.h"

#include <filesystem>
#include <fstream>
Expand All @@ -42,6 +45,9 @@ std::pair<bool, std::string> RifFaultReactivationModelExporter::exportToStream(
const std::string& exportDirectory,
const RimFaultReactivationModel& rimModel )
{
auto dataAccess = extractAndExportModelData( rimModel );
if ( !dataAccess ) return { false, "Unable to get necessary data from the input case." };

std::string applicationNameAndVersion = std::string( RI_APPLICATION_NAME ) + " " + std::string( STRPRODUCTVER );

using PartBorderSurface = RimFaultReactivation::BorderSurface;
Expand Down Expand Up @@ -90,8 +96,6 @@ std::pair<bool, std::string> RifFaultReactivationModelExporter::exportToStream(
double waterDensity = rimModel.waterDensity();
double seaWaterLoad = RiaWellLogUnitTools<double>::gravityAcceleration() * seaBedDepth * waterDensity;

auto dataAccess = rimModel.dataAccess();

auto model = rimModel.model();
CAF_ASSERT( !model.isNull() );

Expand Down Expand Up @@ -808,4 +812,45 @@ std::string RifFaultReactivationModelExporter::createFileName( const std::string
std::string RifFaultReactivationModelExporter::createFilePath( const std::string& dir, const std::string& fileName )
{
return dir + "/" + fileName;
};
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RifFaultReactivationModelExporter::exportModelSettings( const RimFaultReactivationModel& rimModel )
{
auto model = rimModel.model();

if ( model.isNull() ) return false;
if ( !model->isValid() ) return false;

QMap<QString, QVariant> settings;

auto [topPosition, bottomPosition] = model->faultTopBottom();
auto faultNormal = model->faultNormal();

// make sure we move horizontally, and along the 2D model
faultNormal.z() = 0.0;
faultNormal.normalize();
faultNormal = faultNormal ^ cvf::Vec3d::Z_AXIS;

RimFaultReactivationTools::addSettingsToMap( settings, faultNormal, topPosition, bottomPosition );
return ResInsightInternalJson::JsonWriter::encodeFile( rimModel.settingsFilename(), settings );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::shared_ptr<RimFaultReactivationDataAccess>
RifFaultReactivationModelExporter::extractAndExportModelData( const RimFaultReactivationModel& rimModel )
{
if ( !exportModelSettings( rimModel ) ) return nullptr;

auto eCase = rimModel.eclipseCase();
if ( eCase == nullptr ) return nullptr;

// extract data for each timestep
auto dataAccess = std::make_shared<RimFaultReactivationDataAccess>( eCase, rimModel.geoMechCase(), rimModel.selectedTimeStepIndexes() );
dataAccess->extractModelData( *rimModel.model() );
return dataAccess;
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,7 @@ class RifFaultReactivationModelExporter
static std::string createFileName( const std::string& title, const std::string& stepName );

static std::string createFilePath( const std::string& dir, const std::string& fileName );

static bool exportModelSettings( const RimFaultReactivationModel& model );
static std::shared_ptr<RimFaultReactivationDataAccess> extractAndExportModelData( const RimFaultReactivationModel& model );
};
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
#include "RimEclipseView.h"
#include "RimFaultInView.h"
#include "RimFaultInViewCollection.h"
#include "RimFaultReactivationDataAccess.h"
#include "RimFaultReactivationEnums.h"
#include "RimFaultReactivationTools.h"
#include "RimGeoMechCase.h"
Expand Down Expand Up @@ -542,22 +541,20 @@ void RimFaultReactivationModel::defineEditorAttribute( const caf::PdmFieldHandle
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimEclipseCase* RimFaultReactivationModel::eclipseCase()
RimEclipseCase* RimFaultReactivationModel::eclipseCase() const
{
auto eCase = firstAncestorOrThisOfType<RimEclipseCase>();

if ( eCase == nullptr )
{
eCase = dynamic_cast<RimEclipseCase*>( RiaApplication::instance()->activeGridView()->ownerCase() );
}

return eCase;
if ( eCase != nullptr )
return eCase;
else
return dynamic_cast<RimEclipseCase*>( RiaApplication::instance()->activeGridView()->ownerCase() );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RimGeoMechCase* RimFaultReactivationModel::geoMechCase()
RimGeoMechCase* RimFaultReactivationModel::geoMechCase() const
{
return m_geomechCase();
}
Expand Down Expand Up @@ -602,6 +599,22 @@ std::vector<QDateTime> RimFaultReactivationModel::selectedTimeSteps() const
return dates;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::vector<size_t> RimFaultReactivationModel::selectedTimeStepIndexes() const
{
std::vector<size_t> selectedTimeStepIndexes;
for ( auto& timeStep : selectedTimeSteps() )
{
auto idx = std::find( m_availableTimeSteps.begin(), m_availableTimeSteps.end(), timeStep );
if ( idx == m_availableTimeSteps.end() ) return {};

selectedTimeStepIndexes.push_back( idx - m_availableTimeSteps.begin() );
}
return selectedTimeStepIndexes;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -660,60 +673,6 @@ QString RimFaultReactivationModel::baseFilename() const
return tmp;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimFaultReactivationModel::exportModelSettings()
{
if ( m_2Dmodel.isNull() ) return false;
if ( !m_2Dmodel->isValid() ) return false;

QMap<QString, QVariant> settings;

auto [topPosition, bottomPosition] = m_2Dmodel->faultTopBottom();
auto faultNormal = m_2Dmodel->faultNormal();

// make sure we move horizontally, and along the 2D model
faultNormal.z() = 0.0;
faultNormal.normalize();
faultNormal = faultNormal ^ cvf::Vec3d::Z_AXIS;

RimFaultReactivationTools::addSettingsToMap( settings, faultNormal, topPosition, bottomPosition );

QDir directory( baseDir() );
return ResInsightInternalJson::JsonWriter::encodeFile( settingsFilename(), settings );
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RimFaultReactivationModel::extractAndExportModelData()
{
if ( m_dataAccess ) m_dataAccess->clearModelData();

if ( !exportModelSettings() ) return false;

auto eCase = eclipseCase();
if ( eCase == nullptr ) return false;

// get the selected time step indexes
std::vector<size_t> selectedTimeStepIndexes;
for ( auto& timeStep : selectedTimeSteps() )
{
auto idx = std::find( m_availableTimeSteps.begin(), m_availableTimeSteps.end(), timeStep );
if ( idx == m_availableTimeSteps.end() ) return false;

selectedTimeStepIndexes.push_back( idx - m_availableTimeSteps.begin() );
}

// extract data for each timestep
m_dataAccess = std::make_shared<RimFaultReactivationDataAccess>( eCase, geoMechCase(), selectedTimeStepIndexes );
m_dataAccess->extractModelData( *model() );
m_dataAccess.reset();

return true;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -741,14 +700,6 @@ std::array<double, 3> RimFaultReactivationModel::materialParameters( ElementSets
return retVal;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::shared_ptr<RimFaultReactivationDataAccess> RimFaultReactivationModel::dataAccess() const
{
return m_dataAccess;
}

//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#pragma once

#include "RimCheckableNamedObject.h"
#include "RimFaultReactivationDataAccess.h"
#include "RimFaultReactivationEnums.h"
#include "RimPolylinePickerInterface.h"
#include "RimPolylinesDataInterface.h"
Expand Down Expand Up @@ -53,7 +52,6 @@ class RimTimeStepFilter;
class RivFaultReactivationModelPartMgr;
class RigBasicPlane;
class RigFaultReactivationModel;
class RimFaultReactivationDataAccess;

namespace cvf
{
Expand Down Expand Up @@ -102,12 +100,11 @@ class RimFaultReactivationModel : public RimCheckableNamedObject, public RimPoly
cvf::ref<RigFaultReactivationModel> model() const;
bool showModel() const;

bool extractAndExportModelData();

QString baseDir() const;
void setBaseDir( QString path );

std::vector<QDateTime> selectedTimeSteps() const;
std::vector<size_t> selectedTimeStepIndexes() const;

std::array<double, 3> materialParameters( ElementSets elementSet ) const;

Expand All @@ -119,8 +116,6 @@ class RimFaultReactivationModel : public RimCheckableNamedObject, public RimPoly

void updateTimeSteps();

std::shared_ptr<RimFaultReactivationDataAccess> dataAccess() const;

bool useGridVoidRatio() const;
bool useGridPorePressure() const;
bool useGridTemperature() const;
Expand All @@ -131,6 +126,9 @@ class RimFaultReactivationModel : public RimCheckableNamedObject, public RimPoly
double seaBedDepth() const;
double waterDensity() const;

RimEclipseCase* eclipseCase() const;
RimGeoMechCase* geoMechCase() const;

protected:
caf::PdmFieldHandle* userDescriptionField() override;
QList<caf::PdmOptionItemInfo> calculateValueOptions( const caf::PdmFieldHandle* fieldNeedingOptions ) override;
Expand All @@ -139,13 +137,8 @@ class RimFaultReactivationModel : public RimCheckableNamedObject, public RimPoly
void fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue ) override;
void defineEditorAttribute( const caf::PdmFieldHandle* field, QString uiConfigName, caf::PdmUiEditorAttribute* attribute ) override;

RimEclipseCase* eclipseCase();
RimGeoMechCase* geoMechCase();

QString baseFilename() const;

bool exportModelSettings();

private:
std::shared_ptr<RicPolylineTargetsPickEventHandler> m_pickTargetsEventHandler;

Expand Down Expand Up @@ -197,6 +190,4 @@ class RimFaultReactivationModel : public RimCheckableNamedObject, public RimPoly
caf::PdmChildArrayField<RimParameterGroup*> m_materialParameters;

std::vector<QDateTime> m_availableTimeSteps;

std::shared_ptr<RimFaultReactivationDataAccess> m_dataAccess;
};

0 comments on commit 097cee4

Please sign in to comment.