Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
parameterlist/example/ParameterList/cxx_main.cpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
12 #include "Teuchos_Array.hpp"
13 #include "Teuchos_Version.hpp"
14 #include "Teuchos_as.hpp"
16 
17 int main(int argc, char* argv[])
18 {
20  using Teuchos::RCP;
21  using Teuchos::Array;
22  using Teuchos::tuple;
23  using Teuchos::as;
24 
25  bool success = false;
26  bool verbose = true;
27  try {
28 
29  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
30 
31  // Creating an empty parameter list looks like:
32  ParameterList myPL;
33 
34  // Setting parameters in this list can be easily done:
35  myPL.set("Max Iters", 1550, "Determines the maximum number of iterations in the solver");
36  myPL.set("Tolerance", 1e-10, "The tolerance used for the convergence check");
37 
38  // For the "Solver" option, create a validator that will automatically
39  // create documentation for this parameter but will also help in validation.
40  RCP<Teuchos::StringToIntegralParameterEntryValidator<int> >
41  solverValidator = Teuchos::rcp(
43  Teuchos::tuple<std::string>( "GMRES", "CG", "TFQMR" )
44  ,"Solver"
45  )
46  );
47  myPL.set(
48  "Solver"
49  ,"GMRES" // This will be validated by solverValidator right here!
50  ,"The type of solver to use"
51  ,solverValidator
52  );
53 
54  /* The templated ``set'' method should cast the input {\it value} to the
55  correct data type. However, in the case where the compiler is not casting the input
56  value to the expected data type, an explicit cast can be used with the ``set'' method:
57  */
58  myPL.set("Tolerance", as<float>(1e-10), "The tolerance used for the convergence check");
59 
60  /* Reference-counted pointers can also be passed through a ParameterList.
61  To illustrate this we will use the Array class to create an array of 10 doubles
62  representing an initial guess for a linear solver, whose memory is being managed by a
63  RCP.
64  */
65 
66  myPL.set<Array<double> >("Initial Guess", tuple<double>( 10, 0.0 ),
67  "The initial guess as a RCP to an array object.");
68 
69  /* A hierarchy of parameter lists can be constructed using {\tt ParameterList}. This
70  means another parameter list is a valid {\it value} in any parameter list. To create a sublist
71  in a parameter list and obtain a reference to it:
72  */
73  ParameterList& Prec_List = myPL.sublist("Preconditioner", false,
74  "Sublist that defines the preconditioner.");
75 
76  // Now this parameter list can be filled with values:
77  Prec_List.set("Type", "ILU", "The tpye of preconditioner to use");
78  Prec_List.set("Drop Tolerance", 1e-3,
79  "The tolerance below which entries from the\n""factorization are left out of the factors.");
80 
81  // The parameter list can be queried about the existance of a parameter, sublist, or type:
82  // Has a solver been chosen?
83  bool solver_defined = false, prec_defined = false, dtol_double = false;
84  solver_defined = myPL.isParameter("Solver");
85  TEUCHOS_ASSERT_EQUALITY(solver_defined, true);
86  // Has a preconditioner been chosen?
87  prec_defined = myPL.isSublist("Preconditioner");
88  TEUCHOS_ASSERT_EQUALITY(prec_defined, true);
89  // Has a tolerance been chosen and is it a double-precision number?
90  bool tol_double = false;
91  tol_double = myPL.INVALID_TEMPLATE_QUALIFIER isType<double>("Tolerance");
92  TEUCHOS_ASSERT_EQUALITY(tol_double, false); // It is 'float'!
93  // Has a drop tolerance been chosen and is it a double-precision number?
94  dtol_double = Teuchos::isParameterType<double>(Prec_List, "Drop Tolerance");
95  TEUCHOS_ASSERT_EQUALITY(dtol_double, true);
96 
97  // Parameters can be retrieved from the parameter list in quite a few ways:
98  // Get method that creates and sets the parameter if it doesn't exist.
99  int its = -1;
100  its = myPL.get("Max Iters", 1200);
101  TEUCHOS_ASSERT_EQUALITY(its, 1550); // Was already ste
102  // Get method that retrieves a parameter of a particular type that must exist.
103  float tol = -1.0;
104  tol = myPL.get<float>("Tolerance");
105  TEUCHOS_ASSERT_EQUALITY(tol, as<float>(1e-10));
106  // Get the "Solver" value and validate!
107  std::string
108  solver = solverValidator->validateString(
109  Teuchos::getParameter<std::string>(myPL,"Solver")
110  );
111 
112  // We can use this same syntax to get arrays out, like the initial guess.
113  Array<double> init_guess = myPL.get<Array<double> >("Initial Guess");
114 
115  std::cout << "\n# Printing this parameter list using opeator<<(...) ...\n\n";
116  std::cout << myPL << std::endl;
117 
118  std::cout << "\n# Printing the parameter list only showing documentation fields ...\n\n";
119  myPL.print(std::cout,
120  ParameterList::PrintOptions().showDoc(true).indent(2).showTypes(true));
121 
122  /* It is important to note that mispelled parameters
123  (with additional space characters, capitalizations, etc.) may be ignored.
124  Therefore, it is important to be aware that a given parameter has not been used.
125  Unused parameters can be printed with method:
126  */
127  std::cout << "\n# Showing unused parameters ...\n\n";
128  myPL.unused( std::cout );
129 
130  success = true;
131  }
132  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
133  return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
134 }
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
std::string Teuchos_Version()
A list of parameters of arbitrary type.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
int main(int argc, char *argv[])
Templated array class derived from the STL std::vector.
Smart reference counting pointer class for automatic garbage collection.
#define TEUCHOS_ASSERT_EQUALITY(val1, val2)
This macro is checks that to numbers are equal and if not then throws an exception with a good error ...
Definition of Teuchos::as, for conversions between types.
Replacement for std::vector that is compatible with the Teuchos Memory Management classes...