48 #include "Thyra_PreconditionerFactoryHelpers.hpp"
50 #include "Thyra_ScaledAdjointLinearOpBase.hpp"
53 #include "EpetraExt_ProductOperator.h"
63 const std::string AOOLOWSF_epetraPrecOp_str
64 =
"AOOLOWSF::epetraPrecOp";
65 const std::string AOOLOWSF_aztec_epetra_epetraFwdOp_str
66 =
"AOOLOWSF::aztec_epetra_epetraFwdOp";
67 const std::string AOOLOWSF_aztec_epetra_epetraAdjOp_str
68 =
"AOOLOWSF::aztec_epetra_epetraAdjOp";
69 const std::string AOOLOWSF_rowmatrix_epetraFwdOp_str
70 =
"AOOLOWSF::rowmatrix_epetraFwdOp";
71 const std::string AOOLOWSF_rowmatrix_epetraPrecOp_str
72 =
"AOOLOWSF::rowmatrix_epetraPrecOp";
73 const std::string AOOLOWSF_aztec_fwd_epetra_epetraPrecOp_str
74 =
"AOOLOWSF::aztec_fwd_epetra_epetraPrecOp";
75 const std::string AOOLOWSF_aztec_adj_epetra_epetraPrecOp_str
76 =
"AOOLOWSF::aztec_adj_epetra_epetraPrecOp";
77 const std::string AOOLOWSF_setPrecondtionerOperator_str
78 =
"AOOLOWSF::setPrecondtionerOperator";
79 const std::string AOOLOWSF_constructedAztecPreconditoner_str
80 =
"AOOLOWSF::constructedAztecPreconditoner";
83 const std::string ForwardSolve_name =
"Forward Solve";
84 const std::string AdjointSolve_name =
"Adjoint Solve";
85 const std::string MaxIterations_name =
"Max Iterations";
86 const int MaxIterations_default = 400;
87 const std::string Tolerance_name =
"Tolerance";
88 const double Tolerance_default = 1e-6;
89 const std::string OutputEveryRhs_name =
"Output Every RHS";
90 const bool OutputEveryRhs_default =
false;
91 const std::string AztecOO_Settings_name =
"AztecOO Settings";
107 ,defaultFwdMaxIterations_(MaxIterations_default)
108 ,defaultFwdTolerance_(Tolerance_default)
109 ,defaultAdjMaxIterations_(MaxIterations_default)
110 ,defaultAdjTolerance_(Tolerance_default)
111 ,outputEveryRhs_(OutputEveryRhs_default)
112 ,useAztecPrec_(false)
130 const Teuchos::RCP<PreconditionerFactoryBase<double> > &precFactory,
131 const std::string &precFactoryName
136 precFactoryValidPL = precFactory->getValidParameters();
137 const std::string _precFactoryName =
138 ( precFactoryName !=
""
140 : ( precFactoryValidPL.get()
141 ? precFactoryValidPL->name()
142 :
"GENERIC PRECONDITIONER FACTORY"
159 Teuchos::RCP<PreconditionerFactoryBase<double> > *precFactory,
160 std::string *precFactoryName
172 const LinearOpSourceBase<double> &fwdOpSrc
175 return epetraFwdOpViewExtractor_->isCompatible(*fwdOpSrc.getOp());
187 const Teuchos::RCP<
const LinearOpSourceBase<double> > &fwdOpSrc,
188 LinearOpWithSolveBase<double> *Op,
189 const ESupportSolveUse supportSolveUse
197 const Teuchos::RCP<
const LinearOpSourceBase<double> > &fwdOpSrc,
198 LinearOpWithSolveBase<double> *Op
206 const EPreconditionerInputType precOpType
213 "Aztec Preconditioner",
"none"
217 case PRECONDITIONER_INPUT_TYPE_AS_OPERATOR:
219 case PRECONDITIONER_INPUT_TYPE_AS_MATRIX:
229 const Teuchos::RCP<
const LinearOpSourceBase<double> > &fwdOpSrc,
230 const Teuchos::RCP<
const PreconditionerBase<double> > &prec,
231 LinearOpWithSolveBase<double> *Op,
232 const ESupportSolveUse supportSolveUse
241 const Teuchos::RCP<
const LinearOpSourceBase<double> > &fwdOpSrc,
242 const Teuchos::RCP<
const LinearOpSourceBase<double> > &approxFwdOpSrc,
243 LinearOpWithSolveBase<double> *Op,
244 const ESupportSolveUse supportSolveUse
254 LinearOpWithSolveBase<double> *Op,
255 Teuchos::RCP<
const LinearOpSourceBase<double> > *fwdOpSrc,
257 Teuchos::RCP<
const LinearOpSourceBase<double> > *approxFwdOpSrc,
258 ESupportSolveUse *supportSolveUse
265 *aztecOp = &Teuchos::dyn_cast<AztecOOLinearOpWithSolve>(*Op);
270 if(fwdOpSrc) *fwdOpSrc = _fwdOpSrc;
271 if(approxFwdOpSrc) *approxFwdOpSrc = _approxFwdOpSrc;
278 if(prec) *prec = _prec;
319 if( nestedPFSublistExists || !alreadyHasSublist ) {
323 Teuchos::readVerboseObjectSublist(&*
paramList_,
this);
362 std::ostringstream oss;
363 oss <<
"Thyra::AztecOOLinearOpWithSolveFactory{";
364 oss <<
"precFactory=";
381 if(validParamList.
get()==NULL) {
385 OutputEveryRhs_name,OutputEveryRhs_default
386 ,
"Determines if output is created for each individual RHS (true or 1) or if output\n"
387 "is just created for an entire set of RHSs (false or 0)."
392 &fwdSolvePL = validParamList->
sublist(
393 ForwardSolve_name,
false
394 ,
"Gives the options for the forward solve."
397 Tolerance_name,Tolerance_default
398 ,
"The tolerence used in the convergence check (see the convergence test\n"
399 "in the sublist \"" + AztecOO_Settings_name +
"\")"
402 MaxIterations_name,MaxIterations_default
403 ,
"The maximum number of iterations the AztecOO solver is allowed to perform."
406 AztecOO_Settings_name,
false
407 ,
"Sets the parameters on the AztecOO object itself."
408 ).setParameters(*aztecParamList);
410 &adjSolvePL = validParamList->
sublist(
411 AdjointSolve_name,
false
412 ,
"The options for the adjoint solve.\n"
413 "If this sublist is missing then the parameters from the\n"
414 "\""+ForwardSolve_name+
"\" sublist are used instead."
419 return validParamList;
430 precFactoryValidParamList =
precFactory_->getValidParameters();
431 if(precFactoryValidParamList.
get()) {
433 *precFactoryValidParamList);
441 const Teuchos::RCP<
const LinearOpSourceBase<double> > &fwdOpSrc,
442 const Teuchos::RCP<
const PreconditionerBase<double> > &prec,
443 const Teuchos::RCP<
const LinearOpSourceBase<double> > &approxFwdOpSrc,
444 const bool reusePrec,
445 LinearOpWithSolveBase<double> *Op
451 using Teuchos::rcp_dynamic_cast;
452 using Teuchos::rcp_const_cast;
453 using Teuchos::set_extra_data;
454 using Teuchos::get_optional_extra_data;
455 using Teuchos::get_optional_nonconst_extra_data;
456 using Teuchos::outArg;
457 typedef EpetraExt::ProductOperator PO;
463 *out <<
"\nEntering Thyra::AztecOOLinearOpWithSolveFactory::initializeOp_impl(...) ...\n";
466 VOTSPF precFactoryOutputTempState(
precFactory_,out,verbLevel);
480 tmpFwdOp = fwdOpSrc->getOp(),
481 tmpApproxFwdOp = ( approxFwdOpSrc.get() ? approxFwdOpSrc->getOp() : Teuchos::null );
484 if ( dynamic_cast<const EpetraLinearOpBase*>(tmpFwdOp.get())!=0 )
487 approxFwdOp = tmpApproxFwdOp;
491 fwdOp = makeEpetraWrapper(tmpFwdOp);
495 dynamic_cast<const EpetraLinearOpBase*>(&*tmpApproxFwdOp.get())
498 approxFwdOp = makeEpetraWrapper(tmpApproxFwdOp);
506 *aztecOp = &Teuchos::dyn_cast<AztecOOLinearOpWithSolve>(*Op);
512 EOpTransp epetra_epetraFwdOpTransp;
515 double epetra_epetraFwdOpScalar;
516 epetraFwdOpViewExtractor_->getEpetraOpView(
518 outArg(epetra_epetraFwdOp), outArg(epetra_epetraFwdOpTransp),
519 outArg(epetra_epetraFwdOpApplyAs), outArg(epetra_epetraFwdOpAdjointSupport),
520 outArg(epetra_epetraFwdOpScalar)
523 epetra_epetraFwdOp.
get()==NULL, std::logic_error
524 ,
"Error, The input fwdOp object must be fully initialized "
525 "before calling this function!"
542 ? Teuchos::rcp_const_cast<PreconditionerBase<double> >(
561 if (precUsed.
get()) {
566 !( left.
get() || right.
get() || unspecified.
get() ), std::logic_error
567 ,
"Error, at least one preconditoner linear operator objects must be set!"
569 if(unspecified.
get()) {
570 rightPrecOp = unspecified;
575 left.
get(),std::logic_error
576 ,
"Error, we can not currently handle a left"
577 " preconditioner with the AztecOO/Thyra adapters!"
582 double wrappedPrecOpScalar = 0.0;
583 EOpTransp wrappedPrecOpTransp =
NOTRANS;
587 EOpTransp epetra_epetraPrecOpTransp;
590 EOpTransp overall_epetra_epetraPrecOpTransp=
NOTRANS;
591 if(rightPrecOp.
get()) {
594 rightPrecOp,&wrappedPrecOpScalar,&wrappedPrecOpTransp,&tmpWrappedPrecOp);
595 if( dynamic_cast<const EpetraLinearOpBase*>(&*tmpWrappedPrecOp) ) {
596 wrappedPrecOp = tmpWrappedPrecOp;
599 wrappedPrecOp = makeEpetraWrapper(tmpWrappedPrecOp);
601 epetraPrecOp = rcp_dynamic_cast<const EpetraLinearOpBase>(
603 epetraPrecOp->getEpetraOpView(
604 outArg(epetra_epetraPrecOp), outArg(epetra_epetraPrecOpTransp),
605 outArg(epetra_epetraPrecOpApplyAs), outArg(epetra_epetraPrecOpAdjointSupport));
607 epetra_epetraPrecOp.
get()==NULL,std::logic_error
608 ,
"Error, The input prec object and its embedded preconditioner"
609 " operator must be fully initialized before calling this function!"
618 overall_epetra_epetraPrecOpTransp
620 real_trans(wrappedPrecOpTransp),
621 real_trans(epetra_epetraPrecOpTransp)
629 if(approxFwdOp.
get()) {
632 unwrap(approxFwdOp,&wrappedPrecOpScalar,&wrappedPrecOpTransp,&wrappedPrecOp);
633 epetraPrecOp = rcp_dynamic_cast<const EpetraLinearOpBase>(
635 epetraPrecOp->getEpetraOpView(
636 outArg(epetra_epetraPrecOp), outArg(epetra_epetraPrecOpTransp),
637 outArg(epetra_epetraPrecOpApplyAs), outArg(epetra_epetraPrecOpAdjointSupport)
640 epetra_epetraPrecOp.
get()==NULL,std::logic_error
641 ,
"Error, The input approxFwdOp object must be fully initialized"
642 " before calling this function!"
652 overall_epetra_epetraPrecOpTransp
654 real_trans(wrappedPrecOpTransp),
655 real_trans(epetra_epetraPrecOpTransp)
664 rowmatrix_epetraFwdOp = rcp_dynamic_cast<const Epetra_RowMatrix>(
666 rowmatrix_epetraPrecOp = rcp_dynamic_cast<const Epetra_RowMatrix>(
667 epetra_epetraPrecOp);
673 enum ELocalPrecType {
674 PT_NONE, PT_AZTEC_FROM_OP, PT_AZTEC_FROM_APPROX_FWD_MATRIX,
675 PT_FROM_PREC_OP, PT_UPPER_BOUND
677 ELocalPrecType localPrecType = PT_UPPER_BOUND;
680 localPrecType = PT_NONE;
685 localPrecType = PT_AZTEC_FROM_OP;
690 localPrecType = PT_AZTEC_FROM_APPROX_FWD_MATRIX;
692 else if( precUsed.
get() ) {
695 localPrecType = PT_FROM_PREC_OP;
698 (localPrecType == PT_UPPER_BOUND, std::logic_error,
699 "AztecOOLinearOpWithSolveFactory::initializeOp_impl(...): "
700 "localPrecType == PT_UPPER_BOUND. This means that previously, "
701 "this value might have been used uninitialized. "
702 "Please report this bug to the Stratimikos developers.");
717 bool old_isExternalPrec;
721 double old_aztecSolverScalar;
732 ,&old_aztecSolverScalar
734 if( old_aztecFwdSolver.
get()==NULL ) {
742 aztecFwdSolver = old_aztecFwdSolver;
743 aztecAdjSolver = old_aztecAdjSolver;
744 startingOver =
false;
747 Ptr<bool> constructedAztecPreconditioner;
751 !
is_null(constructedAztecPreconditioner = get_optional_nonconst_extra_data<bool>(
752 aztecFwdSolver,
"AOOLOWSF::constructedAztecPreconditoner") )
754 *constructedAztecPreconditioner
757 aztecFwdSolver->DestroyPreconditioner();
758 *constructedAztecPreconditioner =
false;
762 Ptr<bool> setPreconditionerOperator;
764 localPrecType != PT_FROM_PREC_OP
765 && !
is_null( setPreconditionerOperator = get_optional_nonconst_extra_data<bool>(
766 aztecFwdSolver,
"AOOLOWSF::setPreconditonerOperator") )
767 && *setPreconditionerOperator
783 aztecFwdSolver =
rcp(
new AztecOO());
784 aztecFwdSolver->SetAztecOption(AZ_diagnostics,AZ_none);
785 aztecFwdSolver->SetAztecOption(AZ_keep_info,1);
790 localPrecType!=PT_AZTEC_FROM_OP && localPrecType!=PT_AZTEC_FROM_APPROX_FWD_MATRIX
793 aztecAdjSolver =
rcp(
new AztecOO());
794 aztecAdjSolver->SetAztecOption(AZ_diagnostics,AZ_none);
819 aztec_epetra_epetraFwdOp,
820 aztec_epetra_epetraAdjOp;
824 = { epetra_epetraFwdOp };
831 ? PO::APPLY_MODE_APPLY
832 : PO::APPLY_MODE_APPLY_INVERSE };
836 epetraOpsApplyMode[0] == PO::APPLY_MODE_APPLY
839 aztec_epetra_epetraFwdOp = epetra_epetraFwdOp;
843 aztec_epetra_epetraFwdOp =
rcp(
844 new PO(1,epetraOps,epetraOpsTransp,epetraOpsApplyMode));
849 aztec_epetra_epetraFwdOp.
get() != aztecFwdSolver->GetUserOperator()
854 aztecFwdSolver->SetUserOperator(
855 const_cast<Epetra_Operator*>(&*aztec_epetra_epetraFwdOp));
857 aztec_epetra_epetraFwdOp, AOOLOWSF_aztec_epetra_epetraFwdOp_str,
862 if( aztecAdjSolver.
get() ) {
863 epetraOpsTransp[0] = (
864 epetra_epetraFwdOpTransp==
NOTRANS
871 epetraOpsApplyMode[0] == PO::APPLY_MODE_APPLY
874 aztec_epetra_epetraAdjOp = epetra_epetraFwdOp;
877 aztec_epetra_epetraAdjOp =
rcp(
878 new PO(1,epetraOps,epetraOpsTransp,epetraOpsApplyMode));
880 aztecAdjSolver->SetUserOperator(
881 const_cast<Epetra_Operator*>(&*aztec_epetra_epetraAdjOp));
883 aztec_epetra_epetraAdjOp, AOOLOWSF_aztec_epetra_epetraAdjOp_str,
892 aztec_fwd_epetra_epetraPrecOp,
893 aztec_adj_epetra_epetraPrecOp;
894 bool setAztecPreconditioner =
false;
895 switch(localPrecType) {
902 case PT_AZTEC_FROM_OP: {
907 if( startingOver || !reusePrec ) {
909 rowmatrix_epetraFwdOp.
get()==NULL, std::logic_error,
910 "AztecOOLinearOpWithSolveFactory::initializeOp_impl(...): "
911 "Error, There is no preconditioner given by client, but the client "
912 "passed in an Epetra_Operator for the forward operator of type \'"
913 <<
typeName(*epetra_epetraFwdOp)<<
"\' that does not "
914 "support the Epetra_RowMatrix interface!"
917 epetra_epetraFwdOpTransp!=
NOTRANS, std::logic_error,
918 "AztecOOLinearOpWithSolveFactory::initializeOp_impl(...):"
919 " Error, There is no preconditioner given by client and the client "
920 "passed in an Epetra_RowMatrix for the forward operator but the "
921 "overall transpose is not NOTRANS and therefore we can can just "
922 "hand this over to aztec without making a copy which is not supported here!"
924 aztecFwdSolver->SetPrecMatrix(
925 const_cast<Epetra_RowMatrix*>(&*rowmatrix_epetraFwdOp));
927 rowmatrix_epetraFwdOp, AOOLOWSF_rowmatrix_epetraFwdOp_str,
931 setAztecPreconditioner =
true;
934 case PT_AZTEC_FROM_APPROX_FWD_MATRIX: {
939 if( startingOver || !reusePrec ) {
941 rowmatrix_epetraPrecOp.get()==NULL, std::logic_error
942 ,
"AztecOOLinearOpWithSolveFactor::initializeOp_impl(...): The client "
943 "passed in an Epetra_Operator for the preconditioner matrix of type \'"
944 <<
typeName(*epetra_epetraPrecOp)<<
"\' that does not "
945 "support the Epetra_RowMatrix interface!"
948 overall_epetra_epetraPrecOpTransp!=
NOTRANS, std::logic_error
949 ,
"AztecOOLinearOpWithSolveFactor::initializeOp_impl(...): Error, The client "
950 "passed in an Epetra_RowMatrix for the preconditoner matrix but the overall "
951 "transpose is not NOTRANS and therefore we can can just "
952 "hand this over to aztec without making a copy which is not supported here!"
954 aztecFwdSolver->SetPrecMatrix(
955 const_cast<Epetra_RowMatrix*>(&*rowmatrix_epetraPrecOp));
957 rowmatrix_epetraPrecOp, AOOLOWSF_rowmatrix_epetraPrecOp_str,
961 setAztecPreconditioner =
true;
964 case PT_FROM_PREC_OP: {
971 = { epetra_epetraPrecOp };
974 = { overall_epetra_epetraPrecOpTransp==
NOTRANS
980 theEpetraOpsApplyMode[]
982 ? PO::APPLY_MODE_APPLY_INVERSE
983 : PO::APPLY_MODE_APPLY };
990 aztec_fwd_epetra_epetraPrecOp = epetra_epetraPrecOp;
993 aztec_fwd_epetra_epetraPrecOp =
rcp(
new PO(1,theEpetraOps,theEpetraOpsTransp,theEpetraOpsApplyMode));
995 aztecFwdSolver->SetPrecOperator(
996 const_cast<Epetra_Operator*>(&*aztec_fwd_epetra_epetraPrecOp));
998 aztec_fwd_epetra_epetraPrecOp, AOOLOWSF_aztec_fwd_epetra_epetraPrecOp_str,
1003 aztecAdjSolver.
get()
1008 theEpetraOpsTransp[0] = (
1009 overall_epetra_epetraPrecOpTransp==
NOTRANS
1019 aztec_adj_epetra_epetraPrecOp = epetra_epetraPrecOp;
1022 aztec_adj_epetra_epetraPrecOp =
rcp(
1023 new PO(1,theEpetraOps,theEpetraOpsTransp,theEpetraOpsApplyMode));
1025 aztecAdjSolver->SetPrecOperator(
1026 const_cast<Epetra_Operator*>(&*aztec_adj_epetra_epetraPrecOp));
1028 aztec_adj_epetra_epetraPrecOp, AOOLOWSF_aztec_adj_epetra_epetraPrecOp_str,
1031 set_extra_data<bool>(
1032 true, AOOLOWSF_setPrecondtionerOperator_str,
1045 if(setAztecPreconditioner) {
1046 if( startingOver || !reusePrec ) {
1047 double condNumEst = -1.0;
1050 set_extra_data<bool>(
1051 true, AOOLOWSF_constructedAztecPreconditoner_str,
1063 if(aztecAdjSolver.
get() && aztecAdjSolver->GetPrecOperator()) {
1065 fwdOp, fwdOpSrc,precUsed, prec.
get()!=NULL, approxFwdOpSrc,
1066 aztecFwdSolver,
true, aztecAdjSolver,
true, epetra_epetraFwdOpScalar
1071 fwdOp, fwdOpSrc, precUsed, prec.
get()!=NULL, approxFwdOpSrc,
1072 aztecFwdSolver,
true,
null,
false, epetra_epetraFwdOpScalar
1080 aztecOp->setOStream(this->getOStream());
1081 if(!
is_null(this->getOverridingOStream()))
1082 aztecOp->setOverridingOStream(this->getOverridingOStream());
1083 aztecOp->setVerbLevel(this->getVerbLevel());
1085 #ifdef TEUCHOS_DEBUG
1091 *out <<
"\nLeaving Thyra::AztecOOLinearOpWithSolveFactory::initializeOp_impl(...) ...\n";