Teko  Version of the Day
Teko_InverseLibrary.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_InverseLibrary.hpp"
48 
49 #include "Teko_SolveInverseFactory.hpp"
50 #include "Teko_PreconditionerInverseFactory.hpp"
51 #include "Teko_BlockPreconditionerFactory.hpp"
52 
53 #include "Teko_NeumannSeriesPreconditionerFactory.hpp"
54 #include "Teuchos_AbstractFactoryStd.hpp"
55 
56 #include <algorithm>
57 
58 using Teuchos::RCP;
59 using Teuchos::rcp;
60 
61 namespace Teko {
62 
66 void addToStratimikosBuilder(Stratimikos::DefaultLinearSolverBuilder & builder)
67 {
68  typedef Thyra::PreconditionerFactoryBase<double> PrecFactory;
69 
70  RCP<const Teuchos::AbstractFactory<Thyra::PreconditionerFactoryBase<double> > > factory;
71 
72  factory = Teuchos::abstractFactoryStd<PrecFactory,Teko::NeumannSeriesPreconditionerFactory<double> >();
73  builder.setPreconditioningStrategyFactory(factory,"Neumann Series");
74 }
75 
76 InverseLibrary::InverseLibrary()
77 {
78  Teko_DEBUG_SCOPE("InverseLibrary::InverseLibrary", 10);
79 
80  // setup some valid Stratimikos parameters
82 
83  // set valid solve factory names
84  stratValidSolver_.push_back("Belos");
85  stratValidSolver_.push_back("Amesos");
86  stratValidSolver_.push_back("AztecOO");
87 
88  // set valid preconditioner factory name
89  stratValidPrecond_.push_back("ML");
90  stratValidPrecond_.push_back("Ifpack");
91  stratValidPrecond_.push_back("Neumann Series");
92 
93  // set valid Teko preconditioner factory names
95 
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;
101  Teko_DEBUG_MSG_END()
102 }
103 
105 void InverseLibrary::addInverse(const std::string & label,const Teuchos::ParameterList & pl)
106 {
107  // strip out the label
108  const std::string type = pl.get<std::string>("Type");
109 
110  // copy the parameter list so we can modify it
111  Teuchos::ParameterList settingsList;
112  settingsList.set(type,pl);
113  settingsList.sublist(type).remove("Type");
114 
115  // is this a Stratimikos preconditioner or solver
116  if(std::find(stratValidPrecond_.begin(),stratValidPrecond_.end(),type)!=stratValidPrecond_.end()) {
117  // this is a Stratimikos preconditioner factory
118  addStratPrecond(label,type,settingsList);
119  }
120  else if(std::find(stratValidSolver_.begin(),stratValidSolver_.end(),type)!=stratValidSolver_.end()) {
121  // this is a Stratimikos preconditioner factory
122  addStratSolver(label,type,settingsList);
123  }
124  else if(std::find(blockValidPrecond_.begin(),blockValidPrecond_.end(),type)!=blockValidPrecond_.end()) {
125  // this is a Teko preconditioner factory
126  addBlockPrecond(label,type,settingsList);
127  }
128  else {
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);
133  }
134 }
135 
137 void InverseLibrary::addStratSolver(const std::string & label,const std::string & type,const Teuchos::ParameterList & pl)
138 {
139  // add some additional parameters onto the list
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");
144 
145  stratSolver_[label] = stratList;
146 }
147 
149 void InverseLibrary::addStratPrecond(const std::string & label,const std::string & type,const Teuchos::ParameterList & pl)
150 {
151  // add some additional parameters onto the list
152  RCP<Teuchos::ParameterList> stratList = rcp(new Teuchos::ParameterList());
153  stratList->set("Preconditioner Type",type);
154  stratList->set("Preconditioner Types",pl);
155 
156  stratPrecond_[label] = stratList;
157 }
158 
160 void InverseLibrary::addBlockPrecond(const std::string & label,const std::string & type,const Teuchos::ParameterList & pl)
161 {
162  // add some additional parameters onto the list
163  RCP<Teuchos::ParameterList> blockList = rcp(new Teuchos::ParameterList());
164  blockList->set("Preconditioner Type",type);
165  blockList->set("Preconditioner Settings",pl.sublist(type));
166 
167  // add the Teko preconditioner parameter list into the library
168  blockPrecond_[label] = blockList;
169 }
170 
178 Teuchos::RCP<const Teuchos::ParameterList> InverseLibrary::getParameterList(const std::string & label) const
179 {
180  std::map<std::string,RCP<const Teuchos::ParameterList> >::const_iterator itr;
181 
182  // check preconditioners
183  itr = stratPrecond_.find(label);
184  if(itr!=stratPrecond_.end()) return itr->second;
185 
186  // check solvers
187  itr = stratSolver_.find(label);
188  if(itr!=stratSolver_.end()) return itr->second;
189 
190  // check solvers
191  itr = blockPrecond_.find(label);
192  if(itr!=blockPrecond_.end()) return itr->second;
193 
194  return Teuchos::null;
195 }
196 
198 Teuchos::RCP<InverseFactory> InverseLibrary::getInverseFactory(const std::string & label) const
199 {
200  Teko_DEBUG_SCOPE("InverseLibrary::getInverseFactory",10);
201 
202  std::map<std::string,RCP<const Teuchos::ParameterList> >::const_iterator itr;
203 
204  bool isStratSolver=false,isStratPrecond=false,isBlockPrecond=false;
205 
206  // is this a Stratimikos solver?
207  itr = stratPrecond_.find(label);
208  isStratPrecond = itr!=stratPrecond_.end();
209 
210  // is this a Stratimikos preconditioner?
211  if(not isStratPrecond) {
212  itr = stratSolver_.find(label);
213  isStratSolver = itr!=stratSolver_.end();
214  }
215 
216  // must be a "block" preconditioner
217  if(not (isStratSolver || isStratPrecond)) {
218  itr = blockPrecond_.find(label);
219  isBlockPrecond = itr!=blockPrecond_.end();
220  }
221 
222  Teko_DEBUG_MSG("Inverse \"" << label << "\" is of type "
223  << "strat prec = " << isStratPrecond << ", "
224  << "strat solv = " << isStratSolver << ", "
225  << "block prec = " << isBlockPrecond,3);
226 
227  // Must be one of Strat solver, strat preconditioner, block preconditioner
228  if(not (isStratSolver || isStratPrecond || isBlockPrecond)) {
229  RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
230 
231  *out << "InverseLibrary::getInverseFactory could not find \"" << label << "\" ... aborting\n";
232  *out << "Choose one of: " << std::endl;
233 
234  *out << " Stratimikos preconditioners = ";
235  for(itr=stratPrecond_.begin();itr!=stratPrecond_.end();++itr)
236  *out << " \"" << itr->first << "\"\n";
237  *out << std::endl;
238 
239  *out << " Stratimikos solvers = ";
240  for(itr=stratSolver_.begin();itr!=stratSolver_.end();++itr)
241  *out << " \"" << itr->first << "\"\n";
242  *out << std::endl;
243 
244  *out << " Block preconditioners = ";
245  for(itr=blockPrecond_.begin();itr!=blockPrecond_.end();++itr)
246  *out << " \"" << itr->first << "\"\n";
247  *out << std::endl;
248 
249  TEUCHOS_ASSERT(isStratSolver || isStratPrecond || isBlockPrecond);
250  }
251 
252  RCP<const Teuchos::ParameterList> pl = itr->second;
253 
254  // build inverse factory
255  if(isStratPrecond) {
256  // remove required parameters
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");
264  }
265 
266  // print some debuggin info
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();
270 
271  if(xtraParams!=Teuchos::null) {
272  DEBUG_STREAM << "Printing extra parameters: " << std::endl;
273  Teko_DEBUG_PUSHTAB(); xtraParams->print(DEBUG_STREAM); Teko_DEBUG_POPTAB();
274  }
275  Teko_DEBUG_MSG_END();
276 
277  Stratimikos::DefaultLinearSolverBuilder strat;
278  addToStratimikosBuilder(strat);
279  strat.setParameterList(plCopy);
280 
281  // try to build a preconditioner factory
282  RCP<Thyra::PreconditionerFactoryBase<double> > precFact = strat.createPreconditioningStrategy(type);
283 
284  // string must map to a preconditioner
285  RCP<Teko::PreconditionerInverseFactory> precInvFact
286  = rcp(new PreconditionerInverseFactory(precFact,xtraParams,getRequestHandler()));
287  precInvFact->setupParameterListFromRequestHandler();
288  return precInvFact;
289  }
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");
293 
294  // get preconditioner name, remove "Use Preconditioner" parameter
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);
301  }
302 
303  // build Thyra preconditioner factory
304  RCP<Thyra::PreconditionerFactoryBase<double> > precFactory;
305  if(precName!="None") {
306  // we will manually set the preconditioner, so set this to null
307  solveList->set<std::string>("Preconditioner Type","None");
308 
309  // build inverse that preconditioner corresponds to
310  RCP<PreconditionerInverseFactory> precInvFactory
311  = Teuchos::rcp_dynamic_cast<PreconditionerInverseFactory>(getInverseFactory(precName));
312 
313  // extract preconditioner factory from preconditioner _inverse_ factory
314  precFactory = precInvFactory->getPrecFactory();
315  }
316 
317  Stratimikos::DefaultLinearSolverBuilder strat;
318  addToStratimikosBuilder(strat);
319  strat.setParameterList(solveList);
320 
321  // try to build a solver factory
322  RCP<Thyra::LinearOpWithSolveFactoryBase<double> > solveFact = strat.createLinearSolveStrategy(type);
323  if(precFactory!=Teuchos::null)
324  solveFact->setPreconditionerFactory(precFactory,precName);
325 
326  // if its around, build a InverseFactory
327  return rcp(new SolveInverseFactory(solveFact));
328  }
329  else if(isBlockPrecond) {
330  try {
331  std::string type = pl->get<std::string>("Preconditioner Type");
332  const Teuchos::ParameterList & settings = pl->sublist("Preconditioner Settings");
333 
334  // build preconditioner factory from the string
335  RCP<PreconditionerFactory> precFact
336  = PreconditionerFactory::buildPreconditionerFactory(type,settings,Teuchos::rcpFromRef(*this));
337 
338  TEUCHOS_ASSERT(precFact!=Teuchos::null);
339 
340  // return the inverse factory object
341  return rcp(new PreconditionerInverseFactory(precFact,getRequestHandler()));
342  }
343  catch(std::exception & e) {
344  RCP<Teuchos::FancyOStream> out = Teko::getOutputStream();
345 
346  *out << "Teko: \"getInverseFactory\" failed, Parameter List =\n";
347  pl->print(*out);
348 
349  *out << "*** THROWN EXCEPTION ***\n";
350  *out << e.what() << std::endl;
351  *out << "************************\n";
352 
353  throw e;
354  }
355  }
356 
357  TEUCHOS_ASSERT(false);
358 }
359 
361 void InverseLibrary::PrintAvailableInverses(std::ostream & os) const
362 {
363  std::map<std::string,Teuchos::RCP<const Teuchos::ParameterList> >::const_iterator itr;
364 
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);
370  os << std::endl;
371  }
372 
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);
378  os << std::endl;
379  }
380 
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);
386  os << std::endl;
387  }
388 }
389 
399 RCP<InverseLibrary> InverseLibrary::buildFromParameterList(const Teuchos::ParameterList & pl,bool useStratDefaults)
400 {
401  // build from Stratimikos or allocate a new inverse library
402  RCP<InverseLibrary> invLib;
403  if(useStratDefaults)
404  invLib = InverseLibrary::buildFromStratimikos();
405  else
406  invLib = rcp(new InverseLibrary());
407 
408  // to convert the void* like entry
409  Teuchos::ParameterList * temp = 0;
410 
411  // loop over all entries in parameter list
412  Teuchos::ParameterList::ConstIterator itr;
413  for(itr=pl.begin();itr!=pl.end();++itr) {
414  // get current entry
415  std::string label = itr->first;
416  Teuchos::ParameterList & list = itr->second.getValue(temp);
417 
418  // add to library
419  invLib->addInverse(label,list);
420  }
421 
422  return invLib;
423 }
424 
434 Teuchos::RCP<InverseLibrary> InverseLibrary::buildFromStratimikos(const Stratimikos::DefaultLinearSolverBuilder & strat)
435 {
436  RCP<InverseLibrary> invLib = rcp(new InverseLibrary());
437 
438  // get default inveres in Stratimikos
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"));
442 
443  Teuchos::ParameterList::ConstIterator itr;
444  Teuchos::ParameterList * temp = 0;
445 
446  // loop over all entries in solver list
447  for(itr=lst.begin();itr!=lst.end();++itr) {
448  // get current entry
449  std::string label = itr->first;
450  Teuchos::ParameterList & list = itr->second.getValue(temp);
451  list.set("Type",label);
452 
453  // add to library
454  invLib->addInverse(label,list);
455  }
456 
457  // loop over all entries in preconditioner list
458  for(itr=pft.begin();itr!=pft.end();++itr) {
459  // get current entry
460  std::string label = itr->first;
461  Teuchos::ParameterList & list = itr->second.getValue(temp);
462  list.set("Type",label);
463 
464  // add to library
465  invLib->addInverse(label,list);
466  }
467 
468  return invLib;
469 }
470 
471 } // end namespace Teko
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).