mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-25 18:55:39 -06:00
Make RiaWeightedAverageCalculator a template class.
This commit is contained in:
@@ -34,6 +34,7 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaArcCurveCalculator.h
|
|||||||
${CMAKE_CURRENT_LIST_DIR}/RiaJCurveCalculator.h
|
${CMAKE_CURRENT_LIST_DIR}/RiaJCurveCalculator.h
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RiaOffshoreSphericalCoords.h
|
${CMAKE_CURRENT_LIST_DIR}/RiaOffshoreSphericalCoords.h
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedAverageCalculator.h
|
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedAverageCalculator.h
|
||||||
|
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedAverageCalculator.inl
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedGeometricMeanCalculator.h
|
${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}/RiaSCurveCalculator.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RiaArcCurveCalculator.cpp
|
${CMAKE_CURRENT_LIST_DIR}/RiaArcCurveCalculator.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RiaJCurveCalculator.cpp
|
${CMAKE_CURRENT_LIST_DIR}/RiaJCurveCalculator.cpp
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedAverageCalculator.cpp
|
|
||||||
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedGeometricMeanCalculator.cpp
|
${CMAKE_CURRENT_LIST_DIR}/RiaWeightedGeometricMeanCalculator.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -21,17 +21,20 @@
|
|||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
template<class T>
|
||||||
class RiaWeightedAverageCalculator
|
class RiaWeightedAverageCalculator
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
RiaWeightedAverageCalculator();
|
RiaWeightedAverageCalculator();
|
||||||
|
|
||||||
void addValueAndWeight(double value, double weight);
|
void addValueAndWeight(T value, double weight);
|
||||||
|
|
||||||
double weightedAverage() const;
|
T weightedAverage() const;
|
||||||
double aggregatedWeight() const;
|
double aggregatedWeight() const;
|
||||||
|
bool validAggregatedWeight() const;
|
||||||
private:
|
private:
|
||||||
double m_aggregatedValue;
|
T m_aggregatedValue;
|
||||||
double m_aggregatedWeight;
|
double m_aggregatedWeight;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#include "RiaWeightedAverageCalculator.inl"
|
||||||
@@ -16,15 +16,14 @@
|
|||||||
//
|
//
|
||||||
/////////////////////////////////////////////////////////////////////////////////
|
/////////////////////////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
#include "RiaWeightedAverageCalculator.h"
|
|
||||||
|
|
||||||
#include "cvfAssert.h"
|
#include "cvfAssert.h"
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
RiaWeightedAverageCalculator::RiaWeightedAverageCalculator()
|
template<class T>
|
||||||
: m_aggregatedValue(0.0)
|
RiaWeightedAverageCalculator<T>::RiaWeightedAverageCalculator()
|
||||||
|
: m_aggregatedValue(T{})
|
||||||
, m_aggregatedWeight(0.0)
|
, 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);
|
CVF_ASSERT(weight >= 0.0);
|
||||||
|
|
||||||
m_aggregatedValue += value * weight;
|
m_aggregatedValue = m_aggregatedValue + value * weight;
|
||||||
m_aggregatedWeight += 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 T{};
|
||||||
return 0.0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
///
|
///
|
||||||
//--------------------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------------------
|
||||||
double RiaWeightedAverageCalculator::aggregatedWeight() const
|
template<class T>
|
||||||
|
double RiaWeightedAverageCalculator<T>::aggregatedWeight() const
|
||||||
{
|
{
|
||||||
return m_aggregatedWeight;
|
return m_aggregatedWeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
///
|
||||||
|
//--------------------------------------------------------------------------------------------------
|
||||||
|
template<class T>
|
||||||
|
bool RiaWeightedAverageCalculator<T>::validAggregatedWeight() const
|
||||||
|
{
|
||||||
|
return m_aggregatedWeight > 1.0e-12;
|
||||||
|
}
|
||||||
@@ -477,8 +477,8 @@ FractureWidthAndConductivity
|
|||||||
nameUnit.first, nameUnit.second, m_activeTimeStepIndex, fractureTemplateUnit());
|
nameUnit.first, nameUnit.second, m_activeTimeStepIndex, fractureTemplateUnit());
|
||||||
}
|
}
|
||||||
|
|
||||||
RiaWeightedAverageCalculator widthCalc;
|
RiaWeightedAverageCalculator<double> widthCalc;
|
||||||
RiaWeightedAverageCalculator conductivityCalc;
|
RiaWeightedAverageCalculator<double> conductivityCalc;
|
||||||
|
|
||||||
RigWellPathStimplanIntersector intersector(rimWellPath->wellPathGeometry(), fractureInstance);
|
RigWellPathStimplanIntersector intersector(rimWellPath->wellPathGeometry(), fractureInstance);
|
||||||
for (const auto& v : intersector.intersections())
|
for (const auto& v : intersector.intersections())
|
||||||
@@ -497,10 +497,15 @@ FractureWidthAndConductivity
|
|||||||
intersectionLength);
|
intersectionLength);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (conductivityCalc.validAggregatedWeight())
|
||||||
weightedConductivity = conductivityCalc.weightedAverage();
|
{
|
||||||
weightedWidth = widthCalc.weightedAverage();
|
weightedConductivity = conductivityCalc.weightedAverage();
|
||||||
totalLength = widthCalc.aggregatedWeight();
|
}
|
||||||
|
if (widthCalc.validAggregatedWeight())
|
||||||
|
{
|
||||||
|
weightedWidth = widthCalc.weightedAverage();
|
||||||
|
totalLength = widthCalc.aggregatedWeight();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (totalLength > 1e-7)
|
if (totalLength > 1e-7)
|
||||||
|
|||||||
@@ -9,15 +9,14 @@
|
|||||||
TEST(RiaWeightedAverageCalculator, BasicUsage)
|
TEST(RiaWeightedAverageCalculator, BasicUsage)
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
RiaWeightedAverageCalculator calc;
|
RiaWeightedAverageCalculator<double> calc;
|
||||||
|
|
||||||
EXPECT_DOUBLE_EQ(0.0, calc.aggregatedWeight());
|
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> values {3.0, 6.0};
|
||||||
std::vector<double> weights {1.0, 2.0};
|
std::vector<double> weights {1.0, 2.0};
|
||||||
@@ -26,7 +25,7 @@ TEST(RiaWeightedAverageCalculator, BasicUsage)
|
|||||||
{
|
{
|
||||||
calc.addValueAndWeight(values[i], weights[i]);
|
calc.addValueAndWeight(values[i], weights[i]);
|
||||||
}
|
}
|
||||||
|
EXPECT_TRUE(calc.validAggregatedWeight());
|
||||||
EXPECT_DOUBLE_EQ(3.0, calc.aggregatedWeight());
|
EXPECT_DOUBLE_EQ(3.0, calc.aggregatedWeight());
|
||||||
EXPECT_DOUBLE_EQ(5.0, calc.weightedAverage());
|
EXPECT_DOUBLE_EQ(5.0, calc.weightedAverage());
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user