42#ifndef BELOS_LSQR_ITER_HPP
43#define BELOS_LSQR_ITER_HPP
59#include "Teuchos_SerialDenseMatrix.hpp"
60#include "Teuchos_SerialDenseVector.hpp"
61#include "Teuchos_ScalarTraits.hpp"
62#include "Teuchos_ParameterList.hpp"
63#include "Teuchos_TimeMonitor.hpp"
74template<
class ScalarType,
class MV,
class OP>
84 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
99 Teuchos::ParameterList ¶ms );
181 Teuchos::RCP<const MV>
getNativeResiduals( std::vector<MagnitudeType> * )
const {
return Teuchos::null; }
203 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
204 "LSQRIter::setBlockSize(): Cannot use a block size that is not one.");
223 const Teuchos::RCP<Belos::LinearProblem<ScalarType,MV,OP> >
lp_;
224 const Teuchos::RCP<Belos::OutputManager<ScalarType> >
om_;
225 const Teuchos::RCP<Belos::StatusTest<ScalarType,MV,OP> >
stest_;
286 template<
class ScalarType,
class MV,
class OP>
290 Teuchos::ParameterList ¶ms ):
309 template <
class ScalarType,
class MV,
class OP>
314 Teuchos::RCP<const MV> rhsMV =
lp_->getInitPrecResVec();
315 Teuchos::RCP<const MV> lhsMV =
lp_->getLHS();
316 if (lhsMV == Teuchos::null || rhsMV == Teuchos::null) {
324 if (
U_ == Teuchos::null) {
326 TEUCHOS_TEST_FOR_EXCEPTION(rhsMV == Teuchos::null, std::invalid_argument,
"LSQRIter::setStateSize(): linear problem does not specify right hand multivector to clone from.");
327 TEUCHOS_TEST_FOR_EXCEPTION(lhsMV == Teuchos::null, std::invalid_argument,
"LSQRIter::setStateSize(): linear problem does not specify left hand multivector to clone from.");
343 template <
class ScalarType,
class MV,
class OP>
353 "LSQRIter::initialize(): Cannot initialize state storage!");
355 std::string errstr(
"LSQRIter::initialize(): Specified multivectors must have a consistent length and width.");
360 RCP<const MV> lhsMV =
lp_->getLHS();
362 const bool debugSerialLSQR =
false;
364 if( debugSerialLSQR )
366 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> lhsNorm(1);
368 std::cout <<
"initializeLSQR lhsNorm " << lhsNorm[0] << std::endl;
372 RCP<const MV> rhsMV =
lp_->getInitPrecResVec();
373 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
374 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
377 RCP<const OP> M_left =
lp_->getLeftPrec();
378 RCP<const OP> A =
lp_->getOperator();
379 RCP<const OP> M_right =
lp_->getRightPrec();
381 if( debugSerialLSQR )
383 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> rhsNorm(1);
385 std::cout <<
"initializeLSQR | U_ | : " << rhsNorm[0] << std::endl;
397 if ( M_left.is_null())
410 if (! M_right.is_null())
432 template <
class ScalarType,
class MV,
class OP>
443 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
444 const MagnitudeType MTzero = Teuchos::ScalarTraits<MagnitudeType>::zero();
445 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
448 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> alpha(1);
449 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> beta(1);
450 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> xi(1);
453 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> wnorm2(1);
454 ScalarType rhobar, phibar, cs1, phi, rho, cs, sn, theta, xxnorm = MTzero, common;
455 ScalarType zetabar, sn1, psi, res = zero, ddnorm = zero, gamma, tau;
456 ScalarType anorm2 = zero;
457 ScalarType cs2 = -one, sn2 = zero, gammabar, zeta = zero, delta;
462 Teuchos::RCP<MV> AtU;
464 const bool debugSerialLSQR =
false;
467 Teuchos::RCP<MV> cur_soln_vec =
lp_->getCurrLHSVec();
472 "LSQRIter::iterate(): current linear system has more than one vector!" );
477 if( SCT::real(beta[0]) > zero )
490 if( debugSerialLSQR )
494 std::cout <<
iter_ <<
" First alpha " << alpha[0] <<
" beta " << beta[0] <<
" lambda " <<
lambda_ << std::endl;
496 if( SCT::real(alpha[0]) > zero )
500 if( beta[0] * alpha[0] > zero )
510 RCP<const OP> M_left =
lp_->getLeftPrec();
511 RCP<const OP> A =
lp_->getOperator();
512 RCP<const OP> M_right =
lp_->getRightPrec();
532 if ( M_right.is_null() )
543 if (! M_left.is_null())
550 if ( !( M_left.is_null() && M_right.is_null() )
551 && debugSerialLSQR &&
iter_ == 1)
557 if (! M_left.is_null())
567 if ( !( M_right.is_null() ) )
575 std::cout <<
"| V alpha - A' u |= " << xi[0] << std::endl;
577 Teuchos::SerialDenseMatrix<int,ScalarType> uotuo(1,1);
579 std::cout <<
"<U, U> = " << printMat(uotuo) << std::endl;
581 std::cout <<
"alpha = " << alpha[0] << std::endl;
583 Teuchos::SerialDenseMatrix<int,ScalarType> utav(1,1);
585 std::cout <<
"<AV, U> = alpha = " << printMat(utav) << std::endl;
594 if ( SCT::real(beta[0]) > zero )
599 if (M_left.is_null())
609 if (! M_right.is_null())
623 if ( SCT::real(alpha[0]) > zero )
630 common = Teuchos::ScalarTraits< ScalarType >::squareroot(rhobar*rhobar +
lambda_*
lambda_);
631 cs1 = rhobar / common;
634 phibar = cs1 * phibar;
638 rho = Teuchos::ScalarTraits< ScalarType >::squareroot(rhobar*rhobar +
lambda_*
lambda_ + beta[0]*beta[0]);
641 theta = sn * alpha[0];
642 rhobar = -cs * alpha[0];
644 phibar = sn * phibar;
648 gammabar = -cs2 * rho;
649 zetabar = (phi - delta*zeta) / gammabar;
650 sol_norm_ = Teuchos::ScalarTraits< ScalarType >::squareroot(xxnorm + zetabar*zetabar);
651 gamma = Teuchos::ScalarTraits< ScalarType >::squareroot(gammabar*gammabar + theta*theta);
652 cs2 = gammabar / gamma;
654 zeta = (phi - delta*zeta) / gamma;
658 if ( M_right.is_null())
666 MVT::MvAddMv( phi / rho, *tempInDomainOfA, one, *cur_soln_vec, *cur_soln_vec);
670 ddnorm += (one / rho)*(one / rho) * wnorm2[0]*wnorm2[0];
673 frob_mat_norm_ = Teuchos::ScalarTraits< ScalarType >::squareroot(anorm2);
676 resid_norm_ = Teuchos::ScalarTraits< ScalarType >::squareroot(phibar*phibar + res);
677 mat_resid_norm_ = alpha[0] * Teuchos::ScalarTraits< ScalarType >::magnitude(tau);
Belos header file which uses auto-configuration information to include necessary C++ headers.
IterationState contains the data that defines the state of the LSQR solver at any given time.
Class which describes the linear problem to be solved by the iterative solver.
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.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
virtual ~LSQRIter()
Destructor.
LSQRIter(const Teuchos::RCP< Belos::LinearProblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< Belos::OutputManager< ScalarType > > &printer, const Teuchos::RCP< Belos::StatusTest< ScalarType, MV, OP > > &tester, Teuchos::ParameterList ¶ms)
LSQRIter constructor with linear problem, solver utilities, and parameter list of solver options.
bool isInitialized()
States whether the solver has been initialized or not.
void iterate()
This method performs LSQR iterations until the status test indicates the need to stop or an error occ...
ScalarType frob_mat_norm_
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *) const
Get the norms of the residuals native to the solver.
LSQRIterationState< 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.
const Teuchos::RCP< Belos::StatusTest< ScalarType, MV, OP > > stest_
Belos::OperatorTraits< ScalarType, MV, OP > OPT
int getNumIters() const
Get the current iteration count.
void resetNumIters(int iter=0)
Reset the iteration count.
Teuchos::ScalarTraits< ScalarType > SCT
Belos::MultiVecTraits< ScalarType, MV > MVT
void initializeLSQR(LSQRIterationState< ScalarType, MV > &newstate)
Initialize the solver to an iterate, completing the initial state.
const Teuchos::RCP< Belos::OutputManager< ScalarType > > om_
void setStateSize()
Method for initalizing the state storage needed by LSQR.
ScalarType mat_resid_norm_
SCT::magnitudeType MagnitudeType
bool stateStorageInitialized_
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver to solve this linear problem.
const Teuchos::RCP< Belos::LinearProblem< ScalarType, MV, OP > > lp_
void initialize()
The solver is initialized using initializeLSQR.
const Belos::LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
LSQRIterateFailure is thrown when the LSQRIteration object is unable to compute the next iterate in t...
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 void MvNorm(const MV &mv, std::vector< typename Teuchos::ScalarTraits< ScalarType >::magnitudeType > &normvec, NormType type=TwoNorm)
Compute the norm of each individual vector of mv. Upon return, normvec[i] holds the value of ,...
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 int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
Class which defines basic traits for the operator type.
static void Apply(const OP &Op, const MV &x, MV &y, ETrans trans=NOTRANS)
Apply Op to x, putting the result into y.
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 LSQRIteration state variables, ...
ScalarType sol_norm
An estimate of the norm of the solution.
ScalarType frob_mat_norm
An approximation to the Frobenius norm of A.
Teuchos::RCP< const MV > V
Bidiagonalization vector.
ScalarType mat_resid_norm
An estimate of the norm of A^T*resid.
Teuchos::RCP< const MV > U
Bidiagonalization vector.
Teuchos::RCP< const MV > W
The search direction vector.
Teuchos::ScalarTraits< ScalarType >::magnitudeType lambda
The damping value.
ScalarType bnorm
The norm of the RHS vector b.
ScalarType resid_norm
The current residual norm.
ScalarType mat_cond_num
An approximation to the condition number of A.