10 #include "Epetra/Teko_StridedEpetraOperator.hpp"
11 #include "Epetra/Teko_StridedMappingStrategy.hpp"
12 #include "Epetra/Teko_ReorderedMappingStrategy.hpp"
14 #include "Teuchos_VerboseObject.hpp"
16 #include "Thyra_LinearOpBase.hpp"
17 #include "Thyra_EpetraLinearOp.hpp"
18 #include "Thyra_EpetraThyraWrappers.hpp"
19 #include "Thyra_DefaultProductMultiVector.hpp"
20 #include "Thyra_DefaultProductVectorSpace.hpp"
21 #include "Thyra_DefaultBlockedLinearOp.hpp"
22 #include "Thyra_get_Epetra_Operator.hpp"
24 #include "Epetra_Vector.h"
25 #include "EpetraExt_MultiVectorOut.h"
26 #include "EpetraExt_RowMatrixOut.h"
35 using Teuchos::rcp_dynamic_cast;
37 StridedEpetraOperator::StridedEpetraOperator(
int numVars,
38 const Teuchos::RCP<const Epetra_Operator>& content,
39 const std::string& label)
40 : Teko::Epetra::EpetraOperatorWrapper(), label_(label) {
41 std::vector<int> vars;
44 for (
int i = 0; i < numVars; i++) vars.push_back(1);
46 SetContent(vars, content);
49 StridedEpetraOperator::StridedEpetraOperator(
const std::vector<int>& vars,
50 const Teuchos::RCP<const Epetra_Operator>& content,
51 const std::string& label)
52 : Teko::Epetra::EpetraOperatorWrapper(), label_(label) {
53 SetContent(vars, content);
56 void StridedEpetraOperator::SetContent(
const std::vector<int>& vars,
57 const Teuchos::RCP<const Epetra_Operator>& content) {
58 fullContent_ = content;
59 stridedMapping_ = rcp(
new StridedMappingStrategy(
60 vars, Teuchos::rcpFromRef(fullContent_->OperatorDomainMap()), fullContent_->Comm()));
61 SetMapStrategy(stridedMapping_);
64 BuildBlockedOperator();
67 void StridedEpetraOperator::BuildBlockedOperator() {
68 TEUCHOS_ASSERT(stridedMapping_ != Teuchos::null);
71 const RCP<const Epetra_CrsMatrix> crsContent =
72 rcp_dynamic_cast<
const Epetra_CrsMatrix>(fullContent_);
75 if (stridedOperator_ == Teuchos::null) {
76 stridedOperator_ = stridedMapping_->buildBlockedThyraOp(crsContent, label_);
78 const RCP<Thyra::BlockedLinearOpBase<double> > blkOp =
79 rcp_dynamic_cast<Thyra::BlockedLinearOpBase<double> >(stridedOperator_,
true);
80 stridedMapping_->rebuildBlockedThyraOp(crsContent, blkOp);
84 SetOperator(stridedOperator_,
false);
87 if (reorderManager_ != Teuchos::null) Reorder(*reorderManager_);
90 const Teuchos::RCP<const Epetra_Operator> StridedEpetraOperator::GetBlock(
int i,
int j)
const {
91 const RCP<const Thyra::BlockedLinearOpBase<double> > blkOp =
92 Teuchos::rcp_dynamic_cast<
const Thyra::BlockedLinearOpBase<double> >(getThyraOp());
94 return Thyra::get_Epetra_Operator(*blkOp->getBlock(i, j));
100 void StridedEpetraOperator::Reorder(
const BlockReorderManager& brm) {
101 reorderManager_ = rcp(
new BlockReorderManager(brm));
104 RCP<const MappingStrategy> reorderMapping =
105 rcp(
new ReorderedMappingStrategy(*reorderManager_, stridedMapping_));
106 RCP<const Thyra::BlockedLinearOpBase<double> > blockOp =
107 rcp_dynamic_cast<
const Thyra::BlockedLinearOpBase<double> >(stridedOperator_);
109 RCP<const Thyra::LinearOpBase<double> > A = buildReorderedLinearOp(*reorderManager_, blockOp);
112 SetMapStrategy(reorderMapping);
113 SetOperator(A,
false);
117 void StridedEpetraOperator::RemoveReording() {
118 SetMapStrategy(stridedMapping_);
119 SetOperator(stridedOperator_,
false);
120 reorderManager_ = Teuchos::null;
125 void StridedEpetraOperator::WriteBlocks(
const std::string& prefix)
const {
126 RCP<Thyra::PhysicallyBlockedLinearOpBase<double> > blockOp =
127 rcp_dynamic_cast<Thyra::PhysicallyBlockedLinearOpBase<double> >(stridedOperator_);
130 int rows = Teko::blockRowCount(blockOp);
132 for (
int i = 0; i < rows; i++) {
133 for (
int j = 0; j < rows; j++) {
135 RCP<const Epetra_RowMatrix> mat = Teuchos::rcp_dynamic_cast<
const Epetra_RowMatrix>(
136 Thyra::get_Epetra_Operator(*blockOp->getBlock(i, j)));
139 EpetraExt::RowMatrixToMatrixMarketFile(formatBlockName(prefix, i, j, rows).c_str(), *mat);
151 std::string StridedEpetraOperator::PrintNorm(
const eNormType& nrmType,
const char newline) {
152 BlockedLinearOp blockOp = toBlockedLinearOp(stridedOperator_);
155 int rowCount = Teko::blockRowCount(blockOp);
156 int colCount = Teko::blockRowCount(blockOp);
158 std::stringstream ss;
160 ss << std::scientific;
161 for (
int row = 0; row < rowCount; row++) {
162 for (
int col = 0; col < colCount; col++) {
164 RCP<const Epetra_CrsMatrix> mat = Teuchos::rcp_dynamic_cast<
const Epetra_CrsMatrix>(
165 Thyra::get_Epetra_Operator(*blockOp->getBlock(row, col)));
170 case Inf: norm = mat->NormInf();
break;
171 case One: norm = mat->NormOne();
break;
172 case Frobenius: norm = mat->NormFrobenius();
break;
174 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::runtime_error,
175 "StridedEpetraOperator::eNormType incorrectly specified");
186 bool StridedEpetraOperator::testAgainstFullOperator(
int count,
double tol)
const {
187 Epetra_Vector xf(OperatorRangeMap());
188 Epetra_Vector xs(OperatorRangeMap());
189 Epetra_Vector y(OperatorDomainMap());
193 double diffNorm = 0.0, trueNorm = 0.0;
194 for (
int i = 0; i < count; i++) {
201 fullContent_->Apply(y, xf);
204 xs.Update(-1.0, xf, 1.0);
209 result &= (diffNorm / trueNorm < tol);