From f3abbf4981c3525af569b7af4a69115d65ded342 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Fri, 7 Jun 2019 10:16:33 +0200 Subject: [PATCH 1/3] UDAValue: add method assert_numeric() --- opm/parser/eclipse/Deck/UDAValue.hpp | 3 +++ src/opm/parser/eclipse/Deck/UDAValue.cpp | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/opm/parser/eclipse/Deck/UDAValue.hpp b/opm/parser/eclipse/Deck/UDAValue.hpp index fc3b46ec2..abeffe64b 100644 --- a/opm/parser/eclipse/Deck/UDAValue.hpp +++ b/opm/parser/eclipse/Deck/UDAValue.hpp @@ -41,6 +41,9 @@ public: template bool is() const; + + void assert_numeric() const; + void assert_numeric(const std::string& error_msg) const; void set_dim(const Dimension& dim) const; const Dimension& get_dim() const; diff --git a/src/opm/parser/eclipse/Deck/UDAValue.cpp b/src/opm/parser/eclipse/Deck/UDAValue.cpp index 1512b8120..8e005ce66 100644 --- a/src/opm/parser/eclipse/Deck/UDAValue.cpp +++ b/src/opm/parser/eclipse/Deck/UDAValue.cpp @@ -40,6 +40,18 @@ UDAValue::UDAValue(const std::string& value): { } +void UDAValue::assert_numeric() const { + std::string msg = "Internal error: The support for use of UDQ/UDA is not complete in opm/flow. The string: '" + this->string_value + "' must be numeric"; + this->assert_numeric(msg); +} + + +void UDAValue::assert_numeric(const std::string& error_msg) const { + if (this->numeric_value) + return; + + throw std::invalid_argument(error_msg); +} template<> bool UDAValue::is() const { From 44e21101bc785bb52e43829f309fbe76b7103fd0 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Fri, 7 Jun 2019 10:17:29 +0200 Subject: [PATCH 2/3] UDAValue: the get() method will return SI values --- src/opm/parser/eclipse/Deck/UDAValue.cpp | 5 +++-- tests/parser/UDQTests.cpp | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/opm/parser/eclipse/Deck/UDAValue.cpp b/src/opm/parser/eclipse/Deck/UDAValue.cpp index 8e005ce66..7f565ebf4 100644 --- a/src/opm/parser/eclipse/Deck/UDAValue.cpp +++ b/src/opm/parser/eclipse/Deck/UDAValue.cpp @@ -67,8 +67,9 @@ bool UDAValue::is() const { template<> double UDAValue::get() const { - if (this->numeric_value) - return this->double_value; + this->assert_numeric(); + return this->dim.convertRawToSi(this->double_value); +} throw std::invalid_argument("UDAValue does not hold a numerical value"); } diff --git a/tests/parser/UDQTests.cpp b/tests/parser/UDQTests.cpp index 6aad58292..a0b7db5e6 100644 --- a/tests/parser/UDQTests.cpp +++ b/tests/parser/UDQTests.cpp @@ -1056,3 +1056,16 @@ BOOST_AUTO_TEST_CASE(UDA_VALUE) { BOOST_CHECK_EQUAL( value2.get(), std::string("FUBHP")); BOOST_CHECK_THROW( value2.get(), std::invalid_argument); } + + +/* + The unit/dimension handling in the UDAvalue is hacky at best. +*/ + +BOOST_AUTO_TEST_CASE(UDA_VALUE_DIM) { + UDAValue value0(1); + Dimension dim("DUMMY", 10); + BOOST_CHECK_EQUAL( value0.get(), 1); + value0.set_dim( dim ); + BOOST_CHECK_EQUAL( value0.get(), 10); +} From edbc9d2af72a2ad757a550775449635a25c74bc1 Mon Sep 17 00:00:00 2001 From: Joakim Hove Date: Fri, 7 Jun 2019 10:17:57 +0200 Subject: [PATCH 3/3] UDAValue: add reset() methods --- opm/parser/eclipse/Deck/UDAValue.hpp | 2 ++ src/opm/parser/eclipse/Deck/UDAValue.cpp | 15 ++++++++++++++- tests/parser/UDQTests.cpp | 6 ++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/opm/parser/eclipse/Deck/UDAValue.hpp b/opm/parser/eclipse/Deck/UDAValue.hpp index abeffe64b..e42821259 100644 --- a/opm/parser/eclipse/Deck/UDAValue.hpp +++ b/opm/parser/eclipse/Deck/UDAValue.hpp @@ -41,6 +41,8 @@ public: template bool is() const; + void reset(double value); + void reset(const std::string& value); void assert_numeric() const; void assert_numeric(const std::string& error_msg) const; diff --git a/src/opm/parser/eclipse/Deck/UDAValue.cpp b/src/opm/parser/eclipse/Deck/UDAValue.cpp index 7f565ebf4..b83a4fe14 100644 --- a/src/opm/parser/eclipse/Deck/UDAValue.cpp +++ b/src/opm/parser/eclipse/Deck/UDAValue.cpp @@ -71,7 +71,14 @@ double UDAValue::get() const { return this->dim.convertRawToSi(this->double_value); } - throw std::invalid_argument("UDAValue does not hold a numerical value"); +void UDAValue::reset(double value) { + this->double_value = value; + this->numeric_value = true; +} + +void UDAValue::reset(const std::string& value) { + this->string_value = value; + this->numeric_value = false; } template<> @@ -96,6 +103,9 @@ bool UDAValue::operator==(const UDAValue& other) const { if (this->numeric_value != other.numeric_value) return false; + if (this->dim != other.dim) + return false; + if (this->numeric_value) return (this->double_value == other.double_value); @@ -114,4 +124,7 @@ std::ostream& operator<<( std::ostream& stream, const UDAValue& uda_value ) { stream << "'" << uda_value.get() << "'"; return stream; } + + + } diff --git a/tests/parser/UDQTests.cpp b/tests/parser/UDQTests.cpp index a0b7db5e6..5995710e8 100644 --- a/tests/parser/UDQTests.cpp +++ b/tests/parser/UDQTests.cpp @@ -1041,6 +1041,12 @@ BOOST_AUTO_TEST_CASE(UDA_VALUE) { BOOST_CHECK(!value0.is()); BOOST_CHECK_EQUAL( value0.get(), 0); BOOST_CHECK_THROW( value0.get(), std::invalid_argument); + value0.reset( 10 ); + BOOST_CHECK_EQUAL( value0.get(), 10); + BOOST_CHECK_THROW( value0.get(), std::invalid_argument); + value0.reset( "STRING" ); + BOOST_CHECK_EQUAL( value0.get(), std::string("STRING")); + BOOST_CHECK_THROW( value0.get(), std::invalid_argument); UDAValue value1(10);