43 #ifndef BELOS_LINEAR_PROBLEM_HPP
44 #define BELOS_LINEAR_PROBLEM_HPP
81 template <
class ScalarType,
class MV,
class OP>
177 void setLSIndex (
const std::vector<int>& index);
197 void setLabel (
const std::string& label);
237 bool updateLP =
false,
377 const std::vector<int>
getLSIndex()
const {
return(rhsIndex_); }
394 return Teuchos::tuple(timerOp_,timerPrec_);
441 void apply(
const MV& x, MV& y )
const;
450 void applyOp(
const MV& x, MV& y )
const;
523 std::vector<int> rhsIndex_;
545 bool solutionUpdated_;
560 template <
class ScalarType,
class MV,
class OP>
570 solutionUpdated_(false),
575 template <
class ScalarType,
class MV,
class OP>
591 solutionUpdated_(false),
596 template <
class ScalarType,
class MV,
class OP>
600 curX_(Problem.curX_),
602 curB_(Problem.curB_),
607 timerOp_(Problem.timerOp_),
608 timerPrec_(Problem.timerPrec_),
609 blocksize_(Problem.blocksize_),
610 num2Solve_(Problem.num2Solve_),
611 rhsIndex_(Problem.rhsIndex_),
612 lsNum_(Problem.lsNum_),
613 Left_Scale_(Problem.Left_Scale_),
614 Right_Scale_(Problem.Right_Scale_),
615 isSet_(Problem.isSet_),
616 isHermitian_(Problem.isHermitian_),
617 solutionUpdated_(Problem.solutionUpdated_),
618 label_(Problem.label_)
622 template <
class ScalarType,
class MV,
class OP>
626 template <
class ScalarType,
class MV,
class OP>
634 curB_ = Teuchos::null;
635 curX_ = Teuchos::null;
638 int validIdx = 0, ivalidIdx = 0;
639 blocksize_ = rhsIndex_.size();
640 std::vector<int> vldIndex( blocksize_ );
641 std::vector<int> newIndex( blocksize_ );
642 std::vector<int> iIndex( blocksize_ );
643 for (
int i=0; i<blocksize_; ++i) {
644 if (rhsIndex_[i] > -1) {
645 vldIndex[validIdx] = rhsIndex_[i];
646 newIndex[validIdx] = i;
650 iIndex[ivalidIdx] = i;
654 vldIndex.resize(validIdx);
655 newIndex.resize(validIdx);
656 iIndex.resize(ivalidIdx);
657 num2Solve_ = validIdx;
660 if (num2Solve_ != blocksize_) {
661 newIndex.resize(num2Solve_);
662 vldIndex.resize(num2Solve_);
666 curX_ = MVT::Clone( *X_, blocksize_ );
669 MVT::MvRandom(*tmpCurB);
673 MVT::SetBlock( *tptr, newIndex, *tmpCurB );
677 tptr = MVT::CloneView( *X_, vldIndex );
678 MVT::SetBlock( *tptr, newIndex, *curX_ );
680 solutionUpdated_ =
false;
683 curX_ = MVT::CloneViewNonConst( *X_, rhsIndex_ );
684 curB_ = MVT::CloneView( *B_, rhsIndex_ );
693 template <
class ScalarType,
class MV,
class OP>
700 if (num2Solve_ < blocksize_) {
705 std::vector<int> newIndex( num2Solve_ );
706 std::vector<int> vldIndex( num2Solve_ );
707 for (
int i=0; i<blocksize_; ++i) {
708 if ( rhsIndex_[i] > -1 ) {
709 vldIndex[validIdx] = rhsIndex_[i];
710 newIndex[validIdx] = i;
715 MVT::SetBlock( *tptr, vldIndex, *X_ );
721 curX_ = Teuchos::null;
722 curB_ = Teuchos::null;
727 template <
class ScalarType,
class MV,
class OP>
751 MVT::MvAddMv( 1.0, *curX_, scale, *update, *curX_ );
756 RCP<MV> rightPrecUpdate =
757 MVT::Clone (*update, MVT::GetNumberVecs (*update));
759 #ifdef BELOS_TEUCHOS_TIME_MONITOR
762 OPT::Apply( *RP_, *update, *rightPrecUpdate );
765 MVT::MvAddMv( 1.0, *curX_, scale, *rightPrecUpdate, *curX_ );
767 solutionUpdated_ =
true;
774 newSoln = MVT::Clone (*update, MVT::GetNumberVecs (*update));
778 MVT::MvAddMv( 1.0, *curX_, scale, *update, *newSoln );
783 RCP<MV> rightPrecUpdate =
784 MVT::Clone (*update, MVT::GetNumberVecs (*update));
786 #ifdef BELOS_TEUCHOS_TIME_MONITOR
789 OPT::Apply( *RP_, *update, *rightPrecUpdate );
792 MVT::MvAddMv( 1.0, *curX_, scale, *rightPrecUpdate, *newSoln );
799 template <
class ScalarType,
class MV,
class OP>
802 if (label != label_) {
805 if (timerOp_ != Teuchos::null) {
806 std::string opLabel = label_ +
": Operation Op*x";
807 #ifdef BELOS_TEUCHOS_TIME_MONITOR
811 if (timerPrec_ != Teuchos::null) {
812 std::string precLabel = label_ +
": Operation Prec*x";
813 #ifdef BELOS_TEUCHOS_TIME_MONITOR
820 template <
class ScalarType,
class MV,
class OP>
827 if (timerOp_ == Teuchos::null) {
828 std::string opLabel = label_ +
": Operation Op*x";
829 #ifdef BELOS_TEUCHOS_TIME_MONITOR
833 if (timerPrec_ == Teuchos::null) {
834 std::string precLabel = label_ +
": Operation Prec*x";
835 #ifdef BELOS_TEUCHOS_TIME_MONITOR
841 if (newX != Teuchos::null)
843 if (newB != Teuchos::null)
848 curX_ = Teuchos::null;
849 curB_ = Teuchos::null;
853 if (A_ == Teuchos::null || X_ == Teuchos::null || B_ == Teuchos::null) {
861 solutionUpdated_ =
false;
864 if (R0_==Teuchos::null || MVT::GetNumberVecs( *R0_ )!=MVT::GetNumberVecs( *B_ )) {
865 R0_ = MVT::Clone( *B_, MVT::GetNumberVecs( *B_ ) );
867 computeCurrResVec( &*R0_, &*X_, &*B_ );
869 if (LP_!=Teuchos::null) {
870 if (PR0_==Teuchos::null || MVT::GetNumberVecs( *PR0_ )!=MVT::GetNumberVecs( *B_ )) {
871 PR0_ = MVT::Clone( *B_, MVT::GetNumberVecs( *B_ ) );
874 #ifdef BELOS_TEUCHOS_TIME_MONITOR
877 OPT::Apply( *LP_, *R0_, *PR0_ );
891 template <
class ScalarType,
class MV,
class OP>
898 return Teuchos::null;
902 template <
class ScalarType,
class MV,
class OP>
909 return Teuchos::null;
913 template <
class ScalarType,
class MV,
class OP>
919 const bool leftPrec = LP_ != null;
920 const bool rightPrec = RP_ != null;
926 RCP<MV> ytemp = (leftPrec || rightPrec) ? MVT::Clone (y, MVT::GetNumberVecs (y)) : null;
931 if (!leftPrec && !rightPrec){
932 #ifdef BELOS_TEUCHOS_TIME_MONITOR
935 OPT::Apply( *A_, x, y );
940 else if( leftPrec && rightPrec )
943 #ifdef BELOS_TEUCHOS_TIME_MONITOR
946 OPT::Apply( *RP_, x, y );
949 #ifdef BELOS_TEUCHOS_TIME_MONITOR
952 OPT::Apply( *A_, y, *ytemp );
955 #ifdef BELOS_TEUCHOS_TIME_MONITOR
958 OPT::Apply( *LP_, *ytemp, y );
967 #ifdef BELOS_TEUCHOS_TIME_MONITOR
970 OPT::Apply( *A_, x, *ytemp );
973 #ifdef BELOS_TEUCHOS_TIME_MONITOR
976 OPT::Apply( *LP_, *ytemp, y );
985 #ifdef BELOS_TEUCHOS_TIME_MONITOR
988 OPT::Apply( *RP_, x, *ytemp );
991 #ifdef BELOS_TEUCHOS_TIME_MONITOR
994 OPT::Apply( *A_, *ytemp, y );
999 template <
class ScalarType,
class MV,
class OP>
1002 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1005 OPT::Apply( *A_,x, y);
1013 template <
class ScalarType,
class MV,
class OP>
1015 if (LP_!=Teuchos::null) {
1016 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1019 return ( OPT::Apply( *LP_,x, y) );
1027 template <
class ScalarType,
class MV,
class OP>
1029 if (RP_!=Teuchos::null) {
1030 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1033 return ( OPT::Apply( *RP_,x, y) );
1041 template <
class ScalarType,
class MV,
class OP>
1047 if (LP_!=Teuchos::null)
1051 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1054 OPT::Apply( *A_, *X, *R_temp );
1056 MVT::MvAddMv( -1.0, *R_temp, 1.0, *B, *R_temp );
1058 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1061 OPT::Apply( *LP_, *R_temp, *R );
1067 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1070 OPT::Apply( *A_, *X, *R );
1072 MVT::MvAddMv( -1.0, *R, 1.0, *B, *R );
1088 if (LP_!=Teuchos::null)
1090 Teuchos::RCP<MV> R_temp = MVT::Clone( *localB, MVT::GetNumberVecs( *localB ) );
1092 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1095 OPT::Apply( *A_, *localX, *R_temp );
1097 MVT::MvAddMv( -1.0, *R_temp, 1.0, *localB, *R_temp );
1099 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1102 OPT::Apply( *LP_, *R_temp, *R );
1108 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1111 OPT::Apply( *A_, *localX, *R );
1113 MVT::MvAddMv( -1.0, *R, 1.0, *localB, *R );
1120 template <
class ScalarType,
class MV,
class OP>
1127 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1130 OPT::Apply( *A_, *X, *R );
1132 MVT::MvAddMv( -1.0, *R, 1.0, *B, *R );
1148 #ifdef BELOS_TEUCHOS_TIME_MONITOR
1151 OPT::Apply( *A_, *localX, *R );
1153 MVT::MvAddMv( -1.0, *R, 1.0, *localB, *R );
Teuchos::RCP< MV > getLHS() const
A pointer to the left-hand side X.
Exception thrown to signal error with the Belos::LinearProblem object.
void apply(const MV &x, MV &y) const
Apply the composite operator of this linear problem to x, returning y.
void setHermitian(bool isSym=true)
Tell the linear problem that the (preconditioned) operator is Hermitian.
bool isProblemSet() const
Whether the problem has been set.
Teuchos::RCP< const MV > getCurrRHSVec()
Get a pointer to the current right-hand side of the linear system.
Teuchos::RCP< MV > getCurrLHSVec()
Get a pointer to the current left-hand side (solution) of the linear system.
Teuchos::RCP< MV > updateSolution(const Teuchos::RCP< MV > &update=Teuchos::null, bool updateLP=false, ScalarType scale=Teuchos::ScalarTraits< ScalarType >::one())
Compute the new solution to the linear system using the given update vector.
Declaration of basic traits for the multivector type.
virtual ~LinearProblem(void)
Destructor (declared virtual for memory safety of derived classes).
bool setProblem(const Teuchos::RCP< MV > &newX=Teuchos::null, const Teuchos::RCP< const MV > &newB=Teuchos::null)
Set up the linear problem manager.
LinearProblem(void)
Default constructor.
void setOperator(const Teuchos::RCP< const OP > &A)
Set the operator A of the linear problem .
Teuchos::RCP< MV > updateSolution(const Teuchos::RCP< MV > &update=Teuchos::null, ScalarType scale=Teuchos::ScalarTraits< ScalarType >::one()) const
Compute the new solution to the linear system using the given update vector.
Teuchos::RCP< const MV > getRHS() const
A pointer to the right-hand side B.
Class which defines basic traits for the operator type.
void setLSIndex(const std::vector< int > &index)
Tell the linear problem which linear system(s) need to be solved next.
void computeCurrPrecResVec(MV *R, const MV *X=0, const MV *B=0) const
Compute a residual R for this operator given a solution X, and right-hand side B. ...
void setRHS(const Teuchos::RCP< const MV > &B)
Set right-hand-side B of linear problem .
bool isRightPrec() const
Whether the linear system is being preconditioned on the right.
Traits class which defines basic operations on multivectors.
void applyOp(const MV &x, MV &y) const
Apply ONLY the operator to x, returning y.
Teuchos::RCP< const OP > getOperator() const
A pointer to the (unpreconditioned) operator A.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
bool isSolutionUpdated() const
Has the current approximate solution been updated?
void setLeftPrec(const Teuchos::RCP< const OP > &LP)
Set left preconditioner (LP) of linear problem .
Teuchos::RCP< const MV > getInitResVec() const
A pointer to the initial unpreconditioned residual vector.
A linear system to solve, and its associated information.
int getLSNumber() const
The number of linear systems that have been set.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
The timers for this object.
bool isLeftPrec() const
Whether the linear system is being preconditioned on the left.
void applyRightPrec(const MV &x, MV &y) const
Apply ONLY the right preconditioner to x, returning y.
void setLabel(const std::string &label)
Set the label prefix used by the timers in this object.
Teuchos::RCP< const MV > getInitPrecResVec() const
A pointer to the preconditioned initial residual vector.
void applyLeftPrec(const MV &x, MV &y) const
Apply ONLY the left preconditioner to x, returning y.
Teuchos::RCP< const OP > getLeftPrec() const
Get a pointer to the left preconditioner.
const std::vector< int > getLSIndex() const
(Zero-based) indices of the linear system(s) currently being solved.
void computeCurrResVec(MV *R, const MV *X=0, const MV *B=0) const
Compute a residual R for this operator given a solution X, and right-hand side B. ...
LinearProblemError(const std::string &what_arg)
Teuchos::RCP< const OP > getRightPrec() const
Get a pointer to the right preconditioner.
void setCurrLS()
Tell the linear problem that the solver is finished with the current linear system.
Class which defines basic traits for the operator type.
bool isHermitian() const
Whether the (preconditioned) operator is Hermitian.
Parent class to all Belos exceptions.
void setRightPrec(const Teuchos::RCP< const OP > &RP)
Set right preconditioner (RP) of linear problem .
void setLHS(const Teuchos::RCP< MV > &X)
Set left-hand-side X of linear problem .