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;
}