10#ifndef BELOS_MINRES_ITER_HPP
11#define BELOS_MINRES_ITER_HPP
41#include "Teuchos_ScalarTraits.hpp"
42#include "Teuchos_ParameterList.hpp"
43#include "Teuchos_TimeMonitor.hpp"
60template<
class ScalarType,
class MV,
class OP,
class DM>
71 typedef Teuchos::ScalarTraits< ScalarType >
SCT;
73 typedef Teuchos::ScalarTraits< MagnitudeType >
SMT;
89 const Teuchos::ParameterList&
params);
150 throw std::logic_error(
"getState() cannot be called unless "
151 "the state has been initialized");
176 Teuchos::RCP<const MV>
186 return Teuchos::null;
211 "Belos::MinresIter::setBlockSize(): Cannot use a block size that is not one.");
231 const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM> > lp_;
232 const Teuchos::RCP< OutputManager< ScalarType > > om_;
233 const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > stest_;
251 bool stateStorageInitialized_;
267 Teuchos::RCP< MV > Y_;
269 Teuchos::RCP< MV > R1_;
271 Teuchos::RCP< MV > R2_;
273 Teuchos::RCP< MV > W_;
275 Teuchos::RCP< MV > W1_;
277 Teuchos::RCP< MV > W2_;
281 Teuchos::RCP<DM> tmpDM;
287 template<
class ScalarType,
class MV,
class OP,
class DM>
291 const Teuchos::ParameterList & ):
296 stateStorageInitialized_(
false),
304 template <
class ScalarType,
class MV,
class OP,
class DM>
307 if (!stateStorageInitialized_) {
310 Teuchos::RCP< const MV >
lhsMV = lp_->getLHS();
311 Teuchos::RCP< const MV >
rhsMV = lp_->getRHS();
312 if (
lhsMV == Teuchos::null &&
rhsMV == Teuchos::null) {
313 stateStorageInitialized_ =
false;
320 if (Y_ == Teuchos::null) {
324 std::invalid_argument,
325 "Belos::MinresIter::setStateSize(): linear problem does not specify multivectors to clone from.");
326 Y_ = MVT::Clone( *
tmp, 1 );
327 R1_ = MVT::Clone( *
tmp, 1 );
328 R2_ = MVT::Clone( *
tmp, 1 );
329 W_ = MVT::Clone( *
tmp, 1 );
330 W1_ = MVT::Clone( *
tmp, 1 );
331 W2_ = MVT::Clone( *
tmp, 1 );
334 stateStorageInitialized_ =
true;
342 template <
class ScalarType,
class MV,
class OP,
class DM>
346 if (!stateStorageInitialized_)
350 std::invalid_argument,
351 "Belos::MinresIter::initialize(): Cannot initialize state storage!" );
354 std::invalid_argument,
355 "Belos::MinresIter::initialize(): MinresIterationState does not have initial residual.");
357 std::string
errstr(
"Belos::MinresIter::initialize(): Specified multivectors must have a consistent length and width.");
359 std::invalid_argument,
362 std::invalid_argument,
377 MVT::MvInit ( *W2_ );
379 if ( lp_->getLeftPrec() != Teuchos::null ) {
380 lp_->applyLeftPrec( *
newstate.Y, *Y_ );
381 if ( lp_->getRightPrec() != Teuchos::null ) {
382 Teuchos::RCP<MV>
tmp = MVT::CloneCopy( *Y_ );
383 lp_->applyRightPrec( *
tmp, *Y_ );
386 else if ( lp_->getRightPrec() != Teuchos::null ) {
387 lp_->applyRightPrec( *
newstate.Y, *Y_ );
398 tmpDM = DMT::Create(1,1);
400 DMT::SyncDeviceToHost(*tmpDM);
401 beta1_ = DMT::ValueConst(*tmpDM,0,0);
404 std::invalid_argument,
405 "The preconditioner is not positive definite." );
407 if( SCT::magnitude(beta1_) ==
m_zero )
414 beta1_ = SCT::squareroot( beta1_ );
423 template <
class ScalarType,
class MV,
class OP,
class DM>
429 if (initialized_ ==
false) {
440 phibar_ = Teuchos::ScalarTraits<ScalarType>::magnitude( beta1_ );
457 Teuchos::RCP<MV> V = MVT::Clone( *Y_, 1 );
466 "Belos::MinresIter::iterate(): current linear system has more than one vector!" );
471 while (stest_->checkStatus(
this) !=
Passed) {
478 MVT::MvAddMv (
one / beta, *Y_,
zero, *Y_, *V);
481 lp_->applyOp (*V, *Y_);
484 MVT::MvAddMv (
one, *Y_, -beta/
oldBeta, *R1_, *Y_);
487 MVT::MvTransMv (
one, *V, *Y_, *tmpDM);
488 DMT::SyncDeviceToHost(*tmpDM);
489 alpha = DMT::ValueConst(*tmpDM,0,0);
492 MVT::MvAddMv (
one, *Y_, -alpha/beta, *R2_, *Y_);
502 if ( lp_->getLeftPrec() != Teuchos::null ) {
503 lp_->applyLeftPrec( *R2_, *Y_ );
504 if ( lp_->getRightPrec() != Teuchos::null ) {
505 Teuchos::RCP<MV>
tmp = MVT::CloneCopy( *Y_ );
506 lp_->applyRightPrec( *
tmp, *Y_ );
509 else if ( lp_->getRightPrec() != Teuchos::null ) {
510 lp_->applyRightPrec( *R2_, *Y_ );
513 MVT::Assign( *R2_, *Y_ );
518 MVT::MvTransMv(
one, *R2_, *Y_, *tmpDM);
519 DMT::SyncDeviceToHost(*tmpDM);
520 beta = DMT::ValueConst(*tmpDM,0,0);
534 "Belos::MinresIter::iterate(): Encountered negative "
535 "value " << beta <<
" for r2^H*M*r2 at itera"
536 "tion " << iter_ <<
": MINRES cannot continue." );
537 beta = SCT::squareroot( beta );
551 this->symOrtho(
gbar, beta, &cs, &sn, &
gamma);
554 phibar_ = Teuchos::ScalarTraits<ScalarType>::magnitude( sn * phibar_ );
558 MVT::Assign( *W_, *W1_ );
565 MVT::MvAddMv(
one, *V, -
oldeps, *W1_, *W_ );
566 MVT::MvAddMv(
one, *W_, -
delta, *W2_, *W_ );
572 lp_->updateSolution();
582 template <
class ScalarType,
class MV,
class OP,
class DM>
605 if ( Teuchos::ScalarTraits<ScalarType>::real(
b) <
m_zero )
606 *
s = -
one / SCT::squareroot(
one+tau*tau );
608 *
s =
one / SCT::squareroot(
one+tau*tau );
613 if ( Teuchos::ScalarTraits<ScalarType>::real(
a) <
m_zero )
614 *
c = -
one / SCT::squareroot(
one+tau*tau );
616 *
c =
one / SCT::squareroot(
one+tau*tau );
Belos header file which uses auto-configuration information to include necessary C++ headers.
Class which describes the linear problem to be solved by the iterative solver.
Pure virtual base class which augments the basic interface for a minimal residual linear solver itera...
Declaration of basic traits for the multivector type.
Class which defines basic traits for the operator type.
Class which manages the output and verbosity of the Belos solvers.
Pure virtual base class for defining the status testing capabilities of Belos.
Collection of types and exceptions used within the Belos solvers.
SCT::magnitudeType MagnitudeType
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
Teuchos::ScalarTraits< MagnitudeType > SMT
int getNumIters() const
Get the current iteration count.
MultiVecTraits< ScalarType, MV, DM > MVT
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this linear problem.
DenseMatTraits< ScalarType, DM > DMT
const LinearProblem< ScalarType, MV, OP, DM > & getProblem() const
Get a constant reference to the linear problem.
MinresIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP, DM > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP, DM > > &tester, const Teuchos::ParameterList ¶ms)
Constructor.
virtual ~MinresIter()
Destructor.
Teuchos::ScalarTraits< ScalarType > SCT
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the residuals native to the solver.
void initialize()
Initialize the solver.
bool isInitialized()
States whether the solver has been initialized or not.
void symOrtho(ScalarType a, ScalarType b, ScalarType *c, ScalarType *s, ScalarType *r)
OperatorTraits< ScalarType, MV, OP > OPT
void initializeMinres(const MinresIterationState< ScalarType, MV > &newstate)
Initialize the solver to an iterate, providing a complete state.
void iterate()
Perform MINRES iterations until convergence or error.
MinresIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
bool isInitialized() const
States whether the solver has been initialized or not.
void resetNumIters(int iter=0)
Reset the iteration count.
MinresIterateFailure is thrown when the MinresIteration object is unable to compute the next iterate ...
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).