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 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Teuchos: Common Tools Package
6 // Copyright (2004) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
46 #include "Teuchos_Array.hpp"
47 #include "Teuchos_Version.hpp"
48 #include "Teuchos_as.hpp"
50 
51 int main(int argc, char* argv[])
52 {
54  using Teuchos::RCP;
55  using Teuchos::Array;
56  using Teuchos::tuple;
57  using Teuchos::as;
58 
59  bool success = false;
60  bool verbose = true;
61  try {
62 
63  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
64 
65  // Creating an empty parameter list looks like:
66  ParameterList myPL;
67 
68  // Setting parameters in this list can be easily done:
69  myPL.set("Max Iters", 1550, "Determines the maximum number of iterations in the solver");
70  myPL.set("Tolerance", 1e-10, "The tolerance used for the convergence check");
71 
72  // For the "Solver" option, create a validator that will automatically
73  // create documentation for this parameter but will also help in validation.
74  RCP<Teuchos::StringToIntegralParameterEntryValidator<int> >
75  solverValidator = Teuchos::rcp(
77  Teuchos::tuple<std::string>( "GMRES", "CG", "TFQMR" )
78  ,"Solver"
79  )
80  );
81  myPL.set(
82  "Solver"
83  ,"GMRES" // This will be validated by solverValidator right here!
84  ,"The type of solver to use"
85  ,solverValidator
86  );
87 
88  /* The templated ``set'' method should cast the input {\it value} to the
89  correct data type. However, in the case where the compiler is not casting the input
90  value to the expected data type, an explicit cast can be used with the ``set'' method:
91  */
92  myPL.set("Tolerance", as<float>(1e-10), "The tolerance used for the convergence check");
93 
94  /* Reference-counted pointers can also be passed through a ParameterList.
95  To illustrate this we will use the Array class to create an array of 10 doubles
96  representing an initial guess for a linear solver, whose memory is being managed by a
97  RCP.
98  */
99 
100  myPL.set<Array<double> >("Initial Guess", tuple<double>( 10, 0.0 ),
101  "The initial guess as a RCP to an array object.");
102 
103  /* A hierarchy of parameter lists can be constructed using {\tt ParameterList}. This
104  means another parameter list is a valid {\it value} in any parameter list. To create a sublist
105  in a parameter list and obtain a reference to it:
106  */
107  ParameterList& Prec_List = myPL.sublist("Preconditioner", false,
108  "Sublist that defines the preconditioner.");
109 
110  // Now this parameter list can be filled with values:
111  Prec_List.set("Type", "ILU", "The tpye of preconditioner to use");
112  Prec_List.set("Drop Tolerance", 1e-3,
113  "The tolerance below which entries from the\n""factorization are left out of the factors.");
114 
115  // The parameter list can be queried about the existance of a parameter, sublist, or type:
116  // Has a solver been chosen?
117  bool solver_defined = false, prec_defined = false, dtol_double = false;
118  solver_defined = myPL.isParameter("Solver");
119  TEUCHOS_ASSERT_EQUALITY(solver_defined, true);
120  // Has a preconditioner been chosen?
121  prec_defined = myPL.isSublist("Preconditioner");
122  TEUCHOS_ASSERT_EQUALITY(prec_defined, true);
123  // Has a tolerance been chosen and is it a double-precision number?
124  bool tol_double = false;
125  tol_double = myPL.INVALID_TEMPLATE_QUALIFIER isType<double>("Tolerance");
126  TEUCHOS_ASSERT_EQUALITY(tol_double, false); // It is 'float'!
127  // Has a drop tolerance been chosen and is it a double-precision number?
128  dtol_double = Teuchos::isParameterType<double>(Prec_List, "Drop Tolerance");
129  TEUCHOS_ASSERT_EQUALITY(dtol_double, true);
130 
131  // Parameters can be retrieved from the parameter list in quite a few ways:
132  // Get method that creates and sets the parameter if it doesn't exist.
133  int its = -1;
134  its = myPL.get("Max Iters", 1200);
135  TEUCHOS_ASSERT_EQUALITY(its, 1550); // Was already ste
136  // Get method that retrieves a parameter of a particular type that must exist.
137  float tol = -1.0;
138  tol = myPL.get<float>("Tolerance");
139  TEUCHOS_ASSERT_EQUALITY(tol, as<float>(1e-10));
140  // Get the "Solver" value and validate!
141  std::string
142  solver = solverValidator->validateString(
143  Teuchos::getParameter<std::string>(myPL,"Solver")
144  );
145 
146  // We can use this same syntax to get arrays out, like the initial guess.
147  Array<double> init_guess = myPL.get<Array<double> >("Initial Guess");
148 
149  std::cout << "\n# Printing this parameter list using opeator<<(...) ...\n\n";
150  std::cout << myPL << std::endl;
151 
152  std::cout << "\n# Printing the parameter list only showing documentation fields ...\n\n";
153  myPL.print(std::cout,
154  ParameterList::PrintOptions().showDoc(true).indent(2).showTypes(true));
155 
156  /* It is important to note that mispelled parameters
157  (with additional space characters, capitalizations, etc.) may be ignored.
158  Therefore, it is important to be aware that a given parameter has not been used.
159  Unused parameters can be printed with method:
160  */
161  std::cout << "\n# Showing unused parameters ...\n\n";
162  myPL.unused( std::cout );
163 
164  success = true;
165  }
166  TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
167  return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
168 }
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...