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;
112 using Teuchos::rcp_const_cast;
113 using Teuchos::rcp_dynamic_cast;
114 
115 namespace Teko {
116 
118 InverseLinearOp buildInverse(const InverseFactory& factory, const LinearOp& A) {
119  InverseLinearOp inv;
120  try {
121  inv = factory.buildInverse(A);
122  } catch (std::exception& e) {
123  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
124 
125  *out << "Teko: \"buildInverse\" could not construct the inverse operator using ";
126  *out << "\"" << factory.toString() << "\"" << std::endl;
127  *out << std::endl;
128  *out << "*** THROWN EXCEPTION ***\n";
129  *out << e.what() << std::endl;
130  *out << "************************\n";
131 
132  throw e;
133  }
134 
135  return inv;
136 }
137 
149 InverseLinearOp buildInverse(const InverseFactory& factory, const LinearOp& A,
150  const LinearOp& precOp) {
151  Teko_DEBUG_SCOPE("buildInverse(factory,A,precOp)", 10);
152  InverseLinearOp inv;
153  try {
154  inv = factory.buildInverse(A, precOp);
155  } catch (std::exception& e) {
156  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
157 
158  *out << "Teko: \"buildInverse\" could not construct the inverse operator using ";
159  *out << "\"" << factory.toString() << "\"" << std::endl;
160  *out << std::endl;
161  *out << "*** THROWN EXCEPTION ***\n";
162  *out << e.what() << std::endl;
163  *out << "************************\n";
164 
165  throw e;
166  }
167 
168  return inv;
169 }
170 
174 void rebuildInverse(const InverseFactory& factory, const LinearOp& A, InverseLinearOp& invA) {
175  InverseLinearOp inv;
176  try {
177  factory.rebuildInverse(A, invA);
178  } catch (std::exception& e) {
179  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
180 
181  *out << "Teko: \"rebuildInverse\" could not construct the inverse operator using ";
182  *out << "\"" << factory.toString() << "\"" << std::endl;
183  *out << std::endl;
184  *out << "*** THROWN EXCEPTION ***\n";
185  *out << e.what() << std::endl;
186  *out << "************************\n";
187 
188  throw e;
189  }
190 }
191 
207 void rebuildInverse(const InverseFactory& factory, const LinearOp& A, const LinearOp& precOp,
208  InverseLinearOp& invA) {
209  InverseLinearOp inv;
210  try {
211  factory.rebuildInverse(A, precOp, invA);
212  } catch (std::exception& e) {
213  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
214 
215  *out << "Teko: \"rebuildInverse\" could not construct the inverse operator using ";
216  *out << "\"" << factory.toString() << "\"" << std::endl;
217  *out << std::endl;
218  *out << "*** THROWN EXCEPTION ***\n";
219  *out << e.what() << std::endl;
220  *out << "************************\n";
221 
222  throw e;
223  }
224 }
225 
237 RCP<InverseFactory> invFactoryFromParamList(const Teuchos::ParameterList& list,
238  const std::string& type) {
239  RCP<Teuchos::ParameterList> myList = rcp(new Teuchos::ParameterList(list));
240 
241  Stratimikos::DefaultLinearSolverBuilder strat;
242  addToStratimikosBuilder(rcpFromRef(strat));
243  strat.setParameterList(myList);
244 
245  try {
246  // try to build a preconditioner factory
247  RCP<Thyra::PreconditionerFactoryBase<double> > precFact =
248  strat.createPreconditioningStrategy(type);
249 
250  // string must map to a preconditioner
251  return rcp(new PreconditionerInverseFactory(precFact, Teuchos::null));
252  } catch (const Teuchos::Exceptions::InvalidParameterValue& exp) {
253  }
254 
255  try {
256  // try to build a solver factory
257  RCP<Thyra::LinearOpWithSolveFactoryBase<double> > solveFact =
258  strat.createLinearSolveStrategy(type);
259 
260  // if its around, build a InverseFactory
261  return rcp(new SolveInverseFactory(solveFact));
262  } catch (const Teuchos::Exceptions::InvalidParameterValue& exp) {
263  }
264 
265  return Teuchos::null;
266  ;
267 }
268 
277 Teuchos::RCP<const Teuchos::ParameterList> invFactoryValidParameters() {
278  Stratimikos::DefaultLinearSolverBuilder strat;
279 
280  // extract valid parameter list from Stratimikos
281  return strat.getValidParameters();
282 }
283 
284 } // 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)