Teuchos Package Browser (Single Doxygen Collection) Version of the Day
test/ParameterList/cxx_main.cpp
Go to the documentation of this file.
00001 // @HEADER
00002 // ***********************************************************************
00003 //
00004 //                    Teuchos: Common Tools Package
00005 //                 Copyright (2004) Sandia Corporation
00006 //
00007 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
00008 // license for use of this work by or on behalf of the U.S. Government.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Michael A. Heroux (maherou@sandia.gov)
00038 //
00039 // ***********************************************************************
00040 // @HEADER
00041 
00042 #include "Teuchos_ParameterList.hpp"
00043 #include "Teuchos_GlobalMPISession.hpp"
00044 #include "Teuchos_CommandLineProcessor.hpp"
00045 #include "Teuchos_getConst.hpp"
00046 #include "Teuchos_Version.hpp"
00047 #include "Teuchos_StandardCatchMacros.hpp"
00048 #include "Teuchos_FancyOStream.hpp"
00049 #include "Teuchos_ArrayRCP.hpp"
00050 #include "Teuchos_StandardParameterEntryValidators.hpp"
00051 
00052 #ifdef HAVE_TEUCHOS_EXTENDED
00053 #include "Teuchos_XMLParameterListHelpers.hpp"
00054 #endif
00055 
00056 #ifdef HAVE_MPI
00057 #include "mpi.h"
00058 #endif
00059 
00060 using Teuchos::CommandLineProcessor;
00061 using Teuchos::ParameterList;
00062 using Teuchos::getParameter;
00063 typedef ParameterList::PrintOptions PLPrintOptions;
00064 using Teuchos::ParameterEntry;
00065 using Teuchos::OSTab;
00066 using Teuchos::rcp;
00067 
00068 void print_break() { std::cout << "---------------------------------------------------" << std::endl; }
00069 double Plus ( double a, double b ) { return a+b; }
00070 
00071 int main( int argc, char *argv[] )
00072 {
00073 
00074   using std::cout;
00075   using std::endl;
00076 
00077   bool verbose = true;
00078   int FailedTests = 0;
00079   bool result;
00080 
00081   Teuchos::GlobalMPISession mpiSession(&argc,&argv);
00082   const int procRank = Teuchos::GlobalMPISession::getRank();
00083 
00084   bool success = true;
00085 
00086   try {
00087 
00088   // Read options from the command line. 
00089   CommandLineProcessor  clp(false); // Don't throw exceptions
00090   clp.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not." );
00091   CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
00092   if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
00093     cout << "Processor "<< procRank <<", parse_return "<< parse_return << std::endl;
00094     cout << "End Result: TEST FAILED" << std::endl;
00095     return parse_return;
00096   }
00097 
00098   // Only print on 0 processor
00099   if (procRank != 0 && verbose)
00100     verbose = false;
00101 
00102   if (verbose)
00103     cout << Teuchos::Teuchos_Version() << std::endl << std::endl;
00104 
00105   //-----------------------------------------------------------
00106   // Create Main Parameter List / Sublist Structure
00107   //-----------------------------------------------------------
00108 
00109   ParameterList PL_Main("PL_Main");
00110   const std::string Direction_Doc = "This sublist controls how direction is computed.";
00111   ParameterList& PL_Direction = PL_Main.sublist("Direction",false,Direction_Doc);
00112   ParameterList& PL_Newton = PL_Direction.sublist("Newton");
00113   ParameterList& PL_LinSol = PL_Newton.sublist("Linear Solver");
00114   ParameterList& PL_LineSearch = PL_Main.sublist("Line Search");
00115 
00116   //-----------------------------------------------------------
00117   // Check Parameter List Structure
00118   //-----------------------------------------------------------
00119   if (verbose) {
00120     print_break();
00121     cout << "Empty Parameter List Structure" << std::endl;
00122     print_break();
00123     cout<<PL_Main<< std::endl;
00124   }
00125   if (verbose) cout << "Is 'Direction' recognized as a sublist of 'Main' ... ";
00126   if ( PL_Main.isSublist( "Direction" ) ) {  
00127     if (verbose) cout << "yes"<< std::endl;
00128   } else {
00129     if (verbose) cout << "no"<< std::endl;
00130     FailedTests++;        
00131   }
00132   if (verbose) cout << "Is 'Newton' recognized as a sublist of 'Direction' ... ";
00133   if ( PL_Direction.isSublist( "Newton" ) ) {  
00134     if (verbose) cout << "yes"<< std::endl;
00135   } else {
00136     if (verbose) cout << "no"<< std::endl;
00137     FailedTests++;        
00138   }
00139   if (verbose) cout << "Is 'Linear Solver' recognized as a sublist of 'Newton' ... ";
00140   if ( PL_Newton.isSublist( "Linear Solver" ) ) {  
00141     if (verbose) cout << "yes"<< std::endl;
00142   } else {
00143     if (verbose) cout << "no"<< std::endl;
00144     FailedTests++;        
00145   }
00146   if (verbose) cout << "Is 'Line Search' recognized as a sublist of 'Main' ... ";
00147   if ( PL_Main.isSublist( "Line Search" ) ) {  
00148     if (verbose) cout << "yes"<< std::endl;
00149   } else {
00150     if (verbose) cout << "no"<< std::endl;
00151     FailedTests++;        
00152   }
00153 
00154   if (verbose) cout << "Is subist documentation std::string maintained ...\n";
00155   {
00156     Teuchos::OSTab tab(cout);
00157     const Teuchos::ParameterEntry
00158       *paramEntry = PL_Main.getEntryPtr("Direction");
00159     TEST_FOR_EXCEPT(0==paramEntry);
00160     const std::string extracted_Direction_Doc = paramEntry->docString();
00161     if (verbose) tab.o() << "Expected doc std::string = \"" << Direction_Doc << "\"\n";
00162     if (verbose) tab.o() << "Extracted doc std::string = \"" << extracted_Direction_Doc << "\"\n";
00163     if (extracted_Direction_Doc == Direction_Doc) {
00164       if (verbose) tab.o() << "passed!  They match :-)\n";
00165     }
00166     else {
00167       if (verbose) tab.o() << "failed!  They do not match :-("<< std::endl;
00168       FailedTests++;        
00169     }
00170   }
00171   
00172 
00173   //-----------------------------------------------------------
00174   // Fill in Direction Sublist
00175   //-----------------------------------------------------------
00176 
00177   double tol = 0.0;
00178   bool RBNS = false;
00179   PL_Direction.get("Method", "Newton");
00180   PL_LinSol.set("Tol",1e-5);
00181   tol = PL_LinSol.get("Tolerance",1e-10);
00182   RBNS = PL_Newton.get("Rescue Bad Newton Solve", true );
00183 
00184   //-----------------------------------------------------------
00185   // Print out Direction Sublist
00186   //-----------------------------------------------------------
00187   if (verbose) {
00188     print_break();
00189     cout << "Direction Parameter List" << std::endl;
00190     print_break();
00191     PL_Direction.print(cout);
00192   }
00193   if (verbose) cout << "Is 'Newton' recognized as a parameter of 'Direction' ... ";
00194   if ( PL_Direction.isParameter( "Newton" ) ) {  
00195     if (verbose) cout << "yes"<< std::endl;
00196   } else {
00197     if (verbose) cout << "no"<< std::endl;
00198     FailedTests++;        
00199   }
00200   if (verbose) cout << "Is 'Tolerance' recognized as a parameter of 'Newton' ... ";
00201   if ( PL_Newton.isParameter( "Tolerance" ) ) {  
00202     if (verbose) cout << "yes (should be no)"<< std::endl;
00203     FailedTests++;
00204   } else {
00205     if (verbose) cout << "no (as expected)"<< std::endl;
00206   }
00207   if (verbose) cout << "Is 'Tolerance' recognized as a parameter of 'Linear Solver' ... ";
00208   if ( PL_LinSol.isParameter( "Tolerance" ) ) {  
00209     if (verbose) cout << "yes"<< std::endl;
00210   } else {
00211     if (verbose) cout << "no"<< std::endl;
00212     FailedTests++;        
00213   }
00214   if (verbose) cout << "Is 'Rescue Bad Newton Solve' recognized as a parameter of 'Newton' ... ";
00215   if ( PL_Newton.isParameter( "Rescue Bad Newton Solve" ) ) {  
00216     if (verbose) cout << "yes"<< std::endl;
00217   } else {
00218     if (verbose) cout << "no"<< std::endl;
00219     FailedTests++;
00220   }
00221 
00222   //-----------------------------------------------------------
00223   // Line Search Sublist 
00224   // (if there are no failures, this will be constructed and added)
00225   //-----------------------------------------------------------
00226   if (!FailedTests) {
00227     int ARI = 0, default_step = 0, max_iter_inc = 0, rec_step = 0;
00228     double alpha_factor = 0.0, min_bnds_factor = 0.0, max_bnds_factor = 0.0;
00229     bool force_interp = true, use_cntrs = false;
00230     std::string ls_method = "Polynomial";
00231     // This is to force a char* to be passed into the get method to see if the template
00232     // specialization for char* is working.
00233     char* ls_method_char = const_cast<char *>(ls_method.c_str());
00234     ParameterList PL_My_LineSearch("PL_My_LineSearch");
00235     ls_method = PL_My_LineSearch.get("Method", ls_method_char);
00236     ParameterList& PL_Polynomial = PL_My_LineSearch.sublist("Polynomial");
00237     ARI = PL_Polynomial.get("Allowed Relative Increase", 100 );
00238     alpha_factor = PL_Polynomial.get("Alpha Factor", 0.0001 );
00239     default_step = PL_Polynomial.get("Default Step", 1 );
00240     force_interp = PL_Polynomial.get("Force Interpolation", false );
00241     std::string interp_type = PL_Polynomial.get("Interpolation Type", "Cubic" );
00242     max_bnds_factor = PL_Polynomial.get("Max Bounds Factor", 0.5 );
00243     PL_Polynomial.set("Max Iters", 3 );
00244     max_iter_inc = PL_Polynomial.get("Maximum Iteration for Increase", 0 );
00245     min_bnds_factor = PL_Polynomial.get("Min Bounds Factor", 0.1 );
00246     rec_step = PL_Polynomial.get("Recovery Step", 1 );
00247     std::string rec_step_type = PL_Polynomial.get("Recovery Step Type", "Constant");
00248     std::string suff_dec_cond = PL_Polynomial.get("Sufficient Decrease Condition", "Armijo-Goldstein" );
00249     use_cntrs = PL_Polynomial.get("Use Counters", true );
00250     PL_Main.set("Nonlinear Solver", "Line Search Based");
00251 
00252     //-----------------------------------------------------------
00253     // Set the Line Search Parameter List equal to the one just constructed
00254     //-----------------------------------------------------------
00255     PL_LineSearch.setParameters(PL_My_LineSearch);
00256     ParameterList& PL_My_Polynomial = PL_LineSearch.sublist("Polynomial");
00257     if (verbose) cout<< "Is 'operator=' functional ... ";
00258     if ( PL_My_Polynomial.isParameter("Recovery Step Type") ) {
00259       if (verbose) cout<< "yes" << std::endl;
00260     } else {
00261       if (verbose) cout<< "no" << std::endl;
00262       FailedTests++;
00263     }
00264 
00265     //-----------------------------------------------------------
00266     // Set Copying of parameter sublists and names
00267     //-----------------------------------------------------------
00268 
00269     if (verbose) {
00270       print_break();
00271       if (verbose) cout << "Test copying of sublist\n";
00272       print_break();
00273       PL_Direction.print(cout);
00274     }
00275     {
00276       const ParameterList
00277         &linearSolverPL = PL_Main.sublist("Direction").sublist("Newton").sublist("Line Search");
00278       const ParameterList
00279         linearSolverPL_copy(linearSolverPL);
00280       if (verbose) cout << "linearSolverPL.name() = " << linearSolverPL.name() << endl;
00281       if (verbose) cout << "linearSolverPL_copy.name() = " << linearSolverPL_copy.name() << endl;
00282       if (verbose) cout << "linearSolverPL_copy == linearSolverPL.name() : ";
00283       if (linearSolverPL_copy == linearSolverPL.name()) {
00284         if (verbose) cout << "passed" << endl;
00285       }
00286       else {
00287         if (verbose) cout << "failed" << endl;
00288         FailedTests++;
00289       }
00290     }
00291 
00292     if (verbose) {
00293       print_break();
00294       if (verbose) cout << "General tests\n";
00295       print_break();
00296       PL_Direction.print(cout);
00297     }
00298 
00299     ParameterList Copied_PL_Main(PL_Main);
00300 
00301     if (verbose) cout << "Copied_PL_Main.name() == PL_Main.name() : ";
00302     if (Copied_PL_Main.name() == PL_Main.name()) {
00303       if (verbose) cout << "passed" << endl;
00304     }
00305     else {
00306       if (verbose) cout << "failed" << endl;
00307       FailedTests++;
00308       if (verbose) cout << "Copyed_PL_Main.name() = " << Copied_PL_Main.name() << endl;
00309     }
00310 
00311     if (verbose) cout<< "Is the copy constructor functional ... ";
00312     if ( Copied_PL_Main.isParameter("Nonlinear Solver") ) {
00313       if (verbose) cout<< "yes" << std::endl;
00314     } else {
00315       if (verbose) cout<< "no" << std::endl;
00316       FailedTests++;
00317     }  
00318 
00319     bool tempMeth = true;
00320 
00321     //-----------------------------------------------------------
00322     // Check the templated 'get' method.
00323     //-----------------------------------------------------------
00324     //
00325     //-----------------------------------------------------------
00326     // Retrieve some information from the parameter list using templated "get" method.
00327     // (This will be tested using the INVALID_TEMPLATE_QUALIFIER which indicates whether a
00328     //  non-templated code needs ".template" before the method name )
00329     //-----------------------------------------------------------
00330     int max_iters = 0, max_iters_again = 0;
00331     std::string nonlin_solver;
00332     tempMeth = true;
00333     try {
00334       max_iters = PL_My_Polynomial.INVALID_TEMPLATE_QUALIFIER get<int>("Max Iters");
00335       max_iters_again = Teuchos::getConst(PL_My_Polynomial).INVALID_TEMPLATE_QUALIFIER get<int>("Max Iters");
00336       nonlin_solver = PL_Main.INVALID_TEMPLATE_QUALIFIER get<std::string>("Nonlinear Solver");
00337     }
00338     catch( const Teuchos::Exceptions::InvalidParameter&) { tempMeth = false; }  
00339     if (verbose) {
00340       cout<< "Is the templated 'get' method functional ... "<<std::endl;
00341       cout<< "  Can we retrieve information using the CORRECT variable type ... ";
00342     }
00343     if (tempMeth && max_iters==3) { if (verbose) cout << "yes" << std::endl; }
00344     else { if (verbose) cout << "no" << std::endl; FailedTests++; }
00345     if (verbose) {
00346       cout<< "  Can we retrieve const information using the CORRECT variable type ... ";
00347     }
00348     if (tempMeth && max_iters_again==3) { if (verbose) cout << "yes" << std::endl; }
00349     else { if (verbose) cout << "no" << std::endl; FailedTests++; }
00350 
00351     //-----------------------------------------------------------
00352     // Retrieve some information from the parameter list that we know is a bad "get".
00353     // (This will be tested using the INVALID_TEMPLATE_QUALIFIER which indicates whether a
00354     //  non-templated code needs ".template" before the method name )
00355     //-----------------------------------------------------------
00356     float mbf = 0.0;
00357     tempMeth = false;
00358     try {
00359       mbf = PL_LinSol.INVALID_TEMPLATE_QUALIFIER get<float>( "Tol" );
00360       FailedTests++;
00361     }
00362     catch( const Teuchos::Exceptions::InvalidParameter&) {
00363       tempMeth = true;
00364     }
00365     if (verbose) {
00366       cout<< "  Can we retrieve information using the WRONG variable type ... ";
00367     }
00368     if (tempMeth) { if (verbose) cout << "no" << std::endl; }
00369     else { if (verbose) cout << "yes" << std::endl; }
00370 
00371     //-----------------------------------------------------------
00372     // Retrieve some information from the parameter list using templated "get" method.
00373     // (This will be tested using the INVALID_TEMPLATE_QUALIFIER which indicates whether a
00374     //  non-templated code needs ".template" before the method name )
00375     //-----------------------------------------------------------
00376     tempMeth = true;
00377     try {
00378       max_iters = PL_My_Polynomial.INVALID_TEMPLATE_QUALIFIER get<int>("Max Iters");
00379       nonlin_solver = PL_Main.INVALID_TEMPLATE_QUALIFIER get<std::string>("Nonlinear Solver");
00380     }
00381     catch( const Teuchos::Exceptions::InvalidParameter&) { tempMeth = false; }  
00382     if (verbose) {
00383       cout<< "Is the templated 'get' method functional ... "<<std::endl;
00384       cout<< "  Can we retrieve information using the CORRECT variable type ... ";
00385     }
00386     if (tempMeth && max_iters==3) { if (verbose) cout << "yes" << std::endl; }
00387     else { if (verbose) cout << "no" << std::endl; FailedTests++; }
00388 
00389     //-----------------------------------------------------------
00390     // Retrieve some information from the parameter list that we know is a bad "get".
00391     //-----------------------------------------------------------
00392     tempMeth = false;
00393     try {
00394       mbf = PL_LinSol.INVALID_TEMPLATE_QUALIFIER get<float>( "Tol" );
00395       FailedTests++;
00396     }
00397     catch( const Teuchos::Exceptions::InvalidParameter&) {
00398       tempMeth = true;
00399     }
00400     if (verbose) {
00401       cout<< "  Can we retrieve information using the WRONG variable type ... ";
00402     }
00403     if (tempMeth) { if (verbose) cout << "no" << std::endl; }
00404     else { if (verbose) cout << "yes" << std::endl; }
00405 
00406     //-----------------------------------------------------------
00407     // Check the templated 'getPtr' method.
00408     //-----------------------------------------------------------
00409 
00410     //-----------------------------------------------------------
00411     // Retrieve some information from the parameter list using templated "get" method.
00412     // (This will be tested using the INVALID_TEMPLATE_QUALIFIER which indicates whether a
00413     //  non-templated code needs ".template" before the method name )
00414     //-----------------------------------------------------------
00415     int *max_iters_ptr = 0;
00416     const int *max_iters_ptr_again = 0;
00417     std::string* nonlin_solver_ptr;
00418 
00419     max_iters_ptr = PL_My_Polynomial.INVALID_TEMPLATE_QUALIFIER getPtr<int>("Max Iters");
00420     max_iters_ptr_again = Teuchos::getConst(PL_My_Polynomial).INVALID_TEMPLATE_QUALIFIER getPtr<int>("Max Iters");
00421     nonlin_solver_ptr = PL_Main.INVALID_TEMPLATE_QUALIFIER getPtr<std::string>("Nonlinear Solver");
00422 
00423     if (verbose) {
00424       cout<< "Is the templated 'getPtr' method functional ... "<<std::endl;
00425       cout<< "  Can we retrieve information using the CORRECT variable type ... ";
00426     }
00427     if (max_iters_ptr) {
00428       if ((*max_iters_ptr)==3) {
00429         if (verbose) cout << "yes" << std::endl; 
00430       }
00431       else { if (verbose) cout << "no" << std::endl; FailedTests++; }
00432     }
00433     if (verbose) {
00434       cout<< "  Can we retrieve const information using the CORRECT variable type ... ";
00435     }
00436     if (max_iters_ptr_again) {
00437       if ((*max_iters_ptr_again)==3) {
00438         if (verbose) cout << "yes" << std::endl; 
00439       }
00440       else { if (verbose) cout << "no" << std::endl; FailedTests++; }
00441     }
00442 
00443     //-----------------------------------------------------------
00444     // Retrieve some information from the parameter list that we know is a bad "get".
00445     // (This will be tested using the INVALID_TEMPLATE_QUALIFIER which indicates whether a
00446     //  non-templated code needs ".template" before the method name )
00447     //-----------------------------------------------------------
00448     float* mbf_ptr = 0;
00449 
00450     mbf_ptr = PL_LinSol.INVALID_TEMPLATE_QUALIFIER getPtr<float>( "Tol" );
00451 
00452     if (mbf_ptr)
00453       ++FailedTests;        
00454 
00455     if (verbose) {
00456       cout<< "  Can we retrieve information using the WRONG variable type ... ";
00457     }
00458     if (!mbf_ptr) { if (verbose) cout << "no" << std::endl; }
00459     else { if (verbose) cout << "yes" << std::endl; }
00460 
00461     //-----------------------------------------------------------
00462     // Retrieve some information from the parameter list using templated "get" method.
00463     // (This will be tested using the INVALID_TEMPLATE_QUALIFIER which indicates whether a
00464     //  non-templated code needs ".template" before the method name )
00465     //-----------------------------------------------------------
00466 
00467     max_iters_ptr = PL_My_Polynomial.INVALID_TEMPLATE_QUALIFIER getPtr<int>("Max Iters");
00468     nonlin_solver_ptr = PL_Main.INVALID_TEMPLATE_QUALIFIER getPtr<std::string>("Nonlinear Solver");
00469 
00470     if (verbose) {
00471       cout<< "Is the templated 'getPtr' method functional ... "<<std::endl;
00472       cout<< "  Can we retrieve information using the CORRECT variable type ... ";
00473     }
00474     if (max_iters_ptr) {
00475       if ((*max_iters_ptr)==3) {
00476         if (verbose) cout << "yes" << std::endl; 
00477       }
00478       else { if (verbose) cout << "no" << std::endl; FailedTests++; }
00479     }
00480 
00481     //-----------------------------------------------------------
00482     // Retrieve some information from the parameter list that we know is a bad "get".
00483     // (This will be tested using the INVALID_TEMPLATE_QUALIFIER which indicates whether a
00484     //  non-templated code needs ".template" before the method name )
00485     //-----------------------------------------------------------
00486 
00487     mbf_ptr = PL_LinSol.INVALID_TEMPLATE_QUALIFIER getPtr<float>( "Tol" );
00488 
00489     if (mbf_ptr)
00490       ++FailedTests;        
00491 
00492     if (verbose) {
00493       cout<< "  Can we retrieve information using the WRONG variable type ... ";
00494     }
00495     if (!mbf_ptr) { if (verbose) cout << "no" << std::endl; }
00496     else { if (verbose) cout << "yes" << std::endl; }
00497 
00498     //-----------------------------------------------------------
00499     // Check the 'getParameter' helper function.
00500     //-----------------------------------------------------------
00501     int def_step = 0;
00502     double alpha_fact = 0.0;
00503     tempMeth = true;
00504     try {
00505       def_step = Teuchos::getParameter<int>(PL_Polynomial, "Default Step");
00506       alpha_fact = Teuchos::getParameter<double>(PL_Polynomial, "Alpha Factor");
00507     }
00508     catch( const Teuchos::Exceptions::InvalidParameter&) { tempMeth = false; }
00509     if (verbose && def_step==1) {
00510       cout<< "Is the helper function 'getParameter' functional ... ";
00511     }
00512     if (tempMeth) { if (verbose) cout << "yes" << std::endl; }
00513     else { if (verbose) cout << "no" << std::endl; FailedTests++; }
00514 
00515     //-----------------------------------------------------------
00516     // Check templated isType functionality
00517     // (This will be tested using the INVALID_TEMPLATE_QUALIFIER which indicates whether a
00518     //  non-templated code needs ".template" before the method name )
00519     //-----------------------------------------------------------
00520     bool PT1, PT2, PT3;
00521     PT1 = PL_Polynomial.INVALID_TEMPLATE_QUALIFIER isType<int>("Default Step");
00522     PT2 = PL_Polynomial.INVALID_TEMPLATE_QUALIFIER isType<long int>("Default Step");
00523     PT3 = PL_Polynomial.INVALID_TEMPLATE_QUALIFIER isType<std::string>("Interpolation Type");
00524     if (verbose) {
00525       cout<< "Is the templated 'isType' method functional ... "<<std::endl;
00526       cout<< "  Is the 'Default Step' of type 'int' ... ";
00527     }
00528     if (PT1) { if (verbose) cout<< "yes" << std::endl; }
00529     else { if (verbose) cout<< "no" << std::endl; FailedTests++; }
00530     if (verbose) {
00531       cout<< "  Is the 'Default Step' of type 'long int' ... ";
00532     }
00533     if (PT2) { if (verbose) cout<< "yes" << std::endl; FailedTests++; }
00534     else { if (verbose) cout<< "no (as expected)" << std::endl; }
00535     if (verbose) {
00536       cout<< "  Is the 'Interpolation Type' of type 'std::string' ... ";
00537     }
00538     if (PT3) { if (verbose) cout<< "yes" << std::endl; }
00539     else { if (verbose) cout<< "no" << std::endl; FailedTests++; }
00540 
00541     //-----------------------------------------------------------
00542     // Check the 'isParameterType' helper function.
00543     //-----------------------------------------------------------
00544     bool PT4, PT5;
00545     PT4 = Teuchos::isParameterType<double>(PL_Polynomial, "Max Bounds Factor");
00546     PT5 = Teuchos::isParameterType<float>(PL_Polynomial, "Max Bounds Factor");    
00547     if (verbose) {
00548       cout<< "Is the helper function 'isParameterType' functional ... "<<std::endl;
00549       cout<< "  Is the 'Max Bounds Factor' of type 'double' ... ";
00550     }
00551     if (PT4) { if (verbose) cout<< "yes" <<std::endl; }
00552     else { if (verbose) cout<< "no" << std::endl; FailedTests++; }
00553     if (verbose) {
00554       cout<< "  Is the 'Max Bounds Factor' of type 'float' ... ";
00555     }
00556     if (PT5) { if (verbose) cout<< "yes" <<std::endl; FailedTests++; }
00557     else { if (verbose) cout<< "no (as expected)" << std::endl; }
00558 
00559     //-----------------------------------------------------------
00560     // Can we pass a pointer to a std::vector to the parameter list.
00561     //-----------------------------------------------------------
00562     Teuchos::ArrayRCP<double> tempvec1_arcp = Teuchos::arcp<double>(10);
00563     {
00564       double * tempvec1 = tempvec1_arcp.get();
00565       for (int i=0; i<10; i++) { tempvec1[i] = i; }
00566       PL_Main.set( "Address of Norm Vector", tempvec1 );
00567       double* tempvec2 = Teuchos::getParameter<double*>( PL_Main, "Address of Norm Vector" );
00568       tempvec1[4] = 2.0; tempvec1[6] = 1.0;
00569       if (verbose) {
00570         cout<< "Can we pass a pointer to a std::vector to a parameter list ... ";
00571       }
00572       if ((tempvec2[4]-tempvec1[4])!=0.0 || (tempvec2[6]-tempvec1[6])!=0.0) {
00573         if (verbose) { cout<<"no"<<std::endl; }
00574         FailedTests++;
00575       } else {
00576         if (verbose) { cout<<"yes"<<std::endl; }
00577       }
00578     }
00579 
00580     //-----------------------------------------------------------
00581     // We can add Array<T> objects of various types T as std::string parameters!
00582     //-----------------------------------------------------------
00583     if(verbose) {
00584       print_break();
00585       cout << "Setting int and double array objects as std::string parameters ...\n";
00586       print_break();
00587     }
00588     const Teuchos::Array<int>
00589       intArray = Teuchos::tuple<int>(0,1,2,3,4,5,6);
00590     const Teuchos::Array<double>
00591       doubleArray = Teuchos::tuple<double>(0,1.0,2.0,3.0,4.0,5.0,6.0);
00592     //const Teuchos::Array<bool>
00593     //boolArray = Teuchos::tuple<bool>(true,true,false,false);
00594     Teuchos::setStringParameterFromArray("Int Array",intArray,&PL_Main);
00595     Teuchos::setStringParameterFromArray("Double Array",doubleArray,&PL_Main);
00596     //Teuchos::setStringParameterFromArray("Bool Array",boolArray,&PL_Main);
00597     if(verbose) {
00598       print_break();
00599       cout << "Testing retrieval of set array objects ...\n";
00600       print_break();
00601     }
00602     {
00603 
00604       const Teuchos::Array<int>
00605         readIntArray = Teuchos::getArrayFromStringParameter<int>(PL_Main,"Int Array");
00606       result = readIntArray == intArray;
00607       if(!result) ++FailedTests;
00608       if(verbose)
00609         cout
00610           << "readIntArray = " << readIntArray << " == intArray = " << intArray << " ? "
00611           << (result ? "passed" : "failed")
00612           << "\n";
00613 
00614       const Teuchos::Array<int>
00615         readDoubleAsIntArray = Teuchos::getArrayFromStringParameter<int>(PL_Main,"Double Array");
00616       result = readDoubleAsIntArray == intArray;
00617       if(!result) ++FailedTests;
00618       if(verbose)
00619         cout
00620           << "readDoubleAsIntArray = " << readDoubleAsIntArray << " == intArray = " << intArray << " ? "
00621           << (result ? "passed" : "failed")
00622           << "\n";
00623 
00624 /*
00625       const Teuchos::Array<bool>
00626         readBoolArray = Teuchos::getArrayFromStringParameter<bool>(PL_Main,"Bool Array");
00627       result = readBoolArray == boolArray;
00628       if(!result) ++FailedTests;
00629       if(verbose)
00630         cout
00631           << "readBoolArray = " << readBoolArray << " == boolArray = " << boolArray << " ? "
00632           << (result ? "passed" : "failed")
00633           << "\n";
00634 */
00635 
00636       if(verbose) {
00637         print_break();
00638       }
00639       
00640     }
00641 
00642     //-----------------------------------------------------------
00643     // We can directly set the array strings!
00644     //-----------------------------------------------------------
00645 
00646     if(verbose) {
00647       print_break();
00648       cout << "Setting a array of doubles as a std::string parameter directly ...\n";
00649       print_break();
00650     }
00651 
00652     PL_Main.set(
00653       "Double Array"
00654       ,"  {\n"
00655       "      0.00000\n"
00656       "      ,1.0e0\n"
00657       "      ,0.2e1\n"
00658       "      ,30.0e-1\n"
00659       "      ,4\n"
00660       "      ,5.0000\n"
00661       "      ,6\n"
00662       "  }\n  "
00663       );
00664 
00665     {
00666 
00667       const Teuchos::Array<double>
00668         readDoubleArray = Teuchos::getArrayFromStringParameter<double>(PL_Main,"Double Array");
00669       Teuchos::Array<int>
00670         doubleAsIntArray(readDoubleArray.size());
00671       for(int i=0;i<static_cast<int>(readDoubleArray.size());++i)
00672         doubleAsIntArray[i] = static_cast<int>(readDoubleArray[i]);
00673       result = doubleAsIntArray == intArray;
00674       if(!result) ++FailedTests;
00675       if(verbose)
00676         cout
00677           << "doubleAsIntArray = " << doubleAsIntArray << " == intArray = " << intArray << " ? "
00678           << (result ? "passed" : "failed")
00679           << "\n";
00680 
00681       if(verbose) {
00682         print_break();
00683       }
00684       
00685     }
00686 
00687     //-----------------------------------------------------------
00688     // Can we pass a pointer to a function to the parameter list.
00689     // Use a simple function, pass it in and get it back out ...
00690     // ( HKT 03/23/2004 This test is not supported on Janus )
00691     //-----------------------------------------------------------
00692 #ifndef JANUS_STLPORT 
00693     double (*pt2Function) (double, double);
00694     PL_Main.set( "Address to Simple Function", &Plus );
00695     pt2Function = Teuchos::getParameter<double(*)(double,double)>( PL_Main, "Address to Simple Function" ); 
00696     if (verbose) {
00697       cout<< "Can we pass a pointer to a function to a parameter list ... ";
00698     }
00699     if ( pt2Function( 1.0, 2.0 ) != 3.0 ) {
00700       if (verbose) cout<<"no"<<std::endl;
00701       FailedTests++;
00702     } else {
00703       if (verbose) cout<<"yes"<<std::endl;
00704     }    
00705 #endif
00706   }
00707 
00708   //-----------------------------------------------------------
00709   // We can store and retrieve void* pointers!
00710   //-----------------------------------------------------------
00711   
00712   {
00713     ParameterList pl;
00714     int someInt = 1;
00715     void *someIntPtr = &someInt;
00716     pl.set("Some Pointer", someIntPtr);
00717     void *someIntPtrRtn = getParameter<void*>(pl, "Some Pointer");
00718     TEST_FOR_EXCEPT(someIntPtrRtn != someIntPtr);
00719     if (verbose)
00720       cout << "someIntPtrRtn = " << someIntPtrRtn << " == " << someIntPtr << " : ";
00721     if (someIntPtrRtn == someIntPtr) {
00722       if (verbose) cout << "passed\n";
00723     }
00724     else {
00725       if (verbose) cout << "failed\n";
00726       FailedTests++;
00727     }
00728   }
00729 
00730   //-----------------------------------------------------------
00731   // Print using the public iterators
00732   // KL - 7 August 2004
00733   //-----------------------------------------------------------
00734   ParameterList::ConstIterator iter;
00735   
00736   if (verbose) 
00737   {
00738     print_break();
00739     cout << " printing using public iterators " 
00740          << std::endl;
00741     print_break();
00742   }
00743   for (iter = PL_Main.begin(); iter != PL_Main.end(); ++iter)
00744   {
00745     const ParameterEntry& val = PL_Main.entry(iter);
00746     const std::string& name = PL_Main.name(iter);
00747     if (val.isList())
00748     {
00749       if (verbose) cout << name << std::endl;
00750       const ParameterList& sublist = Teuchos::getValue<ParameterList>(val);
00751       ParameterList::ConstIterator i;
00752       for (i=sublist.begin(); i != sublist.end(); ++i)
00753       {
00754         const std::string& nm = sublist.name(i);              
00755         const ParameterEntry& v = sublist.entry(i);
00756         if (v.isList())
00757         {
00758           if (verbose) cout << "  " << nm << std::endl;
00759           if (verbose) Teuchos::getValue<ParameterList>(v).print(cout, 6);
00760         }
00761         else
00762         {
00763           if (verbose) cout << "  " << nm << " " << v << std::endl;
00764         }
00765       }
00766     }
00767     else
00768     {
00769       if (verbose) cout << name << " " << val << std::endl;
00770     }
00771   }
00772 
00773 
00774 #if defined(HAVE_TEUCHOS_EXTENDED)
00775 
00776   try {
00777 
00778     if (verbose) {
00779 
00780       print_break();
00781       cout << "writing to XML std::ostream" << std::endl;
00782       print_break();
00783       writeParameterListToXmlOStream(PL_Main,cout);
00784 
00785       print_break();
00786       cout << "writing to XML file" << std::endl;
00787       print_break();
00788       writeParameterListToXmlFile(PL_Main,"PL_Main.xml");
00789 
00790       print_break();
00791       cout << "reading from XML file" << std::endl;
00792       print_break();
00793       ParameterList readBack;
00794       updateParametersFromXmlFile("PL_Main.xml",&readBack);
00795       if (verbose) readBack.print(cout);
00796 
00797       print_break();
00798       cout << "reading from XML std::string" << std::endl;
00799       print_break();
00800       std::ifstream xmlInFile("PL_Main.xml");
00801       std::string xmlStr;
00802       while(!xmlInFile.eof()) {
00803         std::string line;
00804         std::getline(xmlInFile,line);
00805         xmlStr += line + "\n";
00806       }
00807       readBack = ParameterList();
00808       updateParametersFromXmlString(xmlStr,&readBack);
00809       if (verbose) readBack.print(cout);
00810 
00811     }
00812 
00813   }
00814   catch(const std::exception& e)
00815   {
00816     if(verbose) {
00817       std::cerr << "caught std::exception:\n\n";
00818       OSTab tab(std::cerr);
00819       std::cerr << e.what() << std::endl;
00820     }
00821     FailedTests++;
00822   }
00823 
00824 #endif // defined(HAVE_TEUCHOS_EXTENDED)
00825 
00826   //-----------------------------------------------------------
00827   // Print out main list
00828   //-----------------------------------------------------------
00829 
00830   if (verbose) {
00831     print_break();
00832     cout << "The Final Parameter List" << std::endl;
00833     print_break();
00834     PL_Main.print(cout);
00835     print_break();
00836     cout << "The unused parameters" << std::endl;
00837     PL_Main.unused(cout);
00838   }
00839 
00840   //-----------------------------------------------------------
00841   // Show error outputs
00842   //-----------------------------------------------------------
00843 
00844   if (verbose) {
00845     print_break();
00846     cout << "Accessing a sublist using the wrong name (should throw a Teuchos::Exceptions::InvalidParameterName std::exception)...\n";
00847     print_break();
00848   }
00849   try {
00850     PL_Main.sublist("Direction").sublist("Newton").sublist("Linear Solvers",true);
00851     if (verbose) cout << "Did not throw std::exception, error!\n";
00852     ++FailedTests;
00853   }
00854   catch(const Teuchos::Exceptions::InvalidParameterName &e) {
00855     std::cerr << "caught expected Teuchos::Exceptions::InvalidParameterName:\n\n";
00856     OSTab tab(std::cerr);
00857     std::cerr << e.what() << std::endl;
00858   }
00859   if (verbose) {
00860     print_break();
00861     cout << "Accessing a parameter using the wrong name (should throw a Teuchos::Exceptions::InvalidParameterName std::exception)...\n";
00862     print_break();
00863   }
00864   try {
00865     Teuchos::getParameter<int>(PL_Main.sublist("Direction").sublist("Newton").sublist("Linear Solver"),"Tolerances");
00866     if (verbose) cout << "Did not throw std::exception, error!\n";
00867     ++FailedTests;
00868   }
00869   catch(const Teuchos::Exceptions::InvalidParameterName &e) {
00870     if(verbose) {
00871       std::cerr << "caught expected Teuchos::Exceptions::InvalidParameterName:\n\n";
00872       OSTab tab(std::cerr);
00873       std::cerr << e.what() << std::endl;
00874     }
00875   }
00876   catch(const std::exception &e) {
00877     if(verbose) {
00878       std::cerr << "caught unexpected std::exception:\n\n";
00879       OSTab tab(std::cerr);
00880       std::cerr << e.what() << std::endl;
00881     }
00882     ++FailedTests;
00883   }
00884 
00885   if (verbose) {
00886     print_break();
00887     cout << "Accessing a parameter using the wrong parameter type (should throw a Teuchos::Exceptions::InvalidParameterType std::exception)...\n";
00888     print_break();
00889   }
00890   try {
00891     Teuchos::getParameter<int>(PL_Main.sublist("Direction").sublist("Newton").sublist("Linear Solver"),"Tolerance");
00892     if (verbose) cout << "Did not throw std::exception, error!\n";
00893     ++FailedTests;
00894   }
00895   catch(const Teuchos::Exceptions::InvalidParameterType &e) {
00896     if(verbose) {
00897       std::cerr << "caught expected Teuchos::Exceptions::InvalidParameterType:\n\n";
00898       OSTab tab(std::cerr);
00899       std::cerr << e.what() << std::endl;
00900     }
00901   }
00902   catch(const std::exception &e) {
00903     if(verbose) {
00904       std::cerr << "caught unexpected std::exception:\n\n";
00905       OSTab tab(std::cerr);
00906       std::cerr << e.what() << std::endl;
00907     }
00908     ++FailedTests;
00909   }
00910 
00911   //-----------------------------------------------------------
00912   // Validate the parameter list
00913   //-----------------------------------------------------------
00914 
00915   // Create a validator version of PL_Main that we will validate against!
00916   Teuchos::ParameterList PL_Main_valid("PL_Main_copy");
00917   PL_Main_valid.setParameters(PL_Main);
00918 
00919   // Create a validator for the "Nonlinear Solver" parameter
00920   Teuchos::setStringToIntegralParameter<int>(
00921     "Nonlinear Solver", "Line Search Based",
00922     "Selects the type of nonlinear solver to use",
00923     Teuchos::tuple<std::string>("Line Search Based","Trust Region Based"),
00924     &PL_Main
00925     );
00926 
00927 /*    
00928   Teuchos::RCP<Teuchos::StringToIntegralParameterEntryValidator<int> >
00929     nonlinearSolverValidator = rcp(
00930     new Teuchos::StringToIntegralParameterEntryValidator<int>(
00931       Teuchos::tuple<std::string>("Line Search Based","Trust Region Based")
00932       ,"Nonlinear Solver"
00933       )
00934     );
00935   PL_Main_valid.set(
00936     "Nonlinear Solver", "Line Search Based"
00937     ,"Selects the type of nonlinear solver to use"
00938     ,nonlinearSolverValidator
00939     );
00940 */
00941 
00942   // Create a validator for the parameter "Line Search"->"Polynomial"->"Max Iters"
00943   // that accepts an 'int', a 'double' or a 'std::string' value!
00944   typedef Teuchos::AnyNumberParameterEntryValidator::AcceptedTypes AcceptedTypes;
00945   Teuchos::RCP<Teuchos::AnyNumberParameterEntryValidator>
00946     linesearchMaxItersValiator = rcp(
00947       new Teuchos::AnyNumberParameterEntryValidator(
00948         Teuchos::AnyNumberParameterEntryValidator::PREFER_INT, // Not used here!
00949         AcceptedTypes(false).allowInt(true).allowDouble(true).allowString(true)
00950         )
00951       );
00952   PL_Main_valid.sublist("Line Search").sublist("Polynomial").set(
00953     "Max Iters",3
00954     ,"The maximum number of inner linear search iterations allowed."
00955     ,linesearchMaxItersValiator
00956     );
00957 
00958   // Create a validator for the parameter "Direction"->"Newton"->"Linear Solver"->"Tol"
00959   // that accepts a 'double' or a 'std::string' value!
00960   typedef Teuchos::AnyNumberParameterEntryValidator::AcceptedTypes AcceptedTypes;
00961   Teuchos::RCP<Teuchos::AnyNumberParameterEntryValidator>
00962     linSolveTolValidator = rcp(
00963       new Teuchos::AnyNumberParameterEntryValidator(
00964         Teuchos::AnyNumberParameterEntryValidator::PREFER_INT, // Not used here!
00965         AcceptedTypes(false).allowDouble(true).allowString(true)
00966         )
00967       );
00968   PL_Main_valid.sublist("Direction",true).sublist("Newton",true)
00969     .sublist("Linear Solver",true).set(
00970       "Tol", double(1e-5)
00971       ,"Select the linear solve tolerance"
00972     ,linSolveTolValidator
00973     );
00974 
00975   if (verbose) {
00976     print_break();
00977     cout << "Validating the parameter list against itself (should not throw std::exception)...\n";
00978     print_break();
00979   }
00980   try {
00981     PL_Main.validateParameters(PL_Main_valid);
00982     if (verbose) cout << "Did not throw std::exception, success!\n\n";
00983   }
00984   catch(const std::exception &e) {
00985     if(verbose) {
00986       std::cerr << "caught unexpected std::exception:\n\n";
00987       OSTab tab(std::cerr);
00988       std::cerr << e.what() << std::endl;
00989     }
00990     ++FailedTests;
00991   }
00992 
00993   if (verbose) {
00994     print_break();
00995     cout << "Adding an invalid parameter type then validating (should throw a Teuchos::Exceptions::InvalidParameterType std::exception)...\n";
00996     print_break();
00997   }
00998   try {
00999     PL_Main.sublist("Line Search").sublist("Polynomial").set("Max Iters",(short int)(3)); // Should be an int!
01000     PL_Main.validateParameters(PL_Main_valid);
01001     if (verbose) cout << "Did not throw std::exception, error!\n";
01002     ++FailedTests;
01003   }
01004   catch(const Teuchos::Exceptions::InvalidParameterType &e) {
01005     if(verbose) {
01006       std::cerr << "caught expected Teuchos::Exceptions::InvalidParameterType:\n\n";
01007       OSTab tab(std::cerr);
01008       std::cerr << e.what() << std::endl;
01009     }
01010   }
01011   catch(const std::exception &e) {
01012     if(verbose) {
01013       std::cerr << "caught unexpected std::exception:\n\n";
01014       OSTab tab(std::cerr);
01015       std::cerr << e.what() << std::endl;
01016     }
01017     ++FailedTests;
01018   }
01019   PL_Main.sublist("Line Search").sublist("Polynomial").set("Max Iters",3); // Put back the valid int!
01020 
01021   if (verbose) {
01022     print_break();
01023     cout << "Adding an invalid parameter name then validating (should throw a Teuchos::Exceptions::InvalidParameterName std::exception)...\n";
01024     print_break();
01025   }
01026   try {
01027     PL_Main.sublist("Line Search").sublist("Polynomial").set("Max Iter",10);
01028     PL_Main.validateParameters(PL_Main_valid);
01029     if (verbose) cout << "Did not throw std::exception, error!\n";
01030     ++FailedTests;
01031   }
01032   catch(const Teuchos::Exceptions::InvalidParameterName &e) {
01033     if(verbose) {
01034       std::cerr << "caught expected Teuchos::Exceptions::InvalidParameterName:\n\n";
01035       OSTab tab(std::cerr);
01036       std::cerr << e.what() << std::endl;
01037     }
01038   }
01039   catch(const std::exception &e) {
01040     if(verbose) {
01041       std::cerr << "caught unexpected std::exception:\n\n";
01042       OSTab tab(std::cerr);
01043       std::cerr << e.what() << std::endl;
01044     }
01045     ++FailedTests;
01046   }
01047   PL_Main.sublist("Line Search").sublist("Polynomial").remove("Max Iter");
01048 
01049   if (verbose) {
01050     print_break();
01051     cout << "Adding an invalid parameter type then validating using validator (should throw a Teuchos::Exceptions::InvalidParameterType std::exception)...\n";
01052     print_break();
01053   }
01054   try {
01055     PL_Main.set("Nonlinear Solver",int(0)); // Should be a std::string!
01056     PL_Main.validateParameters(PL_Main_valid);
01057     if (verbose) cout << "Did not throw std::exception, error!\n";
01058     ++FailedTests;
01059   }
01060   catch(const Teuchos::Exceptions::InvalidParameterType &e) {
01061     if(verbose) {
01062       std::cerr << "caught expected Teuchos::Exceptions::InvalidParameterType:\n\n";
01063       OSTab tab(std::cerr);
01064       std::cerr << e.what() << std::endl;
01065     }
01066   }
01067   catch(const std::exception &e) {
01068     if(verbose) {
01069       std::cerr << "caught unexpected std::exception:\n\n";
01070       OSTab tab(std::cerr);
01071       std::cerr << e.what() << std::endl;
01072     }
01073     ++FailedTests;
01074   }
01075   PL_Main.set("Nonlinear Solver","Line Search Based"); // Put back the valid value!
01076 
01077   if (verbose) {
01078     print_break();
01079     cout << "Adding an invalid parameter value then validating using validator (should throw a Teuchos::Exceptions::InvalidParameterValue std::exception)...\n";
01080     print_break();
01081   }
01082   try {
01083     PL_Main.set("Nonlinear Solver","LineSearch Based"); // Should be "Line Search Based"!
01084     PL_Main.validateParameters(PL_Main_valid);
01085     if (verbose) cout << "Did not throw std::exception, error!\n";
01086     ++FailedTests;
01087   }
01088   catch(const Teuchos::Exceptions::InvalidParameterValue &e) {
01089     if(verbose) {
01090       std::cerr << "caught expected Teuchos::Exceptions::InvalidParameterValue:\n\n";
01091       OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01092     }
01093   }
01094   catch(const std::exception &e) {
01095     if(verbose) {
01096       std::cerr << "caught unexpected std::exception:\n\n";
01097       OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01098     }
01099     ++FailedTests;
01100   }
01101   PL_Main.set("Nonlinear Solver","Line Search Based"); // Put back the valid value!
01102 
01103   if (verbose) {
01104     print_break();
01105     cout << "Use the validator to access integral value (should *not* throw std::exception)...\n";
01106     print_break();
01107   }
01108   try {
01109     const int
01110       nonlinearSolverValue = Teuchos::getIntegralValue<int>(PL_Main,"Nonlinear Solver");
01111     const bool
01112       l_result = (nonlinearSolverValue == 0);
01113     cout
01114       << "Read value = " << nonlinearSolverValue << " == 0 : "
01115       << ( l_result ? "passed" : "failed") << "\n";
01116     if(!l_result) ++FailedTests;
01117   }
01118   catch(const std::exception &e) {
01119     if(verbose) {
01120       std::cerr << "caught unexpected std::exception:\n\n";
01121       OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01122     }
01123     ++FailedTests;
01124   }
01125 
01126   if (verbose) {
01127     print_break();
01128     cout << "Use the validator to access std::string value (should *not* throw std::exception)...\n";
01129     print_break();
01130   }
01131   try {
01132     const std::string
01133       nonlinearSolverValue = Teuchos::getStringValue<int>(PL_Main,"Nonlinear Solver");
01134     const bool
01135       l_result = (nonlinearSolverValue == "Line Search Based");
01136     cout
01137       << "Read value = \"" << nonlinearSolverValue << " == \"Line Search Based\" : "
01138       << ( l_result ? "passed" : "failed") << "\n";
01139     if(!l_result) ++FailedTests;
01140   }
01141   catch(const std::exception &e) {
01142     if(verbose) {
01143       std::cerr << "caught unexpected std::exception:\n\n";
01144       OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01145     }
01146     ++FailedTests;
01147   }
01148 
01149   //-----------------------------------------------------------
01150   // Validate and set defaults
01151   //-----------------------------------------------------------
01152 
01153   // Set the default parameters for an emplty list
01154   ParameterList validatedPL;
01155 
01156   if (verbose) {
01157     print_break();
01158     cout << "Validating and setting defaults for an empty parameter list (should not throw) ...\n";
01159     print_break();
01160   }
01161   try {
01162     validatedPL.validateParametersAndSetDefaults(PL_Main_valid);
01163     if (verbose) cout << "Did not throw std::exception, success!\n\n";
01164   }
01165   catch(const std::exception &e) {
01166     if(verbose) {
01167       std::cerr << "caught unexpected std::exception:\n\n";
01168       OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01169     }
01170     ++FailedTests;
01171   }
01172 
01173   if (verbose) {
01174     print_break();
01175     cout << "Parameter list with defaults set:" << std::endl;
01176     print_break();
01177     validatedPL.print(cout,PLPrintOptions().showTypes(true).showDoc(true));
01178     print_break();
01179   }
01180 
01181   if (verbose) {
01182     print_break();
01183     cout << "Checking that validatedPL and PL_Main_valid have the same values : ";
01184   }
01185   result = haveSameValues(validatedPL,PL_Main_valid);
01186   if(!result)
01187     ++FailedTests;
01188   if (verbose) {
01189     cout << ( result ? "passed" : "failed" ) << "\n";
01190     print_break();
01191   }
01192 
01193   //
01194   // Testing access of numbers using validator where validator is not buried
01195   // in the parameter
01196   //
01197 
01198   for( int type_i = 0; type_i < 3; ++type_i ) {
01199 
01200     ParameterList &Polynomial_sublist
01201       = PL_Main.sublist("Line Search",true).sublist("Polynomial",true);
01202     
01203     std::string typeName;
01204 
01205     // Set the input type
01206 
01207     switch(type_i) {
01208       case 0:
01209         typeName = "int";
01210         Polynomial_sublist.set("Max Iters",(int)(3));
01211         break;
01212       case 1:
01213         typeName = "double";
01214         Polynomial_sublist.set("Max Iters",(double)(3.0));
01215         break;
01216       case 2:
01217         typeName = "std::string";
01218         Polynomial_sublist.set("Max Iters",(std::string)("3"));
01219         break;
01220       default:
01221         TEST_FOR_EXCEPT(true);
01222     }
01223 
01224     // Extract using external validator
01225 
01226     if (verbose) {
01227       print_break();
01228       cout << "Use the external number validator to access a "<<typeName<<" as an int ...\n";
01229       print_break();
01230     }
01231     try {
01232       const int
01233         lineserchMaxIters
01234         = linesearchMaxItersValiator->getInt(
01235           PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01236           ,"Max Iters",0
01237           );
01238       const bool
01239         l_result = (lineserchMaxIters == int(3));
01240       cout
01241         << "Read value = " << lineserchMaxIters << " == 3 : "
01242         << ( l_result ? "passed" : "failed") << "\n";
01243       if(!l_result) ++FailedTests;
01244     }
01245     catch(const std::exception &e) {
01246       if(verbose) {
01247         std::cerr << "caught unexpected std::exception:\n\n";
01248         OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01249       }
01250       ++FailedTests;
01251     }
01252 
01253     if (verbose) {
01254       print_break();
01255       cout << "Use the external number validator to access a "<<typeName<<" as a double ...\n";
01256       print_break();
01257     }
01258     try {
01259       const double
01260         lineserchMaxIters
01261         = linesearchMaxItersValiator->getDouble(
01262           PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01263           ,"Max Iters",0.0
01264           );
01265       const bool
01266         l_result = (lineserchMaxIters == double(3.0));
01267       cout
01268         << "Read value = " << lineserchMaxIters << " == 3 : "
01269         << ( l_result ? "passed" : "failed") << "\n";
01270       if(!l_result) ++FailedTests;
01271     }
01272     catch(const std::exception &e) {
01273       if(verbose) {
01274         std::cerr << "caught unexpected std::exception:\n\n";
01275         OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01276       }
01277       ++FailedTests;
01278     }
01279 
01280     if (verbose) {
01281       print_break();
01282       cout << "Use the external number validator to access a "<<typeName<<" as a std::string ...\n";
01283       print_break();
01284     }
01285     try {
01286       const std::string
01287         lineserchMaxIters
01288         = linesearchMaxItersValiator->getString(
01289           PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01290           ,"Max Iters","0"
01291           );
01292       const bool
01293         l_result = (lineserchMaxIters == "3");
01294       cout
01295         << "Read value = \"" << lineserchMaxIters << "\" == \"3\" : "
01296         << ( l_result ? "passed" : "failed") << "\n";
01297       if(!l_result) ++FailedTests;
01298     }
01299     catch(const std::exception &e) {
01300       if(verbose) {
01301         std::cerr << "caught unexpected std::exception:\n\n";
01302         OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01303       }
01304       ++FailedTests;
01305     }
01306 
01307     // Extract using nonmember functions
01308 
01309     if (verbose) {
01310       print_break();
01311       cout << "Use the nomember help function to access a "<<typeName<<" as an int ...\n";
01312       print_break();
01313     }
01314     try {
01315       const int
01316         lineserchMaxIters
01317         = Teuchos::getIntParameter(
01318           PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01319           ,"Max Iters"
01320           );
01321       const bool
01322         l_result = (lineserchMaxIters == int(3));
01323       cout
01324         << "Read value = " << lineserchMaxIters << " == 3 : "
01325         << ( l_result ? "passed" : "failed") << "\n";
01326       if(!l_result) ++FailedTests;
01327     }
01328     catch(const std::exception &e) {
01329       if(verbose) {
01330         std::cerr << "caught unexpected std::exception:\n\n";
01331         OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01332       }
01333       ++FailedTests;
01334     }
01335 
01336     if (verbose) {
01337       print_break();
01338       cout << "Use the nomember help function to access a "<<typeName<<" as a double ...\n";
01339       print_break();
01340     }
01341     try {
01342       const double
01343         lineserchMaxIters
01344         = Teuchos::getDoubleParameter(
01345           PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01346           ,"Max Iters"
01347           );
01348       const bool
01349         l_result = (lineserchMaxIters == double(3.0));
01350       cout
01351         << "Read value = " << lineserchMaxIters << " == 3 : "
01352         << ( l_result ? "passed" : "failed") << "\n";
01353       if(!l_result) ++FailedTests;
01354     }
01355     catch(const std::exception &e) {
01356       if(verbose) {
01357         std::cerr << "caught unexpected std::exception:\n\n";
01358         OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01359       }
01360       ++FailedTests;
01361     }
01362 
01363     if (verbose) {
01364       print_break();
01365       cout << "Use the nomember help function to access a "<<typeName<<" as a std::string ...\n";
01366       print_break();
01367     }
01368     try {
01369       const std::string
01370         lineserchMaxIters
01371         = Teuchos::getNumericStringParameter(
01372           PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01373           ,"Max Iters"
01374           );
01375       const bool
01376         l_result = (lineserchMaxIters == "3");
01377       cout
01378         << "Read value = \"" << lineserchMaxIters << "\" == \"3\" : "
01379         << ( l_result ? "passed" : "failed") << "\n";
01380       if(!l_result) ++FailedTests;
01381     }
01382     catch(const std::exception &e) {
01383       if(verbose) {
01384         std::cerr << "caught unexpected std::exception:\n\n";
01385         OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01386       }
01387       ++FailedTests;
01388     }
01389 
01390   }
01391 
01392   //
01393   // Testing access of numbers using validator where validator is buried in
01394   // the parameter
01395   //
01396 
01397   for( int type_i = 0; type_i < 3; ++type_i ) {
01398 
01399     ParameterList &Polynomial_sublist
01400       = PL_Main.sublist("Line Search",true).sublist("Polynomial",true);
01401     
01402     std::string typeName;
01403 
01404     // Set the input type
01405 
01406     switch(type_i) {
01407       case 0:
01408         typeName = "int";
01409         Teuchos::setIntParameter("Max Iters",3,"",&Polynomial_sublist);
01410         break;
01411       case 1:
01412         typeName = "double";
01413         Teuchos::setDoubleParameter("Max Iters",3.0,"",&Polynomial_sublist);
01414         break;
01415       case 2:
01416         typeName = "std::string";
01417         Teuchos::setNumericStringParameter("Max Iters","3","",&Polynomial_sublist);
01418         break;
01419       default:
01420         TEST_FOR_EXCEPT(true);
01421     }
01422 
01423     // Extract using nonmember functions (which should use the internal validator)
01424 
01425     if (verbose) {
01426       print_break();
01427       cout << "Use the nomember help function to access a "<<typeName<<" as an int ...\n";
01428       print_break();
01429     }
01430     try {
01431       const int
01432         lineserchMaxIters
01433         = Teuchos::getIntParameter(
01434           PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01435           ,"Max Iters"
01436           );
01437       const bool
01438         l_result = (lineserchMaxIters == int(3));
01439       cout
01440         << "Read value = " << lineserchMaxIters << " == 3 : "
01441         << ( l_result ? "passed" : "failed") << "\n";
01442       if(!l_result) ++FailedTests;
01443     }
01444     catch(const std::exception &e) {
01445       if(verbose) {
01446         std::cerr << "caught unexpected std::exception:\n\n";
01447         OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01448       }
01449       ++FailedTests;
01450     }
01451 
01452     if (verbose) {
01453       print_break();
01454       cout << "Use the nomember help function to access a "<<typeName<<" as a double ...\n";
01455       print_break();
01456     }
01457     try {
01458       const double
01459         lineserchMaxIters
01460         = Teuchos::getDoubleParameter(
01461           PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01462           ,"Max Iters"
01463           );
01464       const bool
01465         l_result = (lineserchMaxIters == double(3.0));
01466       cout
01467         << "Read value = " << lineserchMaxIters << " == 3 : "
01468         << ( l_result ? "passed" : "failed") << "\n";
01469       if(!l_result) ++FailedTests;
01470     }
01471     catch(const std::exception &e) {
01472       if(verbose) {
01473         std::cerr << "caught unexpected std::exception:\n\n";
01474         OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01475       }
01476       ++FailedTests;
01477     }
01478 
01479     if (verbose) {
01480       print_break();
01481       cout << "Use the nomember help function to access a "<<typeName<<" as a std::string ...\n";
01482       print_break();
01483     }
01484     try {
01485       const std::string
01486         lineserchMaxIters
01487         = Teuchos::getNumericStringParameter(
01488           PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01489           ,"Max Iters"
01490           );
01491       const bool
01492         l_result = (lineserchMaxIters == "3");
01493       cout
01494         << "Read value = \"" << lineserchMaxIters << "\" == \"3\" : "
01495         << ( l_result ? "passed" : "failed") << "\n";
01496       if(!l_result) ++FailedTests;
01497     }
01498     catch(const std::exception &e) {
01499       if(verbose) {
01500         std::cerr << "caught unexpected std::exception:\n\n";
01501         OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01502       }
01503       ++FailedTests;
01504     }
01505 
01506   }
01507 
01508   //
01509   // Testing access of numbers where correct number is set using
01510   // validateParametersAndSetDefaults(...) with no special access.
01511   //
01512 
01513   for( int type_i = 0; type_i < 3; ++type_i ) {
01514 
01515     ParameterList valid_PL_Main(PL_Main);
01516 
01517     ParameterList &Polynomial_sublist
01518       = PL_Main.sublist("Line Search",true).sublist("Polynomial",true);
01519     
01520     std::string typeName;
01521 
01522     // Set the input type
01523 
01524     switch(type_i) {
01525       case 0:
01526         typeName = "int";
01527         Teuchos::setIntParameter("Max Iters",3,"",&Polynomial_sublist);
01528         break;
01529       case 1:
01530         typeName = "double";
01531         Teuchos::setDoubleParameter("Max Iters",3.0,"",&Polynomial_sublist);
01532         break;
01533       case 2:
01534         typeName = "std::string";
01535         Teuchos::setNumericStringParameter("Max Iters","3","",&Polynomial_sublist);
01536         break;
01537       default:
01538         TEST_FOR_EXCEPT(true);
01539     }
01540 
01541     // Extract using nonmember functions (which should use the internal validator)
01542 
01543     if (verbose) {
01544       print_break();
01545       cout << "Use validateParemetersAndSetDefaults(...) to access a "<<typeName<<" as an int ...\n";
01546       print_break();
01547     }
01548     try {
01549       Teuchos::setIntParameter(
01550         "Max Iters", 0, "",
01551         &valid_PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01552         );
01553       ParameterList copied_PL_Main(PL_Main);
01554       copied_PL_Main.validateParametersAndSetDefaults(valid_PL_Main);
01555       const int
01556         lineserchMaxIters
01557         = Teuchos::getParameter<int>(
01558           copied_PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01559           ,"Max Iters"
01560           );
01561       const bool
01562         l_result = (lineserchMaxIters == int(3));
01563       cout
01564         << "Read value = " << lineserchMaxIters << " == 3 : "
01565         << ( l_result ? "passed" : "failed") << "\n";
01566       if(!l_result) ++FailedTests;
01567     }
01568     catch(const std::exception &e) {
01569       if(verbose) {
01570         std::cerr << "caught unexpected std::exception:\n\n";
01571         OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01572       }
01573       ++FailedTests;
01574     }
01575 
01576     if (verbose) {
01577       print_break();
01578       cout << "Use validateParemetersAndSetDefaults(...) to access a "<<typeName<<" as a double ...\n";
01579       print_break();
01580     }
01581     try {
01582       Teuchos::setDoubleParameter(
01583         "Max Iters", 0.0, "",
01584         &valid_PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01585         );
01586       ParameterList copied_PL_Main(PL_Main);
01587       copied_PL_Main.validateParametersAndSetDefaults(valid_PL_Main);
01588       const double
01589         lineserchMaxIters
01590         = Teuchos::getParameter<double>(
01591           copied_PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01592           ,"Max Iters"
01593           );
01594       const bool
01595         l_result = (lineserchMaxIters == double(3.0));
01596       cout
01597         << "Read value = " << lineserchMaxIters << " == 3 : "
01598         << ( l_result ? "passed" : "failed") << "\n";
01599       if(!l_result) ++FailedTests;
01600     }
01601     catch(const std::exception &e) {
01602       if(verbose) {
01603         std::cerr << "caught unexpected std::exception:\n\n";
01604         OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01605       }
01606       ++FailedTests;
01607     }
01608 
01609     if (verbose) {
01610       print_break();
01611       cout << "Use validateParemetersAndSetDefaults(...) to access a "<<typeName<<" as a std::string ...\n";
01612       print_break();
01613     }
01614     try {
01615       Teuchos::setNumericStringParameter(
01616         "Max Iters", "0", "",
01617         &valid_PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01618         );
01619       ParameterList copied_PL_Main(PL_Main);
01620       copied_PL_Main.validateParametersAndSetDefaults(valid_PL_Main);
01621       const std::string
01622         lineserchMaxIters
01623         = Teuchos::getParameter<std::string>(
01624           copied_PL_Main.sublist("Line Search",true).sublist("Polynomial",true)
01625           ,"Max Iters"
01626           );
01627       const bool
01628         l_result = (lineserchMaxIters == "3");
01629       cout
01630         << "Read value = \"" << lineserchMaxIters << "\" == \"3\" : "
01631         << ( l_result ? "passed" : "failed") << "\n";
01632       if(!l_result) ++FailedTests;
01633     }
01634     catch(const std::exception &e) {
01635       if(verbose) {
01636         std::cerr << "caught unexpected std::exception:\n\n";
01637         OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01638       }
01639       ++FailedTests;
01640     }
01641 
01642   }
01643 
01644   if (verbose) {
01645     print_break();
01646     cout << "Adding an invalid sublist then validating (should throw a Teuchos::Exceptions::InvalidParameterName std::exception)...\n";
01647     print_break();
01648   }
01649   try {
01650     PL_Main.sublist("Line Search").sublist("Polynomials").set("Max Iters",3); // param correct, sublist wrong
01651     PL_Main.validateParameters(PL_Main_valid);
01652     if (verbose) cout << "Did not throw std::exception, error!\n";
01653     ++FailedTests;
01654   }
01655   catch(const Teuchos::Exceptions::InvalidParameterName &e) {
01656     if(verbose) {
01657       std::cerr << "caught expected Teuchos::Exceptions::InvalidParameterName:\n\n";
01658       OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01659     }
01660   }
01661   catch(const std::exception &e) {
01662     if(verbose) {
01663       std::cerr << "caught unexpected std::exception:\n\n";
01664       OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01665     }
01666     ++FailedTests;
01667   }
01668   PL_Main.sublist("Line Search").remove("Polynomials");
01669 
01670   if (verbose) {
01671     print_break();
01672     cout << "Validating only the top level list (should not throw std::exception)...\n";
01673     print_break();
01674   }
01675   try {
01676     PL_Main.validateParameters(PL_Main_valid,0);
01677     if (verbose) cout << "Did not throw std::exception, success!\n\n";
01678   }
01679   catch(const std::exception &e) {
01680     if(verbose) {
01681       std::cerr << "caught unexpected std::exception:\n\n";
01682       OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
01683     }
01684     ++FailedTests;
01685   }
01686 
01687   //-----------------------------------------------------------
01688   // Compare lists
01689   //-----------------------------------------------------------
01690 
01691   if (verbose) {
01692     print_break();
01693     cout << "Checking that PL_Main == PL_Main == true : ";
01694   }
01695   result = (PL_Main == PL_Main);
01696   if(!result)
01697     ++FailedTests;
01698   if (verbose) {
01699     cout << ( result ? "passed" : "failed" ) << "\n";
01700     print_break();
01701   }
01702 
01703   if (verbose) {
01704     print_break();
01705     cout << "Checking that PL_Main != PL_Main == false : ";
01706   }
01707   result = !(PL_Main != PL_Main);
01708   if(!result)
01709     ++FailedTests;
01710   if (verbose) {
01711     cout << ( result ? "passed" : "failed" ) << "\n";
01712     print_break();
01713   }
01714 
01715   if (verbose) {
01716     print_break();
01717     cout << "Checking that PL_Main and PL_Main have the same values : ";
01718   }
01719   result = haveSameValues(PL_Main,PL_Main);
01720   if(!result)
01721     ++FailedTests;
01722   if (verbose) {
01723     cout << ( result ? "passed" : "failed" ) << "\n";
01724     print_break();
01725   }
01726 
01727   if (verbose) {
01728     print_break();
01729     cout << "Create copy PL_Main_copy, change PL_Main_copy, and check that PL_Main != PL_Main == true : ";
01730   }
01731   ParameterList PL_Main_copy(PL_Main);
01732   PL_Main_copy.sublist("Line Search",true).sublist("Polynomial",true).set("Max Iters",100); // Not the default!
01733   result = (PL_Main_copy != PL_Main);
01734   if(!result)
01735     ++FailedTests;
01736   if (verbose) {
01737     cout << ( result ? "passed" : "failed" ) << "\n";
01738     print_break();
01739   }
01740 
01741   //-----------------------------------------------------------
01742   // Print out main list showing the types
01743   //-----------------------------------------------------------
01744 
01745   if (verbose) {
01746     print_break();
01747     cout << "The Final Parameter List with Types and Documentation" << std::endl;
01748     print_break();
01749     PL_Main.print(cout,PLPrintOptions().showTypes(true).showDoc(true));
01750     print_break();
01751     cout << "The unused parameters" << std::endl;
01752     PL_Main.unused(cout);
01753     print_break();
01754     cout << "Number of Failed Tests : " << FailedTests << std::endl;
01755     print_break();
01756   }
01757 
01758   //-----------------------------------------------------------
01759   // Return -1 if there are any failed tests, 
01760   // else 0 will be returned indicating a clean finish!  
01761   //-----------------------------------------------------------
01762 
01763   } // end try
01764   TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success);
01765   if(!success) ++FailedTests;
01766 
01767   if ( FailedTests > 0 ) { 
01768     cout << "End Result: TEST FAILED" << std::endl;
01769     return 1; // Can't return negative numbers from main()!
01770   }
01771 
01772   if ( FailedTests == 0 )
01773     cout << "End Result: TEST PASSED" << std::endl;
01774 
01775   return 0;
01776 
01777 }
01778 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines