25#ifndef TPSA_NEWTON_METHOD_HPP
26#define TPSA_NEWTON_METHOD_HPP
28#include <opm/common/Exceptions.hpp>
30#include <opm/models/tpsa/tpsabaseproperties.hpp>
31#include <opm/models/tpsa/tpsanewtonmethodparams.hpp>
35#include <opm/simulators/linalg/PropertyTree.hpp>
56template <
class TypeTag>
72 using IstlMatrix =
typename SparseMatrixAdapter::IstlMatrix;
81 : simulator_(simulator)
82 , linearSolver_(simulator)
86 , numLinearizations_(0)
97 LinearSolverBackend::registerParameters();
109 prePostProcessTimer_.halt();
110 linearizeTimer_.halt();
115 SolutionVector& nextSolution =
model().solution(0);
116 SolutionVector currentSolution(nextSolution);
117 GlobalEqVector solutionUpdate(nextSolution.size());
119 Linearizer& linearizer =
model().linearizer();
121 TimerGuard prePostProcessTimerGuard(prePostProcessTimer_);
124 prePostProcessTimer_.start();
126 prePostProcessTimer_.stop();
129 TimerGuard innerPrePostProcessTimerGuard(prePostProcessTimer_);
130 TimerGuard linearizeTimerGuard(linearizeTimer_);
137 prePostProcessTimer_.start();
139 prePostProcessTimer_.stop();
142 currentSolution = nextSolution;
145 linearizeTimer_.start();
147 linearizeTimer_.stop();
151 auto& residual = linearizer.residual();
152 const auto& jacobian = linearizer.jacobian();
153 linearSolver_.prepare(jacobian, residual);
154 linearSolver_.getResidual(residual);
159 updateTimer_.start();
166 prePostProcessTimer_.start();
168 prePostProcessTimer_.stop();
175 solutionUpdate = 0.0;
176 const bool conv = linearSolver_.solve(solutionUpdate);
182 std::cout <<
"TPSA: Linear solver did not converge!" << std::endl;
185 prePostProcessTimer_.start();
187 prePostProcessTimer_.stop();
193 updateTimer_.start();
194 update_(nextSolution, currentSolution, solutionUpdate, residual);
198 prePostProcessTimer_.start();
200 prePostProcessTimer_.stop();
203 catch (
const Dune::Exception& e)
206 std::cout <<
"TPSA: Newton method caught exception: \""
207 << e.what() <<
"\"\n" << std::flush;
210 prePostProcessTimer_.start();
212 prePostProcessTimer_.stop();
216 catch (
const NumericalProblem& e)
219 std::cout <<
"TPSA: Newton method caught exception: \""
220 << e.what() <<
"\"\n" << std::flush;
223 prePostProcessTimer_.start();
225 prePostProcessTimer_.stop();
233 linearizeTimer_.realTimeElapsed() +
234 solveTimer_.realTimeElapsed() +
235 updateTimer_.realTimeElapsed();
236 const auto default_precision{std::cout.precision()};
237 std::cout << std::setprecision(2)
241 <<
"linearization = "
242 << linearizeTimer_.realTimeElapsed() <<
"s ("
243 << 100 * linearizeTimer_.realTimeElapsed() / elapsedTot <<
"%) | "
245 << solveTimer_.realTimeElapsed() <<
"s ("
246 << 100 * solveTimer_.realTimeElapsed() / elapsedTot <<
"%) | "
248 << updateTimer_.realTimeElapsed() <<
"s ("
249 << 100 * updateTimer_.realTimeElapsed() / elapsedTot <<
"%)"
250 <<
"\n" << std::flush;
251 std::cout << std::setprecision(default_precision);
256 prePostProcessTimer_.start();
259 std::cout <<
"TPSA: Newton iterations did not converge!" << std::endl;
261 prePostProcessTimer_.stop();
281 {
return simulator_.problem(); }
289 {
return simulator_.problem(); }
297 {
return simulator_.problem().geoMechModel(); }
305 {
return simulator_.problem().geoMechModel(); }
313 {
return linearSolver_; }
321 {
return linearSolver_; }
329 {
return numIterations_; }
337 {
return numLinearizations_; }
345 {
return params_.tolerance_; }
353 {
return params_.minIterations_; }
361 {
return prePostProcessTimer_; }
369 {
return linearizeTimer_; }
377 {
return solveTimer_; }
385 {
return updateTimer_; }
396 {
return simulator_.gridView().comm().rank() == 0 ? params_.verbosity_ : 0; }
404 numLinearizations_ = 0;
422 model().linearizer().linearizeDomain();
423 ++numLinearizations_;
433 const GlobalEqVector& currentResidual)
436 Scalar newtonMaxError = params_.maxError_;
440 const auto& elemMapper = simulator_.model().elementMapper();
441 for (
const auto& elem : elements(simulator_.gridView(), Dune::Partitions::interior)) {
442 unsigned dofIdx = elemMapper.index(elem);
443 const auto& r = currentResidual[dofIdx];
444 for (
unsigned eqIdx = 0; eqIdx < r.size(); ++eqIdx) {
445 error_ = max(std::abs(r[eqIdx] *
model().eqWeight(dofIdx, eqIdx)), error_);
450 error_ = simulator_.gridView().comm().max(error_);
453 if (error_ > newtonMaxError) {
454 throw NumericalProblem(
"TPSA: Newton error " + std::to_string(
double(error_)) +
455 " is larger than maximum allowed error of " +
456 std::to_string(
double(newtonMaxError)));
472 const SolutionVector& currentSolution,
473 const GlobalEqVector& solutionUpdate,
474 const GlobalEqVector& currentResidual)
477 if (!std::isfinite(solutionUpdate.one_norm())) {
478 throw NumericalProblem(
"TPSA: Non-finite update in Newton!");
481 std::size_t numGridDof =
model().numGridDof();
482 for (
unsigned dofIdx = 0; dofIdx < numGridDof; ++dofIdx) {
484 nextSolution[dofIdx],
485 currentSolution[dofIdx],
486 solutionUpdate[dofIdx],
487 currentResidual[dofIdx]);
491 model().updateMaterialState(0);
503 PrimaryVariables& nextValue,
504 const PrimaryVariables& currentValue,
505 const EqVector& update,
508 nextValue = currentValue;
522 std::cout <<
"TPSA: End Newton iteration " << numIterations_ <<
""
523 <<
" with error = " << error_
548 return error_ * 4.0 < lastError_;
558 { numIterations_ = params_.targetIterations_ * 2; }
561 LinearSolverBackend linearSolver_;
563 Timer prePostProcessTimer_;
564 Timer linearizeTimer_;
573 int numLinearizations_;
Manages the initializing and running of time dependent problems.
Definition simulator.hh:84
A simple class which makes sure that a timer gets stopped if an exception is thrown.
Definition timerguard.hh:42
Provides an encapsulation to measure the system time.
Definition timer.hpp:46
Scalar tolerance() const
Return the current tolerance at which the Newton method considers itself to be converged.
Definition tpsanewtonmethod.hpp:344
void update_(SolutionVector &nextSolution, const SolutionVector ¤tSolution, const GlobalEqVector &solutionUpdate, const GlobalEqVector ¤tResidual)
Update the current solution with a delta vector.
Definition tpsanewtonmethod.hpp:471
int numLinearizations() const
Returns the number of linearizations that has done since the Newton method was invoked.
Definition tpsanewtonmethod.hpp:336
Problem & problem()
Returns a reference to the object describing the current physical problem.
Definition tpsanewtonmethod.hpp:280
static void registerParameters()
Register all run-time parameters for the Newton method.
Definition tpsanewtonmethod.hpp:95
void endIteration_()
Indicates that one Newton iteration was finished.
Definition tpsanewtonmethod.hpp:515
const Timer & solveTimer() const
Return linear solver timer.
Definition tpsanewtonmethod.hpp:376
const Model & model() const
Returns a reference to the geomechanics model.
Definition tpsanewtonmethod.hpp:304
TpsaNewtonMethod(Simulator &simulator)
Constructor.
Definition tpsanewtonmethod.hpp:80
const Timer & linearizeTimer() const
Return linearization timer.
Definition tpsanewtonmethod.hpp:368
Scalar minIterations() const
Returns minimum number of Newton iterations used.
Definition tpsanewtonmethod.hpp:352
void beginIteration_()
Calculations at the beginning of a Newton iteration.
Definition tpsanewtonmethod.hpp:411
bool converged() const
Returns true if the error of the solution is below the tolerance.
Definition tpsanewtonmethod.hpp:272
bool proceed_() const
Returns true iff another Newton iteration should be done.
Definition tpsanewtonmethod.hpp:533
int numIterations() const
Returns the number of iterations done since the Newton method was invoked.
Definition tpsanewtonmethod.hpp:328
void linearizeDomain_()
Linearize the global non-linear system of equations associated with the spatial domain.
Definition tpsanewtonmethod.hpp:420
const LinearSolverBackend & linearSolver() const
Returns the linear solver backend object for external use.
Definition tpsanewtonmethod.hpp:320
void updatePrimaryVariables_(unsigned, PrimaryVariables &nextValue, const PrimaryVariables ¤tValue, const EqVector &update, const EqVector &)
Update a single primary variables object.
Definition tpsanewtonmethod.hpp:502
bool apply()
Run the Newton method.
Definition tpsanewtonmethod.hpp:106
LinearSolverBackend & linearSolver()
Returns the linear solver backend object for external use.
Definition tpsanewtonmethod.hpp:312
const Timer & prePostProcessTimer() const
Return post-process timer.
Definition tpsanewtonmethod.hpp:360
const Problem & problem() const
Returns a reference to the object describing the current physical problem.
Definition tpsanewtonmethod.hpp:288
Model & model()
Returns a reference to the geomechanics model.
Definition tpsanewtonmethod.hpp:296
void begin_()
Called before the Newton method is applied to an non-linear system of equations.
Definition tpsanewtonmethod.hpp:401
void failed_()
Called if the Newton method broke down.
Definition tpsanewtonmethod.hpp:557
void preSolve_(const SolutionVector &, const GlobalEqVector ¤tResidual)
Compute error before a Newton iteration.
Definition tpsanewtonmethod.hpp:432
const Timer & updateTimer() const
Return solution update timer.
Definition tpsanewtonmethod.hpp:384
int verbosity_() const
Verbosity level of Newton print messages.
Definition tpsanewtonmethod.hpp:395
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilbioeffectsmodules.hh:45
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
Struct holding the parameters for TpsaNewtonMethod.
Definition tpsanewtonmethodparams.hpp:60
static void registerParameters()
Register runtime parameters for TPSA Newton method.
Definition tpsanewtonmethodparams.cpp:39
Provides an encapsulation to measure the system time.
A simple class which makes sure that a timer gets stopped if an exception is thrown.