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