PlayaCompoundTester.hpp
Go to the documentation of this file.
00001 /* @HEADER@ */
00002 // ************************************************************************
00003 // 
00004 //                 Playa: Programmable Linear Algebra
00005 //                 Copyright 2012 Sandia Corporation
00006 // 
00007 // Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
00008 // the U.S. Government retains certain rights in this software.
00009 //
00010 // Redistribution and use in source and binary forms, with or without
00011 // modification, are permitted provided that the following conditions are
00012 // met:
00013 //
00014 // 1. Redistributions of source code must retain the above copyright
00015 // notice, this list of conditions and the following disclaimer.
00016 //
00017 // 2. Redistributions in binary form must reproduce the above copyright
00018 // notice, this list of conditions and the following disclaimer in the
00019 // documentation and/or other materials provided with the distribution.
00020 //
00021 // 3. Neither the name of the Corporation nor the names of the
00022 // contributors may be used to endorse or promote products derived from
00023 // this software without specific prior written permission.
00024 //
00025 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
00026 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00028 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
00029 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
00030 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
00031 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00032 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00033 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00034 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00035 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00036 //
00037 // Questions? Contact Kevin Long (kevin.long@ttu.edu)
00038 // 
00039 
00040 /* @HEADER@ */
00041 
00042 
00043 #ifndef PLAYA_COMPOUNDTESTER_HPP
00044 #define PLAYA_COMPOUNDTESTER_HPP
00045 
00046 #include "PlayaLinearOperatorDecl.hpp"
00047 #include "PlayaSimpleComposedOpDecl.hpp"
00048 #include "PlayaSimpleScaledOpDecl.hpp"
00049 #include "PlayaSimpleAddedOpDecl.hpp"
00050 #include "PlayaSimpleDiagonalOpDecl.hpp"
00051 #include "PlayaTesterBase.hpp"
00052 #include "Teuchos_ScalarTraits.hpp"
00053 #include "PlayaLinearCombinationImpl.hpp"
00054 
00055 #ifndef HAVE_TEUCHOS_EXPLICIT_INSTANTIATION
00056 #include "PlayaSimpleComposedOpImpl.hpp"
00057 #include "PlayaSimpleTransposedOpImpl.hpp"
00058 #include "PlayaSimpleScaledOpImpl.hpp"
00059 #include "PlayaSimpleAddedOpImpl.hpp"
00060 #include "PlayaSimpleDiagonalOpImpl.hpp"
00061 #include "PlayaRandomSparseMatrixBuilderImpl.hpp"
00062 #endif
00063 
00064 using namespace Playa;
00065 using namespace Teuchos;
00066 
00067 
00068 
00069 namespace Playa
00070 {
00071 
00072 /** */
00073 template <class Scalar>
00074 class CompoundTester : public TesterBase<Scalar>
00075 {
00076 public:
00077   /** \brief Local typedef for promoted scalar magnitude */
00078   typedef typename Teuchos::ScalarTraits<Scalar>::magnitudeType ScalarMag;
00079 
00080   /** */
00081   CompoundTester(const LinearOperator<Scalar>& A,
00082     const LinearOperator<Scalar>& B,
00083     const TestSpecifier<Scalar>& sumSpec,
00084     const TestSpecifier<Scalar>& composedSpec,
00085     const TestSpecifier<Scalar>& scaledSpec,
00086     const TestSpecifier<Scalar>& diagSpec);
00087 
00088   /** */
00089   bool runAllTests() const ;
00090 
00091   /** */
00092   bool sumTest() const ;
00093 
00094   /** */
00095   bool composedTest() const ;
00096 
00097   /** */
00098   bool scaledTest() const ;
00099 
00100   /** */
00101   bool diagTest() const ;
00102 
00103 
00104 private:
00105 
00106   LinearOperator<Scalar> A_;
00107 
00108   LinearOperator<Scalar> B_;
00109 
00110   TestSpecifier<Scalar> sumSpec_;
00111 
00112   TestSpecifier<Scalar> composedSpec_;
00113 
00114   TestSpecifier<Scalar> scaledSpec_;
00115 
00116   TestSpecifier<Scalar> diagSpec_;
00117 
00118 };
00119 
00120 template <class Scalar> 
00121 inline CompoundTester<Scalar>
00122 ::CompoundTester(const LinearOperator<Scalar>& A,
00123   const LinearOperator<Scalar>& B,
00124   const TestSpecifier<Scalar>& sumSpec,
00125   const TestSpecifier<Scalar>& composedSpec,
00126   const TestSpecifier<Scalar>& scaledSpec,
00127   const TestSpecifier<Scalar>& diagSpec)
00128   : TesterBase<Scalar>(), 
00129     A_(A),
00130     B_(B),
00131     sumSpec_(sumSpec),
00132     composedSpec_(composedSpec),
00133     scaledSpec_(scaledSpec),
00134     diagSpec_(diagSpec)
00135 {;}
00136 
00137 template <class Scalar> 
00138 inline bool CompoundTester<Scalar>
00139 ::runAllTests() const
00140 {
00141   bool pass = true;
00142 
00143   pass = sumTest() && pass;
00144   pass = composedTest() && pass;
00145   pass = scaledTest() && pass;
00146   pass = diagTest() && pass;
00147 
00148   return pass;
00149 }
00150 
00151 template <class Scalar> 
00152 inline bool CompoundTester<Scalar>
00153 ::sumTest() const 
00154 {
00155   if (sumSpec_.doTest())
00156   {
00157     Out::root() << "running operator addition test..." << std::endl;
00158     LinearOperator<Scalar> sum = A_ + B_;
00159 
00160     Vector<Scalar> x = A_.domain().createMember();
00161     this->randomizeVec(x);
00162     Out::root() << "computing y1 = (A+B)*x..." << std::endl;
00163     Vector<Scalar> y1 = sum*x;
00164     Out::root() << "computing y2 = A*x + B*x..." << std::endl;
00165     Vector<Scalar> y2 = A_*x + B_*x;
00166     
00167     ScalarMag err = (y1 - y2).norm2();
00168 
00169     Out::root() << "|y1-y2| = " << err << std::endl;
00170         
00171     return this->checkTest(sumSpec_, err, "operator addition");
00172   }
00173   Out::root() << "skipping operator addition test..." << std::endl;
00174   return true;
00175 }
00176 
00177 
00178 template <class Scalar> 
00179 inline bool CompoundTester<Scalar>
00180 ::composedTest() const 
00181 {
00182   if (composedSpec_.doTest())
00183   {
00184     Out::root() << "running operator composition test..." << std::endl;
00185     LinearOperator<Scalar> composed = A_ * B_;
00186 
00187     Vector<Scalar> x = B_.domain().createMember();
00188     this->randomizeVec(x);
00189     Out::root() << "computing y1 = (A*B)*x..." << std::endl;
00190     Vector<Scalar> y1 = composed*x;
00191     Out::root() << "computing y2 = B*x..." << std::endl;
00192     Vector<Scalar> y2 = B_*x;
00193     Out::root() << "computing y3 = A*y2..." << std::endl;
00194     Vector<Scalar> y3 = A_*y2;
00195 
00196     ScalarMag err = (y1 - y3).norm2();
00197 
00198     Out::root() << "|y1-y3| = " << err << std::endl;
00199     return this->checkTest(composedSpec_, err, "operator composition");
00200   }
00201   Out::root() << "skipping operator composition test..." << std::endl;
00202   return true;
00203 }
00204 
00205 
00206 template <class Scalar> 
00207 inline bool CompoundTester<Scalar>
00208 ::scaledTest() const 
00209 {
00210   if (scaledSpec_.doTest())
00211   {
00212     Out::root() << "running operator scaling test..." << std::endl;
00213     Scalar alpha = sqrt(2.0);
00214     LinearOperator<Scalar> scaled = alpha*A_;
00215 
00216     Vector<Scalar> x = A_.domain().createMember();
00217     this->randomizeVec(x);
00218     Out::root() << "computing y1 = (alpha*A)*x..." << std::endl;
00219     Vector<Scalar> y1 = scaled*x;
00220     Out::root() << "computing y2 = A*x..." << std::endl;
00221     Vector<Scalar> y2 = A_*x;
00222     Out::root() << "computing y3 = alpha*y2..." << std::endl;
00223     Vector<Scalar> y3 = alpha*y2;
00224 
00225     ScalarMag err = (y1 - y3).norm2();
00226 
00227     Out::root() << "|y1-y3| = " << err << std::endl;
00228     return this->checkTest(composedSpec_, err, "operator scaling");
00229   }
00230   Out::root() << "skipping operator scaling test..." << std::endl;
00231   return true;
00232 }
00233 
00234   
00235 
00236 template <class Scalar> 
00237 inline bool CompoundTester<Scalar>
00238 ::diagTest() const 
00239 {
00240   if (diagSpec_.doTest())
00241   {
00242     Out::root() << "running diagonal operator test..." << std::endl;
00243 
00244     Vector<Scalar> x = A_.domain().createMember();
00245     this->randomizeVec(x);
00246 
00247     Vector<Scalar> d = A_.domain().createMember();
00248     this->randomizeVec(d);
00249         
00250     LinearOperator<Scalar> D = diagonalOperator(d);
00251 
00252     Out::root() << "computing y1 = D*x..." << std::endl;
00253     Vector<Scalar> y1 = D*x;
00254     Out::root() << "computing y2 = d .* x..." << std::endl;
00255     Vector<Scalar> y2 = x.dotStar(d);
00256 
00257     ScalarMag err = (y1 - y2).norm2();
00258 
00259     Out::root() << "|y1-y2| = " << err << std::endl;
00260     return this->checkTest(diagSpec_, err, "diagonal operator");
00261   }
00262   Out::root() << "skipping diagonal operator test..." << std::endl;
00263   return true;
00264 }
00265 
00266   
00267   
00268 }
00269 #endif

Site Contact