42#ifndef BELOS_MINRES_ITER_HPP
43#define BELOS_MINRES_ITER_HPP
72#include "Teuchos_SerialDenseMatrix.hpp"
73#include "Teuchos_SerialDenseVector.hpp"
74#include "Teuchos_ScalarTraits.hpp"
75#include "Teuchos_ParameterList.hpp"
76#include "Teuchos_TimeMonitor.hpp"
93template<
class ScalarType,
class MV,
class OP>
103 typedef Teuchos::ScalarTraits< ScalarType >
SCT;
105 typedef Teuchos::ScalarTraits< MagnitudeType >
SMT;
121 const Teuchos::ParameterList& params);
182 throw std::logic_error(
"getState() cannot be called unless "
183 "the state has been initialized");
208 Teuchos::RCP<const MV>
213 std::vector<MagnitudeType>& theNorms = *norms;
214 if (theNorms.size() < 1)
216 theNorms[0] = phibar_;
218 return Teuchos::null;
227 void symOrtho( ScalarType a, ScalarType b, ScalarType *c, ScalarType *s, ScalarType *r );
242 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
243 "Belos::MinresIter::setBlockSize(): Cannot use a block size that is not one.");
263 const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > lp_;
264 const Teuchos::RCP< OutputManager< ScalarType > > om_;
265 const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > stest_;
283 bool stateStorageInitialized_;
299 Teuchos::RCP< MV > Y_;
301 Teuchos::RCP< MV > R1_;
303 Teuchos::RCP< MV > R2_;
305 Teuchos::RCP< MV > W_;
307 Teuchos::RCP< MV > W1_;
309 Teuchos::RCP< MV > W2_;
318 Teuchos::SerialDenseMatrix<int,ScalarType> beta1_;
324 template<
class ScalarType,
class MV,
class OP>
328 const Teuchos::ParameterList & ):
333 stateStorageInitialized_(false),
341 template <
class ScalarType,
class MV,
class OP>
342 void MinresIter<ScalarType,MV,OP>::setStateSize ()
344 if (!stateStorageInitialized_) {
347 Teuchos::RCP< const MV > lhsMV = lp_->getLHS();
348 Teuchos::RCP< const MV > rhsMV = lp_->getRHS();
349 if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
350 stateStorageInitialized_ =
false;
357 if (Y_ == Teuchos::null) {
359 Teuchos::RCP< const MV > tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
360 TEUCHOS_TEST_FOR_EXCEPTION( tmp == Teuchos::null,
361 std::invalid_argument,
362 "Belos::MinresIter::setStateSize(): linear problem does not specify multivectors to clone from.");
363 Y_ = MVT::Clone( *tmp, 1 );
364 R1_ = MVT::Clone( *tmp, 1 );
365 R2_ = MVT::Clone( *tmp, 1 );
366 W_ = MVT::Clone( *tmp, 1 );
367 W1_ = MVT::Clone( *tmp, 1 );
368 W2_ = MVT::Clone( *tmp, 1 );
371 stateStorageInitialized_ =
true;
379 template <
class ScalarType,
class MV,
class OP>
383 if (!stateStorageInitialized_)
386 TEUCHOS_TEST_FOR_EXCEPTION( !stateStorageInitialized_,
387 std::invalid_argument,
388 "Belos::MinresIter::initialize(): Cannot initialize state storage!" );
390 TEUCHOS_TEST_FOR_EXCEPTION( newstate.
Y == Teuchos::null,
391 std::invalid_argument,
392 "Belos::MinresIter::initialize(): MinresIterationState does not have initial residual.");
394 std::string errstr(
"Belos::MinresIter::initialize(): Specified multivectors must have a consistent length and width.");
396 std::invalid_argument,
399 std::invalid_argument,
403 const ScalarType one = SCT::one();
416 if ( lp_->getLeftPrec() != Teuchos::null ) {
417 lp_->applyLeftPrec( *newstate.
Y, *Y_ );
418 if ( lp_->getRightPrec() != Teuchos::null ) {
420 lp_->applyRightPrec( *tmp, *Y_ );
423 else if ( lp_->getRightPrec() != Teuchos::null ) {
424 lp_->applyRightPrec( *newstate.
Y, *Y_ );
427 if (newstate.
Y != Y_) {
434 beta1_ = Teuchos::SerialDenseMatrix<int,ScalarType>( 1, 1 );
437 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(beta1_(0,0)) < m_zero,
438 std::invalid_argument,
439 "The preconditioner is not positive definite." );
441 if( SCT::magnitude(beta1_(0,0)) == m_zero )
444 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
448 beta1_(0,0) = SCT::squareroot( beta1_(0,0) );
457 template <
class ScalarType,
class MV,
class OP>
463 if (initialized_ ==
false) {
468 const ScalarType one = SCT::one();
469 const ScalarType zero = SCT::zero();
473 Teuchos::SerialDenseMatrix<int,ScalarType> alpha( 1, 1 );
474 Teuchos::SerialDenseMatrix<int,ScalarType> beta( beta1_ );
475 phibar_ = Teuchos::ScalarTraits<ScalarType>::magnitude( beta1_(0,0) );
478 ScalarType oldBeta = zero;
479 ScalarType epsln = zero;
480 ScalarType cs = -one;
481 ScalarType sn = zero;
482 ScalarType dbar = zero;
493 Teuchos::RCP<MV> tmpY, tmpW;
496 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
501 "Belos::MinresIter::iterate(): current linear system has more than one vector!" );
506 while (stest_->checkStatus(
this) !=
Passed) {
516 lp_->applyOp (*V, *Y_);
525 MVT::MvAddMv (one, *Y_, -alpha(0,0)/beta(0,0), *R2_, *Y_);
535 if ( lp_->getLeftPrec() != Teuchos::null ) {
536 lp_->applyLeftPrec( *R2_, *Y_ );
537 if ( lp_->getRightPrec() != Teuchos::null ) {
539 lp_->applyRightPrec( *tmp, *Y_ );
542 else if ( lp_->getRightPrec() != Teuchos::null ) {
543 lp_->applyRightPrec( *R2_, *Y_ );
563 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(beta(0,0)) < m_zero,
565 "Belos::MinresIter::iterate(): Encountered negative "
566 "value " << beta(0,0) <<
" for r2^H*M*r2 at itera"
567 "tion " << iter_ <<
": MINRES cannot continue." );
568 beta(0,0) = SCT::squareroot( beta(0,0) );
576 delta = cs*dbar + sn*alpha(0,0);
577 gbar = sn*dbar - cs*alpha(0,0);
578 epsln = sn*beta(0,0);
579 dbar = - cs*beta(0,0);
582 this->
symOrtho(gbar, beta(0,0), &cs, &sn, &gamma);
585 phibar_ = Teuchos::ScalarTraits<ScalarType>::magnitude( sn * phibar_ );
602 MVT::MvAddMv( one, *cur_soln_vec, phi, *W_, *cur_soln_vec );
603 lp_->updateSolution();
613 template <
class ScalarType,
class MV,
class OP>
615 ScalarType *c, ScalarType *s, ScalarType *r
618 const ScalarType one = SCT::one();
619 const ScalarType zero = SCT::zero();
623 if ( absB == m_zero ) {
626 if ( absA == m_zero )
630 }
else if ( absA == m_zero ) {
634 }
else if ( absB >= absA ) {
635 ScalarType tau = a / b;
636 if ( Teuchos::ScalarTraits<ScalarType>::real(b) < m_zero )
637 *s = -one / SCT::squareroot( one+tau*tau );
639 *s = one / SCT::squareroot( one+tau*tau );
643 ScalarType tau = b / a;
644 if ( Teuchos::ScalarTraits<ScalarType>::real(a) < m_zero )
645 *c = -one / SCT::squareroot( one+tau*tau );
647 *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.
void initializeMinres(const MinresIterationState< ScalarType, MV > &newstate)
Initialize the solver to an iterate, providing a complete state.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
MinresIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &tester, const Teuchos::ParameterList ¶ms)
Constructor.
bool isInitialized()
States whether the solver has been initialized or not.
int getNumIters() const
Get the current iteration count.
bool isInitialized() const
States whether the solver has been initialized or not.
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this linear problem.
MinresIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
MultiVecTraits< ScalarType, MV > MVT
Teuchos::ScalarTraits< MagnitudeType > SMT
void initialize()
Initialize the solver.
void symOrtho(ScalarType a, ScalarType b, ScalarType *c, ScalarType *s, ScalarType *r)
void iterate()
Perform MINRES iterations until convergence or error.
SCT::magnitudeType MagnitudeType
Teuchos::ScalarTraits< ScalarType > SCT
OperatorTraits< ScalarType, MV, OP > OPT
void resetNumIters(int iter=0)
Reset the iteration count.
virtual ~MinresIter()
Destructor.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
MinresIterateFailure is thrown when the MinresIteration object is unable to compute the next iterate ...
Traits class which defines basic operations on multivectors.
static void MvTransMv(const ScalarType alpha, const MV &A, const MV &mv, Teuchos::SerialDenseMatrix< int, ScalarType > &B)
Compute a dense matrix B through the matrix-matrix multiply .
static void MvScale(MV &mv, const ScalarType alpha)
Scale each element of the vectors in mv with alpha.
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
Creates a new MV and copies contents of mv into the new vector (deep copy).
static ptrdiff_t GetGlobalLength(const MV &mv)
Return the number of rows in the given multivector mv.
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
Replace mv with .
static void MvInit(MV &mv, const ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
Replace each element of the vectors in mv with alpha.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
static void Assign(const MV &A, MV &mv)
mv := A
Class which defines basic traits for the operator type.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
A pure virtual class for defining the status tests for the Belos iterative solvers.
Structure to contain pointers to MinresIteration state variables.
Teuchos::RCP< const MV > W
The current direction vector.
Teuchos::RCP< const MV > R2
Previous residual.
Teuchos::RCP< const MV > W1
Previous direction vector.
Teuchos::RCP< const MV > Y
The current residual.
Teuchos::RCP< const MV > W2
Previous direction vector.
Teuchos::RCP< const MV > R1
Previous residual.