10#ifndef BELOS_MINRES_ITER_HPP
11#define BELOS_MINRES_ITER_HPP
40#include "Teuchos_SerialDenseMatrix.hpp"
41#include "Teuchos_SerialDenseVector.hpp"
42#include "Teuchos_ScalarTraits.hpp"
43#include "Teuchos_ParameterList.hpp"
44#include "Teuchos_TimeMonitor.hpp"
61template<
class ScalarType,
class MV,
class OP>
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>
181 std::vector<MagnitudeType>& theNorms = *norms;
182 if (theNorms.size() < 1)
184 theNorms[0] = phibar_;
186 return Teuchos::null;
195 void symOrtho( ScalarType a, ScalarType b, ScalarType *c, ScalarType *s, ScalarType *r );
210 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
211 "Belos::MinresIter::setBlockSize(): Cannot use a block size that is not one.");
231 const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > lp_;
232 const Teuchos::RCP< OutputManager< ScalarType > > om_;
233 const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > 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_;
286 Teuchos::SerialDenseMatrix<int,ScalarType> beta1_;
292 template<
class ScalarType,
class MV,
class OP>
296 const Teuchos::ParameterList & ):
301 stateStorageInitialized_(false),
309 template <
class ScalarType,
class MV,
class OP>
310 void MinresIter<ScalarType,MV,OP>::setStateSize ()
312 if (!stateStorageInitialized_) {
315 Teuchos::RCP< const MV > lhsMV = lp_->getLHS();
316 Teuchos::RCP< const MV > rhsMV = lp_->getRHS();
317 if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
318 stateStorageInitialized_ =
false;
325 if (Y_ == Teuchos::null) {
327 Teuchos::RCP< const MV > tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
328 TEUCHOS_TEST_FOR_EXCEPTION( tmp == Teuchos::null,
329 std::invalid_argument,
330 "Belos::MinresIter::setStateSize(): linear problem does not specify multivectors to clone from.");
331 Y_ = MVT::Clone( *tmp, 1 );
332 R1_ = MVT::Clone( *tmp, 1 );
333 R2_ = MVT::Clone( *tmp, 1 );
334 W_ = MVT::Clone( *tmp, 1 );
335 W1_ = MVT::Clone( *tmp, 1 );
336 W2_ = MVT::Clone( *tmp, 1 );
339 stateStorageInitialized_ =
true;
347 template <
class ScalarType,
class MV,
class OP>
351 if (!stateStorageInitialized_)
354 TEUCHOS_TEST_FOR_EXCEPTION( !stateStorageInitialized_,
355 std::invalid_argument,
356 "Belos::MinresIter::initialize(): Cannot initialize state storage!" );
358 TEUCHOS_TEST_FOR_EXCEPTION( newstate.
Y == Teuchos::null,
359 std::invalid_argument,
360 "Belos::MinresIter::initialize(): MinresIterationState does not have initial residual.");
362 std::string errstr(
"Belos::MinresIter::initialize(): Specified multivectors must have a consistent length and width.");
364 std::invalid_argument,
367 std::invalid_argument,
371 const ScalarType one = SCT::one();
384 if ( lp_->getLeftPrec() != Teuchos::null ) {
385 lp_->applyLeftPrec( *newstate.
Y, *Y_ );
386 if ( lp_->getRightPrec() != Teuchos::null ) {
388 lp_->applyRightPrec( *tmp, *Y_ );
391 else if ( lp_->getRightPrec() != Teuchos::null ) {
392 lp_->applyRightPrec( *newstate.
Y, *Y_ );
395 if (newstate.
Y != Y_) {
402 beta1_ = Teuchos::SerialDenseMatrix<int,ScalarType>( 1, 1 );
405 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(beta1_(0,0)) < m_zero,
406 std::invalid_argument,
407 "The preconditioner is not positive definite." );
409 if( SCT::magnitude(beta1_(0,0)) == m_zero )
412 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
416 beta1_(0,0) = SCT::squareroot( beta1_(0,0) );
425 template <
class ScalarType,
class MV,
class OP>
431 if (initialized_ ==
false) {
436 const ScalarType one = SCT::one();
437 const ScalarType zero = SCT::zero();
441 Teuchos::SerialDenseMatrix<int,ScalarType> alpha( 1, 1 );
442 Teuchos::SerialDenseMatrix<int,ScalarType> beta( beta1_ );
443 phibar_ = Teuchos::ScalarTraits<ScalarType>::magnitude( beta1_(0,0) );
446 ScalarType oldBeta = zero;
447 ScalarType epsln = zero;
448 ScalarType cs = -one;
449 ScalarType sn = zero;
450 ScalarType dbar = zero;
461 Teuchos::RCP<MV> tmpY, tmpW;
464 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
469 "Belos::MinresIter::iterate(): current linear system has more than one vector!" );
474 while (stest_->checkStatus(
this) !=
Passed) {
484 lp_->applyOp (*V, *Y_);
493 MVT::MvAddMv (one, *Y_, -alpha(0,0)/beta(0,0), *R2_, *Y_);
503 if ( lp_->getLeftPrec() != Teuchos::null ) {
504 lp_->applyLeftPrec( *R2_, *Y_ );
505 if ( lp_->getRightPrec() != Teuchos::null ) {
507 lp_->applyRightPrec( *tmp, *Y_ );
510 else if ( lp_->getRightPrec() != Teuchos::null ) {
511 lp_->applyRightPrec( *R2_, *Y_ );
531 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(beta(0,0)) < m_zero,
533 "Belos::MinresIter::iterate(): Encountered negative "
534 "value " << beta(0,0) <<
" for r2^H*M*r2 at itera"
535 "tion " << iter_ <<
": MINRES cannot continue." );
536 beta(0,0) = SCT::squareroot( beta(0,0) );
544 delta = cs*dbar + sn*alpha(0,0);
545 gbar = sn*dbar - cs*alpha(0,0);
546 epsln = sn*beta(0,0);
547 dbar = - cs*beta(0,0);
550 this->
symOrtho(gbar, beta(0,0), &cs, &sn, &gamma);
553 phibar_ = Teuchos::ScalarTraits<ScalarType>::magnitude( sn * phibar_ );
570 MVT::MvAddMv( one, *cur_soln_vec, phi, *W_, *cur_soln_vec );
571 lp_->updateSolution();
581 template <
class ScalarType,
class MV,
class OP>
583 ScalarType *c, ScalarType *s, ScalarType *r
586 const ScalarType one = SCT::one();
587 const ScalarType zero = SCT::zero();
591 if ( absB == m_zero ) {
594 if ( absA == m_zero )
598 }
else if ( absA == m_zero ) {
602 }
else if ( absB >= absA ) {
603 ScalarType tau = a / b;
604 if ( Teuchos::ScalarTraits<ScalarType>::real(b) < m_zero )
605 *s = -one / SCT::squareroot( one+tau*tau );
607 *s = one / SCT::squareroot( one+tau*tau );
611 ScalarType tau = b / a;
612 if ( Teuchos::ScalarTraits<ScalarType>::real(a) < m_zero )
613 *c = -one / SCT::squareroot( one+tau*tau );
615 *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.