Make mean calculators more consistent and add an harmonic mean calculator.

This commit is contained in:
Gaute Lindkvist
2018-09-14 09:12:05 +02:00
parent 6f42d6ae1f
commit 9953c56c35
12 changed files with 224 additions and 36 deletions

View File

@@ -33,9 +33,10 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaSCurveCalculator.h
${CMAKE_CURRENT_LIST_DIR}/RiaArcCurveCalculator.h
${CMAKE_CURRENT_LIST_DIR}/RiaJCurveCalculator.h
${CMAKE_CURRENT_LIST_DIR}/RiaOffshoreSphericalCoords.h
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedAverageCalculator.h
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedAverageCalculator.inl
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedMeanCalculator.h
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedMeanCalculator.inl
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedGeometricMeanCalculator.h
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedHarmonicMeanCalculator.h
${CMAKE_CURRENT_LIST_DIR}/RiaOptionItemFactory.h
)
@@ -73,6 +74,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaSCurveCalculator.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaArcCurveCalculator.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaJCurveCalculator.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedGeometricMeanCalculator.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedHarmonicMeanCalculator.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaOptionItemFactory.cpp
)

View File

@@ -0,0 +1,72 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2018- Statoil 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 "RiaWeightedHarmonicMeanCalculator.h"
#include "cvfAssert.h"
#include <cmath>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaWeightedHarmonicMeanCalculator::RiaWeightedHarmonicMeanCalculator()
: m_aggregatedWeightedValue(0.0)
, m_aggregatedWeight(0.0)
{
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaWeightedHarmonicMeanCalculator::addValueAndWeight(double value, double weight)
{
CVF_ASSERT(weight > 1.0e-12 && std::abs(value) > 1.0e-12);
m_aggregatedWeightedValue += weight / value;
m_aggregatedWeight += weight;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaWeightedHarmonicMeanCalculator::weightedMean() const
{
if (validAggregatedWeight())
{
return m_aggregatedWeight / m_aggregatedWeightedValue;
}
CVF_ASSERT(false);
return 0.0;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaWeightedHarmonicMeanCalculator::aggregatedWeight() const
{
return m_aggregatedWeight;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
bool RiaWeightedHarmonicMeanCalculator::validAggregatedWeight() const
{
return m_aggregatedWeight > 1.0e-12;
}

View File

@@ -0,0 +1,38 @@
/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2018- Statoil 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
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
class RiaWeightedHarmonicMeanCalculator
{
public:
RiaWeightedHarmonicMeanCalculator();
void addValueAndWeight(double value, double weight);
double weightedMean() const;
double aggregatedWeight() const;
bool validAggregatedWeight() const;
private:
double m_aggregatedWeightedValue;
double m_aggregatedWeight;
};

View File

@@ -22,14 +22,14 @@
///
//--------------------------------------------------------------------------------------------------
template<class T>
class RiaWeightedAverageCalculator
class RiaWeightedMeanCalculator
{
public:
RiaWeightedAverageCalculator();
RiaWeightedMeanCalculator();
void addValueAndWeight(T value, double weight);
T weightedAverage() const;
T weightedMean() const;
double aggregatedWeight() const;
bool validAggregatedWeight() const;
private:
@@ -37,4 +37,4 @@ private:
double m_aggregatedWeight;
};
#include "RiaWeightedAverageCalculator.inl"
#include "RiaWeightedMeanCalculator.inl"

View File

@@ -22,7 +22,7 @@
///
//--------------------------------------------------------------------------------------------------
template<class T>
RiaWeightedAverageCalculator<T>::RiaWeightedAverageCalculator()
RiaWeightedMeanCalculator<T>::RiaWeightedMeanCalculator()
: m_aggregatedValue(T{})
, m_aggregatedWeight(0.0)
{
@@ -32,7 +32,7 @@ RiaWeightedAverageCalculator<T>::RiaWeightedAverageCalculator()
///
//--------------------------------------------------------------------------------------------------
template<class T>
void RiaWeightedAverageCalculator<T>::addValueAndWeight(T value, double weight)
void RiaWeightedMeanCalculator<T>::addValueAndWeight(T value, double weight)
{
CVF_ASSERT(weight >= 0.0);
@@ -44,7 +44,7 @@ void RiaWeightedAverageCalculator<T>::addValueAndWeight(T value, double weight)
///
//--------------------------------------------------------------------------------------------------
template<class T>
T RiaWeightedAverageCalculator<T>::weightedAverage() const
T RiaWeightedMeanCalculator<T>::weightedMean() const
{
bool validWeights = validAggregatedWeight();
CVF_TIGHT_ASSERT(validWeights);
@@ -59,7 +59,7 @@ T RiaWeightedAverageCalculator<T>::weightedAverage() const
///
//--------------------------------------------------------------------------------------------------
template<class T>
double RiaWeightedAverageCalculator<T>::aggregatedWeight() const
double RiaWeightedMeanCalculator<T>::aggregatedWeight() const
{
return m_aggregatedWeight;
}
@@ -69,7 +69,7 @@ double RiaWeightedAverageCalculator<T>::aggregatedWeight() const
///
//--------------------------------------------------------------------------------------------------
template<class T>
bool RiaWeightedAverageCalculator<T>::validAggregatedWeight() const
bool RiaWeightedMeanCalculator<T>::validAggregatedWeight() const
{
return m_aggregatedWeight > 1.0e-12;
}

View File

@@ -21,7 +21,6 @@
#include "RicWellPathFractureReportItem.h"
#include "RiaLogging.h"
#include "RiaWeightedAverageCalculator.h"
#include "RimEclipseCase.h"
#include "RimEclipseView.h"

View File

@@ -22,7 +22,7 @@
#include "RiaCompletionTypeCalculationScheduler.h"
#include "RiaFractureDefines.h"
#include "RiaLogging.h"
#include "RiaWeightedAverageCalculator.h"
#include "RiaWeightedMeanCalculator.h"
#include "RifStimPlanXmlReader.h"
@@ -474,8 +474,8 @@ FractureWidthAndConductivity
nameUnit.first, nameUnit.second, m_activeTimeStepIndex, fractureTemplateUnit());
}
RiaWeightedAverageCalculator<double> widthCalc;
RiaWeightedAverageCalculator<double> conductivityCalc;
RiaWeightedMeanCalculator<double> widthCalc;
RiaWeightedMeanCalculator<double> conductivityCalc;
RigWellPathStimplanIntersector intersector(rimWellPath->wellPathGeometry(), fractureInstance);
for (const auto& v : intersector.intersections())
@@ -496,11 +496,11 @@ FractureWidthAndConductivity
}
if (conductivityCalc.validAggregatedWeight())
{
weightedConductivity = conductivityCalc.weightedAverage();
weightedConductivity = conductivityCalc.weightedMean();
}
if (widthCalc.validAggregatedWeight())
{
weightedWidth = widthCalc.weightedAverage();
weightedWidth = widthCalc.weightedMean();
totalLength = widthCalc.aggregatedWeight();
}
}

View File

@@ -32,7 +32,7 @@
#include "RigResultAccessorFactory.h"
#include "RigTransmissibilityCondenser.h"
#include "RiaWeightedAverageCalculator.h"
#include "RiaWeightedMeanCalculator.h"
#include "RimEclipseCase.h"
#include "RimEllipseFractureTemplate.h"
#include "RimFracture.h"
@@ -146,7 +146,7 @@ double RigEclipseToStimPlanCalculator::totalEclipseAreaOpenForFlow() const
//--------------------------------------------------------------------------------------------------
double RigEclipseToStimPlanCalculator::areaWeightedMatrixTransmissibility() const
{
RiaWeightedAverageCalculator<double> calc;
RiaWeightedMeanCalculator<double> calc;
for (const auto& singleCellCalc : m_singleFractureCellCalculators)
{
@@ -155,7 +155,7 @@ double RigEclipseToStimPlanCalculator::areaWeightedMatrixTransmissibility() cons
calc.addValueAndWeight(calulator.aggregatedMatrixTransmissibility(), calulator.areaOpenForFlow());
}
return calc.weightedAverage();
return calc.weightedMean();
}
//--------------------------------------------------------------------------------------------------
@@ -174,7 +174,7 @@ double RigEclipseToStimPlanCalculator::areaWeightedWidth() const
auto stimPlanFractureTemplate = dynamic_cast<const RimStimPlanFractureTemplate*>(m_fracture->fractureTemplate());
if (stimPlanFractureTemplate)
{
RiaWeightedAverageCalculator<double> calc;
RiaWeightedMeanCalculator<double> calc;
auto widthValues = stimPlanFractureTemplate->widthResultValues();
@@ -188,7 +188,7 @@ double RigEclipseToStimPlanCalculator::areaWeightedWidth() const
calc.addValueAndWeight(widthValue, cellArea);
}
width = calc.weightedAverage();
width = calc.weightedMean();
}
return width;
@@ -199,7 +199,7 @@ double RigEclipseToStimPlanCalculator::areaWeightedWidth() const
//--------------------------------------------------------------------------------------------------
double RigEclipseToStimPlanCalculator::areaWeightedConductivity() const
{
RiaWeightedAverageCalculator<double> calc;
RiaWeightedMeanCalculator<double> calc;
for (const auto& singleCellCalc : m_singleFractureCellCalculators)
{
@@ -208,7 +208,7 @@ double RigEclipseToStimPlanCalculator::areaWeightedConductivity() const
calc.addValueAndWeight(singleCellCalc.second.fractureCell().getConductivityValue(), cellArea);
}
return calc.weightedAverage();
return calc.weightedMean();
}
//--------------------------------------------------------------------------------------------------

View File

@@ -23,7 +23,7 @@
#include "RigGeoMechWellLogExtractor.h"
#include "RiaDefines.h"
#include "RiaWeightedAverageCalculator.h"
#include "RiaWeightedMeanCalculator.h"
#include "RigFemTypes.h"
#include "RigGeoMechBoreHoleStressCalculator.h"
#include "RigFemPart.h"
@@ -316,7 +316,7 @@ void RigGeoMechWellLogExtractor::wellPathScaledCurveData(const RigFemResultAddre
float averageUnscaledValue = std::numeric_limits<float>::infinity();
bool validAverage = averageIntersectionValuesToSegmentValue(intersectionIdx, interpolatedInterfaceValues, std::numeric_limits<float>::infinity(), &averageUnscaledValue);
if (resAddr.fieldName == "PP")
if (resAddr.fieldName == "PP" && validAverage)
{
double segmentPorePressureFromGrid = averageUnscaledValue;
averageUnscaledValue = calculatePorePressureInSegment(intersectionIdx, segmentPorePressureFromGrid, hydroStaticPorePressureBar, effectiveDepthMeters, poreElementPressuresPascal);
@@ -776,7 +776,7 @@ double RigGeoMechWellLogExtractor::getWellLogSegmentValue(size_t intersectionIdx
endMD = m_intersectionMeasuredDepths[intersectionIdx];
}
RiaWeightedAverageCalculator<double> averageCalc;
RiaWeightedMeanCalculator<double> averageCalc;
for (auto& depthAndValue : wellLogValues)
{
if (cvf::Math::valueInRange(depthAndValue.first, startMD, endMD))
@@ -794,7 +794,7 @@ double RigGeoMechWellLogExtractor::getWellLogSegmentValue(size_t intersectionIdx
}
if (averageCalc.validAggregatedWeight())
{
return averageCalc.weightedAverage();
return averageCalc.weightedMean();
}
return std::numeric_limits<double>::infinity();
@@ -842,12 +842,12 @@ bool RigGeoMechWellLogExtractor::averageIntersectionValuesToSegmentValue(size_t
return false;
}
RiaWeightedAverageCalculator<T> averageCalc;
RiaWeightedMeanCalculator<T> averageCalc;
averageCalc.addValueAndWeight(value1, dist2);
averageCalc.addValueAndWeight(value2, dist1);
if (averageCalc.validAggregatedWeight())
{
*averagedCellValue = averageCalc.weightedAverage();
*averagedCellValue = averageCalc.weightedMean();
}
return true;
}

View File

@@ -47,8 +47,9 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaTimeHistoryCurveTools-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/SolveSpaceSolver-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaPolyArcLineSampler-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RifEclipseDataTableFormatter-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedAverage-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedMean-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedGeometricMeanCalculator-Test.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedHarmonicMeanCalculator-Test.cpp
)
list(APPEND CODE_HEADER_FILES

View File

@@ -0,0 +1,76 @@
#include "gtest/gtest.h"
#include "RiaWeightedHarmonicMeanCalculator.h"
#include <cmath>
#include <numeric>
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST(RiaWeightedHarmonicMeanCalculator, BasicUsage)
{
{
RiaWeightedHarmonicMeanCalculator calc;
EXPECT_DOUBLE_EQ(0.0, calc.aggregatedWeight());
EXPECT_FALSE(calc.validAggregatedWeight());
}
{
RiaWeightedHarmonicMeanCalculator calc;
std::vector<double> values {1, 4, 4};
std::vector<double> weights {1, 1, 1};
for (size_t i = 0; i< values.size(); i++)
{
calc.addValueAndWeight(values[i], weights[i]);
}
double expectedValue = 2.0;
EXPECT_DOUBLE_EQ(3.0, calc.aggregatedWeight());
EXPECT_NEAR(expectedValue, calc.weightedMean(), 1e-10);
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST(RiaWeightedHarmonicMeanCalculator, WeightedValues)
{
{
RiaWeightedHarmonicMeanCalculator calc;
std::vector<double> values{ 10, 5, 4, 3 };
std::vector<double> weights{ 10, 5, 4, 3 };
for (size_t i = 0; i < values.size(); i++)
{
calc.addValueAndWeight(values[i], weights[i]);
}
double sumWeights = std::accumulate(weights.begin(), weights.end(), 0.0);
EXPECT_DOUBLE_EQ(sumWeights, calc.aggregatedWeight());
EXPECT_NEAR(sumWeights, calc.weightedMean(), 1e-8);
}
{
RiaWeightedHarmonicMeanCalculator calc;
std::vector<double> values{ 2.0, 3.0, 1.0, 4.0 };
std::vector<double> weights{ 1.0, 2.0, 7.0, 3.0 };
for (size_t i = 0; i < values.size(); i++)
{
calc.addValueAndWeight(values[i], weights[i]);
}
double sumWeights = std::accumulate(weights.begin(), weights.end(), 0.0);
double aggregatedWeightAndValues = 1.0 / 2.0 + 2.0 / 3.0 + 7.0 / 1.0 + 3.0 / 4.0;
double expectedValue = sumWeights / aggregatedWeightAndValues;
EXPECT_DOUBLE_EQ(sumWeights, calc.aggregatedWeight());
EXPECT_NEAR(expectedValue, calc.weightedMean(), 1.0e-8);
}
}

View File

@@ -1,22 +1,22 @@
#include "gtest/gtest.h"
#include "RiaWeightedAverageCalculator.h"
#include "RiaWeightedMeanCalculator.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST(RiaWeightedAverageCalculator, BasicUsage)
TEST(RiaWeightedMeanCalculator, BasicUsage)
{
{
RiaWeightedAverageCalculator<double> calc;
RiaWeightedMeanCalculator<double> calc;
EXPECT_DOUBLE_EQ(0.0, calc.aggregatedWeight());
EXPECT_FALSE(calc.validAggregatedWeight());
}
{
RiaWeightedAverageCalculator<double> calc;
RiaWeightedMeanCalculator<double> calc;
std::vector<double> values {3.0, 6.0};
std::vector<double> weights {1.0, 2.0};
@@ -27,6 +27,6 @@ TEST(RiaWeightedAverageCalculator, BasicUsage)
}
EXPECT_TRUE(calc.validAggregatedWeight());
EXPECT_DOUBLE_EQ(3.0, calc.aggregatedWeight());
EXPECT_DOUBLE_EQ(5.0, calc.weightedAverage());
EXPECT_DOUBLE_EQ(5.0, calc.weightedMean());
}
}