26 #ifndef EWOMS_NEWTON_METHOD_HH
27 #define EWOMS_NEWTON_METHOD_HH
31 #include <opm/common/Exceptions.hpp>
32 #include <opm/common/ErrorMacros.hpp>
34 #include <opm/material/common/ClassName.hpp>
38 #include <dune/istl/istlexception.hh>
39 #include <dune/common/version.hh>
40 #if DUNE_VERSION_NEWER(DUNE_COMMON, 2,3)
41 #include <dune/common/parallel/mpihelper.hh>
43 #include <dune/common/mpihelper.hh>
53 template <
class TypeTag>
59 namespace Properties {
161 template <
class TypeTag>
164 typedef typename GET_PROP_TYPE(TypeTag,
NewtonMethod) Implementation;
165 typedef typename GET_PROP_TYPE(TypeTag, Scalar) Scalar;
166 typedef typename GET_PROP_TYPE(TypeTag, Simulator) Simulator;
167 typedef typename GET_PROP_TYPE(TypeTag, Problem) Problem;
168 typedef typename GET_PROP_TYPE(TypeTag, Model) Model;
170 typedef typename GET_PROP_TYPE(TypeTag, SolutionVector) SolutionVector;
171 typedef typename GET_PROP_TYPE(TypeTag, GlobalEqVector) GlobalEqVector;
172 typedef typename GET_PROP_TYPE(TypeTag, PrimaryVariables) PrimaryVariables;
173 typedef typename GET_PROP_TYPE(TypeTag, EqVector) EqVector;
174 typedef typename GET_PROP_TYPE(TypeTag, Linearizer) Linearizer;
175 typedef typename GET_PROP_TYPE(TypeTag, JacobianMatrix) JacobianMatrix;
176 typedef typename GET_PROP_TYPE(TypeTag, LinearSolverBackend) LinearSolverBackend;
177 typedef typename GET_PROP_TYPE(TypeTag, NewtonConvergenceWriter) ConvergenceWriter;
179 typedef typename
Dune::MPIHelper::MPICommunicator Communicator;
180 typedef
Dune::CollectiveCommunication<Communicator> CollectiveCommunication;
200 LinearSolverBackend::registerParameters();
203 "Specify whether the Newton method should inform "
204 "the user about its progress or not");
206 "Write the convergence behaviour of the Newton "
207 "method to a VTK file");
209 "The 'optimimum' number of Newton iterations per "
212 "The maximum number of Newton iterations per time "
215 "The maximum raw error tolerated by the Newton"
216 "method for considering a solution to be "
219 "The maximum error tolerated by the Newton "
220 "method to which does not cause an abort");
286 const char *clearRemainingLine =
"\n";
287 if (isatty(fileno(stdout))) {
288 static const char blubb[] = { 0x1b,
'[',
'K',
'\r', 0 };
289 clearRemainingLine = blubb;
292 SolutionVector &nextSolution =
model().solution(0);
293 SolutionVector currentSolution(nextSolution);
294 GlobalEqVector solutionUpdate(nextSolution.size());
296 Linearizer &linearizer =
model().linearizer();
299 asImp_().begin_(nextSolution);
305 Timer prePostProcessTimer;
315 prePostProcessTimer.
start();
316 asImp_().beginIteration_();
317 prePostProcessTimer.
stop();
322 currentSolution = nextSolution;
324 if (asImp_().verbose_()) {
325 std::cout <<
"Linearize: r(x^k) = dS/dt + div F - q; M = grad r"
326 << clearRemainingLine
331 asImp_().linearize_();
337 std::cout <<
"Solve: M deltax^k = r"
338 << clearRemainingLine
348 auto b = linearizer.residual();
349 bool converged = asImp_().solveLinear_(linearizer.matrix(), solutionUpdate, b);
356 std::cout <<
"Newton: Newton solver did not converge\n" << std::flush;
359 prePostProcessTimer.
start();
361 prePostProcessTimer.
stop();
368 if (asImp_().verbose_()) {
369 std::cout <<
"Update: x^(k+1) = x^k - deltax^k"
370 << clearRemainingLine
377 asImp_().updateError_(nextSolution,
381 asImp_().update_(nextSolution, currentSolution, solutionUpdate, b);
387 prePostProcessTimer.
start();
388 asImp_().endIteration_(nextSolution, currentSolution);
389 prePostProcessTimer.
stop();
393 catch (
const Dune::Exception &e)
402 std::cout <<
"Newton method caught exception: \""
403 << e.what() <<
"\"\n" << std::flush;
407 catch (
const Opm::NumericalProblem &e)
417 std::cout <<
"Newton method caught exception: \""
418 << e.what() <<
"\"\n" << std::flush;
424 if (asImp_().
verbose_() && isatty(fileno(stdout)))
425 std::cout << clearRemainingLine
437 std::cout <<
"Linearization/solve/update time: "
442 << updateTime_ <<
"("
443 << 100 * updateTime_/elapsedTot <<
"%)"
444 <<
"\n" << std::flush;
455 asImp_().succeeded_();
500 return oldTimeStep / (1.0 + percent);
505 return oldTimeStep * (1.0 + percent / 1.2);
551 {
model().linearizer().linearize(); }
582 const SolutionVector ¤tSolution,
583 const GlobalEqVector ¤tResidual,
584 const GlobalEqVector &solutionUpdate)
591 for (
unsigned i = 0; i < currentResidual.size(); ++i) {
592 if (i >=
model().numGridDof() ||
model().dofTotalVolume(i) <= 0)
595 const auto &r = currentResidual[i];
596 for (
unsigned j = 0; j < r.size(); ++j)
606 OPM_THROW(Opm::NumericalProblem,
607 "Newton: Error " <<
error_
608 <<
" is larger than maximum allowed error of "
627 const SolutionVector ¤tSolution,
628 const GlobalEqVector &solutionUpdate,
629 const GlobalEqVector ¤tResidual)
633 asImp_().writeConvergence_(currentSolution, solutionUpdate);
636 if (!std::isfinite(solutionUpdate.two_norm2()))
637 OPM_THROW(Opm::NumericalProblem,
"Non-finite update!");
639 for (
unsigned dofIdx = 0; dofIdx < currentSolution.size(); ++dofIdx) {
640 asImp_().updatePrimaryVariables_(dofIdx,
641 nextSolution[dofIdx],
642 currentSolution[dofIdx],
643 solutionUpdate[dofIdx],
644 currentResidual[dofIdx]);
652 PrimaryVariables& nextValue,
653 const PrimaryVariables& currentValue,
654 const EqVector& update,
655 const EqVector& currentResidual)
657 nextValue = currentValue;
668 const GlobalEqVector &solutionUpdate)
684 const SolutionVector ¤tSolution)
785 Implementation &asImp_()
786 {
return *
static_cast<Implementation *
>(
this); }
787 const Implementation &asImp_()
const
788 {
return *
static_cast<const Implementation *
>(
this); }
void writeConvergence_(const SolutionVector ¤tSolution, const GlobalEqVector &solutionUpdate)
Write the convergence behaviour of the newton method to disk.
Definition: newtonmethod.hh:667
Problem & problem()
Returns a reference to the object describing the current physical problem.
Definition: newtonmethod.hh:233
A convergence writer for the Newton method which does nothing.
Definition: nullconvergencewriter.hh:47
void endIteration_(const SolutionVector &nextSolution, const SolutionVector ¤tSolution)
Indicates that one Newton iteration was finished.
Definition: newtonmethod.hh:683
Ewoms::Timer updateTimer_
Definition: newtonmethod.hh:758
void beginIteration_()
Indicates the beginning of a Newton iteration.
Definition: newtonmethod.hh:541
Scalar solveTime_
Definition: newtonmethod.hh:761
Scalar tolerance_
Definition: newtonmethod.hh:768
Simulator & simulator_
Definition: newtonmethod.hh:754
SET_SCALAR_PROP(NumericModel, EndTime,-1e100)
The default value for the simulation's end time.
Problem & problem()
Return the object which specifies the pysical setup of the simulation.
Definition: simulator.hh:189
Scalar solveTime() const
Returns the wall time spend so far for solving the linear systems for all iterations of the current t...
Definition: newtonmethod.hh:472
void halt()
Stop the measurement and always return 0 for all timing values.
Definition: timer.hh:86
Scalar suggestTimeStepSize(Scalar oldTimeStep) const
Suggest a new time-step size based on the old time-step size.
Definition: newtonmethod.hh:491
The multi-dimensional Newton method.
Definition: newtonmethod.hh:54
void end_()
Indicates that we're done solving the non-linear system of equations.
Definition: newtonmethod.hh:725
Scalar lastError_
Definition: newtonmethod.hh:767
Scalar updateTime_
Definition: newtonmethod.hh:762
static void registerParameters()
Register all run-time parameters for the Newton method.
Definition: newtonmethod.hh:198
bool proceed_() const
Returns true iff another Newton iteration should be done.
Definition: newtonmethod.hh:701
void begin_(const SolutionVector &u)
Called before the Newton method is applied to an non-linear system of equations.
Definition: newtonmethod.hh:530
std::ostringstream & endIterMsg()
Message that should be printed for the user after the end of an iteration.
Definition: newtonmethod.hh:512
Model & model()
Returns a reference to the numeric model.
Definition: newtonmethod.hh:245
void addPrePostProcessTime(Scalar value)
Definition: simulator.hh:623
SET_INT_PROP(NumericModel, GridGlobalRefinements, 0)
Provides an encapsulation to measure the system time.
Definition: cartesianindexmapper.hh:31
NEW_PROP_TAG(Grid)
The type of the DUNE grid.
This file provides the infrastructure to retrieve run-time parameters.
int targetIterations_() const
Definition: newtonmethod.hh:748
SET_TYPE_PROP(NumericModel, Scalar, double)
Set the default type of scalar values to double.
Scalar error_
Definition: newtonmethod.hh:766
bool apply()
Run the Newton method.
Definition: newtonmethod.hh:281
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:73
const Model & model() const
Returns a reference to the numeric model.
Definition: newtonmethod.hh:251
Definition: baseauxiliarymodule.hh:35
Ewoms::Timer solveTimer_
Definition: newtonmethod.hh:757
void succeeded_()
Called if the Newton method was successful.
Definition: newtonmethod.hh:744
void updateError_(const SolutionVector &nextSolution, const SolutionVector ¤tSolution, const GlobalEqVector ¤tResidual, const GlobalEqVector &solutionUpdate)
Update the error of the solution given the previous iteration.
Definition: newtonmethod.hh:581
Scalar tolerance() const
Return the current tolerance at which the Newton method considers itself to be converged.
Definition: newtonmethod.hh:265
ConvergenceWriter convergenceWriter_
Definition: newtonmethod.hh:782
int maxIterations_() const
Definition: newtonmethod.hh:751
NewtonMethod(Simulator &simulator)
Definition: newtonmethod.hh:183
#define EWOMS_REGISTER_PARAM(TypeTag, ParamType, ParamName, Description)
Register a run-time parameter.
Definition: parametersystem.hh:64
std::ostringstream endIterMsgStream_
Definition: newtonmethod.hh:764
Model & model()
Return the physical model used in the simulation.
Definition: simulator.hh:176
A convergence writer for the Newton method which does nothing.
void setTolerance(Scalar value)
Set the current tolerance at which the Newton method considers itself to be converged.
Definition: newtonmethod.hh:272
void update_(SolutionVector &nextSolution, const SolutionVector ¤tSolution, const GlobalEqVector &solutionUpdate, const GlobalEqVector ¤tResidual)
Update the current solution with a delta vector.
Definition: newtonmethod.hh:626
int numIterations() const
Returns the number of iterations done since the Newton method was invoked.
Definition: newtonmethod.hh:258
void linearize_()
Linearize the global non-linear system of equations.
Definition: newtonmethod.hh:550
void failed_()
Called if the Newton method broke down.
Definition: newtonmethod.hh:736
Scalar updateTime() const
Returns the wall time spend so far for updating the iterative solutions of the non-linear system for ...
Definition: newtonmethod.hh:480
Ewoms::Timer linearizeTimer_
Definition: newtonmethod.hh:756
LinearSolverBackend linearSolver_
Definition: newtonmethod.hh:774
Provides the magic behind the eWoms property system.
double realTimeElapsed() const
Return the real time [s] elapsed.
Definition: timer.hh:100
Provides an encapsulation to measure the system time.
Definition: timer.hh:47
bool converged() const
Returns true if the error of the solution is below the tolerance.
Definition: newtonmethod.hh:227
bool verbose_() const
Returns true if the Newton method ought to be chatty.
Definition: newtonmethod.hh:519
SET_BOOL_PROP(FvBaseDiscretization, EnableVtkOutput, true)
Enable the VTK output by default.
void updatePrimaryVariables_(int globalDofIdx, PrimaryVariables &nextValue, const PrimaryVariables ¤tValue, const EqVector &update, const EqVector ¤tResidual)
Update a single primary variables object.
Definition: newtonmethod.hh:651
bool solveLinear_(const JacobianMatrix &A, GlobalEqVector &x, GlobalEqVector &b)
Solve the linear system of equations .
Definition: newtonmethod.hh:563
void start()
Start counting the time resources used by the simulation.
Definition: timer.hh:68
CollectiveCommunication comm_
Definition: newtonmethod.hh:778
Scalar linearizeTime_
Definition: newtonmethod.hh:760
const Problem & problem() const
Returns a reference to the object describing the current physical problem.
Definition: newtonmethod.hh:239
int numIterations_
Definition: newtonmethod.hh:771
Scalar linearizeTime() const
Returns the wall time spend so far for linearizing the non-linear system for all iterations of the cu...
Definition: newtonmethod.hh:464
#define EWOMS_GET_PARAM(TypeTag, ParamType, ParamName)
Retrieve a runtime parameter.
Definition: parametersystem.hh:95
void stop()
Stop counting the time resources used by the simulation.
Definition: timer.hh:77