1#ifndef OPM_FLOW_NONLINEAR_SYSTEM_IMPL_HEADER_INCLUDED
2#define OPM_FLOW_NONLINEAR_SYSTEM_IMPL_HEADER_INCLUDED
21#ifndef OPM_FLOW_NONLINEAR_SYSTEM_HEADER_INCLUDED
26#include <dune/common/timer.hh>
28#include <opm/common/ErrorMacros.hpp>
29#include <opm/common/TimingMacros.hpp>
41template <
class TypeTag>
46 return assembleReservoir(well_model_);
49template <
class TypeTag>
54 applyTUNING(param_, tuning);
57template <
class TypeTag>
62 applyTUNINGDP(param_, tuning_dp);
65template <
class TypeTag>
70 OPM_TIMEBLOCK(updateSolution);
72 const bool shouldStore = shouldStoreSolutionUpdate();
74 prepareSolutionUpdate();
77 auto& newtonMethod = simulator_.model().newtonMethod();
78 auto& solution = simulator_.model().solution(0);
80 newtonMethod.applyUpdate(solution,
86 OPM_TIMEBLOCK(invalidateAndUpdateIntensiveQuantities);
87 simulator_.model().invalidateAndUpdateIntensiveQuantities(0);
91 storeSolutionUpdate(dx);
95template <
class TypeTag>
96template <
class LogFailure>
100 const int componentIdx,
101 const std::string_view componentName,
102 const std::span<const Scalar> residuals,
103 const std::span<const ConvergenceReport::ReservoirFailure::Type> types,
104 const std::span<const Scalar> tolerances,
105 const Scalar maxResidualAllowed,
106 LogFailure&& logFailure)
const
108 if (residuals.size() != types.size() || residuals.size() != tolerances.size()) {
109 OPM_THROW(std::logic_error,
"Mismatched reservoir convergence metric sizes.");
113 for (std::size_t metricIdx = 0; metricIdx < residuals.size(); ++metricIdx) {
114 const auto residual = residuals[metricIdx];
115 const auto type = types[metricIdx];
116 const auto tolerance = tolerances[metricIdx];
118 if (std::isnan(residual)) {
120 logFailure(
"NaN residual for " + std::string(componentName) +
" equation.");
122 else if (residual > maxResidualAllowed) {
124 logFailure(
"Too large residual for " + std::string(componentName) +
" equation.");
126 else if (residual < 0.0) {
128 logFailure(
"Negative residual for " + std::string(componentName) +
" equation.");
130 else if (residual > tolerance) {
138template <
class TypeTag>
143 const bool terminal_output)
144 : simulator_(simulator)
145 , grid_(simulator_.vanguard().grid())
146 , terminal_output_(terminal_output)
147 , enable_state_rollback_(Parameters::
Get<Parameters::EnableStateRollback>())
149 , well_model_(wellModel)
150 , current_relaxation_(1.0)
151 , dx_old_(simulator_.model().numGridDof())
154template <
class TypeTag>
164 Dune::Timer perfTimer;
169 report += this->assembleReservoir(well_model_);
174 simulator_.problem().markTimestepInitialized();
178 failureReport_ += report;
183template <
class TypeTag>
189 Dune::Timer perfTimer;
193 if (grid_.comm().size() > 1 && grid_.comm().max(lastStepFailed) != grid_.comm().min(lastStepFailed)) {
194 OPM_THROW(std::runtime_error,
195 "Misalignment of the parallel simulation run in prepareStep "
196 "- the previous step succeeded on some ranks but failed on others.");
199 if (lastStepFailed) {
202 simulator_.problem().updateFailed();
205 simulator_.problem().advanceTimeLevel();
212 simulator_.problem().resetIterationForNewTimestep();
213 simulator_.problem().beginTimeStep();
219template <
class TypeTag>
220template <
class WellModelType>
225 simulator_.problem().beginIteration();
226 simulator_.model().linearizer().linearizeDomain();
227 simulator_.problem().endIteration();
228 return wellModel.lastReport();
231template <
class TypeTag>
232template <
class ModelParametersType>
236 const Tuning& tuning)
238 param.tolerance_cnv_ = tuning.TRGCNV;
239 param.tolerance_cnv_relaxed_ = tuning.XXXCNV;
240 param.tolerance_mb_ = tuning.TRGMBE;
241 param.tolerance_mb_relaxed_ = tuning.XXXMBE;
242 param.newton_max_iter_ = tuning.NEWTMX;
243 param.newton_min_iter_ = tuning.NEWTMN;
246template <
class TypeTag>
247template <
class ModelParametersType>
251 const TuningDp& tuning_dp)
253 param.tolerance_max_dp_ = tuning_dp.TRGDDP;
254 param.tolerance_max_ds_ = tuning_dp.TRGDDS;
255 param.tolerance_max_drs_ = tuning_dp.TRGDDRS;
256 param.tolerance_max_drv_ = tuning_dp.TRGDDRV;
259template <
class TypeTag>
260template <
class ValueType>
261std::tuple<ValueType, ValueType>
264 const ValueType primaryVolumeLocal,
265 const ValueType secondaryVolumeLocal,
266 std::vector<ValueType>& sumValues,
267 std::vector<ValueType>& maxValues,
268 std::vector<ValueType>& averagedValues)
270 OPM_TIMEBLOCK(convergenceReduction);
272 ValueType primaryVolume = primaryVolumeLocal;
273 ValueType secondaryVolume = secondaryVolumeLocal;
275 if (comm.size() > 1) {
276 std::vector<ValueType> sumBuffer;
277 std::vector<ValueType> maxBuffer;
278 const int numComp = averagedValues.size();
279 sumBuffer.reserve(2 * numComp + 2);
280 maxBuffer.reserve(numComp);
282 for (
int compIdx = 0; compIdx < numComp; ++compIdx) {
283 sumBuffer.push_back(averagedValues[compIdx]);
284 sumBuffer.push_back(sumValues[compIdx]);
285 maxBuffer.push_back(maxValues[compIdx]);
288 sumBuffer.push_back(primaryVolume);
289 sumBuffer.push_back(secondaryVolume);
291 comm.sum(sumBuffer.data(), sumBuffer.size());
292 comm.max(maxBuffer.data(), maxBuffer.size());
294 for (
int compIdx = 0, buffIdx = 0; compIdx < numComp; ++compIdx, ++buffIdx) {
295 averagedValues[compIdx] = sumBuffer[buffIdx];
297 sumValues[compIdx] = sumBuffer[buffIdx];
300 for (
int compIdx = 0; compIdx < numComp; ++compIdx) {
301 maxValues[compIdx] = maxBuffer[compIdx];
304 primaryVolume = sumBuffer[sumBuffer.size() - 2];
305 secondaryVolume = sumBuffer.back();
308 return {primaryVolume, secondaryVolume};
Defines some fundamental parameters for all models.
Definition: ConvergenceReport.hpp:38
void setReservoirConvergenceMetric(Args &&... args)
Definition: ConvergenceReport.hpp:303
void setReservoirFailed(const ReservoirFailure &rf)
Definition: ConvergenceReport.hpp:290
void updateTUNINGDP(const TuningDp &tuning_dp)
Definition: NonlinearSystem_impl.hpp:60
void applyTUNINGDP(ModelParametersType ¶m, const TuningDp &tuning_dp)
Definition: NonlinearSystem_impl.hpp:250
std::tuple< ValueType, ValueType > convergenceReduction(Parallel::Communication comm, const ValueType primaryVolumeLocal, const ValueType secondaryVolumeLocal, std::vector< ValueType > &sumValues, std::vector< ValueType > &maxValues, std::vector< ValueType > &averagedValues)
Definition: NonlinearSystem_impl.hpp:263
void updateSolution(const GlobalEqVector &dx)
Definition: NonlinearSystem_impl.hpp:68
void addReservoirConvergenceMetrics(ConvergenceReport &report, const int componentIdx, const std::string_view componentName, const std::span< const Scalar > residuals, const std::span< const ConvergenceReport::ReservoirFailure::Type > types, const std::span< const Scalar > tolerances, const Scalar maxResidualAllowed, LogFailure &&logFailure) const
Definition: NonlinearSystem_impl.hpp:99
GetPropType< TypeTag, Properties::Scalar > Scalar
Definition: NonlinearSystem.hpp:51
virtual void initialLinearization(SimulatorReportSingle &report, int minIter, int maxIter, const SimulatorTimerInterface &timer)
Definition: NonlinearSystem_impl.hpp:157
void updateTUNING(const Tuning &tuning)
Definition: NonlinearSystem_impl.hpp:52
GetPropType< TypeTag, Properties::Simulator > Simulator
Definition: NonlinearSystem.hpp:47
void applyTUNING(ModelParametersType ¶m, const Tuning &tuning)
Definition: NonlinearSystem_impl.hpp:235
GetPropType< TypeTag, Properties::GlobalEqVector > GlobalEqVector
Definition: NonlinearSystem.hpp:52
SimulatorReportSingle prepareStep(const SimulatorTimerInterface &timer)
Definition: NonlinearSystem_impl.hpp:186
GetPropType< TypeTag, Properties::WellModel > WellModel
Definition: NonlinearSystem.hpp:54
NonlinearSystem(Simulator &simulator, const ModelParameters ¶m, WellModel &wellModel, const bool terminal_output)
Definition: NonlinearSystem_impl.hpp:140
SimulatorReportSingle assembleReservoir(const SimulatorTimerInterface &timer)
Definition: NonlinearSystem_impl.hpp:44
Interface class for SimulatorTimer objects, to be improved.
Definition: SimulatorTimerInterface.hpp:34
virtual bool lastStepFailed() const =0
Return true if last time step failed.
virtual double currentStepLength() const =0
virtual double simulationTimeElapsed() const =0
Dune::Communication< MPIComm > Communication
Definition: ParallelCommunication.hpp:30
auto Get(bool errorIfNotRegistered=true)
Retrieve a runtime parameter.
Definition: parametersystem.hpp:192
Definition: blackoilbioeffectsmodules.hh:45
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 assemble_time
Definition: SimulatorReport.hpp:39
double pre_post_time
Definition: SimulatorReport.hpp:40
unsigned int total_linearizations
Definition: SimulatorReport.hpp:49