Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_InverseFactoryOperator.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_InverseFactoryOperator.hpp"
48 
49 // Teko includes
50 #include "Teko_BasicMappingStrategy.hpp"
51 
52 // Thyra includes
53 #include "Thyra_EpetraLinearOp.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 Epetra {
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 
101 void InverseFactoryOperator::buildInverseOperator(const Teuchos::RCP<const Epetra_Operator>& A,
102  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<double> > 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 
131 void InverseFactoryOperator::buildInverseOperator(const Teuchos::RCP<Epetra_Operator>& A,
132  bool /* clear */) {
133  setConstFwdOp_ = false;
134 
135  fwdOp_.initialize(A);
136 
137  buildInverseOperator(A.getConst());
138 
139  TEUCHOS_ASSERT(setConstFwdOp_ == true);
140 }
141 
154 void InverseFactoryOperator::rebuildInverseOperator(const Teuchos::RCP<const Epetra_Operator>& A) {
155  Teko_DEBUG_SCOPE("InverseFactoryOperator::rebuildPreconditioner", 10);
156 
157  // if the inverse hasn't been built yet, rebuild from scratch
158  if (not firstBuildComplete_) {
159  buildInverseOperator(A, false);
160  return;
161  }
162 
163  RCP<const Thyra::LinearOpBase<double> > thyraA = extractLinearOp(A);
164  Teko::rebuildInverse(*inverseFactory_, thyraA, invOperator_);
165 
166  if (setConstFwdOp_) fwdOp_.initialize(A);
167 
168  SetOperator(invOperator_, false);
169 
170  setConstFwdOp_ = true;
171 
172  TEUCHOS_ASSERT(getForwardOp() != Teuchos::null);
173  TEUCHOS_ASSERT(invOperator_ != Teuchos::null);
174  TEUCHOS_ASSERT(getThyraOp() != Teuchos::null);
175  TEUCHOS_ASSERT(firstBuildComplete_ == true);
176 }
177 
178 void InverseFactoryOperator::rebuildInverseOperator(const Teuchos::RCP<Epetra_Operator>& A) {
179  setConstFwdOp_ = false;
180 
181  fwdOp_.initialize(A);
182 
183  // build from constant epetra operator
184  rebuildInverseOperator(A.getConst());
185 
186  TEUCHOS_ASSERT(setConstFwdOp_ == true);
187 }
188 
189 Teuchos::RCP<const Thyra::LinearOpBase<double> > InverseFactoryOperator::extractLinearOp(
190  const Teuchos::RCP<const Epetra_Operator>& A) const {
191  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
192  const RCP<const EpetraOperatorWrapper>& eow = rcp_dynamic_cast<const EpetraOperatorWrapper>(A);
193 
194  // if it is an EpetraOperatorWrapper, then get the Thyra operator
195  if (eow != Teuchos::null) return eow->getThyraOp();
196 
197  // otherwise wrap it up as a thyra operator
198  return Thyra::epetraLinearOp(A);
199 }
200 
201 Teuchos::RCP<const MappingStrategy> InverseFactoryOperator::extractMappingStrategy(
202  const Teuchos::RCP<const Epetra_Operator>& A) const {
203  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
204  const RCP<const EpetraOperatorWrapper>& eow = rcp_dynamic_cast<const EpetraOperatorWrapper>(A);
205 
206  // if it is an EpetraOperatorWrapper, then get the Thyra operator
207  if (eow != Teuchos::null) return eow->getMapStrategy();
208 
209  // otherwise wrap it up as a thyra operator
210  RCP<const Epetra_Map> range = rcpFromRef(A->OperatorRangeMap());
211  RCP<const Epetra_Map> domain = rcpFromRef(A->OperatorDomainMap());
212  return rcp(new BasicMappingStrategy(range, domain, A->Comm()));
213 }
214 
215 } // end namespace Epetra
216 } // end namespace Teko
void rebuildInverse(const InverseFactory &factory, const LinearOp &A, InverseLinearOp &invA)
Teuchos::RCP< const Epetra_Operator > getForwardOp() const
virtual void rebuildInverseOperator(const Teuchos::RCP< const Epetra_Operator > &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...
virtual void initInverse(bool clearOld=false)
Build the underlying data structure for the inverse operator.
Flip a mapping strategy object around to give the &quot;inverse&quot; mapping strategy.
const RCP< const MappingStrategy > getMapStrategy() const
Get the mapping strategy for this wrapper (translate between Thyra and Epetra)
InverseLinearOp buildInverse(const InverseFactory &factory, const LinearOp &A)
Build an inverse operator using a factory and a linear operator.
const RCP< const Thyra::LinearOpBase< double > > getThyraOp() const
Return the thyra operator associated with this wrapper.
virtual void buildInverseOperator(const Teuchos::RCP< const Epetra_Operator > &A, bool clear=true)
Build this inverse operator from an Epetra_Operator passed in to this object.