Teko  Version of the Day
 All Classes Files Functions Variables Pages
Teko_PreconditionerFactory.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_Config.h"
48 #include "Teko_PreconditionerFactory.hpp"
49 
50 #include "Teko_InverseLibrary.hpp"
51 #include "Teko_Preconditioner.hpp"
52 
53 // Specific preconditioners included for dynamic creation
54 #include "Teko_JacobiPreconditionerFactory.hpp"
55 #include "Teko_GaussSeidelPreconditionerFactory.hpp"
56 #include "Teko_AddPreconditionerFactory.hpp"
57 #include "Teko_MultPreconditionerFactory.hpp"
58 #include "Teko_LU2x2PreconditionerFactory.hpp"
59 #include "Teko_IterativePreconditionerFactory.hpp"
60 #include "Teko_DiagnosticPreconditionerFactory.hpp"
61 #include "Teko_DiagonallyScaledPreconditionerFactory.hpp"
62 #include "Teko_DiagonalPreconditionerFactory.hpp"
63 #include "Teko_ProbingPreconditionerFactory.hpp"
64 #include "NS/Teko_LSCPreconditionerFactory.hpp"
65 #include "NS/Teko_SIMPLEPreconditionerFactory.hpp"
66 
67 #ifdef Teko_ENABLE_ML_SMOOTHERS
68 #include "Teko_SmootherPreconditionerFactory.hpp"
69 #include "Teko_MLPreconditionerFactory.hpp"
70 #endif
71 
72 
73 #include "Thyra_DefaultPreconditioner.hpp"
74 
75 using namespace Thyra;
76 using Teuchos::RCP;
77 
78 namespace Teko {
80 
82 bool PreconditionerFactory::isCompatible(const Thyra::LinearOpSourceBase<double> &fwdOpSrc) const
83 {
84  RCP<const Thyra::LinearOpBase<double> > A = fwdOpSrc.getOp();
85  return A!=Teuchos::null;
86 }
87 
89 RCP<Thyra::PreconditionerBase<double> > PreconditionerFactory::createPrec() const
90 {
91  // build a preconditioner, give it some inital state
92  RCP<Preconditioner> bp = rcp(new Preconditioner());
93  bp->setStateObject(buildPreconditionerState());
94  bp->getStateObject()->setInitialized(false);
95 
96  return bp;
97 }
98 
100 void PreconditionerFactory::initializePrec(const RCP<const LinearOpSourceBase<double> > & ASrc,
101  PreconditionerBase<double> * prec,
102  const ESupportSolveUse supportSolveUse) const
103 {
104  // get the blocked linear operator
105  LinearOp A = Teuchos::rcp_const_cast<Thyra::LinearOpBase<double> >(ASrc->getOp());
106 
107  Preconditioner * blkPrec = dynamic_cast<Preconditioner *>(prec);
108  TEUCHOS_ASSERT(blkPrec!=0);
109 
110  // grab the state object
111  RCP<PreconditionerState> state = blkPrec->getStateObject();
112  state->setInitialized(false);
113 
114  // build the preconditioner
115  const RCP<const LinearOpBase<double> > M = buildPreconditionerOperator(A,*state);
116 
117  // set the request handler for the
118  setOpRequestHandler(*this,M);
119 
120  // must first cast that to be initialized
121  DefaultPreconditioner<double> & dPrec = Teuchos::dyn_cast<DefaultPreconditioner<double> >(*prec);
122  dPrec.initializeUnspecified(Teuchos::rcp_const_cast<LinearOpBase<double> >(M));
123 }
124 
126 void PreconditionerFactory::initializePrec(const RCP<const LinearOpSourceBase<double> > & ASrc,
127  const RCP<const Thyra::MultiVectorBase<double> > & solnVec,
128  PreconditionerBase<double> * prec,
129  const ESupportSolveUse supportSolveUse) const
130 {
131  Preconditioner * blkPrec = dynamic_cast<Preconditioner *>(prec);
132  blkPrec->setSourceVector(Teuchos::rcp_const_cast<Thyra::MultiVectorBase<double> >(solnVec));
133 
134  initializePrec(ASrc,prec,supportSolveUse);
135 }
136 
138 void PreconditionerFactory::uninitializePrec(PreconditionerBase<double> * prec,
139  RCP<const LinearOpSourceBase<double> > * fwdOpSrc,
140  ESupportSolveUse *supportSolveUse) const
141 {
142  // Preconditioner * blkPrec = dynamic_cast<Preconditioner *>(prec);
143 
144  // what do I do here?
145  TEUCHOS_TEST_FOR_EXCEPT_MSG(true,"\"PreconditionerFactory::uninitializePrec not implemented\"");
146 }
147 
148 // for ParameterListAcceptor
150 
152 void PreconditionerFactory::setParameterList(const RCP<Teuchos::ParameterList> & paramList)
153 {
154  paramList_ = paramList;
155 }
156 
158 RCP< Teuchos::ParameterList > PreconditionerFactory::getNonconstParameterList()
159 {
160  return paramList_;
161 }
162 
164 RCP< Teuchos::ParameterList > PreconditionerFactory::unsetParameterList()
165 {
166  RCP<Teuchos::ParameterList> _paramList = paramList_;
167  paramList_ = Teuchos::null;
168  return _paramList;
169 }
170 
172 void PreconditionerFactory::setInverseLibrary(const RCP<const InverseLibrary> & il)
173 {
174  inverseLibrary_ = il;
175 }
176 
178 RCP<const InverseLibrary> PreconditionerFactory::getInverseLibrary() const
179 {
180  // lazily build the inverse library only when needed
181  if(inverseLibrary_==Teuchos::null)
182  return InverseLibrary::buildFromStratimikos();
183 
184  return inverseLibrary_;
185 }
186 
188 // Static members and methods
190 
192 void PreconditionerFactory::setOpRequestHandler(const RequestHandlerContainer & rhc,const LinearOp & op)
193 {
194  ModifiableLinearOp mlo = Teuchos::rcp_const_cast<Thyra::LinearOpBase<double> >(op);
195 
196  // conditionally set the request handler
197  RCP<RequestHandlerContainer> reqHandCont = Teuchos::rcp_dynamic_cast<RequestHandlerContainer>(mlo);
198  if(reqHandCont!=Teuchos::null) {
199  reqHandCont->setRequestHandler(rhc.getRequestHandler());
200  }
201  else {
202  // is null
203  }
204 
205 }
206 
208 CloneFactory<PreconditionerFactory> PreconditionerFactory::precFactoryBuilder_;
209 
224 RCP<PreconditionerFactory>
225 PreconditionerFactory::buildPreconditionerFactory(const std::string & name,
226  const Teuchos::ParameterList & settings,
227  const RCP<const InverseLibrary> & invLib)
228 {
229  Teko_DEBUG_SCOPE("PreconditionerFactory::buildPreconditionerFactory",10);
230 
231  // initialize the defaults if necessary
232  if(precFactoryBuilder_.cloneCount()==0) initializePrecFactoryBuilder();
233 
234  // request the preconditioner factory from the CloneFactory
235  RCP<PreconditionerFactory> precFact = precFactoryBuilder_.build(name);
236 
237  Teko_DEBUG_MSG_BEGIN(5);
238  DEBUG_STREAM << "Looked up \"" << name << "\"" << std::endl;
239  DEBUG_STREAM << "Built " << precFact << std::endl;
240  Teko_DEBUG_MSG_END();
241 
242  if(precFact==Teuchos::null)
243  return Teuchos::null;
244 
245  // add in the inverse library
246  if(invLib!=Teuchos::null) {
247  precFact->setInverseLibrary(invLib);
248  precFact->setRequestHandler(invLib->getRequestHandler());
249  }
250 
251  // now that inverse library has been set,
252  // pass in the parameter list
253  precFact->initializeFromParameterList(settings);
254 
255  return precFact;
256 }
257 
271 void PreconditionerFactory::addPreconditionerFactory(const std::string & name,const RCP<Cloneable> & clone)
272 {
273  // initialize the defaults if necessary
274  if(precFactoryBuilder_.cloneCount()==0) initializePrecFactoryBuilder();
275 
276  // add clone to builder
277  precFactoryBuilder_.addClone(name,clone);
278 }
279 
281 void PreconditionerFactory::initializePrecFactoryBuilder()
282 {
283  RCP<Cloneable> clone;
284 
285  // add various preconditioners to factory
286  clone = rcp(new AutoClone<LU2x2PreconditionerFactory>());
287  precFactoryBuilder_.addClone("Block LU2x2",clone);
288 
289  clone = rcp(new AutoClone<JacobiPreconditionerFactory>());
290  precFactoryBuilder_.addClone("Block Jacobi",clone);
291 
293  precFactoryBuilder_.addClone("Block Gauss-Seidel",clone);
294 
295  clone = rcp(new AutoClone<AddPreconditionerFactory>());
296  precFactoryBuilder_.addClone("Block Add",clone);
297 
298  clone = rcp(new AutoClone<MultPreconditionerFactory>());
299  precFactoryBuilder_.addClone("Block Multiply",clone);
300 
301  clone = rcp(new AutoClone<NS::LSCPreconditionerFactory>());
302  precFactoryBuilder_.addClone("NS LSC",clone);
303 
305  precFactoryBuilder_.addClone("NS SIMPLE",clone);
306 
308  precFactoryBuilder_.addClone("Iterative Preconditioner",clone);
309 
310  clone = rcp(new AutoClone<DiagonalPreconditionerFactory>());
311  precFactoryBuilder_.addClone("Explicit Diagonal Preconditioner",clone);
312 
314  precFactoryBuilder_.addClone("Diagnostic Inverse",clone);
315 
317  precFactoryBuilder_.addClone("Diagonal Scaling",clone);
318 
319 #ifdef Teko_ENABLE_Isorropia
320  clone = rcp(new AutoClone<ProbingPreconditionerFactory>());
321  precFactoryBuilder_.addClone("Probing Preconditioner",clone);
322 #endif
323 
324 #ifdef Teko_ENABLE_ML_SMOOTHERS
325  clone = rcp(new AutoClone<MLPreconditionerFactory>());
326  precFactoryBuilder_.addClone("Blocked ML Preconditioner",clone);
327 #endif
328 }
329 
330 void PreconditionerFactory::getPreconditionerFactoryNames(std::vector<std::string> & names)
331 {
332  // initialize the defaults if necessary
333  if(precFactoryBuilder_.cloneCount()==0) initializePrecFactoryBuilder();
334  precFactoryBuilder_.getCloneNames(names);
335 }
336 
337 } // end namespace Teko
virtual void setRequestHandler(const Teuchos::RCP< RequestHandler > &rh)=0
Set the request handler with pointers to the appropriate callbacks.
An extension of the Thyra::DefaultPreconditioner class with some specializations useful for use withi...
virtual Teuchos::RCP< RequestHandler > getRequestHandler() const =0
Get the request handler with pointers to the appropriate callbacks.