00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
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
00039
00040
00041
00042
00043
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
00070
00071
00072
00073
00074
00075
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;
00082 for( k = 1; k < dim - 1; ++k ) {
00083 lower[k-1] = -one; diag[k] = two; upper[k] = -one;
00084 }
00085 lower[k-1] = -one; diag[k] = two;
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
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
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
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 }
00124
00125
00126
00127
00128
00129
00130
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
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
00179 result = runPowerMethodExample<float>(
00180 dim, maxNumIters, tolerance, dumpAll);
00181 if(!result) success = false;
00182 #endif
00183
00184
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
00193 result = runPowerMethodExample<std::complex<float> >(
00194 dim, maxNumIters, tolerance, dumpAll);
00195 if(!result) success = false;
00196 #endif
00197
00198
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
00208 result = runPowerMethodExample<mpf_class >(
00209 dim, maxNumIters, tolerance, dumpAll);
00210 if(!result) success = false;
00211
00212 #ifdef HAVE_THYRA_COMPLEX
00213
00214
00215
00216
00217
00218
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 }