42 #ifndef STRATIMIKOS_LINEARSOLVERBUILDER_DEF_HPP
43 #define STRATIMIKOS_LINEARSOLVERBUILDER_DEF_HPP
47 #include "Stratimikos_InternalConfig.h"
48 #include "Thyra_DelayedLinearOpWithSolveFactory.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 #if defined(HAVE_STRATIMIKOS_IFPACK2) && defined(HAVE_STRATIMIKOS_THYRATPETRAADAPTERS)
70 # include "Thyra_Ifpack2PreconditionerFactory.hpp"
71 # include "Tpetra_CrsMatrix.hpp"
73 #ifdef HAVE_STRATIMIKOS_ML
81 const std::string LinearSolverType_name =
"Linear Solver Type";
82 const std::string LinearSolverTypes_name =
"Linear Solver Types";
83 const std::string PreconditionerType_name =
"Preconditioner Type";
84 const std::string PreconditionerTypes_name =
"Preconditioner Types";
85 const std::string None_name =
"None";
86 const std::string EnableDelayedSolverConstruction_name =
"Enable Delayed Solver Construction";
87 const bool EnableDelayedSolverConstruction_default =
false;
93 namespace Stratimikos {
99 template<
class Scalar>
101 const std::string ¶msXmlFileName_in,
102 const std::string &extraParamsXmlString_in,
103 const std::string ¶msUsedXmlOutFileName_in,
104 const std::string ¶msXmlFileNameOption_in,
105 const std::string &extraParamsXmlStringOption_in,
106 const std::string ¶msUsedXmlOutFileNameOption_in,
107 const bool &replaceDuplicateFactories_in
109 :paramsXmlFileName_(paramsXmlFileName_in)
110 ,extraParamsXmlString_(extraParamsXmlString_in)
111 ,paramsUsedXmlOutFileName_(paramsUsedXmlOutFileName_in)
112 ,paramsXmlFileNameOption_(paramsXmlFileNameOption_in)
113 ,extraParamsXmlStringOption_(extraParamsXmlStringOption_in)
114 ,paramsUsedXmlOutFileNameOption_(paramsUsedXmlOutFileNameOption_in)
115 ,replaceDuplicateFactories_(replaceDuplicateFactories_in)
116 ,enableDelayedSolverConstruction_(EnableDelayedSolverConstruction_default)
122 template<
class Scalar>
128 paramList_->validateParameters(*this->getValidParameters());
134 template<
class Scalar>
137 &solveStrategyFactory,
138 const std::string &solveStrategyName,
139 const bool makeDefault
142 const int existingNameIdx =
143 this->getAndAssertExistingFactoryNameIdx(
"setLinearSolveStrategyFactory()",
144 validLowsfNames_(), solveStrategyName);
145 if (existingNameIdx >= 0) {
146 validLowsfNames_[existingNameIdx] = solveStrategyName;
147 lowsfArray_[existingNameIdx] = solveStrategyFactory;
150 validLowsfNames_.push_back(solveStrategyName);
151 lowsfArray_.push_back(solveStrategyFactory);
155 setDefaultLinearSolveStrategyFactoryName(solveStrategyName);
160 template<
class Scalar>
162 const std::string &solveStrategyName)
164 defaultLOWSF_ = solveStrategyName;
168 template<
class Scalar>
171 &precStrategyFactory,
172 const std::string &precStrategyName,
173 const bool makeDefault
176 const int existingNameIdx =
177 this->getAndAssertExistingFactoryNameIdx(
"setPreconditioningStrategyFactory()",
178 validPfNames_(), precStrategyName);
179 if (existingNameIdx >= 0) {
180 validPfNames_[existingNameIdx] = precStrategyName;
181 pfArray_[existingNameIdx-1] = precStrategyFactory;
184 validPfNames_.push_back(precStrategyName);
185 pfArray_.push_back(precStrategyFactory);
189 setDefaultPreconditioningStrategyFactoryName(precStrategyName);
194 template<
class Scalar>
196 const std::string &precStrategyName)
198 defaultPF_ = precStrategyName;
202 template<
class Scalar>
207 paramsXmlFileNameOption().c_str(),¶msXmlFileName_
208 ,
"Name of an XML file containing parameters for linear solver "
209 "options to be appended first."
212 extraParamsXmlStringOption().c_str(),&extraParamsXmlString_
213 ,
"An XML string containing linear solver parameters to be appended second."
216 paramsUsedXmlOutFileNameOption().c_str(),¶msUsedXmlOutFileName_
217 ,
"Name of an XML file that can be written with the parameter list after it "
218 "has been used on completion of this program."
223 template<
class Scalar>
226 using Teuchos::parameterList;
228 using Teuchos::updateParametersFromXmlFile;
229 using Teuchos::updateParametersFromXmlString;
232 if (!paramList_.get()) {
233 paramList_ = parameterList(
"LinearSolverBuilder");
235 if (paramsXmlFileName().length()) {
237 *out << endl <<
"Reading parameters from XML file \""
238 << paramsXmlFileName() <<
"\" ..." << endl;
240 updateParametersFromXmlFile (paramsXmlFileName (), paramList_.ptr());
242 if (extraParamsXmlString().length()) {
244 *out << endl <<
"Appending extra parameters from the XML string \""
245 << extraParamsXmlString() <<
"\" ..." << endl;
247 updateParametersFromXmlString (extraParamsXmlString (), paramList_.ptr());
249 setParameterList(paramList_);
253 template<
class Scalar>
255 const Thyra::LinearOpWithSolveFactoryBase<Scalar> &,
256 const std::string &outputXmlFileName
259 justInTimeInitialize();
260 const std::string xmlOutputFile =
261 ( outputXmlFileName.length() ? outputXmlFileName : paramsUsedXmlOutFileName() );
262 if (xmlOutputFile.length()) {
263 Teuchos::writeParameterListToXmlFile(*paramList_, xmlOutputFile);
268 template<
class Scalar>
272 justInTimeInitialize();
273 return lowsfValidator_->getStringValue(*paramList_, LinearSolverType_name,
278 template<
class Scalar>
282 justInTimeInitialize();
283 return pfValidator_->getStringValue(*paramList_, PreconditionerType_name,
291 template<
class Scalar>
298 paramList_ = paramList;
299 enableDelayedSolverConstruction_ = paramList_->
get(
300 EnableDelayedSolverConstruction_name, EnableDelayedSolverConstruction_default );
304 template<
class Scalar>
312 template<
class Scalar>
322 template<
class Scalar>
330 template<
class Scalar>
334 using Teuchos::rcp_implicit_cast;
336 if (
is_null(validParamList_)) {
342 validLowsfNames_,LinearSolverType_name
346 LinearSolverType_name, defaultLOWSF_,
347 (std::string(
"Determines the type of linear solver that will be used.\n")
348 +
"The parameters for each solver type are specified in the sublist \""
349 + LinearSolverTypes_name +
"\"").c_str(),
350 rcp_implicit_cast<const PEV>(lowsfValidator_)
353 LinearSolverTypes_name,
false,
354 "Sublists for each of the linear solver types set using the parameter\n"
355 "\"" + LinearSolverType_name +
"\". Note that the options for each\n"
356 "linear solver type given below will only be used if linear solvers\n"
357 "of that type are created. It is fine to list parameter sublists for\n"
358 "linear solver types that are not used."
360 for(
int i = 0; i < static_cast<int>(lowsfArray_.size()); ++i ) {
362 &lsname = validLowsfNames_[i];
364 lowsf = lowsfArray_[i]->create();
365 linearSolverTypesSL.
sublist(lsname).setParameters(*lowsf->getValidParameters()
366 ).disableRecursiveValidation();
371 validPfNames_, PreconditionerType_name ) );
373 PreconditionerType_name, defaultPF_,
374 (std::string(
"Determines the type of preconditioner that will be used.\n")
375 +
"This option is only meaningful for linear solvers that accept preconditioner"
376 +
" factory objects!\n"
377 +
"The parameters for each preconditioner are specified in the sublist \""
378 + PreconditionerTypes_name +
"\"").c_str(),
379 rcp_implicit_cast<const PEV>(pfValidator_)
382 PreconditionerTypes_name,
false,
383 "Sublists for each of the preconditioner types set using the parameter\n"
384 "\"" + PreconditionerType_name +
"\". Note that the options for each\n"
385 "preconditioner type given below will only be used if preconditioners\n"
386 "of that type are created. It is fine to list parameter sublists for\n"
387 "preconditioner types that are not used."
389 for(
int i = 0; i < static_cast<int>(pfArray_.size()); ++i ) {
391 &pfname = validPfNames_[i+1];
393 pf = pfArray_[i]->create();
394 precTypesSL.
sublist(pfname).setParameters(*pf->getValidParameters()
395 ).disableRecursiveValidation();
399 EnableDelayedSolverConstruction_name, EnableDelayedSolverConstruction_default,
400 "When this option is set to true, the linear solver factory will be wrapped\n"
401 "in a delayed evaluation Decorator factory object. This results in a delay\n"
402 "in the creation of a linear solver (and the associated preconditioner) until\n"
403 "the first solve is actually performed. This helps in cases where it is not\n"
404 "known a-priori if a linear solve will be needed on a given linear operator and\n"
405 "therefore can significantly improve performance for some types of algorithms\n"
406 "such as NOX and LOCA."
409 validParamList_ = validParamList;
411 return validParamList_;
418 template<
class Scalar>
421 const std::string &linearSolveStrategyName
424 justInTimeInitialize();
427 #ifdef THYRA_DEFAULT_REAL_LINEAR_SOLVER_BUILDER_DUMP
428 std::cout <<
"\nEntering LinearSolverBuilder"
429 <<
"::createLinearSolveStrategy(...) ...\n";
430 std::cout <<
"\nlinearSolveStrategyName = \""
431 << linearSolveStrategyName <<
"\"\n";
432 std::cout <<
"\nlinearSolveStrategyName.length() = "
433 << linearSolveStrategyName.length() <<
"\n";
434 std::cout <<
"\ndefaultLOWSF_ = \"" << defaultLOWSF_ <<
"\"\n";
435 std::cout <<
"\nthis->getLinearSolveStrategyName() = \""
436 << this->getLinearSolveStrategyName() <<
"\"\n";
439 lsname = ( linearSolveStrategyName.length()
440 ? linearSolveStrategyName
441 : this->getLinearSolveStrategyName() );
442 #ifdef THYRA_DEFAULT_REAL_LINEAR_SOLVER_BUILDER_DUMP
443 std::cout <<
"\nlsname = \"" << lsname <<
"\"\n";
448 ls_idx = lowsfValidator_->getIntegralValue(lsname, LinearSolverType_name);
452 lowsf = lowsfArray_[ls_idx]->create();
455 if(lowsf->acceptsPreconditionerFactory()) {
456 const std::string &pfName = this->getPreconditionerStrategyName();
458 pf = this->createPreconditioningStrategy(pfName);
460 lowsf->setPreconditionerFactory(pf,pfName);
465 lowsf->setParameterList(
466 sublist(sublist(paramList_, LinearSolverTypes_name), lsname));
468 if (enableDelayedSolverConstruction_) {
470 new Thyra::DelayedLinearOpWithSolveFactory<Scalar>(lowsf)
479 template<
class Scalar>
482 const std::string &preconditioningStrategyName
485 justInTimeInitialize();
489 pfname = ( preconditioningStrategyName.length()
490 ? preconditioningStrategyName
491 : this->getPreconditionerStrategyName() );
497 pf_idx = pfValidator_->getIntegralValue(pfname, PreconditionerType_name);
499 pf = pfArray_[pf_idx-1]->create();
500 pf->setParameterList(
501 sublist(sublist(paramList_, PreconditionerTypes_name), pfname));
511 template<
class Scalar>
516 using Teuchos::abstractFactoryStd;
519 defaultPF_ = None_name;
520 validLowsfNames_.resize(0);
521 validPfNames_.resize(0);
522 validPfNames_.push_back(None_name);
528 #ifdef HAVE_STRATIMIKOS_AMESOS2
529 setLinearSolveStrategyFactory(
530 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<Scalar>,
536 #ifdef HAVE_STRATIMIKOS_BELOS
537 setLinearSolveStrategyFactory(
538 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<Scalar>,
544 #if defined(HAVE_STRATIMIKOS_IFPACK2) && defined(HAVE_STRATIMIKOS_THYRATPETRAADAPTERS)
545 setPreconditioningStrategyFactory(
546 abstractFactoryStd<Thyra::PreconditionerFactoryBase<Scalar>,
547 Thyra::Ifpack2PreconditionerFactory<Tpetra::CrsMatrix<Scalar>>>(),
560 using Scalar = double;
562 using Teuchos::abstractFactoryStd;
565 defaultPF_ = None_name;
566 validLowsfNames_.resize(0);
567 validPfNames_.resize(0);
568 validPfNames_.push_back(None_name);
574 #ifdef HAVE_STRATIMIKOS_AMESOS2
575 setLinearSolveStrategyFactory(
576 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<Scalar>,
582 #ifdef HAVE_STRATIMIKOS_BELOS
583 setLinearSolveStrategyFactory(
584 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<Scalar>,
590 #ifdef HAVE_STRATIMIKOS_AMESOS
591 setLinearSolveStrategyFactory(
592 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<Scalar>,
598 #if defined(HAVE_STRATIMIKOS_EPETRAEXT) && defined(HAVE_STRATIMIKOS_AZTECOO)
599 setLinearSolveStrategyFactory(
600 abstractFactoryStd<Thyra::LinearOpWithSolveFactoryBase<Scalar>,
609 #ifdef HAVE_STRATIMIKOS_AMESOS
611 setDefaultLinearSolveStrategyFactoryName(
"Amesos");
619 #ifdef HAVE_STRATIMIKOS_ML
620 setPreconditioningStrategyFactory(
621 abstractFactoryStd<Thyra::PreconditionerFactoryBase<Scalar>,
627 #if defined(HAVE_STRATIMIKOS_IFPACK2) && defined(HAVE_STRATIMIKOS_THYRATPETRAADAPTERS)
628 setPreconditioningStrategyFactory(
629 abstractFactoryStd<Thyra::PreconditionerFactoryBase<Scalar>,
630 Thyra::Ifpack2PreconditionerFactory<Tpetra::CrsMatrix<Scalar>>>(),
635 #ifdef HAVE_STRATIMIKOS_IFPACK
636 setPreconditioningStrategyFactory(
637 abstractFactoryStd<Thyra::PreconditionerFactoryBase<Scalar>,
648 template<
class Scalar>
651 paramList_.assert_not_null();
652 if (
is_null(validParamList_)) {
654 this->getValidParameters();
659 template<
class Scalar>
662 const std::string &name)
const
664 const int existingNameIdx =
667 (!replaceDuplicateFactories_ && existingNameIdx >= 0), std::logic_error,
668 "ERROR: "<<setFunctionName<<
": the name='"<<name<<
"' already exists"
669 <<
" at index="<<existingNameIdx<<
"!\n"
671 <<
"TIP: To allow duplicates, change the property replaceDuplicateFactories from"
672 <<
" false to true by calling <thisObject>.replaceDuplicateFactories(true)"
673 <<
" where <thisObject> is of type Stratimikos::LinearSolverBuilder<Scalar>!");
674 return existingNameIdx;
685 #define STRATIMIKOS_LINEARSOLVERBUILDER_INSTANT(SCALAR) \
687 template class LinearSolverBuilder<SCALAR >;
693 #endif // STRATIMIKOS_LINEARSOLVERBUILDER_DEF_HPP
Concrete LinearOpWithSolveFactoryBase adapter subclass that uses Amesos direct solvers.
RCP< ParameterList > getNonconstParameterList()
void initializeDefaults()
Concrete preconditioner factory subclass based on Ifpack.
LinearOpWithSolveFactoryBase subclass implemented in terms of AztecOO.
bool is_null(const boost::shared_ptr< T > &p)
RCP< ParameterList > unsetParameterList()
RCP< const ParameterList > getParameterList() const
T & get(ParameterList &l, const std::string &name)
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", const bool &replaceDuplicateFactories=true)
Construct with default parameters.
ParameterList & set(std::string const &name, T const &value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
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.
void setDefaultLinearSolveStrategyFactoryName(const std::string &solveStrategyName)
Set the default linear solver factory name.
void justInTimeInitialize() const
RCP< ParameterList > sublist(const RCP< ParameterList > ¶mList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Concrete preconditioner factory subclass based on ML.
RCP< Thyra::PreconditionerFactoryBase< Scalar > > createPreconditioningStrategy(const std::string &preconditioningStrategyName) const
std::string getPreconditionerStrategyName() const
Get the name of the preconditioner strategy that will be created on the next call to this->createPrec...
void readParameters(std::ostream *out)
Force the parameters to be read from a file and/or an extra XML string.
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.
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
void setParameterList(RCP< ParameterList > const ¶mList)
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
std::string getLinearSolveStrategyName() const
Get the name of the linear solver strategy that will be created on the next call to this->createLinea...
LinearOpWithSolveFactoryBase subclass implemented in terms of Belos.
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
RCP< Thyra::LinearOpWithSolveFactoryBase< Scalar > > createLinearSolveStrategy(const std::string &linearSolveStrategyName) const
bool nonnull(const boost::shared_ptr< T > &p)
int getAndAssertExistingFactoryNameIdx(const std::string &setFunctionName, const Teuchos::ArrayView< std::string > namesArray, const std::string &name) const
Concrete subclass of Thyra::LinearSolverBuilderBase for creating Thyra::LinearOpWithSolveFactoryBase ...
void setDefaultPreconditioningStrategyFactoryName(const std::string &precStrategyName)
Set the default linear solver factory name.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
int existingNameIndex(const Teuchos::ArrayView< std::string > namesArray, const std::string &name)
Return the index of a name in an array of names.
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...
void setupCLP(Teuchos::CommandLineProcessor *clp)
Setup the command-line processor to read in the needed data to extra the parameters from...
RCP< const ParameterList > getValidParameters() const