Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_SolveInverseFactory.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_SolveInverseFactory.hpp"
48 
49 // Thyra includes
50 #include "Thyra_DefaultLinearOpSource.hpp"
51 #include "Thyra_DefaultInverseLinearOp.hpp"
52 #include "Thyra_DefaultPreconditioner.hpp"
53 
54 // Stratimikos includes
55 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
56 
57 // Teko includes
58 #include "Teko_Utilities.hpp"
59 #include "Teko_BlockPreconditionerFactory.hpp"
60 #include "Teko_Preconditioner.hpp"
61 #include "Teko_PreconditionerLinearOp.hpp"
62 
63 using Teuchos::rcp;
64 using Teuchos::RCP;
65 using Teuchos::rcp_const_cast;
66 using Teuchos::rcp_dynamic_cast;
67 
68 namespace Teko {
69 
79 SolveInverseFactory::SolveInverseFactory(
80  const Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<double> >& lowsFactory)
81  : lowsFactory_(lowsFactory) {}
82 
84 SolveInverseFactory::SolveInverseFactory(const SolveInverseFactory& siFactory)
85  : lowsFactory_(siFactory.lowsFactory_) {}
86 
96 InverseLinearOp SolveInverseFactory::buildInverse(const LinearOp& linearOp) const {
97  Teko_DEBUG_SCOPE("SolveInverseFactory::buildInverse(linearOp)", 10);
98 
99  // build and initialize inverse linear op with solve
100  Teuchos::RCP<Thyra::LinearOpWithSolveBase<double> > invLOWS = lowsFactory_->createOp();
101  lowsFactory_->initializeOp(Thyra::defaultLinearOpSource(linearOp), &*invLOWS,
102  Thyra::SUPPORT_SOLVE_FORWARD_ONLY);
103 
104  return Thyra::nonconstInverse<double>(invLOWS);
105 }
106 
119 InverseLinearOp SolveInverseFactory::buildInverse(const LinearOp& linearOp,
120  const LinearOp& precOp) const {
121  Teko_DEBUG_SCOPE("SolveInverseFactory::buildInverse(linearOp,precOp)", 10);
122 
123  // build and initialize inverse linear op with solve
124  Teuchos::RCP<Thyra::LinearOpWithSolveBase<double> > invLOWS = lowsFactory_->createOp();
125  lowsFactory_->initializePreconditionedOp(Thyra::defaultLinearOpSource(linearOp),
126  Thyra::unspecifiedPrec(precOp), &*invLOWS,
127  Thyra::SUPPORT_SOLVE_FORWARD_ONLY);
128 
129  return Thyra::nonconstInverse<double>(invLOWS);
130 }
131 
143 void SolveInverseFactory::rebuildInverse(const LinearOp& source, InverseLinearOp& dest) const {
144  RCP<Thyra::DefaultInverseLinearOp<double> > invDest =
145  rcp_dynamic_cast<Thyra::DefaultInverseLinearOp<double> >(dest);
146  RCP<Thyra::LinearOpWithSolveBase<double> > lows = invDest->getNonconstLows();
147 
148  // This stems from confusion of if the linear op with solve initializeAndResuseOp actually
149  // rebuilds the preconditioner. It seems not to and thus we have a fairly substantial problem.
150  // lowsFactory_->initializeAndReuseOp(Thyra::defaultLinearOpSource(source),&*lows);
151  lowsFactory_->initializeOp(Thyra::defaultLinearOpSource(source), &*lows);
152 }
153 
166 void SolveInverseFactory::rebuildInverse(const LinearOp& source, const LinearOp& precOp,
167  InverseLinearOp& dest) const {
168  RCP<Thyra::DefaultInverseLinearOp<double> > invDest =
169  rcp_dynamic_cast<Thyra::DefaultInverseLinearOp<double> >(dest);
170  RCP<Thyra::LinearOpWithSolveBase<double> > lows = invDest->getNonconstLows();
171 
172  // lowsFactory_->initializeAndReuseOp(Thyra::defaultLinearOpSource(source),&*lows);
173  lowsFactory_->initializePreconditionedOp(Thyra::defaultLinearOpSource(source),
174  Thyra::unspecifiedPrec(precOp), &*lows,
175  Thyra::SUPPORT_SOLVE_FORWARD_ONLY);
176 }
177 
186 Teuchos::RCP<const Teuchos::ParameterList> SolveInverseFactory::getParameterList() const {
187  return lowsFactory_->getParameterList();
188 }
189 
190 } // end namespace Teko