44 #include "Teko_TpetraOperatorWrapper.hpp"
45 #include "Thyra_SpmdVectorBase.hpp"
46 #include "Thyra_MultiVectorStdOps.hpp"
47 #include "Teuchos_DefaultSerialComm.hpp"
48 #include "Thyra_TpetraLinearOp.hpp"
49 #include "Tpetra_Vector.hpp"
50 #include "Thyra_TpetraThyraWrappers.hpp"
53 #include "Thyra_BlockedLinearOpBase.hpp"
54 #include "Thyra_ProductVectorSpaceBase.hpp"
55 #include "Thyra_TpetraLinearOp.hpp"
57 #include "Teko_TpetraThyraConverter.hpp"
58 #include "Teuchos_Ptr.hpp"
61 namespace TpetraHelpers {
63 using namespace Teuchos;
64 using namespace Thyra;
66 DefaultMappingStrategy::DefaultMappingStrategy(
67 const RCP<
const Thyra::LinearOpBase<double> >& thyraOp,
68 const Teuchos::Comm<Thyra::Ordinal>& comm) {
69 RCP<Teuchos::Comm<Thyra::Ordinal> > newComm = comm.duplicate();
72 domainSpace_ = thyraOp->domain();
73 rangeSpace_ = thyraOp->range();
75 domainMap_ = Teko::TpetraHelpers::thyraVSToTpetraMap(*domainSpace_, newComm);
76 rangeMap_ = Teko::TpetraHelpers::thyraVSToTpetraMap(*rangeSpace_, newComm);
80 const Tpetra::MultiVector<ST, LO, GO, NT>& x,
81 const Ptr<Thyra::MultiVectorBase<ST> >& thyraVec)
const {
82 Teko::TpetraHelpers::blockTpetraToThyra(x, thyraVec);
86 const RCP<
const Thyra::MultiVectorBase<ST> >& thyraVec,
87 Tpetra::MultiVector<ST, LO, GO, NT>& v)
const {
88 Teko::TpetraHelpers::blockThyraToTpetra(thyraVec, v);
91 TpetraOperatorWrapper::TpetraOperatorWrapper() {
92 useTranspose_ =
false;
93 mapStrategy_ = Teuchos::null;
94 thyraOp_ = Teuchos::null;
95 comm_ = Teuchos::null;
96 label_ = Teuchos::null;
99 TpetraOperatorWrapper::TpetraOperatorWrapper(
const RCP<
const Thyra::LinearOpBase<ST> >& thyraOp) {
100 SetOperator(thyraOp);
103 TpetraOperatorWrapper::TpetraOperatorWrapper(
const RCP<
const Thyra::LinearOpBase<ST> >& thyraOp,
104 const RCP<const MappingStrategy>& mapStrategy)
105 : mapStrategy_(mapStrategy) {
106 SetOperator(thyraOp);
109 TpetraOperatorWrapper::TpetraOperatorWrapper(
const RCP<const MappingStrategy>& mapStrategy)
110 : mapStrategy_(mapStrategy) {
111 useTranspose_ =
false;
112 thyraOp_ = Teuchos::null;
113 comm_ = Teuchos::null;
114 label_ = Teuchos::null;
117 void TpetraOperatorWrapper::SetOperator(
const RCP<
const LinearOpBase<ST> >& thyraOp,
119 useTranspose_ =
false;
121 comm_ = getThyraComm(*thyraOp);
122 label_ = thyraOp_->description();
123 if (mapStrategy_ == Teuchos::null && buildMap)
124 mapStrategy_ = Teuchos::rcp(
new DefaultMappingStrategy(thyraOp, *comm_));
127 double TpetraOperatorWrapper::NormInf()
const {
128 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
129 "TpetraOperatorWrapper::NormInf not implemated");
133 void TpetraOperatorWrapper::apply(
const Tpetra::MultiVector<ST, LO, GO, NT>& X,
134 Tpetra::MultiVector<ST, LO, GO, NT>& Y,
135 Teuchos::ETransp , ST alpha, ST beta)
const {
136 if (!useTranspose_) {
138 RCP<Thyra::MultiVectorBase<ST> > tX;
139 RCP<Thyra::MultiVectorBase<ST> > tY;
141 tX = Thyra::createMembers(thyraOp_->domain(), X.getNumVectors());
142 tY = Thyra::createMembers(thyraOp_->range(), X.getNumVectors());
144 Thyra::assign(tX.ptr(), 0.0);
145 Thyra::assign(tY.ptr(), 0.0);
148 mapStrategy_->copyTpetraIntoThyra(X, tX.ptr());
149 mapStrategy_->copyTpetraIntoThyra(
153 thyraOp_->apply(Thyra::NOTRANS, *tX, tY.ptr(), alpha, beta);
156 mapStrategy_->copyThyraIntoTpetra(tY, Y);
158 TEUCHOS_ASSERT(
false);
162 void TpetraOperatorWrapper::applyInverse(
const Tpetra::MultiVector<ST, LO, GO, NT>& ,
163 Tpetra::MultiVector<ST, LO, GO, NT>& ,
164 Teuchos::ETransp , ST ,
166 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
167 "TpetraOperatorWrapper::applyInverse not implemented");
170 RCP<const Teuchos::Comm<Thyra::Ordinal> > TpetraOperatorWrapper::getThyraComm(
171 const Thyra::LinearOpBase<ST>& inOp)
const {
172 RCP<const VectorSpaceBase<ST> > vs = inOp.domain();
174 RCP<const SpmdVectorSpaceBase<ST> > spmd;
175 RCP<const VectorSpaceBase<ST> > current = vs;
176 while (current != Teuchos::null) {
178 RCP<const ProductVectorSpaceBase<ST> > prod =
179 rcp_dynamic_cast<
const ProductVectorSpaceBase<ST> >(current);
182 if (prod == Teuchos::null) {
184 spmd = rcp_dynamic_cast<
const SpmdVectorSpaceBase<ST> >(current);
188 current = prod->getBlock(0);
191 TEUCHOS_TEST_FOR_EXCEPTION(spmd == Teuchos::null, std::runtime_error,
192 "TpetraOperatorWrapper requires std::vector space "
193 "blocks to be SPMD std::vector spaces");
195 return spmd->getComm();
261 const RCP<const Thyra::BlockedLinearOpBase<ST> > blkOp =
262 Teuchos::rcp_dynamic_cast<
const Thyra::BlockedLinearOpBase<ST> >(
getThyraOp());
264 return blkOp->productRange()->numBlocks();
268 const RCP<const Thyra::BlockedLinearOpBase<ST> > blkOp =
269 Teuchos::rcp_dynamic_cast<
const Thyra::BlockedLinearOpBase<ST> >(
getThyraOp());
271 return blkOp->productDomain()->numBlocks();
276 const RCP<const Thyra::BlockedLinearOpBase<ST> > blkOp =
277 Teuchos::rcp_dynamic_cast<
const Thyra::BlockedLinearOpBase<ST> >(
getThyraOp());
279 RCP<const Thyra::TpetraLinearOp<ST, LO, GO, NT> > tOp =
280 rcp_dynamic_cast<
const Thyra::TpetraLinearOp<ST, LO, GO, NT> >(blkOp->getBlock(i, j));
282 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.