Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_InverseFactory.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 /*
48 // @header
49 //
50 // ***********************************************************************
51 //
52 // teko: a package for block and physics based preconditioning
53 // copyright 2010 sandia corporation
54 //
55 // under the terms of contract de-ac04-94al85000 with sandia corporation,
56 // the u.s. government retains certain rights in this software.
57 //
58 // redistribution and use in source and binary forms, with or without
59 // modification, are permitted provided that the following conditions are
60 // met:
61 //
62 // 1. redistributions of source code must retain the above copyright
63 // notice, this list of conditions and the following disclaimer.
64 //
65 // 2. redistributions in binary form must reproduce the above copyright
66 // notice, this list of conditions and the following disclaimer in the
67 // documentation and/or other materials provided with the distribution.
68 //
69 // 3. neither the name of the corporation nor the names of the
70 // contributors may be used to endorse or promote products derived from
71 // this software without specific prior written permission.
72 //
73 // this software is provided by sandia corporation "as is" and any
74 // express or implied warranties, including, but not limited to, the
75 // implied warranties of merchantability and fitness for a particular
76 // purpose are disclaimed. in no event shall sandia corporation or the
77 // contributors be liable for any direct, indirect, incidental, special,
78 // exemplary, or consequential damages (including, but not limited to,
79 // procurement of substitute goods or services; loss of use, data, or
80 // profits; or business interruption) however caused and on any theory of
81 // liability, whether in contract, strict liability, or tort (including
82 // negligence or otherwise) arising in any way out of the use of this
83 // software, even if advised of the possibility of such damage.
84 //
85 // questions? contact eric c. cyr (eccyr@sandia.gov)
86 //
87 // ***********************************************************************
88 //
89 // @header
90 */
91 
92 #include "Teko_InverseFactory.hpp"
93 
94 // Thyra includes
95 #include "Thyra_DefaultLinearOpSource.hpp"
96 #include "Thyra_DefaultInverseLinearOp.hpp"
97 #include "Thyra_DefaultPreconditioner.hpp"
98 
99 // Stratimikos includes
100 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
101 
102 // Teko includes
103 #include "Teko_Utilities.hpp"
104 #include "Teko_BlockPreconditionerFactory.hpp"
105 #include "Teko_Preconditioner.hpp"
106 #include "Teko_PreconditionerLinearOp.hpp"
107 #include "Teko_SolveInverseFactory.hpp"
108 #include "Teko_PreconditionerInverseFactory.hpp"
109 
110 using Teuchos::rcp;
111 using Teuchos::rcp_const_cast;
112 using Teuchos::rcp_dynamic_cast;
113 using Teuchos::RCP;
114 
115 namespace Teko {
116 
118 InverseLinearOp buildInverse(const InverseFactory & factory,const LinearOp & A)
119 {
120  InverseLinearOp inv;
121  try {
122  inv = factory.buildInverse(A);
123  }
124  catch(std::exception & e) {
125  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
126 
127  *out << "Teko: \"buildInverse\" could not construct the inverse operator using ";
128  *out << "\"" << factory.toString() << "\"" << std::endl;
129  *out << std::endl;
130  *out << "*** THROWN EXCEPTION ***\n";
131  *out << e.what() << std::endl;
132  *out << "************************\n";
133 
134  throw e;
135  }
136 
137  return inv;
138 }
139 
151 InverseLinearOp buildInverse(const InverseFactory & factory,const LinearOp & A,const LinearOp & precOp)
152 {
153  Teko_DEBUG_SCOPE("buildInverse(factory,A,precOp)",10);
154  InverseLinearOp inv;
155  try {
156  inv = factory.buildInverse(A,precOp);
157  }
158  catch(std::exception & e) {
159  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
160 
161  *out << "Teko: \"buildInverse\" could not construct the inverse operator using ";
162  *out << "\"" << factory.toString() << "\"" << std::endl;
163  *out << std::endl;
164  *out << "*** THROWN EXCEPTION ***\n";
165  *out << e.what() << std::endl;
166  *out << "************************\n";
167 
168  throw e;
169  }
170 
171  return inv;
172 }
173 
177 void rebuildInverse(const InverseFactory & factory, const LinearOp & A, InverseLinearOp & invA)
178 {
179  InverseLinearOp inv;
180  try {
181  factory.rebuildInverse(A,invA);
182  }
183  catch(std::exception & e) {
184  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
185 
186  *out << "Teko: \"rebuildInverse\" could not construct the inverse operator using ";
187  *out << "\"" << factory.toString() << "\"" << std::endl;
188  *out << std::endl;
189  *out << "*** THROWN EXCEPTION ***\n";
190  *out << e.what() << std::endl;
191  *out << "************************\n";
192 
193  throw e;
194  }
195 }
196 
212 void rebuildInverse(const InverseFactory & factory, const LinearOp & A,const LinearOp & precOp, InverseLinearOp & invA)
213 {
214  InverseLinearOp inv;
215  try {
216  factory.rebuildInverse(A,precOp,invA);
217  }
218  catch(std::exception & e) {
219  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
220 
221  *out << "Teko: \"rebuildInverse\" could not construct the inverse operator using ";
222  *out << "\"" << factory.toString() << "\"" << std::endl;
223  *out << std::endl;
224  *out << "*** THROWN EXCEPTION ***\n";
225  *out << e.what() << std::endl;
226  *out << "************************\n";
227 
228  throw e;
229  }
230 }
231 
243 RCP<InverseFactory> invFactoryFromParamList(const Teuchos::ParameterList & list,const std::string & type)
244 {
245  RCP<Teuchos::ParameterList> myList = rcp(new Teuchos::ParameterList(list));
246 
247  Stratimikos::DefaultLinearSolverBuilder strat;
248  addToStratimikosBuilder(rcpFromRef(strat));
249  strat.setParameterList(myList);
250 
251  try {
252  // try to build a preconditioner factory
253  RCP<Thyra::PreconditionerFactoryBase<double> > precFact = strat.createPreconditioningStrategy(type);
254 
255  // string must map to a preconditioner
256  return rcp(new PreconditionerInverseFactory(precFact,Teuchos::null));
257  }
258  catch(const Teuchos::Exceptions::InvalidParameterValue & exp) { }
259 
260  try {
261  // try to build a solver factory
262  RCP<Thyra::LinearOpWithSolveFactoryBase<double> > solveFact = strat.createLinearSolveStrategy(type);
263 
264  // if its around, build a InverseFactory
265  return rcp(new SolveInverseFactory(solveFact));
266  }
267  catch(const Teuchos::Exceptions::InvalidParameterValue & exp) { }
268 
269  return Teuchos::null;;
270 }
271 
280 Teuchos::RCP<const Teuchos::ParameterList> invFactoryValidParameters()
281 {
282  Stratimikos::DefaultLinearSolverBuilder strat;
283 
284  // extract valid parameter list from Stratimikos
285  return strat.getValidParameters();
286 }
287 
288 } // end namespace Teko
void rebuildInverse(const InverseFactory &factory, const LinearOp &A, InverseLinearOp &invA)
Abstract class for building an inverse operator.
virtual InverseLinearOp buildInverse(const LinearOp &linearOp) const =0
Build an inverse operator.
RCP< InverseFactory > invFactoryFromParamList(const Teuchos::ParameterList &list, const std::string &type)
Build an InverseFactory object from a ParameterList, as specified in Stratimikos. ...
virtual std::string toString() const =0
InverseLinearOp buildInverse(const InverseFactory &factory, const LinearOp &A, const LinearOp &precOp)
InverseLinearOp buildInverse(const InverseFactory &factory, const LinearOp &A)
Build an inverse operator using a factory and a linear operator.
Teuchos::RCP< const Teuchos::ParameterList > invFactoryValidParameters()
Get a valid parameter list for the inverse factory class.
virtual void rebuildInverse(const LinearOp &source, InverseLinearOp &dest) const =0
Pass in an already constructed inverse operator. Update the inverse operator based on the new source ...
void rebuildInverse(const InverseFactory &factory, const LinearOp &A, const LinearOp &precOp, InverseLinearOp &invA)