43 #ifndef BELOS_LINEAR_PROBLEM_HPP 44 #define BELOS_LINEAR_PROBLEM_HPP 51 #include "Teuchos_ParameterList.hpp" 52 #include "Teuchos_TimeMonitor.hpp" 81 template <
class ScalarType,
class MV,
class OP>
103 const Teuchos::RCP<MV> &X,
104 const Teuchos::RCP<const MV> &B);
133 void setLHS (
const Teuchos::RCP<MV> &X) {
142 void setRHS (
const Teuchos::RCP<const MV> &B) {
196 void setLSIndex (
const std::vector<int>& index);
216 void setLabel (
const std::string& label);
255 updateSolution (
const Teuchos::RCP<MV>& update = Teuchos::null,
256 bool updateLP =
false,
257 ScalarType scale = Teuchos::ScalarTraits<ScalarType>::one());
277 ScalarType scale = Teuchos::ScalarTraits<ScalarType>::one() )
const 311 setProblem (
const Teuchos::RCP<MV> &newX = Teuchos::null,
312 const Teuchos::RCP<const MV> &newB = Teuchos::null);
323 Teuchos::RCP<MV>
getLHS()
const {
return(X_); }
326 Teuchos::RCP<const MV>
getRHS()
const {
return(B_); }
329 Teuchos::RCP<const MV> getInitResVec()
const;
335 Teuchos::RCP<const MV> getInitPrecResVec()
const;
351 Teuchos::RCP<MV> getCurrLHSVec();
367 Teuchos::RCP<const MV> getCurrRHSVec();
396 const std::vector<int>
getLSIndex()
const {
return(rhsIndex_); }
412 Teuchos::Array<Teuchos::RCP<Teuchos::Time> >
getTimers()
const {
413 return Teuchos::tuple(timerOp_,timerPrec_);
460 void apply(
const MV& x, MV& y )
const;
469 void applyOp(
const MV& x, MV& y )
const;
477 void applyLeftPrec(
const MV& x, MV& y )
const;
485 void applyRightPrec(
const MV& x, MV& y )
const;
492 void computeCurrResVec( MV* R ,
const MV* X = 0,
const MV* B = 0 )
const;
499 void computeCurrPrecResVec( MV* R,
const MV* X = 0,
const MV* B = 0 )
const;
506 Teuchos::RCP<const OP> A_;
512 Teuchos::RCP<MV> curX_;
515 Teuchos::RCP<const MV> B_;
518 Teuchos::RCP<const MV> curB_;
521 Teuchos::RCP<MV> R0_;
524 Teuchos::RCP<MV> PR0_;
527 Teuchos::RCP<const MV> R0_user_;
530 Teuchos::RCP<const MV> PR0_user_;
533 Teuchos::RCP<const OP> LP_;
536 Teuchos::RCP<const OP> RP_;
539 mutable Teuchos::RCP<Teuchos::Time> timerOp_, timerPrec_;
548 std::vector<int> rhsIndex_;
564 bool solutionUpdated_;
579 template <
class ScalarType,
class MV,
class OP>
587 solutionUpdated_(false),
592 template <
class ScalarType,
class MV,
class OP>
594 const Teuchos::RCP<MV> &X,
595 const Teuchos::RCP<const MV> &B
606 solutionUpdated_(false),
611 template <
class ScalarType,
class MV,
class OP>
615 curX_(Problem.curX_),
617 curB_(Problem.curB_),
620 R0_user_(Problem.R0_user_),
621 PR0_user_(Problem.PR0_user_),
624 timerOp_(Problem.timerOp_),
625 timerPrec_(Problem.timerPrec_),
626 blocksize_(Problem.blocksize_),
627 num2Solve_(Problem.num2Solve_),
628 rhsIndex_(Problem.rhsIndex_),
629 lsNum_(Problem.lsNum_),
630 isSet_(Problem.isSet_),
631 isHermitian_(Problem.isHermitian_),
632 solutionUpdated_(Problem.solutionUpdated_),
633 label_(Problem.label_)
637 template <
class ScalarType,
class MV,
class OP>
641 template <
class ScalarType,
class MV,
class OP>
649 curB_ = Teuchos::null;
650 curX_ = Teuchos::null;
653 int validIdx = 0, ivalidIdx = 0;
654 blocksize_ = rhsIndex_.size();
655 std::vector<int> vldIndex( blocksize_ );
656 std::vector<int> newIndex( blocksize_ );
657 std::vector<int> iIndex( blocksize_ );
658 for (
int i=0; i<blocksize_; ++i) {
659 if (rhsIndex_[i] > -1) {
660 vldIndex[validIdx] = rhsIndex_[i];
661 newIndex[validIdx] = i;
665 iIndex[ivalidIdx] = i;
669 vldIndex.resize(validIdx);
670 newIndex.resize(validIdx);
671 iIndex.resize(ivalidIdx);
672 num2Solve_ = validIdx;
675 if (num2Solve_ != blocksize_) {
676 newIndex.resize(num2Solve_);
677 vldIndex.resize(num2Solve_);
683 Teuchos::RCP<MV> tmpCurB =
MVT::Clone( *B_, blocksize_ );
695 solutionUpdated_ =
false;
708 template <
class ScalarType,
class MV,
class OP>
715 if (num2Solve_ < blocksize_) {
720 std::vector<int> newIndex( num2Solve_ );
721 std::vector<int> vldIndex( num2Solve_ );
722 for (
int i=0; i<blocksize_; ++i) {
723 if ( rhsIndex_[i] > -1 ) {
724 vldIndex[validIdx] = rhsIndex_[i];
725 newIndex[validIdx] = i;
736 curX_ = Teuchos::null;
737 curB_ = Teuchos::null;
742 template <
class ScalarType,
class MV,
class OP>
753 if (update.is_null())
771 RCP<MV> rightPrecUpdate =
774 #ifdef BELOS_TEUCHOS_TIME_MONITOR 775 Teuchos::TimeMonitor PrecTimer (*timerPrec_);
777 OPT::Apply( *RP_, *update, *rightPrecUpdate );
780 MVT::MvAddMv( 1.0, *curX_, scale, *rightPrecUpdate, *curX_ );
782 solutionUpdated_ =
true;
798 RCP<MV> rightPrecUpdate =
801 #ifdef BELOS_TEUCHOS_TIME_MONITOR 802 Teuchos::TimeMonitor PrecTimer(*timerPrec_);
804 OPT::Apply( *RP_, *update, *rightPrecUpdate );
807 MVT::MvAddMv( 1.0, *curX_, scale, *rightPrecUpdate, *newSoln );
814 template <
class ScalarType,
class MV,
class OP>
817 if (label != label_) {
820 if (timerOp_ != Teuchos::null) {
821 std::string opLabel = label_ +
": Operation Op*x";
822 #ifdef BELOS_TEUCHOS_TIME_MONITOR 823 timerOp_ = Teuchos::TimeMonitor::getNewCounter( opLabel );
826 if (timerPrec_ != Teuchos::null) {
827 std::string precLabel = label_ +
": Operation Prec*x";
828 #ifdef BELOS_TEUCHOS_TIME_MONITOR 829 timerPrec_ = Teuchos::TimeMonitor::getNewCounter( precLabel );
835 template <
class ScalarType,
class MV,
class OP>
839 const Teuchos::RCP<const MV> &newB)
842 if (timerOp_ == Teuchos::null) {
843 std::string opLabel = label_ +
": Operation Op*x";
844 #ifdef BELOS_TEUCHOS_TIME_MONITOR 845 timerOp_ = Teuchos::TimeMonitor::getNewCounter( opLabel );
848 if (timerPrec_ == Teuchos::null) {
849 std::string precLabel = label_ +
": Operation Prec*x";
850 #ifdef BELOS_TEUCHOS_TIME_MONITOR 851 timerPrec_ = Teuchos::TimeMonitor::getNewCounter( precLabel );
856 if (newX != Teuchos::null)
858 if (newB != Teuchos::null)
863 curX_ = Teuchos::null;
864 curB_ = Teuchos::null;
868 if (A_ == Teuchos::null || X_ == Teuchos::null || B_ == Teuchos::null) {
876 solutionUpdated_ =
false;
879 if(Teuchos::is_null(R0_user_)) {
885 if (LP_!=Teuchos::null) {
890 #ifdef BELOS_TEUCHOS_TIME_MONITOR 891 Teuchos::TimeMonitor PrecTimer(*timerPrec_);
905 R0_user_ = Teuchos::null;
909 if (LP_!=Teuchos::null) {
912 if (PR0_user_==Teuchos::null || (PR0_user_==R0_) || (PR0_user_==R0_user_)
917 #ifdef BELOS_TEUCHOS_TIME_MONITOR 918 Teuchos::TimeMonitor PrecTimer(*timerPrec_);
922 PR0_user_ = Teuchos::null;
929 if (R0_user_!=Teuchos::null)
931 PR0_user_ = R0_user_;
935 PR0_user_ = Teuchos::null;
948 template <
class ScalarType,
class MV,
class OP>
951 if(Teuchos::nonnull(R0_user_)) {
957 template <
class ScalarType,
class MV,
class OP>
960 if(Teuchos::nonnull(PR0_user_)) {
966 template <
class ScalarType,
class MV,
class OP>
973 return Teuchos::null;
977 template <
class ScalarType,
class MV,
class OP>
984 return Teuchos::null;
988 template <
class ScalarType,
class MV,
class OP>
994 const bool leftPrec = LP_ != null;
995 const bool rightPrec = RP_ != null;
1006 if (!leftPrec && !rightPrec){
1007 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1008 Teuchos::TimeMonitor OpTimer(*timerOp_);
1015 else if( leftPrec && rightPrec )
1018 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1019 Teuchos::TimeMonitor PrecTimer(*timerPrec_);
1024 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1025 Teuchos::TimeMonitor OpTimer(*timerOp_);
1030 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1031 Teuchos::TimeMonitor PrecTimer(*timerPrec_);
1042 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1043 Teuchos::TimeMonitor OpTimer(*timerOp_);
1048 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1049 Teuchos::TimeMonitor PrecTimer(*timerPrec_);
1060 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1061 Teuchos::TimeMonitor PrecTimer(*timerPrec_);
1066 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1067 Teuchos::TimeMonitor OpTimer(*timerOp_);
1074 template <
class ScalarType,
class MV,
class OP>
1077 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1078 Teuchos::TimeMonitor OpTimer(*timerOp_);
1083 MVT::MvAddMv( Teuchos::ScalarTraits<ScalarType>::one(), x,
1084 Teuchos::ScalarTraits<ScalarType>::zero(), x, y );
1088 template <
class ScalarType,
class MV,
class OP>
1090 if (LP_!=Teuchos::null) {
1091 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1092 Teuchos::TimeMonitor PrecTimer(*timerPrec_);
1097 MVT::MvAddMv( Teuchos::ScalarTraits<ScalarType>::one(), x,
1098 Teuchos::ScalarTraits<ScalarType>::zero(), x, y );
1102 template <
class ScalarType,
class MV,
class OP>
1104 if (RP_!=Teuchos::null) {
1105 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1106 Teuchos::TimeMonitor PrecTimer(*timerPrec_);
1111 MVT::MvAddMv( Teuchos::ScalarTraits<ScalarType>::one(), x,
1112 Teuchos::ScalarTraits<ScalarType>::zero(), x, y );
1116 template <
class ScalarType,
class MV,
class OP>
1122 if (LP_!=Teuchos::null)
1126 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1127 Teuchos::TimeMonitor OpTimer(*timerOp_);
1133 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1134 Teuchos::TimeMonitor PrecTimer(*timerPrec_);
1142 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1143 Teuchos::TimeMonitor OpTimer(*timerOp_);
1152 Teuchos::RCP<const MV> localB, localX;
1154 localB = Teuchos::rcp( B,
false );
1159 localX = Teuchos::rcp( X,
false );
1163 if (LP_!=Teuchos::null)
1167 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1168 Teuchos::TimeMonitor OpTimer(*timerOp_);
1174 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1175 Teuchos::TimeMonitor PrecTimer(*timerPrec_);
1183 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1184 Teuchos::TimeMonitor OpTimer(*timerOp_);
1195 template <
class ScalarType,
class MV,
class OP>
1202 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1203 Teuchos::TimeMonitor OpTimer(*timerOp_);
1211 Teuchos::RCP<const MV> localB, localX;
1213 localB = Teuchos::rcp( B,
false );
1218 localX = Teuchos::rcp( X,
false );
1223 #ifdef BELOS_TEUCHOS_TIME_MONITOR 1224 Teuchos::TimeMonitor OpTimer(*timerOp_);
Teuchos::RCP< const MV > getRHS() const
A pointer to the right-hand side B.
Exception thrown to signal error with the Belos::LinearProblem object.
void setHermitian(bool isSym=true)
Tell the linear problem that the (preconditioned) operator is Hermitian.
const std::vector< int > getLSIndex() const
(Zero-based) indices of the linear system(s) currently being solved.
Teuchos::RCP< const MV > getCurrRHSVec()
Get a pointer to the current right-hand side of the linear system.
static void MvRandom(MV &mv)
Replace the vectors in mv with random vectors.
Teuchos::RCP< MV > getCurrLHSVec()
Get a pointer to the current left-hand side (solution) of the linear system.
Teuchos::RCP< const MV > getInitResVec() const
A pointer to the initial unpreconditioned residual vector.
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.
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).
bool isRightPrec() const
Whether the linear system is being preconditioned on the right.
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.
Teuchos::Array< Teuchos::RCP< Teuchos::Time > > getTimers() const
The timers for this object.
bool isHermitian() const
Whether the (preconditioned) operator is Hermitian.
void applyLeftPrec(const MV &x, MV &y) const
Apply ONLY the left preconditioner to x, returning y.
LinearProblem(void)
Default constructor.
static void MvInit(MV &mv, const ScalarType alpha=Teuchos::ScalarTraits< ScalarType >::zero())
Replace each element of the vectors in mv with alpha.
void setOperator(const Teuchos::RCP< const OP > &A)
Set the operator A of the linear problem .
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.
static int GetNumberVecs(const MV &mv)
Obtain the number of vectors in mv.
static void MvAddMv(const ScalarType alpha, const MV &A, const ScalarType beta, const MV &B, MV &mv)
Replace mv with .
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.
void setRHS(const Teuchos::RCP< const MV > &B)
Set right-hand-side B of linear problem .
Traits class which defines basic operations on multivectors.
static void SetBlock(const MV &A, const std::vector< int > &index, MV &mv)
Copy the vectors in A to a set of vectors in mv indicated by the indices given in index...
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.
static void Apply(const OP &Op, const MV &x, MV &y, ETrans trans=NOTRANS)
Apply Op to x, putting the result into y.
void setLeftPrec(const Teuchos::RCP< const OP > &LP)
Set left preconditioner (LP) of linear problem .
Teuchos::RCP< MV > getLHS() const
A pointer to the left-hand side X.
A linear system to solve, and its associated information.
void applyRightPrec(const MV &x, MV &y) const
Apply ONLY the right preconditioner to x, returning y.
void applyOp(const MV &x, MV &y) const
Apply ONLY the operator to x, returning y.
Teuchos::RCP< const OP > getRightPrec() const
Get a pointer to the right preconditioner.
void setLabel(const std::string &label)
Set the label prefix used by the timers in this object.
int getLSNumber() const
The number of linear systems that have been set.
Teuchos::RCP< const OP > getLeftPrec() const
Get a pointer to the left preconditioner.
bool isProblemSet() const
Whether the problem has been set.
LinearProblemError(const std::string &what_arg)
bool isSolutionUpdated() const
Has the current approximate solution been updated?
void setCurrLS()
Tell the linear problem that the solver is finished with the current linear system.
bool isLeftPrec() const
Whether the linear system is being preconditioned on the left.
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. ...
Class which defines basic traits for the operator type.
Parent class to all Belos exceptions.
Teuchos::RCP< const OP > getOperator() const
A pointer to the (unpreconditioned) operator A.
void setRightPrec(const Teuchos::RCP< const OP > &RP)
Set right preconditioner (RP) of linear problem .
void setInitResVec(const Teuchos::RCP< const MV > &R0)
Set the user-defined residual of linear problem .
void setLHS(const Teuchos::RCP< MV > &X)
Set left-hand-side X of linear problem .
void apply(const MV &x, MV &y) const
Apply the composite operator of this linear problem to x, returning y.
Teuchos::RCP< const MV > getInitPrecResVec() const
A pointer to the preconditioned initial residual vector.
void setInitPrecResVec(const Teuchos::RCP< const MV > &PR0)
Set the user-defined preconditioned residual of linear problem .
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. ...