sillyPowerMethod_serial.cpp

Click here for a more detailed discussion of this example program.

00001 // @HEADER
00002 // ***********************************************************************
00003 // 
00004 //    Thyra: Interfaces and Support for Abstract Numerical Algorithms
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 "sillyPowerMethod.hpp"
00030 #include "ExampleTridiagSerialLinearOp.hpp"
00031 #include "Thyra_TestingTools.hpp"
00032 #include "Teuchos_CommandLineProcessor.hpp"
00033 #include "Teuchos_VerboseObject.hpp"
00034 #include "Teuchos_as.hpp"
00035 #include "Teuchos_StandardCatchMacros.hpp"
00036 
00037 //
00038 // This example function is meant to show how easy it is to create
00039 // serial Thyra objects and use them with an ANA.
00040 //
00041 // This example uses a silly concrete tridiagonal matrix class
00042 // called SillyTridiagSerialLinearOp that demonstrates how
00043 // to write such subclasses.
00044 //
00045 template<class Scalar>
00046 bool runPowerMethodExample(
00047   const int dim,
00048   const int maxNumIters,
00049   const typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance,
00050   const bool  dumpAll
00051   )
00052 {
00053 
00054   using Teuchos::OSTab;
00055   using Teuchos::outArg;
00056   typedef Teuchos::ScalarTraits<Scalar> ST;
00057   typedef typename ST::magnitudeType    ScalarMag;
00058   
00059   bool success = true;
00060   bool result;
00061 
00062   Teuchos::RCP<Teuchos::FancyOStream> out =
00063     Teuchos::VerboseObjectBase::getDefaultOStream();
00064 
00065   *out << "\n***\n*** Running power method example using scalar type = \'"
00066     << ST::name() << "\' ...\n***\n" << std::scientific;
00067 
00068   //
00069   // (1) Setup the initial tridiagonal operator
00070   //
00071   //       [  2  -1             ]
00072   //       [ -1   2  -1         ]
00073   //  A =  [      .   .   .     ]
00074   //       [          -1  2  -1 ]
00075   //       [             -1   2 ]
00076   //
00077   *out << "\n(1) Constructing tridiagonal matrix A of dimension = " << dim << " ...\n";
00078   std::vector<Scalar> lower(dim-1), diag(dim), upper(dim-1);
00079   const Scalar one = ST::one(), two = Scalar(2)*one;
00080   int k = 0;
00081   diag[k] = two; upper[k] = -one;                        //  First row
00082   for( k = 1; k < dim - 1; ++k ) {
00083     lower[k-1] = -one; diag[k] = two; upper[k] = -one;   //  Middle rows
00084   }
00085   lower[k-1] = -one; diag[k] = two;                      //  Last row
00086   Teuchos::RCP<ExampleTridiagSerialLinearOp<Scalar> >
00087     A = Teuchos::rcp( new ExampleTridiagSerialLinearOp<Scalar>(dim,&lower[0],&diag[0],&upper[0]) );
00088   if (dumpAll) *out << "\nA =\n" << *A;
00089 
00090   //
00091   // (2) Run the power method ANA
00092   //
00093   *out << "\n(2) Running the power method on matrix A ...\n";
00094   Scalar     lambda      = ST::zero();
00095   {
00096     OSTab tab(out);
00097     result = sillyPowerMethod(*A, maxNumIters, tolerance, outArg(lambda), *out);
00098     if(!result) success = false;
00099     *out << "\nEstimate of dominate eigenvalue lambda = " << lambda << std::endl;
00100   }
00101 
00102   //
00103   // (3) Increase dominance of first eigenvalue
00104   //
00105   *out << "\n(3) Increasing first diagonal entry by factor of 10 ...\n";
00106   diag[0] *= 10;
00107   A->initialize(dim,&lower[0],&diag[0],&upper[0]);
00108   if (dumpAll) *out << "A =\n" << *A;
00109 
00110   //
00111   // (4) Run the power method ANA
00112   //
00113   *out << "\n(4) Running the power method again on matrix A ...\n";
00114   {
00115     OSTab tab(out);
00116     result = sillyPowerMethod(*A, maxNumIters, tolerance, outArg(lambda), *out);
00117     if(!result) success = false;
00118     *out << "\nEstimate of dominate eigenvalue lambda = " << lambda << std::endl;
00119   }
00120   
00121   return success;
00122 
00123 } // end runPowerMethodExample()
00124 
00125 
00126 //
00127 // Main driver program for serial implementation of the power method.
00128 //
00129 // Note that several different scalar types are used here (float,
00130 // double, std::complex<float>, std::complex<double> etc.).
00131 //
00132 int main(int argc, char *argv[])
00133 {
00134 
00135   using Teuchos::as;
00136   using Teuchos::CommandLineProcessor;
00137  
00138   bool success = true;
00139   bool result;
00140 
00141   Teuchos::RCP<Teuchos::FancyOStream>
00142     out = Teuchos::VerboseObjectBase::getDefaultOStream();
00143 
00144   try {
00145 
00146     //
00147     // Read in command-line options
00148     //
00149     
00150     CommandLineProcessor  clp;
00151     clp.throwExceptions(false);
00152     clp.addOutputSetupOptions(true);
00153 
00154     int dim = 4;
00155     clp.setOption( "dim", &dim, "Dimension of the linear system." );
00156 
00157     bool dumpAll = false;
00158     clp.setOption( "dump-all", "no-dump", &dumpAll,
00159       "Determines if quantities are dumped or not." );
00160 
00161     double tolerance = 1e-3;
00162     clp.setOption( "tol", &tolerance, "Final tolerance of eigen system." );
00163 
00164     double maxItersDimFactor = 10.0;
00165     clp.setOption( "max-iters-dim-factor", &maxItersDimFactor,
00166       "Factor to multiple dim by to get maxIters." );
00167 
00168     CommandLineProcessor::EParseCommandLineReturn
00169       parse_return = clp.parse(argc,argv);
00170     if (parse_return != CommandLineProcessor::PARSE_SUCCESSFUL)
00171       return parse_return;
00172 
00173     TEST_FOR_EXCEPTION( dim < 2, std::logic_error, "Error, dim=" << dim << " < 2 is not allowed!" );
00174 
00175     int maxNumIters = as<int>(maxItersDimFactor*dim);
00176     
00177 #if defined(HAVE_THYRA_FLOAT)
00178     // Run using float
00179     result = runPowerMethodExample<float>(
00180       dim, maxNumIters, tolerance, dumpAll);
00181     if(!result) success = false;
00182 #endif
00183 
00184     // Run using double
00185     result = runPowerMethodExample<double>(
00186       dim, maxNumIters, tolerance, dumpAll);
00187     if(!result) success = false;
00188 
00189 #ifdef HAVE_THYRA_COMPLEX
00190     
00191 #if defined(HAVE_THYRA_FLOAT)
00192     // Run using std::complex<float>
00193     result = runPowerMethodExample<std::complex<float> >(
00194       dim, maxNumIters, tolerance, dumpAll);
00195     if(!result) success = false;
00196 #endif
00197     
00198     // Run using std::complex<double>
00199     result = runPowerMethodExample<std::complex<double> >(
00200       dim, maxNumIters, tolerance, dumpAll);
00201     if(!result) success = false;
00202 
00203 #endif // HAVE_THYRA_COMPLEX
00204 
00205 #ifdef HAVE_TEUCHOS_GNU_MP
00206     
00207     // Run using mpf_class
00208     result = runPowerMethodExample<mpf_class >(
00209       dim, maxNumIters, tolerance, dumpAll);
00210     if(!result) success = false;
00211 
00212 #ifdef HAVE_THYRA_COMPLEX
00213     
00214     // Run using std::complex<mpf_class>
00215     //result = runPowerMethodExample<std::complex<mpf_class> >(
00216     //  dim, maxNumIters, tolerance, dumpAll);
00217     //if(!result) success = false;
00218     //The above commented-out code throws a floating-point exception?
00219  
00220 #endif // HAVE_THYRA_COMPLEX
00221 
00222 
00223 #endif // HAVE_TEUCHOS_GNU_MP
00224     
00225   }
00226   TEUCHOS_STANDARD_CATCH_STATEMENTS(true, *out, success)
00227   
00228   if (success)
00229     *out << "\nCongratulations! All of the tests checked out!\n";
00230   else
00231     *out << "\nOh no! At least one of the tests failed!\n";
00232   
00233   return success ? 0 : 1;
00234 
00235 } // end main()

Generated on Wed May 12 21:26:53 2010 for Thyra Operator/Vector Support by  doxygen 1.4.7