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> 56 template <
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();
236 const auto default_precision{std::cout.precision()};
237 std::cout << std::setprecision(2)
241 <<
"linearization = " 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_;
void linearizeDomain_()
Linearize the global non-linear system of equations associated with the spatial domain.
Definition: tpsanewtonmethod.hpp:420
Model & model()
Returns a reference to the geomechanics model.
Definition: tpsanewtonmethod.hpp:296
Scalar minIterations() const
Returns minimum number of Newton iterations used.
Definition: tpsanewtonmethod.hpp:352
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
void updatePrimaryVariables_(unsigned, PrimaryVariables &nextValue, const PrimaryVariables ¤tValue, const EqVector &update, const EqVector &)
Update a single primary variables object.
Definition: tpsanewtonmethod.hpp:502
int numLinearizations() const
Returns the number of linearizations that has done since the Newton method was invoked.
Definition: tpsanewtonmethod.hpp:336
void beginIteration_()
Calculations at the beginning of a Newton iteration.
Definition: tpsanewtonmethod.hpp:411
Newton method solving for generic TPSA model.
Definition: tpsanewtonmethod.hpp:57
void start()
Start counting the time resources used by the simulation.
Definition: timer.cpp:46
const Timer & prePostProcessTimer() const
Return post-process timer.
Definition: tpsanewtonmethod.hpp:360
const Model & model() const
Returns a reference to the geomechanics model.
Definition: tpsanewtonmethod.hpp:304
Struct holding the parameters for TpsaNewtonMethod.
Definition: tpsanewtonmethodparams.hpp:59
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition: blackoilbioeffectsmodules.hh:45
const LinearSolverBackend & linearSolver() const
Returns the linear solver backend object for external use.
Definition: tpsanewtonmethod.hpp:320
void preSolve_(const SolutionVector &, const GlobalEqVector ¤tResidual)
Compute error before a Newton iteration.
Definition: tpsanewtonmethod.hpp:432
TpsaNewtonMethod(Simulator &simulator)
Constructor.
Definition: tpsanewtonmethod.hpp:80
static void registerParameters()
Register runtime parameters for TPSA Newton method.
Definition: tpsanewtonmethodparams.cpp:39
const Problem & problem() const
Returns a reference to the object describing the current physical problem.
Definition: tpsanewtonmethod.hpp:288
static void registerParameters()
Register all run-time parameters for the Newton method.
Definition: tpsanewtonmethod.hpp:95
Problem & problem()
Returns a reference to the object describing the current physical problem.
Definition: tpsanewtonmethod.hpp:280
double realTimeElapsed() const
Return the real time [s] elapsed during the periods the timer was active since the last reset...
Definition: timer.cpp:90
A simple class which makes sure that a timer gets stopped if an exception is thrown.
Definition: timerguard.hh:41
Scalar tolerance() const
Return the current tolerance at which the Newton method considers itself to be converged.
Definition: tpsanewtonmethod.hpp:344
Provides an encapsulation to measure the system time.
Definition: timer.hpp:45
LinearSolverBackend & linearSolver()
Returns the linear solver backend object for external use.
Definition: tpsanewtonmethod.hpp:312
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
const Timer & updateTimer() const
Return solution update timer.
Definition: tpsanewtonmethod.hpp:384
const Timer & solveTimer() const
Return linear solver timer.
Definition: tpsanewtonmethod.hpp:376
Provides an encapsulation to measure the system time.
int numIterations() const
Returns the number of iterations done since the Newton method was invoked.
Definition: tpsanewtonmethod.hpp:328
void endIteration_()
Indicates that one Newton iteration was finished.
Definition: tpsanewtonmethod.hpp:515
A simple class which makes sure that a timer gets stopped if an exception is thrown.
int verbosity_() const
Verbosity level of Newton print messages.
Definition: tpsanewtonmethod.hpp:395
bool proceed_() const
Returns true iff another Newton iteration should be done.
Definition: tpsanewtonmethod.hpp:533
void halt()
Stop the measurement reset all timing values.
Definition: timer.cpp:75
void begin_()
Called before the Newton method is applied to an non-linear system of equations.
Definition: tpsanewtonmethod.hpp:401
const Timer & linearizeTimer() const
Return linearization timer.
Definition: tpsanewtonmethod.hpp:368
bool apply()
Run the Newton method.
Definition: tpsanewtonmethod.hpp:106
Manages the initializing and running of time dependent problems.
Definition: simulator.hh:83
double stop()
Stop counting the time resources.
Definition: timer.cpp:52
bool converged() const
Returns true if the error of the solution is below the tolerance.
Definition: tpsanewtonmethod.hpp:272
void failed_()
Called if the Newton method broke down.
Definition: tpsanewtonmethod.hpp:557