mirror of
https://github.com/OPM/ResInsight.git
synced 2025-02-09 23:16:00 -06:00
#2560 Unit conversion : Add conversion based on parsing unit text string
This commit is contained in:
parent
fbe3e4fbda
commit
2d01824ab3
@ -124,3 +124,58 @@ QString RiaEclipseUnitTools::unitStringPressure(UnitSystem unitSystem)
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
double RiaEclipseUnitTools::convertToMeter(double sourceValue, const QString& unitText)
|
||||
{
|
||||
QString timmed = unitText.trimmed();
|
||||
|
||||
if (timmed.compare("m", Qt::CaseInsensitive) == 0)
|
||||
{
|
||||
return sourceValue;
|
||||
}
|
||||
else if (timmed.compare("cm", Qt::CaseInsensitive) == 0)
|
||||
{
|
||||
return sourceValue / 100.0;
|
||||
}
|
||||
else if (timmed.compare("in", Qt::CaseInsensitive) == 0)
|
||||
{
|
||||
return RiaEclipseUnitTools::inchToMeter(sourceValue);
|
||||
}
|
||||
else if (timmed.compare("ft", Qt::CaseInsensitive) == 0)
|
||||
{
|
||||
return RiaEclipseUnitTools::feetToMeter(sourceValue);
|
||||
}
|
||||
|
||||
return HUGE_VAL;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
double RiaEclipseUnitTools::convertToFeet(double sourceValue, const QString& unitText)
|
||||
{
|
||||
QString timmed = unitText.trimmed();
|
||||
|
||||
if (timmed.compare("ft", Qt::CaseInsensitive) == 0)
|
||||
{
|
||||
return sourceValue;
|
||||
}
|
||||
else if (timmed.compare("in", Qt::CaseInsensitive) == 0)
|
||||
{
|
||||
return RiaEclipseUnitTools::inchToFeet(sourceValue);
|
||||
}
|
||||
else if (timmed.compare("cm", Qt::CaseInsensitive) == 0)
|
||||
{
|
||||
double meter = sourceValue / 100.0;
|
||||
return RiaEclipseUnitTools::meterToFeet(meter);
|
||||
}
|
||||
else if (timmed.compare("m", Qt::CaseInsensitive) == 0)
|
||||
{
|
||||
return RiaEclipseUnitTools::meterToFeet(sourceValue);
|
||||
}
|
||||
|
||||
return HUGE_VAL;
|
||||
}
|
||||
|
||||
|
@ -51,5 +51,8 @@ public:
|
||||
static double convertSurfaceGasFlowRateToOilEquivalents(UnitSystem, double eclGasFlowRate);
|
||||
|
||||
static QString unitStringPressure(UnitSystem unitSystem);
|
||||
|
||||
static double convertToMeter(double sourceValue, const QString& unitText);
|
||||
static double convertToFeet(double sourceValue, const QString& unitText);
|
||||
};
|
||||
|
||||
|
@ -38,6 +38,8 @@ ${CMAKE_CURRENT_LIST_DIR}/RiaProjectFileVersionTools-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RifElementPropertyTableReader-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RimRelocatePath-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RigTransmissibilityCondenser-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RigTransmissibilityCondenser-Test.cpp
|
||||
${CMAKE_CURRENT_LIST_DIR}/RiaEclipseUnitTools-Test
|
||||
)
|
||||
|
||||
list(APPEND CODE_HEADER_FILES
|
||||
|
76
ApplicationCode/UnitTests/RiaEclipseUnitTools-Test.cpp
Normal file
76
ApplicationCode/UnitTests/RiaEclipseUnitTools-Test.cpp
Normal file
@ -0,0 +1,76 @@
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "RiaEclipseUnitTools.h"
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaEclipseUnitTools, TestConversionToMeter)
|
||||
{
|
||||
double deltaRange = 1e-7;
|
||||
|
||||
{
|
||||
double sourceValue = RiaEclipseUnitTools::feetPerMeter();
|
||||
QString unitText = "ft";
|
||||
double destValue = RiaEclipseUnitTools::convertToMeter(sourceValue, unitText);
|
||||
EXPECT_NEAR(1.0, destValue, deltaRange);
|
||||
}
|
||||
|
||||
{
|
||||
double sourceValue = RiaEclipseUnitTools::feetPerMeter() * 12.0;
|
||||
QString unitText = "in";
|
||||
double destValue = RiaEclipseUnitTools::convertToMeter(sourceValue, unitText);
|
||||
EXPECT_NEAR(1.0, destValue, deltaRange);
|
||||
}
|
||||
|
||||
{
|
||||
double sourceValue = 1.0;
|
||||
QString unitText = "m";
|
||||
double destValue = RiaEclipseUnitTools::convertToMeter(sourceValue, unitText);
|
||||
EXPECT_NEAR(1.0, destValue, deltaRange);
|
||||
}
|
||||
|
||||
{
|
||||
double sourceValue = 100.0;
|
||||
QString unitText = "cm";
|
||||
double destValue = RiaEclipseUnitTools::convertToMeter(sourceValue, unitText);
|
||||
EXPECT_NEAR(1.0, destValue, deltaRange);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
///
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST(RiaEclipseUnitTools, TestConversionToFeet)
|
||||
{
|
||||
double deltaRange = 1e-7;
|
||||
|
||||
{
|
||||
double sourceValue = 1.0;
|
||||
QString unitText = "ft";
|
||||
double destValue = RiaEclipseUnitTools::convertToFeet(sourceValue, unitText);
|
||||
EXPECT_NEAR(1.0, destValue, deltaRange);
|
||||
}
|
||||
|
||||
{
|
||||
double sourceValue = 1.0 * 12.0;
|
||||
QString unitText = "in";
|
||||
double destValue = RiaEclipseUnitTools::convertToFeet(sourceValue, unitText);
|
||||
EXPECT_NEAR(1.0, destValue, deltaRange);
|
||||
}
|
||||
|
||||
{
|
||||
double sourceValue = RiaEclipseUnitTools::meterPerFeet();
|
||||
QString unitText = "m";
|
||||
double destValue = RiaEclipseUnitTools::convertToFeet(sourceValue, unitText);
|
||||
EXPECT_NEAR(1.0, destValue, deltaRange);
|
||||
}
|
||||
|
||||
{
|
||||
double sourceValue = RiaEclipseUnitTools::meterPerFeet() * 100.0;
|
||||
QString unitText = "cm";
|
||||
double destValue = RiaEclipseUnitTools::convertToFeet(sourceValue, unitText);
|
||||
EXPECT_NEAR(1.0, destValue, deltaRange);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user