Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_EpetraBlockPreconditioner.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_EpetraBlockPreconditioner.hpp"
48 #include "Teko_Preconditioner.hpp"
49 
50 // Thyra includes
51 #include "Thyra_DefaultLinearOpSource.hpp"
52 #include "Thyra_EpetraLinearOp.hpp"
53 
54 // Teuchos includes
55 #include "Teuchos_Time.hpp"
56 
57 // Teko includes
58 #include "Teko_BasicMappingStrategy.hpp"
59 
60 namespace Teko {
61 namespace Epetra {
62 
63 using Teuchos::RCP;
64 using Teuchos::rcp;
65 using Teuchos::rcp_dynamic_cast;
66 using Teuchos::rcpFromRef;
67 
74 EpetraBlockPreconditioner::EpetraBlockPreconditioner(
75  const Teuchos::RCP<const PreconditionerFactory>& bfp)
76  : preconFactory_(bfp), firstBuildComplete_(false) {}
77 
79  if ((not clearOld) && preconObj_ != Teuchos::null) return;
80  preconObj_ = preconFactory_->createPrec();
81 }
82 
95 void EpetraBlockPreconditioner::buildPreconditioner(const Teuchos::RCP<const Epetra_Operator>& A,
96  bool clear) {
97  Teko_DEBUG_SCOPE("EBP::buildPreconditioner", 10);
98 
99  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
100  RCP<const Thyra::LinearOpBase<double> > thyraA = extractLinearOp(A);
101 
102  // set the mapping strategy
103  // SetMapStrategy(rcp(new InverseMappingStrategy(eow->getMapStrategy())));
104  SetMapStrategy(rcp(new InverseMappingStrategy(extractMappingStrategy(A))));
105 
106  // build preconObj_
107  initPreconditioner(clear);
108 
109  // actually build the preconditioner
110  RCP<const Thyra::LinearOpSourceBase<double> > lOpSrc = Thyra::defaultLinearOpSource(thyraA);
111  preconFactory_->initializePrec(lOpSrc, &*preconObj_, Thyra::SUPPORT_SOLVE_UNSPECIFIED);
112 
113  // extract preconditioner operator
114  RCP<const Thyra::LinearOpBase<double> > preconditioner = preconObj_->getUnspecifiedPrecOp();
115 
116  SetOperator(preconditioner, false);
117 
118  firstBuildComplete_ = true;
119 
120  TEUCHOS_ASSERT(preconObj_ != Teuchos::null);
121  TEUCHOS_ASSERT(getThyraOp() != Teuchos::null);
122  TEUCHOS_ASSERT(getMapStrategy() != Teuchos::null);
123  TEUCHOS_ASSERT(firstBuildComplete_ == true);
124 }
125 
139 void EpetraBlockPreconditioner::buildPreconditioner(const Teuchos::RCP<const Epetra_Operator>& A,
140  const Epetra_MultiVector& epetra_mv,
141  bool clear) {
142  Teko_DEBUG_SCOPE("EBP::buildPreconditioner - with solution", 10);
143 
144  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
145  RCP<const Thyra::LinearOpBase<double> > thyraA = extractLinearOp(A);
146 
147  // set the mapping strategy
148  SetMapStrategy(rcp(new InverseMappingStrategy(extractMappingStrategy(A))));
149 
150  TEUCHOS_ASSERT(getMapStrategy() != Teuchos::null);
151 
152  // build the thyra version of the source multivector
153  RCP<Thyra::MultiVectorBase<double> > thyra_mv =
154  Thyra::createMembers(thyraA->range(), epetra_mv.NumVectors());
155  getMapStrategy()->copyEpetraIntoThyra(epetra_mv, thyra_mv.ptr());
156 
157  // build preconObj_
158  initPreconditioner(clear);
159 
160  // actually build the preconditioner
161  preconFactory_->initializePrec(Thyra::defaultLinearOpSource(thyraA), thyra_mv, &*preconObj_,
162  Thyra::SUPPORT_SOLVE_UNSPECIFIED);
163  RCP<const Thyra::LinearOpBase<double> > preconditioner = preconObj_->getUnspecifiedPrecOp();
164 
165  SetOperator(preconditioner, false);
166 
167  firstBuildComplete_ = true;
168 
169  TEUCHOS_ASSERT(preconObj_ != Teuchos::null);
170  TEUCHOS_ASSERT(getThyraOp() != Teuchos::null);
171  TEUCHOS_ASSERT(getMapStrategy() != Teuchos::null);
172  TEUCHOS_ASSERT(firstBuildComplete_ == true);
173 }
174 
189  const Teuchos::RCP<const Epetra_Operator>& A) {
190  Teko_DEBUG_SCOPE("EBP::rebuildPreconditioner", 10);
191 
192  // if the preconditioner hasn't been built yet, rebuild from scratch
193  if (not firstBuildComplete_) {
194  buildPreconditioner(A, false);
195  return;
196  }
197  Teko_DEBUG_EXPR(Teuchos::Time timer(""));
198 
199  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
200  Teko_DEBUG_EXPR(timer.start(true));
201  RCP<const Thyra::LinearOpBase<double> > thyraA = extractLinearOp(A);
202  Teko_DEBUG_EXPR(timer.stop());
203  Teko_DEBUG_MSG("EBP::rebuild get thyraop time = " << timer.totalElapsedTime(), 2);
204 
205  // reinitialize the preconditioner
206  Teko_DEBUG_EXPR(timer.start(true));
207  preconFactory_->initializePrec(Thyra::defaultLinearOpSource(thyraA), &*preconObj_,
208  Thyra::SUPPORT_SOLVE_UNSPECIFIED);
209  RCP<const Thyra::LinearOpBase<double> > preconditioner = preconObj_->getUnspecifiedPrecOp();
210  Teko_DEBUG_EXPR(timer.stop());
211  Teko_DEBUG_MSG("EBP::rebuild initialize prec time = " << timer.totalElapsedTime(), 2);
212 
213  Teko_DEBUG_EXPR(timer.start(true));
214  SetOperator(preconditioner, false);
215  Teko_DEBUG_EXPR(timer.stop());
216  Teko_DEBUG_MSG("EBP::rebuild set operator time = " << timer.totalElapsedTime(), 2);
217 
218  TEUCHOS_ASSERT(preconObj_ != Teuchos::null);
219  TEUCHOS_ASSERT(getThyraOp() != Teuchos::null);
220  TEUCHOS_ASSERT(firstBuildComplete_ == true);
221 }
222 
236 void EpetraBlockPreconditioner::rebuildPreconditioner(const Teuchos::RCP<const Epetra_Operator>& A,
237  const Epetra_MultiVector& epetra_mv) {
238  Teko_DEBUG_SCOPE("EBP::rebuildPreconditioner - with solution", 10);
239 
240  // if the preconditioner hasn't been built yet, rebuild from scratch
241  if (not firstBuildComplete_) {
242  buildPreconditioner(A, epetra_mv, false);
243  return;
244  }
245  Teko_DEBUG_EXPR(Teuchos::Time timer(""));
246 
247  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
248  Teko_DEBUG_EXPR(timer.start(true));
249  RCP<const Thyra::LinearOpBase<double> > thyraA = extractLinearOp(A);
250  Teko_DEBUG_EXPR(timer.stop());
251  Teko_DEBUG_MSG("EBP::rebuild get thyraop time = " << timer.totalElapsedTime(), 2);
252 
253  // build the thyra version of the source multivector
254  Teko_DEBUG_EXPR(timer.start(true));
255  RCP<Thyra::MultiVectorBase<double> > thyra_mv =
256  Thyra::createMembers(thyraA->range(), epetra_mv.NumVectors());
257  getMapStrategy()->copyEpetraIntoThyra(epetra_mv, thyra_mv.ptr());
258  Teko_DEBUG_EXPR(timer.stop());
259  Teko_DEBUG_MSG("EBP::rebuild vector copy time = " << timer.totalElapsedTime(), 2);
260 
261  // reinitialize the preconditioner
262  Teko_DEBUG_EXPR(timer.start(true));
263  preconFactory_->initializePrec(Thyra::defaultLinearOpSource(thyraA), thyra_mv, &*preconObj_,
264  Thyra::SUPPORT_SOLVE_UNSPECIFIED);
265  RCP<const Thyra::LinearOpBase<double> > preconditioner = preconObj_->getUnspecifiedPrecOp();
266  Teko_DEBUG_EXPR(timer.stop());
267  Teko_DEBUG_MSG("EBP::rebuild initialize prec time = " << timer.totalElapsedTime(), 2);
268 
269  Teko_DEBUG_EXPR(timer.start(true));
270  SetOperator(preconditioner, false);
271  Teko_DEBUG_EXPR(timer.stop());
272  Teko_DEBUG_MSG("EBP::rebuild set operator time = " << timer.totalElapsedTime(), 2);
273 
274  TEUCHOS_ASSERT(preconObj_ != Teuchos::null);
275  TEUCHOS_ASSERT(getThyraOp() != Teuchos::null);
276  TEUCHOS_ASSERT(firstBuildComplete_ == true);
277 }
278 
288 Teuchos::RCP<PreconditionerState> EpetraBlockPreconditioner::getPreconditionerState() {
289  Teuchos::RCP<Preconditioner> bp = rcp_dynamic_cast<Preconditioner>(preconObj_);
290 
291  if (bp != Teuchos::null) return bp->getStateObject();
292 
293  return Teuchos::null;
294 }
295 
305 Teuchos::RCP<const PreconditionerState> EpetraBlockPreconditioner::getPreconditionerState() const {
306  Teuchos::RCP<const Preconditioner> bp = rcp_dynamic_cast<const Preconditioner>(preconObj_);
307 
308  if (bp != Teuchos::null) return bp->getStateObject();
309 
310  return Teuchos::null;
311 }
312 
313 Teuchos::RCP<const Thyra::LinearOpBase<double> > EpetraBlockPreconditioner::extractLinearOp(
314  const Teuchos::RCP<const Epetra_Operator>& A) const {
315  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
316  const RCP<const EpetraOperatorWrapper>& eow = rcp_dynamic_cast<const EpetraOperatorWrapper>(A);
317 
318  // if it is an EpetraOperatorWrapper, then get the Thyra operator
319  if (eow != Teuchos::null) return eow->getThyraOp();
320 
321  // otherwise wrap it up as a thyra operator
322  return Thyra::epetraLinearOp(A);
323 }
324 
325 Teuchos::RCP<const MappingStrategy> EpetraBlockPreconditioner::extractMappingStrategy(
326  const Teuchos::RCP<const Epetra_Operator>& A) const {
327  // extract EpetraOperatorWrapper (throw on failure) and corresponding thyra operator
328  const RCP<const EpetraOperatorWrapper>& eow = rcp_dynamic_cast<const EpetraOperatorWrapper>(A);
329 
330  // if it is an EpetraOperatorWrapper, then get the Thyra operator
331  if (eow != Teuchos::null) return eow->getMapStrategy();
332 
333  // otherwise wrap it up as a thyra operator
334  RCP<const Epetra_Map> range = rcpFromRef(A->OperatorRangeMap());
335  RCP<const Epetra_Map> domain = rcpFromRef(A->OperatorDomainMap());
336  return rcp(new BasicMappingStrategy(range, domain, A->Comm()));
337 }
338 
339 } // end namespace Epetra
340 } // end namespace Teko
virtual const RCP< PreconditionerState > getStateObject()
virtual void buildPreconditioner(const Teuchos::RCP< const Epetra_Operator > &A, bool clear=true)
Build this preconditioner from an Epetra_Operator passed in to this object.
virtual void initPreconditioner(bool clearOld=false)
Build the underlying data structure for the preconditioner.
virtual void rebuildPreconditioner(const Teuchos::RCP< const Epetra_Operator > &A)
Rebuild this preconditioner 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 Teuchos::RCP< PreconditionerState > getPreconditionerState()
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)
An extension of the Thyra::DefaultPreconditioner class with some specializations useful for use withi...
const RCP< const Thyra::LinearOpBase< double > > getThyraOp() const
Return the thyra operator associated with this wrapper.