NonlinearSystemCompositional_impl.hpp
Go to the documentation of this file.
1/*
2 Copyright 2026, SINTEF Digital
3
4 This file is part of the Open Porous Media project (OPM).
5
6 OPM is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 OPM is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with OPM. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#ifndef OPM_NONLINEAR_SYSTEM_COMPOSITIONAL_IMPL_HEADER_INCLUDED
21#define OPM_NONLINEAR_SYSTEM_COMPOSITIONAL_IMPL_HEADER_INCLUDED
22
23#ifndef OPM_NONLINEAR_SYSTEM_COMPOSITIONAL_HEADER_INCLUDED
24#include <config.h>
26#endif
27
28#include <dune/common/timer.hh>
29
30#include <opm/common/ErrorMacros.hpp>
31#include <opm/common/OpmLog/OpmLog.hpp>
32
34
37
38#include <algorithm>
39#include <array>
40#include <cmath>
41#include <stdexcept>
42
43namespace Opm {
44
45template <class TypeTag>
48 const ModelParameters& param,
49 CompWellModel<TypeTag>& wellModel,
50 const bool terminalOutput)
51 : ParentType(simulator, param, wellModel, terminalOutput)
52{
54 || Parameters::Get<Parameters::AcceleratorMode>() != "none") {
55 OPM_THROW(std::invalid_argument,
56 "Compositional runs solve the linear systems on the CPU only."
57 " Please use --linear-solver-accelerator=cpu and --accelerator-mode=none.");
58 }
59
60 this->convergence_reports_.reserve(64);
61}
62
63template <class TypeTag>
67{
69 Dune::Timer perfTimer;
70 perfTimer.start();
71
72 const int lastStepFailed = timer.lastStepFailed();
73 if (this->grid_.comm().size() > 1
74 && this->grid_.comm().max(lastStepFailed) != this->grid_.comm().min(lastStepFailed)) {
75 OPM_THROW(std::runtime_error,
76 "Misalignment of the parallel simulation run in prepareStep "
77 "- the previous step succeeded on some ranks but failed on others.");
78 }
79
80 if (lastStepFailed) {
81 this->wellModel().restoreLastValidState();
82 this->simulator_.model().updateFailed();
83 }
84 else {
85 this->simulator_.model().advanceTimeLevel();
86 }
87
88 this->simulator_.setTime(timer.simulationTimeElapsed());
89 this->simulator_.setTimeStepSize(timer.currentStepLength());
90
91 this->simulator_.problem().resetIterationForNewTimestep();
92 this->simulator_.problem().beginTimeStep();
93
94 report.pre_post_time += perfTimer.stop();
95 return report;
96}
97
98template <class TypeTag>
99void
102 const int minIter,
103 const int maxIter,
104 const SimulatorTimerInterface& timer)
105{
106 ParentType::initialLinearization(report,
107 minIter,
108 maxIter,
109 timer);
110 Dune::Timer perfTimer;
111 perfTimer.start();
112
113 auto convrep = ConvergenceReport{timer.simulationTimeElapsed()};
114 const auto residualMetrics = this->reservoirResidualMetrics();
115 const auto tolerance = this->simulator_.model().newtonMethod().tolerance();
116
117 for (int compIdx = 0; compIdx < numEq; ++compIdx) {
118 const std::array<Scalar, 1> residual{residualMetrics[compIdx]};
119 const std::array<ConvergenceReport::ReservoirFailure::Type, 1> types{
121 };
122 const std::array<Scalar, 1> tolerances{tolerance};
123
124 this->addReservoirConvergenceMetrics(
125 convrep,
126 compIdx,
127 this->compNames_.name(compIdx),
128 residual,
129 types,
130 tolerances,
131 this->param_.max_residual_allowed_,
132 [this](const std::string& message)
133 {
134 if (this->terminal_output_) {
135 OpmLog::debug(message);
136 }
137 });
138 }
139
140 const auto severity = convrep.severityOfWorstFailure();
141 const bool wellConverged = this->wellModel().getWellConvergence();
142 report.converged = convrep.converged() && wellConverged &&
143 this->simulator_.problem().iterationContext().iteration() >= minIter;
144 // a well stopped or reopened here needs more iterations with its new status
145 if (report.converged && this->wellModel().updateWellStatusAtConvergence()) {
146 report.converged = false;
147 }
148
149 this->convergence_reports_.back().report.push_back(std::move(convrep));
150 report.update_time += perfTimer.stop();
151 this->residual_norms_history_.push_back(residualMetrics);
152
154 this->failureReport_ += report;
155 OPM_THROW_PROBLEM(NumericalProblem, "NaN residual found!");
156 }
157
159 this->failureReport_ += report;
160 OPM_THROW_NOLOG(NumericalProblem, "Too large residual found!");
161 }
162}
163
164template <class TypeTag>
165template <class NonlinearSolverType>
166SimulatorReportSingle
169 NonlinearSolverType& nonlinearSolver)
170{
171 if (this->simulator_.problem().iterationContext().needsTimestepInit()) {
172 this->residual_norms_history_.clear();
173 this->current_relaxation_ = 1.0;
174 this->dx_old_ = 0.0;
175 this->convergence_reports_.push_back({timer.reportStepNum(), timer.currentStepNum(), {}});
176 this->convergence_reports_.back().report.reserve(numEq);
177 }
178
179 auto result = this->nonlinearIterationNewton(timer, nonlinearSolver);
180 this->simulator_.problem().advanceIteration();
181 return result;
182}
183
184template <class TypeTag>
185template <class NonlinearSolverType>
189 NonlinearSolverType& nonlinearSolver)
190{
191 OPM_TIMEFUNCTION();
192
194 Dune::Timer perfTimer;
195
196 this->initialLinearization(report,
197 this->param_.newton_min_iter_,
198 this->param_.newton_max_iter_,
199 timer);
200
201 if (!report.converged) {
202 perfTimer.reset();
203 perfTimer.start();
204 report.total_newton_iterations = 1;
205
206 BVector x(this->simulator_.model().numGridDof());
207 this->linear_solve_setup_time_ = 0.0;
208
209 try {
210 auto& linearizer = this->simulator_.model().linearizer();
211 linearizer.linearizeAuxiliaryEquations();
212 linearizer.finalize();
213
214 this->solveJacobianSystem(x);
215
216 report.linear_solve_setup_time += this->linear_solve_setup_time_;
217 report.linear_solve_time += perfTimer.stop();
218 report.total_linear_iterations += this->linearIterationsLastSolve();
219 }
220 catch (...) {
221 report.linear_solve_setup_time += this->linear_solve_setup_time_;
222 report.linear_solve_time += perfTimer.stop();
223 report.total_linear_iterations += this->linearIterationsLastSolve();
224
225 this->failureReport_ += report;
226 throw;
227 }
228
229 perfTimer.reset();
230 perfTimer.start();
231
232 auto& model = this->simulator_.model();
233 for (unsigned auxModIdx = 0; auxModIdx < model.numAuxiliaryModules(); ++auxModIdx) {
234 model.auxiliaryModule(auxModIdx)->postSolve(x);
235 }
236
237 if (this->param_.use_update_stabilization_) {
238 bool isOscillate = false;
239 bool isStagnate = false;
240 nonlinearSolver.detectOscillations(this->residual_norms_history_,
241 this->residual_norms_history_.size() - 1,
242 isOscillate,
243 isStagnate);
244
245 if (isOscillate) {
246 this->current_relaxation_ -= nonlinearSolver.relaxIncrement();
247 this->current_relaxation_ = std::max(this->current_relaxation_, nonlinearSolver.relaxMax());
248
249 if (this->terminalOutputEnabled()) {
250 OpmLog::info(" Oscillating behavior detected: Relaxation set to "
251 + std::to_string(this->current_relaxation_));
252 }
253 }
254
255 nonlinearSolver.stabilizeNonlinearUpdate(x, this->dx_old_, this->current_relaxation_);
256 }
257
258 this->updateSolution(x);
259 report.update_time += perfTimer.stop();
260 }
261
262 return report;
263}
264
265template <class TypeTag>
268relativeChange() const
269{
270 Scalar resultDelta = 0.0;
271 Scalar resultDenom = 0.0;
272
273 const auto& elemMapper = this->simulator_.model().elementMapper();
274 const auto& gridView = this->simulator_.gridView();
275
276 for (const auto& elem : elements(gridView, Dune::Partitions::interior)) {
277 const unsigned globalElemIdx = elemMapper.index(elem);
278 const auto& priVarsNew = this->simulator_.model().solution(/*timeIdx=*/0)[globalElemIdx];
279 const auto& priVarsOld = this->simulator_.model().solution(/*timeIdx=*/1)[globalElemIdx];
280
281 for (int pvIdx = 0; pvIdx < static_cast<int>(priVarsNew.size()); ++pvIdx) {
282 const auto delta = priVarsNew[pvIdx] - priVarsOld[pvIdx];
283 resultDelta += delta * delta;
284 resultDenom += priVarsNew[pvIdx] * priVarsNew[pvIdx];
285 }
286 }
287
288 resultDelta = gridView.comm().sum(resultDelta);
289 resultDenom = gridView.comm().sum(resultDenom);
290
291 return resultDenom > 0.0 ? resultDelta / resultDenom : 0.0;
292}
293
294template <class TypeTag>
295void
298{
299 auto& jacobian = this->simulator_.model().linearizer().jacobian();
300 auto& residual = this->simulator_.model().linearizer().residual();
301 auto& linSolver = this->simulator_.model().newtonMethod().linearSolver();
302
303 // Checked per solve, since wells can open during the run.
304 checkUmfpackMatrixAddWell(linSolver.activeSolverPrm(),
305 this->param_.matrix_add_well_contributions_,
306 !this->wellModel().empty());
307
308 x = 0.0;
309
310 Dune::Timer perfTimer;
311 perfTimer.start();
312 linSolver.prepare(jacobian, residual);
313 this->linear_solve_setup_time_ = perfTimer.stop();
314 linSolver.setResidual(residual);
315 linSolver.getResidual(residual);
316 linSolver.setMatrix(jacobian);
317 linSolver.solve(x);
318}
319
320template <class TypeTag>
321std::vector<typename NonlinearSystemCompositional<TypeTag>::Scalar>
324{
325 const auto& model = this->simulator_.model();
326 const auto& residual = model.linearizer().residual();
327 const auto& constraintsMap = model.linearizer().constraintsMap();
328
329 std::vector<Scalar> residualMetrics(numEq, 0.0);
330
331 // Ghost-cell residuals may omit fluxes from neighbors beyond the overlap.
332 // Use the owning rank's residual to assess convergence.
333 const auto& elemMapper = model.elementMapper();
334 for (const auto& elem : elements(this->simulator_.gridView(), Dune::Partitions::interior)) {
335 const unsigned dofIdx = elemMapper.index(elem);
336 if (dofIdx >= model.numGridDof() || model.dofTotalVolume(dofIdx) <= 0.0) {
337 continue;
338 }
339
340 if (constraintsMap.count(dofIdx) > 0) {
341 continue;
342 }
343
344 const auto& localResidual = residual[dofIdx];
345 for (int eqIdx = 0; eqIdx < numEq; ++eqIdx) {
346 residualMetrics[eqIdx] = std::max(
347 residualMetrics[eqIdx],
348 std::abs(localResidual[eqIdx] * model.eqWeight(dofIdx, eqIdx)));
349 }
350 }
351
352 if (this->grid_.comm().size() > 1 && !residualMetrics.empty()) {
353 this->grid_.comm().max(residualMetrics.data(), residualMetrics.size());
354 }
355
356 return residualMetrics;
357}
358
359} // namespace Opm
360
361#endif // OPM_NONLINEAR_SYSTEM_COMPOSITIONAL_IMPL_HEADER_INCLUDED
Definition: ConvergenceReport.hpp:38
Definition: NonlinearSystemCompositional.hpp:42
SimulatorReportSingle prepareStep(const SimulatorTimerInterface &timer)
Definition: NonlinearSystemCompositional_impl.hpp:66
void initialLinearization(SimulatorReportSingle &report, int minIter, int maxIter, const SimulatorTimerInterface &timer) override
Definition: NonlinearSystemCompositional_impl.hpp:101
typename ParentType::Scalar Scalar
Definition: NonlinearSystemCompositional.hpp:49
NonlinearSystemCompositional(Simulator &simulator, const ModelParameters &param, CompWellModel< TypeTag > &wellModel, bool terminalOutput)
Definition: NonlinearSystemCompositional_impl.hpp:47
Dune::BlockVector< VectorBlockType > BVector
Definition: NonlinearSystemCompositional.hpp:57
Definition: NonlinearSystem.hpp:44
std::vector< StepReport > convergence_reports_
Definition: NonlinearSystem.hpp:170
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: NonlinearSystem.hpp:51
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: NonlinearSystem.hpp:47
Interface class for SimulatorTimer objects, to be improved.
Definition: SimulatorTimerInterface.hpp:34
virtual int reportStepNum() const
Current report step number. This might differ from currentStepNum in case of sub stepping.
Definition: SimulatorTimerInterface.hpp:109
virtual bool lastStepFailed() const =0
Return true if last time step failed.
virtual double currentStepLength() const =0
virtual double simulationTimeElapsed() const =0
virtual int currentStepNum() const =0
LinearSolverAcceleratorType linearSolverAcceleratorTypeFromCLI()
Converts the CLI option for linear solver accelerator to a LinearSolverAcceleratorType.
Definition: LinearSolverAcceleratorType.hpp:89
Definition: blackoilbioeffectsmodules.hh:45
void checkUmfpackMatrixAddWell(const PropertyTree &prm, bool matrixAddWellContributions, bool hasWells)
std::string to_string(const ConvergenceReport::ReservoirFailure::Type t)
This file provides the infrastructure to retrieve run-time parameters.
Solver parameters for the NonlinearSystemBlackOilReservoir.
Definition: BlackoilModelParameters.hpp:208
A struct for returning timing data from a simulator to its caller.
Definition: SimulatorReport.hpp:34
double linear_solve_time
Definition: SimulatorReport.hpp:43
bool converged
Definition: SimulatorReport.hpp:57
double pre_post_time
Definition: SimulatorReport.hpp:40
double linear_solve_setup_time
Definition: SimulatorReport.hpp:42
unsigned int total_newton_iterations
Definition: SimulatorReport.hpp:50
double update_time
Definition: SimulatorReport.hpp:45
unsigned int total_linear_iterations
Definition: SimulatorReport.hpp:51