2018-06-06 03:59:41 -05:00
|
|
|
/*
|
|
|
|
Copyright 2015 SINTEF ICT, Applied Mathematics.
|
|
|
|
Copyright 2015 Statoil ASA.
|
|
|
|
|
|
|
|
This file is part of the Open Porous Media project (OPM).
|
|
|
|
|
|
|
|
OPM is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
OPM is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with OPM. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef OPM_NON_LINEAR_SOLVER_EBOS_HPP
|
|
|
|
#define OPM_NON_LINEAR_SOLVER_EBOS_HPP
|
|
|
|
|
2019-06-20 03:30:16 -05:00
|
|
|
#include <opm/simulators/timestepping/SimulatorReport.hpp>
|
2018-06-06 03:59:41 -05:00
|
|
|
#include <opm/common/ErrorMacros.hpp>
|
2018-06-06 03:59:41 -05:00
|
|
|
#include <opm/simulators/timestepping/SimulatorTimerInterface.hpp>
|
2018-06-21 05:14:17 -05:00
|
|
|
|
2019-09-16 03:58:20 -05:00
|
|
|
#include <opm/models/utils/parametersystem.hh>
|
|
|
|
#include <opm/models/utils/propertysystem.hh>
|
2020-08-28 03:44:10 -05:00
|
|
|
#include <opm/models/utils/basicproperties.hh>
|
2022-10-19 00:23:07 -05:00
|
|
|
#include <opm/models/nonlinear/newtonmethodproperties.hh>
|
2020-09-21 10:43:09 -05:00
|
|
|
#include <opm/common/Exceptions.hpp>
|
2018-06-21 05:14:17 -05:00
|
|
|
|
2018-06-06 03:59:41 -05:00
|
|
|
#include <dune/common/fmatrix.hh>
|
|
|
|
#include <dune/istl/bcrsmatrix.hh>
|
|
|
|
#include <memory>
|
|
|
|
|
2020-08-21 06:42:08 -05:00
|
|
|
namespace Opm::Properties {
|
2018-06-21 05:14:17 -05:00
|
|
|
|
2020-08-27 03:30:29 -05:00
|
|
|
namespace TTag {
|
|
|
|
struct FlowNonLinearSolver {};
|
|
|
|
}
|
2018-06-21 05:14:17 -05:00
|
|
|
|
2020-08-27 04:38:38 -05:00
|
|
|
template<class TypeTag, class MyTypeTag>
|
|
|
|
struct NewtonMaxRelax {
|
|
|
|
using type = UndefinedProperty;
|
|
|
|
};
|
2022-10-19 00:23:07 -05:00
|
|
|
|
|
|
|
// we are reusing NewtonMaxIterations from opm-models
|
|
|
|
// See opm/models/nonlinear/newtonmethodproperties.hh
|
|
|
|
|
2020-08-27 04:38:38 -05:00
|
|
|
template<class TypeTag, class MyTypeTag>
|
2022-09-30 06:52:18 -05:00
|
|
|
struct NewtonMinIterations{
|
2020-08-27 04:38:38 -05:00
|
|
|
using type = UndefinedProperty;
|
|
|
|
};
|
|
|
|
template<class TypeTag, class MyTypeTag>
|
|
|
|
struct NewtonRelaxationType{
|
|
|
|
using type = UndefinedProperty;
|
|
|
|
};
|
2018-06-21 05:14:17 -05:00
|
|
|
|
2020-08-27 04:38:38 -05:00
|
|
|
template<class TypeTag>
|
|
|
|
struct NewtonMaxRelax<TypeTag, TTag::FlowNonLinearSolver> {
|
|
|
|
using type = GetPropType<TypeTag, Scalar>;
|
|
|
|
static constexpr type value = 0.5;
|
|
|
|
};
|
2020-08-27 04:38:38 -05:00
|
|
|
template<class TypeTag>
|
2022-10-19 00:23:07 -05:00
|
|
|
struct NewtonMaxIterations<TypeTag, TTag::FlowNonLinearSolver> {
|
2020-08-27 04:38:38 -05:00
|
|
|
static constexpr int value = 20;
|
|
|
|
};
|
|
|
|
template<class TypeTag>
|
2022-09-30 06:52:18 -05:00
|
|
|
struct NewtonMinIterations<TypeTag, TTag::FlowNonLinearSolver> {
|
2020-08-27 04:38:38 -05:00
|
|
|
static constexpr int value = 1;
|
|
|
|
};
|
2020-08-27 04:38:38 -05:00
|
|
|
template<class TypeTag>
|
|
|
|
struct NewtonRelaxationType<TypeTag, TTag::FlowNonLinearSolver> {
|
|
|
|
static constexpr auto value = "dampen";
|
|
|
|
};
|
2018-06-21 05:14:17 -05:00
|
|
|
|
2020-08-21 06:42:08 -05:00
|
|
|
} // namespace Opm::Properties
|
2018-06-21 05:14:17 -05:00
|
|
|
|
2018-06-06 03:59:41 -05:00
|
|
|
namespace Opm {
|
|
|
|
|
2023-03-01 03:23:32 -06:00
|
|
|
class WellState;
|
|
|
|
|
|
|
|
// Available relaxation scheme types.
|
|
|
|
enum class NonlinearRelaxType {
|
|
|
|
Dampen,
|
|
|
|
SOR
|
|
|
|
};
|
|
|
|
|
2023-03-01 03:23:32 -06:00
|
|
|
namespace detail {
|
|
|
|
|
|
|
|
/// Detect oscillation or stagnation in a given residual history.
|
|
|
|
void detectOscillations(const std::vector<std::vector<double>>& residualHistory,
|
|
|
|
const int it, const int numPhases, const double relaxRelTol,
|
|
|
|
bool& oscillate, bool& stagnate);
|
|
|
|
|
2023-03-01 03:23:32 -06:00
|
|
|
/// Apply a stabilization to dx, depending on dxOld and relaxation parameters.
|
|
|
|
/// Implemention for Dune block vectors.
|
|
|
|
template <class BVector>
|
|
|
|
void stabilizeNonlinearUpdate(BVector& dx, BVector& dxOld,
|
|
|
|
const double omega, NonlinearRelaxType relaxType);
|
2018-06-06 03:59:41 -05:00
|
|
|
|
2023-03-01 03:23:32 -06:00
|
|
|
}
|
2023-03-01 03:30:42 -06:00
|
|
|
|
2018-06-06 03:59:41 -05:00
|
|
|
/// A nonlinear solver class suitable for general fully-implicit models,
|
|
|
|
/// as well as pressure, transport and sequential models.
|
2018-06-21 05:14:17 -05:00
|
|
|
template <class TypeTag, class PhysicalModel>
|
2018-06-06 03:59:41 -05:00
|
|
|
class NonlinearSolverEbos
|
|
|
|
{
|
2020-08-26 03:49:52 -05:00
|
|
|
using Scalar = GetPropType<TypeTag, Properties::Scalar>;
|
2018-06-21 05:14:17 -05:00
|
|
|
|
2018-06-06 03:59:41 -05:00
|
|
|
public:
|
|
|
|
// Solver parameters controlling nonlinear process.
|
2018-06-21 05:14:17 -05:00
|
|
|
struct SolverParameters
|
2018-06-06 03:59:41 -05:00
|
|
|
{
|
2023-03-01 03:30:42 -06:00
|
|
|
NonlinearRelaxType relaxType_;
|
2018-06-06 03:59:41 -05:00
|
|
|
double relaxMax_;
|
|
|
|
double relaxIncrement_;
|
|
|
|
double relaxRelTol_;
|
|
|
|
int maxIter_; // max nonlinear iterations
|
|
|
|
int minIter_; // min nonlinear iterations
|
|
|
|
|
2018-06-21 05:14:17 -05:00
|
|
|
SolverParameters()
|
2018-06-06 03:59:41 -05:00
|
|
|
{
|
|
|
|
// set default values
|
|
|
|
reset();
|
|
|
|
|
|
|
|
// overload with given parameters
|
2018-08-06 08:59:35 -05:00
|
|
|
relaxMax_ = EWOMS_GET_PARAM(TypeTag, Scalar, NewtonMaxRelax);
|
2022-10-19 00:23:07 -05:00
|
|
|
maxIter_ = EWOMS_GET_PARAM(TypeTag, int, NewtonMaxIterations);
|
2022-09-30 06:52:18 -05:00
|
|
|
minIter_ = EWOMS_GET_PARAM(TypeTag, int, NewtonMinIterations);
|
2018-06-06 03:59:41 -05:00
|
|
|
|
2018-08-06 08:59:35 -05:00
|
|
|
const auto& relaxationTypeString = EWOMS_GET_PARAM(TypeTag, std::string, NewtonRelaxationType);
|
2018-06-21 05:14:17 -05:00
|
|
|
if (relaxationTypeString == "dampen") {
|
2023-03-01 03:30:42 -06:00
|
|
|
relaxType_ = NonlinearRelaxType::Dampen;
|
2018-06-21 05:14:17 -05:00
|
|
|
} else if (relaxationTypeString == "sor") {
|
2023-03-01 03:30:42 -06:00
|
|
|
relaxType_ = NonlinearRelaxType::SOR;
|
2018-06-06 03:59:41 -05:00
|
|
|
} else {
|
2023-01-02 08:17:49 -06:00
|
|
|
OPM_THROW(std::runtime_error,
|
|
|
|
"Unknown Relaxtion Type " + relaxationTypeString);
|
2018-06-06 03:59:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-21 05:14:17 -05:00
|
|
|
static void registerParameters()
|
|
|
|
{
|
2022-10-19 14:15:59 -05:00
|
|
|
EWOMS_REGISTER_PARAM(TypeTag, Scalar, NewtonMaxRelax, "The maximum relaxation factor of a Newton iteration");
|
2022-10-19 14:03:52 -05:00
|
|
|
EWOMS_REGISTER_PARAM(TypeTag, int, NewtonMaxIterations, "The maximum number of Newton iterations per time step");
|
2022-10-19 14:15:59 -05:00
|
|
|
EWOMS_REGISTER_PARAM(TypeTag, int, NewtonMinIterations, "The minimum number of Newton iterations per time step");
|
|
|
|
EWOMS_REGISTER_PARAM(TypeTag, std::string, NewtonRelaxationType, "The type of relaxation used by Newton method");
|
2018-06-21 05:14:17 -05:00
|
|
|
}
|
2018-06-06 03:59:41 -05:00
|
|
|
|
|
|
|
void reset()
|
|
|
|
{
|
|
|
|
// default values for the solver parameters
|
2023-03-01 03:30:42 -06:00
|
|
|
relaxType_ = NonlinearRelaxType::Dampen;
|
2018-06-06 03:59:41 -05:00
|
|
|
relaxMax_ = 0.5;
|
|
|
|
relaxIncrement_ = 0.1;
|
|
|
|
relaxRelTol_ = 0.2;
|
|
|
|
maxIter_ = 10;
|
|
|
|
minIter_ = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
// Forwarding types from PhysicalModel.
|
2021-05-20 06:32:18 -05:00
|
|
|
//typedef typename PhysicalModel::WellState WellState;
|
2018-06-06 03:59:41 -05:00
|
|
|
|
|
|
|
// --------- Public methods ---------
|
|
|
|
|
|
|
|
/// Construct solver for a given model.
|
|
|
|
///
|
|
|
|
/// The model is a std::unique_ptr because the object to which model points to is
|
|
|
|
/// not allowed to be deleted as long as the NonlinearSolver object exists.
|
|
|
|
///
|
|
|
|
/// \param[in] param parameters controlling nonlinear process
|
|
|
|
/// \param[in, out] model physical simulation model.
|
2018-06-21 05:14:17 -05:00
|
|
|
NonlinearSolverEbos(const SolverParameters& param,
|
2018-06-06 03:59:41 -05:00
|
|
|
std::unique_ptr<PhysicalModel> model)
|
|
|
|
: param_(param)
|
|
|
|
, model_(std::move(model))
|
|
|
|
, linearizations_(0)
|
|
|
|
, nonlinearIterations_(0)
|
|
|
|
, linearIterations_(0)
|
|
|
|
, wellIterations_(0)
|
|
|
|
, nonlinearIterationsLast_(0)
|
|
|
|
, linearIterationsLast_(0)
|
|
|
|
, wellIterationsLast_(0)
|
|
|
|
{
|
|
|
|
if (!model_) {
|
|
|
|
OPM_THROW(std::logic_error, "Must provide a non-null model argument for NonlinearSolver.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-07 09:13:39 -05:00
|
|
|
SimulatorReportSingle step(const SimulatorTimerInterface& timer)
|
2018-06-06 03:59:41 -05:00
|
|
|
{
|
2020-05-07 09:13:39 -05:00
|
|
|
SimulatorReportSingle report;
|
|
|
|
report.global_time = timer.simulationTimeElapsed();
|
2020-05-07 14:19:31 -05:00
|
|
|
report.timestep_length = timer.currentStepLength();
|
|
|
|
|
2018-06-06 03:59:41 -05:00
|
|
|
// Do model-specific once-per-step calculations.
|
2020-12-10 06:39:01 -06:00
|
|
|
report += model_->prepareStep(timer);
|
2018-06-06 03:59:41 -05:00
|
|
|
|
|
|
|
int iteration = 0;
|
|
|
|
|
|
|
|
// Let the model do one nonlinear iteration.
|
|
|
|
|
|
|
|
// Set up for main solver loop.
|
|
|
|
bool converged = false;
|
|
|
|
|
|
|
|
// ---------- Main nonlinear solver loop ----------
|
|
|
|
do {
|
|
|
|
try {
|
|
|
|
// Do the nonlinear step. If we are in a converged state, the
|
|
|
|
// model will usually do an early return without an expensive
|
|
|
|
// solve, unless the minIter() count has not been reached yet.
|
2020-05-07 14:19:31 -05:00
|
|
|
auto iterReport = model_->nonlinearIteration(iteration, timer, *this);
|
2020-05-07 09:13:39 -05:00
|
|
|
iterReport.global_time = timer.simulationTimeElapsed();
|
2018-06-06 03:59:41 -05:00
|
|
|
report += iterReport;
|
|
|
|
report.converged = iterReport.converged;
|
|
|
|
|
|
|
|
converged = report.converged;
|
|
|
|
iteration += 1;
|
|
|
|
}
|
|
|
|
catch (...) {
|
|
|
|
// if an iteration fails during a time step, all previous iterations
|
|
|
|
// count as a failure as well
|
2020-05-07 14:19:31 -05:00
|
|
|
failureReport_ = report;
|
2018-06-06 03:59:41 -05:00
|
|
|
failureReport_ += model_->failureReport();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ( (!converged && (iteration <= maxIter())) || (iteration <= minIter()));
|
|
|
|
|
|
|
|
if (!converged) {
|
2020-05-07 15:09:17 -05:00
|
|
|
failureReport_ = report;
|
2018-06-06 03:59:41 -05:00
|
|
|
|
|
|
|
std::string msg = "Solver convergence failure - Failed to complete a time step within " + std::to_string(maxIter()) + " iterations.";
|
2021-05-05 04:22:44 -05:00
|
|
|
OPM_THROW_NOLOG(TooManyIterations, msg);
|
2018-06-06 03:59:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do model-specific post-step actions.
|
2020-12-10 06:39:01 -06:00
|
|
|
report += model_->afterStep(timer);
|
2018-06-06 03:59:41 -05:00
|
|
|
report.converged = true;
|
2020-05-07 09:13:39 -05:00
|
|
|
return report;
|
2018-06-06 03:59:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// return the statistics if the step() method failed
|
2020-05-07 09:13:39 -05:00
|
|
|
const SimulatorReportSingle& failureReport() const
|
2018-06-06 03:59:41 -05:00
|
|
|
{ return failureReport_; }
|
|
|
|
|
|
|
|
/// Number of linearizations used in all calls to step().
|
|
|
|
int linearizations() const
|
|
|
|
{ return linearizations_; }
|
|
|
|
|
|
|
|
/// Number of full nonlinear solver iterations used in all calls to step().
|
|
|
|
int nonlinearIterations() const
|
|
|
|
{ return nonlinearIterations_; }
|
|
|
|
|
|
|
|
/// Number of linear solver iterations used in all calls to step().
|
|
|
|
int linearIterations() const
|
|
|
|
{ return linearIterations_; }
|
|
|
|
|
|
|
|
/// Number of well iterations used in all calls to step().
|
|
|
|
int wellIterations() const
|
|
|
|
{ return wellIterations_; }
|
|
|
|
|
|
|
|
/// Number of nonlinear solver iterations used in the last call to step().
|
|
|
|
int nonlinearIterationsLastStep() const
|
|
|
|
{ return nonlinearIterationsLast_; }
|
|
|
|
|
|
|
|
/// Number of linear solver iterations used in the last call to step().
|
|
|
|
int linearIterationsLastStep() const
|
|
|
|
{ return linearIterationsLast_; }
|
|
|
|
|
|
|
|
/// Number of well iterations used in all calls to step().
|
|
|
|
int wellIterationsLastStep() const
|
|
|
|
{ return wellIterationsLast_; }
|
|
|
|
|
|
|
|
std::vector<std::vector<double> >
|
|
|
|
computeFluidInPlace(const std::vector<int>& fipnum) const
|
|
|
|
{ return model_->computeFluidInPlace(fipnum); }
|
|
|
|
|
|
|
|
/// Reference to physical model.
|
|
|
|
const PhysicalModel& model() const
|
|
|
|
{ return *model_; }
|
|
|
|
|
|
|
|
/// Mutable reference to physical model.
|
|
|
|
PhysicalModel& model()
|
|
|
|
{ return *model_; }
|
|
|
|
|
|
|
|
/// Detect oscillation or stagnation in a given residual history.
|
|
|
|
void detectOscillations(const std::vector<std::vector<double>>& residualHistory,
|
|
|
|
const int it, bool& oscillate, bool& stagnate) const
|
|
|
|
{
|
2023-03-01 03:23:32 -06:00
|
|
|
detail::detectOscillations(residualHistory, it, model_->numPhases(),
|
|
|
|
this->relaxRelTol(), oscillate, stagnate);
|
2018-06-06 03:59:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// Apply a stabilization to dx, depending on dxOld and relaxation parameters.
|
|
|
|
/// Implemention for Dune block vectors.
|
|
|
|
template <class BVector>
|
|
|
|
void stabilizeNonlinearUpdate(BVector& dx, BVector& dxOld, const double omega) const
|
|
|
|
{
|
2023-03-01 03:23:32 -06:00
|
|
|
detail::stabilizeNonlinearUpdate(dx, dxOld, omega, this->relaxType());
|
2018-06-06 03:59:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The greatest relaxation factor (i.e. smallest factor) allowed.
|
|
|
|
double relaxMax() const
|
|
|
|
{ return param_.relaxMax_; }
|
|
|
|
|
|
|
|
/// The step-change size for the relaxation factor.
|
|
|
|
double relaxIncrement() const
|
|
|
|
{ return param_.relaxIncrement_; }
|
|
|
|
|
|
|
|
/// The relaxation type (Dampen or SOR).
|
2023-03-01 03:30:42 -06:00
|
|
|
NonlinearRelaxType relaxType() const
|
2018-06-06 03:59:41 -05:00
|
|
|
{ return param_.relaxType_; }
|
|
|
|
|
|
|
|
/// The relaxation relative tolerance.
|
|
|
|
double relaxRelTol() const
|
|
|
|
{ return param_.relaxRelTol_; }
|
|
|
|
|
|
|
|
/// The maximum number of nonlinear iterations allowed.
|
|
|
|
int maxIter() const
|
|
|
|
{ return param_.maxIter_; }
|
|
|
|
|
|
|
|
/// The minimum number of nonlinear iterations allowed.
|
|
|
|
int minIter() const
|
|
|
|
{ return param_.minIter_; }
|
|
|
|
|
|
|
|
/// Set parameters to override those given at construction time.
|
2018-06-21 05:14:17 -05:00
|
|
|
void setParameters(const SolverParameters& param)
|
2018-06-06 03:59:41 -05:00
|
|
|
{ param_ = param; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
// --------- Data members ---------
|
2020-05-07 09:13:39 -05:00
|
|
|
SimulatorReportSingle failureReport_;
|
2018-06-21 05:14:17 -05:00
|
|
|
SolverParameters param_;
|
2018-06-06 03:59:41 -05:00
|
|
|
std::unique_ptr<PhysicalModel> model_;
|
|
|
|
int linearizations_;
|
|
|
|
int nonlinearIterations_;
|
|
|
|
int linearIterations_;
|
|
|
|
int wellIterations_;
|
|
|
|
int nonlinearIterationsLast_;
|
|
|
|
int linearIterationsLast_;
|
|
|
|
int wellIterationsLast_;
|
|
|
|
};
|
2023-03-01 03:23:32 -06:00
|
|
|
|
2018-06-06 03:59:41 -05:00
|
|
|
} // namespace Opm
|
|
|
|
|
|
|
|
#endif // OPM_NON_LINEAR_SOLVER_EBOS_HPP
|