From 8ea4e933aaeed1e3bd6bb323dd7c3c1b34175998 Mon Sep 17 00:00:00 2001 From: Kristian Bendiksen Date: Wed, 4 May 2022 09:13:56 +0200 Subject: [PATCH] Grid Calculator: compute results for all time steps --- .../ProjectDataModel/RimGridCalculation.cpp | 132 +++++++++++------- .../RimGridCalculationVariable.cpp | 61 ++------ .../RimGridCalculationVariable.h | 3 +- 3 files changed, 92 insertions(+), 104 deletions(-) diff --git a/ApplicationLibCode/ProjectDataModel/RimGridCalculation.cpp b/ApplicationLibCode/ProjectDataModel/RimGridCalculation.cpp index 4ebda949fd..ea31f92dd6 100644 --- a/ApplicationLibCode/ProjectDataModel/RimGridCalculation.cpp +++ b/ApplicationLibCode/ProjectDataModel/RimGridCalculation.cpp @@ -59,21 +59,27 @@ bool RimGridCalculation::calculate() auto porosityModel = RiaDefines::PorosityModelType::MATRIX_MODEL; - RimEclipseCase* eclipseCase = nullptr; - std::vector> values; + RimEclipseCase* eclipseCase = findEclipseCaseFromVariables(); + if ( !eclipseCase ) + { + RiaLogging::errorInMessageBox( nullptr, + "Expression Parser", + QString( "No case found for calculation : %1" ).arg( leftHandSideVariableName ) ); + return false; + } + + const size_t timeStepCount = eclipseCase->results( porosityModel )->maxTimeStepCount(); + + std::vector>> values; for ( size_t i = 0; i < m_variables.size(); i++ ) { RimGridCalculationVariable* v = dynamic_cast( m_variables[i] ); - // Use the first defined eclipse case from for output - if ( !eclipseCase ) eclipseCase = v->eclipseCase(); - if ( !v->eclipseCase() ) { RiaLogging::errorInMessageBox( nullptr, "Expression Parser", QString( "No case defined for variable : %1" ).arg( v->name() ) ); - return false; } @@ -82,11 +88,11 @@ bool RimGridCalculation::calculate() RiaLogging::errorInMessageBox( nullptr, "Expression Parser", QString( "No result variable defined for variable : %1" ).arg( v->name() ) ); - return false; } - RigEclipseResultAddress resAddr( v->resultCategoryType(), v->resultVariable() ); + auto resultCategoryType = v->resultCategoryType(); + RigEclipseResultAddress resAddr( resultCategoryType, v->resultVariable() ); if ( !eclipseCase->results( porosityModel )->ensureKnownResultLoaded( resAddr ) ) { RiaLogging::errorInMessageBox( nullptr, @@ -95,59 +101,79 @@ bool RimGridCalculation::calculate() return false; } + int timeStep = v->timeStep(); + std::vector> inputValues = eclipseCase->results( porosityModel )->cellScalarResults( resAddr ); - - values.push_back( inputValues[0] ); - } - - ExpressionParser parser; - for ( size_t i = 0; i < m_variables.size(); i++ ) - { - RimGridCalculationVariable* v = dynamic_cast( m_variables[i] ); - - parser.assignVector( v->name(), values[i] ); - } - - std::vector resultValues; - resultValues.resize( values[0].size() ); - parser.assignVector( leftHandSideVariableName, resultValues ); - - QString errorText; - bool evaluatedOk = parser.expandIfStatementsAndEvaluate( m_expression, &errorText ); - - if ( evaluatedOk ) - { - m_timesteps.v().clear(); - m_calculatedValues.v().clear(); - - RigEclipseResultAddress resAddr( RiaDefines::ResultCatType::GENERATED, leftHandSideVariableName ); - - if ( !eclipseCase->results( porosityModel )->ensureKnownResultLoaded( resAddr ) ) + if ( resultCategoryType == RiaDefines::ResultCatType::STATIC_NATIVE ) { - eclipseCase->results( porosityModel )->createResultEntry( resAddr, true ); + // Use static data for all time steps + inputValues.resize( timeStepCount ); + for ( size_t tsId = 1; tsId < timeStepCount; tsId++ ) + { + inputValues[tsId] = inputValues[0]; + } + } + else if ( timeStep != RimGridCalculationVariable::allTimeStepsValue() ) + { + // Use data from a specific time step for this variable for all result time steps + for ( size_t tsId = 0; tsId < timeStepCount; tsId++ ) + { + if ( static_cast( tsId ) != timeStep ) + { + inputValues[tsId] = inputValues[timeStep]; + } + } } - eclipseCase->results( porosityModel )->clearScalarResult( resAddr ); - - std::vector>* scalarResultFrames = - eclipseCase->results( porosityModel )->modifiableCellScalarResultTimesteps( resAddr ); - size_t timeStepCount = eclipseCase->results( porosityModel )->maxTimeStepCount(); - scalarResultFrames->resize( timeStepCount ); - - size_t tsId = 0; - scalarResultFrames->at( tsId ) = resultValues; - - m_isDirty = false; + values.push_back( inputValues ); } - else + + RigEclipseResultAddress resAddr( RiaDefines::ResultCatType::GENERATED, leftHandSideVariableName ); + + if ( !eclipseCase->results( porosityModel )->ensureKnownResultLoaded( resAddr ) ) { - QString s = "The following error message was received from the parser library : \n\n"; - s += errorText; - - RiaLogging::errorInMessageBox( nullptr, "Expression Parser", s ); + eclipseCase->results( porosityModel )->createResultEntry( resAddr, true ); } - return evaluatedOk; + eclipseCase->results( porosityModel )->clearScalarResult( resAddr ); + + std::vector>* scalarResultFrames = + eclipseCase->results( porosityModel )->modifiableCellScalarResultTimesteps( resAddr ); + scalarResultFrames->resize( timeStepCount ); + + for ( size_t tsId = 0; tsId < timeStepCount; tsId++ ) + { + ExpressionParser parser; + for ( size_t i = 0; i < m_variables.size(); i++ ) + { + RimGridCalculationVariable* v = dynamic_cast( m_variables[i] ); + parser.assignVector( v->name(), values[i][tsId] ); + } + + std::vector resultValues; + resultValues.resize( values[0][tsId].size() ); + parser.assignVector( leftHandSideVariableName, resultValues ); + + QString errorText; + bool evaluatedOk = parser.expandIfStatementsAndEvaluate( m_expression, &errorText ); + + if ( evaluatedOk ) + { + scalarResultFrames->at( tsId ) = resultValues; + + m_isDirty = false; + } + else + { + QString s = "The following error message was received from the parser library : \n\n"; + s += errorText; + + RiaLogging::errorInMessageBox( nullptr, "Expression Parser", s ); + return false; + } + } + + return true; } //-------------------------------------------------------------------------------------------------- diff --git a/ApplicationLibCode/ProjectDataModel/RimGridCalculationVariable.cpp b/ApplicationLibCode/ProjectDataModel/RimGridCalculationVariable.cpp index 149ecf9c87..6a85dfa2e9 100644 --- a/ApplicationLibCode/ProjectDataModel/RimGridCalculationVariable.cpp +++ b/ApplicationLibCode/ProjectDataModel/RimGridCalculationVariable.cpp @@ -40,56 +40,7 @@ RimGridCalculationVariable::RimGridCalculationVariable() CAF_PDM_InitFieldNoDefault( &m_resultType, "ResultType", "Type" ); CAF_PDM_InitField( &m_resultVariable, "ResultVariable", RiaResultNames::undefinedResultName(), "Variable" ); CAF_PDM_InitFieldNoDefault( &m_eclipseCase, "EclipseGridCase", "Grid Case" ); - CAF_PDM_InitField( &m_timeStep, "TimeStep", 0, "Time Step" ); -} - -//-------------------------------------------------------------------------------------------------- -/// -//-------------------------------------------------------------------------------------------------- -void RimGridCalculationVariable::fieldChangedByUi( const caf::PdmFieldHandle* changedField, - const QVariant& oldValue, - const QVariant& newValue ) -{ - // if ( changedField == &m_button ) - // { - // bool updateContainingEditor = false; - - // { - // RiuGridVectorSelectionDialog dlg( nullptr ); - // dlg.hideEnsembles(); - - // readDataFromApplicationStore( &dlg ); - - // if ( dlg.exec() == QDialog::Accepted ) - // { - // std::vector curveSelection = dlg.curveSelection(); - // if ( curveSelection.size() > 0 ) - // { - // m_case = curveSelection[0].summaryCase(); - // m_summaryAddress->setAddress( curveSelection[0].summaryAddress() ); - - // writeDataToApplicationStore(); - - // updateContainingEditor = true; - // } - // } - // } - - // if ( updateContainingEditor ) - // { - // RimGridCalculation* rimCalculation = nullptr; - // this->firstAncestorOrThisOfTypeAsserted( rimCalculation ); - - // // RimCalculation is pointed to by RicGridCurveCalculator in a PtrField - // // Update editors connected to RicGridCurveCalculator - // std::vector referringObjects; - // rimCalculation->objectsWithReferringPtrFields( referringObjects ); - // for ( auto o : referringObjects ) - // { - // o->uiCapability()->updateConnectedEditors(); - // } - // } - // } + CAF_PDM_InitField( &m_timeStep, "TimeStep", allTimeStepsValue(), "Time Step" ); } //-------------------------------------------------------------------------------------------------- @@ -159,6 +110,8 @@ QList } else if ( fieldNeedingOptions == &m_timeStep ) { + options.push_back( caf::PdmOptionItemInfo( "All timesteps", allTimeStepsValue() ) ); + RimTools::timeStepsForCase( m_eclipseCase(), &options ); } @@ -218,3 +171,11 @@ int RimGridCalculationVariable::timeStep() const { return m_timeStep; } + +//-------------------------------------------------------------------------------------------------- +/// +//-------------------------------------------------------------------------------------------------- +int RimGridCalculationVariable::allTimeStepsValue() +{ + return -1; +} diff --git a/ApplicationLibCode/ProjectDataModel/RimGridCalculationVariable.h b/ApplicationLibCode/ProjectDataModel/RimGridCalculationVariable.h index b9396d048f..7f388cafda 100644 --- a/ApplicationLibCode/ProjectDataModel/RimGridCalculationVariable.h +++ b/ApplicationLibCode/ProjectDataModel/RimGridCalculationVariable.h @@ -49,8 +49,9 @@ public: QString resultVariable() const; int timeStep() const; + static int allTimeStepsValue(); + private: - void fieldChangedByUi( const caf::PdmFieldHandle* changedField, const QVariant& oldValue, const QVariant& newValue ) override; void defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering ) override; QList calculateValueOptions( const caf::PdmFieldHandle* fieldNeedingOptions,