From 749ea93e245f03aefb6cc45f2649004f22d5bd56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Atgeirr=20Fl=C3=B8=20Rasmussen?= Date: Wed, 8 Nov 2023 15:16:17 +0100 Subject: [PATCH] Add and use OPM_DEFLOG_PROBLEM macro. This should replace OPM_DEFLOG_THROW in places where the problem category is more appropriate than the error category. In this commit, uses of OPM_DEFLOG_THROW have been replaced whenever the exception class used was NumericalProblem. --- .../utils/DeferredLoggingErrorHelpers.hpp | 19 ++++++++++++++++++- opm/simulators/wells/MSWellHelpers.cpp | 2 +- .../wells/MultisegmentWell_impl.hpp | 10 +++++----- .../wells/StandardWellPrimaryVariables.cpp | 10 +++++----- opm/simulators/wells/StandardWell_impl.hpp | 2 +- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/opm/simulators/utils/DeferredLoggingErrorHelpers.hpp b/opm/simulators/utils/DeferredLoggingErrorHelpers.hpp index d65954d41..354d901d6 100644 --- a/opm/simulators/utils/DeferredLoggingErrorHelpers.hpp +++ b/opm/simulators/utils/DeferredLoggingErrorHelpers.hpp @@ -32,7 +32,7 @@ #include #include -// Macro to throw an exception. +// Macro to log an error and throw an exception. // Inspired by ErrorMacros.hpp in opm-common. // NOTE: For this macro to work, the // exception class must exhibit a constructor with the signature @@ -51,6 +51,23 @@ throw Exception(oss_); \ } while (false) +// Macro to log a problem and throw an exception. +// Idenitical to OPM_DEFLOG_THROW() except for using +// the "problem" category instead of "error" for the +// log message. The Exception argument will typically +// be NumericalProblem. +// +// Usage: OPM_DEFLOG_PROBLEM(ExceptionClass, "Error message", DeferredLogger); +#define OPM_DEFLOG_PROBLEM(Exception, message, deferred_logger) \ + do { \ + std::string oss_ = std::string{"["} + __FILE__ + ":" + \ + std::to_string(__LINE__) + "] " + \ + message; \ + deferred_logger.problem(oss_); \ + throw Exception(oss_); \ + } while (false) + + namespace { void _throw(Opm::ExceptionType::ExcEnum exc_type, diff --git a/opm/simulators/wells/MSWellHelpers.cpp b/opm/simulators/wells/MSWellHelpers.cpp index 9d912585f..7f347a7f9 100644 --- a/opm/simulators/wells/MSWellHelpers.cpp +++ b/opm/simulators/wells/MSWellHelpers.cpp @@ -206,7 +206,7 @@ invDX(const MatrixType& D, VectorType x, DeferredLogger& deferred_logger) linsolver.apply(y, x, res); if ( !res.converged ) { - OPM_DEFLOG_THROW(NumericalProblem, "the invDX did not converge ", deferred_logger); + OPM_DEFLOG_PROBLEM(NumericalProblem, "the invDX did not converge ", deferred_logger); } return y; diff --git a/opm/simulators/wells/MultisegmentWell_impl.hpp b/opm/simulators/wells/MultisegmentWell_impl.hpp index d255f6daa..881246ae0 100644 --- a/opm/simulators/wells/MultisegmentWell_impl.hpp +++ b/opm/simulators/wells/MultisegmentWell_impl.hpp @@ -848,11 +848,11 @@ namespace Opm const Value d = 1.0 - rv * rs; if (getValue(d) == 0.0) { - OPM_DEFLOG_THROW(NumericalProblem, - fmt::format("Zero d value obtained for well {} " - "during flux calculation with rs {} and rv {}", - this->name(), rs, rv), - deferred_logger); + OPM_DEFLOG_PROBLEM(NumericalProblem, + fmt::format("Zero d value obtained for well {} " + "during flux calculation with rs {} and rv {}", + this->name(), rs, rv), + deferred_logger); } const Value tmp_oil = (cmix_s[oilCompIdx] - rv * cmix_s[gasCompIdx]) / d; diff --git a/opm/simulators/wells/StandardWellPrimaryVariables.cpp b/opm/simulators/wells/StandardWellPrimaryVariables.cpp index 4d387ecb0..ece8c18b0 100644 --- a/opm/simulators/wells/StandardWellPrimaryVariables.cpp +++ b/opm/simulators/wells/StandardWellPrimaryVariables.cpp @@ -68,7 +68,7 @@ Scalar relaxationFactorFraction(const Scalar old_value, constexpr double epislon = std::numeric_limits::epsilon(); if (old_value < -epislon || old_value > 1.0 + epislon) { const std::string msg = fmt::format(" illegal fraction value {} {} is found for well {}", value_name, old_value, well_name); - OPM_DEFLOG_THROW(Opm::NumericalProblem, msg, deferred_logger); + OPM_DEFLOG_PROBLEM(Opm::NumericalProblem, msg, deferred_logger); } const Scalar& safe_old_value = std::clamp(old_value, 0.0, 1.0); @@ -707,7 +707,7 @@ relaxationFactorFractionsProducer(const BVectorWell& dwells, DeferredLogger& def } if (relaxation_factor < 0.0 || relaxation_factor > 1.0) { const std::string msg = fmt::format(" illegal relaxation factor {} is obtained for well {}", relaxation_factor, this->well_.name()); - OPM_DEFLOG_THROW(NumericalProblem, msg, deferred_logger); + OPM_DEFLOG_PROBLEM(NumericalProblem, msg, deferred_logger); } } return relaxation_factor; @@ -719,9 +719,9 @@ checkFinite(DeferredLogger& deferred_logger) const { for (const Scalar v : value_) { if (!isfinite(v)) - OPM_DEFLOG_THROW(NumericalProblem, - "Infinite primary variable after update from wellState, well: " + well_.name(), - deferred_logger); + OPM_DEFLOG_PROBLEM(NumericalProblem, + "Infinite primary variable after update from wellState, well: " + well_.name(), + deferred_logger); } } diff --git a/opm/simulators/wells/StandardWell_impl.hpp b/opm/simulators/wells/StandardWell_impl.hpp index 89145604e..4cb54f2ef 100644 --- a/opm/simulators/wells/StandardWell_impl.hpp +++ b/opm/simulators/wells/StandardWell_impl.hpp @@ -467,7 +467,7 @@ namespace Opm try { this->linSys_.invert(); } catch( ... ) { - OPM_DEFLOG_THROW(NumericalProblem, "Error when inverting local well equations for well " + name(), deferred_logger); + OPM_DEFLOG_PROBLEM(NumericalProblem, "Error when inverting local well equations for well " + name(), deferred_logger); } }