ParameterList/cxx_main.cpp
This is an example of how to use the
Teuchos::ParameterList class.
#include "Teuchos_ParameterList.hpp"
#include "Teuchos_StandardParameterEntryValidators.hpp"
#include "Teuchos_Array.hpp"
#include "Teuchos_Version.hpp"
int main(int argc, char* argv[])
{
std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
Teuchos::ParameterList My_List;
My_List.set("Max Iters", 1550, "Determines the maximum number of iterations in the solver");
My_List.set("Tolerance", 1e-10, "The tolerance used for the convergence check");
Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<int> >
solverValidator = Teuchos::rcp(
new Teuchos::StringToIntegralParameterEntryValidator<int>(
Teuchos::tuple<std::string>( "GMRES", "CG", "TFQMR" )
,"Solver"
)
);
My_List.set(
"Solver"
,"GMRES"
,"The type of solver to use."
,solverValidator
);
My_List.set("Tolerance", (float)(1e-10), "The tolerance used for the convergence check");
Teuchos::RCP<Teuchos::Array<double> > rcp_Array =
Teuchos::rcp( new Teuchos::Array<double>( 10, 0.0 ) );
My_List.set("Initial Guess", rcp_Array, "The initial guess as a RCP to an array object.");
Teuchos::ParameterList&
Prec_List = My_List.sublist("Preconditioner",false,"Sublist that defines the preconditioner.");
Prec_List.set("Type", "ILU", "The tpye of preconditioner to use");
Prec_List.set("Drop Tolerance", 1e-3
,"The tolerance below which entries from the\n""factorization are left out of the factors.");
bool solver_defined = false, prec_defined = false, dtol_double = false;
solver_defined = My_List.isParameter("Solver");
prec_defined = My_List.isSublist("Preconditioner");
bool tol_double = false;
tol_double = My_List.INVALID_TEMPLATE_QUALIFIER isType<double>("Tolerance");
dtol_double = Teuchos::isParameterType<double>(Prec_List, "Drop Tolerance");
int its = 0;
its = My_List.get("Max Iters", 1200);
float tol;
tol = My_List.INVALID_TEMPLATE_QUALIFIER get<float>("Tolerance");
std::string
solver = solverValidator->validateString(
Teuchos::getParameter<std::string>(My_List,"Solver")
);
try {
tol = My_List.INVALID_TEMPLATE_QUALIFIER get<float>("Tolerance");
}
catch ( std::exception& e) {
tol = 1e-6;
}
try {
tol = Teuchos::getParameter<float>(My_List, "Tolerance");
}
catch ( std::exception& e) {
tol = 1e-6;
}
try {
Teuchos::RCP<Teuchos::Array<double> > init_guess =
Teuchos::getParameter<Teuchos::RCP<Teuchos::Array<double> > >(My_List, "Initial Guess");
}
catch ( std::exception& e) {
std::cout << e.what() << std::endl;
}
std::cout << "\n# Printing this parameter list using opeator<<(...) ...\n\n";
std::cout << My_List << std::endl;
std::cout << "\n# Printing the parameter list only showing documentation fields ...\n\n";
My_List.print(std::cout,Teuchos::ParameterList::PrintOptions().showDoc(true).indent(2).showTypes(true));
std::cout << "\n# Showing unused parameters ...\n\n";
My_List.unused( std::cout );
return 0;
}