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 = DefaultDenseMatrix<
int,ScalarType>>
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, 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>
305 void MinresIter<ScalarType,MV,OP,DM>::setStateSize ()
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) {
322 Teuchos::RCP< const MV > tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
323 TEUCHOS_TEST_FOR_EXCEPTION( tmp == 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_)
349 TEUCHOS_TEST_FOR_EXCEPTION( !stateStorageInitialized_,
350 std::invalid_argument,
351 "Belos::MinresIter::initialize(): Cannot initialize state storage!" );
353 TEUCHOS_TEST_FOR_EXCEPTION( newstate.
Y == Teuchos::null,
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,
366 const ScalarType one = SCT::one();
379 if ( lp_->getLeftPrec() != Teuchos::null ) {
380 lp_->applyLeftPrec( *newstate.
Y, *Y_ );
381 if ( lp_->getRightPrec() != Teuchos::null ) {
383 lp_->applyRightPrec( *tmp, *Y_ );
386 else if ( lp_->getRightPrec() != Teuchos::null ) {
387 lp_->applyRightPrec( *newstate.
Y, *Y_ );
390 if (newstate.
Y != Y_) {
403 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(beta1_) < m_zero,
404 std::invalid_argument,
405 "The preconditioner is not positive definite." );
407 if( SCT::magnitude(beta1_) == m_zero )
410 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
414 beta1_ = SCT::squareroot( beta1_ );
423 template <
class ScalarType,
class MV,
class OP,
class DM>
429 if (initialized_ ==
false) {
434 const ScalarType one = SCT::one();
435 const ScalarType zero = SCT::zero();
439 ScalarType alpha, beta = beta1_;
440 phibar_ = Teuchos::ScalarTraits<ScalarType>::magnitude( beta1_ );
443 ScalarType oldBeta = zero;
444 ScalarType epsln = zero;
445 ScalarType cs = -one;
446 ScalarType sn = zero;
447 ScalarType dbar = zero;
458 Teuchos::RCP<MV> tmpY, tmpW;
461 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
466 "Belos::MinresIter::iterate(): current linear system has more than one vector!" );
471 while (stest_->checkStatus(
this) !=
Passed) {
481 lp_->applyOp (*V, *Y_);
502 if ( lp_->getLeftPrec() != Teuchos::null ) {
503 lp_->applyLeftPrec( *R2_, *Y_ );
504 if ( lp_->getRightPrec() != Teuchos::null ) {
506 lp_->applyRightPrec( *tmp, *Y_ );
509 else if ( lp_->getRightPrec() != Teuchos::null ) {
510 lp_->applyRightPrec( *R2_, *Y_ );
532 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(beta) < m_zero,
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 );
545 delta = cs*dbar + sn*alpha;
546 gbar = sn*dbar - cs*alpha;
551 this->
symOrtho(gbar, beta, &cs, &sn, &gamma);
554 phibar_ = Teuchos::ScalarTraits<ScalarType>::magnitude( sn * phibar_ );
571 MVT::MvAddMv( one, *cur_soln_vec, phi, *W_, *cur_soln_vec );
572 lp_->updateSolution();
582 template <
class ScalarType,
class MV,
class OP,
class DM>
584 ScalarType *c, ScalarType *s, ScalarType *r
587 const ScalarType one = SCT::one();
588 const ScalarType zero = SCT::zero();
592 if ( absB == m_zero ) {
595 if ( absA == m_zero )
599 }
else if ( absA == m_zero ) {
603 }
else if ( absB >= absA ) {
604 ScalarType tau = a / b;
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 );
612 ScalarType tau = b / a;
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.
Virtual base class which defines basic traits for the multi-vector type.
static const ScalarType & ValueConst(const DM &dm, const int i, const int j)
Access a const reference to the (i,j) entry of dm, e_i^T dm e_j.
static void SyncDeviceToHost(DM &dm)
static Teuchos::RCP< DM > Create()
Creates a new empty DM with no dimension.
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
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 ...
static void MvInit(MV &mv, const ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
static Teuchos::RCP< MV > CloneCopy(const MV &mv)
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
static void Assign(const MV &A, MV &mv)
static void MvTransMv(const ScalarType alpha, const MV &A, const MV &mv, DM &B)
static ptrdiff_t GetGlobalLength(const MV &mv)
static void MvScale(MV &mv, const ScalarType alpha)
static int GetNumberVecs(const MV &mv)
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.