Merge pull request #2050 from joakim-hove/udq-cast-error

Improve error message for invalid udq_cast()
This commit is contained in:
Joakim Hove 2020-10-27 19:49:51 +01:00 committed by GitHub
commit e5519c456c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 30 additions and 14 deletions

View File

@ -19,13 +19,15 @@
#include <iostream> #include <iostream>
#include <cstring> #include <cstring>
#include <tuple> #include <tuple>
#include <fmt/format.h>
#include <exception>
#include <opm/parser/eclipse/Parser/ParseContext.hpp> #include <opm/parser/eclipse/Parser/ParseContext.hpp>
#include <opm/parser/eclipse/Parser/ErrorGuard.hpp> #include <opm/parser/eclipse/Parser/ErrorGuard.hpp>
#include <opm/parser/eclipse/EclipseState/Schedule/UDQ/UDQASTNode.hpp> #include <opm/parser/eclipse/EclipseState/Schedule/UDQ/UDQASTNode.hpp>
#include <opm/parser/eclipse/EclipseState/Schedule/UDQ/UDQDefine.hpp> #include <opm/parser/eclipse/EclipseState/Schedule/UDQ/UDQDefine.hpp>
#include <opm/parser/eclipse/EclipseState/Schedule/UDQ/UDQEnums.hpp> #include <opm/parser/eclipse/EclipseState/Schedule/UDQ/UDQEnums.hpp>
#include <opm/common/OpmLog/OpmLog.hpp>
#include "../../../Parser/raw/RawConsts.hpp" #include "../../../Parser/raw/RawConsts.hpp"
#include "UDQToken.hpp" #include "UDQToken.hpp"
@ -240,15 +242,26 @@ void UDQDefine::required_summary(std::unordered_set<std::string>& summary_keys)
} }
UDQSet UDQDefine::eval(const UDQContext& context) const { UDQSet UDQDefine::eval(const UDQContext& context) const {
UDQSet res = this->ast->eval(this->m_var_type, context); std::optional<UDQSet> res;
res.name( this->m_keyword ); try {
res = this->ast->eval(this->m_var_type, context);
if (!dynamic_type_check(this->var_type(), res.var_type())) { res->name( this->m_keyword );
std::string msg = "Invalid runtime type conversion detected when evaluating UDQ"; if (!dynamic_type_check(this->var_type(), res->var_type())) {
throw std::invalid_argument(msg); std::string msg = "Invalid runtime type conversion detected when evaluating UDQ";
throw std::invalid_argument(msg);
}
} catch (const std::exception& exc) {
auto msg = fmt::format("Problem evaluating UDQ {}\n"
"In {} line {}\n"
"Internal error: {}", this->m_keyword, this->m_location.filename, this->m_location.lineno, exc.what());
OpmLog::error(msg);
std::throw_with_nested(exc);
} }
if (!res)
throw std::logic_error("Bug in UDQDefine::eval()");
if (res.var_type() == UDQVarType::SCALAR) {
if (res->var_type() == UDQVarType::SCALAR) {
/* /*
If the right hand side evaluates to a scalar that scalar value should If the right hand side evaluates to a scalar that scalar value should
be set for all wells in the wellset: be set for all wells in the wellset:
@ -265,7 +278,7 @@ UDQSet UDQDefine::eval(const UDQContext& context) const {
regarding the semantics of group sets. regarding the semantics of group sets.
*/ */
const auto& scalar_value = res[0].value(); const auto& scalar_value = res->operator[](0).value();
if (this->var_type() == UDQVarType::WELL_VAR) { if (this->var_type() == UDQVarType::WELL_VAR) {
const std::vector<std::string> wells = context.wells(); const std::vector<std::string> wells = context.wells();
UDQSet well_res = UDQSet::wells(this->m_keyword, wells); UDQSet well_res = UDQSet::wells(this->m_keyword, wells);
@ -287,7 +300,7 @@ UDQSet UDQDefine::eval(const UDQContext& context) const {
} }
} }
return res; return *res;
} }
const KeywordLocation& UDQDefine::location() const { const KeywordLocation& UDQDefine::location() const {

View File

@ -19,6 +19,7 @@
#include <fnmatch.h> #include <fnmatch.h>
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#include <fmt/format.h>
#include <opm/parser/eclipse/EclipseState/Schedule/UDQ/UDQSet.hpp> #include <opm/parser/eclipse/EclipseState/Schedule/UDQ/UDQSet.hpp>
@ -485,8 +486,10 @@ UDQSet udq_cast(const UDQSet& lhs, const UDQSet& rhs)
{ {
if (lhs.size() != rhs.size()) { if (lhs.size() != rhs.size()) {
if (lhs.var_type() != UDQVarType::SCALAR) { if (lhs.var_type() != UDQVarType::SCALAR) {
printf("s1: %ld s2: %ld \n", lhs.size(), rhs.size()); auto msg = fmt::format("Type/size mismatch when combining UDQs {}(size={}, type={}) and {}(size={}, type={})",
throw std::logic_error("Type/size mismatch"); lhs.name(), lhs.size(), lhs.var_type(),
rhs.name(), rhs.size(), rhs.var_type());
throw std::logic_error(msg);
} }
if (rhs.var_type() == UDQVarType::WELL_VAR) if (rhs.var_type() == UDQVarType::WELL_VAR)

View File

@ -922,7 +922,7 @@ inline void keywordMISC( SummaryConfig::keyword_list& list,
if (!udq.has_unit(location.keyword)) { if (!udq.has_unit(location.keyword)) {
std::string msg = "Summary output requested for UDQ {keyword}\n" std::string msg = "Summary output requested for UDQ {keyword}\n"
"In {file} line {line}\n" "In {file} line {line}\n"
"No unit define in the SCHEDULE section"; "No unit defined in the SCHEDULE section for {keyword}";
parseContext.handleError(ParseContext::SUMMARY_UDQ_MISSING_UNIT, msg, location, errors); parseContext.handleError(ParseContext::SUMMARY_UDQ_MISSING_UNIT, msg, location, errors);
} }
} }

View File

@ -2189,7 +2189,7 @@ UDQ
UDQState udq_state(undefined_value); UDQState udq_state(undefined_value);
SummaryState st(std::chrono::system_clock::now()); SummaryState st(std::chrono::system_clock::now());
BOOST_CHECK_THROW(udq.eval(0, st, udq_state), std::out_of_range); BOOST_CHECK_THROW(udq.eval(0, st, udq_state), std::exception);
} }