47 #ifndef THYRA_MUELU_PRECONDITIONER_FACTORY_DECL_HPP 48 #define THYRA_MUELU_PRECONDITIONER_FACTORY_DECL_HPP 52 #ifdef HAVE_MUELU_STRATIMIKOS 55 #include "Thyra_DefaultPreconditioner.hpp" 56 #include "Thyra_BlockedLinearOpBase.hpp" 58 #ifdef HAVE_MUELU_TPETRA 59 #include "Thyra_TpetraLinearOp.hpp" 60 #include "Thyra_TpetraThyraWrappers.hpp" 62 #ifdef HAVE_MUELU_EPETRA 63 #include "Thyra_EpetraLinearOp.hpp" 66 #include "Teuchos_Ptr.hpp" 67 #include "Teuchos_TestForException.hpp" 68 #include "Teuchos_Assert.hpp" 69 #include "Teuchos_Time.hpp" 76 #include <MueLu_Hierarchy.hpp> 78 #include <MueLu_HierarchyUtils.hpp> 79 #include <MueLu_Utilities.hpp> 80 #include <MueLu_ParameterListInterpreter.hpp> 81 #include <MueLu_MLParameterListInterpreter.hpp> 85 #ifdef HAVE_MUELU_TPETRA 86 #include <MueLu_TpetraOperator.hpp> 88 #ifdef HAVE_MUELU_EPETRA 92 #include "Thyra_PreconditionerFactoryBase.hpp" 107 template <
class Scalar,
class LocalOrdinal,
class GlobalOrdinal,
class Node = KokkosClassic::DefaultNode::DefaultNodeType>
122 bool isCompatible(
const LinearOpSourceBase<Scalar>& fwdOp)
const;
124 Teuchos::RCP<PreconditionerBase<Scalar> >
createPrec()
const;
126 void initializePrec(
const Teuchos::RCP<
const LinearOpSourceBase<Scalar> >& fwdOp,
127 PreconditionerBase<Scalar>* prec,
128 const ESupportSolveUse supportSolveUse
132 Teuchos::RCP<
const LinearOpSourceBase<Scalar> >* fwdOp,
133 ESupportSolveUse* supportSolveUse
142 void setParameterList(
const Teuchos::RCP<Teuchos::ParameterList>& paramList);
171 #ifdef HAVE_MUELU_EPETRA 200 const RCP<const LinearOpBase<Scalar> > fwdOp = fwdOpSrc.getOp();
202 #ifdef HAVE_MUELU_TPETRA 203 if (Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::isTpetra(fwdOp))
return true;
206 #ifdef HAVE_MUELU_EPETRA 207 if (Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::isEpetra(fwdOp))
return true;
210 if (Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::isBlockedOperator(fwdOp))
return true;
216 Teuchos::RCP<PreconditionerBase<Scalar> >
createPrec()
const {
217 return Teuchos::rcp(
new DefaultPreconditioner<Scalar>);
221 void initializePrec(
const Teuchos::RCP<
const LinearOpSourceBase<Scalar> >& fwdOpSrc,
222 PreconditionerBase<Scalar>* prec,
223 const ESupportSolveUse supportSolveUse
225 using Teuchos::rcp_dynamic_cast;
230 typedef Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node> XpThyUtils;
236 typedef Thyra::LinearOpBase<Scalar> ThyLinOpBase;
237 #ifdef HAVE_MUELU_TPETRA 239 #if ((defined(EPETRA_HAVE_OMP) && (defined(HAVE_TPETRA_INST_OPENMP) && defined(HAVE_TPETRA_INST_INT_INT))) || \ 240 (!defined(EPETRA_HAVE_OMP) && (defined(HAVE_TPETRA_INST_SERIAL) && defined(HAVE_TPETRA_INST_INT_INT)))) 242 typedef Tpetra::Operator<Scalar,LocalOrdinal,GlobalOrdinal,Node> TpOp;
243 typedef Thyra::TpetraLinearOp<Scalar,LocalOrdinal,GlobalOrdinal,Node> ThyTpLinOp;
246 #if defined(HAVE_MUELU_EPETRA) 247 typedef MueLu::EpetraOperator MueEpOp;
248 typedef Thyra::EpetraLinearOp ThyEpLinOp;
256 TEUCHOS_ASSERT(Teuchos::nonnull(fwdOpSrc));
258 TEUCHOS_ASSERT(prec);
264 const RCP<const ThyLinOpBase> fwdOp = fwdOpSrc->getOp();
265 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(fwdOp));
268 bool bIsEpetra = XpThyUtils::isEpetra(fwdOp);
269 bool bIsTpetra = XpThyUtils::isTpetra(fwdOp);
270 bool bIsBlocked = XpThyUtils::isBlockedOperator(fwdOp);
271 TEUCHOS_TEST_FOR_EXCEPT((bIsEpetra ==
true && bIsTpetra ==
true));
272 TEUCHOS_TEST_FOR_EXCEPT((bIsEpetra == bIsTpetra) && bIsBlocked ==
false);
273 TEUCHOS_TEST_FOR_EXCEPT((bIsEpetra != bIsTpetra) && bIsBlocked ==
true);
275 RCP<XpMat> A = Teuchos::null;
277 Teuchos::RCP<const Thyra::BlockedLinearOpBase<Scalar> > ThyBlockedOp =
278 Teuchos::rcp_dynamic_cast<
const Thyra::BlockedLinearOpBase<Scalar> >(fwdOp);
279 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(ThyBlockedOp));
281 TEUCHOS_TEST_FOR_EXCEPT(ThyBlockedOp->blockExists(0,0)==
false);
283 Teuchos::RCP<const LinearOpBase<Scalar> > b00 = ThyBlockedOp->getBlock(0,0);
284 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(b00));
286 RCP<const XpCrsMat > xpetraFwdCrsMat00 = XpThyUtils::toXpetra(b00);
287 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(xpetraFwdCrsMat00));
290 RCP<XpCrsMat> xpetraFwdCrsMatNonConst00 = Teuchos::rcp_const_cast<XpCrsMat>(xpetraFwdCrsMat00);
291 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(xpetraFwdCrsMatNonConst00));
295 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(A00));
297 RCP<const XpMap> rowmap00 = A00->getRowMap();
298 RCP< const Teuchos::Comm< int > > comm = rowmap00->getComm();
301 RCP<XpBlockedCrsMat> bMat = Teuchos::rcp(
new XpBlockedCrsMat(ThyBlockedOp, comm));
302 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(bMat));
307 RCP<const XpCrsMat > xpetraFwdCrsMat = XpThyUtils::toXpetra(fwdOp);
308 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(xpetraFwdCrsMat));
311 RCP<XpCrsMat> xpetraFwdCrsMatNonConst = Teuchos::rcp_const_cast<XpCrsMat>(xpetraFwdCrsMat);
312 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(xpetraFwdCrsMatNonConst));
317 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(A));
320 const Teuchos::Ptr<DefaultPreconditioner<Scalar> > defaultPrec = Teuchos::ptr(
dynamic_cast<DefaultPreconditioner<Scalar> *
>(prec));
321 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(defaultPrec));
324 RCP<ThyLinOpBase> thyra_precOp = Teuchos::null;
325 thyra_precOp = rcp_dynamic_cast<Thyra::LinearOpBase<Scalar> >(defaultPrec->getNonconstUnspecifiedPrecOp(),
true);
328 RCP<MueLu::Hierarchy<Scalar,LocalOrdinal,GlobalOrdinal,Node> > H = Teuchos::null;
333 const bool startingOver = (thyra_precOp.is_null() || !paramList.isParameter(
"reuse: type") || paramList.get<std::string>(
"reuse: type") ==
"none");
335 if (startingOver ==
true) {
337 Teuchos::RCP<XpMultVecDouble> coordinates = Teuchos::null;
341 RCP<XpMultVec> nullspace = Teuchos::null;
342 #ifdef HAVE_MUELU_TPETRA 344 #if ((defined(EPETRA_HAVE_OMP) && (defined(HAVE_TPETRA_INST_OPENMP) && defined(HAVE_TPETRA_INST_INT_INT))) || \ 345 (!defined(EPETRA_HAVE_OMP) && (defined(HAVE_TPETRA_INST_SERIAL) && defined(HAVE_TPETRA_INST_INT_INT)))) 346 typedef Tpetra::MultiVector<Scalar, LocalOrdinal, GlobalOrdinal, Node> tMV;
347 RCP<tMV> tpetra_nullspace = Teuchos::null;
348 if (paramList.isType<Teuchos::RCP<tMV> >(
"Nullspace")) {
349 tpetra_nullspace = paramList.get<RCP<tMV> >(
"Nullspace");
350 paramList.remove(
"Nullspace");
351 nullspace = MueLu::TpetraMultiVector_To_XpetraMultiVector<Scalar,LocalOrdinal,GlobalOrdinal,Node>(tpetra_nullspace);
352 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(nullspace));
356 "Thyra::MueLuPreconditionerFactory: Tpetra does not support GO=int and or EpetraNode.");
360 #ifdef HAVE_MUELU_EPETRA 362 RCP<Epetra_MultiVector> epetra_nullspace = Teuchos::null;
363 if (paramList.isType<RCP<Epetra_MultiVector> >(
"Nullspace")) {
364 epetra_nullspace = paramList.get<RCP<Epetra_MultiVector> >(
"Nullspace");
365 paramList.remove(
"Nullspace");
368 nullspace = rcp_dynamic_cast<XpMultVec>(xpEpNullspaceMult);
369 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(nullspace));
380 #if defined(HAVE_MUELU_TPETRA) 382 #if ((defined(EPETRA_HAVE_OMP) && (defined(HAVE_TPETRA_INST_OPENMP) && defined(HAVE_TPETRA_INST_INT_INT))) || \ 383 (!defined(EPETRA_HAVE_OMP) && (defined(HAVE_TPETRA_INST_SERIAL) && defined(HAVE_TPETRA_INST_INT_INT)))) 384 RCP<ThyTpLinOp> tpetr_precOp = rcp_dynamic_cast<ThyTpLinOp>(thyra_precOp);
385 RCP<MueTpOp> muelu_precOp = rcp_dynamic_cast<MueTpOp>(tpetr_precOp->getTpetraOperator(),
true);
387 H = muelu_precOp->GetHierarchy();
390 "Thyra::MueLuPreconditionerFactory: Tpetra does not support GO=int and or EpetraNode.");
394 #if defined(HAVE_MUELU_EPETRA)// && defined(HAVE_MUELU_SERIAL) 396 RCP<ThyEpLinOp> epetr_precOp = rcp_dynamic_cast<ThyEpLinOp>(thyra_precOp);
397 RCP<MueEpOp> muelu_precOp = rcp_dynamic_cast<MueEpOp>(epetr_precOp->epetra_op(),
true);
405 "Thyra::MueLuPreconditionerFactory: Hierarchy has no levels in it");
407 "Thyra::MueLuPreconditionerFactory: Hierarchy has no fine level operator");
408 RCP<MueLu::Level> level0 = H->
GetLevel(0);
409 RCP<XpOp> O0 = level0->Get<RCP<XpOp> >(
"A");
410 RCP<XpMat> A0 = rcp_dynamic_cast<XpMat>(O0);
416 A->SetFixedBlockSize(A0->GetFixedBlockSize());
426 RCP<ThyLinOpBase > thyraPrecOp = Teuchos::null;
427 #if defined(HAVE_MUELU_TPETRA) 429 #if ((defined(EPETRA_HAVE_OMP) && (defined(HAVE_TPETRA_INST_OPENMP) && defined(HAVE_TPETRA_INST_INT_INT))) || \ 430 (!defined(EPETRA_HAVE_OMP) && (defined(HAVE_TPETRA_INST_SERIAL) && defined(HAVE_TPETRA_INST_INT_INT)))) 431 RCP<MueTpOp> muelu_tpetraOp = rcp(
new MueTpOp(H));
432 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(muelu_tpetraOp));
433 RCP<TpOp> tpOp = Teuchos::rcp_dynamic_cast<TpOp>(muelu_tpetraOp);
434 thyraPrecOp = Thyra::createLinearOp<Scalar, LocalOrdinal, GlobalOrdinal, Node>(tpOp);
437 "Thyra::MueLuPreconditionerFactory: Tpetra does not support GO=int and or EpetraNode.");
442 #if defined(HAVE_MUELU_EPETRA) 444 RCP<MueLu::Hierarchy<double,int,int,Xpetra::EpetraNode> > epetraH =
447 "Thyra::MueLuPreconditionerFactory: Failed to cast Hierarchy to Hierarchy<double,int,int,Xpetra::EpetraNode>. Epetra runs only on the Serial node.");
448 RCP<MueEpOp> muelu_epetraOp = rcp(
new MueEpOp(epetraH));
449 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(muelu_epetraOp));
451 set_extra_data(fwdOp,
"IFPF::fwdOp", Teuchos::inOutArg(muelu_epetraOp), Teuchos::POST_DESTROY,
false);
452 RCP<ThyEpLinOp> thyra_epetraOp = Thyra::nonconstEpetraLinearOp(muelu_epetraOp, NOTRANS, EPETRA_OP_APPLY_APPLY_INVERSE, EPETRA_OP_ADJOINT_UNSUPPORTED);
453 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(thyra_epetraOp));
454 thyraPrecOp = rcp_dynamic_cast<ThyLinOpBase>(thyra_epetraOp);
459 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::nonnull(thyraPrecOp));
462 const RCP<MueXpOp> muelu_xpetraOp = rcp(
new MueXpOp(H));
464 RCP<const VectorSpaceBase<Scalar> > thyraRangeSpace = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyra(muelu_xpetraOp->getRangeMap());
465 RCP<const VectorSpaceBase<Scalar> > thyraDomainSpace = Xpetra::ThyraUtils<Scalar,LocalOrdinal,GlobalOrdinal,Node>::toThyra(muelu_xpetraOp->getDomainMap());
468 thyraPrecOp = Thyra::xpetraLinearOp<Scalar, LocalOrdinal, GlobalOrdinal, Node>(thyraRangeSpace, thyraDomainSpace,xpOp);
471 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(thyraPrecOp));
473 defaultPrec->initializeUnspecified(thyraPrecOp);
478 Teuchos::RCP<
const LinearOpSourceBase<Scalar> >* fwdOp,
479 ESupportSolveUse* supportSolveUse
481 TEUCHOS_ASSERT(prec);
484 const Teuchos::Ptr<DefaultPreconditioner<Scalar> > defaultPrec = Teuchos::ptr(
dynamic_cast<DefaultPreconditioner<Scalar> *
>(prec));
485 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(defaultPrec));
489 *fwdOp = Teuchos::null;
492 if (supportSolveUse) {
494 *supportSolveUse = Thyra::SUPPORT_SOLVE_UNSPECIFIED;
497 defaultPrec->uninitialize();
507 TEUCHOS_TEST_FOR_EXCEPT(Teuchos::is_null(paramList));
512 RCP<ParameterList> savedParamList =
paramList_;
514 return savedParamList;
522 static RCP<const ParameterList> validPL;
524 if (Teuchos::is_null(validPL))
525 validPL = rcp(
new ParameterList());
535 std::string
description()
const {
return "Thyra::MueLuPreconditionerFactory"; }
545 #endif // HAVE_MUELU_EPETRA 549 #endif // #ifdef HAVE_MUELU_STRATIMIKOS 551 #endif // THYRA_MUELU_PRECONDITIONER_FACTORY_DECL_HPP
MueLuPreconditionerFactory()
RCP< Level > & GetLevel(const int levelID=0)
Retrieve a certain level from hierarchy.
void uninitializePrec(PreconditionerBase< Scalar > *prec, Teuchos::RCP< const LinearOpSourceBase< Scalar > > *fwdOp, ESupportSolveUse *supportSolveUse) const
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Teuchos::RCP< MueLu::Hierarchy< Scalar, LocalOrdinal, GlobalOrdinal, Node > > CreateXpetraPreconditioner(Teuchos::RCP< Xpetra::Matrix< Scalar, LocalOrdinal, GlobalOrdinal, Node > > op, const Teuchos::ParameterList &inParamList, Teuchos::RCP< Xpetra::MultiVector< double, LocalOrdinal, GlobalOrdinal, Node > > coords=Teuchos::null, Teuchos::RCP< Xpetra::MultiVector< Scalar, LocalOrdinal, GlobalOrdinal, Node > > nullspace=Teuchos::null)
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
Teuchos::RCP< Teuchos::ParameterList > paramList_
static RCP< Xpetra::MultiVector< double, LocalOrdinal, GlobalOrdinal, Node > > ExtractCoordinatesFromParameterList(ParameterList ¶mList)
void initializePrec(const Teuchos::RCP< const LinearOpSourceBase< Scalar > > &fwdOp, PreconditionerBase< Scalar > *prec, const ESupportSolveUse supportSolveUse) const
bool isCompatible(const LinearOpSourceBase< Scalar > &fwdOp) const
Teuchos::RCP< Teuchos::ParameterList > paramList_
Concrete preconditioner factory subclass for Thyra based on MueLu.Add support for MueLu preconditione...
void uninitializePrec(PreconditionerBase< Scalar > *prec, Teuchos::RCP< const LinearOpSourceBase< Scalar > > *fwdOp, ESupportSolveUse *supportSolveUse) const
bool isCompatible(const LinearOpSourceBase< Scalar > &fwdOpSrc) const
Teuchos::RCP< PreconditionerBase< Scalar > > createPrec() const
std::string description() const
Teuchos::RCP< const Teuchos::ParameterList > getParameterList() const
Teuchos::RCP< PreconditionerBase< Scalar > > createPrec() const
std::string description() const
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > ¶mList)
MueLuPreconditionerFactory()
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
Teuchos::RCP< const Teuchos::ParameterList > getValidParameters() const
Teuchos::RCP< const Teuchos::ParameterList > getParameterList() const
Wraps an existing MueLu::Hierarchy as a Tpetra::Operator.
void initializePrec(const Teuchos::RCP< const LinearOpSourceBase< Scalar > > &fwdOpSrc, PreconditionerBase< Scalar > *prec, const ESupportSolveUse supportSolveUse) const
Exception throws to report errors in the internal logical of the program.
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
Provides methods to build a multigrid hierarchy and apply multigrid cycles.
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > ¶mList)
Wraps an existing MueLu::Hierarchy as a Xpetra::Operator.