42 #ifndef BELOS_CG_SINGLE_RED_ITER_HPP 43 #define BELOS_CG_SINGLE_RED_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" 77 template<
class ScalarType,
class MV,
class OP>
87 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
101 Teuchos::ParameterList ¶ms );
199 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
200 "Belos::CGSingleRedIter::setBlockSize(): Cannot use a block size that is not one.");
219 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > lp_;
220 const Teuchos::RCP<OutputManager<ScalarType> > om_;
221 const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > stest_;
234 bool stateStorageInitialized_;
249 Teuchos::RCP<MV> AZ_;
259 Teuchos::RCP<MV> AP_;
265 template<
class ScalarType,
class MV,
class OP>
269 Teuchos::ParameterList ¶ms ):
274 stateStorageInitialized_(false),
281 template <
class ScalarType,
class MV,
class OP>
284 if (!stateStorageInitialized_) {
287 Teuchos::RCP<const MV> lhsMV = lp_->getLHS();
288 Teuchos::RCP<const MV> rhsMV = lp_->getRHS();
289 if (lhsMV == Teuchos::null && rhsMV == Teuchos::null) {
290 stateStorageInitialized_ =
false;
297 if (R_ == Teuchos::null) {
299 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
300 TEUCHOS_TEST_FOR_EXCEPTION(tmp == Teuchos::null,std::invalid_argument,
301 "Belos::CGSingleRedIter::setStateSize(): linear problem does not specify multivectors to clone from.");
308 std::vector<int> index(1,0);
315 stateStorageInitialized_ =
true;
323 template <
class ScalarType,
class MV,
class OP>
327 if (!stateStorageInitialized_)
330 TEUCHOS_TEST_FOR_EXCEPTION(!stateStorageInitialized_,std::invalid_argument,
331 "Belos::CGSingleRedIter::initialize(): Cannot initialize state storage!");
335 std::string errstr(
"Belos::CGSingleRedIter::initialize(): Specified multivectors must have a consistent length and width.");
337 if (newstate.
R != Teuchos::null) {
340 std::invalid_argument, errstr );
342 std::invalid_argument, errstr );
345 if (newstate.
R != R_) {
353 if ( lp_->getLeftPrec() != Teuchos::null ) {
354 lp_->applyLeftPrec( *R_, *Z_ );
355 if ( lp_->getRightPrec() != Teuchos::null ) {
357 lp_->applyRightPrec( *Z_, *tmp );
361 else if ( lp_->getRightPrec() != Teuchos::null ) {
362 lp_->applyRightPrec( *R_, *Z_ );
370 lp_->applyOp( *Z_, *AZ_ );
377 TEUCHOS_TEST_FOR_EXCEPTION(newstate.
R == Teuchos::null,std::invalid_argument,
378 "Belos::CGSingleRedIter::initialize(): CGIterationState does not have initial residual.");
388 template <
class ScalarType,
class MV,
class OP>
394 if (initialized_ ==
false) {
399 Teuchos::SerialDenseMatrix<int,ScalarType> sHz( 2, 1 );
400 ScalarType rHz, rHz_old, alpha, beta, delta;
403 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
404 const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
407 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
411 "Belos::CGSingleRedIter::iterate(): current linear system has more than one vector!" );
421 "Belos::CGSingleRedIter::iterate(): non-positive value for p^H*A*p encountered!" );
430 MVT::MvAddMv( one, *cur_soln_vec, alpha, *P_, *cur_soln_vec );
431 lp_->updateSolution();
439 if (stest_->checkStatus(
this) ==
Passed) {
447 if ( lp_->getLeftPrec() != Teuchos::null ) {
448 lp_->applyLeftPrec( *R_, *Z_ );
449 if ( lp_->getRightPrec() != Teuchos::null ) {
451 lp_->applyRightPrec( *Z_, *tmp );
455 else if ( lp_->getRightPrec() != Teuchos::null ) {
456 lp_->applyRightPrec( *R_, *Z_ );
463 lp_->applyOp( *Z_, *AZ_ );
473 beta = rHz / rHz_old;
474 alpha = rHz / (delta - (beta*rHz / alpha));
478 "Belos::CGSingleRedIter::iterate(): non-positive value for p^H*A*p encountered!" );
Teuchos::RCP< const MV > R
The current residual.
int getNumIters() const
Get the current iteration count.
Collection of types and exceptions used within the Belos solvers.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
Class which manages the output and verbosity of the Belos solvers.
Structure to contain pointers to CGIteration state variables.
Pure virtual base class which augments the basic interface for a conjugate gradient linear solver ite...
Pure virtual base class for defining the status testing capabilities of Belos.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *norms) const
Get the norms of the residuals native to the solver.
Declaration of basic traits for the multivector type.
void iterate()
This method performs CG iterations until the status test indicates the need to stop or an error occur...
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 .
A pure virtual class for defining the status tests for the Belos iterative solvers.
Class which defines basic traits for the operator type.
CGIterateFailure is thrown when the CGIteration object is unable to compute the next iterate in the C...
Teuchos::RCP< const MV > AP
The matrix A applied to current decent direction vector.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
static void Assign(const MV &A, MV &mv)
mv := A
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
Replace mv with .
MultiVecTraits< ScalarType, MV > MVT
Traits class which defines basic operations on multivectors.
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
static Teuchos::RCP< MV > CloneViewNonConst(MV &mv, const std::vector< int > &index)
Creates a new MV that shares the selected contents of mv (shallow copy).
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
void setBlockSize(int blockSize)
Set the blocksize to be used by the iterative solver in solving this linear problem.
A linear system to solve, and its associated information.
Class which describes the linear problem to be solved by the iterative solver.
bool isInitialized()
States whether the solver has been initialized or not.
CGIterationState< 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.
virtual ~CGSingleRedIter()
Destructor.
SCT::magnitudeType MagnitudeType
void initializeCG(CGIterationState< ScalarType, MV > &newstate)
Initialize the solver to an iterate, providing a complete state.
Teuchos::RCP< const MV > Z
The current preconditioned residual.
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
static ptrdiff_t GetGlobalLength(const MV &mv)
Return the number of rows in the given multivector mv.
CGSingleRedIter(const Teuchos::RCP< LinearProblem< ScalarType, MV, OP > > &problem, const Teuchos::RCP< OutputManager< ScalarType > > &printer, const Teuchos::RCP< StatusTest< ScalarType, MV, OP > > &tester, Teuchos::ParameterList ¶ms)
CGSingleRedIter constructor with linear problem, solver utilities, and parameter list of solver optio...
Class which defines basic traits for the operator type.
This class implements the preconditioned single-reduction Conjugate Gradient (CG) iteration...
Teuchos::ScalarTraits< ScalarType > SCT
Belos header file which uses auto-configuration information to include necessary C++ headers...
void resetNumIters(int iter=0)
Reset the iteration count.
OperatorTraits< ScalarType, MV, OP > OPT
Teuchos::RCP< const MV > P
The current decent direction vector.