Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_BlockUpperTriInverseOp.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_BlockUpperTriInverseOp.hpp"
48 
49 #include "Teuchos_Utils.hpp"
50 
51 namespace Teko {
52 
53 using Teuchos::RCP;
54 
65 BlockUpperTriInverseOp::BlockUpperTriInverseOp(BlockedLinearOp& U,
66  const std::vector<LinearOp>& invDiag)
67  : U_(U) {
68  invDiag_ = invDiag;
69 
70  // sanity check
71  int blocks = blockRowCount(U_);
72  TEUCHOS_ASSERT(blocks > 0);
73  TEUCHOS_ASSERT(blocks == blockColCount(U_));
74  TEUCHOS_ASSERT(blocks == (int)invDiag_.size());
75 
76  // create the range and product space
78 
79  // just flip flop them!
80  productRange_ = U->productDomain();
81  productDomain_ = U->productRange();
82 }
83 
84 void BlockUpperTriInverseOp::implicitApply(const BlockedMultiVector& src, BlockedMultiVector& dst,
85  const double alpha, const double beta) const {
86  // call the no tranpose version
87  implicitApply(Thyra::NOTRANS, src, dst, alpha, beta);
88 }
89 
102 void BlockUpperTriInverseOp::implicitApply(const Thyra::EOpTransp M_trans,
103  const BlockedMultiVector& src, BlockedMultiVector& dst,
104  const double alpha, const double beta) const {
105  int blocks = blockCount(src);
106 
107  TEUCHOS_ASSERT(blocks == blockRowCount(U_));
108  TEUCHOS_ASSERT(blocks == blockCount(dst));
109 
110  // build a scrap vector for storing work
111  srcScrap_ = datacopy(src, srcScrap_);
112  BlockedMultiVector dstCopy;
113  if (beta != 0.0) {
114  dstScrap_ = datacopy(dst, dstScrap_);
115  dstCopy = dstScrap_;
116  } else
117  dstCopy = dst; // shallow copy
118 
119  // extract the blocks components from
120  // the source and destination vectors
121  std::vector<MultiVector> dstVec;
122  std::vector<MultiVector> scrapVec;
123  for (int b = 0; b < blocks; b++) {
124  dstVec.push_back(getBlock(b, dstCopy));
125  scrapVec.push_back(getBlock(b, srcScrap_));
126  }
127 
128  // run back-substituion: run over each column
129  // From Heath pg. 66
130  if (M_trans == Thyra::NOTRANS) {
131  for (int b = blocks - 1; b >= 0; b--) {
132  applyOp(invDiag_[b], scrapVec[b], dstVec[b]);
133 
134  // loop over each row
135  for (int i = 0; i < b; i++) {
136  LinearOp u_ib = getBlock(i, b, U_);
137  if (u_ib != Teuchos::null) {
138  applyOp(u_ib, dstVec[b], scrapVec[i], -1.0, 1.0);
139  }
140  }
141  }
142  } else if (M_trans == Thyra::TRANS || M_trans == Thyra::CONJTRANS) {
143  for (int b = 0; b < blocks; b++) {
144  applyTransposeOp(invDiag_[b], scrapVec[b], dstVec[b]);
145 
146  // loop over each row
147  for (int i = b + 1; i < blocks; i++) {
148  LinearOp u_bi = getBlock(b, i, U_);
149  if (u_bi != Teuchos::null) {
150  applyTransposeOp(u_bi, dstVec[b], scrapVec[i], -1.0, 1.0);
151  }
152  }
153  }
154  } else {
155  TEUCHOS_TEST_FOR_EXCEPT(true);
156  }
157 
158  // scale result by alpha
159  if (beta != 0)
160  update(alpha, dstCopy, beta, dst); // dst = alpha * dstCopy + beta * dst
161  else if (alpha != 1.0)
162  scale(alpha, dst); // dst = alpha * dst
163 }
164 
165 void BlockUpperTriInverseOp::describe(Teuchos::FancyOStream& out_arg,
166  const Teuchos::EVerbosityLevel verbLevel) const {
167  using Teuchos::OSTab;
168 
169  RCP<Teuchos::FancyOStream> out = rcp(&out_arg, false);
170  OSTab tab(out);
171  switch (verbLevel) {
172  case Teuchos::VERB_DEFAULT:
173  case Teuchos::VERB_LOW: *out << this->description() << std::endl; break;
174  case Teuchos::VERB_MEDIUM:
175  case Teuchos::VERB_HIGH:
176  case Teuchos::VERB_EXTREME: {
177  *out << Teuchos::Describable::description() << "{"
178  << "rangeDim=" << this->range()->dim() << ",domainDim=" << this->domain()->dim()
179  << ",rows=" << blockRowCount(U_) << ",cols=" << blockColCount(U_) << "}\n";
180  {
181  OSTab tab2(out);
182  *out << "[U Operator] = ";
183  *out << Teuchos::describe(*U_, verbLevel);
184  }
185  {
186  OSTab tab2(out);
187  *out << "[invDiag Operators]:\n";
188  tab.incrTab();
189  for (int i = 0; i < blockRowCount(U_); i++) {
190  *out << "[invD(" << i << ")] = ";
191  *out << Teuchos::describe(*invDiag_[i], verbLevel);
192  }
193  }
194  break;
195  }
196  default: TEUCHOS_TEST_FOR_EXCEPT(true); // Should never get here!
197  }
198 }
199 
200 } // end namespace Teko
virtual VectorSpace domain() const
Domain space of this operator.
std::vector< LinearOp > invDiag_
(Approximate) Inverses of the diagonal operators
virtual VectorSpace range() const
Range space of this operator.
Teuchos::RCP< const Thyra::ProductVectorSpaceBase< double > > productRange_
Range vector space.
virtual void implicitApply(const BlockedMultiVector &x, BlockedMultiVector &y, const double alpha=1.0, const double beta=0.0) const
Perform a matrix vector multiply with this operator.
const BlockedLinearOp U_
operator
Teuchos::RCP< const Thyra::ProductVectorSpaceBase< double > > productDomain_
Domain vector space.