42#ifndef STRATIMIKOS_LINEARSOLVERBUILDER_DEF_HPP
43#define STRATIMIKOS_LINEARSOLVERBUILDER_DEF_HPP
47#include "Stratimikos_InternalConfig.h"
48#include "Thyra_DelayedLinearOpWithSolveFactory.hpp"
49#include "Teuchos_AbstractFactoryStd.hpp"
50#include "Teuchos_CommandLineProcessor.hpp"
51#include "Teuchos_XMLParameterListHelpers.hpp"
52#include "Teuchos_GlobalMPISession.hpp"
54#ifdef HAVE_STRATIMIKOS_AMESOS
57#ifdef HAVE_STRATIMIKOS_AMESOS2
58# include "Thyra_Amesos2LinearOpWithSolveFactory.hpp"
60#if defined(HAVE_STRATIMIKOS_EPETRAEXT) && defined(HAVE_STRATIMIKOS_AZTECOO)
63#ifdef HAVE_STRATIMIKOS_BELOS
64# include "Thyra_BelosLinearOpWithSolveFactory.hpp"
66#ifdef HAVE_STRATIMIKOS_IFPACK
69#ifdef HAVE_STRATIMIKOS_ML
77const std::string LinearSolverType_name =
"Linear Solver Type";
78const std::string LinearSolverTypes_name =
"Linear Solver Types";
79const std::string PreconditionerType_name =
"Preconditioner Type";
80const std::string PreconditionerTypes_name =
"Preconditioner Types";
81const std::string None_name =
"None";
82const std::string EnableDelayedSolverConstruction_name =
"Enable Delayed Solver Construction";
83const bool EnableDelayedSolverConstruction_default =
false;
97 const std::string ¶msXmlFileName_in
98 ,
const std::string &extraParamsXmlString_in
99 ,
const std::string ¶msUsedXmlOutFileName_in
100 ,
const std::string ¶msXmlFileNameOption_in
101 ,
const std::string &extraParamsXmlStringOption_in
102 ,
const std::string ¶msUsedXmlOutFileNameOption_in
104 :paramsXmlFileName_(paramsXmlFileName_in)
105 ,extraParamsXmlString_(extraParamsXmlString_in)
106 ,paramsUsedXmlOutFileName_(paramsUsedXmlOutFileName_in)
107 ,paramsXmlFileNameOption_(paramsXmlFileNameOption_in)
108 ,extraParamsXmlStringOption_(extraParamsXmlStringOption_in)
109 ,paramsUsedXmlOutFileNameOption_(paramsUsedXmlOutFileNameOption_in)
110 ,enableDelayedSolverConstruction_(EnableDelayedSolverConstruction_default)
116template<
class Scalar>
121 if (nonnull(paramList_)) {
122 paramList_->validateParameters(*this->getValidParameters());
128template<
class Scalar>
130 const RCP<
const AbstractFactory<Thyra::LinearOpWithSolveFactoryBase<Scalar> > >
131 &solveStrategyFactory,
132 const std::string &solveStrategyName,
133 const bool makeDefault
136 validLowsfNames_.push_back(solveStrategyName);
137 lowsfArray_.push_back(solveStrategyFactory);
138 validParamList_ = Teuchos::null;
140 setDefaultLinearSolveStrategyFactoryName(solveStrategyName);
145template<
class Scalar>
147 const std::string &solveStrategyName)
149 defaultLOWSF_ = solveStrategyName;
153template<
class Scalar>
155 const RCP<
const AbstractFactory<Thyra::PreconditionerFactoryBase<Scalar> > >
156 &precStrategyFactory,
157 const std::string &precStrategyName,
158 const bool makeDefault
161 validPfNames_.push_back(precStrategyName);
162 pfArray_.push_back(precStrategyFactory);
163 validParamList_ = Teuchos::null;
165 setDefaultPreconditioningStrategyFactoryName(precStrategyName);
170template<
class Scalar>
172 const std::string &precStrategyName)
174 defaultPF_ = precStrategyName;
178template<
class Scalar>
181 TEUCHOS_TEST_FOR_EXCEPT(clp==NULL);
183 paramsXmlFileNameOption().c_str(),¶msXmlFileName_
184 ,
"Name of an XML file containing parameters for linear solver "
185 "options to be appended first."
188 extraParamsXmlStringOption().c_str(),&extraParamsXmlString_
189 ,
"An XML string containing linear solver parameters to be appended second."
192 paramsUsedXmlOutFileNameOption().c_str(),¶msUsedXmlOutFileName_
193 ,
"Name of an XML file that can be written with the parameter list after it "
194 "has been used on completion of this program."
199template<
class Scalar>
202 using Teuchos::parameterList;
204 using Teuchos::updateParametersFromXmlFile;
205 using Teuchos::updateParametersFromXmlString;
208 if (!paramList_.get()) {
209 paramList_ = parameterList(
"LinearSolverBuilder");
211 if (paramsXmlFileName().length()) {
213 *out << endl <<
"Reading parameters from XML file \""
214 << paramsXmlFileName() <<
"\" ..." << endl;
216 updateParametersFromXmlFile (paramsXmlFileName (), paramList_.ptr());
218 if (extraParamsXmlString().length()) {
220 *out << endl <<
"Appending extra parameters from the XML string \""
221 << extraParamsXmlString() <<
"\" ..." << endl;
223 updateParametersFromXmlString (extraParamsXmlString (), paramList_.ptr());
225 setParameterList(paramList_);
229template<
class Scalar>
231 const Thyra::LinearOpWithSolveFactoryBase<Scalar> &,
232 const std::string &outputXmlFileName
235 justInTimeInitialize();
236 const std::string xmlOutputFile =
237 ( outputXmlFileName.length() ? outputXmlFileName : paramsUsedXmlOutFileName() );
238 if (xmlOutputFile.length()) {
239 Teuchos::writeParameterListToXmlFile(*paramList_, xmlOutputFile);
244template<
class Scalar>
248 justInTimeInitialize();
249 return lowsfValidator_->getStringValue(*paramList_, LinearSolverType_name,
254template<
class Scalar>
258 justInTimeInitialize();
259 return pfValidator_->getStringValue(*paramList_, PreconditionerType_name,
267template<
class Scalar>
269 RCP<Teuchos::ParameterList>
const& paramList
272 TEUCHOS_TEST_FOR_EXCEPT(is_null(paramList));
273 paramList->validateParameters(*this->getValidParameters());
274 paramList_ = paramList;
275 enableDelayedSolverConstruction_ = paramList_->get(
276 EnableDelayedSolverConstruction_name, EnableDelayedSolverConstruction_default );
280template<
class Scalar>
281RCP<Teuchos::ParameterList>
288template<
class Scalar>
289RCP<Teuchos::ParameterList>
292 RCP<Teuchos::ParameterList> _paramList = paramList_;
293 paramList_ = Teuchos::null;
298template<
class Scalar>
299RCP<const Teuchos::ParameterList>
306template<
class Scalar>
307RCP<const Teuchos::ParameterList>
310 using Teuchos::rcp_implicit_cast;
311 typedef Teuchos::ParameterEntryValidator PEV;
312 if (is_null(validParamList_)) {
313 RCP<Teuchos::ParameterList>
314 validParamList = Teuchos::rcp(
new Teuchos::ParameterList);
316 lowsfValidator_ = Teuchos::rcp(
317 new Teuchos::StringToIntegralParameterEntryValidator<int>(
318 validLowsfNames_,LinearSolverType_name
322 LinearSolverType_name, defaultLOWSF_,
323 (std::string(
"Determines the type of linear solver that will be used.\n")
324 +
"The parameters for each solver type are specified in the sublist \""
325 + LinearSolverTypes_name +
"\"").c_str(),
326 rcp_implicit_cast<const PEV>(lowsfValidator_)
328 Teuchos::ParameterList &linearSolverTypesSL = validParamList->sublist(
329 LinearSolverTypes_name,
false,
330 "Sublists for each of the linear solver types set using the parameter\n"
331 "\"" + LinearSolverType_name +
"\". Note that the options for each\n"
332 "linear solver type given below will only be used if linear solvers\n"
333 "of that type are created. It is fine to list parameter sublists for\n"
334 "linear solver types that are not used."
336 for(
int i = 0; i < static_cast<int>(lowsfArray_.size()); ++i ) {
338 &lsname = validLowsfNames_[i];
339 const RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >
340 lowsf = lowsfArray_[i]->create();
341 linearSolverTypesSL.sublist(lsname).setParameters(*lowsf->getValidParameters()
342 ).disableRecursiveValidation();
345 pfValidator_ = Teuchos::rcp(
346 new Teuchos::StringToIntegralParameterEntryValidator<int>(
347 validPfNames_, PreconditionerType_name ) );
349 PreconditionerType_name, defaultPF_,
350 (std::string(
"Determines the type of preconditioner that will be used.\n")
351 +
"This option is only meaningful for linear solvers that accept preconditioner"
352 +
" factory objects!\n"
353 +
"The parameters for each preconditioner are specified in the sublist \""
354 + PreconditionerTypes_name +
"\"").c_str(),
355 rcp_implicit_cast<const PEV>(pfValidator_)
357 Teuchos::ParameterList &precTypesSL = validParamList->sublist(
358 PreconditionerTypes_name,
false,
359 "Sublists for each of the preconditioner types set using the parameter\n"
360 "\"" + PreconditionerType_name +
"\". Note that the options for each\n"
361 "preconditioner type given below will only be used if preconditioners\n"
362 "of that type are created. It is fine to list parameter sublists for\n"
363 "preconditioner types that are not used."
365 for(
int i = 0; i < static_cast<int>(pfArray_.size()); ++i ) {
367 &pfname = validPfNames_[i+1];
368 const RCP<Thyra::PreconditionerFactoryBase<Scalar> >
369 pf = pfArray_[i]->create();
370 precTypesSL.sublist(pfname).setParameters(*pf->getValidParameters()
371 ).disableRecursiveValidation();
375 EnableDelayedSolverConstruction_name, EnableDelayedSolverConstruction_default,
376 "When this option is set to true, the linear solver factory will be wrapped\n"
377 "in a delayed evaluation Decorator factory object. This results in a delay\n"
378 "in the creation of a linear solver (and the associated preconditioner) until\n"
379 "the first solve is actually performed. This helps in cases where it is not\n"
380 "known a-priori if a linear solve will be needed on a given linear operator and\n"
381 "therefore can significantly improve performance for some types of algorithms\n"
382 "such as NOX and LOCA."
385 validParamList_ = validParamList;
387 return validParamList_;
394template<
class Scalar>
395RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >
397 const std::string &linearSolveStrategyName
400 justInTimeInitialize();
403#ifdef THYRA_DEFAULT_REAL_LINEAR_SOLVER_BUILDER_DUMP
404 std::cout <<
"\nEntering LinearSolverBuilder"
405 <<
"::createLinearSolveStrategy(...) ...\n";
406 std::cout <<
"\nlinearSolveStrategyName = \""
407 << linearSolveStrategyName <<
"\"\n";
408 std::cout <<
"\nlinearSolveStrategyName.length() = "
409 << linearSolveStrategyName.length() <<
"\n";
410 std::cout <<
"\ndefaultLOWSF_ = \"" << defaultLOWSF_ <<
"\"\n";
411 std::cout <<
"\nthis->getLinearSolveStrategyName() = \""
412 << this->getLinearSolveStrategyName() <<
"\"\n";
415 lsname = ( linearSolveStrategyName.length()
416 ? linearSolveStrategyName
417 : this->getLinearSolveStrategyName() );
418#ifdef THYRA_DEFAULT_REAL_LINEAR_SOLVER_BUILDER_DUMP
419 std::cout <<
"\nlsname = \"" << lsname <<
"\"\n";
424 ls_idx = lowsfValidator_->getIntegralValue(lsname, LinearSolverType_name);
427 RCP<Thyra::LinearOpWithSolveFactoryBase<Scalar> >
428 lowsf = lowsfArray_[ls_idx]->create();
431 if(lowsf->acceptsPreconditionerFactory()) {
432 const std::string &pfName = this->getPreconditionerStrategyName();
433 RCP<Thyra::PreconditionerFactoryBase<Scalar> >
434 pf = this->createPreconditioningStrategy(pfName);
436 lowsf->setPreconditionerFactory(pf,pfName);
441 lowsf->setParameterList(
442 sublist(sublist(paramList_, LinearSolverTypes_name), lsname));
444 if (enableDelayedSolverConstruction_) {
446 new Thyra::DelayedLinearOpWithSolveFactory<Scalar>(lowsf)
455template<
class Scalar>
456RCP<Thyra::PreconditionerFactoryBase<Scalar> >
458 const std::string &preconditioningStrategyName
461 justInTimeInitialize();
465 pfname = ( preconditioningStrategyName.length()
466 ? preconditioningStrategyName
467 : this->getPreconditionerStrategyName() );
468 RCP<Thyra::PreconditionerFactoryBase<Scalar> >
473 pf_idx = pfValidator_->getIntegralValue(pfname, PreconditionerType_name);
475 pf = pfArray_[pf_idx-1]->create();
476 pf->setParameterList(
477 sublist(sublist(paramList_, PreconditionerTypes_name), pfname));
487template<
class Scalar>
492 using Teuchos::abstractFactoryStd;
495 defaultPF_ = None_name;
496 validLowsfNames_.resize(0);
497 validPfNames_.resize(0);
498 validPfNames_.push_back(None_name);
504#ifdef HAVE_STRATIMIKOS_AMESOS2
505 setLinearSolveStrategyFactory(
506 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<Scalar>,
512#ifdef HAVE_STRATIMIKOS_BELOS
513 setLinearSolveStrategyFactory(
514 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<Scalar>,
527 using Scalar = double;
529 using Teuchos::abstractFactoryStd;
532 defaultPF_ = None_name;
533 validLowsfNames_.resize(0);
534 validPfNames_.resize(0);
535 validPfNames_.push_back(None_name);
541#ifdef HAVE_STRATIMIKOS_AMESOS2
542 setLinearSolveStrategyFactory(
543 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<Scalar>,
549#ifdef HAVE_STRATIMIKOS_BELOS
550 setLinearSolveStrategyFactory(
551 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<Scalar>,
557#ifdef HAVE_STRATIMIKOS_AMESOS
558 setLinearSolveStrategyFactory(
559 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<Scalar>,
565#if defined(HAVE_STRATIMIKOS_EPETRAEXT) && defined(HAVE_STRATIMIKOS_AZTECOO)
566 setLinearSolveStrategyFactory(
567 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<Scalar>,
576#ifdef HAVE_STRATIMIKOS_AMESOS
577 if (Teuchos::GlobalMPISession::getNProc() == 1) {
578 setDefaultLinearSolveStrategyFactoryName(
"Amesos");
586#ifdef HAVE_STRATIMIKOS_ML
587 setPreconditioningStrategyFactory(
588 abstractFactoryStd<Thyra::PreconditionerFactoryBase<Scalar>,
594#ifdef HAVE_STRATIMIKOS_IFPACK
595 setPreconditioningStrategyFactory(
596 abstractFactoryStd<Thyra::PreconditionerFactoryBase<Scalar>,
606template<
class Scalar>
609 paramList_.assert_not_null();
610 if (is_null(validParamList_)) {
612 this->getValidParameters();
624#define STRATIMIKOS_LINEARSOLVERBUILDER_INSTANT(SCALAR) \
626 template class LinearSolverBuilder<SCALAR >;
Concrete subclass of Thyra::LinearSolverBuilderBase for creating LinearOpWithSolveFactoryBase objects...
void setLinearSolveStrategyFactory(const RCP< const AbstractFactory< Thyra::LinearOpWithSolveFactoryBase< Scalar > > > &solveStrategyFactory, const std::string &solveStrategyName, const bool makeDefault=false)
Set a new linear solver strategy factory object.
RCP< Thyra::PreconditionerFactoryBase< Scalar > > createPreconditioningStrategy(const std::string &preconditioningStrategyName) const
RCP< const ParameterList > getParameterList() const
std::string getLinearSolveStrategyName() const
Get the name of the linear solver strategy that will be created on the next call to this->createLinea...
void setDefaultPreconditioningStrategyFactoryName(const std::string &precStrategyName)
Set the default linear solver factory name.
RCP< const ParameterList > getValidParameters() const
void initializeDefaults()
RCP< Thyra::LinearOpWithSolveFactoryBase< Scalar > > createLinearSolveStrategy(const std::string &linearSolveStrategyName) const
std::string getPreconditionerStrategyName() const
Get the name of the preconditioner strategy that will be created on the next call to this->createPrec...
void setParameterList(RCP< ParameterList > const ¶mList)
LinearSolverBuilder(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.
void setDefaultLinearSolveStrategyFactoryName(const std::string &solveStrategyName)
Set the default linear solver factory name.
void setupCLP(Teuchos::CommandLineProcessor *clp)
Setup the command-line processor to read in the needed data to extra the parameters from.
RCP< ParameterList > getNonconstParameterList()
void readParameters(std::ostream *out)
Force the parameters to be read from a file and/or an extra XML string.
void justInTimeInitialize() const
void setPreconditioningStrategyFactory(const RCP< const AbstractFactory< Thyra::PreconditionerFactoryBase< Scalar > > > &precStrategyFactory, const std::string &precStrategyName, const bool makeDefault=false)
Set a new preconditioner strategy factory object.
RCP< ParameterList > unsetParameterList()
void writeParamsFile(const Thyra::LinearOpWithSolveFactoryBase< Scalar > &lowsFactory, const std::string &outputXmlFileName="") const
Write the parameters list for a LinearOpWithSolveFactoryBase object to a file after the parameters ar...
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.