This file is indexed.

/usr/include/trilinos/BelosStatusTestFactory.hpp is in libtrilinos-belos-dev 12.4.2-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
//@HEADER
// ************************************************************************
//
//                 Belos: Block Linear Solvers Package
//                  Copyright 2004 Sandia Corporation
//
// Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
// the U.S. Government retains certain rights in this software.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Neither the name of the Corporation nor the names of the
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Questions? Contact Michael A. Heroux (maherou@sandia.gov)
//
// ************************************************************************
//@HEADER

#include <BelosStatusTestCombo.hpp>
#include <BelosStatusTestGenResNorm.hpp>
#include <BelosStatusTestImpResNorm.hpp>
#include <BelosStatusTestMaxIters.hpp>
#include <Teuchos_ParameterList.hpp>

namespace Belos {

  /// \class StatusTestFactory
  /// \author Mark Hoemmen
  /// \brief A factory for making common cases of stopping criteria.
  ///
  /// Belos::StatusTest is an abstract interface for different
  /// stopping criteria ("status tests") for the iterative methods
  /// implemented in Belos.  There are many different kinds of status
  /// tests implemented in Belos, and they can be combined in
  /// different ways to make aggregate status tests (e.g., "reached
  /// max number of iterations, or residual norm converged").  We
  /// include a few common cases of those aggregate status tests in
  /// this factory class so that people implementing subclasses of
  /// Belos::SolverManager can minimize code duplication.  
  ///
  /// This factory also provides ways to change the convergence
  /// tolerance and/or maximum number of iterations, in place, for
  /// existing and possibly aggregate status tests.  This is an
  /// experimental optimization that attempts to avoid the overhead of
  /// constructing new status tests, but it comes at the cost of
  /// requiring \f$O(1)\f$ dynamic casts for each test in an aggregate
  /// collection (StatusTestCombo) of tests.  It may be useful if you
  /// are not allowed to create new status test objects, for example
  /// if you only have access to them through a copied RCP and not
  /// through a reference to the RCP.
  /// 
  /// The general idea of factory classes like this one is to avoid
  /// duplicated code whenever possible.  Scientific programmers' time
  /// is limited, so it's often better to avoid duplicated code, even
  /// if it means adding a new class or making the nonduplicated code
  /// a bit more complicated.
  template<class Scalar, class MV, class OP>
  class StatusTestFactory {
  public:
    typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType magnitude_type;
    typedef StatusTest<Scalar,MV,OP> base_test;
    typedef StatusTestGenResNorm<Scalar,MV,OP> res_norm_test;
    typedef StatusTestMaxIters<Scalar,MV,OP> max_iter_test;
    typedef StatusTestCombo<Scalar,MV,OP> combo_test;

    /// \brief Status test suitable for (preconditioned) GMRES.
    ///
    /// The returned status test stops iteration if the maximum number
    /// of iterations has been reached, OR if the residual norm has
    /// converged.  The latter first iterates until the implicit
    /// residual norm (the one computed as a side effect of solving
    /// the projected least-squares problem in GMRES) reaches the
    /// given tolerance.  Then, it switches to computing the explicit
    /// residual norm (\f$\|b - Ax\|_2\f$) and iterates until that has
    /// reached the tolerance.
    ///
    /// \param convTol [in] Convergence tolerance.  The meaning of
    ///   this depends on the scaling used.
    ///
    /// \param maxIterCount [in] Maximum number of iterations of GMRES
    ///   to execute before stopping.
    ///
    /// \param haveLeftPreconditioner [in] Whether we will be running
    ///   GMRES with a left preconditioner.  This affects the residual
    ///   norm computation.
    ///
    /// \param implicitScaleType [in] Type of scaling to use for the
    ///   implicit residual norm computation.  Default is to scale by
    ///   the norm of the preconditioned (if applicable) initial
    ///   residual vector.  The \c stringToScaleType() method might be
    ///   useful.
    ///
    /// \param explicitScaleType [in] Type of scaling to use for the
    ///   explicit residual norm computation.  Default is to scale by
    ///   the norm of the (nonpreconditioned) initial residual vector.
    ///   The \c stringToScaleType() method might be useful.
    ///
    /// \param blockSize [in] Number of linear system(s) that the
    ///   solver can solve at one time.  Block iterative methods
    ///   support blockSize > 1.  Non-block iterative methods require
    ///   blockSize = 1.  This parameter is only used to verify
    ///   defQuorum, if the latter is not -1.
    ///
    /// \param defQuorum [in] "Deflation Quorum": number of converged
    ///   systems before deflation is allowed.  Cannot be larger than
    ///   "Block Size".  -1 is the default in
    ///   Belos::StatusTestGenResNorm, and it means that all of the
    ///   systems must converge before deflation is allowed.
    ///
    /// \param showMaxResNormOnly [in] "Show Maximum Residual Norm
    ///   Only" is only meaningful when the "Block Size" parameter is
    ///   > 1.
    ///
    /// \return Aggregate status test for GMRES.
    static Teuchos::RCP<base_test>
    gmresTest (const magnitude_type convTol,
	       const int maxIterCount,
	       const bool haveLeftPreconditioner,
	       const ScaleType implicitScaleType = Belos::NormOfPrecInitRes,
	       const ScaleType explicitScaleType = Belos::NormOfInitRes,
	       const int blockSize = 1,
	       const int defQuorum = -1,
	       const bool showMaxResNormOnly = false);

    /// \brief Overloaded \c gmresTest() for ParameterList input.
    ///
    /// Does the same thing as the other \c gmresTest(), except it
    /// reads the values from the given parameter list.  The parameter
    /// list must be nonnull and valid.  We make only a modest effort
    /// to fill in default values for nonrequired parameters only.  We
    /// throw std::invalid_argument if any required parameter is
    /// missing or any provided value is invalid.
    static Teuchos::RCP<base_test>
    gmresTest (const bool haveLeftPreconditioner, 
	       const Teuchos::RCP<Teuchos::ParameterList>& params);

    /// \brief Change convergence tolerance and max number of iterations.
    ///
    /// The changes take place in place in the given status test.  If
    /// the status test is a StatusTestCombo, we recurse on all
    /// children, changing them if they are of the appropriate type.
    /// If we don't find anything to change, we do nothing, except
    /// report back in the return value.
    ///
    /// \return First element of the pair is whether we found at least
    ///   one residual norm test for which to change the convergence
    ///   tolerance.  Second element is whether we found at least one
    ///   maximum number of iterations test for which to change the
    ///   maximum iteration count.
    ///
    /// \note This method does nothing to protect against infinite
    ///   recursion if the StatusTestCombo tests were constructed
    ///   incorrectly.  However, StatusTestCombo itself prevents you
    ///   from forming loops in the graph of status tests, so this
    ///   method should always terminate.
    ///
    /// \note The given status test does _not_ need to be one that
    ///   this factory created.  This method should work with any
    ///   Belos::StatusTest instance.
    ///
    static std::pair<bool, bool>
    changeConvTolAndMaxIters (const Teuchos::RCP<base_test>& test, 
			      const magnitude_type convTol,
			      const int maxIterCount);

    /// \brief Change max number of iterations in place.
    /// 
    /// See the notes for \c changeConvTolAndMaxIters().
    ///
    /// \return Whether we found at least one test to change.
    static bool
    changeMaxNumIters (const Teuchos::RCP<base_test>& test, 
		       const int maxIterCount);

    /// \brief Change convergence tolerance in place.
    /// 
    /// See the notes for \c changeConvTolAndMaxIters().
    ///
    /// \return Whether we found at least one test to change.
    static bool
    changeConvTol (const Teuchos::RCP<base_test>& test, 
		   const magnitude_type convTol);

    /// Convert string to enum that tells residual test how to scale.
    ///
    /// \param scaleType [in] A string describing the type of
    /// scaling to be used in a residual norm convergence test.
    /// Valid values include:
    /// - "Norm of Initial Residual"
    /// - "Norm of Preconditioned Initial Residual"
    /// - "Norm of RHS" / "Norm of Right-Hand Side"
    /// - "None" (no scaling)
    ///
    /// \return The ScaleType enum value that tells the residual test
    ///   how to perform scaling.
    static ScaleType
    stringToScaleType (const std::string& scaleType);

    /// \brief Default parameters for a status test suitable for GMRES.
    ///
    /// This method is nonconst because it creates and caches the list
    /// of default parameters on demand.
    Teuchos::RCP<const Teuchos::ParameterList> getValidGmresParameters ();

  private:
    //! Default parameters for a status test suitable for GMRES.
    Teuchos::RCP<const Teuchos::ParameterList> defaultGmresParams_;
  };


  template<class Scalar, class MV, class OP>
  Teuchos::RCP<typename StatusTestFactory<Scalar, MV, OP>::base_test>
  StatusTestFactory<Scalar, MV, OP>::
  gmresTest (const typename StatusTestFactory<Scalar, MV, OP>::magnitude_type convTol,
	     const int maxIterCount,
	     const bool haveLeftPreconditioner,
	     const ScaleType implicitScaleType = NormOfPrecInitRes,
	     const ScaleType explicitScaleType = NormOfInitRes,
	     const int blockSize = 1,
	     const int defQuorum = -1,
	     const bool showMaxResNormOnly = false)
  {
    using Teuchos::null;
    using Teuchos::ParameterList;
    using Teuchos::RCP;
    using Teuchos::rcp;

    TEUCHOS_TEST_FOR_EXCEPTION(blockSize < 1, std::invalid_argument,
		       "blockSize (= " << blockSize << ") must be >= 1.");
    TEUCHOS_TEST_FOR_EXCEPTION(defQuorum > blockSize, std::invalid_argument,
		       "defQuorum (= " << defQuorum << ") may be no larger "
		       "than blockSize (= " << blockSize << ").");

    // The "implicit" residual test checks the "native" residual
    // norm to determine if convergence was achieved.  It is less
    // expensive than the "explicit" residual test.
    RCP<res_norm_test> implicitTest 
      = rcp (new res_norm_test (convTol, defQuorum));
    implicitTest->defineScaleForm (stringToScaleType (implicitScaleType), 
				   Belos::TwoNorm);
    implicitTest->setShowMaxResNormOnly (showMaxResNormOnly);

    // If there's a left preconditioner, create a combined status
    // test that check first the "explicit," then the "implicit"
    // residual norm, requiring that both have converged to within
    // the specified tolerance.  Otherwise, we only perform the
    // "implicit" test.
    RCP<res_norm_test> explicitTest;
    if (haveLeftPreconditioner) { // ! problem_->getLeftPrec().is_null()
      explicitTest = rcp (new res_norm_test (convTol, defQuorum));
      explicitTest->defineResForm (res_norm_test::Explicit, Belos::TwoNorm);
      explicitTest->defineScaleForm (stringToScaleType (explicitScaleType),
				     Belos::TwoNorm);
      explicitTest->setShowMaxResNormOnly (showMaxResNormOnly);
    }
    else {
      explicitTest = null;
    }

    // Full convergence test:
    //
    // First, the implicit residual norm test,
    // Followed by the explicit residual norm test if applicable,
    // Followed by the user-defined convergence test if supplied.
    RCP<base_test> convTest;
    if (explicitTest.is_null()) {
      convTest = implicitTest;
    } 
    else {
      // The "explicit" residual test is only performed once the
      // native ("implicit") residual is below the convergence
      // tolerance.
      convTest = rcp (new combo_test (combo_test::SEQ, 
				      implicitTest, 
				      explicitTest));
    }

    // Stopping criterion for maximum number of iterations.
    RCP<max_iter_test> maxIterTest = rcp (new max_iter_test (maxIterCount));

    // The "final" stopping criterion:
    //
    // Either we've run out of iterations, OR we've converged.
    return rcp (new combo_test (combo_test::OR, maxIterTest, convTest));
  }


  template<class Scalar, class MV, class OP>
  Teuchos::RCP<typename StatusTestFactory<Scalar, MV, OP>::base_test>
  StatusTestFactory<Scalar, MV, OP>::
  gmresTest (const bool haveLeftPreconditioner, 
	     const Teuchos::RCP<Teuchos::ParameterList>& params)
  {
    using Teuchos::Exceptions::InvalidParameter;
    using std::string;
    typedef Teuchos::ScalarTraits<magnitude_type> STM;

    // "Convergence Tolerance" is a required parameter and must be
    // nonnegative.
    const magnitude_type convTol = 
      params->get<magnitude_type> ("Convergence Tolerance");
    TEUCHOS_TEST_FOR_EXCEPTION(convTol < STM::zero(), std::invalid_argument,
		       "Convergence tolerance " << convTol 
		       << " is negative.");
    // "Maximum Iterations" is a required parameter and must be nonnegative.
    const int maxIterCount = params->get<int> ("Maximum Iterations");
    TEUCHOS_TEST_FOR_EXCEPTION(maxIterCount < 0, std::invalid_argument,
		       "Maximum number of iterations " << maxIterCount
		       << " is negative.");

    // PseudoBlockGmresSolMgr uses as defaults the preconditioned
    // initial residual for the implicit test, and the
    // unpreconditioned initial residual for the explicit test.
    ScaleType implicitScaleType = NormOfPrecInitRes;
    {
      const std::string defaultImplicitScaleType ("Norm of Preconditioned Initial Residual");
      implicitScaleType = stringToScaleType (params->get ("Implicit Residual Scaling", defaultImplicitScaleType));
    }
    ScaleType explicitScaleType = NormOfInitRes;
    {
      const std::string defaultExplicitScaleType ("Norm of Initial Residual");
      explicitScaleType = stringToScaleType (params->get ("Explicit Residual Scaling", defaultExplicitScaleType));
    }
    const int defaultBlockSize = 1;
    int blockSize = params->get ("Block Size", defaultBlockSize);

    const int defaultDefQuorum = -1;
    int defQuorum = params->get ("Deflation Quorum", defaultDefQuorum);

    const bool defaultShowMaxResNormOnly = false;
    bool showMaxResNormOnly = params->get ("Show Maximum Residual Norm Only", defaultShowMaxResNormOnly);

    return gmresTest (convTol, maxIterCount, haveLeftPreconditioner,
		      implicitScaleType, explicitScaleType, blockSize,
		      defQuorum, showMaxResNormOnly);
  }

  template<class Scalar, class MV, class OP>
  Teuchos::RCP<const Teuchos::ParameterList>
  getValidGmresParameters ()
  {
    using Teuchos::ParameterList;
    using Teuchos::parameterList;
    using Teuchos::RCP;
    typedef Teuchos::ScalarTraits<Scalar> STS;
    typedef Teuchos::ScalarTraits<magnitude_type> STM;

    if (defaultGmresParams_.is_null()) {
      // These two parameters are required.  We supply defaults only
      // as examples.
      const magnitude_type convTol = STS::squareroot (STS::eps());
      const int maxIterCount = 200;

      // PseudoBlockGmresSolMgr uses as defaults the preconditioned
      // initial residual for the implicit test, and the
      // unpreconditioned initial residual for the explicit test.  We
      // take these as the standard for all GMRES variants.
      const std::string defaultImplicitScaleType ("Norm of Preconditioned Initial Residual");
      const std::string defaultExplicitScaleType ("Norm of Initial Residual");
      const int defaultBlockSize = 1;
      const int defaultDefQuorum = -1;
      const bool defaultShowMaxResNormOnly = false;

      RCP<ParameterList> params = parameterList ();
      params->set ("Convergence Tolerance", convTol);
      params->set ("Maximum Iterations", maxIterCount);
      params->set ("Implicit Residual Scaling", defaultImplicitScaleType);
      params->set ("Explicit Residual Scaling", defaultExplicitScaleType);
      params->set ("Block Size", defaultBlockSize);
      params->set ("Deflation Quorum", defaultDefQuorum);
      params->set ("Show Maximum Residual Norm Only", defaultShowMaxResNormOnly);

      defaultGmresParams_ = params;
    }
    return defaultGmresParams_;
  }


  template<class Scalar, class MV, class OP>
  bool
  StatusTestFactory<Scalar, MV, OP>::
  changeMaxNumIters (const Teuchos::RCP<typename StatusTestFactory<Scalar, MV, OP>::base_test>& test,
		     const int maxIterCount)
  {
    using Teuchos::rcp_dynamic_cast;
    using Teuchos::nonnull;
    using Teuchos::RCP;
    using Teuchos::rcp;

    // We declare "success" if the test or at least one of its
    // children (if it's a combo_test) is a max_iter_test.
    bool success = false;
    RCP<max_iter_test> maxIterTest = rcp_dynamic_cast<max_iter_test> (test);
    if (nonnull (maxIterTest))
      {
	test->setMaxIters (maxIterCount);
	success = true;
      }
    else
      {
	RCP<combo_test> comboTest = rcp_dynamic_cast<combo_test> (test);
	if (nonnull (comboTest))
	  {
	    typedef typename combo_test::st_vector st_vector;
	    typedef typename st_vector::size_type size_type;
	    st_vector tests = test->getStatusTests ();
	    // We could use boost lambda to remove this for loop.
	    // Standard STL doesn't have a version of mem_fun for
	    // member functions that take > 1 argument.
	    for (size_type k = 0; result || k < tests.end(); ++k)
	      { // Recurse on all children, since it's possible for
		// more than one child to be a max_iter_test.
		const bool result =
		  changeMaxNumIters (tests[k], maxIterCount);
		success = result || success;
	      }
	  }
      }
    return success;
  }

  template<class Scalar, class MV, class OP>
  std::pair<bool, bool>
  StatusTestFactory<Scalar, MV, OP>::
  changeConvTolAndMaxNumIters (const Teuchos::RCP<typename StatusTestFactory<Scalar, MV, OP>::base_test>& test,
			       const typename StatusTestFactory<Scalar, MV, OP>::magnitude_type convTol,
			       const int maxIterCount)
  {
    using Teuchos::rcp_dynamic_cast;
    using Teuchos::nonnull;
    using Teuchos::RCP;
    using Teuchos::rcp;
    typedef StatusTestResNorm<Scalar,MV,OP> res_norm_base_test;
    RCP<max_iter_test> maxIterTest = rcp_dynamic_cast<max_iter_test> (test);

    // We declare "success" if we encountered a res_norm_base_test
    // _and_ a max_iter_test somewhere along the recursion path.
    bool foundResNormTest = false;
    bool foundMaxIterTest = false;

    RCP<res_norm_base_test> normTest = 
      rcp_dynamic_cast<res_norm_base_test> (test);
    if (nonnull (normTest))
      { 
	// NOTE (mfh 03 Mar 2011) setTolerance() returns an int
	// result.  However, all subclasses' implementations return 0
	// here, and all of them always (re)set the tolerance, so I
	// think it's OK to ignore the result.
	(void) test->setTolerance (convTol);
	foundResNormTest = true;
      }
    else 
      {
	RCP<max_iter_test> maxIterTest = 
	  rcp_dynamic_cast<max_iter_test> (test);
	if (nonnull (maxIterTest))
	  {
	    test->setMaxIters (maxIterCount);
	    foundMaxIterTest = true;
	  }
      }
    if (! foundResNormTest && ! foundMaxIterTest)
      {
	RCP<combo_test> comboTest = rcp_dynamic_cast<combo_test> (test);
	if (nonnull (comboTest))
	  {
	    typedef typename combo_test::st_vector st_vector;
	    typedef typename st_vector::size_type size_type;
	    st_vector tests = test->getStatusTests ();
	    // We could use boost lambda to remove this for loop.
	    // Standard STL doesn't have a version of mem_fun for
	    // member functions that take > 1 argument.
	    for (size_type k = 0; result || k < tests.end(); ++k)
	      { // Recurse on all children.
		const std::pair<bool, bool> result = 
		  changeConvTolAndMaxIters (tests[k], convTol, maxIterCount);
		foundResNormTest = result.first || foundResNormTest;
		foundMaxIterTest = result.second || foundMaxIterTest;		  
	      }
	  }
      }
    return std::make_pair (foundResNormTest, foundMaxIterTest);
  }

  template<class Scalar, class MV, class OP>
  bool
  StatusTestFactory<Scalar, MV, OP>::
  changeConvTol (const Teuchos::RCP<typename StatusTestFactory<Scalar, MV, OP>::base_test>& test,
		 const typename StatusTestFactory<Scalar, MV, OP>::magnitude_type convTol)
  {
    using Teuchos::rcp_dynamic_cast;
    using Teuchos::nonnull;
    using Teuchos::RCP;
    using Teuchos::rcp;
    typedef StatusTestResNorm<Scalar,MV,OP> res_norm_base_test;

    // We declare "success" if the test or at least one of its
    // children (if it's a combo_test) is a res_norm_base_test.
    bool success = false;
    RCP<res_norm_base_test> normTest = 
      rcp_dynamic_cast<res_norm_base_test> (test);
    if (nonnull (normTest))
      { 
	// NOTE (mfh 03 Mar 2011) setTolerance() returns an int
	// result.  However, all subclasses' implementations return 0
	// here, and all of them always (re)set the tolerance, so I
	// think it's OK to ignore the result.
	(void) test->setTolerance (convTol);
	success = true;
      }
    else
      {
	RCP<combo_test> comboTest = rcp_dynamic_cast<combo_test> (test);
	if (nonnull (comboTest))
	  {
	    typedef typename combo_test::st_vector st_vector;
	    typedef typename st_vector::size_type size_type;
	    st_vector tests = test->getStatusTests ();
	    // We could use boost lambda to remove this for loop.
	    // Standard STL doesn't have a version of mem_fun for
	    // member functions that take > 1 argument.
	    for (size_type k = 0; result || k < tests.end(); ++k)
	      { // Recurse on all children, since it's possible for
		// more than one child to be a res_norm_base_test.
		const bool result = changeConvTol (tests[k], convTol);
		success = result || success;
	      }
	  }
      }
    return success;
  }


  template<class Scalar, class MV, class OP>
  static ScaleType
  StatusTestFactory<Scalar, MV, OP>::
  stringToScaleType (const std::string& scaleType) 
  {
    const char* validNames[] = {
      "Norm of Initial Residual", 
      "Norm of Preconditioned Initial Residual",
      "Norm of RHS",
      "Norm of Right-Hand Side",
      "None"
    };
    const int numValidNames = 5;
    const ScaleType correspondingOutputs[] = {
      Belos::NormOfInitRes,
      Belos::NormOfPrecInitRes,
      Belos::NormOfRHS,
      Belos::NormOfRHS,
      Belos::None
    };
    for (int k = 0; k < numValidNames; ++k)
      {
	if (scaleType == validNames[k])
	  return correspondingOutputs[k];
      }
    TEUCHOS_TEST_FOR_EXCEPTION (true, std::logic_error,
			"Invalid residual scaling type \"" << scaleType 
			<< "\".");
  }

} // namespace Belos