18#ifndef OPM_WELL_SOLVE_RECOVERY_HPP
19#define OPM_WELL_SOLVE_RECOVERY_HPP
21#include <opm/common/Exceptions.hpp>
34template<
class Communication>
37 return (comm.size() > 1) ? (comm.max(
static_cast<int>(condition)) != 0) : condition;
60template<
class Communication,
class Solve,
class Recover>
64 constexpr int numerical_problem = 1;
65 constexpr int fatal = 2;
69 std::exception_ptr fatal_exception;
71 std::forward<Solve>(solve)();
72 }
catch (
const NumericalProblem& e) {
73 outcome = numerical_problem;
77 fatal_exception = std::current_exception();
81 const int agreed = (comm.size() > 1) ? comm.max(outcome) : outcome;
82 if (agreed == fatal) {
83 if (fatal_exception) {
84 std::rethrow_exception(fatal_exception);
86 throw std::runtime_error(
"The solve of a well failed on another rank of the well.");
88 if (agreed == numerical_problem) {
89 std::forward<Recover>(recover)(outcome == numerical_problem, std::as_const(reason));
109template<
class Wells,
class GetCommunication,
class Solve,
class Recover>
111 GetCommunication&& get_communication,
115 bool any_recovered =
false;
116 std::exception_ptr deferred;
117 for (
auto& well : wells) {
120 get_communication(well),
121 [&]() { solve(well); },
122 [&](
const bool failed_on_this_rank,
const std::string& reason) {
123 recover(well, failed_on_this_rank, reason);
125 any_recovered = any_recovered || recovered;
128 deferred = std::current_exception();
133 std::rethrow_exception(deferred);
135 return any_recovered;
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
Definition: blackoilbioeffectsmodules.hh:45
bool anyRank(const Communication &comm, const bool condition)
Whether a condition holds on any rank of the communicator.
Definition: WellSolveRecovery.hpp:35
bool solveWithSharedRecovery(const Communication &comm, Solve &&solve, Recover &&recover)
Run a solve of a well and recover from a numerical failure on all ranks of the well.
Definition: WellSolveRecovery.hpp:61
bool solveWellsWithSharedRecovery(Wells &&wells, GetCommunication &&get_communication, Solve &&solve, Recover &&recover)
Solve a list of wells, and recover from numerical failures, with the failures of the wells deferred.
Definition: WellSolveRecovery.hpp:110