10 #include "Thyra_AztecOOLinearOpWithSolve.hpp"
11 #include "Thyra_LinearOpWithSolveHelpers.hpp"
13 #include "Thyra_EpetraOperatorWrapper.hpp"
14 #include "Teuchos_BLAS_types.hpp"
15 #include "Teuchos_TimeMonitor.hpp"
16 #include "Teuchos_Time.hpp"
17 #include "Teuchos_implicit_cast.hpp"
47 class SetAztecSolveState {
55 ~SetAztecSolveState();
66 SetAztecSolveState::SetAztecSolveState(
72 :aztecSolver_(aztecSolver.assert_not_null())
77 verbLevel_ = verbLevel;
83 fancyOStream_= Teuchos::tab(
88 aztecSolver_->SetOutputStream(*fancyOStream_);
89 aztecSolver_->SetErrorStream(*fancyOStream_);
99 outputFrequency_ = aztecSolver_->GetAllAztecOptions()[AZ_output];
100 aztecSolver_->SetAztecOption(AZ_output,0);
104 convergenceTest_ = aztecSolver_->GetAztecOption(AZ_conv);
116 aztecSolver_->SetAztecOption(AZ_conv,AZ_rhs);
125 aztecSolver_->SetAztecOption(AZ_conv,AZ_r0);
134 SetAztecSolveState::~SetAztecSolveState()
140 aztecSolver_->SetOutputStream(std::cout);
141 aztecSolver_->SetErrorStream(std::cerr);
142 *fancyOStream_ <<
"\n";
146 aztecSolver_->SetAztecOption(AZ_output,outputFrequency_);
150 aztecSolver_->SetAztecOption(AZ_conv,convergenceTest_);
165 const int fwdDefaultMaxIterations_in
166 ,
const double fwdDefaultTol_in
167 ,
const int adjDefaultMaxIterations_in
168 ,
const double adjDefaultTol_in
169 ,
const bool outputEveryRhs_in
171 :fwdDefaultMaxIterations_(fwdDefaultMaxIterations_in)
172 ,fwdDefaultTol_(fwdDefaultTol_in)
173 ,adjDefaultMaxIterations_(adjDefaultMaxIterations_in)
174 ,adjDefaultTol_(adjDefaultTol_in)
175 ,outputEveryRhs_(outputEveryRhs_in)
176 ,isExternalPrec_(false)
177 ,allowInexactFwdSolve_(false)
178 ,allowInexactAdjSolve_(false)
179 ,aztecSolverScalar_(0.0)
187 ,
const bool isExternalPrec_in
190 ,
const bool allowInexactFwdSolve
192 ,
const bool allowInexactAdjSolve
193 ,
const double aztecSolverScalar
202 fwdOpSrc_ = fwdOpSrc;
203 isExternalPrec_ = isExternalPrec_in;
205 approxFwdOpSrc_ = approxFwdOpSrc;
206 aztecFwdSolver_ = aztecFwdSolver;
207 allowInexactFwdSolve_ = allowInexactFwdSolve;
208 aztecAdjSolver_ = aztecAdjSolver;
209 allowInexactAdjSolve_ = allowInexactAdjSolve;
210 aztecSolverScalar_ = aztecSolverScalar;
211 const std::string fwdOpLabel = fwdOp_->getObjectLabel();
212 if (fwdOpLabel.length())
221 _fwdOpSrc = fwdOpSrc_;
222 fwdOpSrc_ = Teuchos::null;
232 prec_ = Teuchos::null;
239 return isExternalPrec_;
247 _approxFwdOpSrc = approxFwdOpSrc_;
248 approxFwdOpSrc_ = Teuchos::null;
249 return _approxFwdOpSrc;
257 bool *isExternalPrec_inout,
260 bool *allowInexactFwdSolve,
262 bool *allowInexactAdjSolve,
263 double *aztecSolverScalar
266 if (fwdOp) *fwdOp = fwdOp_;
267 if (fwdOpSrc) *fwdOpSrc = fwdOpSrc_;
268 if (prec) *prec = prec_;
269 if (isExternalPrec_inout) *isExternalPrec_inout = isExternalPrec_;
270 if (approxFwdOpSrc) *approxFwdOpSrc = approxFwdOpSrc_;
271 if (aztecFwdSolver) *aztecFwdSolver = aztecFwdSolver_;
272 if (allowInexactFwdSolve) *allowInexactFwdSolve = allowInexactFwdSolve_;
273 if (aztecAdjSolver) *aztecAdjSolver = aztecAdjSolver_;
274 if (allowInexactAdjSolve) *allowInexactAdjSolve = allowInexactAdjSolve_;
275 if (aztecSolverScalar) *aztecSolverScalar = aztecSolverScalar_;
277 fwdOp_ = Teuchos::null;
278 fwdOpSrc_ = Teuchos::null;
279 prec_ = Teuchos::null;
280 isExternalPrec_ =
false;
281 approxFwdOpSrc_ = Teuchos::null;
282 aztecFwdSolver_ = Teuchos::null;
283 allowInexactFwdSolve_ =
false;
284 aztecAdjSolver_ = Teuchos::null;
285 allowInexactAdjSolve_ =
false;
286 aztecSolverScalar_ = 0.0;
296 return ( fwdOp_.get() ? fwdOp_->range() : Teuchos::null );
303 return ( fwdOp_.get() ? fwdOp_->domain() : Teuchos::null );
310 return Teuchos::null;
319 std::ostringstream oss;
323 oss <<
"fwdOp="<<fwdOp_->description()<<
"";
337 using Teuchos::describe;
349 <<
"rangeDim=" << this->
range()->dim()
350 <<
",domainDim="<< this->
domain()->dim() <<
"}\n";
353 out <<
"fwdOp = " <<
describe(*fwdOp_,verbLevel);
356 out <<
"prec = " <<
describe(*prec_,verbLevel);
358 if (!
is_null(aztecFwdSolver_)) {
365 <<
"Aztec Fwd Mat = "
369 <<
"Aztec Fwd Prec Op = "
373 <<
"Aztec Fwd Prec Mat = "
376 if (!
is_null(aztecAdjSolver_)) {
383 <<
"Aztec Adj Mat = "
387 <<
"Aztec Adj Prec Op = "
391 <<
"Aztec Adj Prec Mat = "
410 return ::Thyra::opSupported(*fwdOp_,M_trans);
422 Thyra::apply( *fwdOp_, M_trans, X, Y, alpha, beta );
433 return (
nonnull(aztecAdjSolver_));
453 allowInexactFwdSolve_
464 allowInexactFwdSolve_
472 if (aztecAdjSolver_.
get()==NULL)
486 allowInexactFwdSolve_
497 allowInexactFwdSolve_
521 using Teuchos::rcpFromRef;
522 using Teuchos::rcpFromPtr;
527 THYRA_FUNC_TIME_MONITOR(
"Stratimikos: AztecOOLOWS");
529 totalTimer.start(
true);
533 OSTab tab = this->getOSTab();
535 *out <<
"\nSolving block system using AztecOO ...\n\n";
543 solveMeasureType = solveCriteria->solveMeasureType;
544 assertSupportsSolveMeasureType(*
this, M_trans, solveMeasureType);
556 aztecSolver = ( aztecOpTransp ==
NOTRANS ? aztecFwdSolver_ : aztecAdjSolver_ );
570 double tol = ( aztecOpTransp==
NOTRANS ? fwdDefaultTol() : adjDefaultTol() );
571 int maxIterations = ( aztecOpTransp==
NOTRANS
572 ? fwdDefaultMaxIterations() : adjDefaultMaxIterations() );
573 bool isDefaultSolveCriteria =
true;
575 if ( solveCriteria->requestedTol != SC::unspecifiedTolerance() ) {
576 tol = solveCriteria->requestedTol;
577 isDefaultSolveCriteria =
false;
579 if (
nonnull(solveCriteria->extraParameters)) {
580 maxIterations = solveCriteria->extraParameters->get(
"Maximum Iterations",maxIterations);
594 if (opWrapper == 0) {
603 int totalIterations = 0;
612 const int m = B.
domain()->dim();
614 for(
int j = 0; j < m; ++j ) {
616 THYRA_FUNC_TIME_MONITOR_DIFF(
"Stratimikos: AztecOOLOWS:SingleSolve", SingleSolve);
629 if (opWrapper == 0) {
630 epetra_b_j = rcpFromRef(*const_cast<Epetra_Vector*>((*epetra_B)(j)));
631 epetra_x_j = rcpFromRef(*(*epetra_X)(j));
646 aztecSolver->
SetRHS(&*epetra_b_j);
647 aztecSolver->
SetLHS(&*epetra_x_j);
655 setAztecSolveState(aztecSolver,out,verbLevel,solveMeasureType);
656 aztecSolver->
Iterate( maxIterations, tol );
667 if (aztecSolverScalar_ != 1.0)
668 epetra_x_j->Scale(1.0/aztecSolverScalar_);
673 if (opWrapper != 0) {
681 const int iterations = aztecSolver->
NumIters();
684 std::ostringstream oss;
685 bool converged =
false;
686 if (AZ_status[AZ_why]==AZ_normal) { oss <<
"Aztec returned AZ_normal."; converged =
true; }
687 else if (AZ_status[AZ_why]==AZ_param) oss <<
"Aztec returned AZ_param.";
688 else if (AZ_status[AZ_why]==AZ_breakdown) oss <<
"Aztec returned AZ_breakdown.";
689 else if (AZ_status[AZ_why]==AZ_loss) oss <<
"Aztec returned AZ_loss.";
690 else if (AZ_status[AZ_why]==AZ_ill_cond) oss <<
"Aztec returned AZ_ill_cond.";
691 else if (AZ_status[AZ_why]==AZ_maxits) oss <<
"Aztec returned AZ_maxits.";
692 else oss <<
"Aztec returned an unknown status?";
693 oss <<
" Iterations = " << iterations <<
".";
694 oss <<
" Achieved Tolerance = " << achievedTol <<
".";
696 if (out.
get() &&
static_cast<int>(verbLevel) > static_cast<int>(
Teuchos::VERB_NONE) && outputEveryRhs())
702 totalIterations += iterations;
704 solveStatus.
message = oss.str();
705 if ( isDefaultSolveCriteria ) {
727 epetra_X = Teuchos::null;
728 epetra_B = Teuchos::null;
735 if (isDefaultSolveCriteria) {
737 overallSolveStatus.
achievedTol = SS::unknownTolerance();
743 std::ostringstream oss;
747 <<
" on m = "<<m<<
" RHSs using " << totalIterations <<
" cumulative iterations"
748 <<
" for an average of " << (totalIterations/m) <<
" iterations/RHS and"
749 <<
" total CPU time of "<<totalTimer.totalElapsedTime()<<
" sec.";
750 overallSolveStatus.
message = oss.str();
769 <<
"\nTotal solve time = "<<totalTimer.totalElapsedTime()<<
" sec\n";
771 return overallSolveStatus;
RCP< Epetra_MultiVector > get_Epetra_MultiVector(const Epetra_Map &map, const RCP< MultiVectorBase< double > > &mv)
virtual bool opSupportedImpl(EOpTransp M_trans) const
bool is_null(const boost::shared_ptr< T > &p)
void copyEpetraIntoThyra(const Epetra_MultiVector &x, const Ptr< VectorBase< double > > &thyraVec) const
basic_OSTab< char > OSTab
RCP< T > create_weak() const
const double * GetAztecStatus() const
basic_FancyOStream< CharT, Traits > & o() const
RCP< const LinearOpSourceBase< double > > extract_fwdOpSrc()
Extract the forward LinearOpBase<double> object so that it can be modified.
bool isExternalPrec() const
Determine if the preconditioner was external or not.
double ScaledResidual() const
std::string description() const
EOpTransp real_trans(EOpTransp transp)
virtual bool solveSupportsSolveMeasureTypeImpl(EOpTransp M_trans, const SolveMeasureType &solveMeasureType) const
virtual const Epetra_Map & OperatorDomainMap() const =0
virtual void applyImpl(const EOpTransp M_trans, const MultiVectorBase< double > &X, const Ptr< MultiVectorBase< double > > &Y, const double alpha, const double beta) const
virtual bool solveSupportsImpl(EOpTransp M_trans) const
RCP< const VectorSpaceBase< double > > domain() const
virtual const Epetra_Map & OperatorRangeMap() const =0
void describe(Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel) const
void start(bool reset=false)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
int SetLHS(Epetra_MultiVector *X)
RCP< const VectorBase< Scalar > > col(Ordinal j) const
virtual std::string description() const
TypeTo implicit_cast(const TypeFrom &t)
Epetra_Operator * GetPrecOperator() const
virtual void setObjectLabel(const std::string &objectLabel)
int SetRHS(Epetra_MultiVector *B)
Epetra_RowMatrix * GetPrecMatrix() const
Epetra_Operator * GetUserOperator() const
void copyThyraIntoEpetra(const VectorBase< double > &thyraVec, Epetra_MultiVector &x) const
virtual RCP< const VectorSpaceBase< Scalar > > domain() const =0
bool nonnull(const boost::shared_ptr< T > &p)
void uninitialize(RCP< const LinearOpBase< double > > *fwdOp=NULL, RCP< const LinearOpSourceBase< double > > *fwdOpSrc=NULL, RCP< const PreconditionerBase< double > > *prec=NULL, bool *isExternalPrec=NULL, RCP< const LinearOpSourceBase< double > > *approxFwdOpSrc=NULL, RCP< AztecOO > *aztecFwdSolver=NULL, bool *allowInexactFwdSolve=NULL, RCP< AztecOO > *aztecAdjSolver=NULL, bool *allowInexactAdjSolve=NULL, double *aztecSolverScalar=NULL)
Uninitialize.
RCP< const PreconditionerBase< double > > extract_prec()
Extract the preconditioner.
Epetra_RowMatrix * GetUserMatrix() const
double totalElapsedTime(bool readCurrentTime=false) const
RCP< ParameterList > extraParameters
#define TEUCHOS_ASSERT(assertion_test)
void initialize(const RCP< const LinearOpBase< double > > &fwdOp, const RCP< const LinearOpSourceBase< double > > &fwdOpSrc, const RCP< const PreconditionerBase< double > > &prec, const bool isExternalPrec, const RCP< const LinearOpSourceBase< double > > &approxFwdOpSrc, const RCP< AztecOO > &aztecFwdSolver, const bool allowInexactFwdSolve=false, const RCP< AztecOO > &aztecAdjSolver=Teuchos::null, const bool allowInexactAdjSolve=false, const double aztecSolverScalar=1.0)
Sets up this object.
RCP< const LinearOpBase< double > > clone() const
int Iterate(long long MaxIters, double Tolerance)
RCP< const VectorSpaceBase< double > > range() const
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
SolveStatus< double > solveImpl(const EOpTransp M_trans, const MultiVectorBase< double > &B, const Ptr< MultiVectorBase< double > > &X, const Ptr< const SolveCriteria< double > > solveCriteria) const
std::string typeName(const T &t)
RCP< const LinearOpSourceBase< double > > extract_approxFwdOpSrc()
Extract the approximate forward LinearOpBase<double> object used to build the preconditioner.
AztecOOLinearOpWithSolve(const int fwdDefaultMaxIterations=400, const double fwdDefaultTol=1e-6, const int adjDefaultMaxIterations=400, const double adjDefaultTol=1e-6, const bool outputEveryRhs=false)