Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_BlockedEpetraOperator.cpp
1 /*
2 // @HEADER
3 //
4 // ***********************************************************************
5 //
6 // Teko: A package for block and physics based preconditioning
7 // Copyright 2010 Sandia Corporation
8 //
9 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
10 // the U.S. Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are
14 // met:
15 //
16 // 1. Redistributions of source code must retain the above copyright
17 // notice, this list of conditions and the following disclaimer.
18 //
19 // 2. Redistributions in binary form must reproduce the above copyright
20 // notice, this list of conditions and the following disclaimer in the
21 // documentation and/or other materials provided with the distribution.
22 //
23 // 3. Neither the name of the Corporation nor the names of the
24 // contributors may be used to endorse or promote products derived from
25 // this software without specific prior written permission.
26 //
27 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
28 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
31 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
32 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
34 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
35 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
36 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 //
39 // Questions? Contact Eric C. Cyr (eccyr@sandia.gov)
40 //
41 // ***********************************************************************
42 //
43 // @HEADER
44 
45 */
46 
47 #include "Teko_BlockedEpetraOperator.hpp"
48 #include "Teko_BlockedMappingStrategy.hpp"
49 #include "Teko_ReorderedMappingStrategy.hpp"
50 
51 #include "Teuchos_VerboseObject.hpp"
52 
53 #include "Thyra_LinearOpBase.hpp"
54 #include "Thyra_EpetraLinearOp.hpp"
55 #include "Thyra_EpetraThyraWrappers.hpp"
56 #include "Thyra_DefaultProductMultiVector.hpp"
57 #include "Thyra_DefaultProductVectorSpace.hpp"
58 #include "Thyra_DefaultBlockedLinearOp.hpp"
59 #include "Thyra_get_Epetra_Operator.hpp"
60 
61 #include "EpetraExt_MultiVectorOut.h"
62 #include "EpetraExt_RowMatrixOut.h"
63 
64 #include "Teko_Utilities.hpp"
65 
66 namespace Teko {
67 namespace Epetra {
68 
69 using Teuchos::RCP;
70 using Teuchos::rcp;
71 using Teuchos::rcp_dynamic_cast;
72 
73 BlockedEpetraOperator::BlockedEpetraOperator(const std::vector<std::vector<int> >& vars,
74  const Teuchos::RCP<const Epetra_Operator>& content,
75  const std::string& label)
76  : Teko::Epetra::EpetraOperatorWrapper(), label_(label) {
77  SetContent(vars, content);
78 }
79 
80 void BlockedEpetraOperator::SetContent(const std::vector<std::vector<int> >& vars,
81  const Teuchos::RCP<const Epetra_Operator>& content) {
82  fullContent_ = content;
83  blockedMapping_ = rcp(new BlockedMappingStrategy(
84  vars, Teuchos::rcpFromRef(fullContent_->OperatorDomainMap()), fullContent_->Comm()));
85  SetMapStrategy(blockedMapping_);
86 
87  // build thyra operator
88  BuildBlockedOperator();
89 }
90 
91 void BlockedEpetraOperator::BuildBlockedOperator() {
92  TEUCHOS_ASSERT(blockedMapping_ != Teuchos::null);
93 
94  // get a CRS matrix
95  const RCP<const Epetra_CrsMatrix> crsContent =
96  rcp_dynamic_cast<const Epetra_CrsMatrix>(fullContent_);
97 
98  // ask the strategy to build the Thyra operator for you
99  if (blockedOperator_ == Teuchos::null) {
100  blockedOperator_ = blockedMapping_->buildBlockedThyraOp(crsContent, label_);
101  } else {
102  const RCP<Thyra::BlockedLinearOpBase<double> > blkOp =
103  rcp_dynamic_cast<Thyra::BlockedLinearOpBase<double> >(blockedOperator_, true);
104  blockedMapping_->rebuildBlockedThyraOp(crsContent, blkOp);
105  }
106 
107  // set whatever is returned
108  SetOperator(blockedOperator_, false);
109 
110  // reorder if neccessary
111  if (reorderManager_ != Teuchos::null) Reorder(*reorderManager_);
112 }
113 
114 const Teuchos::RCP<const Epetra_Operator> BlockedEpetraOperator::GetBlock(int i, int j) const {
115  const RCP<const Thyra::BlockedLinearOpBase<double> > blkOp =
116  Teuchos::rcp_dynamic_cast<const Thyra::BlockedLinearOpBase<double> >(getThyraOp());
117 
118  return Thyra::get_Epetra_Operator(*blkOp->getBlock(i, j));
119 }
120 
125  reorderManager_ = rcp(new BlockReorderManager(brm));
126 
127  // build reordered objects
128  RCP<const MappingStrategy> reorderMapping =
129  rcp(new ReorderedMappingStrategy(*reorderManager_, blockedMapping_));
130  RCP<const Thyra::BlockedLinearOpBase<double> > blockOp =
131  rcp_dynamic_cast<const Thyra::BlockedLinearOpBase<double> >(blockedOperator_);
132 
133  RCP<const Thyra::LinearOpBase<double> > A = buildReorderedLinearOp(*reorderManager_, blockOp);
134 
135  // set them as working values
136  SetMapStrategy(reorderMapping);
137  SetOperator(A, false);
138 }
139 
142  SetMapStrategy(blockedMapping_);
143  SetOperator(blockedOperator_, false);
144  reorderManager_ = Teuchos::null;
145 }
146 
149 void BlockedEpetraOperator::WriteBlocks(const std::string& prefix) const {
150  RCP<Thyra::PhysicallyBlockedLinearOpBase<double> > blockOp =
151  rcp_dynamic_cast<Thyra::PhysicallyBlockedLinearOpBase<double> >(blockedOperator_);
152 
153  // get size of blocked block operator
154  int rows = Teko::blockRowCount(blockOp);
155 
156  for (int i = 0; i < rows; i++) {
157  for (int j = 0; j < rows; j++) {
158  // get the row matrix object
159  RCP<const Epetra_RowMatrix> mat = Teuchos::rcp_dynamic_cast<const Epetra_RowMatrix>(
160  Thyra::get_Epetra_Operator(*blockOp->getBlock(i, j)));
161 
162  // write to file
163  EpetraExt::RowMatrixToMatrixMarketFile(formatBlockName(prefix, i, j, rows).c_str(), *mat);
164  }
165  }
166 }
167 
168 bool BlockedEpetraOperator::testAgainstFullOperator(int count, double tol) const {
169  Epetra_Vector xf(OperatorRangeMap());
170  Epetra_Vector xs(OperatorRangeMap());
171  Epetra_Vector y(OperatorDomainMap());
172 
173  // test operator many times
174  bool result = true;
175  double diffNorm = 0.0, trueNorm = 0.0;
176  for (int i = 0; i < count; i++) {
177  xf.PutScalar(0.0);
178  xs.PutScalar(0.0);
179  y.Random();
180 
181  // apply operator
182  Apply(y, xs); // xs = A*y
183  fullContent_->Apply(y, xf); // xf = A*y
184 
185  // compute norms
186  xs.Update(-1.0, xf, 1.0);
187  xs.Norm2(&diffNorm);
188  xf.Norm2(&trueNorm);
189 
190  // check result
191  result &= (diffNorm / trueNorm < tol);
192  }
193  return result;
194 }
195 
196 } // end namespace Epetra
197 } // end namespace Teko
bool testAgainstFullOperator(int count, double tol) const
Helps perform sanity checks.
Class that describes how a flat blocked operator should be reordered.
void Reorder(const BlockReorderManager &brm)
Implements the Epetra_Operator interface with a Thyra LinearOperator. This enables the use of absrtac...
virtual void SetContent(const std::vector< std::vector< int > > &vars, const Teuchos::RCP< const Epetra_Operator > &content)
void RemoveReording()
Remove any reordering on this object.
BlockedEpetraOperator(const std::vector< std::vector< int > > &vars, const Teuchos::RCP< const Epetra_Operator > &content, const std::string &label="<ANYM>")
const RCP< const Thyra::LinearOpBase< double > > getThyraOp() const
Return the thyra operator associated with this wrapper.
virtual void WriteBlocks(const std::string &prefix) const