Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_BlockedTpetraOperator.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_BlockedTpetraOperator.hpp"
48 #include "Teko_TpetraBlockedMappingStrategy.hpp"
49 #include "Teko_TpetraReorderedMappingStrategy.hpp"
50 
51 #include "Teuchos_VerboseObject.hpp"
52 
53 #include "Thyra_LinearOpBase.hpp"
54 #include "Thyra_TpetraLinearOp.hpp"
55 #include "Thyra_TpetraThyraWrappers.hpp"
56 #include "Thyra_DefaultProductMultiVector.hpp"
57 #include "Thyra_DefaultProductVectorSpace.hpp"
58 #include "Thyra_DefaultBlockedLinearOp.hpp"
59 
60 #include "MatrixMarket_Tpetra.hpp"
61 
62 #include "Teko_Utilities.hpp"
63 
64 namespace Teko {
65 namespace TpetraHelpers {
66 
67 using Teuchos::RCP;
68 using Teuchos::rcp;
69 using Teuchos::rcp_dynamic_cast;
70 
72  const std::vector<std::vector<GO> >& vars,
73  const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> >& content, const std::string& label)
74  : Teko::TpetraHelpers::TpetraOperatorWrapper(), label_(label) {
75  SetContent(vars, content);
76 }
77 
79  const std::vector<std::vector<GO> >& vars,
80  const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> >& content) {
81  fullContent_ = content;
82  blockedMapping_ = rcp(new TpetraBlockedMappingStrategy(vars, fullContent_->getDomainMap(),
83  *fullContent_->getDomainMap()->getComm()));
84  SetMapStrategy(blockedMapping_);
85 
86  // build thyra operator
87  BuildBlockedOperator();
88 }
89 
90 void BlockedTpetraOperator::BuildBlockedOperator() {
91  TEUCHOS_ASSERT(blockedMapping_ != Teuchos::null);
92 
93  // get a CRS matrix
94  const RCP<const Tpetra::CrsMatrix<ST, LO, GO, NT> > crsContent =
95  rcp_dynamic_cast<const Tpetra::CrsMatrix<ST, LO, GO, NT> >(fullContent_);
96 
97  // ask the strategy to build the Thyra operator for you
98  if (blockedOperator_ == Teuchos::null) {
99  blockedOperator_ = blockedMapping_->buildBlockedThyraOp(crsContent, label_);
100  } else {
101  const RCP<Thyra::BlockedLinearOpBase<ST> > blkOp =
102  rcp_dynamic_cast<Thyra::BlockedLinearOpBase<ST> >(blockedOperator_, true);
103  blockedMapping_->rebuildBlockedThyraOp(crsContent, blkOp);
104  }
105 
106  // set whatever is returned
107  SetOperator(blockedOperator_, false);
108 
109  // reorder if neccessary
110  if (reorderManager_ != Teuchos::null) Reorder(*reorderManager_);
111 }
112 
113 const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> > BlockedTpetraOperator::GetBlock(
114  int i, int j) const {
115  const RCP<const Thyra::BlockedLinearOpBase<ST> > blkOp =
116  Teuchos::rcp_dynamic_cast<const Thyra::BlockedLinearOpBase<ST> >(getThyraOp());
117 
118  RCP<const Thyra::TpetraLinearOp<ST, LO, GO, NT> > tOp =
119  rcp_dynamic_cast<const Thyra::TpetraLinearOp<ST, LO, GO, NT> >(blkOp->getBlock(i, j), true);
120  return tOp->getConstTpetraOperator();
121 }
122 
127  reorderManager_ = rcp(new BlockReorderManager(brm));
128 
129  // build reordered objects
130  RCP<const MappingStrategy> reorderMapping =
131  rcp(new TpetraReorderedMappingStrategy(*reorderManager_, blockedMapping_));
132  RCP<const Thyra::BlockedLinearOpBase<ST> > blockOp =
133  rcp_dynamic_cast<const Thyra::BlockedLinearOpBase<ST> >(blockedOperator_);
134 
135  RCP<const Thyra::LinearOpBase<ST> > A = buildReorderedLinearOp(*reorderManager_, blockOp);
136 
137  // set them as working values
138  SetMapStrategy(reorderMapping);
139  SetOperator(A, false);
140 }
141 
144  SetMapStrategy(blockedMapping_);
145  SetOperator(blockedOperator_, false);
146  reorderManager_ = Teuchos::null;
147 }
148 
151 void BlockedTpetraOperator::WriteBlocks(const std::string& prefix) const {
152  RCP<Thyra::PhysicallyBlockedLinearOpBase<ST> > blockOp =
153  rcp_dynamic_cast<Thyra::PhysicallyBlockedLinearOpBase<ST> >(blockedOperator_);
154 
155  // get size of blocked block operator
156  int rows = Teko::blockRowCount(blockOp);
157 
158  for (int i = 0; i < rows; i++) {
159  for (int j = 0; j < rows; j++) {
160  // get the row matrix object
161  RCP<const Thyra::TpetraLinearOp<ST, LO, GO, NT> > tOp =
162  rcp_dynamic_cast<const Thyra::TpetraLinearOp<ST, LO, GO, NT> >(blockOp->getBlock(i, j));
163  RCP<const Tpetra::CrsMatrix<ST, LO, GO, NT> > mat =
164  Teuchos::rcp_dynamic_cast<const Tpetra::CrsMatrix<ST, LO, GO, NT> >(
165  tOp->getConstTpetraOperator());
166 
167  // write to file
168  Tpetra::MatrixMarket::Writer<Tpetra::CrsMatrix<ST, LO, GO, NT> >::writeSparseFile(
169  formatBlockName(prefix, i, j, rows).c_str(), mat);
170  }
171  }
172 }
173 
174 bool BlockedTpetraOperator::testAgainstFullOperator(int count, ST tol) const {
175  Tpetra::Vector<ST, LO, GO, NT> xf(getRangeMap());
176  Tpetra::Vector<ST, LO, GO, NT> xs(getRangeMap());
177  Tpetra::Vector<ST, LO, GO, NT> y(getDomainMap());
178 
179  // test operator many times
180  bool result = true;
181  ST diffNorm = 0.0, trueNorm = 0.0;
182  for (int i = 0; i < count; i++) {
183  xf.putScalar(0.0);
184  xs.putScalar(0.0);
185  y.randomize();
186 
187  // apply operator
188  apply(y, xs); // xs = A*y
189  fullContent_->apply(y, xf); // xf = A*y
190 
191  // compute norms
192  xs.update(-1.0, xf, 1.0);
193  diffNorm = xs.norm2();
194  trueNorm = xf.norm2();
195 
196  // check result
197  result &= (diffNorm / trueNorm < tol);
198  }
199 
200  return result;
201 }
202 
203 } // end namespace TpetraHelpers
204 } // end namespace Teko
Class that describes how a flat blocked operator should be reordered.
void RemoveReording()
Remove any reordering on this object.
const RCP< const Thyra::LinearOpBase< ST > > getThyraOp() const
Return the thyra operator associated with this wrapper.
virtual void WriteBlocks(const std::string &prefix) const
BlockedTpetraOperator(const std::vector< std::vector< GO > > &vars, const Teuchos::RCP< const Tpetra::Operator< ST, LO, GO, NT > > &content, const std::string &label="<ANYM>")
bool testAgainstFullOperator(int count, ST tol) const
Helps perform sanity checks.
virtual void SetContent(const std::vector< std::vector< GO > > &vars, const Teuchos::RCP< const Tpetra::Operator< ST, LO, GO, NT > > &content)
Implements the Epetra_Operator interface with a Thyra LinearOperator. This enables the use of absrtac...