21 #ifndef OPM_NON_LINEAR_SOLVER_HPP 22 #define OPM_NON_LINEAR_SOLVER_HPP 24 #include <dune/common/fmatrix.hh> 25 #include <dune/istl/bcrsmatrix.hh> 27 #include <opm/common/ErrorMacros.hpp> 28 #include <opm/common/Exceptions.hpp> 29 #include <opm/common/TimingMacros.hpp> 31 #include <opm/models/nonlinear/newtonmethodparams.hpp> 32 #include <opm/models/nonlinear/newtonmethodproperties.hh> 37 #include <opm/simulators/timestepping/SimulatorReport.hpp> 38 #include <opm/simulators/timestepping/SimulatorTimerInterface.hpp> 39 #include <opm/simulators/timestepping/TimeStepControl.hpp> 45 template<
class Scalar>
55 enum class NonlinearRelaxType {
63 template<
class Scalar>
64 void detectOscillations(
const std::vector<std::vector<Scalar>>& residualHistory,
65 const int it,
const int numPhases,
const Scalar relaxRelTol,
66 const int minimumOscillatingPhases,
67 bool& oscillate,
bool& stagnate);
71 template <
class BVector,
class Scalar>
72 void stabilizeNonlinearUpdate(BVector& dx, BVector& dxOld,
73 const Scalar omega, NonlinearRelaxType relaxType);
78 template<
class Scalar>
81 NonlinearRelaxType relaxType_;
83 Scalar relaxIncrement_;
88 static void registerParameters();
95 template <
class TypeTag,
class PhysicalModel>
113 std::unique_ptr<PhysicalModel>
model)
115 , model_(std::move(
model))
117 , nonlinearIterations_(0)
118 , linearIterations_(0)
120 , nonlinearIterationsLast_(0)
121 , linearIterationsLast_(0)
122 , wellIterationsLast_(0)
125 OPM_THROW(std::logic_error,
"Must provide a non-null model argument for NonlinearSolver.");
138 report += model_->prepareStep(timer);
141 bool converged =
false;
149 auto iterReport = model_->nonlinearIteration(timer, *
this);
151 report += iterReport;
152 report.converged = iterReport.converged;
154 converged = report.converged;
159 failureReport_ = report;
160 failureReport_ += model_->failureReport();
164 while ( (!converged && (model_->simulator().problem().iterationContext().iteration() <= this->
model().param().newton_max_iter_)) ||
165 (model_->simulator().problem().iterationContext().iteration() <= this->
model().param().newton_min_iter_));
168 failureReport_ = report;
170 std::string msg =
"Solver convergence failure - Failed to complete a time step within ";
171 msg += std::to_string(
model().param().newton_max_iter_) +
" iterations.";
172 OPM_THROW_NOLOG(TooManyIterations, msg);
174 auto relativeChange = model_->relativeChange();
176 report.converged =
false;
177 report.time_step_rejected =
true;
178 failureReport_ = report;
180 std::string msg =
"Relative change in solution for time step was " + std::to_string(relativeChange);
181 msg +=
", which is larger than the tolerance accepted by the timestepping algorithm.";
182 OPM_THROW_NOLOG(TimeSteppingBreakdown, msg);
185 report.converged =
true;
191 {
return failureReport_; }
195 {
return linearizations_; }
199 {
return nonlinearIterations_; }
203 {
return linearIterations_; }
207 {
return wellIterations_; }
211 {
return nonlinearIterationsLast_; }
215 {
return linearIterationsLast_; }
219 {
return wellIterationsLast_; }
221 std::vector<std::vector<Scalar> >
222 computeFluidInPlace(
const std::vector<int>& fipnum)
const 223 {
return model_->computeFluidInPlace(fipnum); }
235 const int it,
bool& oscillate,
bool& stagnate)
const 237 detail::detectOscillations(residualHistory, it, model_->numPhases(),
244 template <
class BVector>
247 detail::stabilizeNonlinearUpdate(dx, dxOld, omega, this->
relaxType());
252 {
return param_.relaxMax_; }
256 {
return param_.relaxIncrement_; }
260 {
return param_.relaxType_; }
264 {
return param_.relaxRelTol_; }
273 SolverParameters param_;
274 std::unique_ptr<PhysicalModel> model_;
276 int nonlinearIterations_;
277 int linearIterations_;
279 int nonlinearIterationsLast_;
280 int linearIterationsLast_;
281 int wellIterationsLast_;
286 #endif // OPM_NON_LINEAR_SOLVER_HPP int nonlinearIterations() const
Number of full nonlinear solver iterations used in all calls to step().
Definition: NonlinearSolver.hpp:198
A nonlinear solver class suitable for general fully-implicit models, as well as pressure, transport and sequential models.
Definition: NonlinearSolver.hpp:96
int linearIterationsLastStep() const
Number of linear solver iterations used in the last call to step().
Definition: NonlinearSolver.hpp:214
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(...))
Definition: propertysystem.hh:233
Definition: NonlinearSolver.hpp:79
Definition: NonlinearSolver.hpp:48
Structs needed for tpfalinearizer and its gpuparams struct extracted to be defined in one place that ...
Definition: blackoilbioeffectsmodules.hh:45
int wellIterations() const
Number of well iterations used in all calls to step().
Definition: NonlinearSolver.hpp:206
Definition: blackoilnewtonmethodparams.hpp:31
Scalar relaxRelTol() const
The relaxation relative tolerance.
Definition: NonlinearSolver.hpp:263
int wellIterationsLastStep() const
Number of well iterations used in all calls to step().
Definition: NonlinearSolver.hpp:218
void stabilizeNonlinearUpdate(BVector &dx, BVector &dxOld, const Scalar omega) const
Apply a stabilization to dx, depending on dxOld and relaxation parameters.
Definition: NonlinearSolver.hpp:245
virtual double currentStepLength() const =0
Current step length.
int linearizations() const
Number of linearizations used in all calls to step().
Definition: NonlinearSolver.hpp:194
const SimulatorReportSingle & failureReport() const
return the statistics if the step() method failed
Definition: NonlinearSolver.hpp:190
Scalar relaxMax() const
The greatest relaxation factor (i.e. smallest factor) allowed.
Definition: NonlinearSolver.hpp:251
virtual bool timeStepAccepted(const double error, const double timeStepJustCompleted) const =0
For the general 3rd order controller, the internal shifting of errors and time steps happens here...
NonlinearRelaxType relaxType() const
The relaxation type (Dampen or SOR).
Definition: NonlinearSolver.hpp:259
const PhysicalModel & model() const
Reference to physical model.
Definition: NonlinearSolver.hpp:226
int linearIterations() const
Number of linear solver iterations used in all calls to step().
Definition: NonlinearSolver.hpp:202
A struct for returning timing data from a simulator to its caller.
Definition: SimulatorReport.hpp:33
NonlinearSolver(const SolverParameters ¶m, std::unique_ptr< PhysicalModel > model)
Construct solver for a given model.
Definition: NonlinearSolver.hpp:112
Interface class for SimulatorTimer objects, to be improved.
Definition: SimulatorTimerInterface.hpp:33
The Opm property system, traits with inheritance.
void detectOscillations(const std::vector< std::vector< Scalar >> &residualHistory, const int it, bool &oscillate, bool &stagnate) const
Detect oscillation or stagnation in a given residual history.
Definition: NonlinearSolver.hpp:234
void setParameters(const SolverParameters ¶m)
Set parameters to override those given at construction time.
Definition: NonlinearSolver.hpp:267
PhysicalModel & model()
Mutable reference to physical model.
Definition: NonlinearSolver.hpp:230
TimeStepControlInterface.
Definition: TimeStepControlInterface.hpp:50
Definition: NonlinearSolver.hpp:46
Scalar relaxIncrement() const
The step-change size for the relaxation factor.
Definition: NonlinearSolver.hpp:255
Defines a type tags and some fundamental properties all models.
virtual double simulationTimeElapsed() const =0
Time elapsed since the start of the simulation until the beginning of the current time step [s]...
int nonlinearIterationsLastStep() const
Number of nonlinear solver iterations used in the last call to step().
Definition: NonlinearSolver.hpp:210