Teko Version of the Day
Loading...
Searching...
No Matches
Teko_PreconditionerFactory.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#include "Teko_Config.h"
11#include "Teko_PreconditionerFactory.hpp"
12
13#include "Teko_InverseLibrary.hpp"
14#include "Teko_Preconditioner.hpp"
15
16// Specific preconditioners included for dynamic creation
17#include "Teko_JacobiPreconditionerFactory.hpp"
18#include "Teko_GaussSeidelPreconditionerFactory.hpp"
19#include "Teko_HierarchicalGaussSeidelPreconditionerFactory.hpp"
20#include "Teko_AddPreconditionerFactory.hpp"
21#include "Teko_MultPreconditionerFactory.hpp"
22#include "Teko_LU2x2PreconditionerFactory.hpp"
23#include "Teko_IterativePreconditionerFactory.hpp"
24#include "Teko_DiagnosticPreconditionerFactory.hpp"
25#include "Teko_DiagonallyScaledPreconditionerFactory.hpp"
26#include "Teko_DiagonalPreconditionerFactory.hpp"
27#include "Teko_AdaptivePreconditionerFactory.hpp"
28#include "Teko_ProbingPreconditionerFactory.hpp"
29#include "Teko_IdentityPreconditionerFactory.hpp"
30#include "NS/Teko_LSCPreconditionerFactory.hpp"
31#include "NS/Teko_SIMPLEPreconditionerFactory.hpp"
32#include "NS/Teko_TimingsSIMPLEPreconditionerFactory.hpp"
33
34#include "Thyra_DefaultPreconditioner.hpp"
35
36using namespace Thyra;
37using Teuchos::RCP;
38
39namespace Teko {
41
43bool PreconditionerFactory::isCompatible(const Thyra::LinearOpSourceBase<double> &fwdOpSrc) const {
44 RCP<const Thyra::LinearOpBase<double> > A = fwdOpSrc.getOp();
45 return A != Teuchos::null;
46}
47
49RCP<Thyra::PreconditionerBase<double> > PreconditionerFactory::createPrec() const {
50 // build a preconditioner, give it some inital state
51 RCP<Preconditioner> bp = rcp(new Preconditioner());
52 bp->setStateObject(buildPreconditionerState());
53 bp->getStateObject()->setInitialized(false);
54
55 return bp;
56}
57
59void PreconditionerFactory::initializePrec(const RCP<const LinearOpSourceBase<double> > &ASrc,
60 PreconditionerBase<double> *prec,
61 const ESupportSolveUse /* supportSolveUse */) const {
62 // get the blocked linear operator
63 LinearOp A = Teuchos::rcp_const_cast<Thyra::LinearOpBase<double> >(ASrc->getOp());
64
65 Preconditioner *blkPrec = dynamic_cast<Preconditioner *>(prec);
66 TEUCHOS_ASSERT(blkPrec != 0);
67
68 // grab the state object
69 RCP<PreconditionerState> state = blkPrec->getStateObject();
70 state->setInitialized(false);
71
72 // build the preconditioner
73 const RCP<const LinearOpBase<double> > M = buildPreconditionerOperator(A, *state);
74
75 // set the request handler for the
76 setOpRequestHandler(*this, M);
77
78 // must first cast that to be initialized
79 DefaultPreconditioner<double> &dPrec = Teuchos::dyn_cast<DefaultPreconditioner<double> >(*prec);
80 dPrec.initializeUnspecified(Teuchos::rcp_const_cast<LinearOpBase<double> >(M));
81}
82
85 const RCP<const LinearOpSourceBase<double> > &ASrc,
86 const RCP<const Thyra::MultiVectorBase<double> > &solnVec, PreconditionerBase<double> *prec,
87 const ESupportSolveUse supportSolveUse) const {
88 Preconditioner *blkPrec = dynamic_cast<Preconditioner *>(prec);
89 blkPrec->setSourceVector(Teuchos::rcp_const_cast<Thyra::MultiVectorBase<double> >(solnVec));
90
91 initializePrec(ASrc, prec, supportSolveUse);
92}
93
96 PreconditionerBase<double> * /* prec */,
97 RCP<const LinearOpSourceBase<double> > * /* fwdOpSrc */,
98 ESupportSolveUse * /* supportSolveUse */) const {
99 // Preconditioner * blkPrec = dynamic_cast<Preconditioner *>(prec);
100
101 // what do I do here?
102 TEUCHOS_TEST_FOR_EXCEPT_MSG(true, "\"PreconditionerFactory::uninitializePrec not implemented\"");
103}
104
105// for ParameterListAcceptor
107
109void PreconditionerFactory::setParameterList(const RCP<Teuchos::ParameterList> &paramList) {
110 paramList_ = paramList;
111}
112
114RCP<Teuchos::ParameterList> PreconditionerFactory::getNonconstParameterList() { return paramList_; }
115
117RCP<Teuchos::ParameterList> PreconditionerFactory::unsetParameterList() {
118 RCP<Teuchos::ParameterList> _paramList = paramList_;
119 paramList_ = Teuchos::null;
120 return _paramList;
121}
122
124void PreconditionerFactory::setInverseLibrary(const RCP<const InverseLibrary> &il) {
125 inverseLibrary_ = il;
126}
127
129RCP<const InverseLibrary> PreconditionerFactory::getInverseLibrary() const {
130 // lazily build the inverse library only when needed
131 if (inverseLibrary_ == Teuchos::null) return InverseLibrary::buildFromStratimikos();
132
133 return inverseLibrary_;
134}
135
137// Static members and methods
139
141void PreconditionerFactory::setOpRequestHandler(const RequestHandlerContainer &rhc,
142 const LinearOp &op) {
143 ModifiableLinearOp mlo = Teuchos::rcp_const_cast<Thyra::LinearOpBase<double> >(op);
144
145 // conditionally set the request handler
146 RCP<RequestHandlerContainer> reqHandCont =
147 Teuchos::rcp_dynamic_cast<RequestHandlerContainer>(mlo);
148 if (reqHandCont != Teuchos::null) {
149 reqHandCont->setRequestHandler(rhc.getRequestHandler());
150 } else {
151 // is null
152 }
153}
154
156CloneFactory<PreconditionerFactory> PreconditionerFactory::precFactoryBuilder_;
157
173 const std::string &name, const Teuchos::ParameterList &settings,
174 const RCP<const InverseLibrary> &invLib) {
175 Teko_DEBUG_SCOPE("PreconditionerFactory::buildPreconditionerFactory", 10);
176
177 // initialize the defaults if necessary
178 if (precFactoryBuilder_.cloneCount() == 0) initializePrecFactoryBuilder();
179
180 // request the preconditioner factory from the CloneFactory
181 RCP<PreconditionerFactory> precFact = precFactoryBuilder_.build(name);
182
183 Teko_DEBUG_MSG_BEGIN(5);
184 DEBUG_STREAM << "Looked up \"" << name << "\"" << std::endl;
185 DEBUG_STREAM << "Built " << precFact << std::endl;
186 Teko_DEBUG_MSG_END();
187
188 if (precFact == Teuchos::null) return Teuchos::null;
189
190 // add in the inverse library
191 if (invLib != Teuchos::null) {
192 precFact->setInverseLibrary(invLib);
193 precFact->setRequestHandler(invLib->getRequestHandler());
194 }
195
196 // now that inverse library has been set,
197 // pass in the parameter list
198 precFact->initializeFromParameterList(settings);
199
200 return precFact;
201}
202
217 const RCP<Cloneable> &clone) {
218 // initialize the defaults if necessary
219 if (precFactoryBuilder_.cloneCount() == 0) initializePrecFactoryBuilder();
220
221 // add clone to builder
222 precFactoryBuilder_.addClone(name, clone);
223}
224
226void PreconditionerFactory::initializePrecFactoryBuilder() {
227 RCP<Cloneable> clone;
228
229 // add various preconditioners to factory
230 clone = rcp(new AutoClone<LU2x2PreconditionerFactory>());
231 precFactoryBuilder_.addClone("Block LU2x2", clone);
232
234 precFactoryBuilder_.addClone("Block Jacobi", clone);
235
237 precFactoryBuilder_.addClone("Block Gauss-Seidel", clone);
238
240 precFactoryBuilder_.addClone("Hierarchical Block Gauss-Seidel", clone);
241
242 clone = rcp(new AutoClone<AddPreconditionerFactory>());
243 precFactoryBuilder_.addClone("Block Add", clone);
244
245 clone = rcp(new AutoClone<MultPreconditionerFactory>());
246 precFactoryBuilder_.addClone("Block Multiply", clone);
247
249 precFactoryBuilder_.addClone("NS LSC", clone);
250
252 precFactoryBuilder_.addClone("NS SIMPLE", clone);
253
255 precFactoryBuilder_.addClone("NS SIMPLE-Timed", clone);
256
258 precFactoryBuilder_.addClone("Iterative Preconditioner", clone);
259
261 precFactoryBuilder_.addClone("Explicit Diagonal Preconditioner", clone);
262
264 precFactoryBuilder_.addClone("Diagnostic Inverse", clone);
265
267 precFactoryBuilder_.addClone("Diagonal Scaling", clone);
268
270 precFactoryBuilder_.addClone("Identity", clone);
271
273 precFactoryBuilder_.addClone("Adaptive", clone);
274
276 precFactoryBuilder_.addClone("Probing Preconditioner", clone);
277}
278
279void PreconditionerFactory::getPreconditionerFactoryNames(std::vector<std::string> &names) {
280 // initialize the defaults if necessary
281 if (precFactoryBuilder_.cloneCount() == 0) initializePrecFactoryBuilder();
282 precFactoryBuilder_.getCloneNames(names);
283}
284
285} // end namespace Teko
void initializePrec(const Teuchos::RCP< const Thyra::LinearOpSourceBase< double > > &fwdOpSrc, const Teuchos::RCP< const Thyra::MultiVectorBase< double > > &solnVec, Thyra::PreconditionerBase< double > *precOp, const Thyra::ESupportSolveUse supportSolveUse) const
initialize a newly created preconditioner object
Teuchos::RCP< Teuchos::ParameterList > getNonconstParameterList()
Get the parameter list that was set using setParameterList().
void setInverseLibrary(const Teuchos::RCP< const InverseLibrary > &il)
Set the inverse library used by this preconditioner factory.
static Teuchos::RCP< PreconditionerFactory > buildPreconditionerFactory(const std::string &name, const Teuchos::ParameterList &settings, const Teuchos::RCP< const InverseLibrary > &invLib=Teuchos::null)
Builder function for creating preconditioner factories (yes this is a factory factory).
Teuchos::RCP< Thyra::PreconditionerBase< double > > createPrec() const
create an instance of the preconditioner
void setParameterList(const Teuchos::RCP< Teuchos::ParameterList > &paramList)
Set parameters from a parameter list and return with default values.
Teuchos::RCP< Teuchos::ParameterList > paramList_
for ParameterListAcceptor
static void addPreconditionerFactory(const std::string &name, const Teuchos::RCP< Cloneable > &clone)
Add a preconditioner factory to the builder. This is done using the clone pattern.
virtual LinearOp buildPreconditionerOperator(LinearOp &lo, PreconditionerState &state) const =0
Function that is called to build the preconditioner for the linear operator that is passed in.
void uninitializePrec(Thyra::PreconditionerBase< double > *prec, Teuchos::RCP< const Thyra::LinearOpSourceBase< double > > *fwdOpSrc, Thyra::ESupportSolveUse *supportSolveUse) const
wipe clean a already initialized preconditioner object
bool isCompatible(const Thyra::LinearOpSourceBase< double > &fwdOpSrc) const
is this operator compatiable with the preconditioner factory?
static void getPreconditionerFactoryNames(std::vector< std::string > &names)
Get the names of the block preconditioner factories.
virtual Teuchos::RCP< PreconditionerState > buildPreconditionerState() const
Function that permits the construction of an arbitrary PreconditionerState object.
Teuchos::RCP< const InverseLibrary > getInverseLibrary() const
Get the inverse library used by this preconditioner factory.
Teuchos::RCP< Teuchos::ParameterList > unsetParameterList()
Unset the parameter list that was set using setParameterList().
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.