Teuchos Package Browser (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
parameterlist/example/ParameterList/cxx_main.cpp
Go to the documentation of this file.
00001 /*
00002 // @HEADER
00003 // ***********************************************************************
00004 //
00005 //                    Teuchos: Common Tools Package
00006 //                 Copyright (2004) Sandia Corporation
00007 //
00008 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00009 // license for use of this work by or on behalf of the U.S. Government.
00010 //
00011 // Redistribution and use in source and binary forms, with or without
00012 // modification, are permitted provided that the following conditions are
00013 // met:
00014 //
00015 // 1. Redistributions of source code must retain the above copyright
00016 // notice, this list of conditions and the following disclaimer.
00017 //
00018 // 2. Redistributions in binary form must reproduce the above copyright
00019 // notice, this list of conditions and the following disclaimer in the
00020 // documentation and/or other materials provided with the distribution.
00021 //
00022 // 3. Neither the name of the Corporation nor the names of the
00023 // contributors may be used to endorse or promote products derived from
00024 // this software without specific prior written permission.
00025 //
00026 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00027 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00028 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00029 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00030 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00031 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00032 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00033 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00034 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00035 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00036 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037 //
00038 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00039 //
00040 // ***********************************************************************
00041 // @HEADER
00042 */
00043 
00044 #include "Teuchos_ParameterList.hpp"
00045 #include "Teuchos_StandardParameterEntryValidators.hpp"
00046 #include "Teuchos_Array.hpp"
00047 #include "Teuchos_Version.hpp"
00048 #include "Teuchos_as.hpp"
00049 #include "Teuchos_StandardCatchMacros.hpp"
00050 
00051 int main(int argc, char* argv[])
00052 {
00053   using Teuchos::ParameterList;
00054   using Teuchos::RCP;
00055   using Teuchos::Array;
00056   using Teuchos::tuple;
00057   using Teuchos::as;
00058 
00059   bool success = false;
00060   bool verbose = true;
00061   try {
00062 
00063     std::cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00064 
00065     // Creating an empty parameter list looks like:
00066     ParameterList myPL;
00067 
00068     // Setting parameters in this list can be easily done:
00069     myPL.set("Max Iters", 1550, "Determines the maximum number of iterations in the solver");
00070     myPL.set("Tolerance", 1e-10, "The tolerance used for the convergence check");
00071 
00072     // For the "Solver" option, create a validator that will automatically
00073     // create documentation for this parameter but will also help in validation.
00074     RCP<Teuchos::StringToIntegralParameterEntryValidator<int> >
00075       solverValidator = Teuchos::rcp(
00076           new Teuchos::StringToIntegralParameterEntryValidator<int>(
00077             Teuchos::tuple<std::string>( "GMRES", "CG", "TFQMR" )
00078             ,"Solver"
00079             )
00080           );
00081     myPL.set(
00082         "Solver"
00083         ,"GMRES" // This will be validated by solverValidator right here!
00084         ,"The type of solver to use"
00085         ,solverValidator
00086         );
00087 
00088     /* The templated ``set'' method should cast the input {\it value} to the
00089        correct data type.  However, in the case where the compiler is not casting the input
00090        value to the expected data type, an explicit cast can be used with the ``set'' method:
00091        */
00092     myPL.set("Tolerance", as<float>(1e-10), "The tolerance used for the convergence check");
00093 
00094     /* Reference-counted pointers can also be passed through a ParameterList.
00095        To illustrate this we will use the Array class to create an array of 10 doubles
00096        representing an initial guess for a linear solver, whose memory is being managed by a
00097        RCP.
00098        */
00099 
00100     myPL.set<Array<double> >("Initial Guess", tuple<double>( 10, 0.0 ),
00101         "The initial guess as a RCP to an array object.");
00102 
00103     /* A hierarchy of parameter lists can be constructed using {\tt ParameterList}.  This
00104        means another parameter list is a valid {\it value} in any parameter list.  To create a sublist
00105        in a parameter list and obtain a reference to it:
00106        */
00107     ParameterList& Prec_List = myPL.sublist("Preconditioner", false,
00108         "Sublist that defines the preconditioner.");
00109 
00110     // Now this parameter list can be filled with values:
00111     Prec_List.set("Type", "ILU", "The tpye of preconditioner to use");
00112     Prec_List.set("Drop Tolerance", 1e-3,
00113         "The tolerance below which entries from the\n""factorization are left out of the factors.");
00114 
00115     // The parameter list can be queried about the existance of a parameter, sublist, or type:
00116     // Has a solver been chosen?
00117     bool solver_defined = false, prec_defined = false, dtol_double = false;
00118     solver_defined = myPL.isParameter("Solver");
00119     TEUCHOS_ASSERT_EQUALITY(solver_defined, true);
00120     // Has a preconditioner been chosen?
00121     prec_defined = myPL.isSublist("Preconditioner");
00122     TEUCHOS_ASSERT_EQUALITY(prec_defined, true);
00123     // Has a tolerance been chosen and is it a double-precision number?
00124     bool tol_double = false;
00125     tol_double = myPL.INVALID_TEMPLATE_QUALIFIER isType<double>("Tolerance");
00126     TEUCHOS_ASSERT_EQUALITY(tol_double, false); // It is 'float'!
00127     // Has a drop tolerance been chosen and is it a double-precision number?
00128     dtol_double = Teuchos::isParameterType<double>(Prec_List, "Drop Tolerance");
00129     TEUCHOS_ASSERT_EQUALITY(dtol_double, true);
00130 
00131     // Parameters can be retrieved from the parameter list in quite a few ways:
00132     // Get method that creates and sets the parameter if it doesn't exist.
00133     int its = -1;
00134     its = myPL.get("Max Iters", 1200);
00135     TEUCHOS_ASSERT_EQUALITY(its, 1550); // Was already ste
00136     // Get method that retrieves a parameter of a particular type that must exist.
00137     float tol = -1.0;
00138     tol = myPL.get<float>("Tolerance");
00139     TEUCHOS_ASSERT_EQUALITY(tol, as<float>(1e-10));
00140     // Get the "Solver" value and validate!
00141     std::string
00142       solver = solverValidator->validateString(
00143           Teuchos::getParameter<std::string>(myPL,"Solver")
00144           );
00145 
00146     // We can use this same syntax to get arrays out, like the initial guess.
00147     Array<double> init_guess = myPL.get<Array<double> >("Initial Guess");
00148 
00149     std::cout << "\n# Printing this parameter list using opeator<<(...) ...\n\n";
00150     std::cout << myPL << std::endl;
00151 
00152     std::cout << "\n# Printing the parameter list only showing documentation fields ...\n\n";
00153     myPL.print(std::cout,
00154         ParameterList::PrintOptions().showDoc(true).indent(2).showTypes(true));
00155 
00156     /* It is important to note that mispelled parameters
00157        (with additional space characters, capitalizations, etc.) may be ignored.
00158        Therefore, it is important to be aware that a given parameter has not been used.
00159        Unused parameters can be printed with method:
00160        */
00161     std::cout << "\n# Showing unused parameters ...\n\n";
00162     myPL.unused( std::cout );
00163 
00164     success = true;
00165   }
00166   TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success);
00167   return ( success ? EXIT_SUCCESS : EXIT_FAILURE );
00168 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines