Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
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"
49 
50 int main(int argc, char* argv[])
51 {
52 
54  using Teuchos::RCP;
55  using Teuchos::Array;
56  using Teuchos::tuple;
57  using Teuchos::as;
58 
59  std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
60 
61  // Creating an empty parameter list looks like:
62  ParameterList myPL;
63 
64  // Setting parameters in this list can be easily done:
65  myPL.set("Max Iters", 1550, "Determines the maximum number of iterations in the solver");
66  myPL.set("Tolerance", 1e-10, "The tolerance used for the convergence check");
67 
68  // For the "Solver" option, create a validator that will automatically
69  // create documentation for this parameter but will also help in validation.
70  RCP<Teuchos::StringToIntegralParameterEntryValidator<int> >
71  solverValidator = Teuchos::rcp(
73  Teuchos::tuple<std::string>( "GMRES", "CG", "TFQMR" )
74  ,"Solver"
75  )
76  );
77  myPL.set(
78  "Solver"
79  ,"GMRES" // This will be validated by solverValidator right here!
80  ,"The type of solver to use"
81  ,solverValidator
82  );
83 
84  /* The templated ``set'' method should cast the input {\it value} to the
85  correct data type. However, in the case where the compiler is not casting the input
86  value to the expected data type, an explicit cast can be used with the ``set'' method:
87  */
88  myPL.set("Tolerance", as<float>(1e-10), "The tolerance used for the convergence check");
89 
90  /* Reference-counted pointers can also be passed through a ParameterList.
91  To illustrate this we will use the Array class to create an array of 10 doubles
92  representing an initial guess for a linear solver, whose memory is being managed by a
93  RCP.
94  */
95 
96  myPL.set<Array<double> >("Initial Guess", tuple<double>( 10, 0.0 ),
97  "The initial guess as a RCP to an array object.");
98 
99  /* A hierarchy of parameter lists can be constructed using {\tt ParameterList}. This
100  means another parameter list is a valid {\it value} in any parameter list. To create a sublist
101  in a parameter list and obtain a reference to it:
102  */
103  ParameterList& Prec_List = myPL.sublist("Preconditioner", false,
104  "Sublist that defines the preconditioner.");
105 
106  // Now this parameter list can be filled with values:
107  Prec_List.set("Type", "ILU", "The tpye of preconditioner to use");
108  Prec_List.set("Drop Tolerance", 1e-3,
109  "The tolerance below which entries from the\n""factorization are left out of the factors.");
110 
111  // The parameter list can be queried about the existance of a parameter, sublist, or type:
112  // Has a solver been chosen?
113  bool solver_defined = false, prec_defined = false, dtol_double = false;
114  solver_defined = myPL.isParameter("Solver");
115  TEUCHOS_ASSERT_EQUALITY(solver_defined, true);
116  // Has a preconditioner been chosen?
117  prec_defined = myPL.isSublist("Preconditioner");
118  TEUCHOS_ASSERT_EQUALITY(prec_defined, true);
119  // Has a tolerance been chosen and is it a double-precision number?
120  bool tol_double = false;
121  tol_double = myPL.INVALID_TEMPLATE_QUALIFIER isType<double>("Tolerance");
122  TEUCHOS_ASSERT_EQUALITY(tol_double, false); // It is 'float'!
123  // Has a drop tolerance been chosen and is it a double-precision number?
124  dtol_double = Teuchos::isParameterType<double>(Prec_List, "Drop Tolerance");
125  TEUCHOS_ASSERT_EQUALITY(dtol_double, true);
126 
127  // Parameters can be retrieved from the parameter list in quite a few ways:
128  // Get method that creates and sets the parameter if it doesn't exist.
129  int its = -1;
130  its = myPL.get("Max Iters", 1200);
131  TEUCHOS_ASSERT_EQUALITY(its, 1550); // Was already ste
132  // Get method that retrieves a parameter of a particular type that must exist.
133  float tol = -1.0;
134  tol = myPL.get<float>("Tolerance");
135  TEUCHOS_ASSERT_EQUALITY(tol, as<float>(1e-10));
136  // Get the "Solver" value and validate!
137  std::string
138  solver = solverValidator->validateString(
139  Teuchos::getParameter<std::string>(myPL,"Solver")
140  );
141 
142  // We can use this same syntax to get arrays out, like the initial guess.
143  Array<double> init_guess = myPL.get<Array<double> >("Initial Guess");
144 
145  std::cout << "\n# Printing this parameter list using opeator<<(...) ...\n\n";
146  std::cout << myPL << std::endl;
147 
148  std::cout << "\n# Printing the parameter list only showing documentation fields ...\n\n";
149  myPL.print(std::cout,
150  ParameterList::PrintOptions().showDoc(true).indent(2).showTypes(true));
151 
152  /* It is important to note that mispelled parameters
153  (with additional space characters, capitalizations, etc.) may be ignored.
154  Therefore, it is important to be aware that a given parameter has not been used.
155  Unused parameters can be printed with method:
156  */
157  std::cout << "\n# Showing unused parameters ...\n\n";
158  myPL.unused( std::cout );
159 
160  return 0;
161 
162 }
int main(int argc, char *argv[])
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
std::string Teuchos_Version()
A list of parameters of arbitrary type.
TypeTo as(const TypeFrom &t)
Convert from one value type to another.
Definition: Teuchos_as.hpp:287
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.
Memory-safe templated array class that encapsulates std::vector.