43 #include "Thyra_LinearOpWithSolveHelpers.hpp"
79 class SetAztecSolveState {
85 const Thyra::SolveMeasureType &solveMeasureType
87 ~SetAztecSolveState();
98 SetAztecSolveState::SetAztecSolveState(
102 const Thyra::SolveMeasureType &solveMeasureType
104 :aztecSolver_(aztecSolver.assert_not_null())
109 verbLevel_ = verbLevel;
115 fancyOStream_= Teuchos::tab(
120 aztecSolver_->SetOutputStream(*fancyOStream_);
121 aztecSolver_->SetErrorStream(*fancyOStream_);
131 outputFrequency_ = aztecSolver_->GetAllAztecOptions()[AZ_output];
132 aztecSolver_->SetAztecOption(AZ_output,0);
136 convergenceTest_ = aztecSolver_->GetAztecOption(AZ_conv);
137 if (solveMeasureType.useDefault())
143 Thyra::SOLVE_MEASURE_NORM_RESIDUAL,
144 Thyra::SOLVE_MEASURE_NORM_RHS
148 aztecSolver_->SetAztecOption(AZ_conv,AZ_rhs);
152 Thyra::SOLVE_MEASURE_NORM_RESIDUAL,
153 Thyra::SOLVE_MEASURE_NORM_INIT_RESIDUAL
157 aztecSolver_->SetAztecOption(AZ_conv,AZ_r0);
166 SetAztecSolveState::~SetAztecSolveState()
172 aztecSolver_->SetOutputStream(std::cout);
173 aztecSolver_->SetErrorStream(std::cerr);
174 *fancyOStream_ <<
"\n";
178 aztecSolver_->SetAztecOption(AZ_output,outputFrequency_);
182 aztecSolver_->SetAztecOption(AZ_conv,convergenceTest_);
197 const int fwdDefaultMaxIterations_in
198 ,
const double fwdDefaultTol_in
199 ,
const int adjDefaultMaxIterations_in
200 ,
const double adjDefaultTol_in
201 ,
const bool outputEveryRhs_in
203 :fwdDefaultMaxIterations_(fwdDefaultMaxIterations_in)
204 ,fwdDefaultTol_(fwdDefaultTol_in)
205 ,adjDefaultMaxIterations_(adjDefaultMaxIterations_in)
206 ,adjDefaultTol_(adjDefaultTol_in)
207 ,outputEveryRhs_(outputEveryRhs_in)
208 ,isExternalPrec_(false)
209 ,allowInexactFwdSolve_(false)
210 ,allowInexactAdjSolve_(false)
211 ,aztecSolverScalar_(0.0)
216 const RCP<
const LinearOpBase<double> > &fwdOp
217 ,
const RCP<
const LinearOpSourceBase<double> > &fwdOpSrc
218 ,
const RCP<
const PreconditionerBase<double> > &prec
219 ,
const bool isExternalPrec_in
220 ,
const RCP<
const LinearOpSourceBase<double> > &approxFwdOpSrc
222 ,
const bool allowInexactFwdSolve
224 ,
const bool allowInexactAdjSolve
225 ,
const double aztecSolverScalar
243 const std::string fwdOpLabel = fwdOp_->getObjectLabel();
244 if (fwdOpLabel.length())
245 this->setObjectLabel(
"lows("+fwdOpLabel+
")" );
281 return _approxFwdOpSrc;
286 RCP<
const LinearOpBase<double> > *fwdOp,
287 RCP<
const LinearOpSourceBase<double> > *fwdOpSrc,
288 RCP<
const PreconditionerBase<double> > *prec,
289 bool *isExternalPrec_inout,
290 RCP<
const LinearOpSourceBase<double> > *approxFwdOpSrc,
292 bool *allowInexactFwdSolve,
294 bool *allowInexactAdjSolve,
295 double *aztecSolverScalar
298 if (fwdOp) *fwdOp =
fwdOp_;
300 if (prec) *prec =
prec_;
351 std::ostringstream oss;
355 oss <<
"fwdOp="<<
fwdOp_->description()<<
"";
369 using Teuchos::describe;
381 <<
"rangeDim=" << this->
range()->dim()
382 <<
",domainDim="<< this->
domain()->dim() <<
"}\n";
397 <<
"Aztec Fwd Mat = "
401 <<
"Aztec Fwd Prec Op = "
405 <<
"Aztec Fwd Prec Mat = "
415 <<
"Aztec Adj Mat = "
419 <<
"Aztec Adj Prec Op = "
423 <<
"Aztec Adj Prec Mat = "
442 return ::Thyra::opSupported(*
fwdOp_,M_trans);
447 const EOpTransp M_trans,
448 const MultiVectorBase<double> &X,
449 const Ptr<MultiVectorBase<double> > &Y,
454 Thyra::apply( *
fwdOp_, M_trans, X, Y, alpha, beta );
464 if (real_trans(M_trans)==
NOTRANS)
return true;
471 EOpTransp M_trans,
const SolveMeasureType& solveMeasureType
474 if (real_trans(M_trans)==
NOTRANS) {
475 if (solveMeasureType.useDefault())
481 SOLVE_MEASURE_NORM_RESIDUAL,
482 SOLVE_MEASURE_NORM_RHS
492 SOLVE_MEASURE_NORM_RESIDUAL,
493 SOLVE_MEASURE_NORM_INIT_RESIDUAL
508 else if (solveMeasureType.useDefault())
514 SOLVE_MEASURE_NORM_RESIDUAL,
515 SOLVE_MEASURE_NORM_RHS
525 SOLVE_MEASURE_NORM_RESIDUAL,
526 SOLVE_MEASURE_NORM_INIT_RESIDUAL
545 const EOpTransp M_trans,
546 const MultiVectorBase<double> &
B,
547 const Ptr<MultiVectorBase<double> > &X,
548 const Ptr<
const SolveCriteria<double> > solveCriteria
553 using Teuchos::rcpFromRef;
554 using Teuchos::rcpFromPtr;
556 typedef SolveCriteria<double> SC;
557 typedef SolveStatus<double> SS;
559 THYRA_FUNC_TIME_MONITOR(
"Stratimikos: AztecOOLOWS");
561 totalTimer.start(
true);
565 OSTab tab = this->getOSTab();
567 *out <<
"\nSolving block system using AztecOO ...\n\n";
573 SolveMeasureType solveMeasureType;
575 solveMeasureType = solveCriteria->solveMeasureType;
576 assertSupportsSolveMeasureType(*
this, M_trans, solveMeasureType);
582 const EOpTransp aztecOpTransp = real_trans(M_trans);
589 const Epetra_Operator
590 *aztecOp = aztecSolver->GetUserOperator();
596 &opRangeMap = aztecOp->OperatorRangeMap(),
597 &opDomainMap = aztecOp->OperatorDomainMap();
602 double tol = ( aztecOpTransp==
NOTRANS ? fwdDefaultTol() : adjDefaultTol() );
603 int maxIterations = ( aztecOpTransp==
NOTRANS
604 ? fwdDefaultMaxIterations() : adjDefaultMaxIterations() );
605 bool isDefaultSolveCriteria =
true;
607 if ( solveCriteria->requestedTol != SC::unspecifiedTolerance() ) {
608 tol = solveCriteria->requestedTol;
609 isDefaultSolveCriteria =
false;
611 if (
nonnull(solveCriteria->extraParameters)) {
612 maxIterations = solveCriteria->extraParameters->
get(
"Maximum Iterations",maxIterations);
626 if (opWrapper == 0) {
635 int totalIterations = 0;
636 SolveStatus<double> solveStatus;
637 solveStatus.solveStatus = SOLVE_STATUS_CONVERGED;
638 solveStatus.achievedTol = -1.0;
644 const int m = B.domain()->dim();
646 for(
int j = 0; j < m; ++j ) {
648 THYRA_FUNC_TIME_MONITOR_DIFF(
"Stratimikos: AztecOOLOWS:SingleSolve", SingleSolve);
661 if (opWrapper == 0) {
662 epetra_b_j = rcpFromRef(*const_cast<Epetra_Vector*>((*epetra_B)(j)));
663 epetra_x_j = rcpFromRef(*(*epetra_X)(j));
667 epetra_b_j =
rcp(
new Epetra_Vector(opRangeMap));
668 epetra_x_j =
rcp(
new Epetra_Vector(opDomainMap));
678 aztecSolver->SetRHS(&*epetra_b_j);
679 aztecSolver->SetLHS(&*epetra_x_j);
687 setAztecSolveState(aztecSolver,out,verbLevel,solveMeasureType);
688 aztecSolver->Iterate( maxIterations, tol );
705 if (opWrapper != 0) {
713 const int iterations = aztecSolver->NumIters();
714 const double achievedTol = aztecSolver->ScaledResidual();
715 const double *AZ_status = aztecSolver->GetAztecStatus();
716 std::ostringstream oss;
717 bool converged =
false;
718 if (AZ_status[AZ_why]==AZ_normal) { oss <<
"Aztec returned AZ_normal."; converged =
true; }
719 else if (AZ_status[AZ_why]==AZ_param) oss <<
"Aztec returned AZ_param.";
720 else if (AZ_status[AZ_why]==AZ_breakdown) oss <<
"Aztec returned AZ_breakdown.";
721 else if (AZ_status[AZ_why]==AZ_loss) oss <<
"Aztec returned AZ_loss.";
722 else if (AZ_status[AZ_why]==AZ_ill_cond) oss <<
"Aztec returned AZ_ill_cond.";
723 else if (AZ_status[AZ_why]==AZ_maxits) oss <<
"Aztec returned AZ_maxits.";
724 else oss <<
"Aztec returned an unknown status?";
725 oss <<
" Iterations = " << iterations <<
".";
726 oss <<
" Achieved Tolerance = " << achievedTol <<
".";
728 if (out.
get() &&
static_cast<int>(verbLevel) > static_cast<int>(
Teuchos::VERB_NONE) && outputEveryRhs())
731 solveStatus.achievedTol =
TEUCHOS_MAX(solveStatus.achievedTol, achievedTol);
734 totalIterations += iterations;
736 solveStatus.message = oss.str();
737 if ( isDefaultSolveCriteria ) {
738 switch(solveStatus.solveStatus) {
739 case SOLVE_STATUS_UNKNOWN:
742 case SOLVE_STATUS_CONVERGED:
743 solveStatus.solveStatus = ( converged ? SOLVE_STATUS_CONVERGED : SOLVE_STATUS_UNCONVERGED );
745 case SOLVE_STATUS_UNCONVERGED:
754 aztecSolver->UnsetLHSRHS();
766 SolveStatus<double> overallSolveStatus;
767 if (isDefaultSolveCriteria) {
768 overallSolveStatus.solveStatus = SOLVE_STATUS_UNKNOWN;
769 overallSolveStatus.achievedTol = SS::unknownTolerance();
772 overallSolveStatus.solveStatus = solveStatus.solveStatus;
773 overallSolveStatus.achievedTol = solveStatus.achievedTol;
775 std::ostringstream oss;
778 << ( overallSolveStatus.solveStatus==SOLVE_STATUS_CONVERGED ?
"converged" :
"unconverged" )
779 <<
" on m = "<<m<<
" RHSs using " << totalIterations <<
" cumulative iterations"
780 <<
" for an average of " << (totalIterations/m) <<
" iterations/RHS and"
781 <<
" total CPU time of "<<totalTimer.totalElapsedTime()<<
" sec.";
782 overallSolveStatus.message = oss.str();
785 if (overallSolveStatus.extraParameters.is_null()) {
786 overallSolveStatus.extraParameters = Teuchos::parameterList ();
788 overallSolveStatus.extraParameters->set (
"AztecOO/Iteration Count",
791 overallSolveStatus.extraParameters->set (
"Iteration Count",
793 overallSolveStatus.extraParameters->set (
"AztecOO/Achieved Tolerance",
794 overallSolveStatus.achievedTol);
801 <<
"\nTotal solve time = "<<totalTimer.totalElapsedTime()<<
" sec\n";
803 return overallSolveStatus;
RCP< Epetra_MultiVector > get_Epetra_MultiVector(const Epetra_Map &map, const RCP< MultiVectorBase< double > > &mv)
virtual bool opSupportedImpl(EOpTransp M_trans) const
std::string typeName(const T &t)
bool is_null(const boost::shared_ptr< T > &p)
void copyEpetraIntoThyra(const Epetra_MultiVector &x, const Ptr< VectorBase< double > > &thyraVec) const
RCP< const PreconditionerBase< double > > prec_
RCP< T > create_weak() 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.
RCP< AztecOO > aztecFwdSolver_
bool isExternalPrec() const
Determine if the preconditioner was external or not.
std::string description() const
virtual bool solveSupportsSolveMeasureTypeImpl(EOpTransp M_trans, const SolveMeasureType &solveMeasureType) const
virtual void applyImpl(const EOpTransp M_trans, const MultiVectorBase< double > &X, const Ptr< MultiVectorBase< double > > &Y, const double alpha, const double beta) const
basic_OSTab< char > OSTab
virtual bool solveSupportsImpl(EOpTransp M_trans) const
RCP< const VectorSpaceBase< double > > domain() const
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)
EpetraExt::ModelEvaluator::MPDerivative convert(const ModelEvaluatorBase::MPDerivative &derivative, const RCP< const Epetra_Map > &fnc_map, const RCP< const Epetra_Map > &var_map)
bool allowInexactAdjSolve_
virtual std::string description() const
bool allowInexactFwdSolve_
#define TEUCHOS_MAX(x, y)
TypeTo implicit_cast(const TypeFrom &t)
double aztecSolverScalar_
RCP< const LinearOpBase< double > > fwdOp_
RCP< const LinearOpSourceBase< double > > fwdOpSrc_
void copyThyraIntoEpetra(const VectorBase< double > &thyraVec, Epetra_MultiVector &x) const
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.
double totalElapsedTime(bool readCurrentTime=false) const
#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
RCP< const LinearOpSourceBase< double > > approxFwdOpSrc_
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
RCP< AztecOO > aztecAdjSolver_
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)