10 #include "Teko_TpetraOperatorWrapper.hpp"
11 #include "Thyra_SpmdVectorBase.hpp"
12 #include "Thyra_MultiVectorStdOps.hpp"
13 #include "Teuchos_DefaultSerialComm.hpp"
14 #include "Thyra_TpetraLinearOp.hpp"
15 #include "Tpetra_Vector.hpp"
16 #include "Thyra_TpetraThyraWrappers.hpp"
19 #include "Thyra_BlockedLinearOpBase.hpp"
20 #include "Thyra_ProductVectorSpaceBase.hpp"
21 #include "Thyra_TpetraLinearOp.hpp"
23 #include "Teko_TpetraThyraConverter.hpp"
24 #include "Teuchos_Ptr.hpp"
27 namespace TpetraHelpers {
29 using namespace Teuchos;
30 using namespace Thyra;
32 DefaultMappingStrategy::DefaultMappingStrategy(
33 const RCP<
const Thyra::LinearOpBase<double> >& thyraOp,
34 const Teuchos::Comm<Thyra::Ordinal>& comm) {
35 RCP<Teuchos::Comm<Thyra::Ordinal> > newComm = comm.duplicate();
38 domainSpace_ = thyraOp->domain();
39 rangeSpace_ = thyraOp->range();
41 domainMap_ = Teko::TpetraHelpers::thyraVSToTpetraMap(*domainSpace_, newComm);
42 rangeMap_ = Teko::TpetraHelpers::thyraVSToTpetraMap(*rangeSpace_, newComm);
46 const Tpetra::MultiVector<ST, LO, GO, NT>& x,
47 const Ptr<Thyra::MultiVectorBase<ST> >& thyraVec)
const {
48 Teko::TpetraHelpers::blockTpetraToThyra(x, thyraVec);
52 const RCP<
const Thyra::MultiVectorBase<ST> >& thyraVec,
53 Tpetra::MultiVector<ST, LO, GO, NT>& v)
const {
54 Teko::TpetraHelpers::blockThyraToTpetra(thyraVec, v);
57 TpetraOperatorWrapper::TpetraOperatorWrapper() {
58 useTranspose_ =
false;
59 mapStrategy_ = Teuchos::null;
60 thyraOp_ = Teuchos::null;
61 comm_ = Teuchos::null;
62 label_ = Teuchos::null;
65 TpetraOperatorWrapper::TpetraOperatorWrapper(
const RCP<
const Thyra::LinearOpBase<ST> >& thyraOp) {
69 TpetraOperatorWrapper::TpetraOperatorWrapper(
const RCP<
const Thyra::LinearOpBase<ST> >& thyraOp,
70 const RCP<const MappingStrategy>& mapStrategy)
71 : mapStrategy_(mapStrategy) {
75 TpetraOperatorWrapper::TpetraOperatorWrapper(
const RCP<const MappingStrategy>& mapStrategy)
76 : mapStrategy_(mapStrategy) {
77 useTranspose_ =
false;
78 thyraOp_ = Teuchos::null;
79 comm_ = Teuchos::null;
80 label_ = Teuchos::null;
83 void TpetraOperatorWrapper::SetOperator(
const RCP<
const LinearOpBase<ST> >& thyraOp,
85 useTranspose_ =
false;
87 comm_ = getThyraComm(*thyraOp);
88 label_ = thyraOp_->description();
89 if (mapStrategy_ == Teuchos::null && buildMap)
90 mapStrategy_ = Teuchos::rcp(
new DefaultMappingStrategy(thyraOp, *comm_));
93 double TpetraOperatorWrapper::NormInf()
const {
94 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
95 "TpetraOperatorWrapper::NormInf not implemated");
99 void TpetraOperatorWrapper::apply(
const Tpetra::MultiVector<ST, LO, GO, NT>& X,
100 Tpetra::MultiVector<ST, LO, GO, NT>& Y,
101 Teuchos::ETransp , ST alpha, ST beta)
const {
102 if (!useTranspose_) {
104 RCP<Thyra::MultiVectorBase<ST> > tX;
105 RCP<Thyra::MultiVectorBase<ST> > tY;
107 tX = Thyra::createMembers(thyraOp_->domain(), X.getNumVectors());
108 tY = Thyra::createMembers(thyraOp_->range(), X.getNumVectors());
110 Thyra::assign(tX.ptr(), 0.0);
111 Thyra::assign(tY.ptr(), 0.0);
114 mapStrategy_->copyTpetraIntoThyra(X, tX.ptr());
115 mapStrategy_->copyTpetraIntoThyra(
119 thyraOp_->apply(Thyra::NOTRANS, *tX, tY.ptr(), alpha, beta);
122 mapStrategy_->copyThyraIntoTpetra(tY, Y);
124 TEUCHOS_ASSERT(
false);
128 void TpetraOperatorWrapper::applyInverse(
const Tpetra::MultiVector<ST, LO, GO, NT>& ,
129 Tpetra::MultiVector<ST, LO, GO, NT>& ,
130 Teuchos::ETransp , ST ,
132 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
133 "TpetraOperatorWrapper::applyInverse not implemented");
136 RCP<const Teuchos::Comm<Thyra::Ordinal> > TpetraOperatorWrapper::getThyraComm(
137 const Thyra::LinearOpBase<ST>& inOp)
const {
138 RCP<const VectorSpaceBase<ST> > vs = inOp.domain();
140 RCP<const SpmdVectorSpaceBase<ST> > spmd;
141 RCP<const VectorSpaceBase<ST> > current = vs;
142 while (current != Teuchos::null) {
144 RCP<const ProductVectorSpaceBase<ST> > prod =
145 rcp_dynamic_cast<
const ProductVectorSpaceBase<ST> >(current);
148 if (prod == Teuchos::null) {
150 spmd = rcp_dynamic_cast<
const SpmdVectorSpaceBase<ST> >(current);
154 current = prod->getBlock(0);
157 TEUCHOS_TEST_FOR_EXCEPTION(spmd == Teuchos::null, std::runtime_error,
158 "TpetraOperatorWrapper requires std::vector space "
159 "blocks to be SPMD std::vector spaces");
161 return spmd->getComm();
227 const RCP<const Thyra::BlockedLinearOpBase<ST> > blkOp =
228 Teuchos::rcp_dynamic_cast<
const Thyra::BlockedLinearOpBase<ST> >(
getThyraOp());
230 return blkOp->productRange()->numBlocks();
234 const RCP<const Thyra::BlockedLinearOpBase<ST> > blkOp =
235 Teuchos::rcp_dynamic_cast<
const Thyra::BlockedLinearOpBase<ST> >(
getThyraOp());
237 return blkOp->productDomain()->numBlocks();
242 const RCP<const Thyra::BlockedLinearOpBase<ST> > blkOp =
243 Teuchos::rcp_dynamic_cast<
const Thyra::BlockedLinearOpBase<ST> >(
getThyraOp());
245 RCP<const Thyra::TpetraLinearOp<ST, LO, GO, NT> > tOp =
246 rcp_dynamic_cast<
const Thyra::TpetraLinearOp<ST, LO, GO, NT> >(blkOp->getBlock(i, j));
248 return tOp->getConstTpetraOperator();
virtual void copyThyraIntoTpetra(const RCP< const Thyra::MultiVectorBase< ST > > &thyraX, Tpetra::MultiVector< ST, LO, GO, NT > &tpetraX) const
Copy an Thyra::MultiVectorBase into a Epetra_MultiVector.
const RCP< const Thyra::LinearOpBase< ST > > getThyraOp() const
Return the thyra operator associated with this wrapper.
virtual void copyTpetraIntoThyra(const Tpetra::MultiVector< ST, LO, GO, NT > &tpetraX, const Teuchos::Ptr< Thyra::MultiVectorBase< ST > > &thyraX) const
Copy an Epetra_MultiVector into a Thyra::MultiVectorBase.
Teuchos::RCP< const Tpetra::Operator< ST, LO, GO, NT > > GetBlock(int i, int j) const
Grab the i,j block.
virtual int GetBlockRowCount()
Get the number of block rows in this operator.
virtual int GetBlockColCount()
Get the number of block columns in this operator.