From bd31317000629cb08ed2dbb4a67bc09ad4bc84d7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jacob=20St=C3=B8ren?= <jacob.storen@ceetronSolutions.com>
Date: Thu, 12 Jan 2017 14:16:39 +0100
Subject: [PATCH] #1099 Flow results only available when the case has fluxes

---
 .../ProjectDataModel/RimEclipseResultCase.cpp | 11 ++++---
 .../RimEclipseResultDefinition.cpp            | 33 ++++++++++++++++++-
 .../RigCaseCellResultsData.cpp                | 15 +++++++++
 .../RigCaseCellResultsData.h                  |  1 +
 .../ReservoirDataModel/RigFlowDiagResults.cpp |  2 ++
 5 files changed, 57 insertions(+), 5 deletions(-)

diff --git a/ApplicationCode/ProjectDataModel/RimEclipseResultCase.cpp b/ApplicationCode/ProjectDataModel/RimEclipseResultCase.cpp
index 8b8942e219..1ef17af0e4 100644
--- a/ApplicationCode/ProjectDataModel/RimEclipseResultCase.cpp
+++ b/ApplicationCode/ProjectDataModel/RimEclipseResultCase.cpp
@@ -60,9 +60,6 @@ RimEclipseResultCase::RimEclipseResultCase()
 
     CAF_PDM_InitFieldNoDefault (&m_flowDiagSolutions, "FlowDiagSolutions", "Flow Diagnostics Solutions", "", "", "");
 
-    // TODO: Create a solution by default only when flux data is available
-    m_flowDiagSolutions.push_back( new RimFlowDiagSolution());
-
     // Obsolete, unused field
     CAF_PDM_InitField(&caseDirectory, "CaseFolder", QString(), "Directory", "", "" ,"");
     caseDirectory.xmlCapability()->setIOWritable(false); 
@@ -73,7 +70,7 @@ RimEclipseResultCase::RimEclipseResultCase()
     flipYAxis.xmlCapability()->setIOWritable(true);
     //flipYAxis.uiCapability()->setUiHidden(true);
 
-    m_flowDagSolverInterface = new RigFlowDiagSolverInterface(this);
+
 
     m_activeCellInfoIsReadFromFile = false;
     m_gridAndWellDataIsReadFromFile = false;
@@ -135,6 +132,12 @@ bool RimEclipseResultCase::openEclipseGridFile()
     m_gridAndWellDataIsReadFromFile = true;
     m_activeCellInfoIsReadFromFile = true;
 
+    if (reservoirData()->results(RifReaderInterface::MATRIX_RESULTS)->hasFlowDiagUsableFluxes())
+    {
+        m_flowDiagSolutions.push_back( new RimFlowDiagSolution());
+        m_flowDagSolverInterface = new RigFlowDiagSolverInterface(this);
+    }
+    
     return true;
  }
 
diff --git a/ApplicationCode/ProjectDataModel/RimEclipseResultDefinition.cpp b/ApplicationCode/ProjectDataModel/RimEclipseResultDefinition.cpp
index 21b02c220a..6b68021cb6 100644
--- a/ApplicationCode/ProjectDataModel/RimEclipseResultDefinition.cpp
+++ b/ApplicationCode/ProjectDataModel/RimEclipseResultDefinition.cpp
@@ -318,11 +318,42 @@ QList<caf::PdmOptionItemInfo> RimEclipseResultDefinition::calculateValueOptions(
 {
     QList<caf::PdmOptionItemInfo> options;
 
+    if ( fieldNeedingOptions == &m_resultTypeUiField )
+    {
+
+        bool hasFlowDiagFluxes = false;
+        RimEclipseResultCase* eclResCase = dynamic_cast<RimEclipseResultCase*>(m_eclipseCase.p());
+        if ( eclResCase && eclResCase->reservoirData() )
+        {
+            hasFlowDiagFluxes = eclResCase->reservoirData()->results(RifReaderInterface::MATRIX_RESULTS)->hasFlowDiagUsableFluxes();
+        }
+
+        // Do not include flow diag results if not available
+
+        if ( !hasFlowDiagFluxes )
+        {
+            using ResCatEnum = caf::AppEnum< RimDefines::ResultCatType >;
+            for ( int i = 0; i < ResCatEnum::size(); ++i )
+            {
+                RimDefines::ResultCatType resType = ResCatEnum::fromIndex(i);
+                if ( resType != RimDefines::FLOW_DIAGNOSTICS )
+                {
+                    QString uiString = ResCatEnum::uiTextFromIndex(i);
+                    options.push_back(caf::PdmOptionItemInfo(uiString, resType));
+                }
+            }
+        }
+        else
+        {
+            // Do nothing, and thereby use the defaults of the AppEnum field
+        }
+    }
+
     if ( m_resultTypeUiField() != RimDefines::FLOW_DIAGNOSTICS )
     {
         if ( fieldNeedingOptions == &m_resultVariableUiField )
         {
-            options = calcOptionsForVariableUiFieldStandard();
+                options = calcOptionsForVariableUiFieldStandard();
         }
     }
     else
diff --git a/ApplicationCode/ReservoirDataModel/RigCaseCellResultsData.cpp b/ApplicationCode/ReservoirDataModel/RigCaseCellResultsData.cpp
index afe7069ea2..76000c04dd 100644
--- a/ApplicationCode/ReservoirDataModel/RigCaseCellResultsData.cpp
+++ b/ApplicationCode/ReservoirDataModel/RigCaseCellResultsData.cpp
@@ -373,6 +373,21 @@ bool RigCaseCellResultsData::isUsingGlobalActiveIndex(size_t scalarResultIndex)
     return true;
 }
 
+//--------------------------------------------------------------------------------------------------
+/// 
+//--------------------------------------------------------------------------------------------------
+bool RigCaseCellResultsData::hasFlowDiagUsableFluxes() const
+{
+    QStringList dynResVarNames = resultNames(RimDefines::DYNAMIC_NATIVE);
+
+    bool hasFlowFluxes = true;
+    hasFlowFluxes = hasFlowFluxes && dynResVarNames.contains("FLRWATI+");
+    hasFlowFluxes = hasFlowFluxes && dynResVarNames.contains("FLROILI+");
+    hasFlowFluxes = hasFlowFluxes && dynResVarNames.contains("FLRGASI+");
+
+    return hasFlowFluxes;
+}
+
 //--------------------------------------------------------------------------------------------------
 /// 
 //--------------------------------------------------------------------------------------------------
diff --git a/ApplicationCode/ReservoirDataModel/RigCaseCellResultsData.h b/ApplicationCode/ReservoirDataModel/RigCaseCellResultsData.h
index 9705489cb5..840a837a9d 100644
--- a/ApplicationCode/ReservoirDataModel/RigCaseCellResultsData.h
+++ b/ApplicationCode/ReservoirDataModel/RigCaseCellResultsData.h
@@ -69,6 +69,7 @@ public:
     size_t                                             maxTimeStepCount(size_t* scalarResultIndex = NULL) const; 
     QStringList                                        resultNames(RimDefines::ResultCatType type) const;
     bool                                               isUsingGlobalActiveIndex(size_t scalarResultIndex) const;
+    bool                                               hasFlowDiagUsableFluxes() const;
 
     QDateTime                                          timeStepDate(size_t scalarResultIndex, size_t timeStepIndex) const;
     std::vector<QDateTime>                             timeStepDates(size_t scalarResultIndex) const;
diff --git a/ApplicationCode/ReservoirDataModel/RigFlowDiagResults.cpp b/ApplicationCode/ReservoirDataModel/RigFlowDiagResults.cpp
index e7c52118bb..d7c501c513 100644
--- a/ApplicationCode/ReservoirDataModel/RigFlowDiagResults.cpp
+++ b/ApplicationCode/ReservoirDataModel/RigFlowDiagResults.cpp
@@ -84,6 +84,8 @@ const std::vector<double>* RigFlowDiagResults::findOrCalculateResult(const RigFl
 
     // We need to access the native data from the opm solver
 
+    if (!solverInterface()) return nullptr;
+
     if (!m_hasAtemptedNativeResults[frameIndex])
     {