10#ifndef BELOS_PSEUDO_BLOCK_STOCHASTIC_CG_ITER_HPP
11#define BELOS_PSEUDO_BLOCK_STOCHASTIC_CG_ITER_HPP
28#include "Teuchos_SerialDenseMatrix.hpp"
29#include "Teuchos_SerialDenseVector.hpp"
30#include "Teuchos_SerialDenseHelpers.hpp"
31#include "Teuchos_ScalarTraits.hpp"
32#include "Teuchos_ParameterList.hpp"
33#include "Teuchos_TimeMonitor.hpp"
52 template<
class ScalarType,
class MV,
class OP>
62 typedef Teuchos::ScalarTraits<ScalarType>
SCT;
76 Teuchos::ParameterList ¶ms );
186 TEUCHOS_TEST_FOR_EXCEPTION(blockSize!=1,std::invalid_argument,
187 "Belos::PseudoBlockStochasticCGIter::setBlockSize(): Cannot use a block size that is not one.");
198 inline Teuchos::SerialDenseVector<int,ScalarType>& normal() {
202 const double p0 = -0.322232431088;
203 const double p1 = -1.0;
204 const double p2 = -0.342242088547;
205 const double p3 = -0.204231210245e-1;
206 const double p4 = -0.453642210148e-4;
207 const double q0 = 0.993484626060e-1;
208 const double q1 = 0.588581570495;
209 const double q2 = 0.531103462366;
210 const double q3 = 0.103537752850;
211 const double q4 = 0.38560700634e-2;
215 Teuchos::randomSyncedMatrix( randvec_ );
217 for (
int i=0; i<numRHS_; i++)
220 r=0.5*SCT::real(randvec_[i]) + 1.0;
223 if(r < 0.5) y=std::sqrt(-2.0 * log(r));
224 else y=std::sqrt(-2.0 * log(1.0 - r));
226 p = p0 + y * (p1 + y* (p2 + y * (p3 + y * p4)));
227 q = q0 + y * (q1 + y* (q2 + y * (q3 + y * q4)));
229 if(r < 0.5) z = (p / q) - y;
230 else z = y - (p / q);
232 randvec_[i] = Teuchos::as<ScalarType,double>(z);
241 const Teuchos::RCP<LinearProblem<ScalarType,MV,OP> > lp_;
242 const Teuchos::RCP<OutputManager<ScalarType> > om_;
243 const Teuchos::RCP<StatusTest<ScalarType,MV,OP> > stest_;
263 bool assertPositiveDefiniteness_;
278 Teuchos::RCP<MV> AP_;
284 Teuchos::SerialDenseVector<int,ScalarType> randvec_;
290 template<
class ScalarType,
class MV,
class OP>
294 Teuchos::ParameterList ¶ms ):
301 assertPositiveDefiniteness_( params.get(
"Assert Positive Definiteness", true) )
308 template <
class ScalarType,
class MV,
class OP>
312 Teuchos::RCP<const MV> lhsMV = lp_->getCurrLHSVec();
313 Teuchos::RCP<const MV> rhsMV = lp_->getCurrRHSVec();
314 TEUCHOS_TEST_FOR_EXCEPTION((lhsMV==Teuchos::null && rhsMV==Teuchos::null),std::invalid_argument,
315 "Belos::PseudoBlockStochasticCGIter::initialize(): Cannot initialize state storage!");
318 Teuchos::RCP<const MV> tmp = ( (rhsMV!=Teuchos::null)? rhsMV: lhsMV );
335 randvec_.size( numRHS_ );
339 std::string errstr(
"Belos::BlockPseudoStochasticCGIter::initialize(): Specified multivectors must have a consistent length and width.");
342 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
343 const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
345 if (!Teuchos::is_null(newstate.
R)) {
348 std::invalid_argument, errstr );
350 std::invalid_argument, errstr );
353 if (newstate.
R != R_) {
361 if ( lp_->getLeftPrec() != Teuchos::null ) {
362 lp_->applyLeftPrec( *R_, *Z_ );
363 if ( lp_->getRightPrec() != Teuchos::null ) {
364 Teuchos::RCP<MV> tmp2 =
MVT::Clone( *Z_, numRHS_ );
365 lp_->applyRightPrec( *Z_, *tmp2 );
369 else if ( lp_->getRightPrec() != Teuchos::null ) {
370 lp_->applyRightPrec( *R_, *Z_ );
379 TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::is_null(newstate.
R),std::invalid_argument,
380 "Belos::StochasticCGIter::initialize(): CGStateIterState does not have initial residual.");
390 template <
class ScalarType,
class MV,
class OP>
396 if (initialized_ ==
false) {
402 std::vector<int> index(1);
403 std::vector<ScalarType> rHz( numRHS_ ), rHz_old( numRHS_ ), pAp( numRHS_ );
404 Teuchos::SerialDenseMatrix<int, ScalarType> alpha( numRHS_,numRHS_ ), beta( numRHS_,numRHS_ ), zeta(numRHS_,numRHS_);
407 const ScalarType one = Teuchos::ScalarTraits<ScalarType>::one();
408 const MagnitudeType zero = Teuchos::ScalarTraits<MagnitudeType>::zero();
411 Teuchos::RCP<MV> cur_soln_vec = lp_->getCurrLHSVec();
416 if ( assertPositiveDefiniteness_ )
417 for (i=0; i<numRHS_; ++i)
418 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(rHz[i]) < zero,
420 "Belos::PseudoBlockStochasticCGIter::iterate(): negative value for r^H*M*r encountered!" );
425 while (stest_->checkStatus(
this) !=
Passed) {
431 lp_->applyOp( *P_, *AP_ );
436 Teuchos::SerialDenseVector<int,ScalarType>& z = normal();
438 for (i=0; i<numRHS_; ++i) {
439 if ( assertPositiveDefiniteness_ )
441 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(pAp[i]) <= zero,
443 "Belos::PseudoBlockStochasticCGIter::iterate(): non-positive value for p^H*A*p encountered!" );
445 alpha(i,i) = rHz[i] / pAp[i];
448 zeta(i,i) = z[i] / Teuchos::ScalarTraits<ScalarType>::squareroot(pAp[i]);
455 lp_->updateSolution();
463 for (i=0; i<numRHS_; ++i) {
474 if ( lp_->getLeftPrec() != Teuchos::null ) {
475 lp_->applyLeftPrec( *R_, *Z_ );
476 if ( lp_->getRightPrec() != Teuchos::null ) {
477 Teuchos::RCP<MV> tmp =
MVT::Clone( *Z_, numRHS_ );
478 lp_->applyRightPrec( *Z_, *tmp );
482 else if ( lp_->getRightPrec() != Teuchos::null ) {
483 lp_->applyRightPrec( *R_, *Z_ );
490 if ( assertPositiveDefiniteness_ )
491 for (i=0; i<numRHS_; ++i)
492 TEUCHOS_TEST_FOR_EXCEPTION( SCT::real(rHz[i]) < zero,
494 "Belos::PseudoBlockStochasticCGIter::iterate(): negative value for r^H*M*r encountered!" );
497 for (i=0; i<numRHS_; ++i) {
498 beta(i,i) = rHz[i] / rHz_old[i];
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.
Templated virtual class for providing orthogonalization/orthonormalization methods with matrix-based ...
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.
Pure virtual base class which augments the basic interface for a stochastic conjugate gradient linear...
Collection of types and exceptions used within the Belos solvers.
CGPositiveDefiniteFailure is thrown when the the CG 'alpha = p^H*A*P' value is less than zero,...
Traits class which defines basic operations on multivectors.
static void MvTimesMatAddMv(const ScalarType alpha, const MV &A, const Teuchos::SerialDenseMatrix< int, ScalarType > &B, const ScalarType beta, MV &mv)
Update mv with .
static ptrdiff_t GetGlobalLength(const MV &mv)
Return the number of rows in the given multivector mv.
static void MvDot(const MV &mv, const MV &A, std::vector< ScalarType > &b)
Compute a vector b where the components are the individual dot-products of the i-th columns of A and ...
static Teuchos::RCP< MV > Clone(const MV &mv, const int numvecs)
Creates a new empty MV containing numvecs columns.
static Teuchos::RCP< const MV > CloneView(const MV &mv, const std::vector< int > &index)
Creates a new const MV that shares the selected contents of mv (shallow copy).
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 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.
Belos's basic output manager for sending information of select verbosity levels to the appropriate ou...
void initialize()
Initialize the solver with the initial vectors from the linear problem or random data.
PseudoBlockStochasticCGIter(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)
PseudoBlockStochasticCGIter constructor with linear problem, solver utilities, and parameter list of ...
bool isInitialized()
States whether the solver has been initialized or not.
int getNumIters() const
Get the current iteration count.
Teuchos::ScalarTraits< ScalarType > SCT
SCT::magnitudeType MagnitudeType
void initializeCG(StochasticCGIterationState< ScalarType, MV > &newstate)
Initialize the solver to an iterate, providing a complete state.
int getBlockSize() const
Get the blocksize to be used by the iterative solver in solving this linear problem.
Teuchos::RCP< const MV > getNativeResiduals(std::vector< MagnitudeType > *) const
const LinearProblem< ScalarType, MV, OP > & getProblem() const
Get a constant reference to the linear problem.
MultiVecTraits< ScalarType, MV > MVT
StochasticCGIterationState< ScalarType, MV > getState() const
Get the current state of the linear solver.
Teuchos::RCP< MV > getStochasticVector() const
Get the stochastic vector.
void resetNumIters(int iter=0)
Reset the iteration count.
void iterate()
This method performs stochastic CG iterations on each linear system until the status test indicates t...
OperatorTraits< ScalarType, MV, OP > OPT
Teuchos::RCP< MV > getCurrentUpdate() const
Get the current update to the linear system.
void setBlockSize(int blockSize)
Set the blocksize.
virtual ~PseudoBlockStochasticCGIter()
Destructor.
A pure virtual class for defining the status tests for the Belos iterative solvers.
Structure to contain pointers to CGIteration state variables.
Teuchos::RCP< const MV > Y
The current stochastic recurrence vector.
Teuchos::RCP< const MV > AP
The matrix A applied to current decent direction vector.
Teuchos::RCP< const MV > P
The current decent direction vector.
Teuchos::RCP< const MV > Z
The current preconditioned residual.
Teuchos::RCP< const MV > R
The current residual.