44#include "Stratimikos_InternalConfig.h"
45#include "Stratimikos_DefaultLinearSolverBuilder.hpp"
46#include "Thyra_DelayedLinearOpWithSolveFactory.hpp"
47#include "Teuchos_AbstractFactoryStd.hpp"
48#include "Teuchos_CommandLineProcessor.hpp"
49#include "Teuchos_XMLParameterListHelpers.hpp"
50#include "Teuchos_GlobalMPISession.hpp"
52#ifdef HAVE_STRATIMIKOS_AMESOS
53# include "Thyra_AmesosLinearOpWithSolveFactory.hpp"
55#ifdef HAVE_STRATIMIKOS_AMESOS2
56# include "Thyra_Amesos2LinearOpWithSolveFactory.hpp"
58#if defined(HAVE_STRATIMIKOS_EPETRAEXT) && defined(HAVE_STRATIMIKOS_AZTECOO)
59# include "Thyra_AztecOOLinearOpWithSolveFactory.hpp"
61#ifdef HAVE_STRATIMIKOS_BELOS
62# include "Thyra_BelosLinearOpWithSolveFactory.hpp"
64#ifdef HAVE_STRATIMIKOS_IFPACK
65# include "Thyra_IfpackPreconditionerFactory.hpp"
67#ifdef HAVE_STRATIMIKOS_ML
68# include "Thyra_MLPreconditionerFactory.hpp"
75const std::string LinearSolverType_name =
"Linear Solver Type";
76const std::string LinearSolverTypes_name =
"Linear Solver Types";
77const std::string PreconditionerType_name =
"Preconditioner Type";
78const std::string PreconditionerTypes_name =
"Preconditioner Types";
79const std::string None_name =
"None";
80const std::string EnableDelayedSolverConstruction_name =
"Enable Delayed Solver Construction";
81const bool EnableDelayedSolverConstruction_default =
false;
87namespace Stratimikos {
94 const std::string ¶msXmlFileName_in
95 ,
const std::string &extraParamsXmlString_in
96 ,
const std::string ¶msUsedXmlOutFileName_in
97 ,
const std::string ¶msXmlFileNameOption_in
98 ,
const std::string &extraParamsXmlStringOption_in
99 ,
const std::string ¶msUsedXmlOutFileNameOption_in
101 :paramsXmlFileName_(paramsXmlFileName_in)
102 ,extraParamsXmlString_(extraParamsXmlString_in)
103 ,paramsUsedXmlOutFileName_(paramsUsedXmlOutFileName_in)
104 ,paramsXmlFileNameOption_(paramsXmlFileNameOption_in)
105 ,extraParamsXmlStringOption_(extraParamsXmlStringOption_in)
106 ,paramsUsedXmlOutFileNameOption_(paramsUsedXmlOutFileNameOption_in)
107 ,enableDelayedSolverConstruction_(EnableDelayedSolverConstruction_default)
109 this->initializeDefaults();
117 if (nonnull(paramList_)) {
125 const RCP<
const AbstractFactory<Thyra::LinearOpWithSolveFactoryBase<double> > >
126 &solveStrategyFactory,
127 const std::string &solveStrategyName,
128 const bool makeDefault
131 validLowsfNames_.push_back(solveStrategyName);
132 lowsfArray_.push_back(solveStrategyFactory);
133 validParamList_ = Teuchos::null;
141 const std::string &solveStrategyName)
143 defaultLOWSF_ = solveStrategyName;
148 const RCP<
const AbstractFactory<Thyra::PreconditionerFactoryBase<double> > >
149 &precStrategyFactory,
150 const std::string &precStrategyName,
151 const bool makeDefault
154 validPfNames_.push_back(precStrategyName);
155 pfArray_.push_back(precStrategyFactory);
156 validParamList_ = Teuchos::null;
164 const std::string &precStrategyName)
166 defaultPF_ = precStrategyName;
172 TEUCHOS_TEST_FOR_EXCEPT(clp==NULL);
174 paramsXmlFileNameOption().c_str(),¶msXmlFileName_
175 ,
"Name of an XML file containing parameters for linear solver "
176 "options to be appended first."
179 extraParamsXmlStringOption().c_str(),&extraParamsXmlString_
180 ,
"An XML string containing linear solver parameters to be appended second."
183 paramsUsedXmlOutFileNameOption().c_str(),¶msUsedXmlOutFileName_
184 ,
"Name of an XML file that can be written with the parameter list after it "
185 "has been used on completion of this program."
192 using Teuchos::parameterList;
194 using Teuchos::updateParametersFromXmlFile;
195 using Teuchos::updateParametersFromXmlString;
198 if (!paramList_.get()) {
199 paramList_ = parameterList(
"DefaultLinearSolverBuilder");
201 if (paramsXmlFileName().length()) {
203 *out << endl <<
"Reading parameters from XML file \""
204 << paramsXmlFileName() <<
"\" ..." << endl;
206 updateParametersFromXmlFile (paramsXmlFileName (), paramList_.ptr());
208 if (extraParamsXmlString().length()) {
210 *out << endl <<
"Appending extra parameters from the XML string \""
211 << extraParamsXmlString() <<
"\" ..." << endl;
213 updateParametersFromXmlString (extraParamsXmlString (), paramList_.ptr());
220 const Thyra::LinearOpWithSolveFactoryBase<double> &,
221 const std::string &outputXmlFileName
224 justInTimeInitialize();
225 const std::string xmlOutputFile =
226 ( outputXmlFileName.length() ? outputXmlFileName : paramsUsedXmlOutFileName() );
227 if (xmlOutputFile.length()) {
228 Teuchos::writeParameterListToXmlFile(*paramList_, xmlOutputFile);
236 justInTimeInitialize();
237 return lowsfValidator_->getStringValue(*paramList_, LinearSolverType_name,
245 justInTimeInitialize();
246 return pfValidator_->getStringValue(*paramList_, PreconditionerType_name,
255 RCP<Teuchos::ParameterList>
const& paramList
258 TEUCHOS_TEST_FOR_EXCEPT(is_null(paramList));
260 paramList_ = paramList;
261 enableDelayedSolverConstruction_ = paramList_->get(
262 EnableDelayedSolverConstruction_name, EnableDelayedSolverConstruction_default );
266RCP<Teuchos::ParameterList>
273RCP<Teuchos::ParameterList>
276 RCP<Teuchos::ParameterList> _paramList = paramList_;
277 paramList_ = Teuchos::null;
282RCP<const Teuchos::ParameterList>
289RCP<const Teuchos::ParameterList>
292 using Teuchos::rcp_implicit_cast;
293 typedef Teuchos::ParameterEntryValidator PEV;
294 if (is_null(validParamList_)) {
295 RCP<Teuchos::ParameterList>
296 validParamList = Teuchos::rcp(
new Teuchos::ParameterList);
298 lowsfValidator_ = Teuchos::rcp(
299 new Teuchos::StringToIntegralParameterEntryValidator<int>(
300 validLowsfNames_,LinearSolverType_name
304 LinearSolverType_name, defaultLOWSF_,
305 (std::string(
"Determines the type of linear solver that will be used.\n")
306 +
"The parameters for each solver type are specified in the sublist \""
307 + LinearSolverTypes_name +
"\"").c_str(),
308 rcp_implicit_cast<const PEV>(lowsfValidator_)
310 Teuchos::ParameterList &linearSolverTypesSL = validParamList->sublist(
311 LinearSolverTypes_name,
false,
312 "Sublists for each of the linear solver types set using the parameter\n"
313 "\"" + LinearSolverType_name +
"\". Note that the options for each\n"
314 "linear solver type given below will only be used if linear solvers\n"
315 "of that type are created. It is fine to list parameter sublists for\n"
316 "linear solver types that are not used."
318 for(
int i = 0; i < static_cast<int>(lowsfArray_.size()); ++i ) {
320 &lsname = validLowsfNames_[i];
321 const RCP<Thyra::LinearOpWithSolveFactoryBase<double> >
322 lowsf = lowsfArray_[i]->create();
323 linearSolverTypesSL.sublist(lsname).setParameters(*lowsf->getValidParameters()
324 ).disableRecursiveValidation();
327 pfValidator_ = Teuchos::rcp(
328 new Teuchos::StringToIntegralParameterEntryValidator<int>(
329 validPfNames_, PreconditionerType_name ) );
331 PreconditionerType_name, defaultPF_,
332 (std::string(
"Determines the type of preconditioner that will be used.\n")
333 +
"This option is only meaningful for linear solvers that accept preconditioner"
334 +
" factory objects!\n"
335 +
"The parameters for each preconditioner are specified in the sublist \""
336 + PreconditionerTypes_name +
"\"").c_str(),
337 rcp_implicit_cast<const PEV>(pfValidator_)
339 Teuchos::ParameterList &precTypesSL = validParamList->sublist(
340 PreconditionerTypes_name,
false,
341 "Sublists for each of the preconditioner types set using the parameter\n"
342 "\"" + PreconditionerType_name +
"\". Note that the options for each\n"
343 "preconditioner type given below will only be used if preconditioners\n"
344 "of that type are created. It is fine to list parameter sublists for\n"
345 "preconditioner types that are not used."
347 for(
int i = 0; i < static_cast<int>(pfArray_.size()); ++i ) {
349 &pfname = validPfNames_[i+1];
350 const RCP<Thyra::PreconditionerFactoryBase<double> >
351 pf = pfArray_[i]->create();
352 precTypesSL.sublist(pfname).setParameters(*pf->getValidParameters()
353 ).disableRecursiveValidation();
357 EnableDelayedSolverConstruction_name, EnableDelayedSolverConstruction_default,
358 "When this option is set to true, the linear solver factory will be wrapped\n"
359 "in a delayed evaluation Decorator factory object. This results in a delay\n"
360 "in the creation of a linear solver (and the associated preconditioner) until\n"
361 "the first solve is actually performed. This helps in cases where it is not\n"
362 "known a-priori if a linear solve will be needed on a given linear operator and\n"
363 "therefore can significantly improve performance for some types of algorithms\n"
364 "such as NOX and LOCA."
367 validParamList_ = validParamList;
369 return validParamList_;
376RCP<Thyra::LinearOpWithSolveFactoryBase<double> >
378 const std::string &linearSolveStrategyName
381 justInTimeInitialize();
384#ifdef THYRA_DEFAULT_REAL_LINEAR_SOLVER_BUILDER_DUMP
385 std::cout <<
"\nEntering DefaultLinearSolverBuilder"
386 <<
"::createLinearSolveStrategy(...) ...\n";
387 std::cout <<
"\nlinearSolveStrategyName = \""
388 << linearSolveStrategyName <<
"\"\n";
389 std::cout <<
"\nlinearSolveStrategyName.length() = "
390 << linearSolveStrategyName.length() <<
"\n";
391 std::cout <<
"\ndefaultLOWSF_ = \"" << defaultLOWSF_ <<
"\"\n";
392 std::cout <<
"\nthis->getLinearSolveStrategyName() = \""
396 lsname = ( linearSolveStrategyName.length()
397 ? linearSolveStrategyName
399#ifdef THYRA_DEFAULT_REAL_LINEAR_SOLVER_BUILDER_DUMP
400 std::cout <<
"\nlsname = \"" << lsname <<
"\"\n";
405 ls_idx = lowsfValidator_->getIntegralValue(lsname, LinearSolverType_name);
408 RCP<Thyra::LinearOpWithSolveFactoryBase<double> >
409 lowsf = lowsfArray_[ls_idx]->create();
412 if(lowsf->acceptsPreconditionerFactory()) {
414 RCP<Thyra::PreconditionerFactoryBase<double> >
417 lowsf->setPreconditionerFactory(pf,pfName);
422 lowsf->setParameterList(
423 sublist(sublist(paramList_, LinearSolverTypes_name), lsname));
425 if (enableDelayedSolverConstruction_) {
427 new Thyra::DelayedLinearOpWithSolveFactory<double>(lowsf)
436RCP<Thyra::PreconditionerFactoryBase<double> >
438 const std::string &preconditioningStrategyName
441 justInTimeInitialize();
445 pfname = ( preconditioningStrategyName.length()
446 ? preconditioningStrategyName
448 RCP<Thyra::PreconditionerFactoryBase<double> >
453 pf_idx = pfValidator_->getIntegralValue(pfname, PreconditionerType_name);
455 pf = pfArray_[pf_idx-1]->create();
456 pf->setParameterList(
457 sublist(sublist(paramList_, PreconditionerTypes_name), pfname));
468void DefaultLinearSolverBuilder::initializeDefaults()
472 using Teuchos::abstractFactoryStd;
475 defaultPF_ = None_name;
476 validLowsfNames_.resize(0);
477 validPfNames_.resize(0);
478 validPfNames_.push_back(None_name);
484#ifdef HAVE_STRATIMIKOS_AMESOS2
486 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<double>,
487 Thyra::Amesos2LinearOpWithSolveFactory<double>>(),
492#ifdef HAVE_STRATIMIKOS_BELOS
494 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<double>,
500#ifdef HAVE_STRATIMIKOS_AMESOS
502 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<double>,
508#if defined(HAVE_STRATIMIKOS_EPETRAEXT) && defined(HAVE_STRATIMIKOS_AZTECOO)
510 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<double>,
519#ifdef HAVE_STRATIMIKOS_AMESOS
520 if (Teuchos::GlobalMPISession::getNProc() == 1) {
529#ifdef HAVE_STRATIMIKOS_ML
531 abstractFactoryStd<Thyra::PreconditionerFactoryBase<double>,
537#ifdef HAVE_STRATIMIKOS_IFPACK
539 abstractFactoryStd<Thyra::PreconditionerFactoryBase<double>,
550void DefaultLinearSolverBuilder::justInTimeInitialize()
const
552 paramList_.assert_not_null();
553 if (is_null(validParamList_)) {
void setupCLP(Teuchos::CommandLineProcessor *clp)
Setup the command-line processor to read in the needed data to extra the parameters from.
RCP< ParameterList > unsetParameterList()
void writeParamsFile(const Thyra::LinearOpWithSolveFactoryBase< double > &lowsFactory, const std::string &outputXmlFileName="") const
Write the parameters list for a LinearOpWithSolveFactoryBase object to a file after the parameters ar...
void setPreconditioningStrategyFactory(const RCP< const AbstractFactory< Thyra::PreconditionerFactoryBase< double > > > &precStrategyFactory, const std::string &precStrategyName, const bool makeDefault=false)
Set a new preconditioner strategy factory object.
RCP< Thyra::LinearOpWithSolveFactoryBase< double > > createLinearSolveStrategy(const std::string &linearSolveStrategyName) const
void setDefaultPreconditioningStrategyFactoryName(const std::string &precStrategyName)
Set the default linear solver factory name.
RCP< Thyra::PreconditionerFactoryBase< double > > createPreconditioningStrategy(const std::string &preconditioningStrategyName) const
RCP< ParameterList > getNonconstParameterList()
~DefaultLinearSolverBuilder()
DefaultLinearSolverBuilder(const std::string ¶msXmlFileName="", const std::string &extraParamsXmlString="", const std::string ¶msUsedXmlOutFileName="", const std::string ¶msXmlFileNameOption="linear-solver-params-file", const std::string &extraParamsXmlStringOption="extra-linear-solver-params", const std::string ¶msUsedXmlOutFileNameOption="linear-solver-params-used-file")
Construct with default parameters.
std::string getLinearSolveStrategyName() const
Get the name of the linear solver strategy that will be created on the next call to this->createLinea...
std::string getPreconditionerStrategyName() const
Get the name of the preconditioner strategy that will be created on the next call to this->createPrec...
void setDefaultLinearSolveStrategyFactoryName(const std::string &solveStrategyName)
Set the default linear solver factory name.
void readParameters(std::ostream *out)
Force the parameters to be read from a file and/or an extra XML string.
RCP< const ParameterList > getParameterList() const
void setLinearSolveStrategyFactory(const RCP< const AbstractFactory< Thyra::LinearOpWithSolveFactoryBase< double > > > &solveStrategyFactory, const std::string &solveStrategyName, const bool makeDefault=false)
Set a new linear solver strategy factory object.
void setParameterList(RCP< ParameterList > const ¶mList)
RCP< const ParameterList > getValidParameters() const
Concrete LinearOpWithSolveFactoryBase adapter subclass that uses Amesos direct solvers.
LinearOpWithSolveFactoryBase subclass implemented in terms of AztecOO.
LinearOpWithSolveFactoryBase subclass implemented in terms of Belos.
Concrete preconditioner factory subclass based on Ifpack.
Concrete preconditioner factory subclass based on ML.