WellSolveRecovery.hpp
Go to the documentation of this file.
1/*
2 This file is part of the Open Porous Media project (OPM).
3
4 OPM is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 OPM is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with OPM. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18#ifndef OPM_WELL_SOLVE_RECOVERY_HPP
19#define OPM_WELL_SOLVE_RECOVERY_HPP
20
21#include <opm/common/Exceptions.hpp>
22
23#include <exception>
24#include <stdexcept>
25#include <string>
26#include <utility>
27
28namespace Opm {
29
34template<class Communication>
35bool anyRank(const Communication& comm, const bool condition)
36{
37 return (comm.size() > 1) ? (comm.max(static_cast<int>(condition)) != 0) : condition;
38}
39
60template<class Communication, class Solve, class Recover>
61bool solveWithSharedRecovery(const Communication& comm, Solve&& solve, Recover&& recover)
62{
63 constexpr int ok = 0;
64 constexpr int numerical_problem = 1;
65 constexpr int fatal = 2;
66
67 int outcome = ok;
68 std::string reason;
69 std::exception_ptr fatal_exception;
70 try {
71 std::forward<Solve>(solve)();
72 } catch (const NumericalProblem& e) {
73 outcome = numerical_problem;
74 reason = e.what();
75 } catch (...) {
76 outcome = fatal;
77 fatal_exception = std::current_exception();
78 }
79
80 // the most severe outcome on any rank
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);
85 }
86 throw std::runtime_error("The solve of a well failed on another rank of the well.");
87 }
88 if (agreed == numerical_problem) {
89 std::forward<Recover>(recover)(outcome == numerical_problem, std::as_const(reason));
90 return true;
91 }
92 return false;
93}
94
109template<class Wells, class GetCommunication, class Solve, class Recover>
111 GetCommunication&& get_communication,
112 Solve&& solve,
113 Recover&& recover)
114{
115 bool any_recovered = false;
116 std::exception_ptr deferred;
117 for (auto& well : wells) {
118 try {
119 const bool recovered = solveWithSharedRecovery(
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);
124 });
125 any_recovered = any_recovered || recovered;
126 } catch (...) {
127 if (!deferred) {
128 deferred = std::current_exception();
129 }
130 }
131 }
132 if (deferred) {
133 std::rethrow_exception(deferred);
134 }
135 return any_recovered;
136}
137
138} // namespace Opm
139
140#endif // OPM_WELL_SOLVE_RECOVERY_HPP
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