20 #ifdef HAVE_TEUCHOS_EXTENDED
31 using Teuchos::getParameter;
36 using Teuchos::inoutArg;
38 void print_break() { std::cout <<
"---------------------------------------------------" << std::endl; }
39 double Plus (
double a,
double b ) {
return a+b; }
41 int main(
int argc,
char *argv[] )
60 clp.
setOption(
"verbose",
"quiet", &verbose,
"Set if output is printed or not." );
61 CommandLineProcessor::EParseCommandLineReturn parse_return = clp.
parse(argc,argv);
62 if( parse_return != CommandLineProcessor::PARSE_SUCCESSFUL ) {
63 cout <<
"Processor "<< procRank <<
", parse_return "<< parse_return << std::endl;
64 cout <<
"End Result: TEST FAILED" << std::endl;
69 if (procRank != 0 && verbose)
80 const std::string Direction_Doc =
"This sublist controls how direction is computed.";
91 cout <<
"Empty Parameter List Structure" << std::endl;
93 cout<<PL_Main<< std::endl;
95 if (verbose) cout <<
"Is 'Direction' recognized as a sublist of 'Main' ... ";
97 if (verbose) cout <<
"yes"<< std::endl;
99 if (verbose) cout <<
"no"<< std::endl;
102 if (verbose) cout <<
"Is 'Newton' recognized as a sublist of 'Direction' ... ";
103 if ( PL_Direction.
isSublist(
"Newton" ) ) {
104 if (verbose) cout <<
"yes"<< std::endl;
106 if (verbose) cout <<
"no"<< std::endl;
109 if (verbose) cout <<
"Is 'Linear Solver' recognized as a sublist of 'Newton' ... ";
110 if ( PL_Newton.
isSublist(
"Linear Solver" ) ) {
111 if (verbose) cout <<
"yes"<< std::endl;
113 if (verbose) cout <<
"no"<< std::endl;
116 if (verbose) cout <<
"Is 'Line Search' recognized as a sublist of 'Main' ... ";
117 if ( PL_Main.
isSublist(
"Line Search" ) ) {
118 if (verbose) cout <<
"yes"<< std::endl;
120 if (verbose) cout <<
"no"<< std::endl;
124 if (verbose) cout <<
"Is subist documentation std::string maintained ...\n";
130 const std::string extracted_Direction_Doc = paramEntry->
docString();
131 if (verbose) tab.
o() <<
"Expected doc std::string = \"" << Direction_Doc <<
"\"\n";
132 if (verbose) tab.
o() <<
"Extracted doc std::string = \"" << extracted_Direction_Doc <<
"\"\n";
133 if (extracted_Direction_Doc == Direction_Doc) {
134 if (verbose) tab.
o() <<
"passed! They match :-)\n";
137 if (verbose) tab.
o() <<
"failed! They do not match :-("<< std::endl;
149 PL_Direction.
get(
"Method",
"Newton");
150 PL_LinSol.
set(
"Tol",1e-5);
151 tol = PL_LinSol.
get(
"Tolerance",1e-10);
153 RBNS = PL_Newton.
get(
"Rescue Bad Newton Solve",
true );
161 cout <<
"Direction Parameter List" << std::endl;
163 PL_Direction.
print(cout);
165 if (verbose) cout <<
"Is 'Newton' recognized as a parameter of 'Direction' ... ";
167 if (verbose) cout <<
"yes"<< std::endl;
169 if (verbose) cout <<
"no"<< std::endl;
172 if (verbose) cout <<
"Is 'Tolerance' recognized as a parameter of 'Newton' ... ";
174 if (verbose) cout <<
"yes (should be no)"<< std::endl;
177 if (verbose) cout <<
"no (as expected)"<< std::endl;
179 if (verbose) cout <<
"Is 'Tolerance' recognized as a parameter of 'Linear Solver' ... ";
181 if (verbose) cout <<
"yes"<< std::endl;
183 if (verbose) cout <<
"no"<< std::endl;
186 if (verbose) cout <<
"Is 'Rescue Bad Newton Solve' recognized as a parameter of 'Newton' ... ";
187 if ( PL_Newton.
isParameter(
"Rescue Bad Newton Solve" ) ) {
188 if (verbose) cout <<
"yes"<< std::endl;
190 if (verbose) cout <<
"no"<< std::endl;
199 int ARI = 0, default_step = 0, max_iter_inc = 0, rec_step = 0;
200 double alpha_factor = 0.0, min_bnds_factor = 0.0, max_bnds_factor = 0.0;
201 bool force_interp =
true, use_cntrs =
false;
202 std::string ls_method =
"Polynomial";
205 char* ls_method_char =
const_cast<char *
>(ls_method.c_str());
207 ls_method = PL_My_LineSearch.
get(
"Method", ls_method_char);
209 ARI = PL_Polynomial.
get(
"Allowed Relative Increase", 100 );
211 alpha_factor = PL_Polynomial.
get(
"Alpha Factor", 0.0001 );
213 default_step = PL_Polynomial.
get(
"Default Step", 1 );
215 force_interp = PL_Polynomial.
get(
"Force Interpolation",
false );
217 std::string interp_type = PL_Polynomial.
get(
"Interpolation Type",
"Cubic" );
218 max_bnds_factor = PL_Polynomial.
get(
"Max Bounds Factor", 0.5 );
219 (void)max_bnds_factor;
220 PL_Polynomial.
set(
"Max Iters", 3 );
221 max_iter_inc = PL_Polynomial.
get(
"Maximum Iteration for Increase", 0 );
223 min_bnds_factor = PL_Polynomial.
get(
"Min Bounds Factor", 0.1 );
224 (void)min_bnds_factor;
225 rec_step = PL_Polynomial.
get(
"Recovery Step", 1 );
227 std::string rec_step_type = PL_Polynomial.
get(
"Recovery Step Type",
"Constant");
228 std::string suff_dec_cond = PL_Polynomial.
get(
"Sufficient Decrease Condition",
"Armijo-Goldstein" );
229 use_cntrs = PL_Polynomial.
get(
"Use Counters",
true );
231 PL_Main.
set(
"Nonlinear Solver",
"Line Search Based");
238 if (verbose) cout<<
"Is 'operator=' functional ... ";
239 if ( PL_My_Polynomial.
isParameter(
"Recovery Step Type") ) {
240 if (verbose) cout<<
"yes" << std::endl;
242 if (verbose) cout<<
"no" << std::endl;
252 if (verbose) cout <<
"Test copying of sublist\n";
254 PL_Direction.
print(cout);
260 linearSolverPL_copy(linearSolverPL);
261 if (verbose) cout <<
"linearSolverPL.name() = " << linearSolverPL.
name() << endl;
262 if (verbose) cout <<
"linearSolverPL_copy.name() = " << linearSolverPL_copy.
name() << endl;
263 if (verbose) cout <<
"linearSolverPL_copy == linearSolverPL.name() : ";
264 if (linearSolverPL_copy == linearSolverPL.
name()) {
265 if (verbose) cout <<
"passed" << endl;
268 if (verbose) cout <<
"failed" << endl;
275 if (verbose) cout <<
"General tests\n";
277 PL_Direction.
print(cout);
282 if (verbose) cout <<
"Copied_PL_Main.name() == PL_Main.name() : ";
283 if (Copied_PL_Main.
name() == PL_Main.
name()) {
284 if (verbose) cout <<
"passed" << endl;
287 if (verbose) cout <<
"failed" << endl;
289 if (verbose) cout <<
"Copyed_PL_Main.name() = " << Copied_PL_Main.
name() << endl;
292 if (verbose) cout<<
"Is the copy constructor functional ... ";
293 if ( Copied_PL_Main.
isParameter(
"Nonlinear Solver") ) {
294 if (verbose) cout<<
"yes" << std::endl;
296 if (verbose) cout<<
"no" << std::endl;
300 bool tempMeth =
true;
311 int max_iters = 0, max_iters_again = 0;
312 std::string nonlin_solver;
315 max_iters = PL_My_Polynomial.INVALID_TEMPLATE_QUALIFIER get<int>(
"Max Iters");
316 max_iters_again =
Teuchos::getConst(PL_My_Polynomial).INVALID_TEMPLATE_QUALIFIER get<int>(
"Max Iters");
317 nonlin_solver = PL_Main.INVALID_TEMPLATE_QUALIFIER get<std::string>(
"Nonlinear Solver");
321 cout<<
"Is the templated 'get' method functional ... "<<std::endl;
322 cout<<
" Can we retrieve information using the CORRECT variable type ... ";
324 if (tempMeth && max_iters==3) {
if (verbose) cout <<
"yes" << std::endl; }
325 else {
if (verbose) cout <<
"no" << std::endl; FailedTests++; }
327 cout<<
" Can we retrieve const information using the CORRECT variable type ... ";
329 if (tempMeth && max_iters_again==3) {
if (verbose) cout <<
"yes" << std::endl; }
330 else {
if (verbose) cout <<
"no" << std::endl; FailedTests++; }
340 mbf = PL_LinSol.INVALID_TEMPLATE_QUALIFIER get<float>(
"Tol" );
348 cout<<
" Can we retrieve information using the WRONG variable type ... ";
350 if (tempMeth) {
if (verbose) cout <<
"no" << std::endl; }
351 else {
if (verbose) cout <<
"yes" << std::endl; }
360 max_iters = PL_My_Polynomial.INVALID_TEMPLATE_QUALIFIER get<int>(
"Max Iters");
361 nonlin_solver = PL_Main.INVALID_TEMPLATE_QUALIFIER get<std::string>(
"Nonlinear Solver");
365 cout<<
"Is the templated 'get' method functional ... "<<std::endl;
366 cout<<
" Can we retrieve information using the CORRECT variable type ... ";
368 if (tempMeth && max_iters==3) {
if (verbose) cout <<
"yes" << std::endl; }
369 else {
if (verbose) cout <<
"no" << std::endl; FailedTests++; }
376 mbf = PL_LinSol.INVALID_TEMPLATE_QUALIFIER get<float>(
"Tol" );
384 cout<<
" Can we retrieve information using the WRONG variable type ... ";
386 if (tempMeth) {
if (verbose) cout <<
"no" << std::endl; }
387 else {
if (verbose) cout <<
"yes" << std::endl; }
398 int *max_iters_ptr = 0;
399 const int *max_iters_ptr_again = 0;
400 std::string* nonlin_solver_ptr;
402 max_iters_ptr = PL_My_Polynomial.INVALID_TEMPLATE_QUALIFIER getPtr<int>(
"Max Iters");
403 max_iters_ptr_again =
Teuchos::getConst(PL_My_Polynomial).INVALID_TEMPLATE_QUALIFIER getPtr<int>(
"Max Iters");
404 nonlin_solver_ptr = PL_Main.INVALID_TEMPLATE_QUALIFIER getPtr<std::string>(
"Nonlinear Solver");
407 cout<<
"Is the templated 'getPtr' method functional ... "<<std::endl;
408 cout<<
" Can we retrieve information using the CORRECT variable type ... ";
411 if ((*max_iters_ptr)==3) {
412 if (verbose) cout <<
"yes" << std::endl;
414 else {
if (verbose) cout <<
"no" << std::endl; FailedTests++; }
417 cout<<
" Can we retrieve const information using the CORRECT variable type ... ";
419 if (max_iters_ptr_again) {
420 if ((*max_iters_ptr_again)==3) {
421 if (verbose) cout <<
"yes" << std::endl;
423 else {
if (verbose) cout <<
"no" << std::endl; FailedTests++; }
433 mbf_ptr = PL_LinSol.INVALID_TEMPLATE_QUALIFIER getPtr<float>(
"Tol" );
439 cout<<
" Can we retrieve information using the WRONG variable type ... ";
441 if (!mbf_ptr) {
if (verbose) cout <<
"no" << std::endl; }
442 else {
if (verbose) cout <<
"yes" << std::endl; }
450 max_iters_ptr = PL_My_Polynomial.INVALID_TEMPLATE_QUALIFIER getPtr<int>(
"Max Iters");
451 nonlin_solver_ptr = PL_Main.INVALID_TEMPLATE_QUALIFIER getPtr<std::string>(
"Nonlinear Solver");
452 (void)nonlin_solver_ptr;
455 cout<<
"Is the templated 'getPtr' method functional ... "<<std::endl;
456 cout<<
" Can we retrieve information using the CORRECT variable type ... ";
459 if ((*max_iters_ptr)==3) {
460 if (verbose) cout <<
"yes" << std::endl;
462 else {
if (verbose) cout <<
"no" << std::endl; FailedTests++; }
471 mbf_ptr = PL_LinSol.INVALID_TEMPLATE_QUALIFIER getPtr<float>(
"Tol" );
477 cout<<
" Can we retrieve information using the WRONG variable type ... ";
479 if (!mbf_ptr) {
if (verbose) cout <<
"no" << std::endl; }
480 else {
if (verbose) cout <<
"yes" << std::endl; }
486 double alpha_fact = 0.0;
489 def_step = Teuchos::getParameter<int>(PL_Polynomial,
"Default Step");
490 alpha_fact = Teuchos::getParameter<double>(PL_Polynomial,
"Alpha Factor");
494 if (verbose && def_step==1) {
495 cout<<
"Is the helper function 'getParameter' functional ... ";
497 if (tempMeth) {
if (verbose) cout <<
"yes" << std::endl; }
498 else {
if (verbose) cout <<
"no" << std::endl; FailedTests++; }
506 PT1 = PL_Polynomial.INVALID_TEMPLATE_QUALIFIER isType<int>(
"Default Step");
507 PT2 = PL_Polynomial.INVALID_TEMPLATE_QUALIFIER isType<long int>(
"Default Step");
508 PT3 = PL_Polynomial.INVALID_TEMPLATE_QUALIFIER isType<std::string>(
"Interpolation Type");
510 cout<<
"Is the templated 'isType' method functional ... "<<std::endl;
511 cout<<
" Is the 'Default Step' of type 'int' ... ";
513 if (PT1) {
if (verbose) cout<<
"yes" << std::endl; }
514 else {
if (verbose) cout<<
"no" << std::endl; FailedTests++; }
516 cout<<
" Is the 'Default Step' of type 'long int' ... ";
518 if (PT2) {
if (verbose) cout<<
"yes" << std::endl; FailedTests++; }
519 else {
if (verbose) cout<<
"no (as expected)" << std::endl; }
521 cout<<
" Is the 'Interpolation Type' of type 'std::string' ... ";
523 if (PT3) {
if (verbose) cout<<
"yes" << std::endl; }
524 else {
if (verbose) cout<<
"no" << std::endl; FailedTests++; }
530 PT4 = Teuchos::isParameterType<double>(PL_Polynomial,
"Max Bounds Factor");
531 PT5 = Teuchos::isParameterType<float>(PL_Polynomial,
"Max Bounds Factor");
533 cout<<
"Is the helper function 'isParameterType' functional ... "<<std::endl;
534 cout<<
" Is the 'Max Bounds Factor' of type 'double' ... ";
536 if (PT4) {
if (verbose) cout<<
"yes" <<std::endl; }
537 else {
if (verbose) cout<<
"no" << std::endl; FailedTests++; }
539 cout<<
" Is the 'Max Bounds Factor' of type 'float' ... ";
541 if (PT5) {
if (verbose) cout<<
"yes" <<std::endl; FailedTests++; }
542 else {
if (verbose) cout<<
"no (as expected)" << std::endl; }
549 double * tempvec1 = tempvec1_arcp.
get();
550 for (
int i=0; i<10; i++) { tempvec1[i] = i; }
551 PL_Main.
set(
"Address of Norm Vector", tempvec1 );
552 double* tempvec2 = Teuchos::getParameter<double*>( PL_Main,
"Address of Norm Vector" );
553 tempvec1[4] = 2.0; tempvec1[6] = 1.0;
555 cout<<
"Can we pass a pointer to a std::vector to a parameter list ... ";
557 if ((tempvec2[4]-tempvec1[4])!=0.0 || (tempvec2[6]-tempvec1[6])!=0.0) {
558 if (verbose) { cout<<
"no"<<std::endl; }
561 if (verbose) { cout<<
"yes"<<std::endl; }
570 cout <<
"Setting int and double array objects as std::string parameters ...\n";
574 intArray = Teuchos::tuple<int>(0,1,2,3,4,5,6);
576 doubleArray = Teuchos::tuple<double>(0,1.0,2.0,3.0,4.0,5.0,6.0);
579 Teuchos::setStringParameterFromArray(
"Int Array",intArray,&PL_Main);
580 Teuchos::setStringParameterFromArray(
"Double Array",doubleArray,&PL_Main);
584 cout <<
"Testing retrieval of set array objects ...\n";
590 readIntArray = Teuchos::getArrayFromStringParameter<int>(PL_Main,
"Int Array");
591 result = readIntArray == intArray;
592 if(!result) ++FailedTests;
595 <<
"readIntArray = " << readIntArray <<
" == intArray = " << intArray <<
" ? "
596 << (result ?
"passed" :
"failed")
600 readDoubleAsIntArray = Teuchos::getArrayFromStringParameter<int>(PL_Main,
"Double Array");
601 result = readDoubleAsIntArray == intArray;
602 if(!result) ++FailedTests;
605 <<
"readDoubleAsIntArray = " << readDoubleAsIntArray <<
" == intArray = " << intArray <<
" ? "
606 << (result ?
"passed" :
"failed")
633 cout <<
"Setting a array of doubles as a std::string parameter directly ...\n";
653 readDoubleArray = Teuchos::getArrayFromStringParameter<double>(PL_Main,
"Double Array");
655 doubleAsIntArray(readDoubleArray.
size());
656 for(
int i=0;i<static_cast<int>(readDoubleArray.
size());++i)
657 doubleAsIntArray[i] = static_cast<int>(readDoubleArray[i]);
658 result = doubleAsIntArray == intArray;
659 if(!result) ++FailedTests;
662 <<
"doubleAsIntArray = " << doubleAsIntArray <<
" == intArray = " << intArray <<
" ? "
663 << (result ?
"passed" :
"failed")
677 #ifndef JANUS_STLPORT
678 double (*pt2Function) (double, double);
679 PL_Main.
set(
"Address to Simple Function", &
Plus );
680 pt2Function = Teuchos::getParameter<double(*)(double,double)>( PL_Main,
"Address to Simple Function" );
682 cout<<
"Can we pass a pointer to a function to a parameter list ... ";
684 if ( pt2Function( 1.0, 2.0 ) != 3.0 ) {
685 if (verbose) cout<<
"no"<<std::endl;
688 if (verbose) cout<<
"yes"<<std::endl;
700 void *someIntPtr = &someInt;
701 pl.
set(
"Some Pointer", someIntPtr);
702 void *someIntPtrRtn = getParameter<void*>(pl,
"Some Pointer");
705 cout <<
"someIntPtrRtn = " << someIntPtrRtn <<
" == " << someIntPtr <<
" : ";
706 if (someIntPtrRtn == someIntPtr) {
707 if (verbose) cout <<
"passed\n";
710 if (verbose) cout <<
"failed\n";
719 ParameterList::ConstIterator iter;
724 cout <<
" printing using public iterators "
728 for (iter = PL_Main.
begin(); iter != PL_Main.
end(); ++iter)
731 const std::string& name = PL_Main.
name(iter);
734 if (verbose) cout << name << std::endl;
736 ParameterList::ConstIterator i;
737 for (i=sublist.
begin(); i != sublist.
end(); ++i)
739 const std::string& nm = sublist.
name(i);
743 if (verbose) cout <<
" " << nm << std::endl;
744 if (verbose) Teuchos::getValue<ParameterList>(v).print(cout, 6);
748 if (verbose) cout <<
" " << nm <<
" " << v << std::endl;
754 if (verbose) cout << name <<
" " << val << std::endl;
759 #if defined(HAVE_TEUCHOS_EXTENDED)
766 cout <<
"writing to XML std::ostream" << std::endl;
771 cout <<
"writing to XML file" << std::endl;
776 cout <<
"reading from XML file" << std::endl;
780 if (verbose) readBack.
print(cout);
783 cout <<
"reading from XML std::string" << std::endl;
785 std::ifstream xmlInFile(
"PL_Main.xml");
787 while(!xmlInFile.eof()) {
789 std::getline(xmlInFile,line);
790 xmlStr += line +
"\n";
794 if (verbose) readBack.
print(cout);
799 catch(
const std::exception& e)
802 std::cerr <<
"caught std::exception:\n\n";
804 std::cerr << e.what() << std::endl;
809 #endif // defined(HAVE_TEUCHOS_EXTENDED)
817 cout <<
"The Final Parameter List" << std::endl;
821 cout <<
"The unused parameters" << std::endl;
831 cout <<
"Accessing a sublist using the wrong name (should throw a Teuchos::Exceptions::InvalidParameterName std::exception)...\n";
836 if (verbose) cout <<
"Did not throw std::exception, error!\n";
840 std::cerr <<
"caught expected Teuchos::Exceptions::InvalidParameterName:\n\n";
842 std::cerr << e.what() << std::endl;
846 cout <<
"Accessing a parameter using the wrong name (should throw a Teuchos::Exceptions::InvalidParameterName std::exception)...\n";
850 Teuchos::getParameter<int>(PL_Main.
sublist(
"Direction").
sublist(
"Newton").
sublist(
"Linear Solver"),
"Tolerances");
851 if (verbose) cout <<
"Did not throw std::exception, error!\n";
856 std::cerr <<
"caught expected Teuchos::Exceptions::InvalidParameterName:\n\n";
858 std::cerr << e.what() << std::endl;
861 catch(
const std::exception &e) {
863 std::cerr <<
"caught unexpected std::exception:\n\n";
865 std::cerr << e.what() << std::endl;
872 cout <<
"Accessing a parameter using the wrong parameter type (should throw a Teuchos::Exceptions::InvalidParameterType std::exception)...\n";
876 Teuchos::getParameter<int>(PL_Main.
sublist(
"Direction").
sublist(
"Newton").
sublist(
"Linear Solver"),
"Tolerance");
877 if (verbose) cout <<
"Did not throw std::exception, error!\n";
882 std::cerr <<
"caught expected Teuchos::Exceptions::InvalidParameterType:\n\n";
884 std::cerr << e.what() << std::endl;
887 catch(
const std::exception &e) {
889 std::cerr <<
"caught unexpected std::exception:\n\n";
891 std::cerr << e.what() << std::endl;
905 Teuchos::setStringToIntegralParameter<int>(
906 "Nonlinear Solver",
"Line Search Based",
907 "Selects the type of nonlinear solver to use",
908 Teuchos::tuple<std::string>(
"Line Search Based",
"Trust Region Based"),
931 linesearchMaxItersValiator =
rcp(
934 AcceptedTypes(
false).allowInt(
true).allowDouble(
true).allowString(
true)
939 ,
"The maximum number of inner linear search iterations allowed."
940 ,linesearchMaxItersValiator
947 linSolveTolValidator =
rcp(
950 AcceptedTypes(
false).allowDouble(
true).allowString(
true)
956 ,
"Select the linear solve tolerance"
957 ,linSolveTolValidator
962 cout <<
"Validating the parameter list against itself (should not throw std::exception)...\n";
967 if (verbose) cout <<
"Did not throw std::exception, success!\n\n";
969 catch(
const std::exception &e) {
971 std::cerr <<
"caught unexpected std::exception:\n\n";
973 std::cerr << e.what() << std::endl;
980 cout <<
"Adding an invalid parameter type then validating (should throw a Teuchos::Exceptions::InvalidParameterType std::exception)...\n";
984 PL_Main.
sublist(
"Line Search").
sublist(
"Polynomial").
set(
"Max Iters",(
short int)(3));
986 if (verbose) cout <<
"Did not throw std::exception, error!\n";
991 std::cerr <<
"caught expected Teuchos::Exceptions::InvalidParameterType:\n\n";
993 std::cerr << e.what() << std::endl;
996 catch(
const std::exception &e) {
998 std::cerr <<
"caught unexpected std::exception:\n\n";
1000 std::cerr << e.what() << std::endl;
1008 cout <<
"Adding an invalid parameter name then validating (should throw a Teuchos::Exceptions::InvalidParameterName std::exception)...\n";
1014 if (verbose) cout <<
"Did not throw std::exception, error!\n";
1019 std::cerr <<
"caught expected Teuchos::Exceptions::InvalidParameterName:\n\n";
1021 std::cerr << e.what() << std::endl;
1024 catch(
const std::exception &e) {
1026 std::cerr <<
"caught unexpected std::exception:\n\n";
1028 std::cerr << e.what() << std::endl;
1036 cout <<
"Adding an invalid parameter type then validating using validator (should throw a Teuchos::Exceptions::InvalidParameterType std::exception)...\n";
1040 PL_Main.
set(
"Nonlinear Solver",
int(0));
1042 if (verbose) cout <<
"Did not throw std::exception, error!\n";
1047 std::cerr <<
"caught expected Teuchos::Exceptions::InvalidParameterType:\n\n";
1049 std::cerr << e.what() << std::endl;
1052 catch(
const std::exception &e) {
1054 std::cerr <<
"caught unexpected std::exception:\n\n";
1056 std::cerr << e.what() << std::endl;
1060 PL_Main.
set(
"Nonlinear Solver",
"Line Search Based");
1064 cout <<
"Adding an invalid parameter value then validating using validator (should throw a Teuchos::Exceptions::InvalidParameterValue std::exception)...\n";
1068 PL_Main.
set(
"Nonlinear Solver",
"LineSearch Based");
1070 if (verbose) cout <<
"Did not throw std::exception, error!\n";
1075 std::cerr <<
"caught expected Teuchos::Exceptions::InvalidParameterValue:\n\n";
1076 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1079 catch(
const std::exception &e) {
1081 std::cerr <<
"caught unexpected std::exception:\n\n";
1082 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1086 PL_Main.
set(
"Nonlinear Solver",
"Line Search Based");
1090 cout <<
"Use the validator to access integral value (should *not* throw std::exception)...\n";
1095 nonlinearSolverValue = Teuchos::getIntegralValue<int>(PL_Main,
"Nonlinear Solver");
1097 l_result = (nonlinearSolverValue == 0);
1099 <<
"Read value = " << nonlinearSolverValue <<
" == 0 : "
1100 << ( l_result ?
"passed" :
"failed") <<
"\n";
1101 if(!l_result) ++FailedTests;
1103 catch(
const std::exception &e) {
1105 std::cerr <<
"caught unexpected std::exception:\n\n";
1106 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1113 cout <<
"Use the validator to access std::string value (should *not* throw std::exception)...\n";
1118 nonlinearSolverValue = Teuchos::getStringValue<int>(PL_Main,
"Nonlinear Solver");
1120 l_result = (nonlinearSolverValue ==
"Line Search Based");
1122 <<
"Read value = \"" << nonlinearSolverValue <<
" == \"Line Search Based\" : "
1123 << ( l_result ?
"passed" :
"failed") <<
"\n";
1124 if(!l_result) ++FailedTests;
1126 catch(
const std::exception &e) {
1128 std::cerr <<
"caught unexpected std::exception:\n\n";
1129 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1143 cout <<
"Validating and setting defaults for an empty parameter list (should not throw) ...\n";
1148 if (verbose) cout <<
"Did not throw std::exception, success!\n\n";
1150 catch(
const std::exception &e) {
1152 std::cerr <<
"caught unexpected std::exception:\n\n";
1153 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1160 cout <<
"Parameter list with defaults set:" << std::endl;
1168 cout <<
"Checking that validatedPL and PL_Main_valid have the same values : ";
1174 cout << ( result ?
"passed" :
"failed" ) <<
"\n";
1183 for(
int type_i = 0; type_i < 3; ++type_i ) {
1186 = PL_Main.
sublist(
"Line Search",
true).
sublist(
"Polynomial",
true);
1195 Polynomial_sublist.
set(
"Max Iters",(
int)(3));
1198 typeName =
"double";
1199 Polynomial_sublist.
set(
"Max Iters",(
double)(3.0));
1202 typeName =
"std::string";
1203 Polynomial_sublist.
set(
"Max Iters",(std::string)(
"3"));
1213 cout <<
"Use the external number validator to access a "<<typeName<<
" as an int ...\n";
1219 = linesearchMaxItersValiator->
getInt(
1224 l_result = (lineserchMaxIters == int(3));
1226 <<
"Read value = " << lineserchMaxIters <<
" == 3 : "
1227 << ( l_result ?
"passed" :
"failed") <<
"\n";
1228 if(!l_result) ++FailedTests;
1230 catch(
const std::exception &e) {
1232 std::cerr <<
"caught unexpected std::exception:\n\n";
1233 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1240 cout <<
"Use the external number validator to access a "<<typeName<<
" as a double ...\n";
1246 = linesearchMaxItersValiator->
getDouble(
1251 l_result = (lineserchMaxIters == double(3.0));
1253 <<
"Read value = " << lineserchMaxIters <<
" == 3 : "
1254 << ( l_result ?
"passed" :
"failed") <<
"\n";
1255 if(!l_result) ++FailedTests;
1257 catch(
const std::exception &e) {
1259 std::cerr <<
"caught unexpected std::exception:\n\n";
1260 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1267 cout <<
"Use the external number validator to access a "<<typeName<<
" as a std::string ...\n";
1273 = linesearchMaxItersValiator->
getString(
1278 l_result = (lineserchMaxIters ==
"3");
1280 <<
"Read value = \"" << lineserchMaxIters <<
"\" == \"3\" : "
1281 << ( l_result ?
"passed" :
"failed") <<
"\n";
1282 if(!l_result) ++FailedTests;
1284 catch(
const std::exception &e) {
1286 std::cerr <<
"caught unexpected std::exception:\n\n";
1287 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1296 cout <<
"Use the nomember help function to access a "<<typeName<<
" as an int ...\n";
1302 = Teuchos::getIntParameter(
1307 l_result = (lineserchMaxIters == int(3));
1309 <<
"Read value = " << lineserchMaxIters <<
" == 3 : "
1310 << ( l_result ?
"passed" :
"failed") <<
"\n";
1311 if(!l_result) ++FailedTests;
1313 catch(
const std::exception &e) {
1315 std::cerr <<
"caught unexpected std::exception:\n\n";
1316 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1323 cout <<
"Use the nomember help function to access a "<<typeName<<
" as a double ...\n";
1329 = Teuchos::getDoubleParameter(
1334 l_result = (lineserchMaxIters == double(3.0));
1336 <<
"Read value = " << lineserchMaxIters <<
" == 3 : "
1337 << ( l_result ?
"passed" :
"failed") <<
"\n";
1338 if(!l_result) ++FailedTests;
1340 catch(
const std::exception &e) {
1342 std::cerr <<
"caught unexpected std::exception:\n\n";
1343 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1350 cout <<
"Use the nomember help function to access a "<<typeName<<
" as a std::string ...\n";
1356 = Teuchos::getNumericStringParameter(
1361 l_result = (lineserchMaxIters ==
"3");
1363 <<
"Read value = \"" << lineserchMaxIters <<
"\" == \"3\" : "
1364 << ( l_result ?
"passed" :
"failed") <<
"\n";
1365 if(!l_result) ++FailedTests;
1367 catch(
const std::exception &e) {
1369 std::cerr <<
"caught unexpected std::exception:\n\n";
1370 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1382 for(
int type_i = 0; type_i < 4; ++type_i ) {
1385 = PL_Main.
sublist(
"Line Search",
true).
sublist(
"Polynomial",
true);
1394 Teuchos::setIntParameter(
"Max Iters",3,
"",&Polynomial_sublist);
1397 typeName =
"double";
1398 Teuchos::setDoubleParameter(
"Max Iters",3.0,
"",&Polynomial_sublist);
1401 typeName =
"std::string";
1402 Teuchos::setNumericStringParameter(
"Max Iters",
"3",
"",&Polynomial_sublist);
1405 typeName =
"long long";
1406 Teuchos::setLongLongParameter(
"Max Iters",3,
"",&Polynomial_sublist);
1416 cout <<
"Use the nomember help function to access a "<<typeName<<
" as an int ...\n";
1422 = Teuchos::getIntParameter(
1427 l_result = (lineserchMaxIters == int(3));
1429 <<
"Read value = " << lineserchMaxIters <<
" == 3 : "
1430 << ( l_result ?
"passed" :
"failed") <<
"\n";
1431 if(!l_result) ++FailedTests;
1433 catch(
const std::exception &e) {
1435 std::cerr <<
"caught unexpected std::exception:\n\n";
1436 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1443 cout <<
"Use the nomember help function to access a "<<typeName<<
" as a double ...\n";
1449 = Teuchos::getDoubleParameter(
1454 l_result = (lineserchMaxIters == double(3.0));
1456 <<
"Read value = " << lineserchMaxIters <<
" == 3 : "
1457 << ( l_result ?
"passed" :
"failed") <<
"\n";
1458 if(!l_result) ++FailedTests;
1460 catch(
const std::exception &e) {
1462 std::cerr <<
"caught unexpected std::exception:\n\n";
1463 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1470 cout <<
"Use the nomember help function to access a "<<typeName<<
" as a std::string ...\n";
1476 = Teuchos::getNumericStringParameter(
1481 l_result = (lineserchMaxIters ==
"3");
1483 <<
"Read value = \"" << lineserchMaxIters <<
"\" == \"3\" : "
1484 << ( l_result ?
"passed" :
"failed") <<
"\n";
1485 if(!l_result) ++FailedTests;
1487 catch(
const std::exception &e) {
1489 std::cerr <<
"caught unexpected std::exception:\n\n";
1490 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1497 cout <<
"Use the nomember help function to access a "<<typeName<<
" as an long long ...\n";
1503 = Teuchos::getLongLongParameter(
1508 l_result = (lineserchMaxIters == (
long long)(3));
1510 <<
"Read value = " << lineserchMaxIters <<
" == 3 : "
1511 << ( l_result ?
"passed" :
"failed") <<
"\n";
1512 if(!l_result) ++FailedTests;
1514 catch(
const std::exception &e) {
1516 std::cerr <<
"caught unexpected std::exception:\n\n";
1517 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1529 for(
int type_i = 0; type_i < 3; ++type_i ) {
1534 = PL_Main.
sublist(
"Line Search",
true).
sublist(
"Polynomial",
true);
1543 Teuchos::setIntParameter(
"Max Iters",3,
"",&Polynomial_sublist);
1546 typeName =
"double";
1547 Teuchos::setDoubleParameter(
"Max Iters",3.0,
"",&Polynomial_sublist);
1550 typeName =
"std::string";
1551 Teuchos::setNumericStringParameter(
"Max Iters",
"3",
"",&Polynomial_sublist);
1561 cout <<
"Use validateParemetersAndSetDefaults(...) to access a "<<typeName<<
" as an int ...\n";
1565 Teuchos::setIntParameter(
1567 &valid_PL_Main.
sublist(
"Line Search",
true).
sublist(
"Polynomial",
true)
1570 copied_PL_Main.validateParametersAndSetDefaults(valid_PL_Main);
1573 = Teuchos::getParameter<int>(
1574 copied_PL_Main.sublist(
"Line Search",
true).sublist(
"Polynomial",
true)
1578 l_result = (lineserchMaxIters == int(3));
1580 <<
"Read value = " << lineserchMaxIters <<
" == 3 : "
1581 << ( l_result ?
"passed" :
"failed") <<
"\n";
1582 if(!l_result) ++FailedTests;
1584 catch(
const std::exception &e) {
1586 std::cerr <<
"caught unexpected std::exception:\n\n";
1587 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1594 cout <<
"Use validateParemetersAndSetDefaults(...) to access a "<<typeName<<
" as a double ...\n";
1598 Teuchos::setDoubleParameter(
1599 "Max Iters", 0.0,
"",
1600 &valid_PL_Main.
sublist(
"Line Search",
true).
sublist(
"Polynomial",
true)
1603 copied_PL_Main.validateParametersAndSetDefaults(valid_PL_Main);
1606 = Teuchos::getParameter<double>(
1607 copied_PL_Main.sublist(
"Line Search",
true).sublist(
"Polynomial",
true)
1611 l_result = (lineserchMaxIters == double(3.0));
1613 <<
"Read value = " << lineserchMaxIters <<
" == 3 : "
1614 << ( l_result ?
"passed" :
"failed") <<
"\n";
1615 if(!l_result) ++FailedTests;
1617 catch(
const std::exception &e) {
1619 std::cerr <<
"caught unexpected std::exception:\n\n";
1620 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1627 cout <<
"Use validateParemetersAndSetDefaults(...) to access a "<<typeName<<
" as a std::string ...\n";
1631 Teuchos::setNumericStringParameter(
1632 "Max Iters",
"0",
"",
1633 &valid_PL_Main.
sublist(
"Line Search",
true).
sublist(
"Polynomial",
true)
1636 copied_PL_Main.validateParametersAndSetDefaults(valid_PL_Main);
1639 = Teuchos::getParameter<std::string>(
1640 copied_PL_Main.sublist(
"Line Search",
true).sublist(
"Polynomial",
true)
1644 l_result = (lineserchMaxIters ==
"3");
1646 <<
"Read value = \"" << lineserchMaxIters <<
"\" == \"3\" : "
1647 << ( l_result ?
"passed" :
"failed") <<
"\n";
1648 if(!l_result) ++FailedTests;
1650 catch(
const std::exception &e) {
1652 std::cerr <<
"caught unexpected std::exception:\n\n";
1653 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1662 cout <<
"Adding an invalid sublist then validating (should throw a Teuchos::Exceptions::InvalidParameterName std::exception)...\n";
1668 if (verbose) cout <<
"Did not throw std::exception, error!\n";
1673 std::cerr <<
"caught expected Teuchos::Exceptions::InvalidParameterName:\n\n";
1674 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1677 catch(
const std::exception &e) {
1679 std::cerr <<
"caught unexpected std::exception:\n\n";
1680 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1688 cout <<
"Validating only the top level list (should not throw std::exception)...\n";
1693 if (verbose) cout <<
"Did not throw std::exception, success!\n\n";
1695 catch(
const std::exception &e) {
1697 std::cerr <<
"caught unexpected std::exception:\n\n";
1698 OSTab tab(std::cerr); std::cerr << e.what() << std::endl;
1709 cout <<
"Checking that PL_Main == PL_Main == true : ";
1711 result = (PL_Main == PL_Main);
1715 cout << ( result ?
"passed" :
"failed" ) <<
"\n";
1721 cout <<
"Checking that PL_Main != PL_Main == false : ";
1723 result = !(PL_Main != PL_Main);
1727 cout << ( result ?
"passed" :
"failed" ) <<
"\n";
1733 cout <<
"Checking that PL_Main and PL_Main have the same values : ";
1739 cout << ( result ?
"passed" :
"failed" ) <<
"\n";
1745 cout <<
"Create copy PL_Main_copy, change PL_Main_copy, and check that PL_Main != PL_Main == true : ";
1748 PL_Main_copy.
sublist(
"Line Search",
true).
sublist(
"Polynomial",
true).
set(
"Max Iters",100);
1749 result = (PL_Main_copy != PL_Main);
1753 cout << ( result ?
"passed" :
"failed" ) <<
"\n";
1763 cout <<
"The Final Parameter List with Types and Documentation" << std::endl;
1767 cout <<
"The unused parameters" << std::endl;
1770 cout <<
"Number of Failed Tests : " << FailedTests << std::endl;
1781 if(!success) ++FailedTests;
1783 if ( FailedTests > 0 ) {
1784 cout <<
"End Result: TEST FAILED" << std::endl;
1788 if ( FailedTests == 0 )
1789 cout <<
"End Result: TEST PASSED" << std::endl;
void print() const
Print function to use in debugging in a debugger.
RCP< T > rcp(const boost::shared_ptr< T > &sptr)
Conversion function that takes in a boost::shared_ptr object and spits out a Teuchos::RCP object...
const std::string & name() const
The name of this ParameterList.
static int getRank()
The rank of the calling process in MPI_COMM_WORLD.
ConstIterator end() const
An iterator pointing beyond the last entry.
std::string typeName(const T &t)
Template function for returning the concrete type name of a passed-in object.
T & get(const std::string &name, T def_value)
Return the parameter's value, or the default value if it is not there.
basic_FancyOStream< CharT, Traits > & o() const
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT bool haveSameValues(const ParameterList &list1, const ParameterList &list2, bool verbose=false)
Returns true if two parameter lists have the same values.
This object is held as the "value" in the Teuchos::ParameterList std::map.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void updateParametersFromXmlFile(const std::string &xmlFileName, const Ptr< ParameterList > ¶mList)
Reads XML parameters from a file and updates those already in the given parameter list...
const T & getConst(T &t)
Return a constant reference to an object given a non-const reference.
std::string getString(const ParameterEntry &entry, const std::string ¶mName="", const std::string &sublistName="", const bool activeQuery=true) const
Get a std::string value from a parameter entry.
RCP< ParameterList > sublist(const RCP< ParameterList > ¶mList, const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Return a RCP to a sublist in another RCP-ed parameter list.
ParameterList & set(std::string const &name, T &&value, std::string const &docString="", RCP< const ParameterEntryValidator > const &validator=null)
Templated set method.
Tabbing class for helping to create formated, indented output for a basic_FancyOStream object...
Initialize, finalize, and query the global MPI session.
Simple helper functions that make it easy to read and write XML to and from a parameterlist.
basic_OSTab< char > OSTab
ParameterEntry * getEntryPtr(const std::string &name)
Retrieves the pointer for an entry with the name name if it exists.
Determines the types that are accepted.
bool isParameter(const std::string &name) const
Whether the given parameter exists in this list.
bool remove(std::string const &name, bool throwIfNotExists=true)
Remove a parameter (does not depend on the type of the parameter).
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Deprecated.
Templated Parameter List class.
void setOption(const char option_true[], const char option_false[], bool *option_val, const char documentation[]=NULL)
Set a boolean option.
double Plus(double a, double b)
double getDouble(const ParameterEntry &entry, const std::string ¶mName="", const std::string &sublistName="", const bool activeQuery=true) const
Get a double value from a parameter entry. will call std::stod.
bool isSublist(const std::string &name) const
Whether the given sublist exists in this list.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void writeParameterListToXmlFile(const ParameterList ¶mList, const std::string &xmlFileName, RCP< const DependencySheet > depSheet=null)
Write parameters and sublist to an XML file.
#define TEUCHOS_STANDARD_CATCH_STATEMENTS(VERBOSE, ERR_STREAM, SUCCESS_FLAG)
Simple macro that catches and reports standard exceptions and other exceptions.
EParseCommandLineReturn parse(int argc, char *argv[], std::ostream *errout=&std::cerr) const
Parse a command line.
void validateParametersAndSetDefaults(ParameterList const &validParamList, int const depth=1000)
Validate the parameters in this list given valid selections in the input list and set defaults for th...
Ptr< T > inoutArg(T &arg)
create a non-persisting (required or optional) input/output argument for a function call...
ConstIterator begin() const
An iterator pointing to the first entry.
ParameterList::PrintOptions PLPrintOptions
bool isList() const
Return whether or not the value itself is a list.
std::string Teuchos_Version()
A list of parameters of arbitrary type.
void validateParameters(ParameterList const &validParamList, int const depth=1000, EValidateUsed const validateUsed=VALIDATE_USED_ENABLED, EValidateDefaults const validateDefaults=VALIDATE_DEFAULTS_ENABLED) const
Validate the parameters in this list given valid selections in the input list.
ParameterList & setParameters(const ParameterList &source)
int main(int argc, char *argv[])
const ParameterEntry & entry(ConstIterator i) const
Access to ParameterEntry (i.e., returns i->second)
int getInt(const ParameterEntry &entry, const std::string ¶mName="", const std::string &sublistName="", const bool activeQuery=true) const
Get an integer value from a parameter entry. will call std::stoi Note that std::stoi throws on badly ...
A MPI utilities class, providing methods for initializing, finalizing, and querying the global MPI se...
Basic command line parser for input from (argc,argv[])
void unused(std::ostream &os) const
Print out unused parameters in the ParameterList.
ParameterList & sublist(const std::string &name, bool mustAlreadyExist=false, const std::string &docString="")
Creates an empty sublist and returns a reference to the sublist name. If the list already exists...
Smart reference counting pointer class for automatic garbage collection.
RCP< basic_FancyOStream< CharT, Traits > > tab(const RCP< basic_FancyOStream< CharT, Traits > > &out, const int tabs=1, const std::basic_string< CharT, Traits > linePrefix="")
Create a tab for an RCP-wrapped basic_FancyOStream object to cause the indentation of all output auto...
std::string docString() const
Return the (optional) documentation std::string.
Standard implementation of a ParameterEntryValidator that accepts numbers from a number of different ...
T * get() const
Get the raw C++ pointer to the underlying object.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void writeParameterListToXmlOStream(const ParameterList ¶mList, std::ostream &xmlOut, RCP< const DependencySheet > depSheet=null)
Write parameters and sublists in XML format to an std::ostream.
TEUCHOSPARAMETERLIST_LIB_DLL_EXPORT void updateParametersFromXmlString(const std::string &xmlStr, const Ptr< ParameterList > ¶mList, bool overwrite=true)
Reads XML parameters from a std::string and updates those already in the given parameter list...
Class that helps parse command line input arguments from (argc,argv[]) and set options.
#define TEUCHOS_TEST_FOR_EXCEPT(throw_exception_test)
This macro is designed to be a short version of TEUCHOS_TEST_FOR_EXCEPTION() that is easier to call...
Reference-counted smart pointer for managing arrays.