ResInsight/ApplicationCode/ProjectDataModel/Summary/RimDerivedSummaryCase.cpp

354 lines
12 KiB
C++
Raw Normal View History

2018-06-25 08:14:47 -05:00
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2016- Statoil ASA
//
2018-06-25 08:14:47 -05:00
// 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.
//
2018-06-25 08:14:47 -05:00
// 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>
2018-06-25 08:14:47 -05:00
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "RimDerivedSummaryCase.h"
2018-06-25 08:14:47 -05:00
#include "RiaApplication.h"
#include "RiaCurveMerger.h"
#include "RiaLogging.h"
2018-06-25 08:14:47 -05:00
#include "RifDerivedEnsembleReader.h"
#include "RimProject.h"
#include "RimSummaryPlot.h"
2018-06-25 08:14:47 -05:00
namespace caf
{
template <>
void caf::AppEnum<DerivedSummaryOperator>::setUp()
{
addItem( DerivedSummaryOperator::DERIVED_OPERATOR_SUB, "Sub", "-" );
addItem( DerivedSummaryOperator::DERIVED_OPERATOR_ADD, "Add", "+" );
setDefault( DerivedSummaryOperator::DERIVED_OPERATOR_SUB );
}
} // namespace caf
2018-06-25 08:14:47 -05:00
CAF_PDM_SOURCE_INIT( RimDerivedSummaryCase, "RimDerivedEnsembleCase" );
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
RimDerivedSummaryCase::RimDerivedSummaryCase()
: m_summaryCase1( nullptr )
, m_summaryCase2( nullptr )
2018-06-25 08:14:47 -05:00
{
CAF_PDM_InitObject( "Summary Case", ":/SummaryCase16x16.png", "", "" );
CAF_PDM_InitFieldNoDefault( &m_summaryCase1, "SummaryCase1", "SummaryCase1", "", "", "" );
CAF_PDM_InitFieldNoDefault( &m_summaryCase2, "SummaryCase2", "SummaryCase2", "", "", "" );
CAF_PDM_InitFieldNoDefault( &m_operator, "Operator", "Operator", "", "", "" );
m_inUse = false;
2018-06-25 08:14:47 -05:00
}
2018-06-25 08:58:28 -05:00
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:58:28 -05:00
//--------------------------------------------------------------------------------------------------
RimDerivedSummaryCase::~RimDerivedSummaryCase()
{
}
2018-06-25 08:58:28 -05:00
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
void RimDerivedSummaryCase::setInUse( bool inUse )
2018-06-25 08:14:47 -05:00
{
m_inUse = inUse;
if ( !m_inUse )
2018-06-25 08:14:47 -05:00
{
m_summaryCase1 = nullptr;
m_summaryCase2 = nullptr;
m_dataCache.clear();
2018-06-25 08:14:47 -05:00
}
}
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
bool RimDerivedSummaryCase::isInUse() const
2018-06-25 08:14:47 -05:00
{
return m_inUse;
}
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
void RimDerivedSummaryCase::setSummaryCases( RimSummaryCase* sumCase1, RimSummaryCase* sumCase2 )
2018-06-25 08:14:47 -05:00
{
if ( !sumCase1 || !sumCase2 ) return;
2018-06-25 08:14:47 -05:00
m_summaryCase1 = sumCase1;
m_summaryCase2 = sumCase2;
}
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
bool RimDerivedSummaryCase::needsCalculation( const RifEclipseSummaryAddress& address ) const
2018-06-25 08:14:47 -05:00
{
return m_dataCache.count( address ) == 0;
2018-06-25 08:14:47 -05:00
}
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
const std::vector<time_t>& RimDerivedSummaryCase::timeSteps( const RifEclipseSummaryAddress& address ) const
2018-06-25 08:14:47 -05:00
{
if ( m_dataCache.count( address ) == 0 )
{
static std::vector<time_t> empty;
return empty;
}
return m_dataCache.at( address ).first;
2018-06-25 08:14:47 -05:00
}
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
const std::vector<double>& RimDerivedSummaryCase::values( const RifEclipseSummaryAddress& address ) const
2018-06-25 08:14:47 -05:00
{
if ( m_dataCache.count( address ) == 0 )
{
static std::vector<double> empty;
return empty;
}
return m_dataCache.at( address ).second;
2018-06-25 08:14:47 -05:00
}
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
void RimDerivedSummaryCase::calculate( const RifEclipseSummaryAddress& address )
2018-06-25 08:14:47 -05:00
{
clearData( address );
2018-06-25 08:14:47 -05:00
RifSummaryReaderInterface* reader1 = m_summaryCase1 ? m_summaryCase1->summaryReader() : nullptr;
RifSummaryReaderInterface* reader2 = m_summaryCase2 ? m_summaryCase2->summaryReader() : nullptr;
if ( !reader1 || !reader2 ) return;
2018-06-25 08:14:47 -05:00
if ( !reader1->hasAddress( address ) || !reader2->hasAddress( address ) )
{
std::string text = address.uiText();
RiaLogging::warning(
"Derived Ensemble : At least one of the ensembles does not contain the summary address : " +
QString::fromStdString( text ) );
return;
}
RiaTimeHistoryCurveMerger merger;
std::vector<double> values1;
std::vector<double> values2;
2018-06-25 08:14:47 -05:00
reader1->values( address, &values1 );
reader2->values( address, &values2 );
2018-06-25 08:14:47 -05:00
merger.addCurveData( reader1->timeSteps( address ), values1 );
merger.addCurveData( reader2->timeSteps( address ), values2 );
2018-06-25 08:14:47 -05:00
merger.computeInterpolatedValues();
const std::vector<double>& allValues1 = merger.interpolatedYValuesForAllXValues( 0 );
const std::vector<double>& allValues2 = merger.interpolatedYValuesForAllXValues( 1 );
size_t sampleCount = merger.allXValues().size();
2018-06-25 08:14:47 -05:00
std::vector<double> calculatedValues;
calculatedValues.reserve( sampleCount );
for ( size_t i = 0; i < sampleCount; i++ )
2018-06-25 08:14:47 -05:00
{
if ( m_operator() == DerivedSummaryOperator::DERIVED_OPERATOR_SUB )
2018-06-25 08:14:47 -05:00
{
calculatedValues.push_back( allValues1[i] - allValues2[i] );
2018-06-25 08:14:47 -05:00
}
else if ( m_operator() == DerivedSummaryOperator::DERIVED_OPERATOR_ADD )
2018-06-25 08:14:47 -05:00
{
calculatedValues.push_back( allValues1[i] + allValues2[i] );
2018-06-25 08:14:47 -05:00
}
}
auto& dataItem = m_dataCache[address];
dataItem.first = merger.allXValues();
2018-06-25 08:14:47 -05:00
dataItem.second = calculatedValues;
}
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
QString RimDerivedSummaryCase::caseName() const
2018-06-25 08:14:47 -05:00
{
if ( m_summaryCase1 && m_summaryCase2 )
{
auto case1Name = m_summaryCase1->displayCaseName();
auto case2Name = m_summaryCase2->displayCaseName();
2018-06-25 08:14:47 -05:00
if ( case1Name == case2Name )
return case1Name;
else
return QString( "%1/%2" ).arg( case1Name ).arg( case2Name );
}
return m_shortName;
2018-06-25 08:14:47 -05:00
}
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
void RimDerivedSummaryCase::createSummaryReaderInterface()
2018-06-25 08:14:47 -05:00
{
RifSummaryReaderInterface* summaryCase1Reader = nullptr;
if ( m_summaryCase1 )
{
if ( !m_summaryCase1->summaryReader() )
{
m_summaryCase1->createSummaryReaderInterface();
}
summaryCase1Reader = m_summaryCase1->summaryReader();
}
m_reader.reset( new RifDerivedEnsembleReader( this, summaryCase1Reader ) );
2018-06-25 08:14:47 -05:00
}
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
RifSummaryReaderInterface* RimDerivedSummaryCase::summaryReader()
2018-06-25 08:14:47 -05:00
{
return m_reader.get();
}
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
void RimDerivedSummaryCase::updateFilePathsFromProjectPath( const QString& newProjectPath, const QString& oldProjectPath )
2018-06-25 08:14:47 -05:00
{
// NOP
}
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
void RimDerivedSummaryCase::setOperator( DerivedSummaryOperator oper )
2018-06-25 08:14:47 -05:00
{
m_operator = oper;
2018-06-25 08:14:47 -05:00
}
//--------------------------------------------------------------------------------------------------
///
2018-06-25 08:14:47 -05:00
//--------------------------------------------------------------------------------------------------
void RimDerivedSummaryCase::clearData( const RifEclipseSummaryAddress& address )
2018-06-25 08:14:47 -05:00
{
m_dataCache.erase( address );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDerivedSummaryCase::defineUiOrdering( QString uiConfigName, caf::PdmUiOrdering& uiOrdering )
{
// Base class
uiOrdering.add( &m_shortName );
// This class
uiOrdering.add( &m_summaryCase1 );
uiOrdering.add( &m_operator );
uiOrdering.add( &m_summaryCase2 );
uiOrdering.skipRemainingFields();
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
QList<caf::PdmOptionItemInfo>
RimDerivedSummaryCase::calculateValueOptions( const caf::PdmFieldHandle* fieldNeedingOptions, bool* useOptionsOnly )
{
QList<caf::PdmOptionItemInfo> options;
RimProject* proj = RiaApplication::instance()->project();
auto summaryCases = proj->allSummaryCases();
if ( fieldNeedingOptions == &m_summaryCase1 || fieldNeedingOptions == &m_summaryCase2 )
{
for ( auto c : summaryCases )
{
if ( c != this ) options.push_back( caf::PdmOptionItemInfo( c->displayCaseName(), c ) );
}
}
return options;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RimDerivedSummaryCase::fieldChangedByUi( const caf::PdmFieldHandle* changedField,
const QVariant& oldValue,
const QVariant& newValue )
{
bool reloadData = false;
if ( changedField == &m_summaryCase2 || changedField == &m_summaryCase1 )
{
if ( !m_reader )
{
createSummaryReaderInterface();
}
if ( m_reader )
{
m_reader->updateData( m_summaryCase1(), m_summaryCase2() );
}
reloadData = true;
}
else if ( changedField == &m_operator )
{
reloadData = true;
}
else
{
RimSummaryCase::fieldChangedByUi( changedField, oldValue, newValue );
}
if ( reloadData )
{
m_dataCache.clear();
std::vector<caf::PdmObjectHandle*> referringObjects;
this->objectsWithReferringPtrFields( referringObjects );
std::set<RimSummaryPlot*> plotsToUpdate;
for ( auto o : referringObjects )
{
RimSummaryPlot* sumPlot = nullptr;
o->firstAncestorOrThisOfType( sumPlot );
if ( sumPlot )
{
plotsToUpdate.insert( sumPlot );
}
}
for ( auto p : plotsToUpdate )
{
p->loadDataAndUpdate();
}
}
2018-06-25 08:14:47 -05:00
}