Make RiaWeightedAverageCalculator a template class.

This commit is contained in:
Gaute Lindkvist 2018-09-06 19:58:28 +02:00
parent 51434dd304
commit 11253f2dbd
5 changed files with 48 additions and 28 deletions

View File

@ -34,6 +34,7 @@ ${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}/RiaWeightedGeometricMeanCalculator.h
)
@ -70,7 +71,6 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaPolyArcLineSampler.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaSCurveCalculator.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaArcCurveCalculator.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaJCurveCalculator.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedAverageCalculator.cpp
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedGeometricMeanCalculator.cpp
)

View File

@ -21,17 +21,20 @@
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template<class T>
class RiaWeightedAverageCalculator
{
public:
RiaWeightedAverageCalculator();
void addValueAndWeight(double value, double weight);
void addValueAndWeight(T value, double weight);
double weightedAverage() const;
T weightedAverage() const;
double aggregatedWeight() const;
bool validAggregatedWeight() const;
private:
double m_aggregatedValue;
T m_aggregatedValue;
double m_aggregatedWeight;
};
#include "RiaWeightedAverageCalculator.inl"

View File

@ -16,15 +16,14 @@
//
/////////////////////////////////////////////////////////////////////////////////
#include "RiaWeightedAverageCalculator.h"
#include "cvfAssert.h"
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
RiaWeightedAverageCalculator::RiaWeightedAverageCalculator()
: m_aggregatedValue(0.0)
template<class T>
RiaWeightedAverageCalculator<T>::RiaWeightedAverageCalculator()
: m_aggregatedValue(T{})
, m_aggregatedWeight(0.0)
{
}
@ -32,31 +31,45 @@ RiaWeightedAverageCalculator::RiaWeightedAverageCalculator()
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
void RiaWeightedAverageCalculator::addValueAndWeight(double value, double weight)
template<class T>
void RiaWeightedAverageCalculator<T>::addValueAndWeight(T value, double weight)
{
CVF_ASSERT(weight >= 0.0);
m_aggregatedValue += value * weight;
m_aggregatedValue = m_aggregatedValue + value * weight;
m_aggregatedWeight += weight;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaWeightedAverageCalculator::weightedAverage() const
template<class T>
T RiaWeightedAverageCalculator<T>::weightedAverage() const
{
if (m_aggregatedWeight > 1e-7)
bool validWeights = validAggregatedWeight();
CVF_TIGHT_ASSERT(validWeights);
if (validWeights)
{
return m_aggregatedValue / m_aggregatedWeight;
return m_aggregatedValue * (1.0 / m_aggregatedWeight);
}
return 0.0;
return T{};
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
double RiaWeightedAverageCalculator::aggregatedWeight() const
template<class T>
double RiaWeightedAverageCalculator<T>::aggregatedWeight() const
{
return m_aggregatedWeight;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
template<class T>
bool RiaWeightedAverageCalculator<T>::validAggregatedWeight() const
{
return m_aggregatedWeight > 1.0e-12;
}

View File

@ -477,8 +477,8 @@ FractureWidthAndConductivity
nameUnit.first, nameUnit.second, m_activeTimeStepIndex, fractureTemplateUnit());
}
RiaWeightedAverageCalculator widthCalc;
RiaWeightedAverageCalculator conductivityCalc;
RiaWeightedAverageCalculator<double> widthCalc;
RiaWeightedAverageCalculator<double> conductivityCalc;
RigWellPathStimplanIntersector intersector(rimWellPath->wellPathGeometry(), fractureInstance);
for (const auto& v : intersector.intersections())
@ -497,10 +497,15 @@ FractureWidthAndConductivity
intersectionLength);
}
}
weightedConductivity = conductivityCalc.weightedAverage();
weightedWidth = widthCalc.weightedAverage();
totalLength = widthCalc.aggregatedWeight();
if (conductivityCalc.validAggregatedWeight())
{
weightedConductivity = conductivityCalc.weightedAverage();
}
if (widthCalc.validAggregatedWeight())
{
weightedWidth = widthCalc.weightedAverage();
totalLength = widthCalc.aggregatedWeight();
}
}
if (totalLength > 1e-7)

View File

@ -9,15 +9,14 @@
TEST(RiaWeightedAverageCalculator, BasicUsage)
{
{
RiaWeightedAverageCalculator calc;
RiaWeightedAverageCalculator<double> calc;
EXPECT_DOUBLE_EQ(0.0, calc.aggregatedWeight());
EXPECT_DOUBLE_EQ(0.0, calc.weightedAverage());
EXPECT_FALSE(calc.validAggregatedWeight());
}
{
RiaWeightedAverageCalculator calc;
RiaWeightedAverageCalculator<double> calc;
std::vector<double> values {3.0, 6.0};
std::vector<double> weights {1.0, 2.0};
@ -26,7 +25,7 @@ TEST(RiaWeightedAverageCalculator, BasicUsage)
{
calc.addValueAndWeight(values[i], weights[i]);
}
EXPECT_TRUE(calc.validAggregatedWeight());
EXPECT_DOUBLE_EQ(3.0, calc.aggregatedWeight());
EXPECT_DOUBLE_EQ(5.0, calc.weightedAverage());
}