Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_TpetraInverseFactoryOperator.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_TpetraInverseFactoryOperator.hpp"
48 
49 // Teko includes
50 #include "Teko_TpetraBasicMappingStrategy.hpp"
51 
52 // Thyra includes
53 #include "Thyra_TpetraLinearOp.hpp"
54 
55 using Teuchos::RCP;
56 using Teuchos::rcp;
57 using Teuchos::rcp_dynamic_cast;
58 using Teuchos::rcpFromRef;
59 
60 namespace Teko {
61 namespace TpetraHelpers {
62 
69 InverseFactoryOperator::InverseFactoryOperator(const Teuchos::RCP<const InverseFactory>& ifp)
70  : inverseFactory_(ifp), firstBuildComplete_(false), setConstFwdOp_(true) {}
71 
85  if (not clearOld) return;
86  invOperator_ = Teuchos::null;
87 }
88 
102  const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> >& A, bool clear) {
103  Teko_DEBUG_SCOPE("InverseFactoryOperator::buildInverseOperator", 10);
104 
105  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
106  RCP<const Thyra::LinearOpBase<ST> > thyraA = extractLinearOp(A);
107 
108  // set the mapping strategy
109  SetMapStrategy(rcp(new InverseMappingStrategy(extractMappingStrategy(A))));
110 
111  initInverse(clear);
112 
113  // actually build the inverse operator
114  invOperator_ = Teko::buildInverse(*inverseFactory_, thyraA);
115 
116  SetOperator(invOperator_, false);
117 
118  firstBuildComplete_ = true;
119 
120  if (setConstFwdOp_) fwdOp_ = A;
121 
122  setConstFwdOp_ = true;
123 
124  TEUCHOS_ASSERT(invOperator_ != Teuchos::null);
125  TEUCHOS_ASSERT(getForwardOp() != Teuchos::null);
126  TEUCHOS_ASSERT(getThyraOp() != Teuchos::null);
127  TEUCHOS_ASSERT(getMapStrategy() != Teuchos::null);
128  TEUCHOS_ASSERT(firstBuildComplete_ == true);
129 }
130 
132  const Teuchos::RCP<Tpetra::Operator<ST, LO, GO, NT> >& A, bool /* clear */) {
133  setConstFwdOp_ = false;
134 
135  fwdOp_.initialize(A);
136 
137  buildInverseOperator(A.getConst());
138 
139  TEUCHOS_ASSERT(setConstFwdOp_ == true);
140 }
141 
155  const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> >& A) {
156  Teko_DEBUG_SCOPE("InverseFactoryOperator::rebuildPreconditioner", 10);
157 
158  // if the inverse hasn't been built yet, rebuild from scratch
159  if (not firstBuildComplete_) {
160  buildInverseOperator(A, false);
161  return;
162  }
163 
164  RCP<const Thyra::LinearOpBase<ST> > thyraA = extractLinearOp(A);
165  Teko::rebuildInverse(*inverseFactory_, thyraA, invOperator_);
166 
167  if (setConstFwdOp_) fwdOp_.initialize(A);
168 
169  SetOperator(invOperator_, false);
170 
171  setConstFwdOp_ = true;
172 
173  TEUCHOS_ASSERT(getForwardOp() != Teuchos::null);
174  TEUCHOS_ASSERT(invOperator_ != Teuchos::null);
175  TEUCHOS_ASSERT(getThyraOp() != Teuchos::null);
176  TEUCHOS_ASSERT(firstBuildComplete_ == true);
177 }
178 
180  const Teuchos::RCP<Tpetra::Operator<ST, LO, GO, NT> >& A) {
181  setConstFwdOp_ = false;
182 
183  fwdOp_.initialize(A);
184 
185  // build from constant epetra operator
186  rebuildInverseOperator(A.getConst());
187 
188  TEUCHOS_ASSERT(setConstFwdOp_ == true);
189 }
190 
191 Teuchos::RCP<const Thyra::LinearOpBase<ST> > InverseFactoryOperator::extractLinearOp(
192  const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> >& A) const {
193  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
194  const RCP<const TpetraOperatorWrapper>& eow = rcp_dynamic_cast<const TpetraOperatorWrapper>(A);
195 
196  // if it is an EpetraOperatorWrapper, then get the Thyra operator
197  if (eow != Teuchos::null) return eow->getThyraOp();
198 
199  // otherwise wrap it up as a thyra operator
200  return Thyra::constTpetraLinearOp<ST, LO, GO, NT>(
201  Thyra::tpetraVectorSpace<ST, LO, GO, NT>(A->getRangeMap()),
202  Thyra::tpetraVectorSpace<ST, LO, GO, NT>(A->getDomainMap()), A);
203 }
204 
205 Teuchos::RCP<const MappingStrategy> InverseFactoryOperator::extractMappingStrategy(
206  const Teuchos::RCP<const Tpetra::Operator<ST, LO, GO, NT> >& A) const {
207  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
208  const RCP<const TpetraOperatorWrapper>& eow = rcp_dynamic_cast<const TpetraOperatorWrapper>(A);
209 
210  // if it is an EpetraOperatorWrapper, then get the Thyra operator
211  if (eow != Teuchos::null) return eow->getMapStrategy();
212 
213  // otherwise wrap it up as a thyra operator
214  RCP<const Tpetra::Map<LO, GO, NT> > range = A->getRangeMap();
215  RCP<const Tpetra::Map<LO, GO, NT> > domain = A->getDomainMap();
216  return rcp(
217  new BasicMappingStrategy(range, domain, *Thyra::convertTpetraToThyraComm(range->getComm())));
218 }
219 
220 } // end namespace TpetraHelpers
221 } // end namespace Teko
void rebuildInverse(const InverseFactory &factory, const LinearOp &A, InverseLinearOp &invA)
virtual void initInverse(bool clearOld=false)
Build the underlying data structure for the inverse operator.
virtual void buildInverseOperator(const Teuchos::RCP< const Tpetra::Operator< ST, LO, GO, NT > > &A, bool clear=true)
Build this inverse operator from an Epetra_Operator passed in to this object.
Teuchos::RCP< const Tpetra::Operator< ST, LO, GO, NT > > getForwardOp() const
const RCP< const Thyra::LinearOpBase< ST > > getThyraOp() const
Return the thyra operator associated with this wrapper.
Flip a mapping strategy object around to give the &quot;inverse&quot; mapping strategy.
InverseLinearOp buildInverse(const InverseFactory &factory, const LinearOp &A)
Build an inverse operator using a factory and a linear operator.
const RCP< const MappingStrategy > getMapStrategy() const
Get the mapping strategy for this wrapper (translate between Thyra and Epetra)
virtual void rebuildInverseOperator(const Teuchos::RCP< const Tpetra::Operator< ST, LO, GO, NT > > &A)
Rebuild this inverse from an Epetra_Operator passed in this to object.
Implements the Epetra_Operator interface with a Thyra LinearOperator. This enables the use of absrtac...