47 #include "Teko_InverseLibrary.hpp"
49 #include "Teko_SolveInverseFactory.hpp"
50 #include "Teko_PreconditionerInverseFactory.hpp"
51 #include "Teko_BlockPreconditionerFactory.hpp"
53 #include "Teko_NeumannSeriesPreconditionerFactory.hpp"
54 #include "Teuchos_AbstractFactoryStd.hpp"
66 void addToStratimikosBuilder(Stratimikos::DefaultLinearSolverBuilder & builder)
68 typedef Thyra::PreconditionerFactoryBase<double> PrecFactory;
70 RCP<const Teuchos::AbstractFactory<Thyra::PreconditionerFactoryBase<double> > > factory;
72 factory = Teuchos::abstractFactoryStd<PrecFactory,Teko::NeumannSeriesPreconditionerFactory<double> >();
73 builder.setPreconditioningStrategyFactory(factory,
"Neumann Series");
76 InverseLibrary::InverseLibrary()
78 Teko_DEBUG_SCOPE(
"InverseLibrary::InverseLibrary", 10);
84 stratValidSolver_.push_back(
"Belos");
85 stratValidSolver_.push_back(
"Amesos");
86 stratValidSolver_.push_back(
"AztecOO");
89 stratValidPrecond_.push_back(
"ML");
90 stratValidPrecond_.push_back(
"Ifpack");
91 stratValidPrecond_.push_back(
"Neumann Series");
96 Teko_DEBUG_MSG_BEGIN(10)
97 DEBUG_STREAM << "Loaded \"block\" preconditioners = ";
98 for(std::
size_t i=0;i<blockValidPrecond_.size();i++)
99 DEBUG_STREAM << blockValidPrecond_[i] << ", ";
100 DEBUG_STREAM << std::endl;
105 void InverseLibrary::addInverse(const std::
string & label,const Teuchos::ParameterList & pl)
108 const std::string type = pl.get<std::string>(
"Type");
111 Teuchos::ParameterList settingsList;
112 settingsList.set(type,pl);
113 settingsList.sublist(type).remove(
"Type");
116 if(std::find(stratValidPrecond_.begin(),stratValidPrecond_.end(),type)!=stratValidPrecond_.end()) {
118 addStratPrecond(label,type,settingsList);
120 else if(std::find(stratValidSolver_.begin(),stratValidSolver_.end(),type)!=stratValidSolver_.end()) {
122 addStratSolver(label,type,settingsList);
124 else if(std::find(blockValidPrecond_.begin(),blockValidPrecond_.end(),type)!=blockValidPrecond_.end()) {
126 addBlockPrecond(label,type,settingsList);
129 Teuchos::FancyOStream & os = *Teko::getOutputStream();
130 os <<
"ERROR: Could not find inverse type \"" << type
131 <<
"\" required by inverse name \"" << label <<
"\"" << std::endl;
132 TEUCHOS_ASSERT(
false);
137 void InverseLibrary::addStratSolver(
const std::string & label,
const std::string & type,
const Teuchos::ParameterList & pl)
140 RCP<Teuchos::ParameterList> stratList = rcp(
new Teuchos::ParameterList());
141 stratList->set(
"Linear Solver Type",type);
142 stratList->set(
"Linear Solver Types",pl);
143 stratList->set(
"Preconditioner Type",
"None");
145 stratSolver_[label] = stratList;
149 void InverseLibrary::addStratPrecond(
const std::string & label,
const std::string & type,
const Teuchos::ParameterList & pl)
152 RCP<Teuchos::ParameterList> stratList = rcp(
new Teuchos::ParameterList());
153 stratList->set(
"Preconditioner Type",type);
154 stratList->set(
"Preconditioner Types",pl);
156 stratPrecond_[label] = stratList;
160 void InverseLibrary::addBlockPrecond(
const std::string & label,
const std::string & type,
const Teuchos::ParameterList & pl)
163 RCP<Teuchos::ParameterList> blockList = rcp(
new Teuchos::ParameterList());
164 blockList->set(
"Preconditioner Type",type);
165 blockList->set(
"Preconditioner Settings",pl.sublist(type));
168 blockPrecond_[label] = blockList;
178 Teuchos::RCP<const Teuchos::ParameterList> InverseLibrary::getParameterList(
const std::string & label)
const
180 std::map<std::string,RCP<const Teuchos::ParameterList> >::const_iterator itr;
183 itr = stratPrecond_.find(label);
184 if(itr!=stratPrecond_.end())
return itr->second;
187 itr = stratSolver_.find(label);
188 if(itr!=stratSolver_.end())
return itr->second;
191 itr = blockPrecond_.find(label);
192 if(itr!=blockPrecond_.end())
return itr->second;
194 return Teuchos::null;
198 Teuchos::RCP<InverseFactory> InverseLibrary::getInverseFactory(
const std::string & label)
const
200 Teko_DEBUG_SCOPE(
"InverseLibrary::getInverseFactory",10);
202 std::map<std::string,RCP<const Teuchos::ParameterList> >::const_iterator itr;
204 bool isStratSolver=
false,isStratPrecond=
false,isBlockPrecond=
false;
207 itr = stratPrecond_.find(label);
208 isStratPrecond = itr!=stratPrecond_.end();
211 if(not isStratPrecond) {
212 itr = stratSolver_.find(label);
213 isStratSolver = itr!=stratSolver_.end();
217 if(not (isStratSolver || isStratPrecond)) {
218 itr = blockPrecond_.find(label);
219 isBlockPrecond = itr!=blockPrecond_.end();
222 Teko_DEBUG_MSG(
"Inverse \"" << label <<
"\" is of type "
223 <<
"strat prec = " << isStratPrecond <<
", "
224 <<
"strat solv = " << isStratSolver <<
", "
225 <<
"block prec = " << isBlockPrecond,3);
228 if(not (isStratSolver || isStratPrecond || isBlockPrecond)) {
229 RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
231 *out <<
"InverseLibrary::getInverseFactory could not find \"" << label <<
"\" ... aborting\n";
232 *out <<
"Choose one of: " << std::endl;
234 *out <<
" Stratimikos preconditioners = ";
235 for(itr=stratPrecond_.begin();itr!=stratPrecond_.end();++itr)
236 *out <<
" \"" << itr->first <<
"\"\n";
239 *out <<
" Stratimikos solvers = ";
240 for(itr=stratSolver_.begin();itr!=stratSolver_.end();++itr)
241 *out <<
" \"" << itr->first <<
"\"\n";
244 *out <<
" Block preconditioners = ";
245 for(itr=blockPrecond_.begin();itr!=blockPrecond_.end();++itr)
246 *out <<
" \"" << itr->first <<
"\"\n";
249 TEUCHOS_ASSERT(isStratSolver || isStratPrecond || isBlockPrecond);
252 RCP<const Teuchos::ParameterList> pl = itr->second;
257 RCP<Teuchos::ParameterList> plCopy = rcp(
new Teuchos::ParameterList(*pl));
258 std::string type = plCopy->get<std::string>(
"Preconditioner Type");
259 RCP<Teuchos::ParameterList> xtraParams;
260 if(plCopy->sublist(
"Preconditioner Types").sublist(type).isParameter(
"Required Parameters")) {
261 xtraParams = rcp(
new Teuchos::ParameterList(
262 plCopy->sublist(
"Preconditioner Types").sublist(type).sublist(
"Required Parameters")));
263 plCopy->sublist(
"Preconditioner Types").sublist(type).remove(
"Required Parameters");
267 Teko_DEBUG_MSG_BEGIN(10);
268 DEBUG_STREAM <<
"Printing parameter list: " << std::endl;
269 Teko_DEBUG_PUSHTAB(); plCopy->print(DEBUG_STREAM); Teko_DEBUG_POPTAB();
271 if(xtraParams!=Teuchos::null) {
272 DEBUG_STREAM <<
"Printing extra parameters: " << std::endl;
273 Teko_DEBUG_PUSHTAB(); xtraParams->print(DEBUG_STREAM); Teko_DEBUG_POPTAB();
275 Teko_DEBUG_MSG_END();
277 Stratimikos::DefaultLinearSolverBuilder strat;
278 addToStratimikosBuilder(strat);
279 strat.setParameterList(plCopy);
282 RCP<Thyra::PreconditionerFactoryBase<double> > precFact = strat.createPreconditioningStrategy(type);
285 RCP<Teko::PreconditionerInverseFactory> precInvFact
286 = rcp(
new PreconditionerInverseFactory(precFact,xtraParams,getRequestHandler()));
287 precInvFact->setupParameterListFromRequestHandler();
290 else if(isStratSolver) {
291 RCP<Teuchos::ParameterList> solveList = rcp(
new Teuchos::ParameterList(*pl));
292 std::string type = solveList->get<std::string>(
"Linear Solver Type");
295 Teuchos::ParameterList & solveSettings = solveList->sublist(
"Linear Solver Types").sublist(type);
296 std::string precKeyWord =
"Use Preconditioner";
297 std::string precName =
"None";
298 if(solveSettings.isParameter(precKeyWord)) {
299 precName = solveSettings.get<std::string>(precKeyWord);
300 solveSettings.remove(precKeyWord);
304 RCP<Thyra::PreconditionerFactoryBase<double> > precFactory;
305 if(precName!=
"None") {
307 solveList->set<std::string>(
"Preconditioner Type",
"None");
310 RCP<PreconditionerInverseFactory> precInvFactory
311 = Teuchos::rcp_dynamic_cast<PreconditionerInverseFactory>(getInverseFactory(precName));
314 precFactory = precInvFactory->getPrecFactory();
317 Stratimikos::DefaultLinearSolverBuilder strat;
318 addToStratimikosBuilder(strat);
319 strat.setParameterList(solveList);
322 RCP<Thyra::LinearOpWithSolveFactoryBase<double> > solveFact = strat.createLinearSolveStrategy(type);
323 if(precFactory!=Teuchos::null)
324 solveFact->setPreconditionerFactory(precFactory,precName);
327 return rcp(
new SolveInverseFactory(solveFact));
329 else if(isBlockPrecond) {
331 std::string type = pl->get<std::string>(
"Preconditioner Type");
332 const Teuchos::ParameterList & settings = pl->sublist(
"Preconditioner Settings");
335 RCP<PreconditionerFactory> precFact
338 TEUCHOS_ASSERT(precFact!=Teuchos::null);
341 return rcp(
new PreconditionerInverseFactory(precFact,getRequestHandler()));
343 catch(std::exception & e) {
344 RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
346 *out <<
"Teko: \"getInverseFactory\" failed, Parameter List =\n";
349 *out <<
"*** THROWN EXCEPTION ***\n";
350 *out << e.what() << std::endl;
351 *out <<
"************************\n";
357 TEUCHOS_ASSERT(
false);
361 void InverseLibrary::PrintAvailableInverses(std::ostream & os)
const
363 std::map<std::string,Teuchos::RCP<const Teuchos::ParameterList> >::const_iterator itr;
365 os <<
"Stratimikos Solvers: " << std::endl;
366 os <<
"********************************" << std::endl;
367 for(itr=stratSolver_.begin();itr!=stratSolver_.end();++itr) {
368 os <<
"name = \"" << itr->first <<
"\"" << std::endl;
369 itr->second->print(os);
373 os <<
"Stratimikos Preconditioners: " << std::endl;
374 os <<
"********************************" << std::endl;
375 for(itr=stratPrecond_.begin();itr!=stratPrecond_.end();++itr) {
376 os <<
"name = \"" << itr->first <<
"\"" << std::endl;
377 itr->second->print(os);
381 os <<
"Teko Preconditioners: " << std::endl;
382 os <<
"********************************" << std::endl;
383 for(itr=blockPrecond_.begin();itr!=blockPrecond_.end();++itr) {
384 os <<
"name = \"" << itr->first <<
"\"" << std::endl;
385 itr->second->print(os);
399 RCP<InverseLibrary> InverseLibrary::buildFromParameterList(
const Teuchos::ParameterList & pl,
bool useStratDefaults)
402 RCP<InverseLibrary> invLib;
404 invLib = InverseLibrary::buildFromStratimikos();
406 invLib = rcp(
new InverseLibrary());
409 Teuchos::ParameterList * temp = 0;
412 Teuchos::ParameterList::ConstIterator itr;
413 for(itr=pl.begin();itr!=pl.end();++itr) {
415 std::string label = itr->first;
416 Teuchos::ParameterList & list = itr->second.getValue(temp);
419 invLib->addInverse(label,list);
434 Teuchos::RCP<InverseLibrary> InverseLibrary::buildFromStratimikos(
const Stratimikos::DefaultLinearSolverBuilder & strat)
436 RCP<InverseLibrary> invLib = rcp(
new InverseLibrary());
439 RCP<Teuchos::ParameterList> pl = rcp(
new Teuchos::ParameterList(*strat.getValidParameters()));
440 Teuchos::ParameterList lst(pl->sublist(
"Linear Solver Types"));
441 Teuchos::ParameterList pft(pl->sublist(
"Preconditioner Types"));
443 Teuchos::ParameterList::ConstIterator itr;
444 Teuchos::ParameterList * temp = 0;
447 for(itr=lst.begin();itr!=lst.end();++itr) {
449 std::string label = itr->first;
450 Teuchos::ParameterList & list = itr->second.getValue(temp);
451 list.set(
"Type",label);
454 invLib->addInverse(label,list);
458 for(itr=pft.begin();itr!=pft.end();++itr) {
460 std::string label = itr->first;
461 Teuchos::ParameterList & list = itr->second.getValue(temp);
462 list.set(
"Type",label);
465 invLib->addInverse(label,list);
static void getPreconditionerFactoryNames(std::vector< std::string > &names)
Get the names of the block preconditioner factories.
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).