PlayaCompoundTester.hpp

00001 /* @HEADER@ */
00002 //   
00003  /* @HEADER@ */
00004 
00005 
00006 #ifndef PLAYA_COMPOUNDTESTER_HPP
00007 #define PLAYA_COMPOUNDTESTER_HPP
00008 
00009 #include "PlayaLinearOperatorDecl.hpp"
00010 #include "PlayaSimpleComposedOpDecl.hpp"
00011 #include "PlayaSimpleScaledOpDecl.hpp"
00012 #include "PlayaSimpleAddedOpDecl.hpp"
00013 #include "PlayaSimpleDiagonalOpDecl.hpp"
00014 #include "PlayaTesterBase.hpp"
00015 #include "Teuchos_ScalarTraits.hpp"
00016 #include "PlayaLinearCombinationImpl.hpp"
00017 
00018 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00019 #include "PlayaSimpleComposedOpImpl.hpp"
00020 #include "PlayaSimpleTransposedOpImpl.hpp"
00021 #include "PlayaSimpleScaledOpImpl.hpp"
00022 #include "PlayaSimpleAddedOpImpl.hpp"
00023 #include "PlayaSimpleDiagonalOpImpl.hpp"
00024 #include "PlayaRandomSparseMatrixBuilderImpl.hpp"
00025 #endif
00026 
00027 using namespace Playa;
00028 using namespace Teuchos;
00029 
00030 
00031 
00032 namespace Playa
00033 {
00034 
00036 template <class Scalar>
00037 class CompoundTester : public TesterBase<Scalar>
00038 {
00039 public:
00041   typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType ScalarMag;
00042 
00044   CompoundTester(const LinearOperator<Scalar>& A,
00045     const LinearOperator<Scalar>& B,
00046     const TestSpecifier<Scalar>& sumSpec,
00047     const TestSpecifier<Scalar>& composedSpec,
00048     const TestSpecifier<Scalar>& scaledSpec,
00049     const TestSpecifier<Scalar>& diagSpec);
00050 
00052   bool runAllTests() const ;
00053 
00055   bool sumTest() const ;
00056 
00058   bool composedTest() const ;
00059 
00061   bool scaledTest() const ;
00062 
00064   bool diagTest() const ;
00065 
00066 
00067 private:
00068 
00069   LinearOperator<Scalar> A_;
00070 
00071   LinearOperator<Scalar> B_;
00072 
00073   TestSpecifier<Scalar> sumSpec_;
00074 
00075   TestSpecifier<Scalar> composedSpec_;
00076 
00077   TestSpecifier<Scalar> scaledSpec_;
00078 
00079   TestSpecifier<Scalar> diagSpec_;
00080 
00081 };
00082 
00083 template <class Scalar> 
00084 inline CompoundTester<Scalar>
00085 ::CompoundTester(const LinearOperator<Scalar>& A,
00086   const LinearOperator<Scalar>& B,
00087   const TestSpecifier<Scalar>& sumSpec,
00088   const TestSpecifier<Scalar>& composedSpec,
00089   const TestSpecifier<Scalar>& scaledSpec,
00090   const TestSpecifier<Scalar>& diagSpec)
00091   : TesterBase<Scalar>(), 
00092     A_(A),
00093     B_(B),
00094     sumSpec_(sumSpec),
00095     composedSpec_(composedSpec),
00096     scaledSpec_(scaledSpec),
00097     diagSpec_(diagSpec)
00098 {;}
00099 
00100 template <class Scalar> 
00101 inline bool CompoundTester<Scalar>
00102 ::runAllTests() const
00103 {
00104   bool pass = true;
00105 
00106   pass = sumTest() && pass;
00107   pass = composedTest() && pass;
00108   pass = scaledTest() && pass;
00109   pass = diagTest() && pass;
00110 
00111   return pass;
00112 }
00113 
00114 template <class Scalar> 
00115 inline bool CompoundTester<Scalar>
00116 ::sumTest() const 
00117 {
00118   if (sumSpec_.doTest())
00119   {
00120     Out::root() << "running operator addition test..." << std::endl;
00121     LinearOperator<Scalar> sum = A_ + B_;
00122 
00123     Vector<Scalar> x = A_.domain().createMember();
00124     this->randomizeVec(x);
00125     Out::root() << "computing y1 = (A+B)*x..." << std::endl;
00126     Vector<Scalar> y1 = sum*x;
00127     Out::root() << "computing y2 = A*x + B*x..." << std::endl;
00128     Vector<Scalar> y2 = A_*x + B_*x;
00129     
00130     ScalarMag err = (y1 - y2).norm2();
00131 
00132     Out::root() << "|y1-y2| = " << err << std::endl;
00133         
00134     return this->checkTest(sumSpec_, err, "operator addition");
00135   }
00136   Out::root() << "skipping operator addition test..." << std::endl;
00137   return true;
00138 }
00139 
00140 
00141 template <class Scalar> 
00142 inline bool CompoundTester<Scalar>
00143 ::composedTest() const 
00144 {
00145   if (composedSpec_.doTest())
00146   {
00147     Out::root() << "running operator composition test..." << std::endl;
00148     LinearOperator<Scalar> composed = A_ * B_;
00149 
00150     Vector<Scalar> x = B_.domain().createMember();
00151     this->randomizeVec(x);
00152     Out::root() << "computing y1 = (A*B)*x..." << std::endl;
00153     Vector<Scalar> y1 = composed*x;
00154     Out::root() << "computing y2 = B*x..." << std::endl;
00155     Vector<Scalar> y2 = B_*x;
00156     Out::root() << "computing y3 = A*y2..." << std::endl;
00157     Vector<Scalar> y3 = A_*y2;
00158 
00159     ScalarMag err = (y1 - y3).norm2();
00160 
00161     Out::root() << "|y1-y3| = " << err << std::endl;
00162     return this->checkTest(composedSpec_, err, "operator composition");
00163   }
00164   Out::root() << "skipping operator composition test..." << std::endl;
00165   return true;
00166 }
00167 
00168 
00169 template <class Scalar> 
00170 inline bool CompoundTester<Scalar>
00171 ::scaledTest() const 
00172 {
00173   if (scaledSpec_.doTest())
00174   {
00175     Out::root() << "running operator scaling test..." << std::endl;
00176     Scalar alpha = sqrt(2.0);
00177     LinearOperator<Scalar> scaled = alpha*A_;
00178 
00179     Vector<Scalar> x = A_.domain().createMember();
00180     this->randomizeVec(x);
00181     Out::root() << "computing y1 = (alpha*A)*x..." << std::endl;
00182     Vector<Scalar> y1 = scaled*x;
00183     Out::root() << "computing y2 = A*x..." << std::endl;
00184     Vector<Scalar> y2 = A_*x;
00185     Out::root() << "computing y3 = alpha*y2..." << std::endl;
00186     Vector<Scalar> y3 = alpha*y2;
00187 
00188     ScalarMag err = (y1 - y3).norm2();
00189 
00190     Out::root() << "|y1-y3| = " << err << std::endl;
00191     return this->checkTest(composedSpec_, err, "operator scaling");
00192   }
00193   Out::root() << "skipping operator scaling test..." << std::endl;
00194   return true;
00195 }
00196 
00197   
00198 
00199 template <class Scalar> 
00200 inline bool CompoundTester<Scalar>
00201 ::diagTest() const 
00202 {
00203   if (diagSpec_.doTest())
00204   {
00205     Out::root() << "running diagonal operator test..." << std::endl;
00206 
00207     Vector<Scalar> x = A_.domain().createMember();
00208     this->randomizeVec(x);
00209 
00210     Vector<Scalar> d = A_.domain().createMember();
00211     this->randomizeVec(d);
00212         
00213     LinearOperator<Scalar> D = diagonalOperator(d);
00214 
00215     Out::root() << "computing y1 = D*x..." << std::endl;
00216     Vector<Scalar> y1 = D*x;
00217     Out::root() << "computing y2 = d .* x..." << std::endl;
00218     Vector<Scalar> y2 = x.dotStar(d);
00219 
00220     ScalarMag err = (y1 - y2).norm2();
00221 
00222     Out::root() << "|y1-y2| = " << err << std::endl;
00223     return this->checkTest(diagSpec_, err, "diagonal operator");
00224   }
00225   Out::root() << "skipping diagonal operator test..." << std::endl;
00226   return true;
00227 }
00228 
00229   
00230   
00231 }
00232 #endif

doxygen