10#ifndef BELOS_LSQR_ITER_HPP
11#define BELOS_LSQR_ITER_HPP
27#include "Teuchos_SerialDenseMatrix.hpp"
28#include "Teuchos_SerialDenseVector.hpp"
29#include "Teuchos_ScalarTraits.hpp"
30#include "Teuchos_ParameterList.hpp"
31#include "Teuchos_TimeMonitor.hpp"
42template<
class ScalarType,
class MV,
class OP>
52 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
67 Teuchos::ParameterList &
params );
125 state.lambda = lambda_;
126 state.resid_norm = resid_norm_;
127 state.frob_mat_norm = frob_mat_norm_;
128 state.mat_cond_num = mat_cond_num_;
129 state.mat_resid_norm = mat_resid_norm_;
130 state.sol_norm = sol_norm_;
131 state.bnorm = bnorm_;
149 Teuchos::RCP<const MV>
getNativeResiduals( std::vector<MagnitudeType> * )
const {
return Teuchos::null; }
172 "LSQRIter::setBlockSize(): Cannot use a block size that is not one.");
191 const Teuchos::RCP<Belos::LinearProblem<ScalarType,MV,OP> > lp_;
192 const Teuchos::RCP<Belos::OutputManager<ScalarType> > om_;
193 const Teuchos::RCP<Belos::StatusTest<ScalarType,MV,OP> > stest_;
208 bool stateStorageInitialized_;
254 template<
class ScalarType,
class MV,
class OP>
258 Teuchos::ParameterList &
params ):
263 stateStorageInitialized_(
false),
269 mat_resid_norm_(0.0),
277 template <
class ScalarType,
class MV,
class OP>
280 if (!stateStorageInitialized_) {
282 Teuchos::RCP<const MV>
rhsMV = lp_->getInitPrecResVec();
283 Teuchos::RCP<const MV>
lhsMV = lp_->getLHS();
284 if (
lhsMV == Teuchos::null ||
rhsMV == Teuchos::null) {
285 stateStorageInitialized_ =
false;
292 if (U_ == Teuchos::null) {
294 TEUCHOS_TEST_FOR_EXCEPTION(
rhsMV == Teuchos::null, std::invalid_argument,
"LSQRIter::setStateSize(): linear problem does not specify right hand multivector to clone from.");
295 TEUCHOS_TEST_FOR_EXCEPTION(
lhsMV == Teuchos::null, std::invalid_argument,
"LSQRIter::setStateSize(): linear problem does not specify left hand multivector to clone from.");
297 U_ = MVT::Clone( *
rhsMV, 1 );
298 V_ = MVT::Clone( *
lhsMV, 1 );
299 W_ = MVT::Clone( *
lhsMV, 1 );
303 stateStorageInitialized_ =
true;
311 template <
class ScalarType,
class MV,
class OP>
317 if (!stateStorageInitialized_)
321 "LSQRIter::initialize(): Cannot initialize state storage!");
323 std::string
errstr(
"LSQRIter::initialize(): Specified multivectors must have a consistent length and width.");
334 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
lhsNorm(1);
336 std::cout <<
"initializeLSQR lhsNorm " <<
lhsNorm[0] << std::endl;
341 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
342 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
351 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
rhsNorm(1);
353 std::cout <<
"initializeLSQR | U_ | : " <<
rhsNorm[0] << std::endl;
387 MVT::MvAddMv(
one, *V_,
zero, *V_, *W_);
389 frob_mat_norm_ =
zero;
400 template <
class ScalarType,
class MV,
class OP>
406 if (initialized_ ==
false) {
411 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
413 const ScalarType zero = Teuchos::ScalarTraits<ScalarType>::zero();
416 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> alpha(1);
417 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType> beta(1);
418 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
xi(1);
421 std::vector<typename Teuchos::ScalarTraits<ScalarType>::magnitudeType>
wnorm2(1);
429 AV = MVT::Clone( *U_, 1);
430 Teuchos::RCP<MV>
AtU;
431 AtU = MVT::Clone( *V_, 1);
440 "LSQRIter::iterate(): current linear system has more than one vector!" );
444 MVT::MvNorm( *U_, beta );
445 if( SCT::real(beta[0]) >
zero )
447 MVT::MvScale( *U_,
one / beta[0] );
452 MVT::MvScale( *V_,
one / beta[0] );
457 MVT::MvNorm( *V_, alpha );
462 std::cout << iter_ <<
" First alpha " << alpha[0] <<
" beta " << beta[0] <<
" lambda " << lambda_ << std::endl;
464 if( SCT::real(alpha[0]) >
zero )
466 MVT::MvScale( *V_,
one / alpha[0] );
468 if( beta[0] * alpha[0] >
zero )
470 MVT::MvScale( *W_,
one / (beta[0] * alpha[0]) );
474 MVT::MvScale( *W_,
zero );
485 resid_norm_ = beta[0];
486 mat_resid_norm_ = alpha[0] * beta[0];
502 OPT::Apply(*
A, *V_, *
AV);
541 MVT::MvAddMv(
one, *
AtU, -alpha[0], *V_, *
AtU );
542 MVT::MvNorm( *
AtU,
xi );
543 std::cout <<
"| V alpha - A' u |= " <<
xi[0] << std::endl;
545 Teuchos::SerialDenseMatrix<int,ScalarType>
uotuo(1,1);
546 MVT::MvTransMv(
one, *U_, *U_,
uotuo );
549 std::cout <<
"alpha = " << alpha[0] << std::endl;
551 Teuchos::SerialDenseMatrix<int,ScalarType>
utav(1,1);
553 std::cout <<
"<AV, U> = alpha = " <<
printMat(
utav) << std::endl;
556 MVT::MvAddMv(
one, *
AV, -alpha[0], *U_, *U_ );
557 MVT::MvNorm( *U_, beta);
559 anorm2 += alpha[0]*alpha[0] + beta[0]*beta[0] + lambda_*lambda_;
562 if ( SCT::real(beta[0]) >
zero )
565 MVT::MvScale( *U_,
one / beta[0] );
583 MVT::MvAddMv(
one, *
AtU, -beta[0], *V_, *V_ );
584 MVT::MvNorm( *V_, alpha );
591 if ( SCT::real(alpha[0]) >
zero )
593 MVT::MvScale( *V_,
one / alpha[0] );
598 common = Teuchos::ScalarTraits< ScalarType >::squareroot(
rhobar*
rhobar + lambda_*lambda_);
606 rho = Teuchos::ScalarTraits< ScalarType >::squareroot(
rhobar*
rhobar + lambda_*lambda_ + beta[0]*beta[0]);
609 theta = sn * alpha[0];
637 MVT::MvNorm( *W_,
wnorm2 );
639 MVT::MvAddMv(
one, *V_, -theta / rho, *W_, *W_ );
641 frob_mat_norm_ = Teuchos::ScalarTraits< ScalarType >::squareroot(
anorm2);
642 mat_cond_num_ = frob_mat_norm_ * Teuchos::ScalarTraits< ScalarType >::squareroot(
ddnorm);
644 resid_norm_ = Teuchos::ScalarTraits< ScalarType >::squareroot(
phibar*
phibar +
res);
645 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.
Implementation of the LSQR iteration.
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...
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.
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.
SCT::magnitudeType MagnitudeType
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver to solve this linear problem.
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...
Alternative run-time polymorphic interface for operators.
Operator()
Default constructor (does nothing).