Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_InverseFactory.cpp
1 // @HEADER
2 // *****************************************************************************
3 // Teko: A package for block and physics based preconditioning
4 //
5 // Copyright 2010 NTESS and the Teko contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 /*
11 // @header
12 //
13 // ***********************************************************************
14 //
15 // teko: a package for block and physics based preconditioning
16 // copyright 2010 sandia corporation
17 //
18 // under the terms of contract de-ac04-94al85000 with sandia corporation,
19 // the u.s. government retains certain rights in this software.
20 //
21 // redistribution and use in source and binary forms, with or without
22 // modification, are permitted provided that the following conditions are
23 // met:
24 //
25 // 1. redistributions of source code must retain the above copyright
26 // notice, this list of conditions and the following disclaimer.
27 //
28 // 2. redistributions in binary form must reproduce the above copyright
29 // notice, this list of conditions and the following disclaimer in the
30 // documentation and/or other materials provided with the distribution.
31 //
32 // 3. neither the name of the corporation nor the names of the
33 // contributors may be used to endorse or promote products derived from
34 // this software without specific prior written permission.
35 //
36 // this software is provided by sandia corporation "as is" and any
37 // express or implied warranties, including, but not limited to, the
38 // implied warranties of merchantability and fitness for a particular
39 // purpose are disclaimed. in no event shall sandia corporation or the
40 // contributors be liable for any direct, indirect, incidental, special,
41 // exemplary, or consequential damages (including, but not limited to,
42 // procurement of substitute goods or services; loss of use, data, or
43 // profits; or business interruption) however caused and on any theory of
44 // liability, whether in contract, strict liability, or tort (including
45 // negligence or otherwise) arising in any way out of the use of this
46 // software, even if advised of the possibility of such damage.
47 //
48 // questions? contact eric c. cyr (eccyr@sandia.gov)
49 //
50 // ***********************************************************************
51 //
52 // @header
53 */
54 
55 #include "Teko_InverseFactory.hpp"
56 
57 // Thyra includes
58 #include "Thyra_DefaultLinearOpSource.hpp"
59 #include "Thyra_DefaultInverseLinearOp.hpp"
60 #include "Thyra_DefaultPreconditioner.hpp"
61 
62 // Stratimikos includes
63 #include "Stratimikos_DefaultLinearSolverBuilder.hpp"
64 
65 // Teko includes
66 #include "Teko_Utilities.hpp"
67 #include "Teko_BlockPreconditionerFactory.hpp"
68 #include "Teko_Preconditioner.hpp"
69 #include "Teko_PreconditionerLinearOp.hpp"
70 #include "Teko_SolveInverseFactory.hpp"
71 #include "Teko_PreconditionerInverseFactory.hpp"
72 
73 using Teuchos::rcp;
74 using Teuchos::RCP;
75 using Teuchos::rcp_const_cast;
76 using Teuchos::rcp_dynamic_cast;
77 
78 namespace Teko {
79 
81 InverseLinearOp buildInverse(const InverseFactory& factory, const LinearOp& A) {
82  InverseLinearOp inv;
83  try {
84  inv = factory.buildInverse(A);
85  } catch (std::exception& e) {
86  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
87 
88  *out << "Teko: \"buildInverse\" could not construct the inverse operator using ";
89  *out << "\"" << factory.toString() << "\"" << std::endl;
90  *out << std::endl;
91  *out << "*** THROWN EXCEPTION ***\n";
92  *out << e.what() << std::endl;
93  *out << "************************\n";
94 
95  throw e;
96  }
97 
98  return inv;
99 }
100 
112 InverseLinearOp buildInverse(const InverseFactory& factory, const LinearOp& A,
113  const LinearOp& precOp) {
114  Teko_DEBUG_SCOPE("buildInverse(factory,A,precOp)", 10);
115  InverseLinearOp inv;
116  try {
117  inv = factory.buildInverse(A, precOp);
118  } catch (std::exception& e) {
119  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
120 
121  *out << "Teko: \"buildInverse\" could not construct the inverse operator using ";
122  *out << "\"" << factory.toString() << "\"" << std::endl;
123  *out << std::endl;
124  *out << "*** THROWN EXCEPTION ***\n";
125  *out << e.what() << std::endl;
126  *out << "************************\n";
127 
128  throw e;
129  }
130 
131  return inv;
132 }
133 
137 void rebuildInverse(const InverseFactory& factory, const LinearOp& A, InverseLinearOp& invA) {
138  InverseLinearOp inv;
139  try {
140  factory.rebuildInverse(A, invA);
141  } catch (std::exception& e) {
142  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
143 
144  *out << "Teko: \"rebuildInverse\" could not construct the inverse operator using ";
145  *out << "\"" << factory.toString() << "\"" << std::endl;
146  *out << std::endl;
147  *out << "*** THROWN EXCEPTION ***\n";
148  *out << e.what() << std::endl;
149  *out << "************************\n";
150 
151  throw e;
152  }
153 }
154 
170 void rebuildInverse(const InverseFactory& factory, const LinearOp& A, const LinearOp& precOp,
171  InverseLinearOp& invA) {
172  InverseLinearOp inv;
173  try {
174  factory.rebuildInverse(A, precOp, invA);
175  } catch (std::exception& e) {
176  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
177 
178  *out << "Teko: \"rebuildInverse\" could not construct the inverse operator using ";
179  *out << "\"" << factory.toString() << "\"" << std::endl;
180  *out << std::endl;
181  *out << "*** THROWN EXCEPTION ***\n";
182  *out << e.what() << std::endl;
183  *out << "************************\n";
184 
185  throw e;
186  }
187 }
188 
200 RCP<InverseFactory> invFactoryFromParamList(const Teuchos::ParameterList& list,
201  const std::string& type) {
202  RCP<Teuchos::ParameterList> myList = rcp(new Teuchos::ParameterList(list));
203 
204  Stratimikos::DefaultLinearSolverBuilder strat;
205  addToStratimikosBuilder(rcpFromRef(strat));
206  strat.setParameterList(myList);
207 
208  try {
209  // try to build a preconditioner factory
210  RCP<Thyra::PreconditionerFactoryBase<double> > precFact =
211  strat.createPreconditioningStrategy(type);
212 
213  // string must map to a preconditioner
214  return rcp(new PreconditionerInverseFactory(precFact, Teuchos::null));
215  } catch (const Teuchos::Exceptions::InvalidParameterValue& exp) {
216  }
217 
218  try {
219  // try to build a solver factory
220  RCP<Thyra::LinearOpWithSolveFactoryBase<double> > solveFact =
221  strat.createLinearSolveStrategy(type);
222 
223  // if its around, build a InverseFactory
224  return rcp(new SolveInverseFactory(solveFact));
225  } catch (const Teuchos::Exceptions::InvalidParameterValue& exp) {
226  }
227 
228  return Teuchos::null;
229  ;
230 }
231 
240 Teuchos::RCP<const Teuchos::ParameterList> invFactoryValidParameters() {
241  Stratimikos::DefaultLinearSolverBuilder strat;
242 
243  // extract valid parameter list from Stratimikos
244  return strat.getValidParameters();
245 }
246 
247 } // 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)